summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/nuc980/rtt_port/drv_i2s.c
blob: ea6344dcb90fe3e92bc9cefd4d085dd448fc3dd8 (plain)
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
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author           Notes
* 2020-12-12      Wayne Lin        First version
*
******************************************************************************/

#include <rtconfig.h>

#if defined(BSP_USING_I2S)

#include <rtdevice.h>
#include <drv_i2s.h>
#include "NuMicro.h"

/* Private define ---------------------------------------------------------------*/
#define DBG_ENABLE
#define DBG_LEVEL DBG_LOG
#define DBG_SECTION_NAME  "i2s"
#define DBG_COLOR
#include <rtdbg.h>

#define I2S_RSR_R_DMA_RIA_IRQ_Pos   (0)
#define I2S_RSR_R_DMA_RIA_IRQ_Msk   (1<<I2S_RSR_R_DMA_RIA_IRQ_Pos)
#define I2S_RSR_R_DMA_RIA_SN_Pos    (5)
#define I2S_RSR_R_DMA_RIA_SN_Msk    (7<<I2S_RSR_R_DMA_RIA_SN_Pos)

#define I2S_GLBCON_P_DMA_IRQ_Pos       (10)
#define I2S_GLBCON_P_DMA_IRQ_Msk       (1<<I2S_GLBCON_P_DMA_IRQ_Pos)
#define I2S_GLBCON_R_DMA_IRQ_Pos       (11)
#define I2S_GLBCON_R_DMA_IRQ_Msk       (1<<I2S_GLBCON_R_DMA_IRQ_Pos)

#define I2S_PSP_P_DMA_RIA_I_Pos        (0)
#define I2S_PSP_P_DMA_RIA_I_Msk        (1<<I2S_PSP_P_DMA_RIA_I_Pos)
#define I2S_PSP_DMA_DATA_ZERO_IRQ_Pos  (3)
#define I2S_PSP_DMA_DATA_ZERO_IRQ_Msk  (1<<I2S_PSP_DMA_DATA_ZERO_IRQ_Pos)
#define I2S_PSP_DMA_CNTER_IRQ_Pos      (4)
#define I2S_PSP_DMA_CNTER_IRQ_Msk      (1<<I2S_PSP_DMA_CNTER_IRQ_Pos)

#define I2S_RESET_PLAY_Pos        (5)
#define I2S_RESET_PLAY_Msk        (1<<I2S_RESET_PLAY_Pos)
#define I2S_RESET_RECORD_Pos      (6)
#define I2S_RESET_RECORD_Msk      (1<<I2S_RESET_RECORD_Pos)

/* Private functions ------------------------------------------------------------*/
static rt_err_t nu_i2s_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps);
static rt_err_t nu_i2s_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps);
static rt_err_t nu_i2s_init(struct rt_audio_device *audio);
static rt_err_t nu_i2s_start(struct rt_audio_device *audio, int stream);
static rt_err_t nu_i2s_stop(struct rt_audio_device *audio, int stream);
static void nu_i2s_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info);
/* Public functions -------------------------------------------------------------*/
rt_err_t nu_i2s_acodec_register(nu_acodec_ops_t);

/* Private variables ------------------------------------------------------------*/
static struct nu_i2s g_nu_i2s_dev =
{
    .name = "sound0",
    .irqn = IRQ_I2S,
    .rstidx = I2SRST,
    .clkidx = I2SCKEN,
};

