/
nrfx_i2s.c
635 lines (546 loc) · 21.3 KB
/
nrfx_i2s.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
* Copyright (c) 2015 - 2024, Nordic Semiconductor ASA
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* 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 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 the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 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 <nrfx.h>
#if NRFX_CHECK(NRFX_I2S_ENABLED)
#include <nrfx_i2s.h>
#include <haly/nrfy_gpio.h>
#define NRFX_LOG_MODULE I2S
#include <nrfx_log.h>
#define EVT_TO_STR(event) \
(event == NRF_I2S_EVENT_RXPTRUPD ? "NRF_I2S_EVENT_RXPTRUPD" : \
(event == NRF_I2S_EVENT_TXPTRUPD ? "NRF_I2S_EVENT_TXPTRUPD" : \
(event == NRF_I2S_EVENT_STOPPED ? "NRF_I2S_EVENT_STOPPED" : \
"UNKNOWN EVENT")))
#if !defined(USE_WORKAROUND_FOR_I2S_STOP_ANOMALY) && \
(defined(NRF52_SERIES) || defined(NRF91_SERIES))
// Enable workaround for nRF52 Series anomaly 194 / nRF9160 anomaly 1
// (STOP task does not switch off all resources).
#define USE_WORKAROUND_FOR_I2S_STOP_ANOMALY 1
#endif
#if !defined(USE_WORKAROUND_FOR_ANOMALY_170) && defined(NRF52_SERIES)
// Enable workaround for nRF52 Series anomaly 170
// (when reading the value of PSEL registers, the CONNECT field might not
// return the same value that has been written to it).
#define USE_WORKAROUND_FOR_ANOMALY_170 1
#endif
#if !defined(USE_WORKAROUND_FOR_ANOMALY_196) && defined(NRF52_SERIES)
// Enable workaround for nRF52 Series anomaly 196
// (PSEL acquires GPIO regardless of ENABLE).
#define USE_WORKAROUND_FOR_ANOMALY_196 1
#endif
// Control block - driver instance local data.
typedef struct
{
nrfx_i2s_data_handler_t handler;
nrfx_drv_state_t state;
bool use_rx : 1;
bool use_tx : 1;
bool rx_ready : 1;
bool tx_ready : 1;
bool buffers_needed : 1;
bool buffers_reused : 1;
bool skip_gpio_cfg : 1;
bool skip_psel_cfg : 1;
#if !NRFX_API_VER_AT_LEAST(3, 3, 0)
uint16_t buffer_size;
#endif
nrfx_i2s_buffers_t next_buffers;
nrfx_i2s_buffers_t current_buffers;
} nrfx_i2s_cb_t;
static nrfx_i2s_cb_t m_cb[NRFX_I2S_ENABLED_COUNT];
static void configure_pins(nrfx_i2s_config_t const * p_config)
{
if (!p_config->skip_gpio_cfg)
{
// Configure pins used by the peripheral:
// - SCK and LRCK (required) - depending on the mode of operation these
// pins are configured as outputs (in Master mode) or inputs (in Slave
// mode).
if (p_config->mode == NRF_I2S_MODE_MASTER)
{
nrfy_gpio_cfg_output(p_config->sck_pin);
nrfy_gpio_cfg_output(p_config->lrck_pin);
#if NRF_GPIO_HAS_CLOCKPIN && defined(NRF_I2S_CLOCKPIN_SCK_NEEDED)
nrfy_gpio_pin_clock_set(p_config->sck_pin, true);
#endif
#if NRF_GPIO_HAS_CLOCKPIN && defined(NRF_I2S_CLOCKPIN_LRCK_NEEDED)
nrfy_gpio_pin_clock_set(p_config->lrck_pin, true);
#endif
}
else
{
nrfy_gpio_cfg_input(p_config->sck_pin, NRF_GPIO_PIN_NOPULL);
nrfy_gpio_cfg_input(p_config->lrck_pin, NRF_GPIO_PIN_NOPULL);
}
// - MCK (optional) - always output,
if (p_config->mck_pin != NRF_I2S_PIN_NOT_CONNECTED)
{
nrfy_gpio_cfg_output(p_config->mck_pin);
#if NRF_GPIO_HAS_CLOCKPIN && defined(NRF_I2S_CLOCKPIN_MCK_NEEDED)
nrfy_gpio_pin_clock_set(p_config->mck_pin, true);
#endif
}
// - SDOUT (optional) - always output,
if (p_config->sdout_pin != NRF_I2S_PIN_NOT_CONNECTED)
{
nrfy_gpio_cfg_output(p_config->sdout_pin);
}
// - SDIN (optional) - always input.
if (p_config->sdin_pin != NRF_I2S_PIN_NOT_CONNECTED)
{
nrfy_gpio_cfg_input(p_config->sdin_pin, NRF_GPIO_PIN_NOPULL);
}
}
}
static void deconfigure_pins(nrfx_i2s_t const * p_instance)
{
nrf_i2s_pins_t pins;
nrfy_i2s_pins_get(p_instance->p_reg, &pins);
#if NRFX_CHECK(USE_WORKAROUND_FOR_ANOMALY_170)
// Create bitmask for extracting pin number from PSEL register.
uint32_t pin_mask = NRF_I2S_PSEL_SCK_PIN_MASK;
#if NRF_I2S_HAS_GPIO_PORT_SELECTION
// If device supports more than one GPIO port, take port number into account as well.
pin_mask |= NRF_I2S_PSEL_SCK_PORT_MASK;
#endif
#else
uint32_t pin_mask = 0xFFFFFFFF;
#endif // USE_WORKAROUND_FOR_ANOMALY_170
nrfy_gpio_cfg_default(pins.sck_pin & pin_mask);
nrfy_gpio_cfg_default(pins.lrck_pin & pin_mask);
if (pins.mck_pin != NRF_I2S_PIN_NOT_CONNECTED)
{
nrfy_gpio_cfg_default(pins.mck_pin & pin_mask);
}
if (pins.sdout_pin != NRF_I2S_PIN_NOT_CONNECTED)
{
nrfy_gpio_cfg_default(pins.sdout_pin & pin_mask);
}
if (pins.sdin_pin != NRF_I2S_PIN_NOT_CONNECTED)
{
nrfy_gpio_cfg_default(pins.sdin_pin & pin_mask);
}
}
static inline bool validate_config(nrf_i2s_mode_t mode,
nrf_i2s_ratio_t ratio,
nrf_i2s_swidth_t swidth)
{
// The MCK/LRCK ratio has to be a multiple of 2 * sample width.
if (mode == NRF_I2S_MODE_MASTER)
{
if (swidth == NRF_I2S_SWIDTH_16BIT)
{
if (ratio == NRF_I2S_RATIO_48X)
{
return false;
}
}
if (swidth == NRF_I2S_SWIDTH_24BIT)
{
if ((ratio == NRF_I2S_RATIO_32X) ||
(ratio == NRF_I2S_RATIO_64X) ||
(ratio == NRF_I2S_RATIO_128X) ||
(ratio == NRF_I2S_RATIO_256X) ||
(ratio == NRF_I2S_RATIO_512X))
{
return false;
}
}
#if NRF_I2S_HAS_SWIDTH_32BIT
if (swidth == NRF_I2S_SWIDTH_32BIT)
{
if ((ratio == NRF_I2S_RATIO_32X) ||
(ratio == NRF_I2S_RATIO_48X) ||
(ratio == NRF_I2S_RATIO_96X))
{
return false;
}
}
#endif
}
return true;
}
nrfx_err_t nrfx_i2s_init(nrfx_i2s_t const * p_instance,
nrfx_i2s_config_t const * p_config,
nrfx_i2s_data_handler_t handler)
{
NRFX_ASSERT(p_config);
NRFX_ASSERT(handler);
nrfx_err_t err_code;
nrfx_i2s_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
if (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED)
{
#if NRFX_API_VER_AT_LEAST(3, 2, 0)
err_code = NRFX_ERROR_ALREADY;
#else
err_code = NRFX_ERROR_INVALID_STATE;
#endif
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
if (!validate_config(p_config->mode,
p_config->ratio,
p_config->sample_width))
{
err_code = NRFX_ERROR_INVALID_PARAM;
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
configure_pins(p_config);
nrfy_i2s_config_t nrfy_config =
{
.config = {
.mode = p_config->mode,
.format = p_config->format,
.alignment = p_config->alignment,
.sample_width = p_config->sample_width,
.channels = p_config->channels,
.mck_setup = p_config->mck_setup,
.ratio = p_config->ratio,
},
.pins = {
.sck_pin = p_config->sck_pin,
.lrck_pin = p_config->lrck_pin,
.mck_pin = p_config->mck_pin,
.sdout_pin = p_config->sdout_pin,
.sdin_pin = p_config->sdin_pin,
},
#if NRF_I2S_HAS_CLKCONFIG
.clksrc = p_config->clksrc,
.enable_bypass = p_config->enable_bypass,
#endif
.skip_psel_cfg = p_config->skip_psel_cfg
};
nrfy_i2s_periph_configure(p_instance->p_reg, &nrfy_config);
p_cb->handler = handler;
p_cb->skip_gpio_cfg = p_config->skip_gpio_cfg;
p_cb->skip_psel_cfg = p_config->skip_psel_cfg;
nrfy_i2s_int_init(p_instance->p_reg,
NRF_I2S_INT_RXPTRUPD_MASK |
NRF_I2S_INT_TXPTRUPD_MASK |
NRF_I2S_INT_STOPPED_MASK,
p_config->irq_priority,
false);
p_cb->state = NRFX_DRV_STATE_INITIALIZED;
NRFX_LOG_INFO("Initialized.");
return NRFX_SUCCESS;
}
void nrfx_i2s_uninit(nrfx_i2s_t const * p_instance)
{
nrfx_i2s_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
nrfx_i2s_stop(p_instance);
nrfy_i2s_int_uninit(p_instance->p_reg);
nrfy_i2s_disable(p_instance->p_reg);
if (!p_cb->skip_gpio_cfg)
{
deconfigure_pins(p_instance);
}
#if NRFX_CHECK(USE_WORKAROUND_FOR_ANOMALY_196)
if (!p_cb->skip_psel_cfg)
{
// Disabling I2S is insufficient to release pins acquired by the peripheral.
// Explicit disconnect is needed.
nrf_i2s_pins_t pins = {
.sck_pin = NRF_I2S_PIN_NOT_CONNECTED,
.lrck_pin = NRF_I2S_PIN_NOT_CONNECTED,
.mck_pin = NRF_I2S_PIN_NOT_CONNECTED,
.sdout_pin = NRF_I2S_PIN_NOT_CONNECTED,
.sdin_pin = NRF_I2S_PIN_NOT_CONNECTED,
};
nrfy_i2s_pins_set(p_instance->p_reg, &pins);
}
#endif
p_cb->state = NRFX_DRV_STATE_UNINITIALIZED;
NRFX_LOG_INFO("Uninitialized.");
}
bool nrfx_i2s_init_check(nrfx_i2s_t const * p_instance)
{
nrfx_i2s_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
return (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
}
nrfx_err_t nrfx_i2s_start(nrfx_i2s_t const * p_instance,
nrfx_i2s_buffers_t const * p_initial_buffers,
#if !NRFX_API_VER_AT_LEAST(3, 3, 0)
uint16_t buffer_size,
#endif
uint8_t flags)
{
NRFX_ASSERT(p_initial_buffers != NULL);
NRFX_ASSERT(p_initial_buffers->p_rx_buffer != NULL ||
p_initial_buffers->p_tx_buffer != NULL);
NRFX_ASSERT((p_initial_buffers->p_rx_buffer == NULL) ||
(nrfx_is_in_ram(p_initial_buffers->p_rx_buffer) &&
nrfx_is_word_aligned(p_initial_buffers->p_rx_buffer)));
NRFX_ASSERT((p_initial_buffers->p_tx_buffer == NULL) ||
(nrfx_is_in_ram(p_initial_buffers->p_tx_buffer) &&
nrfx_is_word_aligned(p_initial_buffers->p_tx_buffer)));
#if NRFX_API_VER_AT_LEAST(3, 3, 0)
NRFX_ASSERT(p_initial_buffers->buffer_size != 0);
#else
NRFX_ASSERT(buffer_size != 0);
#endif
(void)(flags);
nrfx_err_t err_code;
nrfx_i2s_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
if (p_cb->state != NRFX_DRV_STATE_INITIALIZED)
{
err_code = NRFX_ERROR_INVALID_STATE;
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
if (((p_initial_buffers->p_rx_buffer != NULL)
&& !nrfx_is_in_ram(p_initial_buffers->p_rx_buffer))
||
((p_initial_buffers->p_tx_buffer != NULL)
&& !nrfx_is_in_ram(p_initial_buffers->p_tx_buffer)))
{
err_code = NRFX_ERROR_INVALID_ADDR;
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
p_cb->use_rx = (p_initial_buffers->p_rx_buffer != NULL);
p_cb->use_tx = (p_initial_buffers->p_tx_buffer != NULL);
p_cb->rx_ready = false;
p_cb->tx_ready = false;
p_cb->buffers_needed = false;
#if !NRFX_API_VER_AT_LEAST(3, 3, 0)
p_cb->buffer_size = buffer_size;
#endif
// Set the provided initial buffers as next, they will become the current
// ones after the IRQ handler is called for the first time, what will occur
// right after the START task is triggered.
p_cb->next_buffers = *p_initial_buffers;
p_cb->current_buffers.p_rx_buffer = NULL;
p_cb->current_buffers.p_tx_buffer = NULL;
nrfy_i2s_enable(p_instance->p_reg);
p_cb->state = NRFX_DRV_STATE_POWERED_ON;
/* Clear spurious RXPTRUPD and TXPTRUPD events (see nRF52 anomaly 55). */
nrfy_i2s_event_clear(p_instance->p_reg, NRF_I2S_EVENT_RXPTRUPD);
nrfy_i2s_event_clear(p_instance->p_reg, NRF_I2S_EVENT_TXPTRUPD);
nrfy_i2s_int_enable(p_instance->p_reg,
(p_cb->use_rx ? NRF_I2S_INT_RXPTRUPD_MASK : 0UL) |
(p_cb->use_tx ? NRF_I2S_INT_TXPTRUPD_MASK : 0UL) |
NRF_I2S_INT_STOPPED_MASK);
#if NRFX_API_VER_AT_LEAST(3, 3, 0)
nrfy_i2s_buffers_set(p_instance->p_reg, &p_cb->next_buffers);
#else
const nrfy_i2s_xfer_desc_t xfer = {
.p_rx_buffer = p_cb->next_buffers.p_rx_buffer,
.p_tx_buffer = p_cb->next_buffers.p_tx_buffer,
.buffer_size = p_cb->buffer_size,
};
nrfy_i2s_buffers_set(p_instance->p_reg, &xfer);
#endif
nrfy_i2s_xfer_start(p_instance->p_reg, NULL);
NRFX_LOG_INFO("Started.");
return NRFX_SUCCESS;
}
nrfx_err_t nrfx_i2s_next_buffers_set(nrfx_i2s_t const * p_instance,
nrfx_i2s_buffers_t const * p_buffers)
{
nrfx_err_t err_code;
nrfx_i2s_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
NRFX_ASSERT(p_cb->state == NRFX_DRV_STATE_POWERED_ON);
NRFX_ASSERT(p_buffers);
NRFX_ASSERT((p_buffers->p_rx_buffer == NULL) ||
(nrfx_is_in_ram(p_buffers->p_rx_buffer) &&
nrfx_is_word_aligned(p_buffers->p_rx_buffer)));
NRFX_ASSERT((p_buffers->p_tx_buffer == NULL) ||
(nrfx_is_in_ram(p_buffers->p_tx_buffer) &&
nrfx_is_word_aligned(p_buffers->p_tx_buffer)));
#if NRFX_API_VER_AT_LEAST(3, 3, 0)
NRFX_ASSERT(p_buffers->buffer_size != 0);
#endif
if (!p_cb->buffers_needed)
{
err_code = NRFX_ERROR_INVALID_STATE;
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
if (((p_buffers->p_rx_buffer != NULL)
&& !nrfx_is_in_ram(p_buffers->p_rx_buffer))
||
((p_buffers->p_tx_buffer != NULL)
&& !nrfx_is_in_ram(p_buffers->p_tx_buffer)))
{
err_code = NRFX_ERROR_INVALID_ADDR;
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
if (p_cb->use_tx)
{
NRFX_ASSERT(p_buffers->p_tx_buffer != NULL);
}
if (p_cb->use_rx)
{
NRFX_ASSERT(p_buffers->p_rx_buffer != NULL);
}
#if NRFX_API_VER_AT_LEAST(3, 3, 0)
nrfy_i2s_buffers_set(p_instance->p_reg, p_buffers);
#else
nrfy_i2s_xfer_desc_t xfer = {
.p_rx_buffer = p_buffers->p_rx_buffer,
.p_tx_buffer = p_buffers->p_tx_buffer,
.buffer_size = p_cb->buffer_size,
};
nrfy_i2s_buffers_set(p_instance->p_reg, &xfer);
#endif
p_cb->next_buffers = *p_buffers;
p_cb->buffers_needed = false;
return NRFX_SUCCESS;
}
void nrfx_i2s_stop(nrfx_i2s_t const * p_instance)
{
nrfx_i2s_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
p_cb->buffers_needed = false;
// First disable interrupts, then trigger the STOP task, so no spurious
// RXPTRUPD and TXPTRUPD events (see nRF52 anomaly 55) are processed.
nrfy_i2s_int_disable(p_instance->p_reg, NRF_I2S_INT_RXPTRUPD_MASK |
NRF_I2S_INT_TXPTRUPD_MASK);
nrfy_i2s_abort(p_instance->p_reg, NULL);
#if NRFX_CHECK(USE_WORKAROUND_FOR_I2S_STOP_ANOMALY)
*((volatile uint32_t *)(((uint32_t)p_instance->p_reg) + 0x38)) = 1;
*((volatile uint32_t *)(((uint32_t)p_instance->p_reg) + 0x3C)) = 1;
#endif
}
static void irq_handler(NRF_I2S_Type * p_reg, nrfx_i2s_cb_t * p_cb)
{
uint32_t event_mask;
nrfy_i2s_xfer_desc_t * p_xfer;
#if NRFX_API_VER_AT_LEAST(3, 3, 0)
p_xfer = &p_cb->current_buffers;
#else
nrfy_i2s_xfer_desc_t xfer = {
.p_rx_buffer = p_cb->current_buffers.p_rx_buffer,
.p_tx_buffer = p_cb->current_buffers.p_tx_buffer,
.buffer_size = p_cb->buffer_size,
};
p_xfer = &xfer;
#endif
event_mask = nrfy_i2s_events_process(p_reg,
NRFY_EVENT_TO_INT_BITMASK(NRF_I2S_EVENT_TXPTRUPD) |
NRFY_EVENT_TO_INT_BITMASK(NRF_I2S_EVENT_RXPTRUPD) |
NRFY_EVENT_TO_INT_BITMASK(NRF_I2S_EVENT_STOPPED),
p_xfer);
if (event_mask & NRFY_EVENT_TO_INT_BITMASK(NRF_I2S_EVENT_TXPTRUPD))
{
p_cb->tx_ready = true;
if (p_cb->use_tx && p_cb->buffers_needed)
{
p_cb->buffers_reused = true;
}
}
if (event_mask & NRFY_EVENT_TO_INT_BITMASK(NRF_I2S_EVENT_RXPTRUPD))
{
p_cb->rx_ready = true;
if (p_cb->use_rx && p_cb->buffers_needed)
{
p_cb->buffers_reused = true;
}
}
if (event_mask & NRFY_EVENT_TO_INT_BITMASK(NRF_I2S_EVENT_STOPPED))
{
nrfy_i2s_int_disable(p_reg, NRF_I2S_INT_STOPPED_MASK);
nrfy_i2s_disable(p_reg);
// When stopped, release all buffers, including these scheduled for
// the next part of the transfer, and signal that the transfer has
// finished.
p_cb->handler(&p_cb->current_buffers, 0);
// Change the state of the driver before calling the handler with
// the flag signaling that the transfer has finished, so that it is
// possible to start a new transfer directly from the handler function.
p_cb->state = NRFX_DRV_STATE_INITIALIZED;
NRFX_LOG_INFO("Stopped.");
p_cb->handler(&p_cb->next_buffers, NRFX_I2S_STATUS_TRANSFER_STOPPED);
}
else
{
// Check if the requested transfer has been completed:
// - full-duplex mode
if ((p_cb->use_tx && p_cb->use_rx &&
p_cb->tx_ready && p_cb->rx_ready) ||
// - TX only mode
(!p_cb->use_rx && p_cb->tx_ready) ||
// - RX only mode
(!p_cb->use_tx && p_cb->rx_ready))
{
p_cb->tx_ready = false;
p_cb->rx_ready = false;
// If the application did not supply the buffers for the next
// part of the transfer until this moment, the current buffers
// cannot be released, since the I2S peripheral already started
// using them. Signal this situation to the application by
// passing NULL instead of the structure with released buffers.
if (p_cb->buffers_reused)
{
p_cb->buffers_reused = false;
// This will most likely be set at this point. However, there is
// a small time window between TXPTRUPD and RXPTRUPD events,
// and it is theoretically possible that next buffers will be
// set in this window, so to be sure this flag is set to true,
// set it explicitly.
p_cb->buffers_needed = true;
p_cb->handler(NULL, NRFX_I2S_STATUS_NEXT_BUFFERS_NEEDED);
}
else
{
// Buffers that have been used by the I2S peripheral (current)
// are now released and will be returned to the application,
// and the ones scheduled to be used as next become the current
// ones.
nrfx_i2s_buffers_t released_buffers = p_cb->current_buffers;
p_cb->current_buffers = p_cb->next_buffers;
p_cb->next_buffers.p_rx_buffer = NULL;
p_cb->next_buffers.p_tx_buffer = NULL;
p_cb->buffers_needed = true;
p_cb->handler(&released_buffers, NRFX_I2S_STATUS_NEXT_BUFFERS_NEEDED);
}
}
}
}
NRFX_INSTANCE_IRQ_HANDLERS(I2S, i2s)
#endif // NRFX_CHECK(NRFX_I2S_ENABLED)