主结构体
typedef struct os_q { /* QUEUE CONTROL BLOCK */
struct os_q *OSQPtr; /* Link to next queue control block in list of free blocks */
void **OSQStart; /* Ptr to start of queue data */
void **OSQEnd; /* Ptr to end of queue data */
void **OSQIn; /* Ptr to where next message will be inserted in the Q */
void **OSQOut; /* Ptr to where next message will be extracted from the Q */
INT16U OSQSize; /* Size of queue (maximum number of entries) */
INT16U OSQEntries; /* Current number of entries in the queue */
} OS_Q;
typedef struct os_q_data {
void *OSMsg; /* Pointer to next message to be extracted from queue */
INT16U OSNMsgs; /* Number of messages in message queue */
INT16U OSQSize; /* Size of message queue */
OS_PRIO OSEventTbl[OS_EVENT_TBL_SIZE]; /* List of tasks waiting for event to occur */
OS_PRIO OSEventGrp; /* Group corresponding to tasks waiting for event to occur */
} OS_Q_DATA;
typedef struct os_event {
INT8U OSEventType; /* Type of event control block (see OS_EVENT_TYPE_xxxx) */
void *OSEventPtr; /* Pointer to message or queue structure */
INT16U OSEventCnt; /* Semaphore Count (not used if other EVENT type) */
OS_PRIO OSEventGrp; /* Group corresponding to tasks waiting for event to occur */
OS_PRIO OSEventTbl[OS_EVENT_TBL_SIZE]; /* List of tasks waiting for event to occur */
#if OS_EVENT_NAME_EN > 0u
INT8U *OSEventName;
#endif
} OS_EVENT;
API函数
//无等待地从消息队列中得到一个消息
void *OSQAccept (OS_EVENT *pevent,
INT8U *perr);
//创建
OS_EVENT *OSQCreate (void **start,
INT16U size);
//删除
OS_EVENT *OSQDel (OS_EVENT *pevent,
INT8U opt,
INT8U *perr);
//清空
INT8U OSQFlush (OS_EVENT *pevent);
//等待-1,接收消息
void *OSQPend (OS_EVENT *pevent,
INT32U timeout,
INT8U *perr);
//取消等待
INT8U OSQPendAbort (OS_EVENT *pevent,
INT8U opt,
INT8U *perr);
//释放+1,发送消息(队列式)
INT8U OSQPost (OS_EVENT *pevent,
void *pmsg);
//发送消息(堆栈式)
INT8U OSQPostFront (OS_EVENT *pevent,
void *pmsg);
//单选项的消息发送
INT8U OSQPostOpt (OS_EVENT *pevent,
void *pmsg,
INT8U opt);
//查询
INT8U OSQQuery (OS_EVENT *pevent,
OS_Q_DATA *p_q_data);
应用举例
#define OSQ_MSG_NUM 1 //消息队列消息数量
OS_EVENT *DATA_Msg;
void *MsgGrp[OSQ_MSG_NUM];
void start_task(void *pdata)
{
OS_CRITICAL_ENTER(); //进入临界区
DATA_Msg = OSQCreate ((void** )&MsgGrp[0],
(INT16U )OSQ_MSG_NUM);
OS_EXIT_CRITICAL();
}
void msgpost_task(void *p_arg)
{
static u8 msg_num;
OS_Q_DATA p_q_data;
OSQQuery(DATA_Msg, &p_q_data);
if(p_q_data.OSNMsgs >= OSQ_MSG_NUM)
{
printf("OSQQuery OSNMsgs: %d
", p_q_data.OSNMsgs);
return;
}
msg_num++;
OSQPost((OS_EVENT* )DATA_Msg,
(void* )&msg_num);
}
void msgpend_task(void *p_arg)
{
void *p;
INT8U err;
u8 msg_num;
p = OSQPend((OS_EVENT* )DATA_Msg,
(INT32U )0,
(INT8U* )&err);
msg_num = *p;
}