static void nu_i2s_isr(int vector, void *param)
{
    nu_i2s_t psNuI2s = (nu_i2s_t)param;
    volatile uint32_t u32RegAudCtl = inpw(REG_I2S_CON);

    if (u32RegAudCtl & I2S_GLBCON_P_DMA_IRQ_Msk)
    {
        volatile uint32_t u32RegPlayStatus = inpw(REG_I2S_PSR);
        outpw(REG_I2S_CON, u32RegAudCtl | I2S_GLBCON_P_DMA_IRQ_Msk);  //Clear TX INT

        if (u32RegPlayStatus & I2S_PSP_DMA_CNTER_IRQ_Msk)
        {
            outpw(REG_I2S_PSR, I2S_PSP_DMA_CNTER_IRQ_Msk);
            rt_kprintf("\ndebug:DMA_COUNTER_IRQ occur\n");
        }

        if (u32RegPlayStatus & I2S_PSP_DMA_DATA_ZERO_IRQ_Msk)
        {
            outpw(REG_I2S_PSR, I2S_PSP_DMA_DATA_ZERO_IRQ_Msk);
            rt_kprintf("\ndebug:DMA_DATA_ZERO_IRQ occur\n");
        }

        if (u32RegPlayStatus & I2S_PSP_P_DMA_RIA_I_Msk)
        {
            /* Clear Playback status of DMA reach indicate address interrupt. */
            outpw(REG_I2S_PSR, I2S_PSP_P_DMA_RIA_I_Msk);
            rt_audio_tx_complete(&psNuI2s->audio);
        }
    }

    if (u32RegAudCtl & I2S_GLBCON_R_DMA_IRQ_Msk)
    {
        volatile uint32_t u32RegRecordStatus = inpw(REG_I2S_RSR);

        outpw(REG_I2S_CON, u32RegAudCtl | I2S_GLBCON_R_DMA_IRQ_Msk);  //Clear RX INT

        /* Record DMA Reach Indicative Address Interrupt Request Bit */
        /* 0 = Record DMA address does not reach the indicative address by R_DMA_IRQ_SEL */
        /* 1 = Record DMA address does reach the indicative address by R_DMA_IRQ_SEL */
        /* Note: This bit is readable, and can only be cleared by writing '1' to it. */
        if (u32RegRecordStatus & I2S_RSR_R_DMA_RIA_IRQ_Msk)
        {
            nu_i2s_dai_t psNuI2sDai = &psNuI2s->i2s_dais[NU_I2S_DAI_CAPTURE];

            /*
            Record DMA Reach Indicative Address Section Number Bit (Read Only)
            R_DMA_IRQ_SEL (I2S_GLBCON[15:14]) = 01, R_DMA_RIA_SN[2:0]= 1, 0.
            R_DMA_IRQ_SEL (I2S_GLBCON[15:14]) = 10, R_DMA_RIA_SN[2:0]= 1, 2, 3, 0.
            R_DMA_IRQ_SEL (I2S_GLBCON[15:14]) = 11, R_DMA_RIA_SN[2:0]= 1, 2, 3, 4, 5, 6, 7, 0.
            */
            uint8_t u8FifoBlockIdx = (u32RegRecordStatus & I2S_RSR_R_DMA_RIA_SN_Msk) >> I2S_RSR_R_DMA_RIA_SN_Pos;

            rt_uint8_t *pbuf = (uint8_t *)((uint32_t)&psNuI2sDai->fifo[u8FifoBlockIdx * NU_I2S_DMA_BUF_BLOCK_SIZE] | NONCACHEABLE);

            /* Clear Record status of DMA reach indicate address interrupt. */
            outpw(REG_I2S_RSR, I2S_RSR_R_DMA_RIA_IRQ_Msk);

            /* Report upper layer. */
            rt_audio_rx_done(&psNuI2s->audio, pbuf, NU_I2S_DMA_BUF_BLOCK_SIZE);
        }
    }

}

static rt_bool_t nu_i2s_capacity_check(struct rt_audio_configure *pconfig)
{
    switch (pconfig->samplebits)
    {
    case  8:
    case 16:
    case 24:
        break;
    default:
        goto exit_nu_i2s_capacity_check;
    }

    switch (pconfig->channels)
    {
    case 1:
    case 2:
        break;
    default:
        goto exit_nu_i2s_capacity_check;
    }

    return RT_TRUE;

exit_nu_i2s_capacity_check:

    return RT_FALSE;
}

