uart1.c
Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2011, Ascending Technologies GmbH
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are met:
00008 
00009  * Redistributions of source code must retain the above copyright notice,
00010    this list of conditions and the following disclaimer.
00011  * Redistributions in binary form must reproduce the above copyright
00012    notice, this list of conditions and the following disclaimer in the
00013    documentation and/or other materials provided with the distribution.
00014 
00015 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
00016 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
00019 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00020 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00021 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00022 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00023 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00024 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00025 DAMAGE.
00026 
00027  */
00028 
00029 #include "LPC214x.h"
00030 #include "system.h"
00031 #include "main.h"
00032 #include "uart1.h"
00033 #include "irq.h"
00034 #include "hardware.h"
00035 #include "gpsmath.h"
00036 #include "ssp.h"
00037 #include "sdk.h"
00038 #include "ublox.h"
00039 
00040 unsigned char packets;
00041 unsigned char DataOutputsPerSecond;
00042 unsigned int uart_cnt;
00043 
00044 unsigned char data_requested=0;
00045 extern int ZeroDepth;
00046 
00047 unsigned short current_chksum;
00048 unsigned char chksum_to_check=0;
00049 unsigned char chksum_trigger=1;
00050 
00051 unsigned char transmission1_running=0;
00052 unsigned char trigger_transmission=0;
00053 
00054 volatile unsigned char baudrate1_change=0;
00055 
00056 unsigned char send_buffer[16];
00057 unsigned char *tx_buff;
00058 unsigned char UART1_syncstate=0;
00059 unsigned int UART1_rxcount=0;
00060 unsigned char *UART1_rxptr;
00061 
00062 unsigned char UART_CalibDoneFlag = 0;
00063 
00064 static volatile unsigned char rb_busy=0;
00065 
00066 unsigned char startstring[]={'>','*','>'};
00067 unsigned char stopstring[]={'<','#','<'};
00068 
00069 
00070 void uart1ISR(void) __irq
00071 {
00072   unsigned char t;
00073   IENABLE;
00074   unsigned iir = U1IIR;
00075   // Handle UART interrupt
00076   switch ((iir >> 1) & 0x7)
00077     {
00078       case 1:
00079                   // THRE interrupt
00080                  if (ringbuffer1(RBREAD, &t, 1))
00081                  {
00082                    transmission1_running=1;
00083                    UART1WriteChar(t);
00084                  }
00085                  else
00086                  {
00087                    transmission1_running=0;
00088                  }
00089         break;
00090       case 2:
00091         // RX interrupt
00092             uBloxReceiveHandler(U1RBR);
00093             break;
00094       case 3:
00095         // RLS interrupt
00096         break;
00097       case 6:
00098         // CTI interrupt
00099         break;
00100    }
00101   IDISABLE;
00102   VICVectAddr = 0;              /* Acknowledge Interrupt */
00103 }
00104 
00105 
00106 
00107 
00108 
00109 void UART1Initialize(unsigned int baud)
00110 {
00111   unsigned int divisor = peripheralClockFrequency() / (16 * baud);
00112 //UART1
00113   U1LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
00114   U1DLL = divisor & 0xFF;
00115   U1DLM = (divisor >> 8) & 0xFF;
00116   U1LCR &= ~0x80; /* Disable DLAB */
00117   U1FCR = 1;
00118 }
00119 
00120 
00121 
00122 //Write to UART1
00123 void UART1WriteChar(unsigned char ch)
00124 {
00125   while ((U1LSR & 0x20) == 0);
00126   U1THR = ch;
00127 }
00128 
00129 
00130 
00131 unsigned char UART1ReadChar(void)
00132 {
00133   while ((U1LSR & 0x01) == 0);
00134   return U1RBR;
00135 }
00136 
00137 
00138 
00139 void UART1_send(unsigned char *buffer, unsigned char length)
00140 {
00141   unsigned char cnt=0;
00142   while(length--)
00143   {
00144     while (!(U1LSR & 0x20)); //wait until U1THR is empty
00145     U1THR = buffer[cnt++];
00146   }
00147 }
00148 
00149 
00150 
00151 
00152 void UART1_send_ringbuffer(void)
00153 {
00154   unsigned char t;
00155   if(!transmission1_running)
00156   {
00157     if(ringbuffer1(RBREAD, &t, 1))
00158     {
00159       transmission1_running=1;
00160       UART1WriteChar(t);
00161     }
00162   }
00163 }
00164 
00165 int ringbuffer1(unsigned char rw, unsigned char *data, unsigned int count)      //returns 1 when write/read was successful, 0 elsewise
00166 {
00167     static volatile unsigned char buffer[RINGBUFFERSIZE];
00168 //      static volatile unsigned int pfirst=0, plast=0; //Pointers to first and last to read byte
00169         static volatile unsigned int read_pointer, write_pointer;
00170         static volatile unsigned int content=0;
00171         unsigned int p=0;
00172     unsigned int p2=0;
00173 
00174         if(rw==RBWRITE)
00175         {
00176                 if(count<RINGBUFFERSIZE-content)        //enough space in buffer?
00177                 {
00178                         while(p<count)
00179                         {
00180                                 buffer[write_pointer++]=data[p++];
00181                         }
00182             content+=count;
00183             return(1);
00184                 }
00185         }
00186         else if(rw==RBREAD)
00187         {
00188                 if(content>=count)
00189                 {
00190                         while(p2<count)
00191                         {
00192                                 data[p2++]=buffer[read_pointer++];
00193                         }
00194             content-=count;
00195             if(!content) //buffer empty
00196             {
00197                 write_pointer=0;
00198                 read_pointer=0;
00199             }
00200                         return(1);
00201                 }
00202         }
00203         else if(rw==RBFREE)
00204         {
00205           if(content) return 0;
00206           else return(RINGBUFFERSIZE-11);
00207         }
00208 
00209         return(0);
00210 }


asctec_hl_firmware
Author(s): Markus Achtelik, Michael Achtelik, Stephan Weiss, Laurent Kneip
autogenerated on Tue Jan 7 2014 11:05:19