debug0.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
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 EGNITE SOFTWARE GMBH 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 EGNITE
00021  * SOFTWARE GMBH 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.ethernut.de/
00031  *
00032  */
00033 
00074 #include <dev/debug.h>
00075 
00076 #include <cfg/os.h>
00077 #include <sys/device.h>
00078 #include <sys/file.h>
00079 #include <sys/timer.h>
00080 
00085 
00086 static NUTFILE dbgfile;
00087 
00095 static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
00096 {
00097     if(req == UART_SETSPEED) {
00098 #if defined(__AVR_ENHANCED__) && ((NUT_CPU_FREQ == 12000000) || (NUT_CPU_FREQ == 16000000))
00099         /* On enhanced MCUs with 12.0 or 16.0 MHz we use double rate mode,
00100          * so we can use 115200 bps with 12.0 MHz crystals
00101          * and 57600 with 16.0 MHz crystals.
00102          */
00103         sbi(UCSR0A, U2X0);
00104         outb(UBRR, (u_char) ((((2UL * NutGetCpuClock()) / (*((u_long *)conf) * 8UL)) + 1UL) / 2UL) - 1UL);
00105 #else
00106         outb(UBRR, (u_char) ((((2UL * NutGetCpuClock()) / (*((u_long *)conf) * 16UL)) + 1UL) / 2UL) - 1UL);
00107 #endif
00108         return 0;
00109     }
00110     return -1;
00111 }
00112 
00121 static int DebugInit(NUTDEVICE * dev)
00122 {
00123     /* Note: Default baudrate has been set in nutinit.c */
00124     UCR = BV(RXEN) | BV(TXEN);
00125     return 0;
00126 }
00127 
00134 static void DebugPut(char ch)
00135 {
00136     while((USR & BV(UDRE)) == 0);
00137     UDR = ch;
00138     if(ch == '\n')
00139         DebugPut('\r');
00140 }
00141 
00150 static int DebugWrite(NUTFILE * fp, CONST void *buffer, int len)
00151 {
00152     int c = len;
00153     CONST char *cp = buffer;
00154 
00155     while(c--)
00156         DebugPut(*cp++);
00157     return len;
00158 }
00159 
00168 static int DebugWrite_P(NUTFILE * fp, PGM_P buffer, int len)
00169 {
00170     int c = len;
00171     PGM_P cp = buffer;
00172 
00173     while(c--) {
00174         DebugPut(PRG_RDB(cp));
00175         cp++;
00176     }
00177     return len;
00178 }
00179 
00185 static NUTFILE *DebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
00186 {
00187     dbgfile.nf_next = 0;
00188     dbgfile.nf_dev = dev;
00189     dbgfile.nf_fcb = 0;
00190 
00191     return &dbgfile;
00192 }
00193 
00199 static int DebugClose(NUTFILE * fp)
00200 {
00201     return 0;
00202 }
00203 
00207 NUTDEVICE devDebug0 = {
00208     0,                          
00209     {'u', 'a', 'r', 't', '0', 0, 0, 0, 0},      
00210     0,                          
00211     0,                          
00212     0,                          
00213     0,                          
00214     0,                          
00215     DebugInit,                  
00216     DebugIOCtl,                 
00217     0,
00218     DebugWrite,
00219     DebugWrite_P,
00220     DebugOpen,
00221     DebugClose,
00222     0
00223 };
00224 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/