]> git.sur5r.net Git - freertos/blob - Demo/HCS12_CodeWarrior_banked/CODE/TickTimer.C
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / HCS12_CodeWarrior_banked / CODE / TickTimer.C
1 /** ###################################################################\r
2 **     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.\r
3 **     Filename  : TickTimer.C\r
4 **     Project   : RTOSDemo\r
5 **     Processor : MC9S12DP256BCPV\r
6 **     Beantype  : TimerInt\r
7 **     Version   : Bean 02.063, Driver 01.05, CPU db: 2.87.283\r
8 **     Compiler  : Metrowerks HC12 C Compiler\r
9 **     Date/Time : 18/06/2005, 16:21\r
10 **     Abstract  :\r
11 **         This bean "TimerInt" implements a periodic interrupt.\r
12 **         When the bean and its events are enabled, the "OnInterrupt"\r
13 **         event is called periodically with the period that you specify.\r
14 **         TimerInt supports also changing the period in runtime.\r
15 **         The source of periodic interrupt can be timer compare or reload \r
16 **         register or timer-overflow interrupt (of free running counter).\r
17 **     Settings  :\r
18 **         Timer name                  : ECT (16-bit)\r
19 **         Compare name                : TC0\r
20 **         Counter shared              : No\r
21 **\r
22 **         High-speed CPU mode\r
23 **             Prescaler               : divide-by-8\r
24 **             Clock                   : 3124000 Hz\r
25 **           Initial period/frequency\r
26 **             Xtal ticks              : 16000\r
27 **             microseconds            : 1000\r
28 **             milliseconds            : 1\r
29 **             seconds (real)          : 0.0010000\r
30 **             Hz                      : 1000\r
31 **             kHz                     : 1\r
32 **\r
33 **         Runtime setting             : period/frequency interval (continual setting)\r
34 **             ticks                   : 16000 to 320000 ticks\r
35 **             microseconds            : 1000 to 20000 microseconds\r
36 **             milliseconds            : 1 to 20 milliseconds\r
37 **             seconds (real)          : 0.0010000 to 0.0200000 seconds\r
38 **             Hz                      : 50 to 1000 Hz\r
39 **\r
40 **         Initialization:\r
41 **              Timer                  : Enabled\r
42 **              Events                 : Enabled\r
43 **\r
44 **         Timer registers\r
45 **              Counter                : TCNT      [68]\r
46 **              Mode                   : TIOS      [64]\r
47 **              Run                    : TSCR1     [70]\r
48 **              Prescaler              : TSCR2     [77]\r
49 **\r
50 **         Compare registers\r
51 **              Compare                : TC0       [80]\r
52 **\r
53 **         Flip-flop registers\r
54 **              Mode                   : TCTL2     [73]\r
55 **     Contents  :\r
56 **         Enable           - byte TickTimer_Enable(void);\r
57 **         SetPeriodTicks16 - byte TickTimer_SetPeriodTicks16(word Ticks);\r
58 **         SetPeriodTicks32 - byte TickTimer_SetPeriodTicks32(dword Ticks);\r
59 **         SetPeriodUS      - byte TickTimer_SetPeriodUS(word Time);\r
60 **         SetPeriodMS      - byte TickTimer_SetPeriodMS(word Time);\r
61 **         SetFreqHz        - byte TickTimer_SetFreqHz(word Freq);\r
62 **\r
63 **     (c) Copyright UNIS, spol. s r.o. 1997-2002\r
64 **     UNIS, spol. s r.o.\r
65 **     Jundrovska 33\r
66 **     624 00 Brno\r
67 **     Czech Republic\r
68 **     http      : www.processorexpert.com\r
69 **     mail      : info@processorexpert.com\r
70 ** ###################################################################*/\r
71 \r
72 \r
73 /* MODULE TickTimer. */\r
74 \r
75 #include "Events.h"\r
76 #include "TickTimer.h"\r
77 \r
78 /* Definition of DATA and CODE segments for this bean. User can specify where\r
79    these segments will be located on "Build options" tab of the selected CPU bean. */\r
80 #pragma DATA_SEG TickTimer_DATA        /* Data section for this module. */\r
81 #pragma CODE_SEG TickTimer_CODE        /* Code section for this module. */\r
82 \r
83 static word CmpHighVal;                /* Compare register value for high speed CPU mode */\r
84 \r
85 \r
86 /*\r
87 ** ===================================================================\r
88 **     Method      :  SetCV (bean TimerInt)\r
89 **\r
90 **     Description :\r
91 **         This method is internal. It is used by Processor Expert\r
92 **         only.\r
93 ** ===================================================================\r
94 */\r
95 static void SetCV(word Val)\r
96 {\r
97   if (Val == 0)                        /* If the given value is zero */\r
98     Val = 65535;                       /* then change it to the maximal one */\r
99   TC0 = Val;                           /* Store given value to the compare register */\r
100   TC7 = Val;                           /* Store given value to the modulo register */\r
101 }\r
102 \r
103 /*\r
104 ** ===================================================================\r
105 **     Method      :  SetPV (bean TimerInt)\r
106 **\r
107 **     Description :\r
108 **         This method is internal. It is used by Processor Expert\r
109 **         only.\r
110 ** ===================================================================\r
111 */\r
112 static void SetPV(byte Val)\r
113 {\r
114   TSCR2_PR = Val;                      /* Store given value to the prescaler */\r
115 }\r
116 \r
117 /*\r
118 ** ===================================================================\r
119 **     Method      :  HWEnDi (bean TimerInt)\r
120 **\r
121 **     Description :\r
122 **         This method is internal. It is used by Processor Expert\r
123 **         only.\r
124 ** ===================================================================\r
125 */\r
126 static void HWEnDi(void)\r
127 {\r
128     TFLG1 = 1;                         /* Reset interrupt request flag */\r
129     TIE_C0I = 1;                       /* Enable interrupt */\r
130 }\r
131 \r
132 /*\r
133 ** ===================================================================\r
134 **     Method      :  TickTimer_Enable (bean TimerInt)\r
135 **\r
136 **     Description :\r
137 **         Enable the bean - it starts the timer. Events may be\r
138 **         generated ("DisableEvent"/"EnableEvent").\r
139 **     Parameters  : None\r
140 **     Returns     :\r
141 **         ---             - Error code, possible codes:\r
142 **                           ERR_OK - OK\r
143 **                           ERR_SPEED - This device does not work in\r
144 **                           the active speed mode\r
145 ** ===================================================================\r
146 */\r
147 byte TickTimer_Enable(void)\r
148 {\r
149   HWEnDi();                            /* Enable the device */\r
150   return ERR_OK;                       /* OK */\r
151 }\r
152 \r
153 /*\r
154 ** ===================================================================\r
155 **     Method      :  TickTimer_SetPeriodTicks16 (bean TimerInt)\r
156 **\r
157 **     Description :\r
158 **         This method sets the new period of the generated events.\r
159 **         The period is expressed in Xtal ticks as a 16-bit unsigned\r
160 **         integer number.\r
161 **         This method is available only if runtime setting type\r
162 **         'from interval' is selected in the Timing dialog box in\r
163 **         Runtime setting area.\r
164 **     Parameters  :\r
165 **         NAME            - DESCRIPTION\r
166 **         Ticks           - Period to set [in Xtal ticks]\r
167 **                      (16000 to 65535 ticks)\r
168 **     Returns     :\r
169 **         ---             - Error code, possible codes:\r
170 **                           ERR_OK - OK\r
171 **                           ERR_SPEED - This device does not work in\r
172 **                           the active speed mode\r
173 **                           ERR_MATH - Overflow during evaluation\r
174 **                           ERR_RANGE - Parameter out of range\r
175 ** ===================================================================\r
176 */\r
177 byte TickTimer_SetPeriodTicks16(word Ticks)\r
178 {\r
179   dlong rtval;                         /* Result of two 32-bit numbers multiplication */\r
180   word rtword;                         /* Result of 64-bit number division */\r
181 \r
182   if (Ticks < 16000)                   /* Is the given value out of range? */\r
183     return ERR_RANGE;                  /* If yes then error */\r
184   PE_Timer_LngMul((dword)Ticks,838592365,&rtval); /* Multiply given value and high speed CPU mode coefficient */\r
185   if (PE_Timer_LngHi4(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */\r
186     rtword = 65535;                    /* If yes then use maximal possible value */\r
187   CmpHighVal = rtword;                 /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */\r
188   SetCV(CmpHighVal);                   /* Store appropriate value to the compare register according to the selected high speed CPU mode */\r
189   return ERR_OK;                       /* OK */\r
190 }\r
191 \r
192 /*\r
193 ** ===================================================================\r
194 **     Method      :  TickTimer_SetPeriodTicks32 (bean TimerInt)\r
195 **\r
196 **     Description :\r
197 **         This method sets the new period of the generated events.\r
198 **         The period is expressed in Xtal ticks as a 32-bit unsigned\r
199 **         integer number.\r
200 **         This method is available only if runtime setting type\r
201 **         'from interval' is selected in the Timing dialog box in\r
202 **         Runtime setting area.\r
203 **     Parameters  :\r
204 **         NAME            - DESCRIPTION\r
205 **         Ticks           - Period to set [in Xtal ticks]\r
206 **                      (16000 to 320000 ticks)\r
207 **     Returns     :\r
208 **         ---             - Error code, possible codes:\r
209 **                           ERR_OK - OK\r
210 **                           ERR_SPEED - This device does not work in\r
211 **                           the active speed mode\r
212 **                           ERR_MATH - Overflow during evaluation\r
213 **                           ERR_RANGE - Parameter out of range\r
214 ** ===================================================================\r
215 */\r
216 byte TickTimer_SetPeriodTicks32(dword Ticks)\r
217 {\r
218   dlong rtval;                         /* Result of two 32-bit numbers multiplication */\r
219   word rtword;                         /* Result of 64-bit number division */\r
220 \r
221   if ((Ticks > 320000) || (Ticks < 16000)) /* Is the given value out of range? */\r
222     return ERR_RANGE;                  /* Range error */\r
223   PE_Timer_LngMul(Ticks,838592365,&rtval); /* Multiply given value and high speed CPU mode coefficient */\r
224   if (PE_Timer_LngHi4(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */\r
225     rtword = 65535;                    /* If yes then use maximal possible value */\r
226   CmpHighVal = rtword;                 /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */\r
227   SetCV(CmpHighVal);                   /* Store appropriate value to the compare register according to the selected high speed CPU mode */\r
228   return ERR_OK;                       /* OK */\r
229 }\r
230 \r
231 /*\r
232 ** ===================================================================\r
233 **     Method      :  TickTimer_SetPeriodUS (bean TimerInt)\r
234 **\r
235 **     Description :\r
236 **         This method sets the new period of the generated events.\r
237 **         The period is expressed in microseconds as a 16-bit\r
238 **         unsigned integer number.\r
239 **         This method is available only if runtime setting type\r
240 **         'from interval' is selected in the Timing dialog box in\r
241 **         Runtime setting area.\r
242 **     Parameters  :\r
243 **         NAME            - DESCRIPTION\r
244 **         Time            - Period to set [in microseconds]\r
245 **                      (1000 to 20000 microseconds)\r
246 **     Returns     :\r
247 **         ---             - Error code, possible codes:\r
248 **                           ERR_OK - OK\r
249 **                           ERR_SPEED - This device does not work in\r
250 **                           the active speed mode\r
251 **                           ERR_MATH - Overflow during evaluation\r
252 **                           ERR_RANGE - Parameter out of range\r
253 ** ===================================================================\r
254 */\r
255 byte TickTimer_SetPeriodUS(word Time)\r
256 {\r
257   dlong rtval;                         /* Result of two 32-bit numbers multiplication */\r
258   word rtword;                         /* Result of 64-bit number division */\r
259 \r
260   if ((Time > 20000) || (Time < 1000)) /* Is the given value out of range? */\r
261     return ERR_RANGE;                  /* If yes then error */\r
262   PE_Timer_LngMul((dword)Time,52412023,&rtval); /* Multiply given value and high speed CPU mode coefficient */\r
263   if (PE_Timer_LngHi3(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */\r
264     rtword = 65535;                    /* If yes then use maximal possible value */\r
265   CmpHighVal = rtword;                 /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */\r
266   SetCV(CmpHighVal);                   /* Store appropriate value to the compare register according to the selected high speed CPU mode */\r
267   return ERR_OK;                       /* OK */\r
268 }\r
269 \r
270 /*\r
271 ** ===================================================================\r
272 **     Method      :  TickTimer_SetPeriodMS (bean TimerInt)\r
273 **\r
274 **     Description :\r
275 **         This method sets the new period of the generated events.\r
276 **         The period is expressed in miliseconds as a 16-bit\r
277 **         unsigned integer number.\r
278 **         This method is available only if runtime setting type\r
279 **         'from interval' is selected in the Timing dialog box in\r
280 **         Runtime setting area.\r
281 **     Parameters  :\r
282 **         NAME            - DESCRIPTION\r
283 **         Time            - Period to set [in miliseconds]\r
284 **                      (1 to 20 milliseconds)\r
285 **     Returns     :\r
286 **         ---             - Error code, possible codes:\r
287 **                           ERR_OK - OK\r
288 **                           ERR_SPEED - This device does not work in\r
289 **                           the active speed mode\r
290 **                           ERR_MATH - Overflow during evaluation\r
291 **                           ERR_RANGE - Parameter out of range\r
292 ** ===================================================================\r
293 */\r
294 byte TickTimer_SetPeriodMS(word Time)\r
295 {\r
296   dlong rtval;                         /* Result of two 32-bit numbers multiplication */\r
297   word rtword;                         /* Result of 64-bit number division */\r
298 \r
299   if ((Time > 20) || (Time < 1))       /* Is the given value out of range? */\r
300     return ERR_RANGE;                  /* If yes then error */\r
301   PE_Timer_LngMul((dword)Time,204734464,&rtval); /* Multiply given value and high speed CPU mode coefficient */\r
302   if (PE_Timer_LngHi2(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */\r
303     rtword = 65535;                    /* If yes then use maximal possible value */\r
304   CmpHighVal = rtword;                 /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */\r
305   SetCV(CmpHighVal);                   /* Store appropriate value to the compare register according to the selected high speed CPU mode */\r
306   return ERR_OK;                       /* OK */\r
307 }\r
308 \r
309 /*\r
310 ** ===================================================================\r
311 **     Method      :  TickTimer_SetFreqHz (bean TimerInt)\r
312 **\r
313 **     Description :\r
314 **         This method sets the new frequency of the generated\r
315 **         events. The frequency is expressed in Hz as a 16-bit\r
316 **         unsigned integer number.\r
317 **         This method is available only if runtime setting type\r
318 **         'from interval' is selected in the Timing dialog box in\r
319 **         Runtime setting area.\r
320 **     Parameters  :\r
321 **         NAME            - DESCRIPTION\r
322 **         Freq            - Frequency to set [in Hz]\r
323 **                      (50 to 1000 Hz)\r
324 **     Returns     :\r
325 **         ---             - Error code, possible codes:\r
326 **                           ERR_OK - OK\r
327 **                           ERR_SPEED - This device does not work in\r
328 **                           the active speed mode\r
329 **                           ERR_MATH - Overflow during evaluation\r
330 **                           ERR_RANGE - Parameter out of range\r
331 ** ===================================================================\r
332 */\r
333 byte TickTimer_SetFreqHz(word Freq)\r
334 {\r
335   dlong rtval;                         /* Result of two 32-bit numbers division */\r
336   word rtword;                         /* Result of 64-bit number division */\r
337 \r
338   if ((Freq > 1000) || (Freq < 50))    /* Is the given value out of range? */\r
339     return ERR_RANGE;                  /* If yes then error */\r
340   rtval[1] = 799744000 / (dword)Freq;  /* Divide high speed CPU mode coefficient by the given value */\r
341   rtval[0] = 0;                        /* Convert result to the type dlong */\r
342   if (PE_Timer_LngHi1(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */\r
343     rtword = 65535;                    /* If yes then use maximal possible value */\r
344   CmpHighVal = rtword;                 /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */\r
345   SetCV(CmpHighVal);                   /* Store appropriate value to the compare register according to the selected high speed CPU mode */\r
346   return ERR_OK;                       /* OK */\r
347 }\r
348 \r
349 /*\r
350 ** ===================================================================\r
351 **     Method      :  TickTimer_Init (bean TimerInt)\r
352 **\r
353 **     Description :\r
354 **         This method is internal. It is used by Processor Expert\r
355 **         only.\r
356 ** ===================================================================\r
357 */\r
358 void TickTimer_Init(void)\r
359 {\r
360   CmpHighVal = 3124;                   /* Compare register value for high speed CPU mode */\r
361   SetCV(CmpHighVal);                   /* Store appropriate value to the compare register according to the selected high speed CPU mode */\r
362   SetPV(3);                            /* Set prescaler register according to the selected high speed CPU mode */\r
363   HWEnDi();                            /* Enable/disable device according to status flags */\r
364 }\r
365 \r
366 /*\r
367 ** ===================================================================\r
368 **     Method      :  TickTimer_Interrupt (bean TimerInt)\r
369 **\r
370 **     Description :\r
371 **         This method is internal. It is used by Processor Expert\r
372 **         only.\r
373 ** ===================================================================\r
374 */\r
375 #pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module. Placement will be in NON_BANKED area. */\r
376 __interrupt void TickTimer_Interrupt(void)\r
377 {\r
378   TFLG1 = 1;                           /* Reset interrupt request flag */\r
379   TickTimer_OnInterrupt();             /* Invoke user event */\r
380 }\r
381 \r
382 #pragma CODE_SEG TickTimer_CODE        /* Code section for this module. */\r
383 \r
384 /* END TickTimer. */\r
385 \r
386 /*\r
387 ** ###################################################################\r
388 **\r
389 **     This file was created by UNIS Processor Expert 03.33 for \r
390 **     the Motorola HCS12 series of microcontrollers.\r
391 **\r
392 ** ###################################################################\r
393 */\r