]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/sys_tick.c
Merge with default.
[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 #if defined(CFG_MPC5606S)\r
32         Irq_AttachIsr2(tid, NULL, RTC_INT);  /* Attach ISR2 to RTC interrupt */\r
33 #else\r
34         Irq_AttachIsr2(tid, NULL, 7);  /* Attach ISR2 to INTC_SSCIR0_CLR7  */\r
35 #endif\r
36 }\r
37 \r
38 /**\r
39  *\r
40  *\r
41  * @param period_ticks How long the period in timer ticks should be. The timer\r
42  *                     on PowerPC often driver by the CPU clock or some platform clock.\r
43  *\r
44  */\r
45 void Os_SysTickStart(uint32_t period_ticks) {\r
46 #if defined(CFG_MPC5606S)\r
47         CGM.SXOSC_CTL.B.OSCON = 1;      // enable the osc for RTC\r
48         RTC.RTCC.B.CNTEN = 0;           // disable RTC\r
49         RTC.RTCC.B.APIEN = 0;           // disable API\r
50         RTC.RTCS.B.RTCF = 1;            // clear RTC interrupt flag\r
51         RTC.RTCS.B.APIF = 1;            // clear API interrupt flag\r
52         RTC.RTCC.B.RTCIE = 1;           // enable RTC interrupt\r
53         RTC.RTCC.B.APIIE = 0;           // disable API interrupt\r
54         RTC.RTCC.B.FRZEN = 1;           // enable freeze mode\r
55         RTC.RTCC.B.CLKSEL = 2;          // set 16MHz FIRC as input clock\r
56         // ignore period_ticks, and set RTC compare value\r
57         RTC.RTCC.B.RTCVAL = ( ( (uint32_t)(16000000/OsTickFreq) ) >> 10);\r
58         (void)period_ticks;\r
59 \r
60         RTC.RTCC.B.CNTEN = 1;           // start RTC\r
61 #else\r
62 \r
63         // Enable the TB\r
64                 uint32_t tmp;\r
65         tmp = get_spr(SPR_HID0);\r
66         tmp |= HID0_TBEN;\r
67         set_spr(SPR_HID0, tmp);\r
68 \r
69         /* Initialize the Decrementer */\r
70         set_spr(SPR_DEC, period_ticks);\r
71         set_spr(SPR_DECAR, period_ticks);\r
72 \r
73         /* Set autoreload */\r
74         tmp = get_spr(SPR_TCR);\r
75         tmp |= TCR_ARE;\r
76         set_spr(SPR_TCR, tmp);\r
77 \r
78         /* Enable notification */\r
79         tmp = get_spr(SPR_TCR);\r
80         tmp |= TCR_DIE;\r
81         set_spr(SPR_TCR, tmp );\r
82 #endif\r
83 }\r
84 \r
85 /**\r
86  * ???\r
87  * TODO: This function just subtract the max value?! ok??\r
88  *\r
89  * @return\r
90  */\r
91 \r
92 /** @req OS383 */\r
93 TickType Os_SysTickGetValue( void )\r
94 {\r
95         uint32_t timer = get_spr(SPR_DECAR) - get_spr(SPR_DEC);\r
96         return (timer);\r
97 }
98
99 TickType Os_SysTickGetElapsedValue( uint32_t preValue ) {
100         uint32_t curr;
101         uint32_t max;
102
103         curr = get_spr(SPR_DEC);
104         max  = get_spr(SPR_DECAR);
105         return Os_CounterDiff((max - curr),preValue,max);
106 }
107 \r