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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
|
/*
* Copyright (C) 2017-2024 Alibaba Group Holding Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <csi_config.h>
#include <sys_clk.h>
#include <drv/i2s.h>
#include <drv/irq.h>
extern uint16_t i2s_tx_hs_num[];
extern uint16_t i2s_rx_hs_num[];
void wj_i2s_dma_event_cb(csi_dma_ch_t *dma, csi_dma_event_t event, void *arg)
{
CSI_PARAM_CHK_NORETVAL(dma);
csi_i2s_t *i2s = (csi_i2s_t *)dma->parent;
if (event == DMA_EVENT_TRANSFER_ERROR) {/* DMA transfer ERROR */
if (i2s->tx_dma->ch_id == dma->ch_id) {
/* to do tx error action */
i2s->state.error = 1U;
if (i2s->callback) {
i2s->callback(i2s, I2S_EVENT_ERROR, i2s->arg);
}
} else if (i2s->rx_dma->ch_id == dma->ch_id) {
/* to do rx error action */
i2s->state.error = 1U;
if (i2s->callback) {
i2s->callback(i2s, I2S_EVENT_ERROR, i2s->arg);
}
}
} else if (event == DMA_EVENT_TRANSFER_DONE) {/* DMA transfer complete */
if ((i2s->tx_dma != NULL) && (i2s->tx_dma->ch_id == dma->ch_id)) {
/* to do tx action */
uint32_t read_len = i2s->tx_period;
i2s->tx_buf->read = (i2s->tx_buf->read + read_len) % i2s->tx_buf->size;
if (i2s->tx_buf->data_len == 0U) {
i2s->tx_buf->data_len = 0U;
if (i2s->callback) {
i2s->callback(i2s, I2S_EVENT_TX_BUFFER_EMPTY, i2s->arg);
}
} else {
i2s->tx_buf->data_len -= read_len;
}
///< TODO:调用csi_dma_ch_start开始DMA发送数据
if (i2s->callback) {
i2s->callback(i2s, I2S_EVENT_SEND_COMPLETE, i2s->arg);
}
} else {
soc_dcache_clean_invalid_range((unsigned long)(i2s->rx_buf->buffer + i2s->rx_buf->write), i2s->rx_period);
///< TODO:调用csi_dma_ch_start开启DMA接收数据
if (i2s->callback && i2s->rx_buf->data_len != 0) {
i2s->callback(i2s, I2S_EVENT_RECEIVE_COMPLETE, i2s->arg);
} else {
i2s->callback(i2s, I2S_EVENT_RX_BUFFER_FULL, i2s->arg);
}
}
}
}
/**
\brief I2S init
\param[in] i2s i2s handle to operate.
\param[in] idx i2s interface idx
\return error code
*/
csi_error_t csi_i2s_init(csi_i2s_t *i2s, uint32_t idx)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
///< 获取中断号、基地址等相关信息
ret = target_get(DEV_WJ_I2S_TAG, idx, &i2s->dev);
if (ret == CSI_OK) {
///< TODO:清除I2S的中断
}
i2s->rx_dma = NULL;
i2s->tx_dma = NULL;
i2s->rx_buf = NULL;
i2s->tx_buf = NULL;
i2s->state.error = 0U;
i2s->state.readable = 0U;
i2s->state.writeable = 0U;
i2s->priv = (void *)0U;
i2s->callback = NULL;
return ret;
}
/**
\brief I2S uninit
\param[in] i2s i2s handle to operate.
\return none
*/
void csi_i2s_uninit(csi_i2s_t *i2s)
{
CSI_PARAM_CHK_NORETVAL(i2s);
i2s->rx_dma = NULL;
i2s->tx_dma = NULL;
i2s->rx_buf = NULL;
i2s->tx_buf = NULL;
i2s->state.error = 0U;
i2s->state.readable = 0U;
i2s->state.writeable = 0U;
///< TODO:关闭I2S的使能
///< TODO:清除I2S的中断
}
/**
\brief I2S enable
\param[in] i2s i2s handle to operate.
\param[in] en 1 enable, 0 disable
\return none
*/
void csi_i2s_enable(csi_i2s_t *i2s, bool enable)
{
CSI_PARAM_CHK_NORETVAL(i2s);
if (enable) {
///< TODO:使能I2S
} else {
///< TODO:不使能I2S
}
}
/**
\brief I2S config
\param[in] i2s i2s handle to operate.
\param[in] config i2s config param
\return error code
*/
csi_error_t csi_i2s_format(csi_i2s_t *i2s, csi_i2s_format_t *format)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
CSI_PARAM_CHK(format, CSI_ERROR);
csi_error_t ret = CSI_OK;
switch (format->mode) {
case I2S_MODE_MASTER:
///< TODO:设置I2S为主机接收模式
///< TODO:设置I2S为主机发送模式
break;
case I2S_MODE_SLAVE:
///< TODO:设置I2S为从机接收模式
///< TODO:设置I2S为从机发送模式
break;
default:
ret = CSI_ERROR;
break;
}
switch (format->protocol) {
case I2S_PROTOCOL_I2S:
///< TODO:设置I2S的接收为I2S格式
///< TODO:设置I2S的发送为I2S格式
break;
case I2S_PROTOCOL_MSB_JUSTIFIED:
///< TODO:设置I2S的接收为left-justified格式
///< TODO:设置I2S的发送为left-justified格式
break;
case I2S_PROTOCOL_LSB_JUSTIFIED:
///< TODO:设置I2S的接收为right-justified格式
///< TODO:设置I2S的发送为right-justified格式
break;
case I2S_PROTOCOL_PCM:
///< TODO:设置I2S的接收为PCM格式
///< TODO:设置I2S的发送为PCM格式
break;
default:
ret = CSI_ERROR;
break;
}
switch (format->width) {
case I2S_SAMPLE_WIDTH_16BIT:
///< TODO:设置I2S的数据宽度为16位
break;
case I2S_SAMPLE_WIDTH_24BIT:
///< TODO:设置I2S的数据宽度为24位
break;
case I2S_SAMPLE_WIDTH_32BIT:
///< TODO:设置I2S的数据宽度为32位
break;
default:
ret = CSI_UNSUPPORTED;
break;
}
if (format->mode == I2S_MODE_MASTER) {
///< TODO:设置I2S获取wsclk对应的分频寄存器
///< TODO:设置I2S获取reference clock对应的分频寄存器为0
} else {
///< TODO:设置从机接收模式音频输入速率由硬件自动检测
}
if (format->polarity) {
///< TODO:配置发送模式高电平对应的时左声道的数据
///< TODO:配置接收模式高电平对应的时左声道的数据
} else {
///< TODO:配置发送模式高电平对应的时右声道的数据
///< TODO:配置接收模式高电平对应的时右声道的数据
}
switch (format->sclk_nfs) {
case I2S_SCLK_16FS:
///< TODO:配置接收模式sclk的频率为16*FS
break;
case I2S_SCLK_32FS:
///< TODO:配置接收模式sclk的频率为32*FS
break;
case I2S_SCLK_48FS:
///< TODO:配置接收模式sclk的频率为48*FS
format->mclk_nfs = I2S_MCLK_384FS; ///< if SCLK_SEL=01 then mclk_o=384*fs,select MCLK_SEL:384fs
break;
case I2S_SCLK_64FS:
///< TODO:配置接收模式sclk的频率为64*FS
break;
default:
break;
}
if (format->mclk_nfs == I2S_MCLK_384FS) {
///< TODO:配置接收模式mclk的频率为384*FS
} else {
///< TODO:配置接收模式mclk的频率为256*FS
}
return ret;
}
/**
\brief Set the i2s tx mono
\param[in] i2s i2s handle to operate.
\param[in] Mono channel selection.
\param[in] bool mono mode enable
\return error code
*/
csi_error_t csi_i2s_tx_select_sound_channel(csi_i2s_t *i2s, csi_i2s_sound_channel_t ch)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
switch (ch) {
case I2S_LEFT_CHANNEL:
///< TODO:配置发送数据模式数据源是单声道数据(左右声道数据相同)
///< TODO:配置发送模式低电平对应的数据是左声道的数据
break;
case I2S_RIGHT_CHANNEL:
///< TODO:配置发送数据模式数据源是单通道数据(左右声道数据相同)
///< TODO:配置发送模式高电平对应的数据是左声道的数据
break;
case I2S_LEFT_RIGHT_CHANNEL:
///< TODO:配置发送数据模式数据源是双声道数据
break;
default:
break;
}
return ret;
}
/**
\brief Set the i2s rx mono
\param[in] i2s i2s handle to operate.
\param[in] Mono channel selection.
\param[in] bool mono mode enable
\return error code
*/
csi_error_t csi_i2s_rx_select_sound_channel(csi_i2s_t *i2s, csi_i2s_sound_channel_t ch)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
switch (ch) {
case I2S_LEFT_CHANNEL:
///< TODO:配置接收数据模式数据源是单声道数据(左右声道数据相同)
///< TODO:配置接收模式接收的的数据是左声道的数据
break;
case I2S_RIGHT_CHANNEL:
///< TODO:配置接收数据模式数据源是单声道数据(左右声道数据相同)
///< TODO:配置接收模式接收的的数据是右声道的数据
break;
case I2S_LEFT_RIGHT_CHANNEL:
///< TODO:配置接收数据模式数据源是双声道数据
break;
default:
break;
}
return ret;
}
/**
\brief link DMA channel to i2s device
\param[in] i2s i2s handle to operate.
\param[in] rx_dma the DMA channel for receive, when it is NULL means to unused dma.
\return error code
*/
csi_error_t csi_i2s_rx_link_dma(csi_i2s_t *i2s, csi_dma_ch_t *rx_dma)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
if (rx_dma != NULL) {
rx_dma->parent = i2s;
/// 申请DMA通道
ret = csi_dma_ch_alloc(rx_dma, -1, -1);
if (ret == CSI_OK) {
/// 注册DMA传输完成回调函数
csi_dma_ch_attach_callback(rx_dma, wj_i2s_dma_event_cb, NULL);
i2s->rx_dma = rx_dma;
} else {
rx_dma->parent = NULL;
ret = CSI_ERROR;
}
} else {
if (i2s->rx_dma) {
/// 注销DMA传输完成回调函数
csi_dma_ch_detach_callback(i2s->rx_dma);
/// 释放DMA通道
csi_dma_ch_free(i2s->rx_dma);
i2s->rx_dma = NULL;
} else {
ret = CSI_ERROR;
}
}
return ret;
}
/**
\brief link DMA channel to i2s device
\param[in] i2s i2s handle to operate.
\param[in] rx_dma the DMA channel for receive, when it is NULL means to unused dma.
\return error code
*/
csi_error_t csi_i2s_tx_link_dma(csi_i2s_t *i2s, csi_dma_ch_t *tx_dma)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
if (tx_dma != NULL) {
tx_dma->parent = i2s;
/// 申请DMA通道
ret = csi_dma_ch_alloc(tx_dma, -1, -1);
if (ret == CSI_OK) {
/// 注册DMA传输完成回调函数
csi_dma_ch_attach_callback(tx_dma, wj_i2s_dma_event_cb, NULL);
i2s->tx_dma = tx_dma;
} else {
tx_dma->parent = NULL;
ret = CSI_ERROR;
}
} else {
if (i2s->tx_dma) {
/// 注销DMA传输完成回调函数
csi_dma_ch_detach_callback(i2s->tx_dma);
/// 释放DMA通道
csi_dma_ch_free(i2s->tx_dma);
i2s->tx_dma = NULL;
} else {
ret = CSI_ERROR;
}
}
return ret;
}
/**
\brief I2S rx cache config
\param[in] i2s i2s handle to operate.
\param[in] buffer i2s rx cache
\return none
*/
void csi_i2s_rx_set_buffer(csi_i2s_t *i2s, csi_ringbuf_t *buffer)
{
CSI_PARAM_CHK_NORETVAL(i2s);
CSI_PARAM_CHK_NORETVAL(buffer);
i2s->rx_buf = buffer;
}
/**
\brief I2S tx cache config
\param[in] i2s i2s handle to operate.
\param[in] buffer i2s tx cache
\return none
*/
void csi_i2s_tx_set_buffer(csi_i2s_t *i2s, csi_ringbuf_t *buffer)
{
CSI_PARAM_CHK_NORETVAL(i2s);
CSI_PARAM_CHK_NORETVAL(buffer);
i2s->tx_buf = buffer;
}
/**
\brief I2S rx set period.The value of period is to report a receive completion event
\ after each period value data is received.
\param[in] i2s i2s handle to operate.
\param[in] buffer i2s rx cache
\return none
*/
csi_error_t csi_i2s_rx_set_period(csi_i2s_t *i2s, uint32_t period)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
if (period == 0U) {
ret = CSI_ERROR;
} else {
if ((i2s->rx_buf->size % period) != 0U) {
ret = CSI_ERROR;
} else {
i2s->rx_period = period;
}
}
return ret;
}
/**
\brief I2S tx set period.The value of period is to report a receive completion event
\ after each period value data is send.
\param[in] i2s i2s handle to operate.
\param[in] buffer i2s tx cache
\return none
*/
csi_error_t csi_i2s_tx_set_period(csi_i2s_t *i2s, uint32_t period)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
if (period == 0U) {
ret = CSI_ERROR;
} else {
if ((i2s->tx_buf->size % period) != 0U) {
ret = CSI_ERROR;
} else {
i2s->tx_period = period;
}
}
return ret;
}
/**
\brief Get rx csi_ringbuf cache free space
\param[in] i2s i2s handle to operate.
\return buffer free space (bytes)
*/
uint32_t csi_i2s_rx_buffer_avail(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, 0U);
return csi_ringbuf_avail(i2s->rx_buf);
}
/**
\brief Reset the rx csi_ringbuf, discard all data in the cache
\param[in] i2s i2s handle to operate.
\return error code
*/
csi_error_t csi_i2s_rx_buffer_reset(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_ERROR;
csi_ringbuf_reset(i2s->rx_buf);
if (i2s->rx_buf->buffer != NULL) {
memset(i2s->rx_buf->buffer, 0, i2s->rx_buf->size);
}
if ((i2s->rx_buf->read == 0U) && (i2s->rx_buf->write == 0U)) {
ret = CSI_OK;
}
return ret;
}
/**
\brief Get tx csi_ringbuf cache free space
\param[in] i2s i2s handle to operate.
\return buffer free space (bytes)
*/
uint32_t csi_i2s_tx_buffer_avail(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, 0U);
return csi_ringbuf_avail(i2s->tx_buf);
}
/**
\brief Reset the tx csi_ringbuf, discard all data in the cache
\param[in] i2s i2s handle to operate.
\return error code
*/
csi_error_t csi_i2s_tx_buffer_reset(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_ERROR;
csi_ringbuf_reset(i2s->tx_buf);
if (i2s->tx_buf->buffer != NULL) {
memset(i2s->tx_buf->buffer, 0, i2s->tx_buf->size);
}
if ((i2s->tx_buf->read == 0U) && (i2s->tx_buf->write == 0U)) {
ret = CSI_OK;
}
return ret;
}
/**
\brief Receive an amount of data to cache in blocking mode.
\param[in] i2s operate handle.
\param[in] data save receive data.
\param[in] size receive data size.
\param[in] timeout is the number of queries, not time
\return The size of data receive successfully
*/
int32_t csi_i2s_receive(csi_i2s_t *i2s, void *data, uint32_t size)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
CSI_PARAM_CHK(data, CSI_ERROR);
uint8_t *read_data = (void *)data;
int32_t read_size = 0;
if (i2s->rx_dma == NULL) {
///< TODO:关闭i2s
///< TODO:设置i2s为接收模式
///< TODO:打开i2s
///< TODO:循环读数据寄存器接收数据直到接收到指定长度的数据,并把接收到的数据长度存放到read_size
} else {
while (1) {
read_size += (int32_t)csi_ringbuf_out(i2s->rx_buf, (void *)(read_data + (uint32_t)read_size), (size - (uint32_t)read_size));
if ((size - (uint32_t)read_size) <= 0U) {
break;
}
}
}
return read_size;
}
/**
\brief Write data to the cache.
\With asynchronous sending,
\the data is first written to the cache and then output through the I2S interface.
\This function does not block, and the return value is the number
\of data that was successfully written to the cache.
\param[in] i2s operate handle.
\param[in] data send data.
\param[in] size receive data size.
\return The data size that write to cache
*/
uint32_t csi_i2s_send_async(csi_i2s_t *i2s, const void *data, uint32_t size)
{
CSI_PARAM_CHK(i2s, 0U);
CSI_PARAM_CHK(data, 0U);
uint32_t write_len;
write_len = csi_ringbuf_in(i2s->tx_buf, data, size);
if ((uint8_t *)i2s->priv) { ///< if dma is stop, then start it
soc_dcache_clean_invalid_range((unsigned long)(i2s->tx_buf->buffer + i2s->tx_buf->read), i2s->tx_period);
///< TODO:调用csi_dma_ch_start开启DMA通道发送数据
i2s->priv = (void *)0U;
}
return write_len;
}
/**
\brief Transmits an amount of data to cache in blocking mode.
\param[in] i2s operate handle.
\param[in] data send data.
\param[in] size receive data size.
\param[in] timeout is the number of queries, not time
\return The num of data witch is send successful
*/
int32_t csi_i2s_send(csi_i2s_t *i2s, const void *data, uint32_t size)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
CSI_PARAM_CHK(data, CSI_ERROR);
int32_t write_size = 0;
uint8_t *send_data = (void *)data;
if (i2s->tx_dma == NULL) {
///< TODO:清除中断
///< TODO:关闭i2s
///< TODO:设置i2s为发送模式
///< TODO:打开i2s
///< TODO:写数据寄存器发送数据,并把发送数据的大小写到write_size
} else {
while (1) {
write_size += (int32_t)csi_ringbuf_in(i2s->tx_buf, (void *)(send_data + (uint32_t)write_size), (size - (uint32_t)write_size));
if ((uint8_t *)i2s->priv) { ///< if dma is stop, then start it
soc_dcache_clean_invalid_range((unsigned long)(i2s->tx_buf->buffer + i2s->tx_buf->read), i2s->tx_period);
///< TODO:调用csi_dma_ch_start开启DMA通道发送数据
i2s->priv = (void *)0U;
}
if ((size - (uint32_t)write_size) <= 0U) {
break;
}
}
while (!csi_ringbuf_is_empty(i2s->tx_buf));
}
return write_size;
}
/**
\brief Read data from the cache.
\Using asynchronous receive, i2s writes the received data to the cache.
\This function reads data from the cache, returns the number of successful reads,
\and returns 0 if there is no data in the cache.
\param[in] i2s operate handle.
\param[in] data the buf save receive data.
\param[in] size receive data size.
\return The size of data read successfully
*/
uint32_t csi_i2s_receive_async(csi_i2s_t *i2s, void *data, uint32_t size)
{
CSI_PARAM_CHK(i2s, 0U);
CSI_PARAM_CHK(data, 0U);
uint32_t read_len;
read_len = csi_ringbuf_out(i2s->rx_buf, (void *)data, size);
return read_len;
}
/**
\brief start i2s pause asynchronous send
\param[in] i2s operate handle.
\return error code
*/
csi_error_t csi_i2s_send_pause(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
///< TODO:关闭DMA通道
i2s->state.writeable = 0U;
return ret;
}
/**
\brief start i2s resume asynchronous send
\param[in] i2s operate handle.
\return error code
*/
csi_error_t csi_i2s_send_resume(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
soc_dcache_clean_invalid_range((unsigned long)(i2s->tx_buf->buffer + i2s->tx_buf->read), i2s->tx_period);
///< TODO:调用csi_dma_ch_start开启DMA通道发送数据
i2s->state.writeable = 1U;
return ret;
}
/**
\brief start i2s asynchronous send
\param[in] i2s operate handle.
\return error code
*/
csi_error_t csi_i2s_send_start(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
csi_dma_ch_config_t config;
memset(&config, 0, sizeof(csi_dma_ch_config_t));
csi_dma_ch_t *dma_ch = (csi_dma_ch_t *)i2s->tx_dma;
///< TODO:清除i2s中断
///< TODO:关闭i2s
///< TODO:设置i2s为发送模式
///< TODO:使能i2s发送DMA
///< TODO:设置DMA的触发级别为64
config.src_inc = DMA_ADDR_INC;
config.dst_inc = DMA_ADDR_CONSTANT;
config.src_tw = DMA_DATA_WIDTH_32_BITS;
config.dst_tw = DMA_DATA_WIDTH_32_BITS;
config.trans_dir = DMA_MEM2PERH;
config.half_int_en = 0U;
config.handshake = i2s_tx_hs_num[i2s->dev.idx];
///< TODO:使能打开i2s
memset(i2s->tx_buf->buffer, 0, i2s->tx_buf->size);
///< 配置DMA通道
ret = csi_dma_ch_config(dma_ch, &config);
soc_dcache_clean_invalid_range((unsigned long)(i2s->tx_buf->buffer + i2s->tx_buf->read), i2s->tx_period);
///< TODO:调用csi_dma_ch_start开启DMA通道,开始传输数据
///< TODO:清FIFO
i2s->state.writeable = 1U;
return ret;
}
/**
\brief start i2s asynchronous receive
\param[in] i2s operate handle.
\return error code
*/
csi_error_t csi_i2s_receive_start(csi_i2s_t *i2s)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
csi_dma_ch_config_t config;
memset(&config, 0, sizeof(csi_dma_ch_config_t));
csi_dma_ch_t *dma_ch = (csi_dma_ch_t *)i2s->rx_dma;
///< TODO:关闭i2s
///< TODO:设置i2s为接收模式
///< TODO:使能i2s接收DMA
///< TODO:设置DMA的触发级别
config.src_inc = DMA_ADDR_CONSTANT;
config.dst_inc = DMA_ADDR_INC;
config.src_tw = DMA_DATA_WIDTH_32_BITS;
config.dst_tw = DMA_DATA_WIDTH_32_BITS;
config.trans_dir = DMA_PERH2MEM;
config.half_int_en = 0U;
config.handshake = i2s_rx_hs_num[i2s->dev.idx];
///< TODO:使能打开i2s
memset(i2s->rx_buf->buffer, 0, i2s->rx_buf->size);
///< 配置DMA通道
ret = csi_dma_ch_config(dma_ch, &config);
///< TODO:获取接收FIFO中有多少数据
///< TODO:根据接收FIFO中的数据量读数据寄存器,清空接收FIFO
soc_dcache_clean_invalid_range((unsigned long)(i2s->rx_buf->buffer + i2s->rx_buf->write), i2s->rx_period);
///< TODO:调用csi_dma_ch_start开启DMA通道,开始传输数据
i2s->state.readable = 1U;
return ret;
}
/**
\brief stop i2s asynchronous send
\param[in] i2s operate handle.
*/
void csi_i2s_send_stop(csi_i2s_t *i2s)
{
CSI_PARAM_CHK_NORETVAL(i2s);
///< 关闭DMA通道
csi_dma_ch_stop(i2s->tx_dma);
csi_ringbuf_reset(i2s->tx_buf);
memset(i2s->tx_buf->buffer, 0, i2s->tx_buf->size);
///< TODO:等待发送FIFO为空
///< TODO:清FIFO
i2s->state.writeable = 0U;
}
/**
\brief stop i2s asynchronous receive
\param[in] i2s operate handle.
\return error code
*/
void csi_i2s_receive_stop(csi_i2s_t *i2s)
{
CSI_PARAM_CHK_NORETVAL(i2s);
///< 关闭DMA通道
csi_dma_ch_stop(i2s->rx_dma);
csi_ringbuf_reset(i2s->rx_buf);
memset(i2s->rx_buf->buffer, 0, i2s->rx_buf->size);
///< TODO :清FIFO
i2s->state.readable = 0U;
}
/**
\brief attach the callback handler to i2s
\param[in] i2s operate handle.
\param[in] cb callback function
\param[in] arg user can define it by himself as callback's param
\return error code
*/
csi_error_t csi_i2s_attach_callback(csi_i2s_t *i2s, void *callback, void *arg)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
csi_error_t ret = CSI_OK;
i2s->callback = callback;
i2s->arg = arg;
return ret;
}
/**
\brief detach the callback handler
\param[in] i2s operate handle.
\return none
*/
void csi_i2s_detach_callback(csi_i2s_t *i2s)
{
CSI_PARAM_CHK_NORETVAL(i2s);
i2s->callback = NULL;
i2s->arg = NULL;
}
/**
\brief Get i2s status.
\param[in] i2s i2s handle to operate.
\param[out] state i2s state.
\return i2s error code
*/
csi_error_t csi_i2s_get_state(csi_i2s_t *i2s, csi_state_t *state)
{
CSI_PARAM_CHK(i2s, CSI_ERROR);
CSI_PARAM_CHK(state, CSI_ERROR);
*state = i2s->state;
return CSI_OK;
}
#ifdef CONFIG_PM
csi_error_t csi_i2s_enable_pm(csi_i2s_t *i2s)
{
return CSI_UNSUPPORTED;
}
void csi_i2s_disable_pm(csi_i2s_t *i2s)
{
}
#endif
|