main.c 2.79 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 "api/interrupt-sender.h"
Rahix's avatar
Rahix committed
19

schneider's avatar
schneider committed
20
21
22
#include <Heart.h>
#include "GUI_Paint.h"

Rahix's avatar
Rahix committed
23
24
#include "FreeRTOS.h"
#include "task.h"
Rahix's avatar
Rahix committed
25

26
TaskHandle_t dispatcher_task_id;
Rahix's avatar
Rahix committed
27
28
29
30
31

/*
 * 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
32
void vApiDispatcher(void *pvParameters)
Rahix's avatar
Rahix committed
33
{
34
	LOG_DEBUG("dispatcher", "Ready.");
Rahix's avatar
Rahix committed
35
	while (1) {
Rahix's avatar
Rahix committed
36
37
38
39
		if (api_dispatcher_poll()) {
			api_dispatcher_exec();
		}
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
Rahix's avatar
Rahix committed
40
	}
Rahix's avatar
Rahix committed
41
}
Rahix's avatar
Rahix committed
42
43
44

int main(void)
{
45
46
	LOG_INFO("startup", "Epicardium startup ...");

Rahix's avatar
Rahix committed
47
48
	card10_init();
	card10_diag();
Rahix's avatar
Rahix committed
49

schneider's avatar
schneider committed
50
51
52
	Paint_DrawImage(Heart, 0, 0, 160, 80);
	LCD_Update();

53
54
55
	/* TODO: Move this to its own function */
	SCB->SCR |= SCB_SCR_SEVONPEND_Msk;

56
57
58
59
	if (cdcacm_init() < 0) {
		LOG_ERR("startup", "USB-Serial unavailable");
	}

swym's avatar
swym committed
60
	fatfs_init();
61
	api_interrupt_init();
62
	stream_init();
63

64
	LOG_INFO("startup", "Initializing tasks ...");
Rahix's avatar
Rahix committed
65
66
67

	/* Serial */
	if (xTaskCreate(
Rahix's avatar
Rahix committed
68
69
70
71
72
73
		    vSerialTask,
		    (const char *)"Serial",
		    configMINIMAL_STACK_SIZE,
		    NULL,
		    tskIDLE_PRIORITY + 1,
		    NULL) != pdPASS) {
74
		LOG_CRIT("startup", "Failed to create %s task!", "Serial");
Rahix's avatar
Rahix committed
75
76
77
		abort();
	}

78
79
80
81
82
83
84
85
	/* PMIC */
	if (xTaskCreate(
		    vPmicTask,
		    (const char *)"PMIC",
		    configMINIMAL_STACK_SIZE,
		    NULL,
		    tskIDLE_PRIORITY + 1,
		    NULL) != pdPASS) {
86
		LOG_CRIT("startup", "Failed to create %s task!", "PMIC");
87
88
89
90
		abort();
	}

	/* API */
Rahix's avatar
Rahix committed
91
	if (xTaskCreate(
Rahix's avatar
Rahix committed
92
93
		    vApiDispatcher,
		    (const char *)"API Dispatcher",
swym's avatar
swym committed
94
		    configMINIMAL_STACK_SIZE * 16,
Rahix's avatar
Rahix committed
95
96
97
		    NULL,
		    tskIDLE_PRIORITY + 2,
		    &dispatcher_task_id) != pdPASS) {
98
		LOG_CRIT("startup", "Failed to create %s task!", "API Dispatcher");
Rahix's avatar
Rahix committed
99
100
101
		abort();
	}

102
	LOG_INFO("startup", "Initializing dispatcher ...");
Rahix's avatar
Rahix committed
103
104
	api_dispatcher_init();

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	/*
	 * 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 {
			LOG_INFO("startup", "Starting %s on core1 ...", l0dable);
			core1_start(info.isr_vector);
		}
q3k's avatar
q3k committed
120
	} else {
q3k's avatar
q3k committed
121
122
		LOG_INFO("startup", "Starting pycardium on core1 ...");
		core1_start((void *)0x10080000);
q3k's avatar
q3k committed
123
124
	}

Rahix's avatar
Rahix committed
125

126

127
	LOG_INFO("startup", "Starting FreeRTOS ...");
Rahix's avatar
Rahix committed
128
	vTaskStartScheduler();
129
130
131

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