From 5f8a6c7de6680ae92ff334e06e43a85fe9a5d4b8 Mon Sep 17 00:00:00 2001
From: Philipp Claves <pclaves@web.de>
Date: Wed, 27 Jun 2012 20:43:28 +0200
Subject: [PATCH] Revert "PortalControl: Implement (un)lock retry, if the door
 sensor state does not change."

Commit included ATMegaCard changes not meant for production.

This reverts commit a30c706807ff74899bce8135a755248614a66e43.
---
 ATMegaCard/ISO7816-card.c      |  23 +-
 ATMegaCard/ISO7816-card.h      |   1 +
 ATMegaCard/Makefile            |   9 +-
 ATMegaCard/common.h            |   3 +-
 ATMegaCard/main.c              |   3 +-
 ATMegaCard/rng.c               |   4 -
 ATMegaCard/software_serial.c   |  50 +-
 ATMegaCard/software_serial.h   |  20 +-
 Control/cardreader_interface.c |  44 +-
 Control/cardreader_interface.h |   5 +-
 Control/door.c                 | 101 ++--
 Control/log.c                  |  24 +-
 Control/log.h                  |  21 +-
 Control/shell/shell.c          |   4 +-
 Main.sch                       | 829 +++++++++++++--------------------
 common/serial.h                |   9 +-
 16 files changed, 441 insertions(+), 709 deletions(-)

diff --git a/ATMegaCard/ISO7816-card.c b/ATMegaCard/ISO7816-card.c
index d3d8c37..d82cf61 100644
--- a/ATMegaCard/ISO7816-card.c
+++ b/ATMegaCard/ISO7816-card.c
@@ -3,16 +3,6 @@
 #include <util/delay.h>
 #include <string.h>
 
-#define BAUD (F_CPU/372)
-#define USART_NAME UART
-#define PARITY EVEN
-#define STOPBITS 2
-#include "../common/serial.h"
-#define UART_recv_init UART_switch_mode_receive
-#define UART_transmit_init UART_switch_mode_transmit
-#define UART_receive_char_timeout UART_read_char_timeout
-#define UART_ERR_TIMEOUT ERR_TIMEOUT
-
 //ATMega Card
 //ATR: 3B8F014156522049534F3738313620302E31B9
 //Query: 00C101FE3E00000267016400C101FE3E00000267016400C101FE3E000002670164
@@ -42,9 +32,20 @@ uint16_t ISO7816_receive_buffer(uint8_t* buffer, uint8_t length);
 uint8_t  ISO7816_transmit_buffer(uint8_t* buffer, uint8_t length);
 uint8_t  ISO7816_transmit_t1_header(ISO7816_T1_Header* t1_header);
 
