44
44
#include "pinmap_ex.h"
45
45
#include "PeripheralPins.h"
46
46
47
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
48
+ #include "nrfx_spim.h"
49
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
47
50
#include "nrfx_spi.h"
51
+ #endif
48
52
49
53
#if 0
50
54
#define DEBUG_PRINTF (...) printf(__VA_ARGS__)
51
55
#else
52
56
#define DEBUG_PRINTF (...)
53
57
#endif
54
58
59
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
60
+ /* Pre-allocate instances and share them globally. */
61
+ static const nrfx_spim_t nordic_nrf5_spim_instance [4 ] = {
62
+ NRFX_SPIM_INSTANCE (0 ),
63
+ NRFX_SPIM_INSTANCE (1 ),
64
+ NRFX_SPIM_INSTANCE (2 ),
65
+ NRFX_SPIM_INSTANCE (3 )
66
+ };
67
+
68
+ /* Keep track of which instance has been initialized. */
69
+ static bool nordic_nrf5_spi_initialized [4 ] = { false, false, false, false };
70
+
71
+ /* Forware declare interrupt handler. */
72
+ #if DEVICE_SPI_ASYNCH
73
+ static void nordic_nrf5_spi_event_handler (nrfx_spim_evt_t const * p_event , void * p_context );
74
+ #endif
75
+
76
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
77
+
55
78
/* Pre-allocate instances and share them globally. */
56
79
static const nrfx_spi_t nordic_nrf5_spi_instance [3 ] = {
57
80
NRFX_SPI_INSTANCE (0 ),
@@ -66,6 +89,7 @@ static bool nordic_nrf5_spi_initialized[3] = { false, false, false };
66
89
#if DEVICE_SPI_ASYNCH
67
90
static void nordic_nrf5_spi_event_handler (nrfx_spi_evt_t const * p_event , void * p_context );
68
91
#endif
92
+ #endif //NRFX_SPI_ENABLED
69
93
70
94
/* Forward declaration. These functions are implemented in the driver but not
71
95
* set up in the NVIC due to it being relocated.
@@ -74,6 +98,15 @@ void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void);
74
98
void SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler (void );
75
99
void SPIM2_SPIS2_SPI2_IRQHandler (void );
76
100
101
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
102
+
103
+ /* Forward declaration. These functions are implemented in the driver but not
104
+ * set up in the NVIC due to it being relocated.
105
+ */
106
+ void SPIM3_IRQHandler (void );
107
+
108
+ #endif // NRFX_SPIM_ENABLED
109
+
77
110
/**
78
111
* Brief Reconfigure peripheral.
79
112
*
@@ -105,19 +138,36 @@ static void spi_configure_driver_instance(spi_t *obj)
105
138
106
139
/* Clean up and uninitialize peripheral if already initialized. */
107
140
if (nordic_nrf5_spi_initialized [instance ]) {
141
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
142
+ nrfx_spim_uninit (& nordic_nrf5_spim_instance [instance ]);
143
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
108
144
nrfx_spi_uninit (& nordic_nrf5_spi_instance [instance ]);
145
+ #endif
109
146
}
110
147
111
148
#if DEVICE_SPI_ASYNCH
112
149
/* Set callback handler in asynchronous mode. */
113
150
if (spi_inst -> handler ) {
151
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
152
+ nrfx_spim_init (& nordic_nrf5_spim_instance [instance ], & (spi_inst -> config ), nordic_nrf5_spi_event_handler , obj );
153
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
114
154
nrfx_spi_init (& nordic_nrf5_spi_instance [instance ], & (spi_inst -> config ), nordic_nrf5_spi_event_handler , obj );
155
+ #endif
115
156
} else {
157
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
158
+ nrfx_spim_init (& nordic_nrf5_spim_instance [instance ], & (spi_inst -> config ), NULL , NULL );
159
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
116
160
nrfx_spi_init (& nordic_nrf5_spi_instance [instance ], & (spi_inst -> config ), NULL , NULL );
161
+ #endif
117
162
}
118
163
#else
119
164
/* Set callback handler to NULL in synchronous mode. */
120
- nrfx_spi_init (& nordic_nrf5_spi_instance [instance ], & (spi_inst -> config ), NULL , NULL );
165
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
166
+ nrfx_spim_init (& nordic_nrf5_spim_instance [instance ], & (spi_inst -> config ), NULL , NULL );
167
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
168
+ nrfx_spi_init (& nordic_nrf5_spim_instance [instance ], & (spi_inst -> config ), NULL , NULL );
169
+ #endif
170
+
121
171
#endif
122
172
123
173
/* Mark instance as initialized. */
@@ -194,7 +244,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
194
244
195
245
/* Get instance based on requested pins. */
196
246
spi_inst -> instance = pin_instance_spi (mosi , miso , sclk );
247
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
248
+ MBED_ASSERT (spi_inst -> instance < NRFX_SPIM_ENABLED_COUNT );
249
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
197
250
MBED_ASSERT (spi_inst -> instance < NRFX_SPI_ENABLED_COUNT );
251
+ #endif
198
252
199
253
/* Store chip select separately for manual enabling. */
200
254
spi_inst -> cs = ssel ;
@@ -203,14 +257,24 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
203
257
spi_inst -> config .sck_pin = sclk ;
204
258
spi_inst -> config .mosi_pin = mosi ;
205
259
spi_inst -> config .miso_pin = miso ;
260
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
261
+ spi_inst -> config .ss_pin = NRFX_SPIM_PIN_NOT_USED ;
262
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
206
263
spi_inst -> config .ss_pin = NRFX_SPI_PIN_NOT_USED ;
207
-
264
+ #endif
208
265
/* Use the default config. */
209
266
spi_inst -> config .irq_priority = SPI_DEFAULT_CONFIG_IRQ_PRIORITY ;
210
267
spi_inst -> config .orc = SPI_FILL_CHAR ;
268
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
269
+ spi_inst -> config .frequency = NRF_SPIM_FREQ_4M ;
270
+ spi_inst -> config .mode = NRF_SPIM_MODE_0 ;
271
+ spi_inst -> config .bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST ;
272
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
211
273
spi_inst -> config .frequency = NRF_SPI_FREQ_4M ;
212
274
spi_inst -> config .mode = NRF_SPI_MODE_0 ;
213
275
spi_inst -> config .bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST ;
276
+ #endif
277
+
214
278
215
279
#if DEVICE_SPI_ASYNCH
216
280
/* Set default values for asynchronous variables. */
@@ -239,6 +303,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
239
303
NVIC_SetVector (SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn , (uint32_t ) SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler );
240
304
NVIC_SetVector (SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn , (uint32_t ) SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler );
241
305
NVIC_SetVector (SPIM2_SPIS2_SPI2_IRQn , (uint32_t ) SPIM2_SPIS2_SPI2_IRQHandler );
306
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
307
+ NVIC_SetVector (SPIM3_IRQn , (uint32_t ) SPIM3_IRQHandler );
308
+ #endif
242
309
}
243
310
}
244
311
@@ -263,7 +330,11 @@ void spi_free(spi_t *obj)
263
330
int instance = spi_inst -> instance ;
264
331
265
332
/* Use driver uninit to free instance. */
333
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
334
+ nrfx_spim_uninit (& nordic_nrf5_spim_instance [instance ]);
335
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
266
336
nrfx_spi_uninit (& nordic_nrf5_spi_instance [instance ]);
337
+ #endif
267
338
268
339
/* Mark instance as uninitialized. */
269
340
nordic_nrf5_spi_initialized [instance ] = false;
@@ -291,6 +362,20 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
291
362
struct spi_s * spi_inst = obj ;
292
363
#endif
293
364
365
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
366
+ nrf_spim_mode_t new_mode = NRF_SPIM_MODE_0 ;
367
+
368
+ /* Convert Mbed HAL mode to Nordic mode. */
369
+ if (mode == 0 ) {
370
+ new_mode = NRF_SPIM_MODE_0 ;
371
+ } else if (mode == 1 ) {
372
+ new_mode = NRF_SPIM_MODE_1 ;
373
+ } else if (mode == 2 ) {
374
+ new_mode = NRF_SPIM_MODE_2 ;
375
+ } else if (mode == 3 ) {
376
+ new_mode = NRF_SPIM_MODE_3 ;
377
+ }
378
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
294
379
nrf_spi_mode_t new_mode = NRF_SPI_MODE_0 ;
295
380
296
381
/* Convert Mbed HAL mode to Nordic mode. */
@@ -303,6 +388,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
303
388
} else if (mode == 3 ) {
304
389
new_mode = NRF_SPI_MODE_3 ;
305
390
}
391
+ #endif
306
392
307
393
/* Check if configuration has changed. */
308
394
if (spi_inst -> config .mode != new_mode ) {
@@ -333,6 +419,30 @@ void spi_frequency(spi_t *obj, int hz)
333
419
struct spi_s * spi_inst = obj ;
334
420
#endif
335
421
422
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
423
+ nrf_spim_frequency_t new_frequency = NRF_SPIM_FREQ_1M ;
424
+
425
+ /* Convert frequency to Nordic enum type. */
426
+ if (hz < 250000 ) {
427
+ new_frequency = NRF_SPIM_FREQ_125K ;
428
+ } else if (hz < 500000 ) {
429
+ new_frequency = NRF_SPIM_FREQ_250K ;
430
+ } else if (hz < 1000000 ) {
431
+ new_frequency = NRF_SPIM_FREQ_500K ;
432
+ } else if (hz < 2000000 ) {
433
+ new_frequency = NRF_SPIM_FREQ_1M ;
434
+ } else if (hz < 4000000 ) {
435
+ new_frequency = NRF_SPIM_FREQ_2M ;
436
+ } else if (hz < 8000000 ) {
437
+ new_frequency = NRF_SPIM_FREQ_4M ;
438
+ } else if (hz < 16000000 ) {
439
+ new_frequency = NRF_SPIM_FREQ_8M ;
440
+ } else if (hz < 32000000 ) {
441
+ new_frequency = NRF_SPIM_FREQ_16M ;
442
+ } else {
443
+ new_frequency = NRF_SPIM_FREQ_32M ;
444
+ }
445
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
336
446
nrf_spi_frequency_t new_frequency = NRF_SPI_FREQ_1M ;
337
447
338
448
/* Convert frequency to Nordic enum type. */
@@ -351,7 +461,7 @@ void spi_frequency(spi_t *obj, int hz)
351
461
} else {
352
462
new_frequency = NRF_SPI_FREQ_8M ;
353
463
}
354
-
464
+ #endif
355
465
/* Check if configuration has changed. */
356
466
if (spi_inst -> config .frequency != new_frequency ) {
357
467
spi_inst -> config .frequency = new_frequency ;
@@ -370,7 +480,12 @@ void spi_frequency(spi_t *obj, int hz)
370
480
int spi_master_write (spi_t * obj , int value )
371
481
{
372
482
nrfx_err_t ret ;
373
- nrfx_spi_xfer_desc_t desc ;
483
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
484
+ nrfx_spim_xfer_desc_t desc ;
485
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
486
+ nrfx_spi_xfer_desc_t desc ;
487
+ #endif
488
+
374
489
#if DEVICE_SPI_ASYNCH
375
490
struct spi_s * spi_inst = & obj -> spi ;
376
491
#else
@@ -396,7 +511,11 @@ int spi_master_write(spi_t *obj, int value)
396
511
desc .p_rx_buffer = & rx_buff ;
397
512
desc .tx_length = 1 ;
398
513
desc .rx_length = 1 ;
514
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
515
+ ret = nrfx_spim_xfer (& nordic_nrf5_spim_instance [instance ], & desc , 0 );
516
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
399
517
ret = nrfx_spi_xfer (& nordic_nrf5_spi_instance [instance ], & desc , 0 );
518
+ #endif
400
519
401
520
if (ret != NRFX_SUCCESS ) {
402
521
DEBUG_PRINTF ("%d error returned from nrf_spi_xfer\n\r" );
@@ -428,7 +547,12 @@ int spi_master_write(spi_t *obj, int value)
428
547
*/
429
548
int spi_master_block_write (spi_t * obj , const char * tx_buffer , int tx_length , char * rx_buffer , int rx_length , char write_fill )
430
549
{
550
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
551
+ nrfx_spim_xfer_desc_t desc ;
552
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
431
553
nrfx_spi_xfer_desc_t desc ;
554
+ #endif
555
+
432
556
#if DEVICE_SPI_ASYNCH
433
557
struct spi_s * spi_inst = & obj -> spi ;
434
558
#else
@@ -486,9 +610,13 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, cha
486
610
desc .p_rx_buffer = rx_actual_buffer ;
487
611
desc .tx_length = tx_actual_length ;
488
612
desc .rx_length = rx_actual_length ;
613
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
614
+ result = nrfx_spim_xfer (& nordic_nrf5_spim_instance [instance ],
615
+ & desc , 0 );
616
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
489
617
result = nrfx_spi_xfer (& nordic_nrf5_spi_instance [instance ],
490
618
& desc , 0 );
491
-
619
+ #endif
492
620
/* Update loop variables. */
493
621
tx_length -= tx_actual_length ;
494
622
tx_offset += tx_actual_length ;
@@ -623,7 +751,11 @@ void spi_slave_write(spi_t *obj, int value)
623
751
624
752
static ret_code_t spi_master_transfer_async_continue (spi_t * obj )
625
753
{
754
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
755
+ nrfx_spim_xfer_desc_t desc ;
756
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
626
757
nrfx_spi_xfer_desc_t desc ;
758
+ #endif
627
759
/* Remaining data to be transferred. */
628
760
size_t tx_length = obj -> tx_buff .length - obj -> tx_buff .pos ;
629
761
size_t rx_length = obj -> rx_buff .length - obj -> rx_buff .pos ;
@@ -643,13 +775,20 @@ static ret_code_t spi_master_transfer_async_continue(spi_t *obj)
643
775
desc .tx_length = tx_length ;
644
776
desc .rx_length = rx_length ;
645
777
646
- ret_code_t result = nrfx_spi_xfer (& nordic_nrf5_spi_instance [obj -> spi .instance ],
647
- & desc , 0 );
778
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
779
+ ret_code_t result = nrfx_spim_xfer (& nordic_nrf5_spim_instance [obj -> spi .instance ], & desc , 0 );
780
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
781
+ ret_code_t result = nrfx_spi_xfer (& nordic_nrf5_spi_instance [obj -> spi .instance ], & desc , 0 );
782
+ #endif
648
783
return result ;
649
784
}
650
785
651
786
/* Callback function for driver calls. This is called from ISR context. */
787
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
788
+ static void nordic_nrf5_spi_event_handler (nrfx_spim_evt_t const * p_event , void * p_context )
789
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
652
790
static void nordic_nrf5_spi_event_handler (nrfx_spi_evt_t const * p_event , void * p_context )
791
+ #endif
653
792
{
654
793
// Only safe to use with mbed-printf.
655
794
//DEBUG_PRINTF("nordic_nrf5_twi_event_handler: %d %p\r\n", p_event->type, p_context);
@@ -660,7 +799,11 @@ static void nordic_nrf5_spi_event_handler(nrfx_spi_evt_t const *p_event, void *p
660
799
spi_t * obj = (spi_t * ) p_context ;
661
800
struct spi_s * spi_inst = & obj -> spi ;
662
801
802
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
803
+ if (p_event -> type == NRFX_SPIM_EVENT_DONE ) {
804
+ #elif NRFX_CHECK(NRFX_SPI_ENABLED)
663
805
if (p_event -> type == NRFX_SPI_EVENT_DONE ) {
806
+ #endif
664
807
665
808
/* Update buffers with new positions. */
666
809
obj -> tx_buff .pos += p_event -> xfer_desc .tx_length ;
@@ -859,12 +1002,14 @@ void spi_abort_asynch(spi_t *obj)
859
1002
int instance = obj -> spi .instance ;
860
1003
861
1004
/* Abort transfer. */
1005
+ #if NRFX_CHECK (NRFX_SPIM_ENABLED )
1006
+ nrfx_spim_abort (& nordic_nrf5_spim_instance [instance ]);
1007
+ #elif NRFX_CHECK (NRFX_SPI_ENABLED )
862
1008
nrfx_spi_abort (& nordic_nrf5_spi_instance [instance ]);
863
-
1009
+ #endif
864
1010
/* Force reconfiguration. */
865
1011
object_owner_spi2c_set (instance , NULL );
866
1012
}
867
1013
868
1014
#endif // DEVICE_SPI_ASYNCH
869
-
870
1015
#endif // DEVICE_SPI
0 commit comments