2 ******************************************************************************
3 * @file stm32f1xx_hal_cec.c
4 * @author MCD Application Team
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
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
21 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
38 (#) Program the Bit Timing Error Mode and the Bit Period Error Mode in the hcec Init structure.
40 (#) Initialize the CEC registers by calling the HAL_CEC_Init() API.
43 (@) This API (HAL_CEC_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
44 by calling the customed HAL_CEC_MspInit() API.
47 ******************************************************************************
50 * <h2><center>© COPYRIGHT(c) 2016 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 "stm32f1xx_hal.h"
80 #ifdef HAL_CEC_MODULE_ENABLED
82 #if defined(STM32F100xB) || defined(STM32F100xE)
84 /** @addtogroup STM32F1xx_HAL_Driver
89 * @brief HAL CEC module driver
93 /* Private typedef -----------------------------------------------------------*/
94 /* Private define ------------------------------------------------------------*/
95 /** @defgroup CEC_Private_Constants CEC Private Constants
98 #define CEC_CFGR_FIELDS (CEC_CFGR_BTEM | CEC_CFGR_BPEM )
99 #define CEC_FLAG_TRANSMIT_MASK (CEC_FLAG_TSOM|CEC_FLAG_TEOM|CEC_FLAG_TBTRF)
100 #define CEC_FLAG_RECEIVE_MASK (CEC_FLAG_RSOM|CEC_FLAG_REOM|CEC_FLAG_RBTF)
101 #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)
102 #define CEC_RXXFERSIZE_INITIALIZE 0xFFFF /*!< Value used to initialise the RxXferSize of the handle */
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
);
119 /* Exported functions ---------------------------------------------------------*/
121 /** @defgroup CEC_Exported_Functions CEC Exported Functions
125 /** @defgroup CEC_Exported_Functions_Group1 Initialization and de-initialization functions
126 * @brief Initialization and Configuration functions
129 ===============================================================================
130 ##### Initialization and Configuration functions #####
131 ===============================================================================
133 This subsection provides a set of functions allowing to initialize the CEC
134 (+) The following parameters need to be configured:
137 (++) InitiatorAddress
144 * @brief Initializes the CEC mode according to the specified
145 * parameters in the CEC_InitTypeDef and creates the associated handle .
146 * @param hcec: CEC handle
149 HAL_StatusTypeDef
HAL_CEC_Init(CEC_HandleTypeDef
*hcec
)
151 /* Check the CEC handle allocation */
152 if((hcec
== NULL
) ||(hcec
->Init
.RxBuffer
== NULL
))
157 /* Check the parameters */
158 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
159 assert_param(IS_CEC_BIT_TIMING_ERROR_MODE(hcec
->Init
.TimingErrorFree
));
160 assert_param(IS_CEC_BIT_PERIOD_ERROR_MODE(hcec
->Init
.PeriodErrorFree
));
161 assert_param(IS_CEC_ADDRESS(hcec
->Init
.OwnAddress
));
163 if(hcec
->gState
== HAL_CEC_STATE_RESET
)
165 /* Allocate lock resource and initialize it */
166 hcec
->Lock
= HAL_UNLOCKED
;
167 /* Init the low level hardware : GPIO, CLOCK */
168 HAL_CEC_MspInit(hcec
);
170 hcec
->gState
= HAL_CEC_STATE_BUSY
;
172 /* Disable the Peripheral */
173 __HAL_CEC_DISABLE(hcec
);
175 /* Write to CEC Control Register */
176 MODIFY_REG(hcec
->Instance
->CFGR
, CEC_CFGR_FIELDS
, hcec
->Init
.TimingErrorFree
| hcec
->Init
.PeriodErrorFree
);
178 /* Write to CEC Own Address Register */
179 MODIFY_REG(hcec
->Instance
->OAR
, CEC_OAR_OA
, hcec
->Init
.OwnAddress
);
181 /* Configure the prescaler to generate the required 50 microseconds time base.*/
182 MODIFY_REG(hcec
->Instance
->PRES
, CEC_PRES_PRES
, 50U * (HAL_RCC_GetPCLK1Freq()/1000000U) - 1U);
184 /* Enable the following CEC Interrupt */
185 __HAL_CEC_ENABLE_IT(hcec
, CEC_IT_IE
);
187 /* Enable the CEC Peripheral */
188 __HAL_CEC_ENABLE(hcec
);
190 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
191 hcec
->gState
= HAL_CEC_STATE_READY
;
192 hcec
->RxState
= HAL_CEC_STATE_READY
;
198 * @brief DeInitializes the CEC peripheral
199 * @param hcec: CEC handle
202 HAL_StatusTypeDef
HAL_CEC_DeInit(CEC_HandleTypeDef
*hcec
)
204 /* Check the CEC handle allocation */
210 /* Check the parameters */
211 assert_param(IS_CEC_ALL_INSTANCE(hcec
->Instance
));
213 hcec
->gState
= HAL_CEC_STATE_BUSY
;
215 /* DeInit the low level hardware */
216 HAL_CEC_MspDeInit(hcec
);
218 __HAL_RCC_CEC_FORCE_RESET();
219 __HAL_RCC_CEC_RELEASE_RESET();
221 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
222 hcec
->gState
= HAL_CEC_STATE_RESET
;
223 hcec
->RxState
= HAL_CEC_STATE_RESET
;
232 * @brief Initializes the Own Address of the CEC device
233 * @param hcec: CEC handle
234 * @param CEC_OwnAddress: The CEC own address.
237 HAL_StatusTypeDef
HAL_CEC_SetDeviceAddress(CEC_HandleTypeDef
*hcec
, uint16_t CEC_OwnAddress
)
239 /* Check the parameters */
240 assert_param(IS_CEC_OWN_ADDRESS(CEC_OwnAddress
));
242 if ((hcec
->gState
== HAL_CEC_STATE_READY
) && (hcec
->RxState
== HAL_CEC_STATE_READY
))
247 hcec
->gState
= HAL_CEC_STATE_BUSY
;
249 /* Disable the Peripheral */
250 __HAL_CEC_DISABLE(hcec
);
252 if(CEC_OwnAddress
!= CEC_OWN_ADDRESS_NONE
)
254 MODIFY_REG(hcec
->Instance
->OAR
, CEC_OAR_OA
, hcec
->Init
.OwnAddress
);
258 CLEAR_BIT(hcec
->Instance
->OAR
, CEC_OAR_OA
);
261 hcec
->gState
= HAL_CEC_STATE_READY
;
262 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
264 /* Process Unlocked */
267 /* Enable the Peripheral */
268 __HAL_CEC_ENABLE(hcec
);
279 * @brief CEC MSP Init
280 * @param hcec: CEC handle
283 __weak
void HAL_CEC_MspInit(CEC_HandleTypeDef
*hcec
)
285 /* Prevent unused argument(s) compilation warning */
287 /* NOTE : This function should not be modified, when the callback is needed,
288 the HAL_CEC_MspInit can be implemented in the user file
293 * @brief CEC MSP DeInit
294 * @param hcec: CEC handle
297 __weak
void HAL_CEC_MspDeInit(CEC_HandleTypeDef
*hcec
)
299 /* Prevent unused argument(s) compilation warning */
301 /* NOTE : This function should not be modified, when the callback is needed,
302 the HAL_CEC_MspDeInit can be implemented in the user file
310 /** @defgroup CEC_Exported_Functions_Group2 Input and Output operation functions
311 * @brief CEC Transmit/Receive functions
314 ===============================================================================
315 ##### IO operation functions #####
316 ===============================================================================
318 This subsection provides a set of functions allowing to manage the CEC data transfers.
320 (#) The CEC handle must contain the initiator (TX side) and the destination (RX side)
321 logical addresses (4-bit long addresses, 0xF for broadcast messages destination)
323 (#) The communication is performed using Interrupts.
324 These API's return the HAL status.
325 The end of the data processing will be indicated through the
326 dedicated CEC IRQ when using Interrupt mode.
327 The HAL_CEC_TxCpltCallback(), HAL_CEC_RxCpltCallback() user callbacks
328 will be executed respectively at the end of the transmit or Receive process
329 The HAL_CEC_ErrorCallback() user callback will be executed when a communication
332 (#) API's with Interrupt are :
333 (+) HAL_CEC_Transmit_IT()
334 (+) HAL_CEC_IRQHandler()
336 (#) A set of User Callbacks are provided:
337 (+) HAL_CEC_TxCpltCallback()
338 (+) HAL_CEC_RxCpltCallback()
339 (+) HAL_CEC_ErrorCallback()
346 * @brief Send data in interrupt mode
347 * @param hcec: CEC handle
348 * @param InitiatorAddress: Initiator address
349 * @param DestinationAddress: destination logical address
350 * @param pData: pointer to input byte data buffer
351 * @param Size: amount of data to be sent in bytes (without counting the header).
352 * 0 means only the header is sent (ping operation).
353 * Maximum TX size is 15 bytes (1 opcode and up to 14 operands).
356 HAL_StatusTypeDef
HAL_CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
, uint8_t InitiatorAddress
,uint8_t DestinationAddress
, uint8_t *pData
, uint32_t Size
)
358 /* if the IP isn't already busy and if there is no previous transmission
359 already pending due to arbitration lost */
360 if(hcec
->gState
== HAL_CEC_STATE_READY
)
362 if((pData
== NULL
) && (Size
> 0U))
367 assert_param(IS_CEC_ADDRESS(DestinationAddress
));
368 assert_param(IS_CEC_ADDRESS(InitiatorAddress
));
369 assert_param(IS_CEC_MSGSIZE(Size
));
373 hcec
->pTxBuffPtr
= pData
;
374 hcec
->gState
= HAL_CEC_STATE_BUSY_TX
;
375 hcec
->ErrorCode
= HAL_CEC_ERROR_NONE
;
377 /* initialize the number of bytes to send,
378 * 0 means only one header is sent (ping operation) */
379 hcec
->TxXferCount
= Size
;
381 /* send header block */
382 hcec
->Instance
->TXD
= (uint8_t)((uint32_t)InitiatorAddress
<< CEC_INITIATOR_LSB_POS
) | DestinationAddress
;
384 /* Process Unlocked */
387 /* case no data to be sent, sender is only pinging the system */
390 /* Set TX Start of Message (TXSOM) bit */
391 MODIFY_REG(hcec
->Instance
->CSR
, CEC_FLAG_TRANSMIT_MASK
, CEC_FLAG_TSOM
);
395 /* Send a ping command */
396 MODIFY_REG(hcec
->Instance
->CSR
, CEC_FLAG_TRANSMIT_MASK
, CEC_FLAG_TEOM
|CEC_FLAG_TSOM
);
408 * @brief Get size of the received frame.
409 * @param hcec: CEC handle
412 uint32_t HAL_CEC_GetLastReceivedFrameSize(CEC_HandleTypeDef
*hcec
)
414 return hcec
->RxXferSize
;
418 * @brief Change Rx Buffer.
419 * @param hcec: CEC handle
420 * @param Rxbuffer: Rx Buffer
421 * @note This function can be called only inside the HAL_CEC_RxCpltCallback()
424 void HAL_CEC_ChangeRxBuffer(CEC_HandleTypeDef
*hcec
, uint8_t* Rxbuffer
)
426 hcec
->Init
.RxBuffer
= Rxbuffer
;
430 * @brief This function handles CEC interrupt requests.
431 * @param hcec: CEC handle
434 void HAL_CEC_IRQHandler(CEC_HandleTypeDef
*hcec
)
436 /* Save error status register for further error handling purposes */
437 hcec
->ErrorCode
= READ_BIT(hcec
->Instance
->ESR
, CEC_ESR_ALL_ERROR
);
440 if(__HAL_CEC_GET_FLAG(hcec
, CEC_FLAG_TERR
) != RESET
)
442 /* Acknowledgement of the error */
443 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_TERR
);
445 hcec
->gState
= HAL_CEC_STATE_READY
;
449 if(__HAL_CEC_GET_FLAG(hcec
, CEC_FLAG_RERR
) != RESET
)
451 /* Acknowledgement of the error */
452 __HAL_CEC_CLEAR_FLAG(hcec
, CEC_FLAG_RERR
);
453 hcec
->Init
.RxBuffer
-=hcec
->RxXferSize
;
454 hcec
->RxXferSize
= 0U;
455 hcec
->RxState
= HAL_CEC_STATE_READY
;
458 if((hcec
->ErrorCode
& CEC_ESR_ALL_ERROR
) != 0U)
460 /* Error Call Back */
461 HAL_CEC_ErrorCallback(hcec
);
464 /* Transmit byte request or block transfer finished */
465 if(__HAL_CEC_GET_FLAG(hcec
, CEC_FLAG_TBTRF
) != RESET
)
467 CEC_Transmit_IT(hcec
);
470 /* Receive byte or block transfer finished */
471 if(__HAL_CEC_GET_FLAG(hcec
, CEC_FLAG_RBTF
) != RESET
)
473 if(hcec
->RxXferSize
== 0U)
475 /* reception is starting */
476 hcec
->RxState
= HAL_CEC_STATE_BUSY_RX
;
478 CEC_Receive_IT(hcec
);
484 * @brief Tx Transfer completed callback
485 * @param hcec: CEC handle
488 __weak
void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef
*hcec
)
490 /* Prevent unused argument(s) compilation warning */
492 /* NOTE : This function should not be modified, when the callback is needed,
493 the HAL_CEC_TxCpltCallback can be implemented in the user file
498 * @brief Rx Transfer completed callback
499 * @param hcec: CEC handle
500 * @param RxFrameSize: Size of frame
503 __weak
void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef
*hcec
, uint32_t RxFrameSize
)
505 /* Prevent unused argument(s) compilation warning */
508 /* NOTE : This function should not be modified, when the callback is needed,
509 the HAL_CEC_RxCpltCallback can be implemented in the user file
514 * @brief CEC error callbacks
515 * @param hcec: CEC handle
518 __weak
void HAL_CEC_ErrorCallback(CEC_HandleTypeDef
*hcec
)
520 /* Prevent unused argument(s) compilation warning */
522 /* NOTE : This function should not be modified, when the callback is needed,
523 the HAL_CEC_ErrorCallback can be implemented in the user file
530 /** @defgroup CEC_Exported_Functions_Group3 Peripheral Control functions
531 * @brief CEC control functions
534 ===============================================================================
535 ##### Peripheral Control function #####
536 ===============================================================================
538 This subsection provides a set of functions allowing to control the CEC.
539 (+) HAL_CEC_GetState() API can be helpful to check in run-time the state of the CEC peripheral.
540 (+) HAL_CEC_GetError() API can be helpful to check in run-time the error of the CEC peripheral.
545 * @brief return the CEC state
546 * @param hcec: pointer to a CEC_HandleTypeDef structure that contains
547 * the configuration information for the specified CEC module.
550 HAL_CEC_StateTypeDef
HAL_CEC_GetState(CEC_HandleTypeDef
*hcec
)
552 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
553 temp1
= hcec
->gState
;
554 temp2
= hcec
->RxState
;
556 return (HAL_CEC_StateTypeDef
)(temp1
| temp2
);
560 * @brief Return the CEC error code
561 * @param hcec : pointer to a CEC_HandleTypeDef structure that contains
562 * the configuration information for the specified CEC.
563 * @retval CEC Error Code
565 uint32_t HAL_CEC_GetError(CEC_HandleTypeDef
*hcec
)
567 return hcec
->ErrorCode
;
578 /** @addtogroup CEC_Private_Functions
583 * @brief Send data in interrupt mode
584 * @param hcec: CEC handle.
585 * Function called under interruption only, once
586 * interruptions have been enabled by HAL_CEC_Transmit_IT()
589 static HAL_StatusTypeDef
CEC_Transmit_IT(CEC_HandleTypeDef
*hcec
)
591 /* if the IP is already busy or if there is a previous transmission
592 already pending due to arbitration loss */
593 if((hcec
->gState
== HAL_CEC_STATE_BUSY_TX
) || (__HAL_CEC_GET_TRANSMISSION_START_FLAG(hcec
) != RESET
))
595 /* if all data have been sent */
596 if(hcec
->TxXferCount
== 0U)
598 /* Acknowledge successful completion by writing 0x00 */
599 MODIFY_REG(hcec
->Instance
->CSR
, CEC_FLAG_TRANSMIT_MASK
, 0x00U
);
601 hcec
->gState
= HAL_CEC_STATE_READY
;
603 HAL_CEC_TxCpltCallback(hcec
);
609 /* Reduce the number of bytes to transfer by one */
612 /* Write data to TX buffer*/
613 hcec
->Instance
->TXD
= *hcec
->pTxBuffPtr
++;
615 /* If this is the last byte of the ongoing transmission */
616 if(hcec
->TxXferCount
== 0U)
618 /* Acknowledge byte request and signal end of message */
619 MODIFY_REG(hcec
->Instance
->CSR
, CEC_FLAG_TRANSMIT_MASK
, CEC_FLAG_TEOM
);
623 /* Acknowledge byte request by writing 0x00 */
624 MODIFY_REG(hcec
->Instance
->CSR
, CEC_FLAG_TRANSMIT_MASK
, 0x00U
);
637 * @brief Receive data in interrupt mode.
638 * @param hcec: CEC handle.
639 * Function called under interruption only, once
640 * interruptions have been enabled by HAL_CEC_Receive_IT()
643 static HAL_StatusTypeDef
CEC_Receive_IT(CEC_HandleTypeDef
*hcec
)
645 static uint32_t temp
;
647 if(hcec
->RxState
== HAL_CEC_STATE_BUSY_RX
)
649 temp
= hcec
->Instance
->CSR
;
651 /* Store received data */
653 *hcec
->Init
.RxBuffer
++ = hcec
->Instance
->RXD
;
655 /* Acknowledge received byte by writing 0x00 */
656 MODIFY_REG(hcec
->Instance
->CSR
, CEC_FLAG_RECEIVE_MASK
, 0x00U
);
658 /* If the End Of Message is reached */
659 if(HAL_IS_BIT_SET(temp
, CEC_FLAG_REOM
))
661 /* Interrupts are not disabled due to transmission still ongoing */
662 hcec
->RxState
= HAL_CEC_STATE_READY
;
664 HAL_CEC_RxCpltCallback(hcec
, hcec
->RxXferSize
);
687 #endif /* defined(STM32F100xB) || defined(STM32F100xE) */
689 #endif /* HAL_CEC_MODULE_ENABLED */
694 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/