2 ******************************************************************************
3 * @file stm32f4xx_hal_cec.c
4 * @author MCD Application Team
7 * @brief CEC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the High Definition Multimedia Interface
10 * Consumer Electronics Control Peripheral (CEC).
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral Control functions
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
21 The CEC HAL driver can be used as follow:
23 (#) Declare a CEC_HandleTypeDef handle structure.
24 (#) Initialize the CEC low level resources by implementing the HAL_CEC_MspInit ()API:
25 (##) Enable the CEC interface clock.
26 (##) CEC pins configuration:
27 (+++) Enable the clock for the CEC GPIOs.
28 (+++) Configure these CEC pins as alternate function pull-up.
29 (##) NVIC configuration if you need to use interrupt process (HAL_CEC_Transmit_IT()
30 and HAL_CEC_Receive_IT() APIs):
31 (+++) Configure the CEC interrupt priority.
32 (+++) Enable the NVIC CEC IRQ handle.
33 (+++) The specific CEC interrupts (Transmission complete interrupt,
34 RXNE interrupt and Error Interrupts) will be managed using the macros
35 __HAL_CEC_ENABLE_IT() and __HAL_CEC_DISABLE_IT() inside the transmit
38 (#) Program the Signal Free Time (SFT) and SFT option, Tolerance, reception stop in
39 in case of Bit Rising Error, Error-Bit generation conditions, device logical
40 address and Listen mode in the hcec Init structure.
42 (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
45 (@) This API (HAL_CEC_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...)
46 by calling the customed HAL_CEC_MspInit() API.
49 ******************************************************************************
52 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
54 * Redistribution and use in source and binary forms, with or without modification,
55 * are permitted provided that the following conditions are met:
56 * 1. Redistributions of source code must retain the above copyright notice,
57 * this list of conditions and the following disclaimer.
58 * 2. Redistributions in binary form must reproduce the above copyright notice,
59 * this list of conditions and the following disclaimer in the documentation
60 * and/or other materials provided with the distribution.
61 * 3. Neither the name of STMicroelectronics nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
68 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
71 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
72 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
73 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
74 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 ******************************************************************************
79 /* Includes ------------------------------------------------------------------*/
80 #include "stm32f4xx_hal.h"
82 /** @addtogroup STM32F4xx_HAL_Driver
87 * @brief HAL CEC module driver
90 #ifdef HAL_CEC_MODULE_ENABLED
92 #if defined(STM32F446xx)
94 /* Private typedef -----------------------------------------------------------*/
95 /* Private define ------------------------------------------------------------*/
96 /** @defgroup CEC_Private_Constants CEC Private Constants
103 /* Private macro -------------------------------------------------------------*/
104 /* Private variables ---------------------------------------------------------*/
105 /* Private function prototypes -----------------------------------------------*/
106 /** @defgroup CEC_Private_Functions CEC Private Functions
113 /* Exported functions ---------------------------------------------------------*/
115 /** @defgroup CEC_Exported_Functions CEC Exported Functions
119 /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
120 * @brief Initialization and Configuration functions
123 ===============================================================================
124 ##### Initialization and Configuration functions #####
125 ===============================================================================
127 This subsection provides a set of functions allowing to initialize the CEC
128 (+) The following parameters need to be configured:
131 (++) BRERxStop (RX stopped or not upon Bit Rising Error)
132 (++) BREErrorBitGen (Error-Bit generation in case of Bit Rising Error)
133 (++) LBPEErrorBitGen (Error-Bit generation in case of Long Bit Period Error)
134 (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
135 (++) SignalFreeTimeOption (SFT Timer start definition)
136 (++) OwnAddress (CEC device address)
144 * @brief Initializes the CEC mode according to the specified
145 * parameters in the CEC_InitTypeDef and creates the associated handle .
146 * @param hcec: CEC handle
149 HAL_StatusTypeDef
HAL_CEC_Init(CEC_HandleTypeDef
*hcec
)
151 /* Check the CEC handle allocation */
152 if((hcec
== NULL
) ||(hcec
->Init
.RxBuffer
== NULL
))
157 /* Check the parameters */
158 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
159 assert_param(IS_CEC_SIGNALFREETIME(hcec
->Init
.SignalFreeTime
));
160 assert_param(IS_CEC_TOLERANCE(hcec
->Init
.Tolerance
));
161 assert_param(IS_CEC_BRERXSTOP(hcec
->Init
.BRERxStop
));
162 assert_param(IS_CEC_BREERRORBITGEN(hcec
->Init
.BREErrorBitGen
));
163 assert_param(IS_CEC_LBPEERRORBITGEN(hcec
->Init
.LBPEErrorBitGen
));
164 assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec
->Init
.BroadcastMsgNoErrorBitGen
));
165 assert_param(IS_CEC_SFTOP(hcec
->Init
.SignalFreeTimeOption
));
166 assert_param(IS_CEC_LISTENING_MODE(hcec
->Init
.ListenMode
));
167 assert_param(IS_CEC_OWN_ADDRESS(hcec
->Init
.OwnAddress
));
169 if(hcec
->gState
== HAL_CEC_STATE_RESET
)
171 /* Allocate lock resource and initialize it */
172 hcec
->Lock
= HAL_UNLOCKED
;
173 /* Init the low level hardware : GPIO, CLOCK */
174 HAL_CEC_MspInit(hcec
);
176 hcec
->gState
= HAL_CEC_STATE_BUSY
;
178 /* Disable the Peripheral */
179 __HAL_CEC_DISABLE(hcec
);
181 /* Write to CEC Control Register */
182 hcec
->Instance
->CFGR
= hcec
->Init
.SignalFreeTime
| hcec
->Init
.Tolerance
| hcec
->Init
.BRERxStop
|\
183 hcec
->Init
.BREErrorBitGen
| hcec
->Init
.LBPEErrorBitGen
| hcec
->Init
.BroadcastMsgNoErrorBitGen
|\
184 hcec
->Init
.SignalFreeTimeOption
|((uint32_t)(hcec
->Init
.OwnAddress
)<<16U) |\
185 hcec
->Init
.ListenMode
;
187 /* Enable the following CEC Transmission/Reception interrupts as
188 * well as the following CEC Transmission/Reception Errors interrupts
189 * Rx Byte Received IT
190 * End of Reception IT
192 * Rx bit rising error
193 * Rx short bit period error
194 * Rx long bit period error
195 * Rx missing acknowledge
197 * End of Transmission IT
198 * Tx Missing Acknowledge IT
200 * Tx-Buffer Underrun IT
201 * Tx arbitration lost */
202 __HAL_CEC_ENABLE_IT(hcec
, CEC_IT_RXBR
|CEC_IT_RXEND
|CEC_IER_RX_ALL_ERR
|CEC_IT_TXBR
|CEC_IT_TXEND
|CEC_IER_TX_ALL_ERR
);
204 /* Enable the CEC Peripheral */
205 __HAL_CEC_ENABLE(hcec
);
207 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
208 hcec
->gState
= HAL_CEC_STATE_READY
;
209 hcec
->RxState
= HAL_CEC_STATE_READY
;
215 * @brief DeInitializes the CEC peripheral
216 * @param hcec: CEC handle
219 HAL_StatusTypeDef
HAL_CEC_DeInit(CEC_HandleTypeDef
*hcec
)
221 /* Check the CEC handle allocation */
227 /* Check the parameters */
228 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
230 hcec
->gState
= HAL_CEC_STATE_BUSY
;
232 /* DeInit the low level hardware */
233 HAL_CEC_MspDeInit(hcec
);
234 /* Disable the Peripheral */
235 __HAL_CEC_DISABLE(hcec
);
238 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_FLAG_TXEND
|CEC_FLAG_TXBR
|CEC_FLAG_RXBR
|CEC_FLAG_RXEND
|CEC_ISR_ALL_ERROR
);
240 /* Disable the following CEC Transmission/Reception interrupts as
241 * well as the following CEC Transmission/Reception Errors interrupts
242 * Rx Byte Received IT
243 * End of Reception IT
245 * Rx bit rising error
246 * Rx short bit period error
247 * Rx long bit period error
248 * Rx missing acknowledge
250 * End of Transmission IT
251 * Tx Missing Acknowledge IT
253 * Tx-Buffer Underrun IT
254 * Tx arbitration lost */
255 __HAL_CEC_DISABLE_IT(hcec
, CEC_IT_RXBR
|CEC_IT_RXEND
|CEC_IER_RX_ALL_ERR
|CEC_IT_TXBR
|CEC_IT_TXEND
|CEC_IER_TX_ALL_ERR
);
257 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
258 hcec
->gState
= HAL_CEC_STATE_RESET
;
259 hcec
->RxState
= HAL_CEC_STATE_RESET
;
268 * @brief Initializes the Own Address of the CEC device
269 * @param hcec: CEC handle
270 * @param CEC_OwnAddress: The CEC own address.
273 HAL_StatusTypeDef
HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef
*hcec
, uint16_t CEC_OwnAddress
)
275 /* Check the parameters */
276 assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress
));
278 if ((hcec
->gState
== HAL_CEC_STATE_READY
) && (hcec
->RxState
== HAL_CEC_STATE_READY
))
283 hcec
->gState
= HAL_CEC_STATE_BUSY
;
285 /* Disable the Peripheral */
286 __HAL_CEC_DISABLE(hcec
);
288 if(CEC_OwnAddress
!= CEC_OWN_ADDRESS_NONE
)
290 hcec
->Instance
->CFGR
|= ((uint32_t)CEC_OwnAddress
<<16U);
294 hcec
->Instance
->CFGR
&= ~(CEC_CFGR_OAR
);
297 hcec
->gState
= HAL_CEC_STATE_READY
;
298 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
300 /* Process Unlocked */
303 /* Enable the Peripheral */
304 __HAL_CEC_ENABLE(hcec
);
315 * @brief CEC MSP Init
316 * @param hcec: CEC handle
319 __weak
void HAL_CEC_MspInit(CEC_HandleTypeDef
*hcec
)
321 /* Prevent unused argument(s) compilation warning */
323 /* NOTE : This function should not be modified, when the callback is needed,
324 the HAL_CEC_MspInit can be implemented in the user file
329 * @brief CEC MSP DeInit
330 * @param hcec: CEC handle
333 __weak
void HAL_CEC_MspDeInit(CEC_HandleTypeDef
*hcec
)
335 /* Prevent unused argument(s) compilation warning */
337 /* NOTE : This function should not be modified, when the callback is needed,
338 the HAL_CEC_MspDeInit can be implemented in the user file
346 /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
347 * @brief CEC Transmit/Receive functions
350 ===============================================================================
351 ##### IO operation functions #####
352 ===============================================================================
353 This subsection provides a set of functions allowing to manage the CEC data transfers.
355 (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
356 logical addresses (4-bit long addresses, 0x0F for broadcast messages destination)
358 (#) The communication is performed using Interrupts.
359 These API's return the HAL status.
360 The end of the data processing will be indicated through the
361 dedicated CEC IRQ when using Interrupt mode.
362 The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
363 will be executed respectivelly at the end of the transmit or Receive process
364 The HAL_CEC_ErrorCallback()user callback will be executed when a communication
367 (#) API's with Interrupt are :
368 (+) HAL_CEC_Transmit_IT()
369 (+) HAL_CEC_IRQHandler()
371 (#) A set of User Callbacks are provided:
372 (+) HAL_CEC_TxCpltCallback()
373 (+) HAL_CEC_RxCpltCallback()
374 (+) HAL_CEC_ErrorCallback()
381 * @brief Send data in interrupt mode
382 * @param hcec: CEC handle
383 * @param InitiatorAddress: Initiator logical address
384 * @param DestinationAddress: destination logical address
385 * @param pData: pointer to input byte data buffer
386 * @param Size: amount of data to be sent in bytes (without counting the header).
387 * 0 means only the header is sent (ping operation).
388 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
391 HAL_StatusTypeDef
HAL_CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
, uint8_t InitiatorAddress
, uint8_t DestinationAddress
, uint8_t *pData
, uint32_t Size
)
393 /* if the IP isn't already busy and if there is no previous transmission
394 already pending due to arbitration lost */
395 if (hcec
->gState
== HAL_CEC_STATE_READY
)
397 if((pData
== NULL
) && (Size
> 0U))
402 assert_param(IS_CEC_ADDRESS(DestinationAddress
));
403 assert_param(IS_CEC_ADDRESS(InitiatorAddress
));
404 assert_param(IS_CEC_MSGSIZE(Size
));
408 hcec
->pTxBuffPtr
= pData
;
409 hcec
->gState
= HAL_CEC_STATE_BUSY_TX
;
410 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
412 /* initialize the number of bytes to send,
413 * 0 means only one header is sent (ping operation) */
414 hcec
->TxXferCount
= Size
;
416 /* in case of no payload (Size = 0), sender is only pinging the system;
417 Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
420 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
423 /* send header block */
424 hcec
->Instance
->TXDR
= ((uint8_t)(InitiatorAddress
<< CEC_INITIATOR_LSB_POS
) |(uint8_t) DestinationAddress
);
425 /* Set TX Start of Message (TXSOM) bit */
426 __HAL_CEC_FIRST_BYTE_TX_SET(hcec
);
428 /* Process Unlocked */
441 * @brief Get size of the received frame.
442 * @param hcec: CEC handle
445 uint32_t HAL_CEC_GetLastReceivedFrameSize(CEC_HandleTypeDef
*hcec
)
447 return hcec
->RxXferSize
;
451 * @brief Change Rx Buffer.
452 * @param hcec: CEC handle
453 * @param Rxbuffer: Rx Buffer
454 * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
457 void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef
*hcec
, uint8_t* Rxbuffer
)
459 hcec
->Init
.RxBuffer
= Rxbuffer
;
463 * @brief This function handles CEC interrupt requests.
464 * @param hcec: CEC handle
467 void HAL_CEC_IRQHandler(CEC_HandleTypeDef
*hcec
)
470 /* save interrupts register for further error or interrupts handling purposes */
472 reg
= hcec
->Instance
->ISR
;
475 /* ----------------------------Arbitration Lost Management----------------------------------*/
476 /* CEC TX arbitration error interrupt occurred --------------------------------------*/
477 if((reg
& CEC_FLAG_ARBLST
) != RESET
)
479 hcec
->ErrorCode
= HAL_CEC_ERROR_ARBLST
;
480 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_ARBLST
);
483 /* ----------------------------Rx Management----------------------------------*/
484 /* CEC RX byte received interrupt ---------------------------------------------------*/
485 if((reg
& CEC_FLAG_RXBR
) != RESET
)
487 /* reception is starting */
488 hcec
->RxState
= HAL_CEC_STATE_BUSY_RX
;
490 /* read received byte */
491 *hcec
->Init
.RxBuffer
++ = hcec
->Instance
->RXDR
;
492 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RXBR
);
495 /* CEC RX end received interrupt ---------------------------------------------------*/
496 if((reg
& CEC_FLAG_RXEND
) != RESET
)
499 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RXEND
);
501 /* Rx process is completed, restore hcec->RxState to Ready */
502 hcec
->RxState
= HAL_CEC_STATE_READY
;
503 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
504 hcec
->Init
.RxBuffer
-= hcec
->RxXferSize
;
505 HAL_CEC_RxCpltCallback(hcec
, hcec
->RxXferSize
);
506 hcec
->RxXferSize
= 0U;
509 /* ----------------------------Tx Management----------------------------------*/
510 /* CEC TX byte request interrupt ------------------------------------------------*/
511 if((reg
& CEC_FLAG_TXBR
) != RESET
)
513 if (hcec
->TxXferCount
== 0U)
515 /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
516 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
517 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
521 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
524 /* clear Tx-Byte request flag */
525 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_FLAG_TXBR
);
528 /* CEC TX end interrupt ------------------------------------------------*/
529 if((reg
& CEC_FLAG_TXEND
) != RESET
)
531 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_TXEND
);
533 /* Tx process is ended, restore hcec->gState to Ready */
534 hcec
->gState
= HAL_CEC_STATE_READY
;
535 /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
536 start again the Transmission under the Tx call back API */
538 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
539 HAL_CEC_TxCpltCallback(hcec
);
542 /* ----------------------------Rx/Tx Error Management----------------------------------*/
543 if ((reg
& (CEC_ISR_RXOVR
|CEC_ISR_BRE
|CEC_ISR_SBPE
|CEC_ISR_LBPE
|CEC_ISR_RXACKE
|CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
)) != 0U)
545 hcec
->ErrorCode
= reg
;
546 __HAL_CEC_CLEAR_FLAG(hcec
, HAL_CEC_ERROR_RXOVR
|HAL_CEC_ERROR_BRE
|CEC_FLAG_LBPE
|CEC_FLAG_SBPE
|HAL_CEC_ERROR_RXACKE
|HAL_CEC_ERROR_TXUDR
|HAL_CEC_ERROR_TXERR
|HAL_CEC_ERROR_TXACKE
);
549 if((reg
& (CEC_ISR_RXOVR
|CEC_ISR_BRE
|CEC_ISR_SBPE
|CEC_ISR_LBPE
|CEC_ISR_RXACKE
)) != RESET
)
551 hcec
->Init
.RxBuffer
-=hcec
->RxXferSize
;
552 hcec
->RxXferSize
= 0U;
553 hcec
->RxState
= HAL_CEC_STATE_READY
;
555 else if (((reg
& (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
)) != RESET
) && ((reg
& CEC_ISR_ARBLST
) == RESET
))
557 /* Set the CEC state ready to be able to start again the process */
558 hcec
->gState
= HAL_CEC_STATE_READY
;
561 /* Error Call Back */
562 HAL_CEC_ErrorCallback(hcec
);
568 * @brief Tx Transfer completed callback
569 * @param hcec: CEC handle
572 __weak
void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef
*hcec
)
574 /* Prevent unused argument(s) compilation warning */
576 /* NOTE : This function should not be modified, when the callback is needed,
577 the HAL_CEC_TxCpltCallback can be implemented in the user file
582 * @brief Rx Transfer completed callback
583 * @param hcec: CEC handle
584 * @param RxFrameSize: Size of frame
587 __weak
void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef
*hcec
, uint32_t RxFrameSize
)
589 /* Prevent unused argument(s) compilation warning */
592 /* NOTE : This function should not be modified, when the callback is needed,
593 the HAL_CEC_RxCpltCallback can be implemented in the user file
598 * @brief CEC error callbacks
599 * @param hcec: CEC handle
602 __weak
void HAL_CEC_ErrorCallback(CEC_HandleTypeDef
*hcec
)
604 /* Prevent unused argument(s) compilation warning */
606 /* NOTE : This function should not be modified, when the callback is needed,
607 the HAL_CEC_ErrorCallback can be implemented in the user file
614 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
615 * @brief CEC control functions
618 ===============================================================================
619 ##### Peripheral Control function #####
620 ===============================================================================
622 This subsection provides a set of functions allowing to control the CEC.
623 (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
624 (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
629 * @brief return the CEC state
630 * @param hcec: pointer to a CEC_HandleTypeDef structure that contains
631 * the configuration information for the specified CEC module.
634 HAL_CEC_StateTypeDef
HAL_CEC_GetState(CEC_HandleTypeDef
*hcec
)
636 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
637 temp1
= hcec
->gState
;
638 temp2
= hcec
->RxState
;
640 return (HAL_CEC_StateTypeDef
)(temp1
| temp2
);
644 * @brief Return the CEC error code
645 * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
646 * the configuration information for the specified CEC.
647 * @retval CEC Error Code
649 uint32_t HAL_CEC_GetError(CEC_HandleTypeDef
*hcec
)
651 return hcec
->ErrorCode
;
662 #endif /* STM32F446xx */
664 #endif /* HAL_CEC_MODULE_ENABLED */
673 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/