www.pudn.com > EM4469firmware.zip > main.c


/* 
*------------------------------------------------------------------------------- 
*--  RCSId: $Id: main.c,v 0.17 2003-10-10 10:55:40+02 mjg Exp mjg $ 
*--         $Name:  $ 
*------------------------------------------------------------------------------- 
*-- main.c (level1) 
*------------------------------------------------------------------------------- 
*-- $Log: main.c,v $ 
*-- Revision 0.17  2003-10-10 10:55:40+02  mjg 
*-- *** empty log message *** 
*-- 
*-- Revision 0.16  2003-08-21 16:00:21+02  mjg 
*-- RTF capture problem 
*-- 
*-- Revision 0.15  2003-08-20 10:53:20+02  mjg 
*-- to redesign SearchPattern 
*-- 
*-- Revision 0.14  2003-08-20 10:00:46+02  mjg 
*-- to add debug features 
*-- 
*-- Revision 0.13  2003-08-07 08:01:30+02  mjg 
*-- *** empty log message *** 
*-- 
*-- Revision 0.11  2003-07-22 13:27:48+02  mjg 
*-- cloned firmware 
*-- 
*------------------------------------------------------------------------------- 
*/ 
 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
#include "level4.h" 
#include "level3.h" 
#include "level2.h" 
 
// ================================================================== 
// capture variables 
 
uint8_t halfDataRate;                 //half period data rate modul 
uint8_t counter1set;                  //counter1 settings 
uint8_t lwr;                          //last word read 
uint8_t delayed;                      //delayed value 
uint8_t forward_link_type;            //forward link modulation type 
 
uint8_t decode_number;                //decoder routine number 
void (*decode)(void);                 //decoder routine variable 
uint8_t enable_capture;               //enable capture (0=disable, 1=capture enable, 2=debug pulse capture) 
 
uint16_t last_capture;                //counter1 is now reset at capture start, we make a difference 
                                      //between current and last value 
uint8_t currentMaxTimeHi;             //actual hi value of maximum capture time 
 
uint8_t watchdog_reset;               //watchdog activated 
 
// ================================================================== 
// level 1 local variables 
 
register uint8_t captured_byte asm ("r6");      //store_bit current capture bits 
register uint8_t captured_valid asm ("r7");     //store_bit current capture valid bits 
register uint8_t capture_cnt asm ("r8");        //store_bit current capture byte index 
register uint8_t captured_bit_count asm ("r9"); //store_bit current capture bit counter 
 
uint8_t bit_pos;                      //bit position in demodulation 
uint8_t last_bit;                     //for biphase and miller usage 
uint8_t edge;                         //for manchester usage 
uint8_t last_valid;                   //last valid value for invalid sequence compression 
 
uint8_t treshold_0; 
uint8_t treshold_1; 
 
// ================================================================== 
// function declarations 
 
 
// ================================================================== 
 
void avr_ini(void) 
{ 
 
   // ---------------------------------------------------------------  
   // UART initialization                                              
   // ---------------------------------------------------------------  
 
   UARTIni();          // level4 uart init routine 
 
   // ---------------------------------------------------------------  
   // 16-bit Timer/Counter1 initialization                             
   // ---------------------------------------------------------------  
 
   TCCR1A = 0;         // no compare otuput, no PWM mode ...           
   TCCR1B = (1<255) store_pulse(255); else store_pulse((uint8_t)capt); 
  } 
 
  y = 0; 
  (uint8_t)capt += halfDataRate >> 2;   // dynamic resolution (dependent on datarate) 
  (uint8_t)capt -= halfDataRate;        // always subtract datarate/2  
  if ((uint8_t)capt > halfDataRate) {   // when longer than that, try to subtract once more 
    y++; 
    (uint8_t)capt -= halfDataRate; 
  } 
 
  if ((capt<<1) > halfDataRate) {   // check overflow of counter and range 
                                    // this check is also dynamic, depends on current datarate 
                                    // the faster datarate the more precise must the signal be 
 
    if(last_valid == 0) {      
      last_bit = ((~edge)>>ICES1)&1;    // rtf feature 
      store_bit(last_bit, 0);           // rtf feature 
      store_bit ( 0, 1);                // store bad bit 
      last_valid = 1; 
    } 
    last_bit = 0; 
  } else { 
 
    if (y) { 
      last_bit = ((~edge)>>ICES1)&1; 
      store_bit(last_bit, 0); 
      last_valid = 0; 
      bit_pos = 0; 
    } else { 
      if (bit_pos++) { 
        store_bit(last_bit, 0); 
        bit_pos = 0; 
        last_valid = 0; 
      } 
    } 
  } 
 
} 
 
// ================================================================== 
 
 
void biphase_capture(void) 
{ 
  uint8_t y; 
  uint16_t capt; 
  uint16_t icr = ICR1;    
 
  TCCR1B ^= (1<255) store_pulse(255); else store_pulse((uint8_t)capt); 
  } 
 
  y = 0; 
  capt += halfDataRate >> 2;   // dynamic resolution (dependent on datarate) 
  capt -= halfDataRate;        // always subtract datarate/2 
  if (capt > halfDataRate) {   // when longer than that, try to subtract once more 
    y++; 
    capt -= halfDataRate; 
  } 
 
  if ((capt<<1) > halfDataRate) {   // check overflow of counter and range 
                                    // this check is also dynamic, depends on current datarate 
                                    // the faster datarate the more precise must the signal be 
    if (last_valid == 0) { 
      store_bit(bit_pos^1, 0);      // rtf feature 
      store_bit ( 0, 1);            // store bad bit 
      last_valid = 1;  
    } 
    bit_pos = 0; 
 
  } else { 
 
    if (y) { 
      store_bit(1, 0); 
      bit_pos = 0; 
      last_valid = 0; 
    } else { 
      if (bit_pos++) { 
        store_bit(0, 0); 
        bit_pos = 0; 
        last_valid = 0; 
      } 
    } 
  } 
 
} 
 
// ================================================================== 
 
void miller_capture(void) 
{ 
  uint8_t y; 
  uint16_t capt; 
  uint16_t icr = ICR1;    
 
  TCCR1B ^= (1<255) store_pulse(255); else store_pulse((uint8_t)capt); 
  } 
 
  y = 0;                          //first operation must be 16 bits wide (counter can be up to 256 !) 
  capt -= halfDataRate << 1;      // always subtract datarate 
 
                                  // from now on, 8bit operations are enough 
  capt += halfDataRate >> 2;      // dynamic resolution (dependent on datarate) 
  if ((uint8_t)capt > halfDataRate) {   // when longer than that, try to subtract once more 
    y++; 
    (uint8_t)capt -= halfDataRate; 
  } 
 
  if ((uint8_t)capt > halfDataRate) {   // when longer than that, try to subtract once more 
    y++; 
    (uint8_t)capt -= halfDataRate; 
  } 
 
  if ((capt<<1) > halfDataRate) {   // check overflow of counter and range 
                                    // this check is also dynamic, depends on current datarate 
                                    // the faster datarate the more precise must the signal be 
    last_bit = 0; 
    store_bit ( 0, 1);              // store bad bit 
  } else { 
 
    if (y==2) {		            // only combination "101" 
      store_bit(0, 0); 
      store_bit(1, 0); 
      last_bit = 1; 
    } else if (!y) {                // the same bit as previously 
      store_bit(last_bit, 0); 
    } else if (last_bit) {	    // y==1, last_bit=1 
      store_bit(0, 0); 
      last_bit = 0; 
    } else {			    // y==1, last_bit=0 
      store_bit(0, 0); 
      store_bit(1, 0); 
      last_bit = 1; 
    } 
 
  } 
} 
 
// ================================================================== 
// Down Link setup function 
// Requires: maxCaptureTimeLow and maxCaptureTimeHi 
//   (located here instead of level2 because of register variables) 
 
void Capture(uint8_t style) { 
 
  ClearCaptureBuffers(); 
 
  last_valid = 0; 
 
  TCCR1B = 0;                              //disable Counter1 
  captured_bit_count = 0; 
  capture_cnt = 0;                         
  bit_pos = 0; 
 
  TCNT1 = ~maxCaptureTimeLow;               //set timer with initial time 
  currentMaxTimeHi = ~maxCaptureTimeHi; 
 
  TIFR = TIFR | (1<>ICES1)&1; 
        store_bit(last_bit, 0);  
        break; 
      case 2 : 
        store_bit(bit_pos ^ 1, 0);  
        break; 
      default : 
        break; 
    } 
  } 
 
  if (capture_cnt != CAPTURE_SIZE)          //flush captured bits 
    while (captured_bit_count != 0) { 
      store_bit(0,1); 
    } 
 
  enable_capture = 0; 
 
  TCCR1B = counter1set;                          //counter T1 running 
  sbi (TIMSK, TICIE1);                           //enable dummy capture 
} 
 
// ================================================================== 
// Wait 
 
void Wait(uint16_t period) { 
 
  TCCR1B = 0;                               //disable Counter1 
  TCNT1 = ~period;                          //set timer with initial time 
  currentMaxTimeHi = 0xFF; 
 
  TIFR = TIFR | (1<