-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi.c
243 lines (209 loc) · 5.35 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
/************************************************************************/
/* */
/* spi.c -- SPI Interface Definitions */
/* */
/************************************************************************/
/* Author: Michael T. Alexander */
/* Copyright 2009, Digilent Inc. */
/************************************************************************/
/* File Description: */
/* */
/* This module contains interface declarations for SPI communication. */
/* */
/************************************************************************/
/* Revision History: */
/* */
/* 05/21/2009 (MichaelA): created */
/* */
/************************************************************************/
/* ------------------------------------------------------------ */
/* Include File Definitions */
/* ------------------------------------------------------------ */
#include <plib.h>
#include "config.h"
#include "spi.h"
/* ------------------------------------------------------------ */
/* Local Type Definitions */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Global Variables */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Local Variables */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Forward Declarations */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Procedure Definitions */
/* ------------------------------------------------------------ */
/*** SpiInit
**
** Parameters:
** none
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Initialize the SPI interface for use.
**
*/
void SpiInit()
{
// Configure SS pin as digital output.
trisSpiSsClr = ( 1 << bnSpiSs );
// Default SS pin to disabled.
prtSpiSsSet = ( 1 << bnSpiSs );
/* Implementation using SPI2 hardware interface.
*/
//SPI2BRG = 1; // Fsck = 2 MHz @ Fpb = 8 Mhz
//SPI2BRG = 31; // Fsck = 125 kHz @ Fpb = 8 Mhz
SPI2BRG = 7; // Fsck = 500 KHz assuming Fpb = 8 MHz
SPI2CON = ( 1 << bnSpi2On ) | ( 1 << bnSpi2Smp ) | ( 1 << bnSpi2Cke ) | ( 1 << bnSpi2Msten );
}
/* ------------------------------------------------------------ */
/*** SpiEnable
**
** Parameters:
** none
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Enable SPI slave device.
*/
void SpiEnable()
{
prtSpiSsClr = ( 1 << bnSpiSs );
}
/* ------------------------------------------------------------ */
/*** SpiDisable
**
** Parameters:
** none
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Disable SPI slave device.
*/
void SpiDisable()
{
prtSpiSsSet = ( 1 << bnSpiSs );
}
/* ------------------------------------------------------------ */
/*** BSpiPutByte
**
** Parameters:
** bSnd - byte to send
**
** Return Value:
** byte received from slave
**
** Errors:
** none
**
** Description:
** Send a byte to a SPI slave and receive a byte from the
** slave.
*/
BYTE BSpiPutByte( BYTE bSnd )
{
SPI2BUF = bSnd; // write transmit data to buffer
// Wait for receive buffer to become filled.
while ( ! (SPI2STAT & ( 1 << bnSpi2Spirbf )) ) {
asm volatile("nop");
}
return SPI2BUF;
}
/* ------------------------------------------------------------ */
/*** SpiPutBuff
**
** Parameters:
** pbBuff - pointer to a buffer of bytes to send
** cbBuff - number of bytes in buffer
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Send a buffer of bytes to an SPI slave. Discard any
** data received from the slave.
*/
void SpiPutBuff( BYTE* pbBuff, WORD cbBuff )
{
while ( 0 < cbBuff ) {
BSpiPutByte(*pbBuff);
pbBuff++;
cbBuff--;
}
}
/* ------------------------------------------------------------ */
/*** SpiGetBuff
**
** Parameters:
** bFill - a filler byte sent to the slave
** pbBuff - pointer to a buffer of bytes to receive
** cbBuff - number of bytes to receive
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Receive a specified number of bytes from SPI slave.
** The bFill byte is shifted to the slave on every transmission.
*/
void SpiGetBuff( BYTE bFill, BYTE* pbBuff, WORD cbBuff )
{
while ( 0 < cbBuff ) {
*pbBuff = BSpiPutByte(bFill);
pbBuff++;
cbBuff--;
}
}
/* ------------------------------------------------------------ */
/*** SpiPutGetBuff
**
** Parameters:
** pbSnd - a buffer of bytes to send
** pbRcv - a buffer of bytes to receive slave data
** cbSnd - number of bytes to be transferred
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Transfer a specified number of bytes between master and
** slave.
*/
void SpiPutGetBuff( BYTE* pbSnd, BYTE* pbRcv, WORD cbSnd )
{
while ( 0 < cbSnd ) {
*pbRcv = BSpiPutByte(*pbSnd);
pbRcv++;
pbSnd++;
cbSnd--;
}
}
/************************************************************************/