8
8
#include <zephyr/drivers/spi/rtio.h>
9
9
#include <zephyr/drivers/pinctrl.h>
10
10
#include <zephyr/drivers/gpio.h>
11
+ #include <dmm.h>
11
12
#include <soc.h>
12
13
#include <nrfx_spis.h>
13
14
#include <zephyr/pm/device.h>
@@ -40,6 +41,7 @@ struct spi_nrfx_config {
40
41
#endif
41
42
const struct pinctrl_dev_config * pcfg ;
42
43
struct gpio_dt_spec wake_gpio ;
44
+ void * mem_reg ;
43
45
};
44
46
45
47
static inline nrf_spis_mode_t get_nrf_spis_mode (uint16_t operation )
@@ -125,7 +127,11 @@ static int prepare_for_transfer(const struct device *dev,
125
127
uint8_t * rx_buf , size_t rx_buf_len )
126
128
{
127
129
const struct spi_nrfx_config * dev_config = dev -> config ;
130
+ struct spi_nrfx_data * dev_data = dev -> data ;
128
131
nrfx_err_t result ;
132
+ uint8_t * dmm_tx_buf ;
133
+ uint8_t * dmm_rx_buf ;
134
+ int err ;
129
135
130
136
if (tx_buf_len > dev_config -> max_buf_len ||
131
137
rx_buf_len > dev_config -> max_buf_len ) {
@@ -134,14 +140,36 @@ static int prepare_for_transfer(const struct device *dev,
134
140
return - EINVAL ;
135
141
}
136
142
143
+ err = dmm_buffer_out_prepare (dev_config -> mem_reg , tx_buf , tx_buf_len , (void * * )& dmm_tx_buf );
144
+ if (err != 0 ) {
145
+ LOG_ERR ("DMM TX allocation failed err=%d" , err );
146
+ goto out_alloc_failed ;
147
+ }
148
+
149
+ /* Keep user RX buffer address to copy data from DMM RX buffer on transfer completion. */
150
+ dev_data -> ctx .rx_buf = rx_buf ;
151
+ err = dmm_buffer_in_prepare (dev_config -> mem_reg , rx_buf , rx_buf_len , (void * * )& dmm_rx_buf );
152
+ if (err != 0 ) {
153
+ LOG_ERR ("DMM RX allocation failed err=%d" , err );
154
+ goto in_alloc_failed ;
155
+ }
156
+
137
157
result = nrfx_spis_buffers_set (& dev_config -> spis ,
138
- tx_buf , tx_buf_len ,
139
- rx_buf , rx_buf_len );
158
+ dmm_tx_buf , tx_buf_len ,
159
+ dmm_rx_buf , rx_buf_len );
140
160
if (result != NRFX_SUCCESS ) {
141
- return - EIO ;
161
+ err = - EIO ;
162
+ goto buffers_set_failed ;
142
163
}
143
164
144
165
return 0 ;
166
+
167
+ buffers_set_failed :
168
+ dmm_buffer_in_release (dev_config -> mem_reg , rx_buf , rx_buf_len , rx_buf );
169
+ in_alloc_failed :
170
+ dmm_buffer_out_release (dev_config -> mem_reg , (void * )tx_buf );
171
+ out_alloc_failed :
172
+ return err ;
145
173
}
146
174
147
175
static void wake_callback (const struct device * dev , struct gpio_callback * cb ,
@@ -288,9 +316,22 @@ static DEVICE_API(spi, spi_nrfx_driver_api) = {
288
316
289
317
static void event_handler (const nrfx_spis_evt_t * p_event , void * p_context )
290
318
{
291
- struct spi_nrfx_data * dev_data = p_context ;
319
+ const struct device * dev = p_context ;
320
+ struct spi_nrfx_data * dev_data = dev -> data ;
321
+ const struct spi_nrfx_config * dev_config = dev -> config ;
292
322
293
323
if (p_event -> evt_type == NRFX_SPIS_XFER_DONE ) {
324
+ int err ;
325
+
326
+
327
+ err = dmm_buffer_out_release (dev_config -> mem_reg , p_event -> p_tx_buf );
328
+ (void )err ;
329
+ __ASSERT_NO_MSG (err == 0 );
330
+
331
+ err = dmm_buffer_in_release (dev_config -> mem_reg , dev_data -> ctx .rx_buf ,
332
+ p_event -> rx_amount , p_event -> p_rx_buf );
333
+ __ASSERT_NO_MSG (err == 0 );
334
+
294
335
spi_context_complete (& dev_data -> ctx , dev_data -> dev ,
295
336
p_event -> rx_amount );
296
337
@@ -361,7 +402,7 @@ static int spi_nrfx_init(const struct device *dev)
361
402
* actually used are set in configure() when a transfer is prepared.
362
403
*/
363
404
result = nrfx_spis_init (& dev_config -> spis , & dev_config -> config ,
364
- event_handler , dev_data );
405
+ event_handler , ( void * ) dev );
365
406
366
407
if (result != NRFX_SUCCESS ) {
367
408
LOG_ERR ("Failed to initialize device: %s" , dev -> name );
@@ -416,7 +457,10 @@ static int spi_nrfx_init(const struct device *dev)
416
457
* - Name-based HAL IRQ handlers, e.g. nrfx_spis_0_irq_handler
417
458
*/
418
459
419
- #define SPIS (idx ) DT_NODELABEL(spi##idx)
460
+ #define SPIS_NODE (idx ) COND_CODE_1(IS_EQ(idx, 120), (spis##idx), (spi##idx))
461
+
462
+ #define SPIS (idx ) DT_NODELABEL(SPIS_NODE(idx))
463
+
420
464
#define SPIS_PROP (idx , prop ) DT_PROP(SPIS(idx), prop)
421
465
422
466
#define SPI_NRFX_SPIS_DEFINE (idx ) \
@@ -453,6 +497,7 @@ static int spi_nrfx_init(const struct device *dev)
453
497
(.gpd_ctrl = NRF_PERIPH_GET_FREQUENCY(SPIS(idx)) > \
454
498
NRFX_MHZ_TO_HZ(16UL),)) \
455
499
.wake_gpio = GPIO_DT_SPEC_GET_OR(SPIS(idx), wake_gpios, {0}), \
500
+ .mem_reg = DMM_DEV_TO_REG(SPIS(idx)), \
456
501
}; \
457
502
BUILD_ASSERT(!DT_NODE_HAS_PROP(SPIS(idx), wake_gpios) || \
458
503
!(DT_GPIO_FLAGS(SPIS(idx), wake_gpios) & GPIO_ACTIVE_LOW),\
0 commit comments