first init
This commit is contained in:
255
has_project/has_806_os_port/has_os_806_port.c
Normal file
255
has_project/has_806_os_port/has_os_806_port.c
Normal file
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* has_os_806_port.c
|
||||
*
|
||||
* Author: zhangzhaopeng
|
||||
* Version: 1.0
|
||||
* Created on : 2026-02-11
|
||||
* 系统适配层,基于806自定义系统层接口
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "has_os_806_port.h"
|
||||
|
||||
// TODO: 信号量限制最大数量
|
||||
|
||||
#if defined(HAS_OS_USER_DEFINED)
|
||||
|
||||
#define port_printf printf
|
||||
|
||||
/* ------------------------------------------------ 内存分配 API ------------------------------------------------ */
|
||||
void *has_malloc(unsigned int size)
|
||||
{
|
||||
if (size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
return test_slab_malloc(size);
|
||||
}
|
||||
|
||||
void has_free(void *ptr)
|
||||
{
|
||||
if (ptr == NULL) {
|
||||
return;
|
||||
}
|
||||
test_slab_free(ptr);
|
||||
}
|
||||
|
||||
void *has_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
|
||||
{
|
||||
void *new_ptr;
|
||||
unsigned int copy_size;
|
||||
|
||||
if (new_size == 0) {
|
||||
has_free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ptr == NULL) {
|
||||
return has_malloc(new_size);
|
||||
}
|
||||
|
||||
new_ptr = has_malloc(new_size);
|
||||
if (new_ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
copy_size = (old_size < new_size) ? old_size : new_size;
|
||||
if (copy_size > 0) {
|
||||
memcpy(new_ptr, ptr, copy_size);
|
||||
}
|
||||
|
||||
has_free(ptr);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 互斥量初始化
|
||||
*
|
||||
* @param mutex:互斥量
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_mutex_init(has_mutex_t *mutex)
|
||||
{
|
||||
/* to be done */
|
||||
/* example:xr806 */
|
||||
if (OS_MutexCreate(mutex) != OS_OK) {
|
||||
port_printf("create mutex err!!\n\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 互斥量上锁
|
||||
*
|
||||
* @param mutex:互斥量
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_mutex_lock(has_mutex_t *mutex)
|
||||
{
|
||||
if (OS_MutexLock(mutex, OS_WAIT_FOREVER) != OS_OK) {
|
||||
port_printf("lock mutex err!!\n\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 互斥量解锁
|
||||
*
|
||||
* @param mutex:互斥量
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_mutex_unlock(has_mutex_t *mutex)
|
||||
{
|
||||
OS_MutexUnlock(mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 互斥量销毁
|
||||
*
|
||||
* @param mutex:互斥量
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_mutex_deinit(has_mutex_t *mutex)
|
||||
{
|
||||
if (OS_MutexDelete(mutex) != OS_OK) {
|
||||
port_printf("delete mutex err!!\n\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 信号量初始化
|
||||
*
|
||||
* @param sem:信号量
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_sem_init(has_sem_t *sem)
|
||||
{
|
||||
/* to be done */
|
||||
/* example:xr806 */
|
||||
if (OS_SemaphoreCreate(sem, 0, UINT_MAX) != OS_OK) {
|
||||
port_printf("create counting sem error!\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 消息通知
|
||||
*
|
||||
* @param sem:信号量
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_sem_notify(has_sem_t *sem)
|
||||
{
|
||||
/* to be done */
|
||||
/* example:xr806 */
|
||||
if (OS_SemaphoreRelease(sem) != OS_OK) {
|
||||
port_printf("notify thread err!\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 等待消息
|
||||
*
|
||||
* @param sem:信号量
|
||||
* @param ms_timeout:超时时间(ms)其中0:消息直接返回;-1:一直阻塞
|
||||
* @return MSG_WAIT_RET:
|
||||
* WAIT_FAIL = -1, 等待失败
|
||||
* WAIT_TIMEOUT = -1, 等待超时
|
||||
* WAIT_MSG_COME = -1, 有新消息
|
||||
*/
|
||||
enum MSG_WAIT_RET has_sem_wait(has_sem_t *sem, int ms_timeout)
|
||||
{
|
||||
/* to be done */
|
||||
/* example:xr806 */
|
||||
OS_Time_t waitMS;
|
||||
if (ms_timeout < 0) {
|
||||
waitMS = OS_WAIT_FOREVER;
|
||||
} else {
|
||||
waitMS = (OS_Time_t)ms_timeout;
|
||||
}
|
||||
|
||||
if (OS_SemaphoreWait(sem, waitMS) != OS_OK) {
|
||||
// port_printf("sem take fail\n");
|
||||
return WAIT_FAIL;
|
||||
}
|
||||
return WAIT_MSG_COME;
|
||||
}
|
||||
|
||||
int has_sem_deinit(has_sem_t *sem)
|
||||
{
|
||||
if (OS_SemaphoreDelete(sem) != OS_OK) {
|
||||
port_printf("delete sem err!!\n\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------ 线程创建API ------------------------------------------------ */
|
||||
int has_task_create(const char *task_name, int priority, unsigned int stack_size,
|
||||
has_taskentry pfnEntry, void *pEntryParm, OS_THREAD_ID *pthread_id)
|
||||
{
|
||||
(void)task_name;
|
||||
(void)priority;
|
||||
(void)stack_size;
|
||||
(void)pfnEntry;
|
||||
(void)pEntryParm;
|
||||
if (pthread_id != NULL) {
|
||||
*pthread_id = NULL;
|
||||
}
|
||||
port_printf("has_task_create is not implemented for HAS_OS_USER_DEFINED.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int has_task_delete(OS_THREAD_ID thread_id)
|
||||
{
|
||||
(void)thread_id;
|
||||
port_printf("has_task_delete is not implemented for HAS_OS_USER_DEFINED.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t has_get_time_ms(void)
|
||||
{
|
||||
#if defined(OS_TicksToMSecs)
|
||||
return (uint64_t)OS_TicksToMSecs(OS_GetTicks());
|
||||
#else
|
||||
uint64_t ticks = (uint64_t)OS_GetTicks();
|
||||
|
||||
#if defined(OS_HZ) && (OS_HZ > 0)
|
||||
return (ticks * 1000ULL) / (uint64_t)OS_HZ;
|
||||
#else
|
||||
/* 兜底:若 SDK 未提供换算宏且未暴露 HZ,按 tick=ms 处理 */
|
||||
return ticks;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void has_sleep_ms(uint32_t ms)
|
||||
{
|
||||
/* TODO: 若 SDK 提供线程睡眠接口,可替换为 OS 级阻塞延时 */
|
||||
// uint64_t start_ms;
|
||||
|
||||
// if (ms == 0U) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// start_ms = has_get_time_ms();
|
||||
// while ((has_get_time_ms() - start_ms) < (uint64_t)ms) {
|
||||
// }
|
||||
}
|
||||
|
||||
int has_task_exit(OS_THREAD_ID task_id, uint32_t dw_cookie)
|
||||
{
|
||||
(void)task_id;
|
||||
(void)dw_cookie;
|
||||
port_printf("has_task_exit is not implemented for HAS_OS_USER_DEFINED.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
76
has_project/has_806_os_port/has_os_806_port.h
Normal file
76
has_project/has_806_os_port/has_os_806_port.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifndef HAS_OS_806_PROT_H
|
||||
#define HAS_OS_806_PROT_H
|
||||
|
||||
#include "has_platform_config.h"
|
||||
|
||||
|
||||
#if defined(HAS_OS_USER_DEFINED)
|
||||
|
||||
#define HAS_ENABLE_MUTEX_SEM // 支持互斥量和信号量
|
||||
|
||||
|
||||
/* 下面示例是基于xr806移植 */
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../has_wifi_slab.h"
|
||||
#include "../kernel/os/os.h"
|
||||
|
||||
#define has_mutex_t OS_Mutex_t
|
||||
#define has_sem_t OS_Semaphore_t
|
||||
|
||||
#define TASK_PRIORITY_SYS 0
|
||||
#define TASK_PRIORITY_HIGH 1
|
||||
#define TASK_PRIORITY_LOW 8
|
||||
#define TASK_PRIORITY_NORMAL 4
|
||||
#define TASK_PRIORITY_ABOVE_NORMAL 3
|
||||
#define TASK_PRIORITY_BELOW_NORMAL 6
|
||||
typedef void *OS_THREAD_ID;
|
||||
typedef void *(*has_taskentry)(void *param);
|
||||
|
||||
|
||||
#ifdef HAS_ENABLE_MUTEX_SEM
|
||||
enum MSG_WAIT_RET{
|
||||
WAIT_FAIL = -1, // 等待失败
|
||||
WAIT_TIMEOUT, // 等待超时
|
||||
WAIT_MSG_COME, // 需要处理消息
|
||||
};
|
||||
|
||||
int has_mutex_init(has_mutex_t *mutex);
|
||||
int has_mutex_lock(has_mutex_t *mutex);
|
||||
int has_mutex_unlock(has_mutex_t *mutex);
|
||||
int has_mutex_deinit(has_mutex_t *mutex);
|
||||
|
||||
int has_sem_init(has_sem_t *sem);
|
||||
int has_sem_notify(has_sem_t *sem);
|
||||
enum MSG_WAIT_RET has_sem_wait(has_sem_t *sem, int ms_timeout);
|
||||
int has_sem_deinit(has_sem_t *sem);
|
||||
#else
|
||||
|
||||
#define has_mutex_t
|
||||
#define has_mutex_init(m)
|
||||
#define has_mutex_lock(m)
|
||||
#define has_mutex_unlock(m)
|
||||
#define has_mutex_deinit(m)
|
||||
#define has_sem_t
|
||||
#define has_sem_init(s)
|
||||
#define has_sem_notify(s)
|
||||
#define has_sem_wait(s, t)
|
||||
#define has_sem_deinit(s)
|
||||
#endif
|
||||
|
||||
int has_task_create(const char *task_name, int priority, unsigned int stack_size,
|
||||
has_taskentry pfnEntry, void *pEntryParm, OS_THREAD_ID *pthread_id);
|
||||
int has_task_exit(OS_THREAD_ID task_id, uint32_t dw_cookie);
|
||||
int has_task_delete(OS_THREAD_ID thread_id);
|
||||
uint64_t has_get_time_ms(void);
|
||||
void has_sleep_ms(uint32_t ms);
|
||||
|
||||
void *has_malloc(unsigned int size);
|
||||
void *has_realloc(void *ptr, unsigned int old_size, unsigned int new_size);
|
||||
void has_free(void *ptr);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user