-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathSPI.cpp
429 lines (318 loc) · 11.2 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (c) 2016 Thomas Roell. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Thomas Roell, nor the names of its contributors
* may be used to endorse or promote products derived from this Software
* without specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* WITH THE SOFTWARE.
*/
#include "Arduino.h"
#include "stm32l4_wiring_private.h"
#include "SPI.h"
/* The code below deserves some explanation. The SPIClass has really 2 modes of operation.
* One is the beginTransaction()/endTransaction() which locally braces every atomic transaction,
* and one is the old model, where you'd configure the SPI PORT directly. For STM32L4 we
* really, really only want to deal with the transaction model, as this allows to clock gate
* the peripheral. Hence the transfer*() function are wrapped via indirect functions calls.
* If a transfer*() function is called outside a transaction then a virtual beginTransaction()
* is inserted, and if API are called the reconfigure the SPI PORT, then a virtual endTransaction()
* is inserted.
*/
SPIClass::SPIClass(struct _stm32l4_spi_t *spi, unsigned int instance, const struct _stm32l4_spi_pins_t *pins, unsigned int priority, unsigned int mode)
{
_spi = spi;
stm32l4_spi_create(spi, instance, pins, priority, mode);
_selected = false;
_clock = 4000000;
_bitOrder = MSBFIRST;
_dataMode = SPI_MODE0;
_reference = 16000000;
_interruptMask = 0;
_exchangeRoutine = SPIClass::_exchangeSelect;
_exchange8Routine = SPIClass::_exchange8Select;
_exchange16Routine = SPIClass::_exchange16Select;
_completionCallback = NULL;
}
void SPIClass::begin()
{
stm32l4_spi_enable(_spi, SPIClass::_eventCallback, (void*)this, (SPI_EVENT_RECEIVE_DONE | SPI_EVENT_TRANSMIT_DONE | SPI_EVENT_TRANSFER_DONE));
}
void SPIClass::end()
{
if (_selected) {
stm32l4_spi_unselect(_spi);
_exchangeRoutine = SPIClass::_exchangeSelect;
_exchange8Routine = SPIClass::_exchange8Select;
_exchange16Routine = SPIClass::_exchange16Select;
_selected = false;
}
stm32l4_spi_disable(_spi);
}
void SPIClass::usingInterrupt(uint32_t pin)
{
if (!(g_APinDescription[pin].attr & PIN_ATTR_EXTI)) {
return;
}
_interruptMask |= (1ul << ((g_APinDescription[pin].pin & GPIO_PIN_INDEX_MASK) >> GPIO_PIN_INDEX_SHIFT));
}
void SPIClass::notUsingInterrupt(uint32_t pin)
{
if (!(g_APinDescription[pin].attr & PIN_ATTR_EXTI)) {
return;
}
_interruptMask &= ~(1ul << ((g_APinDescription[pin].pin & GPIO_PIN_INDEX_MASK) >> GPIO_PIN_INDEX_SHIFT));
}
void SPIClass::beginTransaction(SPISettings settings)
{
uint32_t option, clock, divide;
option = settings._dataMode | ((settings._bitOrder == MSBFIRST) ? SPI_OPTION_MSB_FIRST : SPI_OPTION_LSB_FIRST);
clock = stm32l4_spi_clock(_spi) / 2;
divide = 0;
while ((clock > settings._clock) && (divide < 7)) {
clock /= 2;
divide++;
}
option |= (divide << SPI_OPTION_DIV_SHIFT);
if (_selected) {
stm32l4_spi_configure(_spi, option);
} else {
_selected = true;
_exchangeRoutine = stm32l4_spi_exchange;
_exchange8Routine = stm32l4_spi_exchange8;
_exchange16Routine = stm32l4_spi_exchange16;
stm32l4_spi_select(_spi, option);
}
if (_interruptMask) {
stm32l4_exti_suspend(&stm32l4_exti, _interruptMask);
}
}
void SPIClass::endTransaction(void)
{
if (_interruptMask) {
stm32l4_exti_resume(&stm32l4_exti, _interruptMask);
}
stm32l4_spi_unselect(_spi);
_exchangeRoutine = SPIClass::_exchangeSelect;
_exchange8Routine = SPIClass::_exchange8Select;
_exchange16Routine = SPIClass::_exchange16Select;
_selected = false;
}
void SPIClass::setBitOrder(BitOrder bitOrder)
{
if (_selected) {
stm32l4_spi_unselect(_spi);
_exchangeRoutine = SPIClass::_exchangeSelect;
_exchange8Routine = SPIClass::_exchange8Select;
_exchange16Routine = SPIClass::_exchange16Select;
_selected = false;
}
_bitOrder = bitOrder;
}
void SPIClass::setDataMode(uint8_t dataMode)
{
if (_selected) {
stm32l4_spi_unselect(_spi);
_exchangeRoutine = SPIClass::_exchangeSelect;
_exchange8Routine = SPIClass::_exchange8Select;
_exchange16Routine = SPIClass::_exchange16Select;
_selected = false;
}
_dataMode = dataMode;
}
void SPIClass::setClockDivider(uint8_t divider)
{
if (_selected) {
stm32l4_spi_unselect(_spi);
_exchangeRoutine = SPIClass::_exchangeSelect;
_exchange8Routine = SPIClass::_exchange8Select;
_exchange16Routine = SPIClass::_exchange16Select;
_selected = false;
}
if (divider != 0) {
_clock = _reference / divider;
}
}
void SPIClass::setClockDividerReference(uint32_t clock)
{
_reference = clock;
}
void SPIClass::attachInterrupt()
{
// Should be enableInterrupt()
}
void SPIClass::detachInterrupt()
{
// Should be disableInterrupt()
}
bool SPIClass::transfer(const void *txBuffer, void *rxBuffer, size_t count, void(*callback)(void))
{
if (!stm32l4_spi_done(_spi)) {
return false;
}
if (!_selected) {
uint32_t option, clock, divide;
option = _dataMode | ((_bitOrder == MSBFIRST) ? SPI_OPTION_MSB_FIRST : SPI_OPTION_LSB_FIRST);
clock = stm32l4_spi_clock(_spi) / 2;
divide = 0;
while ((clock > _clock) && (divide < 7)) {
clock /= 2;
divide++;
}
option |= (divide << SPI_OPTION_DIV_SHIFT);
stm32l4_spi_select(_spi, option);
_selected = true;
_exchangeRoutine = stm32l4_spi_exchange;
_exchange8Routine = stm32l4_spi_exchange8;
_exchange16Routine = stm32l4_spi_exchange16;
}
_completionCallback = callback;
if (rxBuffer) {
if (txBuffer) {
if (!stm32l4_spi_transfer(_spi, static_cast<const uint8_t*>(txBuffer), static_cast<uint8_t*>(rxBuffer), count, 0)) {
_completionCallback = NULL;
return false;
}
} else {
if (!stm32l4_spi_receive(_spi, static_cast<uint8_t*>(rxBuffer), count, 0)) {
_completionCallback = NULL;
return false;
}
}
} else {
if (!stm32l4_spi_transmit(_spi, static_cast<const uint8_t*>(txBuffer), count, 0)) {
_completionCallback = NULL;
return false;
}
}
return true;
}
void SPIClass::flush(void)
{
if (armv7m_core_priority() <= STM32L4_SPI_IRQ_PRIORITY) {
while (!stm32l4_spi_done(_spi)) {
stm32l4_spi_poll(_spi);
}
} else {
while (!stm32l4_spi_done(_spi)) {
armv7m_core_yield();
}
}
}
bool SPIClass::done(void)
{
return stm32l4_spi_done(_spi);
}
bool SPIClass::isEnabled(void)
{
return (_spi->state >= SPI_STATE_READY);
}
void SPIClass::_exchangeSelect(struct _stm32l4_spi_t *spi, const uint8_t *txData, uint8_t *rxData, size_t count)
{
SPIClass *spi_class = reinterpret_cast<class SPIClass*>(spi->context);
uint32_t option, clock, divide;
option = spi_class->_dataMode | ((spi_class->_bitOrder == MSBFIRST) ? SPI_OPTION_MSB_FIRST : SPI_OPTION_LSB_FIRST);
clock = stm32l4_spi_clock(spi) / 2;
divide = 0;
while ((clock > spi_class->_clock) && (divide < 7)) {
clock /= 2;
divide++;
}
option |= (divide << SPI_OPTION_DIV_SHIFT);
stm32l4_spi_select(spi, option);
spi_class->_selected = true;
spi_class->_exchangeRoutine = stm32l4_spi_exchange;
spi_class->_exchange8Routine = stm32l4_spi_exchange8;
spi_class->_exchange16Routine = stm32l4_spi_exchange16;
return (*spi_class->_exchangeRoutine)(spi, txData, rxData, count);
}
uint8_t SPIClass::_exchange8Select(struct _stm32l4_spi_t *spi, uint8_t data)
{
SPIClass *spi_class = reinterpret_cast<class SPIClass*>(spi->context);
uint32_t option, clock, divide;
option = spi_class->_dataMode | ((spi_class->_bitOrder == MSBFIRST) ? SPI_OPTION_MSB_FIRST : SPI_OPTION_LSB_FIRST);
clock = stm32l4_spi_clock(spi) / 2;
divide = 0;
while ((clock > spi_class->_clock) && (divide < 7)) {
clock /= 2;
divide++;
}
option |= (divide << SPI_OPTION_DIV_SHIFT);
stm32l4_spi_select(spi, option);
spi_class->_selected = true;
spi_class->_exchangeRoutine = stm32l4_spi_exchange;
spi_class->_exchange8Routine = stm32l4_spi_exchange8;
spi_class->_exchange16Routine = stm32l4_spi_exchange16;
return (*spi_class->_exchange8Routine)(spi, data);
}
uint16_t SPIClass::_exchange16Select(struct _stm32l4_spi_t *spi, uint16_t data)
{
SPIClass *spi_class = reinterpret_cast<class SPIClass*>(spi->context);
uint32_t option, clock, divide;
option = spi_class->_dataMode | ((spi_class->_bitOrder == MSBFIRST) ? SPI_OPTION_MSB_FIRST : SPI_OPTION_LSB_FIRST);
clock = stm32l4_spi_clock(spi) / 2;
divide = 0;
while ((clock > spi_class->_clock) && (divide < 7)) {
clock /= 2;
divide++;
}
option |= (divide << SPI_OPTION_DIV_SHIFT);
stm32l4_spi_select(spi, option);
spi_class->_selected = true;
spi_class->_exchangeRoutine = stm32l4_spi_exchange;
spi_class->_exchange8Routine = stm32l4_spi_exchange8;
spi_class->_exchange16Routine = stm32l4_spi_exchange16;
return (*spi_class->_exchange16Routine)(spi, data);
}
void SPIClass::EventCallback(uint32_t events)
{
void(*callback)(void);
callback = _completionCallback;
_completionCallback = NULL;
if (callback) {
(*callback)();
}
}
void SPIClass::_eventCallback(void *context, uint32_t events)
{
reinterpret_cast<class SPIClass*>(context)->EventCallback(events);
}
#if SPI_INTERFACES_COUNT > 0
extern const stm32l4_spi_pins_t g_SPIPins;
extern const unsigned int g_SPIInstance;
extern const unsigned int g_SPIMode;
static stm32l4_spi_t _SPI;
SPIClass SPI(&_SPI, g_SPIInstance, &g_SPIPins, STM32L4_SPI_IRQ_PRIORITY, g_SPIMode);
#endif
#if SPI_INTERFACES_COUNT > 1
extern const stm32l4_spi_pins_t g_SPI1Pins;
extern const unsigned int g_SPI1Instance;
extern const unsigned int g_SPI1Mode;
static stm32l4_spi_t _SPI1;
SPIClass SPI1(&_SPI1, g_SPI1Instance, &g_SPI1Pins, STM32L4_SPI_IRQ_PRIORITY, g_SPI1Mode);
#endif
#if SPI_INTERFACES_COUNT > 2
extern const stm32l4_spi_pins_t g_SPI2Pins;
extern const unsigned int g_SPI2Instance;
extern const unsigned int g_SPI2Mode;
static stm32l4_spi_t _SPI2;
SPIClass SPI2(&_SPI2, g_SPI2Instance, &g_SPI2Pins, STM32L4_SPI_IRQ_PRIORITY, g_SPI2Mode);
#endif