1
0
Fork 0
i237/src/main.c

204 lines
4.8 KiB
C
Raw Normal View History

2016-12-18 22:43:53 +02:00
/* Copyright (C) 2016 Arti Zirk <arti.zirk@gmail.com>
*
* This file is part of I237 Door Access program.
*
* I237 Door Access 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
* (at your option) any later version.
*
* I237 Door Access 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 I237 Door Access. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
2016-12-18 15:31:17 +02:00
#include <util/atomic.h>
#include <avr/io.h>
2016-11-07 01:42:18 +02:00
#include <avr/interrupt.h>
2016-10-10 22:24:33 +03:00
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "hmi_msg.h"
2016-11-07 01:42:18 +02:00
#include "../lib/andygock_avr-uart/uart.h"
#include "uart_wrap.h"
#include "print_helper.h"
2016-10-10 22:24:33 +03:00
#include "../lib/hd44780_111/hd44780.h"
2016-12-01 16:19:28 +02:00
#include "../lib/helius_microrl/microrl.h"
#include "cli_microrl.h"
2016-12-18 15:31:17 +02:00
#include "../lib/matejx_avr_lib/mfrc522.h"
2016-12-18 21:34:59 +02:00
#include "rfid.h"
2016-12-01 16:19:28 +02:00
2016-11-07 01:42:18 +02:00
#define BAUDRATE 9600
2016-11-07 01:42:18 +02:00
// For configuring arduino mega pin 25
2016-12-18 21:34:59 +02:00
#define LED_INIT DDRA |= _BV(DDA3)
#define DOOR_INIT DDRA |= _BV(DDA1)
2016-11-07 02:30:33 +02:00
#define LED_TOGGLE PORTA ^= _BV(PORTA3)
2016-12-18 21:34:59 +02:00
#define DOOR_OPEN PORTA |= _BV(PORTA1)
#define DOOR_CLOSE PORTA &= ~_BV(PORTA1)
2016-12-01 16:19:28 +02:00
#define UART_STATUS_MASK 0x00FF
2016-12-18 15:31:17 +02:00
// Current system time
volatile uint32_t system_time;
2016-12-01 16:19:28 +02:00
// Create microrl object and pointer on it
static microrl_t rl;
static microrl_t *prl = &rl;
2016-11-07 02:30:33 +02:00
2016-12-18 15:31:17 +02:00
2016-11-07 02:30:33 +02:00
static inline void init_system_clock(void)
{
TCCR5A = 0; // Clear control register A
TCCR5B = 0; // Clear control register B
TCCR5B |= _BV(WGM52) | _BV(CS52); // CTC and fCPU/256
OCR5A = 62549; // 1 s
TIMSK5 |= _BV(OCIE5A); // Output Compare A Match Interrupt Enable
}
2016-11-07 01:42:18 +02:00
2016-12-18 15:31:17 +02:00
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)
{
2016-12-18 22:43:53 +02:00
MFRC522_init();
2016-12-18 15:31:17 +02:00
PCD_Init();
}
2016-12-01 16:19:28 +02:00
2016-11-07 01:42:18 +02:00
static inline void init_hw (void)
{
2016-11-07 01:42:18 +02:00
// IO init
2016-12-18 21:34:59 +02:00
// Set arduino pin 25 as output
2016-11-07 01:42:18 +02:00
LED_INIT;
2016-12-18 22:43:53 +02:00
2016-12-18 21:34:59 +02:00
// Set Arduino pin 23 as output
DOOR_INIT;
2016-11-07 01:42:18 +02:00
2016-11-07 02:30:33 +02:00
// System clock
init_system_clock();
2016-11-07 01:42:18 +02:00
// UART init
uart0_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
uart3_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
stdout = stdin = &uart0_io;
stderr = &uart3_out;
2016-11-07 01:42:18 +02:00
// LCD init
2016-10-10 22:24:33 +03:00
lcd_init();
lcd_clrscr();
2016-12-18 22:43:53 +02:00
2016-12-18 15:31:17 +02:00
// Init RFID-RC522
init_rfid_reader();
2016-10-10 22:24:33 +03:00
2016-11-07 01:42:18 +02:00
// Enable interupts
sei();
}
static inline void start_ui (void)
{
2016-12-01 16:19:28 +02:00
print_version(stderr);
2016-10-10 22:24:33 +03:00
2016-11-07 01:42:18 +02:00
// print student name
2016-10-23 22:11:20 +03:00
fprintf_P(stdout, PSTR(STUD_NAME));
2016-11-07 01:42:18 +02:00
fputc('\n', stdout); // Add a new line to the uart printout
2016-10-23 22:11:20 +03:00
lcd_puts_P(PSTR(STUD_NAME));
2016-11-07 01:42:18 +02:00
}
2016-12-01 16:19:28 +02:00
static inline void start_cli(void)
2016-11-07 01:42:18 +02:00
{
2016-12-01 16:19:28 +02:00
// Call init with ptr to microrl instance and print callback
microrl_init (prl, cli_print);
// Set callback for execute
microrl_set_execute_callback (prl, cli_execute);
2016-11-07 01:42:18 +02:00
}
2016-11-20 00:51:04 +02:00
static inline void heartbeat (void)
{
2016-12-18 15:31:17 +02:00
static uint32_t time_prev;
uint32_t time_cur = time();
2016-11-07 02:30:33 +02:00
if (time_cur <= time_prev) {
return;
}
time_prev = time_cur;
fprintf_P(stderr, PSTR(UPTIME_MSG "\n"), time_cur);
LED_TOGGLE;
}
2016-12-18 22:43:53 +02:00
static inline void handle_door()
{
2016-12-18 21:34:59 +02:00
Uid uid;
card_t card;
uint32_t time_cur = time();
static uint32_t message_start;
static uint32_t door_open_start;
if (PICC_IsNewCardPresent()) {
PICC_ReadCardSerial(&uid);
card.uid_size = uid.size;
memcpy(&card.uid, &uid.uidByte, uid.size);
card.user = NULL;
card_t *found_card = rfid_find_card(&card);
if (found_card) {
lcd_goto(0x40);
lcd_puts(found_card->user);
2016-12-18 22:43:53 +02:00
for (int8_t i = 16 - strlen(found_card->user); i > -1; i--) {
2016-12-18 21:34:59 +02:00
lcd_putc(' ');
}
DOOR_OPEN;
} else {
DOOR_CLOSE;
lcd_goto(0x40);
2016-12-18 22:19:53 +02:00
lcd_puts_P(access_denied_msg);
2016-12-18 22:43:53 +02:00
for (int8_t i = 16 - strlen_P(access_denied_msg); i > -1; i--) {
2016-12-18 21:34:59 +02:00
lcd_putc(' ');
}
}
door_open_start = time_cur;
message_start = time_cur;
}
2016-12-18 22:43:53 +02:00
if ((message_start + 5) < time_cur) {
message_start = time_cur + 5;
2016-12-18 21:34:59 +02:00
lcd_goto(0x40);
2016-12-18 22:43:53 +02:00
for (int8_t i = 16; i > -1; i--) {
2016-12-18 21:34:59 +02:00
lcd_putc(' ');
}
}
2016-12-18 22:43:53 +02:00
if ((door_open_start + 2) < time_cur) {
2016-12-18 21:34:59 +02:00
DOOR_CLOSE;
}
}
2016-12-01 16:19:28 +02:00
2016-11-07 01:42:18 +02:00
int main (void)
{
init_hw();
start_ui();
2016-12-01 16:19:28 +02:00
start_cli();
2016-10-19 22:51:12 +03:00
2016-11-07 01:42:18 +02:00
while (1) {
2016-11-07 02:30:33 +02:00
heartbeat();
2016-12-01 16:19:28 +02:00
// CLI commands are handled in cli_execute()
microrl_insert_char (prl, cli_get_char());
2016-12-18 21:34:59 +02:00
handle_door();
}
}
2016-11-07 02:30:33 +02:00
2016-12-01 16:19:28 +02:00
2016-11-07 02:30:33 +02:00
// System clock
ISR(TIMER5_COMPA_vect)
{
2016-12-18 15:31:17 +02:00
system_time++;
2016-11-07 02:30:33 +02:00
}