1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "mxc_device.h"
25 #include "mxc_assert.h"
26 #include "mxc_lock.h"
27 #include "mxc_sys.h"
28 #include "mxc_delay.h"
29 #include "dma.h"
30 #include "i2s.h"
31 #include "i2s_reva.h"
32 #include "i2s_regs.h"
33 
MXC_I2S_Init(mxc_i2s_req_t * req)34 int MXC_I2S_Init(mxc_i2s_req_t *req)
35 {
36     MXC_I2S_Shutdown();
37 
38     MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
39     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2S);
40     MXC_GPIO_Config(&gpio_cfg_i2s0);
41 
42     return MXC_I2S_RevA_Init(req);
43 }
44 
MXC_I2S_Shutdown(void)45 int MXC_I2S_Shutdown(void)
46 {
47     MXC_I2S_RevA_Shutdown();
48 
49     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2S);
50     MXC_SYS_Reset_Periph(MXC_SYS_RESET1_I2S);
51 
52     return E_NO_ERROR;
53 }
54 
MXC_I2S_ConfigData(mxc_i2s_req_t * req)55 int MXC_I2S_ConfigData(mxc_i2s_req_t *req)
56 {
57     return MXC_I2S_RevA_ConfigData(req);
58 }
59 
MXC_I2S_TXEnable(void)60 void MXC_I2S_TXEnable(void)
61 {
62     MXC_I2S_RevA_TXEnable();
63 }
64 
MXC_I2S_TXDisable(void)65 void MXC_I2S_TXDisable(void)
66 {
67     MXC_I2S_RevA_TXDisable();
68 }
69 
MXC_I2S_RXEnable(void)70 void MXC_I2S_RXEnable(void)
71 {
72     MXC_I2S_RevA_RXEnable();
73 }
74 
MXC_I2S_RXDisable(void)75 void MXC_I2S_RXDisable(void)
76 {
77     MXC_I2S_RevA_RXDisable();
78 }
79 
MXC_I2S_SetRXThreshold(uint8_t threshold)80 int MXC_I2S_SetRXThreshold(uint8_t threshold)
81 {
82     return MXC_I2S_RevA_SetRXThreshold(threshold);
83 }
84 
MXC_I2S_SetFrequency(mxc_i2s_ch_mode_t mode,uint16_t clkdiv)85 int MXC_I2S_SetFrequency(mxc_i2s_ch_mode_t mode, uint16_t clkdiv)
86 {
87     return MXC_I2S_RevA_SetFrequency(mode, clkdiv);
88 }
89 
MXC_I2S_SetSampleRate(uint32_t smpl_rate,mxc_i2s_wsize_t smpl_sz)90 int MXC_I2S_SetSampleRate(uint32_t smpl_rate, mxc_i2s_wsize_t smpl_sz)
91 {
92     return MXC_I2S_RevA_SetSampleRate((mxc_i2s_reva_regs_t *)MXC_I2S, smpl_rate, smpl_sz,
93                                       ERFO_FREQ);
94 }
95 
MXC_I2S_GetSampleRate(void)96 int MXC_I2S_GetSampleRate(void)
97 {
98     return MXC_I2S_RevA_GetSampleRate((mxc_i2s_reva_regs_t *)MXC_I2S, ERFO_FREQ);
99 }
100 
MXC_I2S_CalculateClockDiv(uint32_t smpl_rate,mxc_i2s_wsize_t smpl_sz)101 int MXC_I2S_CalculateClockDiv(uint32_t smpl_rate, mxc_i2s_wsize_t smpl_sz)
102 {
103     return MXC_I2S_RevA_CalculateClockDiv((mxc_i2s_reva_regs_t *)MXC_I2S, smpl_rate, smpl_sz,
104                                           ERFO_FREQ);
105 }
106 
MXC_I2S_Flush(void)107 void MXC_I2S_Flush(void)
108 {
109     MXC_I2S_RevA_Flush();
110 }
111 
MXC_I2S_FillTXFIFO(void * txData,mxc_i2s_wsize_t wordSize,int len,int smpl_cnt)112 int MXC_I2S_FillTXFIFO(void *txData, mxc_i2s_wsize_t wordSize, int len, int smpl_cnt)
113 {
114     return MXC_I2S_RevA_FillTXFIFO((mxc_i2s_reva_regs_t *)MXC_I2S, txData, wordSize, len, smpl_cnt);
115 }
116 
MXC_I2S_ReadRXFIFO(void * rxData,mxc_i2s_wsize_t wordSize,int len,int smpl_cnt)117 int MXC_I2S_ReadRXFIFO(void *rxData, mxc_i2s_wsize_t wordSize, int len, int smpl_cnt)
118 {
119     return MXC_I2S_RevA_ReadRXFIFO((mxc_i2s_reva_regs_t *)MXC_I2S, rxData, wordSize, len, smpl_cnt);
120 }
121 
MXC_I2S_EnableInt(uint32_t flags)122 void MXC_I2S_EnableInt(uint32_t flags)
123 {
124     MXC_I2S_RevA_EnableInt(flags);
125 }
126 
MXC_I2S_DisableInt(uint32_t flags)127 void MXC_I2S_DisableInt(uint32_t flags)
128 {
129     MXC_I2S_RevA_DisableInt(flags);
130 }
131 
MXC_I2S_GetFlags(void)132 int MXC_I2S_GetFlags(void)
133 {
134     return MXC_I2S_RevA_GetFlags();
135 }
136 
MXC_I2S_ClearFlags(uint32_t flags)137 void MXC_I2S_ClearFlags(uint32_t flags)
138 {
139     MXC_I2S_RevA_ClearFlags(flags);
140 }
141 
MXC_I2S_Transaction(mxc_i2s_req_t * i2s_req)142 int MXC_I2S_Transaction(mxc_i2s_req_t *i2s_req)
143 {
144     return MXC_I2S_RevA_Transaction((mxc_i2s_reva_regs_t *)MXC_I2S, i2s_req);
145 }
146 
MXC_I2S_TransactionAsync(mxc_i2s_req_t * i2s_req)147 int MXC_I2S_TransactionAsync(mxc_i2s_req_t *i2s_req)
148 {
149     return MXC_I2S_RevA_TransactionAsync((mxc_i2s_reva_regs_t *)MXC_I2S, i2s_req);
150 }
151 
MXC_I2S_TXDMAConfig(void * src_addr,int len)152 int MXC_I2S_TXDMAConfig(void *src_addr, int len)
153 {
154     return MXC_I2S_RevA_TXDMAConfig(src_addr, len);
155 }
156 
MXC_I2S_RXDMAConfig(void * dest_addr,int len)157 int MXC_I2S_RXDMAConfig(void *dest_addr, int len)
158 {
159     return MXC_I2S_RevA_RXDMAConfig(dest_addr, len);
160 }
161 
MXC_I2S_Handler(void)162 void MXC_I2S_Handler(void)
163 {
164     MXC_I2S_RevA_Handler((mxc_i2s_reva_regs_t *)MXC_I2S);
165 }
166 
MXC_I2S_RegisterDMACallback(void (* callback)(int,int))167 void MXC_I2S_RegisterDMACallback(void (*callback)(int, int))
168 {
169     MXC_I2S_RegisterDMACallback(callback);
170 }
171 
MXC_I2S_RegisterAsyncCallback(void (* callback)(int))172 void MXC_I2S_RegisterAsyncCallback(void (*callback)(int))
173 {
174     MXC_I2S_RevA_RegisterAsyncCallback(callback);
175 }
176