From: Leo C Date: Sun, 24 Aug 2014 12:16:18 +0000 (+0200) Subject: getenv_ulong(), TWI int opt X-Git-Tag: hexrel-2~16 X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/commitdiff_plain/35e9ec0c03a150e3fe6a3350549573e4c02be67b getenv_ulong(), TWI int opt --- diff --git a/avr/env.c b/avr/env.c index 28437a4..7981116 100644 --- a/avr/env.c +++ b/avr/env.c @@ -775,28 +775,41 @@ int setenv_hex(const char *varname, unsigned long value) return setenv(varname, str); } + /** - * Get an environment variable as a hex value + * Decode the integer value of an environment variable and return it. * - * @param varname Environment variable to get - * @param default_val Return this, if variable does not exist - * @return hex value of variable or default_val + * @param name Name of environemnt variable + * @param base Number base to use (normally 10, or 16 for hex) + * @param default_val Default value to return if the variable is not + * found + * @return the decoded value, or default_val if not found */ -unsigned long getenv_hex(const char *varname, unsigned long default_val) +unsigned long getenv_ulong(const char *name, int base, unsigned long default_val) { - const char *s; + char buf[16]; unsigned long value; - char *endp; + char *vp, *endp; + + env_item_t *ep = envlist_search(name); - s = getenv(varname); - if (s) - value = strtoul(s, &endp, 16); - if (!s || endp == s) - return default_val; + if (ep != NULL ) { + if (ep->flags & EF_V_EEP) { + varval_get(buf, ep->val.eep, sizeof(buf)); + vp = buf; + } else + vp = ep->val.ram; + if (vp != NULL) { + value = strtoul(vp, &endp, base); + if (endp != vp) + return value; + } + } - return value; + return default_val; } + command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { (void) cmdtp; diff --git a/avr/i2c.c b/avr/i2c.c index df97fea..3ab222a 100644 --- a/avr/i2c.c +++ b/avr/i2c.c @@ -99,12 +99,17 @@ uint8_t buf[CONFIG_SYS_I2C_BUFSIZE]; } i2c_msg_t; -static i2c_msg_t xmit; +static volatile i2c_msg_t xmit; ISR(TWI_vect) { - uint8_t next_twcr; + uint8_t tmp_stat; uint8_t tmp_idx; + uint8_t next_twcr; + uint8_t n; + + tmp_idx = xmit.idx; + tmp_stat = xmit.stat; uint8_t twsr = TWSR; @@ -112,62 +117,59 @@ ISR(TWI_vect) case TWI_START: case TWI_REP_START: - xmit.idx = 0; /* reset xmit_buf index */ - xmit.stat = BUSY | START; + tmp_stat = BUSY | START; + tmp_idx = 0; /* reset xmit_buf index */ - tmp_idx = xmit.idx; if (tmp_idx < xmit.len) { /* all bytes transmited? */ TWDR = xmit.buf[tmp_idx]; - xmit.idx = ++tmp_idx; + ++tmp_idx; next_twcr = (1< 255) { - tmptwbr >>= 4; + while (tmp_twbr > 255) { + tmp_twbr >>= 4; twps += 1; } - debug_cond((twps > 3), "TWCLK too low: %lu Hz\n", 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); - twbr = (uint8_t) tmptwbr; _init(); } diff --git a/avr/main.c b/avr/main.c index 9103ddc..fb4e35c 100644 --- a/avr/main.c +++ b/avr/main.c @@ -19,26 +19,53 @@ #include "cli.h" #include "env.h" +#define udelay(n) _delay_us(n) + +static uint8_t mcusr; + /*--------------------------------------------------------------------------*/ #if DEBUG -void preset_ram (void) __attribute__ ((naked)) \ - __attribute__ ((section (".init3"))); -void -preset_ram (void) + +__attribute__ ((naked)) __attribute__ ((section (".init3"))) +void preset_ram (void) { for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++) *p = 0xdd; } -#endif -/*--------------------------------------------------------------------------*/ -static uint8_t mcusr; +static const FLASH char * const FLASH rreasons[] = { + FSTR("Power on"), + FSTR("External"), + FSTR("Brown out"), + FSTR("Watchdog"), + FSTR("JTAG"), + }; + +static +void print_reset_reason(void) +{ + uint8_t r = mcusr & 0x1f; + const FLASH char * const FLASH *p = rreasons; + + printf_P(PSTR("### Reset reason(s): %s"), r ? "" : "none"); + for ( ; r; p++, r >>= 1) { + if (r & 1) { + my_puts_P(*p); + if (r & ~1) + printf_P(PSTR(", ")); + } + } + printf_P(PSTR(".\n")); +} + +#endif static void setup_avr(void) { /* save and clear reset reason(s) */ + /* TODO: move to init section? */ mcusr = MCUSR; MCUSR = 0; @@ -70,36 +97,13 @@ void setup_avr(void) TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt } -static const FLASH char * const FLASH rreasons[] = { - FSTR("Power on"), - FSTR("External"), - FSTR("Brown out"), - FSTR("Watchdog"), - FSTR("JTAG"), - }; - static -void print_reset_reason(void) +int reset_reason_is_power_on(void) { - uint8_t r = mcusr & 0x1f; - const FLASH char * const FLASH *p = rreasons; - - printf_P(PSTR("Reset reason(s): %s"), r ? "" : "none"); - for ( ; r; p++, r >>= 1) { - if (r & 1) { - my_puts_P(*p); - if (r & ~1) - printf_P(PSTR(", ")); - } - } - printf_P(PSTR(".\n")); + return (mcusr & _BV(PORF)) != 0; } - -/*******************************************************************************/ - -#define udelay(n) _delay_us(n) - +/*--------------------------------------------------------------------------*/ /* Stored value of bootdelay, used by autoboot_command() */ static int stored_bootdelay; @@ -160,8 +164,7 @@ const char *bootdelay_process(void) char *s; int bootdelay; - s = getenv("bootdelay"); - bootdelay = s ? atoi(s) : CONFIG_BOOTDELAY; + bootdelay = (int) getenv_ulong("bootdelay", 10, CONFIG_BOOTDELAY); debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); @@ -199,6 +202,9 @@ int main(void) setup_avr(); z80_setup_bus(); + + if (reset_reason_is_power_on()) + _delay_ms(CONFIG_POWRON_DELAY); serial_setup(); sei(); @@ -209,7 +215,14 @@ int main(void) #endif env_init(); - i2c_init(34920); + +#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); + i2c_init(i_speed); +#else + i2c_init(CONFIG_SYS_I2C_CLOCK); +#endif printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n")); diff --git a/include/config.h b/include/config.h index 91b1fae..287a0c5 100644 --- a/include/config.h +++ b/include/config.h @@ -5,6 +5,7 @@ #define CONFIG_ENV_OFFSET 0 #define CONFIG_ENVVAR_MAX 20 +#define CONFIG_POWRON_DELAY 2000 /* ms to wait after power on */ #define CONFIG_BOOTDELAY 4 //#define CONFIG_ZERO_BOOTDELAY_CHECK 1 @@ -16,7 +17,7 @@ #define CONFIG_SYS_I2C_RTC_ADDR 0x50 #define CONFIG_SYS_I2C_BUFSIZE 64 -#define CONFIG_SYS_I2C_CLOCK 100000L /* 100kHz */ +#define CONFIG_SYS_I2C_CLOCK 100000L /* SCL clock frequency in Hz */ #define CONFIG_SYS_CBSIZE 250 #define CONFIG_SYS_ENV_NAMELEN 16 diff --git a/include/env.h b/include/env.h index 9d55273..0a12821 100644 --- a/include/env.h +++ b/include/env.h @@ -4,7 +4,11 @@ int env_init(void); char *getenv(const char *name); +unsigned long getenv_ulong(const char *name, int base, unsigned long default_val); + +#if defined(CONFIG_AUTO_COMPLETE) int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf); +#endif #endif /* ENV_H */