-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cgo: add FreeRTOS compatibility headers
This is especially useful if we ever want to support the ESP-IDF. Currently implemented: - xSemaphoreCreateRecursiveMutex - xSemaphoreDelete - xSemaphoreTakeRecursive - xSemaphoreGiveRecursive
- Loading branch information
Showing
7 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Package freertos provides a compatibility layer on top of the TinyGo | ||
// scheduler for C code that wants to call FreeRTOS functions. One example is | ||
// the ESP-IDF, which expects there to be a FreeRTOS-like RTOS. | ||
package freertos | ||
|
||
// #include <freertos/FreeRTOS.h> | ||
// #include <freertos/semphr.h> | ||
import "C" | ||
import ( | ||
"sync" | ||
"unsafe" | ||
|
||
"internal/task" | ||
) | ||
|
||
type Semaphore struct { | ||
lock sync.Mutex // the lock itself | ||
task *task.Task // the task currently locking this semaphore | ||
count uint32 // how many times this semaphore is locked | ||
} | ||
|
||
//export xSemaphoreCreateRecursiveMutex | ||
func xSemaphoreCreateRecursiveMutex() C.SemaphoreHandle_t { | ||
var mutex Semaphore | ||
return (C.SemaphoreHandle_t)(unsafe.Pointer(&mutex)) | ||
} | ||
|
||
//export vSemaphoreDelete | ||
func vSemaphoreDelete(xSemaphore C.SemaphoreHandle_t) { | ||
mutex := (*Semaphore)(unsafe.Pointer(xSemaphore)) | ||
if mutex.task != nil { | ||
panic("vSemaphoreDelete: still locked") | ||
} | ||
} | ||
|
||
//export xSemaphoreTakeRecursive | ||
func xSemaphoreTakeRecursive(xMutex C.SemaphoreHandle_t, xTicksToWait C.TickType_t) C.BaseType_t { | ||
// TODO: implement xTickToWait, or at least when xTicksToWait equals 0. | ||
mutex := (*Semaphore)(unsafe.Pointer(xMutex)) | ||
if mutex.task == task.Current() { | ||
// Already locked. | ||
mutex.count++ | ||
return 1 // pdTRUE | ||
} | ||
// Not yet locked. | ||
mutex.lock.Lock() | ||
mutex.task = task.Current() | ||
return 1 // pdTRUE | ||
} | ||
|
||
//export xSemaphoreGiveRecursive | ||
func xSemaphoreGiveRecursive(xMutex C.SemaphoreHandle_t) C.BaseType_t { | ||
mutex := (*Semaphore)(unsafe.Pointer(xMutex)) | ||
if mutex.task == task.Current() { | ||
// Already locked. | ||
mutex.count-- | ||
if mutex.count == 0 { | ||
mutex.lock.Unlock() | ||
} | ||
return 1 // pdTRUE | ||
} | ||
panic("xSemaphoreGiveRecursive: not locked by this task") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
typedef uint32_t TickType_t; | ||
typedef int BaseType_t; | ||
typedef unsigned int UBaseType_t; | ||
|
||
#define portMAX_DELAY (TickType_t)0xffffffffUL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
typedef struct QueueDefinition * QueueHandle_t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
// Note: in FreeRTOS, SemaphoreHandle_t is an alias for QueueHandle_t. | ||
typedef struct SemaphoreDefinition * SemaphoreHandle_t; | ||
|
||
SemaphoreHandle_t xSemaphoreCreateRecursiveMutex(void); | ||
|
||
void vSemaphoreDelete(SemaphoreHandle_t xSemaphore); | ||
|
||
// Note: these two functions are macros in FreeRTOS. | ||
BaseType_t xSemaphoreTakeRecursive(SemaphoreHandle_t xMutex, TickType_t xTicksToWait); | ||
BaseType_t xSemaphoreGiveRecursive(SemaphoreHandle_t xMutex); |
Empty file.