uart-suart/uart-suart.c

Date:
18.06.2004
Author:
Martin Hinz <btnode@hinz.ch>
This application connects the software uart with the hardware uart to chat between both uarts! :)

00001 /*
00002  * Copyright (C) 2000-2005 by ETH Zurich
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
00021  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.btnode.ethz.ch
00031  *
00032  * $Id: uart-suart.c,v 1.11 2006/03/23 07:22:25 dyerm Exp $
00033  * 
00034  */
00035 
00066 #include <stdio.h>
00067 #include <io.h>
00068 
00069 #include <suart/btn-suart.h>
00070 #include <dev/usartavr.h>
00071 #include <led/btn-led.h>
00072 #include <sys/thread.h>
00073 #include <hardware/btn-hardware.h>
00074 FILE *uarts, *uart;
00075 
00076 
00077 /*
00078  * thread to listen on application uart and write on software uart
00079  */
00080 THREAD(ListenUart, arg)
00081 {
00082     int got;
00083     char inbuf[20]; 
00084     for(;;){
00085         got = _read(_fileno(uart), inbuf, 20);
00086         btn_led_clear_pattern_queue();
00087         btn_led_add_pattern(BTN_LED_PATTERN_UP, 0,1, 1);
00088         btn_led_add_pattern(BTN_LED_PATTERN_KNIGHT, 0, 4, BTN_LED_INFINITE);
00089         _write(_fileno(uarts), inbuf, got);        
00090     }
00091 } 
00092 
00093 /*
00094  * function to listen on software uart and write to application uart
00095  */
00096 void listen_to_suart(void)
00097 {
00098     int got;
00099     char inbuf[20]; 
00100     for(;;){
00101         got = _read(_fileno(uarts), inbuf, 20);
00102         btn_led_clear_pattern_queue();
00103         btn_led_add_pattern(BTN_LED_PATTERN_DOWN, 0,1, 1);
00104         btn_led_add_pattern(BTN_LED_PATTERN_KNIGHT, 0, 4, BTN_LED_INFINITE);
00105         _write(_fileno(uart), inbuf, got);        
00106     }
00107 } 
00108 
00109 int main(void){   
00110     u_long sbaud = 9600;
00111     btn_hardware_init();    
00112     // initialize software UART
00113     NutRegisterDevice(&S_UART, 0, 0);
00114     uarts = fopen(S_UART.dev_name, "r+");
00115     _ioctl(_fileno(uarts), UART_SETSPEED, &sbaud);
00116     
00117     u_long baud = 57600;
00118     // initialize application UART
00119     //speed of uart set default to 57600
00120     NutRegisterDevice(&APP_UART, 0, 0);
00121     uart = fopen(APP_UART.dev_name, "r+");
00122     _ioctl(_fileno(uart), UART_SETSPEED, &baud);
00123     
00124     
00125     fputs("Software uart. type to chat with hardware uart!\n",uarts); 
00126     fputs("Hardware uart. type to chat with software uart!\n",uart); 
00127     btn_led_init(1);
00128     btn_led_add_pattern(BTN_LED_PATTERN_KNIGHT, 0, 4, BTN_LED_INFINITE);
00129     // start thread for Uart to Software-Uart chat
00130     NutThreadCreate("T_uart", ListenUart, 0, 192);
00131     
00132     listen_to_suart();
00133     return 1;
00134 }

Generated on Wed Apr 29 11:12:28 2009 for BTnut System Software by doxygen 1.5.1