mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-22 23:07:28 +02:00
feat(examples): Add support for lwip build under linux
This commit is contained in:
8
common_components/linux_compat/esp_timer/CMakeLists.txt
Normal file
8
common_components/linux_compat/esp_timer/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
idf_component_register(SRCS esp_timer_linux.c timer_task.cpp
|
||||
INCLUDE_DIRS include)
|
||||
|
||||
set_target_properties(${COMPONENT_LIB} PROPERTIES
|
||||
CXX_STANDARD 17
|
||||
CXX_STANDARD_REQUIRED ON
|
||||
CXX_EXTENSIONS ON
|
||||
)
|
47
common_components/linux_compat/esp_timer/esp_timer_linux.c
Normal file
47
common_components/linux_compat/esp_timer/esp_timer_linux.c
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include <pthread.h>
|
||||
|
||||
void *create_tt(esp_timer_cb_t cb);
|
||||
|
||||
void destroy_tt(void *tt);
|
||||
|
||||
void set_tout(void *tt, uint32_t ms);
|
||||
|
||||
esp_err_t esp_timer_create(const esp_timer_create_args_t *create_args,
|
||||
esp_timer_handle_t *out_handle)
|
||||
{
|
||||
*out_handle = (esp_timer_handle_t)create_tt(create_args->callback);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period)
|
||||
{
|
||||
set_tout(timer, period / 1000);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_stop(esp_timer_handle_t timer)
|
||||
{
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_delete(esp_timer_handle_t timer)
|
||||
{
|
||||
destroy_tt(timer);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int64_t esp_timer_get_time(void)
|
||||
{
|
||||
struct timespec spec;
|
||||
clock_gettime(CLOCK_REALTIME, &spec);
|
||||
return spec.tv_nsec / 1000 + spec.tv_sec * 1000000;
|
||||
}
|
36
common_components/linux_compat/esp_timer/include/esp_timer.h
Normal file
36
common_components/linux_compat/esp_timer/include/esp_timer.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "bsd/string.h"
|
||||
|
||||
typedef struct esp_timer *esp_timer_handle_t;
|
||||
|
||||
typedef void (*esp_timer_cb_t)(void *arg);
|
||||
|
||||
typedef enum {
|
||||
ESP_TIMER_TASK,
|
||||
} esp_timer_dispatch_t;
|
||||
|
||||
typedef struct {
|
||||
esp_timer_cb_t callback; //!< Function to call when timer expires
|
||||
void *arg; //!< Argument to pass to the callback
|
||||
esp_timer_dispatch_t dispatch_method; //!< Call the callback from task or from ISR
|
||||
const char *name; //!< Timer name, used in esp_timer_dump function
|
||||
bool skip_unhandled_events; //!< Skip unhandled events for periodic timers
|
||||
} esp_timer_create_args_t;
|
||||
|
||||
esp_err_t esp_timer_create(const esp_timer_create_args_t *create_args,
|
||||
esp_timer_handle_t *out_handle);
|
||||
esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
|
||||
|
||||
esp_err_t esp_timer_stop(esp_timer_handle_t timer);
|
||||
|
||||
esp_err_t esp_timer_delete(esp_timer_handle_t timer);
|
||||
|
||||
int64_t esp_timer_get_time(void);
|
30
common_components/linux_compat/esp_timer/timer_task.cpp
Normal file
30
common_components/linux_compat/esp_timer/timer_task.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "timer_task.hpp"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
extern "C" void *create_tt(cb_t cb)
|
||||
{
|
||||
auto *tt = new TimerTaskMock(cb);
|
||||
return tt;
|
||||
}
|
||||
|
||||
extern "C" void destroy_tt(void *tt)
|
||||
{
|
||||
auto *timer_task = static_cast<TimerTaskMock *>(tt);
|
||||
delete (timer_task);
|
||||
}
|
||||
|
||||
|
||||
extern "C" void set_tout(void *tt, uint32_t ms)
|
||||
{
|
||||
auto *timer_task = static_cast<TimerTaskMock *>(tt);
|
||||
timer_task->SetTimeout(ms);
|
||||
}
|
57
common_components/linux_compat/esp_timer/timer_task.hpp
Normal file
57
common_components/linux_compat/esp_timer/timer_task.hpp
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
typedef void (*cb_t)(void *arg);
|
||||
|
||||
class TimerTaskMock {
|
||||
public:
|
||||
TimerTaskMock(cb_t cb): cb(cb), active(false), ms(INT32_MAX) {}
|
||||
~TimerTaskMock(void)
|
||||
{
|
||||
active = false;
|
||||
t.join();
|
||||
}
|
||||
|
||||
void SetTimeout(uint32_t m)
|
||||
{
|
||||
ms = m;
|
||||
active = true;
|
||||
t = std::thread(run_static, this);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static void run_static(TimerTaskMock *timer)
|
||||
{
|
||||
timer->run();
|
||||
}
|
||||
|
||||
void run(void)
|
||||
{
|
||||
while (!active.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
while (active.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
cb(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
cb_t cb;
|
||||
std::thread t;
|
||||
std::atomic<bool> active;
|
||||
uint32_t ms;
|
||||
|
||||
};
|
@ -7,6 +7,10 @@
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TaskHandle_t TaskHandle_t
|
||||
#define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
|
||||
|
||||
@ -73,3 +77,7 @@ EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
|
||||
const EventBits_t uxBitsToSet );
|
||||
|
||||
uint32_t xQueueSendToBack(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif //__cplusplus
|
||||
|
Reference in New Issue
Block a user