main.c 2.75 KB
Newer Older
1
#include "modules/modules.h"
2
#include "modules/log.h"
koalo's avatar
koalo committed
3
#include "modules/filesystem.h"
Rahix's avatar
Rahix committed
4
#include "card10-version.h"
schneider's avatar
schneider committed
5

Rahix's avatar
Rahix committed
6
7
#include "FreeRTOS.h"
#include "task.h"
Rahix's avatar
Rahix committed
8

9
#include <stdlib.h>
koalo's avatar
koalo committed
10
#include <string.h>
11

koalo's avatar
koalo committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define BLEMAXCFGBYTES 100
int bleShallStart(void)
{
	int bleConfigFile = epic_file_open("ble.txt", "r");
	if (bleConfigFile < 0) {
		LOG_INFO(
			"startup",
			"can not open ble.txt -> BLE is not started"
		);
		epic_file_close(bleConfigFile);
		return 0;
	}

	char cfgBuf[BLEMAXCFGBYTES + 1];
	int readNum = epic_file_read(bleConfigFile, cfgBuf, BLEMAXCFGBYTES);
	epic_file_close(bleConfigFile);
	if (readNum < 0) {
		LOG_INFO(
			"startup",
			"can not read ble.txt -> BLE is not started"
		);
		return 0;
	}
	cfgBuf[readNum] = '\0';

	char bleActiveStr[]              = "active=true";
	cfgBuf[sizeof(bleActiveStr) - 1] = '\0';

	if (strcmp(cfgBuf, "active=true") != 0) {
		LOG_INFO(
			"startup",
			"ble.txt is not \"active=true\" -> BLE is not started"
		);
		return 0;
	}

	LOG_INFO("startup", "ble.txt is \"active=true\" -> BLE is starting");
	return 1;
}

Rahix's avatar
Rahix committed
52
53
int main(void)
{
54
	LOG_INFO("startup", "Epicardium startup ...");
Rahix's avatar
Rahix committed
55
	LOG_INFO("startup", "Version " CARD10_VERSION);
56

57
	LOG_INFO("startup", "Initializing hardware ...");
58
59
	hardware_early_init();

60
	LOG_INFO("startup", "Initializing tasks ...");
Rahix's avatar
Rahix committed
61
62
63

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

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

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

schneider's avatar
schneider committed
102
	/* BLE */
koalo's avatar
koalo committed
103
104
105
106
107
108
109
110
111
112
113
	if (bleShallStart()) {
		if (xTaskCreate(
			    vBleTask,
			    (const char *)"BLE",
			    configMINIMAL_STACK_SIZE * 10,
			    NULL,
			    tskIDLE_PRIORITY + 1,
			    NULL) != pdPASS) {
			LOG_CRIT("startup", "Failed to create %s task!", "BLE");
			abort();
		}
114
115
	}

Rahix's avatar
Rahix committed
116
117
118
119
120
121
122
123
124
125
	/* 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
126
127
	}

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

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