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