static rt_err_t nu_i2s_dai_setup(nu_i2s_t psNuI2s, struct rt_audio_configure *pconfig)
{
    rt_err_t result = RT_EOK;
    nu_acodec_ops_t pNuACodecOps = RT_NULL;
    RT_ASSERT(psNuI2s->AcodecOps != RT_NULL);
    pNuACodecOps = psNuI2s->AcodecOps;

    /* Open I2S */
    if (nu_i2s_capacity_check(pconfig) == RT_TRUE)
    {
        /* Reset audio codec */
        if (pNuACodecOps->nu_acodec_reset)
            result = pNuACodecOps->nu_acodec_reset();

        if (result != RT_EOK)
            goto exit_nu_i2s_dai_setup;

        /* Setup audio codec */
        if (pNuACodecOps->nu_acodec_init)
            result = pNuACodecOps->nu_acodec_init();

        if (!pNuACodecOps->nu_acodec_init || result != RT_EOK)
            goto exit_nu_i2s_dai_setup;

        /* Setup acodec samplerate/samplebit/channel */
        if (pNuACodecOps->nu_acodec_dsp_control)
            result = pNuACodecOps->nu_acodec_dsp_control(pconfig);

        if (!pNuACodecOps->nu_acodec_dsp_control || result != RT_EOK)
            goto exit_nu_i2s_dai_setup;

        /* Open I2S */
        if (i2sOpen() != 0)
            goto exit_nu_i2s_dai_setup;

        /* Select I2S function */
        i2sIoctl(I2S_SELECT_BLOCK, I2S_BLOCK_I2S, 0);

        /* Select Data width */
        i2sIoctl(I2S_SELECT_BIT, ((pconfig->samplebits / 8) - 1), 0);

        if (pconfig->channels > 1)
        {
            /* Set to stereo */
            i2sIoctl(I2S_SET_CHANNEL, I2S_PLAY, I2S_CHANNEL_P_I2S_TWO);
            i2sIoctl(I2S_SET_CHANNEL, I2S_REC, I2S_CHANNEL_R_I2S_TWO);
        }
        else
        {
            /* Set to mono */
            i2sIoctl(I2S_SET_CHANNEL, I2S_PLAY, I2S_CHANNEL_P_I2S_ONE);
            i2sIoctl(I2S_SET_CHANNEL, I2S_REC, I2S_CHANNEL_R_I2S_LEFT_PCM_SLOT0);
        }

        /* Set DMA interrupt selection to half of DMA buffer */
        switch (NU_I2S_DMA_BUF_BLOCK_NUMBER)
        {
        case 2:
            i2sIoctl(I2S_SET_PLAY_DMA_INT_SEL, I2S_DMA_INT_HALF, 0);
            i2sIoctl(I2S_SET_REC_DMA_INT_SEL, I2S_DMA_INT_HALF, 0);
            break;
        case 4:
            i2sIoctl(I2S_SET_PLAY_DMA_INT_SEL, I2S_DMA_INT_QUARTER, 0);
            i2sIoctl(I2S_SET_REC_DMA_INT_SEL, I2S_DMA_INT_QUARTER, 0);
            break;
        case 8:
            i2sIoctl(I2S_SET_PLAY_DMA_INT_SEL, I2S_DMA_INT_EIGHTH, 0);
            i2sIoctl(I2S_SET_REC_DMA_INT_SEL, I2S_DMA_INT_EIGHTH, 0);
            break;
        default:
            RT_ASSERT(0);
            break;
        }

        /* Set DMA buffer address */
        i2sIoctl(I2S_SET_DMA_ADDRESS, I2S_PLAY, (uint32_t)&psNuI2s->i2s_dais[NU_I2S_DAI_PLAYBACK].fifo[0]);
        i2sIoctl(I2S_SET_DMA_ADDRESS, I2S_REC, (uint32_t)&psNuI2s->i2s_dais[NU_I2S_DAI_CAPTURE].fifo[0]);

        /* Set DMA buffer length */
        i2sIoctl(I2S_SET_DMA_LENGTH, I2S_PLAY, NU_I2S_DMA_FIFO_SIZE);
        i2sIoctl(I2S_SET_DMA_LENGTH, I2S_REC, NU_I2S_DMA_FIFO_SIZE);

        /* Select I2S format */
        i2sIoctl(I2S_SET_I2S_FORMAT, I2S_FORMAT_I2S, 0);

        if (psNuI2s->AcodecOps->role == NU_ACODEC_ROLE_MASTER)
        {
            // Set as slave, source clock is XIN (12MHz)
            i2sIoctl(I2S_SET_MODE, I2S_MODE_SLAVE, 0);
        }
        else
        {
            if (pconfig->samplerate % 11025)
            {
                // 12.288MHz ==> APLL=98.4MHz / 8 = 12.3MHz
                // APLL is 98.4MHz
                /*
                     FB_DV  = 0x28 ->  N=FB_DV+1 -> N=41
                     IN_DV  = 0    ->  M=IN_DV+1 -> M=1
                     OUT_DV = 4    ->  P=4+1     -> P=5
                     Fpllout = 12MHz * N / (M*P) -> Fpllout = 12MHz * 41 / (5*1) = 98.4 MHz
                */
                outpw(REG_CLK_APLLCON, 0xC0008028);

                // Select APLL as I2S source and divider is (7+1)
                outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0x001f0000) | (0x2 << 19) | (0x7 << 24));

                // Set sampleing rate, data width, channel
                i2sSetSampleRate(12300000, pconfig->samplerate, pconfig->samplebits, pconfig->channels);
            }
            else
            {
                // 11.2896MHz ==> APLL=90MHz / 8 = 11.25MHz
                // APLL is 90MHz
                /*
                     FB_DV  = 0x2D ->  N=FB_DV+1 -> N=45
                     IN_DV  = 0    ->  M=IN_DV+1 -> M=1
                     OUT_DV = 5    ->  P=5+1     -> P=6
                     Fpllout = 12MHz * N / (M*P) -> Fpllout = 12MHz * 45 / (6*1) = 90 MHz
                */
                outpw(REG_CLK_APLLCON, 0xC000A02D);

                // Select APLL as I2S source and divider is (7+1)
                outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0x001f0000) | (0x2 << 19) | (0x7 << 24));

                // Set sampleing rate, data width, channel
                i2sSetSampleRate(11250000, pconfig->samplerate, pconfig->samplebits, pconfig->channels);
            }
            // Set as master
            i2sIoctl(I2S_SET_MODE, I2S_MODE_MASTER, 0);
        }

        LOG_I("Open I2S.");

        /* Set unmute */
        if (pNuACodecOps->nu_acodec_mixer_control)
            pNuACodecOps->nu_acodec_mixer_control(AUDIO_MIXER_MUTE, RT_FALSE);
    }
    else
        result = -RT_EINVAL;

