2 ******************************************************************************
3 * @file stm32f10x_cec.c
4 * @author MCD Application Team
7 * @brief This file provides all the CEC firmware functions.
8 ******************************************************************************
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x_cec.h"
24 #include "stm32f10x_rcc.h"
26 /** @addtogroup STM32F10x_StdPeriph_Driver
31 * @brief CEC driver modules
35 /** @defgroup CEC_Private_TypesDefinitions
44 /** @defgroup CEC_Private_Defines
48 /* ------------ CEC registers bit address in the alias region ----------- */
49 #define CEC_OFFSET (CEC_BASE - PERIPH_BASE)
51 /* --- CFGR Register ---*/
53 /* Alias word address of PE bit */
54 #define CFGR_OFFSET (CEC_OFFSET + 0x00)
55 #define PE_BitNumber 0x00
56 #define CFGR_PE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (PE_BitNumber * 4))
58 /* Alias word address of IE bit */
59 #define IE_BitNumber 0x01
60 #define CFGR_IE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (IE_BitNumber * 4))
62 /* --- CSR Register ---*/
64 /* Alias word address of TSOM bit */
65 #define CSR_OFFSET (CEC_OFFSET + 0x10)
66 #define TSOM_BitNumber 0x00
67 #define CSR_TSOM_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TSOM_BitNumber * 4))
69 /* Alias word address of TEOM bit */
70 #define TEOM_BitNumber 0x01
71 #define CSR_TEOM_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TEOM_BitNumber * 4))
73 #define CFGR_CLEAR_Mask (uint8_t)(0xF3) /* CFGR register Mask */
74 #define FLAG_Mask ((uint32_t)0x00FFFFFF) /* CEC FLAG mask */
81 /** @defgroup CEC_Private_Macros
90 /** @defgroup CEC_Private_Variables
99 /** @defgroup CEC_Private_FunctionPrototypes
108 /** @defgroup CEC_Private_Functions
113 * @brief Deinitializes the CEC peripheral registers to their default reset
118 void CEC_DeInit(void)
120 /* Enable CEC reset state */
121 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC
, ENABLE
);
122 /* Release CEC from reset state */
123 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC
, DISABLE
);
128 * @brief Initializes the CEC peripheral according to the specified
129 * parameters in the CEC_InitStruct.
130 * @param CEC_InitStruct: pointer to an CEC_InitTypeDef structure that
131 * contains the configuration information for the specified
135 void CEC_Init(CEC_InitTypeDef
* CEC_InitStruct
)
139 /* Check the parameters */
140 assert_param(IS_CEC_BIT_TIMING_ERROR_MODE(CEC_InitStruct
->CEC_BitTimingMode
));
141 assert_param(IS_CEC_BIT_PERIOD_ERROR_MODE(CEC_InitStruct
->CEC_BitPeriodMode
));
143 /*---------------------------- CEC CFGR Configuration -----------------*/
144 /* Get the CEC CFGR value */
147 /* Clear BTEM and BPEM bits */
148 tmpreg
&= CFGR_CLEAR_Mask
;
150 /* Configure CEC: Bit Timing Error and Bit Period Error */
151 tmpreg
|= (uint16_t)(CEC_InitStruct
->CEC_BitTimingMode
| CEC_InitStruct
->CEC_BitPeriodMode
);
153 /* Write to CEC CFGR register*/
159 * @brief Enables or disables the specified CEC peripheral.
160 * @param NewState: new state of the CEC peripheral.
161 * This parameter can be: ENABLE or DISABLE.
164 void CEC_Cmd(FunctionalState NewState
)
166 /* Check the parameters */
167 assert_param(IS_FUNCTIONAL_STATE(NewState
));
169 *(__IO
uint32_t *) CFGR_PE_BB
= (uint32_t)NewState
;
171 if(NewState
== DISABLE
)
173 /* Wait until the PE bit is cleared by hardware (Idle Line detected) */
174 while((CEC
->CFGR
& CEC_CFGR_PE
) != (uint32_t)RESET
)
181 * @brief Enables or disables the CEC interrupt.
182 * @param NewState: new state of the CEC interrupt.
183 * This parameter can be: ENABLE or DISABLE.
186 void CEC_ITConfig(FunctionalState NewState
)
188 /* Check the parameters */
189 assert_param(IS_FUNCTIONAL_STATE(NewState
));
191 *(__IO
uint32_t *) CFGR_IE_BB
= (uint32_t)NewState
;
195 * @brief Defines the Own Address of the CEC device.
196 * @param CEC_OwnAddress: The CEC own address
199 void CEC_OwnAddressConfig(uint8_t CEC_OwnAddress
)
201 /* Check the parameters */
202 assert_param(IS_CEC_ADDRESS(CEC_OwnAddress
));
204 /* Set the CEC own address */
205 CEC
->OAR
= CEC_OwnAddress
;
209 * @brief Sets the CEC prescaler value.
210 * @param CEC_Prescaler: CEC prescaler new value
213 void CEC_SetPrescaler(uint16_t CEC_Prescaler
)
215 /* Check the parameters */
216 assert_param(IS_CEC_PRESCALER(CEC_Prescaler
));
218 /* Set the Prescaler value*/
219 CEC
->PRES
= CEC_Prescaler
;
223 * @brief Transmits single data through the CEC peripheral.
224 * @param Data: the data to transmit.
227 void CEC_SendDataByte(uint8_t Data
)
235 * @brief Returns the most recent received data by the CEC peripheral.
237 * @retval The received data.
239 uint8_t CEC_ReceiveDataByte(void)
242 return (uint8_t)(CEC
->RXD
);
246 * @brief Starts a new message.
250 void CEC_StartOfMessage(void)
252 /* Starts of new message */
253 *(__IO
uint32_t *) CSR_TSOM_BB
= (uint32_t)0x1;
257 * @brief Transmits message with or without an EOM bit.
258 * @param NewState: new state of the CEC Tx End Of Message.
259 * This parameter can be: ENABLE or DISABLE.
262 void CEC_EndOfMessageCmd(FunctionalState NewState
)
264 /* Check the parameters */
265 assert_param(IS_FUNCTIONAL_STATE(NewState
));
267 /* The data byte will be transmitted with or without an EOM bit*/
268 *(__IO
uint32_t *) CSR_TEOM_BB
= (uint32_t)NewState
;
272 * @brief Gets the CEC flag status
273 * @param CEC_FLAG: specifies the CEC flag to check.
274 * This parameter can be one of the following values:
275 * @arg CEC_FLAG_BTE: Bit Timing Error
276 * @arg CEC_FLAG_BPE: Bit Period Error
277 * @arg CEC_FLAG_RBTFE: Rx Block Transfer Finished Error
278 * @arg CEC_FLAG_SBE: Start Bit Error
279 * @arg CEC_FLAG_ACKE: Block Acknowledge Error
280 * @arg CEC_FLAG_LINE: Line Error
281 * @arg CEC_FLAG_TBTFE: Tx Block Transfer Finished Error
282 * @arg CEC_FLAG_TEOM: Tx End Of Message
283 * @arg CEC_FLAG_TERR: Tx Error
284 * @arg CEC_FLAG_TBTRF: Tx Byte Transfer Request or Block Transfer Finished
285 * @arg CEC_FLAG_RSOM: Rx Start Of Message
286 * @arg CEC_FLAG_REOM: Rx End Of Message
287 * @arg CEC_FLAG_RERR: Rx Error
288 * @arg CEC_FLAG_RBTF: Rx Byte/Block Transfer Finished
289 * @retval The new state of CEC_FLAG (SET or RESET)
291 FlagStatus
CEC_GetFlagStatus(uint32_t CEC_FLAG
)
293 FlagStatus bitstatus
= RESET
;
294 uint32_t cecreg
= 0, cecbase
= 0;
296 /* Check the parameters */
297 assert_param(IS_CEC_GET_FLAG(CEC_FLAG
));
299 /* Get the CEC peripheral base address */
300 cecbase
= (uint32_t)(CEC_BASE
);
302 /* Read flag register index */
303 cecreg
= CEC_FLAG
>> 28;
305 /* Get bit[23:0] of the flag */
306 CEC_FLAG
&= FLAG_Mask
;
310 /* Flag in CEC ESR Register */
311 CEC_FLAG
= (uint32_t)(CEC_FLAG
>> 16);
313 /* Get the CEC ESR register address */
318 /* Get the CEC CSR register address */
322 if(((*(__IO
uint32_t *)cecbase
) & CEC_FLAG
) != (uint32_t)RESET
)
324 /* CEC_FLAG is set */
329 /* CEC_FLAG is reset */
333 /* Return the CEC_FLAG status */
338 * @brief Clears the CEC's pending flags.
339 * @param CEC_FLAG: specifies the flag to clear.
340 * This parameter can be any combination of the following values:
341 * @arg CEC_FLAG_TERR: Tx Error
342 * @arg CEC_FLAG_TBTRF: Tx Byte Transfer Request or Block Transfer Finished
343 * @arg CEC_FLAG_RSOM: Rx Start Of Message
344 * @arg CEC_FLAG_REOM: Rx End Of Message
345 * @arg CEC_FLAG_RERR: Rx Error
346 * @arg CEC_FLAG_RBTF: Rx Byte/Block Transfer Finished
349 void CEC_ClearFlag(uint32_t CEC_FLAG
)
353 /* Check the parameters */
354 assert_param(IS_CEC_CLEAR_FLAG(CEC_FLAG
));
356 tmp
= CEC
->CSR
& 0x2;
358 /* Clear the selected CEC flags */
359 CEC
->CSR
&= (uint32_t)(((~(uint32_t)CEC_FLAG
) & 0xFFFFFFFC) | tmp
);
363 * @brief Checks whether the specified CEC interrupt has occurred or not.
364 * @param CEC_IT: specifies the CEC interrupt source to check.
365 * This parameter can be one of the following values:
366 * @arg CEC_IT_TERR: Tx Error
367 * @arg CEC_IT_TBTF: Tx Block Transfer Finished
368 * @arg CEC_IT_RERR: Rx Error
369 * @arg CEC_IT_RBTF: Rx Block Transfer Finished
370 * @retval The new state of CEC_IT (SET or RESET).
372 ITStatus
CEC_GetITStatus(uint8_t CEC_IT
)
374 ITStatus bitstatus
= RESET
;
375 uint32_t enablestatus
= 0;
377 /* Check the parameters */
378 assert_param(IS_CEC_GET_IT(CEC_IT
));
380 /* Get the CEC IT enable bit status */
381 enablestatus
= (CEC
->CFGR
& (uint8_t)CEC_CFGR_IE
) ;
383 /* Check the status of the specified CEC interrupt */
384 if (((CEC
->CSR
& CEC_IT
) != (uint32_t)RESET
) && enablestatus
)
391 /* CEC_IT is reset */
394 /* Return the CEC_IT status */
399 * @brief Clears the CEC's interrupt pending bits.
400 * @param CEC_IT: specifies the CEC interrupt pending bit to clear.
401 * This parameter can be any combination of the following values:
402 * @arg CEC_IT_TERR: Tx Error
403 * @arg CEC_IT_TBTF: Tx Block Transfer Finished
404 * @arg CEC_IT_RERR: Rx Error
405 * @arg CEC_IT_RBTF: Rx Block Transfer Finished
408 void CEC_ClearITPendingBit(uint16_t CEC_IT
)
412 /* Check the parameters */
413 assert_param(IS_CEC_GET_IT(CEC_IT
));
415 tmp
= CEC
->CSR
& 0x2;
417 /* Clear the selected CEC interrupt pending bits */
418 CEC
->CSR
&= (uint32_t)(((~(uint32_t)CEC_IT
) & 0xFFFFFFFC) | tmp
);
433 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/