1 //###########################################################################
2 //
3 // FILE:    F2837xD_CpuTimers.c
4 //
5 // TITLE:   CPU 32-bit Timers Initialization & Support Functions.
6 //
7 //###########################################################################
8 // $TI Release: F2837xD Support Library v3.05.00.00 $
9 // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
10 // $Copyright:
11 // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
12 //
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions
15 // are met:
16 //
17 //   Redistributions of source code must retain the above copyright
18 //   notice, this list of conditions and the following disclaimer.
19 //
20 //   Redistributions in binary form must reproduce the above copyright
21 //   notice, this list of conditions and the following disclaimer in the
22 //   documentation and/or other materials provided with the
23 //   distribution.
24 //
25 //   Neither the name of Texas Instruments Incorporated nor the names of
26 //   its contributors may be used to endorse or promote products derived
27 //   from this software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 // $
41 //###########################################################################
42 
43 //
44 // Included Files
45 //
46 #include "F2837xD_device.h"
47 #include "F2837xD_Examples.h"
48 
49 //
50 // Globals
51 //
52 struct CPUTIMER_VARS CpuTimer0;
53 struct CPUTIMER_VARS CpuTimer1;
54 struct CPUTIMER_VARS CpuTimer2;
55 
56 //
57 // InitCpuTimers - This function initializes all three CPU timers to a known
58 //                 state.
59 //
InitCpuTimers(void)60 void InitCpuTimers(void)
61 {
62     //
63     // CPU Timer 0
64     // Initialize address pointers to respective timer registers:
65     //
66     CpuTimer0.RegsAddr = &CpuTimer0Regs;
67 
68     //
69     // Initialize timer period to maximum:
70     //
71     CpuTimer0Regs.PRD.all  = 0xFFFFFFFF;
72 
73     //
74     // Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
75     //
76     CpuTimer0Regs.TPR.all  = 0;
77     CpuTimer0Regs.TPRH.all = 0;
78 
79     //
80     // Make sure timer is stopped:
81     //
82     CpuTimer0Regs.TCR.bit.TSS = 1;
83 
84     //
85     // Reload all counter register with period value:
86     //
87     CpuTimer0Regs.TCR.bit.TRB = 1;
88 
89     //
90     // Reset interrupt counters:
91     //
92     CpuTimer0.InterruptCount = 0;
93 
94     //
95     // Initialize address pointers to respective timer registers:
96     //
97     CpuTimer1.RegsAddr = &CpuTimer1Regs;
98     CpuTimer2.RegsAddr = &CpuTimer2Regs;
99 
100     //
101     // Initialize timer period to maximum:
102     //
103     CpuTimer1Regs.PRD.all  = 0xFFFFFFFF;
104     CpuTimer2Regs.PRD.all  = 0xFFFFFFFF;
105 
106     //
107     // Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
108     //
109     CpuTimer1Regs.TPR.all  = 0;
110     CpuTimer1Regs.TPRH.all = 0;
111     CpuTimer2Regs.TPR.all  = 0;
112     CpuTimer2Regs.TPRH.all = 0;
113 
114     //
115     // Make sure timers are stopped:
116     //
117     CpuTimer1Regs.TCR.bit.TSS = 1;
118     CpuTimer2Regs.TCR.bit.TSS = 1;
119 
120     //
121     // Reload all counter register with period value:
122     //
123     CpuTimer1Regs.TCR.bit.TRB = 1;
124     CpuTimer2Regs.TCR.bit.TRB = 1;
125 
126     //
127     // Reset interrupt counters:
128     //
129     CpuTimer1.InterruptCount = 0;
130     CpuTimer2.InterruptCount = 0;
131 }
132 
133 //
134 // ConfigCpuTimer - This function initializes the selected timer to the period
135 //                  specified by the "Freq" and "Period" parameters. The "Freq"
136 //                  is entered as "MHz" and the period in "uSeconds". The timer
137 //                  is held in the stopped state after configuration.
138 //
ConfigCpuTimer(struct CPUTIMER_VARS * Timer,float Freq,float Period)139 void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
140 {
141     Uint32 temp;
142 
143     //
144     // Initialize timer period:
145     //
146     Timer->CPUFreqInMHz = Freq;
147     Timer->PeriodInUSec = Period;
148     temp = (long) (Freq * Period);
149 
150     //
151     // Counter decrements PRD+1 times each period
152     //
153     Timer->RegsAddr->PRD.all = temp - 1;
154 
155     //
156     // Set pre-scale counter to divide by 1 (SYSCLKOUT):
157     //
158     Timer->RegsAddr->TPR.all  = 0;
159     Timer->RegsAddr->TPRH.all  = 0;
160 
161     //
162     // Initialize timer control register:
163     //
164     Timer->RegsAddr->TCR.bit.TSS = 1;     // 1 = Stop timer, 0 = Start/Restart
165                                           // Timer
166     Timer->RegsAddr->TCR.bit.TRB = 1;     // 1 = reload timer
167     Timer->RegsAddr->TCR.bit.SOFT = 0;
168     Timer->RegsAddr->TCR.bit.FREE = 0;    // Timer Free Run Disabled
169     Timer->RegsAddr->TCR.bit.TIE = 1;     // 0 = Disable/ 1 = Enable Timer
170                                           // Interrupt
171 
172     //
173     // Reset interrupt counter:
174     //
175     Timer->InterruptCount = 0;
176 }
177 
178 
179 //
180 // End of file
181 //
182