You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.5 KiB
96 lines
2.5 KiB
#include "zthread.h"
|
|
#include "stdbool.h"
|
|
#include "stm32sdk.h"
|
|
// #include "Middlewares\Third_Party\FreeRTOS\Source\portable\RVDS\ARM_CM4F\port.h"
|
|
#include "zassert.h"
|
|
|
|
#define TAG "zthread"
|
|
|
|
extern uint32_t vPortGetIPSR(void);
|
|
|
|
static __inline bool IS_IRQ(void)
|
|
{
|
|
if (vPortGetIPSR())
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
static void tasksleep(uint32_t ms) { ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(ms)); }
|
|
|
|
static void taskwake(zthread_t *thread) {
|
|
BaseType_t state;
|
|
if (IS_IRQ())
|
|
vTaskNotifyGiveFromISR(thread->handler, &state);
|
|
else
|
|
xTaskNotifyGive(thread->handler);
|
|
}
|
|
|
|
static void prvtaskfunction(void *_taskhandler) {
|
|
zthread_t *thread = (zthread_t *)_taskhandler;
|
|
Z_ASSERT(_taskhandler);
|
|
|
|
while (true) {
|
|
thread->threadisworking = false;
|
|
|
|
// 等待taskworking
|
|
xEventGroupWaitBits(thread->zthreadstartworkevent, 0x01, pdTRUE, pdTRUE, portMAX_DELAY);
|
|
// ZLOGI(TAG, "thread %s startwork", thread->name);
|
|
ZLOGI(TAG, "thread %s startwork", thread->name);
|
|
thread->threadisworking = true;
|
|
thread->taskfunction(NULL);
|
|
ZLOGI(TAG, "thread %s end work", thread->name);
|
|
}
|
|
};
|
|
|
|
void zthread_init(zthread_t *thread, const char *name, TaskFunction_t taskfunction) {
|
|
Z_ASSERT(thread);
|
|
Z_ASSERT(taskfunction);
|
|
Z_ASSERT(name);
|
|
|
|
thread->lock = xSemaphoreCreateMutex();
|
|
Z_ASSERT(thread->lock);
|
|
|
|
if (thread->stacksize == 0) {
|
|
thread->stacksize = ZTHREAD_DEFAULT_STACK_SIZE;
|
|
}
|
|
if (thread->uxPriority == 0) {
|
|
thread->uxPriority = ZTHREAD_DEFAULT_PRIORITY;
|
|
}
|
|
thread->taskfunction = taskfunction;
|
|
thread->zthreadstartworkevent = xEventGroupCreate();
|
|
thread->name = name;
|
|
|
|
xTaskCreate(prvtaskfunction, name, thread->stacksize, (void *)thread, thread->uxPriority, &thread->handler);
|
|
}
|
|
void zthread_start(zthread_t *thread) {
|
|
/**
|
|
* @brief
|
|
*/
|
|
|
|
Z_ASSERT(thread);
|
|
xSemaphoreTake(thread->lock, portMAX_DELAY);
|
|
xEventGroupSetBits(thread->zthreadstartworkevent, 0x01);
|
|
while (thread->threadisworking != true) {
|
|
vTaskDelay(10);
|
|
}
|
|
xSemaphoreGive(thread->lock);
|
|
}
|
|
|
|
bool zthread_is_expect_stop(zthread_t *thread) { return thread->_expect_stop; }
|
|
|
|
void zthread_stop(zthread_t *thread) {
|
|
Z_ASSERT(thread);
|
|
xSemaphoreTake(thread->lock, portMAX_DELAY);
|
|
|
|
thread->_expect_stop = true;
|
|
while (thread->threadisworking == true) {
|
|
taskwake(thread);
|
|
vTaskDelay(10);
|
|
}
|
|
thread->_expect_stop = false;
|
|
xSemaphoreGive(thread->lock);
|
|
}
|
|
|
|
void zthread_sleep(uint32_t ms) { tasksleep(ms); }
|
|
void zthread_weak(zthread_t *thread) { taskwake(thread); }
|