stm32g0xx_hal_cec.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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 function
  10. * + IO operation function
  11. * + Peripheral Control function
  12. *
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2018 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ===============================================================================
  27. ##### How to use this driver #####
  28. ===============================================================================
  29. [..]
  30. The CEC HAL driver can be used as follow:
  31. (#) Declare a CEC_HandleTypeDef handle structure.
  32. (#) Initialize the CEC low level resources by implementing the HAL_CEC_MspInit ()API:
  33. (##) Enable the CEC interface clock.
  34. (##) CEC pins configuration:
  35. (+++) Enable the clock for the CEC GPIOs.
  36. (+++) Configure these CEC pins as alternate function pull-up.
  37. (##) NVIC configuration if you need to use interrupt process (HAL_CEC_Transmit_IT()
  38. and HAL_CEC_Receive_IT() APIs):
  39. (+++) Configure the CEC interrupt priority.
  40. (+++) Enable the NVIC CEC IRQ handle.
  41. (+++) The specific CEC interrupts (Transmission complete interrupt,
  42. RXNE interrupt and Error Interrupts) will be managed using the macros
  43. __HAL_CEC_ENABLE_IT() and __HAL_CEC_DISABLE_IT() inside the transmit
  44. and receive process.
  45. (#) Program the Signal Free Time (SFT) and SFT option, Tolerance, reception stop in
  46. in case of Bit Rising Error, Error-Bit generation conditions, device logical
  47. address and Listen mode in the hcec Init structure.
  48. (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
  49. [..]
  50. (@) This API (HAL_CEC_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
  51. by calling the customed HAL_CEC_MspInit() API.
  52. *** Callback registration ***
  53. =============================================
  54. The compilation define USE_HAL_CEC_REGISTER_CALLBACKS when set to 1
  55. allows the user to configure dynamically the driver callbacks.
  56. Use Functions HAL_CEC_RegisterCallback() or HAL_CEC_RegisterXXXCallback()
  57. to register an interrupt callback.
  58. Function HAL_CEC_RegisterCallback() allows to register following callbacks:
  59. (+) TxCpltCallback : Tx Transfer completed callback.
  60. (+) ErrorCallback : callback for error detection.
  61. (+) MspInitCallback : CEC MspInit.
  62. (+) MspDeInitCallback : CEC MspDeInit.
  63. This function takes as parameters the HAL peripheral handle, the Callback ID
  64. and a pointer to the user callback function.
  65. For specific callback HAL_CEC_RxCpltCallback use dedicated register callbacks
  66. HAL_CEC_RegisterRxCpltCallback().
  67. Use function HAL_CEC_UnRegisterCallback() to reset a callback to the default
  68. weak function.
  69. HAL_CEC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  70. and the Callback ID.
  71. This function allows to reset following callbacks:
  72. (+) TxCpltCallback : Tx Transfer completed callback.
  73. (+) ErrorCallback : callback for error detection.
  74. (+) MspInitCallback : CEC MspInit.
  75. (+) MspDeInitCallback : CEC MspDeInit.
  76. For callback HAL_CEC_RxCpltCallback use dedicated unregister callback :
  77. HAL_CEC_UnRegisterRxCpltCallback().
  78. By default, after the HAL_CEC_Init() and when the state is HAL_CEC_STATE_RESET
  79. all callbacks are set to the corresponding weak functions :
  80. examples HAL_CEC_TxCpltCallback() , HAL_CEC_RxCpltCallback().
  81. Exception done for MspInit and MspDeInit functions that are
  82. reset to the legacy weak function in the HAL_CEC_Init()/ HAL_CEC_DeInit() only when
  83. these callbacks are null (not registered beforehand).
  84. if not, MspInit or MspDeInit are not null, the HAL_CEC_Init() / HAL_CEC_DeInit()
  85. keep and use the user MspInit/MspDeInit functions (registered beforehand)
  86. Callbacks can be registered/unregistered in HAL_CEC_STATE_READY state only.
  87. Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
  88. in HAL_CEC_STATE_READY or HAL_CEC_STATE_RESET state,
  89. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  90. In that case first register the MspInit/MspDeInit user callbacks
  91. using HAL_CEC_RegisterCallback() before calling HAL_CEC_DeInit()
  92. or HAL_CEC_Init() function.
  93. When the compilation define USE_HAL_CEC_REGISTER_CALLBACKS is set to 0 or
  94. not defined, the callback registration feature is not available and all callbacks
  95. are set to the corresponding weak functions.
  96. @endverbatim
  97. ******************************************************************************
  98. */
  99. /* Includes ------------------------------------------------------------------*/
  100. #include "stm32g0xx_hal.h"
  101. /** @addtogroup STM32G0xx_HAL_Driver
  102. * @{
  103. */
  104. /** @defgroup CEC CEC
  105. * @brief HAL CEC module driver
  106. * @{
  107. */
  108. #ifdef HAL_CEC_MODULE_ENABLED
  109. #if defined (CEC)
  110. /* Private typedef -----------------------------------------------------------*/
  111. /* Private define ------------------------------------------------------------*/
  112. /** @defgroup CEC_Private_Constants CEC Private Constants
  113. * @{
  114. */
  115. /**
  116. * @}
  117. */
  118. /* Private macro -------------------------------------------------------------*/
  119. /* Private variables ---------------------------------------------------------*/
  120. /* Private function prototypes -----------------------------------------------*/
  121. /** @defgroup CEC_Private_Functions CEC Private Functions
  122. * @{
  123. */
  124. /**
  125. * @}
  126. */
  127. /* Exported functions ---------------------------------------------------------*/
  128. /** @defgroup CEC_Exported_Functions CEC Exported Functions
  129. * @{
  130. */
  131. /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
  132. * @brief Initialization and Configuration functions
  133. *
  134. @verbatim
  135. ===============================================================================
  136. ##### Initialization and Configuration functions #####
  137. ===============================================================================
  138. [..]
  139. This subsection provides a set of functions allowing to initialize the CEC
  140. (+) The following parameters need to be configured:
  141. (++) SignalFreeTime
  142. (++) Tolerance
  143. (++) BRERxStop (RX stopped or not upon Bit Rising Error)
  144. (++) BREErrorBitGen (Error-Bit generation in case of Bit Rising Error)
  145. (++) LBPEErrorBitGen (Error-Bit generation in case of Long Bit Period Error)
  146. (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
  147. (++) SignalFreeTimeOption (SFT Timer start definition)
  148. (++) OwnAddress (CEC device address)
  149. (++) ListenMode
  150. @endverbatim
  151. * @{
  152. */
  153. /**
  154. * @brief Initializes the CEC mode according to the specified
  155. * parameters in the CEC_InitTypeDef and creates the associated handle .
  156. * @param hcec CEC handle
  157. * @retval HAL status
  158. */
  159. HAL_StatusTypeDef HAL_CEC_Init(CEC_HandleTypeDef *hcec)
  160. {
  161. /* Check the CEC handle allocation */
  162. if ((hcec == NULL) || (hcec->Init.RxBuffer == NULL))
  163. {
  164. return HAL_ERROR;
  165. }
  166. /* Check the parameters */
  167. assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
  168. assert_param(IS_CEC_SIGNALFREETIME(hcec->Init.SignalFreeTime));
  169. assert_param(IS_CEC_TOLERANCE(hcec->Init.Tolerance));
  170. assert_param(IS_CEC_BRERXSTOP(hcec->Init.BRERxStop));
  171. assert_param(IS_CEC_BREERRORBITGEN(hcec->Init.BREErrorBitGen));
  172. assert_param(IS_CEC_LBPEERRORBITGEN(hcec->Init.LBPEErrorBitGen));
  173. assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec->Init.BroadcastMsgNoErrorBitGen));
  174. assert_param(IS_CEC_SFTOP(hcec->Init.SignalFreeTimeOption));
  175. assert_param(IS_CEC_LISTENING_MODE(hcec->Init.ListenMode));
  176. assert_param(IS_CEC_OWN_ADDRESS(hcec->Init.OwnAddress));
  177. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
  178. if (hcec->gState == HAL_CEC_STATE_RESET)
  179. {
  180. /* Allocate lock resource and initialize it */
  181. hcec->Lock = HAL_UNLOCKED;
  182. hcec->TxCpltCallback = HAL_CEC_TxCpltCallback; /* Legacy weak TxCpltCallback */
  183. hcec->RxCpltCallback = HAL_CEC_RxCpltCallback; /* Legacy weak RxCpltCallback */
  184. hcec->ErrorCallback = HAL_CEC_ErrorCallback; /* Legacy weak ErrorCallback */
  185. if (hcec->MspInitCallback == NULL)
  186. {
  187. hcec->MspInitCallback = HAL_CEC_MspInit; /* Legacy weak MspInit */
  188. }
  189. /* Init the low level hardware */
  190. hcec->MspInitCallback(hcec);
  191. }
  192. #else
  193. if (hcec->gState == HAL_CEC_STATE_RESET)
  194. {
  195. /* Allocate lock resource and initialize it */
  196. hcec->Lock = HAL_UNLOCKED;
  197. /* Init the low level hardware : GPIO, CLOCK */
  198. HAL_CEC_MspInit(hcec);
  199. }
  200. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  201. hcec->gState = HAL_CEC_STATE_BUSY;
  202. /* Disable the Peripheral */
  203. __HAL_CEC_DISABLE(hcec);
  204. /* Write to CEC Control Register */
  205. hcec->Instance->CFGR = hcec->Init.SignalFreeTime | hcec->Init.Tolerance | hcec->Init.BRERxStop | \
  206. hcec->Init.BREErrorBitGen | hcec->Init.LBPEErrorBitGen | \
  207. hcec->Init.BroadcastMsgNoErrorBitGen | \
  208. hcec->Init.SignalFreeTimeOption | ((uint32_t)(hcec->Init.OwnAddress) << 16U) | \
  209. hcec->Init.ListenMode;
  210. /* Enable the following CEC Transmission/Reception interrupts as
  211. * well as the following CEC Transmission/Reception Errors interrupts
  212. * Rx Byte Received IT
  213. * End of Reception IT
  214. * Rx overrun
  215. * Rx bit rising error
  216. * Rx short bit period error
  217. * Rx long bit period error
  218. * Rx missing acknowledge
  219. * Tx Byte Request IT
  220. * End of Transmission IT
  221. * Tx Missing Acknowledge IT
  222. * Tx-Error IT
  223. * Tx-Buffer Underrun IT
  224. * Tx arbitration lost */
  225. __HAL_CEC_ENABLE_IT(hcec, CEC_IT_RXBR | CEC_IT_RXEND | CEC_IER_RX_ALL_ERR | CEC_IT_TXBR | CEC_IT_TXEND |
  226. CEC_IER_TX_ALL_ERR);
  227. /* Enable the CEC Peripheral */
  228. __HAL_CEC_ENABLE(hcec);
  229. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  230. hcec->gState = HAL_CEC_STATE_READY;
  231. hcec->RxState = HAL_CEC_STATE_READY;
  232. return HAL_OK;
  233. }
  234. /**
  235. * @brief DeInitializes the CEC peripheral
  236. * @param hcec CEC handle
  237. * @retval HAL status
  238. */
  239. HAL_StatusTypeDef HAL_CEC_DeInit(CEC_HandleTypeDef *hcec)
  240. {
  241. /* Check the CEC handle allocation */
  242. if (hcec == NULL)
  243. {
  244. return HAL_ERROR;
  245. }
  246. /* Check the parameters */
  247. assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
  248. hcec->gState = HAL_CEC_STATE_BUSY;
  249. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
  250. if (hcec->MspDeInitCallback == NULL)
  251. {
  252. hcec->MspDeInitCallback = HAL_CEC_MspDeInit; /* Legacy weak MspDeInit */
  253. }
  254. /* DeInit the low level hardware */
  255. hcec->MspDeInitCallback(hcec);
  256. #else
  257. /* DeInit the low level hardware */
  258. HAL_CEC_MspDeInit(hcec);
  259. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  260. /* Disable the Peripheral */
  261. __HAL_CEC_DISABLE(hcec);
  262. /* Clear Flags */
  263. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXEND | CEC_FLAG_TXBR | CEC_FLAG_RXBR | CEC_FLAG_RXEND | CEC_ISR_ALL_ERROR);
  264. /* Disable the following CEC Transmission/Reception interrupts as
  265. * well as the following CEC Transmission/Reception Errors interrupts
  266. * Rx Byte Received IT
  267. * End of Reception IT
  268. * Rx overrun
  269. * Rx bit rising error
  270. * Rx short bit period error
  271. * Rx long bit period error
  272. * Rx missing acknowledge
  273. * Tx Byte Request IT
  274. * End of Transmission IT
  275. * Tx Missing Acknowledge IT
  276. * Tx-Error IT
  277. * Tx-Buffer Underrun IT
  278. * Tx arbitration lost */
  279. __HAL_CEC_DISABLE_IT(hcec, CEC_IT_RXBR | CEC_IT_RXEND | CEC_IER_RX_ALL_ERR | CEC_IT_TXBR | CEC_IT_TXEND |
  280. CEC_IER_TX_ALL_ERR);
  281. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  282. hcec->gState = HAL_CEC_STATE_RESET;
  283. hcec->RxState = HAL_CEC_STATE_RESET;
  284. /* Process Unlock */
  285. __HAL_UNLOCK(hcec);
  286. return HAL_OK;
  287. }
  288. /**
  289. * @brief Initializes the Own Address of the CEC device
  290. * @param hcec CEC handle
  291. * @param CEC_OwnAddress The CEC own address.
  292. * @retval HAL status
  293. */
  294. HAL_StatusTypeDef HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef *hcec, uint16_t CEC_OwnAddress)
  295. {
  296. /* Check the parameters */
  297. assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress));
  298. if ((hcec->gState == HAL_CEC_STATE_READY) && (hcec->RxState == HAL_CEC_STATE_READY))
  299. {
  300. /* Process Locked */
  301. __HAL_LOCK(hcec);
  302. hcec->gState = HAL_CEC_STATE_BUSY;
  303. /* Disable the Peripheral */
  304. __HAL_CEC_DISABLE(hcec);
  305. if (CEC_OwnAddress != CEC_OWN_ADDRESS_NONE)
  306. {
  307. hcec->Instance->CFGR |= ((uint32_t)CEC_OwnAddress << 16);
  308. }
  309. else
  310. {
  311. hcec->Instance->CFGR &= ~(CEC_CFGR_OAR);
  312. }
  313. hcec->gState = HAL_CEC_STATE_READY;
  314. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  315. /* Process Unlocked */
  316. __HAL_UNLOCK(hcec);
  317. /* Enable the Peripheral */
  318. __HAL_CEC_ENABLE(hcec);
  319. return HAL_OK;
  320. }
  321. else
  322. {
  323. return HAL_BUSY;
  324. }
  325. }
  326. /**
  327. * @brief CEC MSP Init
  328. * @param hcec CEC handle
  329. * @retval None
  330. */
  331. __weak void HAL_CEC_MspInit(CEC_HandleTypeDef *hcec)
  332. {
  333. /* Prevent unused argument(s) compilation warning */
  334. UNUSED(hcec);
  335. /* NOTE : This function should not be modified, when the callback is needed,
  336. the HAL_CEC_MspInit can be implemented in the user file
  337. */
  338. }
  339. /**
  340. * @brief CEC MSP DeInit
  341. * @param hcec CEC handle
  342. * @retval None
  343. */
  344. __weak void HAL_CEC_MspDeInit(CEC_HandleTypeDef *hcec)
  345. {
  346. /* Prevent unused argument(s) compilation warning */
  347. UNUSED(hcec);
  348. /* NOTE : This function should not be modified, when the callback is needed,
  349. the HAL_CEC_MspDeInit can be implemented in the user file
  350. */
  351. }
  352. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
  353. /**
  354. * @brief Register a User CEC Callback
  355. * To be used instead of the weak predefined callback
  356. * @param hcec CEC handle
  357. * @param CallbackID ID of the callback to be registered
  358. * This parameter can be one of the following values:
  359. * @arg HAL_CEC_TX_CPLT_CB_ID Tx Complete callback ID
  360. * @arg HAL_CEC_ERROR_CB_ID Error callback ID
  361. * @arg HAL_CEC_MSPINIT_CB_ID MspInit callback ID
  362. * @arg HAL_CEC_MSPDEINIT_CB_ID MspDeInit callback ID
  363. * @param pCallback pointer to the Callback function
  364. * @retval HAL status
  365. */
  366. HAL_StatusTypeDef HAL_CEC_RegisterCallback(CEC_HandleTypeDef *hcec, HAL_CEC_CallbackIDTypeDef CallbackID,
  367. pCEC_CallbackTypeDef pCallback)
  368. {
  369. HAL_StatusTypeDef status = HAL_OK;
  370. if (pCallback == NULL)
  371. {
  372. /* Update the error code */
  373. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  374. return HAL_ERROR;
  375. }
  376. /* Process locked */
  377. __HAL_LOCK(hcec);
  378. if (hcec->gState == HAL_CEC_STATE_READY)
  379. {
  380. switch (CallbackID)
  381. {
  382. case HAL_CEC_TX_CPLT_CB_ID :
  383. hcec->TxCpltCallback = pCallback;
  384. break;
  385. case HAL_CEC_ERROR_CB_ID :
  386. hcec->ErrorCallback = pCallback;
  387. break;
  388. case HAL_CEC_MSPINIT_CB_ID :
  389. hcec->MspInitCallback = pCallback;
  390. break;
  391. case HAL_CEC_MSPDEINIT_CB_ID :
  392. hcec->MspDeInitCallback = pCallback;
  393. break;
  394. default :
  395. /* Update the error code */
  396. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  397. /* Return error status */
  398. status = HAL_ERROR;
  399. break;
  400. }
  401. }
  402. else if (hcec->gState == HAL_CEC_STATE_RESET)
  403. {
  404. switch (CallbackID)
  405. {
  406. case HAL_CEC_MSPINIT_CB_ID :
  407. hcec->MspInitCallback = pCallback;
  408. break;
  409. case HAL_CEC_MSPDEINIT_CB_ID :
  410. hcec->MspDeInitCallback = pCallback;
  411. break;
  412. default :
  413. /* Update the error code */
  414. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  415. /* Return error status */
  416. status = HAL_ERROR;
  417. break;
  418. }
  419. }
  420. else
  421. {
  422. /* Update the error code */
  423. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  424. /* Return error status */
  425. status = HAL_ERROR;
  426. }
  427. /* Release Lock */
  428. __HAL_UNLOCK(hcec);
  429. return status;
  430. }
  431. /**
  432. * @brief Unregister an CEC Callback
  433. * CEC callback is redirected to the weak predefined callback
  434. * @param hcec uart handle
  435. * @param CallbackID ID of the callback to be unregistered
  436. * This parameter can be one of the following values:
  437. * @arg HAL_CEC_TX_CPLT_CB_ID Tx Complete callback ID
  438. * @arg HAL_CEC_ERROR_CB_ID Error callback ID
  439. * @arg HAL_CEC_MSPINIT_CB_ID MspInit callback ID
  440. * @arg HAL_CEC_MSPDEINIT_CB_ID MspDeInit callback ID
  441. * @retval status
  442. */
  443. HAL_StatusTypeDef HAL_CEC_UnRegisterCallback(CEC_HandleTypeDef *hcec, HAL_CEC_CallbackIDTypeDef CallbackID)
  444. {
  445. HAL_StatusTypeDef status = HAL_OK;
  446. /* Process locked */
  447. __HAL_LOCK(hcec);
  448. if (hcec->gState == HAL_CEC_STATE_READY)
  449. {
  450. switch (CallbackID)
  451. {
  452. case HAL_CEC_TX_CPLT_CB_ID :
  453. hcec->TxCpltCallback = HAL_CEC_TxCpltCallback; /* Legacy weak TxCpltCallback */
  454. break;
  455. case HAL_CEC_ERROR_CB_ID :
  456. hcec->ErrorCallback = HAL_CEC_ErrorCallback; /* Legacy weak ErrorCallback */
  457. break;
  458. case HAL_CEC_MSPINIT_CB_ID :
  459. hcec->MspInitCallback = HAL_CEC_MspInit;
  460. break;
  461. case HAL_CEC_MSPDEINIT_CB_ID :
  462. hcec->MspDeInitCallback = HAL_CEC_MspDeInit;
  463. break;
  464. default :
  465. /* Update the error code */
  466. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  467. /* Return error status */
  468. status = HAL_ERROR;
  469. break;
  470. }
  471. }
  472. else if (hcec->gState == HAL_CEC_STATE_RESET)
  473. {
  474. switch (CallbackID)
  475. {
  476. case HAL_CEC_MSPINIT_CB_ID :
  477. hcec->MspInitCallback = HAL_CEC_MspInit;
  478. break;
  479. case HAL_CEC_MSPDEINIT_CB_ID :
  480. hcec->MspDeInitCallback = HAL_CEC_MspDeInit;
  481. break;
  482. default :
  483. /* Update the error code */
  484. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  485. /* Return error status */
  486. status = HAL_ERROR;
  487. break;
  488. }
  489. }
  490. else
  491. {
  492. /* Update the error code */
  493. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  494. /* Return error status */
  495. status = HAL_ERROR;
  496. }
  497. /* Release Lock */
  498. __HAL_UNLOCK(hcec);
  499. return status;
  500. }
  501. /**
  502. * @brief Register CEC RX complete Callback
  503. * To be used instead of the weak HAL_CEC_RxCpltCallback() predefined callback
  504. * @param hcec CEC handle
  505. * @param pCallback pointer to the Rx transfer compelete Callback function
  506. * @retval HAL status
  507. */
  508. HAL_StatusTypeDef HAL_CEC_RegisterRxCpltCallback(CEC_HandleTypeDef *hcec, pCEC_RxCallbackTypeDef pCallback)
  509. {
  510. HAL_StatusTypeDef status = HAL_OK;
  511. if (pCallback == NULL)
  512. {
  513. /* Update the error code */
  514. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  515. return HAL_ERROR;
  516. }
  517. /* Process locked */
  518. __HAL_LOCK(hcec);
  519. if (HAL_CEC_STATE_READY == hcec->RxState)
  520. {
  521. hcec->RxCpltCallback = pCallback;
  522. }
  523. else
  524. {
  525. /* Update the error code */
  526. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  527. /* Return error status */
  528. status = HAL_ERROR;
  529. }
  530. /* Release Lock */
  531. __HAL_UNLOCK(hcec);
  532. return status;
  533. }
  534. /**
  535. * @brief UnRegister CEC RX complete Callback
  536. * CEC RX complete Callback is redirected to the weak HAL_CEC_RxCpltCallback() predefined callback
  537. * @param hcec CEC handle
  538. * @retval HAL status
  539. */
  540. HAL_StatusTypeDef HAL_CEC_UnRegisterRxCpltCallback(CEC_HandleTypeDef *hcec)
  541. {
  542. HAL_StatusTypeDef status = HAL_OK;
  543. /* Process locked */
  544. __HAL_LOCK(hcec);
  545. if (HAL_CEC_STATE_READY == hcec->RxState)
  546. {
  547. hcec->RxCpltCallback = HAL_CEC_RxCpltCallback; /* Legacy weak CEC RxCpltCallback */
  548. }
  549. else
  550. {
  551. /* Update the error code */
  552. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  553. /* Return error status */
  554. status = HAL_ERROR;
  555. }
  556. /* Release Lock */
  557. __HAL_UNLOCK(hcec);
  558. return status;
  559. }
  560. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  561. /**
  562. * @}
  563. */
  564. /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
  565. * @brief CEC Transmit/Receive functions
  566. *
  567. @verbatim
  568. ===============================================================================
  569. ##### IO operation functions #####
  570. ===============================================================================
  571. This subsection provides a set of functions allowing to manage the CEC data transfers.
  572. (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
  573. logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
  574. (#) The communication is performed using Interrupts.
  575. These API's return the HAL status.
  576. The end of the data processing will be indicated through the
  577. dedicated CEC IRQ when using Interrupt mode.
  578. The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
  579. will be executed respectively at the end of the transmit or Receive process
  580. The HAL_CEC_ErrorCallback() user callback will be executed when a communication
  581. error is detected
  582. (#) API's with Interrupt are :
  583. (+) HAL_CEC_Transmit_IT()
  584. (+) HAL_CEC_IRQHandler()
  585. (#) A set of User Callbacks are provided:
  586. (+) HAL_CEC_TxCpltCallback()
  587. (+) HAL_CEC_RxCpltCallback()
  588. (+) HAL_CEC_ErrorCallback()
  589. @endverbatim
  590. * @{
  591. */
  592. /**
  593. * @brief Send data in interrupt mode
  594. * @param hcec CEC handle
  595. * @param InitiatorAddress Initiator address
  596. * @param DestinationAddress destination logical address
  597. * @param pData pointer to input byte data buffer
  598. * @param Size amount of data to be sent in bytes (without counting the header).
  599. * 0 means only the header is sent (ping operation).
  600. * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
  601. * @retval HAL status
  602. */
  603. HAL_StatusTypeDef HAL_CEC_Transmit_IT(CEC_HandleTypeDef *hcec, uint8_t InitiatorAddress, uint8_t DestinationAddress,
  604. const uint8_t *pData, uint32_t Size)
  605. {
  606. /* if the peripheral isn't already busy and if there is no previous transmission
  607. already pending due to arbitration lost */
  608. if (hcec->gState == HAL_CEC_STATE_READY)
  609. {
  610. if ((pData == NULL) && (Size > 0U))
  611. {
  612. return HAL_ERROR;
  613. }
  614. assert_param(IS_CEC_ADDRESS(DestinationAddress));
  615. assert_param(IS_CEC_ADDRESS(InitiatorAddress));
  616. assert_param(IS_CEC_MSGSIZE(Size));
  617. /* Process Locked */
  618. __HAL_LOCK(hcec);
  619. hcec->pTxBuffPtr = pData;
  620. hcec->gState = HAL_CEC_STATE_BUSY_TX;
  621. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  622. /* initialize the number of bytes to send,
  623. * 0 means only one header is sent (ping operation) */
  624. hcec->TxXferCount = (uint16_t)Size;
  625. /* in case of no payload (Size = 0), sender is only pinging the system;
  626. Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
  627. if (Size == 0U)
  628. {
  629. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  630. }
  631. /* send header block */
  632. hcec->Instance->TXDR = (uint32_t)(((uint32_t)InitiatorAddress << CEC_INITIATOR_LSB_POS) | DestinationAddress);
  633. /* Set TX Start of Message (TXSOM) bit */
  634. __HAL_CEC_FIRST_BYTE_TX_SET(hcec);
  635. /* Process Unlocked */
  636. __HAL_UNLOCK(hcec);
  637. return HAL_OK;
  638. }
  639. else
  640. {
  641. return HAL_BUSY;
  642. }
  643. }
  644. /**
  645. * @brief Get size of the received frame.
  646. * @param hcec CEC handle
  647. * @retval Frame size
  648. */
  649. uint32_t HAL_CEC_GetLastReceivedFrameSize(const CEC_HandleTypeDef *hcec)
  650. {
  651. return hcec->RxXferSize;
  652. }
  653. /**
  654. * @brief Change Rx Buffer.
  655. * @param hcec CEC handle
  656. * @param Rxbuffer Rx Buffer
  657. * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
  658. * @retval Frame size
  659. */
  660. void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef *hcec, uint8_t *Rxbuffer)
  661. {
  662. hcec->Init.RxBuffer = Rxbuffer;
  663. }
  664. /**
  665. * @brief This function handles CEC interrupt requests.
  666. * @param hcec CEC handle
  667. * @retval None
  668. */
  669. void HAL_CEC_IRQHandler(CEC_HandleTypeDef *hcec)
  670. {
  671. /* save interrupts register for further error or interrupts handling purposes */
  672. uint32_t itflag;
  673. itflag = hcec->Instance->ISR;
  674. /* ----------------------------Arbitration Lost Management----------------------------------*/
  675. /* CEC TX arbitration error interrupt occurred --------------------------------------*/
  676. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_ARBLST))
  677. {
  678. hcec->ErrorCode = HAL_CEC_ERROR_ARBLST;
  679. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_ARBLST);
  680. }
  681. /* ----------------------------Rx Management----------------------------------*/
  682. /* CEC RX byte received interrupt ---------------------------------------------------*/
  683. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_RXBR))
  684. {
  685. /* reception is starting */
  686. hcec->RxState = HAL_CEC_STATE_BUSY_RX;
  687. hcec->RxXferSize++;
  688. /* read received byte */
  689. *hcec->Init.RxBuffer = (uint8_t) hcec->Instance->RXDR;
  690. hcec->Init.RxBuffer++;
  691. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXBR);
  692. }
  693. /* CEC RX end received interrupt ---------------------------------------------------*/
  694. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_RXEND))
  695. {
  696. /* clear IT */
  697. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXEND);
  698. /* Rx process is completed, restore hcec->RxState to Ready */
  699. hcec->RxState = HAL_CEC_STATE_READY;
  700. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  701. hcec->Init.RxBuffer -= hcec->RxXferSize;
  702. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1U)
  703. hcec->RxCpltCallback(hcec, hcec->RxXferSize);
  704. #else
  705. HAL_CEC_RxCpltCallback(hcec, hcec->RxXferSize);
  706. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  707. hcec->RxXferSize = 0U;
  708. }
  709. /* ----------------------------Tx Management----------------------------------*/
  710. /* CEC TX byte request interrupt ------------------------------------------------*/
  711. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_TXBR))
  712. {
  713. --hcec->TxXferCount;
  714. if (hcec->TxXferCount == 0U)
  715. {
  716. /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
  717. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  718. }
  719. /* In all cases transmit the byte */
  720. hcec->Instance->TXDR = (uint8_t) * hcec->pTxBuffPtr;
  721. hcec->pTxBuffPtr++;
  722. /* clear Tx-Byte request flag */
  723. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXBR);
  724. }
  725. /* CEC TX end interrupt ------------------------------------------------*/
  726. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_TXEND))
  727. {
  728. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXEND);
  729. /* Tx process is ended, restore hcec->gState to Ready */
  730. hcec->gState = HAL_CEC_STATE_READY;
  731. /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
  732. start again the Transmission under the Tx call back API */
  733. __HAL_UNLOCK(hcec);
  734. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  735. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1U)
  736. hcec->TxCpltCallback(hcec);
  737. #else
  738. HAL_CEC_TxCpltCallback(hcec);
  739. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  740. }
  741. /* ----------------------------Rx/Tx Error Management----------------------------------*/
  742. if ((itflag & (CEC_ISR_RXOVR | CEC_ISR_BRE | CEC_ISR_SBPE | CEC_ISR_LBPE | CEC_ISR_RXACKE | CEC_ISR_TXUDR |
  743. CEC_ISR_TXERR | CEC_ISR_TXACKE)) != 0U)
  744. {
  745. hcec->ErrorCode = itflag;
  746. __HAL_CEC_CLEAR_FLAG(hcec, HAL_CEC_ERROR_RXOVR | HAL_CEC_ERROR_BRE | CEC_FLAG_LBPE | CEC_FLAG_SBPE |
  747. HAL_CEC_ERROR_RXACKE | HAL_CEC_ERROR_TXUDR | HAL_CEC_ERROR_TXERR | HAL_CEC_ERROR_TXACKE);
  748. if ((itflag & (CEC_ISR_RXOVR | CEC_ISR_BRE | CEC_ISR_SBPE | CEC_ISR_LBPE | CEC_ISR_RXACKE)) != 0U)
  749. {
  750. hcec->Init.RxBuffer -= hcec->RxXferSize;
  751. hcec->RxXferSize = 0U;
  752. hcec->RxState = HAL_CEC_STATE_READY;
  753. }
  754. else if (((itflag & CEC_ISR_ARBLST) == 0U) && ((itflag & (CEC_ISR_TXUDR | CEC_ISR_TXERR | CEC_ISR_TXACKE)) != 0U))
  755. {
  756. /* Set the CEC state ready to be able to start again the process */
  757. hcec->gState = HAL_CEC_STATE_READY;
  758. }
  759. else
  760. {
  761. /* Nothing todo*/
  762. }
  763. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1U)
  764. hcec->ErrorCallback(hcec);
  765. #else
  766. /* Error Call Back */
  767. HAL_CEC_ErrorCallback(hcec);
  768. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  769. }
  770. else
  771. {
  772. /* Nothing todo*/
  773. }
  774. }
  775. /**
  776. * @brief Tx Transfer completed callback
  777. * @param hcec CEC handle
  778. * @retval None
  779. */
  780. __weak void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
  781. {
  782. /* Prevent unused argument(s) compilation warning */
  783. UNUSED(hcec);
  784. /* NOTE : This function should not be modified, when the callback is needed,
  785. the HAL_CEC_TxCpltCallback can be implemented in the user file
  786. */
  787. }
  788. /**
  789. * @brief Rx Transfer completed callback
  790. * @param hcec CEC handle
  791. * @param RxFrameSize Size of frame
  792. * @retval None
  793. */
  794. __weak void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec, uint32_t RxFrameSize)
  795. {
  796. /* Prevent unused argument(s) compilation warning */
  797. UNUSED(hcec);
  798. UNUSED(RxFrameSize);
  799. /* NOTE : This function should not be modified, when the callback is needed,
  800. the HAL_CEC_RxCpltCallback can be implemented in the user file
  801. */
  802. }
  803. /**
  804. * @brief CEC error callbacks
  805. * @param hcec CEC handle
  806. * @retval None
  807. */
  808. __weak void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
  809. {
  810. /* Prevent unused argument(s) compilation warning */
  811. UNUSED(hcec);
  812. /* NOTE : This function should not be modified, when the callback is needed,
  813. the HAL_CEC_ErrorCallback can be implemented in the user file
  814. */
  815. }
  816. /**
  817. * @}
  818. */
  819. /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
  820. * @brief CEC control functions
  821. *
  822. @verbatim
  823. ===============================================================================
  824. ##### Peripheral Control function #####
  825. ===============================================================================
  826. [..]
  827. This subsection provides a set of functions allowing to control the CEC.
  828. (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
  829. (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
  830. @endverbatim
  831. * @{
  832. */
  833. /**
  834. * @brief return the CEC state
  835. * @param hcec pointer to a CEC_HandleTypeDef structure that contains
  836. * the configuration information for the specified CEC module.
  837. * @retval HAL state
  838. */
  839. HAL_CEC_StateTypeDef HAL_CEC_GetState(const CEC_HandleTypeDef *hcec)
  840. {
  841. uint32_t temp1;
  842. uint32_t temp2;
  843. temp1 = hcec->gState;
  844. temp2 = hcec->RxState;
  845. return (HAL_CEC_StateTypeDef)(temp1 | temp2);
  846. }
  847. /**
  848. * @brief Return the CEC error code
  849. * @param hcec pointer to a CEC_HandleTypeDef structure that contains
  850. * the configuration information for the specified CEC.
  851. * @retval CEC Error Code
  852. */
  853. uint32_t HAL_CEC_GetError(const CEC_HandleTypeDef *hcec)
  854. {
  855. return hcec->ErrorCode;
  856. }
  857. /**
  858. * @}
  859. */
  860. /**
  861. * @}
  862. */
  863. #endif /* CEC */
  864. #endif /* HAL_CEC_MODULE_ENABLED */
  865. /**
  866. * @}
  867. */
  868. /**
  869. * @}
  870. */