1 /**
2  * @file    i2s.h
3  * @brief   I2S (Inter-Integrated Sound) driver function prototypes and data types.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_I2S_H_
27 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_I2S_H_
28 
29 /* **** Includes **** */
30 #include "mxc_sys.h"
31 #include "dma.h"
32 #include "spimss_regs.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @defgroup i2s Inter-Integrated Sound (I2S)
40  * @ingroup periphlibs
41  * @{
42  */
43 
44 /* **** Definitions **** */
45 
46 typedef enum {
47     I2S_MAP_A = 0,
48     I2S_MAP_B = 1,
49 } mxc_i2s_sys_map_t;
50 
51 typedef enum {
52     LEFT_JUSTIFIED = 0,
53     RIGHT_JUSTIFIED = 1,
54 } mxc_i2s_justify_t;
55 
56 typedef enum {
57     STEREO_MODE = 0,
58     MONO_MODE = 1,
59 } mxc_i2s_audio_mode_t;
60 
61 /** @brief I2S audio directions */
62 typedef enum {
63     AUDIO_OUT = 1,
64     AUDIO_IN = 2,
65 } mxc_i2s_direction_t;
66 
67 /** @brief I2S Configuration Struct */
68 typedef struct {
69     mxc_i2s_sys_map_t map;
70     mxc_i2s_justify_t justify;
71     mxc_i2s_audio_mode_t audio_mode;
72     mxc_i2s_direction_t audio_direction;
73     uint16_t sample_rate;
74     unsigned int start_immediately;
75     unsigned int dma_reload_en;
76     void *src_addr;
77     void *dst_addr;
78     uint32_t length;
79 } mxc_i2s_config_t;
80 
81 /* **** Function Prototypes **** */
82 
83 /**
84  * @brief   Initialize I2S resources
85  *
86  * @param   config        see \ref mxc_i2s_config_t I2S Config Struct
87  * @param   dma_ctz_cb    Function pointer to Count-to-Zero callback function.
88  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
89  */
90 int MXC_I2S_Init(const mxc_i2s_config_t *config, void (*dma_ctz_cb)(int, int));
91 
92 /**
93  * @brief      Release I2S
94  * @details    De-configures the I2S protocol and stops DMA request
95  * @return   \c #E_BAD_PARAM if DMA cannot be stopped, #E_NO_ERROR otherwise
96  */
97 int MXC_I2S_Shutdown(void);
98 
99 /**
100  * @brief      Mute I2S Output
101  * @details    Sets I2S data to zero, continues sending clock and accessing DMA
102  * @return   \c #E_NO_ERROR
103  */
104 int MXC_I2S_Mute(void);
105 
106 /**
107  * @brief      Unmute I2S Output
108  * @details    Restores I2S data
109  * @return   \c #E_NO_ERROR
110  */
111 int MXC_I2S_Unmute(void);
112 
113 /**
114  * @brief      Pause I2S Output
115  * @details    Similar to mute, but stops FIFO and DMA access, clocks continue
116  * @return   \c #E_NO_ERROR
117  */
118 int MXC_I2S_Pause(void);
119 
120 /**
121  * @brief      Unpause I2S Output
122  * @details    Similar to mute, but restarts FIFO and DMA access
123  * @return   \c #E_NO_ERROR
124  */
125 int MXC_I2S_Unpause(void);
126 
127 /**
128  * @brief      Stops I2S Output
129  * @details    Similar to pause, but also halts clock
130  * @return   \c #E_NO_ERROR
131  */
132 int MXC_I2S_Stop(void);
133 
134 /**
135  * @brief      Starts I2S Output
136  * @details    Starts I2S Output, automatically called by configure if requested
137  * @return   \c #E_NO_ERROR
138  */
139 int MXC_I2S_Start(void);
140 
141 /**
142  * @brief      Clears DMA Interrupt Flags
143  * @details    Clears the DMA Interrupt flags, should be called at the end of a dma_ctz_cb
144  * @return   \c #E_NO_ERROR
145  */
146 int MXC_I2S_DMA_ClearFlags(void);
147 
148 /**
149  * @brief      Set DMA Addr (Source or Dest) and bytes to transfer
150  * @param      src_addr The address to read data from (Audio Out)
151  * @param      dst_addr The address to write data to (Audio In)
152  * @param      count    The length of the transfer in bytes
153  * @details    Sets the address to read/write data in memory and the length of
154  *             the transfer. The unused addr parameter is ignored.
155  * @return   \c #E_NO_ERROR
156  */
157 int MXC_I2S_DMA_SetAddrCnt(void *src_addr, void *dst_addr, unsigned int count);
158 
159 /**
160  * @brief      Sets the DMA reload address and count
161  * @param      src_addr The address to read data from (Audio Out)
162  * @param      dst_addr The address to write data to (Audio In)
163  * @param      count    The length of the transfer in bytes
164  * @details    If DMA reload is enabled, when the DMA has transfered $count bytes
165  *             (a CTZ event occurs) the src, dst, and count registers will be
166  *             set to these. The DMA reload flag clears after a reload occurs.
167  * @return   \c #E_NO_ERROR
168  */
169 int MXC_I2S_DMA_SetReload(void *src_addr, void *dst_addr, unsigned int count);
170 /**@} end of group i2s */
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_I2S_H_
177