F2837xD_CpuTimers.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Included Files
  44. //
  45. #include "F2837xD_device.h"
  46. #include "F2837xD_Examples.h"
  47. //
  48. // Globals
  49. //
  50. struct CPUTIMER_VARS CpuTimer0;
  51. struct CPUTIMER_VARS CpuTimer1;
  52. struct CPUTIMER_VARS CpuTimer2;
  53. //
  54. // InitCpuTimers - This function initializes all three CPU timers to a known
  55. // state.
  56. //
  57. void InitCpuTimers(void)
  58. {
  59. //
  60. // CPU Timer 0
  61. // Initialize address pointers to respective timer registers:
  62. //
  63. CpuTimer0.RegsAddr = &CpuTimer0Regs;
  64. //
  65. // Initialize timer period to maximum:
  66. //
  67. CpuTimer0Regs.PRD.all = 0xFFFFFFFF;
  68. //
  69. // Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
  70. //
  71. CpuTimer0Regs.TPR.all = 0;
  72. CpuTimer0Regs.TPRH.all = 0;
  73. //
  74. // Make sure timer is stopped:
  75. //
  76. CpuTimer0Regs.TCR.bit.TSS = 1;
  77. //
  78. // Reload all counter register with period value:
  79. //
  80. CpuTimer0Regs.TCR.bit.TRB = 1;
  81. //
  82. // Reset interrupt counters:
  83. //
  84. CpuTimer0.InterruptCount = 0;
  85. //
  86. // Initialize address pointers to respective timer registers:
  87. //
  88. CpuTimer1.RegsAddr = &CpuTimer1Regs;
  89. CpuTimer2.RegsAddr = &CpuTimer2Regs;
  90. //
  91. // Initialize timer period to maximum:
  92. //
  93. CpuTimer1Regs.PRD.all = 0xFFFFFFFF;
  94. CpuTimer2Regs.PRD.all = 0xFFFFFFFF;
  95. //
  96. // Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
  97. //
  98. CpuTimer1Regs.TPR.all = 0;
  99. CpuTimer1Regs.TPRH.all = 0;
  100. CpuTimer2Regs.TPR.all = 0;
  101. CpuTimer2Regs.TPRH.all = 0;
  102. //
  103. // Make sure timers are stopped:
  104. //
  105. CpuTimer1Regs.TCR.bit.TSS = 1;
  106. CpuTimer2Regs.TCR.bit.TSS = 1;
  107. //
  108. // Reload all counter register with period value:
  109. //
  110. CpuTimer1Regs.TCR.bit.TRB = 1;
  111. CpuTimer2Regs.TCR.bit.TRB = 1;
  112. //
  113. // Reset interrupt counters:
  114. //
  115. CpuTimer1.InterruptCount = 0;
  116. CpuTimer2.InterruptCount = 0;
  117. }
  118. //
  119. // ConfigCpuTimer - This function initializes the selected timer to the period
  120. // specified by the "Freq" and "Period" parameters. The "Freq"
  121. // is entered as "MHz" and the period in "uSeconds". The timer
  122. // is held in the stopped state after configuration.
  123. //
  124. void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
  125. {
  126. Uint32 temp;
  127. //
  128. // Initialize timer period:
  129. //
  130. Timer->CPUFreqInMHz = Freq;
  131. Timer->PeriodInUSec = Period;
  132. temp = (long) (Freq * Period);
  133. //
  134. // Counter decrements PRD+1 times each period
  135. //
  136. Timer->RegsAddr->PRD.all = temp - 1;
  137. //
  138. // Set pre-scale counter to divide by 1 (SYSCLKOUT):
  139. //
  140. Timer->RegsAddr->TPR.all = 0;
  141. Timer->RegsAddr->TPRH.all = 0;
  142. //
  143. // Initialize timer control register:
  144. //
  145. Timer->RegsAddr->TCR.bit.TSS = 1; // 1 = Stop timer, 0 = Start/Restart
  146. // Timer
  147. Timer->RegsAddr->TCR.bit.TRB = 1; // 1 = reload timer
  148. Timer->RegsAddr->TCR.bit.SOFT = 0;
  149. Timer->RegsAddr->TCR.bit.FREE = 0; // Timer Free Run Disabled
  150. Timer->RegsAddr->TCR.bit.TIE = 1; // 0 = Disable/ 1 = Enable Timer
  151. // Interrupt
  152. //
  153. // Reset interrupt counter:
  154. //
  155. Timer->InterruptCount = 0;
  156. }
  157. //
  158. // End of file
  159. //