]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
0470feaba1a967dfae761f03c7f1c0c7a569454e
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 peripherals. Enable individually in respective init function. */
82 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
83 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
85 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
86 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
89 /* disable analog comparator */
99 OCR3A
= F_CPU
/ 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
100 TCCR3B
= (0b01<<WGM32
)|(0b001<<CS30
); /* CTC Mode, Prescaler 1 */
101 TIMSK3
= _BV(OCIE3A
); /* Enable TC2.oca interrupt */
105 int reset_reason_is_power_on(void)
107 return (mcusr
& _BV(PORF
)) != 0;
110 /*--------------------------------------------------------------------------*/
112 /* Stored value of bootdelay, used by autoboot_command() */
113 static int stored_bootdelay
;
116 /***************************************************************************
117 * Watch for 'delay' seconds for autoboot stop.
118 * returns: 0 - no key, allow autoboot
122 static int abortboot(int bootdelay
)
128 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
130 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
132 * Check if key already pressed
133 * Don't check if bootdelay < 0
135 if (bootdelay
>= 0) {
136 if (tstc()) { /* we got a key press */
137 (void) my_getchar(); /* consume input */
138 my_puts_P(PSTR("\b\b\b 0"));
139 abort
= 1; /* don't auto boot */
144 while ((bootdelay
> 0) && (!abort
)) {
149 if (tstc()) { /* we got a key press */
150 abort
= 1; /* don't auto boot */
151 bootdelay
= 0; /* no more delay */
155 } while (!abort
&& get_timer(ts
) < 1000);
157 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
166 const char *bootdelay_process(void)
171 bootdelay
= (int) getenv_ulong(PSTR("bootdelay"), 10, CONFIG_BOOTDELAY
);
174 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
177 s
= getenv(PSTR("bootcmd"));
178 stored_bootdelay
= bootdelay
;
183 void autoboot_command(const char *s
)
185 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: PSTR("<UNDEFINED>"));
188 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
189 run_command_list(s
, -1);
199 s
= bootdelay_process();
212 if (reset_reason_is_power_on())
213 _delay_ms(CONFIG_PWRON_DELAY
);
215 serial_setup(getenv_ulong(PSTR("baudrate"), 10, CONFIG_BAUDRATE
));
219 debug("\n=========================< (RE)START DEBUG >=========================\n");
220 print_reset_reason();
224 unsigned long i_speed
= getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK
);
225 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed
);
228 i2c_init(CONFIG_SYS_I2C_CLOCK
);
231 printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));