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
SoftSPI.h
Go to the documentation of this file.
1/* Arduino DigitalIO Library
2 * Copyright (C) 2013 by William Greiman
3 *
4 * This file is part of the Arduino DigitalIO Library
5 *
6 * This Library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with the Arduino DigitalIO Library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
28#ifndef SoftSPI_h
29#define SoftSPI_h
30#include "DigitalPin.h"
31//------------------------------------------------------------------------------
33#define nop asm volatile("nop\n\t")
34//------------------------------------------------------------------------------
36#define MISO_MODE INPUT
38#define MISO_LEVEL false
40#define MOSI_MODE OUTPUT
42#define SCK_MODE OUTPUT
43//------------------------------------------------------------------------------
48template <uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin, uint8_t Mode = 0> class SoftSPI
49{
50 public:
51 //----------------------------------------------------------------------------
53 void begin()
54 {
55 fastPinConfig(MisoPin, MISO_MODE, MISO_LEVEL);
56 fastPinConfig(MosiPin, MOSI_MODE, !MODE_CPHA(Mode));
57 fastPinConfig(SckPin, SCK_MODE, MODE_CPOL(Mode));
58 }
59 //----------------------------------------------------------------------------
63 inline __attribute__((always_inline)) uint8_t receive()
64 {
65 uint8_t data = 0;
66 receiveBit(7, &data);
67 receiveBit(6, &data);
68 receiveBit(5, &data);
69 receiveBit(4, &data);
70 receiveBit(3, &data);
71 receiveBit(2, &data);
72 receiveBit(1, &data);
73 receiveBit(0, &data);
74 return data;
75 }
76 //----------------------------------------------------------------------------
80 inline __attribute__((always_inline)) void send(uint8_t data)
81 {
82 sendBit(7, data);
83 sendBit(6, data);
84 sendBit(5, data);
85 sendBit(4, data);
86 sendBit(3, data);
87 sendBit(2, data);
88 sendBit(1, data);
89 sendBit(0, data);
90 }
91 //----------------------------------------------------------------------------
96 inline __attribute__((always_inline)) uint8_t transfer(uint8_t txData)
97 {
98 uint8_t rxData = 0;
99 transferBit(7, &rxData, txData);
100 transferBit(6, &rxData, txData);
101 transferBit(5, &rxData, txData);
102 transferBit(4, &rxData, txData);
103 transferBit(3, &rxData, txData);
104 transferBit(2, &rxData, txData);
105 transferBit(1, &rxData, txData);
106 transferBit(0, &rxData, txData);
107 return rxData;
108 }
109
110 private:
111 //----------------------------------------------------------------------------
112 inline __attribute__((always_inline)) bool MODE_CPHA(uint8_t mode)
113 {
114 return (mode & 1) != 0;
115 }
116 inline __attribute__((always_inline)) bool MODE_CPOL(uint8_t mode)
117 {
118 return (mode & 2) != 0;
119 }
120 inline __attribute__((always_inline)) void receiveBit(uint8_t bit, uint8_t *data)
121 {
122 if (MODE_CPHA(Mode))
123 {
124 fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
125 }
126 nop;
128 fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
129 if (fastDigitalRead(MisoPin))
130 *data |= 1 << bit;
131 if (!MODE_CPHA(Mode))
132 {
133 fastDigitalWrite(SckPin, MODE_CPOL(Mode));
134 }
135 }
136 //----------------------------------------------------------------------------
137 inline __attribute__((always_inline)) void sendBit(uint8_t bit, uint8_t data)
138 {
139 if (MODE_CPHA(Mode))
140 {
141 fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
142 }
143 fastDigitalWrite(MosiPin, data & (1 << bit));
144 fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
145 nop;
146 nop;
147 if (!MODE_CPHA(Mode))
148 {
149 fastDigitalWrite(SckPin, MODE_CPOL(Mode));
150 }
151 }
152 //----------------------------------------------------------------------------
153 inline __attribute__((always_inline)) void transferBit(uint8_t bit, uint8_t *rxData, uint8_t txData)
154 {
155 if (MODE_CPHA(Mode))
156 {
157 fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
158 }
159 fastDigitalWrite(MosiPin, txData & (1 << bit));
160 fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
161 if (fastDigitalRead(MisoPin))
162 *rxData |= 1 << bit;
163 if (!MODE_CPHA(Mode))
164 {
165 fastDigitalWrite(SckPin, MODE_CPOL(Mode));
166 }
167 }
168 //----------------------------------------------------------------------------
169};
170#endif // SoftSPI_h
Fast Digital Pin functions.
Fast software SPI.
Definition SoftSPI.h:49
__attribute__((always_inline)) uint8_t transfer(uint8_t txData)
Definition SoftSPI.h:96
nop
Definition SoftSPI.h:127
__attribute__((always_inline)) void send(uint8_t data)
Definition SoftSPI.h:80
__attribute__((always_inline)) void receiveBit(uint8_t bit
__attribute__((always_inline)) uint8_t receive()
Definition SoftSPI.h:63
uint8_t uint8_t txData
Definition SoftSPI.h:154
fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) :!MODE_CPOL(Mode))
uint8_t * data
Definition SoftSPI.h:121
void begin()
Definition SoftSPI.h:53
__attribute__((always_inline)) void transferBit(uint8_t bit
uint8_t * rxData
Definition SoftSPI.h:153
__attribute__((always_inline)) bool MODE_CPOL(uint8_t mode)
Definition SoftSPI.h:116
__attribute__((always_inline)) void sendBit(uint8_t bit
__attribute__((always_inline)) bool MODE_CPHA(uint8_t mode)
Definition SoftSPI.h:112
static uint8_t mode
Definition DigitalPin.h:186