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
7386typedef 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
8093STATIC 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);
0 commit comments