]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/WizC/PIC18/Drivers/Tick/Tick.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / WizC / PIC18 / Drivers / Tick / Tick.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* \r
30 Changes from V3.0.0\r
31         + ISRcode is pulled inline and portTICKisr() is therefore\r
32           deleted from this file.\r
33 \r
34         + Prescaler logic for Timer1 added to allow for a wider\r
35           range of TickRates.\r
36 \r
37 Changes from V3.0.1\r
38 */\r
39 \r
40 #include <FreeRTOS.h>\r
41 #include <task.h>\r
42 \r
43 /* IO port constants. */\r
44 #define portBIT_SET             (1)\r
45 #define portBIT_CLEAR   (0)\r
46 \r
47 /* \r
48  * Hardware setup for the tick.\r
49  * We use a compare match on timer1. Depending on MPU-frequency\r
50  * and requested tickrate, a prescaled value with a matching\r
51  * prescaler are determined.\r
52  */\r
53 #define portTIMER_COMPARE_BASE                  ((APROCFREQ/4)/configTICK_RATE_HZ)\r
54 \r
55 #if portTIMER_COMPARE_BASE   < 0x10000\r
56         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE)\r
57         #define portTIMER_COMPARE_PS1           (portBIT_CLEAR)\r
58         #define portTIMER_COMPARE_PS0           (portBIT_CLEAR)\r
59 #elif portTIMER_COMPARE_BASE < 0x20000\r
60         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 2)\r
61         #define portTIMER_COMPARE_PS1           (portBIT_CLEAR)\r
62         #define portTIMER_COMPARE_PS0           (portBIT_SET)\r
63 #elif portTIMER_COMPARE_BASE < 0x40000\r
64         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 4)\r
65         #define portTIMER_COMPARE_PS1           (portBIT_SET)\r
66         #define portTIMER_COMPARE_PS0           (portBIT_CLEAR)\r
67 #elif portTIMER_COMPARE_BASE < 0x80000\r
68         #define portTIMER_COMPARE_VALUE         (portTIMER_COMPARE_BASE / 8)\r
69         #define portTIMER_COMPARE_PS1           (portBIT_SET)\r
70         #define portTIMER_COMPARE_PS0           (portBIT_SET)\r
71 #else\r
72         #error "TickRate out of range"\r
73 #endif\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /*\r
78  * Setup a timer for a regular tick.\r
79  */\r
80 void portSetupTick( void )\r
81 {\r
82         /*\r
83          * Interrupts are disabled when this function is called.\r
84          */\r
85 \r
86         /*\r
87          * Setup CCP1\r
88          * Provide the tick interrupt using a compare match on timer1.\r
89          */\r
90 \r
91         /*\r
92          * Set the compare match value.\r
93          */\r
94         CCPR1H = ( uint8_t ) ( ( portTIMER_COMPARE_VALUE >> 8 ) & 0xff );\r
95         CCPR1L = ( uint8_t )   ( portTIMER_COMPARE_VALUE & 0xff );\r
96 \r
97         /*\r
98          * Set Compare Special Event Trigger Mode\r
99          */\r
100         bCCP1M3         = portBIT_SET;\r
101         bCCP1M2         = portBIT_CLEAR;\r
102         bCCP1M1         = portBIT_SET;\r
103         bCCP1M0         = portBIT_SET;\r
104 \r
105         /*\r
106          * Enable CCP1 interrupt\r
107          */\r
108         bCCP1IE         = portBIT_SET;\r
109 \r
110         /*\r
111          * We are only going to use the global interrupt bit, so disable\r
112          * interruptpriorities and enable peripheral interrupts.\r
113          */\r
114         bIPEN           = portBIT_CLEAR;\r
115         bPEIE           = portBIT_SET;\r
116 \r
117         /*\r
118          * Set up timer1\r
119          * It will produce the system tick.\r
120          */\r
121 \r
122         /*\r
123          * Clear the time count\r
124          */\r
125         TMR1H = ( uint8_t ) 0x00;\r
126         TMR1L = ( uint8_t ) 0x00;\r
127 \r
128         /*\r
129          * Setup the timer\r
130          */\r
131         bRD16           = portBIT_SET;                          // 16-bit\r
132         bT1CKPS1        = portTIMER_COMPARE_PS1;        // prescaler\r
133         bT1CKPS0        = portTIMER_COMPARE_PS0;        // prescaler\r
134         bT1OSCEN        = portBIT_SET;                          // Oscillator enable\r
135         bT1SYNC         = portBIT_SET;                          // No external clock sync\r
136         bTMR1CS         = portBIT_CLEAR;                        // Internal clock\r
137         \r
138         bTMR1ON         = portBIT_SET;                          // Start timer1\r
139 }\r