timer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2023 Daniele Lacamera <root@danielinux.net>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * This is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2, as
  20. * published by the Free Software Foundation.
  21. *
  22. * frosted is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with frosted. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * Author: Daniele Lacamera
  31. *
  32. *
  33. */
  34. #include <stdint.h>
  35. #include "system.h"
  36. #include <unicore-mx/cm3/nvic.h>
  37. #include <unicore-mx/stm32/rcc.h>
  38. extern int AudioPulse;
  39. #define TIM2_BASE (0x40000000)
  40. #define TIM2_CR1 (*(volatile uint32_t *)(TIM2_BASE + 0x00))
  41. #define TIM2_DIER (*(volatile uint32_t *)(TIM2_BASE + 0x0c))
  42. #define TIM2_CNT (*(volatile uint32_t *)(TIM2_BASE + 0x24))
  43. #define TIM2_PSC (*(volatile uint32_t *)(TIM2_BASE + 0x28))
  44. #define TIM2_ARR (*(volatile uint32_t *)(TIM2_BASE + 0x2c))
  45. #define TIM_DIER_UIE (1 << 0)
  46. #define TIM_CR1_CLOCK_ENABLE (1 << 0)
  47. #define TIM_CR1_UPD_RS (1 << 2)
  48. #define TIM_CR1_EV_DISABLE (1 << 1)
  49. #define TIM2_SR (*(volatile uint32_t *)(TIM2_BASE + 0x10))
  50. #define TIM_SR_UIF (1 << 0)
  51. int timer_start(uint32_t clock, uint32_t prescaler, uint32_t interval_us)
  52. {
  53. uint32_t val = 0;
  54. uint32_t psc = 1;
  55. uint32_t err = 0;
  56. uint32_t reg = 0;
  57. clock = ((clock * prescaler) / 2000000) * interval_us;
  58. while (psc < 65535) {
  59. val = clock / psc;
  60. err = clock % psc;
  61. if ((val < 65535) && (err == 0)) {
  62. val--;
  63. break;
  64. }
  65. val = 0;
  66. psc++;
  67. }
  68. if (val == 0)
  69. return -1;
  70. nvic_irq_enable(NVIC_TIM2_IRQN);
  71. nvic_set_priority(NVIC_TIM2_IRQN, 3);
  72. APB1_CLOCK_RST |= TIM2_APB1_CLOCK_ER_VAL;
  73. DMB();
  74. APB1_CLOCK_RST &= ~TIM2_APB1_CLOCK_ER_VAL;
  75. APB1_CLOCK_ER |= TIM2_APB1_CLOCK_ER_VAL;
  76. TIM2_CR1 = 0;
  77. DMB();
  78. TIM2_PSC = psc;
  79. TIM2_ARR = val;
  80. TIM2_CNT = val - 1;
  81. reg = TIM2_CR1;
  82. reg &= ~TIM_CR1_EV_DISABLE;
  83. reg |= TIM_CR1_CLOCK_ENABLE;
  84. TIM2_CR1 = reg;
  85. TIM2_DIER |= TIM_DIER_UIE;
  86. DMB();
  87. return 0;
  88. }
  89. void timer_stop(void)
  90. {
  91. APB1_CLOCK_RST |= TIM2_APB1_CLOCK_ER_VAL;
  92. DMB();
  93. APB1_CLOCK_RST &= ~TIM2_APB1_CLOCK_ER_VAL;
  94. TIM2_CR1 |= TIM_CR1_EV_DISABLE;
  95. APB1_CLOCK_ER &= ~TIM2_APB1_CLOCK_ER_VAL;
  96. }
  97. void isr_tim2(void) {
  98. TIM2_SR &= ~TIM_SR_UIF;
  99. // AudioPulse++;
  100. }