From e5e1a3492898aa6842073177c20f56d0fc46556d Mon Sep 17 00:00:00 2001 From: Jo Van Bulck Date: Thu, 7 Sep 2017 16:50:07 +0200 Subject: [PATCH] Add support for multi-device SPI bus. --- .gitignore | 1 + include/sancus_support/spi.h | 4 ++-- src/dev/spi.c | 21 +++++++++------------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c56f0f0..6bdd088 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build*/ *~ *.kdev* +*.swp diff --git a/include/sancus_support/spi.h b/include/sancus_support/spi.h index 4228ab6..9651f75 100644 --- a/include/sancus_support/spi.h +++ b/include/sancus_support/spi.h @@ -16,8 +16,8 @@ typedef enum } SpiCpha; void spi_init(SpiCpol cpol, SpiCpha cpha, unsigned int clk_div); -void spi_select(void); -void spi_deselect(void); +void spi_select(int dev); +#define spi_deselect() spi_select(0x0) uint8_t spi_write_read_byte(uint8_t data); void spi_write_byte(uint8_t data); diff --git a/src/dev/spi.c b/src/dev/spi.c index d1f0789..e785642 100644 --- a/src/dev/spi.c +++ b/src/dev/spi.c @@ -4,12 +4,13 @@ #define SPI_CNTRL *(volatile uint8_t*)0x0151 #define SPI_STATUS *(volatile uint8_t*)0x0152 -#define SPI_CPOL 0 -#define SPI_CPHA 1 -#define SPI_SELECT 2 -#define SPI_CLK_DIV 3 +#define SPI_CPOL 0 +#define SPI_CPHA 1 +#define SPI_SELECT 2 +#define SPI_CLK_DIV 4 +#define SPI_SELECT_WIDTH (SPI_CLK_DIV - SPI_SELECT) -#define SPI_BUSY 0x01 +#define SPI_BUSY 0x01 void spi_init(SpiCpol cpol, SpiCpha cpha, unsigned int clk_div) { @@ -18,14 +19,10 @@ void spi_init(SpiCpol cpol, SpiCpha cpha, unsigned int clk_div) (clk_div << SPI_CLK_DIV); } -void spi_select() +void spi_select(int dev) { - SPI_CNTRL |= (1 << SPI_SELECT); -} - -void spi_deselect() -{ - SPI_CNTRL &= ~(1 << SPI_SELECT); + dev &= ((1 << SPI_SELECT_WIDTH) - 1); + SPI_CNTRL |= (dev << SPI_SELECT); } uint8_t spi_write_read_byte(uint8_t data)