astroboy astroboy
/
CoOS_LWIP
Quick and dirty CoOS + LWIP ( Webserver )
CoOS/kernel/queue.c@0:94897d537b31, 2011-09-10 (annotated)
- Committer:
- astroboy
- Date:
- Sat Sep 10 22:41:10 2011 +0000
- Revision:
- 0:94897d537b31
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
astroboy | 0:94897d537b31 | 1 | /** |
astroboy | 0:94897d537b31 | 2 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 3 | * @file queue.c |
astroboy | 0:94897d537b31 | 4 | * @version V1.1.4 |
astroboy | 0:94897d537b31 | 5 | * @date 2011.04.20 |
astroboy | 0:94897d537b31 | 6 | * @brief Queue management implementation code of CooCox CoOS kernel. |
astroboy | 0:94897d537b31 | 7 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 8 | * @copy |
astroboy | 0:94897d537b31 | 9 | * |
astroboy | 0:94897d537b31 | 10 | * INTERNAL FILE,DON'T PUBLIC. |
astroboy | 0:94897d537b31 | 11 | * |
astroboy | 0:94897d537b31 | 12 | * <h2><center>© COPYRIGHT 2009 CooCox </center></h2> |
astroboy | 0:94897d537b31 | 13 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 14 | */ |
astroboy | 0:94897d537b31 | 15 | |
astroboy | 0:94897d537b31 | 16 | /*---------------------------- Include ---------------------------------------*/ |
astroboy | 0:94897d537b31 | 17 | #include <coocox.h> |
astroboy | 0:94897d537b31 | 18 | |
astroboy | 0:94897d537b31 | 19 | |
astroboy | 0:94897d537b31 | 20 | #if CFG_QUEUE_EN > 0 |
astroboy | 0:94897d537b31 | 21 | /*---------------------------- Variable Define -------------------------------*/ |
astroboy | 0:94897d537b31 | 22 | QCB QueueTbl[CFG_MAX_QUEUE] = {{0}}; /*!< Queue control block table */ |
astroboy | 0:94897d537b31 | 23 | U32 QueueIDVessel = 0; /*!< Queue list mask */ |
astroboy | 0:94897d537b31 | 24 | |
astroboy | 0:94897d537b31 | 25 | |
astroboy | 0:94897d537b31 | 26 | |
astroboy | 0:94897d537b31 | 27 | /** |
astroboy | 0:94897d537b31 | 28 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 29 | * @brief Create a queue |
astroboy | 0:94897d537b31 | 30 | * @param[in] qStart Pointer to mail pointer buffer. |
astroboy | 0:94897d537b31 | 31 | * @param[in] size The length of queue. |
astroboy | 0:94897d537b31 | 32 | * @param[in] sortType Mail queue waiting list sort type. |
astroboy | 0:94897d537b31 | 33 | * @param[out] None |
astroboy | 0:94897d537b31 | 34 | * @retval E_CREATE_FAIL Create queue fail. |
astroboy | 0:94897d537b31 | 35 | * @retval others Create queue successful. |
astroboy | 0:94897d537b31 | 36 | * |
astroboy | 0:94897d537b31 | 37 | * @par Description |
astroboy | 0:94897d537b31 | 38 | * @details This function is called to create a queue. |
astroboy | 0:94897d537b31 | 39 | * @note |
astroboy | 0:94897d537b31 | 40 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 41 | */ |
astroboy | 0:94897d537b31 | 42 | OS_EventID CoCreateQueue(void **qStart, U16 size ,U8 sortType) |
astroboy | 0:94897d537b31 | 43 | { |
astroboy | 0:94897d537b31 | 44 | U8 i; |
astroboy | 0:94897d537b31 | 45 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 46 | |
astroboy | 0:94897d537b31 | 47 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 48 | if((qStart == Co_NULL) || (size == 0)) |
astroboy | 0:94897d537b31 | 49 | { |
astroboy | 0:94897d537b31 | 50 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 51 | } |
astroboy | 0:94897d537b31 | 52 | #endif |
astroboy | 0:94897d537b31 | 53 | |
astroboy | 0:94897d537b31 | 54 | OsSchedLock(); |
astroboy | 0:94897d537b31 | 55 | for(i = 0; i < CFG_MAX_QUEUE; i++) |
astroboy | 0:94897d537b31 | 56 | { |
astroboy | 0:94897d537b31 | 57 | /* Assign a free QUEUE control block */ |
astroboy | 0:94897d537b31 | 58 | if((QueueIDVessel & (1 << i)) == 0) |
astroboy | 0:94897d537b31 | 59 | { |
astroboy | 0:94897d537b31 | 60 | QueueIDVessel |= (1<<i); |
astroboy | 0:94897d537b31 | 61 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 62 | |
astroboy | 0:94897d537b31 | 63 | QueueTbl[i].qStart = qStart; /* Initialize the queue */ |
astroboy | 0:94897d537b31 | 64 | QueueTbl[i].id = i; |
astroboy | 0:94897d537b31 | 65 | QueueTbl[i].head = 0; |
astroboy | 0:94897d537b31 | 66 | QueueTbl[i].tail = 0; |
astroboy | 0:94897d537b31 | 67 | QueueTbl[i].qMaxSize = size; |
astroboy | 0:94897d537b31 | 68 | QueueTbl[i].qSize = 0; |
astroboy | 0:94897d537b31 | 69 | |
astroboy | 0:94897d537b31 | 70 | /* Get a event control block and initial the event content */ |
astroboy | 0:94897d537b31 | 71 | pecb = CreatEvent(EVENT_TYPE_QUEUE,sortType,&QueueTbl[i]); |
astroboy | 0:94897d537b31 | 72 | |
astroboy | 0:94897d537b31 | 73 | if(pecb == Co_NULL ) /* If there is no free EVENT control block*/ |
astroboy | 0:94897d537b31 | 74 | { |
astroboy | 0:94897d537b31 | 75 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 76 | } |
astroboy | 0:94897d537b31 | 77 | return (pecb->id); |
astroboy | 0:94897d537b31 | 78 | } |
astroboy | 0:94897d537b31 | 79 | } |
astroboy | 0:94897d537b31 | 80 | |
astroboy | 0:94897d537b31 | 81 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 82 | return E_CREATE_FAIL; /* There is no free QUEUE control block */ |
astroboy | 0:94897d537b31 | 83 | } |
astroboy | 0:94897d537b31 | 84 | |
astroboy | 0:94897d537b31 | 85 | |
astroboy | 0:94897d537b31 | 86 | /** |
astroboy | 0:94897d537b31 | 87 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 88 | * @brief Delete a queue |
astroboy | 0:94897d537b31 | 89 | * @param[in] id Event ID. |
astroboy | 0:94897d537b31 | 90 | * @param[in] opt Delete option. |
astroboy | 0:94897d537b31 | 91 | * @param[out] None |
astroboy | 0:94897d537b31 | 92 | * @retval E_INVALID_ID Invalid event ID. |
astroboy | 0:94897d537b31 | 93 | * @retval E_INVALID_PARAMETER Invalid parameter. |
astroboy | 0:94897d537b31 | 94 | * @retval E_TASK_WAITTING Tasks waitting for the event,delete fail. |
astroboy | 0:94897d537b31 | 95 | * @retval E_OK Event deleted successful. |
astroboy | 0:94897d537b31 | 96 | * |
astroboy | 0:94897d537b31 | 97 | * @par Description |
astroboy | 0:94897d537b31 | 98 | * @details This function is called to delete a queue. |
astroboy | 0:94897d537b31 | 99 | * @note |
astroboy | 0:94897d537b31 | 100 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 101 | */ |
astroboy | 0:94897d537b31 | 102 | StatusType CoDelQueue(OS_EventID id,U8 opt) |
astroboy | 0:94897d537b31 | 103 | { |
astroboy | 0:94897d537b31 | 104 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 105 | P_QCB pqcb; |
astroboy | 0:94897d537b31 | 106 | StatusType err; |
astroboy | 0:94897d537b31 | 107 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 108 | if(id >= CFG_MAX_EVENT) |
astroboy | 0:94897d537b31 | 109 | { |
astroboy | 0:94897d537b31 | 110 | return E_INVALID_ID; /* Invalid id,return error */ |
astroboy | 0:94897d537b31 | 111 | } |
astroboy | 0:94897d537b31 | 112 | #endif |
astroboy | 0:94897d537b31 | 113 | |
astroboy | 0:94897d537b31 | 114 | pecb = &EventTbl[id]; |
astroboy | 0:94897d537b31 | 115 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 116 | if( pecb->eventType != EVENT_TYPE_QUEUE) |
astroboy | 0:94897d537b31 | 117 | { |
astroboy | 0:94897d537b31 | 118 | return E_INVALID_ID; /* The event is not queue,return error*/ |
astroboy | 0:94897d537b31 | 119 | } |
astroboy | 0:94897d537b31 | 120 | #endif |
astroboy | 0:94897d537b31 | 121 | pqcb = (P_QCB)pecb->eventPtr; /* Point at queue control block */ |
astroboy | 0:94897d537b31 | 122 | err = DeleteEvent(pecb,opt); /* Delete the event control block */ |
astroboy | 0:94897d537b31 | 123 | if(err == E_OK) /* If the event block have been deleted */ |
astroboy | 0:94897d537b31 | 124 | { |
astroboy | 0:94897d537b31 | 125 | QueueIDVessel &= ~((U32)(1<<(pqcb->id))); /* Update free queue list */ |
astroboy | 0:94897d537b31 | 126 | pqcb->qStart = Co_NULL; |
astroboy | 0:94897d537b31 | 127 | pqcb->id = 0; |
astroboy | 0:94897d537b31 | 128 | pqcb->head = 0; |
astroboy | 0:94897d537b31 | 129 | pqcb->tail = 0; |
astroboy | 0:94897d537b31 | 130 | pqcb->qMaxSize = 0; |
astroboy | 0:94897d537b31 | 131 | pqcb->qSize = 0; |
astroboy | 0:94897d537b31 | 132 | } |
astroboy | 0:94897d537b31 | 133 | return err; |
astroboy | 0:94897d537b31 | 134 | } |
astroboy | 0:94897d537b31 | 135 | |
astroboy | 0:94897d537b31 | 136 | |
astroboy | 0:94897d537b31 | 137 | |
astroboy | 0:94897d537b31 | 138 | /** |
astroboy | 0:94897d537b31 | 139 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 140 | * @brief Accept a mail from queue |
astroboy | 0:94897d537b31 | 141 | * @param[in] id Event ID. |
astroboy | 0:94897d537b31 | 142 | * @param[out] perr A pointer to error code. |
astroboy | 0:94897d537b31 | 143 | * @retval Co_NULL |
astroboy | 0:94897d537b31 | 144 | * @retval A pointer to mail accepted. |
astroboy | 0:94897d537b31 | 145 | * |
astroboy | 0:94897d537b31 | 146 | * @par Description |
astroboy | 0:94897d537b31 | 147 | * @details This function is called to accept a mail from queue. |
astroboy | 0:94897d537b31 | 148 | * @note |
astroboy | 0:94897d537b31 | 149 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 150 | */ |
astroboy | 0:94897d537b31 | 151 | void* CoAcceptQueueMail(OS_EventID id,StatusType* perr) |
astroboy | 0:94897d537b31 | 152 | { |
astroboy | 0:94897d537b31 | 153 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 154 | P_QCB pqcb; |
astroboy | 0:94897d537b31 | 155 | void* pmail; |
astroboy | 0:94897d537b31 | 156 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 157 | if(id >= CFG_MAX_EVENT) |
astroboy | 0:94897d537b31 | 158 | { |
astroboy | 0:94897d537b31 | 159 | *perr = E_INVALID_ID; /* Invalid id,return error */ |
astroboy | 0:94897d537b31 | 160 | return Co_NULL; |
astroboy | 0:94897d537b31 | 161 | } |
astroboy | 0:94897d537b31 | 162 | #endif |
astroboy | 0:94897d537b31 | 163 | |
astroboy | 0:94897d537b31 | 164 | pecb = &EventTbl[id]; |
astroboy | 0:94897d537b31 | 165 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 166 | if(pecb->eventType != EVENT_TYPE_QUEUE)/* Invalid event control block type*/ |
astroboy | 0:94897d537b31 | 167 | { |
astroboy | 0:94897d537b31 | 168 | *perr = E_INVALID_ID; |
astroboy | 0:94897d537b31 | 169 | return Co_NULL; |
astroboy | 0:94897d537b31 | 170 | } |
astroboy | 0:94897d537b31 | 171 | #endif |
astroboy | 0:94897d537b31 | 172 | pqcb = (P_QCB)pecb->eventPtr; /* Point at queue control block */ |
astroboy | 0:94897d537b31 | 173 | OsSchedLock(); |
astroboy | 0:94897d537b31 | 174 | if(pqcb->qSize != 0) /* If there are any messages in the queue */ |
astroboy | 0:94897d537b31 | 175 | { |
astroboy | 0:94897d537b31 | 176 | /* Extract oldest message from the queue */ |
astroboy | 0:94897d537b31 | 177 | pmail = *(pqcb->qStart + pqcb->head); |
astroboy | 0:94897d537b31 | 178 | pqcb->head++; /* Update the queue head */ |
astroboy | 0:94897d537b31 | 179 | pqcb->qSize--; /* Update the number of messages in the queue */ |
astroboy | 0:94897d537b31 | 180 | if(pqcb->head == pqcb->qMaxSize) |
astroboy | 0:94897d537b31 | 181 | { |
astroboy | 0:94897d537b31 | 182 | pqcb->head = 0; |
astroboy | 0:94897d537b31 | 183 | } |
astroboy | 0:94897d537b31 | 184 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 185 | *perr = E_OK; |
astroboy | 0:94897d537b31 | 186 | return pmail; /* Return message received */ |
astroboy | 0:94897d537b31 | 187 | } |
astroboy | 0:94897d537b31 | 188 | else /* If there is no message in the queue*/ |
astroboy | 0:94897d537b31 | 189 | { |
astroboy | 0:94897d537b31 | 190 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 191 | *perr = E_QUEUE_EMPTY; |
astroboy | 0:94897d537b31 | 192 | return Co_NULL; /* Return Co_NULL */ |
astroboy | 0:94897d537b31 | 193 | } |
astroboy | 0:94897d537b31 | 194 | } |
astroboy | 0:94897d537b31 | 195 | |
astroboy | 0:94897d537b31 | 196 | |
astroboy | 0:94897d537b31 | 197 | |
astroboy | 0:94897d537b31 | 198 | /** |
astroboy | 0:94897d537b31 | 199 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 200 | * @brief Pend for a mail |
astroboy | 0:94897d537b31 | 201 | * @param[in] id Event ID. |
astroboy | 0:94897d537b31 | 202 | * @param[in] timeout The longest time for writting mail. |
astroboy | 0:94897d537b31 | 203 | * @param[out] perr A pointer to error code. |
astroboy | 0:94897d537b31 | 204 | * @retval Co_NULL |
astroboy | 0:94897d537b31 | 205 | * @retval A pointer to mail accept. |
astroboy | 0:94897d537b31 | 206 | * |
astroboy | 0:94897d537b31 | 207 | * @par Description |
astroboy | 0:94897d537b31 | 208 | * @details This function is called to wait for a mail. |
astroboy | 0:94897d537b31 | 209 | * @note |
astroboy | 0:94897d537b31 | 210 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 211 | */ |
astroboy | 0:94897d537b31 | 212 | void* CoPendQueueMail(OS_EventID id,U32 timeout,StatusType* perr) |
astroboy | 0:94897d537b31 | 213 | { |
astroboy | 0:94897d537b31 | 214 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 215 | P_QCB pqcb; |
astroboy | 0:94897d537b31 | 216 | P_OSTCB curTCB; |
astroboy | 0:94897d537b31 | 217 | void* pmail; |
astroboy | 0:94897d537b31 | 218 | if(OSIntNesting > 0) /* If the caller is ISR */ |
astroboy | 0:94897d537b31 | 219 | { |
astroboy | 0:94897d537b31 | 220 | *perr = E_CALL; |
astroboy | 0:94897d537b31 | 221 | return Co_NULL; |
astroboy | 0:94897d537b31 | 222 | } |
astroboy | 0:94897d537b31 | 223 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 224 | if(id >= CFG_MAX_EVENT) |
astroboy | 0:94897d537b31 | 225 | { |
astroboy | 0:94897d537b31 | 226 | *perr = E_INVALID_ID; /* Invalid event id,return error */ |
astroboy | 0:94897d537b31 | 227 | return Co_NULL; |
astroboy | 0:94897d537b31 | 228 | } |
astroboy | 0:94897d537b31 | 229 | #endif |
astroboy | 0:94897d537b31 | 230 | |
astroboy | 0:94897d537b31 | 231 | pecb = &EventTbl[id]; |
astroboy | 0:94897d537b31 | 232 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 233 | if(pecb->eventType != EVENT_TYPE_QUEUE) /* The event type is not queue */ |
astroboy | 0:94897d537b31 | 234 | { |
astroboy | 0:94897d537b31 | 235 | *perr = E_INVALID_ID; |
astroboy | 0:94897d537b31 | 236 | return Co_NULL; |
astroboy | 0:94897d537b31 | 237 | } |
astroboy | 0:94897d537b31 | 238 | #endif |
astroboy | 0:94897d537b31 | 239 | if(OSSchedLock != 0) /* Judge schedule is locked or not? */ |
astroboy | 0:94897d537b31 | 240 | { |
astroboy | 0:94897d537b31 | 241 | *perr = E_OS_IN_LOCK; /* Schedule is locked,return error */ |
astroboy | 0:94897d537b31 | 242 | return Co_NULL; |
astroboy | 0:94897d537b31 | 243 | } |
astroboy | 0:94897d537b31 | 244 | pqcb = (P_QCB)pecb->eventPtr; /* Point at queue control block */ |
astroboy | 0:94897d537b31 | 245 | OsSchedLock(); |
astroboy | 0:94897d537b31 | 246 | if(pqcb->qSize != 0) /* If there are any messages in the queue */ |
astroboy | 0:94897d537b31 | 247 | { |
astroboy | 0:94897d537b31 | 248 | /* Extract oldest message from the queue */ |
astroboy | 0:94897d537b31 | 249 | pmail = *(pqcb->qStart + pqcb->head); |
astroboy | 0:94897d537b31 | 250 | pqcb->head++; /* Update the queue head */ |
astroboy | 0:94897d537b31 | 251 | pqcb->qSize--; /* Update the number of messages in the queue */ |
astroboy | 0:94897d537b31 | 252 | if(pqcb->head == pqcb->qMaxSize)/* Check queue head */ |
astroboy | 0:94897d537b31 | 253 | { |
astroboy | 0:94897d537b31 | 254 | pqcb->head = 0; |
astroboy | 0:94897d537b31 | 255 | } |
astroboy | 0:94897d537b31 | 256 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 257 | *perr = E_OK; |
astroboy | 0:94897d537b31 | 258 | return pmail; /* Return message received */ |
astroboy | 0:94897d537b31 | 259 | } |
astroboy | 0:94897d537b31 | 260 | else /* If there is no message in the queue*/ |
astroboy | 0:94897d537b31 | 261 | { |
astroboy | 0:94897d537b31 | 262 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 263 | curTCB = TCBRunning; |
astroboy | 0:94897d537b31 | 264 | if(timeout == 0) /* If time-out is not configured */ |
astroboy | 0:94897d537b31 | 265 | { |
astroboy | 0:94897d537b31 | 266 | /* Block current task until the event occur */ |
astroboy | 0:94897d537b31 | 267 | EventTaskToWait(pecb,curTCB); |
astroboy | 0:94897d537b31 | 268 | |
astroboy | 0:94897d537b31 | 269 | /* Have recived message or the queue have been deleted */ |
astroboy | 0:94897d537b31 | 270 | pmail = curTCB->pmail; |
astroboy | 0:94897d537b31 | 271 | curTCB->pmail = Co_NULL; |
astroboy | 0:94897d537b31 | 272 | *perr = E_OK; |
astroboy | 0:94897d537b31 | 273 | return pmail; /* Return message received or Co_NULL */ |
astroboy | 0:94897d537b31 | 274 | } |
astroboy | 0:94897d537b31 | 275 | else /* If time-out is configured */ |
astroboy | 0:94897d537b31 | 276 | { |
astroboy | 0:94897d537b31 | 277 | OsSchedLock(); |
astroboy | 0:94897d537b31 | 278 | |
astroboy | 0:94897d537b31 | 279 | /* Block current task until event or timeout occurs */ |
astroboy | 0:94897d537b31 | 280 | EventTaskToWait(pecb,curTCB); |
astroboy | 0:94897d537b31 | 281 | InsertDelayList(curTCB,timeout); |
astroboy | 0:94897d537b31 | 282 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 283 | if(curTCB->pmail == Co_NULL) /* If time-out occurred */ |
astroboy | 0:94897d537b31 | 284 | { |
astroboy | 0:94897d537b31 | 285 | *perr = E_TIMEOUT; |
astroboy | 0:94897d537b31 | 286 | return Co_NULL; |
astroboy | 0:94897d537b31 | 287 | } |
astroboy | 0:94897d537b31 | 288 | else /* If event occured */ |
astroboy | 0:94897d537b31 | 289 | { |
astroboy | 0:94897d537b31 | 290 | pmail = curTCB->pmail; |
astroboy | 0:94897d537b31 | 291 | curTCB->pmail = Co_NULL; |
astroboy | 0:94897d537b31 | 292 | *perr = E_OK; |
astroboy | 0:94897d537b31 | 293 | return pmail; /* Return message received or Co_NULL */ |
astroboy | 0:94897d537b31 | 294 | } |
astroboy | 0:94897d537b31 | 295 | } |
astroboy | 0:94897d537b31 | 296 | } |
astroboy | 0:94897d537b31 | 297 | } |
astroboy | 0:94897d537b31 | 298 | |
astroboy | 0:94897d537b31 | 299 | |
astroboy | 0:94897d537b31 | 300 | |
astroboy | 0:94897d537b31 | 301 | /** |
astroboy | 0:94897d537b31 | 302 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 303 | * @brief Post a mail to queue |
astroboy | 0:94897d537b31 | 304 | * @param[in] id Event ID. |
astroboy | 0:94897d537b31 | 305 | * @param[in] pmail Pointer to mail that want to send. |
astroboy | 0:94897d537b31 | 306 | * @param[out] None |
astroboy | 0:94897d537b31 | 307 | * @retval E_OK |
astroboy | 0:94897d537b31 | 308 | * @retval E_INVALID_ID |
astroboy | 0:94897d537b31 | 309 | * @retval E_QUEUE_FULL |
astroboy | 0:94897d537b31 | 310 | * |
astroboy | 0:94897d537b31 | 311 | * @par Description |
astroboy | 0:94897d537b31 | 312 | * @details This function is called to post a mail to queue. |
astroboy | 0:94897d537b31 | 313 | * @note |
astroboy | 0:94897d537b31 | 314 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 315 | */ |
astroboy | 0:94897d537b31 | 316 | StatusType CoPostQueueMail(OS_EventID id,void* pmail) |
astroboy | 0:94897d537b31 | 317 | { |
astroboy | 0:94897d537b31 | 318 | P_ECB pecb; |
astroboy | 0:94897d537b31 | 319 | P_QCB pqcb; |
astroboy | 0:94897d537b31 | 320 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 321 | if(id >= CFG_MAX_EVENT) |
astroboy | 0:94897d537b31 | 322 | { |
astroboy | 0:94897d537b31 | 323 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 324 | } |
astroboy | 0:94897d537b31 | 325 | #endif |
astroboy | 0:94897d537b31 | 326 | |
astroboy | 0:94897d537b31 | 327 | pecb = &EventTbl[id]; |
astroboy | 0:94897d537b31 | 328 | #if CFG_PAR_CHECKOUT_EN >0 |
astroboy | 0:94897d537b31 | 329 | if(pecb->eventType != EVENT_TYPE_QUEUE) |
astroboy | 0:94897d537b31 | 330 | { |
astroboy | 0:94897d537b31 | 331 | return E_INVALID_ID; /* The event type isn't queue,return */ |
astroboy | 0:94897d537b31 | 332 | } |
astroboy | 0:94897d537b31 | 333 | #endif |
astroboy | 0:94897d537b31 | 334 | pqcb = (P_QCB)pecb->eventPtr; |
astroboy | 0:94897d537b31 | 335 | if(pqcb->qSize == pqcb->qMaxSize) /* If queue is full */ |
astroboy | 0:94897d537b31 | 336 | { |
astroboy | 0:94897d537b31 | 337 | return E_QUEUE_FULL; |
astroboy | 0:94897d537b31 | 338 | } |
astroboy | 0:94897d537b31 | 339 | else /* If queue is not full */ |
astroboy | 0:94897d537b31 | 340 | { |
astroboy | 0:94897d537b31 | 341 | OsSchedLock(); |
astroboy | 0:94897d537b31 | 342 | *(pqcb->qStart + pqcb->tail) = pmail; /* Insert message into queue */ |
astroboy | 0:94897d537b31 | 343 | pqcb->tail++; /* Update queue tail */ |
astroboy | 0:94897d537b31 | 344 | pqcb->qSize++; /* Update the number of messages in the queue */ |
astroboy | 0:94897d537b31 | 345 | if(pqcb->tail == pqcb->qMaxSize) /* Check queue tail */ |
astroboy | 0:94897d537b31 | 346 | { |
astroboy | 0:94897d537b31 | 347 | pqcb->tail = 0; |
astroboy | 0:94897d537b31 | 348 | } |
astroboy | 0:94897d537b31 | 349 | EventTaskToRdy(pecb); /* Check the event waiting list */ |
astroboy | 0:94897d537b31 | 350 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 351 | return E_OK; |
astroboy | 0:94897d537b31 | 352 | } |
astroboy | 0:94897d537b31 | 353 | } |
astroboy | 0:94897d537b31 | 354 | |
astroboy | 0:94897d537b31 | 355 | |
astroboy | 0:94897d537b31 | 356 | /** |
astroboy | 0:94897d537b31 | 357 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 358 | * @brief Post a mail to queue in ISR |
astroboy | 0:94897d537b31 | 359 | * @param[in] id Event ID. |
astroboy | 0:94897d537b31 | 360 | * @param[in] pmail Pointer to mail that want to send. |
astroboy | 0:94897d537b31 | 361 | * @param[out] None |
astroboy | 0:94897d537b31 | 362 | * @retval E_OK |
astroboy | 0:94897d537b31 | 363 | * @retval E_INVALID_ID |
astroboy | 0:94897d537b31 | 364 | * @retval E_QUEUE_FULL |
astroboy | 0:94897d537b31 | 365 | * |
astroboy | 0:94897d537b31 | 366 | * @par Description |
astroboy | 0:94897d537b31 | 367 | * @details This function is called in ISR to post a mail to queue. |
astroboy | 0:94897d537b31 | 368 | * @note |
astroboy | 0:94897d537b31 | 369 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 370 | */ |
astroboy | 0:94897d537b31 | 371 | #if CFG_MAX_SERVICE_REQUEST > 0 |
astroboy | 0:94897d537b31 | 372 | StatusType isr_PostQueueMail(OS_EventID id,void* pmail) |
astroboy | 0:94897d537b31 | 373 | { |
astroboy | 0:94897d537b31 | 374 | if(OSSchedLock > 0) /* If scheduler is locked,(the caller is ISR) */ |
astroboy | 0:94897d537b31 | 375 | { |
astroboy | 0:94897d537b31 | 376 | /* Insert the request into service request queue */ |
astroboy | 0:94897d537b31 | 377 | if(InsertInSRQ(QUEUE_REQ,id,pmail) == Co_FALSE) |
astroboy | 0:94897d537b31 | 378 | { |
astroboy | 0:94897d537b31 | 379 | return E_SEV_REQ_FULL; /* If service request queue is full */ |
astroboy | 0:94897d537b31 | 380 | } |
astroboy | 0:94897d537b31 | 381 | else /* If the request have been inserted into service request queue */ |
astroboy | 0:94897d537b31 | 382 | { |
astroboy | 0:94897d537b31 | 383 | return E_OK; |
astroboy | 0:94897d537b31 | 384 | } |
astroboy | 0:94897d537b31 | 385 | } |
astroboy | 0:94897d537b31 | 386 | else /* The scheduler is unlocked */ |
astroboy | 0:94897d537b31 | 387 | { |
astroboy | 0:94897d537b31 | 388 | return(CoPostQueueMail(id,pmail)); /* Send the message to the queue*/ |
astroboy | 0:94897d537b31 | 389 | } |
astroboy | 0:94897d537b31 | 390 | } |
astroboy | 0:94897d537b31 | 391 | #endif |
astroboy | 0:94897d537b31 | 392 | |
astroboy | 0:94897d537b31 | 393 | #endif |