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

📄 level3.c

📁 em公司的RFID源程序,适合EM4094,绝对震撼!
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*-------------------------------------------------------------------------------
*--  RCSId: $Id: level3.c,v 1.50 2005-04-12 14:54:27+02 mjg Exp mjg $
*--         $Name:  $
*-------------------------------------------------------------------------------
*-- level3.c - High level data transformations and main loop 
*-------------------------------------------------------------------------------
*-- $Log: level3.c,v $
*-- Revision 1.50  2005-04-12 14:54:27+02  mjg
*-- switch to normal mode
*--
*-- Revision 1.49  2005-03-21 17:01:04+01  mjg
*-- Uplink timing tuned
*--
*-- Revision 1.47  2005-02-28 13:35:30+01  mjg
*-- EM4006 uid crc check
*--
*-- Revision 1.46  2005-02-25 11:23:13+01  mjg
*-- EM4006 extraction routine, no crc check
*--
*-- Revision 1.45  2005-02-25 11:19:41+01  mjg
*-- EM4006 extraction routine, no crc check
*--
*-- Revision 1.44  2005-02-25 09:12:50+01  mjg
*-- EM4006 decode mode
*--
*-- Revision 1.43  2005-02-23 16:04:15+01  mjg
*-- EM4006 raw mode, not tested
*--
*-- Revision 1.42  2005-02-21 18:13:55+01  mjg
*-- hubneme IV
*--
*-- Revision 1.41  2005-02-21 10:56:31+01  mjg
*-- hubneme III
*--
*-- Revision 1.40  2005-02-21 10:47:42+01  mjg
*-- hubneme III
*--
*-- Revision 1.38  2005-01-19 15:53:51+01  mjg
*-- EM4034 EAS off support corrected
*--
*-- Revision 1.37  2004-12-13 08:49:33+01  mjg
*-- compiler sensitive code in HWTransaction
*--
*-- Revision 1.36  2004-12-07 17:22:17+01  mjg
*-- version 4 of 1TS ready
*--
*-- Revision 1.35  2004-12-07 15:22:21+01  mjg
*-- 1.33 strikes back!
*--
*-- Revision 1.33  2004-06-30 12:47:41+02  mjg
*-- key selection extension
*--
*-- Revision 1.32  2004-05-31 16:41:51+02  mjg
*-- single carrier too slow caused CRC errors, speeded up
*--
*-- Revision 1.31  2004-05-21 09:29:37+02  mjg
*-- startup inventory, to toggle EAS off for EM4034
*--
*-- Revision 1.30  2004-05-10 09:23:28+02  mjg
*-- to toggle EAS off
*--
*-- Revision 1.29  2004-05-07 08:10:25+02  mjg
*-- HW auth passed
*--
*-- Revision 1.28  2004-04-27 17:05:44+02  mjg
*-- to better test the design
*--
*-- Revision 1.27  2004-04-21 15:48:47+02  mjg
*-- FSK low data rate support
*--
*-- Revision 1.26  2004-04-21 13:05:21+02  mjg
*-- EOF ignored because of FSK and 4034 support
*--
*-- Revision 1.25  2004-04-13 11:21:39+02  mjg
*-- sync
*--
*-- Revision 1.24  2004-03-25 16:09:19+01  mjg
*-- to add delayed EOF feature
*--
*-- Revision 1.23  2004-03-25 15:13:23+01  mjg
*-- to add delayed EOF feature
*--
*-- Revision 1.22  2004-03-24 13:35:36+01  mjg
*-- error msg needs better handling
*--
*-- Revision 1.21  2004-03-15 16:59:06+01  mjg
*-- to check SPI
*--
*-- Revision 1.20  2004-02-16 08:04:11+01  mjg
*-- preliminary CD
*--
*-- Revision 1.18  2004-02-10 19:24:40+01  mjg
*-- FSK to add valid bits
*--
*-- Revision 1.17  2004-02-10 18:30:46+01  mjg
*-- dual carrier raw, decoded 1st try
*--
*-- Revision 1.16  2004-02-09 18:03:33+01  mjg
*-- raw dual carrier capture
*--
*-- Revision 1.15  2004-02-08 12:30:52+01  mjg
*-- Decode var removed, FSK polling added
*--
*-- Revision 1.14  2004-02-07 14:59:47+01  mjg
*-- Response parsing correction
*--
*-- Revision 1.13  2004-02-03 08:42:52+01  mjg
*-- hubneme II
*--
*-- Revision 1.12  2004-02-03 07:24:37+01  mjg
*-- fwd debug implemented and removed from being compiled
*--
*-- Revision 1.11  2004-01-19 14:08:05+01  mjg
*-- hubneme I
*--
*-- Revision 1.10  2004-01-14 14:57:35+01  mjg
*-- to Invenotry version 2
*--
*-- Revision 1.9  2003-12-11 17:37:20+01  mjg
*-- yes
*--
*-- Revision 1.8  2003-12-11 16:33:41+01  mjg
*-- 1TS gives too much responses, stayquiet ?
*--
*-- Revision 1.7  2003-12-10 08:15:48+01  mjg
*-- to add dbg code
*--
*-- Revision 1.6  2003-12-09 12:43:01+01  mjg
*-- to 1TS inventory
*--
*-- Revision 1.5  2003-12-09 12:42:44+01  mjg
*-- to 1TS inventory
*--
*-- Revision 1.3  2003-12-05 08:38:56+01  mjg
*-- write update
*--
*-- Revision 1.2  2003-12-04 15:58:08+01  mjg
*-- to write
*--
*-- Revision 1.1  2003-12-01 08:18:04+01  mjg
*-- single carrier low drate level1 ok
*--
*-- Revision 1.0  2003-11-26 09:01:02+01  mjg
*-- Initial revision
*--
*-------------------------------------------------------------------------------
*/


