summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018/tuer-rfid.c
blob: 29542318158c62202c66c76304664d606c810649 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *  spreadspace avr utils
 *
 *
 *  Copyright (C) 2012 Christian Pointner <equinox@spreadspace.org>
 *
 *  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 <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_C_INTERRUPT 0
//#define PIN_D_INTERRUPT 0
#define CARD_IN 0xe1
#define CARD_OUT 0xe0


#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>

#include "util.h"
#include "led.h"

#include "LUFA/Drivers/Peripheral/TWI.h"
#include "LUFA/Drivers/Misc/RingBuffer.h"

//const uint8_t SL018_ADDR = B10100000:
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_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__))
{
  uint8_t command;
  uint8_t status;
  char data[sizeof(twi_rcv_buff)-3];
  uint8_t len;
} SL018CMD_MESSAGE;

SL018CMD_MESSAGE * twi_message = (SL018CMD_MESSAGE *) &twi_rcv_buff;

/*
             LUFA Library
     Copyright (C) Dean Camera, 2012.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/


#include <LUFA/Drivers/USB/USB.h>
#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);
}

void EVENT_USB_Device_ControlRequest(void)
{
  CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}
/* 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++)
          TWI_SendByte(buffer[pos]);
        // 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; pos<len; pos++)
          TWI_ReceiveByte(&twi_rcv_buff[pos], (pos == len-1) ? 1:0 );
        // Must stop transmission afterwards to release the bus
        TWI_StopTransmission();
    } else {
     return 1;
    }
    return 0;
}


void flash_eeprom_from_usb(void)
{

}


bool sl018_send_buffer_check_error(const unsigned char * buffer)
{
  char txt_buff[10];
  if(sl018_send_buffer(buffer))
  {
    CDC_Device_SendString(&VirtualSerial_CDC_Interface, "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);
      return 0;
    }
  }
  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--)
             {
               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;
  }
}


static void PCint(void)
{
  if( (TAG_STA_PIN >> TAG_STA_BIT) & 1)
  {
    RingBuffer_Insert(&CmdBuffer,CARD_OUT);
    led_on();
  } else {
    RingBuffer_Insert(&CmdBuffer,CARD_IN);
    led_off();
  }
}

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();

  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);
      }
      BytesReceived--;
    }
    while(! RingBuffer_IsEmpty(&CmdBuffer) )
    {
      handle_cmd(RingBuffer_Remove(&CmdBuffer));
    }
    CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
    USB_USBTask();
  }
}