]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
1e5a6f97f94df4eb1610f8a8b3e0e2839904356f
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
9 #include <avr/interrupt.h>
17 #include "con-utils.h"
22 #include "z180-serv.h"
30 uint8_t mcusr
__attribute__ ((section (".noinit")));
33 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
34 void preset_ram (void)
36 for (uint8_t *p
= (uint8_t *) RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
42 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
45 /* save and clear reset reason(s) */
46 /* TODO: move to init section? */
53 /*--------------------------------------------------------------------------*/
56 static const FLASH
char * const FLASH rreasons
[] = {
65 void print_reset_reason(void)
67 uint8_t r
= mcusr
& 0x1f;
68 const FLASH
char * const FLASH
*p
= rreasons
;
70 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
71 for ( ; r
; p
++, r
>>= 1) {
78 printf_P(PSTR(".\n"));
85 Stat
|= S_MSG_PENDING
;
90 Stat
|= S_CON_PENDING
;
98 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
102 /* Disable peripherals. Enable individually in respective init function. */
104 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
105 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
107 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
108 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
111 /* disable analog comparator */
120 PRR1
&= ~_BV(PRTIM4
);
121 OCR4A
= F_CPU
/ 1000 - 1; /* Timer4: 1000Hz interval */
122 TCCR4B
= (0b00<<WGM42
)|(0b001<<CS40
); /* Normal Mode, Prescaler 1 */
123 TIMSK4
= _BV(OCIE4A
); /* Enable Output Compare A interrupt */
125 /* INT5, INT6: falling edge */
126 EICRB
= (EICRB
& ~((0b11 << ISC50
) | (0b11 << ISC60
))) |
127 (0b10 << ISC50
) | (0b10 << ISC60
);
128 /* Reset pending ints */
129 EIFR
|= _BV(INTF5
) | _BV(INTF6
);
130 /* Enable INT5, and INT6 */
131 EIMSK
|= _BV(INT5
) | _BV(INT6
);
135 int reset_reason_is_power_on(void)
137 return (mcusr
& _BV(PORF
)) != 0;
141 void setup_system_time(void)
146 rtc_time
.tm_isdst
= 0;
147 set_system_time(mk_gmtime(&rtc_time
) );
151 /*--------------------------------------------------------------------------*/
153 /* Stored value of bootdelay, used by autoboot_command() */
154 static int stored_bootdelay
;
157 /***************************************************************************
158 * Watch for 'delay' seconds for autoboot stop.
159 * returns: 0 - no key, allow autoboot
163 static int abortboot(int bootdelay
)
169 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
171 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
173 * Check if key already pressed
174 * Don't check if bootdelay < 0
176 if (bootdelay
>= 0) {
177 if (tstc()) { /* we got a key press */
178 (void) my_getchar(1); /* consume input */
179 my_puts_P(PSTR("\b\b\b 0"));
180 abort
= 1; /* don't auto boot */
185 while ((bootdelay
> 0) && (!abort
)) {
190 if (tstc()) { /* we got a key press */
191 abort
= 1; /* don't auto boot */
192 bootdelay
= 0; /* no more delay */
196 } while (!abort
&& get_timer(ts
) < 1000);
198 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
207 const char *bootdelay_process(void)
212 bootdelay
= (int) getenv_ulong(PSTR(ENV_BOOTDELAY
), 10, CONFIG_BOOTDELAY
);
215 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
218 s
= getenv_str(PSTR(ENV_BOOTCMD
));
219 stored_bootdelay
= bootdelay
;
224 void autoboot_command(const char *s
)
226 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: "<UNDEFINED>");
229 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
230 run_command_list(s
, -1);
240 s
= bootdelay_process();
247 extern void setup_mmc(void);
250 for (int i
= 0; i
< GPIO_MAX
; i
++)
251 gpio_config(i
, INPUT_PULLUP
);
256 if (reset_reason_is_power_on())
257 _delay_ms(CONFIG_PWRON_DELAY
);
259 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE
), 10, CONFIG_BAUDRATE
));
263 debug("\n=========================< (RE)START DEBUG >=========================\n");
264 print_reset_reason();
267 i2c_init(CONFIG_SYS_I2C_CLOCK
);
271 printf_P(PSTR("\n" MCU_STRING
"+Z8S180 Stamp Monitor - Version: " VERSION
" \n\n"));