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

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

29
TaskHandle_t dispatcher_task_id;
30

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

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

38
39
	hardware_early_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

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

78
	LOG_INFO("startup", "Initializing tasks ...");
Rahix's avatar
Rahix committed
79
80
81

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

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

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

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

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

Rahix's avatar
Rahix committed
136
137
138
139
140
141
142
143
144
145
	/* 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
146
147
	}

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

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