From d2216717adac7f7e917e13085de6ce8118023d5f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 26 Jan 2024 11:07:55 -0800 Subject: [PATCH] altos: Add SPI bus parameter to ao_spi_speed The stm32f1 series has two SPI busses and they operate from different base clocks, so the computation of the clock divider to get a particular frequency depends upon which SPI bus is being used. Signed-off-by: Keith Packard --- src/drivers/ao_adxl375.c | 2 +- src/drivers/ao_bmi088.c | 2 +- src/drivers/ao_bmx160.c | 2 +- src/drivers/ao_cc1120.c | 2 +- src/drivers/ao_cc115l.c | 2 +- src/drivers/ao_cc1200.c | 2 +- src/drivers/ao_companion.c | 2 +- src/drivers/ao_m25.c | 2 +- src/drivers/ao_mma655x.c | 2 +- src/drivers/ao_mmc5983.c | 2 +- src/drivers/ao_mpu6000.c | 2 +- src/drivers/ao_mpu9250.c | 2 +- src/drivers/ao_ms5607.c | 2 +- src/drivers/ao_st7565.h | 2 +- src/lpc/ao_arch.h | 3 ++- src/samd21/ao_arch_funcs.h | 3 ++- src/snekboard/snekboard.c | 2 +- src/stm/ao_arch_funcs.h | 3 ++- src/stm32l0/ao_arch_funcs.h | 3 ++- src/stmf0/ao_arch_funcs.h | 3 ++- 20 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/drivers/ao_adxl375.c b/src/drivers/ao_adxl375.c index 9e5666cc..70bf92a5 100644 --- a/src/drivers/ao_adxl375.c +++ b/src/drivers/ao_adxl375.c @@ -26,7 +26,7 @@ #define PRINTD(l,...) #endif -#define AO_ADXL375_SPI_SPEED ao_spi_speed(5000000) +#define AO_ADXL375_SPI_SPEED ao_spi_speed(AO_ADXL375_SPI_INDEX, 5000000) struct ao_adxl375_sample ao_adxl375_current; diff --git a/src/drivers/ao_bmi088.c b/src/drivers/ao_bmi088.c index d03fceab..96e995f0 100644 --- a/src/drivers/ao_bmi088.c +++ b/src/drivers/ao_bmi088.c @@ -20,7 +20,7 @@ #include #include -#define AO_BMI088_SPI_SPEED ao_spi_speed(100000) +#define AO_BMI088_SPI_SPEED ao_spi_speed(AO_BMI088_SPI_BUS, 100000) #define ao_bmi088_spi_get() ao_spi_get(AO_BMI088_SPI_BUS, AO_BMI088_SPI_SPEED) #define ao_bmi088_spi_put() ao_spi_put(AO_BMI088_SPI_BUS) diff --git a/src/drivers/ao_bmx160.c b/src/drivers/ao_bmx160.c index eabe0783..0df30f94 100644 --- a/src/drivers/ao_bmx160.c +++ b/src/drivers/ao_bmx160.c @@ -24,7 +24,7 @@ static uint8_t ao_bmx160_configured; static struct ao_bmm150_trim ao_bmm150_trim; -#define AO_BMX160_SPI_SPEED ao_spi_speed(10000000) +#define AO_BMX160_SPI_SPEED ao_spi_speed(AO_BMX160_SPI_BUS, 10000000) #define ao_bmx160_spi_get() ao_spi_get(AO_BMX160_SPI_BUS, AO_BMX160_SPI_SPEED) #define ao_bmx160_spi_put() ao_spi_put(AO_BMX160_SPI_BUS) diff --git a/src/drivers/ao_cc1120.c b/src/drivers/ao_cc1120.c index 133f9803..fe3c24c6 100644 --- a/src/drivers/ao_cc1120.c +++ b/src/drivers/ao_cc1120.c @@ -42,7 +42,7 @@ extern const uint32_t ao_radio_cal; #define FOSC 32000000 -#define AO_CC1120_SPI_SPEED ao_spi_speed(6100000) /* 6.1MHz max with 32MHz osc */ +#define AO_CC1120_SPI_SPEED ao_spi_speed(AO_CC1120_SPI_BUS, 6100000) /* 6.1MHz max with 32MHz osc */ #define ao_radio_try_select(task_id) ao_spi_try_get_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS,AO_CC1120_SPI_SPEED, task_id) #define ao_radio_select() ao_spi_get_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS,AO_CC1120_SPI_SPEED) diff --git a/src/drivers/ao_cc115l.c b/src/drivers/ao_cc115l.c index 65ab5767..606a1804 100644 --- a/src/drivers/ao_cc115l.c +++ b/src/drivers/ao_cc115l.c @@ -39,7 +39,7 @@ static uint8_t ao_radio_abort; /* radio operation should abort */ #define FOSC 26000000 -#define AO_CC115L_SPI_SPEED ao_spi_speed(6500000) /* for back-to-back access */ +#define AO_CC115L_SPI_SPEED ao_spi_speed(AO_CC115L_SPI_BUS, 6500000) /* for back-to-back access */ #define ao_radio_select() ao_spi_get_mask(AO_CC115L_SPI_CS_PORT,(1 << AO_CC115L_SPI_CS_PIN),AO_CC115L_SPI_BUS,AO_CC115L_SPI_SPEED) #define ao_radio_deselect() ao_spi_put_mask(AO_CC115L_SPI_CS_PORT,(1 << AO_CC115L_SPI_CS_PIN),AO_CC115L_SPI_BUS) diff --git a/src/drivers/ao_cc1200.c b/src/drivers/ao_cc1200.c index b295d054..ed1d298e 100644 --- a/src/drivers/ao_cc1200.c +++ b/src/drivers/ao_cc1200.c @@ -53,7 +53,7 @@ extern const uint32_t ao_radio_cal; #define FOSC 40000000 #endif -#define AO_CC1200_SPI_SPEED ao_spi_speed(7700000) /* 7.7MHz max for extended memory reads */ +#define AO_CC1200_SPI_SPEED ao_spi_speed(AO_CC1200_SPI_BUS, 7700000) /* 7.7MHz max for extended memory reads */ #define ao_radio_select() ao_spi_get_mask(AO_CC1200_SPI_CS_PORT,(1 << AO_CC1200_SPI_CS_PIN),AO_CC1200_SPI_BUS,AO_CC1200_SPI_SPEED) #define ao_radio_deselect() ao_spi_put_mask(AO_CC1200_SPI_CS_PORT,(1 << AO_CC1200_SPI_CS_PIN),AO_CC1200_SPI_BUS) diff --git a/src/drivers/ao_companion.c b/src/drivers/ao_companion.c index 2315b63f..bda7805f 100644 --- a/src/drivers/ao_companion.c +++ b/src/drivers/ao_companion.c @@ -28,7 +28,7 @@ #error HAS_COMPANION not set in ao_companion.c #endif -#define AO_COMPANION_SPI_SPEED ao_spi_speed(200000) +#define AO_COMPANION_SPI_SPEED ao_spi_speed(AO_COMPANION_SPI_BUS, 200000) #define COMPANION_SELECT() do { \ ao_spi_get_bit(AO_COMPANION_CS_PORT, \ diff --git a/src/drivers/ao_m25.c b/src/drivers/ao_m25.c index accf943a..4be91e13 100644 --- a/src/drivers/ao_m25.c +++ b/src/drivers/ao_m25.c @@ -101,7 +101,7 @@ static uint8_t ao_m25_mutex; static uint8_t ao_m25_instruction[4]; -#define AO_M25_SPI_SPEED ao_spi_speed(10000000) /* this seems like a reasonable minimum speed to require */ +#define AO_M25_SPI_SPEED ao_spi_speed(AO_M25_SPI_BUS, 10000000) /* this seems like a reasonable minimum speed to require */ #define M25_SELECT(cs) ao_spi_get_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS,AO_M25_SPI_SPEED) #define M25_DESELECT(cs) ao_spi_put_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS) diff --git a/src/drivers/ao_mma655x.c b/src/drivers/ao_mma655x.c index 3fd8e064..f79ea2d7 100644 --- a/src/drivers/ao_mma655x.c +++ b/src/drivers/ao_mma655x.c @@ -30,7 +30,7 @@ #define PRINTD(l,...) #endif -#define AO_MMA655X_SPI_SPEED ao_spi_speed(8333333) /* 120ns clock period */ +#define AO_MMA655X_SPI_SPEED ao_spi_speed(AO_MMA655X_SPI_INDEX, 8333333) /* 120ns clock period */ static void ao_mma655x_start(void) { diff --git a/src/drivers/ao_mmc5983.c b/src/drivers/ao_mmc5983.c index d086fc3e..02df7a26 100644 --- a/src/drivers/ao_mmc5983.c +++ b/src/drivers/ao_mmc5983.c @@ -127,7 +127,7 @@ ao_mmc5983_raw(struct ao_mmc5983_raw *raw) #endif #else -#define AO_MMC5983_SPI_SPEED ao_spi_speed(2000000) +#define AO_MMC5983_SPI_SPEED ao_spi_speed(AO_MMC5983_SPI_INDEX, 2000000) static void ao_mmc5983_start(void) { diff --git a/src/drivers/ao_mpu6000.c b/src/drivers/ao_mpu6000.c index c33032af..e5594a66 100644 --- a/src/drivers/ao_mpu6000.c +++ b/src/drivers/ao_mpu6000.c @@ -32,7 +32,7 @@ static uint8_t ao_mpu6000_configured; #if AO_MPU6000_SPI -#define AO_MPU6000_SPI_SPEED ao_spi_speed(1000000) /* 1Mhz for all register access */ +#define AO_MPU6000_SPI_SPEED ao_spi_speed(AO_MPU6000_SPI_BUS, 1000000) /* 1Mhz for all register access */ #define ao_mpu6000_spi_get() ao_spi_get(AO_MPU6000_SPI_BUS, AO_MPU6000_SPI_SPEED) #define ao_mpu6000_spi_put() ao_spi_put(AO_MPU6000_SPI_BUS) diff --git a/src/drivers/ao_mpu9250.c b/src/drivers/ao_mpu9250.c index a77abf98..63d8d6d7 100644 --- a/src/drivers/ao_mpu9250.c +++ b/src/drivers/ao_mpu9250.c @@ -35,7 +35,7 @@ static uint8_t ao_mpu9250_configured; #if AO_MPU9250_SPI #ifndef AO_MPU9250_SPI_SPEED -#define AO_MPU9250_SPI_SPEED ao_spi_speed(1000000) /* 1MHz max SCLK */ +#define AO_MPU9250_SPI_SPEED ao_spi_speed(AO_MPU9250_SPI_BUS, 1000000) /* 1MHz max SCLK */ #endif #define ao_mpu9250_spi_get() ao_spi_get(AO_MPU9250_SPI_BUS, AO_MPU9250_SPI_SPEED) diff --git a/src/drivers/ao_ms5607.c b/src/drivers/ao_ms5607.c index dce7c0e6..4a9802d7 100644 --- a/src/drivers/ao_ms5607.c +++ b/src/drivers/ao_ms5607.c @@ -25,7 +25,7 @@ struct ao_ms5607_prom ao_ms5607_prom; static uint8_t ms5607_configured; -#define AO_MS5607_SPI_SPEED ao_spi_speed(20000000) +#define AO_MS5607_SPI_SPEED ao_spi_speed(AO_MS5607_SPI_INDEX, 20000000) static void ao_ms5607_start(void) { diff --git a/src/drivers/ao_st7565.h b/src/drivers/ao_st7565.h index e1340f8a..f7ddfa3d 100644 --- a/src/drivers/ao_st7565.h +++ b/src/drivers/ao_st7565.h @@ -47,7 +47,7 @@ #define ST7565_BOOSTER_RATIO_SET 0xf8 #define ST7565_NOP 0xe3 -#define AO_ST7565_SPI_SPEED ao_spi_speed(20000000) +#define AO_ST7565_SPI_SPEED ao_spi_speed(AO_ST7565_SPI_BUS, 10000000) void ao_st7565_update(struct ao_bitmap *bitmap); diff --git a/src/lpc/ao_arch.h b/src/lpc/ao_arch.h index 59f3a7ee..05bb97d9 100644 --- a/src/lpc/ao_arch.h +++ b/src/lpc/ao_arch.h @@ -116,8 +116,9 @@ ao_serial_init(void); #define _AO_SPI_SPEED_62500Hz 768 static inline uint32_t -ao_spi_speed(uint32_t hz) +ao_spi_speed(int index, uint32_t hz) { + (void) index; if (hz >= 4000000) return _AO_SPI_SPEED_4MHz; if (hz >= 2000000) return _AO_SPI_SPEED_2MHz; if (hz >= 1000000) return _AO_SPI_SPEED_1MHz; diff --git a/src/samd21/ao_arch_funcs.h b/src/samd21/ao_arch_funcs.h index 32670b45..754ca3b5 100644 --- a/src/samd21/ao_arch_funcs.h +++ b/src/samd21/ao_arch_funcs.h @@ -282,10 +282,11 @@ ao_spi_put_bit(struct samd21_port *port, uint8_t bit, uint16_t spi_index) } static inline uint8_t -ao_spi_speed(uint32_t hz) +ao_spi_speed(int index, uint32_t hz) { int32_t baud = (int32_t) (AO_SYSCLK / (2 * hz)) - 1; + (void) index; if (baud < 1) baud = 1; if (baud > 255) diff --git a/src/snekboard/snekboard.c b/src/snekboard/snekboard.c index 719ea01d..a829aa13 100644 --- a/src/snekboard/snekboard.c +++ b/src/snekboard/snekboard.c @@ -19,7 +19,7 @@ #define SNEK_CS_PORT (&samd21_port_a) #define SNEK_CS_PIN (11) #define SNEK_SPI_INDEX AO_SPI_0_PA08_PA09_PA10 -#define SNEK_SPI_SPEED ao_spi_speed(1000000) +#define SNEK_SPI_SPEED ao_spi_speed(SNEK_SPI_INDEX, 1000000) static const uint8_t spi_test[] = { 0x55, diff --git a/src/stm/ao_arch_funcs.h b/src/stm/ao_arch_funcs.h index 9922513b..0f2f36a6 100644 --- a/src/stm/ao_arch_funcs.h +++ b/src/stm/ao_arch_funcs.h @@ -34,8 +34,9 @@ #define _AO_SPI_SPEED_62500Hz STM_SPI_CR1_BR_PCLK_256 static inline uint32_t -ao_spi_speed(uint32_t hz) +ao_spi_speed(int index, uint32_t hz) { + (void) index; if (hz >= 4000000) return _AO_SPI_SPEED_4MHz; if (hz >= 2000000) return _AO_SPI_SPEED_2MHz; if (hz >= 1000000) return _AO_SPI_SPEED_1MHz; diff --git a/src/stm32l0/ao_arch_funcs.h b/src/stm32l0/ao_arch_funcs.h index 385fe0e3..5f0f07de 100644 --- a/src/stm32l0/ao_arch_funcs.h +++ b/src/stm32l0/ao_arch_funcs.h @@ -36,8 +36,9 @@ /* Companion bus wants something no faster than 200kHz */ static inline uint32_t -ao_spi_speed(uint32_t hz) +ao_spi_speed(int index, uint32_t hz) { + (void) index; if (hz >= 4000000) return _AO_SPI_SPEED_4MHz; if (hz >= 2000000) return _AO_SPI_SPEED_2MHz; if (hz >= 1000000) return _AO_SPI_SPEED_1MHz; diff --git a/src/stmf0/ao_arch_funcs.h b/src/stmf0/ao_arch_funcs.h index 1a9f28e3..953a0c00 100644 --- a/src/stmf0/ao_arch_funcs.h +++ b/src/stmf0/ao_arch_funcs.h @@ -36,8 +36,9 @@ #define _AO_SPI_SPEED_187500Hz STM_SPI_CR1_BR_PCLK_256 static inline uint32_t -ao_spi_speed(uint32_t hz) +ao_spi_speed(int index, uint32_t hz) { + (void) index; if (hz >=24000000) return _AO_SPI_SPEED_24MHz; if (hz >=12000000) return _AO_SPI_SPEED_12MHz; if (hz >= 6000000) return _AO_SPI_SPEED_6MHz; -- 2.30.2