summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2012-04-09 23:27:43 -0700
committerKeith Packard <keithp@keithp.com>2012-04-14 13:21:09 -0700
commit0dd9e1dd62656a931f9559af6da9131f704f83f9 (patch)
treef0c659cda9e107b8a835c0f815d4153e4da09c8e /src/drivers
parent35e3c47da895bdd868b9b66b98bca64bd82db862 (diff)
altos: Add support for multiple SPI busses and sharing device drivers
The STM32L151 has several SPI busses, and we want to use more than one, so add a 'bus' parameter to the SPI interfaces. To avoid wasting time on AVR and CC1111 processors which only use one SPI bus, elide those parameters from the actual functions by wrapping them with macros. Configuring chip select is now all macroized so that each chip can have its own version, allowing the STM to share the various SPI device drivers with the cc1111 and avr processors. Note that only the M25 driver has been ported; porting the others is 'trivial', but not necessary at this point. Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/ao_25lc1024.c20
-rw-r--r--src/drivers/ao_at45db161d.c18
-rw-r--r--src/drivers/ao_companion.c10
-rw-r--r--src/drivers/ao_m25.c43
-rw-r--r--src/drivers/ao_ms5607.c5
5 files changed, 44 insertions, 52 deletions
diff --git a/src/drivers/ao_25lc1024.c b/src/drivers/ao_25lc1024.c
index 2d047a44..f0fb13c9 100644
--- a/src/drivers/ao_25lc1024.c
+++ b/src/drivers/ao_25lc1024.c
@@ -49,9 +49,9 @@ static __xdata uint8_t ao_ee_mutex;
_asm nop _endasm; \
} while(0)
-#define ao_ee_cs_low() ao_spi_get_bit(EE_CS)
+#define ao_ee_cs_low() ao_spi_get_bit(EE_CS, AO_EE_SPI_BUS)
-#define ao_ee_cs_high() ao_spi_put_bit(EE_CS)
+#define ao_ee_cs_high() ao_spi_put_bit(EE_CS, AO_EE_SPI_BUS)
struct ao_ee_instruction {
uint8_t instruction;
@@ -63,7 +63,7 @@ ao_ee_write_enable(void)
{
ao_ee_cs_low();
ao_ee_instruction.instruction = EE_WREN;
- ao_spi_send(&ao_ee_instruction, 1);
+ ao_spi_send(&ao_ee_instruction, 1, AO_EE_SPI_BUS);
ao_ee_cs_high();
}
@@ -72,8 +72,8 @@ ao_ee_rdsr(void)
{
ao_ee_cs_low();
ao_ee_instruction.instruction = EE_RDSR;
- ao_spi_send(&ao_ee_instruction, 1);
- ao_spi_recv(&ao_ee_instruction, 1);
+ ao_spi_send(&ao_ee_instruction, 1, AO_EE_SPI_BUS);
+ ao_spi_recv(&ao_ee_instruction, 1, AO_EE_SPI_BUS);
ao_ee_cs_high();
return ao_ee_instruction.instruction;
}
@@ -84,7 +84,7 @@ ao_ee_wrsr(uint8_t status)
ao_ee_cs_low();
ao_ee_instruction.instruction = EE_WRSR;
ao_ee_instruction.address[0] = status;
- ao_spi_send(&ao_ee_instruction, 2);
+ ao_spi_send(&ao_ee_instruction, 2, AO_EE_SPI_BUS);
ao_ee_cs_high();
}
@@ -111,8 +111,8 @@ ao_ee_write_block(void)
ao_ee_instruction.address[0] = ao_ee_block >> 8;
ao_ee_instruction.address[1] = ao_ee_block;
ao_ee_instruction.address[2] = 0;
- ao_spi_send(&ao_ee_instruction, 4);
- ao_spi_send(ao_ee_data, EE_BLOCK_SIZE);
+ ao_spi_send(&ao_ee_instruction, 4, AO_EE_SPI_BUS);
+ ao_spi_send(ao_ee_data, EE_BLOCK_SIZE, AO_EE_SPI_BUS);
ao_ee_cs_high();
for (;;) {
uint8_t status = ao_ee_rdsr();
@@ -130,8 +130,8 @@ ao_ee_read_block(void)
ao_ee_instruction.address[0] = ao_ee_block >> 8;
ao_ee_instruction.address[1] = ao_ee_block;
ao_ee_instruction.address[2] = 0;
- ao_spi_send(&ao_ee_instruction, 4);
- ao_spi_recv(ao_ee_data, EE_BLOCK_SIZE);
+ ao_spi_send(&ao_ee_instruction, 4, AO_EE_SPI_BUS);
+ ao_spi_recv(ao_ee_data, EE_BLOCK_SIZE, AO_EE_SPI_BUS);
ao_ee_cs_high();
}
diff --git a/src/drivers/ao_at45db161d.c b/src/drivers/ao_at45db161d.c
index 6cd689e5..afe0080b 100644
--- a/src/drivers/ao_at45db161d.c
+++ b/src/drivers/ao_at45db161d.c
@@ -43,9 +43,9 @@ __xdata uint8_t ao_flash_mutex;
_asm nop _endasm; \
} while(0)
-#define ao_flash_cs_low() ao_spi_get_bit(FLASH_CS)
+#define ao_flash_cs_low() ao_spi_get_bit(FLASH_CS, AO_FLASH_SPI_BUS)
-#define ao_flash_cs_high() ao_spi_put_bit(FLASH_CS)
+#define ao_flash_cs_high() ao_spi_put_bit(FLASH_CS, AO_FLASH_SPI_BUS)
struct ao_flash_instruction {
uint8_t instruction;
@@ -60,7 +60,7 @@ ao_flash_set_pagesize_512(void)
ao_flash_instruction.address[0] = FLASH_SET_512_BYTE_0;
ao_flash_instruction.address[1] = FLASH_SET_512_BYTE_1;
ao_flash_instruction.address[2] = FLASH_SET_512_BYTE_2;
- ao_spi_send(&ao_flash_instruction, 4);
+ ao_spi_send(&ao_flash_instruction, 4, AO_FLASH_SPI_BUS);
ao_flash_cs_high();
}
@@ -70,8 +70,8 @@ ao_flash_read_status(void)
{
ao_flash_cs_low();
ao_flash_instruction.instruction = FLASH_READ_STATUS;
- ao_spi_send(&ao_flash_instruction, 1);
- ao_spi_recv(&ao_flash_instruction, 1);
+ ao_spi_send(&ao_flash_instruction, 1, AO_FLASH_SPI_BUS);
+ ao_spi_recv(&ao_flash_instruction, 1, AO_FLASH_SPI_BUS);
ao_flash_cs_high();
return ao_flash_instruction.instruction;
}
@@ -190,8 +190,8 @@ ao_flash_write_block(void)
ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
ao_flash_instruction.address[2] = 0;
- ao_spi_send(&ao_flash_instruction, 4);
- ao_spi_send(ao_flash_data, ao_storage_block);
+ ao_spi_send(&ao_flash_instruction, 4, AO_FLASH_SPI_BUS);
+ ao_spi_send(ao_flash_data, ao_storage_block, AO_FLASH_SPI_BUS);
ao_flash_cs_high();
ao_flash_write_pending = 1;
}
@@ -208,8 +208,8 @@ ao_flash_read_block(void)
ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
ao_flash_instruction.address[2] = 0;
- ao_spi_send(&ao_flash_instruction, 4);
- ao_spi_recv(ao_flash_data, ao_flash_block_size);
+ ao_spi_send(&ao_flash_instruction, 4, AO_FLASH_SPI_BUS);
+ ao_spi_recv(ao_flash_data, ao_flash_block_size, AO_FLASH_SPI_BUS);
ao_flash_cs_high();
}
diff --git a/src/drivers/ao_companion.c b/src/drivers/ao_companion.c
index 2e587f8e..fe88e998 100644
--- a/src/drivers/ao_companion.c
+++ b/src/drivers/ao_companion.c
@@ -27,8 +27,8 @@
UxGCR_ORDER_MSB | \
(17 << UxGCR_BAUD_E_SHIFT)))
-#define COMPANION_SELECT() do { ao_spi_get_bit(COMPANION_CS); ao_spi_slow(); } while (0)
-#define COMPANION_DESELECT() do { ao_spi_fast(); ao_spi_put_bit(COMPANION_CS); } while (0)
+#define COMPANION_SELECT() do { ao_spi_get_bit(COMPANION_CS, AO_COMPANION_BUS); ao_spi_slow(); } while (0)
+#define COMPANION_DESELECT() do { ao_spi_fast(); ao_spi_put_bit(COMPANION_CS, AO_COMPANION_BUS); } while (0)
__xdata struct ao_companion_command ao_companion_command;
__xdata struct ao_companion_setup ao_companion_setup;
@@ -45,7 +45,7 @@ ao_companion_send_command(uint8_t command)
ao_companion_command.tick = ao_time();
ao_companion_command.serial = ao_serial_number;
ao_companion_command.flight = ao_flight_number;
- ao_spi_send(&ao_companion_command, sizeof (ao_companion_command));
+ ao_spi_send(&ao_companion_command, sizeof (ao_companion_command), AO_COMPANION_SPI_BUS);
}
static uint8_t
@@ -53,7 +53,7 @@ ao_companion_get_setup(void)
{
COMPANION_SELECT();
ao_companion_send_command(AO_COMPANION_SETUP);
- ao_spi_recv(&ao_companion_setup, sizeof (ao_companion_setup));
+ ao_spi_recv(&ao_companion_setup, sizeof (ao_companion_setup), AO_COMPANION_SPI_BUS);
COMPANION_DESELECT();
return (ao_companion_setup.board_id ==
~ao_companion_setup.board_id_inverse);
@@ -65,7 +65,7 @@ ao_companion_get_data(void)
COMPANION_SELECT();
ao_companion_send_command(AO_COMPANION_FETCH);
ao_mutex_get(&ao_companion_mutex);
- ao_spi_recv(&ao_companion_data, ao_companion_setup.channels * 2);
+ ao_spi_recv(&ao_companion_data, ao_companion_setup.channels * 2, AO_COMPANION_SPI_BUS);
ao_mutex_put(&ao_companion_mutex);
COMPANION_DESELECT();
}
diff --git a/src/drivers/ao_m25.c b/src/drivers/ao_m25.c
index 28cb1dd7..c807cd68 100644
--- a/src/drivers/ao_m25.c
+++ b/src/drivers/ao_m25.c
@@ -99,8 +99,8 @@ static __xdata uint8_t ao_m25_mutex;
static __xdata uint8_t ao_m25_instruction[4];
-#define M25_SELECT(cs) ao_spi_get_mask(SPI_CS_PORT,cs)
-#define M25_DESELECT(cs) ao_spi_put_mask(SPI_CS_PORT,cs)
+#define M25_SELECT(cs) ao_spi_get_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS)
+#define M25_DESELECT(cs) ao_spi_put_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS)
#define M25_BLOCK_SHIFT 16
#define M25_BLOCK 65536L
@@ -116,9 +116,9 @@ ao_m25_wait_wip(uint8_t cs)
if (ao_m25_wip & cs) {
M25_SELECT(cs);
ao_m25_instruction[0] = M25_RDSR;
- ao_spi_send(ao_m25_instruction, 1);
+ ao_spi_send(ao_m25_instruction, 1, AO_M25_SPI_BUS);
do {
- ao_spi_recv(ao_m25_instruction, 1);
+ ao_spi_recv(ao_m25_instruction, 1, AO_M25_SPI_BUS);
} while (ao_m25_instruction[0] & M25_STATUS_WIP);
M25_DESELECT(cs);
ao_m25_wip &= ~cs;
@@ -135,7 +135,7 @@ ao_m25_write_enable(uint8_t cs)
{
M25_SELECT(cs);
ao_m25_instruction[0] = M25_WREN;
- ao_spi_send(&ao_m25_instruction, 1);
+ ao_spi_send(&ao_m25_instruction, 1, AO_M25_SPI_BUS);
M25_DESELECT(cs);
ao_m25_wip |= cs;
}
@@ -150,8 +150,8 @@ ao_m25_read_capacity(uint8_t cs)
uint8_t capacity;
M25_SELECT(cs);
ao_m25_instruction[0] = M25_RDID;
- ao_spi_send(ao_m25_instruction, 1);
- ao_spi_recv(ao_m25_instruction, M25_RDID_LEN);
+ ao_spi_send(ao_m25_instruction, 1, AO_M25_SPI_BUS);
+ ao_spi_recv(ao_m25_instruction, M25_RDID_LEN, AO_M25_SPI_BUS);
M25_DESELECT(cs);
/* Check to see if the chip is present */
@@ -183,7 +183,7 @@ ao_m25_set_address(uint32_t pos)
chip = ao_m25_pin[chip];
#else
- chip = M25_CS_MASK;
+ chip = AO_M25_SPI_CS_MASK;
#endif
ao_m25_wait_wip(chip);
@@ -210,7 +210,7 @@ ao_m25_scan(void)
#if M25_MAX_CHIPS > 1
ao_m25_numchips = 0;
for (pin = 1; pin != 0; pin <<= 1) {
- if (M25_CS_MASK & pin) {
+ if (AO_M25_SPI_CS_MASK & pin) {
size = ao_m25_read_capacity(pin);
if (size != 0) {
ao_m25_size[ao_m25_numchips] = size;
@@ -221,7 +221,7 @@ ao_m25_scan(void)
}
}
#else
- ao_m25_total = ao_m25_read_capacity(M25_CS_MASK);
+ ao_m25_total = ao_m25_read_capacity(AO_M25_SPI_CS_MASK);
#endif
if (!ao_m25_total)
return 0;
@@ -253,7 +253,7 @@ ao_storage_erase(uint32_t pos) __reentrant
ao_m25_instruction[0] = M25_SE;
M25_SELECT(cs);
- ao_spi_send(ao_m25_instruction, 4);
+ ao_spi_send(ao_m25_instruction, 4, AO_M25_SPI_BUS);
M25_DESELECT(cs);
ao_m25_wip |= cs;
@@ -280,8 +280,8 @@ ao_storage_device_write(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
ao_m25_instruction[0] = M25_PP;
M25_SELECT(cs);
- ao_spi_send(ao_m25_instruction, 4);
- ao_spi_send(d, len);
+ ao_spi_send(ao_m25_instruction, 4, AO_M25_SPI_BUS);
+ ao_spi_send(d, len, AO_M25_SPI_BUS);
M25_DESELECT(cs);
ao_mutex_put(&ao_m25_mutex);
@@ -306,8 +306,8 @@ ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
/* No need to use the FAST_READ as we're running at only 8MHz */
ao_m25_instruction[0] = M25_READ;
M25_SELECT(cs);
- ao_spi_send(ao_m25_instruction, 4);
- ao_spi_recv(d, len);
+ ao_spi_send(ao_m25_instruction, 4, AO_M25_SPI_BUS);
+ ao_spi_recv(d, len, AO_M25_SPI_BUS);
M25_DESELECT(cs);
ao_mutex_put(&ao_m25_mutex);
@@ -350,14 +350,14 @@ ao_storage_device_info(void) __reentrant
printf ("Available chips:\n");
for (cs = 1; cs != 0; cs <<= 1) {
- if ((M25_CS_MASK & cs) == 0)
+ if ((AO_M25_SPI_CS_MASK & cs) == 0)
continue;
ao_mutex_get(&ao_m25_mutex);
M25_SELECT(cs);
ao_m25_instruction[0] = M25_RDID;
- ao_spi_send(ao_m25_instruction, 1);
- ao_spi_recv(ao_m25_instruction, M25_RDID_LEN);
+ ao_spi_send(ao_m25_instruction, 1, AO_M25_SPI_BUS);
+ ao_spi_recv(ao_m25_instruction, M25_RDID_LEN, AO_M25_SPI_BUS);
M25_DESELECT(cs);
printf ("Select %02x manf %02x type %02x cap %02x uid %02x\n",
@@ -373,10 +373,5 @@ ao_storage_device_info(void) __reentrant
void
ao_storage_device_init(void)
{
- /* Set up chip select wires */
- SPI_CS_PORT |= M25_CS_MASK; /* raise all CS pins */
- SPI_CS_DIR |= M25_CS_MASK; /* set CS pins as outputs */
-#ifdef SPI_CS_SEL
- SPI_CS_SEL &= ~M25_CS_MASK; /* set CS pins as GPIO */
-#endif
+ ao_spi_init_cs (AO_M25_SPI_CS_PORT, AO_M25_SPI_CS_MASK);
}
diff --git a/src/drivers/ao_ms5607.c b/src/drivers/ao_ms5607.c
index 87748272..3c0a310d 100644
--- a/src/drivers/ao_ms5607.c
+++ b/src/drivers/ao_ms5607.c
@@ -155,8 +155,5 @@ void
ao_ms5607_init(void)
{
ao_cmd_register(&ao_ms5607_cmds[0]);
-
- stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN);
- stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 1);
- stm_moder_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, STM_MODER_OUTPUT);
+ ao_spi_init_cs(AO_MS5607_CS_GPIO, (1 << AO_MS5607_CS));
}