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