| AVR Z-LINKŪ |

# serialPortHAL.c

Go to the documentation of this file.

    00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
    00025 /* === Includes ============================================================ */
    00026 #include"serialPortHAL.h"
    00027 /* ==== Macros ============================================================= */
    00028 /* === Typedefs ============================================================ */
    00029 /* === Variables =========================================================== */
    00030 rxDataBlock_t rxData;
    00031 rxState_t rxState;
    00032
    00038 const unsigned char __flash OK[ ] = "OK\r\n";
    00039
    00045 const unsigned char __flash ERROR[] = "ERROR: 0x";
    00046
    00052 const unsigned char __flash ASSOCIATION_NOTIFICATION[] = "+N";
    00053
    00059 const unsigned char __flash DISASSOCIATION_NOTIFICATION[] = "+D";
    00060
    00066 const unsigned char __flash NEW_DATA_NOTIFICATION[] = "+T: ";
    00067
    00072 const unsigned char __flash CRNL[]      = "\r\n";
    00073
    00078 const uint8_t __flash hexTable[] = "0123456789ABCDEF";
    00079
    00080 /* === Prototypes ========================================================== */
    00081 static void sendSymbol( const unsigned char symbol );
    00082 static void sendStringWithLength( const unsigned char *string, const uint8_t strLength );
    00083 static uint8_t errToHex( uint8_t hexNibble );
    00084 /* === Implementation ====================================================== */
    00085
    00097 void serialInterfaceInitialization( void ){
    00098
    00099 #ifdef UART
    00100
    00101         uartInitialization( BR_38400 );
    00102 #else
    00103         ftdiInitialization( );
    00104 #endif
    00105 }
    00106
    00107
    00111 void rxReset( void ){
    00112
    00113         unsigned char dummy = 0x00;
    00114
    00115         RECEIVER_OFF;
    00116
    00117         rxState = SOC;
    00118
    00119         rxData.rx_i = dummy;
    00120         rxData.ATCommand[ rxData.rx_i ] = dummy;
    00121         rxData.parameters[ rxData.rx_i ] = dummy;
    00122         rxData.numberOfReceivedParameters = dummy;
    00123         rxData.receptionComplete = false;
    00124         rxData.error = NO_ERROR;
    00125
    00126 #ifdef UART
    00127         //Following loop is used to ensure that the rx FIFO is flushed.
    00128         //Sometimes it gets cloged up with old data.
    00129         for( ;  UCSR0A & ( 1 << RXC0 ); ){
    00130
    00131                 dummy = UDR0;
    00132         }
    00133 #endif
    00134
    00135         RECEIVER_ON;
    00136 }
    00137
    00142 unsigned char * getATCommand( void ){
    00143
    00144         return  ( unsigned char * )rxData.ATCommand;
    00145 }
    00146
    00151 rxError_t getError( void ){
    00152
    00153         return rxData.error;
    00154 }
    00155
    00156
    00161 bool dataAvailable( void ){
    00162
    00163         return rxData.receptionComplete;
    00164 }
    00165
    00173 unsigned char * getParameterNumber( paramter_t parameterNumber ){
    00174
    00175         if( parameterNumber < NUMBER_OF_PARAMETERS ){
    00176
    00177                 return rxData.parameter[ parameterNumber ];
    00178         }
    00179
    00180
    00181         else{
    00182
    00183                 return NULL_PARAMETER;
    00184         }
    00185 }
    00186
    00196 #pragma inline
    00197 static void sendSymbol( const unsigned char symbol ){
    00198
    00199 #ifdef UART
    00200
    00201         uartSendSymbol( symbol );
    00202 #else
    00203         ftdiSendSymbol( symbol );
    00204 #endif
    00205 }
    00206
    00207
    00213 void sendString( const unsigned char *string ){
    00214
    00215   for( ; *string != '\0'; ){
    00216
    00217         sendSymbol( *string++ );
    00218         }
    00219
    00220         return;
    00221 }
    00222
    00228 void sendFlashString( const unsigned char __flash *string ){
    00229
    00230   for( ; *string != '\0'; ){
    00231
    00232     sendSymbol( *string++ );
    00233   }
    00234
    00235   return;
    00236 }
    00237
    00238
    00242 void sendOK( void ){
    00243
    00244   sendFlashString( OK );
    00245
    00246   return;
    00247 }
    00248
    00249
    00250
    00274 void sendERROR( const chatError_t err ){
    00275
    00276         sendFlashString( ERROR );
    00277
    00278         //Convert from err to ascii hex:
    00279         sendSymbol( errToHex( (err & 0xF0) >> 4 ) );    //MSB
    00280         sendSymbol( errToHex( err & 0x0F ) );   //LSB
    00281
    00282         sendFlashString( CRNL );
    00283
    00284         return;
    00285 }
    00286
    00287
    00292 static uint8_t errToHex( uint8_t hexNibble ){
    00293
    00294         return hexTable[ hexNibble ];
    00295 }
    00296
    00303 void sendAssociationNotification( void ){
    00304
    00305         sendFlashString( ASSOCIATION_NOTIFICATION );
    00306         sendFlashString( CRNL );
    00307
    00308         return;
    00309 }
    00310
    00316 void sendDisAssociationNotification( void ){
    00317
    00318         sendFlashString( DISASSOCIATION_NOTIFICATION );
    00319         sendFlashString( CRNL );
    00320
    00321         return;
    00322 }
    00323
    00324
    00333 void sendDataNotification( uint8_t length, unsigned char *data ){
    00334
    00335         uint8_t i, ii, dataLength;
    00336         unsigned char valueReversed[ 3 ];
    00337         unsigned char asciiValue[ 4 ];
    00338
    00339         dataLength = length;
    00340
    00341         //Make sure that only 3 iterations are made.
    00342         for( i = ii = 0; (ii < 3) && ( dataLength != 0 ) ; ii++ ){
    00343
    00344                 i = dataLength % 10;
    00345                 dataLength = dataLength / 10;
    00346
    00347                 valueReversed[ ii ] = i + '0';
    00348         }
    00349
    00350         asciiValue[ ii ] = '\0';        //Terminate string
    00351
    00352         //Reverse the order of digits.
    00353         for( i = 0, ii; ii > 0 ; ii--, i++ ){
    00354
    00355                 asciiValue[ i ] = valueReversed[ ii - 1 ];
    00356         }
    00357
    00358
    00359         sendFlashString( NEW_DATA_NOTIFICATION );
    00360         sendString( asciiValue );
    00361         //sendStringWithLength( asciiValue, length );   //Dummy...hardcoded.
    00362         sendSymbol( ',' );
    00363         sendStringWithLength( data, length );
    00364         sendFlashString( CRNL );
    00365
    00366         return;
    00367 }
    00368
    00376 static void sendStringWithLength( const unsigned char *string, const uint8_t strLength ){
    00377
    00378   uint8_t i;
    00379
    00380   for( i = 0; i < strLength; i++ ){
    00381
    00382     sendSymbol( *string++ );
    00383   }
    00384
    00385   return;
    00386 }
    00387
    00396 #ifdef UART
    00397         ISR( USART0_RX_vect ){
    00398
    00399         uint8_t receivedData;
    00400
    00401         receivedData = ( uint8_t )UDR0; //Collect data.
    00402 #else
    00403         ISR( INT7_vect ){
    00404
    00405         unsigned char receivedData;
    00406
    00407         receivedData = ( uint8_t )*FTDI_Fifo;   //Collect data.
    00408 #endif
    00409
    00410         switch( rxState ){
    00411
    00412                 case SOC:
    00413
    00414                   if( rxData.rx_i <= LENGTH_OF_COMMAND ){
    00415
    00416                         if( receivedData == PARAMETER_PREAMBLE_A ){
    00417
    00418                                 rxData.ATCommand[ rxData.rx_i ] = '\0';
    00419                                 rxState = PARAM_PREAMBLE;
    00420                         }
    00421
    00422                         else if( receivedData == EOC_DELIMITER ){
    00423
    00424                                 rxData.ATCommand[ rxData.rx_i ] = '\0';
    00425                                 rxState = EOC;
    00426                         }
    00427
    00428                         else{
    00429
    00430                                 rxData.ATCommand[ rxData.rx_i++ ] = receivedData;
    00431                         }
    00432                   }
    00433
    00434                   else{
    00435
    00436                         RECEIVER_OFF;
    00437                         rxData.error = BUFFER_OVERFLOW;
    00438                         rxData.receptionComplete = true;
    00439                   }
    00440
    00441                 break;
    00442
    00443                 case PARAM_PREAMBLE:
    00444
    00445                   if( receivedData == PARAMETER_PREAMBLE_B ){
    00446
    00447                         rxData.rx_i = 0;
    00448                         rxData.parameter[ rxData.numberOfReceivedParameters++ ] = \
    00449                                           &rxData.parameters[ rxData.rx_i ];
    00450                         rxState = COLLECT_PARAMETERS;
    00451                   }
    00452
    00453                   else{
    00454
    00455                         RECEIVER_OFF;
    00456                         rxData.error = UNKNOWN_SEQUENCE;
    00457                         rxData.receptionComplete = true;
    00458                   }
    00459
    00460                 break;
    00461
    00462                 case COLLECT_PARAMETERS:
    00463
    00464
    00465                         if( ( rxData.rx_i < LENGTH_OF_PARAMETER_BUFFER )&&
    00466                                 ( rxData.numberOfReceivedParameters < NUMBER_OF_PARAMETERS ) )
    00467                         {
    00468
    00469                                 if( receivedData == PARAMETER_DELIMITER ){
    00470
    00471                                         rxData.parameters[ rxData.rx_i++ ] = '\0';
    00472                                         rxData.parameter[ rxData.numberOfReceivedParameters++ ] = \
    00473                                           &rxData.parameters[ rxData.rx_i ];
    00474                                 }
    00475
    00476
    00477                                 else if( receivedData == EOC_DELIMITER ){
    00478
    00479                                         rxData.parameters[ rxData.rx_i ] = '\0';
    00480                                         rxState = EOC;
    00481                                 }
    00482
    00483                                 else{
    00484
    00485                                         rxData.parameters[ rxData.rx_i++ ] = receivedData;
    00486                                 }
    00487                   }
    00488
    00489                   else{
    00490
    00491                         RECEIVER_OFF;
    00492                         rxData.error = BUFFER_OVERFLOW;
    00493                         rxData.receptionComplete = true;
    00494                   }
    00495                 break;
    00496
    00497                 case EOC:
    00498
    00499                         RECEIVER_OFF;
    00500
    00501                         if( receivedData != '\n' ){
    00502
    00503                                 rxData.error = UNKNOWN_EOC_SEQUENCE;
    00504                         }
    00505
    00506                         rxData.receptionComplete = true;
    00507                 break;
    00508
    00509                 default:
    00510
    00511                         RECEIVER_OFF;
    00512                         rxData.error = UNKNOWN_RX_STATE;
    00513                         rxData.receptionComplete = true;
    00514                 break;
    00515         }
    00516 }

@DOC_TITLE@