2 ******************************************************************************
3 * @file stm32f0xx_hal_cec.c
4 * @author MCD Application Team
6 * @date 11-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 function
12 * + IO operation function
13 * + Peripheral Control function
16 ===============================================================================
17 ##### How to use this driver #####
18 ===============================================================================
20 The CEC HAL driver can be used as follows:
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.
33 (#) Program the Signal Free Time (SFT) and SFT option, Tolerance, reception stop in
34 in case of Bit Rising Error, Error-Bit generation conditions, device logical
35 address and Listen mode in the hcec Init structure.
37 (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
38 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
39 by calling the customed HAL_CEC_MspInit() API.
41 -@@- The specific CEC interrupts (Transmission complete interrupt,
42 RXNE interrupt and Error Interrupts) will be managed using the macros
43 __HAL_CEC_ENABLE_IT() and __HAL_CEC_DISABLE_IT() inside the transmit
47 ******************************************************************************
50 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
52 * Redistribution and use in source and binary forms, with or without modification,
53 * are permitted provided that the following conditions are met:
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright notice,
57 * this list of conditions and the following disclaimer in the documentation
58 * and/or other materials provided with the distribution.
59 * 3. Neither the name of STMicroelectronics nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
64 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
69 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
70 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
71 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
72 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 ******************************************************************************
77 /* Includes ------------------------------------------------------------------*/
78 #include "stm32f0xx_hal.h"
80 #ifdef HAL_CEC_MODULE_ENABLED
82 #if defined(STM32F042x6) || defined(STM32F048xx) ||\
83 defined(STM32F051x8) || defined(STM32F058xx) ||\
84 defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) ||\
85 defined(STM32F091xC) || defined (STM32F098xx)
87 /** @addtogroup STM32F0xx_HAL_Driver
91 /** @defgroup CEC CEC HAL Module Driver
92 * @brief HAL CEC module driver
96 /* Private typedef -----------------------------------------------------------*/
97 /* Private define ------------------------------------------------------------*/
98 /** @defgroup CEC_Private_Constants CEC Private Constants
101 #define CEC_CFGR_FIELDS (CEC_CFGR_SFT | CEC_CFGR_RXTOL | CEC_CFGR_BRESTP \
102 | CEC_CFGR_BREGEN | CEC_CFGR_LBPEGEN | CEC_CFGR_SFTOPT \
103 | CEC_CFGR_BRDNOGEN | CEC_CFGR_OAR | CEC_CFGR_LSTN)
107 /* Private macro -------------------------------------------------------------*/
108 /* Private variables ---------------------------------------------------------*/
109 /* Private function prototypes -----------------------------------------------*/
110 /** @defgroup CEC_Private_Functions CEC Private Functions
113 static HAL_StatusTypeDef
CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
);
114 static HAL_StatusTypeDef
CEC_Receive_IT(CEC_HandleTypeDef
*hcec
);
118 /* Exported functions ---------------------------------------------------------*/
120 /** @defgroup CEC_Exported_Functions CEC Exported Functions
124 /** @defgroup CEC_Exported_Functions_Group1 Initialization/de-initialization function
125 * @brief Initialization and Configuration functions
128 ===============================================================================
129 ##### Initialization and Configuration functions #####
130 ===============================================================================
132 This subsection provides a set of functions allowing to initialize the CEC
133 (+) The following parameters need to be configured:
136 (++) BRERxStop (RX stopped or not upon Bit Rising Error)
137 (++) BREErrorBitGen (Error-Bit generation in case of Bit Rising Error)
138 (++) LBPEErrorBitGen (Error-Bit generation in case of Long Bit Period Error)
139 (++) BroadcastMsgNoErrorBitGen (Error-bit generation in case of broadcast message error)
140 (++) SignalFreeTimeOption (SFT Timer start definition)
141 (++) OwnAddress (CEC device address)
149 * @brief Initializes the CEC mode according to the specified
150 * parameters in the CEC_InitTypeDef and creates the associated handle .
151 * @param hcec: CEC handle
154 HAL_StatusTypeDef
HAL_CEC_Init(CEC_HandleTypeDef
*hcec
)
156 uint32_t tmpreg
= 0x0;
158 /* Check the CEC handle allocation */
164 /* Check the parameters */
165 assert_param(IS_CEC_SIGNALFREETIME(hcec
->Init
.SignalFreeTime
));
166 assert_param(IS_CEC_TOLERANCE(hcec
->Init
.Tolerance
));
167 assert_param(IS_CEC_BRERXSTOP(hcec
->Init
.BRERxStop
));
168 assert_param(IS_CEC_BREERRORBITGEN(hcec
->Init
.BREErrorBitGen
));
169 assert_param(IS_CEC_LBPEERRORBITGEN(hcec
->Init
.LBPEErrorBitGen
));
170 assert_param(IS_CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION(hcec
->Init
.BroadcastMsgNoErrorBitGen
));
171 assert_param(IS_CEC_SFTOP(hcec
->Init
.SignalFreeTimeOption
));
172 assert_param(IS_CEC_OAR_ADDRESS(hcec
->Init
.OwnAddress
));
173 assert_param(IS_CEC_LISTENING_MODE(hcec
->Init
.ListenMode
));
174 assert_param(IS_CEC_ADDRESS(hcec
->Init
.InitiatorAddress
));
177 if(hcec
->State
== HAL_CEC_STATE_RESET
)
179 /* Init the low level hardware : GPIO, CLOCK */
180 HAL_CEC_MspInit(hcec
);
183 hcec
->State
= HAL_CEC_STATE_BUSY
;
185 /* Disable the Peripheral */
186 __HAL_CEC_DISABLE(hcec
);
188 tmpreg
= hcec
->Init
.SignalFreeTime
;
189 tmpreg
|= hcec
->Init
.Tolerance
;
190 tmpreg
|= hcec
->Init
.BRERxStop
;
191 tmpreg
|= hcec
->Init
.BREErrorBitGen
;
192 tmpreg
|= hcec
->Init
.LBPEErrorBitGen
;
193 tmpreg
|= hcec
->Init
.BroadcastMsgNoErrorBitGen
;
194 tmpreg
|= hcec
->Init
.SignalFreeTimeOption
;
195 tmpreg
|= (hcec
->Init
.OwnAddress
<< CEC_CFGR_OAR_LSB_POS
);
196 tmpreg
|= hcec
->Init
.ListenMode
;
198 /* Write to CEC Control Register */
199 MODIFY_REG(hcec
->Instance
->CFGR
, CEC_CFGR_FIELDS
, tmpreg
);
201 /* Enable the Peripheral */
202 __HAL_CEC_ENABLE(hcec
);
204 hcec
->State
= HAL_CEC_STATE_READY
;
212 * @brief DeInitializes the CEC peripheral
213 * @param hcec: CEC handle
216 HAL_StatusTypeDef
HAL_CEC_DeInit(CEC_HandleTypeDef
*hcec
)
218 /* Check the CEC handle allocation */
224 /* Check the parameters */
225 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
227 hcec
->State
= HAL_CEC_STATE_BUSY
;
229 /* DeInit the low level hardware */
230 HAL_CEC_MspDeInit(hcec
);
231 /* Disable the Peripheral */
232 __HAL_CEC_DISABLE(hcec
);
234 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
235 hcec
->State
= HAL_CEC_STATE_RESET
;
244 * @brief CEC MSP Init
245 * @param hcec: CEC handle
248 __weak
void HAL_CEC_MspInit(CEC_HandleTypeDef
*hcec
)
250 /* NOTE : This function should not be modified, when the callback is needed,
251 the HAL_CEC_MspInit can be implemented in the user file
256 * @brief CEC MSP DeInit
257 * @param hcec: CEC handle
260 __weak
void HAL_CEC_MspDeInit(CEC_HandleTypeDef
*hcec
)
262 /* NOTE : This function should not be modified, when the callback is needed,
263 the HAL_CEC_MspDeInit can be implemented in the user file
271 /** @defgroup CEC_Exported_Functions_Group2 IO operation function
272 * @brief CEC Transmit/Receive functions
275 ===============================================================================
276 ##### IO operation function #####
277 ===============================================================================
278 This subsection provides a set of functions allowing to manage the CEC data transfers.
280 (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
281 logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
283 (#) There are two mode of transfer:
284 (+) Blocking mode: The communication is performed in polling mode.
285 The HAL status of all data processing is returned by the same function
286 after finishing transfer.
287 (+) Non Blocking mode: The communication is performed using Interrupts.
288 These API's return the HAL status.
289 The end of the data processing will be indicated through the
290 dedicated CEC IRQ when using Interrupt mode.
291 The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
292 will be executed respectivelly at the end of the transmit or Receive process
293 The HAL_CEC_ErrorCallback()user callback will be executed when a communication
296 (#) Blocking mode API s are :
297 (+) HAL_CEC_Transmit()
298 (+) HAL_CEC_Receive()
300 (#) Non-Blocking mode API s with Interrupt are :
301 (+) HAL_CEC_Transmit_IT()
302 (+) HAL_CEC_Receive_IT()
303 (+) HAL_CEC_IRQHandler()
305 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
306 (+) HAL_CEC_TxCpltCallback()
307 (+) HAL_CEC_RxCpltCallback()
308 (+) HAL_CEC_ErrorCallback()
315 * @brief Send data in blocking mode
316 * @param hcec: CEC handle
317 * @param DestinationAddress: destination logical address
318 * @param pData: pointer to input byte data buffer
319 * @param Size: amount of data to be sent in bytes (without counting the header).
320 * 0 means only the header is sent (ping operation).
321 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
322 * @param Timeout: Timeout duration.
325 HAL_StatusTypeDef
HAL_CEC_Transmit(CEC_HandleTypeDef
*hcec
, uint8_t DestinationAddress
, uint8_t *pData
, uint32_t Size
, uint32_t Timeout
)
328 uint32_t tempisr
= 0;
329 uint32_t tickstart
= 0;
331 if((hcec
->State
== HAL_CEC_STATE_READY
) && (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec
) == RESET
))
333 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
334 if((pData
== NULL
) && (Size
> 0))
336 hcec
->State
= HAL_CEC_STATE_ERROR
;
340 assert_param(IS_CEC_ADDRESS(DestinationAddress
));
341 assert_param(IS_CEC_MSGSIZE(Size
));
346 hcec
->State
= HAL_CEC_STATE_BUSY_TX
;
348 hcec
->TxXferCount
= Size
;
350 /* case no data to be sent, sender is only pinging the system */
353 /* Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
354 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
357 /* send header block */
358 temp
= ((uint32_t)hcec
->Init
.InitiatorAddress
<< CEC_INITIATOR_LSB_POS
) | DestinationAddress
;
359 hcec
->Instance
->TXDR
= temp
;
360 /* Set TX Start of Message (TXSOM) bit */
361 __HAL_CEC_FIRST_BYTE_TX_SET(hcec
);
363 while (hcec
->TxXferCount
> 0)
367 tickstart
= HAL_GetTick();
368 while(HAL_IS_BIT_CLR(hcec
->Instance
->ISR
, CEC_ISR_TXBR
))
370 if(Timeout
!= HAL_MAX_DELAY
)
372 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
374 hcec
->State
= HAL_CEC_STATE_READY
;
375 /* Process Unlocked */
381 /* check whether error occured while waiting for TXBR to be set:
382 * has Tx underrun occurred ?
383 * has Tx error occurred ?
384 * has Tx Missing Acknowledge error occurred ?
385 * has Arbitration Loss error occurred ? */
386 tempisr
= hcec
->Instance
->ISR
;
387 if ((tempisr
& (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
|CEC_ISR_ARBLST
)) != 0)
389 /* copy ISR for error handling purposes */
390 hcec
->ErrorCode
= tempisr
;
391 /* clear all error flags by default */
392 __HAL_CEC_CLEAR_FLAG(hcec
, (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
|CEC_ISR_ARBLST
));
393 hcec
->State
= HAL_CEC_STATE_ERROR
;
398 /* TXBR to clear BEFORE writing TXDR register */
399 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_TXBR
);
400 if (hcec
->TxXferCount
== 0)
402 /* if last byte transmission, set TX End of Message (TXEOM) bit */
403 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
405 hcec
->Instance
->TXDR
= *pData
++;
407 /* error check after TX byte write up */
408 tempisr
= hcec
->Instance
->ISR
;
409 if ((tempisr
& (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
|CEC_ISR_ARBLST
)) != 0)
411 /* copy ISR for error handling purposes */
412 hcec
->ErrorCode
= tempisr
;
413 /* clear all error flags by default */
414 __HAL_CEC_CLEAR_FLAG(hcec
, (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
|CEC_ISR_ARBLST
));
415 hcec
->State
= HAL_CEC_STATE_ERROR
;
419 } /* end while (while (hcec->TxXferCount > 0)) */
422 /* if no error up to this point, check that transmission is
423 * complete, that is wait until TXEOM is reset */
424 tickstart
= HAL_GetTick();
426 while (HAL_IS_BIT_SET(hcec
->Instance
->CR
, CEC_CR_TXEOM
))
428 if(Timeout
!= HAL_MAX_DELAY
)
430 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
432 hcec
->State
= HAL_CEC_STATE_ERROR
;
439 /* Final error check once all bytes have been transmitted */
440 tempisr
= hcec
->Instance
->ISR
;
441 if ((tempisr
& (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
)) != 0)
443 /* copy ISR for error handling purposes */
444 hcec
->ErrorCode
= tempisr
;
445 /* clear all error flags by default */
446 __HAL_CEC_CLEAR_FLAG(hcec
, (CEC_ISR_TXUDR
|CEC_ISR_TXERR
|CEC_ISR_TXACKE
));
447 hcec
->State
= HAL_CEC_STATE_ERROR
;
452 hcec
->State
= HAL_CEC_STATE_READY
;
464 * @brief Receive data in blocking mode. Must be invoked when RXBR has been set.
465 * @param hcec: CEC handle
466 * @param pData: pointer to received data buffer.
467 * @param Timeout: Timeout duration.
468 * Note that the received data size is not known beforehand, the latter is known
469 * when the reception is complete and is stored in hcec->RxXferSize.
470 * hcec->RxXferSize is the sum of opcodes + operands (0 to 14 operands max).
471 * If only a header is received, hcec->RxXferSize = 0
474 HAL_StatusTypeDef
HAL_CEC_Receive(CEC_HandleTypeDef
*hcec
, uint8_t *pData
, uint32_t Timeout
)
477 uint32_t tickstart
= 0;
479 if (hcec
->State
== HAL_CEC_STATE_READY
)
481 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
484 hcec
->State
= HAL_CEC_STATE_ERROR
;
488 hcec
->RxXferSize
= 0;
493 /* Rx loop until CEC_ISR_RXEND is set */
494 while (HAL_IS_BIT_CLR(hcec
->Instance
->ISR
, CEC_ISR_RXEND
))
496 tickstart
= HAL_GetTick();
497 /* Wait for next byte to be received */
498 while (HAL_IS_BIT_CLR(hcec
->Instance
->ISR
, CEC_ISR_RXBR
))
500 if(Timeout
!= HAL_MAX_DELAY
)
502 if((Timeout
== 0) || ((HAL_GetTick() - tickstart
) > Timeout
))
504 hcec
->State
= HAL_CEC_STATE_READY
;
509 /* any error so far ?
510 * has Rx Missing Acknowledge occurred ?
511 * has Rx Long Bit Period error occurred ?
512 * has Rx Short Bit Period error occurred ?
513 * has Rx Bit Rising error occurred ?
514 * has Rx Overrun error occurred ? */
515 temp
= (uint32_t) (hcec
->Instance
->ISR
);
516 if ((temp
& (CEC_ISR_RXACKE
|CEC_ISR_LBPE
|CEC_ISR_SBPE
|CEC_ISR_BRE
|CEC_ISR_RXOVR
)) != 0)
518 /* copy ISR for error handling purposes */
519 hcec
->ErrorCode
= temp
;
520 /* clear all error flags by default */
521 __HAL_CEC_CLEAR_FLAG(hcec
, (CEC_ISR_RXACKE
|CEC_ISR_LBPE
|CEC_ISR_SBPE
|CEC_ISR_BRE
|CEC_ISR_RXOVR
));
522 hcec
->State
= HAL_CEC_STATE_ERROR
;
526 } /* while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_ISR_RXBR)) */
529 /* read received data */
530 *pData
++ = hcec
->Instance
->RXDR
;
531 temp
= (uint32_t) (hcec
->Instance
->ISR
);
532 /* end of message ? */
533 if ((temp
& CEC_ISR_RXEND
) != 0)
535 assert_param(IS_CEC_MSGSIZE(hcec
->RxXferSize
));
536 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_RXEND
);
537 hcec
->State
= HAL_CEC_STATE_READY
;
542 /* clear Rx-Byte Received flag */
543 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_RXBR
);
544 /* increment payload byte counter */
546 } /* while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_ISR_RXEND)) */
548 /* if the instructions below are executed, it means RXEND was set when RXBR was
549 * set for the first time:
550 * the code within the "while (HAL_IS_BIT_CLR(hcec->Instance->ISR, CEC_ISR_RXEND))"
551 * loop has not been executed and this means a single byte has been sent */
552 *pData
++ = hcec
->Instance
->RXDR
;
553 /* only one header is received: RxXferSize is set to 0 (no operand, no opcode) */
554 hcec
->RxXferSize
= 0;
555 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_RXEND
);
557 hcec
->State
= HAL_CEC_STATE_READY
;
569 * @brief Send data in interrupt mode
570 * @param hcec: CEC handle
571 * @param DestinationAddress: destination logical address
572 * @param pData: pointer to input byte data buffer
573 * @param Size: amount of data to be sent in bytes (without counting the header).
574 * 0 means only the header is sent (ping operation).
575 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
578 HAL_StatusTypeDef
HAL_CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
, uint8_t DestinationAddress
, uint8_t *pData
, uint32_t Size
)
581 /* if the IP isn't already busy and if there is no previous transmission
582 already pending due to arbitration lost */
583 if (((hcec
->State
== HAL_CEC_STATE_READY
) || (hcec
->State
== HAL_CEC_STATE_STANDBY_RX
))
584 && (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec
) == RESET
))
586 if((pData
== NULL
) && (Size
> 0))
588 hcec
->State
= HAL_CEC_STATE_ERROR
;
592 assert_param(IS_CEC_ADDRESS(DestinationAddress
));
593 assert_param(IS_CEC_MSGSIZE(Size
));
597 hcec
->pTxBuffPtr
= pData
;
598 hcec
->State
= HAL_CEC_STATE_BUSY_TX
;
599 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
601 /* Disable Peripheral to write CEC_IER register */
602 __HAL_CEC_DISABLE(hcec
);
604 /* Enable the following two CEC Transmission interrupts as
605 * well as the following CEC Transmission Errors interrupts:
607 * End of Transmission IT
608 * Tx Missing Acknowledge IT
610 * Tx-Buffer Underrun IT
611 * Tx arbitration lost */
612 __HAL_CEC_ENABLE_IT(hcec
, CEC_IER_TXBRIE
|CEC_IER_TXENDIE
|CEC_IER_TX_ALL_ERR
);
614 /* Enable the Peripheral */
615 __HAL_CEC_ENABLE(hcec
);
617 /* initialize the number of bytes to send,
618 * 0 means only one header is sent (ping operation) */
619 hcec
->TxXferCount
= Size
;
621 /* Process Unlocked */
624 /* in case of no payload (Size = 0), sender is only pinging the system;
625 * Set TX End of Message (TXEOM) bit, must be set before writing data to TXDR */
628 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
631 /* send header block */
632 temp
= ((uint32_t)hcec
->Init
.InitiatorAddress
<< CEC_INITIATOR_LSB_POS
) | DestinationAddress
;
633 hcec
->Instance
->TXDR
= temp
;
634 /* Set TX Start of Message (TXSOM) bit */
635 __HAL_CEC_FIRST_BYTE_TX_SET(hcec
);
639 /* if the IP is already busy or if there is a previous transmission
640 already pending due to arbitration loss */
641 else if ((hcec
->State
== HAL_CEC_STATE_BUSY_TX
)
642 || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec
) != RESET
))
645 /* set state to BUSY TX, in case it wasn't set already (case
646 * of transmission new attempt after arbitration loss) */
647 if (hcec
->State
!= HAL_CEC_STATE_BUSY_TX
)
649 hcec
->State
= HAL_CEC_STATE_BUSY_TX
;
652 /* if all data have been sent */
653 if(hcec
->TxXferCount
== 0)
655 /* Disable Peripheral to write CEC_IER register */
656 __HAL_CEC_DISABLE(hcec
);
658 /* Disable the CEC Transmission Interrupts */
659 __HAL_CEC_DISABLE_IT(hcec
, CEC_IER_TXBRIE
|CEC_IER_TXENDIE
);
660 /* Disable the CEC Transmission Error Interrupts */
661 __HAL_CEC_DISABLE_IT(hcec
, CEC_IER_TX_ALL_ERR
);
663 /* Enable the Peripheral */
664 __HAL_CEC_ENABLE(hcec
);
666 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_TXBR
|CEC_ISR_TXEND
);
668 hcec
->State
= HAL_CEC_STATE_READY
;
669 /* Call the Process Unlocked before calling the Tx call back API to give the possibility to
670 start again the Transmission under the Tx call back API */
673 HAL_CEC_TxCpltCallback(hcec
);
679 if (hcec
->TxXferCount
== 1)
681 /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
682 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
684 /* clear Tx-Byte request flag */
685 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_TXBR
);
686 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
689 /* Process Unlocked */
703 * @brief Receive data in interrupt mode.
704 * @param hcec: CEC handle
705 * @param pData: pointer to received data buffer.
706 * Note that the received data size is not known beforehand, the latter is known
707 * when the reception is complete and is stored in hcec->RxXferSize.
708 * hcec->RxXferSize is the sum of opcodes + operands (0 to 14 operands max).
709 * If only a header is received, hcec->RxXferSize = 0
712 HAL_StatusTypeDef
HAL_CEC_Receive_IT(CEC_HandleTypeDef
*hcec
, uint8_t *pData
)
714 if(hcec
->State
== HAL_CEC_STATE_READY
)
718 hcec
->State
= HAL_CEC_STATE_ERROR
;
724 hcec
->RxXferSize
= 0;
725 hcec
->pRxBuffPtr
= pData
;
726 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
727 /* the IP is moving to a ready to receive state */
728 hcec
->State
= HAL_CEC_STATE_STANDBY_RX
;
730 /* Disable Peripheral to write CEC_IER register */
731 __HAL_CEC_DISABLE(hcec
);
733 /* Enable the following CEC Reception Error Interrupts:
735 * Rx bit rising error
736 * Rx short bit period error
737 * Rx long bit period error
738 * Rx missing acknowledge */
739 __HAL_CEC_ENABLE_IT(hcec
, CEC_IER_RX_ALL_ERR
);
741 /* Process Unlocked */
744 /* Enable the following two CEC Reception interrupts:
745 * Rx Byte Received IT
746 * End of Reception IT */
747 __HAL_CEC_ENABLE_IT(hcec
, CEC_IER_RXBRIE
|CEC_IER_RXENDIE
);
749 __HAL_CEC_ENABLE(hcec
);
762 * @brief This function handles CEC interrupt requests.
763 * @param hcec: CEC handle
766 void HAL_CEC_IRQHandler(CEC_HandleTypeDef
*hcec
)
768 /* save interrupts register for further error or interrupts handling purposes */
769 hcec
->ErrorCode
= hcec
->Instance
->ISR
;
770 /* CEC TX missing acknowledge error interrupt occurred -------------------------------------*/
771 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_TXACKE
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_TXACKEIE
) != RESET
))
773 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_TXACKE
);
774 hcec
->State
= HAL_CEC_STATE_ERROR
;
777 /* CEC transmit error interrupt occured --------------------------------------*/
778 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_TXERR
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_TXERRIE
) != RESET
))
780 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_TXERR
);
781 hcec
->State
= HAL_CEC_STATE_ERROR
;
784 /* CEC TX underrun error interrupt occured --------------------------------------*/
785 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_TXUDR
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_TXUDRIE
) != RESET
))
787 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_TXUDR
);
788 hcec
->State
= HAL_CEC_STATE_ERROR
;
791 /* CEC TX arbitration error interrupt occured --------------------------------------*/
792 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_ARBLST
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_ARBLSTIE
) != RESET
))
794 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_ARBLST
);
795 hcec
->State
= HAL_CEC_STATE_ERROR
;
798 /* CEC RX overrun error interrupt occured --------------------------------------*/
799 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_RXOVR
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_RXOVRIE
) != RESET
))
801 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_RXOVR
);
802 hcec
->State
= HAL_CEC_STATE_ERROR
;
805 /* CEC RX bit rising error interrupt occured --------------------------------------*/
806 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_BRE
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_BREIE
) != RESET
))
808 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_BRE
);
809 hcec
->State
= HAL_CEC_STATE_ERROR
;
812 /* CEC RX short bit period error interrupt occured --------------------------------------*/
813 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_SBPE
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_SBPEIE
) != RESET
))
815 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_SBPE
);
816 hcec
->State
= HAL_CEC_STATE_ERROR
;
819 /* CEC RX long bit period error interrupt occured --------------------------------------*/
820 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_LBPE
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_LBPEIE
) != RESET
))
822 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_LBPE
);
823 hcec
->State
= HAL_CEC_STATE_ERROR
;
826 /* CEC RX missing acknowledge error interrupt occured --------------------------------------*/
827 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_RXACKE
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_RXACKEIE
) != RESET
))
829 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_RXACKE
);
830 hcec
->State
= HAL_CEC_STATE_ERROR
;
833 if ((hcec
->ErrorCode
& CEC_ISR_ALL_ERROR
) != 0)
835 HAL_CEC_ErrorCallback(hcec
);
838 /* CEC RX byte received interrupt ---------------------------------------------------*/
839 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_RXBR
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_RXBRIE
) != RESET
))
841 /* RXBR IT is cleared during HAL_CEC_Transmit_IT processing */
842 CEC_Receive_IT(hcec
);
845 /* CEC RX end received interrupt ---------------------------------------------------*/
846 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_RXEND
) != RESET
) && (__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_RXENDIE
) != RESET
))
848 /* RXBR IT is cleared during HAL_CEC_Transmit_IT processing */
849 CEC_Receive_IT(hcec
);
853 /* CEC TX byte request interrupt ------------------------------------------------*/
854 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_TXBR
) != RESET
) &&(__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_TXBRIE
) != RESET
))
856 /* TXBR IT is cleared during HAL_CEC_Transmit_IT processing */
857 CEC_Transmit_IT(hcec
);
860 /* CEC TX end interrupt ------------------------------------------------*/
861 if((__HAL_CEC_GET_IT(hcec
, CEC_ISR_TXEND
) != RESET
) &&(__HAL_CEC_GET_IT_SOURCE(hcec
, CEC_IER_TXENDIE
) != RESET
))
863 /* TXEND IT is cleared during HAL_CEC_Transmit_IT processing */
864 CEC_Transmit_IT(hcec
);
871 * @brief Tx Transfer completed callback
872 * @param hcec: CEC handle
875 __weak
void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef
*hcec
)
877 /* NOTE : This function should not be modified, when the callback is needed,
878 the HAL_CEC_TxCpltCallback can be implemented in the user file
883 * @brief Rx Transfer completed callback
884 * @param hcec: CEC handle
887 __weak
void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef
*hcec
)
889 /* NOTE : This function should not be modified, when the callback is needed,
890 the HAL_CEC_TxCpltCallback can be implemented in the user file
895 * @brief CEC error callbacks
896 * @param hcec: CEC handle
899 __weak
void HAL_CEC_ErrorCallback(CEC_HandleTypeDef
*hcec
)
901 /* NOTE : This function should not be modified, when the callback is needed,
902 the HAL_CEC_ErrorCallback can be implemented in the user file
910 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control function
911 * @brief CEC control functions
914 ===============================================================================
915 ##### Peripheral Control function #####
916 ===============================================================================
918 This subsection provides a set of functions allowing to control the CEC.
919 (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
925 * @brief return the CEC state
926 * @param hcec: CEC handle
929 HAL_CEC_StateTypeDef
HAL_CEC_GetState(CEC_HandleTypeDef
*hcec
)
935 * @brief Return the CEC error code
936 * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
937 * the configuration information for the specified CEC.
938 * @retval CEC Error Code
940 uint32_t HAL_CEC_GetError(CEC_HandleTypeDef
*hcec
)
942 return hcec
->ErrorCode
;
953 /** @defgroup CEC_Private_Functions CEC Private Functions
958 * @brief Send data in interrupt mode
959 * @param hcec: CEC handle.
960 * Function called under interruption only, once
961 * interruptions have been enabled by HAL_CEC_Transmit_IT()
964 static HAL_StatusTypeDef
CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
)
966 /* if the IP is already busy or if there is a previous transmission
967 already pending due to arbitration loss */
968 if ((hcec
->State
== HAL_CEC_STATE_BUSY_TX
)
969 || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec
) != RESET
))
972 /* set state to BUSY TX, in case it wasn't set already (case
973 * of transmission new attempt after arbitration loss) */
974 if (hcec
->State
!= HAL_CEC_STATE_BUSY_TX
)
976 hcec
->State
= HAL_CEC_STATE_BUSY_TX
;
979 /* if all data have been sent */
980 if(hcec
->TxXferCount
== 0)
982 /* Disable Peripheral to write CEC_IER register */
983 __HAL_CEC_DISABLE(hcec
);
985 /* Disable the CEC Transmission Interrupts */
986 __HAL_CEC_DISABLE_IT(hcec
, CEC_IER_TXBRIE
|CEC_IER_TXENDIE
);
987 /* Disable the CEC Transmission Error Interrupts */
988 __HAL_CEC_DISABLE_IT(hcec
, CEC_IER_TX_ALL_ERR
);
990 /* Enable the Peripheral */
991 __HAL_CEC_ENABLE(hcec
);
993 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_TXBR
|CEC_ISR_TXEND
);
995 /* If RX interruptions are enabled, return to HAL_CEC_STATE_STANDBY_RX state */
996 if (__HAL_CEC_GET_IT_SOURCE(hcec
, (CEC_IER_RXBRIE
|CEC_IER_RXENDIE
) ) != RESET
)
998 hcec
->State
= HAL_CEC_STATE_STANDBY_RX
;
1002 hcec
->State
= HAL_CEC_STATE_READY
;
1005 HAL_CEC_TxCpltCallback(hcec
);
1011 if (hcec
->TxXferCount
== 1)
1013 /* if this is the last byte transmission, set TX End of Message (TXEOM) bit */
1014 __HAL_CEC_LAST_BYTE_TX_SET(hcec
);
1016 /* clear Tx-Byte request flag */
1017 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_TXBR
);
1018 hcec
->Instance
->TXDR
= *hcec
->pTxBuffPtr
++;
1019 hcec
->TxXferCount
--;
1032 * @brief Receive data in interrupt mode.
1033 * @param hcec: CEC handle.
1034 * Function called under interruption only, once
1035 * interruptions have been enabled by HAL_CEC_Receive_IT()
1036 * @retval HAL status
1038 static HAL_StatusTypeDef
CEC_Receive_IT(CEC_HandleTypeDef
*hcec
)
1042 /* Three different conditions are tested to carry out the RX IT processing:
1043 * - the IP is in reception stand-by (the IP state is HAL_CEC_STATE_STANDBY_RX) and
1044 * the reception of the first byte is starting
1045 * - a message reception is already on-going (the IP state is HAL_CEC_STATE_BUSY_RX)
1046 * and a new byte is being received
1047 * - a transmission has just been started (the IP state is HAL_CEC_STATE_BUSY_TX)
1048 * but has been interrupted by a new message reception or discarded due to
1049 * arbitration loss: the reception of the first or higher priority message
1050 * (the arbitration winner) is starting */
1051 if ((hcec
->State
== HAL_CEC_STATE_STANDBY_RX
)
1052 || (hcec
->State
== HAL_CEC_STATE_BUSY_RX
)
1053 || (hcec
->State
== HAL_CEC_STATE_BUSY_TX
))
1055 /* reception is starting */
1056 hcec
->State
= HAL_CEC_STATE_BUSY_RX
;
1057 tempisr
= (uint32_t) (hcec
->Instance
->ISR
);
1058 if ((tempisr
& CEC_ISR_RXBR
) != 0)
1060 /* read received byte */
1061 *hcec
->pRxBuffPtr
++ = hcec
->Instance
->RXDR
;
1062 /* if last byte has been received */
1063 if ((tempisr
& CEC_ISR_RXEND
) != 0)
1066 __HAL_CEC_CLEAR_FLAG(hcec
,CEC_ISR_RXBR
|CEC_ISR_RXEND
);
1067 /* RX interrupts are not disabled at this point.
1068 * Indeed, to disable the IT, the IP must be disabled first
1069 * which resets the TXSOM flag. In case of arbitration loss,
1070 * this leads to a transmission abort.
1071 * Therefore, RX interruptions disabling if so required,
1072 * is done in HAL_CEC_RxCpltCallback */
1074 /* IP state is moved to READY.
1075 * If the IP must remain in standby mode to listen
1076 * any new message, it is up to HAL_CEC_RxCpltCallback
1077 * to move it again to HAL_CEC_STATE_STANDBY_RX */
1078 hcec
->State
= HAL_CEC_STATE_READY
;
1080 HAL_CEC_RxCpltCallback(hcec
);
1084 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_ISR_RXBR
);
1113 #endif /* defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F051x8) || defined(STM32F058xx) || */
1114 /* defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || */
1115 /* defined(STM32F091xC) || defined (STM32F098xx) */
1117 #endif /* HAL_CEC_MODULE_ENABLED */
1119 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/