exit_nu_i2s_dai_setup:

    return result;
}

static rt_err_t nu_i2s_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
    rt_err_t result = RT_EOK;
    nu_i2s_t psNuI2s;
    nu_acodec_ops_t pNuACodecOps = RT_NULL;

    RT_ASSERT(audio != RT_NULL);
    RT_ASSERT(caps != RT_NULL);

    psNuI2s = (nu_i2s_t)audio;

    RT_ASSERT(psNuI2s->AcodecOps != RT_NULL);

    pNuACodecOps = psNuI2s->AcodecOps;

    switch (caps->main_type)
    {
    case AUDIO_TYPE_QUERY:
        switch (caps->sub_type)
        {
        case AUDIO_TYPE_QUERY:
            caps->udata.mask = AUDIO_TYPE_INPUT | AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
            break;
        default:
            result = -RT_ERROR;
            break;
        } // switch (caps->sub_type)
        break;

    case AUDIO_TYPE_MIXER:

        if (pNuACodecOps->nu_acodec_mixer_query)
        {
            switch (caps->sub_type)
            {
            case AUDIO_MIXER_QUERY:
                return pNuACodecOps->nu_acodec_mixer_query(AUDIO_MIXER_QUERY, &caps->udata.mask);

            default:
                return pNuACodecOps->nu_acodec_mixer_query(caps->sub_type, (rt_uint32_t *)&caps->udata.value);
            } // switch (caps->sub_type)

        } // if (pNuACodecOps->nu_acodec_mixer_query)

        result = -RT_ERROR;
        break;

    case AUDIO_TYPE_INPUT:
    case AUDIO_TYPE_OUTPUT:

        switch (caps->sub_type)
        {
        case AUDIO_DSP_PARAM:
            caps->udata.config.channels = psNuI2s->config.channels;
            caps->udata.config.samplebits = psNuI2s->config.samplebits;
            caps->udata.config.samplerate = psNuI2s->config.samplerate;
            break;
        case AUDIO_DSP_SAMPLERATE:
            caps->udata.config.samplerate = psNuI2s->config.samplerate;
            break;
        case AUDIO_DSP_CHANNELS:
            caps->udata.config.channels = psNuI2s->config.channels;
            break;
        case AUDIO_DSP_SAMPLEBITS:
            caps->udata.config.samplebits = psNuI2s->config.samplebits;
            break;
        default:
            result = -RT_ERROR;
            break;
        } // switch (caps->sub_type)
        break;

    default:
        result = -RT_ERROR;
        break;

    } // switch (caps->main_type)

    return result;
}

