timers.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. *timer_ctl = timer_ctl_bkp[timer];
  43. *timer_divider = timer_load_bkp[timer];
  44. }
  45. }
  46. void timer_load(unsigned timer, unsigned value)
  47. {
  48. if (is_cx)
  49. {
  50. volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
  51. *timer_load = value;
  52. }
  53. else
  54. {
  55. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x0C * timer);
  56. *timer_value = value;
  57. }
  58. }
  59. unsigned timer_read(unsigned timer)
  60. {
  61. if (is_cx)
  62. {
  63. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x04 + 0x20 * timer);
  64. return *timer_value;
  65. }
  66. else
  67. {
  68. volatile unsigned *timer_value = (unsigned *) (TIMER + 0x0C * timer);
  69. return *timer_value;
  70. }
  71. }