timers.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <os.h>
  2. #include "timers.h"
  3. #define TIMER 0x900D0000
  4. unsigned timer_ctl_bkp[2], timer_load_bkp[2];
  5. void timer_init(unsigned timer)
  6. {
  7. if (is_cx)
  8. {
  9. volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x20 * timer);
  10. volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
  11. timer_ctl_bkp[timer] = *timer_ctl;
  12. timer_load_bkp[timer] = *timer_load;
  13. *timer_ctl &= ~(1 << 7);
  14. *timer_ctl = 0b01100011;
  15. *timer_ctl |= (1 << 7);
  16. }
  17. else
  18. {
  19. volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x0C * timer);
  20. volatile unsigned *timer_divider = (unsigned *) (TIMER + 0x04 + 0x0C * timer);
  21. timer_ctl_bkp[timer] = *timer_ctl;
  22. timer_load_bkp[timer] = *timer_divider;
  23. *timer_ctl = 0;
  24. *timer_divider = 0;
  25. }
  26. }
  27. void timer_restore(unsigned timer)
  28. {
  29. if (is_cx)
  30. {
  31. volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x20 * timer);
  32. volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
  33. *timer_ctl &= ~(1 << 7);
  34. *timer_ctl = timer_ctl_bkp[timer] & ~(1 << 7);
  35. *timer_load = timer_load_bkp[timer];
  36. *timer_ctl = timer_ctl_bkp[timer];
  37. }
  38. else
  39. {
  40. volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x0C * timer);
  41. volatile unsigned *timer_divider = (unsigned *) (TIMER + 0x04 + 0x0C * timer);
  42. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x0C * timer);
  43. *timer_ctl = timer_ctl_bkp[timer];
  44. *timer_divider = timer_load_bkp[timer];
  45. *timer_value = 32;
  46. }
  47. }
  48. void timer_load(unsigned timer, unsigned value)
  49. {
  50. if (is_cx)
  51. {
  52. volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
  53. *timer_load = value;
  54. }
  55. else
  56. {
  57. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x0C * timer);
  58. *timer_value = value;
  59. }
  60. }
  61. unsigned timer_read(unsigned timer)
  62. {
  63. if (is_cx)
  64. {
  65. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x04 + 0x20 * timer);
  66. return *timer_value;
  67. }
  68. else
  69. {
  70. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x0C * timer);
  71. return *timer_value;
  72. }
  73. }