Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

serial.c

00001 #include <pxa250regs.S>
00002 #include <stdarg.h>
00003 #include "serial.h"
00004 
00005 void serial_send_byte(int byte)
00006 {
00007         volatile int * const lsr= (volatile int *)(FFUART_BASE+UART_LSR);
00008         volatile int * const thr= (volatile int *)(FFUART_BASE+UART_THR);
00009 
00010         while (((*lsr) & SIO_LSR_THRE) == 0) ;
00011 
00012         *thr = byte;
00013 }
00014 
00015 int printf(const char *format, ...)
00016 {
00017         va_list ap;
00018         char c;
00019 
00020         va_start(ap, format);
00021         while (1)
00022         {
00023                 c=*format;
00024                 switch (c)
00025                 {
00026                         // end of string
00027                         case 0:
00028                         {
00029                                 goto stopPrintf;
00030                         }
00031                         break;
00032 
00033                         // special char
00034                         case '%':
00035                         {
00036                                 c=*(++format);
00037                                 switch (c)
00038                                 {
00039                                         // end of string
00040                                         case 0:
00041                                         {
00042                                                 goto stopPrintf;
00043                                         }
00044                                         break;
00045 
00046                                         // display %
00047                                         case '%':
00048                                         {
00049                                                 serial_send_byte('%');
00050                                         }
00051                                         break;
00052 
00053                                         // char
00054                                         case 'c':
00055                                         {
00056                                                 char typeC=va_arg(ap, int);
00057                                                 serial_send_byte(typeC);
00058                                         }
00059                                         break;
00060 
00061                                         // string
00062                                         case 's':
00063                                         {
00064                                                 char *typeCP=va_arg(ap, char *);
00065                                                 if (typeCP)
00066                                                 {
00067                                                         while (*typeCP)
00068                                                         {
00069                                                                 serial_send_byte(*typeCP);
00070                                                                 typeCP++;
00071                                                         }
00072                                                 }
00073                                                 else
00074                                                 {
00075                                                         serial_send_byte('n');
00076                                                         serial_send_byte('u');
00077                                                         serial_send_byte('l');
00078                                                         serial_send_byte('l');
00079                                                 }
00080                                         }
00081                                         break;
00082 
00083                                         // hexadecimal unsigned int
00084                                         case 'x':
00085                                         case 'X':
00086                                         {
00087                                                 int i, v;
00088                                                 unsigned int typeUI=va_arg(ap, unsigned int);
00089                                                 char baseChar;
00090                                                 if (c=='x')
00091                                                         baseChar='a';
00092                                                 else
00093                                                         baseChar='A';
00094                                                 for (i=28; i>=0; i-=4)
00095                                                 {
00096                                                         v=(typeUI>>i)&0xF;
00097                                                         serial_send_byte(v < 10 ? ('0'+v) : (baseChar+(v-10)));
00098                                                 }
00099                                         }
00100                                         break;
00101 
00102                                         // binary
00103                                         case 'b':
00104                                         {
00105                                                 int i, v;
00106                                                 unsigned int typeB=va_arg(ap, unsigned int);
00107                                                 for (i=31; i>=0; i--)
00108                                                 {
00109                                                         v=(typeB>>i)&0x1;
00110                                                         serial_send_byte(v == 1 ? '1' : '0');
00111                                                 }
00112                                         }
00113                                         break;
00114 
00115                                         // decimal unsigned int
00116                                         case 'u':
00117                                         {
00118                                                 unsigned int typeUI=va_arg(ap, unsigned int);
00119                                                 unsigned int div;
00120                                                 int hasPut=0;
00121                                                 for (div=1000000000; div>0; div/=10)
00122                                                 {
00123                                                         unsigned int aff=typeUI/div;
00124                                                         typeUI%=div;
00125 
00126                                                         if ((aff!=0) || (hasPut))
00127                                                         {
00128                                                                 hasPut=1;
00129                                                                 serial_send_byte('0'+aff);
00130                                                         }
00131                                                 }
00132                                                 if (!hasPut)
00133                                                         serial_send_byte('0');
00134                                         }
00135                                         break;
00136 
00137                                         // decimal signed int
00138                                         case 'd':
00139                                         {
00140                                                 int typeI=va_arg(ap, int);
00141                                                 unsigned int vAbs;
00142                                                 unsigned int div;
00143                                                 int hasPut=0;
00144                                                 if (typeI<0)
00145                                                 {
00146                                                         vAbs=(unsigned)(-typeI);
00147                                                         serial_send_byte('-');
00148                                                 }
00149                                                 else
00150                                                 {
00151                                                         vAbs=(unsigned)typeI;
00152                                                 }
00153                                                 for (div=1000000000; div>0; div/=10)
00154                                                 {
00155                                                         unsigned int aff=vAbs/div;
00156                                                         vAbs%=div;
00157 
00158                                                         if ((aff!=0) || (hasPut))
00159                                                         {
00160                                                                 hasPut=1;
00161                                                                 serial_send_byte('0'+aff);
00162                                                         }
00163                                                 }
00164                                                 if (!hasPut)
00165                                                         serial_send_byte('0');
00166                                         }
00167                                         break;
00168 
00169                                         // display char if not handled
00170                                         default:
00171                                         {
00172                                                 serial_send_byte(c);
00173                                         }
00174                                         break;
00175                                 }
00176                         }
00177                         break;
00178 
00179                         // other
00180                         default:
00181                         {
00182                                 serial_send_byte(c);
00183                         }
00184                         break;
00185                 }
00186                 format++;
00187         }
00188 
00189 // clean stack and return
00190 stopPrintf:
00191         va_end(ap);
00192 
00193         // TODO : return number of printed character
00194         return 0;
00195 }

Generated on Fri May 16 13:01:45 2003 for Jelie by doxygen1.2.15