From: Leo C Date: Sun, 24 Aug 2014 21:22:58 +0000 (+0200) Subject: use env var 'baudrate' X-Git-Tag: hexrel-2~15 X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/commitdiff_plain/f14850dbf6369dc42df913c407bda51efb1c620c use env var 'baudrate' --- diff --git a/avr/Tupfile b/avr/Tupfile index d0bed48..61cb577 100644 --- a/avr/Tupfile +++ b/avr/Tupfile @@ -14,7 +14,7 @@ SRC_Z = ../z180/hdrom.c MCU_TARGET = atmega1281 F_CPU = 18432000UL -DEFS = -DF_CPU=$(F_CPU) -DBAUD=115200UL +DEFS = -DF_CPU=$(F_CPU) INCLUDES += -I$(TOP)/include diff --git a/avr/cmd_date.c b/avr/cmd_date.c index bc93efc..2396783 100644 --- a/avr/cmd_date.c +++ b/avr/cmd_date.c @@ -31,26 +31,21 @@ command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) switch (argc) { case 2: /* set date & time */ - if (strcmp_P(argv[1],PSTR("reset")) == 0) { - my_puts_P(PSTR("Reset RTC...\n")); - rtc_reset (); - } else { - /* initialize tm with current time */ - rcode = rtc_get (&tm); - - if(!rcode) { - /* insert new date & time */ - if (mk_date (argv[1], &tm) != 0) { - my_puts_P(PSTR("## Bad date format\n")); - break; - } - /* and write to RTC */ - rcode = rtc_set (&tm); - if(rcode) - my_puts_P(PSTR("## Set date failed\n")); - } else { - my_puts_P(PSTR("## Get date failed\n")); + /* initialize tm with current time */ + rcode = rtc_get (&tm); + + if(!rcode) { + /* insert new date & time */ + if (mk_date (argv[1], &tm) != 0) { + my_puts_P(PSTR("## Bad date format\n")); + break; } + /* and write to RTC */ + rcode = rtc_set (&tm); + if(rcode) + my_puts_P(PSTR("## Set date failed\n")); + } else { + my_puts_P(PSTR("## Get date failed\n")); } /* FALL TROUGH */ case 1: /* get date & time */ @@ -60,7 +55,7 @@ command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) my_puts_P(PSTR("## Get date failed\n")); break; } - /* TODO: flash */ + /* TODO: put weekdays[] in flash */ printf_P(PSTR("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n"), tm.tm_year, tm.tm_mon, tm.tm_mday, (tm.tm_wday<0 || tm.tm_wday>6) ? diff --git a/avr/i2c.c b/avr/i2c.c index 3ab222a..5d414af 100644 --- a/avr/i2c.c +++ b/avr/i2c.c @@ -13,9 +13,11 @@ #include "debug.h" #include "i2c.h" -#ifdef DEBUG -//# define DEBUG_I2C -#endif +#define DEBUG_I2C 0 + +#define debug_i2c(fmt, args...) \ + debug_cond(DEBUG_I2C, fmt, ##args) + /* General TWI Master status codes */ #define TWI_START 0x08 /* START has been transmitted */ @@ -205,9 +207,7 @@ ISR(TWI_vect) xmit.stat = tmp_stat; xmit.idx = tmp_idx; -#ifdef DEBUG_I2C - debug("|%02x", twsr); -#endif + debug_i2c("|%02x", twsr); TWCR = next_twcr; } @@ -226,7 +226,7 @@ static void _init(void) /* (Reset TWI hardware state machine.) */ TWCR = TWI_C_DISABLE; _delay_us(5); -#ifdef DEBUG_I2C +#if DEBUG_I2C memset((void *) xmit.buf, 0xdf, sizeof(xmit.buf)); #endif @@ -248,10 +248,6 @@ void i2c_init(uint32_t speed) debug_cond((twps > 3), "*** TWCLK too low: %lu Hz\n", speed); twbr = (uint8_t) tmp_twbr; - - debug("*** i2c_init: i2c_speed: %lu, twbr: %u, twps: %u\n", - speed, twbr, twps); - _init(); } @@ -270,14 +266,14 @@ int_fast8_t i2c_waitready(void) xmit.stat |= timeout; -#ifdef DEBUG_I2C +#if DEBUG_I2C dump_ram((uint8_t *) &xmit, 4, "=== i2c_wait ready: (done)"); _delay_ms(30); #endif return xmit.stat; } -//static +static int i2c_send(uint8_t chip, uint16_t addr, uint8_t alen, uint8_t *buffer, int8_t len) { uint8_t i, n; @@ -297,7 +293,7 @@ int i2c_send(uint8_t chip, uint16_t addr, uint8_t alen, uint8_t *buffer, int8_t xmit.buf[i] = *buffer++; xmit.len = i; -#ifdef DEBUG_I2C +#if DEBUG_I2C dump_ram((uint8_t *) &xmit, 0x20, "=== i2c_send"); _delay_ms(30); #endif @@ -309,7 +305,7 @@ int i2c_send(uint8_t chip, uint16_t addr, uint8_t alen, uint8_t *buffer, int8_t return rc; } -//static +static int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len) { uint8_t rc; @@ -322,7 +318,7 @@ int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len) xmit.len = len + 1; xmit.buf[0] = (chip<<1) | 1; -#ifdef DEBUG_I2C +#if DEBUG_I2C dump_ram((uint8_t *) &xmit, 0x20, "=== i2c_recv: before start"); _delay_ms(30); #endif @@ -330,7 +326,7 @@ int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len) TWCR = (1< #include "debug.h" @@ -11,6 +9,11 @@ #include "rtc.h" #include "i2c.h" +#define DEBUG_RTC 0 + +#define debug_rtc(fmt, args...) \ + debug_cond(DEBUG_RTC, fmt, ##args) + #define REG_CS 0x00 /* control/status */ #define REG_CSEC 0x01 /* hundredth of a second */ #define REG_SEC 0x02 /* seconds */ @@ -45,7 +48,7 @@ int rtc_get (struct rtc_time *tmp) i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS); i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2); - debug("Get RTC year: %u, year/date: %02x, wdays/month: %02x, " + debug_rtc("Get RTC year: %u, year/date: %02x, wdays/month: %02x, " "hour: %02x, min: %02x, sec: %02x, (stat: %02x)\n", year, rtcbuf[6], rtcbuf[5], rtcbuf[4], rtcbuf[3], rtcbuf[2], rtcbuf[0]); @@ -64,7 +67,7 @@ int rtc_get (struct rtc_time *tmp) tmp->tm_isdst= 0; - debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", + debug_rtc( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); @@ -75,7 +78,7 @@ int rtc_set (struct rtc_time *tmp) { uint8_t rtcbuf[NR_OF_REGS]; - debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", + debug_rtc("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); @@ -95,12 +98,3 @@ int rtc_set (struct rtc_time *tmp) return 0; } - -void rtc_reset (void) -{ - uint8_t c = 0; - - i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, &c, 1); -} - - diff --git a/avr/serial.c b/avr/serial.c index b2fea51..a5e2846 100644 --- a/avr/serial.c +++ b/avr/serial.c @@ -36,7 +36,7 @@ uint8_t tx_ring_buffer[BUFFER_SIZE]; /* Initialize UART */ -void usart0_setup(void) { +void usart0_setup(unsigned long baud) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { @@ -48,7 +48,7 @@ void usart0_setup(void) { ring_init(&tx_ring, tx_ring_buffer, BUFFER_SIZE); UCSR0A = 0; - UBRR0 = F_CPU / BAUD / 16 - 1; + UBRR0 = F_CPU / baud / 16 - 1; UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); UCSR0C = 3 << UCSZ00; }; @@ -82,10 +82,10 @@ ISR(USART0_UDRE_vect) /*--------------------------------------------------------------------------*/ -void serial_setup(void) +void serial_setup(unsigned long baud) { stdout = &mystdout; - usart0_setup(); + usart0_setup(baud); } /*--------------------------------------------------------------------------*/ diff --git a/include/config.h b/include/config.h index 287a0c5..396fd59 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,8 @@ #define CONFIG_ENV_OFFSET 0 #define CONFIG_ENVVAR_MAX 20 -#define CONFIG_POWRON_DELAY 2000 /* ms to wait after power on */ +#define CONFIG_BAUDRATE 115200L +#define CONFIG_PWRON_DELAY 2000 /* ms to wait after power on */ #define CONFIG_BOOTDELAY 4 //#define CONFIG_ZERO_BOOTDELAY_CHECK 1 diff --git a/include/rtc.h b/include/rtc.h index 15215a9..51ee424 100644 --- a/include/rtc.h +++ b/include/rtc.h @@ -39,7 +39,6 @@ struct rtc_time { int rtc_get (struct rtc_time *); int rtc_set (struct rtc_time *); -void rtc_reset (void); void GregorianDay (struct rtc_time *); void to_tm (unsigned long, struct rtc_time *); diff --git a/include/serial.h b/include/serial.h index 7414ef1..40ac815 100644 --- a/include/serial.h +++ b/include/serial.h @@ -1,7 +1,7 @@ #ifndef SERIAL_H #define SERIAL_H -void serial_setup(void); +void serial_setup(unsigned long baud); void serial_putc(char); int serial_getc(void); uint_fast8_t serial_tstc(void);