Alternative library and kernel for add-in development on fx-9860G and fx-CG50 under Linux.
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.

155 lines
5.9KB

  1. //---
  2. // gint:timer - Timer operation
  3. //---
  4. #ifndef GINT_TIMER
  5. #define GINT_TIMER
  6. #include <gint/defs/types.h>
  7. #include <gint/mpu/tmu.h>
  8. #include <gint/hardware.h>
  9. /* Timer identifiers
  10. Hardware timers are numbered with integers starting from 0. You can freely
  11. access all the available timers by using their number once you have
  12. configured them with timer_setup(). The number of timers depends on the MPU:
  13. SH3-based: 4 timers, ids 0..3 [SH7355, SH7337]
  14. SH4-based: 9 timers, ids 0..8 [SH7305]
  15. You should be aware that some of these timers are used by default by gint:
  16. - Timer 0 is used by the gray engine on fx9860g.
  17. - Timer 3/8 is used by the keyboard on SH3/SH4.
  18. timer_setup() will fail if you try to use a timer that's already running.
  19. Always check the return value of timer_setup()! Using a timer id that has
  20. not been validated by timer_setup() will work, but do *something else* than
  21. what you intended. */
  22. /* timer_count() - tells how many timers are available on the platform */
  23. #define timer_count() (isSH3() ? 4 : 9)
  24. /* Clock input
  25. Timers count down when their input clock ticks, and fire when their counter
  26. reach 0. The choice of the input clock influences the resolution of the
  27. timer, but if the clock is too fast, the 32-bit counter might not be able to
  28. represent long delays.
  29. Several input clocks are available. The peripheral clock (Po) can be divided
  30. by 4, 16, 64 or 256; as an alternative the external clock TCLK can be used
  31. for counting. I suspect TCLK runs at a fixed frequency of 32768 Hz, but this
  32. has yet to be verified.
  33. You don't really need to choose an input clock unless you are doing
  34. something very specific. In most practical cases you can use timer_default
  35. which is 0. See the timer_delay() function for more information. */
  36. typedef enum
  37. {
  38. timer_Po_4 = 0,
  39. timer_Po_16 = 1,
  40. timer_Po_64 = 2,
  41. timer_Po_256 = 3,
  42. timer_TCLK = 5,
  43. timer_default = timer_Po_4,
  44. } timer_input_t;
  45. //---
  46. // Timer functions
  47. //---
  48. /* timer_setup() - set up a timer
  49. This function configures the requested timer without starting it. On
  50. success, it returns the first argument "timer", which is used as a timer
  51. identifier in all other timer functions. If the requested timer is already
  52. in use, this function fails and returns a negative number.
  53. This function sets the timer delay, the clock source, and registers a
  54. callback function to be called when the timer fires. An argument can be
  55. supplied to the callback function in the form of a pointer.
  56. When the timer fires, the callback function is called with the provided
  57. argument pointer. The callback decides whether the timer should continue
  58. running (by returning 0) or stop (by returning nonzero). In the latter case,
  59. events accumulated while the callback was running are dropped.
  60. It is sometimes difficult to choose a timer constant and a clock source
  61. given a wished delay in seconds, especially when overclock is used. The
  62. timer_delay() function is provided for this purpose.
  63. @timer Requested timer id
  64. @delay Delay between each event (the unit depends on the clock source)
  65. @clock Clock source used by the timer for counting down
  66. @callback Callback function (called when the timer fires)
  67. @arg Passed as argument to the callback function */
  68. int timer_setup(int timer, uint32_t delay, timer_input_t clock,
  69. int (*callback)(volatile void *arg), volatile void *arg);
  70. /* timer_delay() - compute a delay constant from a duration in seconds
  71. This function can used as a facility to calculate the [delay] argument to
  72. the timer_setup() function. It takes a microsecond delay as an argument and
  73. returns the corresponding timer constant. A typical use to start a timer
  74. with a 25 ms interval would be:
  75. timer_setup(0, timer_delay(0, 25 * 1000), 0, callback, arg);
  76. WARNING: Only timers 0 to 2 can count microseconds! Other timers have a
  77. resolution of around 30 us. Counting in ms is safe for all timers, though.
  78. For standard timers (0 to 2) it uses Po / 4 as clock input, which is very
  79. precise and can represent up to 3 minutes' time; for extra timers (3 and
  80. above) the clock is fixed to 32768 Hz.
  81. @timer The timer you are planning to use
  82. @delay_us Requested delay in microseconds */
  83. uint32_t timer_delay(int timer, uint64_t delay_us);
  84. /* timer_start() - start a configured timer
  85. The specified timer will start counting down and fire callbacks at regular
  86. intervals.
  87. @timer Timer id, as returned by timer_setup() */
  88. void timer_start(int timer);
  89. /* timer_reload() - change a timer's delay constant for next interrupts
  90. Changes the delay constant of the given timer. Nothing will happen until the
  91. next callback; then the timer will update its delay to reflect the new
  92. constant. The new delay can be calculated by the timer_delay() function.
  93. @timer Timer id, as returned by timer_setup()
  94. @delay New delay (unit depends on the clock source) */
  95. void timer_reload(int timer, uint32_t delay);
  96. /* timer_pause() - stop a running timer
  97. The specified timer will be paused; its counter will not be reset. A stopped
  98. timer can be resumed anytime by calling timer_start(). If you want to also
  99. reset the counter, use timer_reload().
  100. @timer Timer id, as returned by timer_setup() */
  101. void timer_pause(int timer);
  102. /* timer_stop() - stop and free a timer
  103. Stops and destroys a timer, making its id free for re-use. The id must not
  104. be used anymore until it is returned by a further call to timer_setup().
  105. @timer Timer id, as returned by timer_setup() */
  106. void timer_stop(int timer);
  107. //---
  108. // Predefined timer callbacks
  109. //---
  110. /* timer_timeout() - callback that sets a flag and halts the timer
  111. This predefined callback may be used when a timeout is required. It sets its
  112. argument pointer to 1 and halts the timer. The pointer must be of type
  113. int * and you must declare the variable as volatile int. */
  114. int timer_timeout(volatile void *arg);
  115. #endif /* GINT_TIMER */