hdo  beta
/home/mrazik/Documents/web/old/hdo/at91lib/peripherals/dbgu/dbgu.c
00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2008, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 //------------------------------------------------------------------------------
00031 //         Headers
00032 //------------------------------------------------------------------------------
00033 
00034 #include "dbgu.h"
00035 #include <stdarg.h>
00036 #include <board.h>
00037 
00038 //------------------------------------------------------------------------------
00039 //         Global functions
00040 //------------------------------------------------------------------------------
00041 //------------------------------------------------------------------------------
00050 //------------------------------------------------------------------------------
00051 void DBGU_Configure(
00052     unsigned int mode,
00053     unsigned int baudrate,
00054     unsigned int mck)
00055 {   
00056     // Reset & disable receiver and transmitter, disable interrupts
00057     AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RSTRX | AT91C_US_RSTTX;
00058     AT91C_BASE_DBGU->DBGU_IDR = 0xFFFFFFFF;
00059     
00060     // Configure baud rate
00061     AT91C_BASE_DBGU->DBGU_BRGR = mck / (baudrate * 16);
00062     
00063     // Configure mode register
00064     AT91C_BASE_DBGU->DBGU_MR = mode;
00065     
00066     // Disable DMA channel
00067     AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS;
00068 
00069     // Enable receiver and transmitter
00070     AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN;
00071 }
00072 
00073 //------------------------------------------------------------------------------
00077 //------------------------------------------------------------------------------
00078 void DBGU_PutChar(unsigned char c)
00079 {
00080     // Wait for the transmitter to be ready
00081     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);
00082     
00083     // Send character
00084     AT91C_BASE_DBGU->DBGU_THR = c;
00085     
00086     // Wait for the transfer to complete
00087     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);
00088 }
00089 
00090 //------------------------------------------------------------------------------
00092 //------------------------------------------------------------------------------
00093 unsigned int DBGU_IsRxReady()
00094 {
00095     return (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY);
00096 }
00097 
00098 //------------------------------------------------------------------------------
00102 //------------------------------------------------------------------------------
00103 unsigned char DBGU_GetChar(void)
00104 {
00105     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0);
00106     return AT91C_BASE_DBGU->DBGU_RHR;
00107 }
00108 
00109 #ifndef NOFPUT
00110 #include <stdio.h>
00111 
00112 //------------------------------------------------------------------------------
00119 //------------------------------------------------------------------------------
00120 signed int fputc(signed int c, FILE *pStream)
00121 {
00122     if ((pStream == stdout) || (pStream == stderr)) {
00123     
00124         DBGU_PutChar(c);
00125         return c;
00126     }
00127     else {
00128 
00129         return EOF;
00130     }
00131 }
00132 
00133 //------------------------------------------------------------------------------
00140 //------------------------------------------------------------------------------
00141 signed int fputs(const char *pStr, FILE *pStream)
00142 {
00143     signed int num = 0;
00144 
00145     while (*pStr != 0) {
00146 
00147         if (fputc(*pStr, pStream) == -1) {
00148 
00149             return -1;
00150         }
00151         num++;
00152         pStr++;
00153     }
00154 
00155     return num;
00156 }
00157 
00158 #undef putchar
00159 
00160 //------------------------------------------------------------------------------
00164 //------------------------------------------------------------------------------
00165 signed int putchar(signed int c)
00166 {
00167     return fputc(c, stdout);
00168 }
00169 
00170 #endif //#ifndef NOFPUT
00171