Skip to content

Commit bd925b5

Browse files
committed
stmhal/spi: Enable use of fast software SPI.
1 parent b0eb0d6 commit bd925b5

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#define MICROPY_PY_MACHINE (1)
9595
#define MICROPY_PY_MACHINE_I2C (1)
9696
#define MICROPY_PY_MACHINE_SPI (1)
97+
#define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0)
9798
#define MICROPY_PY_FRAMEBUF (1)
9899

99100
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)

stmhal/spi.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -944,17 +944,28 @@ STATIC MP_DEFINE_CONST_DICT(machine_spi_locals_dict, machine_spi_locals_dict_tab
944944

945945
/* code for soft implementation ***********************************************/
946946

947+
// for the software SPI implementation, this is roughly the max baudrate
948+
#define MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48)
949+
947950
STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
948-
return 500000 / delay_half;
951+
if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
952+
return MAX_BAUDRATE;
953+
} else {
954+
return 500000 / delay_half;
955+
}
949956
}
950957

951958
STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
952-
uint32_t delay_half = 500000 / baudrate;
953-
// round delay_half up so that: actual_baudrate <= requested_baudrate
954-
if (500000 % baudrate != 0) {
955-
delay_half += 1;
959+
if (baudrate >= MAX_BAUDRATE) {
960+
return MICROPY_PY_MACHINE_SPI_MIN_DELAY;
961+
} else {
962+
uint32_t delay_half = 500000 / baudrate;
963+
// round delay_half up so that: actual_baudrate <= requested_baudrate
964+
if (500000 % baudrate != 0) {
965+
delay_half += 1;
966+
}
967+
return delay_half;
956968
}
957-
return delay_half;
958969
}
959970

960971
STATIC void machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {

0 commit comments

Comments
 (0)