-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPI.cpp
320 lines (265 loc) · 7.53 KB
/
SPI.cpp
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
/*
* Copyright (c) 2010 by Cristian Maglie <[email protected]>
* Copyright (c) 2014 by Paul Stoffregen <[email protected]> (Transaction API)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "SPI.h"
#include "Arduino.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "spi_api.h"
#include "spi_ex_api.h"
#include "PinNames.h"
#include "cmsis_os.h"
//extern void log_uart_enable_printf(void);
//extern void log_uart_disable_printf(void);
#ifdef __cplusplus
}
#endif
spi_t spi_obj0;
spi_t spi_obj1;
SPIClass::SPIClass(void *pSpiObj, int mosi, int miso, int clk, int ss)
{
pSpiMaster = pSpiObj;
/* These 4 pins should belong same spi pinmux*/
pinMOSI = mosi;
pinMISO = miso;
pinCLK = clk;
pinSS = ss;
pinUserSS = -1;
defaultFrequency = 20000000;
}
void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
{
bitOrder = settings._bitOrder;
spi_format((spi_t *)pSpiMaster, 8, settings._dataMode, 0);
spi_frequency((spi_t *)pSpiMaster, settings._clock);
//log_uart_disable_printf();
pinUserSS = pin;
pinMode(pinUserSS, OUTPUT);
digitalWrite(pinUserSS, 0);
//log_uart_enable_printf();
}
void SPIClass::beginTransaction(SPISettings settings)
{
beginTransaction(pinSS, settings);
}
void SPIClass::endTransaction(void)
{
if (pinUserSS >= 0) {
digitalWrite(pinUserSS, 1);
pinUserSS = -1;
}
}
void SPIClass::begin(void)
{
#if defined(BOARD_RTL8722DM)
if (pinMOSI == 11) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI0;
} else if (pinMOSI == 21) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI1;
} else {
printf("spi_init: error. wrong spi_idx \r\n");
return;
}
#elif defined(BOARD_RTL8722DM_MINI)
if ((pinMOSI == 9) || (pinMOSI == 4)) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI1;
} else {
printf("spi_init: error. wrong spi_idx \r\n");
return;
}
#elif defined(BOARD_RTL8720DN_BW16)
if (pinMOSI == 12) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI1;
} else {
printf("spi_init: error. wrong spi_idx \r\n");
return;
}
#else
#error chack the SPI pin connections
#endif
spi_init(
(spi_t *)pSpiMaster,
(PinName)g_APinDescription[pinMOSI].pinname,
(PinName)g_APinDescription[pinMISO].pinname,
(PinName)g_APinDescription[pinCLK].pinname,
(PinName)g_APinDescription[pinSS].pinname
);
spi_format((spi_t *)pSpiMaster, 8, 0, 0);
spi_frequency((spi_t *)pSpiMaster, defaultFrequency);
}
void SPIClass::begin(int ss)
{
#if defined(BOARD_RTL8722DM)
if (pinMOSI == 11) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI0;
} else if (pinMOSI == 21) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI1;
} else {
printf("spi_init: error. wrong spi_idx \r\n");
return;
}
#elif defined(BOARD_RTL8722DM_MINI)
if ((pinMOSI == 9) || (pinMOSI == 4)) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI1;
} else {
printf("spi_init: error. wrong spi_idx \r\n");
return;
}
#elif defined(BOARD_RTL8720DN_BW16)
if (pinMOSI == 12) {
((spi_t *)pSpiMaster)->spi_idx = MBED_SPI1;
} else {
printf("spi_init: error. wrong spi_idx \r\n");
return;
}
#else
#error chack the SPI pin connections
#endif
spi_init(
(spi_t *)pSpiMaster,
(PinName)g_APinDescription[pinMOSI].pinname,
(PinName)g_APinDescription[pinMISO].pinname,
(PinName)g_APinDescription[pinCLK].pinname,
(PinName)g_APinDescription[ss].pinname
);
spi_format((spi_t *)pSpiMaster, 8, 0, 0);
spi_frequency((spi_t *)pSpiMaster, defaultFrequency);
}
void SPIClass::end(void)
{
spi_free((spi_t *)pSpiMaster);
}
byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode)
{
byte d;
u8 spi_Index;
u32 spi_addr;
spi_Index = ((spi_t *)pSpiMaster)->spi_idx;
//spi_addr = 0x40042000 + (spi_Index * SSI_REG_OFF);
if (spi_Index == MBED_SPI0) {
spi_addr = SPI0_REG_BASE;
} else if (spi_Index == MBED_SPI1) {
spi_addr = SPI1_REG_BASE;
} else {
printf("error: wrong spi_idx \r\n");
return 0;
}
if (_pin != pinSS) {
pinMode(_pin, OUTPUT);
digitalWrite(_pin, 0);
}
while (!(HAL_READ32(spi_addr, 0x28) & 0x0000002));
HAL_WRITE32(spi_addr, 0x60, (_data & 0xFFFF));
while (!(HAL_READ32(spi_addr, 0x28) & 0x0000008));
d = HAL_READ32(spi_addr, 0x60);
if ((_pin != pinSS) && (_mode == SPI_LAST)) {
digitalWrite(_pin, 1);
}
return d;
}
byte SPIClass::transfer(uint8_t _data, SPITransferMode _mode)
{
(void)_mode;
byte d;
u8 spi_Index;
u32 spi_addr;
spi_Index = ((spi_t *)pSpiMaster)->spi_idx;
//spi_addr = 0x40042000 + (spi_Index * SSI_REG_OFF);
if (spi_Index == MBED_SPI0) {
spi_addr = SPI0_REG_BASE;
} else if (spi_Index == MBED_SPI1) {
spi_addr = SPI1_REG_BASE;
} else {
printf("error: wrong spi_idx \r\n");
return 0;
}
while (!(HAL_READ32(spi_addr, 0x28) & 0x0000002));
HAL_WRITE32(spi_addr, 0x60, _data & 0xFFFF);
while (!(HAL_READ32(spi_addr, 0x28) & 0x0000008));
d = HAL_READ32(spi_addr, 0x60);
return d;
}
void SPIClass::transfer(byte _pin, void *_buf, size_t _count, SPITransferMode _mode)
{
if (_pin != pinSS) {
pinMode(_pin, OUTPUT);
digitalWrite(_pin, 0);
}
spi_master_write_stream((spi_t *)pSpiMaster , (char *)_buf, (uint32_t)_count);
if ((_pin != pinSS) && (_mode == SPI_LAST)) {
digitalWrite(_pin, 1);
}
}
void SPIClass::transfer(void *_buf, size_t _count, SPITransferMode _mode)
{
transfer(pinSS, _buf, _count, _mode);
}
uint16_t SPIClass::transfer16(byte _pin, uint16_t _data, SPITransferMode _mode)
{
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t;
t.val = _data;
if (bitOrder == LSBFIRST) {
t.lsb = transfer(_pin, t.lsb, SPI_CONTINUE);
t.msb = transfer(_pin, t.msb, _mode);
} else {
t.msb = transfer(_pin, t.msb, SPI_CONTINUE);
t.lsb = transfer(_pin, t.lsb, _mode);
}
return _data;
}
uint16_t SPIClass::transfer16(uint16_t _data, SPITransferMode _mode)
{
return transfer16(pinSS, _data, _mode);
}
void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder)
{
(void)_pin;
bitOrder = _bitOrder;
}
void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode)
{
(void)_pin;
spi_format((spi_t *)pSpiMaster, 8, _mode, 0);
}
void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider)
{
(void)_pin;
(void)_divider;
// no affact in Ameba
}
void SPIClass::setBitOrder(BitOrder _order)
{
setBitOrder(pinSS, _order);
}
void SPIClass::setDataMode(uint8_t _mode)
{
setDataMode(pinSS, _mode);
}
void SPIClass::setClockDivider(uint8_t _div)
{
(void)_div;
// no affact in Ameba
}
void SPIClass::setDefaultFrequency(int _frequency)
{
defaultFrequency = _frequency;
}
#if defined(BOARD_RTL8722DM)
SPIClass SPI((void *)(&spi_obj0), 11, 12, 13, 10);
SPIClass SPI1((void *)(&spi_obj1), 21, 20, 19, 18);
#elif defined(BOARD_RTL8722DM_MINI)
SPIClass SPI((void *)(&spi_obj0), 9, 10, 11, 12);
#elif defined(BOARD_RTL8720DN_BW16)
//SPIClass SPI((void *)(&spi_obj0), PA12, PA13, PA14, PA15);
SPIClass SPI((void *)(&spi_obj0), 12, 11, 10, 9);
#else
#error chack the borad supported
#endif