参考代码
This commit is contained in:
24
components/system/include/os/os.h
Executable file
24
components/system/include/os/os.h
Executable file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @file os.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_H__
|
||||
#define __OS_H__
|
||||
|
||||
#include "os/os_thread.h"
|
||||
#include "os/os_timer.h"
|
||||
#include "os/os_work.h"
|
||||
#include "os/os_ipc.h"
|
||||
#include "os/os_semaphore.h"
|
||||
#include "os/os_mutex.h"
|
||||
#include "os/os_service.h"
|
||||
#include "os/os_heap.h"
|
||||
|
||||
#endif /* __OS_H__ */
|
||||
68
components/system/include/os/os_common.h
Executable file
68
components/system/include/os/os_common.h
Executable file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* @file os_common.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_COMMON_H__
|
||||
#define __OS_COMMON_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
OS_PRIORITY_IDLE = 0,
|
||||
OS_PRIORITY_LOWEST = 1,
|
||||
OS_PRIORITY_LOWER = 2,
|
||||
OS_PRIORITY_LOW = 3,
|
||||
OS_PRIORITY_NORMAL = 4,
|
||||
OS_PRIORITY_HIGH = 5,
|
||||
OS_PRIORITY_HIGHER = 6,
|
||||
OS_PRIORITY_HIGHEST = 7
|
||||
} os_priority;
|
||||
|
||||
typedef enum {
|
||||
OS_OK = 0, /* success */
|
||||
OS_FAIL = -1, /* general failure */
|
||||
OS_E_NOMEM = -2, /* out of memory */
|
||||
OS_E_PARAM = -3, /* invalid parameter */
|
||||
OS_E_TIMEOUT = -4, /* operation timeout */
|
||||
OS_E_ISR = -5, /* not allowed in ISR context */
|
||||
} os_state;
|
||||
|
||||
typedef void * os_handle_t;
|
||||
|
||||
typedef size_t os_time_t;
|
||||
|
||||
#define OS_INVALID_HANDLE NULL /* OS invalid handle */
|
||||
#define OS_WAIT_FOREVER 0xffffffffU /* Wait forever timeout value */
|
||||
#define OS_SEMAPHORE_MAX_COUNT 0xffffffffU /* Maximum count value for semaphore */
|
||||
|
||||
#define OS_MSEC_PER_SEC 1000U /* milliseconds per second */
|
||||
#define OS_USEC_PER_MSEC 1000U /* microseconds per millisecond */
|
||||
#define OS_USEC_PER_SEC 1000000U /* microseconds per second */
|
||||
|
||||
#define OS_TICK (OS_USEC_PER_SEC / OS_TICK_RATE) /* microseconds per tick */
|
||||
|
||||
#define OS_SecsToTicks(sec) ((os_time_t)(sec) * OS_TICK_RATE)
|
||||
#define OS_MSecsToTicks(msec) ((os_time_t)(OS_TICK_RATE < OS_MSEC_PER_SEC ? (msec) / (OS_TICK / OS_USEC_PER_MSEC) : (msec) * (OS_USEC_PER_MSEC / OS_TICK)))
|
||||
#define OS_TicksToMSecs(t) ((os_time_t)(OS_TICK_RATE < OS_MSEC_PER_SEC ? (t) * (OS_TICK / OS_USEC_PER_MSEC) : (t) / (OS_USEC_PER_MSEC / OS_TICK)))
|
||||
#define OS_TicksToSecs(t) ((os_time_t)(t) / (OS_USEC_PER_SEC / OS_TICK))
|
||||
|
||||
#define OS_CONTAINER_OF(PTR, TYPE, MEMBER) ((TYPE *)&((uint8_t *)PTR)[-(int)&((TYPE *)0)->MEMBER])
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_COMMON_H__ */
|
||||
33
components/system/include/os/os_heap.h
Executable file
33
components/system/include/os/os_heap.h
Executable file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @file os_heap.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_HEAP_H__
|
||||
#define __OS_HEAP_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
void *os_malloc(size_t size);
|
||||
void *os_calloc(size_t size);
|
||||
void *os_realloc(void *p, size_t size);
|
||||
void os_free(void *p);
|
||||
|
||||
void os_heap_info(size_t *used_size, size_t *free_size, size_t *max_block_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_HEAP_H__ */
|
||||
103
components/system/include/os/os_ipc.h
Executable file
103
components/system/include/os/os_ipc.h
Executable file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @file os_ipc.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_IPC_H__
|
||||
#define __OS_IPC_H__
|
||||
|
||||
#include "os/os_work.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* #ifdef __cplusplus */
|
||||
|
||||
typedef os_handle_t os_fifo_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_fifo_handle_t handle;
|
||||
} os_fifo_t;
|
||||
|
||||
os_state os_fifo_q_create(os_fifo_t *fifo_handle);
|
||||
os_state os_fifo_q_delete(os_fifo_t *fifo_handle);
|
||||
os_state os_fifo_q_clr(os_fifo_t *fifo_handle);
|
||||
|
||||
void os_fifo_q_regist(os_fifo_t *fifo_handle, os_work_t *work_handle, int delay_ticks);
|
||||
void os_fifo_q_unregist(os_fifo_t *fifo_handle);
|
||||
|
||||
void *os_fifo_alloc(size_t size);
|
||||
os_state os_fifo_put(os_fifo_t *fifo_handle, void *fifo_data);
|
||||
|
||||
void *os_fifo_take(os_fifo_t *fifo_handle, os_time_t wait_ms);
|
||||
os_state os_fifo_free(void *fifo_data);
|
||||
|
||||
void *os_fifo_peek_head(os_fifo_t *fifo_handle);
|
||||
void *os_fifo_peek_tail(os_fifo_t *fifo_handle);
|
||||
|
||||
bool os_fifo_q_is_valid(os_fifo_t *fifo_handle);
|
||||
|
||||
typedef os_handle_t os_queue_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_queue_handle_t handle;
|
||||
} os_queue_t;
|
||||
|
||||
os_state os_queue_create(os_queue_t *queue_handle, size_t queueLen, size_t itemSize);
|
||||
os_state os_queue_delete(os_queue_t *queue_handle);
|
||||
os_state os_queue_clr(os_queue_t *queue_handle);
|
||||
|
||||
void os_queue_regist(os_queue_t *queue_handle, os_work_t *work_handle, int delay_ticks);
|
||||
void os_queue_unregist(os_queue_t *queue_handle);
|
||||
|
||||
os_state os_queue_send(os_queue_t *queue_handle, const void *item, os_time_t wait_ms);
|
||||
os_state os_queue_recv(os_queue_t *queue_handle, void *item, os_time_t wait_ms);
|
||||
|
||||
void *os_queue_peek_head(os_queue_t *queue_handle);
|
||||
void *os_queue_peek_tail(os_queue_t *queue_handle);
|
||||
|
||||
size_t os_queue_get_item_size(os_queue_t *queue_handle);
|
||||
|
||||
bool os_queue_is_valid(os_queue_t *queue_handle);
|
||||
|
||||
typedef os_handle_t os_pipe_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_pipe_handle_t handle;
|
||||
} os_pipe_t;
|
||||
|
||||
os_state os_pipe_create(os_pipe_t *pipe_handle, size_t pipe_size);
|
||||
os_state os_pipe_delete(os_pipe_t *pipe_handle);
|
||||
os_state os_pipe_clr(os_pipe_t *pipe_handle);
|
||||
|
||||
void os_pipe_regist(os_pipe_t *pipe_handle, os_work_t *work_handle, int delay_ticks);
|
||||
void os_pipe_unregist(os_pipe_t *pipe_handle);
|
||||
|
||||
size_t os_pipe_poll_write(os_pipe_t *pipe_handle, uint8_t data);
|
||||
size_t os_pipe_fifo_fill(os_pipe_t *pipe_handle, const void *data, size_t size);
|
||||
size_t os_pipe_poll_read(os_pipe_t *pipe_handle, uint8_t *data);
|
||||
size_t os_pipe_fifo_read(os_pipe_t *pipe_handle, void *data, size_t size);
|
||||
|
||||
bool os_pipe_is_ne(os_pipe_t *pipe_handle);
|
||||
size_t os_pipe_get_valid_size(os_pipe_t *pipe_handle);
|
||||
size_t os_pipe_get_empty_size(os_pipe_t *pipe_handle);
|
||||
|
||||
void os_pipe_peek_valid(os_pipe_t *pipe_handle, void **dst_base, size_t *dst_size);
|
||||
void os_pipe_peek_empty(os_pipe_t *pipe_handle, void **dst_base, size_t *dst_size);
|
||||
|
||||
bool os_pipe_is_valid(os_pipe_t *pipe_handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif /* #ifdef __cplusplus */
|
||||
|
||||
#endif /* __OS_IPC_H__ */
|
||||
44
components/system/include/os/os_mutex.h
Executable file
44
components/system/include/os/os_mutex.h
Executable file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @file os_mutex.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_MUTEX_H__
|
||||
#define __OS_MUTEX_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
#include "os/os_thread.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef os_handle_t os_mutex_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_mutex_handle_t handle;
|
||||
} os_mutex_t;
|
||||
|
||||
os_state os_mutex_create(os_mutex_t *mutex);
|
||||
os_state os_mutex_delete(os_mutex_t *mutex);
|
||||
|
||||
os_state os_mutex_lock(os_mutex_t *mutex, os_time_t wait_ms);
|
||||
os_state os_mutex_unlock(os_mutex_t *mutex);
|
||||
|
||||
os_thread_handle_t os_mutex_get_holder(os_mutex_t *mutex);
|
||||
|
||||
bool os_mutex_is_valid(os_mutex_t *mutex);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_MUTEX_H__ */
|
||||
41
components/system/include/os/os_semaphore.h
Executable file
41
components/system/include/os/os_semaphore.h
Executable file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file os_semaphore.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_SEMAPHORE_H__
|
||||
#define __OS_SEMAPHORE_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef os_handle_t os_sem_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_sem_handle_t handle;
|
||||
} os_sem_t;
|
||||
|
||||
os_state os_sem_create(os_sem_t *sem, size_t init_value, size_t max_value);
|
||||
os_state os_sem_delete(os_sem_t *sem);
|
||||
|
||||
os_state os_sem_take(os_sem_t *sem, os_time_t wait_ms);
|
||||
os_state os_sem_release(os_sem_t *sem);
|
||||
|
||||
bool os_sem_is_valid(os_sem_t *sem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_SEMAPHORE_H__ */
|
||||
56
components/system/include/os/os_service.h
Executable file
56
components/system/include/os/os_service.h
Executable file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file os_service.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_SERVER_H__
|
||||
#define __OS_SERVER_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
int os_start(void *heap_mem, size_t heap_size);
|
||||
|
||||
void os_int_entry(void);
|
||||
void os_int_exit(void);
|
||||
|
||||
bool os_is_isr_context(void);
|
||||
|
||||
void os_interrupt_disable(void);
|
||||
void os_interrupt_enable(void);
|
||||
|
||||
void os_scheduler_suspend(void);
|
||||
void os_scheduler_resume(void);
|
||||
|
||||
bool os_scheduler_is_running(void);
|
||||
|
||||
void os_sys_print_info(void);
|
||||
|
||||
os_time_t os_get_sys_time(void);
|
||||
|
||||
size_t os_get_sys_ticks(void);
|
||||
|
||||
os_time_t os_calc_ticks_to_msec(size_t ticks);
|
||||
size_t os_calc_msec_to_ticks(os_time_t msec);
|
||||
|
||||
size_t os_cpu_usage(void);
|
||||
|
||||
int os_get_err(void);
|
||||
|
||||
void os_set_err(int err);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_SERVER_H__ */
|
||||
68
components/system/include/os/os_thread.h
Executable file
68
components/system/include/os/os_thread.h
Executable file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* @file os_thread.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_THREAD_H__
|
||||
#define __OS_THREAD_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef os_handle_t os_thread_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_thread_handle_t handle;
|
||||
} os_thread_t;
|
||||
|
||||
typedef void (*os_thread_entry_t)(void *);
|
||||
|
||||
os_state os_thread_init(os_thread_t *thread,
|
||||
const char *name,
|
||||
os_thread_entry_t entry,
|
||||
void *arg,
|
||||
void *stack_base,
|
||||
size_t stack_size,
|
||||
os_priority priority);
|
||||
|
||||
os_state os_thread_create(os_thread_t *thread,
|
||||
const char *name,
|
||||
os_thread_entry_t entry,
|
||||
void *arg,
|
||||
size_t stack_size,
|
||||
os_priority priority);
|
||||
|
||||
os_state os_thread_delete(os_thread_t *thread);
|
||||
|
||||
void os_thread_sleep(os_time_t msec);
|
||||
|
||||
void os_thread_yield(void);
|
||||
|
||||
void os_thread_suspend(os_thread_t *thread);
|
||||
|
||||
void os_thread_resume(os_thread_t *thread);
|
||||
|
||||
os_thread_handle_t os_thread_get_self(void);
|
||||
|
||||
const char *os_thread_get_name(os_thread_t *thread);
|
||||
|
||||
size_t os_thread_stack_min(os_thread_t *thread);
|
||||
|
||||
bool os_thread_is_valid(os_thread_t *thread);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_THREAD_H__ */
|
||||
55
components/system/include/os/os_timer.h
Executable file
55
components/system/include/os/os_timer.h
Executable file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @file os_timer.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_TIMER_H__
|
||||
#define __OS_TIMER_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef os_handle_t os_timer_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_timer_handle_t handle;
|
||||
} os_timer_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
OS_TIMER_ONCE = 0, /* one shot timer */
|
||||
OS_TIMER_PERIODIC = 1 /* periodic timer */
|
||||
} os_timer_type_t;
|
||||
|
||||
typedef void (*os_timer_cb_fn)(void *arg);
|
||||
|
||||
os_state os_timer_create(os_timer_t *timer, os_timer_cb_fn cb, void *arg);
|
||||
|
||||
os_state os_timer_delete(os_timer_t *timer);
|
||||
|
||||
os_state os_timer_set_period(os_timer_t *timer, os_timer_type_t type, os_time_t period_ms);
|
||||
|
||||
os_state os_timer_start(os_timer_t *timer);
|
||||
|
||||
os_state os_timer_stop(os_timer_t *timer);
|
||||
|
||||
bool os_timer_is_pending(os_timer_t *timer);
|
||||
|
||||
bool os_timer_is_valid(os_timer_t *timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OS_TIMER_H__ */
|
||||
71
components/system/include/os/os_work.h
Executable file
71
components/system/include/os/os_work.h
Executable file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* @file os_work.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __OS_WORK_H__
|
||||
#define __OS_WORK_H__
|
||||
|
||||
#include "os/os_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* #ifdef __cplusplus */
|
||||
|
||||
typedef os_handle_t os_work_q_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_work_q_handle_t handle;
|
||||
} os_work_q_t;
|
||||
|
||||
extern os_work_q_t *default_os_work_q_hdl;
|
||||
|
||||
os_state os_work_q_create(os_work_q_t *work_q_handle,
|
||||
const char *name,
|
||||
size_t stack_size,
|
||||
os_priority priority);
|
||||
|
||||
os_state os_work_q_delete(os_work_q_t *work_q_handle);
|
||||
|
||||
bool os_work_q_is_valid(os_work_q_t *work_q_handle);
|
||||
bool os_work_q_delayed_state(os_work_q_t *work_q_handle);
|
||||
bool os_work_q_ready_state(os_work_q_t *work_q_handle);
|
||||
|
||||
typedef os_handle_t os_work_handle_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_work_handle_t handle;
|
||||
} os_work_t;
|
||||
|
||||
typedef void (*os_work_fn)(void *arg);
|
||||
|
||||
os_state os_work_create(os_work_t *work_handle, const char *name, os_work_fn work_route, void *arg, uint8_t sub_prior);
|
||||
void os_work_delete(os_work_t *work_handle);
|
||||
|
||||
bool os_work_is_valid(os_work_t *work_handle);
|
||||
bool os_work_is_pending(os_work_t *work_handle);
|
||||
os_time_t os_work_time_remain(os_work_t *work_handle);
|
||||
|
||||
void os_work_submit(os_work_q_t *work_q_handle, os_work_t *work_handle, os_time_t delay_ms);
|
||||
void os_work_resume(os_work_t *work_handle, os_time_t delay_ms);
|
||||
void os_work_suspend(os_work_t *work_handle);
|
||||
|
||||
void os_work_yield(os_time_t ms);
|
||||
void os_work_sleep(os_time_t ms);
|
||||
void os_work_later(os_time_t ms);
|
||||
void os_work_later_until(os_time_t ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif /* #ifdef __cplusplus */
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user