Browse Source

Another attempt to get CMSIS semaphores correct.

inmarket 4 years ago
parent
commit
267178254d
2 changed files with 17 additions and 9 deletions
  1. 14 7
      src/gos/gos_cmsis.c
  2. 3 2
      src/gos/gos_cmsis.h

+ 14 - 7
src/gos/gos_cmsis.c

@@ -43,9 +43,9 @@ void gfxSemInit(gfxSem* psem, semcount_t val, semcount_t limit)
43 43
 	osSemaphoreDef_t def;
44 44
 	def.semaphore = psem->semaphore;
45 45
 	
46
-	psem->id = osSemaphoreCreate(&def, limit);
47
-	while(val--)
48
-		osSemaphoreRelease(psem->id);
46
+	if (val > limit) val = limit;
47
+	psem->available = limit - val;
48
+	psem->id = osSemaphoreCreate(&def, val);
49 49
 }
50 50
 
51 51
 void gfxSemDestroy(gfxSem* psem)
@@ -55,22 +55,29 @@ void gfxSemDestroy(gfxSem* psem)
55 55
 
56 56
 bool_t gfxSemWait(gfxSem* psem, delaytime_t ms)
57 57
 {
58
-	return osSemaphoreWait(psem->id, ms) > 0;
58
+	if (osSemaphoreWait(psem->id, ms) == osOK) {
59
+		psem->available++;
60
+		return TRUE;
61
+	}
62
+	return FALSE;
59 63
 }
60 64
 
61 65
 bool_t gfxSemWaitI(gfxSem* psem)
62 66
 {
63
-	return osSemaphoreWait(psem->id, 0) > 0;
67
+	return gfxSemWait(psem, 0);
64 68
 }
65 69
 
66 70
 void gfxSemSignal(gfxSem* psem)
67 71
 {
68
-	osSemaphoreRelease(psem->id);
72
+	gfxSemSignalI(psem);
69 73
 }
70 74
 
71 75
 void gfxSemSignalI(gfxSem* psem)
72 76
 {
73
-	osSemaphoreRelease(psem->id);
77
+	if (psem->available) {
78
+		psem->available--;
79
+		osSemaphoreRelease(psem->id);
80
+	}
74 81
 }
75 82
 
76 83
 gfxThreadHandle gfxThreadCreate(void* stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void* param)

+ 3 - 2
src/gos/gos_cmsis.h

@@ -42,8 +42,9 @@ typedef osPriority			threadpriority_t;
42 42
 #define HIGH_PRIORITY		osPriorityHigh
43 43
 
44 44
 typedef struct gfxSem {
45
-	uint32_t semaphore[2];
46
-	osSemaphoreId id;
45
+	uint32_t		semaphore[2];
46
+	osSemaphoreId	id;
47
+	semcount_t		available;
47 48
 } gfxSem;
48 49
 
49 50
 typedef struct gfxMutex {