aboutsummaryrefslogtreecommitdiffstats
path: root/src/gos/gos_cmsis2.c
blob: b1dd1dd6b21d17d4541179a3fe167ff6892958f0 (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
/*
 * 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.io/license.html
 */

#include "../../gfx.h"
#include <string.h>

#if GFX_USE_OS_CMSIS2

#if !GFX_OS_NO_INIT && !GFX_OS_CALL_UGFXMAIN
	#error "GOS: Either GFX_OS_NO_INIT or GFX_OS_CALL_UGFXMAIN must be defined for CMSIS V2"
#endif

void _gosHeapInit(void);

void _gosInit(void)
{
	#if GFX_OS_NO_INIT && !GFX_OS_INIT_NO_WARNING
		#if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
			#warning "GOS: Operating System initialization has been turned off. Make sure you call osKernelInitialize()."
		#elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
			COMPILER_WARNING("GOS: Operating System initialization has been turned off. Make sure you call osKernelInitialize().")
		#endif
	#endif

	// Set up the heap allocator
	_gosHeapInit();
}

#if !GFX_OS_NO_INIT && GFX_OS_CALL_UGFXMAIN
	static DECLARE_THREAD_FUNCTION(startUGFX_CMSIS2, p) {
		(void) p;
		uGFXMain();
	}
#endif
void _gosPostInit(void)
{
	#if !GFX_OS_NO_INIT && GFX_OS_CALL_UGFXMAIN
		switch(osKernelGetState()) {
		case osKernelInactive:
			osKernelInitialize();
			/* Fall Through */
		case osKernelReady:
			gfxThreadCreate(0, GFX_OS_UGFXMAIN_STACKSIZE, gThreadpriorityNormal, startUGFX_CMSIS2, 0);
			osKernelStart();
			gfxHalt("Unable to start CMSIS V2 scheduler. Out of memory?");
			break;
		default:
			gfxThreadCreate(0, GFX_OS_UGFXMAIN_STACKSIZE, gThreadpriorityNormal, startUGFX_CMSIS2, 0);
			break;
		}
	#endif
}

void _gosDeinit(void)
{
}

void gfxMutexInit(gfxMutex* pmutex)
{
	*pmutex = osMutexNew(NULL);
}

void gfxSemInit(gfxSem* psem, gSemcount val, gSemcount limit)
{
	*psem = osSemaphoreNew(limit, val, NULL);
}

gBool gfxSemWait(gfxSem* psem, gDelay ms)
{
	if (osSemaphoreAcquire(*psem, gfxMillisecondsToTicks(ms)) == osOK)
		return gTrue;
	return gFalse;
}

gThread gfxThreadCreate(void* stackarea, size_t stacksz, gThreadpriority prio, DECLARE_THREAD_FUNCTION((*fn),p), void* param)
{
	osThreadAttr_t def;

	(void)stackarea;

	memset(&def, 0, sizeof(def));
	def.name = "uGFX";
	def.attr_bits = osThreadDetached;	// osThreadJoinable
	def.stack_mem = 0;
	def.stack_size = stacksz;
	def.priority = prio;
	//def.tz_module = ????;

	return osThreadNew((osThreadFunc_t)fn, param, &def);
}

gThreadreturn gfxThreadWait(gThread thread) {
	while(1) {
		switch(osThreadGetState(thread)) {
		case osThreadReady:
		case osThreadRunning:
		case osThreadBlocked:
			gfxYield();
			break;
		default:
			return;
		}
	}
}

#endif /* GFX_USE_OS_CMSIS2 */