Nut/OS  4.10.3
API Reference
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 
00077 #include <dev/debug.h>
00078 
00079 #include <cfg/os.h>
00080 #include <sys/device.h>
00081 #include <sys/file.h>
00082 #include <sys/timer.h>
00083 
00088 
00089 static NUTFILE dbgfile;
00090 
00098 static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
00099 {
00100     if(req == UART_SETSPEED) {
00101 #if defined(__AVR_ENHANCED__) && ((NUT_CPU_FREQ == 12000000) || (NUT_CPU_FREQ == 16000000))
00102         /* On enhanced MCUs with 12.0 or 16.0 MHz we use double rate mode,
00103          * so we can use 115200 bps with 12.0 MHz crystals
00104          * and 57600 with 16.0 MHz crystals.
00105          */
00106         sbi(UCSR0A, U2X0);
00107         outb(UBRR, (uint8_t) ((((2UL * NutGetCpuClock()) / (*((uint32_t *)conf) * 8UL)) + 1UL) / 2UL) - 1UL);
00108 #else
00109         outb(UBRR, (uint8_t) ((((2UL * NutGetCpuClock()) / (*((uint32_t *)conf) * 16UL)) + 1UL) / 2UL) - 1UL);
00110 #endif
00111         return 0;
00112     }
00113     return -1;
00114 }
00115 
00124 static int DebugInit(NUTDEVICE * dev)
00125 {
00126     /* Note: Default baudrate has been set in nutinit.c */
00127     UCR = BV(RXEN) | BV(TXEN);
00128     return 0;
00129 }
00130 
00137 static void DebugPut(char ch)
00138 {
00139     if(ch == '\n') {
00140             while((USR & BV(UDRE)) == 0);
00141             UDR = '\r';
00142     }
00143     while((USR & BV(UDRE)) == 0);
00144     UDR = ch;
00145 }
00146 
00155 static int DebugWrite(NUTFILE * fp, CONST void *buffer, int len)
00156 {
00157     int c = len;
00158     CONST char *cp = buffer;
00159 
00160     while(c--)
00161         DebugPut(*cp++);
00162     return len;
00163 }
00164 
00173 static int DebugWrite_P(NUTFILE * fp, PGM_P buffer, int len)
00174 {
00175     int c = len;
00176     PGM_P cp = buffer;
00177 
00178     while(c--) {
00179         DebugPut(PRG_RDB(cp));
00180         cp++;
00181     }
00182     return len;
00183 }
00184 
00190 static NUTFILE *DebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
00191 {
00192     dbgfile.nf_next = 0;
00193     dbgfile.nf_dev = dev;
00194     dbgfile.nf_fcb = 0;
00195 
00196     return &dbgfile;
00197 }
00198 
00204 static int DebugClose(NUTFILE * fp)
00205 {
00206     return 0;
00207 }
00208 
00212 NUTDEVICE devDebug0 = {
00213     0,                          
00214     {'u', 'a', 'r', 't', '0', 0, 0, 0, 0},      
00215     0,                          
00216     0,                          
00217     0,                          
00218     0,                          
00219     0,                          
00220     DebugInit,                  
00221     DebugIOCtl,                 
00222     0,
00223     DebugWrite,
00224     DebugWrite_P,
00225     DebugOpen,
00226     DebugClose,
00227     0
00228 };
00229