UCOS_TI_LM3S_Keil
 全部 结构体 文件 函数 变量 类型定义 宏定义 
os_time.c
浏览该文件的文档.
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * The Real-Time Kernel
5 * TIME MANAGEMENT
6 *
7 * (c) Copyright 1992-2009, Micrium, Weston, FL
8 * All Rights Reserved
9 *
10 * File : OS_TIME.C
11 * By : Jean J. Labrosse
12 * Version : V2.89
13 *
14 * LICENSING TERMS:
15 * ---------------
16 * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
17 * If you plan on using uC/OS-II in a commercial product you need to contact Micrim to properly license
18 * its use in your product. We provide ALL the source code for your convenience and to help you experience
19 * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
20 * licensing fee.
21 *********************************************************************************************************
22 */
23 
24 #ifndef OS_MASTER_FILE
25 #include <ucos_ii.h>
26 #endif
27 
28 /*
29 *********************************************************************************************************
30 * DELAY TASK 'n' TICKS
31 *
32 * Description: This function is called to delay execution of the currently running task until the
33 * specified number of system ticks expires. This, of course, directly equates to delaying
34 * the current task for some time to expire. No delay will result If the specified delay is
35 * 0. If the specified delay is greater than 0 then, a context switch will result.
36 *
37 * Arguments : ticks is the time delay that the task will be suspended in number of clock 'ticks'.
38 * Note that by specifying 0, the task will not be delayed.
39 *
40 * Returns : none
41 *********************************************************************************************************
42 */
43 
44 void OSTimeDly (INT32U ticks)
45 {
46  INT8U y;
47 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
48  OS_CPU_SR cpu_sr = 0u;
49 #endif
50 
51 
52 
53  if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
54  return;
55  }
56  if (ticks > 0u) { /* 0 means no delay! */
58  y = OSTCBCur->OSTCBY; /* Delay current task */
59  OSRdyTbl[y] &= ~OSTCBCur->OSTCBBitX;
60  if (OSRdyTbl[y] == 0u) {
62  }
63  OSTCBCur->OSTCBDly = ticks; /* Load ticks in TCB */
65  OS_Sched(); /* Find next task to run! */
66  }
67 }
68 /*$PAGE*/
69 /*
70 *********************************************************************************************************
71 * DELAY TASK FOR SPECIFIED TIME
72 *
73 * Description: This function is called to delay execution of the currently running task until some time
74 * expires. This call allows you to specify the delay time in HOURS, MINUTES, SECONDS and
75 * MILLISECONDS instead of ticks.
76 *
77 * Arguments : hours specifies the number of hours that the task will be delayed (max. is 255)
78 * minutes specifies the number of minutes (max. 59)
79 * seconds specifies the number of seconds (max. 59)
80 * ms specifies the number of milliseconds (max. 999)
81 *
82 * Returns : OS_ERR_NONE
83 * OS_ERR_TIME_INVALID_MINUTES
84 * OS_ERR_TIME_INVALID_SECONDS
85 * OS_ERR_TIME_INVALID_MS
86 * OS_ERR_TIME_ZERO_DLY
87 * OS_ERR_TIME_DLY_ISR
88 *
89 * Note(s) : The resolution on the milliseconds depends on the tick rate. For example, you can't do
90 * a 10 mS delay if the ticker interrupts every 100 mS. In this case, the delay would be
91 * set to 0. The actual delay is rounded to the nearest tick.
92 *********************************************************************************************************
93 */
94 
95 #if OS_TIME_DLY_HMSM_EN > 0u
97  INT8U minutes,
98  INT8U seconds,
99  INT16U ms)
100 {
101  INT32U ticks;
102 
103 
104  if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
105  return (OS_ERR_TIME_DLY_ISR);
106  }
107 #if OS_ARG_CHK_EN > 0u
108  if (hours == 0u) {
109  if (minutes == 0u) {
110  if (seconds == 0u) {
111  if (ms == 0u) {
112  return (OS_ERR_TIME_ZERO_DLY);
113  }
114  }
115  }
116  }
117  if (minutes > 59u) {
118  return (OS_ERR_TIME_INVALID_MINUTES); /* Validate arguments to be within range */
119  }
120  if (seconds > 59u) {
122  }
123  if (ms > 999u) {
124  return (OS_ERR_TIME_INVALID_MS);
125  }
126 #endif
127  /* Compute the total number of clock ticks required.. */
128  /* .. (rounded to the nearest tick) */
129  ticks = ((INT32U)hours * 3600uL + (INT32U)minutes * 60uL + (INT32U)seconds) * OS_TICKS_PER_SEC
130  + OS_TICKS_PER_SEC * ((INT32U)ms + 500uL / OS_TICKS_PER_SEC) / 1000uL;
131  OSTimeDly(ticks);
132  return (OS_ERR_NONE);
133 }
134 #endif
135 /*$PAGE*/
136 /*
137 *********************************************************************************************************
138 * RESUME A DELAYED TASK
139 *
140 * Description: This function is used resume a task that has been delayed through a call to either
141 * OSTimeDly() or OSTimeDlyHMSM(). Note that you can call this function to resume a
142 * task that is waiting for an event with timeout. This would make the task look
143 * like a timeout occurred.
144 *
145 * Arguments : prio specifies the priority of the task to resume
146 *
147 * Returns : OS_ERR_NONE Task has been resumed
148 * OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum allowed
149 * (i.e. >= OS_LOWEST_PRIO)
150 * OS_ERR_TIME_NOT_DLY Task is not waiting for time to expire
151 * OS_ERR_TASK_NOT_EXIST The desired task has not been created or has been assigned to a Mutex.
152 *********************************************************************************************************
153 */
154 
155 #if OS_TIME_DLY_RESUME_EN > 0u
157 {
158  OS_TCB *ptcb;
159 #if OS_CRITICAL_METHOD == 3u /* Storage for CPU status register */
160  OS_CPU_SR cpu_sr = 0u;
161 #endif
162 
163 
164 
165  if (prio >= OS_LOWEST_PRIO) {
166  return (OS_ERR_PRIO_INVALID);
167  }
169  ptcb = OSTCBPrioTbl[prio]; /* Make sure that task exist */
170  if (ptcb == (OS_TCB *)0) {
172  return (OS_ERR_TASK_NOT_EXIST); /* The task does not exist */
173  }
174  if (ptcb == OS_TCB_RESERVED) {
176  return (OS_ERR_TASK_NOT_EXIST); /* The task does not exist */
177  }
178  if (ptcb->OSTCBDly == 0u) { /* See if task is delayed */
180  return (OS_ERR_TIME_NOT_DLY); /* Indicate that task was not delayed */
181  }
182 
183  ptcb->OSTCBDly = 0u; /* Clear the time delay */
184  if ((ptcb->OSTCBStat & OS_STAT_PEND_ANY) != OS_STAT_RDY) {
185  ptcb->OSTCBStat &= ~OS_STAT_PEND_ANY; /* Yes, Clear status flag */
186  ptcb->OSTCBStatPend = OS_STAT_PEND_TO; /* Indicate PEND timeout */
187  } else {
189  }
190  if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) { /* Is task suspended? */
191  OSRdyGrp |= ptcb->OSTCBBitY; /* No, Make ready */
192  OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
194  OS_Sched(); /* See if this is new highest priority */
195  } else {
196  OS_EXIT_CRITICAL(); /* Task may be suspended */
197  }
198  return (OS_ERR_NONE);
199 }
200 #endif
201 /*$PAGE*/
202 /*
203 *********************************************************************************************************
204 * GET CURRENT SYSTEM TIME
205 *
206 * Description: This function is used by your application to obtain the current value of the 32-bit
207 * counter which keeps track of the number of clock ticks.
208 *
209 * Arguments : none
210 *
211 * Returns : The current value of OSTime
212 *********************************************************************************************************
213 */
214 
215 #if OS_TIME_GET_SET_EN > 0u
217 {
218  INT32U ticks;
219 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
220  OS_CPU_SR cpu_sr = 0u;
221 #endif
222 
223 
224 
226  ticks = OSTime;
228  return (ticks);
229 }
230 #endif
231 
232 /*
233 *********************************************************************************************************
234 * SET SYSTEM CLOCK
235 *
236 * Description: This function sets the 32-bit counter which keeps track of the number of clock ticks.
237 *
238 * Arguments : ticks specifies the new value that OSTime needs to take.
239 *
240 * Returns : none
241 *********************************************************************************************************
242 */
243 
244 #if OS_TIME_GET_SET_EN > 0u
245 void OSTimeSet (INT32U ticks)
246 {
247 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
248  OS_CPU_SR cpu_sr = 0u;
249 #endif
250 
251 
252 
254  OSTime = ticks;
256 }
257 #endif