aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: b91a975ddfc41818ee1960f3b738d3362f1bbdd9 (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
#include <stm32l476xx.h>
#include <clock.h>
#include <heap.h>
#include <task.h>
#include <gpio.h>
#include <lcd.h>
#include <display.h>
#include <initrd.h>
#include <serial.h>
#include <parser.h>
#include <stack.h>

#include <stdlib.h>
#include <string.h>

void kmain(void);
void task_interpreter(void);

int main(void)
{
	asm("cpsid i");

	// prepare flash latency for 40MHz operation
	FLASH->ACR &= ~(FLASH_ACR_LATENCY);
	FLASH->ACR |= FLASH_ACR_LATENCY_4WS;

	//MPU->CTRL |= MPU_CTRL_ENABLE_Msk | MPU_CTRL_PRIVDEFENA_Msk;
	clock_init();
	gpio_init();

	gpio_mode(GPIOA, 5, OUTPUT);

	serial_init();

	// enable FPU
	SCB->CPACR |= (0xF << 20);

	task_init(kmain);

	while (1);
}

int script_puts(interpreter *it)
{
	char *s = igetarg_string(it, 0);
	//lcd_puts(s);
	asm("mov r0, %0; svc 2" :: "r" (s));
	return 0;
}

int script_delay(interpreter *it)
{
	int ms = igetarg_integer(it, 0);
	delay(ms);
	return 0;
}

void task_interpreter(void)
{
	interpreter it;
	iinit(&it);
	inew_cfunc(&it, "print", script_puts);
	inew_cfunc(&it, "delay", script_delay);

	char *s = initrd_getfile("init");
	if (s == 0)
		goto end;

	char *linebuf = (char *)malloc(120);
	uint32_t i = 0, prev = 0, lc;
	uint32_t size = initrd_getfilesize("init");
	int ret;
	while (i < size) {
		for (; s[i] != '\n' && s[i] != '\0'; i++);
		lc = i - prev;
		if (lc == 0) {
			prev = ++i;
			continue;
		}
		strncpy(linebuf, s + prev, lc + 1);
		linebuf[lc] = '\0';
		ret = idoline(&it, linebuf);
		if (ret < 0)
			break;
		prev = ++i;
	}

	if (ret < 0) {
		lcd_puts("Error: ");
		lcd_puts(itoa(ret, linebuf, 10));
	}
	free(linebuf);
	//iend(&it); // nah

end:
	while (1)
		delay(10);
}

void kmain(void)
{
	asm("cpsie i");

	dsp_init();
	//uint16_t c = 0x38;
	uint16_t c = 0;
	for (int i = 0; i < LCD_HEIGHT; i++) {
		dsp_set_addr(0, i, LCD_WIDTH - 1, i);
		int w = LCD_WIDTH - 1;
		do {
			dsp_write_data(c);//c >> 8);
			dsp_write_data(c);//c & 0xFF);
		} while (w--);
	}

	extern void dsp_puts(const char *);
	dsp_puts("Hello, world! My name is Clyne.");

	//task_start(lcd_handler, 128);
	//delay(200);
	//task_start(task_interpreter, 4096);

	//char *s = initrd_getfile("test.txt");

	while (1) {
		gpio_dout(GPIOA, 5, 1);
		delay(500);
		gpio_dout(GPIOA, 5, 0);
		delay(500);
	}
}