]> git.sur5r.net Git - freertos/blob - Source/portable/WizC/PIC18/Drivers/Tick/Tick.c
Updated to V4.0.5
[freertos] / Source / portable / WizC / PIC18 / Drivers / Tick / Tick.c
1 /*\r
2         FreeRTOS.org V4.0.5 - Copyright (C) 2003-2006 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /* \r
34 Changes from V3.0.0\r
35         + ISRcode is pulled inline and portTICKisr() is therefore\r
36           deleted from this file.\r
37 \r
38         + Prescaler logic for Timer1 added to allow for a wider\r
39           range of TickRates.\r
40 \r
41 Changes from V3.0.1\r
42 */\r
43 \r
44 #include <FreeRTOS.h>\r
45 #include <task.h>\r
46 \r
47 /* IO port constants. */\r
48 #define portBIT_SET             (1)\r
49 #define portBIT_CLEAR   (0)\r
50 \r
51 /* \r
52  * Hardware setup for the tick.\r
53  * We use a compare match on timer1. Depending on MPU-frequency\r
54  * and requested tickrate, a prescaled value with a matching\r
55  * prescaler are determined.\r
56  */\r
57 #define portTIMER_COMPARE_BASE                  ((APROCFREQ/4)/configTICK_RATE_HZ)\r
58 \r
59 #if portTIMER_COMPARE_BASE   < 0x10000\r
60         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE)\r
61         #define portTIMER_COMPARE_PS1           (portBIT_CLEAR)\r
62         #define portTIMER_COMPARE_PS0           (portBIT_CLEAR)\r
63 #elif portTIMER_COMPARE_BASE < 0x20000\r
64         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 2)\r
65         #define portTIMER_COMPARE_PS1           (portBIT_CLEAR)\r
66         #define portTIMER_COMPARE_PS0           (portBIT_SET)\r
67 #elif portTIMER_COMPARE_BASE < 0x40000\r
68         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 4)\r
69         #define portTIMER_COMPARE_PS1           (portBIT_SET)\r
70         #define portTIMER_COMPARE_PS0           (portBIT_CLEAR)\r
71 #elif portTIMER_COMPARE_BASE < 0x80000\r
72         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 8)\r
73         #define portTIMER_COMPARE_PS1           (portBIT_SET)\r
74         #define portTIMER_COMPARE_PS0           (portBIT_SET)\r
75 #else\r
76         #error "TickRate out of range"\r
77 #endif\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /*\r
82  * Setup a timer for a regular tick.\r
83  */\r
84 void portSetupTick( void )\r
85 {\r
86         /*\r
87          * Interrupts are disabled when this function is called.\r
88          */\r
89 \r
90         /*\r
91          * Setup CCP1\r
92          * Provide the tick interrupt using a compare match on timer1.\r
93          */\r
94 \r
95         /*\r
96          * Set the compare match value.\r
97          */\r
98         CCPR1H = ( unsigned portCHAR ) ( ( portTIMER_COMPARE_VALUE >> 8 ) & 0xff );\r
99         CCPR1L = ( unsigned portCHAR )   ( portTIMER_COMPARE_VALUE & 0xff );\r
100 \r
101         /*\r
102          * Set Compare Special Event Trigger Mode\r
103          */\r
104         bCCP1M3         = portBIT_SET;\r
105         bCCP1M2         = portBIT_CLEAR;\r
106         bCCP1M1         = portBIT_SET;\r
107         bCCP1M0         = portBIT_SET;\r
108 \r
109         /*\r
110          * Enable CCP1 interrupt\r
111          */\r
112         bCCP1IE         = portBIT_SET;\r
113 \r
114         /*\r
115          * We are only going to use the global interrupt bit, so disable\r
116          * interruptpriorities and enable peripheral interrupts.\r
117          */\r
118         bIPEN           = portBIT_CLEAR;\r
119         bPEIE           = portBIT_SET;\r
120 \r
121         /*\r
122          * Set up timer1\r
123          * It will produce the system tick.\r
124          */\r
125 \r
126         /*\r
127          * Clear the time count\r
128          */\r
129         TMR1H = ( unsigned portCHAR ) 0x00;\r
130         TMR1L = ( unsigned portCHAR ) 0x00;\r
131 \r
132         /*\r
133          * Setup the timer\r
134          */\r
135         bRD16           = portBIT_SET;                          // 16-bit\r
136         bT1CKPS1        = portTIMER_COMPARE_PS1;        // prescaler\r
137         bT1CKPS0        = portTIMER_COMPARE_PS0;        // prescaler\r
138         bT1OSCEN        = portBIT_SET;                          // Oscillator enable\r
139         bT1SYNC         = portBIT_SET;                          // No external clock sync\r
140         bTMR1CS         = portBIT_CLEAR;                        // Internal clock\r
141         \r
142         bTMR1ON         = portBIT_SET;                          // Start timer1\r
143 }\r