static rt_err_t nu_i2s_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
    rt_err_t result = RT_EOK;
    nu_i2s_t psNuI2s;
    nu_acodec_ops_t pNuACodecOps = RT_NULL;
    int stream = -1;

    RT_ASSERT(audio != RT_NULL);
    RT_ASSERT(caps != RT_NULL);

    psNuI2s = (nu_i2s_t)audio;

    RT_ASSERT(psNuI2s->AcodecOps != RT_NULL);
    pNuACodecOps = psNuI2s->AcodecOps;

    switch (caps->main_type)
    {
    case AUDIO_TYPE_MIXER:
        if (psNuI2s->AcodecOps->nu_acodec_mixer_control)
            psNuI2s->AcodecOps->nu_acodec_mixer_control(caps->sub_type, caps->udata.value);
        break;


    case AUDIO_TYPE_INPUT:
        stream = AUDIO_STREAM_RECORD;
    case AUDIO_TYPE_OUTPUT:
    {
        rt_bool_t bNeedReset = RT_FALSE;

        if (stream < 0)
            stream = AUDIO_STREAM_REPLAY;

        switch (caps->sub_type)
        {
        case AUDIO_DSP_PARAM:
            if (rt_memcmp(&psNuI2s->config, &caps->udata.config, sizeof(struct rt_audio_configure)) != 0)
            {
                rt_memcpy(&psNuI2s->config, &caps->udata.config, sizeof(struct rt_audio_configure));
                bNeedReset = RT_TRUE;
            }
            break;
        case AUDIO_DSP_SAMPLEBITS:
            if (psNuI2s->config.samplerate != caps->udata.config.samplebits)
            {
                psNuI2s->config.samplerate = caps->udata.config.samplebits;
                bNeedReset = RT_TRUE;
            }
            break;
        case AUDIO_DSP_CHANNELS:
            if (psNuI2s->config.channels != caps->udata.config.channels)
            {
                pNuACodecOps->config.channels = caps->udata.config.channels;
                bNeedReset = RT_TRUE;
            }
            break;
        case AUDIO_DSP_SAMPLERATE:
            if (psNuI2s->config.samplerate != caps->udata.config.samplerate)
            {
                psNuI2s->config.samplerate = caps->udata.config.samplerate;
                bNeedReset = RT_TRUE;
            }
            break;
        default:
            result = -RT_ERROR;
            break;
        } // switch (caps->sub_type)

        if (bNeedReset)
        {
            return nu_i2s_start(audio, stream);
        }
    }
    break;
    default:
        result = -RT_ERROR;
        break;
    } // switch (caps->main_type)

    return result;
}

static rt_err_t nu_i2s_init(struct rt_audio_device *audio)
{
    rt_err_t result = RT_EOK;
    nu_i2s_t psNuI2s;

    RT_ASSERT(audio != RT_NULL);

    psNuI2s = (nu_i2s_t)audio;

    /* Enable IP engine clock */
    nu_sys_ipclk_enable(psNuI2s->clkidx);

    /* Reset IP engine */
    nu_sys_ip_reset(psNuI2s->rstidx);

    /* Enable interrupt */
    rt_hw_interrupt_umask(psNuI2s->irqn);

    return -(result);
}

static rt_err_t nu_i2s_start(struct rt_audio_device *audio, int stream)
{
    nu_i2s_t psNuI2s;

    RT_ASSERT(audio != RT_NULL);

    psNuI2s = (nu_i2s_t)audio;

    /* Restart all: I2S and codec. */
    nu_i2s_stop(audio, stream);
    if (nu_i2s_dai_setup(psNuI2s, &psNuI2s->config) != RT_EOK)
        return -RT_ERROR;

    switch (stream)
    {
    case AUDIO_STREAM_REPLAY:
    {
        i2sIoctl(I2S_SET_PLAY, I2S_START_PLAY, 0);

        LOG_I("Start replay.");
    }
    break;

    case AUDIO_STREAM_RECORD:
    {
        i2sIoctl(I2S_SET_RECORD, I2S_START_REC, 0);

        LOG_I("Start record.");
    }
    break;

    default:
        return -RT_ERROR;
    }

    return RT_EOK;
}