+void ISO7816_init(void){
+	UART_init();
+}
+/*
+void ISO7816_sendATR(void){
+	UART_transmit_init();
+	for (uint8_t i = 0; i < sizeof(atr); i++){
+		UART_transmit_char(atr[i]);
+	}
+	UART_recv_init();
+}*/
+
 void ISO7816_sendATR(void){
 	uint8_t checksum = 0;
-	UART_init();
 	UART_transmit_init();
 	UART_transmit_char(atr.initial_char);
 	checksum ^= atr.t0; UART_transmit_char(atr.t0);
diff --git a/ATMegaCard/ISO7816-card.h b/ATMegaCard/ISO7816-card.h
index 896ddbf..9728a13 100644
--- a/ATMegaCard/ISO7816-card.h
+++ b/ATMegaCard/ISO7816-card.h
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "software_serial.h"
 #include "../common/ISO7816-common.h"
 
 #define ISO7816_APDU_READ_TIMEOUT 3000 //300ms
diff --git a/ATMegaCard/Makefile b/ATMegaCard/Makefile
index 1cf13f4..8e5e590 100644
--- a/ATMegaCard/Makefile
+++ b/ATMegaCard/Makefile
@@ -31,7 +31,7 @@
 
 # MCU name
 
-MCU = atmega16
+MCU = atmega163
 
 
 
@@ -41,13 +41,6 @@ ifeq ($(MCU), atmega163)
 	HEX_FILE_NAME = MEGA163
 endif
 
-#Fuse settings for ATmega16
-ifeq ($(MCU), atmega16)
-	#4Mhz internal, BOD, Quick Startup
-	FUSE_BITS = -u -U lfuse:w:0x83:m -U hfuse:w:0xd1:m
-	HEX_FILE_NAME = MEGA16
-endif
-
 # Output format. (can be srec, ihex, binary)
 FORMAT = ihex
 
diff --git a/ATMegaCard/common.h b/ATMegaCard/common.h
index 376f927..3c7f217 100644
--- a/ATMegaCard/common.h
+++ b/ATMegaCard/common.h
@@ -1,2 +1 @@
-//#define F_CPU 3579500
-#define F_CPU 4000000
\ No newline at end of file
+#define F_CPU 3579500
\ No newline at end of file
diff --git a/ATMegaCard/main.c b/ATMegaCard/main.c
index 9d5900b..270701f 100644
--- a/ATMegaCard/main.c
+++ b/ATMegaCard/main.c
@@ -1,7 +1,6 @@
 #include "common.h"
 #include <avr/io.h>
 #include <util/delay.h>
-#include <avr/interrupt.h>
 #include <string.h>
 #include "rng.h"
 
@@ -22,6 +21,8 @@ uint16_t main_handle_adpu(ISO7816_APDU_Header* header,
 uint8_t current_application = 0xFF;
 
 int main(void){
+	//DDRB = (1 << 5) | (1 << 7);
+// 	ISO7816_init();
 	ISO7816_set_apdu_handler(main_handle_adpu);
 	ISO7816_sendATR();
 	sei();
diff --git a/ATMegaCard/rng.c b/ATMegaCard/rng.c
index d8a24f8..de84364 100644
--- a/ATMegaCard/rng.c
+++ b/ATMegaCard/rng.c
@@ -3,10 +3,6 @@
 
 #define ADC_RNG_CHANNEL 0
 
-#ifndef ADCSR
-	#define ADCSR ADCSRA
-#endif
-
 uint16_t seed;
 
 uint8_t rnd_adc_read(void);
diff --git a/ATMegaCard/software_serial.c b/ATMegaCard/software_serial.c
index 934a923..bd9fb39 100644
--- a/ATMegaCard/software_serial.c
+++ b/ATMegaCard/software_serial.c
@@ -11,23 +11,21 @@
 #endif
 
 
-void SoftUART_recv_init(void);
-	
 /**
  * Initializes the Software UART
  */
-void SoftUART_init(void){
+void UART_init(void){
 	#ifdef UART_USE_AS_STDOUT
 		stdout = &UART_stdout; //assign serial output stream as stdout.
 	#endif
 	
-	SoftUART_recv_init();
+	UART_recv_init();
 }
 
 /**
  * Switches the UART to transmit mode
  */
-void SoftUART_transmit_init(void){
+void UART_transmit_init(void){
 	cli(); //interrupts disable (messes up send timings otherwise)
 	SWSERIAL_PORT |= (1 << SWSERIAL_LINE); //data line to high level (otherwise we might trigger a start bit)
 	SWSERIAL_PORT_DR |= (1 << SWSERIAL_LINE); //output
@@ -37,9 +35,9 @@ void SoftUART_transmit_init(void){
  * Sends a 0-terminated string
  * Note: The caller must to switch to transmit mode before calling this
  */
-void SoftUART_transmit_buffer_sz(char* buffer){
+void UART_transmit_buffer_sz(char* buffer){
 	while (buffer[0] != 0){
-		SoftUART_transmit_char(buffer[0]);
+		UART_transmit_char(buffer[0]);
 		buffer++;
 	}
 }
@@ -48,13 +46,13 @@ void SoftUART_transmit_buffer_sz(char* buffer){
  * Sends a byte coverted to hex digits
  * Note: The caller must to switch to transmit mode before calling this
  */
-void SoftUART_transmit_byte_hex(unsigned char b){
+void UART_transmit_byte_hex(unsigned char b){
 
 	unsigned char n;
 	n = '0' + (b >> 4 & 0xF);
-	SoftUART_transmit_char((n > '9') ? n + 7 : n);
+	UART_transmit_char((n > '9') ? n + 7 : n);
 	n = '0' + (b & 0xF);
-	SoftUART_transmit_char((n > '9') ? n + 7 : n);
+	UART_transmit_char((n > '9') ? n + 7 : n);
 }
 
 
@@ -62,7 +60,7 @@ void SoftUART_transmit_byte_hex(unsigned char b){
  * Writes a single character to the data line, bit-by-bit
  * Note: The caller must to switch to transmit mode before calling this
  */
-void SoftUART_transmit_char(unsigned char x){
+void UART_transmit_char(unsigned char x){
 
 	_delay_us(BIT_LENGTH); //guard time beween bytes
 	
@@ -113,7 +111,7 @@ volatile uint8_t recv_valid = 0;    //Did we receive 1 for the stop bits and val
 /**
  * Switch UART into receive mode
  */
-void SoftUART_recv_init(void){
+void UART_recv_init(void){
 	SWSERIAL_PORT_DR &= ~(1 << SWSERIAL_LINE); //set Serial pin to input
 	SWSERIAL_PORT |= (1 << SWSERIAL_LINE); //pullup
 	
@@ -122,15 +120,15 @@ void SoftUART_recv_init(void){
 	TIFR = (1 << ICF1); //clear old interrupt flag.
 	TIMSK = 1 << TICIE1; //enable ICP interrupt, disable timer compare interrupt
 	TCCR1B =  (1 << CS10)   //Timer prescaler = 1
-#ifdef CTC1
+// #ifdef CTC1
 			| (1 << CTC1)   //Clear timer on compare match (important for exact timing, don't do it in software)
-#else
-			| (1 << WGM12)
-#endif
+// #else
+// 			| (1 << WGM12)
+// #endif
 			| (1 << ICNC1) //Capture interrupt noise canceler enable (filters out very short spikes  < 4 clks)
-#ifdef ICES1
-			| (1 << ICES1)
-#endif
+// #ifdef ICES1
+// 			| (1 << ICES1)
+// #endif
 			;
 	
 	OCR1A  = 372; //Default smartcard conversion factor 3571200Hz/372(clks/bit) = 9600bit/s
@@ -141,30 +139,30 @@ void SoftUART_recv_init(void){
 /**
  * Is a byte available to be processed?
  */
-inline uint8_t SoftUART_data_available(void){
+inline uint8_t UART_data_available(void){
 	return recv_avail != 0;
 }
 /**
  * Reads the last byte that was received.
  * Resets the availability flag.
  */
-uint8_t SoftUART_read_char(void){
+uint8_t UART_read_char(void){
 	recv_avail = 0;
 	return recv_buffer2;
 }
 
-uint16_t SoftUART_receive_char_timeout(uint16_t timeout){
+uint16_t UART_receive_char_timeout(uint16_t timeout){
 	while ((timeout > 0) && (recv_avail == 0)) {_delay_us(100); timeout--;}
 	if (recv_avail == 0) return UART_ERR_TIMEOUT;
 	
-	return SoftUART_read_char();
+	return UART_read_char();
 }
 
 /**
  * Enables the timer (interrupt) that samples the data line to receive the bits.
  * Disables the Capture (ICP) interrupt that triggered on the start bit.
  */
-void SoftUART_start_read(void){
+void UART_start_read(void){
 	recv_bits = 0;
 	
 	#if SWSERIAL_PARITY == EVEN
@@ -185,7 +183,7 @@ void SoftUART_start_read(void){
  */
 ISR(TIMER1_CAPT_vect){
 	PORTB |= (1 << 5); //Debug
-	SoftUART_start_read();
+	UART_start_read();
 	PORTB &= ~(1 << 5); //Debug
 }
 
@@ -217,7 +215,7 @@ ISR(TIMER1_COMPA_vect){
 			recv_avail = 1;
 		}
 		recv_bits = 0;
-		SoftUART_recv_init();
+		UART_recv_init();
 	}
 	PORTB &= ~(1 << 7); //Debug
 }
diff --git a/ATMegaCard/software_serial.h b/ATMegaCard/software_serial.h
index ea81610..e7a423a 100644
--- a/ATMegaCard/software_serial.h
+++ b/ATMegaCard/software_serial.h
@@ -41,18 +41,18 @@
 
 #define INST_LENGTH(count) ((1000000*count)/F_CPU)  //(µSec) Duration of count CPU Instructions
 
-void SoftUART_transmit_char(unsigned char data);
+void UART_transmit_char(unsigned char data);
 
-void SoftUARTtransmit_buffer_sz(char* buffer);
-void SoftUARTtransmit_byte_hex(unsigned char b);
+void UART_transmit_buffer_sz(char* buffer);
+void UART_transmit_byte_hex(unsigned char b);
 
-void SoftUARTinit(void);
+void UART_init(void);
 
-void SoftUARTtransmit_init(void);
-void SoftUARTrecv_init(void);
+void UART_transmit_init(void);
+void UART_recv_init(void);
 
-void SoftUARTtransmit_char(unsigned char x);
+void UART_transmit_char(unsigned char x);
 
-uint8_t SoftUARTdata_available(void);
-uint8_t SoftUARTread_char(void);
-uint16_t SoftUARTreceive_char_timeout(uint16_t timeout);
\ No newline at end of file
+uint8_t UART_data_available(void);
+uint8_t UART_read_char(void);
+uint16_t UART_receive_char_timeout(uint16_t timeout);
\ No newline at end of file
diff --git a/Control/cardreader_interface.c b/Control/cardreader_interface.c
index 423063f..242b0c5 100644
--- a/Control/cardreader_interface.c
+++ b/Control/cardreader_interface.c
@@ -32,7 +32,8 @@
  *    Card -> Host:   other ISO7816 status code (-> abort)
 **/
 
-void cardreader_process_message(void);
+
+void unlock(void);
 uint8_t check_card_status(uint8_t* msg_buffer, uint8_t length, uint8_t expected_body_length);
 void cardreader_select_auth_applet(void);
 uint8_t cardreader_select_auth_applet_result(uint8_t* msg_buffer, uint8_t length);
@@ -58,11 +59,11 @@ uint8_t cardreader_sys_get_card_status(void);
 static uint8_t cardreader_power_state = CARDREADER_POWER_STATE_OFF;
 static volatile uint8_t cardreader_startup_event = 0;
 
-#define AUTH_STATE_NONE 1                   //System idle
-#define AUTH_STATE_CARD_INSERTED 2          //Card has been inserted into th reader, auth will be started.
-#define AUTH_STATE_SELECTING_AUTH_APPLET 3  //APDU to select the portal auth application has been sent.
-#define AUTH_STATE_AUTH_REQUESTED_SENT 4    //Authorisation challange has been sent.
-#define AUTH_STATE_AUTH_VERYFIED 5          //Authorisation key has been verified, new key has been sent.
+#define AUTH_STATE_NONE 1
+#define AUTH_STATE_CARD_INSERTED 2
+#define AUTH_STATE_SELECTING_AUTH_APPLET 3
+#define AUTH_STATE_AUTH_REQUESTED_SENT 4
+#define AUTH_STATE_AUTH_VERYFIED 5
 
 volatile uint8_t auth_state = AUTH_STATE_NONE;
 volatile uint16_t state_timeout;
@@ -144,10 +145,8 @@ void cardreader_process(void){
 		return;
 	}
 	
-	if (transport_data_available()) cardreader_process_message();
-}
-
-void cardreader_process_message(void){
+	if (!transport_data_available()) return;
+	
 	uint8_t msg_buffer[100];
 	
 	uint16_t ret = transport_receive_message(msg_buffer, 100, 400);
@@ -338,13 +337,11 @@ uint8_t cardreader_update_key_result(uint8_t* msg_buffer, uint8_t length){
 // 	cardreader_display_show_result(1);
 	
 	cardreader_display_clear();
-// 	if (
-	toggle_lock_unlock(cardreader_display_write_sz_P);
-// 		){
+	if (toggle_lock_unlock(cardreader_display_write_sz_P)){
 // 		cardreader_display_write_sz_P(PSTR("Auth Successful"));
-// 		cardreader_display_move(0,1);
-// 		cardreader_display_write_sz_P(PSTR("Remove Card"));
-// 	}
+		cardreader_display_move(0,1);
+		cardreader_display_write_sz_P(PSTR("Remove Card"));
+	}
 	
 	keystore_update_salt();
 	
@@ -451,6 +448,7 @@ void cardreader_display_show_result(uint8_t success){
 	}
 }
 
+
 void cardreader_display_set_backlight(uint8_t on){
 	cardreader_display_message_t msg = {CARDREADER_MSG_TYPE_DISPALY};
 	msg.command = CARDREADER_DISPLAY_MSG_SET_BACKLIGHT;
@@ -459,18 +457,4 @@ void cardreader_display_set_backlight(uint8_t on){
 	
 	uint8_t success;
 	transport_receive_message(&success, 1, 3000);
-}
-
-void cardreader_display_show_door_retry(void){
-	cardreader_display_move(10,0);
-	cardreader_display_write_sz_P(PSTR("Retry"));
-}
-
-void cardreader_display_show_door_result(uint8_t success){
-	cardreader_display_move(0,1);
-	if (success) {
-		cardreader_display_write_sz_P(PSTR("OK, remove card "));
-	} else {
-		cardreader_display_write_sz_P(PSTR("Mechanical Fail!"));
-	}
 }
\ No newline at end of file
diff --git a/Control/cardreader_interface.h b/Control/cardreader_interface.h
index eaf9e16..8843bdf 100644
--- a/Control/cardreader_interface.h
+++ b/Control/cardreader_interface.h
@@ -23,7 +23,4 @@ void cardreader_process(void);
 void cardreader_tick(void);
 
 uint8_t cardreader_init_card_key(KEY key);
-uint8_t cardreader_clear_card_key(void);
-
-void cardreader_display_show_door_retry(void);
-void cardreader_display_show_door_result(uint8_t success);
\ No newline at end of file
+uint8_t cardreader_clear_card_key(void);
\ No newline at end of file
diff --git a/Control/door.c b/Control/door.c
index 664eaf1..a309e06 100644
--- a/Control/door.c
+++ b/Control/door.c
@@ -1,6 +1,5 @@
 #include "door.h"
 #include "log.h"
-#include "cardreader_interface.h" //for (un)lock messages
 
 uint8_t door_read_pin_status(void);
 uint8_t door_update_status(uint8_t sensor_status);
@@ -21,21 +20,17 @@ const char* const status_names[] PROGMEM = {status_unlocked,
                                             status_unlocking,
                                             status_locking};
 
-uint8_t door_status;
+volatile uint8_t door_status;
 
-uint8_t sensor_candidate_status;
-uint8_t sensor_stability;
+volatile uint8_t sensor_candidate_status;
+volatile uint8_t sensor_stability;
 #define DOOR_PIN_NEEDED_STABILITY 4
 
-#define DOOR_COMMAND_PULSE_TIME 45 //450ms
-uint8_t door_command_pulse_time = 0;
-
-#define DOOR_COMMAND_WAIT_TIMEOUT 900 //9s
-uint16_t door_command_wait_timeout = 0;
-
-uint8_t door_command_retry_count = 0;
-#define DOOR_COMMAND_MAX_RETRY 1
+#define DOOR_COMMAND_PULSE_TIME 35 //350ms
+volatile uint8_t door_command_pulse_time = 0;
 
+#define DOOR_COMMAND_WAIT_TIMEOUT 1000 //10s
+volatile uint16_t door_command_wait_timeout = 0;
 
 void door_init(void){
 	DOOR_PORT &= ~(COMMAND_UNLOCK_PIN | COMMAND_LOCK_PIN); //commnd pins off
@@ -57,9 +52,7 @@ void door_init(void){
 void door_tick(void){
 	if (door_command_pulse_time != 0) {
 		door_command_pulse_time--;
-		if (door_command_pulse_time == 0) {
-			DOOR_PORT &= (uint8_t)~(COMMAND_LOCK_PIN|COMMAND_UNLOCK_PIN);
-		}
+		if (door_command_pulse_time == 0) DOOR_PORT &= (uint8_t)~(COMMAND_LOCK_PIN|COMMAND_UNLOCK_PIN);
 	}
 	
 	if (   ((door_status & DOOR_STATUS_UNLOCKING) && !(door_status & DOOR_STATUS_UNLOCKED))
@@ -67,30 +60,12 @@ void door_tick(void){
 		door_command_wait_timeout--;
 		if (door_command_wait_timeout == 0){
 			
+			printf_P(PSTR("Error: Door command timeout\n"));
 			log_append(LOG_EVENT_DOOR_COMMAND_TIMEOUT, 0);
 			
-			printf_P(PSTR("Error: Door command timeout!"));
+			//TODO: Retry (and turn keymatic power on if currently off)
 			
-			if (door_command_retry_count < DOOR_COMMAND_MAX_RETRY){
-				printf_P(PSTR(" Retry...\n"));
-				cardreader_display_show_door_retry();
-				uint8_t retry = door_command_retry_count;
-				
-				//TODO: turn keymatic power on if currently off?
-				if (door_status & DOOR_STATUS_UNLOCKING) unlock();
-				if (door_status & DOOR_STATUS_LOCKING  ) lock();
-				//lock/unlock resets the counter, restore it.
-				door_command_retry_count = retry + 1;
-				
-			} else {
-				
-				log_append(LOG_EVENT_DOOR_COMMAND_RETRY_FAIL, 0);
-				
-				printf_P(" Giving Up :((\n");
-				cardreader_display_show_door_result(0);
-				
-				door_status &= (uint8_t)~(DOOR_STATUS_UNLOCKING | DOOR_STATUS_LOCKING); //clear activity flags
-			}
+			door_status &= (uint8_t)~(DOOR_STATUS_UNLOCKING | DOOR_STATUS_LOCKING); //clear activity flags
 		}
 	}
 	
@@ -120,15 +95,8 @@ uint8_t door_read_pin_status(void){
 uint8_t door_update_status(uint8_t sensor_status){
 	uint8_t new_door_status = (sensor_status & DOOR_STATUS_HW_MASK) | (door_status & DOOR_STATUS_SW_MASK);
 
-	if ((new_door_status & DOOR_STATUS_LOCKED) && (door_status & DOOR_STATUS_LOCKING)){
-		new_door_status &= (uint8_t)~DOOR_STATUS_LOCKING;
-		cardreader_display_show_door_result(1);
-	}
-		
-	if ((new_door_status & DOOR_STATUS_UNLOCKED) && (door_status & DOOR_STATUS_UNLOCKING)){
-		new_door_status &= (uint8_t)~DOOR_STATUS_UNLOCKING;
-		cardreader_display_show_door_result(1);
-	}
+	if (new_door_status & DOOR_STATUS_LOCKED)   new_door_status &= (uint8_t)~DOOR_STATUS_LOCKING;
+	if (new_door_status & DOOR_STATUS_UNLOCKED) new_door_status &= (uint8_t)~DOOR_STATUS_UNLOCKING;
 	
 	if (is_alarm_status(new_door_status)) new_door_status |= DOOR_STATUS_ALARM;
 	
@@ -146,7 +114,7 @@ uint8_t is_alarm_status(uint8_t door_status){
 	
 	//NOTE: This condition is supposed to trigger, if the door is slammed open.
 	//      We can't use the LOCKED sensor, because the connection between lock bolt and frame will be interrupted by this.
-	//      This connection is what the sensor actually detects. TODO: Can we improve this?
+	//      This connection is what the sensor actually detects. TODO: Can we improove this?
 	//      The UNLOCKED sensor, however will be uneffected, leaving the lock sensors in an
 	//      undefined (neither locked nor unlocked) state. We trigger an alarm if this occurs when the door is open.
 	//      Unfortunately the UNLOCKED has shown failures in the past, possibly causing a false alarm.
@@ -168,7 +136,6 @@ uint8_t door_get_status(void){
 void unlock(void){
 	door_command_pulse_time = DOOR_COMMAND_PULSE_TIME;
 	door_command_wait_timeout = DOOR_COMMAND_WAIT_TIMEOUT;
-	door_command_retry_count = 0;
 	
 	door_status = (door_status & (uint8_t)~DOOR_STATUS_LOCKING) | DOOR_STATUS_UNLOCKING;
 	DOOR_PORT   = (DOOR_PORT   & (uint8_t)~COMMAND_LOCK_PIN)    | COMMAND_UNLOCK_PIN;
@@ -177,9 +144,6 @@ void unlock(void){
 void lock(void){
 	door_command_pulse_time = DOOR_COMMAND_PULSE_TIME;
 	door_command_wait_timeout = DOOR_COMMAND_WAIT_TIMEOUT;
-	door_command_retry_count = 0;
-	
-// 	printf_P(PSTR("."));
 	
 	door_status = (door_status & (uint8_t)~DOOR_STATUS_UNLOCKING) | DOOR_STATUS_LOCKING;
 	DOOR_PORT   = (DOOR_PORT   & (uint8_t)~COMMAND_UNLOCK_PIN)    | COMMAND_LOCK_PIN;
@@ -194,47 +158,34 @@ uint8_t toggle_lock_unlock( void (*msg_target)(const char*) ){
 }
 
 uint8_t unlock_checked( void (*msg_target)(const char*) ){
-	uint8_t ok = 0;
-	const char* msg;
-	
 	if (door_status & DOOR_STATUS_UNLOCKED){
-		msg = PSTR("Already unlocked\n");
+		msg_target(PSTR("Already unlocked\n"));
 	} else if (door_status & DOOR_STATUS_LOCKING){
-		msg = PSTR("Can't unlock while locking\n");
+		msg_target(PSTR("Can't unlock while locking\n"));
 	} else {
 		unlock();
-		msg = PSTR("Unlocking...\n");
-		ok = 1;
+		msg_target(PSTR("Unlocking...\n"));
+		return 1;
 	}
-	
-	printf_P(msg);
-	if (msg_target) msg_target(msg);
-	
-	return ok;
+	return 0;
 }
 
 
 uint8_t lock_checked( void (*msg_target)(const char*) ){
-	uint8_t ok = 0;
-	const char* msg;
 	if (is_alarm_status(door_status)){
-		msg = PSTR("Can't lock in alarm state\n");
+		msg_target(PSTR("Can't lock in alarm state\n"));
 	} else if (door_status & DOOR_STATUS_UNLOCKING) {
-		msg = PSTR("Can't lock while unlocking\n");
+		msg_target(PSTR("Can't lock while unlocking\n"));
 	} else if (door_status & DOOR_STATUS_OPEN){
-		msg = PSTR("Can't lock while door open\n");
+		msg_target(PSTR("Can't lock while door open\n"));
 	} else if (door_status & DOOR_STATUS_LOCKED){
-		msg = PSTR("Already locked\n");
+		msg_target(PSTR("Already locked\n"));
 	} else {
 		lock();
-		msg = PSTR("Locking...\n");
-		ok = 1;
+		msg_target(PSTR("Locking...\n"));
+		return 1;
 	}
-	
-	printf_P(msg);
-	if (msg_target) msg_target(msg);
-	
-	return ok;
+	return 0;
 }
 
 uint8_t door_clear_alarm(void){
diff --git a/Control/log.c b/Control/log.c
index 2158ae3..c2ce307 100644
--- a/Control/log.c
+++ b/Control/log.c
@@ -5,17 +5,16 @@
 #include "door.h"
 #include "power_monitor.h"
 
-const char log_event_startup[]                 PROGMEM = "== System Started ==";
-const char log_event_alarm_raised[]            PROGMEM = "Alarm Raised";
-const char log_event_alarm_changed[]           PROGMEM = "State change during Alarm";
-const char log_event_alarm_login[]             PROGMEM = "Login during Alarm";
-const char log_event_alarm_disabled[]          PROGMEM = "Alarm disabled";
-const char log_event_ac_fail[]                 PROGMEM = "AC Power Fail";
-const char log_event_ac_return[]               PROGMEM = "AC Power Restored";
-const char log_event_main_battery_low[]        PROGMEM = "Main Battery Low";
-const char log_event_keymatic_battery_low[]    PROGMEM = "Keymatic Battery Low";
-const char log_event_door_command_timeout[]    PROGMEM = "Door command Timeout";
-const char log_event_door_command_retry_fail[] PROGMEM = "Door command retry failed";
+const char log_event_startup[]              PROGMEM = "== System Started ==";
+const char log_event_alarm_raised[]         PROGMEM = "Alarm Raised";
+const char log_event_alarm_changed[]        PROGMEM = "State change during Alarm";
+const char log_event_alarm_login[]          PROGMEM = "Login during Alarm";
+const char log_event_alarm_disabled[]       PROGMEM = "Alarm disabled";
+const char log_event_ac_fail[]              PROGMEM = "AC Power Fail";
+const char log_event_ac_return[]            PROGMEM = "AC Power Restored";
+const char log_event_main_battery_low[]     PROGMEM = "Main Battery Low";
+const char log_event_keymatic_battery_low[] PROGMEM = "Keymatic Battery Low";
+const char log_event_door_command_timeout[] PROGMEM = "Door command Timeout";
 
 const char* const event_names[] PROGMEM = {log_event_startup,
                                log_event_alarm_raised,
@@ -26,8 +25,7 @@ const char* const event_names[] PROGMEM = {log_event_startup,
                                log_event_ac_return,
                                log_event_main_battery_low,
                                log_event_keymatic_battery_low,
-                               log_event_door_command_timeout,
-                               log_event_door_command_retry_fail};
+                               log_event_door_command_timeout};
 
 uint8_t log_get_first_entry_index(void){
 	uint8_t l = eeprom_read_byte(LOG_START_POINTER_LOCATION);
diff --git a/Control/log.h b/Control/log.h
index 67825eb..111d446 100644
--- a/Control/log.h
+++ b/Control/log.h
@@ -20,17 +20,16 @@ struct log_entry_t{
 #define LOG_START_POINTER_LOCATION ((void*)(EEPROM_SIZE-1))
 #define LOG_END_POINTER_LOCATION   ((void*)(EEPROM_SIZE-2))
 
-#define LOG_EVENT_STARTUP                 0x00
-#define LOG_EVENT_ALARM_RAISED            0x01
-#define LOG_EVENT_ALARM_CHANGED           0x02
-#define LOG_EVENT_ALARM_LOGIN             0x03
-#define LOG_EVENT_ALARM_DISABLED          0x04
-#define LOG_EVENT_AC_FAIL                 0x05
-#define LOG_EVENT_AC_RETURN               0x06
-#define LOG_EVENT_MAIN_BATTERY_LOW        0x07
-#define LOG_EVENT_KEYMATIC_BATTERY_LOW    0x08
-#define LOG_EVENT_DOOR_COMMAND_TIMEOUT    0x09
-#define LOG_EVENT_DOOR_COMMAND_RETRY_FAIL 0x0A
+#define LOG_EVENT_STARTUP              0x00
+#define LOG_EVENT_ALARM_RAISED         0x01
+#define LOG_EVENT_ALARM_CHANGED        0x02
+#define LOG_EVENT_ALARM_LOGIN          0x03
+#define LOG_EVENT_ALARM_DISABLED       0x04
+#define LOG_EVENT_AC_FAIL              0x05
+#define LOG_EVENT_AC_RETURN            0x06
+#define LOG_EVENT_MAIN_BATTERY_LOW     0x07
+#define LOG_EVENT_KEYMATIC_BATTERY_LOW 0x08
+#define LOG_EVENT_DOOR_COMMAND_TIMEOUT 0x09
 
 #define LOG_EVENT_NONE                 0xFF
 
diff --git a/Control/shell/shell.c b/Control/shell/shell.c
index 158f354..15f840b 100644
--- a/Control/shell/shell.c
+++ b/Control/shell/shell.c
@@ -302,14 +302,14 @@ void cmd_lock(readline_parsed_cmd_t* cmd){
 	if ((cmd->num_args > 0) && (strcmp(cmd->args[0], "-f") == 0)){
 		lock();
 	} else {
-		lock_checked(0);
+		lock_checked(print_P);
 	}
 }
 void cmd_unlock(readline_parsed_cmd_t* cmd){
 	if ((cmd->num_args > 0) && (strcmp(cmd->args[0], "-f") == 0)){
 		unlock();
 	} else {
-		unlock_checked(0);
+		unlock_checked(print_P);
 	}
 }
 
diff --git a/Main.sch b/Main.sch
index ba90b21..1b57a6e 100644
--- a/Main.sch
+++ b/Main.sch
@@ -1,4 +1,4 @@
-EESchema Schematic File Version 2  date Do 15 Mär 2012 14:16:25 CET
+EESchema Schematic File Version 2  date Mi 04 Mai 2011 00:11:36 CEST
 LIBS:power
 LIBS:device
 LIBS:transistors
@@ -35,7 +35,7 @@ EELAYER END
 $Descr A4 11700 8267
 Sheet 1 1
 Title ""
-Date "15 mar 2012"
+Date "3 may 2011"
 Rev ""
 Comp ""
 Comment1 ""
@@ -43,710 +43,521 @@ Comment2 ""
 Comment3 ""
 Comment4 ""
 $EndDescr
+Connection ~ 5350 5650
 Wire Wire Line
-	6600 3300 6600 3100
-Wire Wire Line
-	6600 3100 6600 3000
-Wire Wire Line
-	8100 5750 8000 5750
-Wire Wire Line
-	8000 5750 7900 5750
-Connection ~ 7900 5150
-Wire Wire Line
-	7900 5250 7900 5150
-Connection ~ 2550 1400
-Wire Wire Line
-	2550 1150 2550 1400
-Wire Wire Line
-	2550 1400 2550 1500
-Connection ~ 3150 1400
-Wire Wire Line
-	2950 1400 3150 1400
-Connection ~ 3250 1400
-Wire Wire Line
-	3450 1400 3250 1400
-Wire Wire Line
-	3350 1150 3450 1150
-Wire Wire Line
-	3350 1150 3350 1500
-Connection ~ 3150 5500
-Wire Wire Line
-	3000 5550 3000 5500
-Connection ~ 5250 5550
-Wire Wire Line
-	5250 5350 5250 5550
-Wire Wire Line
-	1850 5200 1850 5500
-Wire Wire Line
-	1850 5500 1750 5500
-Wire Wire Line
-	2400 6150 2050 6150
-Wire Wire Line
-	2050 6150 2050 5300
-Wire Wire Line
-	2050 5300 1750 5300
-Wire Wire Line
-	5800 6950 5800 6900
-Wire Wire Line
-	5800 6900 5650 6900
-Connection ~ 4550 7400
-Wire Wire Line
-	4550 7300 4550 7400
-Wire Wire Line
-	4550 7400 4550 7500
-Wire Wire Line
-	5700 7700 5700 7700
-Wire Wire Line
-	4200 4900 4500 4900
-Wire Wire Line
-	4500 4900 4500 6150
-Wire Wire Line
-	4500 6150 4400 6150
-Wire Wire Line
-	4400 6150 4400 6250
-Wire Wire Line
-	4400 6250 4000 6250
-Connection ~ 8350 2400
-Wire Wire Line
-	8350 2400 8350 2250
-Wire Wire Line
-	8350 2250 8250 2250
-Wire Wire Line
-	8250 2250 8250 2300
-Wire Wire Line
-	8050 2400 8350 2400
-Wire Wire Line
-	8350 2400 8450 2400
-Wire Wire Line
-	4700 1900 5000 1900
-Connection ~ 6200 2900
-Wire Wire Line
-	5450 3400 5300 3400
-Wire Wire Line
-	5300 3400 5300 3500
-Wire Wire Line
-	5300 3500 4200 3500
-Wire Wire Line
-	4200 3500 4200 3400
-Wire Wire Line
-	6050 5650 6050 6450
-Wire Wire Line
-	6050 6450 6050 6550
-Wire Wire Line
-	6050 6550 6050 7500
-Wire Wire Line
-	5850 6350 5850 5850
-Wire Wire Line
-	5850 5850 4800 5850
-Wire Wire Line
-	4800 5850 4800 4600
-Wire Wire Line
-	4800 4600 4200 4600
-Connection ~ 6150 6350
-Wire Wire Line
-	6650 6350 6150 6350
-Wire Wire Line
-	4200 2200 6300 2200
-Wire Wire Line
-	6300 2200 6300 1350
-Wire Wire Line
-	2250 5800 2250 5950
-Wire Wire Line
-	2250 5800 3350 5800
-Wire Wire Line
-	2200 2350 2200 2600
+	5350 5350 5350 5650
 Wire Wire Line
-	8050 6650 8050 6550
+	3350 5650 2250 5650
 Wire Wire Line
-	8050 6550 7350 6550
+	2250 5650 2250 5000
 Wire Wire Line
-	7200 6200 7200 6100
+	2250 5000 2150 5000
 Wire Wire Line
-	7200 6100 7350 6100
+	550  5450 550  5400
 Wire Wire Line
-	7350 6100 7350 6200
+	550  5400 650  5400
 Wire Wire Line
-	7350 6200 7350 6350
+	650  5400 650  5500
 Wire Wire Line
-	8050 2000 8450 2000
+	650  5500 950  5500
 Wire Wire Line
-	8200 4550 7950 4550
+	950  5400 750  5400
 Wire Wire Line
-	7950 4550 7950 4250
+	750  5400 750  6350
 Wire Wire Line
-	5400 2600 4400 2600
+	750  6350 2400 6350
 Wire Wire Line
-	5400 2900 5200 2900
-Connection ~ 7400 3100
+	5650 6000 5650 5950
 Wire Wire Line
-	7250 3100 7400 3100
-Wire Wire Line
-	4200 2400 7050 2400
-Connection ~ 6800 2500
-Wire Wire Line
-	6800 2500 6800 2600
-Wire Wire Line
-	7050 3100 6600 3100
-Wire Wire Line
-	6600 3100 6500 3100
-Wire Wire Line
-	6500 3100 6350 3100
-Wire Wire Line
-	6350 3100 6350 3250
+	5650 5950 5550 5950
+Connection ~ 4450 6450
 Wire Wire Line
-	8450 2600 7400 2600
+	4450 6250 4450 6550
+Connection ~ 8450 2000
 Wire Wire Line
-	5300 2700 5300 2800
+	8450 2300 8450 1950
+Connection ~ 6200 2800
 Wire Wire Line
-	5300 2700 5200 2700
+	6200 2700 6200 3000
+Connection ~ 5200 3100
 Wire Wire Line
-	5100 2900 5100 3000
+	5450 3200 5450 3100
 Wire Wire Line
-	5100 2900 4500 2900
+	5450 3100 5000 3100
 Wire Wire Line
-	5650 4200 5650 4350
+	6150 7600 6150 5550
 Wire Wire Line
-	5650 4350 5650 4650
+	5550 5400 5550 5350
 Wire Wire Line
-	3250 1500 3250 1400
+	6400 1350 6400 2100
 Wire Wire Line
-	3250 1400 3250 1250
+	6400 2100 4200 2100
+Connection ~ 7550 6200
+Connection ~ 7350 6200
 Wire Wire Line
-	4700 2500 6600 2500
+	7700 6200 7350 6200
+Connection ~ 6050 6450
 Wire Wire Line
-	6600 2500 6800 2500
+	6050 6450 6650 6450
 Wire Wire Line
-	6800 2500 8450 2500
+	6200 1350 6100 1350
 Wire Wire Line
-	2400 6850 2000 6850
+	6100 1350 6100 1450
 Wire Wire Line
-	2000 6850 1850 6850
+	3250 5500 3000 5500
+Connection ~ 3150 5500
 Wire Wire Line
-	1850 6850 1700 6850
-Connection ~ 7250 5800
+	1900 550  3250 550 
+Connection ~ 2200 550 
 Wire Wire Line
-	7500 5800 7250 5800
+	6650 7050 6650 7150
 Wire Wire Line
-	7250 5800 7050 5800
+	7350 6650 7450 6650
 Wire Wire Line
-	6950 5700 6950 6000
-Connection ~ 6950 4950
+	8150 6200 8150 6450
 Wire Wire Line
-	6750 4950 6950 4950
+	8150 6450 7350 6450
 Wire Wire Line
-	6950 4950 7150 4950
+	6650 6650 6600 6650
+Connection ~ 7800 4500
 Wire Wire Line
-	6750 4950 6750 5050
+	7800 4500 7850 4500
 Wire Wire Line
-	7050 5000 7050 4850
+	7850 4500 7850 4550
 Wire Wire Line
-	7050 4850 6400 4850
+	7800 4250 7800 4650
 Wire Wire Line
-	6550 4400 6550 4550
+	7800 4650 8200 4650
 Wire Wire Line
-	3150 1500 3150 1400
+	7800 2300 7800 3750
 Wire Wire Line
-	3150 1400 3150 1150
-Connection ~ 5100 4200
+	7800 2300 4200 2300
 Wire Wire Line
-	5150 4200 5100 4200
+	5400 2800 5300 2800
 Wire Wire Line
-	5100 4200 4200 4200
-Connection ~ 1850 6850
-Connection ~ 2000 6850
-Connection ~ 1850 7450
+	5400 3000 5100 3000
 Wire Wire Line
-	7400 2600 7400 3100
+	7250 2600 7050 2600
 Wire Wire Line
-	7400 3100 7400 3750
+	7050 2600 7050 2400
+Connection ~ 6600 2500
 Wire Wire Line
-	7400 3750 7400 4750
-Connection ~ 6150 6650
+	6600 2600 6600 2500
+Connection ~ 6500 3100
 Wire Wire Line
-	6150 6650 5850 6650
-Connection ~ 6050 6550
+	6500 3300 6500 3000
 Wire Wire Line
-	5850 6550 6050 6550
+	6500 3000 6200 3000
 Wire Wire Line
-	5950 7300 5950 5750
+	4400 2900 4400 2600
 Wire Wire Line
-	5950 5750 4900 5750
+	4400 2900 4200 2900
 Wire Wire Line
-	4900 5750 4900 4500
+	5200 2800 5200 2900
 Wire Wire Line
-	4900 4500 4200 4500
-Connection ~ 4450 6350
-Connection ~ 5000 3200
+	5200 2800 4600 2800
 Wire Wire Line
-	1850 4750 1850 4800
+	5500 4550 5650 4550
 Wire Wire Line
-	4200 4800 4600 4800
+	1800 3000 1600 3000
 Wire Wire Line
-	4600 4800 4600 5650
+	1700 6900 1700 6850
 Wire Wire Line
-	4600 5650 3850 5650
+	7050 5700 7050 5800
 Wire Wire Line
-	1850 6450 1850 6050
+	7250 5700 7250 5800
 Wire Wire Line
-	1700 6150 1950 6150
+	7500 4950 7500 5800
 Wire Wire Line
-	5900 4000 5900 4700
+	7700 6000 6950 6000
+Connection ~ 8200 4950
 Wire Wire Line
-	5900 4000 4200 4000
+	7500 4950 8900 4950
 Wire Wire Line
-	6600 3800 6400 3800
+	6950 5000 6950 4950
 Wire Wire Line
-	6400 3800 4200 3800
+	7150 5000 7150 4950
 Wire Wire Line
-	6600 3600 6100 3600
+	7250 5000 7250 4700
 Wire Wire Line
-	6100 3600 4200 3600
+	7250 4700 6400 4700
+Connection ~ 6600 3100
+Connection ~ 3050 550 
 Wire Wire Line
-	5850 1600 7600 1600
-Connection ~ 8200 5050
+	2200 1000 2200 550 
 Wire Wire Line
-	8900 5050 8200 5050
+	2550 1400 2550 1500
+Connection ~ 3150 1400
 Wire Wire Line
-	8200 5050 8100 5050
+	2950 1400 3150 1400
+Connection ~ 3250 550 
+Connection ~ 5650 4350
+Connection ~ 5000 4350
 Wire Wire Line
-	8100 5050 7600 5050
-Connection ~ 6550 4550
+	5150 4350 5000 4350
 Wire Wire Line
-	6800 4450 6800 4400
+	1850 7450 1850 7250
 Wire Wire Line
-	6800 4400 6700 4400
+	6150 7600 5950 7600
 Wire Wire Line
-	6700 4400 6700 4550
+	6150 5550 5100 5550
 Wire Wire Line
-	6700 4550 6550 4550
+	5100 5550 5100 4200
 Wire Wire Line
-	6550 4550 6400 4550
+	5950 7500 6050 7500
 Wire Wire Line
-	6400 4550 6250 4550
+	5000 5650 6050 5650
 Wire Wire Line
-	6250 4550 6100 4550
+	5000 5650 5000 4300
 Wire Wire Line
-	5900 4700 5900 4700
-Connection ~ 6250 4550
-Connection ~ 6550 3900
+	5000 4300 4200 4300
+Connection ~ 5000 3300
 Wire Wire Line
-	6550 4000 6550 3900
-Connection ~ 6250 3700
+	5000 3000 5000 3400
 Wire Wire Line
-	6250 4000 6250 3700
+	3850 5800 4700 5800
 Wire Wire Line
-	6250 4400 6250 4550
+	4700 5800 4700 4700
 Wire Wire Line
-	7700 5150 7700 6000
+	4700 4700 4200 4700
 Wire Wire Line
-	7700 5150 7900 5150
+	1700 5950 2250 5950
 Wire Wire Line
-	7900 5150 8200 5150
+	1700 6250 2400 6250
 Wire Wire Line
-	7800 4750 8200 4750
-Connection ~ 7400 3750
+	5700 4100 5700 4850
 Wire Wire Line
-	7500 3750 7400 3750
+	5700 4100 4200 4100
 Wire Wire Line
-	7600 4450 7600 4250
+	6500 3900 4200 3900
 Wire Wire Line
-	5650 1300 4700 1300
+	6500 3700 4200 3700
 Wire Wire Line
-	4700 1300 4700 1800
+	6600 3100 6600 3000
 Wire Wire Line
-	5200 3150 5200 3100
-Connection ~ 5000 3100
+	7800 3750 7950 3750
 Wire Wire Line
-	1950 6150 1950 6050
+	5700 4850 5900 4850
+Connection ~ 6300 4550
+Connection ~ 6300 3800
 Wire Wire Line
-	1950 6050 2400 6050
+	6300 4150 6300 3800
+Connection ~ 6000 3600
 Wire Wire Line
-	1850 6050 1700 6050
-Connection ~ 2000 7450
+	6000 4150 6000 3600
 Wire Wire Line
-	1700 7350 1700 7450
+	7600 5900 7600 5050
 Wire Wire Line
-	4000 6350 4300 6350
+	7600 5900 7150 5900
 Wire Wire Line
-	4300 6350 4300 5100
+	7150 5900 7150 5700
 Wire Wire Line
-	4300 5100 4200 5100
+	7500 4250 7600 4250
 Wire Wire Line
-	2100 1800 2200 1800
+	7600 3750 7600 1600
 Wire Wire Line
-	1550 2000 1550 2300
+	5450 1600 5350 1600
 Wire Wire Line
-	1550 2300 1550 2600
+	5350 1600 5350 1650
 Wire Wire Line
-	2200 2200 2200 2000
+	2400 7450 1700 7450
 Wire Wire Line
-	2200 2000 1950 2000
+	2000 7450 2000 7050
 Wire Wire Line
-	2200 1800 2200 1500
+	2000 6850 2000 6550
 Wire Wire Line
-	4600 2700 4600 2800
+	4000 6050 4400 6050
 Wire Wire Line
-	4600 2700 4200 2700
+	4400 6050 4400 5000
 Wire Wire Line
-	4200 2800 4500 2800
+	4400 5000 4200 5000
 Wire Wire Line
-	4500 2800 4500 2900
+	4000 6150 4200 6150
 Wire Wire Line
-	1600 3100 1600 3000
+	4200 6150 4200 5200
 Wire Wire Line
-	2200 2600 1950 2600
+	3000 5500 3000 5550
 Wire Wire Line
-	1550 2300 1350 2300
+	1350 2350 1350 2300
 Connection ~ 1550 2300
 Wire Wire Line
-	1350 2300 1350 2350
-Wire Wire Line
-	4200 5200 4200 6150
-Wire Wire Line
-	4200 6150 4000 6150
-Wire Wire Line
-	4200 5000 4400 5000
+	1350 2300 1550 2300
 Wire Wire Line
-	4400 5000 4400 6050
+	1950 2600 2200 2600
 Wire Wire Line
-	4400 6050 4000 6050
+	1600 3000 1600 3100
 Wire Wire Line
-	2000 6850 2000 6550
-Wire Wire Line
-	2000 7450 2000 7050
-Wire Wire Line
-	1700 7450 1850 7450
-Wire Wire Line
-	1850 7450 2000 7450
-Wire Wire Line
-	2000 7450 2400 7450
-Wire Wire Line
-	5350 1650 5350 1600
-Wire Wire Line
-	5350 1600 5450 1600
-Wire Wire Line
-	7600 1600 7600 3750
-Wire Wire Line
-	7600 4250 7500 4250
-Wire Wire Line
-	7150 5700 7150 5900
+	4500 2800 4500 2900
 Wire Wire Line
-	7150 5900 7600 5900
+	4500 2800 4200 2800
 Wire Wire Line
-	7600 5900 7600 5050
+	4200 2700 4600 2700
 Wire Wire Line
-	6100 4150 6100 3600
-Connection ~ 6100 3600
+	4600 2700 4600 2800
 Wire Wire Line
-	6400 4150 6400 3800
-Connection ~ 6400 3800
-Connection ~ 6400 4550
+	2200 1500 2200 1800
 Wire Wire Line
-	5900 4850 5800 4850
+	1950 2000 2200 2000
 Wire Wire Line
-	7950 3750 7800 3750
+	2200 2000 2200 2200
 Wire Wire Line
-	6600 3700 6250 3700
+	1550 2000 1550 2600
 Wire Wire Line
-	6250 3700 4200 3700
+	2200 1800 2100 1800
 Wire Wire Line
-	6600 3900 6550 3900
+	4200 5100 4300 5100
 Wire Wire Line
-	6550 3900 4200 3900
+	4300 5100 4300 6350
 Wire Wire Line
-	4200 4100 5800 4100
+	4300 6350 4000 6350
 Wire Wire Line
-	5800 4100 5800 4850
+	1700 7450 1700 7350
+Connection ~ 2000 7450
 Wire Wire Line
-	1700 6250 2400 6250
+	1700 6050 1850 6050
 Wire Wire Line
-	2250 5950 1700 5950
+	2400 6050 1950 6050
 Wire Wire Line
-	4200 4700 4700 4700
+	1950 6050 1950 6150
+Connection ~ 5000 3100
 Wire Wire Line
-	4700 4700 4700 5800
+	5200 3150 5200 3100
 Wire Wire Line
-	4700 5800 3850 5800
+	4700 1800 4700 1300
 Wire Wire Line
-	5000 3000 5000 3100
+	4700 1300 5650 1300
 Wire Wire Line
-	5000 3100 5000 3200
+	7600 4250 7600 4450
 Wire Wire Line
-	5000 3200 5000 3300
+	7500 3750 7400 3750
+Connection ~ 7400 3750
 Wire Wire Line
-	5000 3300 5000 3400
-Connection ~ 5000 3300
+	7800 4750 8200 4750
 Wire Wire Line
-	4200 4300 5000 4300
+	8200 5150 7700 5150
 Wire Wire Line
-	5000 4300 5000 4350
+	7700 5150 7700 6000
 Wire Wire Line
-	5000 4350 5000 5650
+	6150 4400 6150 4550
 Wire Wire Line
-	5000 5650 5350 5650
+	6150 4000 6150 3700
+Connection ~ 6150 3700
 Wire Wire Line
-	5350 5650 6050 5650
+	6450 4000 6450 3900
+Connection ~ 6450 3900
+Connection ~ 6150 4550
 Wire Wire Line
-	6050 7500 5950 7500
+	5900 4700 5800 4700
 Wire Wire Line
-	5100 4200 5100 5550
+	6000 4550 6600 4550
 Wire Wire Line
-	5100 5550 5250 5550
+	6600 4550 6600 4400
 Wire Wire Line
-	5250 5550 6150 5550
+	6600 4400 6700 4400
 Wire Wire Line
-	5950 7600 6150 7600
+	6700 4400 6700 4450
+Connection ~ 6450 4550
 Wire Wire Line
-	1850 7450 1850 7250
+	7600 5050 8900 5050
+Connection ~ 8200 5050
 Wire Wire Line
-	5150 4350 5000 4350
-Connection ~ 5000 4350
-Connection ~ 5650 4350
+	7600 1600 5850 1600
 Wire Wire Line
-	2200 1000 2200 550 
-Connection ~ 3050 550 
-Connection ~ 6600 3100
+	6500 3600 4200 3600
 Wire Wire Line
-	6400 4700 7250 4700
+	6500 3800 4200 3800
 Wire Wire Line
-	7250 4700 7250 5000
+	4200 4000 5800 4000
 Wire Wire Line
-	7150 4950 7150 5000
+	5800 4000 5800 4700
 Wire Wire Line
-	6950 5000 6950 4950
+	1950 6150 1700 6150
 Wire Wire Line
-	8900 4950 8200 4950
+	1850 6050 1850 6450
 Wire Wire Line
-	8200 4950 7500 4950
-Connection ~ 8200 4950
+	3850 5650 4600 5650
 Wire Wire Line
-	7700 6000 6950 6000
+	4600 5650 4600 4800
 Wire Wire Line
-	7500 4950 7500 5800
+	4600 4800 4200 4800
 Wire Wire Line
-	7250 5700 7250 5800
+	1850 4750 1850 4800
+Connection ~ 5000 3200
+Connection ~ 4450 6350
 Wire Wire Line
-	7050 5800 7050 5700
+	4200 4500 4900 4500
 Wire Wire Line
-	1700 6850 1700 6900
+	4900 4500 4900 5750
 Wire Wire Line
-	1600 3000 1800 3000
+	4900 5750 5950 5750
 Wire Wire Line
-	5650 4650 5500 4650
+	5950 5750 5950 7300
 Wire Wire Line
-	4600 2800 5200 2800
+	5850 6550 6050 6550
+Connection ~ 6050 6550
 Wire Wire Line
-	5200 2800 5200 2900
+	5850 6650 6150 6650
+Connection ~ 6150 6650
 Wire Wire Line
-	4200 2900 4400 2900
+	7400 4750 7400 2600
+Connection ~ 1850 7450
+Connection ~ 2000 6850
+Connection ~ 1850 6850
 Wire Wire Line
-	4400 2900 4400 2600
+	5150 4200 4200 4200
+Connection ~ 5100 4200
 Wire Wire Line
-	6500 3000 6200 3000
+	3450 1400 3250 1400
 Wire Wire Line
-	6500 3000 6500 3100
-Connection ~ 6500 3100
+	3150 1500 3150 1150
 Wire Wire Line
-	6600 2500 6600 2600
-Connection ~ 6600 2500
+	3150 1150 3050 1150
 Wire Wire Line
-	7050 2400 7050 2600
+	3050 1150 3050 550 
 Wire Wire Line
-	7050 2600 7250 2600
+	6450 4400 6450 4550
 Wire Wire Line
-	5100 3000 5400 3000
+	6400 4850 7050 4850
 Wire Wire Line
-	5300 2800 5400 2800
+	7050 4850 7050 5000
 Wire Wire Line
-	4200 2300 7800 2300
+	6750 5050 6750 4950
 Wire Wire Line
-	7800 2300 7800 3750
+	6750 4950 7150 4950
+Connection ~ 6950 4950
 Wire Wire Line
-	8200 4650 7800 4650
+	6950 6000 6950 5700
 Wire Wire Line
-	7800 4650 7800 4500
+	7500 5800 7050 5800
+Connection ~ 7250 5800
 Wire Wire Line
-	7800 4500 7800 4250
+	1700 6850 2400 6850
 Wire Wire Line
-	7850 4550 7850 4500
+	4700 2500 8450 2500
 Wire Wire Line
-	7850 4500 7800 4500
-Connection ~ 7800 4500
+	3250 1500 3250 1250
+Connection ~ 3250 1400
 Wire Wire Line
-	6650 6650 6600 6650
+	5650 4550 5650 4200
 Wire Wire Line
-	7350 6450 8150 6450
+	4500 2900 5100 2900
 Wire Wire Line
-	8150 6450 8150 6200
+	5100 2900 5100 3000
 Wire Wire Line
-	7350 6650 7450 6650
+	5200 2700 5300 2700
 Wire Wire Line
-	6650 7050 6650 7150
-Connection ~ 2200 550 
+	5300 2700 5300 2800
 Wire Wire Line
-	6100 1450 6100 1350
+	7400 2600 8450 2600
 Wire Wire Line
-	6100 1350 6200 1350
+	6350 3250 6350 3100
 Wire Wire Line
-	6050 6450 6650 6450
-Connection ~ 6050 6450
+	6350 3100 7050 3100
 Wire Wire Line
-	7350 6200 7550 6200
+	6800 2500 6800 2600
+Connection ~ 6800 2500
 Wire Wire Line
-	7550 6200 7700 6200
-Connection ~ 7350 6200
-Connection ~ 7550 6200
+	7050 2400 4200 2400
 Wire Wire Line
-	4200 2100 6400 2100
+	7250 3100 7400 3100
+Connection ~ 7400 3100
 Wire Wire Line
-	6400 2100 6400 1350
+	5200 2900 5400 2900
 Wire Wire Line
-	5550 5400 5550 5350
+	4400 2600 5400 2600
 Wire Wire Line
-	6150 7600 6150 6650
+	7950 4250 7950 4550
 Wire Wire Line
-	6150 6650 6150 6350
+	7950 4550 8200 4550
 Wire Wire Line
-	6150 6350 6150 5550
+	8050 2000 8450 2000
 Wire Wire Line
-	5000 3100 5200 3100
+	7350 6350 7350 6100
 Wire Wire Line
-	5200 3100 5450 3100
+	7350 6100 7200 6100
 Wire Wire Line
-	5450 3100 5450 3200
-Connection ~ 5200 3100
+	7200 6100 7200 6200
 Wire Wire Line
-	6200 3000 6200 2900
+	7350 6550 8050 6550
 Wire Wire Line
-	6200 2900 6200 2800
+	8050 6550 8050 6650
 Wire Wire Line
-	6200 2800 6200 2700
-Connection ~ 6200 2800
+	3850 1400 3850 1500
 Wire Wire Line
-	8450 2300 8450 2000
+	2200 2600 2200 2350
 Wire Wire Line
-	8450 2000 8450 1950
-Connection ~ 8450 2000
+	3350 5800 2250 5800
 Wire Wire Line
-	4450 6250 4450 6350
+	2250 5800 2250 5950
 Wire Wire Line
-	4450 6350 4450 6450
+	6300 1350 6300 2200
 Wire Wire Line
-	4450 6450 4450 6550
-Connection ~ 4450 6450
+	6300 2200 4200 2200
 Wire Wire Line
-	5550 5950 5650 5950
+	6150 6350 6650 6350
+Connection ~ 6150 6350
 Wire Wire Line
-	5650 5950 5650 6000
+	4200 4600 4800 4600
 Wire Wire Line
-	2400 6350 750  6350
+	4800 4600 4800 5850
 Wire Wire Line
-	750  6350 750  5400
+	4800 5850 5850 5850
 Wire Wire Line
-	750  5400 950  5400
+	5850 5850 5850 6350
 Wire Wire Line
-	950  5500 650  5500
+	6050 5650 6050 7500
 Wire Wire Line
-	650  5500 650  5400
+	4200 3400 4200 3500
 Wire Wire Line
-	650  5400 550  5400
+	4200 3500 5300 3500
 Wire Wire Line
-	550  5400 550  5450
+	5300 3500 5300 3400
 Wire Wire Line
-	2150 5000 2250 5000
+	5300 3400 5450 3400
+Connection ~ 6200 2900
 Wire Wire Line
-	2250 5000 2250 5650
+	4700 1900 5000 1900
 Wire Wire Line
-	2250 5650 3350 5650
+	8050 2400 8450 2400
 Wire Wire Line
-	5350 5350 5350 5650
-Connection ~ 5350 5650
-Connection ~ 3050 5500
+	8250 2300 8250 2250
 Wire Wire Line
-	3000 5500 3050 5500
+	8250 2250 8350 2250
 Wire Wire Line
-	3050 5500 3150 5500
+	8350 2250 8350 2400
+Connection ~ 8350 2400
 Wire Wire Line
-	3150 5500 3250 5500
+	4000 6250 4400 6250
 Wire Wire Line
-	3250 5500 3350 5500
-Connection ~ 3250 5500
+	4400 6250 4400 6150
 Wire Wire Line
-	1900 550  2200 550 
+	4400 6150 4500 6150
 Wire Wire Line
-	2200 550  3050 550 
+	4500 6150 4500 4900
 Wire Wire Line
-	3050 550  3250 550 
+	4500 4900 4200 4900
 Wire Wire Line
-	3250 550  3450 550 
+	5700 7700 5700 7700
 Wire Wire Line
-	3450 550  3450 1150
-Connection ~ 3250 550 
+	4550 7300 4550 7500
+Connection ~ 4550 7400
 Wire Wire Line
-	3150 1150 3050 1150
+	5650 6900 5800 6900
 Wire Wire Line
-	3050 1150 2950 1150
-Connection ~ 3050 1150
+	5800 6900 5800 6950
 Wire Wire Line
-	3050 1500 3050 1150
+	1750 5300 2050 5300
 Wire Wire Line
-	3050 1150 3050 550 
+	2050 5300 2050 6150
 Wire Wire Line
-	3850 1500 3850 1400
+	2050 6150 2400 6150
 Wire Wire Line
-	3850 1400 3850 1150
-Connection ~ 3850 1400
+	1750 5500 1850 5500
 Wire Wire Line
-	8100 5250 8100 5050
-Connection ~ 8100 5050
+	1850 5500 1850 5200
 Wire Wire Line
-	8000 5850 8000 5750
-Connection ~ 8000 5750
-$Comp
-L GND #PWR?
-U 1 1 4F61E51B
-P 8000 5850
-F 0 "#PWR?" H 8000 5850 30  0001 C CNN
-F 1 "GND" H 8000 5780 30  0001 C CNN
-	1    8000 5850
-	1    0    0    -1  
-$EndComp
-$Comp
-L R R24
-U 1 1 4F61E4F1
-P 8100 5500
-F 0 "R24" V 8180 5500 50  0000 C CNN
-F 1 "100k" V 8100 5500 50  0000 C CNN
-	1    8100 5500
-	1    0    0    -1  
-$EndComp
-$Comp
-L R R23
-U 1 1 4F61E4E6
-P 7900 5500
-F 0 "R23" V 7980 5500 50  0000 C CNN
-F 1 "100k" V 7900 5500 50  0000 C CNN
-	1    7900 5500
-	1    0    0    -1  
-$EndComp
-$Comp
-L C C6
-U 1 1 4F61DFF3
-P 3650 1150
-F 0 "C6" V 3700 1250 50  0000 L CNN
-F 1 "100nF" V 3550 1300 50  0000 L CNN
-	1    3650 1150
-	0    1    1    0   
-$EndComp
-$Comp
-L C C4
-U 1 1 4F61DFE9
-P 2750 1400
-F 0 "C4" V 2800 1500 50  0000 L CNN
-F 1 "100nF" V 2650 1550 50  0000 L CNN
-	1    2750 1400
-	0    1    1    0   
-$EndComp
+	5250 5350 5250 5550
+Connection ~ 5250 5550
 NoConn ~ 950  5700
 NoConn ~ 1750 5700
 NoConn ~ 1750 5400
@@ -1033,8 +844,8 @@ F 1 "RNG" V 6350 1000 50  0000 C CNN
 	1    6300 1000
 	0    -1   -1   0   
 $EndComp
-NoConn ~ 6600 3500
-NoConn ~ 6600 3400
+NoConn ~ 6500 3500
+NoConn ~ 6500 3400
 $Comp
 L R R22
 U 1 1 4D3F19C4
@@ -1074,19 +885,19 @@ $EndComp
 $Comp
 L C C5
 U 1 1 4D3F1433
-P 2750 1150
-F 0 "C5" V 2800 1250 50  0000 L CNN
-F 1 "100nF" V 2650 1300 50  0000 L CNN
-	1    2750 1150
+P 2750 1400
+F 0 "C5" V 2800 1500 50  0000 L CNN
+F 1 "100nF" V 2650 1550 50  0000 L CNN
+	1    2750 1400
 	0    1    1    0   
 $EndComp
 $Comp
 L +5V #PWR07
 U 1 1 4D39D7B1
-P 5500 4650
-F 0 "#PWR07" H 5500 4740 20  0001 C CNN
-F 1 "+5V" H 5500 4740 30  0000 C CNN
-	1    5500 4650
+P 5500 4550
+F 0 "#PWR07" H 5500 4640 20  0001 C CNN
+F 1 "+5V" H 5500 4640 30  0000 C CNN
+	1    5500 4550
 	1    0    0    -1  
 $EndComp
 $Comp
@@ -1193,7 +1004,7 @@ L BC307 Q3
 U 1 1 4D36290D
 P 1950 5000
 F 0 "Q3" V 2150 4900 50  0000 C CNN
-F 1 "PNP BC328" V 1800 5050 50  0000 C CNN
+F 1 "PNP BC557" V 1800 5050 50  0000 C CNN
 	1    1950 5000
 	-1   0    0    1   
 $EndComp
@@ -1290,46 +1101,46 @@ $EndComp
 $Comp
 L GND #PWR020
 U 1 1 4D094C1F
-P 6800 4450
-F 0 "#PWR020" H 6800 4450 30  0001 C CNN
-F 1 "GND" H 6800 4380 30  0001 C CNN
-	1    6800 4450
+P 6700 4450
+F 0 "#PWR020" H 6700 4450 30  0001 C CNN
+F 1 "GND" H 6700 4380 30  0001 C CNN
+	1    6700 4450
 	1    0    0    -1  
 $EndComp
 $Comp
 L C C16
 U 1 1 4D094AF6
-P 6100 4350
-F 0 "C16" H 5950 4400 50  0000 L CNN
-F 1 "100nF" H 5950 4250 50  0000 L CNN
-	1    6100 4350
+P 6000 4350
+F 0 "C16" H 5850 4400 50  0000 L CNN
+F 1 "100nF" H 5850 4250 50  0000 L CNN
+	1    6000 4350
 	1    0    0    -1  
 $EndComp
 $Comp
 L C C19
 U 1 1 4D094AC9
-P 6400 4350
-F 0 "C19" H 6550 4400 50  0000 L CNN
-F 1 "100nF" H 6500 4600 50  0000 L CNN
-	1    6400 4350
+P 6300 4350
+F 0 "C19" H 6450 4400 50  0000 L CNN
+F 1 "100nF" H 6400 4600 50  0000 L CNN
+	1    6300 4350
 	1    0    0    -1  
 $EndComp
 $Comp
 L C C17
 U 1 1 4D094AC6
-P 6250 4200
-F 0 "C17" H 6150 4100 50  0000 L CNN
-F 1 "100nF" H 6300 3950 50  0000 L CNN
-	1    6250 4200
+P 6150 4200
+F 0 "C17" H 6050 4100 50  0000 L CNN
+F 1 "100nF" H 6200 3950 50  0000 L CNN
+	1    6150 4200
 	1    0    0    -1  
 $EndComp
 $Comp
 L C C18
 U 1 1 4D094A8E
-P 6550 4200
-F 0 "C18" H 6600 4300 50  0000 L CNN
-F 1 "100nF" H 6700 4100 50  0000 L CNN
-	1    6550 4200
+P 6450 4200
+F 0 "C18" H 6500 4300 50  0000 L CNN
+F 1 "100nF" H 6600 4100 50  0000 L CNN
+	1    6450 4200
 	-1   0    0    1   
 $EndComp
 $Comp
@@ -1407,10 +1218,10 @@ $EndComp
 $Comp
 L CONN_7 JP7
 U 1 1 4D093CC0
-P 6950 3600
-F 0 "JP7" H 6950 3900 60  0000 C CNN
-F 1 "Sensors" V 7020 3600 60  0000 C CNN
-	1    6950 3600
+P 6850 3600
+F 0 "JP7" H 6850 3900 60  0000 C CNN
+F 1 "Sensors" V 6920 3600 60  0000 C CNN
+	1    6850 3600
 	1    0    0    -1  
 $EndComp
 $Comp
@@ -1671,7 +1482,7 @@ F 1 "MAX232" H 3200 5900 70  0000 C CNN
 	-1   0    0    1   
 $EndComp
 $Comp
-L ATMEGA32-A IC1
+L ATMEGA32-P IC1
 U 1 1 4D091A70
 P 3200 3500
 F 0 "IC1" H 2400 5330 50  0000 L BNN
diff --git a/common/serial.h b/common/serial.h
index 16b1dac..8efe63d 100644
--- a/common/serial.h
+++ b/common/serial.h
@@ -154,15 +154,18 @@ void FUNC_switch_baudrate(uint32_t baud){
 }
 
 void FUNC_switch_mode_receive(void){
-	while ( !( UCSRA & (1 << TXC)) ) {;} //Wait for transmit complete
+	//Wait for empty transmit buffer
+	while ( !( UCSRA & (1 << UDRE)) ) {;}
 	UCSRB &= ~(1<<TXEN);
+	_delay_ms(1);
 	UCSRB |=  (1<<RXEN);
-	while (UCSRA & (1 << RXC)){UDR;} //trash receive buffer
+	while (UCSRA & (1 << RXC)){ //trash receive buffer
+		UDR;
+	}
 }
 void FUNC_switch_mode_transmit(void){
 	UCSRB |=  (1<<TXEN);
 	UCSRB &= ~(1<<RXEN);
-	while (UCSRA & (1 << RXC)){UDR;} //trash receive buffer
 }
 
 
-- 
GitLab