Pārlūkot izejas kodu

Start library integration. Minor code cleanups.

k4be 2 gadi atpakaļ
vecāks
revīzija
7d4980cec0
8 mainītis faili ar 232 papildinājumiem un 121 dzēšanām
  1. 21 22
      soft/HD44780-I2C.c
  2. 16 13
      soft/HD44780-I2C.h
  3. 1 1
      soft/Makefile
  4. 7 3
      soft/ds18b20.c
  5. 74 41
      soft/main.c
  6. 86 11
      soft/main.h
  7. 26 29
      soft/mmc.c
  8. 1 1
      soft/uart1.c

+ 21 - 22
soft/HD44780.c → soft/HD44780-I2C.c

@@ -1,22 +1,22 @@
 /*-------------------------------------------------------------------------------------------------
- * Wyświetlacz alfanumeryczny ze sterownikiem HD44780
- * Sterowanie w trybie 8-bitowym z odczytem flagi zajętości
- * Plik : HD44780.c	
+ * Wyświetlacz alfanumeryczny ze sterownikiem HD44780
+ * Sterowanie w trybie 8-bitowym z odczytem flagi zajętości
+ * Plik : HD44780-I2C.c	
  * Mikrokontroler : Atmel AVR
  * Kompilator : avr-gcc
- * Autor : Radosław Kwiecień
- * Źródło : http://radzio.dxp.pl/hd44780/
+ * Autor : Radosław Kwiecień
+ * Źródło : http://radzio.dxp.pl/hd44780/
  * Data : 24.03.2007
  * Modified: k4be, 2022 (I2C access mode)
  * ------------------------------------------------------------------------------------------------ */
 
-#include "HD44780.h"
+#include "HD44780-I2C.h"
 #include <avr/pgmspace.h>
 
 
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja zapisu bajtu do wyświetacza (bez rozróżnienia instrukcja/dane).
+// Funkcja zapisu bajtu do wyświetacza (bez rozróżnienia instrukcja/dane).
 //
 //-------------------------------------------------------------------------------------------------
 void _LCD_Write(unsigned char dataToWrite)
@@ -31,7 +31,7 @@ void _LCD_Write(unsigned char dataToWrite)
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja odczytu bajtu z wyświetacza (bez rozróżnienia instrukcja/dane).
+// Funkcja odczytu bajtu z wyświetacza (bez rozróżnienia instrukcja/dane).
 //
 //-------------------------------------------------------------------------------------------------
 
@@ -50,7 +50,7 @@ unsigned char _LCD_Read(void)
 
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja zapisu rozkazu do wyświetlacza
+// Funkcja zapisu rozkazu do wyświetlacza
 //
 //-------------------------------------------------------------------------------------------------
 void LCD_WriteCommand(unsigned char commandToWrite)
@@ -69,11 +69,10 @@ unsigned char LCD_ReadStatus(void)
 {
 	expander_set_bit(LCD_RS_PORT, LCD_RS, 0);
 	return _LCD_Read();
-	return 0;
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja zapisu danych do pamięci wyświetlacza
+// Funkcja zapisu danych do pamięci wyświetlacza
 //
 //-------------------------------------------------------------------------------------------------
 void LCD_WriteData(unsigned char dataToWrite)
@@ -84,7 +83,7 @@ void LCD_WriteData(unsigned char dataToWrite)
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja odczytu danych z pamięci wyświetlacza
+// Funkcja odczytu danych z pamięci wyświetlacza
 //
 //-------------------------------------------------------------------------------------------------
 unsigned char LCD_ReadData(void)
@@ -94,7 +93,7 @@ unsigned char LCD_ReadData(void)
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja wyświetlenia napisu na wyswietlaczu.
+// Funkcja wyświetlenia napisu na wyswietlaczu.
 //
 //-------------------------------------------------------------------------------------------------
 void LCD_WriteText(const char * text)
@@ -111,7 +110,7 @@ void LCD_WriteTextP(const char *text)
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja ustawienia współrzędnych ekranowych
+// Funkcja ustawienia współrzędnych ekranowych
 //
 //-------------------------------------------------------------------------------------------------
 void LCD_GoTo(unsigned char x, unsigned char y)
@@ -120,7 +119,7 @@ void LCD_GoTo(unsigned char x, unsigned char y)
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja czyszczenia ekranu wyświetlacza.
+// Funkcja czyszczenia ekranu wyświetlacza.
 //
 //-------------------------------------------------------------------------------------------------
 void LCD_Clear(void)
@@ -130,7 +129,7 @@ void LCD_Clear(void)
 }
 //-------------------------------------------------------------------------------------------------
 //
