aboutsummaryrefslogtreecommitdiffstats
path: root/src/gos/gos_ecos.c
blob: 16ce821bb4583bbe3c2d7666ae8c608b856c9b5f (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
106
107
108
109
110
111
112
113
114
115
/*
 * 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://ugfx.org/license.html
 */

#include "gfx.h"

#if GFX_USE_OS_ECOS

void _gosInit(void)
{
	#if !GFX_NO_OS_INIT
		#error "GOS: Operating System initialization for eCos is not yet implemented in uGFX. Please set GFX_NO_OS_INIT to TRUE in your gfxconf.h"
	#else
		#warning "GOS: Operating System initialization has been turned off. Make sure you call cyg_scheduler_start() before gfxInit() in your application!"
	#endif
}

void _gosDeinit(void)
{
	/* ToDo */
}

void gfxSleepMilliseconds(delaytime_t ms)
{
	switch(ms) {
		case TIME_IMMEDIATE:	cyg_thread_yield();								return;
		case TIME_INFINITE:		cyg_thread_suspend(cyg_thread_self());			return;
		default:				cyg_thread_delay(gfxMillisecondsToTicks(ms));	return;
	}
}

void gfxSleepMicroseconds(delaytime_t ms)
{
	switch(ms) {
		case TIME_IMMEDIATE:														return;
		case TIME_INFINITE:		cyg_thread_suspend(cyg_thread_self());				return;
		default:				cyg_thread_delay(gfxMillisecondsToTicks(ms/1000));	return;
	}
}

void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit)
{
	if (val > limit)
		val = limit;

	psem->limit = limit;
	cyg_semaphore_init(&psem->sem, val);
}

void gfxSemDestroy(gfxSem *psem)
{
	cyg_semaphore_destroy(&psem->sem);
}

bool_t gfxSemWait(gfxSem *psem, delaytime_t ms)
{
	switch(ms) {
	case TIME_IMMEDIATE:	return cyg_semaphore_trywait(&psem->sem);
	case TIME_INFINITE:		return cyg_semaphore_wait(&psem->sem);
	default:				return cyg_semaphore_timed_wait(&psem->sem, gfxMillisecondsToTicks(ms)+cyg_current_time());
	}
}

bool_t gfxSemWaitI(gfxSem *psem)
{
	return cyg_semaphore_trywait(&psem->sem);
}

void gfxSemSignal(gfxSem *psem)
{
	if (psem->limit == MAX_SEMAPHORE_COUNT)
		cyg_semaphore_post(&psem->sem);
	else {
		cyg_scheduler_lock();
		if (gfxSemCounterI(psem) < psem->limit)
			cyg_semaphore_post(&psem->sem);
		cyg_scheduler_unlock();
	}
}

void gfxSemSignalI(gfxSem *psem)
{
	if (psem->limit == MAX_SEMAPHORE_COUNT || gfxSemCounterI(psem) < psem->limit)
		cyg_semaphore_post(&psem->sem);
}

semcount_t gfxSemCounterI(gfxSem *psem) {
	semcount_t	cnt;

	cyg_semaphore_peek(&psem->sem, &cnt);
	return cnt;
}

gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param)
{
	gfxThreadHandle		th;

	if (!stackarea) {
		if (!stacksz) stacksz = CYGNUM_HAL_STACK_SIZE_TYPICAL;
		if (!(stackarea = gfxAlloc(stacksz+sizeof(cyg_thread))))
			return 0;
	}

	if (!stacksz)
		return 0;

    cyg_thread_create(prio, fn, param, "uGFX", (((cyg_thread *)stackarea)+1), stacksz, &th, (cyg_thread *)stackarea);
    cyg_thread_resume(th);
	return th;
}

#endif /* GFX_USE_OS_ECOS */