main.c 2.87 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"
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
#include "gfx.h"
#include "display.h"
Rahix's avatar
Rahix committed
23
#include "card10-version.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

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

37
38
	hardware_early_init();

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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
57

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

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

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

Rahix's avatar
Rahix committed
69
70
71
	LOG_INFO("startup", "Initializing dispatcher ...");
	api_dispatcher_init();

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

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

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

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

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

126
127
128
129
	/* light sensor */
	LOG_INFO("startup", "starting light sensor ...");
	epic_light_sensor_run();

Rahix's avatar
Rahix committed
130
131
132
133
134
135
136
137
138
139
	/* Lifecycle */
	if (xTaskCreate(
		    vLifecycleTask,
		    (const char *)"Lifecycle",
		    configMINIMAL_STACK_SIZE * 4,
		    NULL,
		    tskIDLE_PRIORITY + 1,
		    NULL) != pdPASS) {
		LOG_CRIT("startup", "Failed to create %s task!", "Lifecycle");
		abort();
q3k's avatar
q3k committed
140
141
	}

142
	LOG_INFO("startup", "Starting FreeRTOS ...");
Rahix's avatar
Rahix committed
143
	vTaskStartScheduler();
144
145
146

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