29 #define SPI_HAS_TRANSACTION 31 #define SPI_CLOCK_BASE 256000000 34 #define SPI_CLOCK_DIV1 BCM2835_SPI_CLOCK_DIVIDER_1 35 #define SPI_CLOCK_DIV2 BCM2835_SPI_CLOCK_DIVIDER_2 36 #define SPI_CLOCK_DIV4 BCM2835_SPI_CLOCK_DIVIDER_4 37 #define SPI_CLOCK_DIV8 BCM2835_SPI_CLOCK_DIVIDER_8 38 #define SPI_CLOCK_DIV16 BCM2835_SPI_CLOCK_DIVIDER_16 39 #define SPI_CLOCK_DIV32 BCM2835_SPI_CLOCK_DIVIDER_32 40 #define SPI_CLOCK_DIV64 BCM2835_SPI_CLOCK_DIVIDER_64 41 #define SPI_CLOCK_DIV128 BCM2835_SPI_CLOCK_DIVIDER_128 42 #define SPI_CLOCK_DIV256 BCM2835_SPI_CLOCK_DIVIDER_256 43 #define SPI_CLOCK_DIV512 BCM2835_SPI_CLOCK_DIVIDER_512 44 #define SPI_CLOCK_DIV1024 BCM2835_SPI_CLOCK_DIVIDER_1024 45 #define SPI_CLOCK_DIV2048 BCM2835_SPI_CLOCK_DIVIDER_2048 46 #define SPI_CLOCK_DIV4096 BCM2835_SPI_CLOCK_DIVIDER_4096 47 #define SPI_CLOCK_DIV8192 BCM2835_SPI_CLOCK_DIVIDER_8192 48 #define SPI_CLOCK_DIV16384 BCM2835_SPI_CLOCK_DIVIDER_16384 49 #define SPI_CLOCK_DIV32768 BCM2835_SPI_CLOCK_DIVIDER_32768 50 #define SPI_CLOCK_DIV65536 BCM2835_SPI_CLOCK_DIVIDER_65536 53 #define SPI_MODE0 BCM2835_SPI_MODE0 54 #define SPI_MODE1 BCM2835_SPI_MODE1 55 #define SPI_MODE2 BCM2835_SPI_MODE2 56 #define SPI_MODE3 BCM2835_SPI_MODE3 58 #define LSBFIRST BCM2835_SPI_BIT_ORDER_LSBFIRST 59 #define MSBFIRST BCM2835_SPI_BIT_ORDER_MSBFIRST 61 const uint8_t SS = 24;
62 const uint8_t MOSI = 19;
63 const uint8_t MISO = 21;
64 const uint8_t SCK = 23;
80 init(SPI_CLOCK_DIV32, MSBFIRST, SPI_MODE0);
93 if (clock >= SPI_CLOCK_BASE) {
94 divider = SPI_CLOCK_DIV1;
95 }
else if (clock >= SPI_CLOCK_BASE / 2) {
96 divider = SPI_CLOCK_DIV2;
97 }
else if (clock >= SPI_CLOCK_BASE / 4) {
98 divider = SPI_CLOCK_DIV4;
99 }
else if (clock >= SPI_CLOCK_BASE / 8) {
100 divider = SPI_CLOCK_DIV8;
101 }
else if (clock >= SPI_CLOCK_BASE / 16) {
102 divider = SPI_CLOCK_DIV16;
103 }
else if (clock >= SPI_CLOCK_BASE / 32) {
104 divider = SPI_CLOCK_DIV32;
105 }
else if (clock >= SPI_CLOCK_BASE / 64) {
106 divider = SPI_CLOCK_DIV64;
107 }
else if (clock >= SPI_CLOCK_BASE / 128) {
108 divider = SPI_CLOCK_DIV128;
109 }
else if (clock >= SPI_CLOCK_BASE / 256) {
110 divider = SPI_CLOCK_DIV256;
111 }
else if (clock >= SPI_CLOCK_BASE / 512) {
112 divider = SPI_CLOCK_DIV512;
113 }
else if (clock >= SPI_CLOCK_BASE / 1024) {
114 divider = SPI_CLOCK_DIV1024;
115 }
else if (clock >= SPI_CLOCK_BASE / 2048) {
116 divider = SPI_CLOCK_DIV2048;
117 }
else if (clock >= SPI_CLOCK_BASE / 4096) {
118 divider = SPI_CLOCK_DIV4096;
119 }
else if (clock >= SPI_CLOCK_BASE / 8192) {
120 divider = SPI_CLOCK_DIV8192;
121 }
else if (clock >= SPI_CLOCK_BASE / 16384) {
122 divider = SPI_CLOCK_DIV16384;
123 }
else if (clock >= SPI_CLOCK_BASE / 32768) {
124 divider = SPI_CLOCK_DIV32768;
125 }
else if (clock >= SPI_CLOCK_BASE / 65536) {
126 divider = SPI_CLOCK_DIV65536;
129 divider = SPI_CLOCK_DIV32;
132 init(divider, bitOrder, dataMode);
147 void init(uint16_t divider, uint8_t bitOrder, uint8_t dataMode)
170 inline static uint8_t transfer(uint8_t data);
178 inline static void transfernb(
char* tbuf,
char* rbuf, uint32_t len);
185 inline static void transfern(
char* buf, uint32_t len);
199 static void setBitOrder(uint8_t bit_order);
205 static void setDataMode(uint8_t data_mode);
211 static void setClockDivider(uint16_t divider);
217 static void chipSelect(
int csn_pin);
223 static void beginTransaction(
SPISettings settings);
227 static void endTransaction();
233 static void usingInterrupt(uint8_t interruptNumber);
239 static void notUsingInterrupt(uint8_t interruptNumber);
242 static uint8_t initialized;
257 transfernb(buf, buf, len);
static void transfernb(char *tbuf, char *rbuf, uint32_t len)
Send and receive a number of bytes.
Definition: SPI.h:250
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
SPISettings constructor.
Definition: SPI.h:89
static uint8_t transfer(uint8_t data)
Send and receive a byte.
Definition: SPI.h:245
uint8_t bcm2835_spi_transfer(uint8_t value)
SPISettings()
SPISettings constructor.
Definition: SPI.h:78
uint8_t border
SPI bit order.
Definition: SPI.h:136
uint16_t cdiv
SPI clock divider.
Definition: SPI.h:135
void bcm2835_spi_transfernb(char *tbuf, char *rbuf, uint32_t len)
uint8_t dmode
SPI data mode.
Definition: SPI.h:137
uint32_t clock
SPI clock.
Definition: SPI.h:86
static void transfern(char *buf, uint32_t len)
Send and receive a number of bytes.
Definition: SPI.h:255