1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-4-30     misonyo     the first version.
9  */
10 
11 #ifndef __DRV_SOUND_H_
12 #define __DRV_SOUND_H_
13 
14 #include <rtthread.h>
15 #include <rtdevice.h>
16 #include <board.h>
17 #include <rtdef.h>
18 
19 #include <rthw.h>
20 #include "fsl_sai.h"
21 #include "fsl_dmamux.h"
22 #include "fsl_sai_edma.h"
23 
24 #define AUD_DMA_FIFO_SIZE (2048)
25 #define CODEC_I2C_NAME ("i2c1")
26 /* Select Audio/Video PLL (786.48 MHz) as sai1 clock source */
27 #define DEMO_SAI1_CLOCK_SOURCE_SELECT (2U)
28 /* Clock pre divider for sai1 clock source */
29 #define DEMO_SAI1_CLOCK_SOURCE_PRE_DIVIDER (0U)
30 /* Clock divider for sai1 clock source */
31 #define DEMO_SAI1_CLOCK_SOURCE_DIVIDER (63U)
32 /* Get frequency of sai1 clock */
33 #define AUD_BLOCK_CNT   2
34 #define AUD_BLOCK_SIZE  1024
35 #define AUD_FIFO_SIZE   (AUD_BLOCK_SIZE * AUD_BLOCK_CNT)
36 #define DEMO_SAI_CLK_FREQ                                                        \
37     (CLOCK_GetFreq(kCLOCK_AudioPllClk) / (DEMO_SAI1_CLOCK_SOURCE_DIVIDER + 1U) / \
38      (DEMO_SAI1_CLOCK_SOURCE_PRE_DIVIDER + 1U))
39 
40 
41 #define AUD_DMA_FIFO_SIZE (2048)
42 struct saidma_tx_config
43 {
44     edma_handle_t edma;
45     rt_uint8_t channel;
46     dma_request_source_t request;
47     sai_edma_handle_t txHandle;
48 };
49 struct saidma_rx_config
50 {
51     edma_handle_t edma;
52     rt_uint8_t channel;
53     dma_request_source_t request;
54     sai_edma_handle_t rxHandle;
55 };
56 
57 struct drv_sai {
58     I2S_Type *base;
59     IRQn_Type irqn;
60     struct saidma_tx_config *dma_tx;
61     struct saidma_rx_config *dma_rx;
62     rt_uint8_t dma_flag;
63     int txBlockIndex;
64     int rxBlockIndex;
65 };
66 
67 void sai_init(void);
68 int rt_hw_sound_init(void);
69 
70 #endif
71