-// Funkcja przywrócenia początkowych współrzędnych wyświetlacza.
+// Funkcja przywrócenia początkowych współrzędnych wyświetlacza.
 //
 //-------------------------------------------------------------------------------------------------
 void LCD_Home(void)
@@ -143,7 +142,7 @@ void LCD_Home(void)
 // Procedura inicjalizacji kontrolera HD44780.
 //
 //-------------------------------------------------------------------------------------------------
-void LCD_Initalize(void)
+void LCD_Initialize(void)
 {
 	unsigned char i;
 	LCD_DATA_OUTPUT();
@@ -151,11 +150,11 @@ void LCD_Initalize(void)
 	expander_set_bit_no_send(LCD_RS_PORT, LCD_RS, 0); // wyzerowanie linii RS
 	expander_set_bit_no_send(LCD_E_PORT, LCD_E, 0);  // wyzerowanie linii E
 	expander_set_bit(LCD_RW_PORT, LCD_RW, 0);
-	_delay_ms(100); // oczekiwanie na ustalibizowanie się napiecia zasilajacego
+	_delay_ms(100); // oczekiwanie na ustalibizowanie siÄ™ napiecia zasilajacego
 
 	exp_output[0] = 0x3F;
 	expander_write(0);
-	for(i = 0; i < 3; i++) // trzykrotne powtórzenie bloku instrukcji
+	for(i = 0; i < 3; i++) // trzykrotne powtĂłrzenie bloku instrukcji
 	{
 	  expander_set_bit(LCD_E, LCD_E, 1);
 	  expander_set_bit(LCD_E, LCD_E, 0);
@@ -163,10 +162,10 @@ void LCD_Initalize(void)
 	}
 
 	LCD_WriteCommand(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_8_BIT); // interfejs 4-bity, 2-linie, znak 5x7
-	LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); // wyłączenie wyswietlacza
-	//LCD_WriteCommand(HD44780_CLEAR); // czyszczenie zawartosći pamieci DDRAM
+	LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); // wyłączenie wyswietlacza
+	//LCD_WriteCommand(HD44780_CLEAR); // czyszczenie zawartosæi pamieci DDRAM
 	LCD_WriteCommand(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT);// inkrementaja adresu i przesuwanie kursora
-	LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); // włącz LCD, bez kursora i mrugania
+	LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); // włącz LCD, bez kursora i mrugania
 }
 
 //-------------------------------------------------------------------------------------------------

+ 16 - 13
soft/HD44780.h → soft/HD44780-I2C.h

@@ -1,13 +1,16 @@
-//-------------------------------------------------------------------------------------------------
-// Wyświetlacz alfanumeryczny ze sterownikiem HD44780
-// Sterowanie w trybie 8-bitowym z odczytem flagi zajętości
-// Plik : HD44780.h	
-// Mikrokontroler : Atmel AVR
-// Kompilator : avr-gcc
-// Autor : Radosław Kwiecień
-// Źródło : http://radzio.dxp.pl/hd44780/
-// Data : 24.03.2007
-//-------------------------------------------------------------------------------------------------
+/*-------------------------------------------------------------------------------------------------
+ * Wyświetlacz alfanumeryczny ze sterownikiem HD44780
+ * Sterowanie w trybie 8-bitowym z odczytem flagi zajętości
+ * Plik : HD44780-I2C.h
+ * Mikrokontroler : Atmel AVR
+ * Kompilator : avr-gcc
+ * Autor : Radosław Kwiecień
+ * Źródło : http://radzio.dxp.pl/hd44780/
+ * Data : 24.03.2007
+ * Modified: k4be, 2022 (I2C access mode)
+ * ------------------------------------------------------------------------------------------------ */
+
+#pragma once
 
 #include <avr/io.h>
 #include <util/delay.h>
@@ -15,8 +18,8 @@
 
 //-------------------------------------------------------------------------------------------------
 //
