altos: include targe SPI speed in get request
authorKeith Packard <keithp@keithp.com>
Wed, 27 Jun 2012 21:34:53 +0000 (14:34 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 27 Jun 2012 21:34:53 +0000 (14:34 -0700)
STM needs it to be provided when enabling the SPI device, so just fix
AVR and cc1111 to do the same.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/avr/ao_arch_funcs.h
src/cc1111/ao_arch_funcs.h
src/drivers/ao_25lc1024.c
src/drivers/ao_at45db161d.c
src/drivers/ao_cc1120.c
src/drivers/ao_companion.c
src/drivers/ao_m25.c
src/drivers/ao_ms5607.c
src/stm-demo/ao_demo.c
src/stm/ao_arch_funcs.h
src/stm/ao_spi_stm.c

index 1eeca6cc73c0f1490cc6c1240d55fdcf46df878a..e400c98b62468a25ce961592e2171de5fe586476 100644 (file)
@@ -21,7 +21,7 @@
 
 extern __xdata uint8_t ao_spi_mutex;
 
-#define ao_spi_get_mask(reg,mask,bus) do {     \
+#define ao_spi_get_mask(reg,mask,bus,speed) do {       \
                ao_mutex_get(&ao_spi_mutex);    \
                (reg) &= ~(mask);               \
        } while (0)
@@ -31,13 +31,13 @@ extern __xdata uint8_t      ao_spi_mutex;
                ao_mutex_put(&ao_spi_mutex);    \
        } while (0)
 
-#define ao_spi_get_bit(bit) do {               \
+#define ao_spi_get_bit(reg,bit,pin,bus,speed) do {     \
                ao_mutex_get(&ao_spi_mutex);    \
-               (bit) = 0;                      \
+               (pin) = 0;                      \
        } while (0)
 
-#define ao_spi_put_bit(bit) do {               \
-               (bit) = 1;                      \
+#define ao_spi_put_bit(reg,bit,pin,bus) do {   \
+               (pin) = 1;                      \
                ao_mutex_put(&ao_spi_mutex);    \
        } while (0)
 
index 728f1f76d935507787aa6f8b6ef3749fd6720b3d..0a32296165a8ee7f63ee984018cef7499b6e0042 100644 (file)
 
 extern __xdata uint8_t ao_spi_mutex;
 
-#define ao_spi_get_mask(reg,mask,bus) do {             \
-       ao_mutex_get(&ao_spi_mutex); \
-       (reg) &= ~(mask); \
+#define AO_SPI_SPEED_FAST      17
+#define AO_SPI_SPEED_200kHz    13
+
+#define ao_spi_set_speed(speed) (U0GCR = (UxGCR_CPOL_NEGATIVE |                \
+                                         UxGCR_CPHA_FIRST_EDGE |       \
+                                         UxGCR_ORDER_MSB |             \
+                                         ((speed) << UxGCR_BAUD_E_SHIFT)))
+
+#define ao_spi_get_mask(reg,mask,bus,speed) do {       \
+               ao_mutex_get(&ao_spi_mutex);            \
+               ao_spi_set_speed(speed);                \
+               (reg) &= ~(mask);                       \
        } while (0)
 
 #define ao_spi_put_mask(reg,mask,bus) do {             \
@@ -31,8 +40,10 @@ extern __xdata uint8_t       ao_spi_mutex;
        ao_mutex_put(&ao_spi_mutex); \
        } while (0)
 
-#define ao_spi_get_bit(reg,bit,pin,bus) do {   \
+
+#define ao_spi_get_bit(reg,bit,pin,bus,speed) do {     \
                ao_mutex_get(&ao_spi_mutex);    \
+               ao_spi_set_speed(speed);        \
                pin = 0;                        \
        } while (0)
 
index b25d52c4e7e7e529325ca84ce056885c3485dd2f..c5d811f7bfc73905219c1b9cd5b6fb0ddb683aaf 100644 (file)
@@ -50,7 +50,7 @@ static __xdata uint8_t ao_ee_mutex;
        _asm nop _endasm; \
 } while(0)
 
-#define ao_ee_cs_low() ao_spi_get_bit(EE_CS_PORT, EE_CS_PIN, EE_CS, AO_EE_SPI_BUS)
+#define ao_ee_cs_low() ao_spi_get_bit(EE_CS_PORT, EE_CS_PIN, EE_CS, AO_EE_SPI_BUS, AO_SPI_SPEED_FAST)
 
 #define ao_ee_cs_high()        ao_spi_put_bit(EE_CS_PORT, EE_CS_PIN, EE_CS, AO_EE_SPI_BUS)
 
index 5eb25acf29bbf6efaaa4297899f0b9f95a774950..e7e74153db6c46298eba0556e088380d24351f8f 100644 (file)
@@ -43,7 +43,7 @@ __xdata uint8_t ao_flash_mutex;
        _asm nop _endasm; \
 } while(0)
 
