@@ -105,7 +105,7 @@ static void spi_configure_driver_instance(spi_t *obj)
105
105
106
106
/* Clean up and uninitialize peripheral if already initialized. */
107
107
if (nordic_nrf5_spi_initialized [instance ]) {
108
- nrfx_spi_uninit (& nordic_nrf5_spi_instance [instance ]);
108
+ nrfx_spi_uninit (& nordic_nrf5_spi_instance [instance ]);
109
109
}
110
110
111
111
#if DEVICE_SPI_ASYNCH
@@ -128,6 +128,51 @@ static void spi_configure_driver_instance(spi_t *obj)
128
128
}
129
129
}
130
130
131
+ void spi_get_capabilities (PinName ssel , bool slave , spi_capabilities_t * cap )
132
+ {
133
+ if (slave ) {
134
+ cap -> minimum_frequency = 200000 ; // 200 kHz
135
+ cap -> maximum_frequency = 2000000 ; // 2 MHz
136
+ cap -> word_length = 0x00000080 ; // 8 bit symbols
137
+ cap -> support_slave_mode = false; // to be determined later based on ssel
138
+ cap -> hw_cs_handle = false; // irrelevant in slave mode
139
+ cap -> slave_delay_between_symbols_ns = 2500 ; // 2.5 us
140
+ cap -> clk_modes = 0x0f ; // all clock modes
141
+ #if DEVICE_SPI_ASYNCH
142
+ cap -> async_mode = true;
143
+ #else
144
+ cap -> async_mode = false;
145
+ #endif
146
+ } else {
147
+ cap -> minimum_frequency = 200000 ; // 200 kHz
148
+ cap -> maximum_frequency = 2000000 ; // 2 MHz
149
+ cap -> word_length = 0x00000080 ; // 8 bit symbols
150
+ cap -> support_slave_mode = false; // to be determined later based on ssel
151
+ cap -> hw_cs_handle = false; // to be determined later based on ssel
152
+ cap -> slave_delay_between_symbols_ns = 0 ; // irrelevant in master mode
153
+ cap -> clk_modes = 0x0f ; // all clock modes
154
+ #if DEVICE_SPI_ASYNCH
155
+ cap -> async_mode = true;
156
+ #else
157
+ cap -> async_mode = false;
158
+ #endif
159
+ }
160
+
161
+ // check if given ssel pin is in the cs pinmap
162
+ const PinMap * cs_pins = spi_master_cs_pinmap ();
163
+ PinName pin = NC ;
164
+ while (cs_pins -> pin != NC ) {
165
+ if (cs_pins -> pin == ssel ) {
166
+ #if DEVICE_SPISLAVE
167
+ cap -> support_slave_mode = true;
168
+ #endif
169
+ cap -> hw_cs_handle = true;
170
+ break ;
171
+ }
172
+ cs_pins ++ ;
173
+ }
174
+ }
175
+
131
176
/** Initialize the SPI peripheral
132
177
*
133
178
* Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
@@ -191,7 +236,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
191
236
/* Register interrupt handlers in driver with the NVIC. */
192
237
NVIC_SetVector (SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn , (uint32_t ) SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler );
193
238
NVIC_SetVector (SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn , (uint32_t ) SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler );
194
- NVIC_SetVector (SPIM2_SPIS2_SPI2_IRQn , (uint32_t ) SPIM2_SPIS2_SPI2_IRQHandler );
239
+ NVIC_SetVector (SPIM2_SPIS2_SPI2_IRQn , (uint32_t ) SPIM2_SPIS2_SPI2_IRQHandler );
195
240
}
196
241
}
197
242
@@ -247,13 +292,13 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
247
292
nrf_spi_mode_t new_mode = NRF_SPI_MODE_0 ;
248
293
249
294
/* Convert Mbed HAL mode to Nordic mode. */
250
- if (mode == 0 ) {
295
+ if (mode == 0 ) {
251
296
new_mode = NRF_SPI_MODE_0 ;
252
- } else if (mode == 1 ) {
297
+ } else if (mode == 1 ) {
253
298
new_mode = NRF_SPI_MODE_1 ;
254
- } else if (mode == 2 ) {
299
+ } else if (mode == 2 ) {
255
300
new_mode = NRF_SPI_MODE_2 ;
256
- } else if (mode == 3 ) {
301
+ } else if (mode == 3 ) {
257
302
new_mode = NRF_SPI_MODE_3 ;
258
303
}
259
304
@@ -351,8 +396,9 @@ int spi_master_write(spi_t *obj, int value)
351
396
desc .rx_length = 1 ;
352
397
ret = nrfx_spi_xfer (& nordic_nrf5_spi_instance [instance ], & desc , 0 );
353
398
354
- if (ret != NRFX_SUCCESS )
399
+ if (ret != NRFX_SUCCESS ) {
355
400
DEBUG_PRINTF ("%d error returned from nrf_spi_xfer\n\r" );
401
+ }
356
402
357
403
/* Manually set chip select pin if defined. */
358
404
if (spi_inst -> cs != NC ) {
@@ -421,25 +467,25 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, cha
421
467
int tx_actual_length = (tx_length > 255 ) ? 255 : tx_length ;
422
468
423
469
/* Set tx buffer pointer. Set to NULL if no data is going to be transmitted. */
424
- const uint8_t * tx_actual_buffer = (tx_actual_length > 0 ) ?
425
- (const uint8_t * )(tx_buffer + tx_offset ) :
426
- NULL ;
470
+ const uint8_t * tx_actual_buffer = (tx_actual_length > 0 ) ?
471
+ (const uint8_t * )(tx_buffer + tx_offset ) :
472
+ NULL ;
427
473
428
474
/* Check if rx_length is larger than 255 and if so, limit to 255. */
429
475
int rx_actual_length = (rx_length > 255 ) ? 255 : rx_length ;
430
476
431
477
/* Set rx buffer pointer. Set to NULL if no data is going to be received. */
432
- uint8_t * rx_actual_buffer = (rx_actual_length > 0 ) ?
433
- (uint8_t * )(rx_buffer + rx_offset ) :
434
- NULL ;
478
+ uint8_t * rx_actual_buffer = (rx_actual_length > 0 ) ?
479
+ (uint8_t * )(rx_buffer + rx_offset ) :
480
+ NULL ;
435
481
436
482
/* Blocking transfer. */
437
483
desc .p_tx_buffer = tx_actual_buffer ;
438
484
desc .p_rx_buffer = rx_actual_buffer ;
439
485
desc .tx_length = tx_actual_length ;
440
486
desc .rx_length = rx_actual_length ;
441
487
result = nrfx_spi_xfer (& nordic_nrf5_spi_instance [instance ],
442
- & desc , 0 );
488
+ & desc , 0 );
443
489
444
490
/* Update loop variables. */
445
491
tx_length -= tx_actual_length ;
@@ -596,7 +642,7 @@ static ret_code_t spi_master_transfer_async_continue(spi_t *obj)
596
642
desc .rx_length = rx_length ;
597
643
598
644
ret_code_t result = nrfx_spi_xfer (& nordic_nrf5_spi_instance [obj -> spi .instance ],
599
- & desc , 0 );
645
+ & desc , 0 );
600
646
return result ;
601
647
}
602
648
@@ -662,7 +708,7 @@ static void nordic_nrf5_spi_event_handler(nrfx_spi_evt_t const *p_event, void *p
662
708
callback ();
663
709
}
664
710
665
- /* Transfer failed, signal error if mask is set. */
711
+ /* Transfer failed, signal error if mask is set. */
666
712
} else if (signal_error ) {
667
713
668
714
/* Signal error if event mask matches and event handler is set. */
@@ -721,7 +767,7 @@ void spi_master_transfer(spi_t *obj,
721
767
struct buffer_s * buffer_pointer ;
722
768
723
769
buffer_pointer = & obj -> tx_buff ;
724
- buffer_pointer -> buffer = (void * ) tx ;
770
+ buffer_pointer -> buffer = (void * ) tx ;
725
771
buffer_pointer -> length = tx_length ;
726
772
buffer_pointer -> pos = 0 ;
727
773
buffer_pointer -> width = 8 ;
0 commit comments