debug1.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 
00034 /*
00035  * $Log: debug1.c,v $
00036  * Revision 1.4  2006/10/08 16:48:07  haraldkipp
00037  * Documentation fixed
00038  *
00039  * Revision 1.3  2005/10/17 08:46:53  hwmaier
00040  * Setting baudrate function changed: For CPUs w/ 12 and 16 MHz xtal double rate mode is now used (only if set by NUT_CPU_FREQ)
00041  *
00042  * Revision 1.2  2005/08/02 17:46:45  haraldkipp
00043  * Major API documentation update.
00044  *
00045  * Revision 1.1  2005/07/26 18:02:27  haraldkipp
00046  * Moved from dev.
00047  *
00048  * Revision 1.3  2005/02/06 16:36:59  haraldkipp
00049  * Fixes ICCAVR V7 baudrate miscalculation.
00050  *
00051  * Revision 1.2  2004/02/25 16:19:10  haraldkipp
00052  * Support baudrate settings
00053  *
00054  * Revision 1.1.1.1  2003/05/09 14:40:37  haraldkipp
00055  * Initial using 3.2.1
00056  *
00057  * Revision 1.2  2003/05/06 18:29:49  harald
00058  * ICCAVR port
00059  *
00060  * Revision 1.1  2003/04/07 12:15:27  harald
00061  * First release
00062  *
00063  */
00064 
00065 #include <dev/debug.h>
00066 
00067 #include <cfg/os.h>
00068 #include <sys/timer.h>
00069 #include <sys/device.h>
00070 #include <sys/file.h>
00071 
00076 
00077 #ifdef __AVR_ENHANCED__
00078 
00079 static NUTFILE dbgfile;
00080 
00081 static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
00082 {
00083     if(req == UART_SETSPEED) {
00084 #if defined(__AVR_ENHANCED__) && ((NUT_CPU_FREQ == 12000000) || (NUT_CPU_FREQ == 16000000))
00085         /* On enhanced MCUs we use double rate mode, so we can use 115200 bps
00086         * with 12.0 and 16.0 crystals.
00087         */
00088         sbi(UCSR1A, U2X1);
00089         outb(UBRR1L, (u_char) ((((2UL * NutGetCpuClock()) / (*((u_long *)conf) * 8UL)) + 1UL) / 2UL) - 1UL);
00090 #else
00091         outb(UBRR1L, (u_char) ((((2UL * NutGetCpuClock()) / (*((u_long *)conf) * 16UL)) + 1UL) / 2UL) - 1UL);
00092 #endif
00093         return 0;
00094     }
00095     return -1;
00096 }
00097 
00098 static int DebugInit(NUTDEVICE * dev)
00099 {
00100     /* Note: Default baudrate has been set in nutinit.c */
00101     UCSR1B = BV(RXEN) | BV(TXEN);
00102     return 0;
00103 }
00104 
00105 static void DebugPut(char ch)
00106 {
00107     while((UCSR1A & BV(UDRE)) == 0);
00108     UDR1 = ch;
00109     if(ch == '\n')
00110         DebugPut('\r');
00111 }
00112 
00113 static int DebugWrite(NUTFILE * fp, CONST void *buffer, int len)
00114 {
00115     int c = len;
00116     CONST char *cp = buffer;
00117 
00118     while(c--)
00119         DebugPut(*cp++);
00120     return len;
00121 }
00122 
00123 static int DebugWrite_P(NUTFILE * fp, PGM_P buffer, int len)
00124 {
00125     int c = len;
00126     PGM_P cp = buffer;
00127 
00128     while(c--) {
00129         DebugPut(PRG_RDB(cp));
00130         cp++;
00131     }
00132     return len;
00133 }
00134 
00135 static NUTFILE *DebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
00136 {
00137     dbgfile.nf_next = 0;
00138     dbgfile.nf_dev = dev;
00139     dbgfile.nf_fcb = 0;
00140 
00141     return &dbgfile;
00142 }
00143 
00147 static int DebugClose(NUTFILE * fp)
00148 {
00149     return 0;
00150 }
00151 
00155 NUTDEVICE devDebug1 = {
00156     0,                          
00157     {'u', 'a', 'r', 't', '1', 0, 0, 0, 0},      
00158     0,                          
00159     0,                          
00160     0,                          
00161     0,                          
00162     0,                          
00163     DebugInit,                  
00164     DebugIOCtl,                 
00165     0,
00166     DebugWrite,
00167     DebugWrite_P,
00168     DebugOpen,
00169     DebugClose,
00170     0
00171 };
00172 
00173 #endif
00174 

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