UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


code:interrupt_driven_tsip

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
code:interrupt_driven_tsip [2008/06/16 00:32] – Created laurencebcode:interrupt_driven_tsip [2008/10/11 11:28] (current) laurenceb
Line 1: Line 1:
-<code c> +<code c>#include "main.h"
-#include "main.h"+
 //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; //odd parity 
 + CLEAR; //start bit 
 + delay; 
 + for(n=0;n<8;n++) //the data 
 +
 + if(c & 0x01) 
 +
 + SET; //update parity 
 + p=!p; 
 +
 + else 
 +
 + CLEAR; 
 +
 + c>>=1; 
 + delay; 
 +
 + if(p) //the parity 
 +
 + SET; 
 +
 + else 
 +
 + CLEAR; 
 +
 + delay; 
 + SET; //stop bit 
 + delay; 
 + return 0; 
 +}
    
 ISR(USART_RX_vect) //UART interrupt on mega xx8 series ISR(USART_RX_vect) //UART interrupt on mega xx8 series
 { {
- 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*)&gps; //points to our data 
- 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 ie a new one is coming+
  {  {
- placemarker=0; + state=0;
- LLA=FALSE; +
- VENU=FALSE; //we dont know what sort of packet it is yet+
  }  }
- 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.statusc & 0x0F; //number from 0 to 7, 0=undefined, 7=ground station,4 3D 
- LLA=TRUE+ gps.nosats=(c & 0xF0)>>4; //number of tracked satellites
- break; +
- case 0x56: +
- VENU=TRUE; +
- break; +
- default: //unknown packet, leave it+
  }  }
-+ else if(id==0x4A) //lla(timepacket - 4 floats
- if(LLA) +
-+
- switch(placemarker)+
  {  {
- case 3: + if(placemarker<12) //lla 
- d=&gps.latitude+
- break; + d[27-placemarker]=c
- case 7: + } 
- d=&gps.longitude; + if(placemarker>15 && placemarker<20) //time 
- break; + { 
- case 11: + d[31-placemarker]=c; //time of fix 
- d=&gps.altitude+ } 
- break; +
- default: + else if(id==0x56 && placemarker<12 //enu packet - 3 floats
- }  +
- if(d==&gps.latitude)+
  {  {
- memcpy(d+placemarker-3,&c,1);+ d[11-placemarker]=c;
  }  }
- if(d==&gps.longitude)+ else if(placemarker>27) //max 7 floats
  {  {
- memcpy(d+placemarker-7,&c,1);+ state=0; //we had too much datasomething is wrong
  }  }
- if(d==&gps.latitude)+ placemarker++; //incr placeholder 
 +
 + switch(state) //run the state machine 
 +
 + case 0: 
 + if(c==DLE) //wait for a dle before proceeding
  {  {
- memcpy(d+placemarker-11,&c,1);+ 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 //unknown or not something we are interested in
- if(VENU) +
-+
- switch(placemarker)+
  {  {
- case 3: + state=0;
- d=&gps.veast; +
- break; +
- case 7: +
- d=&gps.vnorth; +
- break; +
- case 11: +
- d=&gps.vup; +
- break; +
- default:+
  }   }
- if(d==&gps.veast)+ break; 
 + case 2: 
 + if(c==DLE) //a dle in the data?
  {  {
- memcpy(d+placemarker-3,&c,1);+ state=3;
  }  }
- if(d==&gps.vnorth)+ break; 
 + case 3: 
 + if(c==ETX)
  {  {
- memcpy(d+placemarker-7,&c,1);+ state=0; //end of packet we arent in one 
 + if(id==0x6D) //the 0x6D packet is sent after a fix calculation or at 1Hz 
 +
 + gps.packetflag=TRUE;//TRUE==1 
 + toggle_pin; //toggles pin D5 - flashing LED tells us gps is working 
 + if(!Gps.packetflag) //main has unlocked the data 
 +
 + Gps=gps; //copy into the global variable 
 +
 + }
  }  }
- if(d==&gps.vup)+ else if(c==DLE) //double stuffed dle
  {  {
- memcpy(d+placemarker-11,&c,1);+ state=4;
  }  }
- if(placemarker==14)+ else //this shouldnt happen
  {  {
- toggle_pin; //toggles pin D5 - flashing LED tells us gps is working + state=0;
- if(!Gps.packetflag) //main has unlocked the data +
-+
- Gps.packetflag=TRUE; //this is usually the last interesting part of the fix info to come through +
- Gps=gps; //copy into the global variable +
- VENU=FALSE; //dont need to run again +
- }+
  }  }
 + break;
 + case 4:
 + if(c==ETX || c==DLE) //state 4 shouldnt get either of these
 + {
 + state=0;
 + }
 + break;
 + default: //something wrong, go to 0
 + state=0;
  }  }
-} +}</code>
-</code>+
code/interrupt_driven_tsip.1213576353.txt.gz · Last modified: 2008/07/19 23:31 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki