timer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* sd2iec - SD/MMC to Commodore serial bus interface/controller
  2. Copyright (C) 2007-2009 Ingo Korb <ingo@akana.de>
  3. Inspiration and low-level SD/MMC access based on code from MMC2IEC
  4. by Lars Pontoppidan et al., see sdcard.c|h and config.h.
  5. FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; version 2 of the License only.
  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. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. timer.h: System timer (and button-debouncer)
  17. */
  18. #ifndef TIMER_H
  19. #define TIMER_H
  20. #include <util/atomic.h>
  21. // Bit masks for the (simulated) keys
  22. #define KEY_NEXT (1<<0)
  23. #define KEY_PREV (1<<1)
  24. #define KEY_HOME (1<<2)
  25. #define KEY_SLEEP (1<<3)
  26. /* Remote display service request */
  27. #define KEY_DISPLAY (1<<4)
  28. #define IGNORE_KEYS (1<<7)
  29. /// Logical keys that were pressed - must be reset by the reader.
  30. extern volatile uint8_t active_keys;
  31. #define key_pressed(x) (active_keys & (x))
  32. #define reset_key(x) active_keys &= (uint8_t)~(x)
  33. #define ignore_keys() active_keys = IGNORE_KEYS;
  34. typedef uint16_t tick_t;
  35. /// Global timing variable, 100 ticks per second
  36. /// Use atomic access or getticks() !
  37. extern volatile tick_t ticks;
  38. #define HZ 100
  39. /**
  40. * getticks - return the current system tick count
  41. *
  42. * This inline function returns the current system tick count.
  43. */
  44. static inline tick_t getticks(void) {
  45. tick_t tmp;
  46. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  47. tmp = ticks;
  48. }
  49. return tmp;
  50. }
  51. #define MS_TO_TICKS(x) (x/10)
  52. /* Adapted from Linux 2.6 include/linux/jiffies.h:
  53. *
  54. * These inlines deal with timer wrapping correctly. You are
  55. * strongly encouraged to use them
  56. * 1. Because people otherwise forget
  57. * 2. Because if the timer wrap changes in future you won't have to
  58. * alter your driver code.
  59. *
  60. * time_after(a,b) returns true if the time a is after time b.
  61. *
  62. * Do this with "<0" and ">=0" to only test the sign of the result. A
  63. * good compiler would generate better code (and a really good compiler
  64. * wouldn't care). Gcc is currently neither.
  65. * (">=0" refers to the time_after_eq macro which wasn't copied)
  66. */
  67. #define time_after(a,b) \
  68. ((int16_t)(b) - (int16_t)(a) < 0)
  69. #define time_before(a,b) time_after(b,a)
  70. /// Calculate timer start value for given timeout in microseconds
  71. #define TIMEOUT_US(x) (256-((float)F_CPU/10000000.0)*x)
  72. /**
  73. * start_timeout - start a timeout using timer0
  74. * @startval: starting value for timer
  75. *
  76. * This function sets timer 0 to the specified value and clears its overflow
  77. * flag. Use in conjunction with TIMEOUT_US to cause a timer overflow after
  78. * a specified number of microseconds. DON'T use a variable as a parameter to
  79. * the TIMEOUT_US macro because that would require run-time float calculations.
  80. */
  81. #define start_timeout(startval) do { TCNT0 = startval; TIFR0 |= _BV(TOV0); } while (0)
  82. /**
  83. * has_timed_out - returns true if timeout was reached
  84. *
  85. * This function returns true if the overflow flag of timer 0 is set which
  86. * (together with start_timeout and TIMEOUT_US) will happen when the
  87. * specified time has elapsed.
  88. */
  89. #define has_timed_out() (TIFR0 & _BV(TOV0))
  90. void timer_init(void);
  91. #endif