Skip to content

Commit 57e4158

Browse files
committed
stmhal: Tidy up and improve consistency across modules.
1 parent d689430 commit 57e4158

5 files changed

Lines changed: 77 additions & 66 deletions

File tree

stmhal/accel.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <string.h>
33

4-
#include <stm32f4xx_hal.h>
4+
#include "stm32f4xx_hal.h"
55

66
#include "nlr.h"
77
#include "misc.h"
@@ -34,7 +34,7 @@ void accel_init(void) {
3434

3535
STATIC void accel_start(void) {
3636
// start the I2C bus
37-
i2c_start(&I2cHandle_X);
37+
i2c_init(&I2CHandle1);
3838

3939
// turn off AVDD, wait 20ms, turn on AVDD, wait 20ms again
4040
GPIOB->BSRRH = GPIO_PIN_5; // turn off
@@ -46,7 +46,7 @@ STATIC void accel_start(void) {
4646

4747
//printf("IsDeviceReady\n");
4848
for (int i = 0; i < 10; i++) {
49-
status = HAL_I2C_IsDeviceReady(&I2cHandle_X, MMA_ADDR, 10, 200);
49+
status = HAL_I2C_IsDeviceReady(&I2CHandle1, MMA_ADDR, 10, 200);
5050
//printf(" got %d\n", status);
5151
if (status == HAL_OK) {
5252
break;
@@ -56,7 +56,7 @@ STATIC void accel_start(void) {
5656
//printf("MemWrite\n");
5757
uint8_t data[1];
5858
data[0] = 1; // active mode
59-
status = HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
59+
status = HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
6060
//printf(" got %d\n", status);
6161
}
6262

@@ -86,7 +86,7 @@ STATIC mp_obj_t pyb_accel_make_new(mp_obj_t type_in, uint n_args, uint n_kw, con
8686

8787
STATIC mp_obj_t read_axis(int axis) {
8888
uint8_t data[1];
89-
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
89+
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
9090
return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0]));
9191
}
9292

@@ -110,7 +110,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z);
110110

111111
STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
112112
uint8_t data[1];
113-
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
113+
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
114114
return mp_obj_new_int(data[0]);
115115
}
116116

@@ -122,7 +122,7 @@ STATIC mp_obj_t pyb_accel_filtered_xyz(mp_obj_t self_in) {
122122
memmove(self->buf, self->buf + NUM_AXIS, NUM_AXIS * (FILT_DEPTH - 1) * sizeof(int16_t));
123123

124124
uint8_t data[NUM_AXIS];
125-
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
125+
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
126126

127127
mp_obj_t tuple[NUM_AXIS];
128128
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_
141141

142142
STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) {
143143
uint8_t data[1];
144-
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
144+
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
145145
return mp_obj_new_int(data[0]);
146146
}
147147

@@ -150,7 +150,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_obj, pyb_accel_read);
150150
STATIC mp_obj_t pyb_accel_write(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) {
151151
uint8_t data[1];
152152
data[0] = mp_obj_get_int(val);
153-
HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
153+
HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
154154
return mp_const_none;
155155
}
156156

stmhal/i2c.c

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,79 @@
11
#include <stdio.h>
22
#include <string.h>
33

4-
#include <stm32f4xx_hal.h>
4+
#include "stm32f4xx_hal.h"
55

66
#include "nlr.h"
77
#include "misc.h"
88
#include "mpconfig.h"
99
#include "qstr.h"
1010
#include "obj.h"
1111
#include "runtime.h"
12+
#include "pin.h"
13+
#include "genhdr/pins.h"
1214
#include "i2c.h"
1315

14-
I2C_HandleTypeDef I2cHandle_X;
15-
I2C_HandleTypeDef I2cHandle_Y;
16+
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
17+
I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
1618

17-
void i2c_init(void) {
18-
// init the I2C1 device
19-
memset(&I2cHandle_X, 0, sizeof(I2C_HandleTypeDef));
20-
I2cHandle_X.Instance = I2C1;
21-
22-
// init the I2C2 device
23-
memset(&I2cHandle_Y, 0, sizeof(I2C_HandleTypeDef));
24-
I2cHandle_Y.Instance = I2C2;
19+
void i2c_init0(void) {
20+
// reset the I2C1 handles
21+
memset(&I2CHandle1, 0, sizeof(I2C_HandleTypeDef));
22+
I2CHandle1.Instance = I2C1;
23+
memset(&I2CHandle2, 0, sizeof(I2C_HandleTypeDef));
24+
I2CHandle2.Instance = I2C2;
2525
}
2626

27-
void i2c_start(I2C_HandleTypeDef *i2c_handle) {
28-
GPIO_InitTypeDef GPIO_InitStructure;
29-
27+
void i2c_init(I2C_HandleTypeDef *i2c) {
3028
// init the GPIO lines
29+
GPIO_InitTypeDef GPIO_InitStructure;
3130
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
3231
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
3332
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
3433

35-
if (i2c_handle == &I2cHandle_X) {
34+
const pin_obj_t *pins[2];
35+
if (i2c == &I2CHandle1) {
3636
// X-skin: X9=PB6=SCL, X10=PB7=SDA
37-
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
37+
pins[0] = &pin_B6;
38+
pins[1] = &pin_B7;
3839
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
39-
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
4040
// enable the I2C clock
4141
__I2C1_CLK_ENABLE();
4242
} else {
4343
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
44-
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
44+
pins[0] = &pin_B10;
45+
pins[1] = &pin_B11;
4546
GPIO_InitStructure.Alternate = GPIO_AF4_I2C2;
46-
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
4747
// enable the I2C clock
4848
__I2C2_CLK_ENABLE();
4949
}
5050

51+
// init the GPIO lines
52+
for (uint i = 0; i < 2; i++) {
53+
GPIO_InitStructure.Pin = pins[i]->pin_mask;
54+
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
55+
}
56+
57+
// enable the I2C clock
58+
if (i2c == &I2CHandle1) {
59+
__I2C1_CLK_ENABLE();
60+
} else {
61+
__I2C2_CLK_ENABLE();
62+
}
63+
5164
// init the I2C device
52-
i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
53-
i2c_handle->Init.ClockSpeed = 400000;
54-
i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
55-
i2c_handle->Init.DutyCycle = I2C_DUTYCYCLE_16_9;
56-
i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
57-
i2c_handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
58-
i2c_handle->Init.OwnAddress1 = 0xfe; // unused
59-
i2c_handle->Init.OwnAddress2 = 0xfe; // unused
60-
61-
if (HAL_I2C_Init(i2c_handle) != HAL_OK) {
65+
i2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
66+
i2c->Init.ClockSpeed = 400000;
67+
i2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
68+
i2c->Init.DutyCycle = I2C_DUTYCYCLE_16_9;
69+
i2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
70+
i2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
71+
i2c->Init.OwnAddress1 = 0xfe; // unused
72+
i2c->Init.OwnAddress2 = 0xfe; // unused
73+
74+
if (HAL_I2C_Init(i2c) != HAL_OK) {
6275
// init error
63-
printf("accel_init: HAL_I2C_Init failed\n");
76+
printf("HardwareError: HAL_I2C_Init failed\n");
6477
return;
6578
}
6679
}
@@ -72,10 +85,10 @@ void i2c_start(I2C_HandleTypeDef *i2c_handle) {
7285

7386
typedef struct _pyb_i2c_obj_t {
7487
mp_obj_base_t base;
75-
I2C_HandleTypeDef *i2c_handle;
88+
I2C_HandleTypeDef *i2c;
7689
} pyb_i2c_obj_t;
7790

78-
STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2cHandle_X}, {{&pyb_i2c_type}, &I2cHandle_Y}};
91+
STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2CHandle1}, {{&pyb_i2c_type}, &I2CHandle2}};
7992

8093
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
8194
// 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
93106
const pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id];
94107

95108
// start the peripheral
96-
i2c_start(i2c_obj->i2c_handle);
109+
i2c_init(i2c_obj->i2c);
97110

98111
return (mp_obj_t)i2c_obj;
99112
}
@@ -104,7 +117,7 @@ STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
104117
machine_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o) << 1;
105118

106119
for (int i = 0; i < 10; i++) {
107-
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c_handle, i2c_addr, 10, 200);
120+
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, i2c_addr, 10, 200);
108121
if (status == HAL_OK) {
109122
return mp_const_true;
110123
}
@@ -123,7 +136,7 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
123136

124137
for (uint addr = 1; addr <= 127; addr++) {
125138
for (int i = 0; i < 10; i++) {
126-
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c_handle, addr << 1, 10, 200);
139+
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 10, 200);
127140
if (status == HAL_OK) {
128141
mp_obj_list_append(list, mp_obj_new_int(addr));
129142
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_
143156

144157
byte *data;
145158
mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data);
146-
HAL_StatusTypeDef status = HAL_I2C_Master_Receive(self->i2c_handle, i2c_addr, data, n, 500);
159+
HAL_StatusTypeDef status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, data, n, 500);
147160

148161
if (status != HAL_OK) {
149162
// 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
161174
HAL_StatusTypeDef status;
162175
if (MP_OBJ_IS_INT(data_in)) {
163176
uint8_t data[1] = {mp_obj_get_int(data_in)};
164-
status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, data, 1, 500);
177+
status = HAL_I2C_Master_Transmit(self->i2c, i2c_addr, data, 1, 500);
165178
} else {
166179
mp_buffer_info_t bufinfo;
167180
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
168-
status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, bufinfo.buf, bufinfo.len, 500);
181+
status = HAL_I2C_Master_Transmit(self->i2c, i2c_addr, bufinfo.buf, bufinfo.len, 500);
169182
}
170183

171184
if (status != HAL_OK) {
@@ -186,7 +199,7 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args) {
186199

187200
byte *data;
188201
mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data);
189-
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, n, 200);
202+
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, n, 200);
190203

191204
//printf("Read got %d\n", status);
192205

@@ -207,11 +220,11 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) {
207220
HAL_StatusTypeDef status;
208221
if (MP_OBJ_IS_INT(args[3])) {
209222
uint8_t data[1] = {mp_obj_get_int(args[3])};
210-
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
223+
status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
211224
} else {
212225
mp_buffer_info_t bufinfo;
213226
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
214-
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
227+
status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
215228
}
216229

217230
//printf("Write got %d\n", status);

stmhal/i2c.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
extern I2C_HandleTypeDef I2cHandle_X;
2-
extern I2C_HandleTypeDef I2cHandle_Y;
1+
extern I2C_HandleTypeDef I2CHandle1;
2+
extern I2C_HandleTypeDef I2CHandle2;
33
extern const mp_obj_type_t pyb_i2c_type;
44

5-
void i2c_init(void);
6-
void i2c_start(I2C_HandleTypeDef *i2c_handle);
5+
void i2c_init0(void);
6+
void i2c_init(I2C_HandleTypeDef *i2c);

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ int main(void) {
467467
//timer_init();
468468
#endif
469469

470-
i2c_init();
470+
i2c_init0();
471471
spi_init0();
472472

473473
#if MICROPY_HW_HAS_MMA7660

stmhal/spi.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,43 @@ void spi_init0(void) {
3333

3434
// TODO allow to take a list of pins to use
3535
void spi_init(SPI_HandleTypeDef *spi) {
36-
// auto-detect the GPIO pins to use
36+
// init the GPIO lines
37+
GPIO_InitTypeDef GPIO_InitStructure;
38+
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
39+
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
40+
GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP
41+
3742
const pin_obj_t *pins[4];
38-
uint32_t af_type;
3943
if (spi->Instance == SPI1) {
4044
// X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
4145
pins[0] = &pin_A4;
4246
pins[1] = &pin_A5;
4347
pins[2] = &pin_A6;
4448
pins[3] = &pin_A7;
45-
af_type = GPIO_AF5_SPI1;
49+
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
4650
} else if (spi->Instance == SPI2) {
4751
// Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
4852
pins[0] = &pin_B12;
4953
pins[1] = &pin_B13;
5054
pins[2] = &pin_B14;
5155
pins[3] = &pin_B15;
52-
af_type = GPIO_AF5_SPI2;
56+
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
5357
#if MICROPY_HW_ENABLE_SPI3
5458
} else if (spi->Instance == SPI3) {
5559
pins[0] = &pin_A4;
5660
pins[1] = &pin_B3;
5761
pins[2] = &pin_B4;
5862
pins[3] = &pin_B5;
59-
af_type = GPIO_AF6_SPI3;
63+
GPIO_InitStructure.Alternate = GPIO_AF6_SPI3;
6064
#endif
6165
} else {
6266
// SPI does not exist for this board
6367
printf("HardwareError: invalid SPI\n");
6468
return;
6569
}
6670

67-
// init the GPIO lines
68-
GPIO_InitTypeDef GPIO_InitStructure;
69-
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
70-
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
71-
GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP
7271
for (uint i = 0; i < 4; i++) {
7372
GPIO_InitStructure.Pin = pins[i]->pin_mask;
74-
GPIO_InitStructure.Alternate = af_type;
7573
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
7674
}
7775

0 commit comments

Comments
 (0)