Skip to content

Commit 33d2847

Browse files
committed
cgo: add FreeRTOS compatibility headers
This is especially useful if we ever want to support the ESP-IDF. Currently implemented: - xSemaphoreCreateRecursiveMutex - xSemaphoreDelete - xSemaphoreTakeRecursive - xSemaphoreGiveRecursive
1 parent 213cdf6 commit 33d2847

File tree

7 files changed

+90
-0
lines changed

7 files changed

+90
-0
lines changed

compileopts/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ func (c *Config) RP2040BootPatch() bool {
201201
// preprocessing.
202202
func (c *Config) CFlags() []string {
203203
var cflags []string
204+
// Compatibility CFlags.
205+
cflags = append(cflags, "-I"+filepath.Join(goenv.Get("TINYGOROOT"), "src/compat/freertos/include"))
206+
// CFlags for the target.
204207
for _, flag := range c.Target.CFlags {
205208
cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
206209
}

loader/goroot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ func needsSyscallPackage(buildTags []string) bool {
220220
func pathsToOverride(needsSyscallPackage bool) map[string]bool {
221221
paths := map[string]bool{
222222
"/": true,
223+
"compat/": false,
223224
"crypto/": true,
224225
"crypto/rand/": false,
225226
"device/": false,

src/compat/freertos/freertos.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package freertos
2+
3+
// #include <freertos/FreeRTOS.h>
4+
// #include <freertos/semphr.h>
5+
import "C"
6+
import (
7+
"sync"
8+
"unsafe"
9+
10+
"internal/task"
11+
)
12+
13+
type Semaphore struct {
14+
lock sync.Mutex // the lock itself
15+
task *task.Task // the task currently locking this semaphore
16+
count uint32 // how many times this semaphore is locked
17+
}
18+
19+
//export xSemaphoreCreateRecursiveMutex
20+
func xSemaphoreCreateRecursiveMutex() C.SemaphoreHandle_t {
21+
var mutex Semaphore
22+
return (C.SemaphoreHandle_t)(unsafe.Pointer(&mutex))
23+
}
24+
25+
//export vSemaphoreDelete
26+
func vSemaphoreDelete(xSemaphore C.SemaphoreHandle_t) {
27+
mutex := (*Semaphore)(unsafe.Pointer(xSemaphore))
28+
if mutex.task != nil {
29+
panic("vSemaphoreDelete: still locked")
30+
}
31+
}
32+
33+
//export xSemaphoreTakeRecursive
34+
func xSemaphoreTakeRecursive(xMutex C.SemaphoreHandle_t, xTicksToWait C.TickType_t) C.BaseType_t {
35+
println("take recursive")
36+
// TODO: implement xTickToWait, or at least when xTicksToWait equals 0.
37+
mutex := (*Semaphore)(unsafe.Pointer(xMutex))
38+
if mutex.task == task.Current() {
39+
// Already locked.
40+
mutex.count++
41+
return 1 // pdTRUE
42+
}
43+
// Not yet locked.
44+
mutex.lock.Lock()
45+
mutex.task = task.Current()
46+
return 1 // pdTRUE
47+
}
48+
49+
//export xSemaphoreGiveRecursive
50+
func xSemaphoreGiveRecursive(xMutex C.SemaphoreHandle_t) C.BaseType_t {
51+
println("give recursive")
52+
mutex := (*Semaphore)(unsafe.Pointer(xMutex))
53+
if mutex.task == task.Current() {
54+
// Already locked.
55+
mutex.count--
56+
if mutex.count == 0 {
57+
mutex.lock.Unlock()
58+
}
59+
return 1 // pdTRUE
60+
}
61+
panic("xSemaphoreGiveRecursive: not locked by this task")
62+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
typedef uint32_t TickType_t;
6+
typedef int BaseType_t;
7+
typedef unsigned int UBaseType_t;
8+
9+
#define portMAX_DELAY (TickType_t)0xffffffffUL
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
typedef struct QueueDefinition * QueueHandle_t;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
// Note: in FreeRTOS, SemaphoreHandle_t is an alias for QueueHandle_t.
4+
typedef struct SemaphoreDefinition * SemaphoreHandle_t;
5+
6+
SemaphoreHandle_t xSemaphoreCreateRecursiveMutex(void);
7+
8+
void vSemaphoreDelete(SemaphoreHandle_t xSemaphore);
9+
10+
// Note: these two functions are macros in FreeRTOS.
11+
BaseType_t xSemaphoreTakeRecursive(SemaphoreHandle_t xMutex, TickType_t xTicksToWait);
12+
BaseType_t xSemaphoreGiveRecursive(SemaphoreHandle_t xMutex);

src/compat/freertos/include/freertos/task.h

Whitespace-only changes.

0 commit comments

Comments
 (0)