]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
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"
28 uint8_t mcusr
__attribute__ ((section (".noinit")));
31 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
32 void preset_ram (void)
34 for (uint8_t *p
= (uint8_t *) RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
40 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
43 /* save and clear reset reason(s) */
44 /* TODO: move to init section? */
51 /*--------------------------------------------------------------------------*/
54 static const FLASH
char * const FLASH rreasons
[] = {
63 void print_reset_reason(void)
65 uint8_t r
= mcusr
& 0x1f;
66 const FLASH
char * const FLASH
*p
= rreasons
;
68 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
69 for ( ; r
; p
++, r
>>= 1) {
76 printf_P(PSTR(".\n"));
83 Stat
|= S_MSG_PENDING
;
88 Stat
|= S_CON_PENDING
;
96 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
100 /* Disable peripherals. Enable individually in respective init function. */
102 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
103 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
105 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
106 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
109 /* disable analog comparator */
118 PRR1
&= ~_BV(PRTIM4
);
119 OCR4A
= F_CPU
/ 1000 - 1; /* Timer4: 1000Hz interval */
120 TCCR4B
= (0b00<<WGM42
)|(0b001<<CS40
); /* Normal Mode, Prescaler 1 */
121 TIMSK4
= _BV(OCIE4A
); /* Enable Output Compare A interrupt */
123 /* INT5, INT6: falling edge */
124 EICRB
= (EICRB
& ~((0b11 << ISC50
) | (0b11 << ISC60
))) |
125 (0b10 << ISC50
) | (0b10 << ISC60
);
126 /* Reset pending ints */
127 EIFR
|= _BV(INTF5
) | _BV(INTF6
);
128 /* Enable INT5, and INT6 */
129 EIMSK
|= _BV(INT5
) | _BV(INT6
);
133 int reset_reason_is_power_on(void)
135 return (mcusr
& _BV(PORF
)) != 0;
139 void setup_system_time(void)
144 rtc_time
.tm_isdst
= 0;
145 set_system_time(mk_gmtime(&rtc_time
) );
150 static void setup_fatfs(void)
155 f_mount(&FatFs0
, "0:", 0);
156 f_mount(&FatFs1
, "1:", 0);
159 /*--------------------------------------------------------------------------*/
161 /* Stored value of bootdelay, used by autoboot_command() */
162 static int stored_bootdelay
;
165 /***************************************************************************
166 * Watch for 'delay' seconds for autoboot stop.
167 * returns: 0 - no key, allow autoboot
171 static int abortboot(int bootdelay
)
177 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
179 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
181 * Check if key already pressed
182 * Don't check if bootdelay < 0
184 if (bootdelay
>= 0) {
185 if (tstc()) { /* we got a key press */
186 (void) my_getchar(1); /* consume input */
187 my_puts_P(PSTR("\b\b\b 0"));
188 abort
= 1; /* don't auto boot */
193 while ((bootdelay
> 0) && (!abort
)) {
198 if (tstc()) { /* we got a key press */
199 abort
= 1; /* don't auto boot */
200 bootdelay
= 0; /* no more delay */
204 } while (!abort
&& get_timer(ts
) < 1000);
206 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
215 const char *bootdelay_process(void)
220 bootdelay
= (int) getenv_ulong(PSTR(ENV_BOOTDELAY
), 10, CONFIG_BOOTDELAY
);
223 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
226 s
= getenv_str(PSTR(ENV_BOOTCMD
));
227 stored_bootdelay
= bootdelay
;
232 void autoboot_command(const char *s
)
234 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: "<UNDEFINED>");
237 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
238 run_command_list(s
, -1);
248 s
= bootdelay_process();
255 extern void setup_mmc(void);
258 for (int i
= 0; i
< GPIO_MAX
; i
++)
259 gpio_config(i
, INPUT_PULLUP
);
264 if (reset_reason_is_power_on())
265 _delay_ms(CONFIG_PWRON_DELAY
);
267 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE
), 10, CONFIG_BAUDRATE
));
271 debug("\n=========================< (RE)START DEBUG >=========================\n");
272 print_reset_reason();
275 i2c_init(CONFIG_SYS_I2C_CLOCK
);
279 printf_P(PSTR("\n" MCU_STRING
"+Z8S180 Stamp Monitor - Version: " VERSION
" \n\n"));