Piconomic Logo www.piconomic.co.za

nmea.h : NMEA parser
[/protocol : Collection of protocol related modules]

GPS NMEA protocol parser. More...

Data Structures

struct  nmea_data_t
 Parsed time, position, quality data. More...

Typedefs

typedef void(* nmea_tx_byte_t )(u8_t data)
 Definition for a pointer to a function that will be called to send a character.
typedef void(* nmea_on_valid_str_t )(const char *data)
 Definition for a pointer to a function that when a valid NMEA string is received.
typedef void(* nmea_on_valid_gps_data_t )(void)
 Definition for a pointer to a function that will be called when GPS data is valid.

Functions

void nmea_init (nmea_tx_byte_t tx_byte, nmea_on_valid_str_t on_valid_str, nmea_on_valid_gps_data_t on_valid_gps_data)
 Initialise NMEA parser module.
void nmea_on_rx_byte (u8_t data)
 Function handler that is fed all raw received data.
void nmea_tx_frame (char *frame)
 Function that is called to send an NMEA frame with the checksum appended.

Detailed Description

Files: nmea.h & nmea.c

See also:
http://en.wikipedia.org/wiki/NMEA_0183

Function Documentation

void nmea_init ( nmea_tx_byte_t  tx_byte,
nmea_on_valid_str_t  on_valid_str,
nmea_on_valid_gps_data_t  on_valid_gps_data 
)
Parameters:
tx_byte Pointer to a function that will be called to transmit a byte.
on_valid_str Pointer to a function that will be called when a valid NMEA string has been received.
on_valid_gps_data Pointer to a function that will be called when the data structure has been completely polulated with valid data.

Definition at line 402 of file nmea.c.

00405 {
00406     // Save function pointers
00407     nmea_tx_byte_fn           = tx_byte;
00408     nmea_on_valid_str_fn      = on_valid_str;
00409     nmea_on_valid_gps_data_fn = on_valid_gps_data;
00410 
00411    // Reset state variables
00412    nmea_rx_state            = NMEA_RX_STATE_START;
00413    nmea_rx_index            = 0;
00414    nmea_data.gga_valid_flag = FALSE;
00415    nmea_data.vtg_valid_flag = FALSE;
00416 }

void nmea_on_rx_byte ( u8_t  data  ) 
Parameters:
[in] data received 8-bit data

Definition at line 418 of file nmea.c.

00419 {   
00420    switch(nmea_rx_state)
00421    {
00422    case NMEA_RX_STATE_START :
00423       {
00424          // Check start sequence
00425          if(data != '$')
00426          {
00427              break;
00428          }
00429          nmea_rx_state = NMEA_RX_STATE_PAYLOAD;
00430          // Reset index and checksum
00431          nmea_rx_checksum = 0;
00432          nmea_rx_index    = 0;
00433          return;
00434       }
00435    case NMEA_RX_STATE_PAYLOAD :
00436       {
00437          // Check for unexpected characters in payload
00438          if(  (data == '$' )
00439             ||(data == '\r')
00440             ||(data == '\n')
00441             ||(data >= 0x80)  )
00442          {
00443              break;
00444          }
00445          // Check for checksum marker
00446          if(data == '*')
00447          {
00448             nmea_rx_state = NMEA_RX_STATE_CHECKSUM1;
00449             return;
00450          }
00451          // Update checksum of payload
00452          nmea_rx_checksum ^= data;
00453          // Put received byte into buffer
00454          nmea_rx_buffer[nmea_rx_index] = data;
00455          // Check for buffer overflow
00456          if (++nmea_rx_index >= (NMEA_BUFFER_SIZE-1))
00457          {
00458              break;
00459          }
00460          return;
00461       }
00462    case NMEA_RX_STATE_CHECKSUM1 :
00463       {
00464          // Check high nibble of checksum
00465          if(!nmea_cmp_nibble_with_hex_ascii(((nmea_rx_checksum>>4)&0x0f), data))
00466          {
00467              break;
00468          }
00469          
00470          nmea_rx_state = NMEA_RX_STATE_CHECKSUM2;
00471          return;
00472       }
00473    case NMEA_RX_STATE_CHECKSUM2 :
00474       {
00475          // Check low nibble of checksum
00476          if(!nmea_cmp_nibble_with_hex_ascii((nmea_rx_checksum&0x0f),data))
00477          {
00478              break;
00479          }
00480 
00481          nmea_rx_state = NMEA_RX_STATE_END_CR;
00482          return;
00483       }
00484    case NMEA_RX_STATE_END_CR :
00485       {
00486          // Check Carriage Return
00487          if(data != '\r')
00488          {
00489              break;
00490          }
00491          nmea_rx_state = NMEA_RX_STATE_END_LF;
00492          return;
00493       }
00494     case NMEA_RX_STATE_END_LF:
00495       {
00496          // Check Line Feed
00497          if(data != '\n')
00498          {
00499              break;
00500          }
00501          // Append terminating zero
00502          nmea_rx_buffer[nmea_rx_index] = '\0';
00503          // String successfully received
00504          nmea_on_rx_frame((char*)nmea_rx_buffer);
00505          break;
00506       }   
00507    }
00508    // Error detected... reset receiver
00509    nmea_rx_state = NMEA_RX_STATE_START;
00510 }

void nmea_tx_frame ( char *  frame  ) 
Parameters:
frame Pointer to zero terminated string.

Definition at line 512 of file nmea.c.

00513 {
00514    u8_t  data;
00515    u8_t  checksum = '$';
00516 
00517    // Send data and calculate checksum
00518    while(*frame)
00519    {
00520        data      = *frame++;
00521        checksum ^= data;
00522        nmea_tx_byte(data);
00523    }
00524 
00525    // Add checksum
00526    nmea_tx_byte('*');
00527    // Send high nibble
00528    if(checksum<0xA0)
00529    {
00530        nmea_tx_byte(((checksum>>4)&0x0f)+'0');
00531    }
00532    else
00533    {
00534        nmea_tx_byte(((checksum>>4)&0x0f)+('A'-10));
00535    }
00536    // Send low nibble
00537    checksum &=0x0f;
00538    if(checksum<0x0A)
00539    {
00540        nmea_tx_byte(checksum+'0');
00541    }
00542    else
00543    {
00544        nmea_tx_byte(checksum+('A'-10));
00545    }
00546 
00547    // Add end sequence
00548    nmea_tx_byte('\r');
00549    nmea_tx_byte('\n');
00550 }

Generated on Fri Aug 13 16:50:38 2010 for Piconomic Firmware Library by doxygen 1.6.3