1
0
فهرست منبع

Start adapting software for my hardware version

k4be 2 سال پیش
والد
کامیت
dfce56306b
11فایلهای تغییر یافته به همراه254 افزوده شده و 201 حذف شده
  1. 1 0
      soft/.gitignore
  2. 5 5
      soft/Makefile
  3. 35 58
      soft/main.c
  4. 24 0
      soft/main.h
  5. 0 103
      soft/suart.S
  6. 0 7
      soft/suart.h
  7. 0 14
      soft/uart.h
  8. 14 14
      soft/uart0.c
  9. 14 0
      soft/uart0.h
  10. 147 0
      soft/uart1.c
  11. 14 0
      soft/uart1.h

+ 1 - 0
soft/.gitignore

@@ -0,0 +1 @@
+obj/

+ 5 - 5
soft/Makefile

@@ -2,12 +2,12 @@
 PROJECT	= glg
 
 ### Source files and search directory
-CSRC    = main.c uart.c ff.c mmc.c
-ASRC    = suart.S xitoa.S stime.S
+CSRC    = main.c uart0.c uart1.c ff.c mmc.c
+ASRC    = xitoa.S stime.S
 VPATH   =
 
 ### Target device
-DEVICE  = atmega328p
+DEVICE  = atmega644pa
 
 ### Optimization level (0, 1, 2, 3, 4 or s)
 OPTIMIZE = s
@@ -19,7 +19,7 @@ CSTD = gnu89
 LIBS	=
 LIBDIRS	=
 INCDIRS	=
-DEFS	= F_CPU=9216000
+DEFS	= F_CPU=8000000
 # DEFS	+= LEDR_UART
 ADEFS	=
 
@@ -123,7 +123,7 @@ version :
 # Display size of file.
 size:
 	@echo
-	$(SIZE) -C --mcu=$(DEVICE) $(PROJECT).elf
+	$(SIZE) $(PROJECT).elf
 
 
 # Link: create ELF output file from object files.

+ 35 - 58
soft/main.c

@@ -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;
 

+ 24 - 0
soft/main.h

@@ -0,0 +1,24 @@
+#pragma once
+
+#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()		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	DELAY_MS(d)		{for (Timer = (BYTE)(d / 10); 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 */
+

+ 0 - 103
soft/suart.S

@@ -1,103 +0,0 @@
-;---------------------------------------------------------------------------;
-; Software implemented UART module                                          ;
-; (C)ChaN, 2005 (http://elm-chan.org/)                                      ;
-;---------------------------------------------------------------------------;
-; Bit rate settings:
-;
-;            1MHz  2MHz  4MHz  6MHz  8MHz  10MHz  12MHz  16MHz  20MHz
-;   2.4kbps   138     -     -     -     -      -      -      -      -
-;   4.8kbps    68   138     -     -     -      -      -      -      -
-;   9.6kbps    33    68   138   208     -      -      -      -      -
-;  19.2kbps     -    33    68   102   138    173    208      -      -
-;  38.4kbps     -     -    33    50    68     85    102    138    172
-;  57.6kbps     -     -    21    33    44     56     68     91    114
-; 115.2kbps     -     -     -     -    21     27     33     44     56
-
-.nolist
-#include <avr/io.h>
-.list
-
-#define	BPS	25	/* Bit delay. (see above table) */
-#define	BIDIR	0	/* 0:Separated Tx/Rx, 1:Shared Tx/Rx */
-#define	USE_RCV	0
-
-#define	OUT_1		sbi _SFR_IO_ADDR(PORTB), 1	/* Output mark */
-#define	OUT_0		cbi _SFR_IO_ADDR(PORTB), 1	/* Output space */
-#define	SKIP_IN_1	sbic _SFR_IO_ADDR(PINB), 1	/* Skip if mark */
-#define	SKIP_IN_0	sbis _SFR_IO_ADDR(PINB), 1	/* Skip if space */
-
-
-
-;---------------------------------------------------------------------------;
-; Transmit a byte in serial format of N81
-;
-;Prototype: void xmit (uint8_t data);
-;Size: 16 words
-
-.global xmit
-.func xmit
-xmit:
-#if BIDIR
-	ldi	r23, BPS-1	;Pre-idle time for bidirectional data line
-5:	dec	r23     	;
-	brne	5b		;/
-#endif
-	in	r0, _SFR_IO_ADDR(SREG)	;Save flags
-
-	com	r24		;C = start bit
-	ldi	r25, 10		;Bit counter
-	cli			;Start critical section
-
-1:	ldi	r23, BPS-1	;----- Bit transferring loop 
-2:	dec	r23     	;Wait for a bit time
-	brne	2b		;/
-	brcs	3f		;MISO = bit to be sent
-	OUT_1			;
-3:	brcc	4f		;
-	OUT_0			;/
-4:	lsr	r24     	;Get next bit into C
-	dec	r25     	;All bits sent?
-	brne	1b	     	;  no, coutinue
-
-	out	_SFR_IO_ADDR(SREG), r0	;End of critical section
-	ret
-.endfunc
-
-
-
-;---------------------------------------------------------------------------;
-; Receive a byte
-;
-;Prototype: uint8_t rcvr (void);
-;Size: 19 words
-#if USE_RCV
-
-.global rcvr
-.func rcvr
-rcvr:
-	in	r0, _SFR_IO_ADDR(SREG)	;Save flags
-
-	ldi	r24, 0x80	;Receiving shift reg
-	cli			;Start critical section
-
-1:	SKIP_IN_1		;Wait for idle
-	rjmp	1b
-2:	SKIP_IN_0		;Wait for start bit
-	rjmp	2b
-	ldi	r25, BPS/2	;Wait for half bit time
-3:	dec	r25
-	brne	3b
-
-4:	ldi	r25, BPS	;----- Bit receiving loop
-5:	dec	r25     	;Wait for a bit time
-	brne	5b		;/
-	lsr	r24     	;Next bit
-	SKIP_IN_0		;Get a data bit into r24.7
-	ori	r24, 0x80
-	brcc	4b	     	;All bits received?  no, continue
-
-	out	_SFR_IO_ADDR(SREG), r0	;End of critical section
-	ret
-.endfunc
-
-#endif

+ 0 - 7
soft/suart.h

@@ -1,7 +0,0 @@
-#ifndef SUART
-#define SUART
-
-void xmit (uint8_t);
-uint8_t rcvr (void);
-
-#endif	/* SUART */

+ 0 - 14
soft/uart.h

@@ -1,14 +0,0 @@
-#ifndef UART_DEFINED
-#define UART_DEFINED
-
-#include <stdint.h>
-#include <avr/io.h>
-#include <avr/interrupt.h>
-
-void uart_init(void);		/* Initialize UART and Flush FIFOs */
-void uart_deinit(void);		/* Deinitialize UART */
-uint8_t uart_get (void);	/* Get a byte from UART Rx FIFO */
-uint8_t uart_test(void);	/* Check number of data in UART Rx FIFO */
-void uart_put (uint8_t);	/* Put a byte into UART Tx FIFO */
-
-#endif

+ 14 - 14
soft/uart.c → soft/uart0.c

@@ -1,12 +1,12 @@
-/*------------------------------------------------*/
-/* UART functions for ATmega48/88/168/328         */
-/*------------------------------------------------*/
+/*---------------------------------------------------------*/
+/* UART functions for ATmega164A/PA/324A/PA/644A/PA/1284/P */
+/*---------------------------------------------------------*/
 
 
 #include <avr/interrupt.h>
-#include "uart.h"
+#include "uart0.h"
 
-#define	UART_BAUD		4800
+#define	UART0_BAUD		9600
 #define	USE_TXINT		0
 #define	SZ_FIFO			512
 
@@ -33,26 +33,26 @@ volatile FIFO TxFifo;
 
 /* Initialize UART */
 
-void uart_init (void)
+void uart0_init (void)
 {
 	UCSR0B = 0;
 
-	PORTD |= _BV(1); DDRD |= _BV(1);	/* Set TXD as output */
-	DDRD &= ~_BV(0); PORTD &= ~_BV(0); 	/* Set RXD as input */
+	PORTD |= _BV(PD1); DDRD |= _BV(PD1);	/* Set TXD as output */
+	DDRD &= ~_BV(PD0); PORTD &= ~_BV(PD0); 	/* Set RXD as input */
 
 	RxFifo.ct = 0; RxFifo.ri = 0; RxFifo.wi = 0;
 #if USE_TXINT
 	TxFifo.ct = 0; TxFifo.ri = 0; TxFifo.wi = 0;
 #endif
 
-	UBRR0L = F_CPU / UART_BAUD / 16 - 1;
+	UBRR0L = F_CPU / UART0_BAUD / 16 - 1;
 	UCSR0B = _BV(RXEN0) | _BV(RXCIE0) | _BV(TXEN0);
 }
 
 
 /* Deinitialize UART */
 
-void uart_deinit (void)
+void uart0_deinit (void)
 {
 	UCSR0B = 0;
 }
@@ -60,13 +60,13 @@ void uart_deinit (void)
 
 /* Get a received character */
 
-uint8_t uart_test (void)
+uint8_t uart0_test (void)
 {
 	return RxFifo.ct;
 }
 
 
-uint8_t uart_get (void)
+uint8_t uart0_get (void)
 {
 	uint8_t d;
 	idx_t i;
@@ -88,7 +88,7 @@ uint8_t uart_get (void)
 
 /* Put a character to transmit */
 
-void uart_put (uint8_t d)
+void uart0_put (uint8_t d)
 {
 #if USE_TXINT
 	idx_t i;
@@ -111,7 +111,7 @@ void uart_put (uint8_t d)
 
 
 /* USART0 RXC interrupt */
-ISR(USART_RX_vect)
+ISR(USART0_RX_vect)
 {
 	uint8_t d;
 	idx_t i;

+ 14 - 0
soft/uart0.h

@@ -0,0 +1,14 @@
+#ifndef UART0_DEFINED
+#define UART0_DEFINED
+
+#include <stdint.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+void uart0_init(void);		/* Initialize UART and Flush FIFOs */
+void uart0_deinit(void);		/* Deinitialize UART */
+uint8_t uart0_get (void);	/* Get a byte from UART Rx FIFO */
+uint8_t uart0_test(void);	/* Check number of data in UART Rx FIFO */
+void uart0_put (uint8_t);	/* Put a byte into UART Tx FIFO */
+
+#endif

+ 147 - 0
soft/uart1.c

@@ -0,0 +1,147 @@
+/*---------------------------------------------------------*/
+/* UART functions for ATmega164A/PA/324A/PA/644A/PA/1284/P */
+/*---------------------------------------------------------*/
+
+
+#include <avr/interrupt.h>
+#include "uart1.h"
+
+#define	UART1_BAUD		9600
+#define	USE_TXINT		0
+#define	SZ_FIFO			512
+
+
+#if SZ_FIFO >= 256
+typedef uint16_t	idx_t;
+#else
+typedef uint8_t		idx_t;
+#endif
+
+
+typedef struct {
+	idx_t wi, ri, ct;
+	uint8_t buff[SZ_FIFO];
+} FIFO;
+static
+volatile FIFO RxFifo;
+#if USE_TXINT
+static
+volatile FIFO TxFifo;
+#endif
+
+
+
+/* Initialize UART */
+
+void uart1_init (void)
+{
+	UCSR1B = 0;
+
+	PORTD |= _BV(PD3); DDRD |= _BV(PD3);	/* Set TXD as output */
+	DDRD &= ~_BV(PD2); PORTD &= ~_BV(PD2); 	/* Set RXD as input */
+
+	RxFifo.ct = 0; RxFifo.ri = 0; RxFifo.wi = 0;
+#if USE_TXINT
+	TxFifo.ct = 0; TxFifo.ri = 0; TxFifo.wi = 0;
+#endif
+
+	UBRR1L = F_CPU / UART1_BAUD / 16 - 1;
+	UCSR1B = _BV(RXEN1) | _BV(RXCIE1) | _BV(TXEN1);
+}
+
+
+/* Deinitialize UART */
+
+void uart1_deinit (void)
+{
+	UCSR1B = 0;
+}
+
+
+/* Get a received character */
+
+uint8_t uart1_test (void)
+{
+	return RxFifo.ct;
+}
+
+
+uint8_t uart1_get (void)
+{
+	uint8_t d;
+	idx_t i;
+
+
+	do {
+		cli(); i = RxFifo.ct; sei();
+	} while (i == 0) ;
+	i = RxFifo.ri;
+	d = RxFifo.buff[i];
+	cli();
+	RxFifo.ct--;
+	sei();
+	RxFifo.ri = (i + 1) % sizeof RxFifo.buff;
+
+	return d;
+}
+
+
+/* Put a character to transmit */
+
+void uart1_put (uint8_t d)
+{
+#if USE_TXINT
+	idx_t i;
+
+	do {
+		cli(); i = TxFifo.ct; sei();
+	} while (i >= sizeof TxFifo.buff);
+	i = TxFifo.wi;
+	TxFifo.buff[i] = d;
+	TxFifo.wi = (i + 1) % sizeof TxFifo.buff;
+	cli();
+	TxFifo.ct++;
+	UCSR1B = _BV(RXEN1)|_BV(RXCIE1)|_BV(TXEN1)|_BV(UDRIE1);
+	sei();
+#else
+	loop_until_bit_is_set(UCSR1A, UDRE1);
+	UDR1 = d;
+#endif
+}
+
+
+/* USART1 RXC interrupt */
+ISR(USART1_RX_vect)
+{
+	uint8_t d;
+	idx_t i;
+
+
+	d = UDR1;
+	i = RxFifo.ct;
+	if (i < sizeof RxFifo.buff) {
+		RxFifo.ct = ++i;
+		i = RxFifo.wi;
+		RxFifo.buff[i] = d;
+		RxFifo.wi = (i + 1) % sizeof RxFifo.buff;
+	}
+}
+
+
+#if USE_TXINT
+ISR(USART1_UDRE_vect)
+{
+	idx_t n, i;
+
+
+	n = TxFifo.ct;
+	if (n) {
+		TxFifo.ct = --n;
+		i = TxFifo.ri;
+		UDR1 = TxFifo.buff[i];
+		TxFifo.ri = (i + 1) % sizeof TxFifo.buff;
+	}
+	if (n == 0)
+		UCSR1B = _BV(RXEN1)|_BV(RXCIE1)|_BV(TXEN1);
+}
+#endif

+ 14 - 0
soft/uart1.h

@@ -0,0 +1,14 @@
+#ifndef UART1_DEFINED
+#define UART1_DEFINED
+
+#include <stdint.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+void uart1_init(void);		/* Initialize UART and Flush FIFOs */
+void uart1_deinit(void);		/* Deinitialize UART */
+uint8_t uart1_get (void);	/* Get a byte from UART Rx FIFO */
+uint8_t uart1_test(void);	/* Check number of data in UART Rx FIFO */
+void uart1_put (uint8_t);	/* Put a byte into UART Tx FIFO */
+
+#endif