pwm_cap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "FreeRTOS.h"
  2. #include "board.h"
  3. #ifdef PWM_CAP_SUPPORT
  4. #include "chip.h"
  5. #include "pwm_cap.h"
  6. #define PWM_CAP_INT_CLEAR (0x10*4)
  7. #define PWM_CAP_INT_EN (0x20*4)
  8. #define PWM_CAP_INT_STA (0x21*4)
  9. #define PWM_CAP_SYS_FRQ (0x00)
  10. #define PWM_CAP_SETTING (0x04)
  11. #define PWM_CAP_CYCLE_CAP (0x08)
  12. #define PWM_FRE_CAP (0x0c)
  13. #define PWM_CAP_REG(x) (REGS_PWM_BASE + 0x100 + 0x10 * (x))
  14. void pwm_cap_Int_Handler(void *para);
  15. static void pwm_cap_clk_config(UINT8 id,UINT32 clk)
  16. {
  17. writel(clk,PWM_CAP_REG(id) + PWM_CAP_SYS_FRQ);
  18. }
  19. static void pwm_cap_en(UINT8 id,UINT8 enable)
  20. {
  21. unsigned int reg;
  22. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  23. if(enable)
  24. reg |= (1UL<<31);
  25. else
  26. reg &= ~(1UL<<31);
  27. writel(reg, PWM_CAP_REG(id) + PWM_CAP_SETTING);
  28. }
  29. static void pwm_cap_int_method(UINT8 id,UINT8 int_method)
  30. {
  31. unsigned int reg;
  32. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  33. reg &= ~(0x3<<28);
  34. reg |= (int_method<<28);
  35. writel(reg,PWM_CAP_REG(id) + PWM_CAP_SETTING);
  36. }
  37. static void pwm_cap_set_glitch(UINT8 id,UINT8 glitch)
  38. {
  39. unsigned int reg;
  40. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  41. reg &= ~(0xF<<24);
  42. reg |= (glitch<<24);
  43. writel(reg,PWM_CAP_REG(id) + PWM_CAP_SETTING);
  44. }
  45. static void pwm_cap_method(UINT8 id,UINT8 cap_method)
  46. {
  47. unsigned int reg;
  48. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  49. reg &= ~(0x1<<30);
  50. reg |= (cap_method<<30);
  51. writel(reg,PWM_CAP_REG(id) + PWM_CAP_SETTING);
  52. }
  53. static void pwm_cap_times(UINT8 id,UINT8 cat_times)
  54. {
  55. unsigned int reg;
  56. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  57. reg &= ~(0xFF<<16);
  58. reg |= (cat_times<<16);
  59. writel(reg,PWM_CAP_REG(id) + PWM_CAP_SETTING);
  60. }
  61. static void pwm_cap_based_unit(UINT8 id,UINT8 cap_based_unit)
  62. {
  63. unsigned int reg;
  64. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  65. reg &= ~(0x7<<12);
  66. reg |= (cap_based_unit<<12);
  67. writel(reg,PWM_CAP_REG(id) + PWM_CAP_SETTING);
  68. }
  69. static void pwm_cap_interval(UINT8 id,UINT8 cap_interval)
  70. {
  71. unsigned int reg;
  72. reg = readl(PWM_CAP_REG(id) + PWM_CAP_SETTING);
  73. reg &= ~(0xFF<<0);
  74. reg |= (cap_interval<<0);
  75. writel(reg,PWM_CAP_REG(id) + PWM_CAP_SETTING);
  76. }
  77. void pwm_Initial_Cap(UINT8 id)
  78. {
  79. pwm_cap_clk_config(id,ulClkGetRate(CLK_APB));
  80. pwm_cap_int_method(id,PWM_CAP_ONCE_FINISH_INT);
  81. pwm_cap_set_glitch(id,PWM_CAP_GLITCH);
  82. pwm_cap_method(id,PWM_CAP_NUM);
  83. pwm_cap_times(id,PWM_CAP_TIMES);
  84. pwm_cap_based_unit(id,PWM_CAP_UINT_1000MS);
  85. pwm_cap_interval(id,PWM_CAP_INTERVAL);
  86. request_irq(PWM_IRQn, 0, pwm_cap_Int_Handler, NULL);
  87. }
  88. void pwm_cap_Int_Handler(void *param)
  89. {
  90. int i;
  91. unsigned int val;
  92. val = readl(PWM_CAP_REG(PWM_CAP_CH0) + PWM_CAP_INT_STA);
  93. for(i=0; i<=PWM_CAP_CH3; i++){
  94. if(val & (1 << i)){
  95. printf("pwm cap[%d] interrupt!\n",i);
  96. writel(1 << i,PWM_CAP_REG(PWM_CAP_CH0) +PWM_CAP_INT_CLEAR);
  97. pwm_getCapVal(i);
  98. writel(0,PWM_CAP_REG(PWM_CAP_CH0) +PWM_CAP_INT_CLEAR);
  99. writel(readl(PWM_CAP_REG(i)+PWM_CAP_SETTING)|(1UL<<31), PWM_CAP_REG(i)+PWM_CAP_SETTING);
  100. }
  101. }
  102. }
  103. double pwm_getCapVal(UINT8 id)
  104. {
  105. printf("pwmin chn:%d fre:%d \n", id, readl(PWM_CAP_REG(id)+PWM_FRE_CAP)/16);
  106. return (double)readl(PWM_CAP_REG(id)+PWM_FRE_CAP)/16;
  107. }
  108. void pwm_enableCapIRQ(UINT8 id,unsigned char en)
  109. {
  110. unsigned int reg = 0;
  111. reg = readl(PWM_CAP_REG(0)+ PWM_CAP_INT_EN);
  112. reg &=~(1<<id);
  113. if(en)
  114. {
  115. reg |=(1<<id);
  116. writel(reg,PWM_CAP_REG(0)+ PWM_CAP_INT_EN);
  117. }
  118. else
  119. writel(reg,PWM_CAP_REG(0)+ PWM_CAP_INT_EN);
  120. }
  121. void pwm_cap_init(UINT8 id)
  122. {
  123. // unsigned int irq_enable = 1;
  124. pwm_Initial_Cap(id);
  125. pwm_enableCapIRQ(id,1);
  126. pwm_cap_en(id,PWM_CAP_ENABLE);
  127. }
  128. #endif