Skip to content

Commit 71a5906

Browse files
authored
Merge pull request hathach#1767 from pete-pjb/master
Allow the use of non-static allocation for FreeRTOS mutexes & queues
2 parents 279e2d6 + de5a67b commit 71a5906

File tree

7 files changed

+109
-50
lines changed

7 files changed

+109
-50
lines changed

examples/device/cdc_msc_freertos/Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ SRC_C += \
2929
$(FREERTOS_SRC)/timers.c \
3030
$(subst ../../../,,$(wildcard ../../../$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/*.c))
3131

32-
# Suppress FreeRTOS warnings
33-
CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
32+
# include heap manage if configSUPPORT_DYNAMIC_ALLOCATION = 1
33+
# SRC_C += $(FREERTOS_SRC)/portable/MemMang/heap_1.c
34+
# CFLAGS += -Wno-error=sign-compare
35+
36+
# Suppress FreeRTOSConfig.h warnings
37+
CFLAGS += -Wno-error=redundant-decls
38+
39+
# Suppress FreeRTOS source warnings
40+
CFLAGS += -Wno-error=cast-qual
3441

3542
# FreeRTOS (lto + Os) linker issue
3643
LDFLAGS += -Wl,--undefined=vTaskSwitchContext

examples/device/cdc_msc_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#define configTICK_RATE_HZ ( 1000 )
7070
#define configMAX_PRIORITIES ( 5 )
7171
#define configMINIMAL_STACK_SIZE ( 128 )
72-
#define configTOTAL_HEAP_SIZE ( 0*1024 ) // dynamic is not used
72+
#define configTOTAL_HEAP_SIZE ( configSUPPORT_DYNAMIC_ALLOCATION*4*1024 )
7373
#define configMAX_TASK_NAME_LEN 16
7474
#define configUSE_16_BIT_TICKS 0
7575
#define configIDLE_SHOULD_YIELD 1

examples/device/cdc_msc_freertos/src/main.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
5252
#endif
5353

54+
#define CDC_STACK_SZIE configMINIMAL_STACK_SIZE
55+
5456
//--------------------------------------------------------------------+
5557
// MACRO CONSTANT TYPEDEF PROTYPES
5658
//--------------------------------------------------------------------+
@@ -66,19 +68,18 @@ enum {
6668
BLINK_SUSPENDED = 2500,
6769
};
6870

69-
// static timer
71+
// static timer & task
72+
#if configSUPPORT_STATIC_ALLOCATION
7073
StaticTimer_t blinky_tmdef;
71-
TimerHandle_t blinky_tm;
7274

73-
// static task
7475
StackType_t usb_device_stack[USBD_STACK_SIZE];
7576
StaticTask_t usb_device_taskdef;
7677

77-
// static task for cdc
78-
#define CDC_STACK_SZIE configMINIMAL_STACK_SIZE
7978
StackType_t cdc_stack[CDC_STACK_SZIE];
8079
StaticTask_t cdc_taskdef;
80+
#endif
8181

82+
TimerHandle_t blinky_tm;
8283

8384
void led_blinky_cb(TimerHandle_t xTimer);
8485
void usb_device_task(void* param);
@@ -92,15 +93,22 @@ int main(void)
9293
{
9394
board_init();
9495

96+
#if configSUPPORT_STATIC_ALLOCATION
9597
// soft timer for blinky
9698
blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb, &blinky_tmdef);
97-
xTimerStart(blinky_tm, 0);
9899

99100
// Create a task for tinyusb device stack
100-
(void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
101+
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
101102

102103
// Create CDC task
103-
(void) xTaskCreateStatic( cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, cdc_stack, &cdc_taskdef);
104+
xTaskCreateStatic(cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, cdc_stack, &cdc_taskdef);
105+
#else
106+
blinky_tm = xTimerCreate(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb);
107+
xTaskCreate( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, NULL);
108+
xTaskCreate( cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, NULL);
109+
#endif
110+
111+
xTimerStart(blinky_tm, 0);
104112

105113
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
106114
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)

examples/device/hid_composite_freertos/Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ SRC_C += \
2828
$(FREERTOS_SRC)/timers.c \
2929
$(subst ../../../,,$(wildcard ../../../$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/*.c))
3030

31-
# Suppress FreeRTOS warnings
32-
CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
31+
# include heap manage if configSUPPORT_DYNAMIC_ALLOCATION = 1
32+
# SRC_C += $(FREERTOS_SRC)/portable/MemMang/heap_1.c
33+
# CFLAGS += -Wno-error=sign-compare
34+
35+
# Suppress FreeRTOSConfig.h warnings
36+
CFLAGS += -Wno-error=redundant-decls
37+
38+
# Suppress FreeRTOS source warnings
39+
CFLAGS += -Wno-error=cast-qual
3340

3441
# FreeRTOS (lto + Os) linker issue
3542
LDFLAGS += -Wl,--undefined=vTaskSwitchContext

examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@
4646
#include "bsp/board_mcu.h"
4747

4848
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
49-
#error "ESP32-Sx should use IDF's FreeRTOSConfig.h"
49+
#error "ESP32-Sx should use IDF's FreeRTOSConfig.h"
5050
#endif
5151

52+
// TODO fix later
5253
#if CFG_TUSB_MCU == OPT_MCU_MM32F327X
53-
// TODO fix/remove later
5454
extern u32 SystemCoreClock;
5555
#else
56+
// FIXME cause redundant-decls warnings
5657
extern uint32_t SystemCoreClock;
5758
#endif
5859

@@ -68,7 +69,7 @@
6869
#define configTICK_RATE_HZ ( 1000 )
6970
#define configMAX_PRIORITIES ( 5 )
7071
#define configMINIMAL_STACK_SIZE ( 128 )
71-
#define configTOTAL_HEAP_SIZE ( 0*1024 ) // dynamic is not used
72+
#define configTOTAL_HEAP_SIZE ( configSUPPORT_DYNAMIC_ALLOCATION*4*1024 )
7273
#define configMAX_TASK_NAME_LEN 16
7374
#define configUSE_16_BIT_TICKS 0
7475
#define configIDLE_SHOULD_YIELD 1

examples/device/hid_composite_freertos/src/main.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
5454
#endif
5555

56+
#define HID_STACK_SZIE configMINIMAL_STACK_SIZE
57+
5658
//--------------------------------------------------------------------+
5759
// MACRO CONSTANT TYPEDEF PROTYPES
5860
//--------------------------------------------------------------------+
@@ -68,19 +70,18 @@ enum {
6870
BLINK_SUSPENDED = 2500,
6971
};
7072

71-
// static timer
73+
// static timer & task
74+
#if configSUPPORT_STATIC_ALLOCATION
7275
StaticTimer_t blinky_tmdef;
73-
TimerHandle_t blinky_tm;
7476

75-
// static task
7677
StackType_t usb_device_stack[USBD_STACK_SIZE];
7778
StaticTask_t usb_device_taskdef;
7879

79-
// static task for hid
80-
#define HID_STACK_SZIE configMINIMAL_STACK_SIZE
8180
StackType_t hid_stack[HID_STACK_SZIE];
8281
StaticTask_t hid_taskdef;
82+
#endif
8383

84+
TimerHandle_t blinky_tm;
8485

8586
void led_blinky_cb(TimerHandle_t xTimer);
8687
void usb_device_task(void* param);
@@ -94,15 +95,22 @@ int main(void)
9495
{
9596
board_init();
9697

98+
#if configSUPPORT_STATIC_ALLOCATION
9799
// soft timer for blinky
98100
blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb, &blinky_tmdef);
99-
xTimerStart(blinky_tm, 0);
100101

101102
// Create a task for tinyusb device stack
102-
(void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
103+
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
103104

104105
// Create HID task
105-
(void) xTaskCreateStatic( hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, hid_stack, &hid_taskdef);
106+
xTaskCreateStatic(hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, hid_stack, &hid_taskdef);
107+
#else
108+
blinky_tm = xTimerCreate(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb);
109+
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, NULL);
110+
xTaskCreate(hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, NULL);
111+
#endif
112+
113+
xTimerStart(blinky_tm, 0);
106114

107115
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
108116
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)

src/osal/osal_freertos.h

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@
3737
extern "C" {
3838
#endif
3939

40+
//--------------------------------------------------------------------+
41+
// MACRO CONSTANT TYPEDEF PROTYPES
42+
//--------------------------------------------------------------------+
43+
44+
#if configSUPPORT_STATIC_ALLOCATION
45+
typedef StaticSemaphore_t osal_semaphore_def_t;
46+
typedef StaticSemaphore_t osal_mutex_def_t;
47+
#else
48+
// not used therefore defined to smallest possible type to save space
49+
typedef uint8_t osal_semaphore_def_t;
50+
typedef uint8_t osal_mutex_def_t;
51+
#endif
52+
53+
typedef SemaphoreHandle_t osal_semaphore_t;
54+
typedef SemaphoreHandle_t osal_mutex_t;
55+
56+
// _int_set is not used with an RTOS
57+
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
58+
static _type _name##_##buf[_depth];\
59+
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf };
60+
61+
typedef struct
62+
{
63+
uint16_t depth;
64+
uint16_t item_sz;
65+
void* buf;
66+
#if configSUPPORT_STATIC_ALLOCATION
67+
StaticQueue_t sq;
68+
#endif
69+
}osal_queue_def_t;
70+
71+
typedef QueueHandle_t osal_queue_t;
72+
73+
//--------------------------------------------------------------------+
74+
// TASK API
75+
//--------------------------------------------------------------------+
76+
4077
TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec)
4178
{
4279
if (msec == OSAL_TIMEOUT_WAIT_FOREVER) return portMAX_DELAY;
@@ -51,9 +88,6 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec)
5188
return ticks;
5289
}
5390

54-
//--------------------------------------------------------------------+
55-
// TASK API
56-
//--------------------------------------------------------------------+
5791
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
5892
{
5993
vTaskDelay( pdMS_TO_TICKS(msec) );
@@ -62,12 +96,15 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
6296
//--------------------------------------------------------------------+
6397
// Semaphore API
6498
//--------------------------------------------------------------------+
65-
typedef StaticSemaphore_t osal_semaphore_def_t;
66-
typedef SemaphoreHandle_t osal_semaphore_t;
6799

68100
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
69101
{
102+
#if configSUPPORT_STATIC_ALLOCATION
70103
return xSemaphoreCreateBinaryStatic(semdef);
104+
#else
105+
(void) semdef;
106+
return xSemaphoreCreateBinary();
107+
#endif
71108
}
72109

73110
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
@@ -92,7 +129,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t se
92129
}
93130
}
94131

95-
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
132+
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
96133
{
97134
return xSemaphoreTake(sem_hdl, _osal_ms2tick(msec));
98135
}
@@ -105,15 +142,18 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t c
105142
//--------------------------------------------------------------------+
106143
// MUTEX API (priority inheritance)
107144
//--------------------------------------------------------------------+
108-
typedef StaticSemaphore_t osal_mutex_def_t;
109-
typedef SemaphoreHandle_t osal_mutex_t;
110145

111146
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
112147
{
148+
#if configSUPPORT_STATIC_ALLOCATION
113149
return xSemaphoreCreateMutexStatic(mdef);
150+
#else
151+
(void) mdef;
152+
return xSemaphoreCreateMutex();
153+
#endif
114154
}
115155

116-
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
156+
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
117157
{
118158
return osal_semaphore_wait(mutex_hdl, msec);
119159
}
@@ -127,25 +167,13 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd
127167
// QUEUE API
128168
//--------------------------------------------------------------------+
129169

130-
// _int_set is not used with an RTOS
131-
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
132-
static _type _name##_##buf[_depth];\
133-
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf };
134-
135-
typedef struct
136-
{
137-
uint16_t depth;
138-
uint16_t item_sz;
139-
void* buf;
140-
141-
StaticQueue_t sq;
142-
}osal_queue_def_t;
143-
144-
typedef QueueHandle_t osal_queue_t;
145-
146170
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
147171
{
148-
return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
172+
#if configSUPPORT_STATIC_ALLOCATION
173+
return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
174+
#else
175+
return xQueueCreate(qdef->depth, qdef->item_sz);
176+
#endif
149177
}
150178

151179
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)

0 commit comments

Comments
 (0)