/* * spreadspace avr utils * * * Copyright (C) 2012 Christian Pointner * * This file is part of spreadspace avr utils. * * spreadspace avr utils is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * spreadspace avr utils is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with spreadspace avr utils. If not, see . */ #define TAG_STA_PIN PINB #define TAG_STA_BIT 7 // #define PIN_B_INTERRUPT 1<> TAG_STA_BIT) & 1) #include #include #include #include #include "util.h" #include "led.h" #include "LUFA/Drivers/Peripheral/TWI.h" #include "LUFA/Drivers/Peripheral/Serial.h" #include "LUFA/Drivers/Misc/RingBuffer.h" #include #define EEPROM_SIZE 1024 typedef union { uint64_t uint; uint32_t uint32[2]; unsigned char byte[8]; } keyslot_t; 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; //============================================ // Command List, preamble + length + command //============================================ const unsigned char SL018CMD_ComSelectCard[] ={1,1}; 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]; typedef struct __attribute__((__packed__)) { uint8_t command; uint8_t status; unsigned char data[sizeof(twi_rcv_buff)-3]; uint8_t len; } SL018CMD_MESSAGE; SL018CMD_MESSAGE * twi_message = (SL018CMD_MESSAGE *) &twi_rcv_buff; uint8_t card_status =0; FILE stdio_stream; FILE * stdio = &stdio_stream; #define KEY_LEN_MAX (sizeof(keyslot_t) - 1) /* this generates a Fletcher8 checksum */ /* code from: http://stackoverflow.com/questions/13491700/8-bit-fletcher-checksum-of-16-byte-data */ unsigned char generate_csum(unsigned char* data) { uint16_t sum1 = 0xf, sum2 = 0xf, len = KEY_LEN_MAX; do { sum2 += ( sum1 += *data++ ); } while (--len); return sum2<<4 | sum1; } /* LUFA Library Copyright (C) Dean Camera, 2012. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ #include #include "lufa-descriptor-usbserial.h" USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { .Config = { .ControlInterfaceNumber = 0, .DataINEndpointNumber = CDC_TX_EPNUM, .DataINEndpointSize = CDC_TXRX_EPSIZE, .DataINEndpointDoubleBank = false, .DataOUTEndpointNumber = CDC_RX_EPNUM, .DataOUTEndpointSize = CDC_TXRX_EPSIZE, .DataOUTEndpointDoubleBank = false, .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, .NotificationEndpointDoubleBank = false, }, }; void EVENT_USB_Device_ConfigurationChanged(void) { CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); CDC_Device_CreateStream(&VirtualSerial_CDC_Interface,stdio); } void EVENT_USB_Device_ControlRequest(void) { CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } void EVENT_USB_Device_Disconnect(void) { Serial_CreateStream(stdio); } /* end LUFA CDC-ACM specific definitions*/ unsigned char sl018_send_buffer(const unsigned char * buffer) { uint8_t len = 0; uint8_t pos = 0; //unsigned char ret; len = buffer[0]; if (TWI_StartTransmission(SL018_ADDR | SL018_WRITE,10) == TWI_ERROR_NoError) { for(pos=0; pos<=len; pos++) if( ! TWI_SendByte(buffer[pos])) { TWI_StopTransmission(); return 1; } // Must stop transmission afterwards to release the bus TWI_StopTransmission(); } else { return 1; } _delay_ms(50); if (TWI_StartTransmission(SL018_ADDR | SL018_READ,10) == TWI_ERROR_NoError) { TWI_ReceiveByte(&len, 0); twi_rcv_buff[255]=len; for(pos=0; pos0) { keyslot.byte[byte_pos++]=fgetc(stdio); BytesReceived--; if (byte_pos == sizeof(keyslot)) { byte_pos=0; eeprom_update_block(&keyslot,&keyslots[keyslot_pos],sizeof(keyslot)); keyslot_pos++; fprintf(stdio,"."); fflush(stdio); led_toggle(); } } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } fprintf(stdio,"\n"); } void dump_eeprom_to_stdio(void) { keyslot_t keyslot; uint8_t keyslot_pos=0; for(keyslot_pos=0;keyslot_posstatus) { fprintf(stdio,"SL018 Cmd,Error: %02X,%02X\n\r",twi_message->command,twi_message->status); return 0; } if(send_twi_message->command != twi_message->command) { fprintf(stdio,"SL018 Cmd missmatch: send, rcv: %02X,%02X\n\r",send_twi_message->command,twi_message->command); } } return 1; } void handle_cmd(uint8_t cmd) { switch(cmd) { case 'r': reset2bootloader(); break; case 'f': //get cardreader firmware version if(sl018_send_buffer_check_error(SL018CMD_ComGetFirmwareVersion)) { fprintf(stdio, "%s\n\r",twi_message->data); } break; case '4': //turn cardreader led off if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOff)) fprintf(stdio, "ok\n\r"); break; case '5': //turn cardreader led on if(sl018_send_buffer_check_error(SL018CMD_ComRedLedOn)) fprintf(stdio, "ok\n\r"); break; case 'e': //flash eeprom flash_eeprom_from_stdio(); break; case 'd': //flash eeprom dump_eeprom_to_stdio(); break; case 's': //get eeprom size fprintf(stdio,"%d\n\r",EEPROM_SIZE); break; default: fprintf(stdio, "error, unknown command %02X\n\r",cmd); return; } } void card_in_action(void) { uint8_t pos; fprintf(stdio, "CARD IN: "); if(sl018_send_buffer_check_error(SL018CMD_ComSelectCard)) { for (pos=twi_message->len - 4 ; pos< 255; pos--) { fprintf(stdio,"%02X",twi_message->data[pos]); } fprintf(stdio, "\n\r"); uint8_t type = twi_message->data[twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status)]; if (0data[0],twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status))) { sl018_send_buffer_check_error(SL018CMD_ComRedLedOn); fprintf(stdio,"open\n\r"); _delay_ms(255); sl018_send_buffer_check_error(SL018CMD_ComRedLedOff); } } else { fprintf(stdio,"Unknown Card Type %02x\n\r",type); } } } int main(void) { MCUSR &= ~(1 << WDRF); wdt_disable(); cpu_init(); led_init(); USB_Init(); TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 200000)); sei(); Serial_Init(115200,false); Serial_CreateStream(stdio); for(;;) { int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); if(BytesReceived > 0) { handle_cmd(fgetc(stdio)); } if(CARD_BOOL != card_status) { card_status = CARD_BOOL; if (!card_status) { card_in_action(); } } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }