hw_pwm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include "global.h"
  2. #include "hw_pwm.h"
  3. typedef struct
  4. {
  5. TIM_TypeDef *base; //对象实体
  6. int unit_in_hz; //1秒多少hz
  7. uint16_t ch; //通道选择
  8. uint16_t irqn; //中断源
  9. } hw_pwm_t;
  10. typedef struct
  11. {
  12. GPIO_TypeDef *pulse_port; //端口
  13. uint16_t pulse_pin; //引脚
  14. uint32_t remap; //复用功能位置
  15. GPIO_TypeDef *dir_port; //方向功能端口
  16. uint16_t dir_pin; //方向功能引脚
  17. GPIO_TypeDef *en_port; //使能功能端口
  18. uint16_t en_pin; //使能功能引脚
  19. GPIO_TypeDef *alr_port; //报警功能端口
  20. uint16_t alr_pin; //报警功能引脚
  21. GPIO_TypeDef *clr_port; //报警清除功能端口
  22. uint16_t clr_pin; //报警清除功能引脚
  23. GPIO_TypeDef *tmode_port; //模式切换端口
  24. uint16_t tmode_pin; //模式切换引脚
  25. GPIO_TypeDef *tarr_port; //转矩达到端口
  26. uint16_t tarr_pin; //转矩达到引脚
  27. } hw_pwm_pin_t;
  28. static hw_pwm_pin_t pin_maps[HW_PWM_NUMBER] = {
  29. { GPIOA, 3, 0, GPIOE, 15, GPIOE, 10, GPIOG, 15, GPIOD, 14, GPIOD, 14, GPIOG, 15},
  30. { GPIOE, 9, GPIO_FullRemap_TIM1, GPIOG, 7, GPIOE, 12, GPIOG, 12, GPIOD, 15 ,GPIOD,15,GPIOG,12},
  31. { GPIOD, 12, GPIO_Remap_TIM4, GPIOE, 14, GPIOE, 13, GPIOG, 10, GPIOD, 0 ,GPIOD,0,GPIOG,10}
  32. };
  33. /* PWM分布
  34. GPIO_Remap_TIM4
  35. TIM4_CH1 PB6 PD12
  36. TIM4_CH2 PB7 PD13
  37. TIM4_CH3 PB8 PD14
  38. TIM4_CH4 PB9 PD15
  39. -------------------------------------
  40. GPIO_PartialRemap_TIM3 GPIO_FullRemap_TIM3
  41. TIM3_CH1 PA6 PB4 PC6
  42. TIM3_CH2 PA7 PB5 PC7
  43. TIM3_CH3 PB0 PB0 PC8
  44. TIM3_CH4 PB1 PB1 PC9
  45. -------------------------------------
  46. GPIO_PartialRemap1_TIM2 GPIO_PartialRemap2_TIM2 GPIO_FullRemap_TIM2
  47. TIM2_CH1 PA0 PA15 PA0 PA15
  48. TIM2_CH2 PA1 PB3 PA1 PB3
  49. TIM2_CH3 PA2 PA2 PC8 PC8
  50. TIM2_CH4 PA3 PA3 PC9 PC9
  51. -------------------------------------
  52. GPIO_FullRemap_TIM1
  53. TIM1_CH1 PA8 PE9
  54. TIM1_CH2 PA9 PE11
  55. TIM1_CH3 PA10 PE13
  56. TIM1_CH4 PA11 PE14
  57. -------------------------------------
  58. */
  59. static hw_pwm_t hw_pwm_maps[HW_PWM_NUMBER] = {
  60. { TIM5, 10000000, 4, TIM5_IRQn }, //TIM3_CH4 GPIOC_9
  61. { TIM1, 10000000, 1, TIM1_UP_IRQn } , //TIM1_CH1 GPIOA_8
  62. { TIM4, 10000000, 1, TIM4_IRQn }
  63. };
  64. //周期寄存器表
  65. uint16_t *hw_pwm_period_registers[HW_PWM_NUMBER];
  66. //占空比寄存器表
  67. uint16_t *hw_pwm_dutycycle_registers[HW_PWM_NUMBER];
  68. //配置寄存器表
  69. uint16_t *hw_pwm_cr_registers[HW_PWM_NUMBER];
  70. //配置寄存器表
  71. uint16_t *hw_pwm_egr_registers[HW_PWM_NUMBER];
  72. //方向管脚读写表
  73. hw_pin_readwrite_t hw_pwm_dir_rw_map[HW_PWM_NUMBER];
  74. //使能管脚读写表
  75. hw_pin_readwrite_t hw_pwm_en_rw_map[HW_PWM_NUMBER];
  76. //回调函数,需要使用PWM前进行注册
  77. static pwm_handle pwm_handle_maps[HW_PWM_NUMBER];
  78. //回调参数
  79. static void *pwm_hanlde_parames[HW_PWM_NUMBER];
  80. //X轴
  81. void TIM5_IRQHandler(void)
  82. {
  83. if(TIM5->SR & (1 << 0))
  84. {
  85. TIM5->SR &= ~(1 << 0);
  86. pwm_handle_maps[HW_PWM_1](pwm_hanlde_parames[HW_PWM_1]);
  87. }
  88. }
  89. //Y轴
  90. void TIM1_UP_IRQHandler(void)
  91. {
  92. if (TIM1->SR & (1 << 0))
  93. {
  94. TIM1->SR &= ~(1 << 0);
  95. pwm_handle_maps[HW_PWM_2](pwm_hanlde_parames[HW_PWM_2]);
  96. }
  97. }
  98. //Z轴
  99. void TIM4_IRQHandler(void)
  100. {
  101. if (TIM4->SR & (1 << 0))
  102. {
  103. TIM4->SR &= ~(1 << 0);
  104. pwm_handle_maps[HW_PWM_3](pwm_hanlde_parames[HW_PWM_3]);
  105. }
  106. }
  107. /**
  108. * 默认指针函数
  109. *
  110. * @author LXZ (121919)
  111. *
  112. * @param handler
  113. */
  114. void hw_pwm_default_handler(void *handler) {
  115. }
  116. /**
  117. * 设置方向
  118. *
  119. * @author lxz
  120. *
  121. * @param no
  122. * @param dir
  123. */
  124. void hw_pwm_set_dir(unsigned char no, int dir) {
  125. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  126. if (pwm_pin->dir_port != (void *)0) {
  127. if (dir) {
  128. pwm_pin->dir_port->BSRR = 1 << (pwm_pin->dir_pin + 16);
  129. }
  130. else {
  131. pwm_pin->dir_port->BSRR = 1 << (pwm_pin->dir_pin);
  132. }
  133. }
  134. }
  135. /**
  136. * 获取方向
  137. *
  138. * @author lxz
  139. *
  140. * @param no
  141. *
  142. * @return int
  143. */
  144. int hw_pwm_get_dir(unsigned char no) {
  145. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  146. if (pwm_pin->dir_port != (void *)0) {
  147. return (pwm_pin->dir_port->IDR >> pwm_pin->dir_pin) & (0x01) == 0x01;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * 设置使能状态
  153. *
  154. * @author lxz
  155. *
  156. * @param no
  157. * @param dir
  158. */
  159. void hw_pwm_set_enable(unsigned char no, int en) {
  160. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  161. if (pwm_pin->en_port != (void *)0) {
  162. if (en) {
  163. pwm_pin->en_port->BSRR = 1 << (pwm_pin->en_pin + 16);
  164. }
  165. else {
  166. pwm_pin->en_port->BSRR = 1 << pwm_pin->en_pin;
  167. }
  168. }
  169. }
  170. /**
  171. * 获取使能状态
  172. *
  173. * @author lxz
  174. *
  175. * @param no
  176. *
  177. * @return int
  178. */
  179. int hw_pwm_get_enable(unsigned char no) {
  180. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  181. if (pwm_pin->en_port != (void *)0) {
  182. return (pwm_pin->en_port->IDR >> pwm_pin->en_pin) & (0x01)==0x01;
  183. }
  184. return 0;
  185. }
  186. /**
  187. * 获取报警状态
  188. *
  189. * @author lxz
  190. *
  191. * @param no
  192. *
  193. * @return int
  194. */
  195. int hw_pwm_get_alr(unsigned char no) {
  196. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  197. if (pwm_pin->alr_port != (void *)0) {
  198. return (pwm_pin->alr_port->IDR >> pwm_pin->alr_pin) & (0x01)==0x01;
  199. }
  200. return 0;
  201. }
  202. /**
  203. * 设置报警清除
  204. *
  205. * @author lxz
  206. *
  207. * @param no
  208. * @param dir
  209. */
  210. void hw_pwm_set_clr(unsigned char no, int en) {
  211. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  212. if (pwm_pin->clr_port != (void *)0) {
  213. if (en) {
  214. pwm_pin->clr_port->BSRR = 1 << (pwm_pin->clr_pin + 16);
  215. }
  216. else {
  217. pwm_pin->clr_port->BSRR = 1 << pwm_pin->clr_pin;
  218. }
  219. }
  220. }
  221. /**
  222. * 获取扭矩到状态
  223. *
  224. * @author lxz
  225. *
  226. * @param no
  227. *
  228. * @return int
  229. */
  230. int hw_pwm_get_tarr(unsigned char no) {
  231. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  232. if (pwm_pin->tarr_port != (void *)0) {
  233. return (pwm_pin->tarr_port->IDR >> pwm_pin->tarr_pin) & (0x01)==0x01;
  234. }
  235. return 0;
  236. }
  237. /**
  238. * 设置转矩模式
  239. *
  240. * @author lxz
  241. *
  242. * @param no
  243. * @param dir
  244. */
  245. void hw_pwm_set_tmode(unsigned char no, int en) {
  246. hw_pwm_pin_t *pwm_pin = &pin_maps[no];
  247. if (pwm_pin->tmode_port != (void *)0) {
  248. if (en) {
  249. pwm_pin->tmode_port->BSRR = 1 << (pwm_pin->tmode_pin + 16);
  250. }
  251. else {
  252. pwm_pin->tmode_port->BSRR = 1 << pwm_pin->tmode_pin;
  253. }
  254. }
  255. }
  256. /**
  257. * 获取脉冲的频率
  258. *
  259. * @author lxz
  260. *
  261. * @param no
  262. *
  263. * @return int
  264. */
  265. int hw_pwm_get_clk(unsigned char no) {
  266. return hw_pwm_maps[no].unit_in_hz;
  267. }
  268. /**
  269. * 启动pwm
  270. *
  271. * @author lxz (2019/5/20/周一)
  272. *
  273. * @param no
  274. */
  275. void hw_pwm_start(unsigned char no) {
  276. HW_PWM_ON(no);
  277. }
  278. /**
  279. * 停止PWM
  280. *
  281. * @author lxz (2019/5/20/周一)
  282. *
  283. * @param no
  284. */
  285. void hw_pwm_stop(unsigned char no) {
  286. HW_PWM_OFF(no);
  287. }
  288. /**
  289. * 设置PWM的周期
  290. *
  291. * @author lxz
  292. *
  293. * @param no
  294. * @param value
  295. */
  296. void hw_pwm_set_period(unsigned char no, int value) {
  297. HW_PWM_PERIOD(no) = value;
  298. HW_PWM_DUTYCYCLE(no) = value >> 1;
  299. }
  300. /**
  301. * 注册PWM中断的处理事件
  302. *
  303. * @author lxz
  304. *
  305. * @param no
  306. * @param handle
  307. * @param para
  308. */
  309. void hw_pwm_it_register(unsigned char no, pwm_handle handle, void *para) {
  310. if (no < HW_PWM_NUMBER) {
  311. pwm_hanlde_parames[no] = para;
  312. pwm_handle_maps[no] = handle;
  313. }
  314. }
  315. static void hw_pwm_pulse_init(int index)
  316. {
  317. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  318. uint32_t port = (uint32_t)pwm_pin->pulse_port;
  319. uint16_t pin = pwm_pin->pulse_pin;
  320. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  321. {
  322. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  323. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  324. GPIO_MODE_AF_PP,
  325. GPIO_SPEED_50M,
  326. GPIO_PUPD_PU);
  327. GPIO_Remap_Set(pwm_pin->remap, 1);
  328. }
  329. }
  330. static void hw_pwm_dir_init(int index)
  331. {
  332. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  333. uint32_t port = (uint32_t)pwm_pin->dir_port;
  334. uint16_t pin = pwm_pin->dir_pin;
  335. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  336. {
  337. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  338. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  339. GPIO_MODE_OUT_PP,
  340. GPIO_SPEED_50M,
  341. GPIO_PUPD_PU);
  342. hw_pwm_dir_rw_map[index].idr = (uint16_t *)(port + 16);
  343. hw_pwm_dir_rw_map[index].odr = (uint16_t *)(port + 24);
  344. hw_pwm_dir_rw_map[index].pin = (1 << pin);
  345. hw_pwm_dir_rw_map[index].level = 0;
  346. }
  347. }
  348. //使能IO初化
  349. static void hw_pwm_on_init(int index)
  350. {
  351. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  352. uint32_t port = (uint32_t)pwm_pin->en_port;
  353. uint16_t pin = pwm_pin->en_pin;
  354. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  355. {
  356. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  357. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  358. GPIO_MODE_OUT_PP,
  359. GPIO_SPEED_50M,
  360. GPIO_PUPD_PU);
  361. hw_pwm_en_rw_map[index].idr = (uint16_t *)(port + 16);
  362. hw_pwm_en_rw_map[index].odr = (uint16_t *)(port + 24);
  363. hw_pwm_en_rw_map[index].pin = (1 << pin);
  364. hw_pwm_en_rw_map[index].level = 1;
  365. //HW_PWM_SET_EN(index, 1);//
  366. }
  367. }
  368. static void hw_pwm_clr_init(int index)
  369. {
  370. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  371. uint32_t port = (uint32_t)pwm_pin->clr_port;
  372. uint16_t pin = pwm_pin->clr_pin;
  373. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  374. {
  375. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  376. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  377. GPIO_MODE_OUT_PP,
  378. GPIO_SPEED_50M,
  379. GPIO_PUPD_PU);
  380. hw_pwm_en_rw_map[index].idr = (uint16_t *)(port + 16);
  381. hw_pwm_en_rw_map[index].odr = (uint16_t *)(port + 24);
  382. hw_pwm_en_rw_map[index].pin = (1 << pin);
  383. hw_pwm_en_rw_map[index].level = 1;
  384. }
  385. }
  386. static void hw_pwm_alr_init(int index)
  387. {
  388. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  389. uint32_t port = (uint32_t)pwm_pin->alr_port;
  390. uint16_t pin = pwm_pin->alr_pin;
  391. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  392. {
  393. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  394. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  395. GPIO_MODE_IPU,
  396. GPIO_SPEED_50M,
  397. GPIO_PUPD_PU);
  398. hw_pwm_en_rw_map[index].idr = (uint16_t *)(port + 16);
  399. hw_pwm_en_rw_map[index].odr = (uint16_t *)(port + 24);
  400. hw_pwm_en_rw_map[index].pin = (1 << pin);
  401. hw_pwm_en_rw_map[index].level = 1;
  402. }
  403. }
  404. static void hw_pwm_tmode_init(int index)
  405. {
  406. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  407. uint32_t port = (uint32_t)pwm_pin->tmode_port;
  408. uint16_t pin = pwm_pin->tmode_pin;
  409. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  410. {
  411. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  412. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  413. GPIO_MODE_OUT_PP,
  414. GPIO_SPEED_50M,
  415. GPIO_PUPD_PU);
  416. hw_pwm_en_rw_map[index].idr = (uint16_t *)(port + 16);
  417. hw_pwm_en_rw_map[index].odr = (uint16_t *)(port + 24);
  418. hw_pwm_en_rw_map[index].pin = (1 << pin);
  419. hw_pwm_en_rw_map[index].level = 1;
  420. }
  421. }
  422. static void hw_pwm_tarr_init(int index)
  423. {
  424. hw_pwm_pin_t *pwm_pin = &pin_maps[index];
  425. uint32_t port = (uint32_t)pwm_pin->tarr_port;
  426. uint16_t pin = pwm_pin->tarr_pin;
  427. if (port >= GPIOA_BASE && port <= GPIOG_BASE)
  428. {
  429. RCC->APB2ENR |= 1 << ((port - APB2PERIPH_BASE) / 0x400);
  430. GPIO_Set((GPIO_TypeDef *)port, 1 << pin,
  431. GPIO_MODE_IPU,
  432. GPIO_SPEED_50M,
  433. GPIO_PUPD_PU);
  434. hw_pwm_en_rw_map[index].idr = (uint16_t *)(port + 16);
  435. hw_pwm_en_rw_map[index].odr = (uint16_t *)(port + 24);
  436. hw_pwm_en_rw_map[index].pin = (1 << pin);
  437. hw_pwm_en_rw_map[index].level = 1;
  438. }
  439. }
  440. static void hw_pwm_timer_init(int index,int clk)
  441. {
  442. hw_pwm_t *pwm = &hw_pwm_maps[index];
  443. uint32_t tim_base = (uint32_t)pwm->base;
  444. TIM_TypeDef *timx = pwm->base;
  445. uint16_t psc = 1;
  446. if (tim_base >= TIM2_BASE && tim_base <= TIM5_BASE)
  447. {
  448. uint32_t en_mask = 1 << ((tim_base - APB1PERIPH_BASE) / 0x400);
  449. do {
  450. RCC->APB1ENR |= en_mask;
  451. } while (RCC->APB1ENR & en_mask == 0);
  452. psc = (clk * 1000000) / (pwm->unit_in_hz) - 1;
  453. }
  454. else if (tim_base >= TIM1_BASE && tim_base <= TIM8_BASE)
  455. {
  456. uint32_t en_mask = 1 << ((tim_base - APB2PERIPH_BASE) / 0x400);
  457. do {
  458. RCC->APB2ENR |= en_mask;
  459. } while (RCC->APB2ENR & en_mask == 0);
  460. psc = (clk * 1000000) / (pwm->unit_in_hz) - 1;
  461. }
  462. else
  463. {
  464. do {
  465. //无效的定时器
  466. } while (1);
  467. }
  468. //初始化定时器
  469. timx->PSC = psc; //设置预分频,得到一个基本的时钟单元
  470. timx->ARR = 1000; //没有意义
  471. timx->SMCR = 0;
  472. timx->DIER |= 1 << 0; //使能更新中断
  473. timx->CR1 &= ~(3 << 8); //不分频
  474. timx->CR1 &= ~(3 << 5); //边沿计数模式
  475. timx->CR1 &= ~(1 << 4); //向上计数
  476. timx->CR1 |= (1 << 2); //限制只有上溢中断
  477. if (timx == TIM8 || timx == TIM1)
  478. {
  479. timx->CR1 |= 1 << 7;
  480. timx->BDTR |= 1 << 15; //MOE使能输出
  481. }
  482. //timx->CR1 |= (1 << 7); //ARR寄存器不缓冲
  483. timx->CCR1 = 0;
  484. timx->CCR2 = 0;
  485. timx->CCR3 = 0;
  486. timx->CCR4 = 0;
  487. timx->CR2 = 0;
  488. timx->CCER |= (1 << ((pwm->ch - 1) * 4)); //使能PWM通道
  489. {
  490. timx->CCER &= ~(1 << ((pwm->ch - 1) * 4 + 1)); //高电平有效
  491. }
  492. if (pwm->ch <= 2)
  493. {
  494. uint32_t ccmr = timx->CCMR1;
  495. uint32_t offset = (pwm->ch - 1) * 8;
  496. ccmr &= ~(3 << (offset + 0)); //输出模式
  497. ccmr |= (1 << (offset + 2)); //比较快速使能
  498. ccmr |= (0 << (offset + 3)); //预装载使能禁止
  499. ccmr |= (7 << (offset + 4)); //PWM模式1,
  500. timx->CCMR1 = ccmr;
  501. }
  502. else
  503. {
  504. uint32_t ccmr = timx->CCMR2;
  505. uint32_t offset = (pwm->ch - 3) * 8;
  506. ccmr &= ~(3 << (offset + 0)); //输出模式
  507. ccmr |= (1 << (offset + 2)); //比较快速使能
  508. ccmr |= (0 << (offset + 3)); //预装载使能禁止
  509. ccmr |= (7 << (offset + 4)); //PWM模式1,
  510. timx->CCMR2 = ccmr;
  511. }
  512. //中断向量使能
  513. //MY_NVIC_Init(1, 3, pwm->irqn, 2);
  514. hw_pwm_period_registers[index] = (uint16_t *)(tim_base + 0x2C);
  515. hw_pwm_dutycycle_registers[index] = (uint16_t *)(tim_base + 0x34 + (pwm->ch - 1) * 4);
  516. hw_pwm_cr_registers[index] = (uint16_t *)(tim_base + 0x00);
  517. hw_pwm_egr_registers[index] = (uint16_t *)(tim_base + 0x10);
  518. HW_PWM_SET_PERIOD(index, 1000);
  519. HW_PWM_ON(index);
  520. hw_delay_us(1);
  521. HW_PWM_OFF(index);
  522. Sys_NVIC_Init(1, 1, pwm->irqn, 1);
  523. }
  524. /**
  525. * 初始化硬件
  526. */
  527. void hw_pwm_init(int clk) {
  528. //初始化IO
  529. int index = 0;
  530. while (index < HW_PWM_NUMBER)
  531. {
  532. hw_pwm_pulse_init(index);
  533. hw_pwm_dir_init(index);
  534. hw_pwm_on_init(index);
  535. hw_pwm_clr_init(index);
  536. hw_pwm_alr_init(index);
  537. hw_pwm_tarr_init(index);
  538. hw_pwm_tmode_init(index);
  539. index++;
  540. }
  541. //设置默认参数变量
  542. index = 0;
  543. while (index < HW_PWM_NUMBER)
  544. {
  545. pwm_handle_maps[index] = hw_pwm_default_handler;
  546. pwm_hanlde_parames[index] = (void *)0;
  547. index++;
  548. }
  549. //初始化硬件
  550. index = 0;
  551. while (index < HW_PWM_NUMBER)
  552. {
  553. hw_pwm_timer_init(index, clk);
  554. index++;
  555. }
  556. }
  557. /**
  558. * 关闭所有的脉冲输出
  559. *
  560. * @author lxz (2019/5/30/周四)
  561. */
  562. void hw_pwm_close_all(void) {
  563. int index = 0;
  564. while (index < HW_PWM_NUMBER) {
  565. HW_PWM_OFF(index);
  566. index++;
  567. }
  568. }