timer.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* ___INGO___ */
  2. #include <arm/NXP/LPC17xx/LPC17xx.h>
  3. #include "bits.h"
  4. #include "config.h"
  5. #include "timer.h"
  6. #include "clock.h"
  7. /* bit definitions */
  8. #define RITINT 0
  9. #define RITEN 3
  10. #define PCRIT 16
  11. volatile tick_t ticks;
  12. extern uint32_t f_cpu;
  13. void __attribute__((weak,noinline)) SysTick_Hook(void) {
  14. // Empty function for hooking the systick handler
  15. }
  16. /* Systick interrupt handler */
  17. void SysTick_Handler(void) {
  18. ticks++;
  19. SysTick_Hook();
  20. }
  21. void timer_init(void) {
  22. /* turn on power to RIT */
  23. BITBAND(LPC_SC->PCONP, PCRIT) = 1;
  24. /* clear RIT mask */
  25. LPC_RIT->RIMASK = 0; //xffffffff;
  26. /* PCLK = CCLK */
  27. BITBAND(LPC_SC->PCLKSEL1, 26) = 1;
  28. BITBAND(LPC_SC->PCLKSEL1, PCLK_TIMER3) = 1;
  29. /* enable SysTick */
  30. SysTick_Config(SysTick->CALIB & SysTick_CALIB_TENMS_Msk);
  31. }
  32. extern int testval;
  33. void delay_us(unsigned int time) {
  34. /* Prepare RIT */
  35. LPC_RIT->RICOUNTER = 0;
  36. LPC_RIT->RICOMPVAL = (CONFIG_CPU_FREQUENCY / 1000000) * time;
  37. LPC_RIT->RICTRL = BV(RITEN) | BV(RITINT);
  38. /* Wait until RIT signals an interrupt */
  39. while (!(BITBAND(LPC_RIT->RICTRL, RITINT))) ;
  40. /* Disable RIT */
  41. LPC_RIT->RICTRL = 0;
  42. }
  43. void delay_ms(unsigned int time) {
  44. /* Prepare RIT */
  45. LPC_RIT->RICOUNTER = 0;
  46. LPC_RIT->RICTRL = BV(RITEN) | BV(RITINT);
  47. LPC_RIT->RICOMPVAL = (f_cpu / 1000) * time;
  48. /* Wait until RIT signals an interrupt */
  49. while (!(BITBAND(LPC_RIT->RICTRL, RITINT))) ;
  50. /* Disable RIT */
  51. LPC_RIT->RICTRL = 0;
  52. }