HW_PWM_ATY.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * @file HW_PWM_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar definition of PWM for users
  20. *
  21. * @version
  22. * - 1_01_220602 > ATY
  23. * -# Preliminary version, first Release
  24. * - Undone
  25. ********************************************************************************
  26. */
  27. #ifndef __HW_PWM_ATY_C
  28. #define __HW_PWM_ATY_C
  29. #include "HW_PWM_ATY.h"
  30. /******************************* For user *************************************/
  31. #if defined(__STC8G_ATY)
  32. /**
  33. * @brief set pulse period with specified duty with PCA
  34. * @param periodS pulse period, us
  35. * @param dutyS pulse duty, 0-100.0 @ %
  36. * @param channelS channel to start or to change param
  37. * @todo any freq with ITM1
  38. */
  39. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  40. {
  41. // P_SW1 |= 0x10; // PCA1 P36
  42. // P3M0 = 0x40; // P36 PP out
  43. CCON = 0x00;
  44. // PWM freq = SYS freq / n / 256
  45. if(periodS <= 10)
  46. CMOD = 0x08; // PCA clk = sys clk, 32/3us 93.75KHz @ 24MHz
  47. else if(periodS <= 21 && periodS > 10)
  48. CMOD = 0x02; // PCA clk = sys clk / 2,
  49. else if(periodS <= 42 && periodS > 21)
  50. CMOD = 0x0A; // PCA clk = sys clk / 4
  51. else if(periodS <= 64 && periodS > 42)
  52. CMOD = 0x0C; // PCA clk = sys clk / 6
  53. else if(periodS <= 85 && periodS > 64)
  54. CMOD = 0x0E; // PCA clk = sys clk / 8
  55. else if(periodS > 85)//periodS <= 128 && periodS > 85)
  56. CMOD = 0x00; // PCA clk = sys clk / 12
  57. else
  58. CMOD = 0x04; // PCA clk = TIM1 IT
  59. CL = 0x00; // init counter value
  60. CH = 0x00;
  61. if(channelS == 1)
  62. {
  63. CCAPM1 = 0x42; // PCA at PWM mode
  64. // PCA_PWM1 = 0x80; // 6bit PWM
  65. // PCA_PWM1 = 0x40; // 7bit PWM
  66. PCA_PWM1 = 0x00; // 8bit PWM
  67. // PCA_PWM1 = 0xC0; // 10bit PWM
  68. CCAP1L = (uint16_t)(0xFFFF * (float)((100 - dutyS) / 100.0));
  69. CCAP1H = (uint16_t)(0xFFFF * (float)((100 - dutyS) / 100.0)) >> 8;
  70. }
  71. CR = 1; // start PCA
  72. }
  73. /**
  74. * @brief stop PWM output with specified level
  75. * @param level IO level after stop PWM
  76. * @param channelS channel to start or to change param
  77. * @note Undone, not real stop
  78. */
  79. void PWM_Stop(uint8_t level, uint8_t channelS)
  80. {
  81. if(level)
  82. PWM_Start(1000, level * 1000, channelS);
  83. else
  84. PWM_Start(1000, 0, channelS);
  85. }
  86. /**
  87. * @brief stop PWM output with specified level with PCA
  88. * @param level IO level after stop PWM
  89. * @note voltage lower than write pin
  90. */
  91. void PWM_Stop(uint8_t level)
  92. {
  93. if(level)
  94. {
  95. PCA_PWM1 &= 0xC0;
  96. CCAP1H = 0x00;
  97. }
  98. else
  99. {
  100. PCA_PWM1 |= 0x3F;
  101. CCAP1H = 0xFF;
  102. }
  103. CR = 0;
  104. }
  105. /**
  106. * @brief set pulse period with specified duty with PCA
  107. * @param periodS pulse period, us
  108. * @param dutyS pulse duty, 0-100.0 @ %
  109. * @param channelS channel to start or to change param
  110. * @todo any freq with ITM1
  111. */
  112. void PWM_StartPulse(uint32_t periodS, uint8_t channelS)
  113. {
  114. CCON = 0x00;
  115. CMOD = 0x08; // PCA clk = sys clk, 32/3us 93.75KHz @ 24MHz
  116. CL = 0x00; // init counter value
  117. CH = 0x00;
  118. if(channelS == 1)
  119. {
  120. CCAPM1 = 0x4D; // PCA at fast pulse mode, enbale IT
  121. CCAP1L = periodS;
  122. CCAP1H = periodS >> 8;
  123. }
  124. CR = 1; // start PCA
  125. EA = 1;
  126. }
  127. void PCA_Isr() interrupt 7
  128. {
  129. CCF1 = 0;
  130. CL = 0x00;
  131. CH = 0x00;
  132. }
  133. #elif defined(__STC51_ATY)
  134. /**
  135. * @brief set pulse period with specified duty
  136. * @param periodS pulse period, us
  137. * @param dutyS pulse duty, 0-100.0 @ %
  138. * @param channelS channel to start or to change param
  139. * @note Undone
  140. */
  141. void PWM_Start(uint32_t periodS, float dutyS, uint8_t channelS)
  142. {
  143. if(dutyS < 0)
  144. dutyS = 0;
  145. P_SW2 |= 0x80;
  146. if(channelS == PWM_CHANNEL_1P || channelS == PWM_CHANNEL_1N){
  147. PWMA_PS = (PWMA_PS & 0xFC) | (PWM_PIN_POS_1 << 0); //
  148. PWMA_CCER1 &= 0xF0;
  149. PWMA_CCMR1 = 0x60;
  150. PWMA_CCER1 |= 0x05;
  151. PWMA_CCR1 = dutyS / 100.0 * periodS;
  152. }
  153. else if(channelS == PWM_CHANNEL_2P || channelS == PWM_CHANNEL_2N){
  154. PWMA_PS = (PWMA_PS & 0xF3) | (PWM_PIN_POS_1 << 2); //
  155. PWMA_CCER1 &= 0x0F;
  156. PWMA_CCMR2 = 0x60;
  157. PWMA_CCER1 |= 0x50;
  158. PWMA_CCR2 = dutyS / 100.0 * periodS;
  159. }
  160. else if(channelS == PWM_CHANNEL_3P || channelS == PWM_CHANNEL_3N){
  161. PWMA_PS = (PWMA_PS & 0xCF) | (PWM_PIN_POS_1 << 4); // PWM3N_2 Heat
  162. PWMA_CCER2 &= 0xF0;
  163. PWMA_CCMR3 = 0x60;
  164. PWMA_CCER2 |= 0x05;
  165. PWMA_CCR3 = dutyS / 100.0 * periodS;
  166. }
  167. else if(channelS == PWM_CHANNEL_4P || channelS == PWM_CHANNEL_4N){
  168. PWMA_PS = (PWMA_PS & 0x3F) | (PWM_PIN_POS_1 << 6); //
  169. PWMA_CCER2 &= 0x0F;
  170. PWMA_CCMR4 = 0x60;
  171. PWMA_CCER2 |= 0x50;
  172. PWMA_CCR4 = dutyS / 100.0 * periodS;
  173. }
  174. else if(channelS == PWM_CHANNEL_5P){
  175. PWMB_PS = (PWMB_PS & 0xFC) | (PWM_PIN_POS_1 << 0); //
  176. PWMB_CCER1 &= 0xF0;
  177. PWMB_CCMR1 = 0x60;
  178. PWMB_CCER1 |= 0x01;
  179. PWMB_CCR5 = dutyS / 100.0 * periodS;
  180. }
  181. else if(channelS == PWM_CHANNEL_6P){
  182. PWMB_PS = (PWMB_PS & 0xF3) | (PWM_PIN_POS_1 << 2); // PWM6_2 DAC_PWM P54
  183. PWMB_CCER1 &= 0x0F; // Set 0 of CCERx before write CCMRx
  184. PWMB_CCMR2 = 0x60; // Set CC6 PWMB output mode
  185. PWMB_CCER1 |= 0x10; // Enable CC6 channel
  186. PWMB_CCR6 = dutyS / 100.0 * periodS; // Set dutys time
  187. }
  188. else if(channelS == PWM_CHANNEL_7P){
  189. PWMB_PS = (PWMB_PS & 0xCF) | (PWM_PIN_POS_0 << 4); //
  190. PWMB_CCER2 &= 0xF0;
  191. PWMB_CCMR3 = 0x60;
  192. PWMB_CCER2 |= 0x01;
  193. PWMB_CCR7 = dutyS / 100.0 * periodS;
  194. }
  195. else if(channelS == PWM_CHANNEL_8P){
  196. PWMB_PS = (PWMB_PS & 0x3F) | (PWM_PIN_POS_0 << 6); // PWM8 23 TMC_STEP
  197. PWMB_CCER2 &= 0x0F;
  198. PWMB_CCMR4 = 0x60;
  199. PWMB_CCER2 |= 0x10;
  200. PWMB_CCR8 = dutyS / 100.0 * periodS;
  201. }
  202. if((channelS & 0xF0) == 0xF0){ // channel 5-8
  203. PWMB_ARR = periodS; // Set period time
  204. PWMB_ENO |= ((uint8_t)1 << ((channelS & 0x0F) * 2)); // Enable channel output
  205. PWMB_BKR = 0x80; // Enable main output
  206. PWMB_CR1 = 0x01; // Start counter
  207. }
  208. else{ // channel 1-4
  209. PWMA_ARR = periodS;
  210. PWMA_ENO |= channelS;
  211. PWMA_BKR = 0x80;
  212. PWMA_CR1 = 0x01;
  213. }
  214. P_SW2 &= 0x7F;
  215. }
  216. /**
  217. * @brief stop PWM output with specified level
  218. * @param level IO level after stop PWM
  219. * @param channelS channel to start or to change param
  220. * @note Undone, not real stop
  221. */
  222. void PWM_Stop(uint8_t level, uint8_t channelS)
  223. {
  224. PWM_Start(1000, level * 1000, channelS);
  225. }
  226. #elif defined(__STM32_HAL_ATY)
  227. #include "tim.h"
  228. /**
  229. * @brief set pulse period with specified duty
  230. * @param periodS pulse period, us
  231. * @param dutyS pulse duty, 0-100.0 @ %
  232. * @param channelS channel to start or to change param
  233. * @note Undone
  234. */
  235. void PWM_Start(uint32_t periodS, float dutyS, uint8_t channelS)
  236. {
  237. #if defined (PWM_T01C1)
  238. if(channelS == PWM_T01C1){
  239. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
  240. // TIM1->CNT = 0;
  241. // TIM1->ARR = periodS;
  242. // TIM1->CCR2 = (uint32_t)(periodS * dutyS);
  243. // TIM1->EGR = TIM_EGR_UG;
  244. HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  245. __HAL_TIM_SET_AUTORELOAD(&htim1, periodS);
  246. __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (uint32_t)(periodS / 100 * dutyS));
  247. }
  248. #endif
  249. #if defined (PWM_T02C2)
  250. if(channelS == PWM_T02C2){
  251. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2);
  252. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  253. __HAL_TIM_SET_AUTORELOAD(&htim2, periodS);
  254. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, (uint32_t)(periodS / 100 * dutyS));
  255. }
  256. #endif
  257. #if defined (PWM_T03C2)
  258. if(channelS == PWM_T03C2){
  259. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
  260. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
  261. __HAL_TIM_SET_AUTORELOAD(&htim3, periodS);
  262. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, (uint32_t)(periodS / 100 * dutyS));
  263. }
  264. #endif
  265. }
  266. /**
  267. * @brief stop PWM output with specified level
  268. * @param level IO level after stop PWM
  269. * @param channelS channel to start or to change param
  270. * @note Undone, not real stop
  271. */
  272. void PWM_Stop(uint8_t level, uint8_t channelS)
  273. {
  274. #ifdef PWM_T01C1
  275. if(channelS == PWM_T01C1){
  276. if(level)
  277. __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, __HAL_TIM_GET_AUTORELOAD(&htim1));
  278. else
  279. __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
  280. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
  281. HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  282. }
  283. #endif
  284. #ifdef PWM_T02C2
  285. if(level)
  286. {
  287. TIM2->ARR = 0;
  288. TIM2->CCR4 = 0;
  289. TIM2->EGR = TIM_EGR_UG;
  290. }
  291. else
  292. {
  293. TIM2->ARR = 0;
  294. TIM2->CCR4 = 1;
  295. TIM2->EGR = TIM_EGR_UG;
  296. }
  297. if(channelS == PWM_T02C2)
  298. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  299. #endif
  300. #ifdef PWM_T03C2
  301. if(level)
  302. {
  303. TIM3->ARR = 0;
  304. TIM3->CCR4 = 0;
  305. TIM3->EGR = TIM_EGR_UG;
  306. }
  307. else
  308. {
  309. TIM3->ARR = 0;
  310. TIM3->CCR4 = 1;
  311. TIM3->EGR = TIM_EGR_UG;
  312. }
  313. if(channelS == PWM_T03C2)
  314. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
  315. #endif
  316. }
  317. // for generate spefied pulse
  318. void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  319. {
  320. }
  321. #elif defined(__ESP8266_RTOS_ATY)
  322. #include "driver/pwm.h"
  323. #define PWM_CHANNEL_FAN 0
  324. #define PWM_CHANNEL_COLD 1
  325. #define PWM_CHANNEL_WARM 2
  326. const uint32_t* channelPin = {4, 2, 15};
  327. uint8_t* dutiesPercent = {50, 50, 50};
  328. /**
  329. * @see https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/api-reference/peripherals/pwm.html
  330. *
  331. */
  332. void PWM_Init(void)
  333. {
  334. uint32_t period = 1000;
  335. uint32_t duties[3];
  336. duties[0] = dutiesPercent[0] / 100.0 * period;
  337. duties[1] = dutiesPercent[1] / 100.0 * period;
  338. duties[2] = dutiesPercent[2] / 100.0 * period;
  339. pwm_init(period, duties, 3, channelPin);
  340. }
  341. /**
  342. * @brief set pulse period with specified duty
  343. * @param periodS pulse period, us
  344. * @param dutyS pulse duty, 0-100.0 @ %
  345. * @param channelS channel to start or to change param
  346. * @todo not tested
  347. */
  348. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  349. {
  350. extern pwm_obj_t* pwm_obj;
  351. if(periodS = !pwm_obj->period)
  352. pwm_set_period(periodS);
  353. pwm_set_duty(channelS, (uint32_t)((float)(dutyS / 100.0) * periodS));
  354. // pwm_set_period_duties(period, allDuties);
  355. pwm_start();
  356. }
  357. /**
  358. * @brief stop PWM output with specified level
  359. * @param level IO level after stop PWM
  360. * @todo not tested
  361. */
  362. void PWM_Stop(uint8_t level)
  363. {
  364. if(level)
  365. pwm_stop(0x00);
  366. else
  367. pwm_stop(0xFF);
  368. pwm_deinit();
  369. }
  370. #endif /* PLATFORM */
  371. #ifdef __DEBUG_HW_PWM_ATY
  372. void PWM_Test(uint8_t testType)
  373. {
  374. if(testType == 11)
  375. {
  376. static dutyCount = 0;
  377. dutyCount++;
  378. UartSendStr("\r\n");
  379. UartSendByte(dutyCount / 100 + '0');
  380. UartSendByte(((uint8_t)dutyCount % 100) / 10 + '0');
  381. UartSendByte((uint8_t)dutyCount % 10 + '0');
  382. UartSendStr("\r\n");
  383. if(dutyCount <= 100)
  384. PWM_Start(10, dutyCount, 1);
  385. else if(dutyCount > 100 && dutyCount < 200)
  386. PWM_Start(10, 200 - dutyCount, 1);
  387. else
  388. dutyCount = 0;
  389. }
  390. else if(testType == 21)
  391. {
  392. static uint8_t tempa = 10;
  393. PWM_Start(tempa, 50, 1);
  394. if(tempa == 10)
  395. tempa = 21;
  396. else if(tempa == 21)
  397. tempa = 42;
  398. else if(tempa == 42)
  399. tempa = 64;
  400. else if(tempa == 64)
  401. tempa = 85;
  402. else if(tempa == 85)
  403. tempa = 128;
  404. else
  405. tempa = 10;
  406. }
  407. else if(testType == 31)
  408. {
  409. static uint8_t stopLevel = 0;
  410. PWM_Start(10, 50, 1);
  411. stopLevel = !stopLevel;
  412. UartSendByte(stopLevel + '0');
  413. PWM_Stop(stopLevel);
  414. // P36 = stopLevel;
  415. }
  416. else if(testType == 22)
  417. {
  418. static uint32_t freqCount = 0xFFFF;
  419. P37 = 0;
  420. P35 = 0;
  421. if(freqCount > 100)
  422. freqCount -= 100;
  423. else if(freqCount > 0 && freqCount <= 100)
  424. freqCount -= 1;
  425. else if(freqCount == 0)
  426. freqCount = 0xFFFF;
  427. PWM_StartPulse(freqCount, 1);
  428. UartSendStr("\r\n");
  429. UartSendByte(freqCount / 10000 + '0');
  430. UartSendByte(((uint16_t)freqCount % 10000) / 1000 + '0');
  431. UartSendByte(((uint16_t)freqCount % 1000) / 100 + '0');
  432. UartSendByte(((uint16_t)freqCount % 100) / 10 + '0');
  433. UartSendByte((uint16_t)freqCount % 10 + '0');
  434. }
  435. }
  436. #endif /* __DEBUG_HW_PWM_ATY */
  437. /******************************************************************************/
  438. /**
  439. * @brief set pulse frequence with half dutycycle
  440. * @param periodS pulse frequence
  441. */
  442. void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  443. {
  444. PWM_Start(periodS, 50, channelS);
  445. }
  446. // /**
  447. // * @brief set pulse frequence with half period
  448. // * @param periodS pulse frequence
  449. // */
  450. // void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  451. // {
  452. // PWM_Start(periodS, periodS / 2, channelS);
  453. // }
  454. #endif /* __HW_PWM_ATY_C */
  455. /******************************** End Of File *********************************/