Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:06

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_cec.c
0004   * @author  MCD Application Team
0005   * @brief   CEC HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the High Definition Multimedia Interface
0008   *          Consumer Electronics Control Peripheral (CEC).
0009   *           + Initialization and de-initialization function
0010   *           + IO operation function
0011   *           + Peripheral Control function
0012   *
0013   *
0014   ******************************************************************************
0015   * @attention
0016   *
0017   * Copyright (c) 2017 STMicroelectronics.
0018   * All rights reserved.
0019   *
0020   * This software is licensed under terms that can be found in the LICENSE file
0021   * in the root directory of this software component.
0022   * If no LICENSE file comes with this software, it is provided AS-IS.
0023   *
0024   ******************************************************************************
0025   @verbatim
0026  ===============================================================================
0027                         ##### How to use this driver #####
0028  ===============================================================================
0029     [..]
0030     The CEC HAL driver can be used as follow:
0031 
0032     (#) Declare a CEC_HandleTypeDef handle structure.
0033     (#) Initialize the CEC low level resources by implementing the HAL_CEC_MspInit ()API:
0034         (##) Enable the CEC interface clock.
0035         (##) CEC pins configuration:
0036             (+++) Enable the clock for the CEC GPIOs.
0037             (+++) Configure these CEC pins as alternate function pull-up.
0038         (##) NVIC configuration if you need to use interrupt process (HAL_CEC_Transmit_IT()
0039              and HAL_CEC_Receive_IT() APIs):
0040             (+++) Configure the CEC interrupt priority.
0041             (+++) Enable the NVIC CEC IRQ handle.
0042             (+++) The specific CEC interrupts (Transmission complete interrupt,
0043                   RXNE interrupt and Error Interrupts) will be managed using the macros
0044                   __HAL_CEC_ENABLE_IT() and __HAL_CEC_DISABLE_IT() inside the transmit
0045                   and receive process.
0046 
0047     (#) Program the Signal Free Time (SFT) and SFT option, Tolerance, reception stop in
0048         in case of Bit Rising Error, Error-Bit generation conditions, device logical
0049         address and Listen mode in the hcec Init structure.
0050 
0051     (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
0052 
0053   [..]
0054     (@) This API (HAL_CEC_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
0055         by calling the customed HAL_CEC_MspInit() API.
0056   *** Callback registration ***
0057   =============================================
0058 
0059   The compilation define  USE_HAL_CEC_REGISTER_CALLBACKS when set to 1
0060   allows the user to configure dynamically the driver callbacks.
0061   Use Functions HAL_CEC_RegisterCallback() or HAL_CEC_RegisterXXXCallback()
0062   to register an interrupt callback.
0063 
0064   Function HAL_CEC_RegisterCallback() allows to register following callbacks:
0065     (+) TxCpltCallback     : Tx Transfer completed callback.
0066     (+) ErrorCallback      : callback for error detection.
0067     (+) MspInitCallback    : CEC MspInit.
0068     (+) MspDeInitCallback  : CEC MspDeInit.
0069   This function takes as parameters the HAL peripheral handle, the Callback ID
0070   and a pointer to the user callback function.
0071 
0072   For specific callback HAL_CEC_RxCpltCallback use dedicated register callbacks
0073   HAL_CEC_RegisterRxCpltCallback().
0074 
0075   Use function HAL_CEC_UnRegisterCallback() to reset a callback to the default
0076   weak function.
0077   HAL_CEC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
0078   and the Callback ID.
0079   This function allows to reset following callbacks:
0080     (+) TxCpltCallback     : Tx Transfer completed callback.
0081     (+) ErrorCallback      : callback for error detection.
0082     (+) MspInitCallback    : CEC MspInit.
0083     (+) MspDeInitCallback  : CEC MspDeInit.
0084 
0085   For callback HAL_CEC_RxCpltCallback use dedicated unregister callback :
0086   HAL_CEC_UnRegisterRxCpltCallback().
0087 
0088   By default, after the HAL_CEC_Init() and when the state is HAL_CEC_STATE_RESET
0089   all callbacks are set to the corresponding weak functions :
0090   examples HAL_CEC_TxCpltCallback() , HAL_CEC_RxCpltCallback().
0091   Exception done for MspInit and MspDeInit functions that are
0092   reset to the legacy weak function in the HAL_CEC_Init()/ HAL_CEC_DeInit() only when
0093   these callbacks are null (not registered beforehand).
0094   if not, MspInit or MspDeInit are not null, the HAL_CEC_Init() / HAL_CEC_DeInit()
0095   keep and use the user MspInit/MspDeInit functions (registered beforehand)
0096 
0097   Callbacks can be registered/unregistered in HAL_CEC_STATE_READY state only.
0098   Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
0099   in HAL_CEC_STATE_READY or HAL_CEC_STATE_RESET state,
0100   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0101   In that case first register the MspInit/MspDeInit user callbacks
0102   using HAL_CEC_RegisterCallback() before calling HAL_CEC_DeInit()
0103   or HAL_CEC_Init() function.
0104 
0105   When the compilation define USE_HAL_CEC_REGISTER_CALLBACKS is set to 0 or
0106   not defined, the callback registration feature is not available and all callbacks
0107   are set to the corresponding weak functions.
0108   @endverbatim
0109   ******************************************************************************
0110   */
0111 
0112 /* Includes ------------------------------------------------------------------*/
0113 #include "stm32h7xx_hal.h"
0114 
0115 /** @addtogroup STM32H7xx_HAL_Driver
0116   * @{
0117   */
0118 
0119 /** @defgroup CEC CEC
0120   * @ingroup RTEMSBSPsARMSTM32H7
0121   * @brief HAL CEC module driver
0122   * @{
0123   */
0124 #ifdef HAL_CEC_MODULE_ENABLED
0125 #if defined (CEC)
0126 
0127 /* Private typedef -----------------------------------------------------------*/
0128 /* Private define ------------------------------------------------------------*/
0129 /** @defgroup CEC_Private_Constants CEC Private Constants
0130   * @ingroup RTEMSBSPsARMSTM32H7
0131   * @{
0132   */
0133 /**
0134   * @}
0135   */
0136 
0137 /* Private macro -------------------------------------------------------------*/
0138 /* Private variables ---------------------------------------------------------*/
0139 /* Private function prototypes -----------------------------------------------*/
0140 /** @defgroup CEC_Private_Functions CEC Private Functions
0141   * @ingroup RTEMSBSPsARMSTM32H7
0142   * @{
0143   */
0144 /**
0145   * @}
0146   */
0147 
0148 /* Exported functions ---------------------------------------------------------*/
0149 
0150 /** @defgroup CEC_Exported_Functions CEC Exported Functions
0151   * @ingroup RTEMSBSPsARMSTM32H7
0152   * @{
0153   */
0154 
0155 /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
0156   * @ingroup RTEMSBSPsARMSTM32H7
0157   *  @brief    Initialization and Configuration functions
0158   *
0159 @verbatim
0160 ===============================================================================
0161             ##### Initialization and Configuration functions #####
0162  ===============================================================================
0163     [..]
0164     This subsection provides a set of functions allowing to initialize the CEC
0165       (+) The following parameters need to be configured:
0166         (++) SignalFreeTime
0167         (++) Tolerance
0168         (++) BRERxStop                 (RX stopped or not upon Bit Rising Error)
0169         (++) BREErrorBitGen            (Error-Bit generation in case of Bit Rising Error)
0170         (++) LBPEErrorBitGen           (Error-Bit generation in case of Long Bit Period Error)
0171         (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
0172         (++) SignalFreeTimeOption      (SFT Timer start definition)
0173         (++) OwnAddress                (CEC device address)
0174         (++) ListenMode
0175 
0176 @endverbatim
0177   * @{
0178   */
0179 
0180 /**
0181   * @brief Initializes the CEC mode according to the specified
0182   *         parameters in the CEC_InitTypeDef and creates the associated handle .
0183   * @param hcec CEC handle
0184   * @retval HAL status
0185   */
0186 HAL_StatusTypeDef HAL_CEC_Init(CEC_HandleTypeDef *hcec)
0187 {
0188   /* Check the CEC handle allocation */
0189   if ((hcec == NULL) || (hcec->Init.RxBuffer == NULL))
0190   {
0191     return HAL_ERROR;
0192   }
0193 
0194   /* Check the parameters */
0195   assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
0196   assert_param(IS_CEC_SIGNALFREETIME(hcec->Init.SignalFreeTime));
0197   assert_param(IS_CEC_TOLERANCE(hcec->Init.Tolerance));
0198   assert_param(IS_CEC_BRERXSTOP(hcec->Init.BRERxStop));
0199   assert_param(IS_CEC_BREERRORBITGEN(hcec->Init.BREErrorBitGen));
0200   assert_param(IS_CEC_LBPEERRORBITGEN(hcec->Init.LBPEErrorBitGen));
0201   assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec->Init.BroadcastMsgNoErrorBitGen));
0202   assert_param(IS_CEC_SFTOP(hcec->Init.SignalFreeTimeOption));
0203   assert_param(IS_CEC_LISTENING_MODE(hcec->Init.ListenMode));
0204   assert_param(IS_CEC_OWN_ADDRESS(hcec->Init.OwnAddress));
0205 
0206 #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
0207   if (hcec->gState == HAL_CEC_STATE_RESET)
0208   {
0209     /* Allocate lock resource and initialize it */
0210     hcec->Lock = HAL_UNLOCKED;
0211 
0212     hcec->TxCpltCallback  = HAL_CEC_TxCpltCallback;  /* Legacy weak TxCpltCallback  */
0213     hcec->RxCpltCallback = HAL_CEC_RxCpltCallback;   /* Legacy weak RxCpltCallback */
0214     hcec->ErrorCallback = HAL_CEC_ErrorCallback;     /* Legacy weak ErrorCallback */
0215 
0216     if (hcec->MspInitCallback == NULL)
0217     {
0218       hcec->MspInitCallback = HAL_CEC_MspInit; /* Legacy weak MspInit  */
0219     }
0220 
0221     /* Init the low level hardware */
0222     hcec->MspInitCallback(hcec);
0223   }
0224 #else
0225   if (hcec->gState == HAL_CEC_STATE_RESET)
0226   {
0227     /* Allocate lock resource and initialize it */
0228     hcec->Lock = HAL_UNLOCKED;
0229     /* Init the low level hardware : GPIO, CLOCK */
0230     HAL_CEC_MspInit(hcec);
0231   }
0232 #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
0233 
0234   hcec->gState = HAL_CEC_STATE_BUSY;
0235 
0236   /* Disable the Peripheral */
0237   __HAL_CEC_DISABLE(hcec);
0238 
0239   /* Write to CEC Control Register */
0240   hcec->Instance->CFGR = hcec->Init.SignalFreeTime | hcec->Init.Tolerance | hcec->Init.BRERxStop | \
0241                          hcec->Init.BREErrorBitGen | hcec->Init.LBPEErrorBitGen | \
0242                          hcec->Init.BroadcastMsgNoErrorBitGen | \
0243                          hcec->Init.SignalFreeTimeOption | ((uint32_t)(hcec->Init.OwnAddress) << 16U) | \
0244                          hcec->Init.ListenMode;
0245 
0246   /* Enable the following CEC Transmission/Reception interrupts as
0247     * well as the following CEC Transmission/Reception Errors interrupts
0248     * Rx Byte Received IT
0249     * End of Reception IT
0250     * Rx overrun
0251     * Rx bit rising error
0252     * Rx short bit period error
0253     * Rx long bit period error
0254     * Rx missing acknowledge
0255     * Tx Byte Request IT
0256     * End of Transmission IT
0257     * Tx Missing Acknowledge IT
0258     * Tx-Error IT
0259     * Tx-Buffer Underrun IT
0260     * Tx arbitration lost   */
0261   __HAL_CEC_ENABLE_IT(hcec, CEC_IT_RXBR | CEC_IT_RXEND | CEC_IER_RX_ALL_ERR | CEC_IT_TXBR | CEC_IT_TXEND |
0262                       CEC_IER_TX_ALL_ERR);
0263 
0264   /* Enable the CEC Peripheral */
0265   __HAL_CEC_ENABLE(hcec);
0266 
0267   hcec->ErrorCode = HAL_CEC_ERROR_NONE;
0268   hcec->gState = HAL_CEC_STATE_READY;
0269   hcec->RxState = HAL_CEC_STATE_READY;
0270 
0271   return HAL_OK;
0272 }
0273 
0274 /**
0275   * @brief DeInitializes the CEC peripheral
0276   * @param hcec CEC handle
0277   * @retval HAL status
0278   */
0279 HAL_StatusTypeDef HAL_CEC_DeInit(CEC_HandleTypeDef *hcec)
0280 {
0281   /* Check the CEC handle allocation */
0282   if (hcec == NULL)
0283   {
0284     return HAL_ERROR;
0285   }
0286 
0287   /* Check the parameters */
0288   assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
0289 
0290   hcec->gState = HAL_CEC_STATE_BUSY;
0291 
0292 #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
0293   if (hcec->MspDeInitCallback == NULL)
0294   {
0295     hcec->MspDeInitCallback = HAL_CEC_MspDeInit; /* Legacy weak MspDeInit  */
0296   }
0297 
0298   /* DeInit the low level hardware */
0299   hcec->MspDeInitCallback(hcec);
0300 
0301 #else
0302   /* DeInit the low level hardware */
0303   HAL_CEC_MspDeInit(hcec);
0304 #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
0305 
0306   /* Disable the Peripheral */
0307   __HAL_CEC_DISABLE(hcec);
0308 
0309   /* Clear Flags */
0310   __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXEND | CEC_FLAG_TXBR | CEC_FLAG_RXBR | CEC_FLAG_RXEND | CEC_ISR_ALL_ERROR);
0311 
0312   /* Disable the following CEC Transmission/Reception interrupts as
0313     * well as the following CEC Transmission/Reception Errors interrupts
0314     * Rx Byte Received IT
0315     * End of Reception IT
0316     * Rx overrun
0317     * Rx bit rising error
0318     * Rx short bit period error
0319     * Rx long bit period error
0320     * Rx missing acknowledge
0321     * Tx Byte Request IT
0322     * End of Transmission IT
0323     * Tx Missing Acknowledge IT
0324     * Tx-Error IT
0325     * Tx-Buffer Underrun IT
0326     * Tx arbitration lost   */
0327   __HAL_CEC_DISABLE_IT(hcec, CEC_IT_RXBR | CEC_IT_RXEND | CEC_IER_RX_ALL_ERR | CEC_IT_TXBR | CEC_IT_TXEND |
0328                        CEC_IER_TX_ALL_ERR);
0329 
0330   hcec->ErrorCode = HAL_CEC_ERROR_NONE;
0331   hcec->gState = HAL_CEC_STATE_RESET;
0332   hcec->RxState = HAL_CEC_STATE_RESET;
0333 
0334   /* Process Unlock */
0335   __HAL_UNLOCK(hcec);
0336 
0337   return HAL_OK;
0338 }
0339 
0340 /**
0341   * @brief Initializes the Own Address of the CEC device
0342   * @param hcec CEC handle
0343   * @param  CEC_OwnAddress The CEC own address.
0344   * @retval HAL status
0345   */
0346 HAL_StatusTypeDef HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef *hcec, uint16_t CEC_OwnAddress)
0347 {
0348   /* Check the parameters */
0349   assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress));
0350 
0351   if ((hcec->gState == HAL_CEC_STATE_READY) && (hcec->RxState == HAL_CEC_STATE_READY))
0352   {
0353     /* Process Locked */
0354     __HAL_LOCK(hcec);
0355 
0356     hcec->gState = HAL_CEC_STATE_BUSY;
0357 
0358     /* Disable the Peripheral */
0359     __HAL_CEC_DISABLE(hcec);
0360 
0361     if (CEC_OwnAddress != CEC_OWN_ADDRESS_NONE)
0362     {
0363       hcec->Instance->CFGR |= ((uint32_t)CEC_OwnAddress << 16);
0364     }
0365     else
0366     {
0367       hcec->Instance->CFGR &= ~(CEC_CFGR_OAR);
0368     }
0369 
0370     hcec->gState = HAL_CEC_STATE_READY;
0371     hcec->ErrorCode = HAL_CEC_ERROR_NONE;
0372 
0373     /* Process Unlocked */
0374     __HAL_UNLOCK(hcec);
0375 
0376     /* Enable the Peripheral */
0377     __HAL_CEC_ENABLE(hcec);
0378 
0379     return  HAL_OK;
0380   }
0381   else
0382   {
0383     return HAL_BUSY;
0384   }
0385 }
0386 
0387 /**
0388   * @brief CEC MSP Init
0389   * @param hcec CEC handle
0390   * @retval None
0391   */
0392 __weak void HAL_CEC_MspInit(CEC_HandleTypeDef *hcec)
0393 {
0394   /* Prevent unused argument(s) compilation warning */
0395   UNUSED(hcec);
0396   /* NOTE : This function should not be modified, when the callback is needed,
0397             the HAL_CEC_MspInit can be implemented in the user file
0398    */
0399 }
0400 
0401 /**
0402   * @brief CEC MSP DeInit
0403   * @param hcec CEC handle
0404   * @retval None
0405   */
0406 __weak void HAL_CEC_MspDeInit(CEC_HandleTypeDef *hcec)
0407 {
0408   /* Prevent unused argument(s) compilation warning */
0409   UNUSED(hcec);
0410   /* NOTE : This function should not be modified, when the callback is needed,
0411             the HAL_CEC_MspDeInit can be implemented in the user file
0412    */
0413 }
0414 #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
0415 /**
0416   * @brief  Register a User CEC Callback
0417   *         To be used instead of the weak predefined callback
0418   * @param  hcec CEC handle
0419   * @param  CallbackID ID of the callback to be registered
0420   *         This parameter can be one of the following values:
0421   *          @arg HAL_CEC_TX_CPLT_CB_ID Tx Complete callback ID
0422   *          @arg HAL_CEC_ERROR_CB_ID Error callback ID
0423   *          @arg HAL_CEC_MSPINIT_CB_ID MspInit callback ID
0424   *          @arg HAL_CEC_MSPDEINIT_CB_ID MspDeInit callback ID
0425   * @param  pCallback pointer to the Callback function
0426   * @retval HAL status
0427   */
0428 HAL_StatusTypeDef HAL_CEC_RegisterCallback(CEC_HandleTypeDef *hcec, HAL_CEC_CallbackIDTypeDef CallbackID,
0429                                            pCEC_CallbackTypeDef pCallback)
0430 {
0431   HAL_StatusTypeDef status = HAL_OK;
0432 
0433   if (pCallback == NULL)
0434   {
0435     /* Update the error code */
0436     hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0437     return HAL_ERROR;
0438   }
0439   /* Process locked */
0440   __HAL_LOCK(hcec);
0441 
0442   if (hcec->gState == HAL_CEC_STATE_READY)
0443   {
0444     switch (CallbackID)
0445     {
0446       case HAL_CEC_TX_CPLT_CB_ID :
0447         hcec->TxCpltCallback = pCallback;
0448         break;
0449 
0450       case HAL_CEC_ERROR_CB_ID :
0451         hcec->ErrorCallback = pCallback;
0452         break;
0453 
0454       case HAL_CEC_MSPINIT_CB_ID :
0455         hcec->MspInitCallback = pCallback;
0456         break;
0457 
0458       case HAL_CEC_MSPDEINIT_CB_ID :
0459         hcec->MspDeInitCallback = pCallback;
0460         break;
0461 
0462       default :
0463         /* Update the error code */
0464         hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0465         /* Return error status */
0466         status =  HAL_ERROR;
0467         break;
0468     }
0469   }
0470   else if (hcec->gState == HAL_CEC_STATE_RESET)
0471   {
0472     switch (CallbackID)
0473     {
0474       case HAL_CEC_MSPINIT_CB_ID :
0475         hcec->MspInitCallback = pCallback;
0476         break;
0477 
0478       case HAL_CEC_MSPDEINIT_CB_ID :
0479         hcec->MspDeInitCallback = pCallback;
0480         break;
0481 
0482       default :
0483         /* Update the error code */
0484         hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0485         /* Return error status */
0486         status =  HAL_ERROR;
0487         break;
0488     }
0489   }
0490   else
0491   {
0492     /* Update the error code */
0493     hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0494     /* Return error status */
0495     status =  HAL_ERROR;
0496   }
0497 
0498   /* Release Lock */
0499   __HAL_UNLOCK(hcec);
0500 
0501   return status;
0502 }
0503 
0504 /**
0505   * @brief  Unregister an CEC Callback
0506   *         CEC callback is redirected to the weak predefined callback
0507   * @param hcec uart handle
0508   * @param CallbackID ID of the callback to be unregistered
0509   *         This parameter can be one of the following values:
0510   *          @arg HAL_CEC_TX_CPLT_CB_ID Tx Complete callback ID
0511   *          @arg HAL_CEC_ERROR_CB_ID Error callback ID
0512   *          @arg HAL_CEC_MSPINIT_CB_ID MspInit callback ID
0513   *          @arg HAL_CEC_MSPDEINIT_CB_ID MspDeInit callback ID
0514   * @retval status
0515   */
0516 HAL_StatusTypeDef HAL_CEC_UnRegisterCallback(CEC_HandleTypeDef *hcec, HAL_CEC_CallbackIDTypeDef CallbackID)
0517 {
0518   HAL_StatusTypeDef status = HAL_OK;
0519 
0520   /* Process locked */
0521   __HAL_LOCK(hcec);
0522 
0523   if (hcec->gState == HAL_CEC_STATE_READY)
0524   {
0525     switch (CallbackID)
0526     {
0527       case HAL_CEC_TX_CPLT_CB_ID :
0528         hcec->TxCpltCallback = HAL_CEC_TxCpltCallback;  /* Legacy weak  TxCpltCallback */
0529         break;
0530 
0531       case HAL_CEC_ERROR_CB_ID :
0532         hcec->ErrorCallback = HAL_CEC_ErrorCallback;  /* Legacy weak ErrorCallback   */
0533         break;
0534 
0535       case HAL_CEC_MSPINIT_CB_ID :
0536         hcec->MspInitCallback = HAL_CEC_MspInit;
0537         break;
0538 
0539       case HAL_CEC_MSPDEINIT_CB_ID :
0540         hcec->MspDeInitCallback = HAL_CEC_MspDeInit;
0541         break;
0542 
0543       default :
0544         /* Update the error code */
0545         hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0546         /* Return error status */
0547         status =  HAL_ERROR;
0548         break;
0549     }
0550   }
0551   else if (hcec->gState == HAL_CEC_STATE_RESET)
0552   {
0553     switch (CallbackID)
0554     {
0555       case HAL_CEC_MSPINIT_CB_ID :
0556         hcec->MspInitCallback = HAL_CEC_MspInit;
0557         break;
0558 
0559       case HAL_CEC_MSPDEINIT_CB_ID :
0560         hcec->MspDeInitCallback = HAL_CEC_MspDeInit;
0561         break;
0562 
0563       default :
0564         /* Update the error code */
0565         hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0566         /* Return error status */
0567         status =  HAL_ERROR;
0568         break;
0569     }
0570   }
0571   else
0572   {
0573     /* Update the error code */
0574     hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0575     /* Return error status */
0576     status =  HAL_ERROR;
0577   }
0578 
0579   /* Release Lock */
0580   __HAL_UNLOCK(hcec);
0581 
0582   return status;
0583 }
0584 
0585 /**
0586   * @brief  Register CEC RX complete Callback
0587   *         To be used instead of the weak HAL_CEC_RxCpltCallback() predefined callback
0588   * @param  hcec CEC handle
0589   * @param  pCallback pointer to the Rx transfer compelete Callback function
0590   * @retval HAL status
0591   */
0592 HAL_StatusTypeDef HAL_CEC_RegisterRxCpltCallback(CEC_HandleTypeDef *hcec, pCEC_RxCallbackTypeDef pCallback)
0593 {
0594   HAL_StatusTypeDef status = HAL_OK;
0595 
0596   if (pCallback == NULL)
0597   {
0598     /* Update the error code */
0599     hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0600     return HAL_ERROR;
0601   }
0602   /* Process locked */
0603   __HAL_LOCK(hcec);
0604 
0605   if (HAL_CEC_STATE_READY == hcec->RxState)
0606   {
0607     hcec->RxCpltCallback = pCallback;
0608   }
0609   else
0610   {
0611     /* Update the error code */
0612     hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0613     /* Return error status */
0614     status =  HAL_ERROR;
0615   }
0616 
0617   /* Release Lock */
0618   __HAL_UNLOCK(hcec);
0619   return status;
0620 }
0621 
0622 /**
0623   * @brief  UnRegister CEC RX complete Callback
0624   *         CEC RX complete Callback is redirected to the weak HAL_CEC_RxCpltCallback() predefined callback
0625   * @param  hcec CEC handle
0626   * @retval HAL status
0627   */
0628 HAL_StatusTypeDef HAL_CEC_UnRegisterRxCpltCallback(CEC_HandleTypeDef *hcec)
0629 {
0630   HAL_StatusTypeDef status = HAL_OK;
0631 
0632   /* Process locked */
0633   __HAL_LOCK(hcec);
0634 
0635   if (HAL_CEC_STATE_READY == hcec->RxState)
0636   {
0637     hcec->RxCpltCallback = HAL_CEC_RxCpltCallback; /* Legacy weak  CEC RxCpltCallback  */
0638   }
0639   else
0640   {
0641     /* Update the error code */
0642     hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
0643     /* Return error status */
0644     status =  HAL_ERROR;
0645   }
0646 
0647   /* Release Lock */
0648   __HAL_UNLOCK(hcec);
0649   return status;
0650 }
0651 #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
0652 
0653 /**
0654   * @}
0655   */
0656 
0657 /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
0658   * @ingroup RTEMSBSPsARMSTM32H7
0659   *  @brief CEC Transmit/Receive functions
0660   *
0661 @verbatim
0662  ===============================================================================
0663                       ##### IO operation functions #####
0664  ===============================================================================
0665     This subsection provides a set of functions allowing to manage the CEC data transfers.
0666 
0667     (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
0668         logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
0669 
0670     (#) The communication is performed using Interrupts.
0671            These API's return the HAL status.
0672            The end of the data processing will be indicated through the
0673            dedicated CEC IRQ when using Interrupt mode.
0674            The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
0675            will be executed respectively at the end of the transmit or Receive process
0676            The HAL_CEC_ErrorCallback() user callback will be executed when a communication
0677            error is detected
0678 
0679     (#) API's with Interrupt are :
0680          (+) HAL_CEC_Transmit_IT()
0681          (+) HAL_CEC_IRQHandler()
0682 
0683     (#) A set of User Callbacks are provided:
0684          (+) HAL_CEC_TxCpltCallback()
0685          (+) HAL_CEC_RxCpltCallback()
0686          (+) HAL_CEC_ErrorCallback()
0687 
0688 @endverbatim
0689   * @{
0690   */
0691 
0692 /**
0693   * @brief Send data in interrupt mode
0694   * @param hcec CEC handle
0695   * @param InitiatorAddress Initiator address
0696   * @param DestinationAddress destination logical address
0697   * @param pData pointer to input byte data buffer
0698   * @param Size amount of data to be sent in bytes (without counting the header).
0699   *              0 means only the header is sent (ping operation).
0700   *              Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
0701   * @retval HAL status
0702   */
0703 HAL_StatusTypeDef HAL_CEC_Transmit_IT(CEC_HandleTypeDef *hcec, uint8_t InitiatorAddress, uint8_t DestinationAddress,
0704                                       const uint8_t *pData, uint32_t Size)
0705 {
0706   /* if the peripheral isn't already busy and if there is no previous transmission
0707      already pending due to arbitration lost */
0708   if (hcec->gState == HAL_CEC_STATE_READY)
0709   {
0710     if ((pData == NULL) && (Size > 0U))
0711     {
0712       return  HAL_ERROR;
0713     }
0714 
0715     assert_param(IS_CEC_ADDRESS(DestinationAddress));
0716     assert_param(IS_CEC_ADDRESS(InitiatorAddress));
0717     assert_param(IS_CEC_MSGSIZE(Size));
0718 
0719     /* Process Locked */
0720     __HAL_LOCK(hcec);
0721     hcec->pTxBuffPtr = pData;
0722     hcec->gState = HAL_CEC_STATE_BUSY_TX;
0723     hcec->ErrorCode = HAL_CEC_ERROR_NONE;
0724 
0725     /* initialize the number of bytes to send,
0726       * 0 means only one header is sent (ping operation) */
0727     hcec->TxXferCount = (uint16_t)Size;
0728 
0729     /* in case of no payload (Size = 0), sender is only pinging the system;
0730        Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
0731     if (Size == 0U)
0732     {
0733       __HAL_CEC_LAST_BYTE_TX_SET(hcec);
0734     }
0735 
0736     /* send header block */
0737     hcec->Instance->TXDR = (uint32_t)(((uint32_t)InitiatorAddress << CEC_INITIATOR_LSB_POS) | DestinationAddress);
0738 
0739     /* Set TX Start of Message  (TXSOM) bit */
0740     __HAL_CEC_FIRST_BYTE_TX_SET(hcec);
0741 
0742     /* Process Unlocked */
0743     __HAL_UNLOCK(hcec);
0744 
0745     return HAL_OK;
0746 
0747   }
0748   else
0749   {
0750     return HAL_BUSY;
0751   }
0752 }
0753 
0754 /**
0755   * @brief Get size of the received frame.
0756   * @param hcec CEC handle
0757   * @retval Frame size
0758   */
0759 uint32_t HAL_CEC_GetLastReceivedFrameSize(const CEC_HandleTypeDef *hcec)
0760 {
0761   return hcec->RxXferSize;
0762 }
0763 
0764 /**
0765   * @brief Change Rx Buffer.
0766   * @param hcec CEC handle
0767   * @param Rxbuffer Rx Buffer
0768   * @note  This function can be called only inside the HAL_CEC_RxCpltCallback()
0769   * @retval Frame size
0770   */
0771 void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef *hcec, uint8_t *Rxbuffer)
0772 {
0773   hcec->Init.RxBuffer = Rxbuffer;
0774 }
0775 
0776 /**
0777   * @brief This function handles CEC interrupt requests.
0778   * @param hcec CEC handle
0779   * @retval None
0780   */
0781 void HAL_CEC_IRQHandler(CEC_HandleTypeDef *hcec)
0782 {
0783 
0784   /* save interrupts register for further error or interrupts handling purposes */
0785   uint32_t itflag;
0786   itflag = hcec->Instance->ISR;
0787 
0788 
0789   /* ----------------------------Arbitration Lost Management----------------------------------*/
0790   /* CEC TX arbitration error interrupt occurred --------------------------------------*/
0791   if (HAL_IS_BIT_SET(itflag, CEC_FLAG_ARBLST))
0792   {
0793     hcec->ErrorCode = HAL_CEC_ERROR_ARBLST;
0794     __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_ARBLST);
0795   }
0796 
0797   /* ----------------------------Rx Management----------------------------------*/
0798   /* CEC RX byte received interrupt  ---------------------------------------------------*/
0799   if (HAL_IS_BIT_SET(itflag, CEC_FLAG_RXBR))
0800   {
0801     /* reception is starting */
0802     hcec->RxState = HAL_CEC_STATE_BUSY_RX;
0803     hcec->RxXferSize++;
0804     /* read received byte */
0805     *hcec->Init.RxBuffer = (uint8_t) hcec->Instance->RXDR;
0806     hcec->Init.RxBuffer++;
0807     __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXBR);
0808   }
0809 
0810   /* CEC RX end received interrupt  ---------------------------------------------------*/
0811   if (HAL_IS_BIT_SET(itflag, CEC_FLAG_RXEND))
0812   {
0813     /* clear IT */
0814     __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXEND);
0815 
0816     /* Rx process is completed, restore hcec->RxState to Ready */
0817     hcec->RxState = HAL_CEC_STATE_READY;
0818     hcec->ErrorCode = HAL_CEC_ERROR_NONE;
0819     hcec->Init.RxBuffer -= hcec->RxXferSize;
0820 #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1U)
0821     hcec->RxCpltCallback(hcec, hcec->RxXferSize);
0822 #else
0823     HAL_CEC_RxCpltCallback(hcec, hcec->RxXferSize);
0824 #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
0825     hcec->RxXferSize = 0U;
0826   }
0827 
0828   /* ----------------------------Tx Management----------------------------------*/
0829   /* CEC TX byte request interrupt ------------------------------------------------*/
0830   if (HAL_IS_BIT_SET(itflag, CEC_FLAG_TXBR))
0831   {
0832     --hcec->TxXferCount;
0833     if (hcec->TxXferCount == 0U)
0834     {
0835       /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
0836       __HAL_CEC_LAST_BYTE_TX_SET(hcec);
0837     }
0838     /* In all cases transmit the byte */
0839     hcec->Instance->TXDR = (uint8_t) * hcec->pTxBuffPtr;
0840     hcec->pTxBuffPtr++;
0841     /* clear Tx-Byte request flag */
0842     __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXBR);
0843   }
0844 
0845   /* CEC TX end interrupt ------------------------------------------------*/
0846   if (HAL_IS_BIT_SET(itflag, CEC_FLAG_TXEND))
0847   {
0848     __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXEND);
0849 
0850     /* Tx process is ended, restore hcec->gState to Ready */
0851     hcec->gState = HAL_CEC_STATE_READY;
0852     /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
0853     start again the Transmission under the Tx call back API */
0854     __HAL_UNLOCK(hcec);
0855     hcec->ErrorCode = HAL_CEC_ERROR_NONE;
0856 #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1U)
0857     hcec->TxCpltCallback(hcec);
0858 #else
0859     HAL_CEC_TxCpltCallback(hcec);
0860 #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
0861   }
0862 
0863   /* ----------------------------Rx/Tx Error Management----------------------------------*/
0864   if ((itflag & (CEC_ISR_RXOVR | CEC_ISR_BRE | CEC_ISR_SBPE | CEC_ISR_LBPE | CEC_ISR_RXACKE | CEC_ISR_TXUDR |
0865                  CEC_ISR_TXERR | CEC_ISR_TXACKE)) != 0U)
0866   {
0867     hcec->ErrorCode = itflag;
0868     __HAL_CEC_CLEAR_FLAG(hcec, HAL_CEC_ERROR_RXOVR | HAL_CEC_ERROR_BRE | CEC_FLAG_LBPE | CEC_FLAG_SBPE |
0869                          HAL_CEC_ERROR_RXACKE | HAL_CEC_ERROR_TXUDR | HAL_CEC_ERROR_TXERR | HAL_CEC_ERROR_TXACKE);
0870 
0871 
0872     if ((itflag & (CEC_ISR_RXOVR | CEC_ISR_BRE | CEC_ISR_SBPE | CEC_ISR_LBPE | CEC_ISR_RXACKE)) != 0U)
0873     {
0874       hcec->Init.RxBuffer -= hcec->RxXferSize;
0875       hcec->RxXferSize = 0U;
0876       hcec->RxState = HAL_CEC_STATE_READY;
0877     }
0878     else if (((itflag & CEC_ISR_ARBLST) == 0U) && ((itflag & (CEC_ISR_TXUDR | CEC_ISR_TXERR | CEC_ISR_TXACKE)) != 0U))
0879     {
0880       /* Set the CEC state ready to be able to start again the process */
0881       hcec->gState = HAL_CEC_STATE_READY;
0882     }
0883     else
0884     {
0885       /* Nothing todo*/
0886     }
0887 #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1U)
0888     hcec->ErrorCallback(hcec);
0889 #else
0890     /* Error  Call Back */
0891     HAL_CEC_ErrorCallback(hcec);
0892 #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
0893   }
0894   else
0895   {
0896     /* Nothing todo*/
0897   }
0898 }
0899 
0900 /**
0901   * @brief Tx Transfer completed callback
0902   * @param hcec CEC handle
0903   * @retval None
0904   */
0905 __weak void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
0906 {
0907   /* Prevent unused argument(s) compilation warning */
0908   UNUSED(hcec);
0909   /* NOTE : This function should not be modified, when the callback is needed,
0910             the HAL_CEC_TxCpltCallback can be implemented in the user file
0911    */
0912 }
0913 
0914 /**
0915   * @brief Rx Transfer completed callback
0916   * @param hcec CEC handle
0917   * @param RxFrameSize Size of frame
0918   * @retval None
0919   */
0920 __weak void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec, uint32_t RxFrameSize)
0921 {
0922   /* Prevent unused argument(s) compilation warning */
0923   UNUSED(hcec);
0924   UNUSED(RxFrameSize);
0925   /* NOTE : This function should not be modified, when the callback is needed,
0926             the HAL_CEC_RxCpltCallback can be implemented in the user file
0927    */
0928 }
0929 
0930 /**
0931   * @brief CEC error callbacks
0932   * @param hcec CEC handle
0933   * @retval None
0934   */
0935 __weak void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
0936 {
0937   /* Prevent unused argument(s) compilation warning */
0938   UNUSED(hcec);
0939   /* NOTE : This function should not be modified, when the callback is needed,
0940             the HAL_CEC_ErrorCallback can be implemented in the user file
0941    */
0942 }
0943 /**
0944   * @}
0945   */
0946 
0947 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
0948   * @ingroup RTEMSBSPsARMSTM32H7
0949   *  @brief   CEC control functions
0950   *
0951 @verbatim
0952  ===============================================================================
0953                       ##### Peripheral Control function #####
0954  ===============================================================================
0955     [..]
0956     This subsection provides a set of functions allowing to control the CEC.
0957      (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
0958      (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
0959 @endverbatim
0960   * @{
0961   */
0962 /**
0963   * @brief return the CEC state
0964   * @param hcec pointer to a CEC_HandleTypeDef structure that contains
0965   *              the configuration information for the specified CEC module.
0966   * @retval HAL state
0967   */
0968 HAL_CEC_StateTypeDef HAL_CEC_GetState(const CEC_HandleTypeDef *hcec)
0969 {
0970   uint32_t temp1;
0971   uint32_t temp2;
0972   temp1 = hcec->gState;
0973   temp2 = hcec->RxState;
0974 
0975   return (HAL_CEC_StateTypeDef)(temp1 | temp2);
0976 }
0977 
0978 /**
0979   * @brief  Return the CEC error code
0980   * @param  hcec  pointer to a CEC_HandleTypeDef structure that contains
0981   *              the configuration information for the specified CEC.
0982   * @retval CEC Error Code
0983   */
0984 uint32_t HAL_CEC_GetError(const CEC_HandleTypeDef *hcec)
0985 {
0986   return hcec->ErrorCode;
0987 }
0988 
0989 /**
0990   * @}
0991   */
0992 
0993 /**
0994   * @}
0995   */
0996 #endif /* CEC */
0997 #endif /* HAL_CEC_MODULE_ENABLED */
0998 /**
0999   * @}
1000   */
1001 
1002 /**
1003   * @}
1004   */