code:interrupt_driven_tsip
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
code:interrupt_driven_tsip [2008/06/16 00:32] – Created laurenceb | code:interrupt_driven_tsip [2008/10/11 11:28] (current) – laurenceb | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <code c> | + | <code c># |
- | #include " | + | |
//global(s) | //global(s) | ||
extern gps_type Gps; | extern gps_type Gps; | ||
- | // Interrupt driven TSIP (lassen iq) parser for Atmel AVR | + | // Interrupt driven TSIP (lassen iq) parser for Atmel AVR (state machine based) |
+ | |||
+ | #define ETX 0x03 //as defined in TSIP specs | ||
+ | #define DLE 0x10 | ||
+ | |||
+ | //8O1 send routine for initial setup | ||
+ | |||
+ | int suart_send(char c) | ||
+ | { | ||
+ | u08 n; | ||
+ | u08 p=FALSE; | ||
+ | CLEAR; | ||
+ | delay; | ||
+ | for(n=0; | ||
+ | { | ||
+ | if(c & 0x01) | ||
+ | { | ||
+ | SET; | ||
+ | p=!p; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | CLEAR; | ||
+ | } | ||
+ | c>> | ||
+ | delay; | ||
+ | } | ||
+ | if(p) // | ||
+ | { | ||
+ | SET; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | CLEAR; | ||
+ | } | ||
+ | delay; | ||
+ | SET; | ||
+ | delay; | ||
+ | return 0; | ||
+ | } | ||
ISR(USART_RX_vect) // | ISR(USART_RX_vect) // | ||
{ | { | ||
- | static u08 LLA; | + | static u08 id; |
- | static u08 VENU; | + | static u08 placemarker; |
- | static u08 placemark; | + | |
static gps_type gps; | static gps_type gps; | ||
- | char c=UDR0; | + | static u08 state; //state machine |
- | char *d; | + | u08 *d=(u08*)& |
- | placemarker++; | + | if(UCSR0A & (BV(FE0)|BV(UPE0))) //a framing or parity error causes us to drop the packet |
- | if(c==0x03) //end of a packet | + | |
{ | { | ||
- | placemarker=0; | + | state=0; |
- | LLA=FALSE; | + | |
- | VENU=FALSE; | + | |
} | } | ||
- | else if(placemarker==2) //packet id | + | char c=UDR0; |
+ | if(state==2 || state==4) //the two states where we can write something | ||
{ | { | ||
- | switch(c) | + | if(!placemarker && id==0x6D) //start of packet - the status byte |
{ | { | ||
- | case 0x4A: | + | gps.status= c & 0x0F; //number from 0 to 7, 0=undefined, |
- | LLA=TRUE; | + | gps.nosats=(c & 0xF0)>> |
- | break; | + | |
- | case 0x56: | + | |
- | VENU=TRUE; | + | |
- | break; | + | |
- | default: | + | |
} | } | ||
- | } | + | else |
- | if(LLA) | + | |
- | { | + | |
- | switch(placemarker) | + | |
{ | { | ||
- | case 3: | + | if(placemarker< |
- | d=& | + | { |
- | break; | + | d[27-placemarker]=c; |
- | case 7: | + | } |
- | d=&gps.longitude; | + | if(placemarker> |
- | break; | + | { |
- | case 11: | + | d[31-placemarker]=c; //time of fix |
- | d=& | + | } |
- | break; | + | } |
- | default: | + | else if(id==0x56 && placemarker< |
- | } | + | |
- | if(d==&gps.latitude) | + | |
{ | { | ||
- | memcpy(d+placemarker-3,&c,1); | + | d[11-placemarker]=c; |
} | } | ||
- | if(d==& | + | else if(placemarker> |
{ | { | ||
- | memcpy(d+placemarker-7,&c,1); | + | state=0; |
} | } | ||
- | if(d==& | + | placemarker++; |
+ | } | ||
+ | switch(state) // | ||
+ | { | ||
+ | case 0: | ||
+ | if(c==DLE) //wait for a dle before proceeding | ||
{ | { | ||
- | memcpy(d+placemarker-11,& | + | state=1; |
} | } | ||
- | if(placemarker==14) | + | break; |
+ | case 1: | ||
+ | placemarker=0; | ||
+ | if(c==0x4A || c==0x6D || c==0x56) //packet ids | ||
{ | { | ||
- | LLA=FALSE; | + | state=2; |
+ | id=c; | ||
} | } | ||
- | } | + | else // |
- | if(VENU) | + | |
- | { | + | |
- | switch(placemarker) | + | |
{ | { | ||
- | case 3: | + | state=0; |
- | d=& | + | |
- | break; | + | |
- | case 7: | + | |
- | d=& | + | |
- | break; | + | |
- | case 11: | + | |
- | d=& | + | |
- | break; | + | |
- | default: | + | |
} | } | ||
- | if(d==& | + | break; |
+ | case 2: | ||
+ | if(c==DLE) //a dle in the data? | ||
{ | { | ||
- | memcpy(d+placemarker-3,&c,1); | + | state=3; |
} | } | ||
- | if(d==& | + | break; |
+ | case 3: | ||
+ | if(c==ETX) | ||
{ | { | ||
- | memcpy(d+placemarker-7,&c,1); | + | state=0; |
+ | if(id==0x6D) // | ||
+ | { | ||
+ | gps.packetflag=TRUE;// | ||
+ | toggle_pin; | ||
+ | if(!Gps.packetflag) //main has unlocked the data | ||
+ | { | ||
+ | Gps=gps; //copy into the global variable | ||
+ | } | ||
+ | } | ||
} | } | ||
- | if(d==& | + | else if(c==DLE) //double stuffed dle |
{ | { | ||
- | memcpy(d+placemarker-11,& | + | state=4; |
} | } | ||
- | if(placemarker==14) | + | else // |
{ | { | ||
- | toggle_pin; | + | state=0; |
- | if(!Gps.packetflag) // | + | |
- | { | + | |
- | Gps.packetflag=TRUE; | + | |
- | Gps=gps; | + | |
- | VENU=FALSE; //dont need to run again | + | |
- | } | + | |
} | } | ||
+ | break; | ||
+ | case 4: | ||
+ | if(c==ETX || c==DLE) // | ||
+ | { | ||
+ | state=0; | ||
+ | } | ||
+ | break; | ||
+ | default: | ||
+ | state=0; | ||
} | } | ||
- | } | + | }</ |
- | </ | + |
code/interrupt_driven_tsip.1213576353.txt.gz · Last modified: 2008/07/19 23:31 (external edit)