From aa7de3ff67199fac308184850aa41e598f3717a1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 31 Mar 2017 12:53:56 +1100 Subject: [PATCH] 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. --- stmhal/boards/LIMIFROG/mpconfigboard.h | 7 --- stmhal/boards/NUCLEO_F767ZI/mpconfigboard.h | 5 --- stmhal/boards/NUCLEO_L476RG/mpconfigboard.h | 4 -- stmhal/boards/STM32F769DISC/mpconfigboard.h | 5 --- stmhal/boards/STM32F7DISC/mpconfigboard.h | 10 ----- stmhal/boards/STM32L476DISC/mpconfigboard.h | 7 --- stmhal/i2c.c | 47 ++++++++++++++++----- 7 files changed, 37 insertions(+), 48 deletions(-) diff --git a/stmhal/boards/LIMIFROG/mpconfigboard.h b/stmhal/boards/LIMIFROG/mpconfigboard.h index 95fd6bf506..d04634ac38 100644 --- a/stmhal/boards/LIMIFROG/mpconfigboard.h +++ b/stmhal/boards/LIMIFROG/mpconfigboard.h @@ -37,13 +37,6 @@ void LIMIFROG_board_early_init(void); #define MICROPY_HW_I2C1_SDA (pin_B9) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// We use an array of baudrates and corresponding TIMINGR values. -// -// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant -// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h -#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}} -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000 -#define MICROPY_HW_I2C_BAUDRATE_MAX 100000 // SPI busses #define MICROPY_HW_SPI1_NSS (pin_A4) diff --git a/stmhal/boards/NUCLEO_F767ZI/mpconfigboard.h b/stmhal/boards/NUCLEO_F767ZI/mpconfigboard.h index 647525384f..cafea136f9 100644 --- a/stmhal/boards/NUCLEO_F767ZI/mpconfigboard.h +++ b/stmhal/boards/NUCLEO_F767ZI/mpconfigboard.h @@ -52,11 +52,6 @@ #define MICROPY_HW_I2C3_SCL (pin_H7) #define MICROPY_HW_I2C3_SDA (pin_H8) -// TODO These should go in i2c.c -#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}} -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000 -#define MICROPY_HW_I2C_BAUDRATE_MAX 100000 - // SPI #define MICROPY_HW_SPI3_NSS (pin_A4) #define MICROPY_HW_SPI3_SCK (pin_B3) diff --git a/stmhal/boards/NUCLEO_L476RG/mpconfigboard.h b/stmhal/boards/NUCLEO_L476RG/mpconfigboard.h index 181580b147..f2474619fe 100644 --- a/stmhal/boards/NUCLEO_L476RG/mpconfigboard.h +++ b/stmhal/boards/NUCLEO_L476RG/mpconfigboard.h @@ -21,10 +21,6 @@ #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 -#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}} -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000 -#define MICROPY_HW_I2C_BAUDRATE_MAX 100000 - #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 // I2C busses diff --git a/stmhal/boards/STM32F769DISC/mpconfigboard.h b/stmhal/boards/STM32F769DISC/mpconfigboard.h index 72465c7994..50b9c1618f 100644 --- a/stmhal/boards/STM32F769DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F769DISC/mpconfigboard.h @@ -43,11 +43,6 @@ #define MICROPY_HW_I2C3_SCL (pin_H7) #define MICROPY_HW_I2C3_SDA (pin_H8) -// TODO These should go in i2c.c -#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}} -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000 -#define MICROPY_HW_I2C_BAUDRATE_MAX 100000 - // SPI #define MICROPY_HW_SPI2_NSS (pin_A11) #define MICROPY_HW_SPI2_SCK (pin_A12) diff --git a/stmhal/boards/STM32F7DISC/mpconfigboard.h b/stmhal/boards/STM32F7DISC/mpconfigboard.h index 38665055f1..44a39c0a1a 100644 --- a/stmhal/boards/STM32F7DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F7DISC/mpconfigboard.h @@ -51,16 +51,6 @@ void STM32F7DISC_board_early_init(void); #define MICROPY_HW_I2C3_SCL (pin_H7) #define MICROPY_HW_I2C3_SDA (pin_H8) -// The STM32F7 uses a TIMINGR register which is configured using an Excel -// Spreadsheet from AN4235: http://www.st.com/web/en/catalog/tools/PF258335 -// We use an array of baudrates and corresponding TIMINGR values. -// -// The value 0x40912732 was obtained from the DISCOVERY_I2Cx_TIMING constant -// defined in the STM32F7Cube file Drivers/BSP/STM32F746G-Discovery/stm32f7456g_discovery.h -#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}} -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000 -#define MICROPY_HW_I2C_BAUDRATE_MAX 100000 - // SPI #define MICROPY_HW_SPI2_NSS (pin_I0) #define MICROPY_HW_SPI2_SCK (pin_I1) diff --git a/stmhal/boards/STM32L476DISC/mpconfigboard.h b/stmhal/boards/STM32L476DISC/mpconfigboard.h index a93b486c68..258a86b606 100644 --- a/stmhal/boards/STM32L476DISC/mpconfigboard.h +++ b/stmhal/boards/STM32L476DISC/mpconfigboard.h @@ -47,13 +47,6 @@ void STM32L476DISC_board_early_init(void); #define MICROPY_HW_I2C1_SDA (pin_B7) #define MICROPY_HW_I2C2_SCL (pin_B10) #define MICROPY_HW_I2C2_SDA (pin_B11) -// We use an array of baudrates and corresponding TIMINGR values. -// -// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant -// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h -#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}} -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000 -#define MICROPY_HW_I2C_BAUDRATE_MAX 100000 // SPI busses #define MICROPY_HW_SPI2_NSS (pin_D0) diff --git a/stmhal/i2c.c b/stmhal/i2c.c index 412a8e3638..e818ec380f 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -37,14 +37,6 @@ #include "dma.h" #include "i2c.h" -#if !defined(MICROPY_HW_I2C_BAUDRATE_DEFAULT) -#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 400000 -#endif - -#if !defined(MICROPY_HW_I2C_BAUDRATE_MAX) -#define MICROPY_HW_I2C_BAUDRATE_MAX 400000 -#endif - /// \moduleref pyb /// \class I2C - a two-wire serial protocol /// @@ -134,10 +126,42 @@ const pyb_i2c_obj_t pyb_i2c_obj[] = { #endif }; -#if defined(MICROPY_HW_I2C_BAUDRATE_TIMING) +#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4) + // The STM32F0, F3, F7 and L4 use a TIMINGR register rather than ClockSpeed and // DutyCycle. +#if defined(STM32F746xx) + +// The value 0x40912732 was obtained from the DISCOVERY_I2Cx_TIMING constant +// defined in the STM32F7Cube file Drivers/BSP/STM32F746G-Discovery/stm32f7456g_discovery.h +#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x40912732}} +#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (100000) +#define MICROPY_HW_I2C_BAUDRATE_MAX (100000) + +#elif defined(STM32F767xx) || defined(STM32F769xx) + +// These timing values are for f_I2CCLK=54MHz and are only approximate +#define MICROPY_HW_I2C_BAUDRATE_TIMING { \ + {100000, 0xb0420f13}, \ + {400000, 0x70330309}, \ + {1000000, 0x50100103}, \ + } +#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (400000) +#define MICROPY_HW_I2C_BAUDRATE_MAX (1000000) + +#elif defined(MCU_SERIES_L4) + +// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant +// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h +#define MICROPY_HW_I2C_BAUDRATE_TIMING {{100000, 0x90112626}} +#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (100000) +#define MICROPY_HW_I2C_BAUDRATE_MAX (100000) + +#else +#error "no I2C timings for this MCU" +#endif + STATIC const struct { uint32_t baudrate; uint32_t timing; @@ -167,6 +191,9 @@ uint32_t i2c_get_baudrate(I2C_InitTypeDef *init) { #else +#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (400000) +#define MICROPY_HW_I2C_BAUDRATE_MAX (400000) + STATIC void i2c_set_baudrate(I2C_InitTypeDef *init, uint32_t baudrate) { init->ClockSpeed = baudrate; init->DutyCycle = I2C_DUTYCYCLE_16_9; @@ -176,7 +203,7 @@ uint32_t i2c_get_baudrate(I2C_InitTypeDef *init) { return init->ClockSpeed; } -#endif // MICROPY_HW_I2C_BAUDRATE_TIMING +#endif void i2c_init0(void) { // reset the I2C1 handles