Nut/OS  4.10.3
API Reference
debug_at91.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2006 by egnite Software GmbH
00003  * Copyright (C) 2010-2011 by egnite GmbH
00004  *
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  */
00035 
00045 #include <cfg/uart.h>
00046 #include <arch/arm/atmel/debug_at91.h>
00047 
00048 #include <dev/debug.h>
00049 #include <sys/timer.h>
00050 
00051 
00056 
00064 uint32_t At91BaudRateDiv(uint32_t baud)
00065 {
00066     return (NutClockGet(NUT_HWCLK_PERIPHERAL) / (8 * baud) + 1) / 2;
00067 }
00068 
00076 int At91DevDebugIOCtl(NUTDEVICE * dev, int req, void *conf)
00077 {
00078     if(req == UART_SETSPEED) {
00079         outr(dev->dev_base + US_BRGR_OFF, At91BaudRateDiv(*((uint32_t *)conf)));
00080         return 0;
00081     }
00082     return -1;
00083 }
00084 
00091 static void DebugPut(CONST NUTDEVICE * dev, char ch)
00092 {
00093     if (ch == '\n') {
00094             while ((inr(dev->dev_base + US_CSR_OFF) & US_TXRDY) == 0);
00095         outr(dev->dev_base + US_THR_OFF, '\r');
00096     }
00097     while ((inr(dev->dev_base + US_CSR_OFF) & US_TXRDY) == 0);
00098     outr(dev->dev_base + US_THR_OFF, ch);
00099 }
00100 
00109 int At91DevDebugWrite(NUTFILE * fp, CONST void *buffer, int len)
00110 {
00111     int c = len;
00112     CONST char *cp = buffer;
00113 
00114     while (c--) {
00115         DebugPut(fp->nf_dev, *cp++);
00116     }
00117     return len;
00118 }
00119 
00120 #ifdef NUT_DEV_DEBUG_READ
00121 
00142 int At91DevDebugRead(NUTFILE * fp, void *buffer, int size)
00143 {
00144     int rc;
00145     unsigned int ch;
00146     char *bp = (char *) buffer;
00147 
00148     /* Wait for the first character, forever. */
00149     for (rc = 0; rc < size; rc++) {
00150         while ((inr(fp->nf_dev->dev_base + US_CSR_OFF) & US_RXRDY) == 0) {
00151             NutSleep(1);
00152             if ((rc || bp == NULL) && 
00153                 (inr(fp->nf_dev->dev_base + US_CSR_OFF) & US_RXRDY) == 0) {
00154                 return rc;
00155             }
00156         }
00157         ch = inr(fp->nf_dev->dev_base + US_RHR_OFF);
00158         if (bp) {
00159             if (ch == '\r') {
00160                 *bp++ = '\n';
00161             } else {
00162                 *bp++ = (char) ch;
00163             }
00164         }
00165     }
00166     return rc;
00167 }
00168 
00180 long At91DevDebugSize(NUTFILE *fp)
00181 {
00182     if (inr(fp->nf_dev->dev_base + US_CSR_OFF) & US_RXRDY) {
00183         return 1;
00184     }
00185     return 0;
00186 }
00187 
00188 #endif
00189 
00195 NUTFILE *At91DevDebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
00196 {
00197     NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
00198 
00199     fp->nf_next = 0;
00200     fp->nf_dev = dev;
00201     fp->nf_fcb = 0;
00202 
00203     return fp;
00204 }
00205 
00211 int At91DevDebugClose(NUTFILE * fp)
00212 {
00213     return 0;
00214 }
00215