api_rt_pwm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include "api_rt_pwm.h"
  2. #include "api_rt_dbg.h"
  3. #include "board_config.h"
  4. #include "gd32f30x.h"
  5. #include <stdint.h>
  6. #include "api_rt_adc.h"
  7. ApiRtPwm_Handle Pwms[1];
  8. /* ========================================================================== */
  9. /* ============================ Api RT Functions ============================ */
  10. /* ========================================================================== */
  11. void iRtPwm_Init()
  12. {
  13. Pwms[0].PwmBase = TIMER0;
  14. for (int i = 0; i < 6; i++)
  15. {
  16. Pwms[0].CompareValues[i] = HW_INIT_HHPWM_PERIOD;
  17. Pwms[0].ActiveCompareValues[i] = HW_INIT_HHPWM_PERIOD;
  18. }
  19. Pwms[0].OutputEnable = 0;
  20. #if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
  21. Pwms[0].SyncSamplingEnable = 1;
  22. Pwms[0].SyncSamplingUpTickCount = 1;
  23. Pwms[0].SyncSamplingUpTick[0] = HW_INIT_HHPWM_PERIOD;
  24. Pwms[0].SyncSamplingDownTickCount = 1;
  25. Pwms[0].SyncSamplingDownTick[0] = HW_SAMPLE_BEFORE_UPDATE_CNTS;
  26. #endif
  27. }
  28. void iRtPwm_ActivateCompareValues(uint8_t devIndex)
  29. {
  30. ASSERT_LESS(devIndex, 1);
  31. for (int i = 0; i < 6; i++)
  32. {
  33. Pwms[devIndex].ActiveCompareValues[i] = Pwms[devIndex].CompareValues[i];
  34. }
  35. }
  36. /* ========================================================================== */
  37. /* ============================== API Functions ============================= */
  38. /* ========================================================================== */
  39. uint32_t iPwm_GetClock(uint8_t devIndex)
  40. {
  41. return HW_TIM_CLOCK_HZ;
  42. }
  43. void iPwm_EnableCount(uint8_t devIndex)
  44. {
  45. uint32_t base = Pwms[devIndex].PwmBase;
  46. TIMER_CTL0(base) |= (uint32_t)BIT(0);
  47. }
  48. void iPwm_DisableCount(uint8_t devIndex)
  49. {
  50. uint32_t base = Pwms[devIndex].PwmBase;
  51. TIMER_CTL0(base) &= ~(uint32_t)BIT(0);
  52. }
  53. uint32_t iPwm_GetCountMax(uint8_t devIndex)
  54. {
  55. uint32_t base = Pwms[devIndex].PwmBase;
  56. uint32_t count_value = 0U;
  57. count_value = TIMER_CAR(base);
  58. return count_value;
  59. }
  60. void iPwm_SetCountMax(uint8_t devIndex, uint32_t countMax)
  61. {
  62. uint32_t base = Pwms[devIndex].PwmBase;
  63. TIMER_CAR(base)= countMax;
  64. }
  65. uint32_t iPwm_GetCount(uint8_t devIndex)
  66. {
  67. uint32_t base = Pwms[devIndex].PwmBase;
  68. uint32_t count_value = 0U;
  69. count_value = TIMER_CNT(base);
  70. return count_value;
  71. }
  72. void iPwm_SetCount(uint8_t devIndex, uint32_t count)
  73. {
  74. uint32_t base = Pwms[devIndex].PwmBase;
  75. TIMER_CNT(base)= count;
  76. }
  77. ApiPwm_CountMode iPwm_GetCountMode(uint8_t devIndex)
  78. {
  79. uint32_t base = Pwms[devIndex].PwmBase;
  80. ApiPwm_CountMode mode = ApiPwm_CountUp;
  81. if ((TIMER_CTL0(base) & (uint16_t)0x0060) == (uint16_t)0x0000)
  82. {
  83. if ((TIMER_CTL0(base) & (uint16_t)0x0010) != 0)
  84. {
  85. mode = ApiPwm_CountUp;
  86. }
  87. #if API_FUNCTION_PWM_COUNTDOWN == SUPPORT
  88. else
  89. {
  90. mode = ApiPwm_CountDown;
  91. }
  92. #endif
  93. }
  94. else
  95. {
  96. mode = ApiPwm_CountUpDown;
  97. }
  98. return mode;
  99. }
  100. void iPwm_SetCountMode(uint8_t devIndex, ApiPwm_CountMode mode)
  101. {
  102. uint32_t base = Pwms[devIndex].PwmBase;
  103. switch (mode)
  104. {
  105. case ApiPwm_CountUp:
  106. TIMER_CTL0(base) &= ~((uint16_t)0x0060);
  107. TIMER_CTL0(base) &= ~(uint16_t)0x0010;
  108. break;
  109. #if FUNCTION_PWM_COUNTDOWN == SUPPORT
  110. case ApiPwm_CountDown:
  111. TIMER_CTL0(base) &= ~((uint16_t)0x0060);
  112. TIMER_CTL0(base) |= (uint16_t)0x0010;
  113. break;
  114. #endif
  115. case ApiPwm_CountUpDown:
  116. TIMER_CTL0(base) |= ((uint16_t)0x0060);
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. ApiPwm_CompareMode iPwm_GetCompareMode(uint8_t devIndex, uint8_t channelIndex)
  123. {
  124. uint32_t base = Pwms[devIndex].PwmBase;
  125. uint8_t conf = 0;
  126. switch (channelIndex)
  127. {
  128. case 0:
  129. conf = TIMER_CHCTL0(base) & (uint32_t)0x000000F0UL;
  130. break;
  131. case 1:
  132. conf = (TIMER_CHCTL0(base) & (uint32_t)0x0000F000UL) >> 16;
  133. break;
  134. case 2:
  135. conf = TIMER_CHCTL1(base) & (uint32_t)0x000000F0UL;
  136. break;
  137. case 3:
  138. conf = (TIMER_CHCTL1(base) & (uint32_t)0x0000F000UL) >> 16;
  139. break;
  140. default:
  141. break;
  142. }
  143. ApiPwm_CompareMode mode = ApiPwm_NoAction;
  144. switch (conf >> 4)
  145. {
  146. case 0x00:
  147. mode = ApiPwm_NoAction;
  148. break;
  149. case 0x01:
  150. mode = ApiPwm_UpMatchSet;
  151. #if FUNCTION_PWM_COUNTDOWN
  152. if (iPwm_GetCountMode(devIndex) == Pwm_CountDown)
  153. mode = ApiPwm_DownMatchSet;
  154. #endif
  155. break;
  156. case 0x02:
  157. mode = ApiPwm_UpMatchClear;
  158. #if FUNCTION_PWM_COUNTDOWN
  159. if (iPwm_GetCountMode(devIndex) == Pwm_CountDown)
  160. mode = ApiPwm_DownMatchClear;
  161. #endif
  162. break;
  163. case 0x03:
  164. mode = ApiPwm_UpMatchToggle;
  165. #if FUNCTION_PWM_COUNTDOWN
  166. if (iPwm_GetCountMode(devIndex) == Pwm_CountDown)
  167. mode = ApiPwm_DownMatchToggle;
  168. #endif
  169. break;
  170. case 0x04:
  171. mode = ApiPwm_ForceClear;
  172. break;
  173. case 0x05:
  174. mode = ApiPwm_ForceSet;
  175. break;
  176. case 0x06:
  177. mode = ApiPwm_HigherClear;
  178. break;
  179. case 0x07:
  180. mode = ApiPwm_HigherSet;
  181. break;
  182. default:
  183. mode = ApiPwm_NoAction;
  184. break;
  185. }
  186. return mode;
  187. }
  188. void iPwm_SetCompareMode(uint8_t devIndex, uint8_t channelIndex, ApiPwm_CompareMode mode)
  189. {
  190. uint32_t base = Pwms[devIndex].PwmBase;
  191. uint8_t conf = 0;
  192. switch (mode)
  193. {
  194. case ApiPwm_NoAction:
  195. conf = 0x00;
  196. break;
  197. case ApiPwm_UpMatchSet:
  198. conf = 0x01;
  199. break;
  200. case ApiPwm_UpMatchClear:
  201. conf = 0x02;
  202. break;
  203. case ApiPwm_UpMatchToggle:
  204. conf = 0x03;
  205. break;
  206. case ApiPwm_ForceClear:
  207. conf = 0x04;
  208. break;
  209. case ApiPwm_ForceSet:
  210. conf = 0x05;
  211. break;
  212. case ApiPwm_HigherClear:
  213. conf = 0x06;
  214. break;
  215. case ApiPwm_HigherSet:
  216. conf = 0x07;
  217. break;
  218. default:
  219. break;
  220. }
  221. #if API_FUNCTION_PWM_COUNTDOWN
  222. if (iPwm_GetCountMode(devIndex) == ApiPwm_CountDown)
  223. {
  224. switch (mode)
  225. {
  226. case ApiPwm_DownMatchSet:
  227. conf = 0x01;
  228. break;
  229. case ApiPwm_DownMatchClear:
  230. conf = 0x02;
  231. break;
  232. case ApiPwm_DownMatchToggle:
  233. conf = 0x03;
  234. break;
  235. }
  236. }
  237. #endif
  238. switch (channelIndex)
  239. {
  240. case 0:
  241. TIMER_CHCTL0(base) &= ~((uint32_t)0x000000F0UL);
  242. TIMER_CHCTL0(base) |= (((uint32_t)conf) << 4);
  243. break;
  244. case 1:
  245. TIMER_CHCTL0(base) &= ~((uint32_t)0x0000F000UL);
  246. TIMER_CHCTL0(base) |= (((uint32_t)conf) << (4 + 8));
  247. break;
  248. case 2:
  249. TIMER_CHCTL0(base) &= ~((uint32_t)0x000000F0UL);
  250. TIMER_CHCTL0(base) |= (((uint32_t)conf) << 4);
  251. break;
  252. case 3:
  253. TIMER_CHCTL0(base) &= ~((uint32_t)0x0000F000UL);
  254. TIMER_CHCTL0(base) |= (((uint32_t)conf) << (4 + 8));
  255. break;
  256. }
  257. }
  258. uint32_t iPwm_GetCompareValue(uint8_t devIndex, uint8_t channelIndex)
  259. {
  260. uint32_t base = Pwms[devIndex].PwmBase;
  261. uint32_t value = 0;
  262. switch (channelIndex)
  263. {
  264. case 0:
  265. value = TIMER_CH0CV(base);
  266. break;
  267. case 1:
  268. value = TIMER_CH1CV(base);
  269. break;
  270. case 2:
  271. value = TIMER_CH2CV(base);
  272. break;
  273. case 3:
  274. value = TIMER_CH3CV(base);
  275. break;
  276. default:
  277. break;
  278. }
  279. return value;
  280. }
  281. void iPwm_SetCompareValue(uint8_t devIndex, uint8_t channelIndex, uint32_t value)
  282. {
  283. ASSERT_LESS(devIndex, 1);
  284. ASSERT_LESS(channelIndex, 3);
  285. Pwms[devIndex].CompareValues[channelIndex] = value;
  286. }
  287. void iPwm_SetCompareValueDelay(uint8_t devIndex, uint8_t channelIndex, uint32_t value)
  288. {
  289. ASSERT_LESS(devIndex, 1);
  290. ASSERT_LESS(channelIndex, 3);
  291. Pwms[devIndex].CompareValues[channelIndex + 3] = value;
  292. }
  293. void iPwm_SetCompareGroupValues16(uint8_t devIndex, uint16_t* values)
  294. {
  295. for (int i = 0; i < 6; i++)
  296. {
  297. Pwms[devIndex].CompareValues[i] = values[i];
  298. }
  299. }
  300. void iPwm_SetCompareValueImmediate(uint8_t devIndex, uint8_t channelIndex, uint32_t value)
  301. {
  302. ASSERT_LESS(devIndex, 1);
  303. ASSERT_LESS(channelIndex, 4);
  304. uint32_t base = Pwms[devIndex].PwmBase;
  305. switch (channelIndex)
  306. {
  307. case 0:
  308. TIMER_CH0CV(base) = value;
  309. break;
  310. case 1:
  311. TIMER_CH1CV(base) = value;
  312. break;
  313. case 2:
  314. TIMER_CH2CV(base) = value;
  315. break;
  316. case 3:
  317. TIMER_CH3CV(base) = value;
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. int8_t iPwm_GetEnableState(uint8_t devIndex)
  324. {
  325. ASSERT_LESS(devIndex, 1);
  326. return Pwms[devIndex].OutputEnable;
  327. }
  328. void iPwm_EnableOutput(uint8_t devIndex)
  329. {
  330. ASSERT_LESS(devIndex, 1);
  331. uint32_t base = Pwms[devIndex].PwmBase;
  332. TIMER_CCHP(base) |= (uint32_t)0x00008000UL;
  333. Pwms[devIndex].OutputEnable = 1;
  334. }
  335. void iPwm_DisableOutput(uint8_t devIndex)
  336. {
  337. ASSERT_LESS(devIndex, 1);
  338. uint32_t base = Pwms[devIndex].PwmBase;
  339. TIMER_CCHP(base) &= ~((uint32_t)0x00008000UL);
  340. Pwms[devIndex].OutputEnable = 0;
  341. }
  342. // // int8_t Pwm_GetChannelEnableState(uint8_t devIndex, uint8_t channelIndex)
  343. // // {
  344. // // }
  345. // // void Pwm_EnableChannelOutput(uint8_t devIndex, uint8_t channelIndex)
  346. // // {
  347. // // ASSERT_LESS(devIndex, 1);
  348. // // ASSERT_LESS(channelIndex, 4);
  349. // // TIM_TypeDef *base = Pwms[devIndex].PwmBase;
  350. // // if (channelIndex < 64)
  351. // // {
  352. // // base->CCER |= ((uint32_t)0x01) << (channelIndex * 4);
  353. // // }
  354. // // else
  355. // // {
  356. // // base->CCER |= ((uint32_t)0x04) << ((channelIndex - 64) * 4);
  357. // // }
  358. // // }
  359. // // void Pwm_DisableChannelOutput(uint8_t devIndex, uint8_t channelIndex)
  360. // // {
  361. // // ASSERT_LESS(devIndex, 1);
  362. // // ASSERT_LESS(channelIndex, 4);
  363. // // TIM_TypeDef *base = Pwms[devIndex].PwmBase;
  364. // // if (channelIndex < 64)
  365. // // {
  366. // // base->CCER &= ~(((uint32_t)0x01) << (channelIndex * 4));
  367. // // }
  368. // // else
  369. // // {
  370. // // base->CCER &= ~(((uint32_t)0x04) << ((channelIndex - 64) * 4));
  371. // // }
  372. // // }
  373. //void iPwm_EnableDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt)
  374. //{
  375. // ASSERT_LESS(devIndex, 1);
  376. //
  377. // switch (interrupt)
  378. // {
  379. // case ApiPwm_CountZeroInt:
  380. // Pwms[devIndex].CountZeroISR.Enable = 1;
  381. // break;
  382. // case ApiPwm_CountMaxInt:
  383. // Pwms[devIndex].CountMaxISR.Enable = 1;
  384. // break;
  385. // case ApiPwm_BreakInt:
  386. // Pwms[devIndex].BreakISR.Enable = 1;
  387. // break;
  388. // default:
  389. // break;
  390. // }
  391. //}
  392. //
  393. //void iPwm_DisableDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt)
  394. //{
  395. // ASSERT_LESS(devIndex, 1);
  396. //
  397. // switch (interrupt)
  398. // {
  399. // case ApiPwm_CountZeroInt:
  400. // Pwms[devIndex].CountZeroISR.Enable = 0;
  401. // break;
  402. // case ApiPwm_CountMaxInt:
  403. // Pwms[devIndex].CountMaxISR.Enable = 0;
  404. // break;
  405. // case ApiPwm_BreakInt:
  406. // Pwms[devIndex].BreakISR.Enable = 0;
  407. // break;
  408. // default:
  409. // break;
  410. // }
  411. //}
  412. //
  413. //void iPwm_EnableChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_ChannelInterrupt interrupt)
  414. //{
  415. // ASSERT_LESS(devIndex, 1);
  416. // ASSERT_LESS(channelIndex, 4);
  417. //
  418. // Pwms[devIndex].ChannelISR[channelIndex].Enable = 1;
  419. //}
  420. //
  421. //void iPwm_DisableChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_ChannelInterrupt interrupt)
  422. //{
  423. // ASSERT_LESS(devIndex, 1);
  424. // ASSERT_LESS(channelIndex, 4);
  425. //
  426. // Pwms[devIndex].ChannelISR[channelIndex].Enable = 0;
  427. //}
  428. //
  429. //void iPwm_BindDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt, void (*action)())
  430. //{
  431. // ASSERT_LESS(devIndex, 1);
  432. //
  433. // switch (interrupt)
  434. // {
  435. // case ApiPwm_CountZeroInt:
  436. // Pwms[devIndex].CountZeroISR.Action = action;
  437. // break;
  438. // case ApiPwm_CountMaxInt:
  439. // Pwms[devIndex].CountMaxISR.Action = action;
  440. // break;
  441. // case ApiPwm_BreakInt:
  442. // Pwms[devIndex].BreakISR.Action = action;
  443. // break;
  444. // default:
  445. // break;
  446. // }
  447. //}
  448. //
  449. //void iPwm_BindChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_DeviceInterrupt interrupt, void (*action)())
  450. //{
  451. // ASSERT_LESS(devIndex, 1);
  452. // ASSERT_LESS(channelIndex, 4);
  453. //
  454. // Pwms[devIndex].ChannelISR[channelIndex].Action = action;
  455. //}
  456. //
  457. //int8_t iPwm_GetBreakState(uint8_t devIndex)
  458. //{
  459. // ASSERT_LESS(devIndex, 1);
  460. //
  461. // TIM_TypeDef *base = Pwms[devIndex].PwmBase;
  462. //
  463. // return (base->SR & ((uint32_t)0x00000080UL)) ? 1 : 0;
  464. //}
  465. //
  466. //void iPwm_ClearBreak(uint8_t devIndex)
  467. //{
  468. // ASSERT_LESS(devIndex, 1);
  469. //
  470. // TIM_TypeDef *base = Pwms[devIndex].PwmBase;
  471. //
  472. // base->SR &= ~((uint32_t)0x00000080UL);
  473. //}
  474. //
  475. #if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
  476. void iPwm_EnableSyncMultiSampling(uint8_t devIndex)
  477. {
  478. Pwms[devIndex].SyncSamplingEnable = 1;
  479. }
  480. void iPwm_DisableSyncMultiSampling(uint8_t devIndex)
  481. {
  482. Pwms[devIndex].SyncSamplingEnable = 0;
  483. Pwms[devIndex].SyncSamplingUpTickCount = 1;
  484. Pwms[devIndex].SyncSamplingUpTick[0] = TIMER_CAR(Pwms[devIndex].PwmBase)>> 1;
  485. Pwms[devIndex].SyncSamplingDownTickCount = 1;
  486. Pwms[devIndex].SyncSamplingDownTick[0] = TIMER_CAR(Pwms[devIndex].PwmBase)>> 1;
  487. }
  488. void iPwm_SyncMultiSamplingCountUp(uint8_t devIndex, uint32_t samplingTicks[], uint8_t samplingCounts)
  489. {
  490. if (Pwms[devIndex].SyncSamplingEnable == 1)
  491. {
  492. Pwms[devIndex].SyncSamplingUpTickCount = samplingCounts;
  493. for (int i = 0; i < samplingCounts; i++)
  494. {
  495. Pwms[devIndex].SyncSamplingUpTick[i] = samplingTicks[i];
  496. }
  497. }
  498. }
  499. void iPwm_SyncMultiSamplingCountDown(uint8_t devIndex, uint32_t samplingTicks[], uint8_t samplingCounts)
  500. {
  501. if (Pwms[devIndex].SyncSamplingEnable == 1)
  502. {
  503. Pwms[devIndex].SyncSamplingDownTickCount = samplingCounts;
  504. for (int i = 0; i < samplingCounts; i++)
  505. {
  506. Pwms[devIndex].SyncSamplingDownTick[i] = samplingTicks[i];
  507. }
  508. }
  509. }
  510. #endif