Tick.c 4.0 KB

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