-#define ao_flash_cs_low()      ao_spi_get_bit(FLASH_CS_PORT, FLASH_CS_PIN, FLASH_CS, AO_FLASH_SPI_BUS)
+#define ao_flash_cs_low()      ao_spi_get_bit(FLASH_CS_PORT, FLASH_CS_PIN, FLASH_CS, AO_FLASH_SPI_BUS, AO_SPI_SPEED_FAST)
 
 #define ao_flash_cs_high()     ao_spi_put_bit(FLASH_CS_PORT, FLASH_CS_PIN, FLASH_CS, AO_FLASH_SPI_BUS)
 
index cc9ebd304bc499c2aec4484588e641e5a0ccf63b..c974613e0a49f2cb60280446f54cd0bb277d7a60 100644 (file)
@@ -33,7 +33,7 @@ const uint32_t        ao_radio_cal = 0x6ca333;
 
 #define FOSC   32000000
 
-#define ao_radio_select()      ao_spi_get_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS)
+#define ao_radio_select()      ao_spi_get_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS,AO_SPI_SPEED_1MHz)
 #define ao_radio_deselect()    ao_spi_put_mask(AO_CC1120_SPI_CS_PORT,(1 << AO_CC1120_SPI_CS_PIN),AO_CC1120_SPI_BUS)
 #define ao_radio_spi_send(d,l) ao_spi_send((d), (l), AO_CC1120_SPI_BUS)
 #define ao_radio_spi_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_CC1120_SPI_BUS)
index a31cc2eadf2e5742917b33872519e382b0b3a75c..85d68c44151c4de4029af86318ad9914d10b906a 100644 (file)
 #include <ao.h>
 #include <ao_companion.h>
 
-#ifndef ao_spi_slow
-#define ao_spi_slow(bus) (U0GCR = (UxGCR_CPOL_NEGATIVE |       \
-                                  UxGCR_CPHA_FIRST_EDGE |      \
-                                  UxGCR_ORDER_MSB |            \
-                                  (13 << UxGCR_BAUD_E_SHIFT)))
-
-#define ao_spi_fast(bus) (U0GCR = (UxGCR_CPOL_NEGATIVE |       \
-                                  UxGCR_CPHA_FIRST_EDGE |      \
-                                  UxGCR_ORDER_MSB |            \
-                                  (17 << UxGCR_BAUD_E_SHIFT)))
+#ifdef MEGAMETRUM
+#define ao_spi_slow(b)
+#define ao_spi_fast(b)
 #endif
 
 #define COMPANION_SELECT()     do {                    \
                ao_spi_get_bit(AO_COMPANION_CS_PORT,    \
                               AO_COMPANION_CS_PIN,     \
                               AO_COMPANION_CS,         \
-                              AO_COMPANION_SPI_BUS);   \
-               ao_spi_slow(AO_COMPANION_SPI_BUS);      \
+                              AO_COMPANION_SPI_BUS   \
+                              AO_SPI_SPEED_200kHz);    \
        } while (0)
 
 #define COMPANION_DESELECT()   do {                    \
