1 /*!
2  * @file        apm32f0xx_cec.c
3  *
4  * @brief       This file contains all the functions for the CEC peripheral
5  *
6  * @version     V1.0.3
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Includes */
27 #include "apm32f0xx_cec.h"
28 #include "apm32f0xx_rcm.h"
29 
30 /** @addtogroup APM32F0xx_StdPeriphDriver
31   @{
32 */
33 
34 /** @addtogroup CEC_Driver
35   @{
36 */
37 
38 /** @defgroup CEC_Macros Macros
39   @{
40 */
41 
42 /**@} end of group CEC_Macros */
43 
44 /** @defgroup CEC_Enumerations Enumerations
45   @{
46 */
47 
48 /**@} end of group CEC_Enumerations */
49 
50 /** @defgroup CEC_Structures Structures
51   @{
52 */
53 
54 /**@} end of group CEC_Structures */
55 
56 /** @defgroup CEC_Variables Variables
57   @{
58 */
59 
60 /**@} end of group CEC_Variables */
61 
62 /** @defgroup CEC_Functions Functions
63   @{
64 */
65 
66 /*!
67  * @brief    Reset CEC peripheral registers to their default values.
68  *
69  * @param    None
70  *
71  * @retval   None
72  */
CEC_Reset(void)73 void CEC_Reset(void)
74 {
75     RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_CEC);
76     RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_CEC);
77 }
78 
79 /*!
80  * @brief    Configs the CEC peripheral according to the specified parameters
81  *           in the cecConfig.
82  *
83  * @param    cecConfig: pointer to an CEC_Config_T structure that is used to
84  *           the configuration information for the specified CEC peripheral.
85  *
86  * @retval   None
87  *
88  * @note     The CEC parameters must be configured before enabling the CEC peripheral.
89  */
CEC_Config(CEC_Config_T * cecConfig)90 void CEC_Config(CEC_Config_T* cecConfig)
91 {
92     CEC->CFG_B.SFTCFG    = cecConfig->signalFreeTime;
93     CEC->CFG_B.RXTCFG   = cecConfig->RxTolerance;
94     CEC->CFG_B.RXSBRERR = cecConfig->stopReception;
95     CEC->CFG_B.GEBRERR  = cecConfig->bitRisingError;
96     CEC->CFG_B.GELBPERR = cecConfig->longPeriodError;
97     CEC->CFG_B.AEBGIB = cecConfig->broadcastrNoGen;
98     CEC->CFG_B.SFTOB  = cecConfig->signalFreeTimeOption;
99 }
100 
101 /*!
102  * @brief    Fills each CEC_InitStruct member with its default value.
103  *
104  * @param    cecConfig: pointer to a CEC_InitTypeDef structure which will
105  *           be initialized.
106  *
107  * @retval   None
108  */
CEC_ConfigStructInit(CEC_Config_T * cecConfig)109 void CEC_ConfigStructInit(CEC_Config_T* cecConfig)
110 {
111     cecConfig->signalFreeTime     = CEC_SINGANL_FREETIME_STANDARD;
112     cecConfig->RxTolerance        = CEC_RX_TOLERANCE_STANDARD;
113     cecConfig->stopReception      = CEC_STOP_RECEPTION_OFF;
114     cecConfig->bitRisingError     = CEC_BIT_RISING_ERR_OFF;
115     cecConfig->longPeriodError    = CEC_LONG_PERIOD_ERR_OFF;
116     cecConfig->broadcastrNoGen    = CEC_BROADCAST_NO_ERR_OFF;
117     cecConfig->signalFreeTimeOption  = CEC_SIGNAL_FREETIME_OPTION_OFF;
118 }
119 
120 /*!
121  * @brief    Enables the CEC peripheral.
122  *
123  * @param    None
124  *
125  * @retval   None
126  */
CEC_Enable(void)127 void CEC_Enable(void)
128 {
129     CEC->CTRL_B.CECEN = SET;
130 }
131 
132 /*!
133  * @brief    Disables the CEC peripheral.
134  *
135  * @param    None
136  *
137  * @retval   None
138  */
CEC_Disable(void)139 void CEC_Disable(void)
140 {
141     CEC->CTRL_B.CECEN = RESET;
142 }
143 
144 /*!
145  * @brief    Enables the CEC Listen Mode.
146  *
147  * @param    None
148  *
149  * @retval   None
150  */
CEC_EnableListenMode(void)151 void CEC_EnableListenMode(void)
152 {
153     CEC->CFG_B.LMODSEL = SET;
154 }
155 
156 /*!
157  * @brief    Disables the CEC Listen Mode.
158  *
159  * @param    None
160  *
161  * @retval   None
162  */
CEC_DisableListenMode(void)163 void CEC_DisableListenMode(void)
164 {
165     CEC->CFG_B.LMODSEL = RESET;
166 }
167 
168 /*!
169  * @brief    Defines the Own Address of the CEC device.
170  *
171  * @param    ownAddress: The CEC own address.
172  *
173  * @retval   None
174  */
CEC_ConfigOwnAddress(uint8_t ownAddress)175 void CEC_ConfigOwnAddress(uint8_t ownAddress)
176 {
177     CEC->CFG_B.OACFG |= (1 << ownAddress);
178 }
179 
180 /*!
181  * @brief    Clears the Own Address of the CEC device.
182  *
183  * @param    CEC_OwnAddress: The CEC own address.
184  *
185  * @retval   None
186  */
CEC_ClearQwnAddress(void)187 void CEC_ClearQwnAddress(void)
188 {
189     CEC->CFG = 0x00;
190 }
191 
192 /*!
193  * @brief    Transmits single data through the CEC peripheral.
194  *
195  * @param    Data: the data to transmit.
196  *
197  * @retval   None
198  */
CEC_TxData(uint8_t Data)199 void CEC_TxData(uint8_t Data)
200 {
201     CEC->TXDATA = Data;
202 }
203 
204 /*!
205  * @brief    Returns the most recent received data by the CEC peripheral.
206  *
207  * @param    None
208  *
209  * @retval   The received data.
210  */
CEC_RxData(void)211 uint8_t CEC_RxData(void)
212 {
213     return (uint8_t)(CEC->RXDATA);
214 }
215 
216 /*!
217  * @brief    Starts a new message.
218  *
219  * @param    None
220  *
221  * @retval   None
222  */
CEC_StartNewMessage(void)223 void CEC_StartNewMessage(void)
224 {
225     CEC->CTRL_B.TXSM = SET;
226 }
227 
228 /*!
229  * @brief  Transmits message with an EOM bit.
230  *
231  * @param  None
232  *
233  * @retval None
234  */
CEC_CompleteMessage(void)235 void CEC_CompleteMessage(void)
236 {
237     CEC->CTRL_B.TXEM = SET;
238 }
239 
240 /*!
241  * @brief    Enables the selected CEC interrupts.
242  *
243  * @param    flag: specifies the CEC interrupt source to be enabled.
244  *            This parameter can be any combination of the following values:
245  *              @arg CEC_INT_RXBR     Rx-Byte Received
246  *              @arg CEC_INT_RXEND    End Of Reception
247  *              @arg CEC_INT_RXOVR    Rx Overrun.
248  *              @arg CEC_INT_BRE      Rx Bit Rising Error
249  *              @arg CEC_INT_SBPE     Rx Short period Error
250  *              @arg CEC_INT_LBPE     Rx Long period Error
251  *              @arg CEC_INT_RXACKE   Rx-Missing Acknowledge
252  *              @arg CEC_INT_ARBLST   Arbitration Lost
253  *              @arg CEC_INT_TXBR     Tx-Byte Request.
254  *              @arg CEC_INT_TXEND    End of Transmission
255  *              @arg CEC_INT_TXUDR    Tx-Buffer Underrun.
256  *              @arg CEC_INT_TXERR    Tx Error.
257  *              @arg CEC_INT_TXACKE   Tx Missing acknowledge Error
258  *
259  * @retval   None
260  */
CEC_EnableInterrupt(uint32_t interrupt)261 void CEC_EnableInterrupt(uint32_t interrupt)
262 {
263     CEC->INTEN |= (uint32_t)interrupt;
264 }
265 
266 /*!
267  * @brief    Disables the selected CEC interrupts.
268  *
269  * @param    flag: specifies the CEC interrupt source to be enabled.
270  *            This parameter can be any combination of the following values:
271  *              @arg CEC_INT_RXBR     Rx-Byte Received
272  *              @arg CEC_INT_RXEND    End Of Reception
273  *              @arg CEC_INT_RXOVR    Rx Overrun.
274  *              @arg CEC_INT_BRE      Rx Bit Rising Error
275  *              @arg CEC_INT_SBPE     Rx Short period Error
276  *              @arg CEC_INT_LBPE     Rx Long period Error
277  *              @arg CEC_INT_RXACKE   Rx-Missing Acknowledge
278  *              @arg CEC_INT_ARBLST   Arbitration Lost
279  *              @arg CEC_INT_TXBR     Tx-Byte Request.
280  *              @arg CEC_INT_TXEND    End of Transmission
281  *              @arg CEC_INT_TXUDR    Tx-Buffer Underrun.
282  *              @arg CEC_INT_TXERR    Tx Error.
283  *              @arg CEC_INT_TXACKE   Tx Missing acknowledge Error
284  *
285  * @retval   None
286  */
CEC_DisableInterrupt(uint32_t interrupt)287 void CEC_DisableInterrupt(uint32_t interrupt)
288 {
289     CEC->INTEN &= ~(uint32_t)interrupt;
290 }
291 
292 /*!
293  * @brief    Read the CEC flag status.
294  *
295  * @param    flag: specifies the CEC interrupt source to be enabled.
296  *            This parameter can be any combination of the following values:
297  *              @arg CEC_FLAG_RXBR      Rx-Byte Received
298  *              @arg CEC_FLAG_RXEND     End Of Reception
299  *              @arg CEC_FLAG_RXOVR     Rx Overrun.
300  *              @arg CEC_FLAG_BRE       Rx Bit Rising Error
301  *              @arg CEC_FLAG_SBPE      Rx Short period Error
302  *              @arg CEC_FLAG_LBPE      Rx Long period Error
303  *              @arg CEC_FLAG_RXACKE    Rx-Missing Acknowledge
304  *              @arg CEC_FLAG_ARBLST    Arbitration Lost
305  *              @arg CEC_FLAG_TXBR      Tx-Byte Request.
306  *              @arg CEC_FLAG_TXEND     End of Transmission
307  *              @arg CEC_FLAG_TXUDR     Tx-Buffer Underrun.
308  *              @arg CEC_FLAG_TXERR     Tx Error.
309  *              @arg CEC_FLAG_TXACKE    Tx Missing acknowledge Error
310  *
311  * @retval   The new state of CEC_FLAG (SET or RESET)
312  */
CEC_ReadStatusFlag(uint32_t flag)313 uint8_t CEC_ReadStatusFlag(uint32_t flag)
314 {
315     uint32_t status;
316 
317     status = (uint32_t)(CEC->STS & flag);
318 
319     if (status == flag)
320     {
321         return SET;
322     }
323 
324     return RESET;
325 }
326 
327 /*!
328  * @brief    Clears the CEC's pending flags.
329  *
330  * @param    flag: specifies the flag to clear.
331  *            This parameter can be any combination of the following values:
332  *              @arg CEC_FLAG_RXBR      Rx-Byte Received
333  *              @arg CEC_FLAG_RXEND     End Of Reception
334  *              @arg CEC_FLAG_RXOVR     Rx Overrun.
335  *              @arg CEC_FLAG_BRE       Rx Bit Rising Error
336  *              @arg CEC_FLAG_SBPE      Rx Short period Error
337  *              @arg CEC_FLAG_LBPE      Rx Long period Error
338  *              @arg CEC_FLAG_RXACKE    Rx-Missing Acknowledge
339  *              @arg CEC_FLAG_ARBLST    Arbitration Lost
340  *              @arg CEC_FLAG_TXBR      Tx-Byte Request.
341  *              @arg CEC_FLAG_TXEND     End of Transmission
342  *              @arg CEC_FLAG_TXUDR     Tx-Buffer Underrun.
343  *              @arg CEC_FLAG_TXERR     Tx Error.
344  *              @arg CEC_FLAG_TXACKE    Tx Missing acknowledge Error
345  *
346  * @retval   None
347  */
CEC_ClearStatusFlag(uint32_t flag)348 void CEC_ClearStatusFlag(uint32_t flag)
349 {
350     CEC->STS = flag;
351 }
352 
353 /*!
354  * @brief    Checks whether the specified CEC interrupt has occurred or not.
355  *
356  * @param    flag: specifies the CEC interrupt source to check.
357  *            This parameter can be one of the following values:
358  *              @arg CEC_INT_RXBR     Rx-Byte Received
359  *              @arg CEC_INT_RXEND    End Of Reception
360  *              @arg CEC_INT_RXOVR    Rx Overrun.
361  *              @arg CEC_INT_BRE      Rx Bit Rising Error
362  *              @arg CEC_INT_SBPE     Rx Short period Error
363  *              @arg CEC_INT_LBPE     Rx Long period Error
364  *              @arg CEC_INT_RXACKE   Rx-Missing Acknowledge
365  *              @arg CEC_INT_ARBLST   Arbitration Lost
366  *              @arg CEC_INT_TXBR     Tx-Byte Request.
367  *              @arg CEC_INT_TXEND    End of Transmission
368  *              @arg CEC_INT_TXUDR    Tx-Buffer Underrun.
369  *              @arg CEC_INT_TXERR    Tx Error.
370  *              @arg CEC_INT_TXACKE   Tx Missing acknowledge Error
371  *
372  * @retval   The new state of Flag (SET or RESET).
373  */
CEC_ReadIntFlag(uint16_t flag)374 uint8_t CEC_ReadIntFlag(uint16_t flag)
375 {
376     uint32_t intEnable;
377     uint32_t intStatus;
378     intEnable = (CEC->INTEN & flag);
379     intStatus = (CEC->STS & flag);
380     if ((intStatus != (uint32_t)RESET) && intEnable)
381     {
382         return SET;
383     }
384     else
385     {
386         return RESET;
387     }
388 }
389 
390 /*!
391  * @brief    Clears the CEC's interrupt flag.
392  *
393  * @param    flag: specifies the CEC interrupt flag to clear.
394  *            This parameter can be any combination of the following values:
395  *              @arg CEC_INT_RXBR     Rx-Byte Received
396  *              @arg CEC_INT_RXEND    End Of Reception
397  *              @arg CEC_INT_RXOVR    Rx Overrun.
398  *              @arg CEC_INT_BRE      Rx Bit Rising Error
399  *              @arg CEC_INT_SBPE     Rx Short period Error
400  *              @arg CEC_INT_LBPE     Rx Long period Error
401  *              @arg CEC_INT_RXACKE   Rx-Missing Acknowledge
402  *              @arg CEC_INT_ARBLST   Arbitration Lost
403  *              @arg CEC_INT_TXBR     Tx-Byte Request.
404  *              @arg CEC_INT_TXEND    End of Transmission
405  *              @arg CEC_INT_TXUDR    Tx-Buffer Underrun.
406  *              @arg CEC_INT_TXERR    Tx Error.
407  *              @arg CEC_INT_TXACKE   Tx Missing acknowledge Error
408  *
409  * @retval   None
410  */
CEC_ClearIntFlag(uint16_t flag)411 void CEC_ClearIntFlag(uint16_t flag)
412 {
413     CEC->STS = flag;
414 }
415 
416 /**@} end of group CEC_Functions */
417 /**@} end of group CEC_Driver */
418 /**@} end of group APM32F0xx_StdPeriphDriver */
419