Commit aa7de3ff authored by Damien George's avatar Damien George
Browse files

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.
parent 6cc1a7a2
......@@ -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)
......
......@@ -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)
......
......@@ -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
......
......@@ -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)
......
......@@ -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)
......
......@@ -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)
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment