diff --git a/package/mqtt/platform_memory.h b/package/mqtt/platform_memory.h index b85d52a96..0147eb072 100644 --- a/package/mqtt/platform_memory.h +++ b/package/mqtt/platform_memory.h @@ -8,7 +8,6 @@ */ #ifndef _PLATFORM_MEMORY_H_ #define _PLATFORM_MEMORY_H_ -#include #include #include #include "PikaObj.h" diff --git a/package/mqtt/platform_mutex.c b/package/mqtt/platform_mutex.c index 8a8462024..9a73e40c5 100644 --- a/package/mqtt/platform_mutex.c +++ b/package/mqtt/platform_mutex.c @@ -11,6 +11,9 @@ PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_init(&(m->mutex), NULL); +#elif PIKA_FREERTOS_ENABLE + m->mutex = xSemaphoreCreateMutex(); + return 0; #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -19,6 +22,8 @@ PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) { PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_lock(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + return xSemaphoreTake(m->mutex, portMAX_DELAY); #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -27,6 +32,8 @@ PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) { PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_trylock(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + return xSemaphoreTake(m->mutex, 0); #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -35,6 +42,8 @@ PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) { int platform_mutex_unlock(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_unlock(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + return xSemaphoreGive(m->mutex); #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -43,6 +52,9 @@ int platform_mutex_unlock(platform_mutex_t* m) { int platform_mutex_destroy(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_destroy(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + vSemaphoreDelete(m->mutex); + return 0; #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif diff --git a/package/mqtt/platform_mutex.h b/package/mqtt/platform_mutex.h index 5b5198d29..b1070a3f3 100644 --- a/package/mqtt/platform_mutex.h +++ b/package/mqtt/platform_mutex.h @@ -8,22 +8,31 @@ */ #ifndef _PLATFORM_MUTEX_H_ #define _PLATFORM_MUTEX_H_ -#include "PikaPlatform.h" +#include "PikaObj.h" #ifdef __linux -#include + #include + typedef struct platform_mutex { + pthread_mutex_t mutex; + } platform_mutex_t; +#elif PIKA_FREERTOS_ENABLE + #include "FreeRTOS.h" + #include "semphr.h" + typedef struct platform_mutex { + SemaphoreHandle_t mutex; + } platform_mutex_t; #else -#include "__platform_thread.h" + /* + You need to create the __platform_thread.h for your platform. + For example: + You can #include in the __platform_thread.h + */ + #include "__platform_thread.h" #endif #ifdef __cplusplus extern "C" { #endif -#ifdef __linux -typedef struct platform_mutex { - pthread_mutex_t mutex; -} platform_mutex_t; -#endif int platform_mutex_init(platform_mutex_t* m); int platform_mutex_lock(platform_mutex_t* m); diff --git a/package/mqtt/platform_thread.c b/package/mqtt/platform_thread.c index 3d58f6adc..5091c875d 100644 --- a/package/mqtt/platform_thread.c +++ b/package/mqtt/platform_thread.c @@ -9,12 +9,13 @@ #include "platform_thread.h" #include "platform_memory.h" -platform_thread_t* platform_thread_init(const char* name, - void (*entry)(void*), - void* const param, - unsigned int stack_size, - unsigned int priority, - unsigned int tick) { +PIKA_WEAK platform_thread_t* platform_thread_init(const char* name, + void (*entry)(void*), + void* const param, + unsigned int stack_size, + unsigned int priority, + unsigned int tick) { +#ifdef __linux int res; platform_thread_t* thread; void* (*thread_entry)(void*); @@ -31,25 +32,64 @@ platform_thread_t* platform_thread_init(const char* name, thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; return thread; +#elif PIKA_FREERTOS_ENABLE + BaseType_t err; + platform_thread_t* thread; + + thread = platform_memory_alloc(sizeof(platform_thread_t)); + + (void)tick; + + err = xTaskCreate(entry, name, stack_size, param, priority, thread->thread); + + if (pdPASS != err) { + platform_memory_free(thread); + return NULL; + } + + return thread; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_thread_startup(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_startup(platform_thread_t* thread) { (void)thread; } -void platform_thread_stop(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_stop(platform_thread_t* thread) { +#ifdef __linux pthread_mutex_lock(&(thread->mutex)); pthread_cond_wait(&(thread->cond), &(thread->mutex)); pthread_mutex_unlock(&(thread->mutex)); +#elif PIKA_FREERTOS_ENABLE + vTaskSuspend(thread->thread); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_thread_start(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_start(platform_thread_t* thread) { +#ifdef __linux pthread_mutex_lock(&(thread->mutex)); pthread_cond_signal(&(thread->cond)); pthread_mutex_unlock(&(thread->mutex)); +#elif PIKA_FREERTOS_ENABLE + vTaskResume(thread->thread); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_thread_destroy(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_destroy(platform_thread_t* thread) { +#ifdef __linux if (NULL != thread) pthread_detach(thread->thread); +#elif PIKA_FREERTOS_ENABLE + if (NULL != thread) + vTaskDelete(thread->thread); + platform_memory_free(thread); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } diff --git a/package/mqtt/platform_thread.h b/package/mqtt/platform_thread.h index 7949572af..11bdf98b1 100644 --- a/package/mqtt/platform_thread.h +++ b/package/mqtt/platform_thread.h @@ -12,7 +12,23 @@ #include "PikaObj.h" #ifdef __linux #include + typedef struct platform_thread { + pthread_t thread; + pthread_mutex_t mutex; + pthread_cond_t cond; + } platform_thread_t; +#elif PIKA_FREERTOS_ENABLE + #include "FreeRTOS.h" + #include "task.h" + typedef struct platform_thread { + TaskHandle_t thread; + } platform_thread_t; #else +/* + You need to create the __platform_thread.h for your platform. + For example: + You can #include in the __platform_thread.h +*/ #include "__platform_thread.h" #endif @@ -20,14 +36,6 @@ extern "C" { #endif -#ifdef __linux -typedef struct platform_thread { - pthread_t thread; - pthread_mutex_t mutex; - pthread_cond_t cond; -} platform_thread_t; -#endif - platform_thread_t* platform_thread_init(const char* name, void (*entry)(void*), void* const param, diff --git a/package/mqtt/platform_timer.c b/package/mqtt/platform_timer.c index bdb60d9a9..246288463 100644 --- a/package/mqtt/platform_timer.c +++ b/package/mqtt/platform_timer.c @@ -9,35 +9,85 @@ #include "platform_timer.h" -void platform_timer_init(platform_timer_t* timer) { +PIKA_WEAK void platform_timer_init(platform_timer_t* timer) { +#ifdef __linux timer->time = (struct timeval){0, 0}; +#elif PIKA_FREERTOS_ENABLE + timer->time = 0; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) { +PIKA_WEAK void platform_timer_cutdown(platform_timer_t* timer, + unsigned int timeout) { +#ifdef __linux struct timeval now; gettimeofday(&now, NULL); struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000}; timeradd(&now, &interval, &timer->time); +#elif PIKA_FREERTOS_ENABLE + timer->time = platform_uptime_ms(); + timer->time += timeout; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -char platform_timer_is_expired(platform_timer_t* timer) { +PIKA_WEAK char platform_timer_is_expired(platform_timer_t* timer) { +#ifdef __linux struct timeval now, res; gettimeofday(&now, NULL); timersub(&timer->time, &now, &res); return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0)); +#elif PIKA_FREERTOS_ENABLE + return platform_uptime_ms() > timer->time ? 1 : 0; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -int platform_timer_remain(platform_timer_t* timer) { +PIKA_WEAK int platform_timer_remain(platform_timer_t* timer) { +#ifdef __linux struct timeval now, res; gettimeofday(&now, NULL); timersub(&timer->time, &now, &res); return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000; +#elif PIKA_FREERTOS_ENABLE + uint32_t now; + now = platform_uptime_ms(); + if (timer->time <= now) { + return 0; + } + return timer->time - now; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -unsigned long platform_timer_now(void) { +PIKA_WEAK unsigned long platform_timer_now(void) { +#ifdef __linux return (unsigned long)time(NULL); +#elif PIKA_FREERTOS_ENABLE + return (unsigned long)platform_uptime_ms(); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_timer_usleep(unsigned long usec) { +PIKA_WEAK void platform_timer_usleep(unsigned long usec) { +#ifdef __linux usleep(usec); +#elif PIKA_FREERTOS_ENABLE + TickType_t tick; + if (usec != 0) { + tick = usec / portTICK_PERIOD_MS; + + if (tick == 0) + tick = 1; + } + vTaskDelay(tick); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } diff --git a/package/mqtt/platform_timer.h b/package/mqtt/platform_timer.h index 584821931..89773bb5f 100644 --- a/package/mqtt/platform_timer.h +++ b/package/mqtt/platform_timer.h @@ -11,9 +11,21 @@ #include #ifdef __linux -#include + #include + typedef struct platform_timer { + struct timeval time; + } platform_timer_t; +#elif PIKA_FREERTOS_ENABLE + #include "FreeRTOS.h" + #include "task.h" + typedef struct platform_timer { + uint32_t time; + } platform_timer_t; #else -#include "__platform_time.h" + /* + You need to create the __platform_timer.h for your platform. + */ + #include "__platform_time.h" #endif #include #include @@ -23,12 +35,6 @@ extern "C" { #endif -#ifdef __linux -typedef struct platform_timer { - struct timeval time; -} platform_timer_t; -#endif - void platform_timer_init(platform_timer_t* timer); void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout); char platform_timer_is_expired(platform_timer_t* timer); diff --git a/port/linux/.vscode/settings.json b/port/linux/.vscode/settings.json index 387b28124..64a856810 100644 --- a/port/linux/.vscode/settings.json +++ b/port/linux/.vscode/settings.json @@ -81,7 +81,8 @@ "PikaPlatform_socket.h": "c", "time.h": "c", "platform_thread.h": "c", - "platform_timer.h": "c" + "platform_timer.h": "c", + "stddef.h": "c" }, "python.formatting.provider": "autopep8", "C_Cpp.errorSquiggles": "Disabled" diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_memory.h b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_memory.h index b85d52a96..0147eb072 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_memory.h +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_memory.h @@ -8,7 +8,6 @@ */ #ifndef _PLATFORM_MEMORY_H_ #define _PLATFORM_MEMORY_H_ -#include #include #include #include "PikaObj.h" diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.c b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.c index 8a8462024..9a73e40c5 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.c +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.c @@ -11,6 +11,9 @@ PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_init(&(m->mutex), NULL); +#elif PIKA_FREERTOS_ENABLE + m->mutex = xSemaphoreCreateMutex(); + return 0; #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -19,6 +22,8 @@ PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) { PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_lock(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + return xSemaphoreTake(m->mutex, portMAX_DELAY); #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -27,6 +32,8 @@ PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) { PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_trylock(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + return xSemaphoreTake(m->mutex, 0); #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -35,6 +42,8 @@ PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) { int platform_mutex_unlock(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_unlock(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + return xSemaphoreGive(m->mutex); #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif @@ -43,6 +52,9 @@ int platform_mutex_unlock(platform_mutex_t* m) { int platform_mutex_destroy(platform_mutex_t* m) { #ifdef __linux return pthread_mutex_destroy(&(m->mutex)); +#elif PIKA_FREERTOS_ENABLE + vSemaphoreDelete(m->mutex); + return 0; #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); #endif diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.h b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.h index 5b5198d29..b1070a3f3 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.h +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_mutex.h @@ -8,22 +8,31 @@ */ #ifndef _PLATFORM_MUTEX_H_ #define _PLATFORM_MUTEX_H_ -#include "PikaPlatform.h" +#include "PikaObj.h" #ifdef __linux -#include + #include + typedef struct platform_mutex { + pthread_mutex_t mutex; + } platform_mutex_t; +#elif PIKA_FREERTOS_ENABLE + #include "FreeRTOS.h" + #include "semphr.h" + typedef struct platform_mutex { + SemaphoreHandle_t mutex; + } platform_mutex_t; #else -#include "__platform_thread.h" + /* + You need to create the __platform_thread.h for your platform. + For example: + You can #include in the __platform_thread.h + */ + #include "__platform_thread.h" #endif #ifdef __cplusplus extern "C" { #endif -#ifdef __linux -typedef struct platform_mutex { - pthread_mutex_t mutex; -} platform_mutex_t; -#endif int platform_mutex_init(platform_mutex_t* m); int platform_mutex_lock(platform_mutex_t* m); diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.c b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.c index 3d58f6adc..5091c875d 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.c +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.c @@ -9,12 +9,13 @@ #include "platform_thread.h" #include "platform_memory.h" -platform_thread_t* platform_thread_init(const char* name, - void (*entry)(void*), - void* const param, - unsigned int stack_size, - unsigned int priority, - unsigned int tick) { +PIKA_WEAK platform_thread_t* platform_thread_init(const char* name, + void (*entry)(void*), + void* const param, + unsigned int stack_size, + unsigned int priority, + unsigned int tick) { +#ifdef __linux int res; platform_thread_t* thread; void* (*thread_entry)(void*); @@ -31,25 +32,64 @@ platform_thread_t* platform_thread_init(const char* name, thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; return thread; +#elif PIKA_FREERTOS_ENABLE + BaseType_t err; + platform_thread_t* thread; + + thread = platform_memory_alloc(sizeof(platform_thread_t)); + + (void)tick; + + err = xTaskCreate(entry, name, stack_size, param, priority, thread->thread); + + if (pdPASS != err) { + platform_memory_free(thread); + return NULL; + } + + return thread; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_thread_startup(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_startup(platform_thread_t* thread) { (void)thread; } -void platform_thread_stop(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_stop(platform_thread_t* thread) { +#ifdef __linux pthread_mutex_lock(&(thread->mutex)); pthread_cond_wait(&(thread->cond), &(thread->mutex)); pthread_mutex_unlock(&(thread->mutex)); +#elif PIKA_FREERTOS_ENABLE + vTaskSuspend(thread->thread); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_thread_start(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_start(platform_thread_t* thread) { +#ifdef __linux pthread_mutex_lock(&(thread->mutex)); pthread_cond_signal(&(thread->cond)); pthread_mutex_unlock(&(thread->mutex)); +#elif PIKA_FREERTOS_ENABLE + vTaskResume(thread->thread); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_thread_destroy(platform_thread_t* thread) { +PIKA_WEAK void platform_thread_destroy(platform_thread_t* thread) { +#ifdef __linux if (NULL != thread) pthread_detach(thread->thread); +#elif PIKA_FREERTOS_ENABLE + if (NULL != thread) + vTaskDelete(thread->thread); + platform_memory_free(thread); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.h b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.h index 7949572af..11bdf98b1 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.h +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_thread.h @@ -12,7 +12,23 @@ #include "PikaObj.h" #ifdef __linux #include + typedef struct platform_thread { + pthread_t thread; + pthread_mutex_t mutex; + pthread_cond_t cond; + } platform_thread_t; +#elif PIKA_FREERTOS_ENABLE + #include "FreeRTOS.h" + #include "task.h" + typedef struct platform_thread { + TaskHandle_t thread; + } platform_thread_t; #else +/* + You need to create the __platform_thread.h for your platform. + For example: + You can #include in the __platform_thread.h +*/ #include "__platform_thread.h" #endif @@ -20,14 +36,6 @@ extern "C" { #endif -#ifdef __linux -typedef struct platform_thread { - pthread_t thread; - pthread_mutex_t mutex; - pthread_cond_t cond; -} platform_thread_t; -#endif - platform_thread_t* platform_thread_init(const char* name, void (*entry)(void*), void* const param, diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.c b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.c index bdb60d9a9..246288463 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.c +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.c @@ -9,35 +9,85 @@ #include "platform_timer.h" -void platform_timer_init(platform_timer_t* timer) { +PIKA_WEAK void platform_timer_init(platform_timer_t* timer) { +#ifdef __linux timer->time = (struct timeval){0, 0}; +#elif PIKA_FREERTOS_ENABLE + timer->time = 0; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) { +PIKA_WEAK void platform_timer_cutdown(platform_timer_t* timer, + unsigned int timeout) { +#ifdef __linux struct timeval now; gettimeofday(&now, NULL); struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000}; timeradd(&now, &interval, &timer->time); +#elif PIKA_FREERTOS_ENABLE + timer->time = platform_uptime_ms(); + timer->time += timeout; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -char platform_timer_is_expired(platform_timer_t* timer) { +PIKA_WEAK char platform_timer_is_expired(platform_timer_t* timer) { +#ifdef __linux struct timeval now, res; gettimeofday(&now, NULL); timersub(&timer->time, &now, &res); return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0)); +#elif PIKA_FREERTOS_ENABLE + return platform_uptime_ms() > timer->time ? 1 : 0; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -int platform_timer_remain(platform_timer_t* timer) { +PIKA_WEAK int platform_timer_remain(platform_timer_t* timer) { +#ifdef __linux struct timeval now, res; gettimeofday(&now, NULL); timersub(&timer->time, &now, &res); return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000; +#elif PIKA_FREERTOS_ENABLE + uint32_t now; + now = platform_uptime_ms(); + if (timer->time <= now) { + return 0; + } + return timer->time - now; +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -unsigned long platform_timer_now(void) { +PIKA_WEAK unsigned long platform_timer_now(void) { +#ifdef __linux return (unsigned long)time(NULL); +#elif PIKA_FREERTOS_ENABLE + return (unsigned long)platform_uptime_ms(); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } -void platform_timer_usleep(unsigned long usec) { +PIKA_WEAK void platform_timer_usleep(unsigned long usec) { +#ifdef __linux usleep(usec); +#elif PIKA_FREERTOS_ENABLE + TickType_t tick; + if (usec != 0) { + tick = usec / portTICK_PERIOD_MS; + + if (tick == 0) + tick = 1; + } + vTaskDelay(tick); +#else + WEAK_FUNCTION_NEED_OVERRIDE_ERROR(); +#endif } diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.h b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.h index 584821931..89773bb5f 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.h +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/platform_timer.h @@ -11,9 +11,21 @@ #include #ifdef __linux -#include + #include + typedef struct platform_timer { + struct timeval time; + } platform_timer_t; +#elif PIKA_FREERTOS_ENABLE + #include "FreeRTOS.h" + #include "task.h" + typedef struct platform_timer { + uint32_t time; + } platform_timer_t; #else -#include "__platform_time.h" + /* + You need to create the __platform_timer.h for your platform. + */ + #include "__platform_time.h" #endif #include #include @@ -23,12 +35,6 @@ extern "C" { #endif -#ifdef __linux -typedef struct platform_timer { - struct timeval time; -} platform_timer_t; -#endif - void platform_timer_init(platform_timer_t* timer); void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout); char platform_timer_is_expired(platform_timer_t* timer); diff --git a/port/linux/package/pikascript/pikascript-lib/socket/PikaPlatform_socket.h b/port/linux/package/pikascript/pikascript-lib/socket/PikaPlatform_socket.h index f7604bfcc..4c17d0554 100644 --- a/port/linux/package/pikascript/pikascript-lib/socket/PikaPlatform_socket.h +++ b/port/linux/package/pikascript/pikascript-lib/socket/PikaPlatform_socket.h @@ -5,6 +5,12 @@ #include #include #include +#elif PIKA_LWIP_ENABLE +#include +#include "lwip/api.h" +#include "lwip/netdb.h" +#include "lwip/opt.h" +#include "lwip/sys.h" #else /* You need to create the __platform_socket.h for your platform.