stm32f1xx_hal_cec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. @endverbatim
  40. ******************************************************************************
  41. * @attention
  42. *
  43. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  44. *
  45. * Redistribution and use in source and binary forms, with or without modification,
  46. * are permitted provided that the following conditions are met:
  47. * 1. Redistributions of source code must retain the above copyright notice,
  48. * this list of conditions and the following disclaimer.
  49. * 2. Redistributions in binary form must reproduce the above copyright notice,
  50. * this list of conditions and the following disclaimer in the documentation
  51. * and/or other materials provided with the distribution.
  52. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  53. * may be used to endorse or promote products derived from this software
  54. * without specific prior written permission.
  55. *
  56. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  57. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  59. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  60. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  61. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  62. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  63. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  64. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  65. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  66. *
  67. ******************************************************************************
  68. */
  69. /* Includes ------------------------------------------------------------------*/
  70. #include "stm32f1xx_hal.h"
  71. #ifdef HAL_CEC_MODULE_ENABLED
  72. #if defined(STM32F100xB) || defined(STM32F100xE)
  73. /** @addtogroup STM32F1xx_HAL_Driver
  74. * @{
  75. */
  76. /** @defgroup CEC CEC
  77. * @brief HAL CEC module driver
  78. * @{
  79. */
  80. /* Private typedef -----------------------------------------------------------*/
  81. /* Private define ------------------------------------------------------------*/
  82. /** @defgroup CEC_Private_Constants CEC Private Constants
  83. * @{
  84. */
  85. #define CEC_CFGR_FIELDS (CEC_CFGR_BTEM | CEC_CFGR_BPEM )
  86. #define CEC_FLAG_TRANSMIT_MASK (CEC_FLAG_TSOM|CEC_FLAG_TEOM|CEC_FLAG_TBTRF)
  87. #define CEC_FLAG_RECEIVE_MASK (CEC_FLAG_RSOM|CEC_FLAG_REOM|CEC_FLAG_RBTF)
  88. #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)
  89. #define CEC_RXXFERSIZE_INITIALIZE 0xFFFF /*!< Value used to initialise the RxXferSize of the handle */
  90. /**
  91. * @}
  92. */
  93. /* Private macro -------------------------------------------------------------*/
  94. /* Private variables ---------------------------------------------------------*/
  95. /* Private function prototypes -----------------------------------------------*/
  96. /** @defgroup CEC_Private_Functions CEC Private Functions
  97. * @{
  98. */
  99. static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec);
  100. static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec);
  101. /**
  102. * @}
  103. */
  104. /* Exported functions ---------------------------------------------------------*/
  105. /** @defgroup CEC_Exported_Functions CEC Exported Functions
  106. * @{
  107. */
  108. /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
  109. * @brief Initialization and Configuration functions
  110. *
  111. @verbatim
  112. ===============================================================================
  113. ##### Initialization and Configuration functions #####
  114. ===============================================================================
  115. [..]
  116. This subsection provides a set of functions allowing to initialize the CEC
  117. (+) The following parameters need to be configured:
  118. (++) TimingErrorFree
  119. (++) PeriodErrorFree
  120. (++) InitiatorAddress
  121. @endverbatim
  122. * @{
  123. */
  124. /**
  125. * @brief Initializes the CEC mode according to the specified
  126. * parameters in the CEC_InitTypeDef and creates the associated handle .
  127. * @param hcec: CEC handle
  128. * @retval HAL status
  129. */
  130. HAL_StatusTypeDef HAL_CEC_Init(CEC_HandleTypeDef *hcec)
  131. {
  132. /* Check the CEC handle allocation */
  133. if((hcec == NULL) ||(hcec->Init.RxBuffer == NULL))
  134. {
  135. return HAL_ERROR;
  136. }
  137. /* Check the parameters */
  138. assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
  139. assert_param(IS_CEC_BIT_TIMING_ERROR_MODE(hcec->Init.TimingErrorFree));
  140. assert_param(IS_CEC_BIT_PERIOD_ERROR_MODE(hcec->Init.PeriodErrorFree));
  141. assert_param(IS_CEC_ADDRESS(hcec->Init.OwnAddress));
  142. if(hcec->gState == HAL_CEC_STATE_RESET)
  143. {
  144. /* Allocate lock resource and initialize it */
  145. hcec->Lock = HAL_UNLOCKED;
  146. /* Init the low level hardware : GPIO, CLOCK */
  147. HAL_CEC_MspInit(hcec);
  148. }
  149. hcec->gState = HAL_CEC_STATE_BUSY;
  150. /* Disable the Peripheral */
  151. __HAL_CEC_DISABLE(hcec);
  152. /* Write to CEC Control Register */
  153. MODIFY_REG(hcec->Instance->CFGR, CEC_CFGR_FIELDS, hcec->Init.TimingErrorFree | hcec->Init.PeriodErrorFree);
  154. /* Write to CEC Own Address Register */
  155. MODIFY_REG(hcec->Instance->OAR, CEC_OAR_OA, hcec->Init.OwnAddress);
  156. /* Configure the prescaler to generate the required 50 microseconds time base.*/
  157. MODIFY_REG(hcec->Instance->PRES, CEC_PRES_PRES, 50U * (HAL_RCC_GetPCLK1Freq()/1000000U) - 1U);
  158. /* Enable the following CEC Interrupt */
  159. __HAL_CEC_ENABLE_IT(hcec, CEC_IT_IE);
  160. /* Enable the CEC Peripheral */
  161. __HAL_CEC_ENABLE(hcec);
  162. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  163. hcec->gState = HAL_CEC_STATE_READY;
  164. hcec->RxState = HAL_CEC_STATE_READY;
  165. return HAL_OK;
  166. }
  167. /**
  168. * @brief DeInitializes the CEC peripheral
  169. * @param hcec: CEC handle
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_CEC_DeInit(CEC_HandleTypeDef *hcec)
  173. {
  174. /* Check the CEC handle allocation */
  175. if(hcec == NULL)
  176. {
  177. return HAL_ERROR;
  178. }
  179. /* Check the parameters */
  180. assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
  181. hcec->gState = HAL_CEC_STATE_BUSY;
  182. /* DeInit the low level hardware */
  183. HAL_CEC_MspDeInit(hcec);
  184. __HAL_RCC_CEC_FORCE_RESET();
  185. __HAL_RCC_CEC_RELEASE_RESET();
  186. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  187. hcec->gState = HAL_CEC_STATE_RESET;
  188. hcec->RxState = HAL_CEC_STATE_RESET;
  189. /* Process Unlock */
  190. __HAL_UNLOCK(hcec);
  191. return HAL_OK;
  192. }
  193. /**
  194. * @brief Initializes the Own Address of the CEC device
  195. * @param hcec: CEC handle
  196. * @param CEC_OwnAddress: The CEC own address.
  197. * @retval HAL status
  198. */
  199. HAL_StatusTypeDef HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef *hcec, uint16_t CEC_OwnAddress)
  200. {
  201. /* Check the parameters */
  202. assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress));
  203. if ((hcec->gState == HAL_CEC_STATE_READY) && (hcec->RxState == HAL_CEC_STATE_READY))
  204. {
  205. /* Process Locked */
  206. __HAL_LOCK(hcec);
  207. hcec->gState = HAL_CEC_STATE_BUSY;
  208. /* Disable the Peripheral */
  209. __HAL_CEC_DISABLE(hcec);
  210. if(CEC_OwnAddress != CEC_OWN_ADDRESS_NONE)
  211. {
  212. MODIFY_REG(hcec->Instance->OAR, CEC_OAR_OA, hcec->Init.OwnAddress);
  213. }
  214. else
  215. {
  216. CLEAR_BIT(hcec->Instance->OAR, CEC_OAR_OA);
  217. }
  218. hcec->gState = HAL_CEC_STATE_READY;
  219. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  220. /* Process Unlocked */
  221. __HAL_UNLOCK(hcec);
  222. /* Enable the Peripheral */
  223. __HAL_CEC_ENABLE(hcec);
  224. return HAL_OK;
  225. }
  226. else
  227. {
  228. return HAL_BUSY;
  229. }
  230. }
  231. /**
  232. * @brief CEC MSP Init
  233. * @param hcec: CEC handle
  234. * @retval None
  235. */
  236. __weak void HAL_CEC_MspInit(CEC_HandleTypeDef *hcec)
  237. {
  238. /* Prevent unused argument(s) compilation warning */
  239. UNUSED(hcec);
  240. /* NOTE : This function should not be modified, when the callback is needed,
  241. the HAL_CEC_MspInit can be implemented in the user file
  242. */
  243. }
  244. /**
  245. * @brief CEC MSP DeInit
  246. * @param hcec: CEC handle
  247. * @retval None
  248. */
  249. __weak void HAL_CEC_MspDeInit(CEC_HandleTypeDef *hcec)
  250. {
  251. /* Prevent unused argument(s) compilation warning */
  252. UNUSED(hcec);
  253. /* NOTE : This function should not be modified, when the callback is needed,
  254. the HAL_CEC_MspDeInit can be implemented in the user file
  255. */
  256. }
  257. /**
  258. * @}
  259. */
  260. /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
  261. * @brief CEC Transmit/Receive functions
  262. *
  263. @verbatim
  264. ===============================================================================
  265. ##### IO operation functions #####
  266. ===============================================================================
  267. [..]
  268. This subsection provides a set of functions allowing to manage the CEC data transfers.
  269. (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
  270. logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
  271. (#) The communication is performed using Interrupts.
  272. These API's return the HAL status.
  273. The end of the data processing will be indicated through the
  274. dedicated CEC IRQ when using Interrupt mode.
  275. The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
  276. will be executed respectively at the end of the transmit or Receive process
  277. The HAL_CEC_ErrorCallback() user callback will be executed when a communication
  278. error is detected
  279. (#) API's with Interrupt are :
  280. (+) HAL_CEC_Transmit_IT()
  281. (+) HAL_CEC_IRQHandler()
  282. (#) A set of User Callbacks are provided:
  283. (+) HAL_CEC_TxCpltCallback()
  284. (+) HAL_CEC_RxCpltCallback()
  285. (+) HAL_CEC_ErrorCallback()
  286. @endverbatim
  287. * @{
  288. */
  289. /**
  290. * @brief Send data in interrupt mode
  291. * @param hcec: CEC handle
  292. * @param InitiatorAddress: Initiator address
  293. * @param DestinationAddress: destination logical address
  294. * @param pData: pointer to input byte data buffer
  295. * @param Size: amount of data to be sent in bytes (without counting the header).
  296. * 0 means only the header is sent (ping operation).
  297. * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
  298. * @retval HAL status
  299. */
  300. HAL_StatusTypeDef HAL_CEC_Transmit_IT(CEC_HandleTypeDef *hcec, uint8_t InitiatorAddress,uint8_t DestinationAddress, uint8_t *pData, uint32_t Size)
  301. {
  302. /* if the IP isn't already busy and if there is no previous transmission
  303. already pending due to arbitration lost */
  304. if(hcec->gState == HAL_CEC_STATE_READY)
  305. {
  306. if((pData == NULL ) && (Size > 0U))
  307. {
  308. return HAL_ERROR;
  309. }
  310. assert_param(IS_CEC_ADDRESS(DestinationAddress));
  311. assert_param(IS_CEC_ADDRESS(InitiatorAddress));
  312. assert_param(IS_CEC_MSGSIZE(Size));
  313. /* Process Locked */
  314. __HAL_LOCK(hcec);
  315. hcec->pTxBuffPtr = pData;
  316. hcec->gState = HAL_CEC_STATE_BUSY_TX;
  317. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  318. /* initialize the number of bytes to send,
  319. * 0 means only one header is sent (ping operation) */
  320. hcec->TxXferCount = Size;
  321. /* send header block */
  322. hcec->Instance->TXD = (uint8_t)((uint32_t)InitiatorAddress << CEC_INITIATOR_LSB_POS) | DestinationAddress;
  323. /* Process Unlocked */
  324. __HAL_UNLOCK(hcec);
  325. /* case no data to be sent, sender is only pinging the system */
  326. if (Size != 0)
  327. {
  328. /* Set TX Start of Message (TXSOM) bit */
  329. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TSOM);
  330. }
  331. else
  332. {
  333. /* Send a ping command */
  334. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM|CEC_FLAG_TSOM);
  335. }
  336. return HAL_OK;
  337. }
  338. else
  339. {
  340. return HAL_BUSY;
  341. }
  342. }
  343. /**
  344. * @brief Get size of the received frame.
  345. * @param hcec: CEC handle
  346. * @retval Frame size
  347. */
  348. uint32_t HAL_CEC_GetLastReceivedFrameSize(CEC_HandleTypeDef *hcec)
  349. {
  350. return hcec->RxXferSize;
  351. }
  352. /**
  353. * @brief Change Rx Buffer.
  354. * @param hcec: CEC handle
  355. * @param Rxbuffer: Rx Buffer
  356. * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
  357. * @retval Frame size
  358. */
  359. void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef *hcec, uint8_t* Rxbuffer)
  360. {
  361. hcec->Init.RxBuffer = Rxbuffer;
  362. }
  363. /**
  364. * @brief This function handles CEC interrupt requests.
  365. * @param hcec: CEC handle
  366. * @retval None
  367. */
  368. void HAL_CEC_IRQHandler(CEC_HandleTypeDef *hcec)
  369. {
  370. /* Save error status register for further error handling purposes */
  371. hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
  372. /* Transmit error */
  373. if(__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TERR) != RESET)
  374. {
  375. /* Acknowledgement of the error */
  376. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TERR);
  377. hcec->gState = HAL_CEC_STATE_READY;
  378. }
  379. /* Receive error */
  380. if(__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RERR) != RESET)
  381. {
  382. /* Acknowledgement of the error */
  383. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
  384. hcec->Init.RxBuffer-=hcec->RxXferSize;
  385. hcec->RxXferSize = 0U;
  386. hcec->RxState = HAL_CEC_STATE_READY;
  387. }
  388. if((hcec->ErrorCode & CEC_ESR_ALL_ERROR) != 0U)
  389. {
  390. /* Error Call Back */
  391. HAL_CEC_ErrorCallback(hcec);
  392. }
  393. /* Transmit byte request or block transfer finished */
  394. if(__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TBTRF) != RESET)
  395. {
  396. CEC_Transmit_IT(hcec);
  397. }
  398. /* Receive byte or block transfer finished */
  399. if(__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RBTF) != RESET)
  400. {
  401. if(hcec->RxXferSize == 0U)
  402. {
  403. /* reception is starting */
  404. hcec->RxState = HAL_CEC_STATE_BUSY_RX;
  405. }
  406. CEC_Receive_IT(hcec);
  407. }
  408. }
  409. /**
  410. * @brief Tx Transfer completed callback
  411. * @param hcec: CEC handle
  412. * @retval None
  413. */
  414. __weak void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
  415. {
  416. /* Prevent unused argument(s) compilation warning */
  417. UNUSED(hcec);
  418. /* NOTE : This function should not be modified, when the callback is needed,
  419. the HAL_CEC_TxCpltCallback can be implemented in the user file
  420. */
  421. }
  422. /**
  423. * @brief Rx Transfer completed callback
  424. * @param hcec: CEC handle
  425. * @param RxFrameSize: Size of frame
  426. * @retval None
  427. */
  428. __weak void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec, uint32_t RxFrameSize)
  429. {
  430. /* Prevent unused argument(s) compilation warning */
  431. UNUSED(hcec);
  432. UNUSED(RxFrameSize);
  433. /* NOTE : This function should not be modified, when the callback is needed,
  434. the HAL_CEC_RxCpltCallback can be implemented in the user file
  435. */
  436. }
  437. /**
  438. * @brief CEC error callbacks
  439. * @param hcec: CEC handle
  440. * @retval None
  441. */
  442. __weak void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
  443. {
  444. /* Prevent unused argument(s) compilation warning */
  445. UNUSED(hcec);
  446. /* NOTE : This function should not be modified, when the callback is needed,
  447. the HAL_CEC_ErrorCallback can be implemented in the user file
  448. */
  449. }
  450. /**
  451. * @}
  452. */
  453. /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control functions
  454. * @brief CEC control functions
  455. *
  456. @verbatim
  457. ===============================================================================
  458. ##### Peripheral Control function #####
  459. ===============================================================================
  460. [..]
  461. This subsection provides a set of functions allowing to control the CEC.
  462. (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
  463. (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
  464. @endverbatim
  465. * @{
  466. */
  467. /**
  468. * @brief return the CEC state
  469. * @param hcec: pointer to a CEC_HandleTypeDef structure that contains
  470. * the configuration information for the specified CEC module.
  471. * @retval HAL state
  472. */
  473. HAL_CEC_StateTypeDef HAL_CEC_GetState(CEC_HandleTypeDef *hcec)
  474. {
  475. uint32_t temp1= 0x00U, temp2 = 0x00U;
  476. temp1 = hcec->gState;
  477. temp2 = hcec->RxState;
  478. return (HAL_CEC_StateTypeDef)(temp1 | temp2);
  479. }
  480. /**
  481. * @brief Return the CEC error code
  482. * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
  483. * the configuration information for the specified CEC.
  484. * @retval CEC Error Code
  485. */
  486. uint32_t HAL_CEC_GetError(CEC_HandleTypeDef *hcec)
  487. {
  488. return hcec->ErrorCode;
  489. }
  490. /**
  491. * @}
  492. */
  493. /**
  494. * @}
  495. */
  496. /** @addtogroup CEC_Private_Functions
  497. * @{
  498. */
  499. /**
  500. * @brief Send data in interrupt mode
  501. * @param hcec: CEC handle.
  502. * Function called under interruption only, once
  503. * interruptions have been enabled by HAL_CEC_Transmit_IT()
  504. * @retval HAL status
  505. */
  506. static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec)
  507. {
  508. /* if the IP is already busy or if there is a previous transmission
  509. already pending due to arbitration loss */
  510. if((hcec->gState == HAL_CEC_STATE_BUSY_TX) || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) != RESET))
  511. {
  512. /* if all data have been sent */
  513. if(hcec->TxXferCount == 0U)
  514. {
  515. /* Acknowledge successful completion by writing 0x00 */
  516. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00U);
  517. hcec->gState = HAL_CEC_STATE_READY;
  518. HAL_CEC_TxCpltCallback(hcec);
  519. return HAL_OK;
  520. }
  521. else
  522. {
  523. /* Reduce the number of bytes to transfer by one */
  524. hcec->TxXferCount--;
  525. /* Write data to TX buffer*/
  526. hcec->Instance->TXD = *hcec->pTxBuffPtr++;
  527. /* If this is the last byte of the ongoing transmission */
  528. if(hcec->TxXferCount == 0U)
  529. {
  530. /* Acknowledge byte request and signal end of message */
  531. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM);
  532. }
  533. else
  534. {
  535. /* Acknowledge byte request by writing 0x00 */
  536. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00U);
  537. }
  538. return HAL_OK;
  539. }
  540. }
  541. else
  542. {
  543. return HAL_BUSY;
  544. }
  545. }
  546. /**
  547. * @brief Receive data in interrupt mode.
  548. * @param hcec: CEC handle.
  549. * Function called under interruption only, once
  550. * interruptions have been enabled by HAL_CEC_Receive_IT()
  551. * @retval HAL status
  552. */
  553. static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec)
  554. {
  555. static uint32_t temp;
  556. if(hcec->RxState == HAL_CEC_STATE_BUSY_RX)
  557. {
  558. temp = hcec->Instance->CSR;
  559. /* Store received data */
  560. hcec->RxXferSize++;
  561. *hcec->Init.RxBuffer++ = hcec->Instance->RXD;
  562. /* Acknowledge received byte by writing 0x00 */
  563. MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_RECEIVE_MASK, 0x00U);
  564. /* If the End Of Message is reached */
  565. if(HAL_IS_BIT_SET(temp, CEC_FLAG_REOM))
  566. {
  567. /* Interrupts are not disabled due to transmission still ongoing */
  568. hcec->RxState = HAL_CEC_STATE_READY;
  569. HAL_CEC_RxCpltCallback(hcec, hcec->RxXferSize);
  570. return HAL_OK;
  571. }
  572. else
  573. {
  574. return HAL_BUSY;
  575. }
  576. }
  577. else
  578. {
  579. return HAL_BUSY;
  580. }
  581. }
  582. /**
  583. * @}
  584. */
  585. /**
  586. * @}
  587. */
  588. #endif /* defined(STM32F100xB) || defined(STM32F100xE) */
  589. #endif /* HAL_CEC_MODULE_ENABLED */
  590. /**
  591. * @}
  592. */
  593. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/