]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
e4c98b8bd8f1fcfb730a649300f484c0b03b686e
7 #include <util/delay.h>
8 #include <avr/interrupt.h>
16 #include "con-utils.h"
22 #define udelay(n) _delay_us(n)
26 /*--------------------------------------------------------------------------*/
29 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
30 void preset_ram (void)
32 for (uint8_t *p
= RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
37 static const FLASH
char * const FLASH rreasons
[] = {
46 void print_reset_reason(void)
48 uint8_t r
= mcusr
& 0x1f;
49 const FLASH
char * const FLASH
*p
= rreasons
;
51 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
52 for ( ; r
; p
++, r
>>= 1) {
59 printf_P(PSTR(".\n"));
67 /* save and clear reset reason(s) */
68 /* TODO: move to init section? */
76 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
80 /* disable unused periphels */
81 PRR0
= _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRADC
);
82 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
83 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
85 /* disable analog comparator */
95 OCR1A
= F_CPU
/ 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
97 TIMSK1
= _BV(OCIE1A
); // Enable TC1.oca interrupt
101 int reset_reason_is_power_on(void)
103 return (mcusr
& _BV(PORF
)) != 0;
106 /*--------------------------------------------------------------------------*/
108 /* Stored value of bootdelay, used by autoboot_command() */
109 static int stored_bootdelay
;
112 /***************************************************************************
113 * Watch for 'delay' seconds for autoboot stop.
114 * returns: 0 - no key, allow autoboot
118 static int abortboot(int bootdelay
)
124 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
126 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
128 * Check if key already pressed
129 * Don't check if bootdelay < 0
131 if (bootdelay
>= 0) {
132 if (tstc()) { /* we got a key press */
133 (void) my_getchar(); /* consume input */
134 my_puts_P(PSTR("\b\b\b 0"));
135 abort
= 1; /* don't auto boot */
140 while ((bootdelay
> 0) && (!abort
)) {
145 if (tstc()) { /* we got a key press */
146 abort
= 1; /* don't auto boot */
147 bootdelay
= 0; /* no more delay */
151 } while (!abort
&& get_timer(ts
) < 1000);
153 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
162 const char *bootdelay_process(void)
167 bootdelay
= (int) getenv_ulong(PSTR("bootdelay"), 10, CONFIG_BOOTDELAY
);
170 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
173 s
= getenv(PSTR("bootcmd"));
174 stored_bootdelay
= bootdelay
;
179 void autoboot_command(const char *s
)
181 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: PSTR("<UNDEFINED>"));
184 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
185 run_command_list(s
, -1);
195 s
= bootdelay_process();
208 if (reset_reason_is_power_on())
209 _delay_ms(CONFIG_PWRON_DELAY
);
211 serial_setup(getenv_ulong(PSTR("baudrate"), 10, CONFIG_BAUDRATE
));
215 debug("\n=========================< (RE)START DEBUG >=========================\n");
216 print_reset_reason();
220 unsigned long i_speed
= getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK
);
221 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed
);
224 i2c_init(CONFIG_SYS_I2C_CLOCK
);
227 printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));