timer.h 3.9 KB

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