t_pwm.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifndef __T_PWM_H_
  2. #define __T_PWM_H_
  3. //#include "..\..\includes\types.h"
  4. //#include "..\gpio\gpio.h"
  5. //#include "..\icu\icu.h"
  6. #include "include.h"
  7. #include "t_bt_3ds.h"
  8. #include "gpio.h"
  9. #include "t_types.h"
  10. #define BASE_PWM_PTR \
  11. ((HW_PWM*)0x00f00100)
  12. #define GET_REG_ADDR(x) (((uint32)(&(x))))
  13. //=====================================================================================
  14. // 16bit PWM & Timer
  15. //=====================================================================================
  16. #define REG_APB_BK_PWM_CTRL (*( (volatile unsigned long *) (APB_PWM_BASE + (0x0*4)) ))
  17. #define bit_PWM0_EN BIT_0
  18. #define bit_PWM0_INT_ENABLE BIT_1
  19. #define bit_PWM0_MODE 2
  20. #define bit_PWM1_EN BIT_4
  21. #define bit_PWM1_INT_ENABLE BIT_5
  22. #define bit_PWM1_MODE 6
  23. #define bit_PWM2_EN BIT_8
  24. #define bit_PWM2_INT_ENABLE BIT_9
  25. #define bit_PWM2_MODE 10
  26. #define bit_PWM3_EN BIT_12
  27. #define bit_PWM3_INT_ENABLE BIT_13
  28. #define bit_PWM3_MODE 14
  29. #define bit_PWM4_EN BIT_16
  30. #define bit_PWM4_INT_ENABLE BIT_17
  31. #define bit_PWM4_MODE 18
  32. #define bit_PWM5_EN BIT_20
  33. #define bit_PWM5_INT_ENABLE BIT_21
  34. #define bit_PWM5_MODE 22
  35. #define REG_APB_BK_PWM_INTR (*( (volatile unsigned long *) (APB_PWM_BASE + (0x1*4)) ))
  36. #define REG_APB_BK_PWM0_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0x2*4)) ))
  37. #define REG_APB_BK_PWM0_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0x3*4)) ))
  38. #define REG_APB_BK_PWM1_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0x4*4)) ))
  39. #define REG_APB_BK_PWM1_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0x5*4)) ))
  40. #define REG_APB_BK_PWM2_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0x6*4)) ))
  41. #define REG_APB_BK_PWM2_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0x7*4)) ))
  42. #define REG_APB_BK_PWM3_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0x8*4)) ))
  43. #define REG_APB_BK_PWM3_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0x9*4)) ))
  44. #define REG_APB_BK_PWM4_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0xA*4)) ))
  45. #define REG_APB_BK_PWM4_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0xB*4)) ))
  46. #define REG_APB_BK_PWM5_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0xC*4)) ))
  47. #define REG_APB_BK_PWM5_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0xD*4)) ))
  48. #define REG_APB_BK_PWM6_CNT (*( (volatile unsigned long *) (APB_PWM_BASE + (0xE*4)) ))
  49. #define REG_APB_BK_PWM6_CAP (*( (volatile unsigned long *) (APB_PWM_BASE + (0xF*4)) ))
  50. #pragma anon_unions
  51. typedef struct HW_PWMU{
  52. union{
  53. uint32 setting;
  54. struct{
  55. uint32 end:16;
  56. //PWM0 counter end-value
  57. uint32 dc:16;
  58. //PWM0 Duty-Cycle. Must smaller or equal to end-value
  59. };
  60. };
  61. union{
  62. uint32 counter;
  63. struct{
  64. uint32 cap_out:16;
  65. //PWM0 capture value. Read-Only.
  66. uint32 :16;
  67. };
  68. };
  69. }HW_PWMU;
  70. typedef struct HW_PWM{
  71. union{
  72. uint32 cfg;
  73. struct{
  74. uint32 pwm0_en:1;
  75. //PWM0 1bit unit enable ;0-Disable. 1-Enable
  76. uint32 pwm0_int_en:1;
  77. //PWM0 1bit interrupt enable;0-Disable. 1-Enable
  78. uint32 pwm0_mode:2;
  79. //PWM0 2bit mode selection;00: PWM Mode;01: TIMER;10: Capture Posedge;11: Cpature Negedge
  80. uint32 pwm1_en:1;
  81. //PWM1 1bit unit enable;0-Disable. ;1-Enable
  82. uint32 pwm1_int_en:1;
  83. //PWM1 1bit interrupt enable;0-Disable. ;1-Enable.
  84. uint32 pwm1_mode:2;
  85. //PWM1 2bit mode selection;00: PWM Mode;01: TIMER;10: Capture Posedge;11: Cpature Negedge
  86. uint32 pwm2_en:1;
  87. //PWM2 1bit unit enable ;0-Disable. 1-Enable
  88. uint32 pwm2_int_en:1;
  89. //PWM2 1bit interrupt enable;0-Disable. 1-Enable
  90. uint32 pwm2_mode:2;
  91. //PWM2 2bit mode selection;00: PWM Mode;01: TIMER;10: Capture Posedge;11: Cpature Negedge
  92. uint32 pwm3_en:1;
  93. //PWM3 1bit unit enable;0-Disable. ;1-Enable
  94. uint32 pwm3_int_en:1;
  95. //PWM3 1bit interrupt enable;0-Disable. ;1-Enable.
  96. uint32 pwm3_mode:2;
  97. //PWM3 2bit mode selection;00: PWM Mode;01: TIMER;10: Capture Posedge;11: Cpature Negedge
  98. uint32 pwm4_en:1;
  99. //PWM4 1bit unit enable ;0-Disable. 1-Enable
  100. uint32 pwm4_int_en:1;
  101. //PWM4 1bit interrupt enable;0-Disable. 1-Enable
  102. uint32 pwm4_mode:2;
  103. //PWM4 2bit mode selection;00: PWM Mode;01: TIMER;10: Capture Posedge;11: Cpature Negedge
  104. uint32 pwm5_en:1;
  105. //PWM5 1bit unit enable;0-Disable. ;1-Enable
  106. uint32 pwm5_int_en:1;
  107. //PWM5 1bit interrupt enable;0-Disable. ;1-Enable.
  108. uint32 pwm5_mode:2;
  109. //PWM5 2bit mode selection;00: PWM Mode;01: TIMER;10: Capture Posedge;11: Cpature Negedge
  110. uint32 :8;
  111. };
  112. };
  113. union{
  114. uint32 REG_0x1;
  115. struct{
  116. uint32 pwm0_int:1;
  117. //PWM0 Interrupt Status. Write 1 to clear.
  118. uint32 pwm1_int:1;
  119. //PWM1 Interrupt Status. Write 1 to clear.
  120. uint32 pwm2_int:1;
  121. //PWM2 Interrupt Status. Write 1 to clear.
  122. uint32 pwm3_int:1;
  123. //PWM3 Interrupt Status. Write 1 to clear.
  124. uint32 pwm4_int:1;
  125. //PWM4 Interrupt Status. Write 1 to clear.
  126. uint32 pwm5_int:1;
  127. //PWM5 Interrupt Status. Write 1 to clear.
  128. uint32 :26;
  129. };
  130. };
  131. HW_PWMU pwms[6];
  132. }HW_PWM;
  133. #define PWM0 \
  134. 0
  135. #define PWM1 \
  136. 1
  137. #define PWM2 \
  138. 2
  139. #define PWM3 \
  140. 3
  141. #define PWM4 \
  142. 4
  143. #define PWM5 \
  144. 5
  145. #define PWM8 \
  146. 8
  147. #define PWM9 \
  148. 9
  149. #define PWM10 \
  150. 10
  151. #define PWM11 \
  152. 11
  153. #define PWM_CLKSEL_32K \
  154. 0
  155. #define PWM_CLKSEL_16M \
  156. 1
  157. #define PWM_CLKSEL_DPLL \
  158. 2
  159. #define PWM_BFD_BS_END 0
  160. #define PWM_BFD_BL_END 16
  161. #define PWM_BFD_BS_DUTY 16
  162. #define PWM_BFD_BL_DUTY 16
  163. #define BK2471_PWM_CTRL REG32(APB_PWM_BASE)
  164. #define BK2471_PWM_SETTING(pn) REG32(APB_PWM_BASE+8+(pn)*8)
  165. #define BK2471_PWM_CAP(pn) REG32(APB_PWM_BASE+0x0c+(pn)*8)
  166. #define PWM_SetupIOL(bl) \
  167. {\
  168. if(bl&0xff)GPIO_SetSF(GPIO1, (bl&0xff));\
  169. if(bl&0xf00)GPIO_SetSF(GPIO0, ((bl)>>8));\
  170. }
  171. #define PWM_OPEN_CLOCK(pn,pwmsel) \
  172. {\
  173. /*pn:pwm编号,0-5 pwmsel:pwm时钟源选择 */\
  174. if((pn)==PWM5){\
  175. /*BASE_ICU_PTR->pwm[4].pwm_clk*/REG_AHB0_ICU_PWM4CLKCON &= _BFD(7,4,3);\
  176. /*BASE_ICU_PTR->pwm[4].pwm_clk*/REG_AHB0_ICU_PWM4CLKCON |= BFD(pwmsel,5,2);\
  177. }else{\
  178. if(pn<8){\
  179. /*BASE_ICU_PTR->pwm[pn].pwm_clk*/REG32(GET_REG_ADDR(REG_AHB0_ICU_PWM0CLKCON)+ (pn)*4) &= _BFD(7,0,3);\
  180. /*BASE_ICU_PTR->pwm[pn].pwm_clk*/REG32(GET_REG_ADDR(REG_AHB0_ICU_PWM0CLKCON)+ (pn)*4) |= BFD(pwmsel,1,2);\
  181. }else{\
  182. /*BASE_ICU_PTR->ceva = 0;*/REG_AHB0_ICU_CEVA_CLKCON=0;\
  183. }\
  184. }\
  185. }
  186. #define PWM_Setup(pn,pres,resol,duty) \
  187. {\
  188. /*pn:pwm编号,0-5 pres:pwm预分频 */\
  189. /*pn>7,resol = end,duty = mid*/\
  190. /*resol:pwm周期(pres*resol<65536) duty:占空比*/\
  191. if((pn)<=PWM5){\
  192. /*BASE_PWM_PTR->pwms[pn].setting*/BK2471_PWM_SETTING(pn) =(BFD((pres)*(resol),PWM_BFD_BS_END,PWM_BFD_BL_END)|\
  193. BFD((pres)*(duty),PWM_BFD_BS_DUTY,PWM_BFD_BL_DUTY));\
  194. }else if((pn)>=PWM8){\
  195. BASE_BT_3DS_PTR->REG_0x12 = ((((pres)*(resol))-1)>>4);\
  196. BASE_BT_3DS_PTR->REG_0x13 = ((((pres)*(resol))-1)&0x0f)<<4;\
  197. BASE_BT_3DS_PTR->pwms[pn-PWM8].d[0]=BFD(0,0,22);\
  198. BASE_BT_3DS_PTR->pwms[pn-PWM8].d[1]=BFD((pres)*(duty),0,22);\
  199. BASE_BT_3DS_PTR->pwms[pn-PWM8].d[2]=BFD((pres)*(resol),0,22);\
  200. }\
  201. }
  202. #define PWM_SET_DUTY(pn,pres,duty) \
  203. {\
  204. /*pn:pwm编号,0-5 duty:占空比*/\
  205. if(pn<8){\
  206. /*BASE_PWM_PTR->pwms[pn].setting*/BK2471_PWM_SETTING(pn) &= _BFD(0xffff,PWM_BFD_BS_DUTY,PWM_BFD_BL_DUTY);\
  207. /*BASE_PWM_PTR->pwms[pn].setting*/BK2471_PWM_SETTING(pn) |= BFD((duty)*(pres),PWM_BFD_BS_DUTY,PWM_BFD_BL_DUTY);\
  208. }else{\
  209. BASE_BT_3DS_PTR->pwms[pn-PWM8].d[1]=BFD((duty)*(pres),0,22);\
  210. }\
  211. }
  212. #define PWM_Open(pn) \
  213. {\
  214. /*pn:pwm编号,0-5*/\
  215. if((pn)<8){/*BASE_PWM_PTR->cfg*/BK2471_PWM_CTRL |= BIT(4*(pn));}\
  216. if((pn)>=8){\
  217. BASE_BT_3DS_PTR->REG_0x0 |= (BIT(1)/*每次都操作*/|BIT((pn))|BIT(((pn)-8)+12));\
  218. }\
  219. }
  220. #define PWM_CLOSE_CLOCK(pn) \
  221. {\
  222. if((pn)==PWM5){\
  223. REG_AHB0_ICU_PWM4CLKCON |= (BIT(4));\
  224. }else{\
  225. if(pn<8){\
  226. REG32(GET_REG_ADDR(REG_AHB0_ICU_PWM0CLKCON)+ (pn)*4) |= BIT(0);\
  227. }else{\
  228. BASE_ICU_PTR->ceva = 1;\
  229. }\
  230. }\
  231. }
  232. #define PWM_CLOSE(pn) \
  233. {\
  234. /*pn:pwm编号,只能是数字0-4*/\
  235. if(pn<8)BK2471_PWM_CTRL &= _BIT(4*(pn));\
  236. else{\
  237. BASE_BT_3DS_PTR->REG_0x0 &=(_BIT((pn))&_BIT(((pn)-8)+12));\
  238. }\
  239. /*PWM_CLOSE_CLOCK(pn);*/\
  240. }
  241. #endif