📄 kal.c
字号:
Status KL_ReceiveMail(KL_mailbox_t *mailbox, void **msg, u32_t timeout)
{
INT8U err;
#if KL_CHK_ARG_EN
if (mailbox == NULL) return KL_INVALID_MAILBOX;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
if (timeout == KL_SUSPEND_FOREVER) timeout = 0;
*msg = OSMboxPend((OS_EVENT *)(*mailbox), timeout, &err);
return (Status)(-err);
}
Status KL_TryRecvMail(KL_mailbox_t *mailbox, void **msg)
{
void *p;
#if KL_CHK_ARG_EN
if (mailbox == NULL) return KL_INVALID_MAILBOX;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
p = OSMboxAccept((OS_EVENT *)(*mailbox));
if (p != NULL){
*msg = p;
return KL_SUCCESS;
}
return KL_MBOX_EMPTY;
}
Status KL_QueryMailboxInfo(KL_mailbox_t *mailbox)
{
return KL_SUCCESS;
}
/*--------------------------------------------------------------
Queue , sizes of queue and message both can be config
----------------------------------------------------------------*/
Status KL_CreateQueue(KL_queue_t *queue,
u8_t *name,
void *start_address,
u32_t queue_size,
u32_t message_size)
{
OS_EVENT *p;
u32_t msg_num;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
if ((start_address == NULL)||((u32_t)start_address%4 != 0)) return KL_INVALID_MEMORY;
if ((queue_size == 0)||\
(message_size == 0)||\
(queue_size < message_size))
return KL_INVALID_SIZE;
#endif
name = name;
msg_num = queue_size/message_size;
p = OSQCreate(start_address, message_size, msg_num);
if (p != NULL){
*queue = (KL_queue_t)p;
return KL_SUCCESS;
}
return KL_UNKNOWN_ERROR;
}
Status KL_DelQueue(KL_queue_t *queue)
{
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
#endif
OSQDel((OS_EVENT *)(*queue), OS_DEL_ALWAYS, &err);
return (Status)(-err);
}
Status KL_FlushQueue(KL_queue_t *queue)
{
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
#endif
err = OSQFlush((OS_EVENT *)(*queue));
return (Status)(-err);
}
Status KL_SendMessage(KL_queue_t *queue, void *msg, u32_t timeout)
{
OS_EVENT *p;
u32_t cnt;
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
p = (OS_EVENT *)(*queue);
cnt = timeout;
err = OSQPostOpt(p, msg, OS_POST_OPT_NONE);
while((err == OS_Q_FULL)&&(cnt--)){
if (timeout == KL_SUSPEND_FOREVER) cnt++;
OSTimeDly(1);
err = OSMboxPostOpt(p, msg, OS_POST_OPT_NONE);
}
if ((timeout > 0)&&(err == OS_MBOX_FULL)&&(cnt == 0))
return KL_TIMEOUT;
return (Status)(-err);
}
Status KL_SendMessagetoFront(KL_queue_t *queue, void *msg, u32_t timeout)
{
OS_EVENT *p;
u32_t cnt;
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
p = (OS_EVENT *)(*queue);
cnt = timeout;
err = OSQPostOpt(p, msg, OS_POST_OPT_FRONT);
while((err == OS_Q_FULL)&&(cnt--)){
if (timeout == KL_SUSPEND_FOREVER) cnt++;
OSTimeDly(1);
err = OSMboxPostOpt(p, msg, OS_POST_OPT_FRONT);
}
if ((timeout > 0)&&(err == OS_MBOX_FULL)&&(cnt == 0))
return KL_TIMEOUT;
return (Status)(-err);
}
Status KL_BroadcastMessage(KL_queue_t *queue, void *msg, u32_t timeout)
{
OS_EVENT *p;
u32_t cnt;
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
p = (OS_EVENT *)(*queue);
cnt = timeout;
err = OSQPostOpt(p, msg, OS_POST_OPT_BROADCAST);
while((err == OS_Q_FULL)&&(cnt--)){
if (timeout == KL_SUSPEND_FOREVER) cnt++;
OSTimeDly(1);
err = OSMboxPostOpt(p, msg, OS_POST_OPT_BROADCAST);
}
if ((timeout > 0)&&(err == OS_MBOX_FULL)&&(cnt == 0))
return KL_TIMEOUT;
return (Status)(-err);
}
Status KL_ReceiveMessage(KL_queue_t *queue, void *msg, u32_t timeout)
{
OS_EVENT *p;
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
p = (OS_EVENT *)(*queue);
if (timeout == KL_SUSPEND_FOREVER) timeout = 0;
OSQPend(p, msg, timeout, &err);
return (Status)(-err);
}
Status KL_TryRecvMessage(KL_queue_t *queue, void *msg)
{
OS_EVENT *p;
INT8U err;
#if KL_CHK_ARG_EN
if (queue == NULL) return KL_INVALID_QUEUE;
if (msg == NULL) return KL_INVALID_MEMORY;
#endif
p = (OS_EVENT *)(*queue);
OSQAccept(p, msg, &err);
return (Status)(-err);
}
Status KL_QueryQueueInfo(KL_queue_t *queue)
{
return KL_SUCCESS;
}
/*--------------------------------------------------------------
Semaphore
----------------------------------------------------------------*/
Status KL_CreateSemaphore(KL_sem_t *sem, u8_t *name, u32_t initial)
{
OS_EVENT *p;
#if KL_CHK_ARG_EN
if (sem == NULL) return KL_INVALID_SEMA;
#endif
name = name;
p = OSSemCreate(initial);
if (p != NULL){
*sem = (KL_sem_t)p;
return KL_SUCCESS;
}
return KL_UNKNOWN_ERROR;
}
Status KL_DelSemaphore(KL_sem_t *sem)
{
INT8U err;
#if KL_CHK_ARG_EN
if (sem == NULL) return KL_INVALID_SEMA;
#endif
OSSemDel((OS_EVENT *)(*sem), OS_DEL_ALWAYS, &err);
return (Status)(-err);
}
Status KL_PostSemaphore(KL_sem_t *sem)
{
OS_EVENT *p;
INT8U err;
#if KL_CHK_ARG_EN
if (sem == NULL) return KL_INVALID_SEMA;
#endif
p = (OS_EVENT *)(*sem);
err = OSSemPost(p);
return (Status)(-err);
}
Status KL_PendSemaphore(KL_sem_t *sem, u32_t timeout)
{
INT8U err;
#if KL_CHK_ARG_EN
if (sem == NULL) return KL_INVALID_SEMA;
#endif
if (timeout == KL_SUSPEND_FOREVER) timeout = 0;
OSSemPend((OS_EVENT *)(*sem), timeout, &err);
return (Status)(-err);
}
Status KL_TryPendSemaphore(KL_sem_t *sem)
{
INT8U err;
#if KL_CHK_ARG_EN
if (sem == NULL) return KL_INVALID_SEMA;
#endif
err = OSSemAccept((OS_EVENT *)(*sem));
return ((err > 0)? KL_SUCCESS : KL_SEMA_NOT_RDY);
}
Status KL_QureySemaphoreInfo(KL_sem_t *sem)
{
return KL_SUCCESS;
}
/*--------------------------------------------------------------
Mutex
----------------------------------------------------------------*/
Status KL_CreateMutex(KL_mutex_t *mutex, u8_t *name)
{
OS_EVENT *p;
INT8U err;
#if KL_CHK_ARG_EN
if (mutex == NULL) return KL_INVALID_MUTEX;
#endif
name = name;
p = OSMutexCreate(0, &err);
if (p != NULL){
*mutex = (KL_sem_t)p;
return KL_SUCCESS;
}
return KL_UNKNOWN_ERROR;
}
Status KL_DelMutex(KL_mutex_t *mutex)
{
INT8U err;
#if KL_CHK_ARG_EN
if (mutex == NULL) return KL_INVALID_MUTEX;
#endif
OSMutexDel((OS_EVENT *)(*mutex), OS_DEL_ALWAYS, &err);
return (Status)(-err);
}
Status KL_PostMutex(KL_mutex_t *mutex)
{
INT8U err;
#if KL_CHK_ARG_EN
if (mutex == NULL) return KL_INVALID_MUTEX;
#endif
err = OSMutexPost((OS_EVENT *)(*mutex));
return (Status)(-err);
}
Status KL_PendMutex(KL_mutex_t *mutex, u32_t timeout)
{
INT8U err;
#if KL_CHK_ARG_EN
if (mutex == NULL) return KL_INVALID_MUTEX;
#endif
if (timeout == KL_SUSPEND_FOREVER) timeout = 0;
OSMutexPend((OS_EVENT *)(*mutex), timeout, &err);
return (Status)(-err);
}
Status KL_TryPendMutex(KL_mutex_t *mutex)
{
INT8U err, r;
#if KL_CHK_ARG_EN
if (mutex == NULL) return KL_INVALID_MUTEX;
#endif
r = OSMutexAccept((OS_EVENT *)(*mutex), &err);
return ((r != 0)? KL_SUCCESS : KL_MUTEX_NOT_RDY);
}
Status KL_QureyMutexInfo(KL_mutex_t *mutex)
{
return KL_SUCCESS;
}
/*--------------------------------------------------------------
Event Group
----------------------------------------------------------------*/
Status KL_CreatEventGroup(KL_eventgrp_t *event, u8_t *name)
{
OS_FLAG_GRP *p;
INT8U err;
#if KL_CHK_ARG_EN
if (event == NULL) return KL_INVALID_EVENTGROUP;
#endif
name = name;
p = OSFlagCreate(0, &err);
if (p != NULL){
*event = (KL_eventgrp_t)p;
return KL_SUCCESS;
}
return KL_UNKNOWN_ERROR;
}
Status KL_DelEventGroup(KL_eventgrp_t *event)
{
INT8U err;
#if KL_CHK_ARG_EN
if (event == NULL) return KL_INVALID_EVENTGROUP;
#endif
OSFlagDel((OS_FLAG_GRP *)(*event), OS_DEL_ALWAYS, &err);
return (Status)(-err);
}
Status KL_PostEventGroup(KL_eventgrp_t *event, KL_flags flags, u32_t opt)
{
INT8U err;
#if KL_CHK_ARG_EN
if (event == NULL) return KL_INVALID_EVENTGROUP;
#endif
OSFlagPost((OS_FLAG_GRP *)(*event), flags, opt, &err);
return (Status)(-err);
}
Status KL_PendEventGroup(KL_eventgrp_t *event, KL_flags flags, u32_t opt,
KL_flags *gotflags, u32_t timeout)
{
INT8U err;
#if KL_CHK_ARG_EN
if (event == NULL) return KL_INVALID_EVENTGROUP;
#endif
if (timeout == KL_SUSPEND_FOREVER) timeout = 0;
*gotflags = OSFlagPend((OS_FLAG_GRP *)(*event), flags, opt, timeout, &err);
return (Status)(-err);
}
Status KL_TryPendEventGroup(KL_eventgrp_t *event, KL_flags flags, u32_t opt, KL_flags *gotflags)
{
INT8U err;
#if KL_CHK_ARG_EN
if (event == NULL) return KL_INVALID_EVENTGROUP;
#endif
*gotflags = OSFlagAccept((OS_FLAG_GRP *)(*event), flags, opt, &err);
return (Status)(-err);
}
Status KL_QureyEventGroupInfo(KL_eventgrp_t *event)
{
return KL_SUCCESS;
}
/*--------------------------------------------------------------
Kenel infomation
----------------------------------------------------------------*/
static u8_t KL_info[] = "uC/OSII 2.83";
u8_t *KL_Info(void)
{
return KL_info;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -