stm32f1xx_hal_cec.c 30 KB

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