From 57e415859a149a3354ad2efb01595d7616c9b564 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 20 Apr 2014 00:30:09 +0100 Subject: [PATCH] stmhal: Tidy up and improve consistency across modules. --- stmhal/accel.c | 18 +++++----- stmhal/i2c.c | 95 ++++++++++++++++++++++++++++---------------------- stmhal/i2c.h | 8 ++--- stmhal/main.c | 2 +- stmhal/spi.c | 20 +++++------ 5 files changed, 77 insertions(+), 66 deletions(-) diff --git a/stmhal/accel.c b/stmhal/accel.c index c09e64e426..339a93ea40 100644 --- a/stmhal/accel.c +++ b/stmhal/accel.c @@ -1,7 +1,7 @@ #include #include -#include +#include "stm32f4xx_hal.h" #include "nlr.h" #include "misc.h" @@ -34,7 +34,7 @@ void accel_init(void) { STATIC void accel_start(void) { // start the I2C bus - i2c_start(&I2cHandle_X); + i2c_init(&I2CHandle1); // turn off AVDD, wait 20ms, turn on AVDD, wait 20ms again GPIOB->BSRRH = GPIO_PIN_5; // turn off @@ -46,7 +46,7 @@ STATIC void accel_start(void) { //printf("IsDeviceReady\n"); for (int i = 0; i < 10; i++) { - status = HAL_I2C_IsDeviceReady(&I2cHandle_X, MMA_ADDR, 10, 200); + status = HAL_I2C_IsDeviceReady(&I2CHandle1, MMA_ADDR, 10, 200); //printf(" got %d\n", status); if (status == HAL_OK) { break; @@ -56,7 +56,7 @@ STATIC void accel_start(void) { //printf("MemWrite\n"); uint8_t data[1]; data[0] = 1; // active mode - status = HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200); + status = HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200); //printf(" got %d\n", status); } @@ -86,7 +86,7 @@ STATIC mp_obj_t pyb_accel_make_new(mp_obj_t type_in, uint n_args, uint n_kw, con STATIC mp_obj_t read_axis(int axis) { uint8_t data[1]; - HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200); + HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200); return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0])); } @@ -110,7 +110,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z); STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) { uint8_t data[1]; - HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200); + HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200); return mp_obj_new_int(data[0]); } @@ -122,7 +122,7 @@ STATIC mp_obj_t pyb_accel_filtered_xyz(mp_obj_t self_in) { memmove(self->buf, self->buf + NUM_AXIS, NUM_AXIS * (FILT_DEPTH - 1) * sizeof(int16_t)); uint8_t data[NUM_AXIS]; - HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200); + HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200); mp_obj_t tuple[NUM_AXIS]; for (int i = 0; i < NUM_AXIS; i++) { @@ -141,7 +141,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_filtered_xyz_obj, pyb_accel_filtered_ STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) { uint8_t data[1]; - HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200); + HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200); return mp_obj_new_int(data[0]); } @@ -150,7 +150,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_obj, pyb_accel_read); STATIC mp_obj_t pyb_accel_write(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) { uint8_t data[1]; data[0] = mp_obj_get_int(val); - HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200); + HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200); return mp_const_none; } diff --git a/stmhal/i2c.c b/stmhal/i2c.c index 6ed1eef105..8a8dd7dc2d 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -1,7 +1,7 @@ #include #include -#include +#include "stm32f4xx_hal.h" #include "nlr.h" #include "misc.h" @@ -9,58 +9,71 @@ #include "qstr.h" #include "obj.h" #include "runtime.h" +#include "pin.h" +#include "genhdr/pins.h" #include "i2c.h" -I2C_HandleTypeDef I2cHandle_X; -I2C_HandleTypeDef I2cHandle_Y; +I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL}; +I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL}; -void i2c_init(void) { - // init the I2C1 device - memset(&I2cHandle_X, 0, sizeof(I2C_HandleTypeDef)); - I2cHandle_X.Instance = I2C1; - - // init the I2C2 device - memset(&I2cHandle_Y, 0, sizeof(I2C_HandleTypeDef)); - I2cHandle_Y.Instance = I2C2; +void i2c_init0(void) { + // reset the I2C1 handles + memset(&I2CHandle1, 0, sizeof(I2C_HandleTypeDef)); + I2CHandle1.Instance = I2C1; + memset(&I2CHandle2, 0, sizeof(I2C_HandleTypeDef)); + I2CHandle2.Instance = I2C2; } -void i2c_start(I2C_HandleTypeDef *i2c_handle) { - GPIO_InitTypeDef GPIO_InitStructure; - +void i2c_init(I2C_HandleTypeDef *i2c) { // init the GPIO lines + GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines - if (i2c_handle == &I2cHandle_X) { + const pin_obj_t *pins[2]; + if (i2c == &I2CHandle1) { // X-skin: X9=PB6=SCL, X10=PB7=SDA - GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7; + pins[0] = &pin_B6; + pins[1] = &pin_B7; GPIO_InitStructure.Alternate = GPIO_AF4_I2C1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); // enable the I2C clock __I2C1_CLK_ENABLE(); } else { // Y-skin: Y9=PB10=SCL, Y10=PB11=SDA - GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11; + pins[0] = &pin_B10; + pins[1] = &pin_B11; GPIO_InitStructure.Alternate = GPIO_AF4_I2C2; - HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); // enable the I2C clock __I2C2_CLK_ENABLE(); } - // init the I2C device - i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - i2c_handle->Init.ClockSpeed = 400000; - i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; - i2c_handle->Init.DutyCycle = I2C_DUTYCYCLE_16_9; - i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; - i2c_handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; - i2c_handle->Init.OwnAddress1 = 0xfe; // unused - i2c_handle->Init.OwnAddress2 = 0xfe; // unused + // init the GPIO lines + for (uint i = 0; i < 2; i++) { + GPIO_InitStructure.Pin = pins[i]->pin_mask; + HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure); + } - if (HAL_I2C_Init(i2c_handle) != HAL_OK) { + // enable the I2C clock + if (i2c == &I2CHandle1) { + __I2C1_CLK_ENABLE(); + } else { + __I2C2_CLK_ENABLE(); + } + + // init the I2C device + i2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + i2c->Init.ClockSpeed = 400000; + i2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; + i2c->Init.DutyCycle = I2C_DUTYCYCLE_16_9; + i2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; + i2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; + i2c->Init.OwnAddress1 = 0xfe; // unused + i2c->Init.OwnAddress2 = 0xfe; // unused + + if (HAL_I2C_Init(i2c) != HAL_OK) { // init error - printf("accel_init: HAL_I2C_Init failed\n"); + printf("HardwareError: HAL_I2C_Init failed\n"); return; } } @@ -72,10 +85,10 @@ void i2c_start(I2C_HandleTypeDef *i2c_handle) { typedef struct _pyb_i2c_obj_t { mp_obj_base_t base; - I2C_HandleTypeDef *i2c_handle; + I2C_HandleTypeDef *i2c; } pyb_i2c_obj_t; -STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2cHandle_X}, {{&pyb_i2c_type}, &I2cHandle_Y}}; +STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2CHandle1}, {{&pyb_i2c_type}, &I2CHandle2}}; STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // check arguments @@ -93,7 +106,7 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const const pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id]; // start the peripheral - i2c_start(i2c_obj->i2c_handle); + i2c_init(i2c_obj->i2c); return (mp_obj_t)i2c_obj; } @@ -104,7 +117,7 @@ STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) { machine_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o) << 1; for (int i = 0; i < 10; i++) { - HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c_handle, i2c_addr, 10, 200); + HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, i2c_addr, 10, 200); if (status == HAL_OK) { return mp_const_true; } @@ -123,7 +136,7 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) { for (uint addr = 1; addr <= 127; addr++) { for (int i = 0; i < 10; i++) { - HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c_handle, addr << 1, 10, 200); + HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 10, 200); if (status == HAL_OK) { mp_obj_list_append(list, mp_obj_new_int(addr)); break; @@ -143,7 +156,7 @@ STATIC mp_obj_t pyb_i2c_read(mp_obj_t self_in, mp_obj_t i2c_addr_in, mp_obj_t n_ byte *data; mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data); - HAL_StatusTypeDef status = HAL_I2C_Master_Receive(self->i2c_handle, i2c_addr, data, n, 500); + HAL_StatusTypeDef status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, data, n, 500); if (status != HAL_OK) { // TODO really need a HardwareError object, or something @@ -161,11 +174,11 @@ STATIC mp_obj_t pyb_i2c_write(mp_obj_t self_in, mp_obj_t i2c_addr_in, mp_obj_t d HAL_StatusTypeDef status; if (MP_OBJ_IS_INT(data_in)) { uint8_t data[1] = {mp_obj_get_int(data_in)}; - status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, data, 1, 500); + status = HAL_I2C_Master_Transmit(self->i2c, i2c_addr, data, 1, 500); } else { mp_buffer_info_t bufinfo; mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); - status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, bufinfo.buf, bufinfo.len, 500); + status = HAL_I2C_Master_Transmit(self->i2c, i2c_addr, bufinfo.buf, bufinfo.len, 500); } if (status != HAL_OK) { @@ -186,7 +199,7 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args) { byte *data; mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data); - HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, n, 200); + HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, n, 200); //printf("Read got %d\n", status); @@ -207,11 +220,11 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) { HAL_StatusTypeDef status; if (MP_OBJ_IS_INT(args[3])) { uint8_t data[1] = {mp_obj_get_int(args[3])}; - status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200); + status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200); } else { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); - status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200); + status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200); } //printf("Write got %d\n", status); diff --git a/stmhal/i2c.h b/stmhal/i2c.h index 32cba42595..00bb744f0c 100644 --- a/stmhal/i2c.h +++ b/stmhal/i2c.h @@ -1,6 +1,6 @@ -extern I2C_HandleTypeDef I2cHandle_X; -extern I2C_HandleTypeDef I2cHandle_Y; +extern I2C_HandleTypeDef I2CHandle1; +extern I2C_HandleTypeDef I2CHandle2; extern const mp_obj_type_t pyb_i2c_type; -void i2c_init(void); -void i2c_start(I2C_HandleTypeDef *i2c_handle); +void i2c_init0(void); +void i2c_init(I2C_HandleTypeDef *i2c); diff --git a/stmhal/main.c b/stmhal/main.c index 1d02439db8..86cbe36011 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -467,7 +467,7 @@ soft_reset: //timer_init(); #endif - i2c_init(); + i2c_init0(); spi_init0(); #if MICROPY_HW_HAS_MMA7660 diff --git a/stmhal/spi.c b/stmhal/spi.c index 070edebb87..53785e6501 100644 --- a/stmhal/spi.c +++ b/stmhal/spi.c @@ -33,30 +33,34 @@ void spi_init0(void) { // TODO allow to take a list of pins to use void spi_init(SPI_HandleTypeDef *spi) { - // auto-detect the GPIO pins to use + // init the GPIO lines + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; + GPIO_InitStructure.Speed = GPIO_SPEED_FAST; + GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP + const pin_obj_t *pins[4]; - uint32_t af_type; if (spi->Instance == SPI1) { // X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI pins[0] = &pin_A4; pins[1] = &pin_A5; pins[2] = &pin_A6; pins[3] = &pin_A7; - af_type = GPIO_AF5_SPI1; + GPIO_InitStructure.Alternate = GPIO_AF5_SPI1; } else if (spi->Instance == SPI2) { // Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI pins[0] = &pin_B12; pins[1] = &pin_B13; pins[2] = &pin_B14; pins[3] = &pin_B15; - af_type = GPIO_AF5_SPI2; + GPIO_InitStructure.Alternate = GPIO_AF5_SPI2; #if MICROPY_HW_ENABLE_SPI3 } else if (spi->Instance == SPI3) { pins[0] = &pin_A4; pins[1] = &pin_B3; pins[2] = &pin_B4; pins[3] = &pin_B5; - af_type = GPIO_AF6_SPI3; + GPIO_InitStructure.Alternate = GPIO_AF6_SPI3; #endif } else { // SPI does not exist for this board @@ -64,14 +68,8 @@ void spi_init(SPI_HandleTypeDef *spi) { return; } - // init the GPIO lines - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; - GPIO_InitStructure.Speed = GPIO_SPEED_FAST; - GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP for (uint i = 0; i < 4; i++) { GPIO_InitStructure.Pin = pins[i]->pin_mask; - GPIO_InitStructure.Alternate = af_type; HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure); }