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
|
/******************************************************************************
* @brief providing APIs for configuring SPI module (SPI).
*
*******************************************************************************
*
* provide APIs for configuring SPI module (SPI).
******************************************************************************/
#include "common.h"
#include "spi.h"
/******************************************************************************
* Local variables
******************************************************************************/
SPI_CallbackType SPI_Callback[MAX_SPI_NO] = {(SPI_CallbackType)NULL};
/******************************************************************************
* Local function prototypes
******************************************************************************/
/******************************************************************************
* Local functions
*****************************************************************************/
/******************************************************************************
* Global functions
******************************************************************************/
/******************************************************************************
* define SPI APIs
*
*//*! @addtogroup spi_api_list
* @{
*******************************************************************************/
/*****************************************************************************//*!
*
* @brief initialize SPI as per params.
*
* @param[in] pSPI point to SPI module type.
* @param[in] pConfig point to configuration parameters.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void SPI_Init(SPI_Type *pSPI, SPI_ConfigType *pConfig)
{
#if defined(CPU_NV32M3)
/* sanity check */
ASSERT((pSPI == SPI0));
SIM->SCGC |= SIM_SCGC_SPI0_MASK;
#else
/* sanity check */
ASSERT((pSPI == SPI0) || (pSPI == SPI1));
/* enable SPI clock gating on */
if( pSPI == SPI0)
{
SIM->SCGC |= SIM_SCGC_SPI0_MASK;
}
else
{
SIM->SCGC |= SIM_SCGC_SPI1_MASK;
}
#endif
/* configure other control bits */
if( pConfig->sSettings.bIntEn)
{
SPI_IntEnable(pSPI);
#if defined(CPU_NV32M3)
NVIC_EnableIRQ(SPI0_IRQn);
#else
if( pSPI == SPI0 )
{
NVIC_EnableIRQ(SPI0_IRQn);
}
else
{
NVIC_EnableIRQ(SPI1_IRQn);
}
#endif
}
if( pConfig->sSettings.bTxIntEn)
{
SPI_TxIntEnable(pSPI);
#if defined(CPU_NV32M3)
NVIC_EnableIRQ(SPI0_IRQn);
#else
if( pSPI == SPI0 )
{
NVIC_EnableIRQ(SPI0_IRQn);
}
else
{
NVIC_EnableIRQ(SPI1_IRQn);
}
#endif
}
if( pConfig->sSettings.bMasterMode)
{
SPI_SetMasterMode(pSPI);
}
else
{
SPI_SetSlaveMode(pSPI);
}
if( pConfig->sSettings.bClkPolarityLow)
{
SPI_SetClockPol(pSPI,1);
}
if( pConfig->sSettings.bClkPhase1)
{
SPI_SetClockPhase(pSPI,1);
}else
{
SPI_SetClockPhase(pSPI,0);
}
if( pConfig->sSettings.bShiftLSBFirst)
{
SPI_SetLSBFirst(pSPI);
}
if( pConfig->sSettings.bMatchIntEn)
{
SPI_MatchIntEnable(pSPI);
}
if( pConfig->sSettings.bModeFaultEn)
{
SPI_ModfEnable(pSPI);
}
if( pConfig->sSettings.bMasterAutoDriveSS)
{
/* set both SSOE and MODFEN bits when auto drive slave SS is enabled */
SPI_SSOutputEnable(pSPI);
SPI_ModfEnable(pSPI);
}
if( pConfig->sSettings.bPinAsOuput)
{
SPI_BidirPinEnable(pSPI);
}
if( pConfig->sSettings.bBidirectionModeEn)
{
SPI_BidirOutEnable(pSPI);
}
if( pConfig->sSettings.bStopInWaitMode)
{
SPI_ClockStopEnable(pSPI);
}
if(pConfig->sSettings.bMasterMode)
{
SPI_SetBaudRate(pSPI,pConfig->u32BusClkHz,pConfig->u32BitRate);
}
/* enable SPI module */
if( pConfig->sSettings.bModuleEn)
{
SPI_Enable(pSPI);
}
}
/*****************************************************************************//*!
*
* @brief SPI set band rate.
*
* @param[in] pSPI point to SPI module type.
* @param[in] u32BusClock Bus clock.
* @param[in] u32Bps set spi's baudrate.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void SPI_SetBaudRate(SPI_Type *pSPI,uint32_t u32BusClock,uint32_t u32Bps)
{
uint32_t u32BitRateDivisor;
uint8_t u8Sppr;
uint8_t u8Spr;
uint8_t u8ReadFlag;
u32BitRateDivisor = u32BusClock/u32Bps; /* calculate bit rate divisor */
u8ReadFlag = 0;
/* find best fit SPPR and SPR */
for (u8Spr = 0; u8Spr <= 8; u8Spr++)
{
for(u8Sppr = 0; u8Sppr <= 7; u8Sppr++)
{
if((u32BitRateDivisor>>(u8Spr+1))<=(u8Sppr+1))
{
u8ReadFlag = 1;
break;
}
}
if(u8ReadFlag)
{
break;
}
}
if(u8Sppr >=8)
{
u8Sppr = 7;
}
if(u8Spr >8)
{
u8Spr = 8;
}
/* set bit rate */
pSPI->BR = SPI_BR_SPPR(u8Sppr) | SPI_BR_SPR(u8Spr);
}
/*****************************************************************************//*!
*
* @brief implement write data to SPI.
*
* @param[in] pSPI pointer to SPI module type.
* @param[in] pWrBuff -- write data buffer pointer.
* @param[in] uiLength -- read/write data length.
* @param[out] pRdBuff -- read data buffer pointer.
*
* @return if <0, means error, 0: success.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
ResultType SPI_TransferWait(SPI_Type *pSPI, SPI_WidthType* pRdBuff, SPI_WidthType *pWrBuff,uint32 uiLength)
{
ResultType err = SPI_ERR_SUCCESS;
uint32_t i;
if(!uiLength)
{
return (err);
}
for(i = 0; i < uiLength; i++)
{
while(!SPI_IsSPTEF(pSPI));
SPI_WriteDataReg(pSPI,pWrBuff[i]);
while(!SPI_IsSPRF(pSPI));
pRdBuff[i] = SPI_ReadDataReg(pSPI);
}
return (err);
}
/*****************************************************************************//*!
*
* @brief Deinitialize SPI to the default state (reset value).
*
* @param[in] pSPI pointer to SPI module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void SPI_DeInit(SPI_Type *pSPI)
{
int16 i;
pSPI->C1 = SPI_C1_DEFAULT;
pSPI->C2 = SPI_C2_DEFAULT;
pSPI->BR = SPI_BR_DEFAULT;
pSPI->M = SPI_M_DEFAULT;
for(i = 0; i<100; i++); /* wait for some cycles for the ISR exit */
}
/*****************************************************************************//*!
*
* @brief set up SPI callback routines to be called by interrupt service routine.
*
* @param[in] pSPI pointer to SPI module type.
* @param[in] pfnCallback callback routine.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void SPI_SetCallback(SPI_Type *pSPI,SPI_CallbackType pfnCallback)
{
uint32_t u32Port = ((uint32_t)pSPI-(uint32_t)SPI0)>>12;
ASSERT(u32Port <2);
SPI_Callback[u32Port] = pfnCallback;
}
/*! @} End of spi_api_list */
/*****************************************************************************//*!
*
* @brief SPI0 interrupt service routine.
*
* @param none.
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void SPI0_Isr(void)
{
if( SPI_Callback[0] )
{
SPI_Callback[0]();
}
}
#ifndef CPU_NV32M3
/*****************************************************************************//*!
*
* @brief SPI1 interrupt service routine.
*
* @param none.
* @return none.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SPI1_Isr(void)
{
if( SPI_Callback[1] )
{
SPI_Callback[1]();
}
}
#endif
|