2 ******************************************************************************
3 * @file stm32f7xx_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 function
12 * + IO operation function
13 * + Peripheral Control function
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...etc)
46 by calling the customed HAL_CEC_MspInit() API.
49 ******************************************************************************
52 * <h2><center>© COPYRIGHT(c) 2016 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 "stm32f7xx_hal.h"
82 /** @addtogroup STM32F7xx_HAL_Driver
87 * @brief HAL CEC module driver
90 #ifdef HAL_CEC_MODULE_ENABLED
92 /* Private typedef -----------------------------------------------------------*/
93 /* Private define ------------------------------------------------------------*/
94 /** @defgroup CEC_Private_Constants CEC Private Constants
101 /* Private macro -------------------------------------------------------------*/
102 /* Private variables ---------------------------------------------------------*/
103 /* Private function prototypes -----------------------------------------------*/
104 /** @defgroup CEC_Private_Functions CEC Private Functions
111 /* Exported functions ---------------------------------------------------------*/
113 /** @defgroup CEC_Exported_Functions CEC Exported Functions
117 /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
118 * @brief Initialization and Configuration functions
121 ===============================================================================
122 ##### Initialization and Configuration functions #####
123 ===============================================================================
125 This subsection provides a set of functions allowing to initialize the CEC
126 (+) The following parameters need to be configured:
129 (++) BRERxStop (RX stopped or not upon Bit Rising Error)
130 (++) BREErrorBitGen (Error-Bit generation in case of Bit Rising Error)
131 (++) LBPEErrorBitGen (Error-Bit generation in case of Long Bit Period Error)
132 (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
133 (++) SignalFreeTimeOption (SFT Timer start definition)
134 (++) OwnAddress (CEC device address)
142 * @brief Initializes the CEC mode according to the specified
143 * parameters in the CEC_InitTypeDef and creates the associated handle .
144 * @param hcec: CEC handle
147 HAL_StatusTypeDef
HAL_CEC_Init(CEC_HandleTypeDef
*hcec
)
149 /* Check the CEC handle allocation */
150 if((hcec
== NULL
) ||(hcec
->Init
.RxBuffer
== NULL
))
155 /* Check the parameters */
156 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
157 assert_param(IS_CEC_SIGNALFREETIME(hcec
->Init
.SignalFreeTime
));
158 assert_param(IS_CEC_TOLERANCE(hcec
->Init
.Tolerance
));
159 assert_param(IS_CEC_BRERXSTOP(hcec
->Init
.BRERxStop
));
160 assert_param(IS_CEC_BREERRORBITGEN(hcec
->Init
.BREErrorBitGen
));
161 assert_param(IS_CEC_LBPEERRORBITGEN(hcec
->Init
.LBPEErrorBitGen
));
162 assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec
->Init
.BroadcastMsgNoErrorBitGen
));
163 assert_param(IS_CEC_SFTOP(hcec
->Init
.SignalFreeTimeOption
));
164 assert_param(IS_CEC_LISTENING_MODE(hcec
->Init
.ListenMode
));
165 assert_param(IS_CEC_OWN_ADDRESS(hcec
->Init
.OwnAddress
));
167 if(hcec
->gState
== HAL_CEC_STATE_RESET
)
169 /* Allocate lock resource and initialize it */
170 hcec
->Lock
= HAL_UNLOCKED
;
171 /* Init the low level hardware : GPIO, CLOCK */
172 HAL_CEC_MspInit(hcec
);
174 hcec
->gState
= HAL_CEC_STATE_BUSY
;
176 /* Disable the Peripheral */
177 __HAL_CEC_DISABLE(hcec
);
179 /* Write to CEC Control Register */
180 hcec
->Instance
->CFGR
= hcec
->Init
.SignalFreeTime
| hcec
->Init
.Tolerance
| hcec
->Init
.BRERxStop
|\
181 hcec
->Init
.BREErrorBitGen
| hcec
->Init
.LBPEErrorBitGen
| hcec
->Init
.BroadcastMsgNoErrorBitGen
|\
182 hcec
->Init
.SignalFreeTimeOption
|((uint32_t)(hcec
->Init
.OwnAddress
)<<16U) |\
183 hcec
->Init
.ListenMode
;
185 /* Enable the following CEC Transmission/Reception interrupts as
186 * well as the following CEC Transmission/Reception Errors interrupts
187 * Rx Byte Received IT
188 * End of Reception IT
190 * Rx bit rising error
191 * Rx short bit period error
192 * Rx long bit period error
193 * Rx missing acknowledge
195 * End of Transmission IT
196 * Tx Missing Acknowledge IT
198 * Tx-Buffer Underrun IT
199 * Tx arbitration lost */
200 __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
);
202 /* Enable the CEC Peripheral */
203 __HAL_CEC_ENABLE(hcec
);
205 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
206 hcec
->gState
= HAL_CEC_STATE_READY
;
207 hcec
->RxState
= HAL_CEC_STATE_READY
;
213 * @brief DeInitializes the CEC peripheral
214 * @param hcec: CEC handle
217 HAL_StatusTypeDef
HAL_CEC_DeInit(CEC_HandleTypeDef
*hcec
)
219 /* Check the CEC handle allocation */
225 /* Check the parameters */
226 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
228 hcec
->gState
= HAL_CEC_STATE_BUSY
;
230 /* DeInit the low level hardware */
231 HAL_CEC_MspDeInit(hcec
);
233 /* Disable the Peripheral */
234 __HAL_CEC_DISABLE(hcec
);
237 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_FLAG_TXEND
|CEC_FLAG_TXBR
|CEC_FLAG_RXBR
|CEC_FLAG_RXEND
|CEC_ISR_ALL_ERROR
);
239 /* Disable the following CEC Transmission/Reception interrupts as
240 * well as the following CEC Transmission/Reception Errors interrupts
241 * Rx Byte Received IT
242 * End of Reception IT
244 * Rx bit rising error
245 * Rx short bit period error
246 * Rx long bit period error
247 * Rx missing acknowledge
249 * End of Transmission IT
250 * Tx Missing Acknowledge IT
252 * Tx-Buffer Underrun IT
253 * Tx arbitration lost */
254 __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
);
256 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
257 hcec
->gState
= HAL_CEC_STATE_RESET
;
258 hcec
->RxState
= HAL_CEC_STATE_RESET
;
267 * @brief Initializes the Own Address of the CEC device
268 * @param hcec: CEC handle
269 * @param CEC_OwnAddress: The CEC own address.
272 HAL_StatusTypeDef
HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef
*hcec
, uint16_t CEC_OwnAddress
)
274 /* Check the parameters */
275 assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress
));
277 if ((hcec
->gState
== HAL_CEC_STATE_READY
) && (hcec
->RxState
== HAL_CEC_STATE_READY
))
282 hcec
->gState
= HAL_CEC_STATE_BUSY
;
284 /* Disable the Peripheral */
285 __HAL_CEC_DISABLE(hcec
);
287 if(CEC_OwnAddress
!= CEC_OWN_ADDRESS_NONE
)
289 hcec
->Instance
->CFGR
|= ((uint32_t)CEC_OwnAddress
<<16);
293 hcec
->Instance
->CFGR
&= ~(CEC_CFGR_OAR
);
296 hcec
->gState
= HAL_CEC_STATE_READY
;
297 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
299 /* Process Unlocked */
302 /* Enable the Peripheral */
303 __HAL_CEC_ENABLE(hcec
);
314 * @brief CEC MSP Init
315 * @param hcec: CEC handle
318 __weak
void HAL_CEC_MspInit(CEC_HandleTypeDef
*hcec
)
320 /* Prevent unused argument(s) compilation warning */
322 /* NOTE : This function should not be modified, when the callback is needed,
323 the HAL_CEC_MspInit can be implemented in the user file
328 * @brief CEC MSP DeInit
329 * @param hcec: CEC handle
332 __weak
void HAL_CEC_MspDeInit(CEC_HandleTypeDef
*hcec
)
334 /* Prevent unused argument(s) compilation warning */
336 /* NOTE : This function should not be modified, when the callback is needed,
337 the HAL_CEC_MspDeInit can be implemented in the user file
345 /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
346 * @brief CEC Transmit/Receive functions
349 ===============================================================================
350 ##### IO operation functions #####
351 ===============================================================================
352 This subsection provides a set of functions allowing to manage the CEC data transfers.
354 (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
355 logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
357 (#) The communication is performed using Interrupts.
358 These API's return the HAL status.
359 The end of the data processing will be indicated through the
360 dedicated CEC IRQ when using Interrupt mode.
361 The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
362 will be executed respectively at the end of the transmit or Receive process
363 The HAL_CEC_ErrorCallback() user callback will be executed when a communication
366 (#) API's with Interrupt are :
367 (+) HAL_CEC_Transmit_IT()
368 (+) HAL_CEC_IRQHandler()
370 (#) A set of User Callbacks are provided:
371 (+) HAL_CEC_TxCpltCallback()
372 (+) HAL_CEC_RxCpltCallback()
373 (+) HAL_CEC_ErrorCallback()
380 * @brief Send data in interrupt mode
381 * @param hcec: CEC handle
382 * @param InitiatorAddress: Initiator address
383 * @param DestinationAddress: destination logical address
384 * @param pData: pointer to input byte data buffer
385 * @param Size: amount of data to be sent in bytes (without counting the header).
386 * 0 means only the header is sent (ping operation).
387 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
390 HAL_StatusTypeDef
HAL_CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
, uint8_t InitiatorAddress
,uint8_t DestinationAddress
, uint8_t *pData
, uint32_t Size
)
392 /* if the IP isn't already busy and if there is no previous transmission
393 already pending due to arbitration lost */
394 if (hcec
->gState
== HAL_CEC_STATE_READY
)
396 if((pData
== NULL
) && (Size
> 0))
401 assert_param(IS_CEC_ADDRESS(DestinationAddress
));
402 assert_param(IS_CEC_ADDRESS(InitiatorAddress
));
403 assert_param(IS_CEC_MSGSIZE(Size
));
407 hcec
->pTxBuffPtr
= pData
;
408 hcec
->gState
= HAL_CEC_STATE_BUSY_TX
;
409 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
411 /* initialize the number of bytes to send,
412 * 0 means only one header is sent (ping operation) */
413 hcec
->TxXferCount
= Size
;
415 /* in case of no payload (Size = 0), sender is only pinging the system;
416 Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
419 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
421 /* send header block */
422 hcec
->Instance
->TXDR
= ((uint8_t)(InitiatorAddress
<< CEC_INITIATOR_LSB_POS
) |(uint8_t) DestinationAddress
);
423 /* Set TX Start of Message (TXSOM) bit */
424 __HAL_CEC_FIRST_BYTE_TX_SET(hcec
);
426 /* Process Unlocked */
439 * @brief Get size of the received frame.
440 * @param hcec: CEC handle
443 uint32_t HAL_CEC_GetLastReceivedFrameSize(CEC_HandleTypeDef
*hcec
)
445 return hcec
->RxXferSize
;
449 * @brief Change Rx Buffer.
450 * @param hcec: CEC handle
451 * @param Rxbuffer: Rx Buffer
452 * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
455 void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef
*hcec
, uint8_t* Rxbuffer
)
457 hcec
->Init
.RxBuffer
= Rxbuffer
;
461 * @brief This function handles CEC interrupt requests.
462 * @param hcec: CEC handle
465 void HAL_CEC_IRQHandler(CEC_HandleTypeDef
*hcec
)
468 /* save interrupts register for further error or interrupts handling purposes */
470 reg
= hcec
->Instance
->ISR
;
473 /* ----------------------------Arbitration Lost Management----------------------------------*/
474 /* CEC TX arbitration error interrupt occurred --------------------------------------*/
475 if((reg
& CEC_FLAG_ARBLST
) != RESET
)
477 hcec
->ErrorCode
= HAL_CEC_ERROR_ARBLST
;
478 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_ARBLST
);
481 /* ----------------------------Rx Management----------------------------------*/
482 /* CEC RX byte received interrupt ---------------------------------------------------*/
483 if((reg
& CEC_FLAG_RXBR
) != RESET
)
485 /* reception is starting */
486 hcec
->RxState
= HAL_CEC_STATE_BUSY_RX
;
488 /* read received byte */
489 *hcec
->Init
.RxBuffer
++ = hcec
->Instance
->RXDR
;
490 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RXBR
);
493 /* CEC RX end received interrupt ---------------------------------------------------*/
494 if((reg
& CEC_FLAG_RXEND
) != RESET
)
497 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RXEND
);
499 /* Rx process is completed, restore hcec->RxState to Ready */
500 hcec
->RxState
= HAL_CEC_STATE_READY
;
501 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
502 hcec
->Init
.RxBuffer
-=hcec
->RxXferSize
;
503 HAL_CEC_RxCpltCallback(hcec
, hcec
->RxXferSize
);
504 hcec
->RxXferSize
= 0;
507 /* ----------------------------Tx Management----------------------------------*/
508 /* CEC TX byte request interrupt ------------------------------------------------*/
509 if((reg
& CEC_FLAG_TXBR
) != RESET
)
511 if (hcec
->TxXferCount
== 0)
513 /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
514 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
515 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
519 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
522 /* clear Tx-Byte request flag */
523 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_FLAG_TXBR
);
526 /* CEC TX end interrupt ------------------------------------------------*/
527 if((reg
& CEC_FLAG_TXEND
) != RESET
)
529 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_TXEND
);
531 /* Tx process is ended, restore hcec->gState to Ready */
532 hcec
->gState
= HAL_CEC_STATE_READY
;
533 /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
534 start again the Transmission under the Tx call back API */
536 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
537 HAL_CEC_TxCpltCallback(hcec
);
540 /* ----------------------------Rx/Tx Error Management----------------------------------*/
541 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
)) != 0)
543 hcec
->ErrorCode
= reg
;
544 __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
);
547 if((reg
& (CEC_ISR_RXOVR
|CEC_ISR_BRE
|CEC_ISR_SBPE
|CEC_ISR_LBPE
|CEC_ISR_RXACKE
)) != RESET
)
549 hcec
->Init
.RxBuffer
-=hcec
->RxXferSize
;
550 hcec
->RxXferSize
= 0;
551 hcec
->RxState
= HAL_CEC_STATE_READY
;
553 else if (((reg
& (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
)) != RESET
) && ((reg
& CEC_ISR_ARBLST
) == RESET
))
555 /* Set the CEC state ready to be able to start again the process */
556 hcec
->gState
= HAL_CEC_STATE_READY
;
559 /* Error Call Back */
560 HAL_CEC_ErrorCallback(hcec
);
566 * @brief Tx Transfer completed callback
567 * @param hcec: CEC handle
570 __weak
void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef
*hcec
)
572 /* Prevent unused argument(s) compilation warning */
574 /* NOTE : This function should not be modified, when the callback is needed,
575 the HAL_CEC_TxCpltCallback can be implemented in the user file
580 * @brief Rx Transfer completed callback
581 * @param hcec: CEC handle
582 * @param RxFrameSize: Size of frame
585 __weak
void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef
*hcec
, uint32_t RxFrameSize
)
587 /* Prevent unused argument(s) compilation warning */
590 /* NOTE : This function should not be modified, when the callback is needed,
591 the HAL_CEC_RxCpltCallback can be implemented in the user file
596 * @brief CEC error callbacks
597 * @param hcec: CEC handle
600 __weak
void HAL_CEC_ErrorCallback(CEC_HandleTypeDef
*hcec
)
602 /* Prevent unused argument(s) compilation warning */
604 /* NOTE : This function should not be modified, when the callback is needed,
605 the HAL_CEC_ErrorCallback can be implemented in the user file
612 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
613 * @brief CEC control functions
616 ===============================================================================
617 ##### Peripheral Control function #####
618 ===============================================================================
620 This subsection provides a set of functions allowing to control the CEC.
621 (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
622 (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
627 * @brief return the CEC state
628 * @param hcec: pointer to a CEC_HandleTypeDef structure that contains
629 * the configuration information for the specified CEC module.
632 HAL_CEC_StateTypeDef
HAL_CEC_GetState(CEC_HandleTypeDef
*hcec
)
634 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
635 temp1
= hcec
->gState
;
636 temp2
= hcec
->RxState
;
638 return (HAL_CEC_StateTypeDef
)(temp1
| temp2
);
642 * @brief Return the CEC error code
643 * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
644 * the configuration information for the specified CEC.
645 * @retval CEC Error Code
647 uint32_t HAL_CEC_GetError(CEC_HandleTypeDef
*hcec
)
649 return hcec
->ErrorCode
;
659 #endif /* HAL_CEC_MODULE_ENABLED */
668 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/