stm32f1xx_hal_cec.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 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 Bit Timing Error Mode and the Bit Period Error Mode in the hcec Init structure.
  46. (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
  47. [..]
  48. (@) This API (HAL_CEC_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
  49. by calling the customed HAL_CEC_MspInit() API.
  50. *** Callback registration ***
  51. =============================================
  52. The compilation define USE_HAL_CEC_REGISTER_CALLBACKS when set to 1
  53. allows the user to configure dynamically the driver callbacks.
  54. Use Functions HAL_CEC_RegisterCallback() or HAL_CEC_RegisterXXXCallback()
  55. to register an interrupt callback.
  56. Function HAL_CEC_RegisterCallback() allows to register following callbacks:
  57. (+) TxCpltCallback : Tx Transfer completed callback.
  58. (+) ErrorCallback : callback for error detection.
  59. (+) MspInitCallback : CEC MspInit.
  60. (+) MspDeInitCallback : CEC MspDeInit.
  61. This function takes as parameters the HAL peripheral handle, the Callback ID
  62. and a pointer to the user callback function.
  63. For specific callback HAL_CEC_RxCpltCallback use dedicated register callbacks
  64. HAL_CEC_RegisterRxCpltCallback().
  65. Use function HAL_CEC_UnRegisterCallback() to reset a callback to the default
  66. weak function.
  67. HAL_CEC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  68. and the Callback ID.
  69. This function allows to reset following callbacks:
  70. (+) TxCpltCallback : Tx Transfer completed callback.
  71. (+) ErrorCallback : callback for error detection.
  72. (+) MspInitCallback : CEC MspInit.
  73. (+) MspDeInitCallback : CEC MspDeInit.
  74. For callback HAL_CEC_RxCpltCallback use dedicated unregister callback :
  75. HAL_CEC_UnRegisterRxCpltCallback().
  76. By default, after the HAL_CEC_Init() and when the state is HAL_CEC_STATE_RESET
  77. all callbacks are set to the corresponding weak functions :
  78. examples HAL_CEC_TxCpltCallback() , HAL_CEC_RxCpltCallback().
  79. Exception done for MspInit and MspDeInit functions that are
  80. reset to the legacy weak function in the HAL_CEC_Init()/ HAL_CEC_DeInit() only when
  81. these callbacks are null (not registered beforehand).
  82. if not, MspInit or MspDeInit are not null, the HAL_CEC_Init() / HAL_CEC_DeInit()
  83. keep and use the user MspInit/MspDeInit functions (registered beforehand)
  84. Callbacks can be registered/unregistered in HAL_CEC_STATE_READY state only.
  85. Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
  86. in HAL_CEC_STATE_READY or HAL_CEC_STATE_RESET state,
  87. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  88. In that case first register the MspInit/MspDeInit user callbacks
  89. using HAL_CEC_RegisterCallback() before calling HAL_CEC_DeInit()
  90. or HAL_CEC_Init() function.
  91. When the compilation define USE_HAL_CEC_REGISTER_CALLBACKS is set to 0 or
  92. not defined, the callback registration feature is not available and all callbacks
  93. are set to the corresponding weak functions.
  94. @endverbatim
  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,
  326. pCEC_CallbackTypeDef pCallback)
  327. {
  328. HAL_StatusTypeDef status = HAL_OK;
  329. if (pCallback == NULL)
  330. {
  331. /* Update the error code */
  332. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  333. return HAL_ERROR;
  334. }
  335. /* Process locked */
  336. __HAL_LOCK(hcec);
  337. if (hcec->gState == HAL_CEC_STATE_READY)
  338. {
  339. switch (CallbackID)
  340. {
  341. case HAL_CEC_TX_CPLT_CB_ID :
  342. hcec->TxCpltCallback = pCallback;
  343. break;
  344. case HAL_CEC_ERROR_CB_ID :
  345. hcec->ErrorCallback = pCallback;
  346. break;
  347. case HAL_CEC_MSPINIT_CB_ID :
  348. hcec->MspInitCallback = pCallback;
  349. break;
  350. case HAL_CEC_MSPDEINIT_CB_ID :
  351. hcec->MspDeInitCallback = pCallback;
  352. break;
  353. default :
  354. /* Update the error code */
  355. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  356. /* Return error status */
  357. status = HAL_ERROR;
  358. break;
  359. }
  360. }
  361. else if (hcec->gState == HAL_CEC_STATE_RESET)
  362. {
  363. switch (CallbackID)
  364. {
  365. case HAL_CEC_MSPINIT_CB_ID :
  366. hcec->MspInitCallback = pCallback;
  367. break;
  368. case HAL_CEC_MSPDEINIT_CB_ID :
  369. hcec->MspDeInitCallback = pCallback;
  370. break;
  371. default :
  372. /* Update the error code */
  373. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  374. /* Return error status */
  375. status = HAL_ERROR;
  376. break;
  377. }
  378. }
  379. else
  380. {
  381. /* Update the error code */
  382. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  383. /* Return error status */
  384. status = HAL_ERROR;
  385. }
  386. /* Release Lock */
  387. __HAL_UNLOCK(hcec);
  388. return status;
  389. }
  390. /**
  391. * @brief Unregister an CEC Callback
  392. * CEC callback is redirected to the weak predefined callback
  393. * @param hcec uart handle
  394. * @param CallbackID ID of the callback to be unregistered
  395. * This parameter can be one of the following values:
  396. * @arg @ref HAL_CEC_TX_CPLT_CB_ID Tx Complete callback ID
  397. * @arg @ref HAL_CEC_ERROR_CB_ID Error callback ID
  398. * @arg @ref HAL_CEC_MSPINIT_CB_ID MspInit callback ID
  399. * @arg @ref HAL_CEC_MSPDEINIT_CB_ID MspDeInit callback ID
  400. * @retval status
  401. */
  402. HAL_StatusTypeDef HAL_CEC_UnRegisterCallback(CEC_HandleTypeDef *hcec, HAL_CEC_CallbackIDTypeDef CallbackID)
  403. {
  404. HAL_StatusTypeDef status = HAL_OK;
  405. /* Process locked */
  406. __HAL_LOCK(hcec);
  407. if (hcec->gState == HAL_CEC_STATE_READY)
  408. {
  409. switch (CallbackID)
  410. {
  411. case HAL_CEC_TX_CPLT_CB_ID :
  412. hcec->TxCpltCallback = HAL_CEC_TxCpltCallback; /* Legacy weak TxCpltCallback */
  413. break;
  414. case HAL_CEC_ERROR_CB_ID :
  415. hcec->ErrorCallback = HAL_CEC_ErrorCallback; /* Legacy weak ErrorCallback */
  416. break;
  417. case HAL_CEC_MSPINIT_CB_ID :
  418. hcec->MspInitCallback = HAL_CEC_MspInit;
  419. break;
  420. case HAL_CEC_MSPDEINIT_CB_ID :
  421. hcec->MspDeInitCallback = HAL_CEC_MspDeInit;
  422. break;
  423. default :
  424. /* Update the error code */
  425. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  426. /* Return error status */
  427. status = HAL_ERROR;
  428. break;
  429. }
  430. }
  431. else if (hcec->gState == HAL_CEC_STATE_RESET)
  432. {
  433. switch (CallbackID)
  434. {
  435. case HAL_CEC_MSPINIT_CB_ID :
  436. hcec->MspInitCallback = HAL_CEC_MspInit;
  437. break;
  438. case HAL_CEC_MSPDEINIT_CB_ID :
  439. hcec->MspDeInitCallback = HAL_CEC_MspDeInit;
  440. break;
  441. default :
  442. /* Update the error code */
  443. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  444. /* Return error status */
  445. status = HAL_ERROR;
  446. break;
  447. }
  448. }
  449. else
  450. {
  451. /* Update the error code */
  452. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  453. /* Return error status */
  454. status = HAL_ERROR;
  455. }
  456. /* Release Lock */
  457. __HAL_UNLOCK(hcec);
  458. return status;
  459. }
  460. /**
  461. * @brief Register CEC RX complete Callback
  462. * To be used instead of the weak HAL_CEC_RxCpltCallback() predefined callback
  463. * @param hcec CEC handle
  464. * @param pCallback pointer to the Rx transfer compelete Callback function
  465. * @retval HAL status
  466. */
  467. HAL_StatusTypeDef HAL_CEC_RegisterRxCpltCallback(CEC_HandleTypeDef *hcec, pCEC_RxCallbackTypeDef pCallback)
  468. {
  469. HAL_StatusTypeDef status = HAL_OK;
  470. if (pCallback == NULL)
  471. {
  472. /* Update the error code */
  473. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  474. return HAL_ERROR;
  475. }
  476. /* Process locked */
  477. __HAL_LOCK(hcec);
  478. if (HAL_CEC_STATE_READY == hcec->RxState)
  479. {
  480. hcec->RxCpltCallback = pCallback;
  481. }
  482. else
  483. {
  484. /* Update the error code */
  485. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  486. /* Return error status */
  487. status = HAL_ERROR;
  488. }
  489. /* Release Lock */
  490. __HAL_UNLOCK(hcec);
  491. return status;
  492. }
  493. /**
  494. * @brief UnRegister CEC RX complete Callback
  495. * CEC RX complete Callback is redirected to the weak HAL_CEC_RxCpltCallback() predefined callback
  496. * @param hcec CEC handle
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_CEC_UnRegisterRxCpltCallback(CEC_HandleTypeDef *hcec)
  500. {
  501. HAL_StatusTypeDef status = HAL_OK;
  502. /* Process locked */
  503. __HAL_LOCK(hcec);
  504. if (HAL_CEC_STATE_READY == hcec->RxState)
  505. {
  506. hcec->RxCpltCallback = HAL_CEC_RxCpltCallback; /* Legacy weak CEC RxCpltCallback */
  507. }
  508. else
  509. {
  510. /* Update the error code */
  511. hcec->ErrorCode |= HAL_CEC_ERROR_INVALID_CALLBACK;
  512. /* Return error status */
  513. status = HAL_ERROR;
  514. }
  515. /* Release Lock */
  516. __HAL_UNLOCK(hcec);
  517. return status;
  518. }
  519. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  520. /**
  521. * @}
  522. */
  523. /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
  524. * @brief CEC Transmit/Receive functions
  525. *
  526. @verbatim
  527. ===============================================================================
  528. ##### IO operation functions #####
  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,
  563. const uint8_t *pData, uint32_t Size)
  564. {
  565. /* if the peripheral isn't already busy and if there is no previous transmission
  566. already pending due to arbitration lost */
  567. if (hcec->gState == HAL_CEC_STATE_READY)
  568. {
  569. if ((pData == NULL) && (Size > 0U))
  570. {
  571. return HAL_ERROR;
  572. }
  573. assert_param(IS_CEC_ADDRESS(DestinationAddress));
  574. assert_param(IS_CEC_ADDRESS(InitiatorAddress));
  575. assert_param(IS_CEC_MSGSIZE(Size));
  576. /* Process Locked */
  577. __HAL_LOCK(hcec);
  578. hcec->pTxBuffPtr = pData;
  579. hcec->gState = HAL_CEC_STATE_BUSY_TX;
  580. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  581. /* initialize the number of bytes to send,
  582. * 0 means only one header is sent (ping operation) */
  583. hcec->TxXferCount = Size;
  584. /* send header block */
  585. hcec->Instance->TXD = (uint8_t)((uint32_t)InitiatorAddress << CEC_INITIATOR_LSB_POS) | DestinationAddress;
  586. /* Process Unlocked */
  587. __HAL_UNLOCK(hcec);
  588. /* case no data to be sent, sender is only pinging the system */
  589. if (Size != 0)
  590. {
  591. /* Set TX Start of Message (TXSOM) bit */
  592. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TSOM);
  593. }
  594. else
  595. {
  596. /* Send a ping command */
  597. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM | CEC_FLAG_TSOM);
  598. }
  599. return HAL_OK;
  600. }
  601. else
  602. {
  603. return HAL_BUSY;
  604. }
  605. }
  606. /**
  607. * @brief Get size of the received frame.
  608. * @param hcec CEC handle
  609. * @retval Frame size
  610. */
  611. uint32_t HAL_CEC_GetLastReceivedFrameSize(const CEC_HandleTypeDef *hcec)
  612. {
  613. return hcec->RxXferSize;
  614. }
  615. /**
  616. * @brief Change Rx Buffer.
  617. * @param hcec CEC handle
  618. * @param Rxbuffer Rx Buffer
  619. * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
  620. * @retval Frame size
  621. */
  622. void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef *hcec, uint8_t *Rxbuffer)
  623. {
  624. hcec->Init.RxBuffer = Rxbuffer;
  625. }
  626. /**
  627. * @brief This function handles CEC interrupt requests.
  628. * @param hcec CEC handle
  629. * @retval None
  630. */
  631. void HAL_CEC_IRQHandler(CEC_HandleTypeDef *hcec)
  632. {
  633. /* save interrupts register for further error or interrupts handling purposes */
  634. uint32_t itflag;
  635. itflag = hcec->Instance->CSR;
  636. /* Save error status register for further error handling purposes */
  637. hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
  638. /* Transmit error */
  639. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_TERR))
  640. {
  641. /* Acknowledgement of the error */
  642. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TERR);
  643. hcec->gState = HAL_CEC_STATE_READY;
  644. }
  645. /* Receive error */
  646. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_RERR))
  647. {
  648. /* Acknowledgement of the error */
  649. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
  650. hcec->Init.RxBuffer -= hcec->RxXferSize;
  651. hcec->RxXferSize = 0U;
  652. hcec->RxState = HAL_CEC_STATE_READY;
  653. }
  654. if ((hcec->ErrorCode & CEC_ESR_ALL_ERROR) != 0U)
  655. {
  656. /* Error Call Back */
  657. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
  658. hcec->ErrorCallback(hcec);
  659. #else
  660. HAL_CEC_ErrorCallback(hcec);
  661. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  662. }
  663. /* Transmit byte request or block transfer finished */
  664. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_TBTRF))
  665. {
  666. CEC_Transmit_IT(hcec);
  667. }
  668. /* Receive byte or block transfer finished */
  669. if (HAL_IS_BIT_SET(itflag, CEC_FLAG_RBTF))
  670. {
  671. if (hcec->RxXferSize == 0U)
  672. {
  673. /* reception is starting */
  674. hcec->RxState = HAL_CEC_STATE_BUSY_RX;
  675. }
  676. CEC_Receive_IT(hcec);
  677. }
  678. }
  679. /**
  680. * @brief Tx Transfer completed callback
  681. * @param hcec CEC handle
  682. * @retval None
  683. */
  684. __weak void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
  685. {
  686. /* Prevent unused argument(s) compilation warning */
  687. UNUSED(hcec);
  688. /* NOTE : This function should not be modified, when the callback is needed,
  689. the HAL_CEC_TxCpltCallback can be implemented in the user file
  690. */
  691. }
  692. /**
  693. * @brief Rx Transfer completed callback
  694. * @param hcec CEC handle
  695. * @param RxFrameSize Size of frame
  696. * @retval None
  697. */
  698. __weak void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec, uint32_t RxFrameSize)
  699. {
  700. /* Prevent unused argument(s) compilation warning */
  701. UNUSED(hcec);
  702. UNUSED(RxFrameSize);
  703. /* NOTE : This function should not be modified, when the callback is needed,
  704. the HAL_CEC_RxCpltCallback can be implemented in the user file
  705. */
  706. }
  707. /**
  708. * @brief CEC error callbacks
  709. * @param hcec CEC handle
  710. * @retval None
  711. */
  712. __weak void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
  713. {
  714. /* Prevent unused argument(s) compilation warning */
  715. UNUSED(hcec);
  716. /* NOTE : This function should not be modified, when the callback is needed,
  717. the HAL_CEC_ErrorCallback can be implemented in the user file
  718. */
  719. }
  720. /**
  721. * @}
  722. */
  723. /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control functions
  724. * @brief CEC control functions
  725. *
  726. @verbatim
  727. ===============================================================================
  728. ##### Peripheral Control function #####
  729. ===============================================================================
  730. [..]
  731. This subsection provides a set of functions allowing to control the CEC.
  732. (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
  733. (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
  734. @endverbatim
  735. * @{
  736. */
  737. /**
  738. * @brief return the CEC state
  739. * @param hcec pointer to a CEC_HandleTypeDef structure that contains
  740. * the configuration information for the specified CEC module.
  741. * @retval HAL state
  742. */
  743. HAL_CEC_StateTypeDef HAL_CEC_GetState(const CEC_HandleTypeDef *hcec)
  744. {
  745. uint32_t temp1;
  746. uint32_t temp2;
  747. temp1 = hcec->gState;
  748. temp2 = hcec->RxState;
  749. return (HAL_CEC_StateTypeDef)(temp1 | temp2);
  750. }
  751. /**
  752. * @brief Return the CEC error code
  753. * @param hcec pointer to a CEC_HandleTypeDef structure that contains
  754. * the configuration information for the specified CEC.
  755. * @retval CEC Error Code
  756. */
  757. uint32_t HAL_CEC_GetError(const CEC_HandleTypeDef *hcec)
  758. {
  759. return hcec->ErrorCode;
  760. }
  761. /**
  762. * @}
  763. */
  764. /**
  765. * @}
  766. */
  767. /** @addtogroup CEC_Private_Functions
  768. * @{
  769. */
  770. /**
  771. * @brief Send data in interrupt mode
  772. * @param hcec CEC handle.
  773. * Function called under interruption only, once
  774. * interruptions have been enabled by HAL_CEC_Transmit_IT()
  775. * @retval HAL status
  776. */
  777. static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec)
  778. {
  779. /* if the peripheral is already busy or if there is a previous transmission
  780. already pending due to arbitration loss */
  781. if ((hcec->gState == HAL_CEC_STATE_BUSY_TX) || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) != RESET))
  782. {
  783. /* if all data have been sent */
  784. if (hcec->TxXferCount == 0U)
  785. {
  786. /* Acknowledge successful completion by writing 0x00 */
  787. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00U);
  788. hcec->gState = HAL_CEC_STATE_READY;
  789. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
  790. hcec->TxCpltCallback(hcec);
  791. #else
  792. HAL_CEC_TxCpltCallback(hcec);
  793. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  794. return HAL_OK;
  795. }
  796. else
  797. {
  798. /* Reduce the number of bytes to transfer by one */
  799. hcec->TxXferCount--;
  800. /* Write data to TX buffer*/
  801. hcec->Instance->TXD = (uint8_t)*hcec->pTxBuffPtr++;
  802. /* If this is the last byte of the ongoing transmission */
  803. if (hcec->TxXferCount == 0U)
  804. {
  805. /* Acknowledge byte request and signal end of message */
  806. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM);
  807. }
  808. else
  809. {
  810. /* Acknowledge byte request by writing 0x00 */
  811. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00U);
  812. }
  813. return HAL_OK;
  814. }
  815. }
  816. else
  817. {
  818. return HAL_BUSY;
  819. }
  820. }
  821. /**
  822. * @brief Receive data in interrupt mode.
  823. * @param hcec CEC handle.
  824. * Function called under interruption only, once
  825. * interruptions have been enabled by HAL_CEC_Receive_IT()
  826. * @retval HAL status
  827. */
  828. static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec)
  829. {
  830. static uint32_t temp;
  831. if (hcec->RxState == HAL_CEC_STATE_BUSY_RX)
  832. {
  833. temp = hcec->Instance->CSR;
  834. /* Store received data */
  835. hcec->RxXferSize++;
  836. *hcec->Init.RxBuffer++ = hcec->Instance->RXD;
  837. /* Acknowledge received byte by writing 0x00 */
  838. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_RECEIVE_MASK, 0x00U);
  839. /* If the End Of Message is reached */
  840. if (HAL_IS_BIT_SET(temp, CEC_FLAG_REOM))
  841. {
  842. /* Interrupts are not disabled due to transmission still ongoing */
  843. hcec->RxState = HAL_CEC_STATE_READY;
  844. #if (USE_HAL_CEC_REGISTER_CALLBACKS == 1)
  845. hcec->RxCpltCallback(hcec, hcec->RxXferSize);
  846. #else
  847. HAL_CEC_RxCpltCallback(hcec, hcec->RxXferSize);
  848. #endif /* USE_HAL_CEC_REGISTER_CALLBACKS */
  849. return HAL_OK;
  850. }
  851. else
  852. {
  853. return HAL_BUSY;
  854. }
  855. }
  856. else
  857. {
  858. return HAL_BUSY;
  859. }
  860. }
  861. /**
  862. * @}
  863. */
  864. /**
  865. * @}
  866. */
  867. #endif /* CEC */
  868. #endif /* HAL_CEC_MODULE_ENABLED */
  869. /**
  870. * @}
  871. */