dispatcher.c 1.01 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdlib.h>
#include "sema.h"
#include "api/dispatcher.h"

int api_dispatcher_init()
{
	int ret;

	ret = SEMA_Init(NULL);
	API_CALL_MEM->call_flag = _API_FLAG_IDLE;

	/*
	 * Enable TX events for both cores.
	 * TODO: Is this the right place?
	 */
	MXC_GCR->evten |= 0x24;

	return ret;
}

Rahix's avatar
Rahix committed
21
22
23
static bool event_ready = false;

bool api_dispatcher_poll_once()
Rahix's avatar
Rahix committed
24
{
Rahix's avatar
Rahix committed
25
26
27
28
	if (event_ready) {
		return false;
	}

Rahix's avatar
Rahix committed
29
30
31
32
	while (SEMA_GetSema(_API_SEMAPHORE) == E_BUSY) {}

	if (API_CALL_MEM->call_flag != _API_FLAG_CALLING) {
		SEMA_FreeSema(_API_SEMAPHORE);
Rahix's avatar
Rahix committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
		return false;
	}

	event_ready = true;
	return true;
}

bool api_dispatcher_poll()
{
	if (event_ready) {
		return true;
	}

	return api_dispatcher_poll_once();
}

api_id_t api_dispatcher_exec()
{
	if (!event_ready) {
Rahix's avatar
Rahix committed
52
53
54
		return 0;
	}

Rahix's avatar
Rahix committed
55
	api_id_t id = API_CALL_MEM->id;
Rahix's avatar
Rahix committed
56
57
58
	__api_dispatch_call(id, API_CALL_MEM->buffer);
	API_CALL_MEM->call_flag = _API_FLAG_RETURNED;

Rahix's avatar
Rahix committed
59
	event_ready = false;
Rahix's avatar
Rahix committed
60
61
62
63
64
65
66
67
	SEMA_FreeSema(_API_SEMAPHORE);

	/* Notify the caller that we returned */
	__SEV();
	__WFE();

	return id;
}