-// Konfiguracja sygnałów sterujących wyświetlaczem.
-// Można zmienić stosownie do potrzeb.
+// Konfiguracja sygnałów sterujących wyświetlaczem.
+// Można zmieniæ stosownie do potrzeb.
 //
 //-------------------------------------------------------------------------------------------------
 #define LCD_RS_PORT 	1
@@ -87,7 +90,7 @@ void LCD_WriteText(const char *);
 void LCD_GoTo(unsigned char, unsigned char);
 void LCD_Clear(void);
 void LCD_Home(void);
-void LCD_Initalize(void);
+void LCD_Initialize(void);
 void LCD_WriteTextP(const char *);
 
 //-------------------------------------------------------------------------------------------------

+ 1 - 1
soft/Makefile

@@ -13,7 +13,7 @@ DEVICE  = atmega644pa
 OPTIMIZE = s
 
 ### C Standard level (c89, gnu89, c99 or gnu99)
-CSTD = gnu89
+CSTD = c99
 
 ### Include dirs, library dirs and definitions
 LIBS	=

+ 7 - 3
soft/ds18b20.c

@@ -1,5 +1,5 @@
 /*
- * ds18b20.c
+ * ds18b20.c - single sensor support
  * k4be 2019
  * License: BSD
  */ 
@@ -19,12 +19,13 @@ union {
 #define MAX_ERRORS 5
 
 unsigned char temp_ok;
-float hs_temp;
+signed int ds18b20_temp;
 
 void gettemp(void){
 	unsigned char i, crc=0, tmp;
 	static unsigned char error_cnt;
 	unsigned char temp_ok_out = 0;
+	signed int temp;
 
 	if(System.timers.owire) return;
 
@@ -45,7 +46,10 @@ void gettemp(void){
 	}
 	if(!crc){
 		if(t.owbuffer[0] != 0x50 || t.owbuffer[1] != 0x05 || t.owbuffer[5] != 0xff || t.owbuffer[7] != 0x10){
-			hs_temp = (float)t.t * 0.0625;
+			temp = t.t;
+			temp *= (int)(0.625*16);
+			temp >>= 4;
+			ds18b20_temp = temp;
 			temp_ok_out = 1;
 		}
 	}

+ 74 - 41
soft/main.c

@@ -6,6 +6,7 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/sleep.h>
+#include <util/delay.h>
 #include <string.h>
 #include "main.h"
 #include "ff.h"
@@ -14,6 +15,10 @@
 #include "uart1.h"
 #include "xitoa.h"
 #include "stime.h"
+#include "ds18b20.h"
+#include "I2C.h"
+#include "expander.h"
+#include "HD44780-I2C.h"
 
 
 FUSES = {0xFF, 0x11, 0xFE};		/* ATmega644PA fuses: Low, High, Extended.
@@ -22,25 +27,27 @@ in the output hex file with program code. However some old flash programmers
 cannot load the fuse bits from hex file. If it is the case, remove this line
 and use these values to program the fuse bits. */
 
+volatile struct system_s System;
 FATFS Fatfs;				/* File system object for each logical drive */
 FIL File1;					/* File object */
 char Line[100];				/* Line buffer */
 
-volatile BYTE Timer;		/* 100Hz decrement timer */
-
 /*---------------------------------------------------------*/
 /* 100Hz timer interrupt generated by OC1A                 */
 /*---------------------------------------------------------*/
 
 ISR(TIMER1_COMPA_vect)
 {
-	BYTE n;
 	static WORD ivt_sync;
 	static BYTE led;
-
-
-	n = Timer;
-	if (n) Timer = --n;;
+	unsigned int *volatile ctimer;
+	unsigned char i;
+	
+	for(i=0; i<sizeof(System.timers)/sizeof(unsigned int); i++){ // decrement every variable from timers struct unless it's already zero
+		ctimer = ((unsigned int *)&System.timers) + i;
+		if(*ctimer)
+			(*ctimer)--;
+	}
 
 	/* Sync interval */
 	if (IVT_SYNC && ++ivt_sync >= IVT_SYNC * 100) {
@@ -109,16 +116,24 @@ UINT get_line (		/* 0:Brownout, >0: Number of bytes received. */
 	char c;
 	UINT i = 0;
 
+	set_timer(recv_timeout, 1000);
 
 	for (;;) {
-		if (FLAGS & F_LVD) return 0;	/* A brownout is detected */
-		if (!uart0_test()) continue;
+		if (FLAGS & F_LVD)
+			return 0;	/* A brownout is detected */
+		if (timer_expired(recv_timeout))
+			return 0; /* timeout; continue the main loop */
+		if (!uart0_test())
+			continue;
 		c = (char)uart0_get();
 		xputc(c);
-		if (i == 0 && c != '$') continue;	/* Find start of line */
+		if (i == 0 && c != '$')
+			continue;	/* Find start of line */
 		buff[i++] = c;
-		if (c == '\n') break;	/* EOL */
-		if (i >= sz_buf) i = 0;	/* Buffer overflow (abort this line) */
+		if (c == '\n')
+			break;	/* EOL */
+		if (i >= sz_buf)
+			i = 0;	/* Buffer overflow (abort this line) */
 	}
 
 	return i;
@@ -133,9 +148,11 @@ void beep (UINT len, BYTE cnt)
 {
 	while (cnt--) {
 		BEEP_ON();
-		DELAY_MS(len);
+		set_timer(beep, len);
+		while(!timer_expired(beep)) {};
 		BEEP_OFF();
-		DELAY_MS(len);
+		set_timer(beep, len);
+		while(!timer_expired(beep)) {};
 	}
 }
 
@@ -185,9 +202,11 @@ BYTE gp_val2 (
 
 
 	n = db[0] - '0';
-	if (n >= 10) return 0;
+	if (n >= 10)
+		return 0;
 	m = db[1] - '0';
-	if (m >= 10) return 0;
+	if (m >= 10)
+		return 0;
 
 	return n * 10 + m;
 }
@@ -205,7 +224,8 @@ time_t gp_rmctime (	/* Get GPS status from RMC sentence */
 	time_t utc;
 
 
-	if (gp_comp(str, PSTR("$GPRMC"))) return 0;	/* Not the RMC */
+	if (gp_comp(str, PSTR("$GPRMC")))
+		return 0;	/* Not the RMC */
 
 	p = gp_col(str, 2);		/* Get status */
 	if (!p || *p != 'A') {
@@ -214,19 +234,22 @@ time_t gp_rmctime (	/* Get GPS status from RMC sentence */
 	}
 
 	p = gp_col(str, 1);		/* Get h:m:s */
-	if (!p) return 0;
+	if (!p)
+		return 0;
 	tmc.tm_hour = gp_val2(p);
 	tmc.tm_min = gp_val2(p+2);
 	tmc.tm_sec = gp_val2(p+4);
 
 	p = gp_col(str, 9);		/* Get y:m:d */
-	if (!p) return 0;
+	if (!p)
+		return 0;
 	tmc.tm_mday = gp_val2(p);
 	tmc.tm_mon = gp_val2(p+2) - 1;
 	tmc.tm_year = gp_val2(p+4) + 100;
 
 	utc = mktime(&tmc);				/* Check time validity */
-	if (utc == -1) return 0;
+	if (utc == -1)
+		return 0;
 
 	FLAGS |= F_GPSOK;
 	return utc;
@@ -238,14 +261,9 @@ time_t gp_rmctime (	/* Get GPS status from RMC sentence */
 static
 void ioinit (void)
 {
-	PORTB = 0b00000011;	 	/* --zzzzHH */
-	DDRB  = 0b00000011;
-
-	PORTC = 0b00111101;		/* --uuuuLH */
-	DDRC  = 0b00000011;
-
-	PORTD = 0b10111110;		/* uLuuuuHz */
-	DDRD  = 0b01000010;
+	BUZZER_DDR |= BUZZER;
+	GPS_DIS_DDR |= GPS_DIS;
+	LEDR_DDR |= LEDR;
 
 	OCR1A = F_CPU/8/100-1;		/* Timer1: 100Hz interval (OC1A) */
 	TCCR1B = _BV(WGM12) | _BV(CS11);
@@ -259,6 +277,11 @@ void ioinit (void)
 	
 	/* uart1 (debug) */	
 	uart1_init();
+	
+	I2C_init();
+	expander_init(0, 0x00, 0x00); /* all as outputs */
+	LCD_Initialize();
+	LCD_Clear();	
 
 	sei();
 }
@@ -276,7 +299,6 @@ int main (void)
 	time_t utc;
 	FRESULT res;
 
-
 	ioinit();
 	xdev_out(uart1_put);
 
@@ -287,14 +309,17 @@ int main (void)
 
 		/* Wait for supply voltage stabled */
 		while (FLAGS & F_LVD) {};
-		DELAY_MS(500);
-		if (FLAGS & F_LVD) continue;
-		if (disk_status(0) & STA_NODISK) continue;
+		_delay_ms(500);
+		if (FLAGS & F_LVD)
+			continue;
+		if (disk_status(0) & STA_NODISK)
+			continue;
 
 		res = f_mount(&Fatfs, "", 1);
 		if (res != FR_OK) {
 			xprintf(PSTR("FS error %u\r\n"), res);
-			err = 2; continue;
+			err = 2;
+			continue;
 		}
 		xputs(PSTR("FS Ok\r\n"));
 		beep(50, 1);				/* 1 beep */
@@ -302,14 +327,16 @@ int main (void)
 		/* Initialize GPS receiver */
 		GPS_ON();		/* GPS power on */
 		FLAGS |= F_POW;
-		DELAY_MS(300);	/* Delay */
+		_delay_ms(300);	/* Delay */
 		uart0_init();	/* Enable UART */
 //		xfprintf(uart0_put, PSTR("$PSRF106,21*0F\r\n"));	/* Send initialization command (depends on the receiver) */
 
 		utc = 0;
-		for (;;) {
+		for (;;) { /* main loop */
+			gettemp();
 			len = get_line(Line, sizeof Line);	/* Receive a line from GPS receiver */
-			if (!len) break;		/* Brownout? */
+			if (!len) continue;
+			if (FLAGS & F_LVD) break; /* brownout */
 
 			if (!utc) {
 				utc = gp_rmctime(Line);	/* Get time in UTC from a valid RMC sentence */
@@ -322,7 +349,9 @@ int main (void)
 						|| f_lseek(&File1, f_size(&File1)) 					/* Append mode */
 						|| f_write(&File1, "\r\n", 2, &bw))					/* Put a blank line as start marker */
 					{
-						utc = 0; err = 2; break;	/* Failed to start logging */
+						utc = 0;
+						err = 2;
+						break;	/* Failed to start logging */
 					}
 					beep(50, 2);		/* Two beeps. Start logging. */
 				}
@@ -330,11 +359,13 @@ int main (void)
 				gp_rmctime(Line);
 				f_write(&File1, Line, len, &bw);
 				if (bw != len) {
-					err = 3; break;
+					err = 3;
+					break;
 				}
 				if (FLAGS & F_SYNC) {
 					if (f_sync(&File1)) {
-						err = 2; break;
+						err = 2;
+						break;
 					}
 					FLAGS &= ~F_SYNC;
 				}
@@ -347,9 +378,11 @@ int main (void)
 		FLAGS &= ~F_POW;
 
 		/* Close file */
-		if (utc && f_close(&File1)) err = 2;
+		if (utc && f_close(&File1))
+			err = 2;
 		disk_ioctl(0, CTRL_POWER_OFF, 0);
-		if (!err) beep(500, 1);	/* Long beep on file close succeeded */
+		if (!err)
+			beep(500, 1);	/* Long beep on file close succeeded */
 	}
 
 }

+ 86 - 11
soft/main.h

@@ -1,20 +1,57 @@
 #pragma once
 
-#define LOCALDIFF	+9			/* Time difference from UTC [hours] */
+#include <avr/interrupt.h>
+#include "expander.h"
+
+#define LOCALDIFF	+1			/* Time difference from UTC [hours] */
 #define	IVT_SYNC	180			/* f_sync() interval (0:no periodic sync) [sec] */
 
-#define	VI_LVL		9.0			/* Blackout threshold [volt] */
-#define	VI_LVH		10.0		/* Recharge threshold [volt] */
-#define	VI_MULT		(10.0 / 130 / 3.0 * 1024)
+#define	VI_LVL		4.2			/* Blackout threshold [volt] */
+#define	VI_LVH		4.8			/* Recharge threshold [volt] */
+#define	VI_MULT		(3.3 / 6.6 / 2.495 * 1024)
+
+/* pin definitions */
+
+#define BUZZER_PORT		PORTA
+#define BUZZER			_BV(PA7)
+#define BUZZER_DDR		DDRA
+
+#define GPS_DIS_PORT	PORTC
+#define GPS_DIS			_BV(PC6)
+#define GPS_DIS_DDR		DDRC
+
+#define LEDR_PORT		PORTA
+#define LEDR			_BV(PA6)
+#define LEDR_DDR		DDRA
+
+#define SD_CS_PORT		PORTB
+#define SD_CS			_BV(PB4)
+#define SD_CS_DDR		DDRB
+
+#define SD_PWROFF_PORT	PORTB
+#define SD_PWROFF		_BV(PB3)
+#define SD_PWROFF_DDR	DDRB
+#define SD_PWROFF_PIN	PINB
+
+#define SD_CD			_BV(PB1)
+#define SD_CD_PIN		PINB
+
+#define SD_WP			_BV(PB2)
+#define SD_WP_PIN		PINB
 
-#define	BEEP_ON()		0
-#define	BEEP_OFF()		0
-#define	LEDG_ON()		PORTB &= ~_BV(0)
-#define	LEDG_OFF()		PORTB |= _BV(0)
-#define GPS_ON()		PORTC |= _BV(1)
-#define GPS_OFF()		PORTC &= ~_BV(1)
+#define LEDG_PORT		1 /* expander */
+#define LEDG			_BV(4)
 
-#define	DELAY_MS(d)		{for (Timer = (BYTE)(d / 10); Timer; ) ; }
+/* on/off macros */
+
+#define	BEEP_ON()		{BUZZER_PORT |= BUZZER;}
+#define	BEEP_OFF()		{BUZZER_PORT &= ~BUZZER;}
+#define LEDR_ON()		{LEDR_PORT |= LEDR;}
+#define LEDR_OFF()		{LEDR_PORT &= ~LEDR;}
+#define	LEDG_ON()		expander_set_bit(LEDG_PORT, LEDG, 1)
+#define	LEDG_OFF()		expander_set_bit(LEDG_PORT, LEDG, 0)
+#define GPS_ON()		{GPS_DIS_PORT &= ~GPS_DIS;}
+#define GPS_OFF()		{GPS_DIS_PORT |= GPS_DIS;}
 
 #define FLAGS	GPIOR0		/* Status flags and bit definitions */
 #define	F_POW	0x80		/* Power */
@@ -22,3 +59,41 @@
 #define	F_SYNC	0x04		/* Sync request */
 #define	F_LVD	0x01		/* Low battery detect */
 
+#define ERROR_NO	0
+#define ERROR_I2C	1
+#define ERROR_I2C_TIMEOUT	2
+
+#define ms(x) (x/100)
+
+struct timers {
+	unsigned int owire;
+	unsigned int beep;
+	unsigned int recv_timeout;
+};
+
+struct system_s {
+	struct timers timers;
+	unsigned int global_error;	
+};
+
+extern volatile struct system_s System;
+
+static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
+static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
+	cli();
+	*data = value;
+	sei();
+}
+static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
+static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
+	unsigned int ret;
+	cli();
+	ret = *data;
+	sei();
+	return ret;
+}
+
+#define set_timer(timer,val) set_timer_counts(timer, ms(val))
+#define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
+#define timer_expired(timer) !atomic_get_uint(&System.timers.timer)
+

+ 26 - 29
soft/mmc.c

@@ -4,26 +4,20 @@
 
 #include <avr/io.h>
 #include "diskio.h"
+#include "main.h"
 
 
 /* Port controls  (Platform dependent) */
-#ifdef LEDR_UART	/* Disable card access indicator when red LED is used as debug output */
-#define LEDR_ON()
-#define LEDR_OFF()
-#else
-#define LEDR_ON()	PORTB &= ~_BV(1)
-#define LEDR_OFF()	PORTB |= _BV(1)
-#endif
-
-#define CS_LOW()	{PORTB &= ~_BV(2); LEDR_ON();}	/* CS=low, LED on */
-#define	CS_HIGH()	{PORTB |= _BV(2); LEDR_OFF();}	/* CS=high, LED off */
-#define POWER_ON()	PORTC &= ~_BV(0)	/* MMC power on */
-#define POWER_OFF()	PORTC |= _BV(0)		/* MMC power off */
-#define POWER		(!(PINC & _BV(0)))	/* Test for power state.   on:true, off:false */
-#define SOCKINS		(!(PIND & _BV(7)))	/* Test for card exist.    yes:true, true:false, default:true */
-#define SOCKWP		0					/* Test for write protect. yes:true, no:false, default:false */
-#define	FCLK_SLOW()	SPCR = 0x52		/* Set slow clock (F_CPU / 64) */
-#define	FCLK_FAST()	SPCR = 0x50		/* Set fast clock (F_CPU / 2) */
+
+#define CS_LOW()	{SD_CS_PORT &= ~SD_CS; LEDR_ON();}	/* CS=low, LED on */
+#define	CS_HIGH()	{SD_CS_PORT |= SD_CS; LEDR_OFF();}	/* CS=high, LED off */
+#define POWER_ON()	{SD_PWROFF_PORT &= ~SD_PWROFF;}	/* MMC power on */
+#define POWER_OFF()	{SD_PWROFF_PORT |= SD_PWROFF;}		/* MMC power off */
+#define POWER		(!(SD_PWROFF_PIN & SD_PWROFF))	/* Test for power state.   on:true, off:false */
+#define SOCKINS		(!(SD_CD_PIN & SD_CD))	/* Test for card exist.    yes:true, true:false, default:true */
+#define SOCKWP		(SD_WP_PIN & SD_WP)		/* Test for write protect. yes:true, no:false, default:false */
+#define	FCLK_SLOW()	SPCR = _BV(MSTR) | _BV(SPE) | _BV(SPR1) | _BV(SPR0)	/* Set slow clock (F_CPU / 64) */
+#define	FCLK_FAST()	SPCR = _BV(MSTR) | _BV(SPE)							/* Set fast clock (F_CPU / 2) */
 
 
 /*--------------------------------------------------------------------------
@@ -80,11 +74,8 @@ void power_off (void)
 {
 	SPCR = 0;				/* Disable SPI function */
 
-	DDRD  &= 0b01111111;	/* Set SCK/MOSI/CS to hi-z, INS# as pull-up */
-	PORTD |= 0b10000000;
-	DDRB  &= 0b11000011;
-	PORTB |= 0b11000011;
-
+	DDRB  &= ~(_BV(PB5) | _BV(PB7));	/* Set SCK/MOSI to hi-z */
+	SD_CS_DDR &= ~SD_CS;
 	POWER_OFF();
 	Stat |= STA_NOINIT;
 }
@@ -92,15 +83,21 @@ void power_off (void)
 static
 void power_on (void)	/* Apply power sequence */
 {
-	for (Timer1 = 30; Timer1; ) ;	/* 300ms */
+	for (Timer1 = 30; Timer1; ) {};	/* 300ms */
 	POWER_ON();						/* Power on */
-	for (Timer1 = 3; Timer1; ) ;	/* 30ms */
+	for (Timer1 = 3; Timer1; ) {};	/* 30ms */
+
+	SD_CS_PORT |= SD_CS;
+	SD_CS_DDR |= SD_CS;
+
+	SD_PWROFF_PORT |= SD_PWROFF;
+	SD_PWROFF_DDR |= SD_PWROFF;
 
-	PORTB = (PORTB & 0b11011111) | 0b00001100;	/* Configure SCK/MOSI/CS as output */
-	DDRB  = (DDRB  & 0b11101111) | 0b00101100;
+	PORTB |= _BV(PB5) | _BV(PB7);	/* Configure SCK/MOSI as output */
+	DDRB  |= _BV(PB5) | _BV(PB7);
 
-	SPCR = 0x52;	/* Enable SPI function in mode 0 */
-	SPSR = 0x01;	/* SPI 2x mode */
+	FCLK_SLOW();	/* Enable SPI function in mode 0 */
+	SPSR = _BV(SPI2X);	/* SPI 2x mode */
 }
 
 
@@ -463,7 +460,7 @@ DRESULT disk_write (
 DRESULT disk_ioctl (
 	BYTE drv,		/* Physical drive nmuber (0) */
 	BYTE ctrl,		/* Control code */
-	void *buff		/* Buffer to send/receive control data */
+	void *buff __attribute((unused))	/* Buffer to send/receive control data */
 )
 {
 	DRESULT res;

+ 1 - 1
soft/uart1.c

@@ -7,7 +7,7 @@
 #include "uart1.h"
 
 #define	UART1_BAUD		9600
-#define	USE_TXINT		0
+#define	USE_TXINT		1
 #define	SZ_FIFO			512