spi.c
4.21 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
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* 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.
*/
/**
* @ingroup cpu_lpc2387
* @{
*
* @file
* @brief Low-level SPI driver implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "cpu.h"
#include "mutex.h"
#include "periph/gpio.h"
#include "periph/spi.h"
#include "periph_conf.h"
#include "board.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#if SPI_0_EN
#define SPI_TX_EMPTY (SSP0SR & SSPSR_TFE)
#define SPI_BUSY (SSP0SR & SSPSR_BSY)
#define SPI_RX_AVAIL (SSP0SR & SSPSR_RNE)
/**
* @brief Array holding one pre-initialized mutex for each SPI device
*/
static mutex_t locks[] = {
#if SPI_0_EN
[SPI_0] = MUTEX_INIT,
#endif
#if SPI_1_EN
[SPI_1] = MUTEX_INIT,
#endif
#if SPI_2_EN
[SPI_2] = MUTEX_INIT
#endif
};
int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
{
(void ) conf;
if (dev) {
return -1;
}
uint32_t f_baud = 0;
switch(speed)
{
case SPI_SPEED_100KHZ:
f_baud = 100;
break;
case SPI_SPEED_400KHZ:
f_baud = 400;
break;
case SPI_SPEED_1MHZ:
f_baud = 1000;
break;
case SPI_SPEED_5MHZ:
f_baud = 5000;
break;
case SPI_SPEED_10MHZ:
f_baud = 10000;
break;
}
#if 0
/* TODO */
switch(conf)
{
case SPI_CONF_FIRST_RISING:
/**< first data bit is transacted on the first rising SCK edge */
cpha = 0;
cpol = 0;
break;
case SPI_CONF_SECOND_RISING:
/**< first data bit is transacted on the second rising SCK edge */
cpha = 1;
cpol = 0;
break;
case SPI_CONF_FIRST_FALLING:
/**< first data bit is transacted on the first falling SCK edge */
cpha = 0;
cpol = 1;
break;
case SPI_CONF_SECOND_FALLING:
/**< first data bit is transacted on the second falling SCK edge */
cpha = 1;
cpol = 1;
break;
}
#endif
/* Power*/
PCONP |= PCSSP0; /* Enable power for SSP0 (default is on)*/
/* PIN Setup*/
spi_conf_pins(dev);
/* Interface Setup*/
SSP0CR0 = 7;
/* Clock Setup*/
uint32_t pclksel;
uint32_t cpsr;
lpc2387_pclk_scale(CLOCK_CORECLOCK / 1000, f_baud, &pclksel, &cpsr);
PCLKSEL1 &= ~(BIT10 | BIT11); /* CCLK to PCLK divider*/
PCLKSEL1 |= pclksel << 10;
SSP0CPSR = cpsr;
/* Enable*/
SSP0CR1 |= BIT1; /* SSP-Enable*/
int dummy;
/* Clear RxFIFO:*/
while (SPI_RX_AVAIL) { /* while RNE (Receive FIFO Not Empty)...*/
dummy = SSP0DR; /* read data*/
}
/* to suppress unused-but-set-variable */
(void) dummy;
return 0;
}
int spi_init_slave(spi_t dev, spi_conf_t conf, char (*cb)(char))
{
(void)dev;
(void)conf;
(void)cb;
printf("%s:%s(): stub\n", RIOT_FILE_RELATIVE, __func__);
/* TODO */
return -1;
}
void spi_transmission_begin(spi_t dev, char reset_val)
{
(void)dev;
(void)reset_val;
printf("%s:%s(): stub\n", RIOT_FILE_RELATIVE, __func__);
/* TODO*/
}
int spi_acquire(spi_t dev)
{
if (dev >= SPI_NUMOF) {
return -1;
}
mutex_lock(&locks[dev]);
return 0;
}
int spi_release(spi_t dev)
{
if (dev >= SPI_NUMOF) {
return -1;
}
mutex_unlock(&locks[dev]);
return 0;
}
int spi_transfer_byte(spi_t dev, char out, char *in)
{
(void) dev;
while (!SPI_TX_EMPTY) {}
SSP0DR = out;
while (SPI_BUSY) {}
while (!SPI_RX_AVAIL) {}
char tmp = (char)SSP0DR;
if (in != NULL) {
*in = tmp;
}
return 1;
}
void spi_poweron(spi_t dev)
{
(void) dev;
}
void spi_poweroff(spi_t dev)
{
(void) dev;
(void) dev;
}
int spi_conf_pins(spi_t dev)
{
switch (dev) {
case 0:
PINSEL3 |= BIT8 + BIT9; /* SCLK */
PINSEL3 |= BIT14 + BIT15; /* MISO */
PINSEL3 |= BIT16 + BIT17; /* MOSI */
return 0;
default:
return -1;
}
}
#endif /* SPI_0_EN */