MCU_TARGET = atmega1281
F_CPU = 18432000UL
-DEFS = -DF_CPU=$(F_CPU) -DBAUD=115200UL
+DEFS = -DF_CPU=$(F_CPU)
INCLUDES += -I$(TOP)/include
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 */
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) ?
#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 */
xmit.stat = tmp_stat;
xmit.idx = tmp_idx;
-#ifdef DEBUG_I2C
- debug("|%02x", twsr);
-#endif
+ debug_i2c("|%02x", twsr);
TWCR = next_twcr;
}
/* (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
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();
}
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;
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
return rc;
}
-//static
+static
int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len)
{
uint8_t rc;
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
TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA);
rc = i2c_waitready();
-#ifdef DEBUG_I2C
+#if DEBUG_I2C
dump_ram((uint8_t *) &xmit, 0x20, "=== i2c_recv: after completion");
_delay_ms(30);
#endif
i2c_send(chip, addr, alen, buffer, len);
rc = i2c_waitready();
- debug("** i2c_write: result=0x%02x\n",rc);
return (rc & XMIT_DONE) != 0;
}
i2c_send(chip, addr, alen, NULL, 0);
}
rc = i2c_recv(chip, buffer, len);
- debug("** i2c_read: result=0x%02x\n",rc);
return !((rc & (XMIT_DONE|DATA_ACK)) == (XMIT_DONE|DATA_ACK));
}
setup_avr();
z80_setup_bus();
+ env_init();
+
if (reset_reason_is_power_on())
- _delay_ms(CONFIG_POWRON_DELAY);
+ _delay_ms(CONFIG_PWRON_DELAY);
- serial_setup();
+ serial_setup(getenv_ulong("baudrate", 10, CONFIG_BAUDRATE));
sei();
#if DEBUG
print_reset_reason();
#endif
- env_init();
-
#if DEBUG
unsigned long i_speed = getenv_ulong("i2c_clock", 10, CONFIG_SYS_I2C_CLOCK);
debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed);
* Date & Time support for Philips PCF8583 RTC
*/
-/* #define DEBUG */
-
#include "common.h"
#include <stdlib.h>
#include "debug.h"
#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 */
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]);
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);
{
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);
return 0;
}
-
-void rtc_reset (void)
-{
- uint8_t c = 0;
-
- i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, &c, 1);
-}
-
-
/* Initialize UART */
-void usart0_setup(void) {
+void usart0_setup(unsigned long baud) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
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;
};
/*--------------------------------------------------------------------------*/
-void serial_setup(void)
+void serial_setup(unsigned long baud)
{
stdout = &mystdout;
- usart0_setup();
+ usart0_setup(baud);
}
/*--------------------------------------------------------------------------*/
#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
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 *);
#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);