From 28feb18a8f6d3dd2d0d78a40d4213407fb54e5dd Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Tue, 27 Apr 2010 18:30:46 -0400 Subject: Added a spi send block function --- libmaple/spi.c | 28 ++++++++++++++++++++++++++-- libmaple/spi.h | 3 ++- 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'libmaple') diff --git a/libmaple/spi.c b/libmaple/spi.c index ddc8346..4f34d1a 100644 --- a/libmaple/spi.c +++ b/libmaple/spi.c @@ -112,7 +112,7 @@ void spi_init(uint32 spi_num, * @param spi_num which spi to send on * @return data shifted back from the slave */ -void spi_tx(uint32 spi_num, uint8 data) { +uint8 spi_tx_byte(uint32 spi_num, uint8 data) { SPI *spi; ASSERT(spi_num == 1 || spi_num == 2); @@ -121,8 +121,32 @@ void spi_tx(uint32 spi_num, uint8 data) { spi->DR = data; - while (!(spi->SR & SR_TXE) || (spi->SR & SR_BSY)) + while (!(spi->SR & SR_TXE) || + (spi->SR & SR_BSY)) ; + + return spi->DR; +} + +uint8 spi_tx(uint32 spi_num, uint8 *buf, uint32 len) { + SPI *spi; + uint32 i; + + ASSERT(spi_num == 1 || spi_num == 2); + spi = (spi_num == 1) ? (SPI*)SPI1_BASE : (SPI*)SPI2_BASE; + + if (!len) { + return 0; + } + + while (i < len) { + spi->DR = buf[len]; + while (!(spi->SR & SR_TXE) || + (spi->SR & SR_BSY)) + ; + i++; + } + return spi->DR; } static void spi_gpio_cfg(const spi_dev *dev) { diff --git a/libmaple/spi.h b/libmaple/spi.h index cde3b42..25c2c6b 100644 --- a/libmaple/spi.h +++ b/libmaple/spi.h @@ -96,7 +96,8 @@ void spi_init(uint32 spi_num, uint32 prescale, uint32 endian, uint32 mode); -void spi_tx(uint32 spi_num, uint8 data); +uint8 spi_tx_byte(uint32 spi_num, uint8 data); +uint8 spi_tx(uint32 spi_num, uint8 *buf, uint32 len); static inline uint8 spi_rx(uint32 spi_num) { SPI *spi; -- cgit v1.2.3 From a7031b147c9ee77d1ed04ec419a7a23edd08014a Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Wed, 28 Apr 2010 19:19:34 -0400 Subject: Various SPI changes. -Read DR after each master send, return the response. -Added a send function to allow you to pass buffers to the SPI peripheral instead of goin a byte at a time. --- libmaple/spi.c | 11 +++++++---- wirish/comm/HardwareSPI.cpp | 8 ++++---- wirish/comm/HardwareSPI.h | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) (limited to 'libmaple') diff --git a/libmaple/spi.c b/libmaple/spi.c index 4f34d1a..aa75c5f 100644 --- a/libmaple/spi.c +++ b/libmaple/spi.c @@ -130,7 +130,8 @@ uint8 spi_tx_byte(uint32 spi_num, uint8 data) { uint8 spi_tx(uint32 spi_num, uint8 *buf, uint32 len) { SPI *spi; - uint32 i; + uint32 i = 0; + uint8 rc; ASSERT(spi_num == 1 || spi_num == 2); spi = (spi_num == 1) ? (SPI*)SPI1_BASE : (SPI*)SPI2_BASE; @@ -140,13 +141,15 @@ uint8 spi_tx(uint32 spi_num, uint8 *buf, uint32 len) { } while (i < len) { - spi->DR = buf[len]; + spi->DR = buf[i]; while (!(spi->SR & SR_TXE) || - (spi->SR & SR_BSY)) + (spi->SR & SR_BSY) || + !(spi->SR & SR_RXNE)) ; + rc = spi->DR; i++; } - return spi->DR; + return rc; } static void spi_gpio_cfg(const spi_dev *dev) { diff --git a/wirish/comm/HardwareSPI.cpp b/wirish/comm/HardwareSPI.cpp index 4379a0d..3dfe10a 100644 --- a/wirish/comm/HardwareSPI.cpp +++ b/wirish/comm/HardwareSPI.cpp @@ -118,12 +118,12 @@ void HardwareSPI::begin(void) { * @brief send a byte out the spi peripheral * @param data byte to send */ -void HardwareSPI::send(uint8 data) { - spi_tx_byte(this->spi_num, data); +uint8 HardwareSPI::send(uint8 data) { + return spi_tx_byte(this->spi_num, data); } -void HardwareSPI::send(uint8 *buf, uint32 len) { - spi_tx(this->spi_num, buf, len); +uint8 HardwareSPI::send(uint8 *buf, uint32 len) { + return spi_tx(this->spi_num, buf, len); } diff --git a/wirish/comm/HardwareSPI.h b/wirish/comm/HardwareSPI.h index 04d67ba..e606c0c 100644 --- a/wirish/comm/HardwareSPI.h +++ b/wirish/comm/HardwareSPI.h @@ -49,8 +49,8 @@ class HardwareSPI { HardwareSPI(uint32 spi_num); void begin(void); void begin(SPIFrequency freq, uint32 endianness, uint32 mode); - void send(uint8 data); - void send(uint8 *data, uint32 length); + uint8 send(uint8 data); + uint8 send(uint8 *data, uint32 length); uint8 recv(void); }; -- cgit v1.2.3