2 ******************************************************************************
3 * @file stm32f3xx_hal_cec.c
4 * @author MCD Application Team
5 * @brief CEC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the High Definition Multimedia Interface
8 * Consumer Electronics Control Peripheral (CEC).
9 * + Initialization and de-initialization functions
10 * + IO operation functions
11 * + Peripheral Control functions
15 ===============================================================================
16 ##### How to use this driver #####
17 ===============================================================================
19 The CEC HAL driver can be used as follow:
21 (#) Declare a CEC_HandleTypeDef handle structure.
22 (#) Initialize the CEC low level resources by implementing the HAL_CEC_MspInit ()API:
23 (##) Enable the CEC interface clock.
24 (##) CEC pins configuration:
25 (+) Enable the clock for the CEC GPIOs.
26 (+) Configure these CEC pins as alternate function pull-up.
27 (##) NVIC configuration if you need to use interrupt process (HAL_CEC_Transmit_IT()
28 and HAL_CEC_Receive_IT() APIs):
29 (+) Configure the CEC interrupt priority.
30 (+) Enable the NVIC CEC IRQ handle.
31 (@) The specific CEC interrupts (Transmission complete interrupt,
32 RXNE interrupt and Error Interrupts) will be managed using the macros
33 __HAL_CEC_ENABLE_IT() and __HAL_CEC_DISABLE_IT() inside the transmit
36 (#) Program the Signal Free Time (SFT) and SFT option, Tolerance, reception stop in
37 in case of Bit Rising Error, Error-Bit generation conditions, device logical
38 address and Listen mode in the hcec Init structure.
40 (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
42 (@) This API (HAL_CEC_Init()) configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
43 by calling the customed HAL_CEC_MspInit() API.
46 ******************************************************************************
49 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
51 * Redistribution and use in source and binary forms, with or without modification,
52 * are permitted provided that the following conditions are met:
53 * 1. Redistributions of source code must retain the above copyright notice,
54 * this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright notice,
56 * this list of conditions and the following disclaimer in the documentation
57 * and/or other materials provided with the distribution.
58 * 3. Neither the name of STMicroelectronics nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
63 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
65 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
68 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
69 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
70 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
71 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 ******************************************************************************
76 /* Includes ------------------------------------------------------------------*/
77 #include "stm32f3xx_hal.h"
79 /** @addtogroup STM32F3xx_HAL_Driver
83 #ifdef HAL_CEC_MODULE_ENABLED
85 #if defined(STM32F373xC) || defined(STM32F378xx)
87 * @brief HAL CEC module driver
91 /* Private typedef -----------------------------------------------------------*/
92 /* Private define ------------------------------------------------------------*/
93 /** @defgroup CEC_Private_Constants CEC Private Constants
100 /* Private macro -------------------------------------------------------------*/
101 /* Private variables ---------------------------------------------------------*/
102 /* Private function prototypes -----------------------------------------------*/
103 /** @defgroup CEC_Private_Functions CEC Private Functions
110 /* Exported functions ---------------------------------------------------------*/
112 /** @defgroup CEC_Exported_Functions CEC Exported Functions
116 /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
117 * @brief Initialization and Configuration functions
120 ===============================================================================
121 ##### Initialization and Configuration functions #####
122 ===============================================================================
124 This subsection provides a set of functions allowing to initialize the CEC
125 (+) The following parameters need to be configured:
128 (++) BRERxStop (RX stopped or not upon Bit Rising Error)
129 (++) BREErrorBitGen (Error-Bit generation in case of Bit Rising Error)
130 (++) LBPEErrorBitGen (Error-Bit generation in case of Long Bit Period Error)
131 (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
132 (++) SignalFreeTimeOption (SFT Timer start definition)
133 (++) OwnAddress (CEC device address)
141 * @brief Initializes the CEC mode according to the specified
142 * parameters in the CEC_InitTypeDef and creates the associated handle .
143 * @param hcec CEC handle
146 HAL_StatusTypeDef
HAL_CEC_Init(CEC_HandleTypeDef
*hcec
)
148 /* Check the CEC handle allocation */
149 if((hcec
== NULL
) ||(hcec
->Init
.RxBuffer
== NULL
))
154 /* Check the parameters */
155 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
156 assert_param(IS_CEC_SIGNALFREETIME(hcec
->Init
.SignalFreeTime
));
157 assert_param(IS_CEC_TOLERANCE(hcec
->Init
.Tolerance
));
158 assert_param(IS_CEC_BRERXSTOP(hcec
->Init
.BRERxStop
));
159 assert_param(IS_CEC_BREERRORBITGEN(hcec
->Init
.BREErrorBitGen
));
160 assert_param(IS_CEC_LBPEERRORBITGEN(hcec
->Init
.LBPEErrorBitGen
));
161 assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec
->Init
.BroadcastMsgNoErrorBitGen
));
162 assert_param(IS_CEC_SFTOP(hcec
->Init
.SignalFreeTimeOption
));
163 assert_param(IS_CEC_LISTENING_MODE(hcec
->Init
.ListenMode
));
164 assert_param(IS_CEC_OWN_ADDRESS(hcec
->Init
.OwnAddress
));
166 if(hcec
->gState
== HAL_CEC_STATE_RESET
)
168 /* Allocate lock resource and initialize it */
169 hcec
->Lock
= HAL_UNLOCKED
;
170 /* Init the low level hardware : GPIO, CLOCK */
171 HAL_CEC_MspInit(hcec
);
173 hcec
->gState
= HAL_CEC_STATE_BUSY
;
175 /* Disable the Peripheral */
176 __HAL_CEC_DISABLE(hcec
);
178 /* Write to CEC Control Register */
179 hcec
->Instance
->CFGR
= hcec
->Init
.SignalFreeTime
| hcec
->Init
.Tolerance
| hcec
->Init
.BRERxStop
|\
180 hcec
->Init
.BREErrorBitGen
| hcec
->Init
.LBPEErrorBitGen
| hcec
->Init
.BroadcastMsgNoErrorBitGen
|\
181 hcec
->Init
.SignalFreeTimeOption
|((uint32_t)(hcec
->Init
.OwnAddress
)<<16U) |\
182 hcec
->Init
.ListenMode
;
184 /* Enable the following CEC Transmission/Reception interrupts as
185 * well as the following CEC Transmission/Reception Errors interrupts
186 * Rx Byte Received IT
187 * End of Reception IT
189 * Rx bit rising error
190 * Rx short bit period error
191 * Rx long bit period error
192 * Rx missing acknowledge
194 * End of Transmission IT
195 * Tx Missing Acknowledge IT
197 * Tx-Buffer Underrun IT
198 * Tx arbitration lost */
199 __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
);
201 /* Enable the CEC Peripheral */
202 __HAL_CEC_ENABLE(hcec
);
204 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
205 hcec
->gState
= HAL_CEC_STATE_READY
;
206 hcec
->RxState
= HAL_CEC_STATE_READY
;
212 * @brief DeInitializes the CEC peripheral
213 * @param hcec CEC handle
216 HAL_StatusTypeDef
HAL_CEC_DeInit(CEC_HandleTypeDef
*hcec
)
218 /* Check the CEC handle allocation */
224 /* Check the parameters */
225 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
227 hcec
->gState
= HAL_CEC_STATE_BUSY
;
229 /* DeInit the low level hardware */
230 HAL_CEC_MspDeInit(hcec
);
231 /* Disable the Peripheral */
232 __HAL_CEC_DISABLE(hcec
);
235 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_FLAG_TXEND
|CEC_FLAG_TXBR
|CEC_FLAG_RXBR
|CEC_FLAG_RXEND
|CEC_ISR_ALL_ERROR
);
237 /* Disable the following CEC Transmission/Reception interrupts as
238 * well as the following CEC Transmission/Reception Errors interrupts
239 * Rx Byte Received IT
240 * End of Reception IT
242 * Rx bit rising error
243 * Rx short bit period error
244 * Rx long bit period error
245 * Rx missing acknowledge
247 * End of Transmission IT
248 * Tx Missing Acknowledge IT
250 * Tx-Buffer Underrun IT
251 * Tx arbitration lost */
252 __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
);
254 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
255 hcec
->gState
= HAL_CEC_STATE_RESET
;
256 hcec
->RxState
= HAL_CEC_STATE_RESET
;
265 * @brief Initializes the Own Address of the CEC device
266 * @param hcec CEC handle
267 * @param CEC_OwnAddress The CEC own address.
270 HAL_StatusTypeDef
HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef
*hcec
, uint16_t CEC_OwnAddress
)
272 /* Check the parameters */
273 assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress
));
275 if ((hcec
->gState
== HAL_CEC_STATE_READY
) && (hcec
->RxState
== HAL_CEC_STATE_READY
))
280 hcec
->gState
= HAL_CEC_STATE_BUSY
;
282 /* Disable the Peripheral */
283 __HAL_CEC_DISABLE(hcec
);
285 if(CEC_OwnAddress
!= CEC_OWN_ADDRESS_NONE
)
287 hcec
->Instance
->CFGR
|= ((uint32_t)CEC_OwnAddress
<<16U);
291 hcec
->Instance
->CFGR
&= ~(CEC_CFGR_OAR
);
294 hcec
->gState
= HAL_CEC_STATE_READY
;
295 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
297 /* Process Unlocked */
300 /* Enable the Peripheral */
301 __HAL_CEC_ENABLE(hcec
);
312 * @brief CEC MSP Init
313 * @param hcec CEC handle
316 __weak
void HAL_CEC_MspInit(CEC_HandleTypeDef
*hcec
)
318 /* Prevent unused argument(s) compilation warning */
320 /* NOTE : This function should not be modified, when the callback is needed,
321 the HAL_CEC_MspInit can be implemented in the user file
326 * @brief CEC MSP DeInit
327 * @param hcec CEC handle
330 __weak
void HAL_CEC_MspDeInit(CEC_HandleTypeDef
*hcec
)
332 /* Prevent unused argument(s) compilation warning */
334 /* NOTE : This function should not be modified, when the callback is needed,
335 the HAL_CEC_MspDeInit can be implemented in the user file
343 /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
344 * @brief CEC Transmit/Receive functions
347 ===============================================================================
348 ##### IO operation functions #####
349 ===============================================================================
350 This subsection provides a set of functions allowing to manage the CEC data transfers.
352 (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
353 logical addresses (4-bit long addresses, 0x0F for broadcast messages destination)
355 (#) The communication is performed using Interrupts.
356 These API's return the HAL status.
357 The end of the data processing will be indicated through the
358 dedicated CEC IRQ when using Interrupt mode.
359 The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
360 will be executed respectivelly at the end of the transmit or Receive process
361 The HAL_CEC_ErrorCallback()user callback will be executed when a communication
364 (#) API's with Interrupt are :
365 (+) HAL_CEC_Transmit_IT()
366 (+) HAL_CEC_IRQHandler()
368 (#) A set of User Callbacks are provided:
369 (+) HAL_CEC_TxCpltCallback()
370 (+) HAL_CEC_RxCpltCallback()
371 (+) HAL_CEC_ErrorCallback()
378 * @brief Send data in interrupt mode
379 * @param hcec CEC handle
380 * @param InitiatorAddress Initiator address
381 * @param DestinationAddress destination logical address
382 * @param pData pointer to input byte data buffer
383 * @param Size amount of data to be sent in bytes (without counting the header).
384 * 0 means only the header is sent (ping operation).
385 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
388 HAL_StatusTypeDef
HAL_CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
, uint8_t InitiatorAddress
, uint8_t DestinationAddress
, uint8_t *pData
, uint32_t Size
)
390 /* if the IP isn't already busy and if there is no previous transmission
391 already pending due to arbitration lost */
392 if (hcec
->gState
== HAL_CEC_STATE_READY
)
394 if((pData
== NULL
) && (Size
> 0U))
399 assert_param(IS_CEC_ADDRESS(DestinationAddress
));
400 assert_param(IS_CEC_ADDRESS(InitiatorAddress
));
401 assert_param(IS_CEC_MSGSIZE(Size
));
405 hcec
->pTxBuffPtr
= pData
;
406 hcec
->gState
= HAL_CEC_STATE_BUSY_TX
;
407 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
409 /* initialize the number of bytes to send,
410 * 0 means only one header is sent (ping operation) */
411 hcec
->TxXferCount
= Size
;
413 /* in case of no payload (Size = 0U), sender is only pinging the system;
414 Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
417 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
420 /* send header block */
421 hcec
->Instance
->TXDR
= ((uint8_t)(InitiatorAddress
<< CEC_INITIATOR_LSB_POS
) |(uint8_t) DestinationAddress
);
422 /* Set TX Start of Message (TXSOM) bit */
423 __HAL_CEC_FIRST_BYTE_TX_SET(hcec
);
425 /* Process Unlocked */
438 * @brief Get size of the received frame.
439 * @param hcec CEC handle
442 uint32_t HAL_CEC_GetLastReceivedFrameSize(CEC_HandleTypeDef
*hcec
)
444 return hcec
->RxXferSize
;
448 * @brief Change Rx Buffer.
449 * @param hcec CEC handle
450 * @param Rxbuffer Rx Buffer
451 * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
454 void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef
*hcec
, uint8_t* Rxbuffer
)
456 hcec
->Init
.RxBuffer
= Rxbuffer
;
460 * @brief This function handles CEC interrupt requests.
461 * @param hcec CEC handle
464 void HAL_CEC_IRQHandler(CEC_HandleTypeDef
*hcec
)
467 /* save interrupts register for further error or interrupts handling purposes */
469 reg
= hcec
->Instance
->ISR
;
472 /* ----------------------------Arbitration Lost Management----------------------------------*/
473 /* CEC TX arbitration error interrupt occurred --------------------------------------*/
474 if((reg
& CEC_FLAG_ARBLST
) != RESET
)
476 hcec
->ErrorCode
= HAL_CEC_ERROR_ARBLST
;
477 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_ARBLST
);
480 /* ----------------------------Rx Management----------------------------------*/
481 /* CEC RX byte received interrupt ---------------------------------------------------*/
482 if((reg
& CEC_FLAG_RXBR
) != RESET
)
484 /* reception is starting */
485 hcec
->RxState
= HAL_CEC_STATE_BUSY_RX
;
487 /* read received byte */
488 *hcec
->Init
.RxBuffer
++ = hcec
->Instance
->RXDR
;
489 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RXBR
);
492 /* CEC RX end received interrupt ---------------------------------------------------*/
493 if((reg
& CEC_FLAG_RXEND
) != RESET
)
496 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RXEND
);
498 /* Rx process is completed, restore hcec->RxState to Ready */
499 hcec
->RxState
= HAL_CEC_STATE_READY
;
500 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
501 hcec
->Init
.RxBuffer
-= hcec
->RxXferSize
;
502 HAL_CEC_RxCpltCallback(hcec
, hcec
->RxXferSize
);
503 hcec
->RxXferSize
= 0U;
506 /* ----------------------------Tx Management----------------------------------*/
507 /* CEC TX byte request interrupt ------------------------------------------------*/
508 if((reg
& CEC_FLAG_TXBR
) != RESET
)
510 if (hcec
->TxXferCount
== 0U)
512 /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
513 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
514 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
518 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
521 /* clear Tx-Byte request flag */
522 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_FLAG_TXBR
);
525 /* CEC TX end interrupt ------------------------------------------------*/
526 if((reg
& CEC_FLAG_TXEND
) != RESET
)
528 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_TXEND
);
530 /* Tx process is ended, restore hcec->gState to Ready */
531 hcec
->gState
= HAL_CEC_STATE_READY
;
532 /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
533 start again the Transmission under the Tx call back API */
535 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
536 HAL_CEC_TxCpltCallback(hcec
);
539 /* ----------------------------Rx/Tx Error Management----------------------------------*/
540 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)
542 hcec
->ErrorCode
= reg
;
543 __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
);
546 if((reg
& (CEC_ISR_RXOVR
|CEC_ISR_BRE
|CEC_ISR_SBPE
|CEC_ISR_LBPE
|CEC_ISR_RXACKE
)) != RESET
)
548 hcec
->Init
.RxBuffer
-=hcec
->RxXferSize
;
549 hcec
->RxXferSize
= 0U;
550 hcec
->RxState
= HAL_CEC_STATE_READY
;
552 else if (((reg
& (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
)) != RESET
) && ((reg
& CEC_ISR_ARBLST
) == RESET
))
554 /* Set the CEC state ready to be able to start again the process */
555 hcec
->gState
= HAL_CEC_STATE_READY
;
558 /* Error Call Back */
559 HAL_CEC_ErrorCallback(hcec
);
565 * @brief Tx Transfer completed callback
566 * @param hcec CEC handle
569 __weak
void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef
*hcec
)
571 /* Prevent unused argument(s) compilation warning */
573 /* NOTE : This function should not be modified, when the callback is needed,
574 the HAL_CEC_TxCpltCallback can be implemented in the user file
579 * @brief Rx Transfer completed callback
580 * @param hcec CEC handle
581 * @param RxFrameSize Size of frame
584 __weak
void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef
*hcec
, uint32_t RxFrameSize
)
586 /* Prevent unused argument(s) compilation warning */
589 /* NOTE : This function should not be modified, when the callback is needed,
590 the HAL_CEC_RxCpltCallback can be implemented in the user file
595 * @brief CEC error callbacks
596 * @param hcec CEC handle
599 __weak
void HAL_CEC_ErrorCallback(CEC_HandleTypeDef
*hcec
)
601 /* Prevent unused argument(s) compilation warning */
603 /* NOTE : This function should not be modified, when the callback is needed,
604 the HAL_CEC_ErrorCallback can be implemented in the user file
611 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
612 * @brief CEC control functions
615 ===============================================================================
616 ##### Peripheral Control function #####
617 ===============================================================================
619 This subsection provides a set of functions allowing to control the CEC.
620 (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
621 (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
626 * @brief return the CEC state
627 * @param hcec pointer to a CEC_HandleTypeDef structure that contains
628 * the configuration information for the specified CEC module.
631 HAL_CEC_StateTypeDef
HAL_CEC_GetState(CEC_HandleTypeDef
*hcec
)
633 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
634 temp1
= hcec
->gState
;
635 temp2
= hcec
->RxState
;
637 return (HAL_CEC_StateTypeDef
)(temp1
| temp2
);
641 * @brief Return the CEC error code
642 * @param hcec pointer to a CEC_HandleTypeDef structure that contains
643 * the configuration information for the specified CEC.
644 * @retval CEC Error Code
646 uint32_t HAL_CEC_GetError(CEC_HandleTypeDef
*hcec
)
648 return hcec
->ErrorCode
;
660 #endif /* defined(STM32F373xC) || defined(STM32F378xx) */
662 #endif /* HAL_CEC_MODULE_ENABLED */
671 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/