-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspi.c
361 lines (313 loc) · 8.06 KB
/
spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Licensed under the EUPL V.1.1, Lizenziert unter EUPL V.1.1
*/
/*
* spi.c
*
* Created on: 12.06.2013
* Author: Florian Brabetz, GSI, [email protected]
*/
#include <stdio.h>
#include "api.h"
#include "spi.h"
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
/*eclipse specific setting, not used during build process*/
#ifndef __AVR_AT90CAN128__
#include <avr/iocan128.h>
#endif
// struct arrays for storing data to transmit and received data
spiByteDataArray spiWriteData;
spiByteDataArray spiReadData;
// initial SPI configuration
spiConfigUnion spiStandardConfiguration = { .bits.bSpr = 0,
.bits.bCpha = 0,
.bits.bCpol = 0,
.bits.bMstr = 1,
.bits.bDord = 0,
.bits.bSpe = 0,
.bits.bSpie = 0,
.bits.bSpi2x = 0,
.bits.bWcol = 0,
.bits.bSpif = 0 };
// initial configuration for chipselectarray -> all chipselects are unused
spiPin spiChipSelectArray[SPI_CHIPSELECT_MAXIMUM] = { { 0 , 0 , false },
{ 0 , 0 , false },
{ 0 , 0 , false },
{ 0 , 0 , false },
{ 0 , 0 , false },
{ 0 , 0 , false },
{ 0 , 0 , false },
{ 0 , 0 , false } };
uint8_t spiInternalChipSelectMask = 0;
uint8_t spiGetChipSelectArrayStatus(void)
{
uint8_t status = 0, i = 0;
for (i = SPI_CHIPSELECT0; i < SPI_CHIPSELECT_MAXIMUM; i++)
{
if (spiChipSelectArray[i].isUsed)
{
status |= (1 << i);
}
}
return status;
}
uint8_t spiAddChipSelect(volatile uint8_t *ptrCurrentPort, uint8_t currentPinNumber, uint8_t chipSelectNumber)
{
uint8_t i;
if (SPI_CHIPSELECT_MAXIMUM <= chipSelectNumber)
{
/* cs number exceeds range */
CommunicationError_p(ERRA, SERIAL_ERROR_arguments_exceed_boundaries, true, NULL );
return 30;
}
if (!spiChipSelectArray[chipSelectNumber].isUsed && (ptrCurrentPort > (uint8_t *) 1))
{
spiChipSelectArray[chipSelectNumber].ptrPort = ptrCurrentPort;
spiChipSelectArray[chipSelectNumber].pinNumber = currentPinNumber;
spiChipSelectArray[chipSelectNumber].isUsed = true;
// initialize chipselect pins as output pins
for (i = 0; i < 8; i++)
{
// PINA address 0x00
// DDRA address 0x01
// PORTA address 0x02
if (spiChipSelectArray[i].isUsed == true)
{
// decrement address by one (now pointing to DDRx) and set the appropriate bit to one
// pin becomes an output pin
*(spiChipSelectArray[i].ptrPort - 1) |= (1 << spiChipSelectArray[i].pinNumber);
}
}
}
else
{
if (spiChipSelectArray[chipSelectNumber].isUsed)
{
// chipSelectNumber already in use
CommunicationError_p(ERRA, dynamicMessage_ErrorIndex, true, PSTR("chipSelect #%i already in use"),
chipSelectNumber + 1);
return 10;
}
if (ptrCurrentPort <= (uint8_t *) 1)
{
// invalid port address
CommunicationError_p(ERRA, SERIAL_ERROR_arguments_exceed_boundaries, true, NULL );
return 20;
}
}
spiSetChipSelectInMask(chipSelectNumber);
return 0;
}
uint8_t spiRemoveChipSelect(uint8_t chipSelectNumber)
{
if (SPI_CHIPSELECT_MAXIMUM <= chipSelectNumber)
{
/* cs number exceeds range */
CommunicationError_p(ERRA, SERIAL_ERROR_arguments_exceed_boundaries, true, NULL );
return 1;
}
else
{
if (spiChipSelectArray[chipSelectNumber].isUsed == true)
{
// decrement address by one (now pointing to DDRx) and set the appropriate bit to zero
// pin becomes an input pin again
*(spiChipSelectArray[chipSelectNumber].ptrPort - 1) &=
~(1 << spiChipSelectArray[chipSelectNumber].pinNumber);
}
spiChipSelectArray[chipSelectNumber].isUsed = false;
spiReleaseChipSelectInMask(chipSelectNumber);
}
return 0;
}
void spiInit(void)
{
uint8_t i;
// DDRB logic 1 -> pin configured as output
// set DDB2(MOSI) and DDB1(SCK) to output.
// DDB3(MISO) is controlled by SPI logic
DDRB |= (1 << DDB2) | (1 << DDB1);
SPCR = (spiStandardConfiguration.data & 0x00FF);
SPSR = ((spiStandardConfiguration.data >> 8) & 0x00FF);
for (i = SPI_CHIPSELECT0; i < SPI_CHIPSELECT_MAXIMUM; i++)
{
spiRemoveChipSelect(i);
}
spiAddChipSelect(&PORTB, PB0, SPI_CHIPSELECT0);
spiReleaseAllChipSelectLines();
spiPurgeWriteData();
spiPurgeReadData();
}
uint8_t spiWriteWithoutChipSelect(uint8_t data)
{
volatile uint8_t counter = 0;
SPDR = data;
while ((!(SPSR & (1 << SPIF))) && (counter < SPI_MAX_WAIT_COUNT))
{
counter++;
}
if (counter >= SPI_MAX_WAIT_COUNT)
{
return 10; // write fail
}
return 0; // write succeed
}
uint8_t spiWriteAndReadWithoutChipSelect(uint8_t byteOrder)
{
uint16_t i;
uint8_t returnValue = 0;
if ((spiWriteData.length + spiReadData.length) > (MAX_LENGTH_COMMAND >> 1))
{
return 20; // not enough space in readbuffer
}
if (SPI_MSBYTE_FIRST == byteOrder)
{
i = 0;
while ((i < spiWriteData.length) && !returnValue)
{
returnValue = spiWriteWithoutChipSelect(spiWriteData.data[i]);
spiReadData.data[i] = spiReadByte();
spiReadData.length++;
i++;
}
}
else
{
i = 1;
while ((i <= spiWriteData.length) && !returnValue)
{
returnValue = spiWriteWithoutChipSelect(spiWriteData.data[spiWriteData.length - i]);
spiReadData.data[spiWriteData.length - i] = spiReadByte();
spiReadData.length++;
i++;
}
}
return returnValue;
}
uint8_t spiWriteAndReadWithChipSelect(uint8_t byteOrder, uint8_t externalChipSelectMask)
{
uint8_t returnValue = 0;
spiSetChosenChipSelect(externalChipSelectMask);
returnValue = spiWriteAndReadWithoutChipSelect(byteOrder);
spiReleaseChosenChipSelect(externalChipSelectMask);
return returnValue;
}
uint8_t spiReadByte(void)
{
return SPDR ;
}
void spiReleaseAllChipSelectLines(void)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if (spiChipSelectArray[i].isUsed)
{
*(spiChipSelectArray[i].ptrPort) |= (1 << (spiChipSelectArray[i].pinNumber));
}
}
}
void spiReleaseChosenChipSelect(uint8_t externalChipSelectMask)
{
uint8_t i = 0;
for (i = 0; i < 8; i++)
{
if (((1 << i) & spiInternalChipSelectMask) & externalChipSelectMask)
{
*(spiChipSelectArray[i].ptrPort) |= (1 << spiChipSelectArray[i].pinNumber);
}
}
}
void spiSetChosenChipSelect(uint8_t externalChipSelectMask)
{
uint8_t i = 0;
//PORTB &= ~(1<<PB0); // selecting chip by putting the chip select line low
for (i = 0; i < 8; i++)
{
if (((1 << i) & spiInternalChipSelectMask) & externalChipSelectMask)
{
*(spiChipSelectArray[i].ptrPort) &= ~(1 << spiChipSelectArray[i].pinNumber);
}
}
}
void spiSetChipSelectInMask(uint8_t chipSelectNumber)
{
spiInternalChipSelectMask |= (1 << chipSelectNumber);
}
void spiReleaseChipSelectInMask(uint8_t chipSelectNumber)
{
spiInternalChipSelectMask &= ~(1 << chipSelectNumber);
}
uint8_t spiGetInternalChipSelectMask(void)
{
return spiInternalChipSelectMask;
}
spiByteDataArray spiGetReadData(void)
{
return spiReadData;
}
void spiPurgeWriteData(void)
{
spiWriteData.length = 0;
}
void spiPurgeReadData(void)
{
spiReadData.length = 0;
}
void spiSetConfiguration(spiConfigUnion newConfig)
{
SPSR = ((newConfig.data) >> 8) & 0x00FF;
SPCR = (newConfig.data) & 0xFF;
}
spiConfigUnion spiGetConfiguration(void)
{
spiConfigUnion currentConfig;
currentConfig.data = ((SPSR << 8) & 0xFF00) | (SPCR & 0x00FF);
return currentConfig;
}
void spiEnable(bool enable)
{
if (enable)
{
SPCR |= (1 << SPE);
}
else
{
SPCR &= ~(1 << SPE);
}
}
uint8_t spiGetCurrentChipSelectBarStatus(void)
{
uint8_t currentChipSelectBarStatus = 0;
uint8_t i;
for (i = SPI_CHIPSELECT0; i < SPI_CHIPSELECT_MAXIMUM; i++)
{
if (spiChipSelectArray[i].isUsed)
{
// PINA address 0x00
// DDRA address 0x01
// PORTA address 0x02
// decrement address by two (now pointing to DDRx) and set the appropriate bit to one
// pin becomes an output pin
if (*(spiChipSelectArray[i].ptrPort - 2) & (1 << spiChipSelectArray[i].pinNumber))
{
currentChipSelectBarStatus |= (1 << i);
}
}
}
return currentChipSelectBarStatus;
}
spiPin * spiGetCurrentChipSelectArray(void)
{
return spiChipSelectArray;
}
volatile uint8_t * spiGetPortFromChipSelect(uint8_t chipSelectNumber)
{
return spiChipSelectArray[chipSelectNumber].ptrPort;
}
uint8_t spiGetPinFromChipSelect(uint8_t chipSelectNumber)
{
return spiChipSelectArray[chipSelectNumber].pinNumber;
}