2 ******************************************************************************
3 * @file stm32f1xx_hal_cec.c
4 * @author MCD Application Team
6 * @date 15-December-2014
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
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The CEC HAL driver can be used as follows:
21 (#) Declare a CEC_HandleTypeDef handle structure.
22 (#) Initialize the CEC low level resources by implementing the HAL_CEC_MspInit ()API:
23 (##) Enable the CEC interface clock.
24 (##) Enable the clock for the CEC GPIOs.
25 (##) Configure these CEC pins as alternate function pull-up.
26 (##) NVIC configuration if you need to use interrupt process (HAL_CEC_Transmit_IT()
27 and HAL_CEC_Receive_IT() APIs):
28 (##) Configure the CEC interrupt priority.
29 (##) Enable the NVIC CEC IRQ handle.
30 (##) The CEC interrupt is activated/deactivated by the HAL driver
32 (#) Program the Bit Timing Error Mode and the Bit Period Error Mode in the hcec Init structure.
34 (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
36 (#) This API (HAL_CEC_Init()) configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
37 by calling the customized HAL_CEC_MspInit() API.
40 ******************************************************************************
43 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
45 * Redistribution and use in source and binary forms, with or without modification,
46 * are permitted provided that the following conditions are met:
47 * 1. Redistributions of source code must retain the above copyright notice,
48 * this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright notice,
50 * this list of conditions and the following disclaimer in the documentation
51 * and/or other materials provided with the distribution.
52 * 3. Neither the name of STMicroelectronics nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
57 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
63 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
64 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
65 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 ******************************************************************************
70 /* Includes ------------------------------------------------------------------*/
71 #include "stm32f1xx_hal.h"
73 #ifdef HAL_CEC_MODULE_ENABLED
75 #if defined(STM32F100xB) || defined(STM32F100xE)
77 /** @addtogroup STM32F1xx_HAL_Driver
82 * @brief HAL CEC module driver
86 /* Private typedef -----------------------------------------------------------*/
87 /* Private define ------------------------------------------------------------*/
88 /** @defgroup CEC_Private_Constants CEC Private Constants
91 #define CEC_CFGR_FIELDS (CEC_CFGR_BTEM | CEC_CFGR_BPEM )
92 #define CEC_FLAG_TRANSMIT_MASK (CEC_FLAG_TSOM|CEC_FLAG_TEOM|CEC_FLAG_TBTRF)
93 #define CEC_FLAG_RECEIVE_MASK (CEC_FLAG_RSOM|CEC_FLAG_REOM|CEC_FLAG_RBTF)
94 #define CEC_ESR_ALL_ERROR (CEC_ESR_BTE|CEC_ESR_BPE|CEC_ESR_RBTFE|CEC_ESR_SBE|CEC_ESR_ACKE|CEC_ESR_LINE|CEC_ESR_TBTFE)
95 #define CEC_RXXFERSIZE_INITIALIZE 0xFFFF /*!< Value used to initialise the RxXferSize of the handle */
100 /* Private macro -------------------------------------------------------------*/
101 /* Private variables ---------------------------------------------------------*/
102 /* Private function prototypes -----------------------------------------------*/
103 /** @defgroup CEC_Private_Functions CEC Private Functions
106 static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec);
107 static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec);
112 /* Exported functions ---------------------------------------------------------*/
114 /** @defgroup CEC_Exported_Functions CEC Exported Functions
118 /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
119 * @brief Initialization and Configuration functions
122 ===============================================================================
123 ##### Initialization and Configuration functions #####
124 ===============================================================================
126 This subsection provides a set of functions allowing to initialize the CEC
127 (+) The following parameters need to be configured:
130 (++) InitiatorAddress
137 * @brief Initializes the CEC mode according to the specified
138 * parameters in the CEC_InitTypeDef and creates the associated handle .
139 * @param hcec: CEC handle
142 HAL_StatusTypeDef HAL_CEC_Init(CEC_HandleTypeDef *hcec)
144 /* Check the CEC handle allocation */
150 /* Check the parameters */
151 assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
152 assert_param(IS_CEC_BIT_TIMING_ERROR_MODE(hcec->Init.TimingErrorFree));
153 assert_param(IS_CEC_BIT_PERIOD_ERROR_MODE(hcec->Init.PeriodErrorFree));
154 assert_param(IS_CEC_ADDRESS(hcec->Init.InitiatorAddress));
156 if(hcec->State == HAL_CEC_STATE_RESET)
158 /* Allocate lock resource and initialize it */
159 hcec-> Lock = HAL_UNLOCKED;
160 /* Init the low level hardware : GPIO, CLOCK */
161 HAL_CEC_MspInit(hcec);
164 hcec->State = HAL_CEC_STATE_BUSY;
166 /* Disable the Peripheral */
167 __HAL_CEC_DISABLE(hcec);
169 /* Write to CEC Control Register */
170 MODIFY_REG(hcec->Instance->CFGR, CEC_CFGR_FIELDS, hcec->Init.TimingErrorFree|hcec->Init.PeriodErrorFree);
172 /* Write to CEC Own Address Register */
173 MODIFY_REG(hcec->Instance->OAR, CEC_OAR_OA, hcec->Init.InitiatorAddress);
175 /* Configure the prescaler to generate the required 50 microseconds time base.*/
176 MODIFY_REG(hcec->Instance->PRES, CEC_PRES_PRES, 50*(HAL_RCC_GetPCLK1Freq()/1000000)-1);
178 /* Enable the Peripheral */
179 __HAL_CEC_ENABLE(hcec);
181 hcec->State = HAL_CEC_STATE_READY;
189 * @brief DeInitializes the CEC peripheral
190 * @param hcec: CEC handle
193 HAL_StatusTypeDef HAL_CEC_DeInit(CEC_HandleTypeDef *hcec)
195 /* Check the CEC handle allocation */
201 /* Check the parameters */
202 assert_param(IS_CEC_ALL_INSTANCE(hcec->Instance));
204 hcec->State = HAL_CEC_STATE_BUSY;
206 /* Set peripheral to reset state */
207 hcec->Instance->CFGR = 0x0;
208 hcec->Instance->OAR = 0x0;
209 hcec->Instance->PRES = 0x0;
210 hcec->Instance->CFGR = 0x0;
211 hcec->Instance->ESR = 0x0;
212 hcec->Instance->CSR = 0x0;
213 hcec->Instance->TXD = 0x0;
214 hcec->Instance->RXD = 0x0;
216 /* Disable the Peripheral */
217 __HAL_CEC_DISABLE(hcec);
219 /* DeInit the low level hardware */
220 HAL_CEC_MspDeInit(hcec);
222 hcec->ErrorCode = HAL_CEC_ERROR_NONE;
223 hcec->State = HAL_CEC_STATE_RESET;
232 * @brief CEC MSP Init
233 * @param hcec: CEC handle
236 __weak void HAL_CEC_MspInit(CEC_HandleTypeDef *hcec)
238 /* NOTE : This function should not be modified, when the callback is needed,
239 the HAL_CEC_MspInit can be implemented in the user file
244 * @brief CEC MSP DeInit
245 * @param hcec: CEC handle
248 __weak void HAL_CEC_MspDeInit(CEC_HandleTypeDef *hcec)
250 /* NOTE : This function should not be modified, when the callback is needed,
251 the HAL_CEC_MspDeInit can be implemented in the user file
259 /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
260 * @brief CEC Transmit/Receive functions
263 ===============================================================================
264 ##### IO operation functions #####
265 ===============================================================================
267 This subsection provides a set of functions allowing to manage the CEC data transfers.
269 (#) There are two modes of transfer:
270 (##) Blocking mode: The communication is performed in polling mode.
271 The HAL status of all data processing is returned by the same function
272 after finishing transfer.
273 (##) No-Blocking mode: The communication is performed using Interrupts.
274 These API's return the HAL status.
275 The end of the data processing will be indicated through the
276 dedicated CEC IRQ when using Interrupt mode.
277 The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
278 will be executed respectivelly at the end of the Transmit or Receive process.
279 The HAL_CEC_ErrorCallback()user callback will be executed when a communication
281 (#) Blocking mode API's are :
282 (##) HAL_CEC_Transmit()
283 (##) HAL_CEC_Receive()
284 (#) Non-Blocking mode API's with Interrupt are :
285 (##) HAL_CEC_Transmit_IT()
286 (##) HAL_CEC_Receive_IT()
287 (##) HAL_CEC_IRQHandler()
288 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
289 (##) HAL_CEC_TxCpltCallback()
290 (##) HAL_CEC_RxCpltCallback()
291 (##) HAL_CEC_ErrorCallback()
298 * @brief Send data in blocking mode
299 * @param hcec: CEC handle
300 * @param DestinationAddress: destination logical address
301 * @param pData: pointer to input byte data buffer
302 * @param Size: amount of data to be sent in bytes (without counting the header).
303 * 0 means only the header is sent (ping operation).
304 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
305 * @param Timeout: Timeout duration.
308 HAL_StatusTypeDef HAL_CEC_Transmit(CEC_HandleTypeDef *hcec, uint8_t DestinationAddress, uint8_t *pData, uint32_t Size, uint32_t Timeout)
311 uint32_t tickstart = 0;
313 /* If the IP is ready */
314 if((hcec->State == HAL_CEC_STATE_READY)
315 && (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) == RESET))
317 /* Basic check on pData pointer */
318 if(((pData == NULL) && (Size > 0)) || (! IS_CEC_MSGSIZE(Size)))
323 assert_param(IS_CEC_ADDRESS(DestinationAddress));
328 /* Enter the transmit mode */
329 hcec->State = HAL_CEC_STATE_BUSY_TX;
330 hcec->ErrorCode = HAL_CEC_ERROR_NONE;
332 /* Initialize the number of bytes to send,
333 * 0 means only one header is sent (ping operation) */
334 hcec->TxXferCount = Size;
336 /* Send header block */
337 temp = (uint8_t)((uint32_t)(hcec->Init.InitiatorAddress) << CEC_INITIATOR_LSB_POS) | DestinationAddress;
338 hcec->Instance->TXD = temp;
340 /* In case no data to be sent, sender is only pinging the system */
343 /* Set TX Start of Message (TXSOM) bit */
344 hcec->Instance->CSR = CEC_FLAG_TSOM;
348 /* Send a ping command */
349 hcec->Instance->CSR = CEC_FLAG_TEOM|CEC_FLAG_TSOM;
352 /* Polling TBTRF bit with timeout handling*/
353 while (hcec->TxXferCount > 0)
355 /* Decreasing of the number of remaining data to receive */
358 /* Timeout handling */
359 tickstart = HAL_GetTick();
361 /* Waiting for the next data transmission */
362 while(HAL_IS_BIT_CLR(hcec->Instance->CSR, CEC_FLAG_TBTRF))
364 /* Timeout handling */
365 if(Timeout != HAL_MAX_DELAY)
367 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
369 hcec->State = HAL_CEC_STATE_READY;
370 /* Process Unlocked */
376 /* Check if an error occured */
377 if(HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_TERR) || HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_RERR))
379 /* Copy ESR for error handling purposes */
380 hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
382 /* Acknowledgement of the error */
383 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TERR);
384 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
386 hcec->State = HAL_CEC_STATE_READY;
392 /* Write the next data to TX buffer */
393 hcec->Instance->TXD = *pData++;
395 /* If this is the last byte of the ongoing transmission */
396 if (hcec->TxXferCount == 0)
398 /* Acknowledge byte request and signal end of message */
399 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM);
403 /* Acknowledge byte request by writing 0x00 */
404 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00);
408 /* Timeout handling */
409 tickstart = HAL_GetTick();
411 /* Wait for message transmission completion (TBTRF is set) */
412 while (HAL_IS_BIT_CLR(hcec->Instance->CSR, CEC_FLAG_TBTRF))
414 /* Timeout handling */
415 if(Timeout != HAL_MAX_DELAY)
417 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
419 hcec->State = HAL_CEC_STATE_READY;
425 /* Check of error during transmission of the last byte */
426 if(HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_TERR) || HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_RERR))
428 /* Copy ESR for error handling purposes */
429 hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
431 /* Acknowledgement of the error */
432 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TERR);
433 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
435 hcec->State = HAL_CEC_STATE_READY;
441 /* Check of error after the last byte transmission */
442 if(HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_TERR) || HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_RERR))
444 /* Copy ESR for error handling purposes */
445 hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
447 /* Acknowledgement of the error */
448 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TERR);
449 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
451 hcec->State = HAL_CEC_STATE_READY;
456 /* Acknowledge successful completion by writing 0x00 */
457 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00);
459 hcec->State = HAL_CEC_STATE_READY;
471 * @brief Receive data in blocking mode.
472 * @param hcec: CEC handle
473 * @param pData: pointer to received data buffer.
474 * @param Timeout: Timeout duration.
475 * @note The received data size is not known beforehand, the latter is known
476 * when the reception is complete and is stored in hcec->RxXferSize.
477 * hcec->RxXferSize is the sum of opcodes + operands (0 to 14 operands max).
478 * If only a header is received, hcec->RxXferSize = 0
481 HAL_StatusTypeDef HAL_CEC_Receive(CEC_HandleTypeDef *hcec, uint8_t *pData, uint32_t Timeout)
484 uint32_t tickstart = 0;
486 if(hcec->State == HAL_CEC_STATE_READY)
493 /* When a ping is received, RxXferSize is 0*/
494 /* When a message is received, RxXferSize contains the number of received bytes */
495 hcec->RxXferSize = CEC_RXXFERSIZE_INITIALIZE;
500 hcec->ErrorCode = HAL_CEC_ERROR_NONE;
502 /* Continue the reception until the End Of Message is received (CEC_FLAG_REOM) */
505 /* Timeout handling */
506 tickstart = HAL_GetTick();
508 /* Wait for next byte to be received */
509 while (HAL_IS_BIT_CLR(hcec->Instance->CSR, CEC_FLAG_RBTF))
511 /* Timeout handling */
512 if(Timeout != HAL_MAX_DELAY)
514 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
516 hcec->State = HAL_CEC_STATE_READY;
522 /* Check if an error occured during the reception */
523 if(HAL_IS_BIT_SET(hcec->Instance->CSR, CEC_FLAG_RERR))
525 /* Copy ESR for error handling purposes */
526 hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
528 /* Acknowledgement of the error */
529 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
531 hcec->State = HAL_CEC_STATE_READY;
537 /* Keep the value of CSR register as the register is cleared during reception process */
538 temp = hcec->Instance->CSR;
540 /* Read received data */
541 *pData++ = hcec->Instance->RXD;
543 /* Acknowledge received byte by writing 0x00 */
544 CLEAR_BIT(hcec->Instance->CSR, CEC_FLAG_RECEIVE_MASK);
546 /* Increment the number of received data */
547 if(hcec->RxXferSize == CEC_RXXFERSIZE_INITIALIZE)
549 hcec->RxXferSize = 0;
556 }while (HAL_IS_BIT_CLR(temp, CEC_FLAG_REOM));
558 hcec->State = HAL_CEC_STATE_READY;
561 if(IS_CEC_MSGSIZE(hcec->RxXferSize))
578 * @brief Send data in interrupt mode
579 * @param hcec: CEC handle
580 * @param DestinationAddress: destination logical address
581 * @param pData: pointer to input byte data buffer
582 * @param Size: amount of data to be sent in bytes (without counting the header).
583 * 0 means only the header is sent (ping operation).
584 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
587 HAL_StatusTypeDef HAL_CEC_Transmit_IT(CEC_HandleTypeDef *hcec, uint8_t DestinationAddress, uint8_t *pData, uint32_t Size)
590 uint32_t tmp_state = 0;
592 tmp_state = hcec->State;
593 if(((tmp_state == HAL_CEC_STATE_READY) || (tmp_state == HAL_CEC_STATE_BUSY_RX))
594 && (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) == RESET))
597 /* Basic check on pData pointer */
598 if(((pData == NULL) && (Size > 0)) || (! IS_CEC_MSGSIZE(Size)))
603 assert_param(IS_CEC_ADDRESS(DestinationAddress));
607 hcec->pTxBuffPtr = pData;
609 /* Check if a receive process is ongoing or not */
610 if(hcec->State == HAL_CEC_STATE_BUSY_RX)
612 hcec->State = HAL_CEC_STATE_BUSY_TX_RX;
614 /* Interrupt are not enabled here because they are already enabled in the Reception process */
618 hcec->State = HAL_CEC_STATE_BUSY_TX;
620 /* Enable the CEC interrupt */
621 __HAL_CEC_ENABLE_IT(hcec, CEC_IT_IE);
624 hcec->ErrorCode = HAL_CEC_ERROR_NONE;
626 /* initialize the number of bytes to send,
627 * 0 means only one header is sent (ping operation) */
628 hcec->TxXferCount = Size;
630 /* send header block */
631 temp = (uint8_t)((uint32_t)(hcec->Init.InitiatorAddress) << CEC_INITIATOR_LSB_POS) | DestinationAddress;
632 hcec->Instance->TXD = temp;
634 /* Process Unlocked */
637 /* case no data to be sent, sender is only pinging the system */
640 /* Set TX Start of Message (TXSOM) bit */
641 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TSOM);
645 /* Send a ping command */
646 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM|CEC_FLAG_TSOM);
658 * @brief Receive data in interrupt mode.
659 * @param hcec: CEC handle
660 * @param pData: pointer to received data buffer.
661 * @note The received data size is not known beforehand, the latter is known
662 * when the reception is complete and is stored in hcec->RxXferSize.
663 * hcec->RxXferSize is the sum of opcodes + operands (0 to 14 operands max).
664 * If only a header is received, hcec->RxXferSize = 0
667 HAL_StatusTypeDef HAL_CEC_Receive_IT(CEC_HandleTypeDef *hcec, uint8_t *pData)
669 uint32_t tmp_state = 0;
670 tmp_state = hcec->State;
671 if((tmp_state == HAL_CEC_STATE_READY) || (tmp_state == HAL_CEC_STATE_BUSY_TX))
678 /* When a ping is received, RxXferSize is 0 */
679 /* When a message is received, RxXferSize contains the number of received bytes */
680 hcec->RxXferSize = CEC_RXXFERSIZE_INITIALIZE;
685 hcec->pRxBuffPtr = pData;
686 hcec->ErrorCode = HAL_CEC_ERROR_NONE;
688 /* Process Unlocked */
691 /* Check if a transmit process is ongoing or not */
692 if(hcec->State == HAL_CEC_STATE_BUSY_TX)
694 hcec->State = HAL_CEC_STATE_BUSY_TX_RX;
698 hcec->State = HAL_CEC_STATE_BUSY_RX;
700 /* Enable CEC interrupt */
701 __HAL_CEC_ENABLE_IT(hcec, CEC_IT_IE);
713 * @brief Get size of the received frame.
714 * @param hcec: CEC handle
717 uint32_t HAL_CEC_GetReceivedFrameSize(CEC_HandleTypeDef *hcec)
719 return hcec->RxXferSize;
723 * @brief This function handles CEC interrupt requests.
724 * @param hcec: CEC handle
727 void HAL_CEC_IRQHandler(CEC_HandleTypeDef *hcec)
729 /* Save error status register for further error handling purposes */
730 hcec->ErrorCode = READ_BIT(hcec->Instance->ESR, CEC_ESR_ALL_ERROR);
733 if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TERR) != RESET))
735 /* Acknowledgement of the error */
736 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_TERR);
738 /* Check if a receive process is ongoing or not */
739 if(hcec->State == HAL_CEC_STATE_BUSY_TX_RX)
741 /* Interrupts are not disabled due to reception still ongoing */
743 hcec->State = HAL_CEC_STATE_BUSY_RX;
747 /* Disable the CEC Transmission Interrupts */
748 __HAL_CEC_DISABLE_IT(hcec, CEC_IT_IE);
750 hcec->State = HAL_CEC_STATE_READY;
755 if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RERR) != RESET))
757 /* Acknowledgement of the error */
758 __HAL_CEC_CLEAR_FLAG(hcec, CEC_FLAG_RERR);
760 /* Check if a transmit process is ongoing or not */
761 if(hcec->State == HAL_CEC_STATE_BUSY_TX_RX)
763 /* Interrupts are not disabled due to reception still ongoing */
765 hcec->State = HAL_CEC_STATE_BUSY_TX;
769 /* Disable the CEC Transmission Interrupts */
770 __HAL_CEC_DISABLE_IT(hcec, CEC_IT_IE);
772 hcec->State = HAL_CEC_STATE_READY;
776 if ((hcec->ErrorCode & CEC_ESR_ALL_ERROR) != 0)
778 HAL_CEC_ErrorCallback(hcec);
781 /* Transmit byte request or block transfer finished */
782 if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_TBTRF) != RESET))
784 CEC_Transmit_IT(hcec);
787 /* Receive byte or block transfer finished */
788 if((__HAL_CEC_GET_FLAG(hcec, CEC_FLAG_RBTF) != RESET))
790 CEC_Receive_IT(hcec);
796 * @brief Tx Transfer completed callback
797 * @param hcec: CEC handle
800 __weak void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
802 /* NOTE : This function should not be modified, when the callback is needed,
803 the HAL_CEC_TxCpltCallback can be implemented in the user file
808 * @brief Rx Transfer completed callback
809 * @param hcec: CEC handle
812 __weak void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec)
814 /* NOTE : This function should not be modified, when the callback is needed,
815 the HAL_CEC_RxCpltCallback can be implemented in the user file
820 * @brief CEC error callbacks
821 * @param hcec: CEC handle
824 __weak void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
826 /* NOTE : This function should not be modified, when the callback is needed,
827 the HAL_CEC_ErrorCallback can be implemented in the user file
835 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control functions
836 * @brief CEC control functions
839 ===============================================================================
840 ##### Peripheral Control functions #####
841 ===============================================================================
843 This subsection provides a set of functions allowing to control the CEC.
844 (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
845 (+) HAL_CEC_GetError() API can be helpful to get the error code of a failed transmission or reception.
851 * @brief return the CEC state
852 * @param hcec: CEC handle
855 HAL_CEC_StateTypeDef HAL_CEC_GetState(CEC_HandleTypeDef *hcec)
861 * @brief Return the CEC error code
862 * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
863 * the configuration information for the specified CEC.
864 * @retval CEC Error Code
866 uint32_t HAL_CEC_GetError(CEC_HandleTypeDef *hcec)
868 return hcec->ErrorCode;
879 /** @addtogroup CEC_Private_Functions
884 * @brief Send data in interrupt mode
885 * @param hcec: CEC handle.
886 * Function called under interruption only, once
887 * interruptions have been enabled by HAL_CEC_Transmit_IT()
890 static HAL_StatusTypeDef CEC_Transmit_IT(CEC_HandleTypeDef *hcec)
892 uint32_t tmp_state = 0;
894 tmp_state = hcec->State;
895 /* if the IP is already busy or if there is a previous transmission
896 already pending due to arbitration loss */
897 if(((tmp_state == HAL_CEC_STATE_BUSY_TX) || (tmp_state == HAL_CEC_STATE_BUSY_TX_RX))
898 || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec) != RESET))
900 /* if all data have been sent */
901 if(hcec->TxXferCount == 0)
903 /* Acknowledge successful completion by writing 0x00 */
904 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00);
906 /* Check if a receive process is ongoing or not */
907 if(hcec->State == HAL_CEC_STATE_BUSY_TX_RX)
909 /* Interrupts are not disabled due to reception still ongoing */
911 hcec->State = HAL_CEC_STATE_BUSY_RX;
915 /* Disable the CEC Transmission Interrupts */
916 __HAL_CEC_DISABLE_IT(hcec, CEC_IT_IE);
918 hcec->State = HAL_CEC_STATE_READY;
921 HAL_CEC_TxCpltCallback(hcec);
927 /* Reduce the number of bytes to transfer by one */
930 /* Write data to TX buffer*/
931 hcec->Instance->TXD = *hcec->pTxBuffPtr++;
933 /* If this is the last byte of the ongoing transmission */
934 if (hcec->TxXferCount == 0)
936 /* Acknowledge byte request and signal end of message */
937 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, CEC_FLAG_TEOM);
941 /* Acknowledge byte request by writing 0x00 */
942 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_TRANSMIT_MASK, 0x00);
955 * @brief Receive data in interrupt mode.
956 * @param hcec: CEC handle.
957 * Function called under interruption only, once
958 * interruptions have been enabled by HAL_CEC_Receive_IT()
961 static HAL_StatusTypeDef CEC_Receive_IT(CEC_HandleTypeDef *hcec)
963 static uint32_t temp;
964 uint32_t tmp_state = 0;
966 tmp_state = hcec->State;
967 if((tmp_state == HAL_CEC_STATE_BUSY_RX) || (tmp_state == HAL_CEC_STATE_BUSY_TX_RX))
969 temp = hcec->Instance->CSR;
971 /* Store received data */
972 *hcec->pRxBuffPtr++ = hcec->Instance->RXD;
974 /* Acknowledge received byte by writing 0x00 */
975 MODIFY_REG(hcec->Instance->CSR, CEC_FLAG_RECEIVE_MASK, 0x00);
977 /* Increment the number of received data */
978 if(hcec->RxXferSize == CEC_RXXFERSIZE_INITIALIZE)
980 hcec->RxXferSize = 0;
987 /* If the End Of Message is reached */
988 if(HAL_IS_BIT_SET(temp, CEC_FLAG_REOM))
990 if(hcec->State == HAL_CEC_STATE_BUSY_TX_RX)
992 /* Interrupts are not disabled due to transmission still ongoing */
994 hcec->State = HAL_CEC_STATE_BUSY_TX;
998 /* Disable the CEC Transmission Interrupts */
999 __HAL_CEC_DISABLE_IT(hcec, CEC_IT_IE);
1001 hcec->State = HAL_CEC_STATE_READY;
1004 HAL_CEC_RxCpltCallback(hcec);
1027 #endif /* defined(STM32F100xB) || defined(STM32F100xE) */
1029 #endif /* HAL_CEC_MODULE_ENABLED */
1034 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/