Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
For additional information see http://www.ethernut.de/
$Log$ Revision 1.6 2009/02/08 01:00:33 thiagocorrea Remove unused variable in sample app
Revision 1.5 2008/01/31 09:38:15 haraldkipp Added return statement in main to avoid warnings with latest GCC.
Revision 1.4 2005/11/22 09:14:13 haraldkipp Replaced specific device names by generalized macros.
Revision 1.3 2004/11/24 16:35:56 haraldkipp Configurable floating point support
Revision 1.2 2004/09/10 10:33:28 haraldkipp Temporarly removed non-configurable FP support
Revision 1.1 2003/08/05 18:59:52 haraldkipp Release 3.3 update
Revision 1.3 2003/02/04 18:19:41 harald Version 3 released
Revision 1.2 2003/02/04 16:24:38 harald Adapted to version 3
Revision 1.1 2002/08/09 12:44:10 harald Renamed for make rules
Revision 1.5 2002/06/12 11:00:10 harald *** empty log message ***
Revision 1.4 2002/06/04 19:13:21 harald *** empty log message ***
Revision 1.3 2002/05/08 16:02:34 harald First Imagecraft compilation
Revision 1.2 2001/08/10 18:20:41 harald GCC version 3 update
Revision 1.1 2001/06/28 18:43:13 harald Preview release
This sample demonstrates the usage of the ATmega on-chip UART. Note, that we don't do any error checking, because without this UART we can't tell the user our problem.
We use floating points. Make sure to link with nutlibcrtf.
00001 00089 #include <cfg/crt.h> /* Floating point configuration. */ 00090 00091 #include <string.h> 00092 #include <stdio.h> 00093 #include <io.h> 00094 00095 #include <dev/board.h> 00096 #include <sys/timer.h> 00097 00098 static char *banner = "\nNut/OS UART Sample\n"; 00099 static prog_char presskey_P[] = "Press any key..."; 00100 static prog_char pgm_ptr[] = "\nHello stranger!\n"; 00101 00102 static char inbuf[128]; 00103 00104 /* 00105 * UART sample. 00106 * 00107 * Some functions do not work with ICCAVR. 00108 */ 00109 int main(void) 00110 { 00111 int got; 00112 char *cp; 00113 u_long baud = 115200; 00114 FILE *uart; 00115 #ifdef STDIO_FLOATING_POINT 00116 float dval = 0.0; 00117 #endif 00118 00119 /* 00120 * Each device must be registered. We do this by referencing the 00121 * device structure of the driver. The advantage is, that only 00122 * those device drivers are included in our flash code, which we 00123 * really need. 00124 * 00125 * The uart0 device is the first one on the ATmega chip. So it 00126 * has no configurable base address or interrupt and we set both 00127 * parameters to zero. 00128 */ 00129 NutRegisterDevice(&DEV_UART, 0, 0); 00130 00131 /* 00132 * Now, as the device is registered, we can open it. The fopen() 00133 * function returns a pointer to a FILE structure, which we use 00134 * for subsequent reading and writing. 00135 */ 00136 uart = fopen(DEV_UART_NAME, "r+"); 00137 00138 /* 00139 * Before doing the first read or write, we set the baudrate. 00140 * This low level function doesn't know about FILE structures 00141 * and we use _fileno() to get the low level file descriptor 00142 * of the stream. 00143 * 00144 * The short sleep allows the UART to settle after the baudrate 00145 * change. 00146 */ 00147 _ioctl(_fileno(uart), UART_SETSPEED, &baud); 00148 00149 /* 00150 * Stream devices can use low level read and write functions. 00151 * Writing program space data is supported too. 00152 */ 00153 _write(_fileno(uart), banner, strlen(banner)); 00154 { 00155 _write_P(_fileno(uart), presskey_P, sizeof(presskey_P)); 00156 } 00157 00158 /* 00159 * Stream devices do buffered I/O. That means, nothing will be 00160 * passed to the hardware device until either the output buffer 00161 * is full or we do a flush. With stream I/O we typically use 00162 * fflush(), but low level writing a null pointer will also flush 00163 * the output buffer. 00164 */ 00165 _write(_fileno(uart), 0, 0); 00166 00167 /* 00168 * The low level function read() will grab all available bytes 00169 * from the input buffer. If the buffer is empty, the call will 00170 * block until something is available for reading. 00171 */ 00172 got = _read(_fileno(uart), inbuf, sizeof(inbuf)); 00173 _write(_fileno(uart), inbuf, got); 00174 00175 /* 00176 * Nut/OS never expects a thread to return. So we enter an 00177 * endless loop here. 00178 */ 00179 for (;;) { 00180 /* 00181 * A bit more advanced input routine is able to read a string 00182 * up to and including the first newline character or until a 00183 * specified maximum number of characters, whichever comes first. 00184 */ 00185 fputs("\nEnter your name: ", uart); 00186 fflush(uart); 00187 fgets(inbuf, sizeof(inbuf), uart); 00188 00189 /* 00190 * Chop off trailing linefeed. 00191 */ 00192 cp = strchr(inbuf, '\n'); 00193 if (cp) 00194 *cp = 0; 00195 00196 /* 00197 * Streams support formatted output as well as printing strings 00198 * from program space. 00199 */ 00200 if (inbuf[0]) 00201 fprintf(uart, "\nHello %s!\n", inbuf); 00202 else { 00203 fputs_P(pgm_ptr, uart); 00204 } 00205 00206 /* 00207 * Just to demonstrate formatted floating point output. 00208 * In order to use this, we need to link the application 00209 * with nutcrtf instead of nutcrt for pure integer. 00210 */ 00211 #ifdef STDIO_FLOATING_POINT 00212 dval += 1.0125; 00213 fprintf(uart, "FP %f\n", dval); 00214 #endif 00215 } 00216 return 0; 00217 }