STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_StdPeriph_Examples/UART1/UART1_Interrupt/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file UART1_Interrupt\main.c
00004   * @brief This file contains the main function for a basic communication between UART1 and UART3 using interrupts.
00005   * @author  MCD Application Team
00006   * @version  V2.2.0
00007   * @date     30-September-2014
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.h"
00030 #include "main.h"
00031 
00032 /**
00033   * @addtogroup UART1_Interrupt
00034   * @{
00035   */
00036 /* Private typedef -----------------------------------------------------------*/
00037 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
00038 /* Private variables ---------------------------------------------------------*/
00039 uint8_t TxBuffer1[] = "UART1 Interrupt Example: UART1 -> UART3 using Interrupt";
00040 uint8_t TxBuffer2[] = "UART1 Interrupt Example: UART3 -> UART1 using Interrupt";  
00041 uint8_t RxBuffer1[RxBufferSize1];
00042 uint8_t RxBuffer2[RxBufferSize2];
00043 __IO uint8_t TxCounter1 = 0x00;
00044 __IO uint8_t TxCounter2 = 0x00;
00045 __IO uint8_t RxCounter1 = 0x00;
00046 __IO uint8_t RxCounter2 = 0x00;
00047 uint8_t NbrOfDataToTransfer1 = TxBufferSize1;
00048 uint8_t NbrOfDataToTransfer2 = TxBufferSize2;
00049 uint8_t NbrOfDataToRead1 = RxBufferSize1;
00050 uint8_t NbrOfDataToRead2 = RxBufferSize2;
00051 __IO TestStatus TransferStatus1 = FAILED;
00052 __IO TestStatus TransferStatus2 = FAILED;
00053 
00054 /* Private function prototypes -----------------------------------------------*/
00055 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00056 static void CLK_Config(void);
00057 static void UART_Config(void);
00058 
00059 /* Private functions ---------------------------------------------------------*/
00060 /**
00061   * @brief  Main program.
00062   * @param  None
00063   * @retval None
00064   */
00065 void main(void)
00066 {
00067   /* CLK configuration -----------------------------------------*/
00068   CLK_Config();
00069 
00070   /* UART configuration -----------------------------------------*/
00071   UART_Config();  
00072 
00073     /* Wait until end of transmission from UART1 to UART3 */
00074     while (GetVar_RxCounter2() < GetVar_NbrOfDataToTransfer1())
00075     {
00076     }
00077     /* Enable UART1 Receive and UART3 Transmit interrupt */
00078     UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
00079     UART3_ITConfig(UART3_IT_TXE, ENABLE);
00080     /* Wait until end of transmission from UART3 to UART1 */
00081     while (GetVar_RxCounter1() < GetVar_NbrOfDataToTransfer2())
00082     {
00083     }
00084 
00085     /* Check the received data with the send ones */
00086     TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);
00087     /* TransferStatus1 = PASSED, if the data transmitted from UART3 and
00088        received by UART1 are the same */
00089     /* TransferStatus1 = FAILED, if the data transmitted from UART3 and
00090        received by UART1 are different */
00091     TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);
00092     /* TransferStatus2 = PASSED, if the data transmitted from UART1 and
00093        received by UART3 are the same */
00094     /* TransferStatus2 = FAILED, if the data transmitted from UART1 and
00095        received by UART3 are different */
00096 
00097     while (1)
00098   {}
00099 }
00100 
00101 /**
00102   * @brief  Configure system clock to run at 16Mhz
00103   * @param  None
00104   * @retval None
00105   */
00106 static void CLK_Config(void)
00107 {
00108     /* Initialization of the clock */
00109     /* Clock divider to HSI/1 */
00110     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00111 }
00112 
00113 /**
00114   * @brief  UART1 and UART3 Configuration for interrupt communication
00115   * @param  None
00116   * @retval None
00117   */
00118 static void UART_Config(void)
00119 {
00120   /* Deinitializes the UART1 and UART3 peripheral */
00121     UART1_DeInit();
00122     UART3_DeInit();
00123     /* UART1 and UART3 configuration -------------------------------------------------*/
00124     /* UART1 and UART3 configured as follow:
00125           - BaudRate = 9600 baud  
00126           - Word Length = 8 Bits
00127           - One Stop Bit
00128           - No parity
00129           - Receive and transmit enabled
00130           - UART1 Clock disabled
00131      */
00132     /* Configure the UART1 */
00133     UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
00134                 UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
00135     
00136     /* Enable UART1 Transmit interrupt*/
00137     UART1_ITConfig(UART1_IT_TXE, ENABLE);
00138     
00139     /* Configure the UART3 */
00140                 UART3_Init((uint32_t)9600, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO,
00141                 UART3_MODE_TXRX_ENABLE);
00142 
00143     /* Enable UART3 Receive interrupt */
00144     UART3_ITConfig(UART3_IT_RXNE_OR, ENABLE);
00145     
00146     /* Enable general interrupts */
00147     enableInterrupts();    
00148 }
00149 
00150 /**
00151   * @brief Compares two buffers.
00152   * @param[in] pBuffer1 First buffer to be compared.
00153   * @param[in] pBuffer2 Second buffer to be compared.
00154   * @param[in] BufferLength Buffer's length
00155   * @retval TestStatus Status of buffer comparison
00156   * - PASSED: pBuffer1 identical to pBuffer2
00157   * - FAILED: pBuffer1 differs from pBuffer2
00158   * @par Required preconditions:
00159   * None
00160   */
00161 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00162 {
00163     while (BufferLength--)
00164     {
00165         if (*pBuffer1 != *pBuffer2)
00166         {
00167             return FAILED;
00168         }
00169 
00170         pBuffer1++;
00171         pBuffer2++;
00172     }
00173 
00174     return PASSED;
00175 }
00176 /**
00177   * @brief Returns TxCounter1 value.
00178   * @param None
00179   * @retval Transmit counter index1
00180   * - uint8_t: TxCounter1
00181   * @par Required preconditions:
00182   * None
00183   */
00184 uint8_t GetVar_TxCounter1(void)
00185 {
00186 return TxCounter1;
00187 }
00188 /**
00189   * @brief Returns TxCounter2 value.
00190   * @param None
00191   * @retval Transmit counter index2
00192   * - uint8_t: TxCounter2
00193   * @par Required preconditions:
00194   * None
00195   */
00196 uint8_t GetVar_TxCounter2(void)
00197 {
00198 return TxCounter2;
00199 }
00200 /**
00201   * @brief Returns RxCounter1 value.
00202   * @param None
00203   * @retval Receive counter index1
00204   * - uint8_t: RxCounter1
00205   * @par Required preconditions:
00206   * None
00207   */
00208 uint8_t GetVar_RxCounter1(void)
00209 {
00210 return RxCounter1;
00211 }
00212 /**
00213   * @brief Returns RxCounter2 value.
00214   * @param None
00215   * @retval Receive counter index2
00216   * - uint8_t: RxCounter2
00217   * @par Required preconditions:
00218   * None
00219   */
00220 uint8_t GetVar_RxCounter2(void)
00221 {
00222 return RxCounter2;
00223 }
00224 /**
00225   * @brief Increments TxCounter1 variable and return its value 
00226   * @param None
00227   * @retval Transmit counter index1++
00228   * - uint8_t: TxCounter1++
00229   * @par Required preconditions:
00230   * None
00231   */
00232 uint8_t IncrementVar_TxCounter1(void)
00233 {
00234         return TxCounter1++;
00235 }
00236 /**
00237   * @brief Increments TxCounter2 variable and return its value 
00238   * @param None
00239   * @retval Transmit counter index2++
00240   * - uint8_t: TxCounter2++
00241   * @par Required preconditions:
00242   * None
00243   */
00244 uint8_t IncrementVar_TxCounter2(void)
00245 {
00246         return TxCounter2++;
00247 }
00248 /**
00249   * @brief Increments RxCounter1 variable and return its value 
00250   * @param None
00251   * @retval Receive counter index1++
00252   * - uint8_t: RxCounter1++
00253   * @par Required preconditions:
00254   * None
00255   */
00256 uint8_t IncrementVar_RxCounter1(void)
00257 {
00258         return RxCounter1++;
00259 }
00260 /**
00261   * @brief Increments RxCounter2 variable and return its value 
00262   * @param None
00263   * @retval Receive counter index2++
00264   * - uint8_t: RxCounter2++
00265   * @par Required preconditions:
00266   * None
00267   */
00268 uint8_t IncrementVar_RxCounter2(void)
00269 {
00270         return RxCounter2++;
00271 }
00272 /**
00273   * @brief Returns NbrOfDataToTransfer1 value.
00274   * @param None
00275   * @retval Tx Buffer Size1
00276   * - uint8_t: NbrOfDataToTransfer1
00277   * @par Required preconditions:
00278   * None
00279   */
00280 uint8_t GetVar_NbrOfDataToTransfer1(void)
00281 {
00282 return NbrOfDataToTransfer1;
00283 }
00284 /**
00285   * @brief Returns NbrOfDataToTransfer2 value.
00286   * @param None
00287   * @retval Tx Buffer Size2
00288   * - uint8_t: NbrOfDataToTransfer2
00289   * @par Required preconditions:
00290   * None
00291   */
00292 uint8_t GetVar_NbrOfDataToTransfer2(void)
00293 {
00294 return NbrOfDataToTransfer2;
00295 }
00296 /**
00297   * @brief Returns NbrOfDataToRead1 value.
00298   * @param None
00299   * @retval Rx Buffer Size1
00300   * - uint8_t: NbrOfDataToRead1
00301   * @par Required preconditions:
00302   * None
00303   */
00304 uint8_t GetVar_NbrOfDataToRead1(void)
00305 {
00306 return NbrOfDataToRead1;
00307 }
00308 /**
00309   * @brief Returns NbrOfDataToRead2 value.
00310   * @param None
00311   * @retval Rx Buffer Size2
00312   * - uint8_t: NbrOfDataToRead2
00313   * @par Required preconditions:
00314   * None
00315   */
00316 uint8_t GetVar_NbrOfDataToRead2(void)
00317 {
00318 return NbrOfDataToRead2;
00319 }
00320 #ifdef USE_FULL_ASSERT
00321 
00322 /**
00323   * @brief  Reports the name of the source file and the source line number
00324   *   where the assert_param error has occurred.
00325   * @param file: pointer to the source file name
00326   * @param line: assert_param error line source number
00327   * @retval None
00328   */
00329 void assert_failed(uint8_t* file, uint32_t line)
00330 { 
00331   /* User can add his own implementation to report the file name and line number,
00332      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00333 
00334   /* Infinite loop */
00335   while (1)
00336   {
00337   }
00338 }
00339 #endif
00340 
00341 /**
00342   * @}
00343   */
00344 
00345 
00346 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

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