STM8S/A Standard Peripherals Drivers: stm8s_uart4.c Source File

STM8S/A

stm8s_uart4.c
Go to the documentation of this file.
00001 /**
00002   ********************************************************************************
00003   * @file    stm8s_uart4.c
00004   * @author  MCD Application Team
00005   * @version V2.3.0
00006   * @date    16-June-2017
00007   * @brief   This file contains all the functions for the UART4 peripheral.
00008    ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s_uart4.h"
00030 
00031 /** @addtogroup STM8S_StdPeriph_Driver
00032   * @{
00033   */
00034 /* Private typedef -----------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 /* Private function prototypes -----------------------------------------------*/
00039 /* Private functions ---------------------------------------------------------*/
00040 /* Public functions ----------------------------------------------------------*/
00041 
00042 /**
00043   * @addtogroup UART4_Public_Functions
00044   * @{
00045   */
00046 
00047 /**
00048   * @brief  Deinitializes the UART peripheral.
00049   * @param  None
00050   * @retval None
00051   */
00052 
00053 void UART4_DeInit(void)
00054 {
00055   /*  Clear the Idle Line Detected bit in the status register by a read
00056   to the UART4_SR register followed by a Read to the UART4_DR register */
00057   (void)UART4->SR;
00058   (void)UART4->DR;
00059   
00060   UART4->BRR2 = UART4_BRR2_RESET_VALUE;  /*  Set UART4_BRR2 to reset value 0x00 */
00061   UART4->BRR1 = UART4_BRR1_RESET_VALUE;  /*  Set UART4_BRR1 to reset value 0x00 */
00062   
00063   UART4->CR1 = UART4_CR1_RESET_VALUE; /*  Set UART4_CR1 to reset value 0x00  */
00064   UART4->CR2 = UART4_CR2_RESET_VALUE; /*  Set UART4_CR2 to reset value 0x00  */
00065   UART4->CR3 = UART4_CR3_RESET_VALUE; /*  Set UART4_CR3 to reset value 0x00  */
00066   UART4->CR4 = UART4_CR4_RESET_VALUE; /*  Set UART4_CR4 to reset value 0x00  */
00067   UART4->CR5 = UART4_CR5_RESET_VALUE; /*  Set UART4_CR5 to reset value 0x00  */
00068   UART4->CR6 = UART4_CR6_RESET_VALUE; /*  Set UART4_CR6 to reset value 0x00  */
00069 }
00070 
00071 /**
00072   * @brief  Initializes the UART4 according to the specified parameters.
00073   * @param  BaudRate: The baudrate.
00074   * @param  WordLength : This parameter can be any of the 
00075   *         @ref UART4_WordLength_TypeDef enumeration.
00076   * @param  StopBits: This parameter can be any of the 
00077   *         @ref UART4_StopBits_TypeDef enumeration.
00078   * @param  Parity: This parameter can be any of the 
00079   *         @ref UART4_Parity_TypeDef enumeration.
00080   * @param  SyncMode: This parameter can be any of the 
00081   *         @ref UART4_SyncMode_TypeDef values.
00082   * @param  Mode: This parameter can be any of the @ref UART4_Mode_TypeDef values
00083   * @retval None
00084   */
00085 void UART4_Init(uint32_t BaudRate, UART4_WordLength_TypeDef WordLength, UART4_StopBits_TypeDef StopBits, UART4_Parity_TypeDef Parity, UART4_SyncMode_TypeDef SyncMode, UART4_Mode_TypeDef Mode)
00086 {
00087   uint8_t BRR2_1 = 0, BRR2_2 = 0;
00088   uint32_t BaudRate_Mantissa = 0, BaudRate_Mantissa100 = 0;
00089   
00090   /* Check the parameters */
00091   assert_param(IS_UART4_BAUDRATE_OK(BaudRate));
00092   assert_param(IS_UART4_WORDLENGTH_OK(WordLength));
00093   assert_param(IS_UART4_STOPBITS_OK(StopBits));
00094   assert_param(IS_UART4_PARITY_OK(Parity));
00095   assert_param(IS_UART4_MODE_OK((uint8_t)Mode));
00096   assert_param(IS_UART4_SYNCMODE_OK((uint8_t)SyncMode));
00097   
00098   /* Clear the word length bit */
00099   UART4->CR1 &= (uint8_t)(~UART4_CR1_M);
00100   /* Set the word length bit according to UART4_WordLength value */
00101   UART4->CR1 |= (uint8_t)WordLength; 
00102   
00103   /* Clear the STOP bits */
00104   UART4->CR3 &= (uint8_t)(~UART4_CR3_STOP);
00105   /* Set the STOP bits number according to UART4_StopBits value  */
00106   UART4->CR3 |= (uint8_t)StopBits; 
00107   
00108   /* Clear the Parity Control bit */
00109   UART4->CR1 &= (uint8_t)(~(UART4_CR1_PCEN | UART4_CR1_PS  ));
00110   /* Set the Parity Control bit to UART4_Parity value */
00111   UART4->CR1 |= (uint8_t)Parity;
00112   
00113   /* Clear the LSB mantissa of UART4DIV  */
00114   UART4->BRR1 &= (uint8_t)(~UART4_BRR1_DIVM);
00115   /* Clear the MSB mantissa of UART4DIV  */
00116   UART4->BRR2 &= (uint8_t)(~UART4_BRR2_DIVM);
00117   /* Clear the Fraction bits of UART4DIV */
00118   UART4->BRR2 &= (uint8_t)(~UART4_BRR2_DIVF);
00119   
00120   /* Set the UART4 BaudRates in BRR1 and BRR2 registers according to UART4_BaudRate value */
00121   BaudRate_Mantissa    = ((uint32_t)CLK_GetClockFreq() / (BaudRate << 4));
00122   BaudRate_Mantissa100 = (((uint32_t)CLK_GetClockFreq() * 100) / (BaudRate << 4));
00123   
00124   /* The fraction and MSB mantissa should be loaded in one step in the BRR2 register*/
00125   /* Set the fraction of UARTDIV  */
00126   BRR2_1 = (uint8_t)((uint8_t)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100))
00127                                 << 4) / 100) & (uint8_t)0x0F); 
00128   BRR2_2 = (uint8_t)((BaudRate_Mantissa >> 4) & (uint8_t)0xF0);
00129   
00130   UART4->BRR2 = (uint8_t)(BRR2_1 | BRR2_2);
00131   /* Set the LSB mantissa of UARTDIV  */
00132   UART4->BRR1 = (uint8_t)BaudRate_Mantissa;           
00133   
00134   /* Disable the Transmitter and Receiver before setting the LBCL, CPOL and CPHA bits */
00135   UART4->CR2 &= (uint8_t)~(UART4_CR2_TEN | UART4_CR2_REN);
00136   /* Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
00137   UART4->CR3 &= (uint8_t)~(UART4_CR3_CPOL | UART4_CR3_CPHA | UART4_CR3_LBCL);
00138   /* Set the Clock Polarity, lock Phase, Last Bit Clock pulse */
00139   UART4->CR3 |= (uint8_t)((uint8_t)SyncMode & (uint8_t)(UART4_CR3_CPOL | \
00140     UART4_CR3_CPHA | UART4_CR3_LBCL));
00141   
00142   if((uint8_t)(Mode & UART4_MODE_TX_ENABLE))
00143   {
00144     /* Set the Transmitter Enable bit */
00145     UART4->CR2 |= (uint8_t)UART4_CR2_TEN;
00146   }
00147   else
00148   {
00149     /* Clear the Transmitter Disable bit */
00150     UART4->CR2 &= (uint8_t)(~UART4_CR2_TEN);
00151   }
00152   if((uint8_t)(Mode & UART4_MODE_RX_ENABLE))
00153   {
00154     /* Set the Receiver Enable bit */
00155     UART4->CR2 |= (uint8_t)UART4_CR2_REN;
00156   }
00157   else
00158   {
00159     /* Clear the Receiver Disable bit */
00160     UART4->CR2 &= (uint8_t)(~UART4_CR2_REN);
00161   }
00162   /* Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit Clock 
00163   pulse bits according to UART4_Mode value */
00164   if((uint8_t)(SyncMode & UART4_SYNCMODE_CLOCK_DISABLE))
00165   {
00166     /* Clear the Clock Enable bit */
00167     UART4->CR3 &= (uint8_t)(~UART4_CR3_CKEN); 
00168   }
00169   else
00170   {
00171     UART4->CR3 |= (uint8_t)((uint8_t)SyncMode & UART4_CR3_CKEN);
00172   }
00173 }
00174 
00175 /**
00176   * @brief  Enable the UART4 peripheral.
00177   * @param  NewState : The new state of the UART Communication.
00178   *         This parameter can be any of the @ref FunctionalState enumeration.
00179   * @retval None
00180   */
00181 void UART4_Cmd(FunctionalState NewState)
00182 {
00183   if(NewState != DISABLE)
00184   {
00185     /* UART4 Enable */
00186     UART4->CR1 &= (uint8_t)(~UART4_CR1_UARTD);
00187   }
00188   else
00189   {
00190     /* UART4 Disable */
00191     UART4->CR1 |= UART4_CR1_UARTD; 
00192   }
00193 }
00194 
00195 /**
00196   * @brief  Enables or disables the specified UART4 interrupts.
00197   * @param  UART4_IT specifies the UART4 interrupt sources to be enabled or disabled.
00198   *         This parameter can be one of the following values:
00199   *         - UART4_IT_LBDF:  LIN Break detection interrupt
00200   *         - UART4_IT_LHDF:  LIN Break detection interrupt
00201   *         - UART4_IT_TXE:  Transmit Data Register empty interrupt
00202   *         - UART4_IT_TC:   Transmission complete interrupt
00203   *         - UART4_IT_RXNE_OR: Receive Data register not empty/Over run error interrupt
00204   *         - UART4_IT_IDLE: Idle line detection interrupt
00205   *         - UART4_IT_PE:   Parity Error interrupt
00206   * @param  NewState new state of the specified UART4 interrupts.
00207   *         This parameter can be: ENABLE or DISABLE.
00208   * @retval None
00209   */
00210 void UART4_ITConfig(UART4_IT_TypeDef UART4_IT, FunctionalState NewState)
00211 {
00212   uint8_t uartreg = 0, itpos = 0x00;
00213   
00214   /* Check the parameters */
00215   assert_param(IS_UART4_CONFIG_IT_OK(UART4_IT));
00216   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00217   
00218   /* Get the UART4 register index */
00219   uartreg = (uint8_t)((uint16_t)UART4_IT >> 0x08);
00220   
00221   /* Get the UART4 IT index */
00222   itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART4_IT & (uint8_t)0x0F));
00223   
00224   if(NewState != DISABLE)
00225   {
00226     /* Enable the Interrupt bits according to UART4_IT mask */
00227     if(uartreg == 0x01)
00228     {
00229       UART4->CR1 |= itpos;
00230     }
00231     else if(uartreg == 0x02)
00232     {
00233       UART4->CR2 |= itpos;
00234     }
00235     else if(uartreg == 0x03)
00236     {
00237       UART4->CR4 |= itpos;
00238     }
00239     else
00240     {
00241       UART4->CR6 |= itpos;
00242     }
00243   }
00244   else
00245   {
00246     /* Disable the interrupt bits according to UART4_IT mask */
00247     if(uartreg == 0x01)
00248     {
00249       UART4->CR1 &= (uint8_t)(~itpos);
00250     }
00251     else if(uartreg == 0x02)
00252     {
00253       UART4->CR2 &= (uint8_t)(~itpos);
00254     }
00255     else if(uartreg == 0x03)
00256     {
00257       UART4->CR4 &= (uint8_t)(~itpos);
00258     }
00259     else
00260     {
00261       UART4->CR6 &= (uint8_t)(~itpos);
00262     }
00263   }
00264 }
00265 
00266 /**
00267   * @brief  Enables or disables the UART�s Half Duplex communication.
00268   * @param  NewState new state of the UART Communication.
00269   *         This parameter can be: ENABLE or DISABLE.
00270   * @retval None
00271   */
00272 void UART4_HalfDuplexCmd(FunctionalState NewState)
00273 {
00274   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00275   
00276   if (NewState != DISABLE)
00277   {
00278     UART4->CR5 |= UART4_CR5_HDSEL;  /**< UART4 Half Duplex Enable  */
00279   }
00280   else
00281   {
00282     UART4->CR5 &= (uint8_t)~UART4_CR5_HDSEL; /**< UART4 Half Duplex Disable */
00283   }
00284 }
00285 
00286 /**
00287   * @brief  Configures the UART4�s IrDA interface.
00288   * @param  UART4_IrDAMode specifies the IrDA mode.
00289   *         This parameter can be any of the @ref UART4_IrDAMode_TypeDef values.
00290   * @retval None
00291   */
00292 void UART4_IrDAConfig(UART4_IrDAMode_TypeDef UART4_IrDAMode)
00293 {
00294   assert_param(IS_UART4_IRDAMODE_OK(UART4_IrDAMode));
00295   
00296   if(UART4_IrDAMode != UART4_IRDAMODE_NORMAL)
00297   {
00298     UART4->CR5 |= UART4_CR5_IRLP;
00299   }
00300   else
00301   {
00302     UART4->CR5 &= ((uint8_t)~UART4_CR5_IRLP);
00303   }
00304 }
00305 
00306 /**
00307   * @brief  Enables or disables the UART4�s IrDA interface.
00308   * @param  NewState new state of the IrDA mode.
00309   *         This parameter can be: ENABLE or DISABLE.
00310   * @retval None
00311   */
00312 void UART4_IrDACmd(FunctionalState NewState)
00313 {
00314   /* Check parameters */
00315   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00316   
00317   if(NewState != DISABLE)
00318   {
00319     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
00320     UART4->CR5 |= UART4_CR5_IREN;
00321   }
00322   else
00323   {
00324     /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
00325     UART4->CR5 &= ((uint8_t)~UART4_CR5_IREN);
00326   }
00327 }
00328 
00329 /**
00330   * @brief  Sets the UART4 LIN Break detection length.
00331   * @param  UART4_LINBreakDetectionLength specifies the LIN break detection length.
00332   *         This parameter can be any of the 
00333   *         @ref UART4_LINBreakDetectionLength_TypeDef values.
00334   * @retval None
00335   */
00336 void UART4_LINBreakDetectionConfig(UART4_LINBreakDetectionLength_TypeDef UART4_LINBreakDetectionLength)
00337 {
00338   /* Check parameters */    
00339   assert_param(IS_UART4_LINBREAKDETECTIONLENGTH_OK(UART4_LINBreakDetectionLength));
00340   
00341   if(UART4_LINBreakDetectionLength != UART4_LINBREAKDETECTIONLENGTH_10BITS)
00342   {
00343     UART4->CR4 |= UART4_CR4_LBDL;
00344   }
00345   else
00346   {
00347     UART4->CR4 &= ((uint8_t)~UART4_CR4_LBDL);
00348   }
00349 }
00350 
00351 /**
00352   * @brief  Configure the UART4 peripheral.
00353   * @param  UART4_Mode specifies the LIN mode.
00354   *         This parameter can be any of the @ref UART4_LinMode_TypeDef values.
00355   * @param  UART4_Autosync specifies the LIN automatic resynchronization mode.
00356   *         This parameter can be any of the @ref UART4_LinAutosync_TypeDef values.
00357   * @param  UART4_DivUp specifies the LIN divider update method.
00358   *         This parameter can be any of the @ref UART4_LinDivUp_TypeDef values.
00359   * @retval None
00360   */
00361 void UART4_LINConfig(UART4_LinMode_TypeDef UART4_Mode, 
00362                      UART4_LinAutosync_TypeDef UART4_Autosync, 
00363                      UART4_LinDivUp_TypeDef UART4_DivUp)
00364 {
00365   /* Check parameters */
00366   assert_param(IS_UART4_SLAVE_OK(UART4_Mode));
00367   assert_param(IS_UART4_AUTOSYNC_OK(UART4_Autosync));
00368   assert_param(IS_UART4_DIVUP_OK(UART4_DivUp));
00369   
00370   if(UART4_Mode != UART4_LIN_MODE_MASTER)
00371   {
00372     UART4->CR6 |=  UART4_CR6_LSLV;
00373   }
00374   else
00375   {
00376     UART4->CR6 &= ((uint8_t)~UART4_CR6_LSLV);
00377   }
00378   
00379   if(UART4_Autosync != UART4_LIN_AUTOSYNC_DISABLE)
00380   {
00381     UART4->CR6 |=  UART4_CR6_LASE ;
00382   }
00383   else
00384   {
00385     UART4->CR6 &= ((uint8_t)~ UART4_CR6_LASE );
00386   }
00387   
00388   if(UART4_DivUp != UART4_LIN_DIVUP_LBRR1)
00389   {
00390     UART4->CR6 |=  UART4_CR6_LDUM;
00391   }
00392   else
00393   {
00394     UART4->CR6 &= ((uint8_t)~ UART4_CR6_LDUM);
00395   }
00396 }
00397 
00398 /**
00399   * @brief  Enables or disables the UART4 LIN mode.
00400   * @param  NewState is new state of the UART4 LIN mode.
00401   *         This parameter can be ENABLE or DISABLE
00402   * @retval None
00403   */
00404 void UART4_LINCmd(FunctionalState NewState)
00405 {
00406   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00407   
00408   if(NewState != DISABLE)
00409   {
00410     /* Enable the LIN mode by setting the LINE bit in the CR2 register */
00411     UART4->CR3 |= UART4_CR3_LINEN;
00412   }
00413   else
00414   {
00415     /* Disable the LIN mode by clearing the LINE bit in the CR2 register */
00416     UART4->CR3 &= ((uint8_t)~UART4_CR3_LINEN);
00417   }
00418 }
00419 
00420 /**
00421   * @brief  Enables or disables the UART4 Smart Card mode.
00422   * @param  NewState: new state of the Smart Card mode.
00423   *         This parameter can be: ENABLE or DISABLE.
00424   * @retval None
00425   */
00426 void UART4_SmartCardCmd(FunctionalState NewState)
00427 {
00428   /* Check parameters */
00429   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00430   
00431   if(NewState != DISABLE)
00432   {
00433     /* Enable the SC mode by setting the SCEN bit in the CR5 register */
00434     UART4->CR5 |= UART4_CR5_SCEN;
00435   }
00436   else
00437   {
00438     /* Disable the SC mode by clearing the SCEN bit in the CR5 register */
00439     UART4->CR5 &= ((uint8_t)(~UART4_CR5_SCEN));
00440   }
00441 }
00442 
00443 /**
00444   * @brief  Enables or disables NACK transmission.
00445   * @param  NewState: new state of the Smart Card mode.
00446   *         This parameter can be: ENABLE or DISABLE.
00447   * @retval None
00448   */
00449 void UART4_SmartCardNACKCmd(FunctionalState NewState)
00450 {
00451   /* Check parameters */
00452   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00453   
00454   if(NewState != DISABLE)
00455   {
00456     /* Enable the NACK transmission by setting the NACK bit in the CR5 register */
00457     UART4->CR5 |= UART4_CR5_NACK;
00458   }
00459   else
00460   {
00461     /* Disable the NACK transmission by clearing the NACK bit in the CR5 register */
00462     UART4->CR5 &= ((uint8_t)~(UART4_CR5_NACK));
00463   }
00464 }
00465 
00466 /**
00467   * @brief  Selects the UART4 WakeUp method.
00468   * @param  UART4_WakeUp: specifies the UART4 wakeup method.
00469   *         This parameter can be any of the @ref UART4_WakeUp_TypeDef values.
00470   * @retval None
00471   */
00472 void UART4_WakeUpConfig(UART4_WakeUp_TypeDef UART4_WakeUp)
00473 {
00474   assert_param(IS_UART4_WAKEUP_OK(UART4_WakeUp));
00475   
00476   UART4->CR1 &= ((uint8_t)~UART4_CR1_WAKE);
00477   UART4->CR1 |= (uint8_t)UART4_WakeUp;
00478 }
00479 
00480 /**
00481   * @brief  Determines if the UART4 is in mute mode or not.
00482   * @param  NewState: new state of the UART4 mode.
00483   *         This parameter can be ENABLE or DISABLE
00484   * @retval None
00485   */
00486 void UART4_ReceiverWakeUpCmd(FunctionalState NewState)
00487 {
00488   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00489   
00490   if(NewState != DISABLE)
00491   {
00492     /* Enable the mute mode UART4 by setting the RWU bit in the CR2 register */
00493     UART4->CR2 |= UART4_CR2_RWU;
00494   }
00495   else
00496   {
00497     /* Disable the mute mode UART4 by clearing the RWU bit in the CR1 register */
00498     UART4->CR2 &= ((uint8_t)~UART4_CR2_RWU);
00499   }
00500 }
00501 
00502 /**
00503   * @brief  Returns the most recent received data by the UART4 peripheral.
00504   * @param  None
00505   * @retval Received Data
00506   */
00507 uint8_t UART4_ReceiveData8(void)
00508 {
00509   return ((uint8_t)UART4->DR);
00510 }
00511 
00512 /**
00513   * @brief  Returns the most recent received data by the UART4 peripheral.
00514   * @param  None  
00515   * @retval Received Data
00516   */
00517 uint16_t UART4_ReceiveData9(void)
00518 {
00519   uint16_t temp = 0;
00520   
00521   temp = ((uint16_t)(((uint16_t)((uint16_t)UART4->CR1 & (uint16_t)UART4_CR1_R8)) << 1));
00522   
00523   return (uint16_t)((((uint16_t)UART4->DR) | temp) & ((uint16_t)0x01FF));
00524 }
00525 
00526 /**
00527   * @brief  Transmits 8 bit data through the UART4 peripheral.
00528   * @param  Data: the data to transmit.
00529   * @retval None
00530   */
00531 void UART4_SendData8(uint8_t Data)
00532 {
00533   /* Transmit Data */
00534   UART4->DR = Data;
00535 }
00536 
00537 /**
00538   * @brief  Transmits 9 bit data through the UART4 peripheral.
00539   * @param  Data: the data to transmit.
00540   * @retval None
00541   */
00542 void UART4_SendData9(uint16_t Data)
00543 {
00544   /* Clear the transmit data bit 8 */
00545   UART4->CR1 &= ((uint8_t)~UART4_CR1_T8);                  
00546   
00547   /* Write the transmit data bit [8] */
00548   UART4->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & UART4_CR1_T8); 
00549   
00550   /* Write the transmit data bit [0:7] */
00551   UART4->DR   = (uint8_t)(Data);                    
00552 }
00553 
00554 /**
00555   * @brief  Transmits break characters.
00556   * @param  None
00557   * @retval None
00558   */
00559 void UART4_SendBreak(void)
00560 {
00561   UART4->CR2 |= UART4_CR2_SBK;
00562 }
00563 
00564 /**
00565   * @brief  Sets the address of the UART4 node.
00566   * @param  UART4_Address: Indicates the address of the UART4 node.
00567   * @retval None
00568   */
00569 void UART4_SetAddress(uint8_t UART4_Address)
00570 {
00571   /*assert_param for x UART4_Address*/
00572   assert_param(IS_UART4_ADDRESS_OK(UART4_Address));
00573   
00574   /* Clear the UART4 address */
00575   UART4->CR4 &= ((uint8_t)~UART4_CR4_ADD);
00576   /* Set the UART4 address node */
00577   UART4->CR4 |= UART4_Address;
00578 }
00579 
00580 /**
00581   * @brief  Sets the specified UART4 guard time.
00582   * @note   SmartCard Mode should be Enabled  
00583   * @param  UART4_GuardTime: specifies the guard time.
00584   * @retval None
00585   */
00586 void UART4_SetGuardTime(uint8_t UART4_GuardTime)
00587 {
00588   /* Set the UART4 guard time */
00589   UART4->GTR = UART4_GuardTime;
00590 }
00591 
00592 /**
00593   * @brief  Sets the system clock prescaler.
00594   * @note   IrDA Low Power mode or smartcard mode should be enabled
00595   * @note   This function is related to SmartCard and IrDa mode.
00596   * @param  UART4_Prescaler: specifies the prescaler clock.
00597   *         This parameter can be one of the following values:
00598   *         @par IrDA Low Power Mode
00599   *         The clock source is divided by the value given in the register (8 bits)
00600   *         - 0000 0000 Reserved
00601   *         - 0000 0001 divides the clock source by 1
00602   *         - 0000 0010 divides the clock source by 2
00603   *         - ...
00604   *        @par Smart Card Mode
00605   *        The clock source is divided by the value given in the register
00606   *        (5 significant bits) multiplied by 2
00607   *         - 0 0000 Reserved
00608   *         - 0 0001 divides the clock source by 2
00609   *         - 0 0010 divides the clock source by 4
00610   *         - 0 0011 divides the clock source by 6
00611   *         - ...
00612   * @retval None
00613   */
00614 void UART4_SetPrescaler(uint8_t UART4_Prescaler)
00615 {
00616   /* Load the UART4 prescaler value*/
00617   UART4->PSCR = UART4_Prescaler;
00618 }
00619 
00620 /**
00621   * @brief  Checks whether the specified UART4 flag is set or not.
00622   * @param  UART4_FLAG specifies the flag to check.
00623   *         This parameter can be any of the @ref UART4_Flag_TypeDef enumeration.
00624   * @retval FlagStatus (SET or RESET)
00625   */
00626 FlagStatus UART4_GetFlagStatus(UART4_Flag_TypeDef UART4_FLAG)
00627 {
00628   FlagStatus status = RESET;
00629   
00630   /* Check parameters */
00631   assert_param(IS_UART4_FLAG_OK(UART4_FLAG));
00632   
00633   /* Check the status of the specified UART4 flag*/
00634   if(UART4_FLAG == UART4_FLAG_LBDF)
00635   {
00636     if((UART4->CR4 & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
00637     {
00638       /* UART4_FLAG is set*/
00639       status = SET;
00640     }
00641     else
00642     {
00643       /* UART4_FLAG is reset*/
00644       status = RESET;
00645     }
00646   }
00647   else if(UART4_FLAG == UART4_FLAG_SBK)
00648   {
00649     if((UART4->CR2 & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
00650     {
00651       /* UART4_FLAG is set*/
00652       status = SET;
00653     }
00654     else
00655     {
00656       /* UART4_FLAG is reset*/
00657       status = RESET;
00658     }
00659   }
00660   else if((UART4_FLAG == UART4_FLAG_LHDF) || (UART4_FLAG == UART4_FLAG_LSF))
00661   {
00662     if((UART4->CR6 & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
00663     {
00664       /* UART4_FLAG is set*/
00665       status = SET;
00666     }
00667     else
00668     {
00669       /* UART4_FLAG is reset*/
00670       status = RESET;
00671     }
00672   }
00673   else
00674   {
00675     if((UART4->SR & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
00676     {
00677       /* UART4_FLAG is set*/
00678       status = SET;
00679     }
00680     else
00681     {
00682       /* UART4_FLAG is reset*/
00683       status = RESET;
00684     }
00685   }
00686   
00687   /* Return the UART4_FLAG status*/
00688   return  status;
00689 }
00690 
00691 /**
00692   * @brief  Clears the UART4 flags.
00693   * @param  UART4_FLAG specifies the flag to clear
00694   *         This parameter can be any combination of the following values:
00695   *         - UART4_FLAG_LBDF: LIN Break detection flag.
00696   *         - UART4_FLAG_LHDF: LIN Header detection flag.
00697   *         - UART4_FLAG_LSF: LIN synchrone field flag.
00698   *         - UART4_FLAG_RXNE: Receive data register not empty flag.
00699   * @note:
00700   *         - PE (Parity error), FE (Framing error), NE (Noise error), 
00701   *           OR (OverRun error) and IDLE (Idle line detected) flags are cleared
00702   *           by software sequence: a read operation to UART4_SR register 
00703   *           (UART4_GetFlagStatus())followed by a read operation to UART4_DR 
00704   *           register(UART4_ReceiveData8() or UART4_ReceiveData9()).
00705   *        
00706   *         - RXNE flag can be also cleared by a read to the UART4_DR register
00707   *           (UART4_ReceiveData8()or UART4_ReceiveData9()).
00708   *
00709   *         - TC flag can be also cleared by software sequence: a read operation
00710   *           to UART4_SR register (UART4_GetFlagStatus()) followed by a write 
00711   *           operation to UART4_DR register (UART4_SendData8() or UART4_SendData9()).
00712   *             
00713   *         - TXE flag is cleared only by a write to the UART4_DR register 
00714   *           (UART4_SendData8() or UART4_SendData9()).
00715   *             
00716   *         - SBK flag is cleared during the stop bit of break.
00717   * @retval None
00718   */
00719 void UART4_ClearFlag(UART4_Flag_TypeDef UART4_FLAG)
00720 {
00721   assert_param(IS_UART4_CLEAR_FLAG_OK(UART4_FLAG));
00722   
00723   /*  Clear the Receive Register Not Empty flag */
00724   if(UART4_FLAG == UART4_FLAG_RXNE)
00725   {
00726     UART4->SR = (uint8_t)~(UART4_SR_RXNE);
00727   }
00728   /*  Clear the LIN Break Detection flag */
00729   else if(UART4_FLAG == UART4_FLAG_LBDF)
00730   {
00731     UART4->CR4 &= (uint8_t)(~UART4_CR4_LBDF);
00732   }
00733   /*  Clear the LIN Header Detection Flag */
00734   else if(UART4_FLAG == UART4_FLAG_LHDF)
00735   {
00736     UART4->CR6 &= (uint8_t)(~UART4_CR6_LHDF);
00737   }
00738   /*  Clear the LIN Synch Field flag */
00739   else
00740   {
00741     UART4->CR6 &= (uint8_t)(~UART4_CR6_LSF);
00742   }
00743 }
00744 
00745 /**
00746   * @brief  Checks whether the specified UART4 interrupt has occurred or not.
00747   * @param  UART4_IT: Specifies the UART4 interrupt pending bit to check.
00748   *         This parameter can be one of the following values:
00749   *         - UART4_IT_LBDF:  LIN Break detection interrupt
00750   *         - UART4_IT_TXE:  Transmit Data Register empty interrupt
00751   *         - UART4_IT_TC:   Transmission complete interrupt
00752   *         - UART4_IT_RXNE: Receive Data register not empty interrupt
00753   *         - UART4_IT_IDLE: Idle line detection interrupt
00754   *         - UART4_IT_OR:  OverRun Error interrupt
00755   *         - UART4_IT_PE:   Parity Error interrupt
00756   * @retval The state of UART4_IT (SET or RESET).
00757   */
00758 ITStatus UART4_GetITStatus(UART4_IT_TypeDef UART4_IT)
00759 {
00760   ITStatus pendingbitstatus = RESET;
00761   uint8_t itpos = 0;
00762   uint8_t itmask1 = 0;
00763   uint8_t itmask2 = 0;
00764   uint8_t enablestatus = 0;
00765   
00766   /* Check parameters */
00767   assert_param(IS_UART4_GET_IT_OK(UART4_IT));
00768   
00769   /* Get the UART4 IT index*/
00770   itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART4_IT & (uint8_t)0x0F));
00771   /* Get the UART4 IT index*/
00772   itmask1 = (uint8_t)((uint8_t)UART4_IT >> (uint8_t)4);
00773   /* Set the IT mask*/
00774   itmask2 = (uint8_t)((uint8_t)1 << itmask1);
00775   
00776   /* Check the status of the specified UART4 pending bit*/
00777   if(UART4_IT == UART4_IT_PE)
00778   {
00779     /* Get the UART4_ITPENDINGBIT enable bit status*/
00780     enablestatus = (uint8_t)((uint8_t)UART4->CR1 & itmask2);
00781     /* Check the status of the specified UART4 interrupt*/
00782     
00783     if(((UART4->SR & itpos) != (uint8_t)0x00) && enablestatus)
00784     {
00785       /* Interrupt occurred*/
00786       pendingbitstatus = SET;
00787     }
00788     else
00789     {
00790       /* Interrupt not occurred*/
00791       pendingbitstatus = RESET;
00792     }
00793   }
00794   else if(UART4_IT == UART4_IT_LBDF)
00795   {
00796     /* Get the UART4_IT enable bit status*/
00797     enablestatus = (uint8_t)((uint8_t)UART4->CR4 & itmask2);
00798     /* Check the status of the specified UART4 interrupt*/
00799     if(((UART4->CR4 & itpos) != (uint8_t)0x00) && enablestatus)
00800     {
00801       /* Interrupt occurred*/
00802       pendingbitstatus = SET;
00803     }
00804     else
00805     {
00806       /* Interrupt not occurred*/
00807       pendingbitstatus = RESET;
00808     }
00809   }
00810   else if(UART4_IT == UART4_IT_LHDF)
00811   {
00812     /* Get the UART4_IT enable bit status*/
00813     enablestatus = (uint8_t)((uint8_t)UART4->CR6 & itmask2);
00814     /* Check the status of the specified UART4 interrupt*/
00815     if(((UART4->CR6 & itpos) != (uint8_t)0x00) && enablestatus)
00816     {
00817       /* Interrupt occurred*/
00818       pendingbitstatus = SET;
00819     }
00820     else
00821     {
00822       /* Interrupt not occurred*/
00823       pendingbitstatus = RESET;
00824     }
00825   }
00826   else
00827   {
00828     /* Get the UART4_IT enable bit status*/
00829     enablestatus = (uint8_t)((uint8_t)UART4->CR2 & itmask2);
00830     /* Check the status of the specified UART4 interrupt*/
00831     if(((UART4->SR & itpos) != (uint8_t)0x00) && enablestatus)
00832     {
00833       /* Interrupt occurred*/
00834       pendingbitstatus = SET;
00835     }
00836     else
00837     {
00838       /* Interrupt not occurred*/
00839       pendingbitstatus = RESET;
00840     }
00841   }
00842   /* Return the UART4_IT status*/
00843   return  pendingbitstatus;
00844 }
00845 
00846 /**
00847   * @brief  Clears the UART4 pending flags.
00848   * @param  UART4_IT specifies the pending bit to clear
00849   *         This parameter can be one of the following values:
00850   *         - UART4_IT_LBDF:  LIN Break detection interrupt
00851   *         - UART4_IT_LHDF:  LIN Header detection interrupt
00852   *         - UART4_IT_RXNE: Receive Data register not empty interrupt.
00853   * @note
00854   *         - PE (Parity error), FE (Framing error), NE (Noise error), 
00855   *           OR (OverRun error) and IDLE (Idle line detected) pending bits are 
00856   *           cleared by software sequence: a read operation to UART4_SR register
00857   *           (UART4_GetITStatus()) followed by a read operation to UART4_DR register
00858   *           (UART4_ReceiveData8() or UART4_ReceiveData9()).
00859   *             
00860   *         - RXNE pending bit can be also cleared by a read to the UART4_DR 
00861   *           register (UART4_ReceiveData8() or UART4_ReceiveData9()).
00862   *              
00863   *         - TC (Transmit complete) pending bit can be cleared by software 
00864   *           sequence: a read operation to UART4_SR register 
00865   *           (UART4_GetITStatus()) followed by a write operation to UART4_DR 
00866   *           register (UART4_SendData8()or UART4_SendData9()).
00867   *               
00868   *         - TXE pending bit is cleared only by a write to the UART4_DR register
00869   *           (UART4_SendData8() or UART4_SendData9()).
00870   * @retval None
00871   */
00872 void UART4_ClearITPendingBit(UART4_IT_TypeDef UART4_IT)
00873 {
00874   assert_param(IS_UART4_CLEAR_IT_OK(UART4_IT));
00875   
00876   /*  Clear the Receive Register Not Empty pending bit */
00877   if(UART4_IT == UART4_IT_RXNE)
00878   {
00879     UART4->SR = (uint8_t)~(UART4_SR_RXNE);
00880   }
00881   /*  Clear the LIN Break Detection pending bit */
00882   else if(UART4_IT == UART4_IT_LBDF)
00883   {
00884     UART4->CR4 &= (uint8_t)~(UART4_CR4_LBDF);
00885   }
00886   /*  Clear the LIN Header Detection pending bit */
00887   else
00888   {
00889     UART4->CR6 &= (uint8_t)(~UART4_CR6_LHDF);
00890   }
00891 }
00892 
00893 /**
00894   * @}
00895   */
00896 
00897 /**
00898   * @}
00899   */
00900 
00901 
00902 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com