comparison src/gpf2/osl/os_tim_fl.c @ 487:91e8dac34ada

src/gpf2/osl: initial import from old freecalypso-sw tree
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 22 Jun 2018 05:56:16 +0000
parents
children c4117b996197
comparison
equal deleted inserted replaced
486:c433cca731a3 487:91e8dac34ada
1 /*
2 * This C module is a reconstruction based on the disassembly of
3 * os_tim.obj in frame_na7_db_fl.lib from the Leonardo package,
4 * subsequently reworked by Space Falcon.
5 */
6
7 /* set of included headers from COFF symtab: */
8 #include <stdio.h>
9 #include "gpfconf.h" /* FreeCalypso addition */
10 #include "../../nucleus/nucleus.h"
11 #include "typedefs.h"
12 #include "os.h"
13 #include "gdi.h"
14 #include "os_types.h"
15 #include "os_glob.h"
16
17 extern UNSIGNED TMD_Timer;
18 extern INT TMD_Timer_State;
19
20 extern T_OS_TIMER_ENTRY TimerTable[];
21 extern T_OS_TIMER_TABLE_ENTRY *p_list[];
22
23 extern void os_Timeout(UNSIGNED t_handle);
24 extern void timer_error(int err);
25
26 unsigned os_time_to_tick_multiplier = TIME_TO_TICK_TDMA_FRAME_MULTIPLIER;
27 unsigned os_tick_to_time_multiplier = TICK_TO_TIME_TDMA_FRAME_MULTIPLIER;
28
29 unsigned volatile t_start_ticks;
30 T_OS_TIMER_TABLE_ENTRY *t_running;
31 int used_timers;
32 int next_t_handle;
33 int volatile t_list_access;
34 int max_used_timers;
35 NU_SEMAPHORE TimSemCB;
36 NU_TIMER os_timer_cb;
37
38 #define BARRIER asm volatile ("": : :"memory")
39
40 GLOBAL LONG
41 os_set_tick(int os_system_tick)
42 {
43 switch (os_system_tick) {
44 case SYSTEM_TICK_TDMA_FRAME:
45 os_time_to_tick_multiplier = TIME_TO_TICK_TDMA_FRAME_MULTIPLIER;
46 os_tick_to_time_multiplier = TICK_TO_TIME_TDMA_FRAME_MULTIPLIER;
47 return(OS_OK);
48 case SYSTEM_TICK_10_MS:
49 os_time_to_tick_multiplier = TIME_TO_TICK_10MS_MULTIPLIER;
50 os_tick_to_time_multiplier = TICK_TO_TIME_10MS_MULTIPLIER;
51 return(OS_OK);
52 default:
53 return(OS_ERROR);
54 }
55 }
56
57 GLOBAL LONG
58 os_TimerInformation(USHORT Index, char *Buffer)
59 {
60 static int t_info_read;
61
62 if (t_info_read) {
63 t_info_read = 0;
64 return(OS_ERROR);
65 }
66 sprintf(Buffer, "Maximum %d of %d available timers running",
67 max_used_timers, MaxSimultaneousTimer);
68 t_info_read = 1;
69 return(OS_OK);
70 }
71
72 GLOBAL LONG
73 os_TimInit(void)
74 {
75 int i;
76
77 if (NU_Create_Semaphore(&TimSemCB, "TIMSEM", 1, NU_PRIORITY)
78 != NU_SUCCESS)
79 return(OS_ERROR);
80 if (NU_Create_Timer(&os_timer_cb, "OS_TIMER", os_Timeout, 0, 1, 0,
81 NU_DISABLE_TIMER) != NU_SUCCESS)
82 return(OS_ERROR);
83 used_timers = 0;
84 max_used_timers = 0;
85 next_t_handle = 1;
86 t_list_access = 0;
87 t_start_ticks = 0;
88 p_list[0] = 0;
89 for (i = 1; i < MaxSimultaneousTimer; i++) {
90 TimerTable[i].entry.status = TMR_FREE;
91 TimerTable[i].entry.next = 0;
92 TimerTable[i].entry.prev = 0;
93 TimerTable[i].next_t_handle = i + 1;
94 p_list[i] = 0;
95 }
96 TimerTable[MaxSimultaneousTimer].entry.status = TMR_FREE;
97 TimerTable[MaxSimultaneousTimer].next_t_handle = 0;
98 t_running = 0;
99 return(OS_OK);
100 }
101
102 GLOBAL LONG
103 os_RecoverTick(OS_TICK ticks)
104 {
105 UNSIGNED current_system_clock;
106
107 current_system_clock = NU_Retrieve_Clock();
108 NU_Set_Clock(current_system_clock + ticks);
109 if (TMD_Timer_State == TM_ACTIVE) {
110 if (TMD_Timer <= ticks) {
111 TMD_Timer_State = TM_EXPIRED;
112 TMD_Timer = 0;
113 } else
114 TMD_Timer -= ticks;
115 }
116 return(OS_OK);
117 }
118
119 GLOBAL LONG
120 os_QueryTimer(OS_HANDLE TaskHandle, OS_HANDLE TimerHandle,
121 OS_TIME *RemainingTime)
122 {
123 T_OS_TIMER_TABLE_ENTRY *timer, *t_iter;
124 OS_TICK c_ticks, r_ticks, e_ticks;
125 STATUS sts;
126
127 if (TimerHandle > MaxSimultaneousTimer)
128 return(OS_ERROR);
129 sts = NU_Obtain_Semaphore(&TimSemCB, NU_SUSPEND);
130 timer = &TimerTable[TimerHandle].entry;
131 if (timer->status == TMR_FREE) {
132 if (sts == NU_SUCCESS)
133 NU_Release_Semaphore(&TimSemCB);
134 return(OS_ERROR);
135 }
136 t_list_access = 1;
137 BARRIER;
138 if (!t_running) {
139 r_ticks = 0;
140 goto out;
141 }
142 c_ticks = NU_Retrieve_Clock();
143 e_ticks = c_ticks - t_start_ticks;
144 t_iter = t_running;
145 if (t_iter->r_ticks >= e_ticks)
146 r_ticks = t_iter->r_ticks - e_ticks;
147 else
148 r_ticks = 0;
149 while (t_iter != timer) {
150 t_iter = t_iter->next;
151 if (t_iter == t_running) {
152 r_ticks = 0;
153 goto out;
154 }
155 r_ticks += t_iter->r_ticks;
156 }
157 out: BARRIER;
158 t_list_access = 0;
159 if (sts == NU_SUCCESS)
160 NU_Release_Semaphore(&TimSemCB);
161 *RemainingTime = SYSTEM_TICKS_TO_TIME(r_ticks);
162 return(OS_OK);
163 }
164
165 GLOBAL LONG
166 os_InactivityTicks(int *next_event, OS_TICK *next_event_ticks)
167 {
168 *next_event = 1;
169 switch (TMD_Timer_State) {
170 case TM_ACTIVE:
171 *next_event_ticks = TMD_Timer;
172 return(OS_OK);
173 case TM_NOT_ACTIVE:
174 *next_event_ticks = 0;
175 *next_event = 0;
176 return(OS_OK);
177 default:
178 *next_event_ticks = 0;
179 return(OS_OK);
180 }
181 }