stm32f1xx_hal_cec.c 21 KB

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