-
Notifications
You must be signed in to change notification settings - Fork 2
/
spi_util.c
283 lines (224 loc) · 8.99 KB
/
spi_util.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
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
/********************************************************************************
* Copyright (C) 2021 Ju, Gyeong-min
********************************************************************************/
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <asm/io.h>
#include "log_util.h"
#include "spi_util.h"
#include "bcm_peri.h"
#include "gpio_util.h"
#include "bcm2835.h"
/*! Return the minimum of 2 numbers */
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
#endif
static volatile uint32_t* bcm2835_spi0 = NULL;
static int spi0_ref_count = 0;
/* Initialise this library. */
int spi_init(u32 peri_base_addr, int size)
{
if (spi0_ref_count++ > 0) return 0;
if(size == 0) size = 0xB0;
if ((bcm2835_spi0 = (volatile uint32_t *)ioremap(peri_base_addr + BCM2835_SPI0_BASE, size)) == NULL) {
am_log_err("io remap failed.");
spi0_ref_count--;
return -EINVAL;
}
gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_ALT0); /* CE0 */
gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_ALT0); /* CE1 */
gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_ALT0); /* MISO */
gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0); /* MOSI */
gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_ALT0); /* CLK */
return 0;
}
/* Close this library and deallocate everything */
void spi_close(void)
{
if (--spi0_ref_count > 0) return;
// gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_INPT); /* CE1 */
// gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_INPT); /* CE0 */
// gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_INPT); /* MISO */
// gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_INPT); /* MOSI */
// gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_INPT); /* CLK */
if (bcm2835_spi0 != NULL) {
iounmap((void *)bcm2835_spi0);
bcm2835_spi0 = NULL;
}
}
int spi_begin(void)
{
volatile uint32_t* paddr;
if (bcm2835_spi0 == NULL)
return -ENODEV; /* bcm2835_init() failed, or not root */
/* Set the SPI CS register to the some sensible defaults */
paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
bcm_peri_write(paddr, 0); /* All 0s */
/* Clear TX and RX fifos */
bcm_peri_write_nb(paddr, BCM2835_SPI0_CS_CLEAR);
return 0; // OK
}
void spi_end(void)
{
/* Set all the SPI0 pins back to input */
// gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_INPT); /* CE1 */
// gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_INPT); /* CE0 */
// gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_INPT); /* MISO */
// gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_INPT); /* MOSI */
// gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_INPT); /* CLK */
}
// void spi_setBitOrder(uint8_t __attribute__((unused)) order)
// {
// /* BCM2835_SPI_BIT_ORDER_MSBFIRST is the only one supported by SPI0 */
// }
/* defaults to 0, which means a divider of 65536.
// The divisor must be a power of 2. Odd numbers
// rounded down. The maximum SPI clock rate is
// of the APB clock
*/
void spi_setClockDivider(uint16_t divider)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CLK/sizeof(uint32_t);
bcm_peri_write(paddr, divider);
}
void spi_setDataMode(uint8_t mode)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
/* Mask in the CPO and CPHA bits of CS */
bcm_peri_set_bits(paddr, mode << 2, BCM2835_SPI0_CS_CPOL | BCM2835_SPI0_CS_CPHA);
}
/* Writes (and reads) a single byte to SPI */
uint8_t spi_transfer(uint8_t value)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/sizeof(uint32_t);
uint32_t ret;
/* This is Polled transfer as per section 10.6.1
// BUG ALERT: what happens if we get interupted in this section, and someone else
// accesses a different peripheral?
// Clear TX and RX fifos
*/
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
/* Set TA = 1 */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
/* Maybe wait for TXD */
while (!(bcm_peri_read(paddr) & BCM2835_SPI0_CS_TXD)) {}
/* Write to FIFO, no barrier */
bcm_peri_write_nb(fifo, value);
/* Wait for DONE to be set */
while (!(bcm_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) {}
/* Read any byte that was sent back by the slave while we sere sending to it */
ret = bcm_peri_read_nb(fifo);
/* Set TA = 0, and also set the barrier */
bcm_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
return ret;
}
/* Writes (and reads) an number of bytes to SPI */
void spi_transfernb(char* tbuf, char* rbuf, uint32_t len)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/sizeof(uint32_t);
uint32_t TXCnt = 0;
uint32_t RXCnt = 0;
/* This is Polled transfer as per section 10.6.1
// BUG ALERT: what happens if we get interupted in this section, and someone else
// accesses a different peripheral?
*/
/* Clear TX and RX fifos */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
/* Set TA = 1 */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
/* Use the FIFO's to reduce the interbyte times */
while((TXCnt < len) || (RXCnt < len))
{
/* TX fifo not full, so add some more bytes */
while((bcm_peri_read(paddr) & BCM2835_SPI0_CS_TXD) && (TXCnt < len))
{
bcm_peri_write_nb(fifo, tbuf[TXCnt]);
TXCnt++;
}
/* Rx fifo not empty, so get the next received bytes */
while((bcm_peri_read(paddr) & BCM2835_SPI0_CS_RXD) && (RXCnt < len))
{
rbuf[RXCnt] = bcm_peri_read_nb(fifo);
RXCnt++;
}
}
/* Wait for DONE to be set */
while (!(bcm_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) {}
/* Set TA = 0, and also set the barrier */
bcm_peri_set_bits(paddr, 0x00, BCM2835_SPI0_CS_TA);
}
/* Writes an number of bytes to SPI */
void spi_writenb(const char* tbuf, uint32_t len)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/sizeof(uint32_t);
uint32_t i;
/* This is Polled transfer as per section 10.6.1
// BUG ALERT: what happens if we get interupted in this section, and someone else
// accesses a different peripheral?
// Answer: an ISR is required to issue the required memory barriers.
*/
/* Clear TX and RX fifos */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
/* Set TA = 1 */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
for (i = 0; i < len; i++)
{
/* Maybe wait for TXD */
while (!(bcm_peri_read(paddr) & BCM2835_SPI0_CS_TXD))
;
/* Write to FIFO, no barrier */
bcm_peri_write_nb(fifo, tbuf[i]);
/* Read from FIFO to prevent stalling */
while (bcm_peri_read(paddr) & BCM2835_SPI0_CS_RXD)
(void) bcm_peri_read_nb(fifo);
}
/* Wait for DONE to be set */
while (!(bcm_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) {
while (bcm_peri_read(paddr) & BCM2835_SPI0_CS_RXD)
(void) bcm_peri_read_nb(fifo);
};
/* Set TA = 0, and also set the barrier */
bcm_peri_set_bits(paddr, 0x00, BCM2835_SPI0_CS_TA);
}
/* Writes (and reads) an number of bytes to SPI
// Read bytes are copied over onto the transmit buffer
*/
void spi_transfern(char* buf, uint32_t len)
{
spi_transfernb(buf, buf, len);
}
void spi_chipSelect(uint8_t cs)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
/* Mask in the CS bits of CS */
bcm_peri_set_bits(paddr, cs, BCM2835_SPI0_CS_CS);
}
void spi_setChipSelectPolarity(uint8_t cs, uint8_t active)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
uint8_t shift = 21 + cs;
/* Mask in the appropriate CSPOLn bit */
bcm_peri_set_bits(paddr, active << shift, 1 << shift);
}
void spi_write(uint16_t data)
{
volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/sizeof(uint32_t);
volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/sizeof(uint32_t);
/* Clear TX and RX fifos */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
/* Set TA = 1 */
bcm_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
/* Maybe wait for TXD */
while (!(bcm_peri_read(paddr) & BCM2835_SPI0_CS_TXD)) {}
/* Write to FIFO */
bcm_peri_write_nb(fifo, (uint32_t) data >> 8);
bcm_peri_write_nb(fifo, data & 0xFF);
/* Wait for DONE to be set */
while (!(bcm_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) {}
/* Set TA = 0, and also set the barrier */
bcm_peri_set_bits(paddr, 0x00, BCM2835_SPI0_CS_TA);
}