main.c 3.19 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"
Rahix's avatar
Rahix committed
9
10

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

21
22
23
#include "Heart.h"
#include "gfx.h"
#include "display.h"
schneider's avatar
schneider committed
24

Rahix's avatar
Rahix committed
25
26
#include "FreeRTOS.h"
#include "task.h"
Rahix's avatar
Rahix committed
27

28
TaskHandle_t dispatcher_task_id;
29

schneider's avatar
schneider committed
30
void vBleTask(void *pvParameters);
Rahix's avatar
Rahix committed
31
32
33
34
35

/*
 * API dispatcher task.  This task will sleep until an API call is issued and
 * then wake up to dispatch it.
 */
Rahix's avatar
Rahix committed
36
void vApiDispatcher(void *pvParameters)
Rahix's avatar
Rahix committed
37
{
38
	LOG_DEBUG("dispatcher", "Ready.");
Rahix's avatar
Rahix committed
39
	while (1) {
Rahix's avatar
Rahix committed
40
41
42
43
		if (api_dispatcher_poll()) {
			api_dispatcher_exec();
		}
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
Rahix's avatar
Rahix committed
44
	}
Rahix's avatar
Rahix committed
45
}
Rahix's avatar
Rahix committed
46
47
48

int main(void)
{
49
50
	LOG_INFO("startup", "Epicardium startup ...");

Rahix's avatar
Rahix committed
51
52
	card10_init();
	card10_diag();
Rahix's avatar
Rahix committed
53

54
55
56
57
	gfx_copy_region_raw(
		&display_screen, 0, 0, 160, 80, 2, (const void *)(Heart)
	);
	gfx_update(&display_screen);
schneider's avatar
schneider committed
58

59
60
61
	/* TODO: Move this to its own function */
	SCB->SCR |= SCB_SCR_SEVONPEND_Msk;

62
63
64
65
	if (cdcacm_init() < 0) {
		LOG_ERR("startup", "USB-Serial unavailable");
	}

swym's avatar
swym committed
66
	fatfs_init();
67
	api_interrupt_init();
68
	stream_init();
69

70
	LOG_INFO("startup", "Initializing tasks ...");
Rahix's avatar
Rahix committed
71
72
73

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

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

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

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

124
	LOG_INFO("startup", "Initializing dispatcher ...");
Rahix's avatar
Rahix committed
125
126
	api_dispatcher_init();

127
128
129
130
131
132
133
134
135
136
137
138
	/*
	 * 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
139
140
141
			LOG_INFO(
				"startup", "Starting %s on core1 ...", l0dable
			);
142
143
			core1_start(info.isr_vector);
		}
q3k's avatar
q3k committed
144
	} else {
q3k's avatar
q3k committed
145
146
		LOG_INFO("startup", "Starting pycardium on core1 ...");
		core1_start((void *)0x10080000);
q3k's avatar
q3k committed
147
148
	}

149
	LOG_INFO("startup", "Starting FreeRTOS ...");
Rahix's avatar
Rahix committed
150
	vTaskStartScheduler();
151
152
153

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