Soldered SD Card Arduino Library 1.0.0
Easily read and write files to and form the SD card breakout! A fork of the original SDFat library by Bill Greiman.
Loading...
Searching...
No Matches
SdSpiBareUnoDriver.h
Go to the documentation of this file.
1
25#ifndef SdSpiBareUnoDriver_h
26#define SdSpiBareUnoDriver_h
32#include "../common/SysCall.h"
33#include <avr/interrupt.h>
34#define nop asm volatile("nop\n\t")
35#ifndef HIGH
36#define HIGH 1
37#endif // HIGH
38#ifndef LOW
39#define LOW 0
40#endif // LOW
41#ifndef INPUT
42#define INPUT 0
43#endif // INPUT
44#ifndef OUTPUT
45#define OUTPUT 1
46#endif // OUTPUT
47
48inline uint8_t unoBit(uint8_t pin)
49{
50 return 1 << (pin < 8 ? pin : pin < 14 ? pin - 8 : pin - 14);
51}
52inline uint8_t unoDigitalRead(uint8_t pin)
53{
54 volatile uint8_t *reg = pin < 8 ? &PIND : pin < 14 ? &PINB : &PINC;
55 return *reg & unoBit(pin);
56}
57inline void unoDigitalWrite(uint8_t pin, uint8_t value)
58{
59 volatile uint8_t *port = pin < 8 ? &PORTD : pin < 14 ? &PORTB : &PORTC;
60 uint8_t bit = unoBit(pin);
61 cli();
62 if (value)
63 {
64 *port |= bit;
65 }
66 else
67 {
68 *port &= ~bit;
69 }
70 sei();
71}
72
73inline void unoPinMode(uint8_t pin, uint8_t mode)
74{
75 uint8_t bit = unoBit(pin);
76 volatile uint8_t *reg = pin < 8 ? &DDRD : pin < 14 ? &DDRB : &DDRC;
77
78 cli();
79 if (mode == OUTPUT)
80 {
81 *reg |= bit;
82 }
83 else
84 {
85 *reg &= ~bit;
86 // handle INPUT pull-up
87 unoDigitalWrite(pin, mode != INPUT);
88 }
89 sei();
90}
91
92#define UNO_SS 10
93#define UNO_MOSI 11
94#define UNO_MISO 12
95#define UNO_SCK 13
96//------------------------------------------------------------------------------
102{
103 public:
105 void activate()
106 {
107 }
109 void end()
110 {
111 }
114 {
115 }
120 void begin(SdSpiConfig spiConfig)
121 {
122 m_csPin = spiConfig.csPin;
123 unoPinMode(m_csPin, OUTPUT);
125 unoDigitalWrite(UNO_SS, HIGH);
126 unoPinMode(UNO_SS, OUTPUT);
127 SPCR |= _BV(MSTR);
128 SPCR |= _BV(SPE);
129 SPSR = 0;
130 unoPinMode(UNO_SCK, OUTPUT);
131 unoPinMode(UNO_MOSI, OUTPUT);
132 }
137 uint8_t receive()
138 {
139 return transfer(0XFF);
140 }
148 uint8_t receive(uint8_t *buf, size_t count)
149 {
150 if (count == 0)
151 {
152 return 0;
153 }
154 uint8_t *pr = buf;
155 SPDR = 0XFF;
156 while (--count > 0)
157 {
158 while (!(SPSR & _BV(SPIF)))
159 {
160 }
161 uint8_t in = SPDR;
162 SPDR = 0XFF;
163 *pr++ = in;
164 // nops to optimize loop for 16MHz CPU 8 MHz SPI
165 nop;
166 nop;
167 }
168 while (!(SPSR & _BV(SPIF)))
169 {
170 }
171 *pr = SPDR;
172 return 0;
173 }
178 void send(uint8_t data)
179 {
180 transfer(data);
181 }
187 void send(const uint8_t *buf, size_t count)
188 {
189 if (count == 0)
190 {
191 return;
192 }
193 SPDR = *buf++;
194 while (--count > 0)
195 {
196 uint8_t b = *buf++;
197 while (!(SPSR & (1 << SPIF)))
198 {
199 }
200 SPDR = b;
201 // nops to optimize loop for 16MHz CPU 8 MHz SPI
202 nop;
203 nop;
204 }
205 while (!(SPSR & (1 << SPIF)))
206 {
207 }
208 }
210 void select()
211 {
213 }
218 void setSckSpeed(uint32_t maxSck)
219 {
220 (void)maxSck;
221 SPSR |= 1 << SPI2X;
222 }
223 static uint8_t transfer(uint8_t data)
224 {
225 SPDR = data;
226 while (!(SPSR & _BV(SPIF)))
227 {
228 } // wait
229 return SPDR;
230 }
232 void unselect()
233 {
235 }
236
237 private:
239};
240#endif // SdSpiBareUnoDriver_h
uint8_t SdCsPin_t
Definition SdFatConfig.h:139
void unoDigitalWrite(uint8_t pin, uint8_t value)
Definition SdSpiBareUnoDriver.h:57
uint8_t unoDigitalRead(uint8_t pin)
Definition SdSpiBareUnoDriver.h:52
uint8_t unoBit(uint8_t pin)
Definition SdSpiBareUnoDriver.h:48
void unoPinMode(uint8_t pin, uint8_t mode)
Definition SdSpiBareUnoDriver.h:73
SPI card configuration.
Definition SdSpiDriver.h:112
const SdCsPin_t csPin
Definition SdSpiDriver.h:152
Optimized SPI class for access to SD and SDHC flash memory cards.
Definition SdSpiBareUnoDriver.h:102
void send(uint8_t data)
Definition SdSpiBareUnoDriver.h:178
void activate()
Definition SdSpiBareUnoDriver.h:105
void deactivate()
Definition SdSpiBareUnoDriver.h:113
void setSckSpeed(uint32_t maxSck)
Definition SdSpiBareUnoDriver.h:218
static uint8_t transfer(uint8_t data)
Definition SdSpiBareUnoDriver.h:223
void end()
Definition SdSpiBareUnoDriver.h:109
uint8_t receive(uint8_t *buf, size_t count)
Definition SdSpiBareUnoDriver.h:148
void unselect()
Definition SdSpiBareUnoDriver.h:232
SdCsPin_t m_csPin
Definition SdSpiBareUnoDriver.h:238
void select()
Definition SdSpiBareUnoDriver.h:210
uint8_t receive()
Definition SdSpiBareUnoDriver.h:137
void send(const uint8_t *buf, size_t count)
Definition SdSpiBareUnoDriver.h:187
void begin(SdSpiConfig spiConfig)
Definition SdSpiBareUnoDriver.h:120
static bool value
Definition DigitalPin.h:210
static uint8_t mode
Definition DigitalPin.h:186