aboutsummaryrefslogtreecommitdiffstats
path: root/src/gos/chibios.c
blob: 0b5011102c1a7b123228a09c058c90285968c76a (plain)
1
2
3
4
5
6
7
8
9
10
11
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://chibios-gfx.com/license.html
 */

/**
 * @file    src/gos/chibios.c
 * @brief   GOS ChibiOS Operating System support.
 */
#include "gfx.h"

#if GFX_USE_OS_CHIBIOS

#include <string.h>

#if !CH_USE_MUTEXES
	#error "GOS: CH_USE_MUTEXES must be defined in chconf.h"
#endif
#if !CH_USE_SEMAPHORES
	#error "GOS: CH_USE_SEMAPHORES must be defined in chconf.h"
#endif

/* Our module initialiser */
void _gosInit(void) {
	/* Don't initialise if the user already has */
	if (!chThdSelf()) {
		halInit();
		chSysInit();
	}
}

void *gfxRealloc(void *ptr, size_t oldsz, size_t newsz) {
	void *np;

	if (newsz <= oldsz)
		return ptr;

	np = gfxAlloc(newsz);
	if (!np)
		return 0;

	if (oldsz)
		memcpy(np, ptr, oldsz);

	return np;
}

void gfxSleepMilliseconds(delaytime_t ms) {
	switch(ms) {
	case TIME_IMMEDIATE:	chThdYield();				return;
	case TIME_INFINITE:		chThdSleep(TIME_INFINITE);	return;
	default:				chThdSleepMilliseconds(ms);	return;
	}
}

void gfxSleepMicroseconds(delaytime_t ms) {
	switch(ms) {
	case TIME_IMMEDIATE:								return;
	case TIME_INFINITE:		chThdSleep(TIME_INFINITE);	return;
	default:				chThdSleepMicroseconds(ms);	return;
	}
}
void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit) {
	if (val > limit) val = limit;
	psem->limit = limit;
	chSemInit(&psem->sem, val);
}
void gfxSemDestroy(gfxSem *psem) {
	chSemReset(&psem->sem, 1);
}
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms) {
	if (ms == TIME_INFINITE) {
		chSemWait(&psem->sem);
		return TRUE;
	}
	return chSemWaitTimeout(&psem->sem, MS2ST(ms)) != RDY_TIMEOUT;
}

void gfxSemSignal(gfxSem *psem) {
	chSysLock();
	if (gfxSemCounterI(psem) < psem->limit)
		chSemSignalI(&psem->sem);
	chSchRescheduleS();
	chSysUnlock();
}

void gfxSemSignalI(gfxSem *psem) {
	if (gfxSemCounterI(psem) < psem->limit)
		chSemSignalI(&psem->sem);
}

gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) {
	if (!stackarea) {
		if (!stacksz) stacksz = 256;
		return chThdCreateFromHeap(0, stacksz, prio, fn, param);
	}

	if (!stacksz) return NULL;
	return chThdCreateStatic(stackarea, stacksz, prio, fn, param);
}

#endif /* GFX_USE_OS_CHIBIOS */
/** @} */