2 ******************************************************************************
3 * @file stm32f4xx_cec.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Consumer Electronics Control (CEC) peripheral
9 * applicable only on STM32F446xx devices:
10 * + Initialization and Configuration
11 * + Data transfers functions
12 * + Interrupts and flags management
15 ==============================================================================
16 ##### CEC features #####
17 ==============================================================================
18 [..] This device provides some features:
19 (#) Supports HDMI-CEC specification 1.4.
20 (#) Supports two source clocks(HSI/244 or LSE).
21 (#) Works in stop mode(without APB clock, but with CEC clock 32KHz).
22 It can genarate an interrupt in the CEC clock domain that the CPU
23 wakes up from the low power mode.
24 (#) Configurable Signal Free Time before of transmission start. The
25 number of nominal data bit periods waited before transmission can be
26 ruled by Hardware or Software.
27 (#) Configurable Peripheral Address (multi-addressing configuration).
28 (#) Supports listen mode.The CEC Messages addressed to different destination
29 can be received without interfering with CEC bus when Listen mode option is enabled.
30 (#) Configurable Rx-Tolerance(Standard and Extended tolerance margin).
31 (#) Error detection with configurable error bit generation.
32 (#) Arbitration lost error in the case of two CEC devices starting at the same time.
34 ##### How to use this driver #####
35 ==============================================================================
36 [..] This driver provides functions to configure and program the CEC device,
38 (#) The source clock can be configured using:
39 (++) RCC_CECCLKConfig(RCC_CECCLK_HSI_Div244) for HSI(Default)
40 (++) RCC_CECCLKConfig(RCC_CECCLK_LSE) for LSE.
41 (#) Enable CEC peripheral clock using RCC_APBPeriphClockCmd(RCC_APBPeriph_CEC, ENABLE).
42 (#) Peripherals alternate function.
43 (++) Connect the pin to the desired peripherals' Alternate Function (AF) using
44 GPIO_PinAFConfig() function.
45 (++) Configure the desired pin in alternate function by:
46 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
47 (++) Select the type open-drain and output speed via GPIO_OType
48 and GPIO_Speed members.
49 (++) Call GPIO_Init() function.
50 (#) Configure the Signal Free Time, Rx Tolerance, Stop reception generation
51 and Bit error generation using the CEC_Init() function.
52 The function CEC_Init() must be called when the CEC peripheral is disabled.
53 (#) Configure the CEC own address by calling the fuction CEC_OwnAddressConfig().
54 (#) Optionally, you can configure the Listen mode using the function CEC_ListenModeCmd().
55 (#) Enable the NVIC and the corresponding interrupt using the function
56 CEC_ITConfig() if you need to use interrupt mode.
57 CEC_ITConfig() must be called before enabling the CEC peripheral.
58 (#) Enable the CEC using the CEC_Cmd() function.
59 (#) Charge the first data byte in the TXDR register using CEC_SendDataByte().
60 (#) Enable the transmission of the Byte of a CEC message using CEC_StartOfMessage()
61 (#) Transmit single data through the CEC peripheral using CEC_SendDataByte()
62 and Receive the last transmitted byte using CEC_ReceiveDataByte().
63 (#) Enable the CEC_EndOfMessage() in order to indicate the last byte of the message.
65 (@) If the listen mode is enabled, Stop reception generation and Bit error generation
66 must be in reset state.
67 (@) If the CEC message consists of only 1 byte, the function CEC_EndOfMessage()
68 must be called before CEC_StartOfMessage().
72 ******************************************************************************
75 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
77 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
78 * You may not use this file except in compliance with the License.
79 * You may obtain a copy of the License at:
81 * http://www.st.com/software_license_agreement_liberty_v2
83 * Unless required by applicable law or agreed to in writing, software
84 * distributed under the License is distributed on an "AS IS" BASIS,
85 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
86 * See the License for the specific language governing permissions and
87 * limitations under the License.
89 ******************************************************************************
92 /* Includes ------------------------------------------------------------------*/
93 #include "stm32f4xx_cec.h"
94 #include "stm32f4xx_rcc.h"
96 /** @addtogroup STM32F4xx_StdPeriph_Driver
101 * @brief CEC driver modules
104 #if defined(STM32F446xx)
105 /* Private typedef -----------------------------------------------------------*/
106 /* Private define ------------------------------------------------------------*/
107 #define BROADCAST_ADDRESS ((uint32_t)0x0000F)
108 #define CFGR_CLEAR_MASK ((uint32_t)0x7000FE00) /* CFGR register Mask */
110 /* Private macro -------------------------------------------------------------*/
111 /* Private variables ---------------------------------------------------------*/
112 /* Private function prototypes -----------------------------------------------*/
113 /* Private functions ---------------------------------------------------------*/
115 /** @defgroup CEC_Private_Functions
119 /** @defgroup CEC_Group1 Initialization and Configuration functions
120 * @brief Initialization and Configuration functions
123 ===============================================================================
124 ##### Initialization and Configuration functions #####
125 ===============================================================================
126 [..] This section provides functions allowing to initialize:
127 (+) CEC own addresses
128 (+) CEC Signal Free Time
130 (+) CEC Stop Reception
131 (+) CEC Bit Rising Error
132 (+) CEC Long Bit Period Error
133 [..] This section provides also a function to configure the CEC peripheral in Listen Mode.
134 Messages addressed to different destination can be received when Listen mode is
135 enabled without interfering with CEC bus.
141 * @brief Deinitializes the CEC peripheral registers to their default reset values.
145 void CEC_DeInit(void)
147 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC
, ENABLE
);
148 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC
, DISABLE
);
152 * @brief Initializes the CEC peripheral according to the specified parameters
153 * in the CEC_InitStruct.
154 * @note The CEC parameters must be configured before enabling the CEC peripheral.
155 * @param CEC_InitStruct: pointer to an CEC_InitTypeDef structure that contains
156 * the configuration information for the specified CEC peripheral.
159 void CEC_Init(CEC_InitTypeDef
* CEC_InitStruct
)
163 /* Check the parameters */
164 assert_param(IS_CEC_SIGNAL_FREE_TIME(CEC_InitStruct
->CEC_SignalFreeTime
));
165 assert_param(IS_CEC_RX_TOLERANCE(CEC_InitStruct
->CEC_RxTolerance
));
166 assert_param(IS_CEC_STOP_RECEPTION(CEC_InitStruct
->CEC_StopReception
));
167 assert_param(IS_CEC_BIT_RISING_ERROR(CEC_InitStruct
->CEC_BitRisingError
));
168 assert_param(IS_CEC_LONG_BIT_PERIOD_ERROR(CEC_InitStruct
->CEC_LongBitPeriodError
));
169 assert_param(IS_CEC_BDR_NO_GEN_ERROR(CEC_InitStruct
->CEC_BRDNoGen
));
170 assert_param(IS_CEC_SFT_OPTION(CEC_InitStruct
->CEC_SFTOption
));
172 /* Get the CEC CFGR value */
175 /* Clear CFGR bits */
176 tmpreg
&= CFGR_CLEAR_MASK
;
178 /* Configure the CEC peripheral */
179 tmpreg
|= (CEC_InitStruct
->CEC_SignalFreeTime
| CEC_InitStruct
->CEC_RxTolerance
|
180 CEC_InitStruct
->CEC_StopReception
| CEC_InitStruct
->CEC_BitRisingError
|
181 CEC_InitStruct
->CEC_LongBitPeriodError
| CEC_InitStruct
->CEC_BRDNoGen
|
182 CEC_InitStruct
->CEC_SFTOption
);
184 /* Write to CEC CFGR register */
189 * @brief Fills each CEC_InitStruct member with its default value.
190 * @param CEC_InitStruct: pointer to a CEC_InitTypeDef structure which will
194 void CEC_StructInit(CEC_InitTypeDef
* CEC_InitStruct
)
196 CEC_InitStruct
->CEC_SignalFreeTime
= CEC_SignalFreeTime_Standard
;
197 CEC_InitStruct
->CEC_RxTolerance
= CEC_RxTolerance_Standard
;
198 CEC_InitStruct
->CEC_StopReception
= CEC_StopReception_Off
;
199 CEC_InitStruct
->CEC_BitRisingError
= CEC_BitRisingError_Off
;
200 CEC_InitStruct
->CEC_LongBitPeriodError
= CEC_LongBitPeriodError_Off
;
201 CEC_InitStruct
->CEC_BRDNoGen
= CEC_BRDNoGen_Off
;
202 CEC_InitStruct
->CEC_SFTOption
= CEC_SFTOption_Off
;
206 * @brief Enables or disables the CEC peripheral.
207 * @param NewState: new state of the CEC peripheral.
208 * This parameter can be: ENABLE or DISABLE.
211 void CEC_Cmd(FunctionalState NewState
)
213 assert_param(IS_FUNCTIONAL_STATE(NewState
));
215 if (NewState
!= DISABLE
)
217 /* Enable the CEC peripheral */
218 CEC
->CR
|= CEC_CR_CECEN
;
222 /* Disable the CEC peripheral */
223 CEC
->CR
&= ~CEC_CR_CECEN
;
228 * @brief Enables or disables the CEC Listen Mode.
229 * @param NewState: new state of the Listen Mode.
230 * This parameter can be: ENABLE or DISABLE.
233 void CEC_ListenModeCmd(FunctionalState NewState
)
235 assert_param(IS_FUNCTIONAL_STATE(NewState
));
237 if (NewState
!= DISABLE
)
239 /* Enable the Listen Mode */
240 CEC
->CFGR
|= CEC_CFGR_LSTN
;
244 /* Disable the Listen Mode */
245 CEC
->CFGR
&= ~CEC_CFGR_LSTN
;
250 * @brief Defines the Own Address of the CEC device.
251 * @param CEC_OwnAddress: The CEC own address.
254 void CEC_OwnAddressConfig(uint8_t CEC_OwnAddress
)
257 /* Check the parameters */
258 assert_param(IS_CEC_ADDRESS(CEC_OwnAddress
));
259 tmp
= 1 <<(CEC_OwnAddress
+ 16);
260 /* Set the CEC own address */
265 * @brief Clears the Own Address of the CEC device.
266 * @param CEC_OwnAddress: The CEC own address.
269 void CEC_OwnAddressClear(void)
271 /* Set the CEC own address */
279 /** @defgroup CEC_Group2 Data transfers functions
280 * @brief Data transfers functions
283 ===============================================================================
284 ##### Data transfers functions #####
285 ===============================================================================
286 [..] This section provides functions allowing the CEC data transfers.The read
287 access of the CEC_RXDR register can be done using the CEC_ReceiveData()function
288 and returns the Rx buffered value. Whereas a write access to the CEC_TXDR can be
289 done using CEC_SendData() function.
295 * @brief Transmits single data through the CEC peripheral.
296 * @param Data: the data to transmit.
299 void CEC_SendData(uint8_t Data
)
306 * @brief Returns the most recent received data by the CEC peripheral.
308 * @retval The received data.
310 uint8_t CEC_ReceiveData(void)
313 return (uint8_t)(CEC
->RXDR
);
317 * @brief Starts a new message.
321 void CEC_StartOfMessage(void)
323 /* Starts of new message */
324 CEC
->CR
|= CEC_CR_TXSOM
;
328 * @brief Transmits message with an EOM bit.
332 void CEC_EndOfMessage(void)
334 /* The data byte will be transmitted with an EOM bit */
335 CEC
->CR
|= CEC_CR_TXEOM
;
342 /** @defgroup CEC_Group3 Interrupts and flags management functions
343 * @brief Interrupts and flags management functions
346 ===============================================================================
347 ##### Interrupts and flags management functions #####
348 ===============================================================================
349 [..] This section provides functions allowing to configure the CEC Interrupts
350 sources and check or clear the flags or pending bits status.
351 [..] The user should identify which mode will be used in his application to manage
352 the communication: Polling mode or Interrupt mode.
354 [..] In polling mode, the CEC can be managed by the following flags:
355 (+) CEC_FLAG_TXACKE : to indicate a missing acknowledge in transmission mode.
356 (+) CEC_FLAG_TXERR : to indicate an error occurs during transmission mode.
357 The initiator detects low impedance in the CEC line.
358 (+) CEC_FLAG_TXUDR : to indicate if an underrun error occurs in transmission mode.
359 The transmission is enabled while the software has not yet
360 loaded any value into the TXDR register.
361 (+) CEC_FLAG_TXEND : to indicate the end of successful transmission.
362 (+) CEC_FLAG_TXBR : to indicate the next transmission data has to be written to TXDR.
363 (+) CEC_FLAG_ARBLST : to indicate arbitration lost in the case of two CEC devices
364 starting at the same time.
365 (+) CEC_FLAG_RXACKE : to indicate a missing acknowledge in receive mode.
366 (+) CEC_FLAG_LBPE : to indicate a long bit period error generated during receive mode.
367 (+) CEC_FLAG_SBPE : to indicate a short bit period error generated during receive mode.
368 (+) CEC_FLAG_BRE : to indicate a bit rising error generated during receive mode.
369 (+) CEC_FLAG_RXOVR : to indicate if an overrun error occur while receiving a CEC message.
370 A byte is not yet received while a new byte is stored in the RXDR register.
371 (+) CEC_FLAG_RXEND : to indicate the end Of reception
372 (+) CEC_FLAG_RXBR : to indicate a new byte has been received from the CEC line and
373 stored into the RXDR buffer.
375 (@)In this Mode, it is advised to use the following functions:
376 FlagStatus CEC_GetFlagStatus(uint16_t CEC_FLAG);
377 void CEC_ClearFlag(uint16_t CEC_FLAG);
379 [..] In Interrupt mode, the CEC can be managed by the following interrupt sources:
380 (+) CEC_IT_TXACKE : to indicate a TX Missing acknowledge
381 (+) CEC_IT_TXACKE : to indicate a missing acknowledge in transmission mode.
382 (+) CEC_IT_TXERR : to indicate an error occurs during transmission mode.
383 The initiator detects low impedance in the CEC line.
384 (+) CEC_IT_TXUDR : to indicate if an underrun error occurs in transmission mode.
385 The transmission is enabled while the software has not yet
386 loaded any value into the TXDR register.
387 (+) CEC_IT_TXEND : to indicate the end of successful transmission.
388 (+) CEC_IT_TXBR : to indicate the next transmission data has to be written to TXDR register.
389 (+) CEC_IT_ARBLST : to indicate arbitration lost in the case of two CEC devices
390 starting at the same time.
391 (+) CEC_IT_RXACKE : to indicate a missing acknowledge in receive mode.
392 (+) CEC_IT_LBPE : to indicate a long bit period error generated during receive mode.
393 (+) CEC_IT_SBPE : to indicate a short bit period error generated during receive mode.
394 (+) CEC_IT_BRE : to indicate a bit rising error generated during receive mode.
395 (+) CEC_IT_RXOVR : to indicate if an overrun error occur while receiving a CEC message.
396 A byte is not yet received while a new byte is stored in the RXDR register.
397 (+) CEC_IT_RXEND : to indicate the end Of reception
398 (+) CEC_IT_RXBR : to indicate a new byte has been received from the CEC line and
399 stored into the RXDR buffer.
401 (@)In this Mode it is advised to use the following functions:
402 void CEC_ITConfig( uint16_t CEC_IT, FunctionalState NewState);
403 ITStatus CEC_GetITStatus(uint16_t CEC_IT);
404 void CEC_ClearITPendingBit(uint16_t CEC_IT);
412 * @brief Enables or disables the selected CEC interrupts.
413 * @param CEC_IT: specifies the CEC interrupt source to be enabled.
414 * This parameter can be any combination of the following values:
415 * @arg CEC_IT_TXACKE: Tx Missing acknowledge Error
416 * @arg CEC_IT_TXERR: Tx Error.
417 * @arg CEC_IT_TXUDR: Tx-Buffer Underrun.
418 * @arg CEC_IT_TXEND: End of Transmission (successful transmission of the last byte).
419 * @arg CEC_IT_TXBR: Tx-Byte Request.
420 * @arg CEC_IT_ARBLST: Arbitration Lost
421 * @arg CEC_IT_RXACKE: Rx-Missing Acknowledge
422 * @arg CEC_IT_LBPE: Rx Long period Error
423 * @arg CEC_IT_SBPE: Rx Short period Error
424 * @arg CEC_IT_BRE: Rx Bit Rising Error
425 * @arg CEC_IT_RXOVR: Rx Overrun.
426 * @arg CEC_IT_RXEND: End Of Reception
427 * @arg CEC_IT_RXBR: Rx-Byte Received
428 * @param NewState: new state of the selected CEC interrupts.
429 * This parameter can be: ENABLE or DISABLE.
432 void CEC_ITConfig(uint16_t CEC_IT
, FunctionalState NewState
)
434 assert_param(IS_FUNCTIONAL_STATE(NewState
));
435 assert_param(IS_CEC_IT(CEC_IT
));
437 if (NewState
!= DISABLE
)
439 /* Enable the selected CEC interrupt */
445 /* Disable the selected CEC interrupt */
451 * @brief Gets the CEC flag status.
452 * @param CEC_FLAG: specifies the CEC flag to check.
453 * This parameter can be one of the following values:
454 * @arg CEC_FLAG_TXACKE: Tx Missing acknowledge Error
455 * @arg CEC_FLAG_TXERR: Tx Error.
456 * @arg CEC_FLAG_TXUDR: Tx-Buffer Underrun.
457 * @arg CEC_FLAG_TXEND: End of transmission (successful transmission of the last byte).
458 * @arg CEC_FLAG_TXBR: Tx-Byte Request.
459 * @arg CEC_FLAG_ARBLST: Arbitration Lost
460 * @arg CEC_FLAG_RXACKE: Rx-Missing Acknowledge
461 * @arg CEC_FLAG_LBPE: Rx Long period Error
462 * @arg CEC_FLAG_SBPE: Rx Short period Error
463 * @arg CEC_FLAG_BRE: Rx Bit Rissing Error
464 * @arg CEC_FLAG_RXOVR: Rx Overrun.
465 * @arg CEC_FLAG_RXEND: End Of Reception.
466 * @arg CEC_FLAG_RXBR: Rx-Byte Received.
467 * @retval The new state of CEC_FLAG (SET or RESET)
469 FlagStatus
CEC_GetFlagStatus(uint16_t CEC_FLAG
)
471 FlagStatus bitstatus
= RESET
;
473 assert_param(IS_CEC_GET_FLAG(CEC_FLAG
));
475 /* Check the status of the specified CEC flag */
476 if ((CEC
->ISR
& CEC_FLAG
) != (uint16_t)RESET
)
478 /* CEC flag is set */
483 /* CEC flag is reset */
487 /* Return the CEC flag status */
492 * @brief Clears the CEC's pending flags.
493 * @param CEC_FLAG: specifies the flag to clear.
494 * This parameter can be any combination of the following values:
495 * @arg CEC_FLAG_TXACKE: Tx Missing acknowledge Error
496 * @arg CEC_FLAG_TXERR: Tx Error
497 * @arg CEC_FLAG_TXUDR: Tx-Buffer Underrun
498 * @arg CEC_FLAG_TXEND: End of transmission (successful transmission of the last byte).
499 * @arg CEC_FLAG_TXBR: Tx-Byte Request
500 * @arg CEC_FLAG_ARBLST: Arbitration Lost
501 * @arg CEC_FLAG_RXACKE: Rx Missing Acknowledge
502 * @arg CEC_FLAG_LBPE: Rx Long period Error
503 * @arg CEC_FLAG_SBPE: Rx Short period Error
504 * @arg CEC_FLAG_BRE: Rx Bit Rising Error
505 * @arg CEC_FLAG_RXOVR: Rx Overrun
506 * @arg CEC_FLAG_RXEND: End Of Reception
507 * @arg CEC_FLAG_RXBR: Rx-Byte Received
510 void CEC_ClearFlag(uint32_t CEC_FLAG
)
512 assert_param(IS_CEC_CLEAR_FLAG(CEC_FLAG
));
514 /* Clear the selected CEC flag */
519 * @brief Checks whether the specified CEC interrupt has occurred or not.
520 * @param CEC_IT: specifies the CEC interrupt source to check.
521 * This parameter can be one of the following values:
522 * @arg CEC_IT_TXACKE: Tx Missing acknowledge Error
523 * @arg CEC_IT_TXERR: Tx Error.
524 * @arg CEC_IT_TXUDR: Tx-Buffer Underrun.
525 * @arg CEC_IT_TXEND: End of transmission (successful transmission of the last byte).
526 * @arg CEC_IT_TXBR: Tx-Byte Request.
527 * @arg CEC_IT_ARBLST: Arbitration Lost.
528 * @arg CEC_IT_RXACKE: Rx-Missing Acknowledge.
529 * @arg CEC_IT_LBPE: Rx Long period Error.
530 * @arg CEC_IT_SBPE: Rx Short period Error.
531 * @arg CEC_IT_BRE: Rx Bit Rising Error.
532 * @arg CEC_IT_RXOVR: Rx Overrun.
533 * @arg CEC_IT_RXEND: End Of Reception.
534 * @arg CEC_IT_RXBR: Rx-Byte Received
535 * @retval The new state of CEC_IT (SET or RESET).
537 ITStatus
CEC_GetITStatus(uint16_t CEC_IT
)
539 ITStatus bitstatus
= RESET
;
540 uint32_t enablestatus
= 0;
542 /* Check the parameters */
543 assert_param(IS_CEC_GET_IT(CEC_IT
));
545 /* Get the CEC IT enable bit status */
546 enablestatus
= (CEC
->IER
& CEC_IT
);
548 /* Check the status of the specified CEC interrupt */
549 if (((CEC
->ISR
& CEC_IT
) != (uint32_t)RESET
) && enablestatus
)
551 /* CEC interrupt is set */
556 /* CEC interrupt is reset */
560 /* Return the CEC interrupt status */
565 * @brief Clears the CEC's interrupt pending bits.
566 * @param CEC_IT: specifies the CEC interrupt pending bit to clear.
567 * This parameter can be any combination of the following values:
568 * @arg CEC_IT_TXACKE: Tx Missing acknowledge Error
569 * @arg CEC_IT_TXERR: Tx Error
570 * @arg CEC_IT_TXUDR: Tx-Buffer Underrun
571 * @arg CEC_IT_TXEND: End of Transmission
572 * @arg CEC_IT_TXBR: Tx-Byte Request
573 * @arg CEC_IT_ARBLST: Arbitration Lost
574 * @arg CEC_IT_RXACKE: Rx-Missing Acknowledge
575 * @arg CEC_IT_LBPE: Rx Long period Error
576 * @arg CEC_IT_SBPE: Rx Short period Error
577 * @arg CEC_IT_BRE: Rx Bit Rising Error
578 * @arg CEC_IT_RXOVR: Rx Overrun
579 * @arg CEC_IT_RXEND: End Of Reception
580 * @arg CEC_IT_RXBR: Rx-Byte Received
583 void CEC_ClearITPendingBit(uint16_t CEC_IT
)
585 assert_param(IS_CEC_IT(CEC_IT
));
587 /* Clear the selected CEC interrupt pending bits */
598 #endif /* STM32F446xx */
608 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/