static rt_err_t nu_i2s_stop(struct rt_audio_device *audio, int stream)
{
    nu_i2s_t psNuI2s;
    nu_i2s_dai_t psNuI2sDai = RT_NULL;

    RT_ASSERT(audio != RT_NULL);

    psNuI2s = (nu_i2s_t)audio;

    switch (stream)
    {
    case AUDIO_STREAM_REPLAY:
        psNuI2sDai = &psNuI2s->i2s_dais[NU_I2S_DAI_PLAYBACK];

        i2sIoctl(I2S_SET_PLAY, I2S_STOP_PLAY, 0);

        LOG_I("Stop replay.");
        break;

    case AUDIO_STREAM_RECORD:
        psNuI2sDai = &psNuI2s->i2s_dais[NU_I2S_DAI_CAPTURE];

        i2sIoctl(I2S_SET_RECORD, I2S_STOP_REC, 0);

        LOG_I("Stop record.");
        break;

    default:
        return -RT_EINVAL;
    }

    /* Close I2S if record and playback path. */
    if (!((inpw(REG_I2S_RESET)&I2S_RESET_PLAY_Msk) || (inpw(REG_I2S_RESET)&I2S_RESET_RECORD_Msk)))
    {
        i2sClose();
        LOG_I("Close I2S.");
    }

    /* Silence */
    rt_memset((void *)psNuI2sDai->fifo, 0, NU_I2S_DMA_FIFO_SIZE);

    return RT_EOK;
}

static void nu_i2s_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
{
    nu_i2s_t psNuI2s;

    RT_ASSERT(audio != RT_NULL);
    RT_ASSERT(info != RT_NULL);

    psNuI2s = (nu_i2s_t)audio;

    /* Define it a NONCACHEABLE address. */
    info->buffer = (rt_uint8_t *)((uint32_t)psNuI2s->i2s_dais[NU_I2S_DAI_PLAYBACK].fifo | NONCACHEABLE) ;
    info->total_size = NU_I2S_DMA_FIFO_SIZE;
    info->block_size = NU_I2S_DMA_BUF_BLOCK_SIZE;
    info->block_count = NU_I2S_DMA_BUF_BLOCK_NUMBER;

    return;
}

static struct rt_audio_ops nu_i2s_audio_ops =
{
    .getcaps     = nu_i2s_getcaps,
    .configure   = nu_i2s_configure,

    .init        = nu_i2s_init,
    .start       = nu_i2s_start,
    .stop        = nu_i2s_stop,
    .transmit    = RT_NULL,
    .buffer_info = nu_i2s_buffer_info
};

int rt_hw_i2s_init(void)
{
    int i = 0;
    nu_i2s_dai_t psNuI2sDai;

    for (i = 0; i < NU_I2S_DAI_CNT; i++)
    {
        psNuI2sDai = &g_nu_i2s_dev.i2s_dais[i];

        /* Allocate playback and record FIFO buffer. */
        psNuI2sDai->fifo = (uint8_t *)rt_malloc_align(NU_I2S_DMA_FIFO_SIZE, 32);
        RT_ASSERT(psNuI2sDai->fifo != RT_NULL);

        rt_memset(psNuI2sDai->fifo, 0, NU_I2S_DMA_FIFO_SIZE);
    }

    /* Register ops of audio device */
    g_nu_i2s_dev.audio.ops  = &nu_i2s_audio_ops;

    /* Register device, RW: it is with replay and record functions. */
    rt_audio_register(&g_nu_i2s_dev.audio, g_nu_i2s_dev.name, RT_DEVICE_FLAG_RDWR, &g_nu_i2s_dev);

    /* Register I2S ISR */
    rt_hw_interrupt_install(g_nu_i2s_dev.irqn, nu_i2s_isr, &g_nu_i2s_dev, g_nu_i2s_dev.name);

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_i2s_init);
#endif //#if defined(BSP_USING_I2S)