Skip to content

Commit 49dcc25

Browse files
committed
stmhal/i2c: Expose the pyb_i2c_obj_t struct and some relevant functions.
So they can be used by other parts of the code.
1 parent 652ca20 commit 49dcc25

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

stmhal/i2c.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,6 @@
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)
108100
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
109101
#endif
@@ -119,7 +111,7 @@ I2C_HandleTypeDef I2CHandle4 = {.Instance = NULL};
119111

120112
STATIC 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+
330342
STATIC 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) {

stmhal/i2c.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "dma.h"
28+
2729
// use this for OwnAddress1 to configure I2C in master mode
2830
#define PYB_I2C_MASTER_ADDRESS (0xfe)
2931

32+
typedef struct _pyb_i2c_obj_t {
33+
mp_obj_base_t base;
34+
I2C_HandleTypeDef *i2c;
35+
const dma_descr_t *tx_dma_descr;
36+
const dma_descr_t *rx_dma_descr;
37+
bool *use_dma;
38+
} pyb_i2c_obj_t;
39+
3040
extern I2C_HandleTypeDef I2CHandle1;
3141
extern I2C_HandleTypeDef I2CHandle2;
3242
extern I2C_HandleTypeDef I2CHandle3;
3343
extern const mp_obj_type_t pyb_i2c_type;
44+
extern const pyb_i2c_obj_t pyb_i2c_obj[4];
3445

3546
void i2c_init0(void);
3647
void i2c_init(I2C_HandleTypeDef *i2c);
48+
void i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq);
49+
uint32_t i2c_get_baudrate(I2C_InitTypeDef *init);
3750
void i2c_ev_irq_handler(mp_uint_t i2c_id);
3851
void i2c_er_irq_handler(mp_uint_t i2c_id);

0 commit comments

Comments
 (0)