From bd925b59c3177542fa69c3c95f9f1a40ced18039 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 Oct 2016 13:51:30 +1100 Subject: [PATCH] stmhal/spi: Enable use of fast software SPI. --- stmhal/mpconfigport.h | 1 + stmhal/spi.c | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 98d1e17e3..aff13f329 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -94,6 +94,7 @@ #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SPI (1) +#define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0) #define MICROPY_PY_FRAMEBUF (1) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) diff --git a/stmhal/spi.c b/stmhal/spi.c index 8ec6f8627..7f805709d 100644 --- a/stmhal/spi.c +++ b/stmhal/spi.c @@ -944,17 +944,28 @@ STATIC MP_DEFINE_CONST_DICT(machine_spi_locals_dict, machine_spi_locals_dict_tab /* code for soft implementation ***********************************************/ +// for the software SPI implementation, this is roughly the max baudrate +#define MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48) + STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) { - return 500000 / delay_half; + if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) { + return MAX_BAUDRATE; + } else { + return 500000 / delay_half; + } } STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) { - uint32_t delay_half = 500000 / baudrate; - // round delay_half up so that: actual_baudrate <= requested_baudrate - if (500000 % baudrate != 0) { - delay_half += 1; + if (baudrate >= MAX_BAUDRATE) { + return MICROPY_PY_MACHINE_SPI_MIN_DELAY; + } else { + uint32_t delay_half = 500000 / baudrate; + // round delay_half up so that: actual_baudrate <= requested_baudrate + if (500000 % baudrate != 0) { + delay_half += 1; + } + return delay_half; } - return delay_half; } STATIC void machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {