初始化版本
This commit is contained in:
67
osal/include/osal.h
Normal file
67
osal/include/osal.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* OSAL (Operating System Abstraction Layer) Main Header
|
||||
*/
|
||||
|
||||
#ifndef __OSAL_H__
|
||||
#define __OSAL_H__
|
||||
|
||||
#include "osal_config.h"
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* System Control */
|
||||
osal_err_t osal_init(void);
|
||||
osal_err_t osal_start(void);
|
||||
|
||||
/* Error Handling */
|
||||
osal_err_t osal_get_errno(int native_err);
|
||||
|
||||
/* System Service */
|
||||
osal_tick_t osal_tick_get(void);
|
||||
osal_err_t osal_delay(osal_tick_t tick);
|
||||
osal_err_t osal_mdelay(osal_int32_t ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Include Modules */
|
||||
|
||||
#if OSAL_USING_THREAD
|
||||
#include "osal_thread.h"
|
||||
#endif
|
||||
|
||||
#if OSAL_USING_SEMAPHORE
|
||||
#include "osal_sem.h"
|
||||
#endif
|
||||
|
||||
#if OSAL_USING_MUTEX
|
||||
#include "osal_mutex.h"
|
||||
#endif
|
||||
|
||||
#if OSAL_USING_EVENT
|
||||
#include "osal_event.h"
|
||||
#endif
|
||||
|
||||
#if OSAL_USING_MESSAGEQUEUE
|
||||
#include "osal_mq.h"
|
||||
#endif
|
||||
|
||||
#if OSAL_USING_TIMER
|
||||
#include "osal_timer.h"
|
||||
#endif
|
||||
|
||||
#if OSAL_USING_HEAP
|
||||
#include "osal_mem.h"
|
||||
#endif
|
||||
|
||||
#include "osal_irq.h"
|
||||
|
||||
#if OSAL_USING_LOG
|
||||
#include "osal_log.h"
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_H__ */
|
||||
34
osal/include/osal_config.h
Normal file
34
osal/include/osal_config.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __OSAL_CONFIG_H__
|
||||
#define __OSAL_CONFIG_H__
|
||||
|
||||
/* Enable/Disable OSAL modules */
|
||||
#define OSAL_USING_THREAD 1
|
||||
#define OSAL_USING_SEMAPHORE 1
|
||||
#define OSAL_USING_MUTEX 1
|
||||
#define OSAL_USING_EVENT 1
|
||||
#define OSAL_USING_MESSAGEQUEUE 1
|
||||
#define OSAL_USING_TIMER 1
|
||||
#define OSAL_USING_HEAP 1
|
||||
#define OSAL_USING_LOG 1
|
||||
|
||||
/* Log Levels */
|
||||
#define OSAL_LOG_LEVEL_NONE 0
|
||||
#define OSAL_LOG_LEVEL_ERROR 1
|
||||
#define OSAL_LOG_LEVEL_WARN 2
|
||||
#define OSAL_LOG_LEVEL_INFO 3
|
||||
#define OSAL_LOG_LEVEL_DEBUG 4
|
||||
|
||||
/* Current Log Level */
|
||||
#ifndef OSAL_LOG_LEVEL
|
||||
#define OSAL_LOG_LEVEL OSAL_LOG_LEVEL_DEBUG
|
||||
#endif
|
||||
|
||||
/* Log Buffer Size */
|
||||
#ifndef OSAL_LOG_BUF_SIZE
|
||||
#define OSAL_LOG_BUF_SIZE 256
|
||||
#endif
|
||||
|
||||
/* Assert */
|
||||
#define OSAL_USING_ASSERT 1
|
||||
|
||||
#endif /* __OSAL_CONFIG_H__ */
|
||||
80
osal/include/osal_def.h
Normal file
80
osal/include/osal_def.h
Normal file
@ -0,0 +1,80 @@
|
||||
#ifndef __OSAL_DEF_H__
|
||||
#define __OSAL_DEF_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Error codes */
|
||||
typedef int osal_err_t;
|
||||
|
||||
#define OSAL_OK 0 /* Operation successful */
|
||||
#define OSAL_ERROR (-1) /* Operation failed */
|
||||
#define OSAL_ETIMEOUT (-2) /* Timeout */
|
||||
#define OSAL_EFULL (-3) /* Resource full */
|
||||
#define OSAL_EEMPTY (-4) /* Resource empty */
|
||||
#define OSAL_ENOMEM (-5) /* No memory */
|
||||
#define OSAL_ENOSYS (-6) /* No system */
|
||||
#define OSAL_EBUSY (-7) /* Busy */
|
||||
#define OSAL_EIO (-8) /* IO error */
|
||||
#define OSAL_EINTR (-9) /* Interrupted */
|
||||
#define OSAL_INVAL (-10) /* Invalid argument */
|
||||
|
||||
/* Backward compatibility */
|
||||
#define OSAL_TIMEOUT OSAL_ETIMEOUT
|
||||
#define OSAL_NO_MEMORY OSAL_ENOMEM
|
||||
|
||||
/* Wait Forever */
|
||||
#define OSAL_WAIT_FOREVER ((osal_tick_t)-1)
|
||||
|
||||
/* Assert */
|
||||
#ifndef OSAL_ASSERT
|
||||
#ifdef OSAL_DEBUG
|
||||
#define OSAL_ASSERT(EX) \
|
||||
if (!(EX)) \
|
||||
{ \
|
||||
extern void osal_kprintf(const char *fmt, ...); \
|
||||
osal_kprintf("(%s) has assert failed at %s:%d.\n", #EX, __FUNCTION__, __LINE__); \
|
||||
while (1); \
|
||||
}
|
||||
#else
|
||||
#define OSAL_ASSERT(EX)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Priority */
|
||||
typedef unsigned char osal_priority_t;
|
||||
|
||||
#define OSAL_PRIORITY_LOWEST 255 /* Lowest priority */
|
||||
#define OSAL_PRIORITY_HIGHEST 0 /* Highest priority */
|
||||
|
||||
/* Basic Types */
|
||||
typedef unsigned int osal_tick_t;
|
||||
typedef unsigned int osal_size_t;
|
||||
typedef int osal_int32_t;
|
||||
typedef unsigned int osal_uint32_t;
|
||||
typedef unsigned char osal_uint8_t;
|
||||
typedef unsigned short osal_uint16_t;
|
||||
typedef void* osal_handle_t;
|
||||
|
||||
/* IPC Flags */
|
||||
#define OSAL_FLAG_FIFO 0x00 /* FIFO scheduling */
|
||||
#define OSAL_FLAG_PRIO 0x01 /* Priority scheduling */
|
||||
|
||||
/* Event Flags */
|
||||
#define OSAL_EVENT_AND 0x01 /* Logic AND */
|
||||
#define OSAL_EVENT_OR 0x02 /* Logic OR */
|
||||
#define OSAL_EVENT_CLEAR 0x04 /* Clear event */
|
||||
|
||||
/* Timer Flags */
|
||||
#define OSAL_TIMER_FLAG_ONE_SHOT 0x00 /* One shot */
|
||||
#define OSAL_TIMER_FLAG_PERIODIC 0x01 /* Periodic */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_DEF_H__ */
|
||||
26
osal/include/osal_event.h
Normal file
26
osal/include/osal_event.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef __OSAL_EVENT_H__
|
||||
#define __OSAL_EVENT_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* osal_event_t;
|
||||
|
||||
/* Event API */
|
||||
osal_event_t osal_event_create(const char* name);
|
||||
osal_err_t osal_event_delete(osal_event_t event);
|
||||
osal_err_t osal_event_send(osal_event_t event, osal_uint32_t set);
|
||||
osal_err_t osal_event_recv(osal_event_t event,
|
||||
osal_uint32_t set,
|
||||
osal_uint8_t opt,
|
||||
osal_int32_t timeout,
|
||||
osal_uint32_t* recved);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_EVENT_H__ */
|
||||
19
osal/include/osal_irq.h
Normal file
19
osal/include/osal_irq.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef __OSAL_IRQ_H__
|
||||
#define __OSAL_IRQ_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* IRQ/Critical Section API */
|
||||
void osal_enter_critical(void);
|
||||
void osal_exit_critical(void);
|
||||
osal_uint8_t osal_interrupt_get_nest(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_IRQ_H__ */
|
||||
44
osal/include/osal_log.h
Normal file
44
osal/include/osal_log.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef __OSAL_LOG_H__
|
||||
#define __OSAL_LOG_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
#include "osal_config.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Log API */
|
||||
void osal_kprintf(const char *fmt, ...);
|
||||
|
||||
/* Log Macros */
|
||||
#if (OSAL_LOG_LEVEL >= OSAL_LOG_LEVEL_ERROR)
|
||||
#define osal_log_e(fmt, ...) osal_kprintf("[Error] " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define osal_log_e(fmt, ...)
|
||||
#endif
|
||||
|
||||
#if (OSAL_LOG_LEVEL >= OSAL_LOG_LEVEL_WARN)
|
||||
#define osal_log_w(fmt, ...) osal_kprintf("[Warning] " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define osal_log_w(fmt, ...)
|
||||
#endif
|
||||
|
||||
#if (OSAL_LOG_LEVEL >= OSAL_LOG_LEVEL_INFO)
|
||||
#define osal_log_i(fmt, ...) osal_kprintf("[Info] " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define osal_log_i(fmt, ...)
|
||||
#endif
|
||||
|
||||
#if (OSAL_LOG_LEVEL >= OSAL_LOG_LEVEL_DEBUG)
|
||||
#define osal_log_d(fmt, ...) osal_kprintf("[Debug] " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define osal_log_d(fmt, ...)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_LOG_H__ */
|
||||
23
osal/include/osal_mem.h
Normal file
23
osal/include/osal_mem.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __OSAL_MEM_H__
|
||||
#define __OSAL_MEM_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Memory API */
|
||||
void* osal_malloc(osal_size_t size);
|
||||
void* osal_calloc(osal_size_t count, osal_size_t size);
|
||||
void* osal_realloc(void* ptr, osal_size_t size);
|
||||
void osal_free(void* ptr);
|
||||
|
||||
void* osal_malloc_align(osal_size_t size, osal_size_t align);
|
||||
void osal_free_align(void* ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_MEM_H__ */
|
||||
25
osal/include/osal_mq.h
Normal file
25
osal/include/osal_mq.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef __OSAL_MQ_H__
|
||||
#define __OSAL_MQ_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* osal_mq_t;
|
||||
|
||||
/* Message Queue API */
|
||||
osal_mq_t osal_mq_create(const char* name,
|
||||
osal_size_t msg_size,
|
||||
osal_size_t max_msgs);
|
||||
osal_err_t osal_mq_delete(osal_mq_t mq);
|
||||
osal_err_t osal_mq_send(osal_mq_t mq, const void* buffer, osal_size_t size);
|
||||
osal_err_t osal_mq_urgent(osal_mq_t mq, const void* buffer, osal_size_t size);
|
||||
osal_err_t osal_mq_recv(osal_mq_t mq, void* buffer, osal_size_t size, osal_int32_t timeout);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_MQ_H__ */
|
||||
23
osal/include/osal_mutex.h
Normal file
23
osal/include/osal_mutex.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __OSAL_MUTEX_H__
|
||||
#define __OSAL_MUTEX_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* osal_mutex_t;
|
||||
|
||||
/* Mutex API */
|
||||
osal_mutex_t osal_mutex_create(const char* name);
|
||||
osal_err_t osal_mutex_delete(osal_mutex_t mutex);
|
||||
osal_err_t osal_mutex_take(osal_mutex_t mutex, osal_int32_t timeout);
|
||||
osal_err_t osal_mutex_trytake(osal_mutex_t mutex);
|
||||
osal_err_t osal_mutex_release(osal_mutex_t mutex);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_MUTEX_H__ */
|
||||
23
osal/include/osal_sem.h
Normal file
23
osal/include/osal_sem.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __OSAL_SEM_H__
|
||||
#define __OSAL_SEM_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* osal_sem_t;
|
||||
|
||||
/* Semaphore API */
|
||||
osal_sem_t osal_sem_create(const char* name, osal_uint32_t value);
|
||||
osal_err_t osal_sem_delete(osal_sem_t sem);
|
||||
osal_err_t osal_sem_take(osal_sem_t sem, osal_int32_t timeout);
|
||||
osal_err_t osal_sem_trytake(osal_sem_t sem);
|
||||
osal_err_t osal_sem_release(osal_sem_t sem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_SEM_H__ */
|
||||
34
osal/include/osal_thread.h
Normal file
34
osal/include/osal_thread.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __OSAL_THREAD_H__
|
||||
#define __OSAL_THREAD_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* osal_thread_t;
|
||||
typedef void (*osal_thread_entry)(void* parameter);
|
||||
|
||||
/* Thread API */
|
||||
osal_thread_t osal_thread_create(const char* name,
|
||||
osal_thread_entry entry,
|
||||
void* parameter,
|
||||
osal_size_t stack_size,
|
||||
osal_priority_t priority);
|
||||
|
||||
osal_err_t osal_thread_delete(osal_thread_t thread);
|
||||
osal_err_t osal_thread_start(osal_thread_t thread);
|
||||
osal_err_t osal_thread_suspend(osal_thread_t thread);
|
||||
osal_err_t osal_thread_resume(osal_thread_t thread);
|
||||
osal_err_t osal_thread_yield(void);
|
||||
osal_err_t osal_thread_control(osal_thread_t thread, int cmd, void* arg);
|
||||
osal_thread_t osal_thread_self(void);
|
||||
osal_err_t osal_thread_delay(osal_tick_t tick);
|
||||
osal_err_t osal_thread_mdelay(osal_int32_t ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_THREAD_H__ */
|
||||
27
osal/include/osal_timer.h
Normal file
27
osal/include/osal_timer.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef __OSAL_TIMER_H__
|
||||
#define __OSAL_TIMER_H__
|
||||
|
||||
#include "osal_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* osal_timer_t;
|
||||
typedef void (*osal_timer_callback)(void* parameter);
|
||||
|
||||
/* Timer API */
|
||||
osal_timer_t osal_timer_create(const char* name,
|
||||
osal_timer_callback timeout,
|
||||
void* parameter,
|
||||
osal_tick_t time,
|
||||
osal_uint8_t flag);
|
||||
osal_err_t osal_timer_delete(osal_timer_t timer);
|
||||
osal_err_t osal_timer_start(osal_timer_t timer);
|
||||
osal_err_t osal_timer_stop(osal_timer_t timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __OSAL_TIMER_H__ */
|
||||
500
osal/src/osal_test.c
Normal file
500
osal/src/osal_test.c
Normal file
@ -0,0 +1,500 @@
|
||||
/*
|
||||
* OSAL (Operating System Abstraction Layer) 测试用例
|
||||
* 功能:验证OSAL API的正确性
|
||||
* 依赖:OSAL和RT-Thread Nano
|
||||
*/
|
||||
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
/* 测试状态 */
|
||||
static int test_status = 0;
|
||||
|
||||
/* 测试线程参数 */
|
||||
struct test_thread_param
|
||||
{
|
||||
osal_sem_t sem;
|
||||
int count;
|
||||
};
|
||||
|
||||
/* 测试线程入口函数 */
|
||||
static void test_thread_entry(void* parameter)
|
||||
{
|
||||
struct test_thread_param* param = (struct test_thread_param*)parameter;
|
||||
|
||||
osal_kprintf("Test thread started\n");
|
||||
|
||||
/* 延时一段时间 */
|
||||
osal_thread_mdelay(1000);
|
||||
|
||||
/* 增加计数 */
|
||||
param->count++;
|
||||
|
||||
/* 释放信号量 */
|
||||
osal_sem_release(param->sem);
|
||||
|
||||
osal_kprintf("Test thread finished\n");
|
||||
}
|
||||
|
||||
/* 测试定时器回调函数 */
|
||||
static void test_timer_callback(void* parameter)
|
||||
{
|
||||
osal_kprintf("Test timer callback\n");
|
||||
test_status = 1;
|
||||
}
|
||||
|
||||
/* 测试内存分配 */
|
||||
static void test_memory(void)
|
||||
{
|
||||
void* ptr1;
|
||||
void* ptr2;
|
||||
|
||||
osal_kprintf("Testing memory management...\n");
|
||||
|
||||
/* 测试内存分配 */
|
||||
ptr1 = osal_malloc(100);
|
||||
if (ptr1 != NULL)
|
||||
{
|
||||
osal_kprintf("osal_malloc passed\n");
|
||||
|
||||
/* 测试内存释放 */
|
||||
osal_free(ptr1);
|
||||
osal_kprintf("osal_free passed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_kprintf("osal_malloc failed\n");
|
||||
test_status = -1;
|
||||
}
|
||||
|
||||
/* 测试对齐内存分配 */
|
||||
ptr2 = osal_malloc_align(100, 4);
|
||||
if (ptr2 != NULL)
|
||||
{
|
||||
osal_kprintf("osal_malloc_align passed\n");
|
||||
|
||||
/* 测试对齐内存释放 */
|
||||
osal_free_align(ptr2);
|
||||
osal_kprintf("osal_free_align passed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_kprintf("osal_malloc_align failed\n");
|
||||
test_status = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 测试线程管理 */
|
||||
static void test_thread(void)
|
||||
{
|
||||
osal_thread_t thread;
|
||||
osal_sem_t sem;
|
||||
struct test_thread_param param;
|
||||
|
||||
osal_kprintf("Testing thread management...\n");
|
||||
|
||||
/* 创建信号量 */
|
||||
sem = osal_sem_create("test_sem", 0);
|
||||
if (sem == NULL)
|
||||
{
|
||||
osal_kprintf("osal_sem_create failed\n");
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 初始化参数 */
|
||||
param.sem = sem;
|
||||
param.count = 0;
|
||||
|
||||
/* 创建线程 */
|
||||
thread = osal_thread_create("test_thread",
|
||||
test_thread_entry,
|
||||
¶m,
|
||||
512,
|
||||
10);
|
||||
|
||||
if (thread == NULL)
|
||||
{
|
||||
osal_kprintf("osal_thread_create failed\n");
|
||||
osal_sem_delete(sem);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_thread_create passed\n");
|
||||
|
||||
/* 启动线程 */
|
||||
if (osal_thread_start(thread) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_thread_start failed\n");
|
||||
osal_thread_delete(thread);
|
||||
osal_sem_delete(sem);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_thread_start passed\n");
|
||||
|
||||
/* 等待线程完成 */
|
||||
if (osal_sem_take(sem, 5000) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_sem_take failed\n");
|
||||
osal_thread_delete(thread);
|
||||
osal_sem_delete(sem);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("Thread count: %d\n", param.count);
|
||||
|
||||
/* 删除线程和信号量 */
|
||||
osal_thread_delete(thread);
|
||||
osal_sem_delete(sem);
|
||||
|
||||
osal_kprintf("Thread test passed\n");
|
||||
}
|
||||
|
||||
/* 测试信号量 */
|
||||
static void test_semaphore(void)
|
||||
{
|
||||
osal_sem_t sem;
|
||||
int i;
|
||||
|
||||
osal_kprintf("Testing semaphore...\n");
|
||||
|
||||
/* 创建信号量 */
|
||||
sem = osal_sem_create("test_sem", 3);
|
||||
if (sem == NULL)
|
||||
{
|
||||
osal_kprintf("osal_sem_create failed\n");
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 测试获取信号量 */
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
if (osal_sem_take(sem, 0) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_sem_take failed\n");
|
||||
osal_sem_delete(sem);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
osal_kprintf("osal_sem_take passed\n");
|
||||
|
||||
/* 测试尝试获取信号量(应该失败) */
|
||||
if (osal_sem_trytake(sem) == OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_sem_trytake should failed\n");
|
||||
test_status = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_kprintf("osal_sem_trytake passed\n");
|
||||
}
|
||||
|
||||
/* 测试释放信号量 */
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
if (osal_sem_release(sem) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_sem_release failed\n");
|
||||
osal_sem_delete(sem);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
osal_kprintf("osal_sem_release passed\n");
|
||||
|
||||
/* 删除信号量 */
|
||||
osal_sem_delete(sem);
|
||||
|
||||
osal_kprintf("Semaphore test passed\n");
|
||||
}
|
||||
|
||||
/* 测试互斥量 */
|
||||
static void test_mutex(void)
|
||||
{
|
||||
osal_mutex_t mutex;
|
||||
|
||||
osal_kprintf("Testing mutex...\n");
|
||||
|
||||
/* 创建互斥量 */
|
||||
mutex = osal_mutex_create("test_mutex");
|
||||
if (mutex == NULL)
|
||||
{
|
||||
osal_kprintf("osal_mutex_create failed\n");
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 测试获取互斥量 */
|
||||
if (osal_mutex_take(mutex, 1000) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_mutex_take failed\n");
|
||||
osal_mutex_delete(mutex);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_mutex_take passed\n");
|
||||
|
||||
/* 测试尝试获取互斥量(应该失败) */
|
||||
if (osal_mutex_trytake(mutex) == OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_mutex_trytake should failed\n");
|
||||
test_status = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_kprintf("osal_mutex_trytake passed\n");
|
||||
}
|
||||
|
||||
/* 测试释放互斥量 */
|
||||
if (osal_mutex_release(mutex) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_mutex_release failed\n");
|
||||
osal_mutex_delete(mutex);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_mutex_release passed\n");
|
||||
|
||||
/* 删除互斥量 */
|
||||
osal_mutex_delete(mutex);
|
||||
|
||||
osal_kprintf("Mutex test passed\n");
|
||||
}
|
||||
|
||||
/* 测试事件 */
|
||||
static void test_event(void)
|
||||
{
|
||||
osal_event_t event;
|
||||
osal_uint32_t recved;
|
||||
|
||||
osal_kprintf("Testing event...\n");
|
||||
|
||||
/* 创建事件 */
|
||||
event = osal_event_create("test_event");
|
||||
if (event == NULL)
|
||||
{
|
||||
osal_kprintf("osal_event_create failed\n");
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 发送事件 */
|
||||
if (osal_event_send(event, 0x0001) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_event_send failed\n");
|
||||
osal_event_delete(event);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_event_send passed\n");
|
||||
|
||||
/* 接收事件 */
|
||||
if (osal_event_recv(event, 0x0001, OSAL_EVENT_OR | OSAL_EVENT_CLEAR, 1000, &recved) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_event_recv failed\n");
|
||||
osal_event_delete(event);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("Received event: 0x%04X\n", recved);
|
||||
osal_kprintf("osal_event_recv passed\n");
|
||||
|
||||
/* 删除事件 */
|
||||
osal_event_delete(event);
|
||||
|
||||
osal_kprintf("Event test passed\n");
|
||||
}
|
||||
|
||||
/* 测试消息队列 */
|
||||
static void test_message_queue(void)
|
||||
{
|
||||
osal_mq_t mq;
|
||||
char send_msg[10] = "Hello";
|
||||
char recv_msg[10];
|
||||
|
||||
osal_kprintf("Testing message queue...\n");
|
||||
|
||||
/* 创建消息队列 */
|
||||
mq = osal_mq_create("test_mq", 10, 5);
|
||||
if (mq == NULL)
|
||||
{
|
||||
osal_kprintf("osal_mq_create failed\n");
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 发送消息 */
|
||||
if (osal_mq_send(mq, send_msg, sizeof(send_msg)) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_mq_send failed\n");
|
||||
osal_mq_delete(mq);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_mq_send passed\n");
|
||||
|
||||
/* 接收消息 */
|
||||
if (osal_mq_recv(mq, recv_msg, sizeof(recv_msg), 1000) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_mq_recv failed\n");
|
||||
osal_mq_delete(mq);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("Received message: %s\n", recv_msg);
|
||||
osal_kprintf("osal_mq_recv passed\n");
|
||||
|
||||
/* 删除消息队列 */
|
||||
osal_mq_delete(mq);
|
||||
|
||||
osal_kprintf("Message queue test passed\n");
|
||||
}
|
||||
|
||||
/* 测试定时器 */
|
||||
static void test_timer(void)
|
||||
{
|
||||
osal_timer_t timer;
|
||||
|
||||
osal_kprintf("Testing timer...\n");
|
||||
|
||||
/* 创建定时器 */
|
||||
timer = osal_timer_create("test_timer",
|
||||
test_timer_callback,
|
||||
NULL,
|
||||
1000,
|
||||
OSAL_TIMER_FLAG_ONE_SHOT);
|
||||
|
||||
if (timer == NULL)
|
||||
{
|
||||
osal_kprintf("osal_timer_create failed\n");
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_timer_create passed\n");
|
||||
|
||||
/* 启动定时器 */
|
||||
if (osal_timer_start(timer) != OSAL_OK)
|
||||
{
|
||||
osal_kprintf("osal_timer_start failed\n");
|
||||
osal_timer_delete(timer);
|
||||
test_status = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
osal_kprintf("osal_timer_start passed\n");
|
||||
|
||||
/* 等待定时器回调 */
|
||||
osal_thread_mdelay(2000);
|
||||
|
||||
/* 检查定时器是否触发 */
|
||||
if (test_status != 1)
|
||||
{
|
||||
osal_kprintf("Timer callback not triggered\n");
|
||||
test_status = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_kprintf("Timer callback triggered\n");
|
||||
}
|
||||
|
||||
/* 停止定时器 */
|
||||
osal_timer_stop(timer);
|
||||
|
||||
/* 删除定时器 */
|
||||
osal_timer_delete(timer);
|
||||
|
||||
osal_kprintf("Timer test passed\n");
|
||||
}
|
||||
|
||||
/* 测试系统服务 */
|
||||
static void test_system(void)
|
||||
{
|
||||
osal_tick_t tick1, tick2;
|
||||
|
||||
osal_kprintf("Testing system services...\n");
|
||||
|
||||
/* 测试获取系统tick */
|
||||
tick1 = osal_tick_get();
|
||||
osal_kprintf("Current tick: %d\n", tick1);
|
||||
|
||||
/* 测试延时 */
|
||||
osal_mdelay(1000);
|
||||
|
||||
tick2 = osal_tick_get();
|
||||
osal_kprintf("Tick after delay: %d\n", tick2);
|
||||
osal_kprintf("Tick difference: %d\n", tick2 - tick1);
|
||||
|
||||
/* 测试临界区 */
|
||||
osal_enter_critical();
|
||||
osal_kprintf("Entered critical section\n");
|
||||
|
||||
/* 测试获取中断嵌套级别 */
|
||||
osal_kprintf("Interrupt nest level: %d\n", osal_interrupt_get_nest());
|
||||
|
||||
osal_exit_critical();
|
||||
osal_kprintf("Exited critical section\n");
|
||||
|
||||
osal_kprintf("System services test passed\n");
|
||||
}
|
||||
|
||||
/* OSAL测试函数 */
|
||||
int osal_test(void)
|
||||
{
|
||||
osal_kprintf("\n=== OSAL Test Start ===\n");
|
||||
|
||||
/* 初始化测试状态 */
|
||||
test_status = 0;
|
||||
|
||||
/* 测试内存管理 */
|
||||
test_memory();
|
||||
|
||||
/* 测试线程管理 */
|
||||
test_thread();
|
||||
|
||||
/* 测试信号量 */
|
||||
test_semaphore();
|
||||
|
||||
/* 测试互斥量 */
|
||||
test_mutex();
|
||||
|
||||
/* 测试事件 */
|
||||
test_event();
|
||||
|
||||
/* 测试消息队列 */
|
||||
test_message_queue();
|
||||
|
||||
/* 测试定时器 */
|
||||
test_timer();
|
||||
|
||||
/* 测试系统服务 */
|
||||
test_system();
|
||||
|
||||
if (test_status == 0)
|
||||
{
|
||||
osal_kprintf("=== All OSAL tests passed! ===\n\n");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_kprintf("=== Some OSAL tests failed! ===\n\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 导出测试函数 */
|
||||
MSH_CMD_EXPORT(osal_test, OSAL test);
|
||||
68
osal/src/rtthread/osal_core.c
Normal file
68
osal/src/rtthread/osal_core.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
/* Error Code Mapping */
|
||||
osal_err_t osal_get_errno(int native_err)
|
||||
{
|
||||
rt_err_t err = (rt_err_t)native_err;
|
||||
|
||||
switch (err)
|
||||
{
|
||||
case RT_EOK:
|
||||
return OSAL_OK;
|
||||
case -RT_ERROR:
|
||||
return OSAL_ERROR;
|
||||
case -RT_ETIMEOUT:
|
||||
return OSAL_ETIMEOUT;
|
||||
case -RT_EFULL:
|
||||
return OSAL_EFULL;
|
||||
case -RT_EEMPTY:
|
||||
return OSAL_EEMPTY;
|
||||
case -RT_ENOMEM:
|
||||
return OSAL_ENOMEM;
|
||||
case -RT_ENOSYS:
|
||||
return OSAL_ENOSYS;
|
||||
case -RT_EBUSY:
|
||||
return OSAL_EBUSY;
|
||||
case -RT_EIO:
|
||||
return OSAL_EIO;
|
||||
case -RT_EINTR:
|
||||
return OSAL_EINTR;
|
||||
case -RT_EINVAL:
|
||||
return OSAL_INVAL;
|
||||
default:
|
||||
return OSAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* System Init */
|
||||
osal_err_t osal_init(void)
|
||||
{
|
||||
/* RT-Thread Nano initialization is done by system/startup */
|
||||
return OSAL_OK;
|
||||
}
|
||||
|
||||
/* System Start */
|
||||
osal_err_t osal_start(void)
|
||||
{
|
||||
/* RT-Thread Nano startup is done by system/startup */
|
||||
return OSAL_OK;
|
||||
}
|
||||
|
||||
/* Get System Tick */
|
||||
osal_tick_t osal_tick_get(void)
|
||||
{
|
||||
return rt_tick_get();
|
||||
}
|
||||
|
||||
/* Delay (Alias for thread delay) */
|
||||
osal_err_t osal_delay(osal_tick_t tick)
|
||||
{
|
||||
return osal_thread_delay(tick);
|
||||
}
|
||||
|
||||
/* Delay ms (Alias for thread mdelay) */
|
||||
osal_err_t osal_mdelay(osal_int32_t ms)
|
||||
{
|
||||
return osal_thread_mdelay(ms);
|
||||
}
|
||||
66
osal/src/rtthread/osal_event.c
Normal file
66
osal/src/rtthread/osal_event.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include "osal_event.h"
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
osal_event_t osal_event_create(const char* name)
|
||||
{
|
||||
OSAL_ASSERT(name != NULL);
|
||||
|
||||
rt_event_t event;
|
||||
|
||||
event = rt_event_create(name, RT_IPC_FLAG_FIFO);
|
||||
|
||||
return (osal_event_t)event;
|
||||
}
|
||||
|
||||
osal_err_t osal_event_delete(osal_event_t event)
|
||||
{
|
||||
if (event == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_event_delete((rt_event_t)event);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_event_send(osal_event_t event, osal_uint32_t set)
|
||||
{
|
||||
if (event == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_event_send((rt_event_t)event, set);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_event_recv(osal_event_t event,
|
||||
osal_uint32_t set,
|
||||
osal_uint8_t opt,
|
||||
osal_int32_t timeout,
|
||||
osal_uint32_t* recved)
|
||||
{
|
||||
if (event == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_uint32_t recv_set;
|
||||
rt_err_t ret;
|
||||
|
||||
ret = rt_event_recv((rt_event_t)event,
|
||||
set,
|
||||
opt,
|
||||
timeout,
|
||||
&recv_set);
|
||||
|
||||
if (recved != NULL)
|
||||
{
|
||||
*recved = recv_set;
|
||||
}
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
17
osal/src/rtthread/osal_irq.c
Normal file
17
osal/src/rtthread/osal_irq.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include "osal_irq.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
void osal_enter_critical(void)
|
||||
{
|
||||
rt_enter_critical();
|
||||
}
|
||||
|
||||
void osal_exit_critical(void)
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
osal_uint8_t osal_interrupt_get_nest(void)
|
||||
{
|
||||
return rt_interrupt_get_nest();
|
||||
}
|
||||
25
osal/src/rtthread/osal_log.c
Normal file
25
osal/src/rtthread/osal_log.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include "osal_log.h"
|
||||
#include "osal_config.h"
|
||||
#include <rtthread.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
void osal_kprintf(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char log_buf[OSAL_LOG_BUF_SIZE];
|
||||
rt_int32_t length;
|
||||
|
||||
va_start(args, fmt);
|
||||
/* rt_vsnprintf is provided by RT-Thread kservice */
|
||||
length = rt_vsnprintf(log_buf, sizeof(log_buf) - 1, fmt, args);
|
||||
if (length > 0)
|
||||
{
|
||||
log_buf[length] = '\0';
|
||||
rt_kprintf("%s", log_buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("osal_kprintf: vsnprintf failed or empty\n");
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
32
osal/src/rtthread/osal_mem.c
Normal file
32
osal/src/rtthread/osal_mem.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include "osal_mem.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
void* osal_malloc(osal_size_t size)
|
||||
{
|
||||
return rt_malloc(size);
|
||||
}
|
||||
|
||||
void* osal_calloc(osal_size_t count, osal_size_t size)
|
||||
{
|
||||
return rt_calloc(count, size);
|
||||
}
|
||||
|
||||
void* osal_realloc(void* ptr, osal_size_t size)
|
||||
{
|
||||
return rt_realloc(ptr, size);
|
||||
}
|
||||
|
||||
void osal_free(void* ptr)
|
||||
{
|
||||
rt_free(ptr);
|
||||
}
|
||||
|
||||
void* osal_malloc_align(osal_size_t size, osal_size_t align)
|
||||
{
|
||||
return rt_malloc_align(size, align);
|
||||
}
|
||||
|
||||
void osal_free_align(void* ptr)
|
||||
{
|
||||
rt_free_align(ptr);
|
||||
}
|
||||
66
osal/src/rtthread/osal_mq.c
Normal file
66
osal/src/rtthread/osal_mq.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include "osal_mq.h"
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
osal_mq_t osal_mq_create(const char* name,
|
||||
osal_size_t msg_size,
|
||||
osal_size_t max_msgs)
|
||||
{
|
||||
OSAL_ASSERT(name != NULL);
|
||||
OSAL_ASSERT(msg_size > 0);
|
||||
OSAL_ASSERT(max_msgs > 0);
|
||||
|
||||
rt_mq_t mq;
|
||||
|
||||
mq = rt_mq_create(name, msg_size, max_msgs, RT_IPC_FLAG_FIFO);
|
||||
|
||||
return (osal_mq_t)mq;
|
||||
}
|
||||
|
||||
osal_err_t osal_mq_delete(osal_mq_t mq)
|
||||
{
|
||||
if (mq == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mq_delete((rt_mq_t)mq);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_mq_send(osal_mq_t mq, const void* buffer, osal_size_t size)
|
||||
{
|
||||
if (mq == NULL || buffer == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mq_send((rt_mq_t)mq, (void*)buffer, size);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_mq_urgent(osal_mq_t mq, const void* buffer, osal_size_t size)
|
||||
{
|
||||
if (mq == NULL || buffer == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mq_urgent((rt_mq_t)mq, (void*)buffer, size);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_mq_recv(osal_mq_t mq, void* buffer, osal_size_t size, osal_int32_t timeout)
|
||||
{
|
||||
if (mq == NULL || buffer == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mq_recv((rt_mq_t)mq, buffer, size, timeout);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
62
osal/src/rtthread/osal_mutex.c
Normal file
62
osal/src/rtthread/osal_mutex.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include "osal_mutex.h"
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
osal_mutex_t osal_mutex_create(const char* name)
|
||||
{
|
||||
OSAL_ASSERT(name != NULL);
|
||||
|
||||
rt_mutex_t mutex;
|
||||
|
||||
mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
|
||||
|
||||
return (osal_mutex_t)mutex;
|
||||
}
|
||||
|
||||
osal_err_t osal_mutex_delete(osal_mutex_t mutex)
|
||||
{
|
||||
if (mutex == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mutex_delete((rt_mutex_t)mutex);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_mutex_take(osal_mutex_t mutex, osal_int32_t timeout)
|
||||
{
|
||||
if (mutex == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mutex_take((rt_mutex_t)mutex, timeout);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_mutex_trytake(osal_mutex_t mutex)
|
||||
{
|
||||
if (mutex == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mutex_trytake((rt_mutex_t)mutex);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_mutex_release(osal_mutex_t mutex)
|
||||
{
|
||||
if (mutex == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_mutex_release((rt_mutex_t)mutex);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
62
osal/src/rtthread/osal_sem.c
Normal file
62
osal/src/rtthread/osal_sem.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include "osal_sem.h"
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
osal_sem_t osal_sem_create(const char* name, osal_uint32_t value)
|
||||
{
|
||||
OSAL_ASSERT(name != NULL);
|
||||
|
||||
rt_sem_t sem;
|
||||
|
||||
sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
|
||||
|
||||
return (osal_sem_t)sem;
|
||||
}
|
||||
|
||||
osal_err_t osal_sem_delete(osal_sem_t sem)
|
||||
{
|
||||
if (sem == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_sem_delete((rt_sem_t)sem);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_sem_take(osal_sem_t sem, osal_int32_t timeout)
|
||||
{
|
||||
if (sem == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_sem_take((rt_sem_t)sem, timeout);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_sem_trytake(osal_sem_t sem)
|
||||
{
|
||||
if (sem == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_sem_trytake((rt_sem_t)sem);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_sem_release(osal_sem_t sem)
|
||||
{
|
||||
if (sem == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_sem_release((rt_sem_t)sem);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
110
osal/src/rtthread/osal_thread.c
Normal file
110
osal/src/rtthread/osal_thread.c
Normal file
@ -0,0 +1,110 @@
|
||||
#include "osal_thread.h"
|
||||
#include "osal.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
osal_thread_t osal_thread_create(const char* name,
|
||||
osal_thread_entry entry,
|
||||
void* parameter,
|
||||
osal_size_t stack_size,
|
||||
osal_priority_t priority)
|
||||
{
|
||||
OSAL_ASSERT(name != NULL);
|
||||
OSAL_ASSERT(entry != NULL);
|
||||
|
||||
rt_thread_t thread;
|
||||
|
||||
thread = rt_thread_create(name,
|
||||
(void (*)(void*))entry,
|
||||
parameter,
|
||||
stack_size,
|
||||
priority,
|
||||
10); /* Default tick */
|
||||
|
||||
return (osal_thread_t)thread;
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_delete(osal_thread_t thread)
|
||||
{
|
||||
if (thread == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_thread_delete((rt_thread_t)thread);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_start(osal_thread_t thread)
|
||||
{
|
||||
if (thread == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_thread_startup((rt_thread_t)thread);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_suspend(osal_thread_t thread)
|
||||
{
|
||||
if (thread == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_thread_suspend((rt_thread_t)thread);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_resume(osal_thread_t thread)
|
||||
{
|
||||
if (thread == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_thread_resume((rt_thread_t)thread);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_yield(void)
|
||||
{
|
||||
rt_err_t ret = rt_thread_yield();
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_control(osal_thread_t thread, int cmd, void* arg)
|
||||
{
|
||||
if (thread == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_thread_control((rt_thread_t)thread, cmd, arg);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_thread_t osal_thread_self(void)
|
||||
{
|
||||
return (osal_thread_t)rt_thread_self();
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_delay(osal_tick_t tick)
|
||||
{
|
||||
rt_err_t ret = rt_thread_delay(tick);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
|
||||
osal_err_t osal_thread_mdelay(osal_int32_t ms)
|
||||
{
|
||||
rt_err_t ret = rt_thread_mdelay(ms);
|
||||
|
||||
return osal_get_errno(ret);
|
||||
}
|
||||
79
osal/src/rtthread/osal_timer.c
Normal file
79
osal/src/rtthread/osal_timer.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include "osal_timer.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
osal_timer_t osal_timer_create(const char* name,
|
||||
osal_timer_callback timeout,
|
||||
void* parameter,
|
||||
osal_tick_t time,
|
||||
osal_uint8_t flag)
|
||||
{
|
||||
OSAL_ASSERT(name != NULL);
|
||||
OSAL_ASSERT(timeout != NULL);
|
||||
|
||||
rt_timer_t timer;
|
||||
|
||||
timer = rt_timer_create(name,
|
||||
(void (*)(void*))timeout,
|
||||
parameter,
|
||||
time,
|
||||
flag);
|
||||
|
||||
return (osal_timer_t)timer;
|
||||
}
|
||||
|
||||
osal_err_t osal_timer_delete(osal_timer_t timer)
|
||||
{
|
||||
if (timer == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_timer_delete((rt_timer_t)timer);
|
||||
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
return OSAL_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OSAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
osal_err_t osal_timer_start(osal_timer_t timer)
|
||||
{
|
||||
if (timer == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_timer_start((rt_timer_t)timer);
|
||||
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
return OSAL_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OSAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
osal_err_t osal_timer_stop(osal_timer_t timer)
|
||||
{
|
||||
if (timer == NULL)
|
||||
{
|
||||
return OSAL_INVAL;
|
||||
}
|
||||
|
||||
rt_err_t ret = rt_timer_stop((rt_timer_t)timer);
|
||||
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
return OSAL_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OSAL_ERROR;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user