STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_cec.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_cec.h"
00024 #include "stm32f10x_rcc.h"
00025 
00048 /* ------------ CEC registers bit address in the alias region ----------- */
00049 #define CEC_OFFSET                (CEC_BASE - PERIPH_BASE)
00050 
00051 /* --- CFGR Register ---*/
00052 
00053 /* Alias word address of PE bit */
00054 #define CFGR_OFFSET                 (CEC_OFFSET + 0x00)
00055 #define PE_BitNumber                0x00
00056 #define CFGR_PE_BB                  (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (PE_BitNumber * 4))
00057 
00058 /* Alias word address of IE bit */
00059 #define IE_BitNumber                0x01
00060 #define CFGR_IE_BB                  (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (IE_BitNumber * 4))
00061 
00062 /* --- CSR Register ---*/
00063 
00064 /* Alias word address of TSOM bit */
00065 #define CSR_OFFSET                  (CEC_OFFSET + 0x10)
00066 #define TSOM_BitNumber              0x00
00067 #define CSR_TSOM_BB                 (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TSOM_BitNumber * 4))
00068 
00069 /* Alias word address of TEOM bit */
00070 #define TEOM_BitNumber              0x01
00071 #define CSR_TEOM_BB                 (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TEOM_BitNumber * 4))
00072   
00073 #define CFGR_CLEAR_Mask            (uint8_t)(0xF3)        /* CFGR register Mask */
00074 #define FLAG_Mask                  ((uint32_t)0x00FFFFFF) /* CEC FLAG mask */
00075  
00118 void CEC_DeInit(void)
00119 {
00120   /* Enable CEC reset state */
00121   RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC, ENABLE);  
00122   /* Release CEC from reset state */
00123   RCC_APB1PeriphResetCmd(RCC_APB1Periph_CEC, DISABLE); 
00124 }
00125 
00126 
00135 void CEC_Init(CEC_InitTypeDef* CEC_InitStruct)
00136 {
00137   uint16_t tmpreg = 0;
00138  
00139   /* Check the parameters */
00140   assert_param(IS_CEC_BIT_TIMING_ERROR_MODE(CEC_InitStruct->CEC_BitTimingMode)); 
00141   assert_param(IS_CEC_BIT_PERIOD_ERROR_MODE(CEC_InitStruct->CEC_BitPeriodMode));
00142      
00143   /*---------------------------- CEC CFGR Configuration -----------------*/
00144   /* Get the CEC CFGR value */
00145   tmpreg = CEC->CFGR;
00146   
00147   /* Clear BTEM and BPEM bits */
00148   tmpreg &= CFGR_CLEAR_Mask;
00149   
00150   /* Configure CEC: Bit Timing Error and Bit Period Error */
00151   tmpreg |= (uint16_t)(CEC_InitStruct->CEC_BitTimingMode | CEC_InitStruct->CEC_BitPeriodMode);
00152 
00153   /* Write to CEC CFGR  register*/
00154   CEC->CFGR = tmpreg;
00155   
00156 }
00157 
00164 void CEC_Cmd(FunctionalState NewState)
00165 {
00166   /* Check the parameters */
00167   assert_param(IS_FUNCTIONAL_STATE(NewState));
00168 
00169   *(__IO uint32_t *) CFGR_PE_BB = (uint32_t)NewState;
00170 
00171   if(NewState == DISABLE)
00172   {
00173     /* Wait until the PE bit is cleared by hardware (Idle Line detected) */
00174     while((CEC->CFGR & CEC_CFGR_PE) != (uint32_t)RESET)
00175     {
00176     }  
00177   }  
00178 }
00179 
00186 void CEC_ITConfig(FunctionalState NewState)
00187 {
00188   /* Check the parameters */
00189   assert_param(IS_FUNCTIONAL_STATE(NewState));
00190 
00191   *(__IO uint32_t *) CFGR_IE_BB = (uint32_t)NewState;
00192 }
00193 
00199 void CEC_OwnAddressConfig(uint8_t CEC_OwnAddress)
00200 {
00201   /* Check the parameters */
00202   assert_param(IS_CEC_ADDRESS(CEC_OwnAddress));
00203 
00204   /* Set the CEC own address */
00205   CEC->OAR = CEC_OwnAddress;
00206 }
00207 
00213 void CEC_SetPrescaler(uint16_t CEC_Prescaler)
00214 {
00215   /* Check the parameters */
00216   assert_param(IS_CEC_PRESCALER(CEC_Prescaler));
00217 
00218   /* Set the  Prescaler value*/
00219   CEC->PRES = CEC_Prescaler;
00220 }
00221 
00227 void CEC_SendDataByte(uint8_t Data)
00228 {  
00229   /* Transmit Data */
00230   CEC->TXD = Data ;
00231 }
00232 
00233 
00239 uint8_t CEC_ReceiveDataByte(void)
00240 {
00241   /* Receive Data */
00242   return (uint8_t)(CEC->RXD);
00243 }
00244 
00250 void CEC_StartOfMessage(void)
00251 {  
00252   /* Starts of new message */
00253   *(__IO uint32_t *) CSR_TSOM_BB = (uint32_t)0x1;
00254 }
00255 
00262 void CEC_EndOfMessageCmd(FunctionalState NewState)
00263 {   
00264   /* Check the parameters */
00265   assert_param(IS_FUNCTIONAL_STATE(NewState));
00266   
00267   /* The data byte will be transmitted with or without an EOM bit*/
00268   *(__IO uint32_t *) CSR_TEOM_BB = (uint32_t)NewState;
00269 }
00270 
00291 FlagStatus CEC_GetFlagStatus(uint32_t CEC_FLAG) 
00292 {
00293   FlagStatus bitstatus = RESET;
00294   uint32_t cecreg = 0, cecbase = 0;
00295   
00296   /* Check the parameters */
00297   assert_param(IS_CEC_GET_FLAG(CEC_FLAG));
00298  
00299   /* Get the CEC peripheral base address */
00300   cecbase = (uint32_t)(CEC_BASE);
00301   
00302   /* Read flag register index */
00303   cecreg = CEC_FLAG >> 28;
00304   
00305   /* Get bit[23:0] of the flag */
00306   CEC_FLAG &= FLAG_Mask;
00307   
00308   if(cecreg != 0)
00309   {
00310     /* Flag in CEC ESR Register */
00311     CEC_FLAG = (uint32_t)(CEC_FLAG >> 16);
00312     
00313     /* Get the CEC ESR register address */
00314     cecbase += 0xC;
00315   }
00316   else
00317   {
00318     /* Get the CEC CSR register address */
00319     cecbase += 0x10;
00320   }
00321   
00322   if(((*(__IO uint32_t *)cecbase) & CEC_FLAG) != (uint32_t)RESET)
00323   {
00324     /* CEC_FLAG is set */
00325     bitstatus = SET;
00326   }
00327   else
00328   {
00329     /* CEC_FLAG is reset */
00330     bitstatus = RESET;
00331   }
00332   
00333   /* Return the CEC_FLAG status */
00334   return  bitstatus;
00335 }
00336 
00349 void CEC_ClearFlag(uint32_t CEC_FLAG)
00350 { 
00351   uint32_t tmp = 0x0;
00352   
00353   /* Check the parameters */
00354   assert_param(IS_CEC_CLEAR_FLAG(CEC_FLAG));
00355 
00356   tmp = CEC->CSR & 0x2;
00357        
00358   /* Clear the selected CEC flags */
00359   CEC->CSR &= (uint32_t)(((~(uint32_t)CEC_FLAG) & 0xFFFFFFFC) | tmp);
00360 }
00361 
00372 ITStatus CEC_GetITStatus(uint8_t CEC_IT)
00373 {
00374   ITStatus bitstatus = RESET;
00375   uint32_t enablestatus = 0;
00376   
00377   /* Check the parameters */
00378    assert_param(IS_CEC_GET_IT(CEC_IT));
00379    
00380   /* Get the CEC IT enable bit status */
00381   enablestatus = (CEC->CFGR & (uint8_t)CEC_CFGR_IE) ;
00382   
00383   /* Check the status of the specified CEC interrupt */
00384   if (((CEC->CSR & CEC_IT) != (uint32_t)RESET) && enablestatus)
00385   {
00386     /* CEC_IT is set */
00387     bitstatus = SET;
00388   }
00389   else
00390   {
00391     /* CEC_IT is reset */
00392     bitstatus = RESET;
00393   }
00394   /* Return the CEC_IT status */
00395   return  bitstatus;
00396 }
00397 
00408 void CEC_ClearITPendingBit(uint16_t CEC_IT)
00409 {
00410   uint32_t tmp = 0x0;
00411   
00412   /* Check the parameters */
00413   assert_param(IS_CEC_GET_IT(CEC_IT));
00414   
00415   tmp = CEC->CSR & 0x2;
00416   
00417   /* Clear the selected CEC interrupt pending bits */
00418   CEC->CSR &= (uint32_t)(((~(uint32_t)CEC_IT) & 0xFFFFFFFC) | tmp);
00419 }
00420 
00433 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/