• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #pragma once
16 #include <common/bk_include.h>
17 #include <driver/i2s_types.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /* @brief Overview about this API header
24  *
25  */
26 
27 /**
28  * @brief I2S API
29  * @defgroup bk_api_i2s I2S group
30  * @{
31  */
32 
33 
34 /**
35  * @brief     Init i2s module driver
36  *
37  * This API init i2s driver :
38  *  - Power on clock
39  *  - config apll clock
40  *  - enable interrupts
41  *
42  * @return
43  *    - BK_OK: succeed
44  *    - others: other errors.
45  */
46 bk_err_t bk_i2s_driver_init(void);
47 
48 /**
49  * @brief     Deinit i2s module driver
50  *
51  * This API deinit i2s driver :
52  *  - Power down clock
53  *  - reconfig apll clock to defailt value
54  *  - disable interrupts
55  *
56  * @return
57  *    - BK_OK: succeed
58  *    - others: other errors.
59  */
60 bk_err_t bk_i2s_driver_deinit(void);
61 
62 /**
63  * @brief     Init i2s module
64  *
65  * This API init the i2s module:
66  *  - Init gpios i2s used
67  *  - Configure the i2s parameters
68  *
69  * Usage example:
70  *
71  *     //init i2s driver
72  *     bk_i2s_driver_init();
73  *
74  *     //init i2s configure
75  *     i2s_config_t i2s_config;
76  *     i2s_config.i2s_en = I2S_DISABLE;
77  *     i2s_config.role = I2S_ROLE_MASTER;
78  *     i2s_config.work_mode = I2S_WORK_MODE_I2S;
79  *     i2s_config.lrck_invert = I2S_LRCK_INVERT_DISABLE;
80  *     i2s_config.sck_invert = I2S_SCK_INVERT_DISABLE;
81  *     i2s_config.lsb_first_en = I2S_LSB_FIRST_DISABLE;
82  *     i2s_config.sync_length = 0;
83  *     i2s_config.data_length = 15;
84  *     i2s_config.pcm_dlength = 0;
85  *     i2s_config.sample_ratio = 0;
86  *     i2s_config.sck_ratio = 0;
87  *     i2s_config.parallel_en = I2S_PARALLEL_DISABLE;
88  *     i2s_config.store_mode = I2S_LRCOM_STORE_16R16L;
89  *     i2s_config.sck_ratio_h4b = 0;
90  *     i2s_config.sample_ratio_h2b = 0;
91  *     i2s_config.txint_level = I2S_TXINT_LEVEL_1;
92  *     i2s_config.rxint_level = I2S_RXINT_LEVEL_1;
93  *     bk_i2s_init(I2S_GPIO_GROUP_0, &i2s_config);
94  *     CLI_LOGI("init i2s driver and config successful \r\n");
95  *
96  * @param id gpio config "GPIO6, GPIO7, GPIO8, GPIO9"/"GPIO40, GPIO41, GPIO42, GPIO43"
97  * @param config i2s parameters include work role, work mode and so on
98  *
99  * @return
100  *    - BK_OK: succeed
101  *    - BK_ERR_I2S_PARAM: config is NULL
102  *    - BK_ERR_I2S_NOT_INIT: i2s driver is not init
103  *    - others: other errors.
104  */
105 bk_err_t bk_i2s_init(i2s_gpio_group_id_t id, const i2s_config_t *config);
106 
107 /**
108  * @brief     Deinit i2s module
109  *
110  * This API deinit the i2s module:
111  *   - reconfig i2s parameters to default value
112  *
113  * @return
114  *    - BK_OK: succeed
115  *    - others: other errors.
116  */
117 bk_err_t bk_i2s_deinit(void);
118 
119 /**
120  * @brief     Get the i2s Rx fifo status
121  *
122  * This API get the i2s Rx fifo status that check whether the fifo data can be read
123  *
124  * @param read_flag save Rx fifo status
125  *
126  * @return
127  *    - BK_OK: succeed
128  *    - BK_ERR_I2S_NOT_INIT: i2s driver is not init
129  *    - others: other errors.
130  */
131 bk_err_t bk_i2s_get_read_ready(uint32_t *read_flag);
132 
133 /**
134  * @brief     Get the i2s Tx fifo status
135  *
136  * This API get the i2s Tx fifo status that check whether the fifo data can be write
137  *
138  * @param read_flag save Tx fifo status
139  *
140  * @return
141  *    - BK_OK: succeed
142  *    - BK_ERR_I2S_NOT_INIT: i2s driver is not init
143  *    - others: other errors.
144  */
145 bk_err_t bk_i2s_get_write_ready(uint32_t *write_flag);
146 
147 /**
148  * @brief     Enable i2s
149  *
150  * This API enable i2s
151  *
152  * @param en_value disable/enable i2s
153  *
154  * @return
155  *    - BK_OK: succeed
156  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
157  *    - others: other errors.
158  */
159 bk_err_t bk_i2s_enable(i2s_en_t en_value);
160 
161 /**
162  * @brief     Config i2s interrupt
163  *
164  * This API config i2s interrupt
165  *
166  * @param int_id interrupt id
167  * @param value 0:disable 1:enable
168  *
169  * @return
170  *    - BK_OK: succeed
171  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
172  *    - others: other errors.
173  */
174 bk_err_t bk_i2s_int_enable(i2s_isr_id_t int_id, uint32_t value);
175 
176 /**
177  * @brief     Config i2s work role
178  *
179  * This API config i2s work role
180  *
181  * @param role work role slave/master
182  *
183  * @return
184  *    - BK_OK: succeed
185  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
186  *    - others: other errors.
187  */
188 bk_err_t bk_i2s_set_role(i2s_role_t role);
189 
190 /**
191  * @brief     Config i2s work mode
192  *
193  * This API config i2s work mode
194  *
195  * @param work_mode i2s work mode
196  *
197  * @return
198  *    - BK_OK: succeed
199  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
200  *    - others: other errors.
201  */
202 bk_err_t bk_i2s_set_work_mode(i2s_work_mode_t work_mode);
203 
204 /**
205  * @brief     Config i2s lrck invert
206  *
207  * This API config i2s lrck invert
208  *
209  * @param lrckrp i2s lrck invert enable/disable
210  *
211  * @return
212  *    - BK_OK: succeed
213  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
214  *    - others: other errors.
215  */
216 bk_err_t bk_i2s_set_lrck_invert(i2s_lrck_invert_en_t lrckrp);
217 
218 /**
219  * @brief     Config i2s sck invert
220  *
221  * This API config i2s sck invert
222  *
223  * @param sck_invert i2s sck invert enable/disable
224  *
225  * @return
226  *    - BK_OK: succeed
227  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
228  *    - others: other errors.
229  */
230 bk_err_t bk_i2s_set_sck_invert(i2s_sck_invert_en_t sck_invert);
231 
232 /**
233  * @brief     Config i2s lsb first
234  *
235  * This API config i2s lsb first
236  *
237  * @param lsb_first i2s lsb first enable/disable
238  *
239  * @return
240  *    - BK_OK: succeed
241  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
242  *    - others: other errors.
243  */
244 bk_err_t bk_i2s_set_lsb_first(i2s_lsb_first_en_t lsb_first);
245 
246 /**
247  * @brief     Config i2s sync length
248  *
249  * This API config i2s sync length
250  *
251  * @param sync_len i2s sync length
252  *
253  * @return
254  *    - BK_OK: succeed
255  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
256  *    - others: other errors.
257  */
258 bk_err_t bk_i2s_set_sync_len(uint32_t sync_len);
259 
260 /**
261  * @brief     Config i2s data length
262  *
263  * This API config i2s data length
264  *
265  * @param data_len i2s data length
266  *
267  * @return
268  *    - BK_OK: succeed
269  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
270  *    - others: other errors.
271  */
272 bk_err_t bk_i2s_set_data_len(uint32_t data_len);
273 
274 /**
275  * @brief     Config i2s pcm D length of 2B+D
276  *
277  * This API config i2s pcm D length of 2B+D
278  *
279  * @param pcm_dlen pcm D length
280  *
281  * @return
282  *    - BK_OK: succeed
283  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
284  *    - others: other errors.
285  */
286 bk_err_t bk_i2s_set_pcm_dlen(uint32_t pcm_dlen);
287 
288 /**
289  * @brief     Config i2s store mode
290  *
291  * This API config i2s store mode
292  *
293  * @param store_mode lrcom store mode LRLR/16R16L
294  *
295  * @attention 1. LRLR mode: left and right channel data are written alternately in chronological order(L->R->L->R->......)
296  * @attention 2. 16R16L mode: left and right channel data are written to FIFO in 32 bits, in which the lower 16 bits
297                  correspond to the left channel and the higher 16 bits correspond to the right channel({R,L}->{R,L}->......)
298  *
299  * @return
300  *    - BK_OK: succeed
301  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
302  *    - others: other errors.
303  */
304 bk_err_t bk_i2s_set_store_mode(i2s_lrcom_store_mode_t store_mode);
305 
306 /**
307  * @brief     clear i2s rx fifo
308  *
309  * This API clear i2s rx fifo
310  *
311  * @return
312  *    - BK_OK: succeed
313  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
314  *    - others: other errors.
315  */
316 bk_err_t bk_i2s_clear_rxfifo(void);
317 
318 /**
319  * @brief     clear i2s tx fifo
320  *
321  * This API clear i2s tx fifo
322  *
323  * @return
324  *    - BK_OK: succeed
325  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
326  *    - others: other errors.
327  */
328 bk_err_t bk_i2s_clear_txfifo(void);
329 
330 /**
331  * @brief     config i2s tx interrupt level
332  *
333  * This API config i2s tx interrupt level
334  *
335  * @param txint_level tx interrupt trigger level
336  *
337  * @return
338  *    - BK_OK: succeed
339  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
340  *    - others: other errors.
341  */
342 bk_err_t bk_i2s_set_txint_level(i2s_txint_level_t txint_level);
343 
344 /**
345  * @brief     config i2s rx interrupt level
346  *
347  * This API config i2s rx interrupt level
348  *
349  * @param rxint_level rx interrupt trigger level
350  *
351  * @return
352  *    - BK_OK: succeed
353  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
354  *    - others: other errors.
355  */
356 bk_err_t bk_i2s_set_rxint_level(i2s_rxint_level_t rxint_level);
357 
358 /**
359  * @brief     write data to i2s tx fifo
360  *
361  * This API write data to i2s tx fifo
362  *
363  * Usage example:
364  *
365  *     //init i2s driver
366  *     bk_i2s_driver_init();
367  *
368  *     //init i2s configure
369  *     i2s_config.i2s_en = I2S_DISABLE;
370  *     i2s_config.role = I2S_ROLE_MASTER;
371  *     i2s_config.work_mode = I2S_WORK_MODE_I2S;
372  *     i2s_config.lrck_invert = I2S_LRCK_INVERT_DISABLE;
373  *     i2s_config.sck_invert = I2S_SCK_INVERT_DISABLE;
374  *     i2s_config.lsb_first_en = I2S_LSB_FIRST_DISABLE;
375  *     i2s_config.sync_length = 0;
376  *     i2s_config.data_length = 15;
377  *     i2s_config.pcm_dlength = 0;
378  *     i2s_config.sample_ratio = 0;
379  *     i2s_config.sck_ratio = 0;
380  *     i2s_config.parallel_en = I2S_PARALLEL_DISABLE;
381  *     i2s_config.store_mode = I2S_LRCOM_STORE_16R16L;
382  *     i2s_config.sck_ratio_h4b = 0;
383  *     i2s_config.sample_ratio_h2b = 0;
384  *     i2s_config.txint_level = I2S_TXINT_LEVEL_1;
385  *     i2s_config.rxint_level = I2S_RXINT_LEVEL_1;
386  *     bk_i2s_init(I2S_GPIO_GROUP_0, &i2s_config);
387  *     CLI_LOGI("init i2s driver and config successful \r\n");
388  *
389  *     //set sample and bitclk ratio
390  *     rate.datawidth = i2s_rate_table[i].datawidth;
391  *     rate.samp_rate = i2s_rate_table[i].samp_rate;
392  *     bk_i2s_set_ratio(&rate);
393  *
394  *     //enable i2s
395  *     bk_i2s_enable(I2S_ENABLE);
396  *     CLI_LOGI("enable i2s successful \r\n");
397  *
398  *     bk_i2s_get_write_ready(&write_flag);
399  *     if (write_flag)
400  *         bk_i2s_write_data(1, &data_buf_rl, 1);
401  *
402  * Usage example:
403  *
404  *     //init i2s driver
405  *     bk_i2s_driver_init();
406  *
407  *     //init i2s configure
408  *     i2s_config_t i2s_config;
409  *     i2s_config.i2s_en = I2S_DISABLE;
410  *     i2s_config.role = I2S_ROLE_MASTER;
411  *     i2s_config.work_mode = I2S_WORK_MODE_I2S;
412  *     i2s_config.lrck_invert = I2S_LRCK_INVERT_DISABLE;
413  *     i2s_config.sck_invert = I2S_SCK_INVERT_DISABLE;
414  *     i2s_config.lsb_first_en = I2S_LSB_FIRST_DISABLE;
415  *     i2s_config.sync_length = 0;
416  *     i2s_config.data_length = 15;
417  *     i2s_config.pcm_dlength = 0;
418  *     i2s_config.sample_ratio = 0;
419  *     i2s_config.sck_ratio = 0;
420  *     i2s_config.parallel_en = I2S_PARALLEL_DISABLE;
421  *     i2s_config.store_mode = I2S_LRCOM_STORE_16R16L;
422  *     i2s_config.sck_ratio_h4b = 0;
423  *     i2s_config.sample_ratio_h2b = 0;
424  *     i2s_config.txint_level = I2S_TXINT_LEVEL_1;
425  *     i2s_config.rxint_level = I2S_RXINT_LEVEL_1;
426  *     bk_i2s_init(I2S_GPIO_GROUP_0, &i2s_config);
427  *     CLI_LOGI("init i2s driver and config successful \r\n");
428  *
429  *     //set sample and bitclk ratio
430  *     rate.datawidth = i2s_rate_table[i].datawidth;
431  *     rate.samp_rate = i2s_rate_table[i].samp_rate;
432  *     bk_i2s_set_ratio(&rate);
433  *
434  *     //enable i2s
435  *     bk_i2s_enable(I2S_ENABLE);
436  *     CLI_LOGI("enable i2s successful \r\n");
437  *
438  *     //get write ready status and write data
439  *     bk_i2s_get_write_ready(&write_flag);
440  *     if (write_flag)
441  *         bk_i2s_write_data(1, &data_buf_rl, 1);
442  *
443  * @param channel_id i2s channel 1/2/3/4
444  * @param data_buf data to write
445  * @param data_len data length to write
446  *
447  * @return
448  *    - BK_OK: succeed
449  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
450  *    - others: other errors.
451  */
452 bk_err_t bk_i2s_write_data(uint32_t channel_id, uint32_t *data_buf, uint32_t data_len);
453 
454 /**
455  * @brief     read data from i2s rx fifo
456  *
457  * This API read data from i2s rx fifo
458  *
459  * Usage example:
460  *
461  *     //init i2s driver
462  *     bk_i2s_driver_init();
463  *
464  *     //init i2s configure
465  *     i2s_config_t i2s_config;
466  *     i2s_config.i2s_en = I2S_DISABLE;
467  *     i2s_config.role = I2S_ROLE_MASTER;
468  *     i2s_config.work_mode = I2S_WORK_MODE_I2S;
469  *     i2s_config.lrck_invert = I2S_LRCK_INVERT_DISABLE;
470  *     i2s_config.sck_invert = I2S_SCK_INVERT_DISABLE;
471  *     i2s_config.lsb_first_en = I2S_LSB_FIRST_DISABLE;
472  *     i2s_config.sync_length = 0;
473  *     i2s_config.data_length = 15;
474  *     i2s_config.pcm_dlength = 0;
475  *     i2s_config.sample_ratio = 0;
476  *     i2s_config.sck_ratio = 0;
477  *     i2s_config.parallel_en = I2S_PARALLEL_DISABLE;
478  *     i2s_config.store_mode = I2S_LRCOM_STORE_16R16L;
479  *     i2s_config.sck_ratio_h4b = 0;
480  *     i2s_config.sample_ratio_h2b = 0;
481  *     i2s_config.txint_level = I2S_TXINT_LEVEL_1;
482  *     i2s_config.rxint_level = I2S_RXINT_LEVEL_1;
483  *     bk_i2s_init(I2S_GPIO_GROUP_0, &i2s_config);
484  *     CLI_LOGI("init i2s driver and config successful \r\n");
485  *
486  *     //set sample and bitclk ratio
487  *     rate.datawidth = i2s_rate_table[i].datawidth;
488  *     rate.samp_rate = i2s_rate_table[i].samp_rate;
489  *     bk_i2s_set_ratio(&rate);
490  *
491  *     //enable i2s
492  *     bk_i2s_enable(I2S_ENABLE);
493  *     CLI_LOGI("enable i2s successful \r\n");
494  *
495  *     //get read ready status and read data
496  *     bk_i2s_get_read_ready(&read_flag);
497  *     if (read_flag)
498  *         bk_i2s_read_data(&data_buf, 1);
499  *
500  * @param data_buf save data read from rx fifo
501  * @param data_len: data length to read
502  *
503  * @return
504  *    - BK_OK: succeed
505  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
506  *    - others: other errors.
507  */
508 bk_err_t bk_i2s_read_data(uint32_t *data_buf, uint32_t data_len);
509 
510 /**
511  * @brief     get i2s data address
512  *
513  * This API get i2s data address
514  *
515  * @param i2s_data_addr save i2s data address
516  *
517  * @return
518  *    - BK_OK: succeed
519  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
520  *    - others: other errors.
521  */
522 bk_err_t bk_i2s_get_data_addr(uint32_t *i2s_data_addr);
523 
524 /**
525  * @brief     config i2s sample rate
526  *
527  * This API config i2s sample rate
528  *
529  * @param rate sample rate and data width
530  *
531  * @return
532  *    - BK_OK: succeed
533  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
534  *    - others: other errors.
535  */
536 bk_err_t bk_i2s_set_ratio(i2s_rate_t *rate);
537 
538 /**
539  * @brief     register i2s isr
540  *
541  * This API config register i2s isr
542  *
543  * Usage example:
544  *
545  *     void cli_i2s_master_txudf_isr(void *param)
546  *     {
547  *         CLI_LOGI("enter cli_i2s_txudf_isr \r\n");
548  *     }
549  *
550  *     //register isr
551  *     bk_i2s_register_i2s_isr(I2S_ISR_CHL1_TXUDF, cli_i2s_master_txudf_isr, NULL);
552  *     CLI_LOGI("register i2s isr successful \r\n");
553  *
554  *     //enable i2s rxovf interrupt
555  *     bk_i2s_int_enable(I2S_ISR_CHL1_TXUDF, I2S_INT_ENABLE);
556  *     CLI_LOGI("enable i2s interrupt successful \r\n");
557  *
558  * @param isr_id i2s isr id
559  * @param isr isr function
560  * @param param isr parameters
561  *
562  * @return
563  *    - BK_OK: succeed
564  *    - BK_ERR_AUD_NOT_INIT: i2s driver is not init
565  *    - BK_ERR_I2S_ISR_ID: i2s isr id is invalid
566  *    - others: other errors.
567  */
568 bk_err_t bk_i2s_register_i2s_isr(i2s_isr_id_t isr_id, i2s_isr_t isr, void *param);
569 
570 
571 /**
572  * @}
573  */
574 
575 #ifdef __cplusplus
576 }
577 #endif
578