spi.c
7.68 KB
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
/*
* Copyright (C) 2015 Loci Controls Inc.
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @addtogroup driver_periph
* @{
*
* @file
* @brief Low-level SPI driver implementation
*
* @author Ian Martin <ian@locicontrols.com>
*
* @}
*/
#include <assert.h>
#include <stdio.h>
#include "cc2538_ssi.h"
#include "cpu.h"
#include "mutex.h"
#include "periph/spi.h"
#include "periph_conf.h"
/* guard file in case no SPI device is defined */
#if SPI_NUMOF
/* clock sources for the SSI_CC register */
#define CS_SYS_DIV 0
#define CS_IO_DIV 1
#define SSI0_MASK (1 << 0)
#define SSI1_MASK (1 << 1)
#ifndef SPI_DATA_BITS_NUMOF
#define SPI_DATA_BITS_NUMOF 8
#endif
#define spin_until(condition) while (!(condition)) thread_yield()
/**
* @brief Array holding one pre-initialized mutex for each SPI device
*/
static mutex_t locks[SPI_NUMOF] = {MUTEX_INIT};
int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
{
cc2538_ssi_t* ssi = spi_config[dev].dev;
if ((unsigned int)dev >= SPI_NUMOF) {
return -1;
}
/* power on the SPI device */
spi_poweron(dev);
/* configure SCK, MISO and MOSI pin */
spi_conf_pins(dev);
/* Disable the SSI and configure it for SPI master mode */
ssi->CR1 = 0;
/* 3. Configure the SSI clock source */
ssi->CC = CS_SYS_DIV;
/* 4. Configure the clock prescale divisor by writing the SSI_CPSR register.
* frequency of the SSIClk is defined by: SSIClk = SysClk / (CPSDVSR x (1 + SCR))
*/
const int32_t speed_lut[] = {
[SPI_SPEED_100KHZ] = 100000 /* Hz */,
[SPI_SPEED_400KHZ] = 400000 /* Hz */,
[SPI_SPEED_1MHZ ] = 1000000 /* Hz */,
[SPI_SPEED_5MHZ ] = 5000000 /* Hz */,
[SPI_SPEED_10MHZ ] = 10000000 /* Hz */,
};
int32_t SysClk = sys_clock_freq();
int32_t f_desired = speed_lut[speed];
int32_t f_actual;
int32_t err;
int32_t best_err = INT32_MAX;
int32_t div1;
int32_t div2;
int32_t best_div1 = 2;
int32_t best_div2 = 1;
/* System clock is first divided by CPSDVSR, then by SCR */
for (div1 = 2; div1 <= 254; div1++) {
div2 = SysClk;
int32_t denom = div1 * f_desired;
div2 += denom / 2;
div2 /= denom;
if (div2 < 1) {
div2 = 1;
}
else if (div2 > 256) {
div2 = 256;
}
f_actual = SysClk / (div1 * div2);
err = f_actual - f_desired;
if (err < 0) {
err = -err;
}
if (err <= best_err) {
best_div1 = div1;
best_div2 = div2;
best_err = err;
}
}
ssi->CPSR = best_div1; /* CPSDVSR */
ssi->CR0bits.SCR = best_div2 - 1; /* Serial clock rate (SCR) */
switch (conf) {
case SPI_CONF_FIRST_RISING:
ssi->CR0bits.SPO = 0;
ssi->CR0bits.SPH = 0;
break;
case SPI_CONF_SECOND_RISING:
ssi->CR0bits.SPO = 0;
ssi->CR0bits.SPH = 1;
break;
case SPI_CONF_FIRST_FALLING:
ssi->CR0bits.SPO = 1;
ssi->CR0bits.SPH = 0;
break;
case SPI_CONF_SECOND_FALLING:
ssi->CR0bits.SPO = 1;
ssi->CR0bits.SPH = 1;
break;
}
ssi->CR0bits.FRF = 0; /* SPI protocol mode */
ssi->CR0bits.DSS = SPI_DATA_BITS_NUMOF - 1; /* The data size */
ssi->CR1bits.SSE = 1;
return 0;
}
int spi_init_slave(spi_t dev, spi_conf_t conf, char(*cb)(char data))
{
/* slave mode is not (yet) supported */
return -1;
}
int spi_conf_pins(spi_t dev)
{
if ((unsigned int)dev >= SPI_NUMOF) {
return -1;
}
switch ((uintptr_t)spi_config[dev].dev) {
case (uintptr_t)SSI0:
IOC_PXX_SEL[spi_config[dev].mosi_pin] = SSI0_TXD;
IOC_PXX_SEL[spi_config[dev].sck_pin ] = SSI0_CLKOUT;
IOC_PXX_SEL[spi_config[dev].cs_pin ] = SSI0_FSSOUT;
IOC_SSIRXD_SSI0 = spi_config[dev].miso_pin;
break;
case (uintptr_t)SSI1:
IOC_PXX_SEL[spi_config[dev].mosi_pin] = SSI1_TXD;
IOC_PXX_SEL[spi_config[dev].sck_pin ] = SSI1_CLKOUT;
IOC_PXX_SEL[spi_config[dev].cs_pin ] = SSI1_FSSOUT;
IOC_SSIRXD_SSI1 = spi_config[dev].miso_pin;
break;
}
IOC_PXX_OVER[spi_config[dev].mosi_pin] = IOC_OVERRIDE_OE;
IOC_PXX_OVER[spi_config[dev].sck_pin ] = IOC_OVERRIDE_OE;
IOC_PXX_OVER[spi_config[dev].cs_pin ] = IOC_OVERRIDE_OE;
IOC_PXX_OVER[spi_config[dev].miso_pin] = IOC_OVERRIDE_DIS;
gpio_hardware_control(spi_config[dev].mosi_pin);
gpio_hardware_control(spi_config[dev].miso_pin);
gpio_hardware_control(spi_config[dev].sck_pin);
gpio_hardware_control(spi_config[dev].cs_pin);
return 0;
}
int spi_acquire(spi_t dev)
{
if ((unsigned int)dev >= SPI_NUMOF) {
return -1;
}
mutex_lock(&locks[dev]);
return 0;
}
int spi_release(spi_t dev)
{
if ((unsigned int)dev >= SPI_NUMOF) {
return -1;
}
mutex_unlock(&locks[dev]);
return 0;
}
static char ssi_flush_input(cc2538_ssi_t *ssi)
{
char tmp = 0;
while (ssi->SRbits.RNE) {
tmp = ssi->DR;
}
return tmp;
}
int spi_transfer_byte(spi_t dev, char out, char *in)
{
cc2538_ssi_t* ssi = spi_config[dev].dev;
char tmp;
ssi_flush_input(ssi);
/* transmit byte */
spin_until(ssi->SRbits.TNF);
ssi->DR = out;
/* receive byte */
spin_until(ssi->SRbits.RNE);
tmp = ssi->DR;
if (in) {
*in = tmp;
}
return 1;
}
int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length)
{
cc2538_ssi_t* ssi = spi_config[dev].dev;
unsigned int tx_n = 0, rx_n = 0;
if ((unsigned int)dev >= SPI_NUMOF) {
return -1;
}
ssi_flush_input(ssi);
/* transmit and receive bytes */
while (tx_n < length) {
spin_until(ssi->SRbits.TNF || ssi->SRbits.RNE);
if (ssi->SRbits.TNF) {
ssi->DR = out[tx_n];
tx_n++;
}
else if (ssi->SRbits.RNE) {
assert(rx_n < length);
in[rx_n] = ssi->DR;
rx_n++;
}
}
/* receive remaining bytes */
while (rx_n < length) {
spin_until(ssi->SRbits.RNE);
assert(rx_n < length);
in[rx_n] = ssi->DR;
rx_n++;
}
return rx_n;
}
void spi_transmission_begin(spi_t dev, char reset_val)
{
/* slave mode is not (yet) supported */
}
void spi_poweron(spi_t dev)
{
switch ((uintptr_t)spi_config[dev].dev) {
case (uintptr_t)SSI0:
/* enable SSI0 in all three power modes */
SYS_CTRL_RCGCSSI |= SSI0_MASK;
SYS_CTRL_SCGCSSI |= SSI0_MASK;
SYS_CTRL_DCGCSSI |= SSI0_MASK;
break;
case (uintptr_t)SSI1:
/* enable SSI1 in all three power modes */
SYS_CTRL_RCGCSSI |= SSI1_MASK;
SYS_CTRL_SCGCSSI |= SSI1_MASK;
SYS_CTRL_DCGCSSI |= SSI1_MASK;
break;
}
}
void spi_poweroff(spi_t dev)
{
switch ((uintptr_t)spi_config[dev].dev) {
case (uintptr_t)SSI0:
/* disable SSI0 in all three power modes */
SYS_CTRL_RCGCSSI &= ~SSI0_MASK;
SYS_CTRL_SCGCSSI &= ~SSI0_MASK;
SYS_CTRL_DCGCSSI &= ~SSI0_MASK;
break;
case (uintptr_t)SSI1:
/* disable SSI1 in all three power modes */
SYS_CTRL_RCGCSSI &= ~SSI1_MASK;
SYS_CTRL_SCGCSSI &= ~SSI1_MASK;
SYS_CTRL_DCGCSSI &= ~SSI1_MASK;
break;
}
}
#endif /* SPI_NUMOF */