main.c 1.8 KB
Newer Older
Rahix's avatar
Rahix committed
1
#include <stdio.h>
Rahix's avatar
Rahix committed
2
3
#include <stdlib.h>

4
#include "max32665.h"
Rahix's avatar
Rahix committed
5
#include "uart.h"
6
#include "cdcacm.h"
Rahix's avatar
Rahix committed
7
8

#include "card10.h"
9
#include "pmic.h"
Rahix's avatar
Rahix committed
10
#include "leds.h"
Rahix's avatar
Rahix committed
11
#include "api/dispatcher.h"
12
#include "modules/serial.h"
Rahix's avatar
Rahix committed
13

Rahix's avatar
Rahix committed
14
15
#include "FreeRTOS.h"
#include "task.h"
Rahix's avatar
Rahix committed
16

17
TaskHandle_t dispatcher_task_id;
Rahix's avatar
Rahix committed
18
19
20
21
22

/*
 * 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
23
void vApiDispatcher(void *pvParameters)
Rahix's avatar
Rahix committed
24
{
Rahix's avatar
Rahix committed
25
	while (1) {
Rahix's avatar
Rahix committed
26
27
28
29
		if (api_dispatcher_poll()) {
			api_dispatcher_exec();
		}
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
Rahix's avatar
Rahix committed
30
	}
Rahix's avatar
Rahix committed
31
}
Rahix's avatar
Rahix committed
32

33
34
35
static void pmic_button(bool falling)
{
	if (falling) {
Rahix's avatar
Rahix committed
36
37
38
39
40
41
42
43
		printf("Resetting ...\n");
		/*
		 * Give the UART fifo time to clear.
		 * TODO: Do this properly
		 */
		for (int i = 0; i < 0x1000000; i++) {
			__asm volatile("nop");
		}
44
45
46
47
		MXC_GCR->rstr0 = MXC_F_GCR_RSTR0_SYSTEM;
	}
}

Rahix's avatar
Rahix committed
48
49
50
51
int main(void)
{
	card10_init();
	card10_diag();
Rahix's avatar
Rahix committed
52

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

56
57
	pmic_set_button_callback(pmic_button);

Rahix's avatar
Rahix committed
58
	cdcacm_init();
59

60
	printf("=> Initializing tasks ...\n");
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) {
Rahix's avatar
Rahix committed
70
71
72
73
74
		printf("Failed to create serial-comms task!\n");
		abort();
	}

	if (xTaskCreate(
Rahix's avatar
Rahix committed
75
76
77
78
79
80
		    vApiDispatcher,
		    (const char *)"API Dispatcher",
		    configMINIMAL_STACK_SIZE,
		    NULL,
		    tskIDLE_PRIORITY + 2,
		    &dispatcher_task_id) != pdPASS) {
Rahix's avatar
Rahix committed
81
82
83
84
		printf("Failed to create api dispatcher task!\n");
		abort();
	}

85
	printf("=> Initializing dispatcher ...\n");
Rahix's avatar
Rahix committed
86
87
	api_dispatcher_init();

88
	printf("=> Starting core1 payload ...\n");
Rahix's avatar
Rahix committed
89
	core1_start();
Rahix's avatar
Rahix committed
90

91
	printf("=> Starting FreeRTOS ...\n");
Rahix's avatar
Rahix committed
92
	vTaskStartScheduler();
Rahix's avatar
Rahix committed
93
	printf("ERROR: FreeRTOS did not start due to unknown error!\n");
Rahix's avatar
Rahix committed
94
}