/
spi_api.c
556 lines (490 loc) · 16.8 KB
/
spi_api.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright (c) 2013 Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
* integrated circuit in a product or a software update for such product, must reproduce
* the above copyright notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary or object form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "spi_api.h"
#if DEVICE_SPI
#include "cmsis.h"
#include "pinmap.h"
#include "mbed_assert.h"
#include "mbed_error.h"
#include "nrf_drv_spi.h"
#include "nrf_drv_spis.h"
#include "app_util_platform.h"
#if DEVICE_SPI_ASYNCH
#define SPI_IDX(obj) ((obj)->spi.spi_idx)
#else
#define SPI_IDX(obj) ((obj)->spi_idx)
#endif
#define SPI_INFO(obj) (&m_spi_info[SPI_IDX(obj)])
#define MASTER_INST(obj) (&m_instances[SPI_IDX(obj)].master)
#define SLAVE_INST(obj) (&m_instances[SPI_IDX(obj)].slave)
typedef struct {
bool initialized;
bool master;
uint8_t sck_pin;
uint8_t mosi_pin;
uint8_t miso_pin;
uint8_t ss_pin;
uint8_t spi_mode;
nrf_drv_spi_frequency_t frequency;
volatile union {
bool busy; // master
bool readable; // slave
} flag;
volatile uint8_t tx_buf;
volatile uint8_t rx_buf;
#if DEVICE_SPI_ASYNCH
uint32_t handler;
uint32_t event;
#endif
} spi_info_t;
static spi_info_t m_spi_info[SPI_COUNT];
typedef struct {
nrf_drv_spi_t master;
nrf_drv_spis_t slave;
} sdk_driver_instances_t;
void SPI0_TWI0_IRQHandler(void);
void SPI1_TWI1_IRQHandler(void);
void SPIM2_SPIS2_SPI2_IRQHandler(void);
static const peripheral_handler_desc_t spi_handler_desc[SPI_COUNT] = {
#if SPI0_ENABLED
{
SPI0_IRQ,
(uint32_t) SPI0_TWI0_IRQHandler
},
#endif
#if SPI1_ENABLED
{
SPI1_IRQ,
(uint32_t) SPI1_TWI1_IRQHandler
},
#endif
#if SPI2_ENABLED
{
SPI2_IRQ,
(uint32_t) SPIM2_SPIS2_SPI2_IRQHandler
},
#endif
};
static sdk_driver_instances_t m_instances[SPI_COUNT] = {
#if SPI0_ENABLED
{
NRF_DRV_SPI_INSTANCE(0),
NRF_DRV_SPIS_INSTANCE(0)
},
#endif
#if SPI1_ENABLED
{
NRF_DRV_SPI_INSTANCE(1),
NRF_DRV_SPIS_INSTANCE(1)
},
#endif
#if SPI2_ENABLED
{
NRF_DRV_SPI_INSTANCE(2),
NRF_DRV_SPIS_INSTANCE(2)
},
#endif
};
static void master_event_handler(uint8_t spi_idx,
nrf_drv_spi_evt_t const *p_event)
{
spi_info_t *p_spi_info = &m_spi_info[spi_idx];
if (p_event->type == NRF_DRV_SPI_EVENT_DONE) {
p_spi_info->flag.busy = false;
if (p_spi_info->handler) {
void (*handler)(void) = (void (*)(void))p_spi_info->handler;
p_spi_info->handler = 0;
handler();
}
}
}
#define MASTER_EVENT_HANDLER(idx) \
static void master_event_handler_##idx(nrf_drv_spi_evt_t const *p_event) { \
master_event_handler(SPI##idx##_INSTANCE_INDEX, p_event); \
}
#if SPI0_ENABLED
MASTER_EVENT_HANDLER(0)
#endif
#if SPI1_ENABLED
MASTER_EVENT_HANDLER(1)
#endif
#if SPI2_ENABLED
MASTER_EVENT_HANDLER(2)
#endif
static nrf_drv_spi_handler_t const m_master_event_handlers[SPI_COUNT] = {
#if SPI0_ENABLED
master_event_handler_0,
#endif
#if SPI1_ENABLED
master_event_handler_1,
#endif
#if SPI2_ENABLED
master_event_handler_2,
#endif
};
static void slave_event_handler(uint8_t spi_idx,
nrf_drv_spis_event_t event)
{
spi_info_t *p_spi_info = &m_spi_info[spi_idx];
if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) {
// Signal that there is some data received that could be read.
p_spi_info->flag.readable = true;
// And prepare for the next transfer.
// Previous data set in 'spi_slave_write' (if any) has been transmitted,
// now use the default one, until some new is set by 'spi_slave_write'.
p_spi_info->tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
nrf_drv_spis_buffers_set(&m_instances[spi_idx].slave,
(uint8_t const *)&p_spi_info->tx_buf, 1,
(uint8_t *)&p_spi_info->rx_buf, 1);
}
}
#define SLAVE_EVENT_HANDLER(idx) \
static void slave_event_handler_##idx(nrf_drv_spis_event_t event) { \
slave_event_handler(SPIS##idx##_INSTANCE_INDEX, event); \
}
#if SPIS0_ENABLED
SLAVE_EVENT_HANDLER(0)
#endif
#if SPIS1_ENABLED
SLAVE_EVENT_HANDLER(1)
#endif
#if SPIS2_ENABLED
SLAVE_EVENT_HANDLER(2)
#endif
static nrf_drv_spis_event_handler_t const m_slave_event_handlers[SPIS_COUNT] = {
#if SPIS0_ENABLED
slave_event_handler_0,
#endif
#if SPIS1_ENABLED
slave_event_handler_1,
#endif
#if SPIS2_ENABLED
slave_event_handler_2,
#endif
};
static void prepare_master_config(nrf_drv_spi_config_t *p_config,
spi_info_t const *p_spi_info)
{
p_config->sck_pin = p_spi_info->sck_pin;
p_config->mosi_pin = p_spi_info->mosi_pin;
p_config->miso_pin = p_spi_info->miso_pin;
p_config->ss_pin = p_spi_info->ss_pin;
p_config->frequency = p_spi_info->frequency;
p_config->mode = (nrf_drv_spi_mode_t)p_spi_info->spi_mode;
p_config->irq_priority = SPI1_CONFIG_IRQ_PRIORITY;
p_config->orc = 0xFF;
p_config->bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
}
static void prepare_slave_config(nrf_drv_spis_config_t *p_config,
spi_info_t const *p_spi_info)
{
p_config->sck_pin = p_spi_info->sck_pin;
p_config->mosi_pin = p_spi_info->mosi_pin;
p_config->miso_pin = p_spi_info->miso_pin;
p_config->csn_pin = p_spi_info->ss_pin;
p_config->mode = (nrf_drv_spis_mode_t)p_spi_info->spi_mode;
p_config->irq_priority = SPIS1_CONFIG_IRQ_PRIORITY;
p_config->orc = NRF_DRV_SPIS_DEFAULT_ORC;
p_config->def = NRF_DRV_SPIS_DEFAULT_DEF;
p_config->bit_order = NRF_DRV_SPIS_BIT_ORDER_MSB_FIRST;
p_config->csn_pullup = NRF_DRV_SPIS_DEFAULT_CSN_PULLUP;
p_config->miso_drive = NRF_DRV_SPIS_DEFAULT_MISO_DRIVE;
}
void spi_init(spi_t *obj,
PinName mosi, PinName miso, PinName sclk, PinName ssel)
{
int i;
// This block is only a workaround that allows to create SPI object several
// times, what would be otherwise impossible in the current implementation
// of mbed driver that does not call spi_free() from SPI destructor.
// Once this mbed's imperfection is corrected, this block should be removed.
for (i = 0; i < SPI_COUNT; ++i) {
spi_info_t *p_spi_info = &m_spi_info[i];
if (p_spi_info->initialized &&
p_spi_info->mosi_pin == (uint8_t)mosi &&
p_spi_info->miso_pin == (uint8_t)miso &&
p_spi_info->sck_pin == (uint8_t)sclk &&
p_spi_info->ss_pin == (uint8_t)ssel) {
// Reuse the already allocated SPI instance (instead of allocating
// a new one), if it appears to be initialized with exactly the same
// pin assignments.
SPI_IDX(obj) = i;
return;
}
}
for (i = 0; i < SPI_COUNT; ++i) {
spi_info_t *p_spi_info = &m_spi_info[i];
if (!p_spi_info->initialized) {
p_spi_info->sck_pin = (uint8_t)sclk;
p_spi_info->mosi_pin = (mosi != NC) ?
(uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED;
p_spi_info->miso_pin = (miso != NC) ?
(uint8_t)miso : NRF_DRV_SPI_PIN_NOT_USED;
p_spi_info->ss_pin = (ssel != NC) ?
(uint8_t)ssel : NRF_DRV_SPI_PIN_NOT_USED;
p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0;
p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M;
NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector);
// By default each SPI instance is initialized to work as a master.
// Should the slave mode be used, the instance will be reconfigured
// appropriately in 'spi_format'.
nrf_drv_spi_config_t config;
prepare_master_config(&config, p_spi_info);
nrf_drv_spi_t const *p_spi = &m_instances[i].master;
ret_code_t ret_code = nrf_drv_spi_init(p_spi,
&config, m_master_event_handlers[i]);
if (ret_code == NRF_SUCCESS) {
p_spi_info->initialized = true;
p_spi_info->master = true;
p_spi_info->flag.busy = false;
#if DEVICE_SPI_ASYNCH
p_spi_info->handler = 0;
#endif
SPI_IDX(obj) = i;
return;
}
}
}
// No available peripheral
error("No available SPI peripheral\r\n");
}
void spi_free(spi_t *obj)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
if (p_spi_info->master) {
nrf_drv_spi_uninit(MASTER_INST(obj));
}
else {
nrf_drv_spis_uninit(SLAVE_INST(obj));
}
p_spi_info->initialized = false;
}
int spi_busy(spi_t *obj)
{
return (int)(SPI_INFO(obj)->flag.busy);
}
void spi_format(spi_t *obj, int bits, int mode, int slave)
{
if (bits != 8) {
error("Only 8-bits SPI is supported\r\n");
}
if (mode > 3) {
error("SPI format error\r\n");
}
spi_info_t *p_spi_info = SPI_INFO(obj);
if (slave)
{
nrf_drv_spis_mode_t spi_modes[4] = {
NRF_DRV_SPIS_MODE_0,
NRF_DRV_SPIS_MODE_1,
NRF_DRV_SPIS_MODE_2,
NRF_DRV_SPIS_MODE_3,
};
nrf_drv_spis_mode_t new_mode = spi_modes[mode];
// If the peripheral is currently working as a master, the SDK driver
// it uses needs to be switched from SPI to SPIS.
if (p_spi_info->master) {
nrf_drv_spi_uninit(MASTER_INST(obj));
}
// I the SPI mode has to be changed, the SDK's SPIS driver needs to be
// re-initialized (there is no other way to change its configuration).
else if (p_spi_info->spi_mode != (uint8_t)new_mode) {
nrf_drv_spis_uninit(SLAVE_INST(obj));
}
else {
return;
}
p_spi_info->spi_mode = (uint8_t)new_mode;
p_spi_info->master = false;
p_spi_info->flag.readable = false;
// Initialize SDK's SPIS driver with the new configuration.
nrf_drv_spis_config_t config;
prepare_slave_config(&config, p_spi_info);
(void)nrf_drv_spis_init(SLAVE_INST(obj), &config,
m_slave_event_handlers[SPI_IDX(obj)]);
// Prepare the slave for transfer.
p_spi_info->tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
nrf_drv_spis_buffers_set(SLAVE_INST(obj),
(uint8_t const *)&p_spi_info->tx_buf, 1,
(uint8_t *)&p_spi_info->rx_buf, 1);
}
else // master
{
nrf_drv_spi_mode_t spi_modes[4] = {
NRF_DRV_SPI_MODE_0,
NRF_DRV_SPI_MODE_1,
NRF_DRV_SPI_MODE_2,
NRF_DRV_SPI_MODE_3,
};
nrf_drv_spi_mode_t new_mode = spi_modes[mode];
// If the peripheral is currently working as a slave, the SDK driver
// it uses needs to be switched from SPIS to SPI.
if (!p_spi_info->master) {
nrf_drv_spis_uninit(SLAVE_INST(obj));
}
// I the SPI mode has to be changed, the SDK's SPI driver needs to be
// re-initialized (there is no other way to change its configuration).
else if (p_spi_info->spi_mode != (uint8_t)new_mode) {
nrf_drv_spi_uninit(MASTER_INST(obj));
}
else {
return;
}
p_spi_info->spi_mode = (uint8_t)new_mode;
p_spi_info->master = true;
p_spi_info->flag.busy = false;
// Initialize SDK's SPI driver with the new configuration.
nrf_drv_spi_config_t config;
prepare_master_config(&config, p_spi_info);
(void)nrf_drv_spi_init(MASTER_INST(obj), &config,
m_master_event_handlers[SPI_IDX(obj)]);
}
}
static nrf_drv_spi_frequency_t freq_translate(int hz)
{
nrf_drv_spi_frequency_t frequency;
if (hz<250000) { //125Kbps
frequency = NRF_DRV_SPI_FREQ_125K;
} else if (hz<500000) { //250Kbps
frequency = NRF_DRV_SPI_FREQ_250K;
} else if (hz<1000000) { //500Kbps
frequency = NRF_DRV_SPI_FREQ_500K;
} else if (hz<2000000) { //1Mbps
frequency = NRF_DRV_SPI_FREQ_1M;
} else if (hz<4000000) { //2Mbps
frequency = NRF_DRV_SPI_FREQ_2M;
} else if (hz<8000000) { //4Mbps
frequency = NRF_DRV_SPI_FREQ_4M;
} else { //8Mbps
frequency = NRF_DRV_SPI_FREQ_8M;
}
return frequency;
}
void spi_frequency(spi_t *obj, int hz)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
nrf_drv_spi_frequency_t new_frequency = freq_translate(hz);
if (p_spi_info->master)
{
if (p_spi_info->frequency != new_frequency) {
p_spi_info->frequency = new_frequency;
nrf_drv_spi_config_t config;
prepare_master_config(&config, p_spi_info);
nrf_drv_spi_t const *p_spi = MASTER_INST(obj);
nrf_drv_spi_uninit(p_spi);
(void)nrf_drv_spi_init(p_spi, &config,
m_master_event_handlers[SPI_IDX(obj)]);
}
}
// There is no need to set anything in slaves when it comes to frequency,
// since slaves just synchronize with the clock provided by a master.
}
int spi_master_write(spi_t *obj, int value)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
#if DEVICE_SPI_ASYNCH
while (p_spi_info->flag.busy) {
}
#endif
p_spi_info->tx_buf = value;
p_spi_info->flag.busy = true;
(void)nrf_drv_spi_transfer(MASTER_INST(obj),
(uint8_t const *)&p_spi_info->tx_buf, 1,
(uint8_t *)&p_spi_info->rx_buf, 1);
while (p_spi_info->flag.busy) {
}
return p_spi_info->rx_buf;
}
int spi_slave_receive(spi_t *obj)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(!p_spi_info->master);
return p_spi_info->flag.readable;
;
}
int spi_slave_read(spi_t *obj)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(!p_spi_info->master);
while (!p_spi_info->flag.readable) {
}
p_spi_info->flag.readable = false;
return p_spi_info->rx_buf;
}
void spi_slave_write(spi_t *obj, int value)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(!p_spi_info->master);
p_spi_info->tx_buf = (uint8_t)value;
}
#if DEVICE_SPI_ASYNCH
void spi_master_transfer(spi_t *obj,
const void *tx, size_t tx_length,
void *rx, size_t rx_length, uint8_t bit_width,
uint32_t handler, uint32_t event, DMAUsage hint)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(p_spi_info->master);
(void)hint;
(void)bit_width;
p_spi_info->handler = handler;
p_spi_info->event = event;
p_spi_info->flag.busy = true;
(void)nrf_drv_spi_transfer(MASTER_INST(obj),
(uint8_t const *)tx, tx_length,
(uint8_t *)rx, rx_length);
}
uint32_t spi_irq_handler_asynch(spi_t *obj)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(p_spi_info->master);
return p_spi_info->event & SPI_EVENT_COMPLETE;
}
uint8_t spi_active(spi_t *obj)
{
spi_info_t *p_spi_info = SPI_INFO(obj);
MBED_ASSERT(p_spi_info->master);
return p_spi_info->flag.busy;
}
void spi_abort_asynch(spi_t *obj)
{
MBED_ASSERT(SPI_INFO(obj)->master);
nrf_drv_spi_abort(MASTER_INST(obj));
}
#endif // DEVICE_SPI_ASYNCH
#endif // DEVICE_SPI