Timers.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "Timers.h"
  2. #define TIMER 0x900D0000
  3. volatile uint32_t *timer_ctl = (uint32_t *) (TIMER + 0x08);
  4. volatile uint32_t *timer_load = (uint32_t *) (TIMER);
  5. volatile uint32_t *timer_value = (uint32_t *) (TIMER + 0x04);
  6. uint32_t timer_ctl_bkp[2], timer_load_bkp[2];
  7. #define TIMERS WalrusRPG::Timers
  8. void TIMERS::init(uint32_t timer)
  9. {
  10. timer_ctl_bkp[timer] = timer_ctl[8 * timer];
  11. timer_load_bkp[timer] = timer_load[8 * timer];
  12. }
  13. void TIMERS::restore(uint32_t timer)
  14. {
  15. timer_ctl[8 * timer] &= ~(1 << 7);
  16. timer_ctl[8 * timer] = timer_ctl_bkp[timer] & ~(1 << 7);
  17. timer_load[8 * timer] = timer_load_bkp[timer];
  18. timer_ctl[8 * timer] = timer_ctl_bkp[timer];
  19. }
  20. void TIMERS::mode(uint32_t timer, bool free_run, bool oneshot, bool interrupt,
  21. uint32_t div, bool full_width_counter)
  22. {
  23. uint32_t mode = 0;
  24. if (!free_run)
  25. mode |= (1 << 6);
  26. if (oneshot)
  27. mode |= (1 << 0);
  28. if (interrupt)
  29. mode |= (1 << 5);
  30. if (full_width_counter)
  31. mode |= (1 << 1);
  32. switch (div)
  33. {
  34. case 256:
  35. mode |= (1 << 3);
  36. break;
  37. case 16:
  38. mode |= (1 << 2);
  39. break;
  40. }
  41. timer_ctl[8 * timer] &= ~(1 << 7);
  42. timer_ctl[8 * timer] = mode;
  43. timer_ctl[8 * timer] |= (1 << 7);
  44. }
  45. void TIMERS::load(uint32_t timer, uint32_t value)
  46. {
  47. timer_load[8 * timer] = 0xFFFFFFFF - value;
  48. }
  49. uint32_t TIMERS::read(uint32_t timer)
  50. {
  51. return 0xFFFFFFFF - timer_value[8 * timer];
  52. }