main.c 3.41 KB
Newer Older
Rahix's avatar
Rahix committed
1
#include <stdio.h>
Rahix's avatar
Rahix committed
2
3
#include <stdlib.h>

4
5
#include <ff.h>

6
#include "max32665.h"
Rahix's avatar
Rahix committed
7
#include "uart.h"
8
#include "cdcacm.h"
9
#include "gpio.h"
Rahix's avatar
Rahix committed
10
11

#include "card10.h"
12
#include "pmic.h"
Rahix's avatar
Rahix committed
13
#include "leds.h"
Rahix's avatar
Rahix committed
14
#include "api/dispatcher.h"
q3k's avatar
q3k committed
15
#include "l0der/l0der.h"
16
#include "modules/modules.h"
17
#include "modules/log.h"
18
#include "modules/stream.h"
19
#include "modules/filesystem.h"
20
#include "api/interrupt-sender.h"
Rahix's avatar
Rahix committed
21

22
23
24
#include "Heart.h"
#include "gfx.h"
#include "display.h"
Rahix's avatar
Rahix committed
25
#include "card10-version.h"
schneider's avatar
schneider committed
26

Rahix's avatar
Rahix committed
27
28
#include "FreeRTOS.h"
#include "task.h"
Rahix's avatar
Rahix committed
29

30
TaskHandle_t dispatcher_task_id;
31

schneider's avatar
schneider committed
32
void vBleTask(void *pvParameters);
Rahix's avatar
Rahix committed
33

Rahix's avatar
Rahix committed
34
35
int main(void)
{
36
	LOG_INFO("startup", "Epicardium startup ...");
Rahix's avatar
Rahix committed
37
	LOG_INFO("startup", "Version " CARD10_VERSION);
38

Rahix's avatar
Rahix committed
39
	card10_init();
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifdef CARD10_DEBUG_CORE1
	LOG_WARN("startup", "Core 1 Debugger Mode");
	static const gpio_cfg_t swclk = {
		PORT_0,
		PIN_7,
		GPIO_FUNC_ALT3,
		GPIO_PAD_NONE,
	};
	static const gpio_cfg_t swdio = {
		PORT_0,
		PIN_6,
		GPIO_FUNC_ALT3,
		GPIO_PAD_NONE,
	};

	GPIO_Config(&swclk);
	GPIO_Config(&swdio);
#endif /* CARD10_DEBUG_CORE1 */
Rahix's avatar
Rahix committed
58

59
60
61
62
	gfx_copy_region_raw(
		&display_screen, 0, 0, 160, 80, 2, (const void *)(Heart)
	);
	gfx_update(&display_screen);
schneider's avatar
schneider committed
63

64
65
66
	/* TODO: Move this to its own function */
	SCB->SCR |= SCB_SCR_SEVONPEND_Msk;

67
68
69
70
	if (cdcacm_init() < 0) {
		LOG_ERR("startup", "USB-Serial unavailable");
	}

swym's avatar
swym committed
71
	fatfs_init();
72
	api_interrupt_init();
73
	stream_init();
74

75
	LOG_INFO("startup", "Initializing tasks ...");
Rahix's avatar
Rahix committed
76
77
78

	/* Serial */
	if (xTaskCreate(
Rahix's avatar
Rahix committed
79
80
81
82
83
84
		    vSerialTask,
		    (const char *)"Serial",
		    configMINIMAL_STACK_SIZE,
		    NULL,
		    tskIDLE_PRIORITY + 1,
		    NULL) != pdPASS) {
85
		LOG_CRIT("startup", "Failed to create %s task!", "Serial");
Rahix's avatar
Rahix committed
86
87
88
		abort();
	}

89
90
91
92
93
94
95
96
	/* PMIC */
	if (xTaskCreate(
		    vPmicTask,
		    (const char *)"PMIC",
		    configMINIMAL_STACK_SIZE,
		    NULL,
		    tskIDLE_PRIORITY + 1,
		    NULL) != pdPASS) {
97
		LOG_CRIT("startup", "Failed to create %s task!", "PMIC");
98
99
100
101
		abort();
	}

	/* API */
Rahix's avatar
Rahix committed
102
	if (xTaskCreate(
Rahix's avatar
Rahix committed
103
104
		    vApiDispatcher,
		    (const char *)"API Dispatcher",
swym's avatar
swym committed
105
		    configMINIMAL_STACK_SIZE * 16,
Rahix's avatar
Rahix committed
106
107
108
		    NULL,
		    tskIDLE_PRIORITY + 2,
		    &dispatcher_task_id) != pdPASS) {
q3k's avatar
q3k committed
109
110
111
112
113
		LOG_CRIT(
			"startup",
			"Failed to create %s task!",
			"API Dispatcher"
		);
Rahix's avatar
Rahix committed
114
115
116
		abort();
	}

schneider's avatar
schneider committed
117
	/* BLE */
118
	if (xTaskCreate(
schneider's avatar
schneider committed
119
120
121
122
123
124
125
		    vBleTask,
		    (const char *)"BLE",
		    configMINIMAL_STACK_SIZE * 10,
		    NULL,
		    tskIDLE_PRIORITY + 1,
		    NULL) != pdPASS) {
		LOG_CRIT("startup", "Failed to create %s task!", "BLE");
126
127
128
		abort();
	}

129
	LOG_INFO("startup", "Initializing dispatcher ...");
Rahix's avatar
Rahix committed
130
131
	api_dispatcher_init();

132
133
134
135
	/* light sensor */
	LOG_INFO("startup", "starting light sensor ...");
	epic_light_sensor_run();

136
137
	core1_boot();

138
139
140
141
142
143
144
145
146
147
148
149
	/*
	 * See if there's a l0dable.elf to run. If not, run pycardium.
	 * This is temporary until epicardium gets a l0dable API from pycardium.
	 */
	const char *l0dable = "l0dable.elf";
	if (f_stat(l0dable, NULL) == FR_OK) {
		LOG_INFO("startup", "Running %s ...", l0dable);
		struct l0dable_info info;
		int res = l0der_load_path(l0dable, &info);
		if (res != 0) {
			LOG_ERR("startup", "l0der failed: %d\n", res);
		} else {
q3k's avatar
q3k committed
150
151
152
			LOG_INFO(
				"startup", "Starting %s on core1 ...", l0dable
			);
153
			core1_load(info.isr_vector, "");
154
		}
q3k's avatar
q3k committed
155
	} else {
156
157
		LOG_INFO("startup", "Starting pycardium on core 1 ...");
		core1_load((void *)0x10080000, "main.py");
q3k's avatar
q3k committed
158
159
	}

160
	LOG_INFO("startup", "Starting FreeRTOS ...");
Rahix's avatar
Rahix committed
161
	vTaskStartScheduler();
162
163
164

	LOG_CRIT("startup", "FreeRTOS did not start due to unknown error!");
	abort();
Rahix's avatar
Rahix committed
165
}