Prof Greg Egan / Mbed 2 deprecated UAVXArm-GKE

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers serial.c Source File

serial.c

00001 // ===============================================================================================
00002 // =                              UAVXArm Quadrocopter Controller                                =
00003 // =                           Copyright (c) 2008 by Prof. Greg Egan                             =
00004 // =                 Original V3.15 Copyright (c) 2007 Ing. Wolfgang Mahringer                   =
00005 // =                     http://code.google.com/p/uavp-mods/ http://uavp.ch                      =
00006 // ===============================================================================================
00007 
00008 //    This is part of UAVXArm.
00009 
00010 //    UAVXArm is free software: you can redistribute it and/or modify it under the terms of the GNU
00011 //    General Public License as published by the Free Software Foundation, either version 3 of the
00012 //    License, or (at your option) any later version.
00013 
00014 //    UAVXArm is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without
00015 //    even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 //    See the GNU General Public License for more details.
00017 
00018 //    You should have received a copy of the GNU General Public License along with this program.
00019 //    If not, see http://www.gnu.org/licenses/
00020 
00021 #include "UAVXArm.h"
00022 
00023 // USART routines
00024 
00025 // Much of this legacy code to support UAVPset, the original GUI for UAVP.
00026 // It is to be replaced by packet based comms ASAP.
00027 
00028 
00029 void TxString(const uint8*);
00030 void TxChar(uint8);
00031 void TxValU(uint8);
00032 void TxValS(int8);
00033 void TxBin8(uint8);
00034 void TxNextLine(uint8);
00035 void TxNibble( uint8);
00036 void TxValH(uint8);
00037 void TxValH16( uint16);
00038 uint8 RxChar(void);
00039 uint8 PollRxChar(void);
00040 uint8 RxNumU(void);
00041 int8 RxNumS(void);
00042 void TxVal32(int32, int8, uint8);
00043 void TxChar( uint8);
00044 void TxESCu8( uint8);
00045 void Sendi16( int16);
00046 void TxESCi8( int8);
00047 void TxESCi16( int16);
00048 void TxESCi24( int24);
00049 void TxESCi32( int32);
00050 
00051 void TxString(const uint8 *pch) {
00052     while ( *pch != '\0' )
00053         TxChar(*pch++);
00054 } // TxString
00055 
00056 void TxChar(uint8 ch) {
00057 
00058     TxCheckSum ^= ch;
00059 
00060     while ( !SERIAL_TELEMETRY.writeable() ) {};
00061     SERIAL_TELEMETRY.putc(ch);
00062 
00063     if ( EchoToLogFile )
00064         TxLogChar(ch);
00065 
00066 } // TxChar
00067 
00068 void TxValU(uint8 v) {
00069     // UAVPSet requires 3 digits exactly ( 000 to 999 )
00070     TxChar((v / 100) + '0');
00071     v %= 100;
00072 
00073     TxChar((v / 10) + '0');
00074     v %= 10;
00075 
00076     TxChar(v + '0');
00077 } // TxValU
00078 
00079 void TxValS(int8 v) {
00080     // UAVPSet requires sign and 3 digits exactly (-999 to 999)
00081     if ( v < 0 ) {
00082         TxChar('-');    // send sign
00083         v = -v;
00084     } else
00085         TxChar('+');    // send sign
00086 
00087     TxValU(v);
00088 } // TxValS
00089 
00090 void TxNextLine(void) {
00091     TxChar(CR);
00092     TxChar(LF);
00093 } // TxNextLine
00094 
00095 void TxBin8(uint8 v) {
00096 
00097     static uint8 i;
00098 
00099     for  (i = 0; i <8; i++) {
00100         if ( v & 0x80 )
00101             TxChar('1');
00102         else TxChar('0');
00103         v <<= 1;
00104     }
00105 } // TxBin8
00106 
00107 void TxNibble(uint8 v) {
00108     if ( v > (uint8)9)
00109         TxChar('A' + v - 10);
00110     else
00111         TxChar('0' + v);
00112 } // TxNibble
00113 
00114 void TxValH(uint8 v) {
00115     TxNibble(v >> 4);
00116     TxNibble(v & 0x0f);
00117 } // TxValH
00118 
00119 void TxValH16(uint16 v) {
00120     TxValH(v >> 8);
00121     TxValH(v);
00122 } // TxValH16
00123 
00124 uint8 PollRxChar(void) {
00125     uint8    ch;
00126 
00127     if (  SERIAL_TELEMETRY.readable() ) {    // a character is waiting in the buffer
00128         ch = SERIAL_TELEMETRY.getc();        // get the character
00129         TxChar(ch);                         // echo it for UAVPSet
00130         return(ch);                         // and return it
00131     }
00132     return( NUL );    // nothing in buffer
00133 
00134 } // PollRxChar
00135 
00136 uint8 RxChar(void) {
00137     uint8    ch;
00138 
00139     while ( !SERIAL_TELEMETRY.readable() ) {};
00140 
00141     ch = SERIAL_TELEMETRY.getc();    // get the character
00142 
00143     return(ch);
00144 } // RxChar
00145 
00146 
00147 uint8 RxNumU(void) {
00148     // UAVPSet sends 2 digits
00149     uint8 ch;
00150     uint8 n;
00151 
00152     n = 0;
00153     do
00154         ch = PollRxChar();
00155     while ( (ch < '0') || (ch > '9') );
00156     n = (ch - '0') * 10;
00157     do
00158         ch = PollRxChar();
00159     while ( (ch < '0') || (ch > '9') );
00160     n += ch - '0';
00161     return(n);
00162 } // RxNumU
00163 
00164 
00165 int8 RxNumS(void) {
00166     // UAVPSet sends sign and 2 digits
00167     uint8 ch;
00168     int8 n;
00169     boolean Neg;
00170     n = 0;
00171 
00172     Neg = false;
00173     do
00174         ch = PollRxChar();
00175     while ( ((ch < '0') || (ch > '9')) &&
00176             (ch != '-') );
00177     if ( ch == '-' ) {
00178         Neg = true;
00179         do
00180             ch = PollRxChar();
00181         while ( (ch < '0') || (ch > '9') );
00182     }
00183     n = (ch - '0') * 10;
00184 
00185     do
00186         ch = PollRxChar();
00187     while ( (ch < '0') || (ch > '9') );
00188     n += ch - '0';
00189     if ( Neg )
00190         n = -n;
00191     return(n);
00192 } // RxNumS
00193 
00194 void TxVal32(int32 V, int8 dp, uint8 Separator) {
00195     uint8 S[16];
00196     int8 c,  zeros, i;
00197     int32 NewV, Rem;
00198 
00199     if (V<0) {
00200         TxChar('-');
00201         V=-V;
00202     }
00203 //    else
00204 //        TxChar(' ');
00205 
00206     c=0;
00207     do {
00208         NewV=V/10;
00209         Rem=V-(NewV*10);
00210         S[c++]=Rem + '0';
00211         V=NewV;
00212     } while (V>0);
00213 
00214     if ((c < ( dp + 1 ) ) && (dp > 0 )) {
00215         TxChar('0');
00216         TxChar('.');
00217     }
00218 
00219     zeros = (int8)dp-c-1;
00220     if ( zeros >= 0 )
00221         for (i = zeros; i>=0; i--)
00222             TxChar('0');
00223 
00224     do {
00225         c--;
00226         TxChar(S[c]);
00227         if ((c==dp)&&(c>0))
00228             TxChar('.');
00229     } while ( c > 0 );
00230 
00231     if ( Separator != NUL )
00232         TxChar(Separator);
00233 } // TxVal32
00234 
00235 void TxESCu8(uint8 ch) {
00236     if ((ch==SOH)||(ch==EOT)||(ch==ESC))
00237         TxChar(ESC);
00238     TxChar(ch);
00239 } // TxESCu8
00240 
00241 void TxESCi8(int8 b) {
00242     if (((uint8)b==SOH)||((uint8)b==EOT)||((uint8)b==ESC))
00243         TxChar(ESC);
00244     TxChar(b);
00245 } // TxESCu8
00246 
00247 void Sendi16(int16 v) {
00248     i16u Temp;
00249 
00250     Temp.i16 = v;
00251     TxChar(Temp.b0);
00252     TxChar(Temp.b1);
00253 } // Sendi16
00254 
00255 void TxESCi16(int16 v) {
00256     i16u Temp;
00257 
00258     Temp.i16 = v;
00259     TxESCu8(Temp.b0);
00260     TxESCu8(Temp.b1);
00261 } // Sendi16
00262 
00263 void TxESCi24(int24 v) {
00264     i24u Temp;
00265 
00266     Temp.i24 = v;
00267     TxESCu8(Temp.b0);
00268     TxESCu8(Temp.b1);
00269     TxESCu8(Temp.b2);
00270 } // Sendi16
00271 
00272 void TxESCi32(int32 v) {
00273     i32u Temp;
00274 
00275     Temp.i32 = v;
00276     TxESCi16(Temp.w0);
00277     TxESCi16(Temp.w1);
00278 } // TxESCi32
00279