You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

116 lines
3.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 11/2014 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  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 General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/interrupt.h>
  20. #include <avr/io.h>
  21. #include "event.h"
  22. #include "target.h"
  23. #include "timer.h"
  24. /* *********************************************************************** */
  25. static uint16_t timers[TIMER_COUNT];
  26. volatile static uint8_t timer_ticked;
  27. /* *********************************************************************** */
  28. ISR(TIMER_TIM_VECT)
  29. {
  30. /* 1ms interrupt */
  31. TIMER_TIM_RELOAD(TIMER_RELOAD);
  32. timer_ticked = 1;
  33. } /* TIM1_OVF_vect */
  34. uint8_t timer_check(uint8_t timer_needed)
  35. {
  36. if (timer_ticked)
  37. {
  38. uint8_t i;
  39. timer_ticked = 0;
  40. for (i = 0; i < TIMER_COUNT; i++)
  41. {
  42. if (timers[i] > 0)
  43. {
  44. timers[i]--;
  45. if (timers[i] == 0)
  46. {
  47. event_queue(EVENT_TYPE_TIMER_ELAPSED, i, 0);
  48. }
  49. else
  50. {
  51. timer_needed = 1;
  52. }
  53. }
  54. }
  55. /* stop timer */
  56. if (timer_needed == 0)
  57. {
  58. TIMER_TIM_DISABLE();
  59. }
  60. return 1;
  61. }
  62. return 0;
  63. } /* timer_check */
  64. uint8_t timer_get_sleep_mode(void)
  65. {
  66. if (TIMER_TIM_RUNNING())
  67. {
  68. return SLEEP_MODE_IDLE;
  69. }
  70. else
  71. {
  72. return SLEEP_MODE_PWR_DOWN;
  73. }
  74. } /* timer_get_sleep_mode */
  75. void timer_event_handler(struct event_entry *event)
  76. {
  77. if ((event->type == EVENT_TYPE_TIMER_SET) &&
  78. (event->num < TIMER_COUNT)
  79. )
  80. {
  81. timers[event->num] = event->value;
  82. /* start timer if needed */
  83. if (!TIMER_TIM_RUNNING())
  84. {
  85. TIMER_TIM_RELOAD(TIMER_RELOAD);
  86. TIMER_TIM_ENABLE();
  87. }
  88. }
  89. } /* timer_event_handler */
  90. void timer_init(void)
  91. {
  92. TIMER_TIM_INIT();
  93. TIMER_TIM_RELOAD(TIMER_RELOAD);
  94. TIMER_TIM_ENABLE();
  95. } /* timer_init */