STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/USART/USART_Printf/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file USART/USART_Printf/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© 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 "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup USART_Printf 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 /* Private function prototypes -----------------------------------------------*/ 00044 static void USART_Config(void); 00045 00046 #ifdef __GNUC__ 00047 /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf 00048 set to 'Yes') calls __io_putchar() */ 00049 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) 00050 #else 00051 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) 00052 #endif /* __GNUC__ */ 00053 00054 /* Private functions ---------------------------------------------------------*/ 00055 00056 00057 /** 00058 * @brief Main program 00059 * @param None 00060 * @retval None 00061 */ 00062 int main(void) 00063 { 00064 /*!< At this stage the microcontroller clock setting is already configured, 00065 this is done through SystemInit() function which is called from startup 00066 file (startup_stm32f0xx.s) before to branch to application main. 00067 To reconfigure the default setting of SystemInit() function, refer to 00068 system_stm32f0xx.c file 00069 */ 00070 00071 /* USART configuration */ 00072 USART_Config(); 00073 00074 /* Output a message on Hyperterminal using printf function */ 00075 printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r"); 00076 00077 /* Loop until the end of transmission */ 00078 /* The software must wait until TC=1. The TC flag remains cleared during all data 00079 transfers and it is set by hardware at the last frame�s end of transmission*/ 00080 while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET) 00081 {} 00082 00083 while (1) 00084 { 00085 } 00086 } 00087 00088 /** 00089 * @brief Configure the USART Device 00090 * @param None 00091 * @retval None 00092 */ 00093 static void USART_Config(void) 00094 { 00095 USART_InitTypeDef USART_InitStructure; 00096 00097 /* USARTx configured as follow: 00098 - BaudRate = 115200 baud 00099 - Word Length = 8 Bits 00100 - Stop Bit = 1 Stop Bit 00101 - Parity = No Parity 00102 - Hardware flow control disabled (RTS and CTS signals) 00103 - Receive and transmit enabled 00104 */ 00105 USART_InitStructure.USART_BaudRate = 115200; 00106 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 00107 USART_InitStructure.USART_StopBits = USART_StopBits_1; 00108 USART_InitStructure.USART_Parity = USART_Parity_No; 00109 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 00110 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 00111 00112 STM_EVAL_COMInit(COM1, &USART_InitStructure); 00113 } 00114 00115 /** 00116 * @brief Retargets the C library printf function to the USART. 00117 * @param None 00118 * @retval None 00119 */ 00120 PUTCHAR_PROTOTYPE 00121 { 00122 /* Place your implementation of fputc here */ 00123 /* e.g. write a character to the USART */ 00124 USART_SendData(EVAL_COM1, (uint8_t) ch); 00125 00126 /* Loop until transmit data register is empty */ 00127 while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET) 00128 {} 00129 00130 return ch; 00131 } 00132 00133 #ifdef USE_FULL_ASSERT 00134 00135 /** 00136 * @brief Reports the name of the source file and the source line number 00137 * where the assert_param error has occurred. 00138 * @param file: pointer to the source file name 00139 * @param line: assert_param error line source number 00140 * @retval None 00141 */ 00142 void assert_failed(uint8_t* file, uint32_t line) 00143 { 00144 /* User can add his own implementation to report the file name and line number, 00145 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00146 00147 /* Infinite loop */ 00148 while (1) 00149 { 00150 } 00151 } 00152 #endif 00153 00154 /** 00155 * @} 00156 */ 00157 00158 /** 00159 * @} 00160 */ 00161 00162 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/