summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018
diff options
context:
space:
mode:
authorOthmar Gsenger <otti@wirdorange.org>2013-01-27 21:19:38 +0000
committerOthmar Gsenger <otti@wirdorange.org>2013-01-27 21:19:38 +0000
commit5acf4ef7d87e2bf347ead39b206b1c66d5ddfe7b (patch)
tree6b2e85639e651643a78d7ada34507ed121fcf84c /usb-i2c-sl018
parentimproved error handling (diff)
eeprom write/dump working
git-svn-id: https://svn.spreadspace.org/avr/trunk@72 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-i2c-sl018')
-rw-r--r--usb-i2c-sl018/tuer-rfid.c182
1 files changed, 89 insertions, 93 deletions
diff --git a/usb-i2c-sl018/tuer-rfid.c b/usb-i2c-sl018/tuer-rfid.c
index f159047..46f3db3 100644
--- a/usb-i2c-sl018/tuer-rfid.c
+++ b/usb-i2c-sl018/tuer-rfid.c
@@ -19,14 +19,15 @@
* You should have received a copy of the GNU General Public License
* along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
*/
+
#define TAG_STA_PIN PINB
#define TAG_STA_BIT 4
-#define PIN_B_INTERRUPT 1<<TAG_STA_BIT
+// #define PIN_B_INTERRUPT 1<<TAG_STA_BIT
//#define PIN_C_INTERRUPT 0
//#define PIN_D_INTERRUPT 0
-#define CARD_IN 0xe1
-#define CARD_OUT 0xe0
-
+// #define CARD_IN 0xe1
+// #define CARD_OUT 0xe0
+#define CARD_BOOL ( (TAG_STA_PIN >> TAG_STA_BIT) & 1)
#include <avr/io.h>
#include <avr/wdt.h>
@@ -39,7 +40,12 @@
#include "LUFA/Drivers/Peripheral/TWI.h"
#include "LUFA/Drivers/Misc/RingBuffer.h"
-//const uint8_t SL018_ADDR = B10100000:
+#include <avr/eeprom.h>
+#define EEPROM_SIZE 1024
+
+typedef unsigned char keyslot_t[8];
+keyslot_t EEMEM keyslots[EEPROM_SIZE/sizeof(keyslot_t)];
+
const uint8_t SL018_ADDR = 0xa0;
const uint8_t SL018_WRITE = 0;
const uint8_t SL018_READ = 1;
@@ -48,24 +54,11 @@ const uint8_t SL018_READ = 1;
//============================================
const unsigned char SL018CMD_ComSelectCard[] ={1,1};
-//const unsigned char SL018CMD_ComLoginSector0[] ={9,2,0+2,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
-//const unsigned char SL018CMD_ComReadBlock1[] ={2,3,1+8};
-//const unsigned char SL018CMD_ComWriteBlock1[] ={18,4,1+8,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
-//const unsigned char SL018CMD_ComIntiPurse1[] ={6,6,1+8,0x78,0x56,0x34,0x12};
-//const unsigned char SL018CMD_ComReadPurse1[] ={2,5,1+8};
-//const unsigned char SL018CMD_ComIncrPurse1[] ={6,8,1+8,0x02,0x00,0x00,0x00};
-//const unsigned char SL018CMD_ComDecrPurse1[] ={6,9,1+8,0x01,0x00,0x00,0x00};
-//const unsigned char SL018CMD_ComCopyValue[] ={3,0x0A,1+8,2+8};
-//const unsigned char SL018CMD_ComReadUltralightPage5[] ={2,0x10,0x05};
-//const unsigned char SL018CMD_ComWriteUltralightPage5[]={6,0x11,0x05,0x12,0x34,0x56,0x78};
const unsigned char SL018CMD_ComRedLedOn[] ={2,0x40,1};
const unsigned char SL018CMD_ComRedLedOff[] ={2,0x40,0};
const unsigned char SL018CMD_ComGetFirmwareVersion[] ={1,0xF0};
unsigned char twi_rcv_buff[256];
-RingBuffer_t CmdBuffer;
-uint8_t CmdBufferData[128];
-
typedef struct __attribute__((__packed__))
{
@@ -76,7 +69,10 @@ typedef struct __attribute__((__packed__))
} SL018CMD_MESSAGE;
SL018CMD_MESSAGE * twi_message = (SL018CMD_MESSAGE *) &twi_rcv_buff;
+uint8_t card_status =0;
+FILE usb_stream;
+FILE * usb = &usb_stream;
/*
LUFA Library
Copyright (C) Dean Camera, 2012.
@@ -161,128 +157,128 @@ unsigned char sl018_send_buffer(const unsigned char * buffer)
void flash_eeprom_from_usb(void)
{
+ keyslot_t keyslot;
+ uint8_t keyslot_pos=0;
+ for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(keyslot);)
+ {
+ int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
+ while(BytesReceived >= sizeof(keyslot)) {
+ fread(&keyslot,sizeof(keyslot),1,usb);
+ eeprom_update_block(&keyslot,&keyslots[keyslot_pos],sizeof(keyslot));
+ BytesReceived-=sizeof(keyslot);
+ keyslot_pos++;
+ }
+ }
}
+void dump_eeprom_to_usb(void)
+{
+ keyslot_t keyslot;
+ uint8_t keyslot_pos=0;
+
+ for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(keyslot);keyslot_pos++)
+ {
+ eeprom_read_block(&keyslot,&keyslots[keyslot_pos],sizeof(keyslot));
+ for (uint8_t i=0; i< sizeof(keyslot); i++)
+ fprintf(usb,"%02X",keyslot[i]);
+ fprintf(usb,"\n\r");
+ }
+}
bool sl018_send_buffer_check_error(const unsigned char * buffer)
{
- char txt_buff[10];
+ SL018CMD_MESSAGE * send_twi_message = (SL018CMD_MESSAGE *) (buffer+1);
if(sl018_send_buffer(buffer))
{
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "I2C error\n\r");
+ fprintf(usb, "I2C error\n\r");
return 0;
} else {
if(twi_message->status)
{
CDC_Device_SendString(&VirtualSerial_CDC_Interface, "SL018 Cmd,Error: ");
- snprintf(txt_buff,sizeof(txt_buff),"%02X,%02X\n\r",twi_message->command,twi_message->status);
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, txt_buff);
+ fprintf(usb,"SL018 Cmd,Error: %02X,%02X\n\r",twi_message->command,twi_message->status);
return 0;
}
+ if(send_twi_message->command != twi_message->command)
+ {
+ fprintf(usb,"SL018 Cmd missmatch: send, rcv: %02X,%02X\n\r",send_twi_message->command,twi_message->command);
+ }
}
return 1;
}
void handle_cmd(uint8_t cmd)
-{
- char foo[10];
- uint8_t pos;
+{
switch(cmd) {
- case 'r': reset2bootloader(); break;
- case 'f': //get cardreader firmware version
- if(sl018_send_buffer_check_error(SL018CMD_ComGetFirmwareVersion))
- {
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, twi_message->data);
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "\n\r");
- }
- break;
- case '4': //turn cardreader led off
- if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOff))
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "ok\n\r");
- break;
- case '5': //turn cardreader led on
- if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOn))
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "ok\n\r");
- break;
- case CARD_IN: // card has been detected, read uid and print to usb
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "CARD IN: ");
- if(sl018_send_buffer_check_error(SL018CMD_ComSelectCard))
- {
- for (pos=twi_message->len - 4 ; pos< 255; pos--)
+ case 'r': reset2bootloader(); break;
+ case 'f': //get cardreader firmware version
+ if(sl018_send_buffer_check_error(SL018CMD_ComGetFirmwareVersion))
{
- snprintf(foo,sizeof(foo),"%02X",twi_message->data[pos]);
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, foo);
- }
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "\n\r");
- }
- break;
- case CARD_OUT:
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "CARD OUT\n\r");
- break;
- case 'e': //flash eeprom
- flash_eeprom_from_usb();
- break;
- default: CDC_Device_SendString(&VirtualSerial_CDC_Interface, "error\n\r"); return;
+ fprintf(usb, "%s\n\r",twi_message->data);
+ }
+ break;
+ case '4': //turn cardreader led off
+ if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOff))
+ fprintf(usb, "ok\n\r");
+ break;
+ case '5': //turn cardreader led on
+ if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOn))
+ fprintf(usb, "ok\n\r");
+ break;
+ case 'e': //flash eeprom
+ flash_eeprom_from_usb();
+ break;
+ case 'd': //flash eeprom
+ dump_eeprom_to_usb();
+ break;
+ case 's': //get eeprom size
+ fprintf(usb,"%d\n\r",EEPROM_SIZE);
+ break;
+ default: fprintf(usb, "error\n\r"); return;
}
}
-
-static void PCint(void)
+void card_in_action(void)
{
- if( (TAG_STA_PIN >> TAG_STA_BIT) & 1)
+ uint8_t pos;
+ fprintf(usb, "CARD IN: ");
+ if(sl018_send_buffer_check_error(SL018CMD_ComSelectCard))
{
- RingBuffer_Insert(&CmdBuffer,CARD_OUT);
- led_on();
- } else {
- RingBuffer_Insert(&CmdBuffer,CARD_IN);
- led_off();
- }
+ for (pos=twi_message->len - 4 ; pos< 255; pos--)
+ {
+ fprintf(usb,"%02X",twi_message->data[pos]);
+ }
+ fprintf(usb, "\n\r");
+ }
}
-SIGNAL(PCINT0_vect) {
- PCint();
-}
-//SIGNAL(PCINT1_vect) {
-// PCint();
-//}
-//SIGNAL(PCINT2_vect) {
-// PCint();
-//}
-
-
int main(void)
{
MCUSR &= ~(1 << WDRF);
wdt_disable();
- PCMSK0=PIN_B_INTERRUPT;
- //PCMSK1=PIN_C_INTERRUPT;
- //PCMSK2=PIN_D_INTERRUPT;
- PCICR|=1<<PCIE0;
- RingBuffer_InitBuffer(&CmdBuffer, CmdBufferData, sizeof(CmdBufferData));
-
cpu_init();
led_init();
led_on();
USB_Init();
TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 200000));
sei();
-
+ CDC_Device_CreateStream(&VirtualSerial_CDC_Interface,usb);
for(;;) {
int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
while(BytesReceived > 0) {
- int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
- if(!(ReceivedByte < 0)) {
- //handle_cmd(ReceivedByte);
- RingBuffer_Insert(&CmdBuffer,(uint8_t) ReceivedByte);
- }
+ handle_cmd(fgetc(usb));
BytesReceived--;
}
- while(! RingBuffer_IsEmpty(&CmdBuffer) )
+ if( CARD_BOOL != card_status)
{
- handle_cmd(RingBuffer_Remove(&CmdBuffer));
+ card_status = CARD_BOOL;
+ if (!card_status)
+ {
+ card_in_action();
+ }
}
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();