34 #define nop asm volatile ("nop\n\t")
37 #define MISO_MODE INPUT
39 #define MISO_LEVEL false
41 #define MOSI_MODE OUTPUT
43 #define SCK_MODE OUTPUT
49 template<u
int8_t MisoPin, u
int8_t MosiPin, u
int8_t SckPin, u
int8_t Mode = 0>
84 void send(uint8_t data)
101 uint8_t transfer(uint8_t txData)
104 transferBit(7, &rxData, txData);
105 transferBit(6, &rxData, txData);
106 transferBit(5, &rxData, txData);
107 transferBit(4, &rxData, txData);
108 transferBit(3, &rxData, txData);
109 transferBit(2, &rxData, txData);
110 transferBit(1, &rxData, txData);
111 transferBit(0, &rxData, txData);
118 bool MODE_CPHA(uint8_t mode)
120 return (mode & 1) != 0;
123 bool MODE_CPOL(uint8_t mode)
125 return (mode & 2) != 0;
128 void receiveBit(uint8_t bit, uint8_t* data)
130 if (MODE_CPHA(Mode)) {
131 fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
135 fastDigitalWrite(SckPin,
136 MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
137 if (fastDigitalRead(MisoPin)) {
140 if (!MODE_CPHA(Mode)) {
141 fastDigitalWrite(SckPin, MODE_CPOL(Mode));
146 void sendBit(uint8_t bit, uint8_t data)
148 if (MODE_CPHA(Mode)) {
149 fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
151 fastDigitalWrite(MosiPin, data & (1 << bit));
152 fastDigitalWrite(SckPin,
153 MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
156 if (!MODE_CPHA(Mode)) {
157 fastDigitalWrite(SckPin, MODE_CPOL(Mode));
162 void transferBit(uint8_t bit, uint8_t* rxData, uint8_t txData)
164 if (MODE_CPHA(Mode)) {
165 fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
167 fastDigitalWrite(MosiPin, txData & (1 << bit));
168 fastDigitalWrite(SckPin,
169 MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
170 if (fastDigitalRead(MisoPin)) {
173 if (!MODE_CPHA(Mode)) {
174 fastDigitalWrite(SckPin, MODE_CPOL(Mode));