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

STM8S/A

STM8S_StdPeriph_Examples/UART1/UART1_HalfDuplex/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    UART1_HalfDuplex\main.c
00004   * @author  MCD Application Team
00005   * @version  V2.2.0
00006   * @date     30-September-2014
00007   * @brief   This file contains the main function for UART1 in Half-Duplex mode example.
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_HalfDuplex
00033   * @{
00034   */
00035 
00036 /* Private typedef -----------------------------------------------------------*/
00037 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00038 
00039 /* Private define ------------------------------------------------------------*/
00040 #define TxBufferSize1   (countof(TxBuffer1) - 1)
00041 #define TxBufferSize2   (countof(TxBuffer2) - 1)
00042 /* Private macro -------------------------------------------------------------*/
00043 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00044 /* To run the transmitter connect the UART1_TX on the UART3_RX, uncomment the line 
00045    "#define transmitter" and comment the line "#define receiver" */
00046 #define transmitter 
00047 
00048 /* To run the transmitter connect the UART1_TX on the UART3_TX , uncomment the line 
00049   "#define receiver" and comment the line "#define transmitter" */
00050 #define receiver 
00051 /* Private variables ---------------------------------------------------------*/
00052 uint8_t TxBuffer1[] = "HalfDuplex Example: UART1 -> UART3 using HalfDuplex mode";
00053 uint8_t TxBuffer2[] = "HalfDuplex Example: UART3 -> UART1 using HalfDuplex mode";
00054 uint8_t RxBuffer1[TxBufferSize2]={0};
00055 uint8_t RxBuffer2[TxBufferSize1]={0};
00056 uint8_t NbrOfDataToRead1 = TxBufferSize2;
00057 uint8_t NbrOfDataToRead2 = TxBufferSize1;
00058 uint8_t TxCounter1 = 0, RxCounter1 = 0;
00059 uint8_t TxCounter2 = 0, RxCounter2 = 0;
00060 __IO TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00061 
00062 /* Private function prototypes -----------------------------------------------*/
00063 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00064 static void CLK_Config(void);
00065 static void UART_Config(void);
00066 static void GPIO_Config(void);
00067 /* Private functions ---------------------------------------------------------*/
00068 
00069 /**
00070   * @brief  Main program.
00071   * @param  None
00072   * @retval None
00073   */
00074 void main(void)
00075 {
00076   /* GPIO configuration -----------------------------------------*/
00077   GPIO_Config();
00078 
00079   /* CLK configuration -----------------------------------------*/
00080   CLK_Config();
00081 
00082   /* UART configuration -----------------------------------------*/
00083   UART_Config();  
00084 
00085 #ifdef transmitter
00086 
00087     while (NbrOfDataToRead2--)
00088     {
00089         /* Wait until end of transmit */
00090         while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET)
00091         {
00092         }
00093         /* Write one byte in the UART1 Transmit Data Register */
00094         UART1_SendData8(TxBuffer1[TxCounter1++]);
00095 
00096         /* Wait the byte is entirely received by UART3 */
00097         while (UART3_GetFlagStatus(UART3_FLAG_RXNE) == RESET)
00098         {
00099         }
00100         /* Store the received byte in the RxBuffer2 */
00101         RxBuffer2[RxCounter2++] = UART3_ReceiveData8();
00102     }
00103     /* Check the received data with the send ones */
00104 
00105     TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
00106     /* TransferStatus = PASSED, if the data transmitted from UART1 and received 
00107        by UART3 are the same */
00108     /* TransferStatus = FAILED, if the data transmitted from UART1 and received 
00109        by UART3 are different */
00110 #endif
00111 #ifdef receiver
00112     /* Clear the UART1 Data Register */
00113     UART1_ReceiveData8();
00114 
00115     while (NbrOfDataToRead1--)
00116     {
00117         /* Wait until end of transmit */
00118         while (UART3_GetFlagStatus(UART3_FLAG_TXE)== RESET)
00119         {
00120         }
00121         /* Write one byte in the UART3 Transmit Data Register */
00122         UART3_SendData8(TxBuffer2[TxCounter2++]);
00123 
00124         /* Wait the byte is entirely received by UART1 */
00125         while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET)
00126         {
00127         }
00128         /* Store the received byte in the RxBuffer1 */
00129         RxBuffer1[RxCounter1++] = UART1_ReceiveData8();
00130     }
00131 
00132     /* Check the received data with the send ones */
00133     TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
00134     /* TransferStatus = PASSED, if the data transmitted from UART3 and received 
00135        by UART1 are the same */
00136     /* TransferStatus = FAILED, if the data transmitted from UART3 and received 
00137        by UART1 are different */
00138 #endif
00139     while (1)
00140   {}
00141 }
00142 
00143 /**
00144   * @brief  Configure system clock to run at 16Mhz
00145   * @param  None
00146   * @retval None
00147   */
00148 static void CLK_Config(void)
00149 {
00150     /* Initialization of the clock */
00151     /* Clock divider to HSI/1 */
00152     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00153 }
00154 
00155 /**
00156   * @brief  Configure UART1 Tx pin as Output open-drain high-impedance level 
00157   * @param  None
00158   * @retval None
00159   */
00160 static void GPIO_Config(void)
00161 {
00162     /* Set PA5 as Output open-drain high-impedance level (UART1_Tx)*/
00163     GPIO_Init(GPIOA, GPIO_PIN_5, GPIO_MODE_OUT_OD_HIZ_FAST);
00164 }
00165 
00166 /**
00167   * @brief  UART1 and UART3 Configuration for half duplex communication
00168   * @param  None
00169   * @retval None
00170   */
00171 static void UART_Config(void)
00172 {
00173   /* UART1 and UART3 configured as follow:
00174         - BaudRate = 230400 baud  
00175         - Word Length = 8 Bits
00176         - One Stop Bit
00177         - No parity
00178         - Receive and transmit enabled
00179   */
00180   UART1_DeInit();
00181   UART1_Init((uint32_t)230400, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
00182               UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
00183 
00184   UART3_DeInit();
00185   UART3_Init((uint32_t)230400, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO,
00186               UART3_MODE_TXRX_ENABLE);
00187    
00188   /* Enable UART1 Half Duplex Mode*/
00189   UART1_HalfDuplexCmd(ENABLE);
00190 }
00191 
00192 /**
00193   * @brief Compares two buffers.
00194   * @param pBuffer1 First buffer to be compared.
00195   * @param pBuffer2 Second buffer to be compared.
00196   * @param BufferLength Buffer's length
00197   * @retval TestStatus Status of buffer comparison
00198   * - PASSED: pBuffer1 identical to pBuffer2
00199   * - FAILED: pBuffer1 differs from pBuffer2
00200   * @par Required preconditions:
00201   * None
00202   */
00203 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00204 {
00205     while (BufferLength--)
00206     {
00207         if (*pBuffer1 != *pBuffer2)
00208         {
00209             return FAILED;
00210         }
00211 
00212         pBuffer1++;
00213         pBuffer2++;
00214     }
00215 
00216     return PASSED;
00217 }
00218 #ifdef USE_FULL_ASSERT
00219 
00220 /**
00221   * @brief  Reports the name of the source file and the source line number
00222   *   where the assert_param error has occurred.
00223   * @param file: pointer to the source file name
00224   * @param line: assert_param error line source number
00225   * @retval None
00226   */
00227 void assert_failed(uint8_t* file, uint32_t line)
00228 { 
00229   /* User can add his own implementation to report the file name and line number,
00230      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00231 
00232   /* Infinite loop */
00233   while (1)
00234   {
00235   }
00236 }
00237 #endif
00238 
00239 /**
00240   * @}
00241   */
00242 
00243 
00244 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

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