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)
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)
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 { \
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)
_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)
_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)
#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)
#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, \
#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
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);
}
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);
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);
*/
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);
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)
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 { \
};
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] = {
{
}
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;
(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));
(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