main.c 3.05 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;
27
28

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

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

int main(void)
{
47
48
	LOG_INFO("startup", "Epicardium startup ...");

Rahix's avatar
Rahix committed
49
50
	card10_init();
	card10_diag();
Rahix's avatar
Rahix committed
51

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

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

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

swym's avatar
swym committed
62
	fatfs_init();
63
	api_interrupt_init();
64
	stream_init();
65

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

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

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

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

108
    /* BLE */
109
110
111
	if (xTaskCreate(
		vBleTask,
		(const char*)"BLE",
112
		configMINIMAL_STACK_SIZE * 10,
113
		NULL,
114
115
		tskIDLE_PRIORITY  + 1,
        NULL
116
117
118
119
120
	) != pdPASS) {
		printf("Failed to create BLE task!\n");
		abort();
	}

121
	LOG_INFO("startup", "Initializing dispatcher ...");
Rahix's avatar
Rahix committed
122
123
	api_dispatcher_init();

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

146
	LOG_INFO("startup", "Starting FreeRTOS ...");
Rahix's avatar
Rahix committed
147
	vTaskStartScheduler();
148
149
150

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