| 
					
				 | 
			
			
				@@ -1,58 +1,33 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-/*---------------------------------------------------------------*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-/* Automotive GPS logger                       (C)ChaN, 2014     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-/*---------------------------------------------------------------*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/*---------------------------------------------------------------* 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * Automotive GPS logger                       (C)ChaN, 2014     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * Modified                                       k4be, 2022     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *---------------------------------------------------------------*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include <avr/io.h> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include <avr/interrupt.h> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include <avr/sleep.h> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include <string.h> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+#include "main.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "ff.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "diskio.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#include "uart.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#include "suart.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+#include "uart0.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+#include "uart1.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "xitoa.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "stime.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-FUSES = {0xE6, 0xD9, 0xFD};		/* ATmega328P fuses: Low, High, Extended. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+FUSES = {0xFF, 0x11, 0xFE};		/* ATmega644PA fuses: Low, High, Extended. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 This is the fuse settings for this project. The fuse bits will be included 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 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. */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define LOCALDIFF	+9			/* 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	BEEP_ON()		TCCR0A = 0b01000010 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define	BEEP_OFF()		TCCR0A = 0b10000010 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#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	DELAY_MS(d)		{for (Timer = (BYTE)(d / 10); Timer; ) ; } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 FATFS Fatfs;				/* File system object for each logical drive */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 FIL File1;					/* File object */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 char Line[100];				/* Line buffer */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 volatile BYTE Timer;		/* 100Hz decrement timer */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define FLAGS	GPIOR0		/* Status flags and bit definitions */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define	F_POW	0x80		/* Power */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define	F_GPSOK	0x08		/* GPS data valid */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define	F_SYNC	0x04		/* Sync request */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#define	F_LVD	0x01		/* Low battery detect */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /*---------------------------------------------------------*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /* 100Hz timer interrupt generated by OC1A                 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /*---------------------------------------------------------*/ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -60,9 +35,8 @@ volatile BYTE Timer;		/* 100Hz decrement timer */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ISR(TIMER1_COMPA_vect) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	BYTE n; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	WORD ad; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	static WORD ivt_sync; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	static BYTE led, lvt; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	static BYTE led; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	n = Timer; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -85,10 +59,18 @@ ISR(TIMER1_COMPA_vect) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		LEDG_OFF(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	/* Power supply monitor */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	/* MMC/SD timer procedure */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	disk_timerproc(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/* Power supply monitor */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ISR(ADC_vect) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	WORD ad; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	static BYTE lvt; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	ad = ADC * 100; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	ADMUX = 7; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	ADCSRA = _BV(ADEN)|_BV(ADSC)|0b110; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if (FLAGS & F_LVD) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if (ad > (WORD)(VI_LVH * VI_MULT) * 100) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			FLAGS &= ~F_LVD; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -102,14 +84,8 @@ ISR(TIMER1_COMPA_vect) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			lvt = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	/* MMC/SD timer procedure */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	disk_timerproc(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /*---------------------------------------------------------*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /* User Provided Timer Function for FatFs module           */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /*---------------------------------------------------------*/ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -136,8 +112,8 @@ UINT get_line (		/* 0:Brownout, >0: Number of bytes received. */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	for (;;) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if (FLAGS & F_LVD) return 0;	/* A brownout is detected */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if (!uart_test()) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		c = (char)uart_get(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if (!uart0_test()) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		c = (char)uart0_get(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		xputc(c); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if (i == 0 && c != '$') continue;	/* Find start of line */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		buff[i++] = c; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -272,15 +248,18 @@ void ioinit (void) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	DDRD  = 0b01000010; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	OCR1A = F_CPU/8/100-1;		/* Timer1: 100Hz interval (OC1A) */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	TCCR1B = 0b00001010; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	TCCR1B = _BV(WGM12) | _BV(CS11); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	TIMSK1 = _BV(OCIE1A);		/* Enable TC1.oca interrupt */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	OCR0A = F_CPU/64/4000/2-1;	/* Timer0: 4kHz sound (OC0A) */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	TCCR0A = 0b10000010; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	TCCR0B = 0b00000011; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	ACSR = _BV(ACD);		/* Disable analog comp */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	/* ADC */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	ADMUX = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	/* uart1 (debug) */	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	uart1_init(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	sei(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -299,9 +278,7 @@ int main (void) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	ioinit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#ifdef LEDR_UART 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	xdev_out(xmit); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#endif 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	xdev_out(uart1_put); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	err = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	for (;;) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -309,7 +286,7 @@ int main (void) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		err = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		/* Wait for supply voltage stabled */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		while (FLAGS & F_LVD) ; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		while (FLAGS & F_LVD) {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		DELAY_MS(500); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if (FLAGS & F_LVD) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if (disk_status(0) & STA_NODISK) continue; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -326,8 +303,8 @@ int main (void) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		GPS_ON();		/* GPS power on */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		FLAGS |= F_POW; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		DELAY_MS(300);	/* Delay */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		uart_init();	/* Enable UART */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		xfprintf(uart_put, PSTR("$PSRF106,21*0F\r\n"));	/* Send initialization command (depends on the receiver) */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		uart0_init();	/* Enable UART */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//		xfprintf(uart0_put, PSTR("$PSRF106,21*0F\r\n"));	/* Send initialization command (depends on the receiver) */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		utc = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		for (;;) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -365,7 +342,7 @@ int main (void) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		/* Stop GPS receiver */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		uart_deinit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		uart0_deinit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		GPS_OFF(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		FLAGS &= ~F_POW; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 |