fw_tim.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "fw_tim.h"
  15. #include "fw_sys.h"
  16. #include "fw_util.h"
  17. /**
  18. * Calculate the initial value of Timer0 & Timer1 counter
  19. * - If the frequency is too high, it will return the value of `limit`, so the timer
  20. * will run in the highest frequency
  21. * - If the frequency is too low, it will return 0, so the timer will run in the
  22. * lowest possible frequency
  23. */
  24. int16_t TIM_Timer0n1_CalculateInitValue(uint16_t frequency, HAL_State_t freq1t, uint16_t limit)
  25. {
  26. uint32_t value = __SYSCLOCK;
  27. if (!freq1t)
  28. value = value / 12;
  29. value = value / frequency;
  30. if (value > limit)
  31. return 0;
  32. else
  33. return limit - value;
  34. }
  35. void TIM_Timer0_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency)
  36. {
  37. uint16_t init;
  38. TIM_Timer0_Set1TMode(freq1t);
  39. TIM_Timer0_SetMode(mode);
  40. if (mode == TIM_TimerMode_8BitAuto)
  41. {
  42. init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFF);
  43. TIM_Timer0_SetInitValue(init & 0xFF, init & 0xFF);
  44. }
  45. else
  46. {
  47. init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFFFF);
  48. TIM_Timer0_SetInitValue(init >> 8, init & 0xFF);
  49. }
  50. }
  51. void TIM_Timer1_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency)
  52. {
  53. uint16_t init;
  54. TIM_Timer1_Set1TMode(freq1t);
  55. TIM_Timer1_SetMode(mode);
  56. if (mode == TIM_TimerMode_8BitAuto)
  57. {
  58. init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFF);
  59. TIM_Timer1_SetInitValue(init & 0xFF, init & 0xFF);
  60. }
  61. else
  62. {
  63. init = TIM_Timer0n1_CalculateInitValue(frequency, freq1t, 0xFF);
  64. TIM_Timer1_SetInitValue(init >> 8, init & 0xFF);
  65. }
  66. }
  67. int16_t _TIM_Timer234_InitValueCalculate(
  68. uint16_t frequency, uint8_t prescaler, HAL_State_t freq1t)
  69. {
  70. uint32_t value = __SYSCLOCK;
  71. if (!freq1t)
  72. value = value / 12;
  73. value = value / ((prescaler + 1) * frequency);
  74. if (value > 0xFFFF)
  75. return 0;
  76. else
  77. return 0xFFFF - value;
  78. }
  79. /**
  80. * Timer2,3,4 Configuration
  81. *
  82. * 1. no interrupt priority
  83. * 2. frequency = SYSCLK / (TMxPS + 1) / (0xFFFF - TxH,TxL) / (1T? 1 : 12)
  84. */
  85. void TIM_Timer2_Config(HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency)
  86. {
  87. uint16_t init = _TIM_Timer234_InitValueCalculate(frequency, prescaler, freq1t);
  88. TIM_Timer2_Set1TMode(freq1t);
  89. TIM_Timer2_SetPreScaler(prescaler);
  90. TIM_Timer2_SetInitValue(init >> 8, init & 0xFF);
  91. }
  92. void TIM_Timer3_Config(
  93. HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency, HAL_State_t intState)
  94. {
  95. uint16_t init = _TIM_Timer234_InitValueCalculate(frequency, prescaler, freq1t);
  96. TIM_Timer3_Set1TMode(freq1t);
  97. TIM_Timer3_SetPreScaler(prescaler);
  98. TIM_Timer3_SetInitValue(init >> 8, init & 0xFF);
  99. EXTI_Timer3_SetIntState(intState);
  100. }
  101. void TIM_Timer4_Config(
  102. HAL_State_t freq1t, uint8_t prescaler, uint16_t frequency, HAL_State_t intState)
  103. {
  104. uint16_t init = _TIM_Timer234_InitValueCalculate(frequency, prescaler, freq1t);
  105. TIM_Timer4_Set1TMode(freq1t);
  106. TIM_Timer4_SetPreScaler(prescaler);
  107. TIM_Timer4_SetInitValue(init >> 8, init & 0xFF);
  108. EXTI_Timer4_SetIntState(intState);
  109. }