Skip to content

Commit aa7de3f

Browse files
committed
stmhal: Move L4/F7 I2C timing constants from mpconfigboard.h to i2c.c.
Such constants are MCU specific so shouldn't be specified in the board config file (else it leads to too much duplication of code). This patch also adds I2C timing values for the F767/F769 for 100k, 400k and 1MHz I2C bus frequencies.
1 parent 6cc1a7a commit aa7de3f

7 files changed

Lines changed: 37 additions & 48 deletions

File tree

stmhal/boards/LIMIFROG/mpconfigboard.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ void LIMIFROG_board_early_init(void);
3737
#define MICROPY_HW_I2C1_SDA (pin_B9)
3838
#define MICROPY_HW_I2C2_SCL (pin_B10)
3939
#define MICROPY_HW_I2C2_SDA (pin_B11)
40-
// We use an array of baudrates and corresponding TIMINGR values.
41-
//
42-
// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant
43-
// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h
44-
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}}
45-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
46-
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
4740

4841
// SPI busses
4942
#define MICROPY_HW_SPI1_NSS (pin_A4)

stmhal/boards/NUCLEO_F767ZI/mpconfigboard.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
#define MICROPY_HW_I2C3_SCL (pin_H7)
5353
#define MICROPY_HW_I2C3_SDA (pin_H8)
5454

55-
// TODO These should go in i2c.c
56-
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}}
57-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
58-
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
59-
6055
// SPI
6156
#define MICROPY_HW_SPI3_NSS (pin_A4)
6257
#define MICROPY_HW_SPI3_SCK (pin_B3)

stmhal/boards/NUCLEO_L476RG/mpconfigboard.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
#define MICROPY_HW_UART_REPL PYB_UART_2
2222
#define MICROPY_HW_UART_REPL_BAUD 115200
2323

24-
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}}
25-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
26-
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
27-
2824
#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4
2925

3026
// I2C busses

stmhal/boards/STM32F769DISC/mpconfigboard.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
#define MICROPY_HW_I2C3_SCL (pin_H7)
4444
#define MICROPY_HW_I2C3_SDA (pin_H8)
4545

46-
// TODO These should go in i2c.c
47-
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}}
48-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
49-
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
50-
5146
// SPI
5247
#define MICROPY_HW_SPI2_NSS (pin_A11)
5348
#define MICROPY_HW_SPI2_SCK (pin_A12)

stmhal/boards/STM32F7DISC/mpconfigboard.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ void STM32F7DISC_board_early_init(void);
5151
#define MICROPY_HW_I2C3_SCL (pin_H7)
5252
#define MICROPY_HW_I2C3_SDA (pin_H8)
5353

54-
// The STM32F7 uses a TIMINGR register which is configured using an Excel
55-
// Spreadsheet from AN4235: http://www.st.com/web/en/catalog/tools/PF258335
56-
// We use an array of baudrates and corresponding TIMINGR values.
57-
//
58-
// The value 0x40912732 was obtained from the DISCOVERY_I2Cx_TIMING constant
59-
// defined in the STM32F7Cube file Drivers/BSP/STM32F746G-Discovery/stm32f7456g_discovery.h
60-
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}}
61-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
62-
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
63-
6454
// SPI
6555
#define MICROPY_HW_SPI2_NSS (pin_I0)
6656
#define MICROPY_HW_SPI2_SCK (pin_I1)

stmhal/boards/STM32L476DISC/mpconfigboard.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ void STM32L476DISC_board_early_init(void);
4747
#define MICROPY_HW_I2C1_SDA (pin_B7)
4848
#define MICROPY_HW_I2C2_SCL (pin_B10)
4949
#define MICROPY_HW_I2C2_SDA (pin_B11)
50-
// We use an array of baudrates and corresponding TIMINGR values.
51-
//
52-
// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant
53-
// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h
54-
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}}
55-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
56-
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
5750

5851
// SPI busses
5952
#define MICROPY_HW_SPI2_NSS (pin_D0)

stmhal/i2c.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@
3737
#include "dma.h"
3838
#include "i2c.h"
3939

40-
#if !defined(MICROPY_HW_I2C_BAUDRATE_DEFAULT)
41-
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 400000
42-
#endif
43-
44-
#if !defined(MICROPY_HW_I2C_BAUDRATE_MAX)
45-
#define MICROPY_HW_I2C_BAUDRATE_MAX 400000
46-
#endif
47-
4840
/// \moduleref pyb
4941
/// \class I2C - a two-wire serial protocol
5042
///
@@ -134,10 +126,42 @@ const pyb_i2c_obj_t pyb_i2c_obj[] = {
134126
#endif
135127
};
136128

137-
#if defined(MICROPY_HW_I2C_BAUDRATE_TIMING)
129+
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
130+
138131
// The STM32F0, F3, F7 and L4 use a TIMINGR register rather than ClockSpeed and
139132
// DutyCycle.
140133

134+
#if defined(STM32F746xx)
135+
136+
// The value 0x40912732 was obtained from the DISCOVERY_I2Cx_TIMING constant
137+
// defined in the STM32F7Cube file Drivers/BSP/STM32F746G-Discovery/stm32f7456g_discovery.h
138+
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}}
139+
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (100000)
140+
#define MICROPY_HW_I2C_BAUDRATE_MAX (100000)
141+
142+
#elif defined(STM32F767xx) || defined(STM32F769xx)
143+
144+
// These timing values are for f_I2CCLK=54MHz and are only approximate
145+
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
146+
{100000, 0xb0420f13}, \
147+
{400000, 0x70330309}, \
148+
{1000000, 0x50100103}, \
149+
}
150+
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (400000)
151+
#define MICROPY_HW_I2C_BAUDRATE_MAX (1000000)
152+
153+
#elif defined(MCU_SERIES_L4)
154+
155+
// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant
156+
// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h
157+
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}}
158+
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (100000)
159+
#define MICROPY_HW_I2C_BAUDRATE_MAX (100000)
160+
161+
#else
162+
#error "no I2C timings for this MCU"
163+
#endif
164+
141165
STATIC const struct {
142166
uint32_t baudrate;
143167
uint32_t timing;
@@ -167,6 +191,9 @@ uint32_t i2c_get_baudrate(I2C_InitTypeDef *init) {
167191

168192
#else
169193

194+
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (400000)
195+
#define MICROPY_HW_I2C_BAUDRATE_MAX (400000)
196+
170197
STATIC void i2c_set_baudrate(I2C_InitTypeDef *init, uint32_t baudrate) {
171198
init->ClockSpeed = baudrate;
172199
init->DutyCycle = I2C_DUTYCYCLE_16_9;
@@ -176,7 +203,7 @@ uint32_t i2c_get_baudrate(I2C_InitTypeDef *init) {
176203
return init->ClockSpeed;
177204
}
178205

179-
#endif // MICROPY_HW_I2C_BAUDRATE_TIMING
206+
#endif
180207

181208
void i2c_init0(void) {
182209
// reset the I2C1 handles

0 commit comments

Comments
 (0)