summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKwabena W. Agyeman <kwagyeman@live.com>2025-09-28 20:44:05 -0700
committerDamien George <damien@micropython.org>2025-10-01 09:31:10 +1000
commitea63e43faf34a8c60eeae69f139ae4b0602a728f (patch)
treee2893d44b95115084ceb4c67b1909f2bfe04006c
parenta80913292153a14424b29bdb9ca8847e8d35cf73 (diff)
alif/machine_spi: Fix init() to only change requested settings.
Signed-off-by: Kwabena W. Agyeman <kwagyeman@live.com>
-rw-r--r--ports/alif/machine_spi.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/ports/alif/machine_spi.c b/ports/alif/machine_spi.c
index b2c14745c..362ffa9b2 100644
--- a/ports/alif/machine_spi.c
+++ b/ports/alif/machine_spi.c
@@ -79,8 +79,8 @@ static inline uint32_t spi_get_clk(machine_spi_obj_t *spi) {
return spi->is_lp ? GetSystemCoreClock() : GetSystemAHBClock();
}
-static void spi_init(machine_spi_obj_t *spi, uint32_t baudrate,
- uint32_t polarity, uint32_t phase, uint32_t bits, uint32_t firstbit) {
+static void spi_init(machine_spi_obj_t *spi, int32_t baudrate,
+ int32_t polarity, int32_t phase, int32_t bits, int32_t firstbit) {
const machine_pin_obj_t *pins[4] = { NULL, NULL, NULL, NULL };
switch (spi->id) {
#if defined(MICROPY_HW_SPI0_SCK)
@@ -161,7 +161,9 @@ static void spi_init(machine_spi_obj_t *spi, uint32_t baudrate,
spi_mask_interrupts(spi->inst);
// Configure baudrate clock
- spi_set_bus_speed(spi->inst, baudrate, spi_get_clk(spi));
+ if (baudrate > 0) {
+ spi_set_bus_speed(spi->inst, baudrate, spi_get_clk(spi));
+ }
// Configure FIFOs
spi_set_tx_threshold(spi->inst, 0);
@@ -172,6 +174,21 @@ static void spi_init(machine_spi_obj_t *spi, uint32_t baudrate,
}
// Configure SPI bus mode.
+ if (!spi->is_lp) {
+ if (polarity < 0) {
+ polarity = (spi->inst->SPI_CTRLR0 & SPI_CTRLR0_SCPOL_HIGH) ? 1 : 0;
+ }
+ if (phase < 0) {
+ phase = (spi->inst->SPI_CTRLR0 & SPI_CTRLR0_SCPH_HIGH) ? 1 : 0;
+ }
+ } else {
+ if (polarity < 0) {
+ polarity = (spi->inst->SPI_CTRLR0 & LPSPI_CTRLR0_SCPOL_HIGH) ? 1 : 0;
+ }
+ if (phase < 0) {
+ phase = (spi->inst->SPI_CTRLR0 & LPSPI_CTRLR0_SCPH_HIGH) ? 1 : 0;
+ }
+ }
uint32_t spi_mode = (polarity << 1) | phase;
if (!spi->is_lp) {
spi_set_mode(spi->inst, spi_mode);
@@ -193,10 +210,12 @@ static void spi_init(machine_spi_obj_t *spi, uint32_t baudrate,
}
// Configure frame size.
- if (!spi->is_lp) {
- spi_set_dfs(spi->inst, bits);
- } else {
- lpspi_set_dfs(spi->inst, bits);
+ if (bits > 0) {
+ if (!spi->is_lp) {
+ spi_set_dfs(spi->inst, bits);
+ } else {
+ lpspi_set_dfs(spi->inst, bits);
+ }
}
// Configure slave select pin