1 /***************************************************************************//**
2 * \file cy_tdm.c
3 * \version 1.0
4 *
5 * Provides an API implementation of the audio dac driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2021, Cypress Semiconductor Corporation. All rights reserved.
10 * You may use this file only in accordance with the license, terms, conditions,
11 * disclaimers, and limitations in the end user license agreement accompanying
12 * the software package with which this file was provided.
13 *******************************************************************************/
14 
15 #include "cy_device.h"
16 
17 #if defined (CY_IP_MXTDM)
18 
19 #include "cy_tdm.h"
20 #include <stdlib.h>
21 
22 #if defined(__cplusplus)
23 extern "C" {
24 
25 #endif /* __cplusplus */
26 
27 
28 cy_en_tdm_status_t Cy_AudioTDM_TX_Init( TDM_TX_STRUCT_Type * base, cy_stc_tdm_config_tx_t const * config);
29 void Cy_AudioTDM_TX_DeInit( TDM_TX_STRUCT_Type * base);
30 
31 cy_en_tdm_status_t Cy_AudioTDM_RX_Init( TDM_RX_STRUCT_Type * base, cy_stc_tdm_config_rx_t const * config);
32 void Cy_AudioTDM_RX_DeInit( TDM_RX_STRUCT_Type * base);
33 
34 /**
35 * \addtogroup group_tdm_functions
36 * \{
37 */
38 
39 /*******************************************************************************
40 * Function Name: Cy_AudioTDM_Init
41 ****************************************************************************//**
42 *
43 * Initializes the TDM module in accordance with a configuration structure.
44 *
45 * \pre If the TDM module is initialized previously, the \ref Cy_AudioTDM_DeInit()
46 * must be called before calling this function.
47 *
48 * \param base The pointer to the TDM instance address.
49 *
50 * \param config The pointer to a configuration structure.
51 *
52 * \return error / status code. See \ref cy_en_tdm_status_t.
53 *
54 * \funcusage
55 * \snippet tdm/snippet/main.c snippet_Cy_AudioTDM_Init
56 *
57 *******************************************************************************/
Cy_AudioTDM_Init(TDM_STRUCT_Type * base,cy_stc_tdm_config_t const * config)58 cy_en_tdm_status_t Cy_AudioTDM_Init( TDM_STRUCT_Type * base, cy_stc_tdm_config_t const * config)
59 {
60     cy_en_tdm_status_t ret = CY_TDM_BAD_PARAM;
61 
62     if((NULL != base) && (NULL != config))
63     {
64        if(config->tx_config->enable)
65        {
66            ret = Cy_AudioTDM_TX_Init(&(base->TDM_TX_STRUCT),(config->tx_config));
67            if(ret == CY_TDM_BAD_PARAM)
68            {
69                return(ret);
70            }
71        }
72        if(config->rx_config->enable)
73        {
74             ret = Cy_AudioTDM_RX_Init(&(base->TDM_RX_STRUCT),(config->rx_config));
75        }
76     }
77     return (ret);
78 }
79 
80 /** \} group_tdm_functions */
81 
82 /**************************************************************************/
83 /* Function Name: Cy_AudioTDM_TX_Init                                     */
84 /**************************************************************************/
85 /*
86 * Initializes the TDM Transmitter module in accordance with a configuration structure.
87 *
88 * \pre If the TDM TX module is initialized previously, the \ref Cy_AudioTX_Tx_DeInit()
89 * must be called before calling this function.
90 *
91 * \param base The pointer to the TDM->TX instance address.
92 *
93 * \param config The pointer to a configuration structure.
94 *
95 * \return error / status code. See \ref cy_en_tdm_status_t.
96 *
97 * \funcusage
98 * \snippet tdm/snippet/main.c snippet_Cy_AudioTDM_Init
99 *
100 ****************************************************************************/
Cy_AudioTDM_TX_Init(TDM_TX_STRUCT_Type * base,cy_stc_tdm_config_tx_t const * config)101 cy_en_tdm_status_t Cy_AudioTDM_TX_Init( TDM_TX_STRUCT_Type * base, cy_stc_tdm_config_tx_t const * config)
102 {
103     cy_en_tdm_status_t          ret     = CY_TDM_SUCCESS;
104     uint16_t clockDiv = config->clkDiv -1U;
105     uint8_t channelNum = config->channelNum - 1U;
106     uint8_t channelSIZE = config->channelSize - 1U;
107     CY_ASSERT_L2(base);
108     CY_ASSERT_L2(config);
109     CY_ASSERT_L2(CY_TDM_IS_CLK_DIV_VALID(clockDiv));
110     CY_ASSERT_L2(CY_TDM_IS_CHANNELS_VALID(channelNum));
111     CY_ASSERT_L2(CY_TDM_IS_CHANNEL_SIZE_VALID(channelSIZE));
112     CY_ASSERT_L2(CY_I2S_TDM_IS_INPUT_SIGNAL_MODE_VALID(config->signalInput));
113 
114     /* Channel Default */
115     if((bool)(TDM_STRUCT_TX_CTL(base) & TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_CTL_ENABLED_Msk))
116     {
117         ret = CY_TDM_BAD_PARAM;
118         return(ret);
119     }
120 
121     /* The TX interface setting */
122     TDM_STRUCT_TX_IF_CTL(base) =
123         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_CLOCK_DIV,         clockDiv)               |
124         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_CLOCK_SEL,         config->clkSel)         |
125         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_SCK_POLARITY,     config->sckPolarity)     |
126         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_FSYNC_POLARITY, config->fsyncPolarity)     |
127         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_FSYNC_FORMAT,     config->fsyncFormat)     |
128         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_CH_NR,             channelNum)             |
129         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_CH_SIZE,         channelSIZE)              |
130         _BOOL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_IF_CTL_I2S_MODE,         config->i2sMode);
131 
132     if(!((bool)(config->masterMode)))
133     {
134         TDM_STRUCT_TX_ROUTE_CTL(base) = _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_ROUTE_CTL_MODE, config->signalInput);
135     }
136 
137     /* Chanel Enable */
138     TDM_STRUCT_TX_CH_CTL(base) = config->chEn;
139     /* The FIFO setting */
140     TDM_STRUCT_TX_FIFO_CTL(base) = _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_FIFO_CTL_TRIGGER_LEVEL,config->fifoTriggerLevel);
141     /* The TC Interface setting */
142     TDM_STRUCT_TX_CTL(base) =
143         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_CTL_WORD_SIZE,    config->wordSize) |
144         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_CTL_FORMAT,     config->format)     |
145         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_CTL_MS, config->masterMode);
146 
147     return (ret);
148 }
149 
150 /**************************************************************************/
151 /* Function Name: Cy_AudioTDM_RX_Init                                     */
152 /**************************************************************************/
Cy_AudioTDM_RX_Init(TDM_RX_STRUCT_Type * base,cy_stc_tdm_config_rx_t const * config)153 cy_en_tdm_status_t Cy_AudioTDM_RX_Init( TDM_RX_STRUCT_Type * base, cy_stc_tdm_config_rx_t const * config)
154 {
155     cy_en_tdm_status_t ret = CY_TDM_SUCCESS;
156     uint16_t clockDiv   = config->clkDiv - 1U;
157     uint8_t channelSIZE = config->channelSize - 1U;
158     uint8_t channelNum  = config->channelNum - 1U;
159     CY_ASSERT_L2(base);
160     CY_ASSERT_L2(config);
161     CY_ASSERT_L2(CY_TDM_IS_CLK_DIV_VALID(clockDiv));
162     CY_ASSERT_L2(CY_TDM_IS_CHANNELS_VALID(channelNum));
163     CY_ASSERT_L2(CY_TDM_IS_CHANNEL_SIZE_VALID(channelSIZE));
164     CY_ASSERT_L2(CY_I2S_TDM_IS_INPUT_SIGNAL_MODE_VALID(config->signalInput));
165 
166     /* The RX interface setting */
167     TDM_STRUCT_RX_IF_CTL(base) =
168         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_CLOCK_DIV,         clockDiv)             |
169         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_CLOCK_SEL,         config->clkSel)       |
170         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_SCK_POLARITY,     config->sckPolarity)   |
171         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_FSYNC_POLARITY, config->fsyncPolarity)   |
172         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_LATE_SAMPLE,     config->lateSample)     |
173         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_FSYNC_FORMAT,     config->fsyncFormat)   |
174         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_CH_NR,             channelNum)           |
175         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_CH_SIZE,         channelSIZE)            |
176         _BOOL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_IF_CTL_I2S_MODE,         config->i2sMode);
177 
178     if(!((bool)(config->masterMode)))
179     {
180         TDM_STRUCT_RX_ROUTE_CTL(base) = _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_ROUTE_CTL_MODE, config->signalInput);
181     }
182 
183     /* Chanel Enable */
184     TDM_STRUCT_RX_CH_CTL(base) = config->chEn;
185     /* The FIFO setting */
186     TDM_STRUCT_RX_FIFO_CTL(base) = _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_FIFO_CTL_TRIGGER_LEVEL,config->fifoTriggerLevel);
187     /* The TC Interface setting */
188     TDM_STRUCT_RX_CTL(base) =
189         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_CTL_WORD_SIZE,            config->wordSize)   |
190         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_CTL_WORD_SIGN_EXTEND,    config->signExtend) |
191         _VAL2FLD(TDM_TDM_STRUCT_TDM_RX_STRUCT_RX_CTL_FORMAT,             config->format)     |
192         _VAL2FLD(TDM_TDM_STRUCT_TDM_TX_STRUCT_TX_CTL_MS, config->masterMode);
193 
194     return (ret);
195 }
196 
197 /**
198 * \addtogroup group_tdm_functions
199 * \{
200 */
201 
202 
203 /*******************************************************************************
204 * Function Name: Cy_AudioTDM_DeInit
205 ****************************************************************************//**
206 * Deinitialize the TDM block.
207 *
208 * \param base base
209 *
210 * \funcusage
211 * \snippet tdm/snippet/main.c snippet_Cy_AudioTDM_DeInit
212 *******************************************************************************/
Cy_AudioTDM_DeInit(TDM_STRUCT_Type * base)213 void Cy_AudioTDM_DeInit( TDM_STRUCT_Type * base)
214 {
215     Cy_AudioTDM_TX_DeInit(&(base->TDM_TX_STRUCT));
216     Cy_AudioTDM_RX_DeInit(&(base->TDM_RX_STRUCT));
217 }
218 
219 /** \} group_tdm_functions */
220 
221 /**************************************************************************/
222 /* Function Name: Cy_AudioTDM_TX_DeInit                                   */
223 /**************************************************************************/
Cy_AudioTDM_TX_DeInit(TDM_TX_STRUCT_Type * base)224 void Cy_AudioTDM_TX_DeInit( TDM_TX_STRUCT_Type * base)
225 {
226     TDM_STRUCT_TX_INTR_TX_MASK(base)    = 0UL;
227     TDM_STRUCT_TX_FIFO_CTL(base)        = 0UL;
228     TDM_STRUCT_TX_IF_CTL(base)            = CY_TDM_TX_IF_CTL_DEFAULT;
229     TDM_STRUCT_TX_CTL(base)                = CY_TDM_TX_CTL_DEFAULT;
230 }
231 
232 /**************************************************************************/
233 /* Function Name: Cy_AudioTDM_RX_DeInit                                   */
234 /**************************************************************************/
Cy_AudioTDM_RX_DeInit(TDM_RX_STRUCT_Type * base)235 void Cy_AudioTDM_RX_DeInit( TDM_RX_STRUCT_Type * base)
236 {
237     TDM_STRUCT_RX_INTR_RX_MASK(base)    = 0UL;
238     TDM_STRUCT_RX_FIFO_CTL(base)        = 0UL;
239     TDM_STRUCT_RX_IF_CTL(base)            = CY_TDM_RX_IF_CTL_DEFAULT;
240     TDM_STRUCT_RX_CTL(base)                = CY_TDM_RX_CTL_DEFAULT;
241 }
242 
243 #if defined(__cplusplus)
244 }
245 #endif /* __cplusplus */
246 
247 
248 #endif /* CY_IP_MXTDM */
249 /******************************************************************************/
250 /* [] END OF FILE */
251 /******************************************************************************/
252