?? level3.c
字號:
/*
*-------------------------------------------------------------------------------
*-- RCSId: $Id: level3.c,v 1.33 2004-06-30 12:47:41+02 mjg Exp mjg $
*-- $Name: $
*-------------------------------------------------------------------------------
*-- level3.c - High level data transformations and main loop
*-------------------------------------------------------------------------------
*-- $Log: level3.c,v $*-- 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)
//register uint8_t capture_cnt asm ("r8"); //store_bit current capture byte index (this is a mirror from main.c)
//--------------------------------------------------------------
//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 maxTRead; //current datarate maximum response capture time
uint16_t maxTGeneral; //current datarate maximum response capture time
uint16_t maxTwa1ee; //
uint8_t signed_crc; //flag of signed response
void SendCaptureData( uint8_t cmd, uint8_t ack );
void Send(uint8_t len, uint16_t wait_time);
void Compute_Timeouts( uint8_t flags );
uint8_t Inventory(void);
uint8_t HW_Authentication(void);
uint8_t ToggleEAS(void);
uint8_t GeneralRead(void);
//--------------------------------------------------------------
// main loop routine
//--------------------------------------------------------------
void main_receiver(void) {
uint8_t check_stat;
uint8_t fwd_bit_count;
uint8_t i;
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
counter1set = (1<<ICNC1) | (1<<CS10);
}
enable_capture = 0;
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 0x85 : //Stay Quiet
Send( cmd_message_len, 0 );
FormatResponse_Short( uart_command, 0x00 );
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
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
WriteSPI( write_4094_low & 0xFFFE, write_4094_hi );
Wait(32768);
WriteSPI( write_4094_low | 1, write_4094_hi );
Wait(1000); //wait for ~2ms
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 0x90 : //general write command
signed_crc = 0;
if (cmd_message[1] == 0xE3) { //signed operation
//sign the CRC
HWSign( &cmd_message[cmd_message_len-2], &cmd_message[cmd_message_len-2], 16);
if ((cmd_message[0] & 0x40) != 0) {
signed_crc = 1;
}
}
Send( cmd_message_len, 0 );
if ((cmd_message[0] & 0x40) && ((cmd_message[1] == 0x21) ||
(cmd_message[1] == 0x22) || (cmd_message[1] == 0x27))) { //optionFlag = '1'
Wait( write_tag_memory_delay );
SendEOF();
maxCaptureTimeLow = (uint8_t)maxTGeneral;
maxCaptureTimeHi = maxTGeneral >> 8;
} else { //optionFlag = '0'
maxCaptureTimeLow = (uint8_t)maxTwa1ee;
maxCaptureTimeHi = maxTwa1ee >> 8;
}
Capture(2);
if (debug_mode == 0) {
clean_data_cnt = 0;
if (sof >= 1) {
i = 248 - ExtractData( 248 ); //extract data, max effective size is 248/8 bytes
if ((i >= 33) && (data_buffer[0] != 0)) { //error message found
i = ERR_EM4035_ERROR_MSG;
clean_data_cnt = 4;
if (signed_crc != 0) {
//unsign
HWSign( (uint8_t*)&data_buffer[clean_data_cnt-2], (uint8_t*)&data_buffer[clean_data_cnt-2], 16);
}
} else if ((i-1) < expectedResponseLen) { //no good header found or not enough data
clean_data_cnt = 0;
i = ERR_EM4035_WRONG_LEN;
} else { //clamp data to bytes
i = 0;
clean_data_cnt = expectedResponseLen / 8;
if (signed_crc != 0) {
//unsign
HWSign( (uint8_t*)&data_buffer[clean_data_cnt-2], (uint8_t*)&data_buffer[clean_data_cnt-2], 16);
}
}
if (clean_data_cnt != 0) {
FormatResponse_Data( uart_command, i, clean_data_cnt, (uint8_t*)data_buffer );
break;
}
} else {
i = ERR_EM4035_NO_SOF;
}
SendCaptureData( 0x90, 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 0xF1 : //write SPI configuration word directly
WriteSPI( write_4094_low, write_4094_hi );
FormatResponse_Short( uart_command, 0x00 );
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;
else
corr >>= 2;
if ((flags & 1) == 1)
pom -= corr;
pom = (38 * (uint32_t)pom) / 32 + 1;
decode_number = flags & 3;
switch (decode_number) {
case 0 : //single carrier, low datarate
halfDataRate = 288 * 4;
pulsesPerBit = 32;
// maxTRead = 7904 + 1; // = 4 * ((768 + 24 * 32 + 256 + 256) + 96 * 512 + (256 + 256 + 24 * 32 + 768)) * 38 / 32 / 32 + 1;
break;
case 1 : //dual carrier, low_datarate
halfDataRate = 288*4;
pulsesPerBit = 32;
// maxTRead = 7842 + 1; //4 * ((27*28 + 24 * 32 + 508) + 96 * 508 + (508 + 24 * 32 + 27*28)) * 38 / 32 / 32 + 1;
break;
case 2 : //single carrier, high datarate
halfDataRate = 288;
pulsesPerBit = 8;
// maxTRead = 1976 + 1; //((768 + 24 * 32 + 256 + 256) + 96 * 512 + (256 + 256 + 24 * 32 + 768)) * 38 / 32 / 32 + 1;
break;
case 3 : //dual carrier, high datarate
halfDataRate = 288;
pulsesPerBit = 8;
// maxTRead = 1960 + 1; //((27*28 + 24 * 32 + 508) + 96 * 508 + (508 + 24 * 32 + 27*28)) * 38 / 32 / 32 + 1;
break;
default :
decode_number = 0xFF;
}
maxTGeneral = pom;
maxTwa1ee = write_tag_memory_delay + pom;
}
//--------------------------------------------------------------
//Output of default read
//
void SendCaptureData(uint8_t cmd, uint8_t ack) {
uint8_t check_stat;
SendByte(0x02);
SendByte(4 + 2 * capture_cnt);
SendByte(cmd);
SendByte(ack);
check_stat = (4 + 2 * capture_cnt) ^ cmd ^ ack;
check_stat ^= Block_Output (capture_cnt, capture.capture_data);
check_stat ^= Block_Output (capture_cnt, capture.capture_valid);
SendByte(check_stat);
SendByte(0x03);
}
//--------------------------------------------------------------
//send command and wait
//
void Send(uint8_t len, uint16_t wait_time) {
uint8_t fwd_bit_count;
int i;
forward_ptr = data_buffer;
fwd_bit_count = 0;
for(i=0; i < len; i++)
fwd_bit_count += Prepare_Data( cmd_message[i] );
Compute_Timeouts(cmd_message[0]);
ClearCaptureBuffers();
maxCaptureTimeLow = (uint8_t)maxTGeneral;
maxCaptureTimeHi = maxTGeneral >> 8;
SendForward(fwd_bit_count);
if (wait_time != 0) Wait( wait_time );
}
//--------------------------------------------------------------
// general read function
//
uint8_t GeneralRead(void) {
uint8_t i = UART_MESSAGE_OK;
signed_crc = 0;
if (cmd_message[1] == 0xE2) { //signed operation
//sign the CRC
HWSign( &cmd_message[cmd_message_len-2], &cmd_message[cmd_message_len-2], 16);
if ((cmd_message[0] & 0x40) != 0) {
signed_crc = 1;
}
}
Send( cmd_message_len, STD_WAIT );
maxCaptureTimeLow = (uint8_t)maxTGeneral;
maxCaptureTimeHi = maxTGeneral >> 8;
Capture(2);
if (debug_mode == 0) {
int i;
clean_data_cnt = 0;
if (sof >= 1) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -