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

📄 level4.c

📁 em公司的RFID源程序,适合EM4094,绝对震撼!
💻 C
📖 第 1 页 / 共 2 页
字号:

    case UART_READ_SIZE :            //capture size byte
      if ((c >= 3) || (c <= 9)) {
        uart_read_bytes = c;
        uart_read_msg_bytes = c;
        uart_state = UART_READ_BYTES;
      } else {
        result = ERR_UART_INTERBYTE_ERR;
        uart_state = UART_WAIT_ERROR_SENT;
      }
      break;

    case UART_READ_BYTES :           //read specified size bytes to allow 

      if (--uart_read_bytes == 0) {  //last byte - ETX expected
        //check ETX
        if (c == 0x03) {
          result = UART_MESSAGE_OK;
          uart_state = UART_VALID;
          uart_in_end = ptr;
        } else {
          result = ERR_UART_NO_ETX;
          uart_state = UART_WAIT_ERROR_SENT;
        }
      }
      break;
    };

    if(++ptr == UART_IN_BUFFER_SIZE)
      ptr = 0;
  }

  if (uart_state == UART_WAIT_ERROR_SENT) {
    uart_in_read = uart_in_write;      //dropped =async
    return result;
  }

  if (uart_state == UART_VALID) {
    result = ParseMessage();            //parse message and commit the command
    uart_in_read = ptr;
    return result;
  }
  return result;
}


// ==================================================================
// copy

void Copy(uint8_t *dst, uint8_t *src, uint8_t len) {

  while (len-- > 0)
    *dst++ = *src++;
}

// ==================================================================
// block copy

void BlockCopy(uint8_t src) {
  Copy(cmd_message, &temp_buffer[src], cmd_message_len);
}


// ==================================================================
// parses valid message format and checks size according to the command type

uint8_t ParseMessage(void) {

  uint8_t byte = 0;
  uint8_t ptr = uart_in_read;
  uint8_t ptr2 = 0, c;
  
  //compute checksum
  byte = uart_in_buffer[ptr];

  while (ptr != uart_in_end) {
    c = uart_in_buffer[ptr++];
    if(ptr == UART_IN_BUFFER_SIZE) 
      ptr = 0;

    byte ^= c;
    temp_buffer[ptr2++] = c;    
  }

  if (byte == 0) {  //checksum ok

    byte = temp_buffer[2];

    //switch in this case is very inefficient

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

    if (byte == 0x83) {                                //1TS Inventory
        if (uart_read_msg_bytes < 8)
          return ERR_UART_INTERBYTE_ERR;

        expectedResponseLen = 0x60;

        cmd_message_len = temp_buffer[1] - 3;   //copy message
        if (cmd_message_len > sizeof(cmd_message)) return ERR_UART_INTERBYTE_ERR;

        BlockCopy( 3 );

        message_flags = cmd_message[0];
    } else

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

    if (byte == 0x85) {                               //Stay Quiet
        if (uart_read_msg_bytes != 15)
          return ERR_UART_INTERBYTE_ERR;

        cmd_message_len = temp_buffer[1] - 3;   //copy message
        if (cmd_message_len > sizeof(cmd_message)) return ERR_UART_INTERBYTE_ERR;

        BlockCopy( 3 );

        Send( cmd_message_len, 0 );

        FormatResponse_Short( byte, UART_MESSAGE_OK );
        byte = 0;

    } else

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

    // 88 - General Read Tag Command
    // 89 - HW Authentication
    // 8A - Toggle EAS Off
    // 8B - Startup Inventory
    // 8C - HW Authentication without selection
    // 8D - Switch to normal mode

    if ((byte >= 0x88)&&(byte <= 0x8D)) {    
        expectedResponseLen = temp_buffer[3];

        cmd_message_len = temp_buffer[1] - 4;   //copy message
        if (cmd_message_len > sizeof(cmd_message)) return ERR_UART_INTERBYTE_ERR;

        BlockCopy( 4 );

        message_flags = cmd_message[0];
    } else

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

    if (byte == 0x90) {                               //Write Tag 
        expectedResponseLen = temp_buffer[3];

        write_tag_memory_delay = *(uint16_t*)&temp_buffer[4];

        cmd_message_len = temp_buffer[1] - 6;   //copy message
        if (cmd_message_len > sizeof(cmd_message)) return ERR_UART_INTERBYTE_ERR;

        BlockCopy( 6 );

    } else

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

    if (byte == 0x98) {                                //Read EM4006 UID

      if (uart_read_msg_bytes != 4)
        return ERR_UART_INTERBYTE_ERR;

      EM4006_scale = temp_buffer[3];                   //scale of EM4006 bit rate 

      if (EM4006_scale < 7)
        return ERR_EM4035_WRONG_DR;

      EM4006_bitRate = ((uint16_t)1) << EM4006_scale;
      EM4006_scale -= 7;

    } else

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

    if (byte == 0xF0) {                               //RF Reset
        if (uart_read_msg_bytes != 4)
          return ERR_UART_INTERBYTE_ERR;
        write_tag_memory_delay = temp_buffer[3];
    } else

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

    if (byte == 0xF1) {                               //EM4094 direct SPI write
        if (uart_read_msg_bytes != 7)
          return ERR_UART_INTERBYTE_ERR;

        write_4094_low = *(uint16_t*)&temp_buffer[3];
        write_4094_hi = *(uint16_t*)&temp_buffer[5];

        WriteSPI( write_4094_low, write_4094_hi );
        FormatResponse_Short( byte, UART_MESSAGE_OK );

        byte = 0;
    } else

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

    if (byte == 0xF8) {                               //Send Capture Data
        if (uart_read_msg_bytes != 3)
          return ERR_UART_INTERBYTE_ERR;
        SendCaptureData( 0xF8, 0 );
        byte = 0;
    } else

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

    if (byte == 0xF9) {                               //Toggle debug mode
        if (uart_read_msg_bytes != 4)
          return ERR_UART_INTERBYTE_ERR;
        debug_mode = temp_buffer[3];
        FormatResponse_Short( 0xF9, 0 );
        byte = 0;
    } else

//...............................................................................................
#if 0
//#ifdef DEBUG
    if (byte == 0xFA) {                               //Forward link pulses tuning      
        if (uart_read_msg_bytes != 0x6)
          return ERR_UART_INTERBYTE_ERR;
        fwd_delays[temp_buffer[3]] = temp_buffer[4] | (temp_buffer[5] << 8);
        FormatResponse_Short( 0xFA, 0 );
        byte = 0;
    } else
#endif
//...............................................................................................

    if (byte == 0xFE) {                               //Switch Coil
        if (uart_read_msg_bytes != 4)
          return ERR_UART_INTERBYTE_ERR;
        switch_coil_byte = temp_buffer[3];

        if (switch_coil_byte & 1) cbi( PORTC, DCLK_PIN );            //set shutdown == antenna on
        else sbi( PORTC, DCLK_PIN );                                 //reset shutdown == antenna off

        FormatResponse_Short( 0xFE, UART_MESSAGE_OK );
        byte = 0;
    } else

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

    if (byte == 0xFD) {                               //Reader Status
        if (uart_read_msg_bytes != 3)
          return ERR_UART_INTERBYTE_ERR;

        FormatResponse_Data( byte, UART_MESSAGE_OK, 4, (uint8_t*)VERSION );
        byte = 0;
    } else {
        return ERR_UART_UNKNOWN_CMD;
    }

    //command was parsed successfully

    uart_command = byte;
    return UART_MESSAGE_OK;
  } else {   //bad checksum
    return ERR_UART_BAD_CRC;
  }
}


// ==================================================================
// ==================================================================
// Response and error emmision
// ==================================================================
// ==================================================================

uint8_t SendHeader(uint8_t cmd, uint8_t ack, uint8_t sz) {

  SendByte(0x02);
  SendByte(sz);
  SendByte(cmd);
  SendByte(ack);

  if (ack == 0) SetLEDOff(); else SetLEDOn();
  return sz ^ cmd ^ ack;
}

//==================================================================
void FormatResponse_Short(uint8_t cmd, uint8_t ack) {

  SendByte(SendHeader(cmd, ack, 0x04));  
  SendByte(0x03);

  if (ack == 0) SetLEDOff(); else SetLEDOn();
}


//==================================================================
//Send one raw byte
// 
void SendByte(uint8_t byte) {
  UDR = byte;
  while (!(UCSRA & (1<<UDRE)))
    {} 
}


//==================================================================

uint8_t Block_Output (uint8_t cnt, uint8_t *data) {
  register uint8_t check_stat;

  check_stat = 0;

  while (cnt > 0) {
    UDR = *data;
    check_stat ^= *data++;
    cnt--;
    while (!(UCSRA & (1<<UDRE)))
      {}; 
  }
  return check_stat;
}

//==================================================================


void FormatResponse_Data(uint8_t cmd, uint8_t ack, uint8_t cnt, uint8_t *data) {

  uint8_t check_stat;

  check_stat = SendHeader(cmd, ack, 4 + cnt);

  check_stat ^= Block_Output( cnt, data );         

  SendByte(check_stat);
  SendByte(0x03); 
}

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

void SendCaptureData(uint8_t cmd, uint8_t ack) {

  uint8_t check_stat;

  check_stat = SendHeader(cmd, ack, 4 + 2 * capture_cnt);

  check_stat ^= Block_Output (capture_cnt, capture.capture_data);

  check_stat ^= Block_Output (capture_cnt, capture.capture_valid);

  SendByte(check_stat);
  SendByte(0x03);
}

⌨️ 快捷键说明

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