#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include "level4.h"
#include "level3.h"
#include "level2.h"
#include "level1.h"

//--------------------------------------------------------------
//global variables

uint8_t maxCaptureTimeLow;           //lower part of current maximum capture time (used to initialise TCNT1)
uint8_t maxCaptureTimeHi;            //upper part of current maximum capture time (expected value 0xFF or 0xFE only)

//--------------------------------------------------------------
//local declarations

#define STD_WAIT 145                  //~318us = (145+13)x32 @ 16MHz , 13x32 uC clocks is a setup reserve

uint8_t clean_data_cnt;               //clean response data length

uint16_t maxTGeneral;                 //current datarate maximum response capture time
uint16_t maxTwa1ee;                   //

uint8_t signed_crc;                   //flag of signed response

uint8_t authSelectFlag;               //disable selection before HW Authentication

void SendCaptureData( uint8_t cmd, uint8_t ack );
void Compute_Timeouts( uint8_t flags );

uint8_t Inventory(void);
uint8_t HW_Authentication(void);
uint8_t ToggleEAS(void);
uint8_t GeneralRead(void);
uint8_t GeneralWrite(void);
void RF_Reset(void);
uint8_t SwitchToNormalMode(void);

//--------------------------------------------------------------
// main loop routine
//--------------------------------------------------------------

void main_receiver(void) {
  uint8_t check_stat;
  uint8_t fwd_bit_count;
  uint8_t i;
  uint16_t pom;

  if (watchdog_reset != 0) {
    FormatResponse_Short( 0x00, ERR_ASIC_ANTENNA_FAULT );
  } else {
    decode_number = 2;
    forward_link_type = 0x01;               //default value = 1 from 4 forward link    
  }

  while (21) {                              // infinite loop

    wdt_reset();                            // reset the watchdog

    if((check_stat = CheckIncommingMessage()) != UART_MESSAGE_OK)     // check any incoming message
      FormatResponse_Short( 0, check_stat );
    else {                                                            // no error, processing command

      forward_ptr = data_buffer;            // initialize fwd pointer
      fwd_bit_count = 0;
  
      switch (uart_command) {

//...............................................................................................

        case 0x83 :                               //1TS Inventory
          Inventory();
          Wait(512);
          FormatResponse_Short( uart_command, Inventory() );
          break;

//...............................................................................................

        case 0x88 :                               //general read command
          i = GeneralRead();

          if (debug_mode == 0) {

            if (clean_data_cnt == 0)
              SendCaptureData( uart_command, i );
            else
              FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );

          } else 
          if (debug_mode == 2) 
            SendCaptureData( uart_command, ERR_EM4035_CAPT_DATA );
          else
            FormatResponse_Data( uart_command, ERR_EM4035_RAW_DATA, capture_cnt, (uint8_t*)capture.capture_data );

          break;

//...............................................................................................

        case 0x89 :                               //HW Authentication
          authSelectFlag = 0;

          i = HW_Authentication();
      
          if (i == UART_MESSAGE_OK)
            FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );
          else
            SendCaptureData( uart_command, i );
          break;

//...............................................................................................

        case 0x8A :                               //Toggle EAS
          i = ToggleEAS();

          if (i == UART_MESSAGE_OK)
            FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );
          else
            SendCaptureData( uart_command, i );
          break;

//...............................................................................................

        case 0x8B :                               //Startup Invenotry

          //RF Reset
          RF_Reset();

          i = GeneralRead();

          if (debug_mode == 0) {

            if (clean_data_cnt == 0)
              SendCaptureData( uart_command, i );
            else
              FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );

          } else 
          if (debug_mode == 2) 
            SendCaptureData( uart_command, ERR_EM4035_CAPT_DATA );
          else
            FormatResponse_Data( uart_command, ERR_EM4035_RAW_DATA, capture_cnt, (uint8_t*)capture.capture_data );

          break;

