stm32f4xx_hal_cec.c 37 KB

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