stm32f7xx_hal_cec.c 37 KB

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