From 111699cae199b8fa994de09ad661ef9d7f1c845e Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sun, 18 Dec 2016 15:31:17 +0200 Subject: [PATCH] Added working rfid card read command --- src/cli_microrl.c | 26 +++++++++++++++++++++++++- src/cli_microrl.h | 1 + src/hmi_msg.c | 2 ++ src/hmi_msg.h | 5 +++++ src/main.c | 32 ++++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/cli_microrl.c b/src/cli_microrl.c index 5dd205d..b4e691e 100644 --- a/src/cli_microrl.c +++ b/src/cli_microrl.c @@ -6,6 +6,7 @@ #include "hmi_msg.h" #include "print_helper.h" #include "cli_microrl.h" +#include "../lib/matejx_avr_lib/mfrc522.h" typedef struct cli_cmd { PGM_P cmd; @@ -19,7 +20,8 @@ const cli_cmd_t cli_cmds[] = { {help_cmd, help_help, cli_print_help, 0}, {ver_cmd, ver_help, cli_print_ver, 0}, {ascii_cmd, ascii_help, cli_print_ascii_tbls, 0}, - {month_cmd, month_help, cli_handle_month, 1} + {month_cmd, month_help, cli_handle_month, 1}, + {read_cmd, read_help, cli_rfid_read, 0} }; @@ -111,6 +113,28 @@ void cli_handle_month(const char *const *argv) } +void cli_rfid_read(const char *const *argv) +{ + (void) argv; + Uid uid; + Uid *uid_ptr = &uid; + printf_P(PSTR("\n")); + if (PICC_IsNewCardPresent()) { + printf("Card selected!\n"); + PICC_ReadCardSerial(uid_ptr); + printf("UID size: 0x%02X\n", uid.size); + printf("UID sak: 0x%02X\n", uid.sak); + printf("Card UID: "); + for (byte i = 0; i < uid.size; i++) { + printf("%02X", uid.uidByte[i]); + } + printf_P(PSTR("\n")); + } else { + printf_P((PSTR("Unable to select card.\n"))); + } +} + + void cli_print_cmd_error(void) { putc('\n', stdout); diff --git a/src/cli_microrl.h b/src/cli_microrl.h index 528ca58..db96363 100644 --- a/src/cli_microrl.h +++ b/src/cli_microrl.h @@ -11,6 +11,7 @@ void print_version(FILE *stream); void cli_print_ver(const char *const *argv); void cli_print_ascii_tbls(const char *const *argv); void cli_handle_month(const char *const *argv); +void cli_rfid_read(const char *const *argv); void cli_print_cmd_error(void); void cli_print_cmd_arg_error(void); int cli_execute(int argc, const char *const *argv); diff --git a/src/hmi_msg.c b/src/hmi_msg.c index 5a8bb55..d6c4f5a 100644 --- a/src/hmi_msg.c +++ b/src/hmi_msg.c @@ -18,3 +18,5 @@ const char ascii_cmd[] PROGMEM = ASCII_CMD; const char ascii_help[] PROGMEM = ASCII_HELP; const char month_cmd[] PROGMEM = MONTH_CMD; const char month_help[] PROGMEM = MONTH_HELP; +const char read_cmd[] PROGMEM = READ_CMD; +const char read_help[] PROGMEM = READ_HELP; diff --git a/src/hmi_msg.h b/src/hmi_msg.h index f498a42..137d0dd 100644 --- a/src/hmi_msg.h +++ b/src/hmi_msg.h @@ -18,6 +18,9 @@ #define CLI_HELP_MSG "Implemented commands:" #define CLI_NO_CMD "Command not implemented.\n Use to get help." #define CLI_ARGS_MSG "To few or to many arguments for this command\nUse " +#define READ_CMD "read" +#define READ_HELP "Read and print out card info that is currently\nin proximity of the reader" + extern PGM_P const months[]; @@ -29,5 +32,7 @@ extern const char ascii_cmd[]; extern const char ascii_help[]; extern const char month_cmd[]; extern const char month_help[]; +extern const char read_cmd[]; +extern const char read_help[]; #endif /* _HMI_MSG_H_ */ diff --git a/src/main.c b/src/main.c index 3b7091c..882ff50 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -12,6 +12,7 @@ #include "../lib/hd44780_111/hd44780.h" #include "../lib/helius_microrl/microrl.h" #include "cli_microrl.h" +#include "../lib/matejx_avr_lib/mfrc522.h" #define BAUDRATE 9600 @@ -21,10 +22,14 @@ #define LED_TOGGLE PORTA ^= _BV(PORTA3) #define UART_STATUS_MASK 0x00FF +// Current system time +volatile uint32_t system_time; + // Create microrl object and pointer on it static microrl_t rl; static microrl_t *prl = &rl; + static inline void init_system_clock(void) { TCCR5A = 0; // Clear control register A @@ -34,6 +39,22 @@ static inline void init_system_clock(void) TIMSK5 |= _BV(OCIE5A); // Output Compare A Match Interrupt Enable } +static inline uint32_t time(void) +{ + uint32_t cur_time; + ATOMIC_BLOCK(ATOMIC_FORCEON) { + cur_time = system_time; + } + return cur_time; +} + + +static inline void init_rfid_reader(void) +{ + MFRC522_init(); + PCD_Init(); +} + static inline void init_hw (void) { @@ -53,6 +74,9 @@ static inline void init_hw (void) // LCD init lcd_init(); lcd_clrscr(); + + // Init RFID-RC522 + init_rfid_reader(); // Enable interupts sei(); @@ -78,8 +102,8 @@ static inline void start_cli(void) static inline void heartbeat (void) { - static time_t time_prev; - time_t time_cur = time(NULL); + static uint32_t time_prev; + uint32_t time_cur = time(); if (time_cur <= time_prev) { return; } @@ -106,5 +130,5 @@ int main (void) // System clock ISR(TIMER5_COMPA_vect) { - system_tick(); + system_time++; }