stm32f4xx_hal_cec.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cec.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  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 functions
  12. * + IO operation functions
  13. * + Peripheral Control functions
  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 Signal Free Time (SFT) and SFT option, Tolerance, reception stop in
  37. in case of Bit Rising Error, Error-Bit generation conditions, device logical
  38. address and Listen mode in the hcec Init structure.
  39. (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
  40. (@) This API (HAL_CEC_Init()) configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  41. by calling the customed HAL_CEC_MspInit() API.
  42. @endverbatim
  43. ******************************************************************************
  44. * @attention
  45. *
  46. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  47. *
  48. * Redistribution and use in source and binary forms, with or without modification,
  49. * are permitted provided that the following conditions are met:
  50. * 1. Redistributions of source code must retain the above copyright notice,
  51. * this list of conditions and the following disclaimer.
  52. * 2. Redistributions in binary form must reproduce the above copyright notice,
  53. * this list of conditions and the following disclaimer in the documentation
  54. * and/or other materials provided with the distribution.
  55. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  56. * may be used to endorse or promote products derived from this software
  57. * without specific prior written permission.
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  60. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  62. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  63. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  64. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  67. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  68. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. *
  70. ******************************************************************************
  71. */
  72. /* Includes ------------------------------------------------------------------*/
  73. #include "stm32f4xx_hal.h"
  74. /** @addtogroup STM32F4xx_HAL_Driver
  75. * @{
  76. */
  77. /** @defgroup CEC CEC
  78. * @brief HAL CEC module driver
  79. * @{
  80. */
  81. #ifdef HAL_CEC_MODULE_ENABLED
  82. #if defined(STM32F446xx)
  83. /* Private typedef -----------------------------------------------------------*/
  84. /* Private define ------------------------------------------------------------*/
  85. /** @defgroup CEC_Private_Constants CEC Private Constants
  86. * @{
  87. */
  88. #define CEC_CFGR_FIELDS (CEC_CFGR_SFT | CEC_CFGR_RXTOL | CEC_CFGR_BRESTP \
  89. | CEC_CFGR_BREGEN | CEC_CFGR_LBPEGEN | CEC_CFGR_SFTOPT \
  90. | CEC_CFGR_BRDNOGEN | CEC_CFGR_OAR | CEC_CFGR_LSTN)
  91. /**
  92. * @}
  93. */
  94. /* Private macro -------------------------------------------------------------*/
  95. /* Private variables ---------------------------------------------------------*/
  96. /* Private function prototypes -----------------------------------------------*/
  97. /** @defgroup CEC_Private_Functions CEC Private Functions
  98. * @{
  99. */
  100. static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec);
  101. static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec);
  102. /**
  103. * @}
  104. */
  105. /* Exported functions ---------------------------------------------------------*/
  106. /** @defgroup CEC_Exported_Functions CEC Exported Functions
  107. * @{
  108. */
  109. /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
  110. * @brief Initialization and Configuration functions
  111. *
  112. @verbatim
  113. ===============================================================================
  114. ##### Initialization and Configuration functions #####
  115. ===============================================================================
  116. [..]
  117. This subsection provides a set of functions allowing to initialize the CEC
  118. (+) The following parameters need to be configured:
  119. (++) SignalFreeTime
  120. (++) Tolerance
  121. (++) BRERxStop (RX stopped or not upon Bit Rising Error)
  122. (++) BREErrorBitGen (Error-Bit generation in case of Bit Rising Error)
  123. (++) LBPEErrorBitGen (Error-Bit generation in case of Long Bit Period Error)
  124. (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
  125. (++) SignalFreeTimeOption (SFT Timer start definition)
  126. (++) OwnAddress (CEC device address)
  127. (++) ListenMode
  128. @endverbatim
  129. * @{
  130. */
  131. /**
  132. * @brief Initializes the CEC mode according to the specified
  133. * parameters in the CEC_InitTypeDef and creates the associated handle .
  134. * @param hcec: CEC handle
  135. * @retval HAL status
  136. */
  137. HAL_StatusTypeDef HAL_CEC_Init(CEC_HandleTypeDef *hcec)
  138. {
  139. uint32_t tmpreg = 0x0;
  140. /* Check the CEC handle allocation */
  141. if(hcec == NULL)
  142. {
  143. return HAL_ERROR;
  144. }
  145. /* Check the parameters */
  146. assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
  147. assert_param(IS_CEC_SIGNALFREETIME(hcec->Init.SignalFreeTime));
  148. assert_param(IS_CEC_TOLERANCE(hcec->Init.Tolerance));
  149. assert_param(IS_CEC_BRERXSTOP(hcec->Init.BRERxStop));
  150. assert_param(IS_CEC_BREERRORBITGEN(hcec->Init.BREErrorBitGen));
  151. assert_param(IS_CEC_LBPEERRORBITGEN(hcec->Init.LBPEErrorBitGen));
  152. assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec->Init.BroadcastMsgNoErrorBitGen));
  153. assert_param(IS_CEC_SFTOP(hcec->Init.SignalFreeTimeOption));
  154. assert_param(IS_CEC_OAR_ADDRESS(hcec->Init.OwnAddress));
  155. assert_param(IS_CEC_LISTENING_MODE(hcec->Init.ListenMode));
  156. assert_param(IS_CEC_ADDRESS(hcec->Init.InitiatorAddress));
  157. if(hcec->State == HAL_CEC_STATE_RESET)
  158. {
  159. /* Allocate lock resource and initialize it */
  160. hcec->Lock = HAL_UNLOCKED;
  161. /* Init the low level hardware : GPIO, CLOCK */
  162. HAL_CEC_MspInit(hcec);
  163. }
  164. hcec->State = HAL_CEC_STATE_BUSY;
  165. /* Disable the Peripheral */
  166. __HAL_CEC_DISABLE(hcec);
  167. tmpreg = hcec->Init.SignalFreeTime;
  168. tmpreg |= hcec->Init.Tolerance;
  169. tmpreg |= hcec->Init.BRERxStop;
  170. tmpreg |= hcec->Init.BREErrorBitGen;
  171. tmpreg |= hcec->Init.LBPEErrorBitGen;
  172. tmpreg |= hcec->Init.BroadcastMsgNoErrorBitGen;
  173. tmpreg |= hcec->Init.SignalFreeTimeOption;
  174. tmpreg |= (hcec->Init.OwnAddress << CEC_CFGR_OAR_LSB_POS);
  175. tmpreg |= hcec->Init.ListenMode;
  176. /* Write to CEC Control Register */
  177. MODIFY_REG(hcec->Instance->CFGR, CEC_CFGR_FIELDS, tmpreg);
  178. /* Enable the Peripheral */
  179. __HAL_CEC_ENABLE(hcec);
  180. hcec->State = HAL_CEC_STATE_READY;
  181. return HAL_OK;
  182. }
  183. /**
  184. * @brief DeInitializes the CEC peripheral
  185. * @param hcec: CEC handle
  186. * @retval HAL status
  187. */
  188. HAL_StatusTypeDef HAL_CEC_DeInit(CEC_HandleTypeDef *hcec)
  189. {
  190. /* Check the CEC handle allocation */
  191. if(hcec == NULL)
  192. {
  193. return HAL_ERROR;
  194. }
  195. /* Check the parameters */
  196. assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
  197. hcec->State = HAL_CEC_STATE_BUSY;
  198. /* DeInit the low level hardware */
  199. HAL_CEC_MspDeInit(hcec);
  200. /* Disable the Peripheral */
  201. __HAL_CEC_DISABLE(hcec);
  202. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  203. hcec->State = HAL_CEC_STATE_RESET;
  204. /* Process Unlock */
  205. __HAL_UNLOCK(hcec);
  206. return HAL_OK;
  207. }
  208. /**
  209. * @brief CEC MSP Init
  210. * @param hcec: CEC handle
  211. * @retval None
  212. */
  213. __weak void HAL_CEC_MspInit(CEC_HandleTypeDef *hcec)
  214. {
  215. /* Prevent unused argument(s) compilation warning */
  216. UNUSED(hcec);
  217. /* NOTE : This function should not be modified, when the callback is needed,
  218. the HAL_CEC_MspInit can be implemented in the user file
  219. */
  220. }
  221. /**
  222. * @brief CEC MSP DeInit
  223. * @param hcec: CEC handle
  224. * @retval None
  225. */
  226. __weak void HAL_CEC_MspDeInit(CEC_HandleTypeDef *hcec)
  227. {
  228. /* Prevent unused argument(s) compilation warning */
  229. UNUSED(hcec);
  230. /* NOTE : This function should not be modified, when the callback is needed,
  231. the HAL_CEC_MspDeInit can be implemented in the user file
  232. */
  233. }
  234. /**
  235. * @}
  236. */
  237. /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
  238. * @brief CEC Transmit/Receive functions
  239. *
  240. @verbatim
  241. ===============================================================================
  242. ##### IO operation functions #####
  243. ===============================================================================
  244. This subsection provides a set of functions allowing to manage the CEC data transfers.
  245. (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
  246. logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
  247. (#) There are two mode of transfer:
  248. (+) Blocking mode: The communication is performed in polling mode.
  249. The HAL status of all data processing is returned by the same function
  250. after finishing transfer.
  251. (+) No-Blocking mode: The communication is performed using Interrupts.
  252. These API's return the HAL status.
  253. The end of the data processing will be indicated through the
  254. dedicated CEC IRQ when using Interrupt mode.
  255. The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
  256. will be executed respectivelly at the end of the transmit or Receive process
  257. The HAL_CEC_ErrorCallback()user callback will be executed when a communication
  258. error is detected
  259. (#) Blocking mode API's are :
  260. (+) HAL_CEC_Transmit()
  261. (+) HAL_CEC_Receive()
  262. (#) Non-Blocking mode API's with Interrupt are :
  263. (+) HAL_CEC_Transmit_IT()
  264. (+) HAL_CEC_Receive_IT()
  265. (+) HAL_CEC_IRQHandler()
  266. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  267. (+) HAL_CEC_TxCpltCallback()
  268. (+) HAL_CEC_RxCpltCallback()
  269. (+) HAL_CEC_ErrorCallback()
  270. @endverbatim
  271. * @{
  272. */
  273. /**
  274. * @brief Send data in blocking mode
  275. * @param hcec: CEC handle
  276. * @param DestinationAddress: destination logical address
  277. * @param pData: pointer to input byte data buffer
  278. * @param Size: amount of data to be sent in bytes (without counting the header).
  279. * 0 means only the header is sent (ping operation).
  280. * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
  281. * @param Timeout: Timeout duration.
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_CEC_Transmit(CEC_HandleTypeDef *hcec, uint8_t DestinationAddress, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  285. {
  286. uint8_t temp = 0;
  287. uint32_t tempisr = 0;
  288. uint32_t tickstart = 0;
  289. if((hcec->State == HAL_CEC_STATE_READY) && (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) == RESET))
  290. {
  291. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  292. if((pData == NULL ) && (Size > 0))
  293. {
  294. hcec->State = HAL_CEC_STATE_ERROR;
  295. return HAL_ERROR;
  296. }
  297. assert_param(IS_CEC_ADDRESS(DestinationAddress));
  298. assert_param(IS_CEC_MSGSIZE(Size));
  299. /* Process Locked */
  300. __HAL_LOCK(hcec);
  301. hcec->State = HAL_CEC_STATE_BUSY_TX;
  302. hcec->TxXferCount = Size;
  303. /* case no data to be sent, sender is only pinging the system */
  304. if (Size == 0)
  305. {
  306. /* Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
  307. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  308. }
  309. /* send header block */
  310. temp = ((uint32_t)hcec->Init.InitiatorAddress << CEC_INITIATOR_LSB_POS) | DestinationAddress;
  311. hcec->Instance->TXDR = temp;
  312. /* Set TX Start of Message (TXSOM) bit */
  313. __HAL_CEC_FIRST_BYTE_TX_SET(hcec);
  314. while (hcec->TxXferCount > 0)
  315. {
  316. hcec->TxXferCount--;
  317. tickstart = HAL_GetTick();
  318. while(HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_FLAG_TXBR))
  319. {
  320. if(Timeout != HAL_MAX_DELAY)
  321. {
  322. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  323. {
  324. hcec->State = HAL_CEC_STATE_TIMEOUT;
  325. /* Process Unlocked */
  326. __HAL_UNLOCK(hcec);
  327. return HAL_TIMEOUT;
  328. }
  329. }
  330. /* check whether error occured while waiting for TXBR to be set:
  331. * has Tx underrun occurred ?
  332. * has Tx error occurred ?
  333. * has Tx Missing Acknowledge error occurred ?
  334. * has Arbitration Loss error occurred ? */
  335. tempisr = hcec->Instance->ISR;
  336. if ((tempisr & (CEC_FLAG_TXUDR|CEC_FLAG_TXERR|CEC_FLAG_TXACKE|CEC_FLAG_ARBLST)) != 0)
  337. {
  338. /* copy ISR for error handling purposes */
  339. hcec->ErrorCode = tempisr;
  340. /* clear all error flags by default */
  341. __HAL_CEC_CLEAR_FLAG(hcec, (CEC_FLAG_TXUDR|CEC_FLAG_TXERR|CEC_FLAG_TXACKE|CEC_FLAG_ARBLST));
  342. hcec->State = HAL_CEC_STATE_ERROR;
  343. __HAL_UNLOCK(hcec);
  344. return HAL_ERROR;
  345. }
  346. }
  347. /* TXBR to clear BEFORE writing TXDR register */
  348. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXBR);
  349. if (hcec->TxXferCount == 0)
  350. {
  351. /* if last byte transmission, set TX End of Message (TXEOM) bit */
  352. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  353. }
  354. hcec->Instance->TXDR = *pData++;
  355. /* error check after TX byte write up */
  356. tempisr = hcec->Instance->ISR;
  357. if ((tempisr & (CEC_FLAG_TXUDR|CEC_FLAG_TXERR|CEC_FLAG_TXACKE|CEC_FLAG_ARBLST)) != 0)
  358. {
  359. /* copy ISR for error handling purposes */
  360. hcec->ErrorCode = tempisr;
  361. /* clear all error flags by default */
  362. __HAL_CEC_CLEAR_FLAG(hcec, (CEC_FLAG_TXUDR|CEC_FLAG_TXERR|CEC_FLAG_TXACKE|CEC_FLAG_ARBLST));
  363. hcec->State = HAL_CEC_STATE_ERROR;
  364. __HAL_UNLOCK(hcec);
  365. return HAL_ERROR;
  366. }
  367. } /* end while (while (hcec->TxXferCount > 0)) */
  368. /* if no error up to this point, check that transmission is
  369. * complete, that is wait until TXEOM is reset */
  370. tickstart = HAL_GetTick();
  371. while (HAL_IS_BIT_SET(hcec->Instance->CR, CEC_CR_TXEOM))
  372. {
  373. if(Timeout != HAL_MAX_DELAY)
  374. {
  375. if((HAL_GetTick() - tickstart) > Timeout)
  376. {
  377. hcec->State = HAL_CEC_STATE_ERROR;
  378. __HAL_UNLOCK(hcec);
  379. return HAL_TIMEOUT;
  380. }
  381. }
  382. }
  383. /* Final error check once all bytes have been transmitted */
  384. tempisr = hcec->Instance->ISR;
  385. if ((tempisr & (CEC_FLAG_TXUDR|CEC_FLAG_TXERR|CEC_FLAG_TXACKE)) != 0)
  386. {
  387. /* copy ISR for error handling purposes */
  388. hcec->ErrorCode = tempisr;
  389. /* clear all error flags by default */
  390. __HAL_CEC_CLEAR_FLAG(hcec, (CEC_FLAG_TXUDR|CEC_FLAG_TXERR|CEC_FLAG_TXACKE));
  391. hcec->State = HAL_CEC_STATE_ERROR;
  392. __HAL_UNLOCK(hcec);
  393. return HAL_ERROR;
  394. }
  395. hcec->State = HAL_CEC_STATE_READY;
  396. __HAL_UNLOCK(hcec);
  397. return HAL_OK;
  398. }
  399. else
  400. {
  401. return HAL_BUSY;
  402. }
  403. }
  404. /**
  405. * @brief Receive data in blocking mode. Must be invoked when RXBR has been set.
  406. * @param hcec: CEC handle
  407. * @param pData: pointer to received data buffer.
  408. * @param Timeout: Timeout duration.
  409. * Note that the received data size is not known beforehand, the latter is known
  410. * when the reception is complete and is stored in hcec->RxXferSize.
  411. * hcec->RxXferSize is the sum of opcodes + operands (0 to 14 operands max).
  412. * If only a header is received, hcec->RxXferSize = 0
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef HAL_CEC_Receive(CEC_HandleTypeDef *hcec, uint8_t *pData, uint32_t Timeout)
  416. {
  417. uint32_t temp;
  418. uint32_t tickstart = 0;
  419. if (hcec->State == HAL_CEC_STATE_READY)
  420. {
  421. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  422. if (pData == NULL )
  423. {
  424. hcec->State = HAL_CEC_STATE_ERROR;
  425. return HAL_ERROR;
  426. }
  427. hcec->RxXferSize = 0;
  428. /* Process Locked */
  429. __HAL_LOCK(hcec);
  430. /* Rx loop until CEC_ISR_RXEND is set */
  431. while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_FLAG_RXEND))
  432. {
  433. tickstart = HAL_GetTick();
  434. /* Wait for next byte to be received */
  435. while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_FLAG_RXBR))
  436. {
  437. if(Timeout != HAL_MAX_DELAY)
  438. {
  439. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  440. {
  441. hcec->State = HAL_CEC_STATE_TIMEOUT;
  442. __HAL_UNLOCK(hcec);
  443. return HAL_TIMEOUT;
  444. }
  445. }
  446. /* any error so far ?
  447. * has Rx Missing Acknowledge occurred ?
  448. * has Rx Long Bit Period error occurred ?
  449. * has Rx Short Bit Period error occurred ?
  450. * has Rx Bit Rising error occurred ?
  451. * has Rx Overrun error occurred ? */
  452. temp = (uint32_t) (hcec->Instance->ISR);
  453. if ((temp & (CEC_FLAG_RXACKE|CEC_FLAG_LBPE|CEC_FLAG_SBPE|CEC_FLAG_BRE|CEC_FLAG_RXOVR)) != 0)
  454. {
  455. /* copy ISR for error handling purposes */
  456. hcec->ErrorCode = temp;
  457. /* clear all error flags by default */
  458. __HAL_CEC_CLEAR_FLAG(hcec,(CEC_FLAG_RXACKE|CEC_FLAG_LBPE|CEC_FLAG_SBPE|CEC_FLAG_BRE|CEC_FLAG_RXOVR));
  459. hcec->State = HAL_CEC_STATE_ERROR;
  460. __HAL_UNLOCK(hcec);
  461. return HAL_ERROR;
  462. }
  463. } /* while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_ISR_RXBR)) */
  464. /* read received data */
  465. *pData++ = hcec->Instance->RXDR;
  466. temp = (uint32_t) (hcec->Instance->ISR);
  467. /* end of message ? */
  468. if ((temp & CEC_ISR_RXEND) != 0)
  469. {
  470. assert_param(IS_CEC_MSGSIZE(hcec->RxXferSize));
  471. __HAL_CEC_CLEAR_FLAG(hcec,CEC_FLAG_RXEND);
  472. hcec->State = HAL_CEC_STATE_READY;
  473. __HAL_UNLOCK(hcec);
  474. return HAL_OK;
  475. }
  476. /* clear Rx-Byte Received flag */
  477. __HAL_CEC_CLEAR_FLAG(hcec,CEC_FLAG_RXBR);
  478. /* increment payload byte counter */
  479. hcec->RxXferSize++;
  480. } /* while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_ISR_RXEND)) */
  481. /* if the instructions below are executed, it means RXEND was set when RXBR was
  482. * set for the first time:
  483. * the code within the "while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_ISR_RXEND))"
  484. * loop has not been executed and this means a single byte has been sent */
  485. *pData++ = hcec->Instance->RXDR;
  486. /* only one header is received: RxXferSize is set to 0 (no operand, no opcode) */
  487. hcec->RxXferSize = 0;
  488. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXEND);
  489. hcec->State = HAL_CEC_STATE_READY;
  490. __HAL_UNLOCK(hcec);
  491. return HAL_OK;
  492. }
  493. else
  494. {
  495. return HAL_BUSY;
  496. }
  497. }
  498. /**
  499. * @brief Send data in interrupt mode
  500. * @param hcec: CEC handle
  501. * @param DestinationAddress: destination logical address
  502. * @param pData: pointer to input byte data buffer
  503. * @param Size: amount of data to be sent in bytes (without counting the header).
  504. * 0 means only the header is sent (ping operation).
  505. * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
  506. * @retval HAL status
  507. */
  508. HAL_StatusTypeDef HAL_CEC_Transmit_IT(CEC_HandleTypeDef *hcec, uint8_t DestinationAddress, uint8_t *pData, uint32_t Size)
  509. {
  510. uint8_t temp = 0;
  511. /* if the IP isn't already busy and if there is no previous transmission
  512. already pending due to arbitration lost */
  513. if (((hcec->State == HAL_CEC_STATE_READY) || (hcec->State == HAL_CEC_STATE_STANDBY_RX))
  514. && (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) == RESET))
  515. {
  516. if((pData == NULL) && (Size > 0))
  517. {
  518. hcec->State = HAL_CEC_STATE_ERROR;
  519. return HAL_ERROR;
  520. }
  521. assert_param(IS_CEC_ADDRESS(DestinationAddress));
  522. assert_param(IS_CEC_MSGSIZE(Size));
  523. /* Process Locked */
  524. __HAL_LOCK(hcec);
  525. hcec->pTxBuffPtr = pData;
  526. hcec->State = HAL_CEC_STATE_BUSY_TX;
  527. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  528. /* Disable Peripheral to write CEC_IER register */
  529. __HAL_CEC_DISABLE(hcec);
  530. /* Enable the following two CEC Transmission interrupts as
  531. * well as the following CEC Transmission Errors interrupts:
  532. * Tx Byte Request IT
  533. * End of Transmission IT
  534. * Tx Missing Acknowledge IT
  535. * Tx-Error IT
  536. * Tx-Buffer Underrun IT
  537. * Tx arbitration lost */
  538. __HAL_CEC_ENABLE_IT(hcec, CEC_IT_TXBR|CEC_IT_TXEND|CEC_IER_TX_ALL_ERR);
  539. /* Enable the Peripheral */
  540. __HAL_CEC_ENABLE(hcec);
  541. /* initialize the number of bytes to send,
  542. * 0 means only one header is sent (ping operation) */
  543. hcec->TxXferCount = Size;
  544. /* Process Unlocked */
  545. __HAL_UNLOCK(hcec);
  546. /* in case of no payload (Size = 0), sender is only pinging the system;
  547. * Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
  548. if (Size == 0)
  549. {
  550. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  551. }
  552. /* send header block */
  553. temp = (uint8_t)((uint32_t)(hcec->Init.InitiatorAddress) << CEC_INITIATOR_LSB_POS) | DestinationAddress;
  554. hcec->Instance->TXDR = temp;
  555. /* Set TX Start of Message (TXSOM) bit */
  556. __HAL_CEC_FIRST_BYTE_TX_SET(hcec);
  557. return HAL_OK;
  558. }
  559. /* if the IP is already busy or if there is a previous transmission
  560. already pending due to arbitration loss */
  561. else if ((hcec->State == HAL_CEC_STATE_BUSY_TX) || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) != RESET))
  562. {
  563. __HAL_LOCK(hcec);
  564. /* set state to BUSY TX, in case it wasn't set already (case
  565. * of transmission new attempt after arbitration loss) */
  566. if (hcec->State != HAL_CEC_STATE_BUSY_TX)
  567. {
  568. hcec->State = HAL_CEC_STATE_BUSY_TX;
  569. }
  570. /* if all data have been sent */
  571. if(hcec->TxXferCount == 0)
  572. {
  573. /* Disable Peripheral to write CEC_IER register */
  574. __HAL_CEC_DISABLE(hcec);
  575. /* Disable the CEC Transmission Interrupts */
  576. __HAL_CEC_DISABLE_IT(hcec, CEC_IT_TXBR|CEC_IT_TXEND);
  577. /* Disable the CEC Transmission Error Interrupts */
  578. __HAL_CEC_DISABLE_IT(hcec, CEC_IER_TX_ALL_ERR);
  579. /* Enable the Peripheral */
  580. __HAL_CEC_ENABLE(hcec);
  581. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  582. hcec->State = HAL_CEC_STATE_READY;
  583. /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
  584. start again the Transmission under the Tx call back API */
  585. __HAL_UNLOCK(hcec);
  586. HAL_CEC_TxCpltCallback(hcec);
  587. return HAL_OK;
  588. }
  589. else
  590. {
  591. if (hcec->TxXferCount == 1)
  592. {
  593. /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
  594. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  595. }
  596. /* clear Tx-Byte request flag */
  597. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXBR);
  598. hcec->Instance->TXDR = *hcec->pTxBuffPtr++;
  599. hcec->TxXferCount--;
  600. /* Process Unlocked */
  601. __HAL_UNLOCK(hcec);
  602. return HAL_OK;
  603. }
  604. }
  605. else
  606. {
  607. return HAL_BUSY;
  608. }
  609. }
  610. /**
  611. * @brief Receive data in interrupt mode.
  612. * @param hcec: CEC handle
  613. * @param pData: pointer to received data buffer.
  614. * Note that the received data size is not known beforehand, the latter is known
  615. * when the reception is complete and is stored in hcec->RxXferSize.
  616. * hcec->RxXferSize is the sum of opcodes + operands (0 to 14 operands max).
  617. * If only a header is received, hcec->RxXferSize = 0
  618. * @retval HAL status
  619. */
  620. HAL_StatusTypeDef HAL_CEC_Receive_IT(CEC_HandleTypeDef *hcec, uint8_t *pData)
  621. {
  622. if(hcec->State == HAL_CEC_STATE_READY)
  623. {
  624. if(pData == NULL)
  625. {
  626. hcec->State = HAL_CEC_STATE_ERROR;
  627. return HAL_ERROR;
  628. }
  629. /* Process Locked */
  630. __HAL_LOCK(hcec);
  631. hcec->RxXferSize = 0;
  632. hcec->pRxBuffPtr = pData;
  633. hcec->ErrorCode = HAL_CEC_ERROR_NONE;
  634. /* the IP is moving to a ready to receive state */
  635. hcec->State = HAL_CEC_STATE_STANDBY_RX;
  636. /* Disable Peripheral to write CEC_IER register */
  637. __HAL_CEC_DISABLE(hcec);
  638. /* Enable the following CEC Reception Error Interrupts:
  639. * Rx overrun
  640. * Rx bit rising error
  641. * Rx short bit period error
  642. * Rx long bit period error
  643. * Rx missing acknowledge */
  644. __HAL_CEC_ENABLE_IT(hcec, CEC_IER_RX_ALL_ERR);
  645. /* Process Unlocked */
  646. __HAL_UNLOCK(hcec);
  647. /* Enable the following two CEC Reception interrupts:
  648. * Rx Byte Received IT
  649. * End of Reception IT */
  650. __HAL_CEC_ENABLE_IT(hcec, CEC_IT_RXBR|CEC_IT_RXEND);
  651. __HAL_CEC_ENABLE(hcec);
  652. return HAL_OK;
  653. }
  654. else
  655. {
  656. return HAL_BUSY;
  657. }
  658. }
  659. /**
  660. * @brief Get size of the received frame.
  661. * @param hcec: CEC handle
  662. * @retval Frame size
  663. */
  664. uint32_t HAL_CEC_GetReceivedFrameSize(CEC_HandleTypeDef *hcec)
  665. {
  666. return hcec->RxXferSize;
  667. }
  668. /**
  669. * @brief This function handles CEC interrupt requests.
  670. * @param hcec: CEC handle
  671. * @retval None
  672. */
  673. void HAL_CEC_IRQHandler(CEC_HandleTypeDef *hcec)
  674. {
  675. /* save interrupts register for further error or interrupts handling purposes */
  676. hcec->ErrorCode = hcec->Instance->ISR;
  677. /* CEC TX missing acknowledge error interrupt occurred -------------------------------------*/
  678. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TXACKE) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_TXACKE) != RESET))
  679. {
  680. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXACKE);
  681. hcec->State = HAL_CEC_STATE_ERROR;
  682. }
  683. /* CEC transmit error interrupt occured --------------------------------------*/
  684. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TXERR) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_TXERR) != RESET))
  685. {
  686. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXERR);
  687. hcec->State = HAL_CEC_STATE_ERROR;
  688. }
  689. /* CEC TX underrun error interrupt occured --------------------------------------*/
  690. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TXUDR) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_TXUDR) != RESET))
  691. {
  692. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TXUDR);
  693. hcec->State = HAL_CEC_STATE_ERROR;
  694. }
  695. /* CEC TX arbitration error interrupt occured --------------------------------------*/
  696. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_ARBLST) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_ARBLST) != RESET))
  697. {
  698. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_ARBLST);
  699. hcec->State = HAL_CEC_STATE_ERROR;
  700. }
  701. /* CEC RX overrun error interrupt occured --------------------------------------*/
  702. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RXOVR) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_RXOVR) != RESET))
  703. {
  704. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXOVR);
  705. hcec->State = HAL_CEC_STATE_ERROR;
  706. }
  707. /* CEC RX bit rising error interrupt occured -------------------------------*/
  708. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_BRE) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_BRE) != RESET))
  709. {
  710. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_BRE);
  711. hcec->State = HAL_CEC_STATE_ERROR;
  712. }
  713. /* CEC RX short bit period error interrupt occured -------------------------*/
  714. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_SBPE) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_SBPE) != RESET))
  715. {
  716. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_SBPE);
  717. hcec->State = HAL_CEC_STATE_ERROR;
  718. }
  719. /* CEC RX long bit period error interrupt occured --------------------------*/
  720. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_LBPE) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_LBPE) != RESET))
  721. {
  722. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_LBPE);
  723. hcec->State = HAL_CEC_STATE_ERROR;
  724. }
  725. /* CEC RX missing acknowledge error interrupt occured ----------------------*/
  726. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RXACKE) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_RXACKE) != RESET))
  727. {
  728. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXACKE);
  729. hcec->State = HAL_CEC_STATE_ERROR;
  730. }
  731. if ((hcec->ErrorCode & CEC_ISR_ALL_ERROR) != 0)
  732. {
  733. HAL_CEC_ErrorCallback(hcec);
  734. }
  735. /* CEC RX byte received interrupt -----------------------------------------*/
  736. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RXBR) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_RXBR) != RESET))
  737. {
  738. /* RXBR IT is cleared during HAL_CEC_Transmit_IT processing */
  739. CEC_Receive_IT(hcec);
  740. }
  741. /* CEC RX end received interrupt ------------------------------------------*/
  742. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RXEND) != RESET) && (__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_RXEND) != RESET))
  743. {
  744. /* RXBR IT is cleared during HAL_CEC_Transmit_IT processing */
  745. CEC_Receive_IT(hcec);
  746. }
  747. /* CEC TX byte request interrupt -------------------------------------------*/
  748. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TXBR) != RESET) &&(__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_TXBR) != RESET))
  749. {
  750. /* TXBR IT is cleared during HAL_CEC_Transmit_IT processing */
  751. CEC_Transmit_IT(hcec);
  752. }
  753. /* CEC TX end interrupt ----------------------------------------------------*/
  754. if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TXEND) != RESET) &&(__HAL_CEC_GET_IT_SOURCE(hcec, CEC_IT_TXEND) != RESET))
  755. {
  756. /* TXEND IT is cleared during HAL_CEC_Transmit_IT processing */
  757. CEC_Transmit_IT(hcec);
  758. }
  759. }
  760. /**
  761. * @brief Tx Transfer completed callback
  762. * @param hcec: CEC handle
  763. * @retval None
  764. */
  765. __weak void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
  766. {
  767. /* Prevent unused argument(s) compilation warning */
  768. UNUSED(hcec);
  769. /* NOTE : This function should not be modified, when the callback is needed,
  770. the HAL_CEC_TxCpltCallback can be implemented in the user file
  771. */
  772. }
  773. /**
  774. * @brief Rx Transfer completed callback
  775. * @param hcec: CEC handle
  776. * @retval None
  777. */
  778. __weak void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec)
  779. {
  780. /* Prevent unused argument(s) compilation warning */
  781. UNUSED(hcec);
  782. /* NOTE : This function should not be modified, when the callback is needed,
  783. the HAL_CEC_TxCpltCallback can be implemented in the user file
  784. */
  785. }
  786. /**
  787. * @brief CEC error callbacks
  788. * @param hcec: CEC handle
  789. * @retval None
  790. */
  791. __weak void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
  792. {
  793. /* Prevent unused argument(s) compilation warning */
  794. UNUSED(hcec);
  795. /* NOTE : This function should not be modified, when the callback is needed,
  796. the HAL_CEC_ErrorCallback can be implemented in the user file
  797. */
  798. }
  799. /**
  800. * @}
  801. */
  802. /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
  803. * @brief CEC control functions
  804. *
  805. @verbatim
  806. ===============================================================================
  807. ##### Peripheral Control function #####
  808. ===============================================================================
  809. [..]
  810. This subsection provides a set of functions allowing to control the CEC.
  811. (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
  812. @endverbatim
  813. * @{
  814. */
  815. /**
  816. * @brief return the CEC state
  817. * @param hcec: CEC handle
  818. * @retval HAL state
  819. */
  820. HAL_CEC_StateTypeDef HAL_CEC_GetState(CEC_HandleTypeDef *hcec)
  821. {
  822. return hcec->State;
  823. }
  824. /**
  825. * @brief Return the CEC error code
  826. * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
  827. * the configuration information for the specified CEC.
  828. * @retval CEC Error Code
  829. */
  830. uint32_t HAL_CEC_GetError(CEC_HandleTypeDef *hcec)
  831. {
  832. return hcec->ErrorCode;
  833. }
  834. /**
  835. * @}
  836. */
  837. /**
  838. * @brief Send data in interrupt mode
  839. * @param hcec: CEC handle.
  840. * Function called under interruption only, once
  841. * interruptions have been enabled by HAL_CEC_Transmit_IT()
  842. * @retval HAL status
  843. */
  844. static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec)
  845. {
  846. /* if the IP is already busy or if there is a previous transmission
  847. already pending due to arbitration loss */
  848. if ((hcec->State == HAL_CEC_STATE_BUSY_TX)
  849. || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) != RESET))
  850. {
  851. __HAL_LOCK(hcec);
  852. /* set state to BUSY TX, in case it wasn't set already (case
  853. * of transmission new attempt after arbitration loss) */
  854. if (hcec->State != HAL_CEC_STATE_BUSY_TX)
  855. {
  856. hcec->State = HAL_CEC_STATE_BUSY_TX;
  857. }
  858. /* if all data have been sent */
  859. if(hcec->TxXferCount == 0)
  860. {
  861. /* Disable Peripheral to write CEC_IER register */
  862. __HAL_CEC_DISABLE(hcec);
  863. /* Disable the CEC Transmission Interrupts */
  864. __HAL_CEC_DISABLE_IT(hcec, CEC_IT_TXBR|CEC_IT_TXEND);
  865. /* Disable the CEC Transmission Error Interrupts */
  866. __HAL_CEC_DISABLE_IT(hcec, CEC_IER_TX_ALL_ERR);
  867. /* Enable the Peripheral */
  868. __HAL_CEC_ENABLE(hcec);
  869. __HAL_CEC_CLEAR_FLAG(hcec,CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  870. hcec->State = HAL_CEC_STATE_READY;
  871. /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
  872. start again the Transmission under the Tx call back API */
  873. __HAL_UNLOCK(hcec);
  874. HAL_CEC_TxCpltCallback(hcec);
  875. return HAL_OK;
  876. }
  877. else
  878. {
  879. if (hcec->TxXferCount == 1)
  880. {
  881. /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
  882. __HAL_CEC_LAST_BYTE_TX_SET(hcec);
  883. }
  884. /* clear Tx-Byte request flag */
  885. __HAL_CEC_CLEAR_FLAG(hcec,CEC_FLAG_TXBR);
  886. hcec->Instance->TXDR = *hcec->pTxBuffPtr++;
  887. hcec->TxXferCount--;
  888. /* Process Unlocked */
  889. __HAL_UNLOCK(hcec);
  890. return HAL_OK;
  891. }
  892. }
  893. else
  894. {
  895. return HAL_BUSY;
  896. }
  897. }
  898. /**
  899. * @brief Receive data in interrupt mode.
  900. * @param hcec: CEC handle.
  901. * Function called under interruption only, once
  902. * interruptions have been enabled by HAL_CEC_Receive_IT()
  903. * @retval HAL status
  904. */
  905. static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec)
  906. {
  907. uint32_t tempisr;
  908. /* Three different conditions are tested to carry out the RX IT processing:
  909. * - the IP is in reception stand-by (the IP state is HAL_CEC_STATE_STANDBY_RX) and
  910. * the reception of the first byte is starting
  911. * - a message reception is already on-going (the IP state is HAL_CEC_STATE_BUSY_RX)
  912. * and a new byte is being received
  913. * - a transmission has just been started (the IP state is HAL_CEC_STATE_BUSY_TX)
  914. * but has been interrupted by a new message reception or discarded due to
  915. * arbitration loss: the reception of the first or higher priority message
  916. * (the arbitration winner) is starting */
  917. if ((hcec->State == HAL_CEC_STATE_STANDBY_RX)
  918. || (hcec->State == HAL_CEC_STATE_BUSY_RX)
  919. || (hcec->State == HAL_CEC_STATE_BUSY_TX))
  920. {
  921. /* reception is starting */
  922. hcec->State = HAL_CEC_STATE_BUSY_RX;
  923. tempisr = (uint32_t) (hcec->Instance->ISR);
  924. if ((tempisr & CEC_FLAG_RXBR) != 0)
  925. {
  926. /* Process Locked */
  927. __HAL_LOCK(hcec);
  928. /* read received byte */
  929. *hcec->pRxBuffPtr++ = hcec->Instance->RXDR;
  930. /* if last byte has been received */
  931. if ((tempisr & CEC_FLAG_RXEND) != 0)
  932. {
  933. /* clear IT */
  934. __HAL_CEC_CLEAR_FLAG(hcec,CEC_FLAG_RXBR|CEC_FLAG_RXEND);
  935. /* RX interrupts are not disabled at this point.
  936. * Indeed, to disable the IT, the IP must be disabled first
  937. * which resets the TXSOM flag. In case of arbitration loss,
  938. * this leads to a transmission abort.
  939. * Therefore, RX interruptions disabling if so required,
  940. * is done in HAL_CEC_RxCpltCallback */
  941. /* IP state is moved to READY.
  942. * If the IP must remain in standby mode to listen
  943. * any new message, it is up to HAL_CEC_RxCpltCallback
  944. * to move it again to HAL_CEC_STATE_STANDBY_RX */
  945. hcec->State = HAL_CEC_STATE_READY;
  946. /* Call the Process Unlocked before calling the Rx call back API */
  947. __HAL_UNLOCK(hcec);
  948. HAL_CEC_RxCpltCallback(hcec);
  949. return HAL_OK;
  950. }
  951. __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RXBR);
  952. hcec->RxXferSize++;
  953. /* Process Unlocked */
  954. __HAL_UNLOCK(hcec);
  955. return HAL_OK;
  956. }
  957. else
  958. {
  959. return HAL_BUSY;
  960. }
  961. }
  962. else
  963. {
  964. return HAL_BUSY;
  965. }
  966. }
  967. /**
  968. * @}
  969. */
  970. #endif /* STM32F446xx */
  971. #endif /* HAL_CEC_MODULE_ENABLED */
  972. /**
  973. * @}
  974. */
  975. /**
  976. * @}
  977. */
  978. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/