]> git.sur5r.net Git - freertos/blob - Source/portable/WizC/PIC18/Drivers/Tick/Tick.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Source / portable / WizC / PIC18 / Drivers / Tick / Tick.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /* \r
49 Changes from V3.0.0\r
50         + ISRcode is pulled inline and portTICKisr() is therefore\r
51           deleted from this file.\r
52 \r
53         + Prescaler logic for Timer1 added to allow for a wider\r
54           range of TickRates.\r
55 \r
56 Changes from V3.0.1\r
57 */\r
58 \r
59 #include <FreeRTOS.h>\r
60 #include <task.h>\r
61 \r
62 /* IO port constants. */\r
63 #define portBIT_SET             (1)\r
64 #define portBIT_CLEAR   (0)\r
65 \r
66 /* \r
67  * Hardware setup for the tick.\r
68  * We use a compare match on timer1. Depending on MPU-frequency\r
69  * and requested tickrate, a prescaled value with a matching\r
70  * prescaler are determined.\r
71  */\r
72 #define portTIMER_COMPARE_BASE                  ((APROCFREQ/4)/configTICK_RATE_HZ)\r
73 \r
74 #if portTIMER_COMPARE_BASE   < 0x10000\r
75         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE)\r
76         #define portTIMER_COMPARE_PS1           (portBIT_CLEAR)\r
77         #define portTIMER_COMPARE_PS0           (portBIT_CLEAR)\r
78 #elif portTIMER_COMPARE_BASE < 0x20000\r
79         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 2)\r
80         #define portTIMER_COMPARE_PS1           (portBIT_CLEAR)\r
81         #define portTIMER_COMPARE_PS0           (portBIT_SET)\r
82 #elif portTIMER_COMPARE_BASE < 0x40000\r
83         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 4)\r
84         #define portTIMER_COMPARE_PS1           (portBIT_SET)\r
85         #define portTIMER_COMPARE_PS0           (portBIT_CLEAR)\r
86 #elif portTIMER_COMPARE_BASE < 0x80000\r
87         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 8)\r
88         #define portTIMER_COMPARE_PS1           (portBIT_SET)\r
89         #define portTIMER_COMPARE_PS0           (portBIT_SET)\r
90 #else\r
91         #error "TickRate out of range"\r
92 #endif\r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /*\r
97  * Setup a timer for a regular tick.\r
98  */\r
99 void portSetupTick( void )\r
100 {\r
101         /*\r
102          * Interrupts are disabled when this function is called.\r
103          */\r
104 \r
105         /*\r
106          * Setup CCP1\r
107          * Provide the tick interrupt using a compare match on timer1.\r
108          */\r
109 \r
110         /*\r
111          * Set the compare match value.\r
112          */\r
113         CCPR1H = ( unsigned portCHAR ) ( ( portTIMER_COMPARE_VALUE >> 8 ) & 0xff );\r
114         CCPR1L = ( unsigned portCHAR )   ( portTIMER_COMPARE_VALUE & 0xff );\r
115 \r
116         /*\r
117          * Set Compare Special Event Trigger Mode\r
118          */\r
119         bCCP1M3         = portBIT_SET;\r
120         bCCP1M2         = portBIT_CLEAR;\r
121         bCCP1M1         = portBIT_SET;\r
122         bCCP1M0         = portBIT_SET;\r
123 \r
124         /*\r
125          * Enable CCP1 interrupt\r
126          */\r
127         bCCP1IE         = portBIT_SET;\r
128 \r
129         /*\r
130          * We are only going to use the global interrupt bit, so disable\r
131          * interruptpriorities and enable peripheral interrupts.\r
132          */\r
133         bIPEN           = portBIT_CLEAR;\r
134         bPEIE           = portBIT_SET;\r
135 \r
136         /*\r
137          * Set up timer1\r
138          * It will produce the system tick.\r
139          */\r
140 \r
141         /*\r
142          * Clear the time count\r
143          */\r
144         TMR1H = ( unsigned portCHAR ) 0x00;\r
145         TMR1L = ( unsigned portCHAR ) 0x00;\r
146 \r
147         /*\r
148          * Setup the timer\r
149          */\r
150         bRD16           = portBIT_SET;                          // 16-bit\r
151         bT1CKPS1        = portTIMER_COMPARE_PS1;        // prescaler\r
152         bT1CKPS0        = portTIMER_COMPARE_PS0;        // prescaler\r
153         bT1OSCEN        = portBIT_SET;                          // Oscillator enable\r
154         bT1SYNC         = portBIT_SET;                          // No external clock sync\r
155         bTMR1CS         = portBIT_CLEAR;                        // Internal clock\r
156         \r
157         bTMR1ON         = portBIT_SET;                          // Start timer1\r
158 }\r