STM32F10x_StdPeriph_Examples/USART/HyperTerminal_HwFlowControl/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/HyperTerminal_HwFlowControl/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 #include "platform_config.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup USART_HyperTerminal_HwFlowControl
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 #define TxBufferSize   (countof(TxBuffer) - 1)
00036 #define RxBufferSize   0xFF
00037 
00038 /* Private macro -------------------------------------------------------------*/
00039 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00040 
00041 /* Private variables ---------------------------------------------------------*/
00042 USART_InitTypeDef USART_InitStructure;
00043 uint8_t TxBuffer[] = "\n\rUSART Hyperterminal Hardware Flow Control Example: USART - \
00044 Hyperterminal communication using hardware flow control\n\r";
00045 uint8_t RxBuffer[RxBufferSize];
00046 uint8_t NbrOfDataToTransfer = TxBufferSize;
00047 uint8_t TxCounter = 0;
00048 uint8_t RxCounter = 0;
00049 
00050 /* Private function prototypes -----------------------------------------------*/
00051 void RCC_Configuration(void);
00052 void GPIO_Configuration(void);
00053   
00054 /* Private functions ---------------------------------------------------------*/
00055 
00056 /**
00057   * @brief   Main program
00058   * @param  None
00059   * @retval None
00060   */
00061 int main(void)
00062 {
00063   /*!< At this stage the microcontroller clock setting is already configured, 
00064        this is done through SystemInit() function which is called from startup
00065        file (startup_stm32f10x_xx.s) before to branch to application main.
00066        To reconfigure the default setting of SystemInit() function, refer to
00067        system_stm32f10x.c file
00068      */     
00069        
00070   /* System Clocks Configuration */
00071   RCC_Configuration();
00072 
00073   /* Configure the GPIO ports */
00074   GPIO_Configuration();
00075   
00076 /* USART2 configuration ------------------------------------------------------*/
00077   /* USART2 configured as follow:
00078         - BaudRate = 115200 baud  
00079         - Word Length = 8 Bits
00080         - One Stop Bit
00081         - No parity
00082         - Hardware flow control enabled (RTS and CTS signals)
00083         - Receive and transmit enabled
00084   */
00085   USART_InitStructure.USART_BaudRate = 115200;
00086   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00087   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00088   USART_InitStructure.USART_Parity = USART_Parity_No ;
00089   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
00090   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00091 
00092   USART_Init(USART2, &USART_InitStructure);
00093   /* Enable the USART2 */
00094   USART_Cmd(USART2, ENABLE);
00095 
00096 /* Communication hyperterminal-USART2 using hardware flow control -------------*/
00097   /* Send a buffer from USART to hyperterminal */  
00098   while(NbrOfDataToTransfer--)
00099   {
00100     USART_SendData(USART2, TxBuffer[TxCounter++]);
00101     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);          
00102   }
00103   
00104   /* Receive a string (Max RxBufferSize bytes) from the Hyperterminal ended by '\r' (Enter key) */
00105   do
00106   { 
00107     if((USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)&&(RxCounter < RxBufferSize))
00108     {
00109        RxBuffer[RxCounter] = USART_ReceiveData(USART2);
00110        USART_SendData(USART2, RxBuffer[RxCounter++]);
00111     }   
00112  
00113   }while((RxBuffer[RxCounter - 1] != '\r')&&(RxCounter != RxBufferSize));
00114 
00115   while (1)
00116   {
00117   }
00118 }
00119 
00120 /**
00121   * @brief  Configures the different system clocks.
00122   * @param  None
00123   * @retval None
00124   */
00125 void RCC_Configuration(void)
00126 {    
00127   /* Enable GPIOx and AFIO clocks */
00128   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx | RCC_APB2Periph_AFIO, ENABLE);
00129 
00130   /* Enable USART2 clocks */
00131   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
00132 }
00133 
00134 /**
00135   * @brief  Configures the different GPIO ports.
00136   * @param  None
00137   * @retval None
00138   */
00139 void GPIO_Configuration(void)
00140 {
00141   GPIO_InitTypeDef GPIO_InitStructure;
00142 
00143 #if defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
00144   /* Enable the USART2 Pins Software Remapping */
00145   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
00146 #endif
00147 
00148   /* Configure USART2 RTS and USART2 Tx as alternate function push-pull */
00149   GPIO_InitStructure.GPIO_Pin = GPIO_RTSPin | GPIO_TxPin;
00150   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00151   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00152   GPIO_Init(GPIOx, &GPIO_InitStructure);
00153 
00154   /* Configure USART2 CTS and USART2 Rx as input floating */
00155   GPIO_InitStructure.GPIO_Pin = GPIO_CTSPin | GPIO_RxPin;
00156   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00157   GPIO_Init(GPIOx, &GPIO_InitStructure);
00158 }
00159 
00160 #ifdef  USE_FULL_ASSERT
00161 
00162 /**
00163   * @brief  Reports the name of the source file and the source line number
00164   *         where the assert_param error has occurred.
00165   * @param  file: pointer to the source file name
00166   * @param  line: assert_param error line source number
00167   * @retval None
00168   */
00169 void assert_failed(uint8_t* file, uint32_t line)
00170 { 
00171   /* User can add his own implementation to report the file name and line number,
00172      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00173 
00174   /* Infinite loop */
00175   while (1)
00176   {
00177   }
00178 }
00179 #endif
00180 
00181 /**
00182   * @}
00183   */ 
00184 
00185 /**
00186   * @}
00187   */ 
00188 
00189 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32