uart.c

Go to the documentation of this file.
00001 /*
00002  ####        # 
00003  #           #
00004  ## ### #  # #
00005  #  #-# #\ # #
00006  #  # # # \# ####
00007  Author: Felipe de Andrade Neves Lavratti
00008 
00009  Copyright: There are no restrictions. Use as you want.   
00010 */
00011 
00012 #include "uart.h"
00013 
00014 
00015 /* the "location parameter is used when the particular channel can be connected
00016  * to many different places. the order is P0_x, P1_x, etc. */
00017 void    UARTInit(unsigned int baud, char channel, char location)
00018 {
00019     unsigned int pclkoffset,  div_l, div_h;
00020 
00021     switch (channel)
00022         {
00023                 case UART_CH_0:
00024                         PCLKSEL0 &= ~(0x3<<6);
00025                         pclkoffset = 6;
00026                         break;
00027 
00028                 case UART_CH_1:
00029                         PCLKSEL0 &= ~(0x3<<8);
00030                         pclkoffset = 8;
00031                         break;
00032 
00033 /*              case UART_CH_2:
00034                         PCLKSEL0 &= ~(0x3<<48);
00035                         pclkoffset = 48;
00036                         break;
00037 
00038                 case UART_CH_3:
00039                         PCLKSEL0 &= ~(0x3<<50);
00040                         pclkoffset = 50;
00041                         break;
00042 */
00043                 default:
00044                         return;
00045         }
00046 
00047 
00048         div_h = ((UART_CLK/baud) >> 12) & 0xff;
00049         div_l = ((UART_CLK/baud+8) >> 4) & 0xff;
00050 
00051         switch (channel)
00052         {
00053                 case UART_CH_0:
00054                         PINSEL0 &= ~(0x00000050);
00055                         PINSEL0 |= 0x00000050;
00056                         PCONP |= 0x1<<3;
00057                         U0LCR = 0x83;   /* 8 bits, no Parity, 1 Stop bit */
00058                         U0DLM = div_h;
00059                         U0DLL = div_l;
00060                         U0LCR = 0x03;   /* DLAB = 0 */
00061                         U0FCR = 0x07;   /* Enable and reset TX and RX FIFO. */
00062                         break;
00063 
00064                 case UART_CH_1:
00065                         switch (location)
00066                         {
00067                                 case 0:
00068                                         PINSEL0 &= ~(0xC0000000);
00069                                         PINSEL0 |= 0x40000000;
00070                                         PINSEL1 &= ~(0x00000003);
00071                                         PINSEL1 |= 0x00000001;
00072                                         break;
00073 
00074                                 case 1:
00075                                         PINSEL4 &= ~(0x0000000F);
00076                                         PINSEL4 |= 0x00000000A;
00077                                         break;
00078 
00079                                 case 2:
00080                                         PINSEL7 |= 0x0000000F;
00081                                         break;
00082                         }
00083                         PCONP |= 0x1<<4;
00084                         U1LCR = 0x83;   /* 8 bits, no Parity, 1 Stop bit */
00085                         U1DLM = div_h;
00086                         U1DLL = div_l;
00087                         U1LCR = 0x03;   /* DLAB = 0 */
00088                         U1FCR = 0x07;   /* Enable and reset TX and RX FIFO. */
00089                         break;
00090 
00091                 case UART_CH_2:
00092                         switch (location)
00093                         {
00094                                 case 0:
00095                                         PINSEL0 &= ~(0x00F00000);
00096                                         PINSEL0 |=   0x00500000;
00097                                         break;
00098 
00099                                 case 1:
00100                                         PINSEL4 &= ~(0x000F0000);
00101                                         PINSEL4 |=   0x000A0000;
00102                                         break;
00103 
00104                                 case 2:
00105                                         PINSEL9 &= ~(0x0000F000);
00106                                         PINSEL9 |=   0x0000A000;
00107                                         break;
00108                         }
00109                         PCONP |= 0x1<<24;
00110                         U2LCR = 0x83;   /* 8 bits, no Parity, 1 Stop bit */
00111                         U2DLM = div_h;
00112                         U2DLL = div_l;
00113                         U2LCR = 0x03;   /* DLAB = 0 */
00114                         U2FCR = 0x07;   /* Enable and reset TX and RX FIFO. */
00115                         break;
00116 
00117                 case UART_CH_3:
00118                         switch (location)
00119                         {
00120                                 case 0:
00121                                         PINSEL0 &= ~(0x0000000F);
00122                                         PINSEL0 |=   0x0000000A;
00123                                         break;
00124 
00125                                 case 1:
00126                                         PINSEL1 |=   0x003C0000;
00127                                         break;
00128 
00129                                 case 2:
00130 
00131                                         PINSEL9 |=   0x0F000000;
00132                                         break;
00133                         }
00134                         PCONP |= 0x1<<25;
00135                         U3LCR = 0x83;   /* 8 bits, no Parity, 1 Stop bit */
00136                         U3DLM = div_h;
00137                         U3DLL = div_l;
00138                         U3LCR = 0x03;   /* DLAB = 0 */
00139                         U3FCR = 0x07;   /* Enable and reset TX and RX FIFO. */
00140                         break;
00141         }
00142 }
00143 
00144 
00145 char UARTIsDataReady(char channel)
00146 {
00147         switch (channel)
00148         {
00149                 case UART_CH_0:
00150                         if (U0LSR & 0x01) return TRUE;
00151                                 else return FALSE;
00152 
00153                 case UART_CH_1:
00154                         if (U1LSR & 0x01) return TRUE;
00155                                 else return FALSE;
00156 
00157                 case UART_CH_2:
00158                         if (U2LSR & 0x01) return TRUE;
00159                                 else return FALSE;
00160 
00161                 case UART_CH_3:
00162                         if (U3LSR & 0x01) return TRUE;
00163                                 else return FALSE;
00164 
00165                 default:
00166                         return FALSE;
00167         }
00168 
00169 }
00170 char UARTWaitForByte(char channel)
00171 {
00172         switch (channel)
00173         {
00174                 case UART_CH_0:
00175                         while((U0LSR & 0x01)==0);
00176                         return U0RBR;
00177 
00178                 case UART_CH_1:
00179                         while((U1LSR & 0x01)==0);
00180                         return U1RBR;
00181 
00182                 case UART_CH_2:
00183                         while((U2LSR & 0x01)==0);
00184                         return U2RBR;
00185 
00186                 case UART_CH_3:
00187                         while((U3LSR & 0x01)==0);
00188                         return U3RBR;
00189 
00190                 default:
00191                         return FALSE;
00192         }
00193 }
00194 
00195 char UARTReceiveByte(char channel)
00196 {
00197         switch (channel)
00198         {
00199                 case UART_CH_0:
00200                         //while((U0LSR & 0x01)==0);     //enquanto não a sinal de caracter vindo da serial.
00201                         return U0RBR;
00202 
00203                 case UART_CH_1:
00204                         return U1RBR;
00205 
00206                 case UART_CH_2:
00207                         return U2RBR;
00208 
00209                 case UART_CH_3:
00210                         return U3RBR;
00211 
00212                 default:
00213                         return FALSE;
00214         }
00215 }
00216 
00217 void    UARTTransmitByte(char ch, char channel)
00218 {
00219         switch (channel)
00220         {
00221                 case UART_CH_0:
00222                         while (!(U0LSR & 0x20));
00223                         U0THR = ch;
00224                         break;
00225 
00226                 case UART_CH_1:
00227                         while (!(U1LSR & 0x20));
00228                         U1THR = ch;
00229                         break;
00230 
00231                 case UART_CH_2:
00232                         while (!(U2LSR & 0x20));
00233                         U2THR = ch;
00234                         break;
00235 
00236                 case UART_CH_3:
00237                         while (!(U3LSR & 0x20));
00238                         U3THR = ch;
00239                         break;
00240 
00241                 default:
00242                         break;
00243         }
00244 
00245 }
00246 
00247 void UARTTransmitString(char *str, char channel)
00248 {
00249         while(*str) UARTTransmitByte(*str++, channel);
00250 }
00251 
00252 /*void UARTTakeString(char *str, char channel)
00253 {
00254         int vd = 0xff;
00255         char tp[10]="\0";
00256         char cond = 0x0d;
00257         do{
00258                         vd = UARTReceiveByte(channel);
00259                 if (vd != cond) sprintf(tp, "%s%c", tp,vd);
00260         }while (vd != cond);
00261         strcpy(str, tp);
00262 }*/
00263 
00264