summaryrefslogtreecommitdiff
path: root/avr/main.c
blob: 86dcc50caa6c506476e333ed06434c5bb1509552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * SPDX-License-Identifier:	GPL-2.0
 */


#include "common.h"
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <stdlib.h>

#include "config.h"
#include "ff.h"
#include "z80-if.h"
#include "i2c.h"
#include "con-utils.h"
#include "serial.h"
#include "timer.h"
#include "cli.h"
#include "env.h"
#include "z180-serv.h"
#include "gpio.h"
#include "time.h"
#include "rtc.h"
#include "debug.h"

uint8_t mcusr __attribute__ ((section (".noinit")));

#if DEBUG
__attribute__ ((naked)) __attribute__ ((section (".init3")))
void preset_ram (void)
{
	for (uint8_t *p = (uint8_t *) RAMSTART; p <= (uint8_t *) RAMEND; p++)
		*p = 0xdd;

}
#endif

__attribute__ ((naked)) __attribute__ ((section (".init3")))
void get_mcusr (void)
{
	/* save and clear reset reason(s) */
	/* TODO: move to init section? */
	mcusr = MCUSR;
	MCUSR = 0;

	wdt_disable();
}

/*--------------------------------------------------------------------------*/
#if DEBUG

static const FLASH char * const FLASH rreasons[] = {
			FSTR("Power on"),
			FSTR("External"),
			FSTR("Brown out"),
			FSTR("Watchdog"),
			FSTR("JTAG"),
		};

static
void print_reset_reason(void)
{
	uint8_t r = mcusr & 0x1f;
	const FLASH char * const FLASH *p = rreasons;

	printf_P(PSTR("### Reset reason(s): %s"), r ? "" : "none");
	for ( ; r; p++, r >>= 1) {
		if (r & 1) {
			my_puts_P(*p);
			if (r & ~1)
				printf_P(PSTR(", "));
		}
	}
	printf_P(PSTR(".\n"));
}

#endif

ISR(INT5_vect)
{
	Stat |= S_MSG_PENDING;
}

ISR(INT6_vect)
{
	Stat |= S_CON_PENDING;
}

static
void setup_avr(void)
{
	/* CPU */

	/* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
	MCUCR = _BV(JTD);
	MCUCR = _BV(JTD);

	/* Disable peripherals. Enable individually in respective init function. */
	PRR0 = _BV(PRTWI) |
		_BV(PRTIM2) | _BV(PRTIM0) | _BV(PRTIM1) |
		_BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);

	PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
		_BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);


	/* disable analog comparator */
	ACSR = _BV(ACD);
	/* Ports */

	/* Clock */
	CLKPR = _BV(CLKPCE);
	CLKPR = 0;

	/* Timer */
	PRR1 &= ~_BV(PRTIM4);
	OCR4A = F_CPU / 1000 - 1;	/* Timer4: 1000Hz interval */
	TCCR4B = (0b00<<WGM42)|(0b001<<CS40); /* Normal Mode, Prescaler 1 */
	TIMSK4 = _BV(OCIE4A);		/* Enable Output Compare A interrupt */

	/* INT5, INT6: falling edge */
	EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
		(0b10 << ISC50) | (0b10 << ISC60);
	/* Reset pending ints */
	EIFR |= _BV(INTF5) | _BV(INTF6);
	/* Enable INT5, and INT6 */
	EIMSK |= _BV(INT5) | _BV(INT6);
}

static
int reset_reason_is_power_on(void)
{
	return (mcusr & _BV(PORF)) != 0;
}

static
void setup_system_time(void)
{
	struct tm rtc_time;

	rtc_get(&rtc_time);
	rtc_time.tm_isdst = 0;
	set_system_time(mk_gmtime(&rtc_time) );
}



static void setup_fatfs(void)
{
	static FATFS FatFs0;
	static FATFS FatFs1;

	f_mount(&FatFs0, "0:", 0);
	f_mount(&FatFs1, "1:", 0);
}

/*--------------------------------------------------------------------------*/

/* Stored value of bootdelay, used by autoboot_command() */
static int  stored_bootdelay;


/***************************************************************************
 * Watch for 'delay' seconds for autoboot stop.
 * returns: 0 - no key, allow autoboot
 *          1 - got key, abort
 */

static int abortboot(int  bootdelay)
{
	int abort = 0;
	uint32_t ts;

	if (bootdelay >= 0)
		printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);

#if defined CONFIG_ZERO_BOOTDELAY_CHECK
	/*
	 * Check if key already pressed
	 * Don't check if bootdelay < 0
	 */
	if (bootdelay >= 0) {
		if (tstc()) {	/* we got a key press	*/
			(void) my_getchar(1);  /* consume input	*/
			my_puts_P(PSTR("\b\b\b 0"));
			abort = 1;	/* don't auto boot	*/
		}
	}
#endif

	while ((bootdelay > 0) && (!abort)) {
		--bootdelay;
		/* delay 1000 ms */
		ts = get_timer(0);
		do {
			if (tstc()) {	/* we got a key press	*/
				abort  = 1;	/* don't auto boot	*/
				bootdelay = 0;	/* no more delay	*/
				break;
			}
			udelay(10000);
		} while (!abort && get_timer(ts) < 1000);

		printf_P(PSTR("\b\b\b%2d "), bootdelay);
	}

	putchar('\n');

	return abort;
}

static
const char *bootdelay_process(void)
{
	char *s;
	int bootdelay;

	bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);


	debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
	_delay_ms(20);

	s = getenv_str(PSTR(ENV_BOOTCMD));
	stored_bootdelay = bootdelay;
	return s;
}

static
void autoboot_command(const char *s)
{
	debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
	_delay_ms(20);

	if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
		run_command_list(s, -1);
	}
}


static
void main_loop(void)
{
	const char *s;

	s = bootdelay_process();
	autoboot_command(s);
	cli_loop();
}

int main(void)
{
	extern void setup_mmc(void);

	setup_avr();
	for (int i = 0; i < GPIO_MAX; i++)
		gpio_config(i, INPUT_PULLUP);
	setup_mmc();
	env_init();
	z80_setup_bus();

	if (reset_reason_is_power_on())
		_delay_ms(CONFIG_PWRON_DELAY);

	serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
	sei();

#if DEBUG
	debug("\n=========================<  (RE)START DEBUG  >=========================\n");
	print_reset_reason();
#endif

	i2c_init(CONFIG_SYS_I2C_CLOCK);
	setup_system_time();
	setup_fatfs();

	printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor - Version: " VERSION  " \n\n"));

	setup_z180_serv();

	main_loop();
}