-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftspi.c
167 lines (140 loc) · 5.29 KB
/
softspi.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
////////////////////////////////////////////////////////////////////////////////
/// @file
/// @brief Soft Serial Peripheral Interface (SPI) driver.
////////////////////////////////////////////////////////////////////////////////
// *****************************************************************************
// ************************** System Include Files *****************************
// *****************************************************************************
// *****************************************************************************
// ************************** User Include Files *******************************
// *****************************************************************************
#include "tcpip/tcpip.h"
#include "system.h"
#include "softspi.h"
// *****************************************************************************
// ************************** Macros *******************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Defines ******************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Definitions **************************************
// *****************************************************************************
static volatile uint32_t spiTxBuf;
static volatile uint32_t *spiRxBuf;
static volatile int spiXferBits;
static volatile int *completeFlag;
// *****************************************************************************
// ************************** Function Prototypes ******************************
// *****************************************************************************
// *****************************************************************************
// ************************** Global Functions *********************************
// *****************************************************************************
int SoftSPIXfer(SOFTSPI_TRANSFER *xfer)
{
// Sanitize the inputs. -----------------------------------------
if ((xfer->numOfBits < 1) || (xfer->numOfBits > 32))
{
// Invalid number of bits to transfer.
return -1;
}
if ((xfer->freq < 0) || (xfer->freq > (GetPeripheralClock() / 2)))
{
// Invalid SPI clock.
return -1;
}
// Take control of software SPI bus if it's available. ----------
if (mT5GetIntEnable())
{
return -1; // Software SPI bus is busy.
}
// Assert slave select line for desired bus device.
switch (xfer->dev)
{
case SOFTSPI_KSZ8895: // Micrel KSZ8895
{
mSetSpiSsLow_KSZ8895();
break;
}
}
// Configure the software SPI timer for desired clock frequency.
if (GetPeripheralClock() % (xfer->freq * 2))
{
// No perfect frequency divisor possible.
// Round up to next period number to be conservative.
WritePeriod5((GetPeripheralClock() / (xfer->freq * 2)) + 1);
}
else
{
// Perfect frequency match.
WritePeriod5(GetPeripheralClock() / (xfer->freq * 2));
}
// Clear the receive buffer.
if (xfer->rxBuf != 0)
{
*xfer->rxBuf = 0;
}
// Set global buffer pointers and bit counter.
spiTxBuf = xfer->txDat;
spiRxBuf = xfer->rxBuf;
spiXferBits = xfer->numOfBits;
completeFlag = &xfer->xferDone;
// Clear the xferDone flag before initiating transfer.
xfer->xferDone = 0;
TMR5 = 0;
mT5ClearIntFlag();
mT5IntEnable(1); // Initiate software SPI transfer.
return 0;
}
// *****************************************************************************
// ************************** Static Functions *********************************
// *****************************************************************************
////////////////////////////////////////////////////////////////////////////////
/// @brief Period soft SPI interrupt.
///
/// This function implements the big-bang operation to perform the SPI
/// protocol.
////////////////////////////////////////////////////////////////////////////////
void __ISR (_TIMER_5_VECTOR, IPL7SRS) Timer5ISR(void)
{
static int spiState = 0;
if (spiState < (2 * spiXferBits))
{
if ((spiState % 2) == 0)
{
// Even state.
mSetSpiClkLow();
if ((spiTxBuf >> (spiXferBits - (spiState / 2) - 1)) & 0x0001)
{
mSetSpiMosiHigh();
}
else
{
mSetSpiMosiLow();
}
}
else
{
// Odd state.
mSetSpiClkHigh();
if (spiRxBuf != 0)
{
*spiRxBuf |=
(mGetSpiMiso() << (spiXferBits - (spiState / 2) - 1));
}
}
spiState++;
}
else
{
mSetSpiClkHigh();
spiState = 0;
mT5IntEnable(0); // Disable Timer 5 interrupts.
// De-assert slave select lines.
mSetSpiSsHigh_KSZ8895();
// Set transfer complete flag.
*completeFlag = 1;
}
mT5ClearIntFlag();
return;
}