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