]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/sys_tick.c
8027488b9ddda39df8bbdf625d3bd2b5123af118
[arc.git] / arch / ppc / mpc55xx / drivers / sys_tick.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 #include "Os.h"\r
17 #include "internal.h"\r
18 #include "irq.h"\r
19 #include "arc.h"\r
20 #include "mpc55xx.h"\r
21 \r
22 /**\r
23  * Init of free running timer.\r
24  */\r
25 \r
26 extern void OsTick( void );\r
27 extern OsTickType OsTickFreq;\r
28 void Os_SysTickInit( void ) {\r
29         TaskType tid;\r
30         tid = Os_Arc_CreateIsr(OsTick, 6 /*prio*/, "OsTick");\r
31         Irq_AttachIsr2(tid, NULL, RTC_INT);  /* Attach ISR2 to RTC interrupt */\r
32 }\r
33 \r
34 /**\r
35  *\r
36  *\r
37  * @param period_ticks How long the period in timer ticks should be. The timer\r
38  *                     on PowerPC often driver by the CPU clock or some platform clock.\r
39  *\r
40  */\r
41 void Os_SysTickStart(uint32_t period_ticks) {\r
42 #if defined(CFG_MPC5606S)\r
43         CGM.SXOSC_CTL.B.OSCON = 1;      // enable the osc for RTC\r
44         RTC.RTCC.B.CNTEN = 0;           // disable RTC\r
45         RTC.RTCC.B.APIEN = 0;           // disable API\r
46         RTC.RTCS.B.RTCF = 1;            // clear RTC interrupt flag\r
47         RTC.RTCS.B.APIF = 1;            // clear API interrupt flag\r
48         RTC.RTCC.B.RTCIE = 1;           // enable RTC interrupt\r
49         RTC.RTCC.B.APIIE = 0;           // disable API interrupt\r
50         RTC.RTCC.B.FRZEN = 1;           // enable freeze mode\r
51         RTC.RTCC.B.CLKSEL = 2;          // set 16MHz FIRC as input clock\r
52         // ignore period_ticks, and set RTC compare value\r
53         RTC.RTCC.B.RTCVAL = ( ( (uint32_t)(16000000/OsTickFreq) ) >> 10);\r
54         (void)period_ticks;\r
55 \r
56         RTC.RTCC.B.CNTEN = 1;           // start RTC\r
57 #else\r
58 \r
59         // Enable the TB\r
60         tmp = get_spr(SPR_HID0);\r
61         tmp |= HID0_TBEN;\r
62         set_spr(SPR_HID0, tmp);\r
63 \r
64         /* Initialize the Decrementer */\r
65         set_spr(SPR_DEC, period_ticks);\r
66         set_spr(SPR_DECAR, period_ticks);\r
67 \r
68         /* Set autoreload */\r
69         tmp = get_spr(SPR_TCR);\r
70         tmp |= TCR_ARE;\r
71         set_spr(SPR_TCR, tmp);\r
72 \r
73         /* Enable notification */\r
74         tmp = get_spr(SPR_TCR);\r
75         tmp |= TCR_DIE;\r
76         set_spr(SPR_TCR, tmp );\r
77 #endif\r
78 }\r
79 \r
80 /**\r
81  * ???\r
82  * TODO: This function just subtract the max value?! ok??\r
83  *\r
84  * @return\r
85  */\r
86 \r
87 /** @req OS383 */\r
88 TickType Os_SysTickGetValue( void )\r
89 {\r
90         uint32_t timer = get_spr(SPR_DECAR) - get_spr(SPR_DEC);\r
91         return (timer);\r
92 }
93
94 TickType Os_SysTickGetElapsedValue( uint32_t preValue ) {
95         uint32_t curr;
96         uint32_t max;
97
98         curr = get_spr(SPR_DEC);
99         max  = get_spr(SPR_DECAR);
100         return Os_CounterDiff((max - curr),preValue,max);
101 }
102 \r