void rt_evt_set (U16 event_flags, OS_TID task_id) { /* Set one or more event flags of a selectable task. */ P_TCB p_tcb; p_tcb = os_active_TCB[task_id-1]; if (p_tcb == NULL) { return; } p_tcb->events |= event_flags; event_flags = p_tcb->waits; /* If the task is not waiting for an event, it should not be put */ /* to ready state. */ if (p_tcb->state == WAIT_AND) { /* Check for AND-connected events */ if ((p_tcb->events & event_flags) == event_flags) { goto wkup; } } if (p_tcb->state == WAIT_OR) { /* Check for OR-connected events */ if (p_tcb->events & event_flags) { p_tcb->waits &= p_tcb->events; wkup: p_tcb->events &= ~event_flags; rt_rmv_dly (p_tcb); p_tcb->state = READY; #ifdef __CMSIS_RTOS rt_ret_val2(p_tcb, 0x08/*osEventSignal*/, p_tcb->waits); #else rt_ret_val (p_tcb, OS_R_EVT); #endif rt_dispatch (p_tcb); } } }
void rt_evt_psh (P_TCB p_CB, U16 set_flags) { /* Check if task has to be waken up */ U16 event_flags; p_CB->events |= set_flags; event_flags = p_CB->waits; if (p_CB->state == WAIT_AND) { /* Check for AND-connected events */ if ((p_CB->events & event_flags) == event_flags) { goto rdy; } } if (p_CB->state == WAIT_OR) { /* Check for OR-connected events */ if (p_CB->events & event_flags) { p_CB->waits &= p_CB->events; rdy: p_CB->events &= ~event_flags; rt_rmv_dly (p_CB); p_CB->state = READY; #ifdef __CMSIS_RTOS rt_ret_val2(p_CB, 0x08/*osEventSignal*/, p_CB->waits); #else rt_ret_val (p_CB, OS_R_EVT); #endif rt_put_prio (&os_rdy, p_CB); } } }
OS_RESULT rt_mbx_send (OS_ID mailbox, void *p_msg, U16 timeout) { /* Send message to a mailbox */ P_MCB p_MCB = mailbox; P_TCB p_TCB; if (p_MCB->state == 1) { /* A task is waiting for message */ p_TCB = rt_get_first ((P_XCB)p_MCB); if (p_MCB->p_lnk == NULL) { p_MCB->state = 0; } #ifdef __CMSIS_RTOS rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg); #else *p_TCB->msg = p_msg; rt_ret_val (p_TCB, OS_R_MBX); #endif rt_rmv_dly (p_TCB); rt_dispatch (p_TCB); } else { /* Store message in mailbox queue */ if (p_MCB->count == p_MCB->size) { /* No free message entry, wait for one. If message queue is full, */ /* then no task is waiting for message. The 'p_MCB->p_lnk' list */ /* pointer can now be reused for send message waits task list. */ if (timeout == 0) { return (OS_R_TMO); } if (p_MCB->p_lnk != NULL) { rt_put_prio ((P_XCB)p_MCB, os_tsk.run); } else { p_MCB->p_lnk = os_tsk.run; os_tsk.run->p_lnk = NULL; os_tsk.run->p_rlnk = (P_TCB)p_MCB; /* Task is waiting to send a message */ p_MCB->state = 2; } os_tsk.run->msg = p_msg; rt_block (timeout, WAIT_MBX); return (OS_R_TMO); } /* Yes, there is a free entry in a mailbox. */ p_MCB->msg[p_MCB->first] = p_msg; rt_inc (&p_MCB->count); if (++p_MCB->first == p_MCB->size) { p_MCB->first = 0; } } return (OS_R_OK); }
void rt_mbx_psh (P_MCB p_CB, void *p_msg) { /* Store the message to the mailbox queue or pass it to task directly. */ P_TCB p_TCB; void *mem; if (p_CB->p_lnk != NULL) switch (p_CB->state) { #ifdef __CMSIS_RTOS case 3: /* Task is waiting to allocate memory, remove it from the waiting list */ mem = rt_alloc_box(p_msg); if (mem == NULL) break; p_TCB = rt_get_first ((P_XCB)p_CB); rt_ret_val(p_TCB, (U32)mem); p_TCB->state = READY; rt_rmv_dly (p_TCB); rt_put_prio (&os_rdy, p_TCB); break; #endif case 2: /* Task is waiting to send a message, remove it from the waiting list */ p_TCB = rt_get_first ((P_XCB)p_CB); #ifdef __CMSIS_RTOS rt_ret_val(p_TCB, 0/*osOK*/); #else rt_ret_val(p_TCB, OS_R_OK); #endif p_CB->msg[p_CB->first] = p_TCB->msg; rt_inc (&p_CB->count); if (++p_CB->first == p_CB->size) { p_CB->first = 0; } p_TCB->state = READY; rt_rmv_dly (p_TCB); rt_put_prio (&os_rdy, p_TCB); break; case 1: /* Task is waiting for a message, pass the message to the task directly */ p_TCB = rt_get_first ((P_XCB)p_CB); #ifdef __CMSIS_RTOS rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg); #else *p_TCB->msg = p_msg; rt_ret_val (p_TCB, OS_R_MBX); #endif p_TCB->state = READY; rt_rmv_dly (p_TCB); rt_put_prio (&os_rdy, p_TCB); break; } else { /* No task is waiting for a message, store it to the mailbox queue */ if (p_CB->count < p_CB->size) { p_CB->msg[p_CB->first] = p_msg; rt_inc (&p_CB->count); if (++p_CB->first == p_CB->size) { p_CB->first = 0; } } else { os_error (OS_ERR_MBX_OVF); } } }