00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00041 #include <string.h>
00042 #include <avr/interrupt.h>
00043
00044 #include "otos_cfg.h"
00045 #include "otos_def.h"
00046 #include "types.h"
00047 #include "memory.h"
00048 #include "task.h"
00049 #include "message.h"
00050 #include "time.h"
00051
00052
00053
00063 OtosMessageQueue* otosCreateMessageQueue(uint8_t size)
00064 {
00065 OtosMessageQueue* pQ;
00066
00067
00068
00069 pQ = otosAllocate(sizeof (OtosMessageQueue) + size * sizeof (OtosMessage));
00070 if (pQ == NULL)
00071 return NULL;
00072
00073
00074 pQ->size = size;
00075 pQ->in = 0;
00076 pQ->out = 0;
00077 pQ->count = 0;
00078
00079 return pQ;
00080 }
00081
00082
00083
00101 uint8_t otosSendMessage(OtosMessageQueue* pQueue, OtosMessage* pMsg)
00102 {
00103 OtosTask* pTmp;
00104 OtosTask* pNextTask;
00105 uint8_t dispatch;
00106
00107
00108 dispatch = ISR_NOT_ACTIVE();
00109 cli();
00110
00111
00112 if (pQueue->count == pQueue->size)
00113 {
00114 sei();
00115 return MESSAGE_QUEUE_FULL;
00116 }
00117
00118
00119 memcpy(&pQueue->msg[pQueue->in], pMsg, sizeof (OtosMessage));
00120 MQUEUE_INC_IN(pQueue);
00121
00122
00123 pTmp = g_pBlockedQueue;
00124 while (pTmp != NULL)
00125 {
00126 pNextTask = pTmp->pNext;
00127
00128 if (pTmp->pWaitMsgQueue == pQueue)
00129 {
00130 pTmp->pWaitMsgQueue = NULL;
00131 pTmp->sleepTicks = 0;
00132 otosWakeup(pTmp);
00133 }
00134
00135 pTmp = pNextTask;
00136 }
00137
00138 if (dispatch)
00139 otosScheduler(SCHED_NORM);
00140 else
00141 g_performDispatch = TRUE;
00142
00143 return MESSAGE_SUCCESS;
00144 }
00145
00146
00168 uint8_t otosReceiveMessage(OtosMessageQueue* pQueue, OtosMessage* pMsg, uint32_t timeout)
00169 {
00170 cli();
00171
00172
00173 if (pQueue->count > 0)
00174 {
00175 memcpy(pMsg, &pQueue->msg[pQueue->out], sizeof (OtosMessage));
00176 MQUEUE_INC_OUT(pQueue);
00177 sei();
00178 return MESSAGE_SUCCESS;
00179 }
00180
00181 if (timeout == 0)
00182 {
00183 sei();
00184 return MESSAGE_QUEUE_EMPTY;
00185 }
00186 else
00187 {
00188 g_pRunningTask->pWaitMsgQueue = pQueue;
00189 g_pRunningTask->sleepTicks = (timeout == INFINITE) ? 0 : timeout;
00190 otosBlock();
00191 cli();
00192 if (g_pRunningTask->pWaitMsgQueue)
00193 {
00194 g_pRunningTask->pWaitMsgQueue = NULL;
00195 sei();
00196 return MESSAGE_TIMEOUT;
00197 }
00198 else
00199 {
00200 memcpy(pMsg, &pQueue->msg[pQueue->out], sizeof (OtosMessage));
00201 MQUEUE_INC_OUT(pQueue);
00202 sei();
00203 return MESSAGE_SUCCESS;
00204 }
00205 }
00206 }
00207
00208