]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
7 #include <util/delay.h>
8 #include <avr/interrupt.h>
16 #include "con-utils.h"
21 #include "z180-serv.h"
23 #define udelay(n) _delay_us(n)
27 /*--------------------------------------------------------------------------*/
30 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
31 void preset_ram (void)
33 for (uint8_t *p
= RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
38 static const FLASH
char * const FLASH rreasons
[] = {
47 void print_reset_reason(void)
49 uint8_t r
= mcusr
& 0x1f;
50 const FLASH
char * const FLASH
*p
= rreasons
;
52 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
53 for ( ; r
; p
++, r
>>= 1) {
60 printf_P(PSTR(".\n"));
67 Stat
|= S_MSG_PENDING
;
72 Stat
|= S_CON_PENDING
;
78 /* save and clear reset reason(s) */
79 /* TODO: move to init section? */
87 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
91 /* Disable peripherals. Enable individually in respective init function. */
93 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
94 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
96 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
97 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
100 /* disable analog comparator */
109 PRR1
&= ~_BV(PRTIM3
);
110 OCR3A
= F_CPU
/ 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
111 TCCR3B
= (0b01<<WGM32
)|(0b001<<CS30
); /* CTC Mode, Prescaler 1 */
112 TIMSK3
= _BV(OCIE3A
); /* Enable TC2.oca interrupt */
114 /* INT5, INT6: falling edge */
115 EICRB
= (EICRB
& ~((0b11 << ISC50
) | (0b11 << ISC60
))) |
116 (0b10 << ISC50
) | (0b10 << ISC60
);
117 /* Reset pending ints */
118 EIFR
|= _BV(INTF5
) | _BV(INTF6
);
119 /* Enable INT5, and INT6 */
120 EIMSK
|= _BV(INT5
) | _BV(INT6
);
124 int reset_reason_is_power_on(void)
126 return (mcusr
& _BV(PORF
)) != 0;
129 /*--------------------------------------------------------------------------*/
131 /* Stored value of bootdelay, used by autoboot_command() */
132 static int stored_bootdelay
;
135 /***************************************************************************
136 * Watch for 'delay' seconds for autoboot stop.
137 * returns: 0 - no key, allow autoboot
141 static int abortboot(int bootdelay
)
147 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
149 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
151 * Check if key already pressed
152 * Don't check if bootdelay < 0
154 if (bootdelay
>= 0) {
155 if (tstc()) { /* we got a key press */
156 (void) my_getchar(1); /* consume input */
157 my_puts_P(PSTR("\b\b\b 0"));
158 abort
= 1; /* don't auto boot */
163 while ((bootdelay
> 0) && (!abort
)) {
168 if (tstc()) { /* we got a key press */
169 abort
= 1; /* don't auto boot */
170 bootdelay
= 0; /* no more delay */
174 } while (!abort
&& get_timer(ts
) < 1000);
176 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
185 const char *bootdelay_process(void)
190 bootdelay
= (int) getenv_ulong(PSTR(ENV_BOOTDELAY
), 10, CONFIG_BOOTDELAY
);
193 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
196 s
= getenv(PSTR(ENV_BOOTCMD
));
197 stored_bootdelay
= bootdelay
;
202 void autoboot_command(const char *s
)
204 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: PSTR("<UNDEFINED>"));
207 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
208 run_command_list(s
, -1);
218 s
= bootdelay_process();
231 if (reset_reason_is_power_on())
232 _delay_ms(CONFIG_PWRON_DELAY
);
234 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE
), 10, CONFIG_BAUDRATE
));
238 debug("\n=========================< (RE)START DEBUG >=========================\n");
239 print_reset_reason();
243 unsigned long i_speed
= getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK
);
244 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed
);
247 i2c_init(CONFIG_SYS_I2C_CLOCK
);
250 printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));