spi_bitbang.c

Go to the documentation of this file.
00001 
00043 #include <cpu/irq.h>
00044 #include <cfg/module.h>
00045 #include "spi_bitbang.h"
00046 #include "hw_spi.h"
00047 
00048 void spi_assertSS(void)
00049 {
00050     ATOMIC(SS_ACTIVE());
00051 }
00052 
00053 void spi_deassertSS(void)
00054 {
00055     ATOMIC(SS_INACTIVE());
00056 }
00057 
00062 uint8_t spi_sendRecv(uint8_t c)
00063 {
00064     uint8_t data = 0;
00065     uint8_t shift = SPI_DATAORDER_START;
00066 
00067 
00068     ATOMIC(
00069         for (int i = 0; i < 8; i++)
00070         {
00071             /* Shift the i-th bit to MOSI */
00072             if (c & shift)
00073                 MOSI_HIGH();
00074             else
00075                 MOSI_LOW();
00076             /* Assert clock */
00077             SCK_ACTIVE();
00078             data |= IS_MISO_HIGH() ? shift : 0;
00079             /* De-assert clock */
00080             SCK_INACTIVE();
00081             SPI_DATAORDER_SHIFT(shift);
00082         }
00083     );
00084     return data;
00085 }
00086 
00087 MOD_DEFINE(spi);
00088 void spi_init(void)
00089 {
00090     ATOMIC(SPI_HW_INIT());
00091     MOD_INIT(spi);
00092 }
00093 
00097 void spi_read(void *_buff, size_t len)
00098 {
00099     uint8_t *buff = (uint8_t *)_buff;
00100 
00101     while (len--)
00102         /* Read byte from spi and put it in buffer. */
00103         *buff++ = spi_sendRecv(0);
00104 
00105 }
00106 
00110 void spi_write(const void *_buff, size_t len)
00111 {
00112     const uint8_t *buff = (const uint8_t *)_buff;
00113 
00114     while (len--)
00115         /* Write byte pointed at by *buff to spi */
00116         spi_sendRecv(*buff++);
00117 
00118 }