stm32f4xx_hal_cec.c 31 KB

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