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

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

#include "card10.h"
Rahix's avatar
Rahix committed
8
#include "leds.h"
Rahix's avatar
Rahix committed
9
#include "api/dispatcher.h"
Rahix's avatar
Rahix committed
10
#include "serial.h"
Rahix's avatar
Rahix committed
11

Rahix's avatar
Rahix committed
12
13
#include "FreeRTOS.h"
#include "task.h"
Rahix's avatar
Rahix committed
14

Rahix's avatar
Rahix committed
15
16
17
static TaskHandle_t dispatcher_task_id;

/* TODO: Move out of main.c */
Rahix's avatar
Rahix committed
18
void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b)
Rahix's avatar
Rahix committed
19
{
Rahix's avatar
Rahix committed
20
21
	leds_set(led, r, g, b);
	leds_update();
Rahix's avatar
Rahix committed
22
23
}

Rahix's avatar
Rahix committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * This hook is called before FreeRTOS enters tickless idle.
 */
void pre_idle_sleep(TickType_t xExpectedIdleTime)
{
	if (xExpectedIdleTime > 0) {
		/*
		 * WFE because the other core should be able to notify
		 * epicardium if it wants to issue an API call.
		 */
		__asm volatile( "dsb" ::: "memory" );
		__asm volatile( "wfe" );
		__asm volatile( "isb" );
	}
}

/*
 * This hook is called after FreeRTOS exits tickless idle.
 */
void post_idle_sleep(TickType_t xExpectedIdleTime)
{
	/* Check whether a new API call was issued. */
	if (api_dispatcher_poll_once()) {
		xTaskNotifyGive(dispatcher_task_id);
	}
}

#if 0
void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime)
{
	if (xExpectedIdleTime > 0) {
		__WFE();
		if (api_dispatcher_poll()) {
			xTaskNotifyGive(dispatcher_task_id);
		}
	}
}
#endif

/*
 * 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
67
void vApiDispatcher(void*pvParameters)
Rahix's avatar
Rahix committed
68
{
Rahix's avatar
Rahix committed
69
	while (1) {
Rahix's avatar
Rahix committed
70
71
72
73
		if (api_dispatcher_poll()) {
			api_dispatcher_exec();
		}
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
Rahix's avatar
Rahix committed
74
	}
Rahix's avatar
Rahix committed
75
}
Rahix's avatar
Rahix committed
76

Rahix's avatar
Rahix committed
77
78
79
80
void vApplicationGetIdleTaskMemory(
	StaticTask_t**ppxIdleTaskTCBBuffer,
	StackType_t**ppxIdleTaskStackBuffer,
	uint32_t *pulIdleTaskStackSize)
81
{
Rahix's avatar
Rahix committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
	/*
	 * If the buffers to be provided to the Idle task are declared inside this
	 * function then they must be declared static - otherwise they will be allocated on
	 * the stack and so not exists after this function exits.
	 */
	static StaticTask_t xIdleTaskTCB;
	static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];

	/*
	 * Pass out a pointer to the StaticTask_t structure in which the Idle task's
	 * ktate will be stored.
	 */
	*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;

	/* Pass out the array that will be used as the Idle task's stack. */
	*ppxIdleTaskStackBuffer = uxIdleTaskStack;

	/*
	 * Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
	 * Note that, as the array is necessarily of type StackType_t,
	 * configMINIMAL_STACK_SIZE is specified in words, not bytes.
	 */
	*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
105
106
}

Rahix's avatar
Rahix committed
107
108
109
110
int main(void)
{
	card10_init();
	card10_diag();
Rahix's avatar
Rahix committed
111

Rahix's avatar
Rahix committed
112
	cdcacm_init();
113

Rahix's avatar
Rahix committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	printf("Initializing tasks ...\n");

	/* Serial */
	if (xTaskCreate(
		vSerialTask,
		(const char*)"Serial",
		configMINIMAL_STACK_SIZE,
		NULL,
		tskIDLE_PRIORITY + 1,
		NULL
	) != pdPASS) {
		printf("Failed to create serial-comms task!\n");
		abort();
	}

	if (xTaskCreate(
		vApiDispatcher,
		(const char*)"API Dispatcher",
		configMINIMAL_STACK_SIZE,
		NULL,
		tskIDLE_PRIORITY  + 2,
Rahix's avatar
Rahix committed
135
		&dispatcher_task_id
Rahix's avatar
Rahix committed
136
137
138
139
140
	) != pdPASS) {
		printf("Failed to create api dispatcher task!\n");
		abort();
	}

Rahix's avatar
Rahix committed
141
142
143
	printf("Initializing dispatcher ...\n");
	api_dispatcher_init();

Rahix's avatar
Rahix committed
144
145
	printf("Staring core1 payload ...\n");
	core1_start();
Rahix's avatar
Rahix committed
146

Rahix's avatar
Rahix committed
147
	vTaskStartScheduler();
Rahix's avatar
Rahix committed
148
	printf("ERROR: FreeRTOS did not start due to unknown error!\n");
Rahix's avatar
Rahix committed
149
}