兼容freeRTOS系统支持,兼容xr806
This commit is contained in:
@@ -2,4 +2,4 @@
|
|||||||
使用方法:
|
使用方法:
|
||||||
1. 在 has_task_msg_os_port.h 选择操作系统,更改宏MSG_OPT_OS。或选择自定义自己实现接口,如果自己定义需要自己实现has_task_msg_os_port.c的相应接口。
|
1. 在 has_task_msg_os_port.h 选择操作系统,更改宏MSG_OPT_OS。或选择自定义自己实现接口,如果自己定义需要自己实现has_task_msg_os_port.c的相应接口。
|
||||||
2. 在 has_task_msg.h 中添加自己负责的软件模块id号(has_module_ID_e),并且完成订阅表 SUBSCIBE_INFO ,要把模块的id、模块要订阅的id等信息填好。
|
2. 在 has_task_msg.h 中添加自己负责的软件模块id号(has_module_ID_e),并且完成订阅表 SUBSCIBE_INFO ,要把模块的id、模块要订阅的id等信息填好。
|
||||||
3. 代码中使用:初始化、发布、订阅和消息回调即可。请参考示例 msg_example.c
|
3. 代码中使用:初始化、发布、订阅和消息回调即可。接口在has_task_msg.h中声明,请参考示例 msg_example.c
|
||||||
@@ -44,11 +44,6 @@ typedef enum {
|
|||||||
*/
|
*/
|
||||||
typedef int (*has_msg_handle_cb)(unsigned char module_id, const unsigned char *buf, unsigned int len);
|
typedef int (*has_msg_handle_cb)(unsigned char module_id, const unsigned char *buf, unsigned int len);
|
||||||
|
|
||||||
|
|
||||||
/* -------------------- 注意 Attention -------------------- */
|
|
||||||
/* 对于同一个id,不能同时在不同线程中调用 *_handle*, *_receive* 和 *delete_all* 的接口 */
|
|
||||||
/* For the same id, the *_handle*, *_receive* and *delete_all* API cannot be called in different threads at the same time */
|
|
||||||
|
|
||||||
/* 基础功能 */
|
/* 基础功能 */
|
||||||
int has_msg_init(void);
|
int has_msg_init(void);
|
||||||
int has_msg_os_init(void);
|
int has_msg_os_init(void);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "list.h"
|
|
||||||
#include "has_task_msg.h"
|
#include "has_task_msg.h"
|
||||||
#include "has_task_msg_os_port.h"
|
#include "has_task_msg_os_port.h"
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ int msg_mutex_init(msg_mutex_t *mutex)
|
|||||||
return 0;
|
return 0;
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
||||||
/* to be done */
|
/* to be done */
|
||||||
|
/* example:xr806 */
|
||||||
|
if (OS_MutexCreate(mutex) != OS_OK) {
|
||||||
|
msg_printf("create mutex err!!\n\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,14 +68,14 @@ int msg_sem_init(msg_sem_t *sem)
|
|||||||
sem->poll_fd.revents = 0;
|
sem->poll_fd.revents = 0;
|
||||||
return 0;
|
return 0;
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_freeRTOS)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_freeRTOS)
|
||||||
*sem = xSemaphoreCreateCounting(UINT_MAX, 0)
|
*sem = xSemaphoreCreateCounting(UINT_MAX, 0);
|
||||||
if (*sem == NULL) {
|
if (*sem == NULL) {
|
||||||
msg_printf("create counting sem error!\n");
|
msg_printf("create counting sem error!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_RT_THREAD)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_RT_THREAD)
|
||||||
*sem = rt_sem_create("has_sem", 0, RT_IPC_FLAG_FIFO)
|
*sem = rt_sem_create("has_sem", 0, RT_IPC_FLAG_FIFO);
|
||||||
if (*sem == RT_NULL) {
|
if (*sem == RT_NULL) {
|
||||||
msg_printf("create counting sem error!\n");
|
msg_printf("create counting sem error!\n");
|
||||||
return -1;
|
return -1;
|
||||||
@@ -77,6 +83,12 @@ int msg_sem_init(msg_sem_t *sem)
|
|||||||
return 0;
|
return 0;
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
||||||
/* to be done */
|
/* to be done */
|
||||||
|
/* example:xr806 */
|
||||||
|
if (OS_SemaphoreCreate(sem, 0, UINT_MAX) != OS_OK) {
|
||||||
|
msg_printf("create counting sem error!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +121,12 @@ int msg_sem_notify(msg_sem_t *sem)
|
|||||||
return 0;
|
return 0;
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
||||||
/* to be done */
|
/* to be done */
|
||||||
|
/* example:xr806 */
|
||||||
|
if (OS_SemaphoreRelease(sem) != OS_OK) {
|
||||||
|
msg_printf("notify thread err!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +167,7 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout)
|
|||||||
msg_printf("something err happened,revents:%x\n", sem->poll_fd.revents);
|
msg_printf("something err happened,revents:%x\n", sem->poll_fd.revents);
|
||||||
return WAIT_FAIL;
|
return WAIT_FAIL;
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_freeRTOS)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_freeRTOS)
|
||||||
const TickType_t xTicksToWait;
|
TickType_t xTicksToWait;
|
||||||
if (ms_timeout < 0) {
|
if (ms_timeout < 0) {
|
||||||
xTicksToWait = portMAX_DELAY;
|
xTicksToWait = portMAX_DELAY;
|
||||||
} else {
|
} else {
|
||||||
@@ -157,7 +175,7 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (xSemaphoreTake(*sem, xTicksToWait) != pdTRUE) {
|
if (xSemaphoreTake(*sem, xTicksToWait) != pdTRUE) {
|
||||||
msg_printf("sem take fail\n");
|
// msg_printf("sem take fail\n");
|
||||||
return WAIT_FAIL;
|
return WAIT_FAIL;
|
||||||
}
|
}
|
||||||
return WAIT_MSG_COME;
|
return WAIT_MSG_COME;
|
||||||
@@ -182,6 +200,19 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout)
|
|||||||
}
|
}
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
||||||
/* to be done */
|
/* 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) {
|
||||||
|
// msg_printf("sem take fail\n");
|
||||||
|
return WAIT_FAIL;
|
||||||
|
}
|
||||||
|
return WAIT_MSG_COME;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#define MSG_OPT_OS_NONE_OR_OSAL 4
|
#define MSG_OPT_OS_NONE_OR_OSAL 4
|
||||||
#define MSG_OPT_OS_USER_DEFINED 5
|
#define MSG_OPT_OS_USER_DEFINED 5
|
||||||
|
|
||||||
/* TODO:rtt和freertos未测过 */
|
/* TODO:rtt未测过 */
|
||||||
#define MSG_OPT_OS MSG_OPT_OS_LINUX
|
#define MSG_OPT_OS MSG_OPT_OS_LINUX
|
||||||
// #define MSG_OPT_OS MSG_OPT_OS_freeRTOS
|
// #define MSG_OPT_OS MSG_OPT_OS_freeRTOS
|
||||||
// #define MSG_OPT_OS MSG_OPT_OS_RT_THREAD
|
// #define MSG_OPT_OS MSG_OPT_OS_RT_THREAD
|
||||||
@@ -30,6 +30,7 @@ enum MSG_WAIT_RET{
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
#ifdef MSG_OPT_MUTEX_SEM
|
#ifdef MSG_OPT_MUTEX_SEM
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
@@ -43,10 +44,6 @@ typedef struct {
|
|||||||
} sync_t;
|
} sync_t;
|
||||||
#define msg_sem_t sync_t
|
#define msg_sem_t sync_t
|
||||||
|
|
||||||
int msg_mutex_init(msg_mutex_t *mutex);
|
|
||||||
int msg_sem_init(msg_sem_t *sem);
|
|
||||||
int msg_sem_notify(msg_sem_t *sem);
|
|
||||||
enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|
||||||
#else
|
#else
|
||||||
#define msg_mutex_t
|
#define msg_mutex_t
|
||||||
#define msg_mutex_lock(m)
|
#define msg_mutex_lock(m)
|
||||||
@@ -64,6 +61,7 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include "FreeRTOS.h"
|
#include "FreeRTOS.h"
|
||||||
#include "semphr.h"
|
#include "semphr.h"
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
#define msg_malloc pvPortMalloc
|
#define msg_malloc pvPortMalloc
|
||||||
#define msg_free vPortFree
|
#define msg_free vPortFree
|
||||||
@@ -84,11 +82,6 @@ static inline void msg_mutex_unlock(msg_mutex_t *mutex) {
|
|||||||
|
|
||||||
#define msg_sem_t SemaphoreHandle_t
|
#define msg_sem_t SemaphoreHandle_t
|
||||||
|
|
||||||
int msg_mutex_init(msg_mutex_t *mutex);
|
|
||||||
int msg_sem_init(msg_sem_t *sem);
|
|
||||||
int msg_sem_notify(msg_sem_t *sem);
|
|
||||||
enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define msg_mutex_t
|
#define msg_mutex_t
|
||||||
#define msg_mutex_lock(m)
|
#define msg_mutex_lock(m)
|
||||||
@@ -99,6 +92,7 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
#define msg_malloc rt_malloc
|
#define msg_malloc rt_malloc
|
||||||
#define msg_free rt_free
|
#define msg_free rt_free
|
||||||
@@ -119,11 +113,6 @@ static inline void msg_mutex_unlock(msg_mutex_t *mutex) {
|
|||||||
|
|
||||||
#define msg_sem_t rt_sem_t
|
#define msg_sem_t rt_sem_t
|
||||||
|
|
||||||
int msg_mutex_init(msg_mutex_t *mutex);
|
|
||||||
int msg_sem_init(msg_sem_t *sem);
|
|
||||||
int msg_sem_notify(msg_sem_t *sem);
|
|
||||||
enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define msg_mutex_t
|
#define msg_mutex_t
|
||||||
#define msg_mutex_lock(m)
|
#define msg_mutex_lock(m)
|
||||||
@@ -133,6 +122,7 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_NONE_OR_OSAL)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_NONE_OR_OSAL)
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "list.h"
|
||||||
#undef MSG_OPT_MUTEX_SEM
|
#undef MSG_OPT_MUTEX_SEM
|
||||||
|
|
||||||
#define msg_mutex_t
|
#define msg_mutex_t
|
||||||
@@ -145,26 +135,33 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|||||||
|
|
||||||
/* ----------------------- for user define ----------------------- */
|
/* ----------------------- for user define ----------------------- */
|
||||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
||||||
|
/* 下面示例是基于xr806移植 */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
|
#include "../has_wifi_slab.h"
|
||||||
|
#include "../kernel/os/os.h"
|
||||||
|
|
||||||
|
#define msg_malloc test_slab_malloc
|
||||||
|
#define msg_free test_slab_free
|
||||||
|
#define msg_printf printf
|
||||||
|
|
||||||
#ifdef MSG_OPT_MUTEX_SEM
|
#ifdef MSG_OPT_MUTEX_SEM
|
||||||
#define msg_mutex_t int/* to be done */
|
#define msg_mutex_t OS_Mutex_t
|
||||||
|
|
||||||
static inline void msg_mutex_lock(msg_mutex_t *mutex) {
|
static inline void msg_mutex_lock(msg_mutex_t *mutex) {
|
||||||
/* to be done */
|
if (OS_MutexLock(mutex, OS_WAIT_FOREVER) != OS_OK) {
|
||||||
|
msg_printf("lock mutex err!!\n\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void msg_mutex_unlock(msg_mutex_t *mutex) {
|
static inline void msg_mutex_unlock(msg_mutex_t *mutex) {
|
||||||
/* to be done */
|
OS_MutexUnlock(mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define msg_sem_t int // to be done
|
#define msg_sem_t OS_Semaphore_t
|
||||||
|
|
||||||
int msg_mutex_init(msg_mutex_t *mutex);
|
|
||||||
int msg_sem_init(msg_sem_t *sem);
|
|
||||||
int msg_sem_notify(msg_sem_t *sem);
|
|
||||||
enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define msg_mutex_t
|
#define msg_mutex_t
|
||||||
@@ -172,14 +169,16 @@ enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
|||||||
#define msg_mutex_unlock(m)
|
#define msg_mutex_unlock(m)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define msg_malloc malloc
|
|
||||||
#define msg_free free
|
|
||||||
#define msg_printf printf
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#error "!Undefined system environment!"
|
#error "!Undefined system environment!"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef MSG_OPT_MUTEX_SEM
|
||||||
|
int msg_mutex_init(msg_mutex_t *mutex);
|
||||||
|
int msg_sem_init(msg_sem_t *sem);
|
||||||
|
int msg_sem_notify(msg_sem_t *sem);
|
||||||
|
enum MSG_WAIT_RET msg_sem_wait(msg_sem_t *sem, int ms_timeout);
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user