STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/UART1/UART1_Printf/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file UART1_Printf\main.c 00004 * @brief This file contains the main function for: retarget the C library printf 00005 * /scanf functions to the UART1 example. 00006 * @author MCD Application Team 00007 * @version V2.2.0 00008 * @date 30-September-2014 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00013 * 00014 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00015 * You may not use this file except in compliance with the License. 00016 * You may obtain a copy of the License at: 00017 * 00018 * http://www.st.com/software_license_agreement_liberty_v2 00019 * 00020 * Unless required by applicable law or agreed to in writing, software 00021 * distributed under the License is distributed on an "AS IS" BASIS, 00022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00023 * See the License for the specific language governing permissions and 00024 * limitations under the License. 00025 * 00026 ****************************************************************************** 00027 */ 00028 00029 /* Includes ------------------------------------------------------------------*/ 00030 #include "stm8s.h" 00031 #include "stdio.h" 00032 /** 00033 * @addtogroup UART1_Printf 00034 * @{ 00035 */ 00036 /* Private typedef -----------------------------------------------------------*/ 00037 /* Private define ------------------------------------------------------------*/ 00038 #ifdef _RAISONANCE_ 00039 #define PUTCHAR_PROTOTYPE int putchar (char c) 00040 #define GETCHAR_PROTOTYPE int getchar (void) 00041 #elif defined (_COSMIC_) 00042 #define PUTCHAR_PROTOTYPE char putchar (char c) 00043 #define GETCHAR_PROTOTYPE char getchar (void) 00044 #else /* _IAR_ */ 00045 #define PUTCHAR_PROTOTYPE int putchar (int c) 00046 #define GETCHAR_PROTOTYPE int getchar (void) 00047 #endif /* _RAISONANCE_ */ 00048 /* Private macro -------------------------------------------------------------*/ 00049 /* Private variables ---------------------------------------------------------*/ 00050 /* Private function prototypes -----------------------------------------------*/ 00051 /* Private functions ---------------------------------------------------------*/ 00052 /** 00053 * @brief Main program. 00054 * @param None 00055 * @retval None 00056 */ 00057 void main(void) 00058 { 00059 char ans; 00060 /*High speed internal clock prescaler: 1*/ 00061 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 00062 00063 UART1_DeInit(); 00064 /* UART1 configuration ------------------------------------------------------*/ 00065 /* UART1 configured as follow: 00066 - BaudRate = 115200 baud 00067 - Word Length = 8 Bits 00068 - One Stop Bit 00069 - No parity 00070 - Receive and transmit enabled 00071 - UART1 Clock disabled 00072 */ 00073 UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, 00074 UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE); 00075 00076 /* Output a message on Hyperterminal using printf function */ 00077 printf("\n\rUART1 Example :retarget the C library printf()/getchar() functions to the UART\n\r"); 00078 printf("\n\rEnter Text\n\r"); 00079 00080 while (1) 00081 { 00082 ans = getchar(); 00083 printf("%c", ans); 00084 } 00085 } 00086 00087 /** 00088 * @brief Retargets the C library printf function to the UART. 00089 * @param c Character to send 00090 * @retval char Character sent 00091 */ 00092 PUTCHAR_PROTOTYPE 00093 { 00094 /* Write a character to the UART1 */ 00095 UART1_SendData8(c); 00096 /* Loop until the end of transmission */ 00097 while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); 00098 00099 return (c); 00100 } 00101 00102 /** 00103 * @brief Retargets the C library scanf function to the USART. 00104 * @param None 00105 * @retval char Character to Read 00106 */ 00107 GETCHAR_PROTOTYPE 00108 { 00109 #ifdef _COSMIC_ 00110 char c = 0; 00111 #else 00112 int c = 0; 00113 #endif 00114 /* Loop until the Read data register flag is SET */ 00115 while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET); 00116 c = UART1_ReceiveData8(); 00117 return (c); 00118 } 00119 00120 #ifdef USE_FULL_ASSERT 00121 00122 /** 00123 * @brief Reports the name of the source file and the source line number 00124 * where the assert_param error has occurred. 00125 * @param file: pointer to the source file name 00126 * @param line: assert_param error line source number 00127 * @retval None 00128 */ 00129 void assert_failed(uint8_t* file, uint32_t line) 00130 { 00131 /* User can add his own implementation to report the file name and line number, 00132 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00133 00134 /* Infinite loop */ 00135 while (1) 00136 { 00137 } 00138 } 00139 #endif 00140 00141 /** 00142 * @} 00143 */ 00144 00145 00146 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/