-               ao_spi_fast(AO_COMPANION_SPI_BUS);      \
                ao_spi_put_bit(AO_COMPANION_CS_PORT,    \
                               AO_COMPANION_CS_PIN,     \
                               AO_COMPANION_CS,         \
index 2983a405ac3d64adda9d8a0715bfff4dedb36ca8..9603c1debbbd0584b4519ff59dbc034dbeec31ad 100644 (file)
@@ -110,7 +110,7 @@ static void ao_boot_radio(void) {
 #define ao_boot_radio()
 #endif
 
-#define M25_SELECT(cs)         do { ao_boot_radio(); ao_spi_get_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS); } while (0)
+#define M25_SELECT(cs)         do { ao_boot_radio(); ao_spi_get_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS, AO_SPI_SPEED_FAST); } while (0)
 #define M25_DESELECT(cs)       ao_spi_put_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS)
 
 #define M25_BLOCK_SHIFT                        16
index e08f4d406e7b4e58281285688af3df3d19b7b771..e1d0507ee8826b955f1e2a2075d8b1522a52b682 100644 (file)
@@ -24,7 +24,7 @@ static uint8_t                        ms5607_configured;
 
 static void
 ao_ms5607_start(void) {
-       ao_spi_get(AO_MS5607_SPI_INDEX);
+       ao_spi_get(AO_MS5607_SPI_INDEX,AO_SPI_SPEED_FAST);
        stm_gpio_set(AO_MS5607_CS_GPIO, AO_MS5607_CS, 0);
 }
 
index 73ace558dae5141015b78a66cb659b9c5276990e..9a581ff91a9ce4dc0ec19fa14f6ef1e01948899d 100644 (file)
@@ -83,7 +83,7 @@ ao_spi_write(void) {
        int i;
 
        for (i = 0; i < 10; i++) {
-               ao_spi_get(0);
+               ao_spi_get(0, AO_SPI_SPEED_FAST);
                stm_gpio_set(&stm_gpioc, 12, 0);
                ao_spi_send(data, 4, 0);
                stm_gpio_set(&stm_gpioc, 12, 1);
@@ -100,7 +100,7 @@ ao_spi_read(void) {
        int i;
 
        for (i = 0; i < 10; i++) {
-               ao_spi_get(0);
+               ao_spi_get(0, AO_SPI_SPEED_FAST);
                stm_gpio_set(&stm_gpioc, 12, 0);
                ao_spi_recv(data, 4, 0);
                stm_gpio_set(&stm_gpioc, 12, 1);
index 62b100631bc0bf87320c0e7afd5ece89fd8caa2b..7820bf798af2568e6b7ef4dc5bebf0394fda036d 100644 (file)
  */
 extern uint8_t ao_spi_mutex[STM_NUM_SPI];
 
+#define AO_SPI_SPEED_FAST      STM_SPI_CR1_BR_PCLK_4
+#define AO_SPI_SPEED_1MHz      STM_SPI_CR1_BR_PCLK_16
+#define AO_SPI_SPEED_200kHz    STM_SPI_CR1_BR_PCLK_256
+
 void
-ao_spi_get(uint8_t spi_index);
+ao_spi_get(uint8_t spi_index, uint32_t speed);
 
 void
 ao_spi_put(uint8_t spi_index);
@@ -40,20 +44,13 @@ ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
 void
 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
 
-#define AO_SPI_SPEED_FAST      STM_SPI_CR1_BR_PCLK_16
-#define AO_SPI_SPEED_200kHz    STM_SPI_CR1_BR_PCLK_256
-
 extern uint16_t        ao_spi_speed[STM_NUM_SPI];
 
-#define ao_spi_slow(bus)       (ao_spi_speed[bus] = AO_SPI_SPEED_200kHz)
-
-#define ao_spi_fast(bus)       (ao_spi_speed[bus] = AO_SPI_SPEED_FAST)
-
 void
 ao_spi_init(void);
 
-#define ao_spi_get_mask(reg,mask,bus) do {             \
-               ao_spi_get(bus);                        \
+#define ao_spi_get_mask(reg,mask,bus, speed) do {              \
+               ao_spi_get(bus, speed);                         \
                (reg)->bsrr = ((uint32_t) mask) << 16;  \
        } while (0)
 
@@ -62,7 +59,7 @@ ao_spi_init(void);
                ao_spi_put(bus);                \
        } while (0)
 
-#define ao_spi_get_bit(reg,bit,pin,bus) ao_spi_get_mask(reg,(1<<bit),bus)
+#define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
 
 #define ao_enable_port(port) do {                                      \
index 262270861257f6d5c321cda0cd9dcf31710e13d0..547de9e50ca0b887864ecaab28986d923b2ed572 100644 (file)
@@ -24,7 +24,6 @@ struct ao_spi_stm_info {
 };
 
 uint8_t                ao_spi_mutex[STM_NUM_SPI];
-uint16_t       ao_spi_speed[STM_NUM_SPI];
 
 static const struct ao_spi_stm_info ao_spi_stm_info[STM_NUM_SPI] = {
        {
@@ -268,7 +267,7 @@ ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index)
 }
 
 void
-ao_spi_get(uint8_t spi_index)
+ao_spi_get(uint8_t spi_index, uint32_t speed)
 {
        struct stm_spi  *stm_spi = ao_spi_stm_info[spi_index].stm_spi;
 
@@ -283,7 +282,7 @@ ao_spi_get(uint8_t spi_index)
                        (1 << STM_SPI_CR1_SSI) |                        /*  ... */
                        (0 << STM_SPI_CR1_LSBFIRST) |                   /* Big endian */
                        (1 << STM_SPI_CR1_SPE) |                        /* Enable SPI unit */
-                       (ao_spi_speed[spi_index] << STM_SPI_CR1_BR) |   /* baud rate to pclk/4 */
+                       (speed << STM_SPI_CR1_BR) |     /* baud rate to pclk/4 */
                        (1 << STM_SPI_CR1_MSTR) |
                        (0 << STM_SPI_CR1_CPOL) |                       /* Format 0 */
                        (0 << STM_SPI_CR1_CPHA));
@@ -311,7 +310,6 @@ ao_spi_channel_init(uint8_t spi_index)
                        (0 << STM_SPI_CR2_SSOE) |
                        (0 << STM_SPI_CR2_TXDMAEN) |
                        (0 << STM_SPI_CR2_RXDMAEN));
-       ao_spi_speed[spi_index] = AO_SPI_SPEED_FAST;
 }
 
 void