//...............................................................................................

        case 0x8C :                               //HW Authentication without Select
          authSelectFlag = 1;
          i = HW_Authentication();
      
          if (i == UART_MESSAGE_OK)
            FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );
          else
            SendCaptureData( uart_command, i );
          break;

//...............................................................................................

        case 0x8D :                               //Switch to Normal mode
          i = SwitchToNormalMode();

          if (clean_data_cnt == 0) {
            SendCaptureData( uart_command, i );
          } else {
            FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );
          }
          break;

//...............................................................................................

        case 0x90 :                               //general write command

          i = GeneralWrite();

          if (debug_mode == 0) {

            if ((i == UART_MESSAGE_OK) && (clean_data_cnt != 0)) {
              FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );
            } else {          
              SendCaptureData( uart_command, i );
            }
          }
          else
          if (debug_mode == 2) 
            SendCaptureData( uart_command, ERR_EM4035_CAPT_DATA );
          else
            FormatResponse_Data( uart_command, ERR_EM4035_RAW_DATA, capture_cnt, (uint8_t*)capture.capture_data );

          break;

//...............................................................................................

        case 0x98 :                               //Read EM4006 UID

          EM4006_bitRate = (19 * (uint32_t)EM4006_bitRate) / 16;
          pom = EM4006_bitRate * 6;               //capture for max 384 bit periods
          EM4006_bitRate >>= 3;                   //T1 runs at clk/8 due to full bitrate range

          ClearCaptureBuffers();

          maxCaptureTimeLow = (uint8_t)pom; 
          maxCaptureTimeHi = pom >> 8; 
   
          Capture(5);

          if (debug_mode == 0) {

            clean_data_cnt = 0;

            i = Extract_EM4006( capture_cnt );

            if (i == UART_MESSAGE_OK)
              FormatResponse_Data( uart_command, UART_MESSAGE_OK, 10, (uint8_t*)data_buffer );
            else
              SendCaptureData( uart_command, i );
          }
          else
          if (debug_mode == 2) 
            SendCaptureData( uart_command, UART_MESSAGE_OK );
          else
            FormatResponse_Data( uart_command, UART_MESSAGE_OK, capture_cnt, (uint8_t*)capture.capture_data );

          break;

//...............................................................................................

        case 0xF0 :                               //RF reset using SPI configuration word
          WriteSPI( write_4094_low & 0xFFFE, write_4094_hi );
          FormatResponse_Short( uart_command, 0x00 );

          wdt_disable();

          i = write_tag_memory_delay;
          while (i-->0) {
            Wait(16384);
          }

          wdt_enable(0x7);

          WriteSPI( write_4094_low | 1, write_4094_hi );
          break;

//...............................................................................................

        case 0x00 :                               //no command
          continue;

//...............................................................................................

        default:
          FormatResponse_Short( 0, ERR_UART_UNKNOWN_CMD );
          break;
      }

      uart_command = 0;

    }

  }
}

//--------------------------------------------------------------
//compute the timeouts according to the flag byte
//
void Compute_Timeouts(uint8_t flags) {

  register uint16_t pom;
  register uint16_t corr;

  pom = expectedResponseLen * 16;        //512RF/32 = 16 TCNT0 pulses per bit
  pom += 128;                            //reserve
  corr = expectedResponseLen / 2 + 2;    //correction for dual carrier

  if ((flags & 2) == 0)
    pom *= 4;

⌨️ 快捷键说明

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