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