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

STM8S/A

STM8S_StdPeriph_Examples/UART1/UART1_Synchronous/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    UART1_Synchronous\main.c
00004   * @brief   This file contains the main function for UART1 Synchronous mode example.
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 
00031 /**
00032   * @addtogroup UART1_Synchronous
00033   * @{
00034   */
00035 
00036 /* Private typedef -----------------------------------------------------------*/
00037 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00038 /* Private define ------------------------------------------------------------*/
00039 #define TxBufferSize1   (countof(TxBuffer1) - 1)
00040 #define TxBufferSize2   (countof(TxBuffer2) - 1)
00041 /* Private macro -------------------------------------------------------------*/
00042 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00043 /* Private variables ---------------------------------------------------------*/
00044 uint8_t TxBuffer1[] = "UART1 Example: UART1 -> SPI using TXE and RXNE Flags";
00045 uint8_t TxBuffer2[] = "UART1 Example: SPI -> UART1 using TXE and RXNE Flags";
00046 uint8_t RxBuffer1[TxBufferSize2] = {0};
00047 uint8_t RxBuffer2[TxBufferSize1] = {0};
00048 uint8_t NbrOfDataToRead = TxBufferSize1;
00049 __IO uint8_t TxCounter = 0, RxCounter = 0;
00050 __IO TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00051 
00052 /* Private function prototypes -----------------------------------------------*/
00053 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00054 void Delay (uint16_t nCount);
00055 static void CLK_Config(void);
00056 static void UART1_Config(void);
00057 static void SPI_Config(void);
00058 /* Private functions ----------------------------------------------------------*/
00059 
00060 /**
00061   * @brief  Main program.
00062   * @param  None
00063   * @retval None
00064   */
00065 void main(void)
00066 {
00067   /* Clock configuration -----------------------------------------*/
00068   CLK_Config();  
00069 
00070   /* UART1 configuration -----------------------------------------*/
00071   UART1_Config();
00072  
00073   /* SPI configuration -----------------------------------------*/
00074   SPI_Config();  
00075   
00076   /* Enable the UART1*/
00077   UART1_Cmd(ENABLE);
00078   
00079   Delay(0xFFF);
00080   
00081   /* Enable the SPI*/
00082   SPI_Cmd(ENABLE);
00083   
00084     while (NbrOfDataToRead--)
00085     {
00086         /* Wait until end of transmit */
00087         while (SPI_GetFlagStatus(SPI_FLAG_TXE)== RESET)
00088         {
00089         }
00090         /* Write one byte in the SPI Transmit Data Register */
00091         SPI_SendData(TxBuffer2[TxCounter]);
00092         /* Write one byte in the UART1 Transmit Data Register */
00093         UART1_SendData8(TxBuffer1[TxCounter++]);
00094         /* Wait until end of transmit */
00095         while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET)
00096         {
00097         }
00098         /* Wait the byte is entirely received by UART1 */
00099         while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET)
00100         {
00101         }
00102         /* Store the received byte in the RxBuffer1 */
00103         RxBuffer1[RxCounter] = UART1_ReceiveData8();
00104         /* Wait the byte is entirely received by SPI */
00105         while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET)
00106         {
00107         }
00108         /* Store the received byte in the RxBuffer2 */
00109         RxBuffer2[RxCounter++] = SPI_ReceiveData();
00110     }
00111 
00112     /* Check the received data with the sent ones */
00113     TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
00114     /* TransferStatus = PASSED, if the data transmitted from UART1 and received by SPI are the same */
00115     /* TransferStatus = FAILED, if the data transmitted from UART1 and received by SPI are different */
00116     TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
00117     /* TransferStatus = PASSED, if the data transmitted from SPI and received by UART1 are the same */
00118     /* TransferStatus = FAILED, if the data transmitted from SPI and received by UART1 are different */
00119 
00120     while (1)
00121     {}
00122 }
00123 
00124 /**
00125   * @brief  Configure system clock to run at 16Mhz
00126   * @param  None
00127   * @retval None
00128   */
00129 static void CLK_Config(void)
00130 {
00131     /* Initialization of the clock */
00132     /* Clock divider to HSI/1 */
00133     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00134 }
00135 
00136 /**
00137   * @brief  Configure UART1 for the synchronous mode communication
00138   * @param  None
00139   * @retval None
00140   */
00141 static void UART1_Config(void)
00142 {
00143   /* UART1 configured as follow:
00144           - Word Length = 8 Bits
00145           - 1 Stop Bit
00146           - No parity
00147           - BaudRate = 9600 baud
00148           - UART1 Clock enabled
00149           - Polarity Low
00150           - Phase Middle
00151           - Last Bit enabled
00152           - Receive and transmit enabled
00153    */
00154   UART1_DeInit();
00155   UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, 
00156               (UART1_SyncMode_TypeDef)(UART1_SYNCMODE_CLOCK_ENABLE | UART1_SYNCMODE_CPOL_LOW |UART1_SYNCMODE_CPHA_MIDDLE |UART1_SYNCMODE_LASTBIT_ENABLE),
00157               UART1_MODE_TXRX_ENABLE);
00158   UART1_Cmd(DISABLE);
00159 }
00160 
00161 /**
00162   * @brief  Configure SPI for the full duplex communication with UART1
00163   * @param  None
00164   * @retval None
00165   */
00166 static void SPI_Config(void)
00167 {
00168   SPI_DeInit();
00169   /* Initialize SPI in Slave mode  */
00170   SPI_Init(SPI_FIRSTBIT_LSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_SLAVE, SPI_CLOCKPOLARITY_LOW,
00171            SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT,(uint8_t)0x07);
00172 }
00173 
00174 /**
00175   * @brief   Compares two buffers.
00176   * @param   pBuffer1 First buffer to be compared.
00177   * @param   pBuffer2 Second buffer to be compared.
00178   * @param   BufferLength Buffer's length
00179   * @retval  TestStatus Status of buffer comparison
00180   * - PASSED: pBuffer1 identical to pBuffer2
00181   * - FAILED: pBuffer1 differs from pBuffer2
00182   */
00183 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00184 {
00185     while (BufferLength--)
00186     {
00187         if (*pBuffer1 != *pBuffer2)
00188         {
00189             return FAILED;
00190         }
00191 
00192         pBuffer1++;
00193         pBuffer2++;
00194     }
00195 
00196     return PASSED;
00197 }
00198 
00199 /**
00200   * @brief  Delay.
00201   * @param  nCount
00202   * @retval None
00203   */
00204 void Delay(uint16_t nCount)
00205 {
00206     /* Decrement nCount value */
00207     while (nCount != 0)
00208     {
00209         nCount--;
00210     }
00211 }
00212 
00213 #ifdef USE_FULL_ASSERT
00214 
00215 /**
00216   * @brief  Reports the name of the source file and the source line number
00217   *   where the assert_param error has occurred.
00218   * @param file: pointer to the source file name
00219   * @param line: assert_param error line source number
00220   * @retval None
00221   */
00222 void assert_failed(uint8_t* file, uint32_t line)
00223 { 
00224   /* User can add his own implementation to report the file name and line number,
00225      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00226 
00227   /* Infinite loop */
00228   while (1)
00229   {
00230   }
00231 }
00232 #endif
00233 
00234 /**
00235   * @}
00236   */
00237 
00238 
00239 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

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