summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-02-06 22:00:08 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-02-06 22:00:08 +0000
commit3fb7cae1a71c87c719894d220bd4a0651a9402e7 (patch)
tree203c1a582d29dd033f564a79368813376468c85b /usb-i2c-sl018
parentcleanup (diff)
entire code now uses stdin and stdout (no stdio any more)
git-svn-id: https://svn.spreadspace.org/avr/trunk@137 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-i2c-sl018')
-rw-r--r--usb-i2c-sl018/keystore.c22
-rw-r--r--usb-i2c-sl018/keystore.h8
-rw-r--r--usb-i2c-sl018/sl018.c44
-rw-r--r--usb-i2c-sl018/sl018.h16
-rw-r--r--usb-i2c-sl018/tuer-rfid.c41
-rw-r--r--usb-i2c-sl018/usb_serial.c62
-rw-r--r--usb-i2c-sl018/usb_serial.h2
7 files changed, 94 insertions, 101 deletions
diff --git a/usb-i2c-sl018/keystore.c b/usb-i2c-sl018/keystore.c
index 05f43ca..c04f90d 100644
--- a/usb-i2c-sl018/keystore.c
+++ b/usb-i2c-sl018/keystore.c
@@ -33,42 +33,42 @@ typedef uint8_t keyslot_t[8];
keyslot_t EEMEM keystore[EEPROM_SIZE/sizeof(keyslot_t)];
-void flash_keystore_from_stdio(FILE * stdio)
+void flash_keystore_from_stdio(void)
{
keyslot_t ks;
uint8_t byte_pos=0;
- fprintf(stdio,"Flashing keys:\n\r");
- fflush(stdio);
+ printf("Flashing keys:\n\r");
+ fflush(stdout);
for(uint8_t ks_pos=0;ks_pos<EEPROM_SIZE/sizeof(ks);) {
usb_serial_task();
int16_t bytes_received = usb_serial_bytes_received();
while(bytes_received > 0) {
- ks[byte_pos++]=fgetc(stdio);
+ ks[byte_pos++]=fgetc(stdin);
bytes_received--;
if (byte_pos == sizeof(ks)) {
byte_pos=0;
eeprom_update_block(&ks,&keystore[ks_pos],sizeof(ks));
ks_pos++;
- fputc('.', stdio);
- fflush(stdio);
+ fputc('.', stdout);
+ fflush(stdout);
led_toggle();
}
}
}
- fprintf(stdio,"\n");
- fputc(0, stdio);
+ printf("\n");
+ fputc(0, stdout);
led_off();
}
-void dump_keystore_to_stdio(FILE * stdio)
+void dump_keystore_to_stdio(void)
{
keyslot_t ks;
for(uint8_t ks_pos=0;ks_pos<EEPROM_SIZE/sizeof(ks);ks_pos++) {
eeprom_read_block(&ks,&keystore[ks_pos],sizeof(ks));
for (uint8_t i=0; i< sizeof(ks); i++)
- fprintf(stdio,"%02X",ks[i]);
- fprintf(stdio,"\n\r");
+ printf("%02X",ks[i]);
+ printf("\n\r");
}
}
diff --git a/usb-i2c-sl018/keystore.h b/usb-i2c-sl018/keystore.h
index a3b42ad..b2adfd6 100644
--- a/usb-i2c-sl018/keystore.h
+++ b/usb-i2c-sl018/keystore.h
@@ -23,9 +23,11 @@
#ifndef R3TUER_keystore_h_INCLUDED
#define R3TUER_keystore_h_INCLUDED
-#include <stdio.h>
-void flash_keystore_from_stdio(FILE * stdio);
-void dump_keystore_to_stdio(FILE * stdio);
+
+#include <stdint.h>
+
+void flash_keystore_from_stdio(void);
+void dump_keystore_to_stdio(void);
uint8_t keystore_check_card(const uint8_t * uid, uint8_t uid_len);
#endif
diff --git a/usb-i2c-sl018/sl018.c b/usb-i2c-sl018/sl018.c
index 7127f12..68d8745 100644
--- a/usb-i2c-sl018/sl018.c
+++ b/usb-i2c-sl018/sl018.c
@@ -20,8 +20,10 @@
* 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/>.
*/
+
#include "sl018.h"
#include "LUFA/Drivers/Peripheral/TWI.h"
+#include <stdio.h>
#define SL018_TWI_ADDR 0xA0
#define SL018_TAG_STA_PIN PIND
@@ -149,78 +151,78 @@ uint8_t sl018_cmd_raw(const uint8_t* twi_send_buf, bool wait_for_answer)
return 0;
}
-uint8_t sl018_reset(FILE * stdio)
+uint8_t sl018_reset(void)
{
if(sl018_cmd_raw(SL018_CMD_ComReset, 0)) {
- fprintf(stdio, "I2C error\n\r");
+ printf("I2C error\n\r");
return 1;
}
return 0;
}
-uint8_t sl018_cmd(const uint8_t* twi_send_buf, FILE * stdio)
+uint8_t sl018_cmd(const uint8_t* twi_send_buf)
{
if(sl018_cmd_raw(twi_send_buf, 1)) {
- fprintf(stdio, "I2C error\n\r");
+ printf("I2C error\n\r");
return 1;
} else {
if(twi_recv_msg->len < 2) {
- fprintf(stdio,"SL018 Cmd,Error: short message received\n\r");
+ printf("SL018 Cmd,Error: short message received\n\r");
return 1;
}
if(twi_recv_msg->status) {
- fprintf(stdio,"SL018 Cmd,Error: '%s','%s'\n\r",SL018_cmd_tostring(twi_recv_msg->command),SL018_status_tostring(twi_recv_msg->status));
+ printf("SL018 Cmd,Error: '%s','%s'\n\r",SL018_cmd_tostring(twi_recv_msg->command),SL018_status_tostring(twi_recv_msg->status));
return 1;
}
sl018_message_t * twi_send_msg = (sl018_message_t *)twi_send_buf;
if(twi_send_msg->command != twi_recv_msg->command) {
- fprintf(stdio,"SL018 Cmd,Error: mismatch of sent and received command code: %02X,%02X\n\r",twi_send_msg->command,twi_recv_msg->command);
+ printf("SL018 Cmd,Error: mismatch of sent and received command code: %02X,%02X\n\r",twi_send_msg->command,twi_recv_msg->command);
}
}
return 0;
}
-void sl018_read_card_uid(uid_t * uid, FILE * stdio)
+void sl018_read_card_uid(uid_t * uid)
{
uid->length=0;
uid->buffer=NULL;
- fprintf(stdio, "CARD IN: ");
- if(!sl018_cmd(SL018_CMD_ComSelectCard,stdio))
+ printf( "CARD IN: ");
+ if(!sl018_cmd(SL018_CMD_ComSelectCard))
{
uint8_t uid_len = twi_recv_msg->len - sizeof(twi_recv_msg->command) - sizeof(twi_recv_msg->status) - 1;
if(uid_len == 255 || uid_len > MAX_UID_LEN) {
- fprintf(stdio," received UID length (%d) is to big for keystore \n\r", uid_len);
+ printf(" received UID length (%d) is to big for keystore \n\r", uid_len);
return;
}
uint8_t type = twi_recv_msg->data[uid_len];
uint8_t expected_uid_len = SL018_tagtype_to_uidlen(type);
if(expected_uid_len != uid_len) {
- fprintf(stdio," Invalid uid length (%d) for tag type: %s\n\r", uid_len, SL018_tagtype_tostring(type));
+ printf(" Invalid uid length (%d) for tag type: %s\n\r", uid_len, SL018_tagtype_tostring(type));
return;
}
for (uint8_t pos=0; pos<uid_len; pos++)
- fprintf(stdio,"%02X",twi_recv_msg->data[uid_len-pos-1]);
- fprintf(stdio, ", %s\n\r", SL018_tagtype_tostring(type));
+ printf("%02X",twi_recv_msg->data[uid_len-pos-1]);
+ printf( ", %s\n\r", SL018_tagtype_tostring(type));
if (0 < type && type < 7) {
uid->length= uid_len;
uid->buffer=twi_recv_msg->data;
} else {
- fprintf(stdio,"Ignoring unknown card type %02x\n\r",type);
+ printf("Ignoring unknown card type %02x\n\r",type);
}
}
}
-void sl018_set_led(uint8_t on,FILE * stdio)
+void sl018_set_led(uint8_t on)
{
if(on)
- sl018_cmd(SL018_CMD_ComRedLedOn,stdio);
+ sl018_cmd(SL018_CMD_ComRedLedOn);
else
- sl018_cmd(SL018_CMD_ComRedLedOff,stdio);
+ sl018_cmd(SL018_CMD_ComRedLedOff);
}
-uint8_t sl018_check_for_new_card(FILE * stdio)
+uint8_t sl018_check_for_new_card(void)
{
static uint8_t card_status = 0;
if(CARD_PRESENT != card_status) {
@@ -231,9 +233,9 @@ uint8_t sl018_check_for_new_card(FILE * stdio)
return 0;
}
-unsigned char * sl018_get_firmware_version(FILE * stdio)
+unsigned char * sl018_get_firmware_version(void)
{
- if(!sl018_cmd(SL018_CMD_ComGetFirmwareVersion,stdio)) {
+ if(!sl018_cmd(SL018_CMD_ComGetFirmwareVersion)) {
twi_recv_msg->data[sizeof(twi_recv_msg->data) - 1] = 0;
return twi_recv_msg->data;
} else {
diff --git a/usb-i2c-sl018/sl018.h b/usb-i2c-sl018/sl018.h
index a82bcc0..d3f3068 100644
--- a/usb-i2c-sl018/sl018.h
+++ b/usb-i2c-sl018/sl018.h
@@ -23,18 +23,18 @@
#ifndef R3TUER_sl018_h_INCLUDED
#define R3TUER_sl018_h_INCLUDED
-#include <stdio.h>
+
+#include <stdint.h>
typedef struct {
uint8_t length;
unsigned char * buffer;
- } uid_t;
-
+} uid_t;
-void sl018_set_led(uint8_t on,FILE * stdio);
-uint8_t sl018_check_for_new_card(FILE * stdio);
-void sl018_read_card_uid(uid_t * uid, FILE * stdio);
-uint8_t sl018_reset(FILE * stdio);
-unsigned char * sl018_get_firmware_version(FILE * stdio);
+void sl018_set_led(uint8_t on);
+uint8_t sl018_check_for_new_card(void);
+void sl018_read_card_uid(uid_t * uid);
+uint8_t sl018_reset(void);
+unsigned char * sl018_get_firmware_version(void);
#endif
diff --git a/usb-i2c-sl018/tuer-rfid.c b/usb-i2c-sl018/tuer-rfid.c
index 6823c71..eef038a 100644
--- a/usb-i2c-sl018/tuer-rfid.c
+++ b/usb-i2c-sl018/tuer-rfid.c
@@ -40,8 +40,6 @@
#include "sl018.h"
#include "keystore.h"
#include "usb_serial.h"
-FILE * * stdio_ptr = NULL;
-
void handle_stdio(uint8_t cmd)
{
@@ -50,33 +48,33 @@ void handle_stdio(uint8_t cmd)
reset2bootloader();
break;
case 'R':
- if(!sl018_reset(*stdio_ptr))
- fprintf(*stdio_ptr, "ok\n\r");
+ if(!sl018_reset())
+ printf("ok\n\r");
break;
case 'f': //get cardreader firmware version
do {
- unsigned char * firmware_str = sl018_get_firmware_version(*stdio_ptr);
+ unsigned char * firmware_str = sl018_get_firmware_version();
if(firmware_str)
- fprintf(*stdio_ptr, "%s\n\r",firmware_str);
+ printf("%s\n\r",firmware_str);
} while(0);
break;
case 'e': //flash eeprom
- flash_keystore_from_stdio(*stdio_ptr);
+ flash_keystore_from_stdio();
break;
case 'd': //dump eeprom - this breaks security!
- dump_keystore_to_stdio(*stdio_ptr);
+ dump_keystore_to_stdio();
break;
case 'o':
if(start_stepper(dir_open))
- fprintf(*stdio_ptr, "ok\n\r");
+ printf("ok\n\r");
else
- fprintf(*stdio_ptr, "error: already in progress\n\r");
+ printf("error: already in progress\n\r");
break;
case 'c':
if(start_stepper(dir_close))
- fprintf(*stdio_ptr, "ok\n\r");
+ printf("ok\n\r");
else
- fprintf(*stdio_ptr, "error: already in progress\n\r");
+ printf("error: already in progress\n\r");
break;
case '0': ledmatrix(off); break;
case '1': ledmatrix(red); break;
@@ -87,23 +85,23 @@ void handle_stdio(uint8_t cmd)
case '6': ledmatrix(green_blink); break;
case '7': ledmatrix(rg_moving); break;
case '8': ledmatrix(rg_blink); break;
- default: fprintf(*stdio_ptr, "error, unknown command %02X '%c'\n\r",cmd, cmd); return;
+ default: printf("error, unknown command %02X '%c'\n\r",cmd, cmd); return;
}
}
void handle_card(void)
{
uid_t uid;
- sl018_read_card_uid(&uid,*stdio_ptr);
+ sl018_read_card_uid(&uid);
if (uid.length)
{
if(keystore_check_card(uid.buffer,uid.length)) {
- sl018_set_led(1,*stdio_ptr);
+ sl018_set_led(1);
_delay_ms(255);
- fprintf(*stdio_ptr,"Card allowed - opening/closing door\n\r"); // TODO: open/close door!
- sl018_set_led(0,*stdio_ptr);
+ printf("Card allowed - opening/closing door\n\r"); // TODO: open/close door!
+ sl018_set_led(0);
} else {
- fprintf(*stdio_ptr,"Card not found in Database\n\r");
+ printf("Card not found in Database\n\r");
}
}
}
@@ -120,19 +118,18 @@ int main(void)
init_heartbeat();
init_stepper();
init_ledmatrix();
- stdio_ptr = usb_serial_get_stdio_ptr();
sei();
- sl018_reset(*stdio_ptr);
+ sl018_reset();
for(;;) {
usb_serial_task();
int16_t bytes_received = usb_serial_bytes_received();
if(bytes_received > 0)
- handle_stdio(fgetc(*stdio_ptr));
+ handle_stdio(fgetc(stdin));
- if (sl018_check_for_new_card(*stdio_ptr))
+ if (sl018_check_for_new_card())
handle_card();
handle_heartbeat();
}
diff --git a/usb-i2c-sl018/usb_serial.c b/usb-i2c-sl018/usb_serial.c
index 5a57480..f08d9b3 100644
--- a/usb-i2c-sl018/usb_serial.c
+++ b/usb-i2c-sl018/usb_serial.c
@@ -21,21 +21,13 @@
* along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <stdio.h>
+
#include "usb_serial.h"
#include <LUFA/Drivers/USB/USB.h>
#include "lufa-descriptor-usbserial.h"
#include "LUFA/Drivers/Peripheral/Serial.h"
-FILE usb_stream;
-FILE serial_stream;
-FILE * stdio = &serial_stream;
-
-FILE * * usb_serial_get_stdio_ptr(void)
-{
- return &stdio;
-}
-
-
/*
LUFA Library
Copyright (C) Dean Camera, 2012.
@@ -64,12 +56,26 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
},
};
+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*/
+
+FILE usb_stream;
+FILE serial_stream;
void usb_serial_init()
{
- USB_Init();
Serial_Init(115200,false);
Serial_CreateStream(&serial_stream);
+
+ USB_Init();
CDC_Device_CreateStream(&VirtualSerial_CDC_Interface,&usb_stream);
}
@@ -79,35 +85,23 @@ void usb_serial_task(void)
USB_USBTask();
}
-void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
+int16_t usb_serial_bytes_received(void)
{
- if(CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
- stdio = &usb_stream;
+ if(stdin == &usb_stream)
+ return CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
else
- stdio = &serial_stream;
-}
-
-void EVENT_USB_Device_Disconnect(void)
-{
- stdio = &serial_stream;
-}
-/* end LUFA CDC-ACM specific definitions*/
-
-void EVENT_USB_Device_ConfigurationChanged(void)
-{
- CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
+ return (int16_t)Serial_IsCharReceived();
}
-void EVENT_USB_Device_ControlRequest(void)
+void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
- CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
+ if(CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
+ stdin = stdout = &usb_stream;
+ else
+ stdin = stdout = &serial_stream;
}
-int16_t usb_serial_bytes_received(void)
+void EVENT_USB_Device_Disconnect(void)
{
- if(stdio == &usb_stream)
- return CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
- else
- return (int16_t)Serial_IsCharReceived();
+ stdin = stdout = &serial_stream;
}
-
diff --git a/usb-i2c-sl018/usb_serial.h b/usb-i2c-sl018/usb_serial.h
index 5f821f8..31ac95e 100644
--- a/usb-i2c-sl018/usb_serial.h
+++ b/usb-i2c-sl018/usb_serial.h
@@ -23,9 +23,7 @@
#ifndef R3TUER_usb_serial_h_INCLUDED
#define R3TUER_usb_serial_h_INCLUDED
-#include <stdio.h>
-FILE * * usb_serial_get_stdio_ptr(void);
void usb_serial_init(void);
void usb_serial_task(void);
int16_t usb_serial_bytes_received(void);