⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 level3.c

📁 EM推出的可读写卡片EM4469
💻 C
📖 第 1 页 / 共 2 页
字号:
    CheckConfiguration();

    forward_link_type = 0x01;            //default value = 4050 forward link
    FormatResponse_Short( 0xFC, ERR_EM4469_BAD_CONF_DATA );
  }
}

//--------------------------------------------------------------
// write configuration values to EEPROM 

void WriteConfiguration(void) {

  eeprom_wb((uint8_t)&eeprom_config_data_rate, config_data_rate);   
  eeprom_wb((uint8_t)&eeprom_config_encoder, config_encoder);
  eeprom_wb((uint8_t)&eeprom_config_delayed, config_delayed);
  eeprom_wb((uint8_t)&eeprom_config_lwr, config_lwr);
  eeprom_wb((uint8_t)&eeprom_config_raw, config_raw);
  eeprom_wb((uint8_t)&eeprom_config_fwlink, config_forward_link);
}

//--------------------------------------------------------------
// pack actual configuration values to get_settings_low and ..._hi

void PackActualSettings(void) {

  uint8_t q;

  if (decode == manchester_capture)
    q = 1;
  else if (decode == biphase_capture)
    q = 2;
  else if (decode == miller_capture)
    q = 3;
  else
    q = 0xF;                               //else not used

  get_settings_low = (q<<6) | (halfDataRate - 1);
  q = (q >> 2) | (delayed << 4) | ((lwr & 3) << 6);
  get_settings_low |= (uint16_t)q << 8;
  get_settings_hi = (raw << 6) | (lwr >> 2) | (forward_link_type >> 1);
}



//-----------------------------------------------------------------
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//Function 0x80 - Read Word
// result in read_tag_memory_word_low, read_tag_memory_word_hi if result=0

uint8_t ReadWord(uint8_t address) {

  uint8_t check_stat;
  uint8_t fwd_bit_count;

  fwd_bit_count = Prepare_Cmd( FWD_CMD_READ );
  fwd_bit_count += Prepare_Addr( address );

  SendForward(fwd_bit_count);
  Wait(tpp_period);

  maxCaptureTimeLow = maxTRead; 
  maxCaptureTimeHi = 0;

  if (debug_mode == 1) {

    Capture(2);
    SendRawData(0x80);
    check_stat = 0xFF;
  }
  else 
  {
    Capture(1);

    read_tag_memory_word_low = 0;
    read_tag_memory_word_hi = 0;
    check_stat = SearchPattern( 0x0A, 0x3F, 0, 10 );  //search ACK[6 lsbits] within first 10 captured bits

    if (check_stat == 0) {
      
      check_stat = SearchPattern( 0x01, 0x3F, 0, 10 );  //search NACK within first 10 captured bits
 
      if (check_stat == 0) {        
        check_stat = ERR_EM4469_NEITHER_ACK;
      } else {
        check_stat = ERR_EM4469_NACK;
      }
    } else {
      check_stat = ExtractData(check_stat);
    }
  }
  return check_stat;
}

//--------------------------------------------------------------
//Function 0x81 - Write Word
// 

uint8_t WriteWord(uint8_t address, uint16_t word_low, uint16_t word_hi) {

  uint8_t check_stat;
  uint8_t fwd_bit_count;

  fwd_bit_count = Prepare_Cmd( FWD_CMD_WRITE );
  fwd_bit_count += Prepare_Addr( address );
  fwd_bit_count += Prepare_Data( word_low, word_hi );

  SendForward(fwd_bit_count);

  Wait(tpp_period);

  maxCaptureTimeHi = 0;
  if (raw == 0)
    maxCaptureTimeLow = maxTWrite;            
  else
    maxCaptureTimeLow = maxTWriteRaw;

  if (debug_mode == 1) {

    Capture(2);
    SendRawData(0x80);
    check_stat = 0xFF;
  }
  else 
  {
    Capture(1);

    if (raw == 0) {                                     //normal write acknowldge
      check_stat = SearchPattern( 0x01, 0x3F, 0, 10 );  //search NACK within first 10 captured bits
      if (check_stat == 0) {
        check_stat = SearchPattern( 0x0A, 0x1F, 0, 80 );    //search ACK[5 lsbits] within captured bits
        if (check_stat == 0) {
          check_stat = ERR_EM4469_NEITHER_ACK;
        } else {
          check_stat = UART_MESSAGE_OK;
        }
      } else {
        check_stat = ERR_EM4469_NACK;
      }

    } else {                                              //acknowledge + read after write data

      check_stat = SearchPattern( 0x01, 0x3F, 0, 10 );    //search NACK[6 lsbits] within first 10 captured bits

      if (check_stat == 0) {
      
        check_stat = SearchPattern( 0x0A, 0x1F, 0, 80 );  //search ACK within captured bits

        if (check_stat == 0) {        
          check_stat = ERR_EM4469_NEITHER_ACK;
        } else {

          check_stat = ExtractData(check_stat);
          if (check_stat == 0) {
            if ( (read_tag_memory_word_low != word_low) || (read_tag_memory_word_hi != word_hi) )
              check_stat = ERR_EM4469_BAD_RAW;
          }
        }
      } else {
        check_stat = ERR_EM4469_NACK;        
      }
    }
  }

  return check_stat;
}




//--------------------------------------------------------------
//Output of default read
// 

