9696#define PYB_I2C_MASTER (0)
9797#define PYB_I2C_SLAVE (1)
9898
99- typedef struct _pyb_i2c_obj_t {
100- mp_obj_base_t base ;
101- I2C_HandleTypeDef * i2c ;
102- const dma_descr_t * tx_dma_descr ;
103- const dma_descr_t * rx_dma_descr ;
104- bool * use_dma ;
105- } pyb_i2c_obj_t ;
106-
10799#if defined(MICROPY_HW_I2C1_SCL )
108100I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL };
109101#endif
@@ -119,7 +111,7 @@ I2C_HandleTypeDef I2CHandle4 = {.Instance = NULL};
119111
120112STATIC bool pyb_i2c_use_dma [4 ];
121113
122- STATIC const pyb_i2c_obj_t pyb_i2c_obj [] = {
114+ const pyb_i2c_obj_t pyb_i2c_obj [] = {
123115 #if defined(MICROPY_HW_I2C1_SCL )
124116 {{& pyb_i2c_type }, & I2CHandle1 , & dma_I2C_1_TX , & dma_I2C_1_RX , & pyb_i2c_use_dma [0 ]},
125117 #else
@@ -164,7 +156,7 @@ STATIC void i2c_set_baudrate(I2C_InitTypeDef *init, uint32_t baudrate) {
164156 "Unsupported I2C baudrate: %lu" , baudrate ));
165157}
166158
167- STATIC uint32_t i2c_get_baudrate (I2C_InitTypeDef * init ) {
159+ uint32_t i2c_get_baudrate (I2C_InitTypeDef * init ) {
168160 for (int i = 0 ; i < NUM_BAUDRATE_TIMINGS ; i ++ ) {
169161 if (pyb_i2c_baudrate_timing [i ].timing == init -> Timing ) {
170162 return pyb_i2c_baudrate_timing [i ].baudrate ;
@@ -180,7 +172,7 @@ STATIC void i2c_set_baudrate(I2C_InitTypeDef *init, uint32_t baudrate) {
180172 init -> DutyCycle = I2C_DUTYCYCLE_16_9 ;
181173}
182174
183- STATIC uint32_t i2c_get_baudrate (I2C_InitTypeDef * init ) {
175+ uint32_t i2c_get_baudrate (I2C_InitTypeDef * init ) {
184176 return init -> ClockSpeed ;
185177}
186178
@@ -327,6 +319,26 @@ void i2c_deinit(I2C_HandleTypeDef *i2c) {
327319 }
328320}
329321
322+ void i2c_init_freq (const pyb_i2c_obj_t * self , mp_int_t freq ) {
323+ I2C_InitTypeDef * init = & self -> i2c -> Init ;
324+
325+ init -> AddressingMode = I2C_ADDRESSINGMODE_7BIT ;
326+ init -> DualAddressMode = I2C_DUALADDRESS_DISABLED ;
327+ init -> GeneralCallMode = I2C_GENERALCALL_DISABLED ;
328+ init -> NoStretchMode = I2C_NOSTRETCH_DISABLE ;
329+ init -> OwnAddress1 = PYB_I2C_MASTER_ADDRESS ;
330+ init -> OwnAddress2 = 0 ; // unused
331+ if (freq != -1 ) {
332+ i2c_set_baudrate (init , MIN (freq , MICROPY_HW_I2C_BAUDRATE_MAX ));
333+ }
334+
335+ * self -> use_dma = false;
336+
337+ // init the I2C bus
338+ i2c_deinit (self -> i2c );
339+ i2c_init (self -> i2c );
340+ }
341+
330342STATIC void i2c_reset_after_error (I2C_HandleTypeDef * i2c ) {
331343 // wait for bus-busy flag to be cleared, with a timeout
332344 for (int timeout = 50 ; timeout > 0 ; -- timeout ) {
0 commit comments