void SendCaptureData(uint8_t cmd) {

  uint8_t check_stat;

  UDR = 0x02;
  while (!(UCSRA & (1<<UDRE)))
    {} 
  UDR = 4 + 2 * capture_cnt;
  while (!(UCSRA & (1<<UDRE)))
    {} 
  UDR = cmd;
  while (!(UCSRA & (1<<UDRE)))
    {} 
  UDR = 0;
  while (!(UCSRA & (1<<UDRE)))
    {} 

  check_stat = (4 + 2 * capture_cnt) ^ cmd;
          
  read_ptr = capture_data;
  read_pos = capture_cnt;             //start sending data
  while (read_pos > 0) {
    UDR = *read_ptr;
    check_stat ^= *read_ptr++;
    read_pos--;
    while (!(UCSRA & (1<<UDRE)))
      {} 
  }

  read_ptr = capture_valid;
  read_pos = capture_cnt;            //start sending valid
  while (read_pos > 0) {
    UDR = *read_ptr;
    check_stat ^= *read_ptr++;
    read_pos--;
    while (!(UCSRA & (1<<UDRE)))
      {} 
  }

  UDR = check_stat;
  while (!(UCSRA & (1<<UDRE)))
    {} 
  UDR = 0x03;
  while (!(UCSRA & (1<<UDRE)))
    {} 
}

//--------------------------------------------------------------

void SendRawData(uint8_t cmd) {

    uint8_t check_stat;

    UDR = 0x02;
    while (!(UCSRA & (1<<UDRE)))
      {} 
    UDR = 4 + capture_cnt;
    while (!(UCSRA & (1<<UDRE)))
      {} 
    UDR = cmd;
    while (!(UCSRA & (1<<UDRE)))
      {} 
    UDR = 0;
    while (!(UCSRA & (1<<UDRE)))
      {} 

    check_stat = (4 + capture_cnt) ^ cmd;
          
    read_ptr = (uint8_t*)uart_out_buffer;
    read_pos = capture_cnt;             //start sending data
    while (read_pos > 0) {
      UDR = *read_ptr;
      check_stat ^= *read_ptr++;
      read_pos--;
      while (!(UCSRA & (1<<UDRE)))
        {} 
    }

    UDR = check_stat;
    while (!(UCSRA & (1<<UDRE)))
      {} 
 
    UDR = 0x03;
    while (!(UCSRA & (1<<UDRE)))
      {} 
}


//--------------------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------

//#define AUTO

//--------------------------------------------------------------
//Read Only data check - suggested smallest code size

uint8_t CheckROData(void) {

  uint8_t line_parity;
  uint8_t col_parity;

  uint32_t data, d = 0;
  uint8_t pom, i, j;

  data = RO_value_low;

  if(((uint8_t)data & 1) != 0) return 0;

  col_parity = (uint8_t)data;
  data >>= 5;

  for(j=0; j<10; j++) {  

    if (j==5) data |= (uint8_t)RO_value_hi << 2;    //merge

    line_parity = (uint8_t)data & 1;
    col_parity ^= (uint8_t)data;
    data >>= 1;

    for(i=0; i<4; i++) {

      if ((i==1)&&(j==5)) data = RO_value_hi;

      d <<= 1;
      pom = (uint8_t)data & 1;
      line_parity ^= pom;
      d |= pom;
      data >>= 1;
    }

#ifdef AUTO
    UDR = line_parity & 1;
    while (!(UCSRA & (1<<UDRE)))
      {} 
#endif

    if (line_parity != 0) return 0;
    if (j == 7) RO_data = d;
  }
      
  if ((col_parity & 0x1E) != 0) return 0;

  RO_custID = (uint8_t)d;
 
  return 1;
}



//----------------------------------------------------------------------------
//Read Only Capture - performs one default read and checks the header presence
//                  - header is (0)111111111
uint8_t ROCapture_lowLevel(void) {

  uint8_t status;
#ifdef AUTO
  uint8_t i;
  uint32_t data;
#endif

  maxCaptureTimeLow = (uint16_t)maxTDefaultRead; 
  maxCaptureTimeHi = (uint8_t)(maxTDefaultRead >> 16);
  Capture(1);

  status = SearchPattern( 0x1FF, 0x3FF, 0, 128 );  //search r/o header within first 128 captured bits
  if (status != 0) {    

    status -= 9;
#ifdef AUTO
    UDR = status;
    while (!(UCSRA & (1<<UDRE)))
      {} 

    read_ptr = capture_data + status/8;
    read_pos = 9;
    while (read_pos > 0) {
      UDR = *read_ptr++;
      read_pos--;
      while (!(UCSRA & (1<<UDRE)))
        {} 
    }

    read_ptr = capture_valid + status/8;
    read_pos = 9;            //start sending valid
    while (read_pos > 0) {
      UDR = *read_ptr++;
      read_pos--;
      while (!(UCSRA & (1<<UDRE)))
        {} 
    }
#endif

    status = TestValidRange(status, 64);
#ifdef AUTO
    UDR = status;
    while (!(UCSRA & (1<<UDRE)))
      {} 
#endif
    if(status != 0) {

#ifdef AUTO
      data = RO_value_hi;
      for(i=0;i<4;i++) {
        UDR = data >> 24;
        data <<= 8;
        while (!(UCSRA & (1<<UDRE)))
          {} 
      }
      data = RO_value_low;
      for(i=0;i<4;i++) {
        UDR = data >> 24;
        data <<= 8;
        while (!(UCSRA & (1<<UDRE)))
          {} 
      }
#endif

      status = CheckROData();
    }

  }
  
  return status;
}


//-----------------------------------------------------------------
//Read Only Capture - configures the decoder and performs two reads

uint8_t ROCapture(uint8_t demod, uint8_t hDRate) {

  uint8_t status;

  config_encoder = demod;
  config_data_rate = hDRate;
  config_lwr = 8;
  CheckConfiguration();

  status = ROCapture_lowLevel();
  
  return status;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -