HW_PWM_ATY.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 with PCA
  75. * @param level IO level after stop PWM
  76. * @note voltage lower than write pin
  77. */
  78. void PWM_Stop(uint8_t level)
  79. {
  80. if(level)
  81. {
  82. PCA_PWM1 &= 0xC0;
  83. CCAP1H = 0x00;
  84. }
  85. else
  86. {
  87. PCA_PWM1 |= 0x3F;
  88. CCAP1H = 0xFF;
  89. }
  90. CR = 0;
  91. }
  92. /**
  93. * @brief set pulse period with specified duty with PCA
  94. * @param periodS pulse period, us
  95. * @param dutyS pulse duty, 0-100.0 @ %
  96. * @param channelS channel to start or to change param
  97. * @todo any freq with ITM1
  98. */
  99. void PWM_StartPulse(uint32_t periodS, uint8_t channelS)
  100. {
  101. CCON = 0x00;
  102. CMOD = 0x08; // PCA clk = sys clk, 32/3us 93.75KHz @ 24MHz
  103. CL = 0x00; // init counter value
  104. CH = 0x00;
  105. if(channelS == 1)
  106. {
  107. CCAPM1 = 0x4D; // PCA at fast pulse mode, enbale IT
  108. CCAP1L = periodS;
  109. CCAP1H = periodS >> 8;
  110. }
  111. CR = 1; // start PCA
  112. EA = 1;
  113. }
  114. void PCA_Isr() interrupt 7
  115. {
  116. CCF1 = 0;
  117. CL = 0x00;
  118. CH = 0x00;
  119. }
  120. #elif defined(__STC51_ATY)
  121. /**
  122. * @brief set pulse period with specified duty
  123. * @param periodS pulse period, us
  124. * @param dutyS pulse duty, 0-100.0 @ %
  125. * @param channelS channel to start or to change param
  126. * @note Undone
  127. */
  128. void PWM_Start(uint32_t periodS, float dutyS, uint8_t channelS)
  129. {
  130. P_SW2 |= 0x80;
  131. if(channelS == PWM_CHANNEL_1P || channelS == PWM_CHANNEL_1N){
  132. PWMA_PS = (PWMA_PS & 0xFC) | (PWM_PIN_POS_1 << 0); //
  133. PWMA_CCER1 &= 0xF0;
  134. PWMA_CCMR1 = 0x60;
  135. PWMA_CCER1 |= 0x05;
  136. PWMA_CCR1 = dutyS / 100.0 * periodS;
  137. }
  138. else if(channelS == PWM_CHANNEL_2P || channelS == PWM_CHANNEL_2N){
  139. PWMA_PS = (PWMA_PS & 0xF3) | (PWM_PIN_POS_1 << 2); //
  140. PWMA_CCER1 &= 0x0F;
  141. PWMA_CCMR2 = 0x60;
  142. PWMA_CCER1 |= 0x50;
  143. PWMA_CCR2 = dutyS / 100.0 * periodS;
  144. }
  145. else if(channelS == PWM_CHANNEL_3P || channelS == PWM_CHANNEL_3N){
  146. PWMA_PS = (PWMA_PS & 0xCF) | (PWM_PIN_POS_1 << 4); // PWM3N_2 Heat
  147. PWMA_CCER2 &= 0xF0;
  148. PWMA_CCMR3 = 0x60;
  149. PWMA_CCER2 |= 0x05;
  150. PWMA_CCR3 = dutyS / 100.0 * periodS;
  151. }
  152. else if(channelS == PWM_CHANNEL_4P || channelS == PWM_CHANNEL_4N){
  153. PWMA_PS = (PWMA_PS & 0x3F) | (PWM_PIN_POS_1 << 6); //
  154. PWMA_CCER2 &= 0x0F;
  155. PWMA_CCMR4 = 0x60;
  156. PWMA_CCER2 |= 0x50;
  157. PWMA_CCR4 = dutyS / 100.0 * periodS;
  158. }
  159. else if(channelS == PWM_CHANNEL_5P){
  160. PWMB_PS = (PWMB_PS & 0xFC) | (PWM_PIN_POS_1 << 0); //
  161. PWMB_CCER1 &= 0xF0;
  162. PWMB_CCMR1 = 0x60;
  163. PWMB_CCER1 |= 0x01;
  164. PWMB_CCR5 = dutyS / 100.0 * periodS;
  165. }
  166. else if(channelS == PWM_CHANNEL_6P){
  167. PWMB_PS = (PWMB_PS & 0xF3) | (PWM_PIN_POS_1 << 2); // PWM6_2 DAC_PWM P54
  168. PWMB_CCER1 &= 0x0F; // Set 0 of CCERx before write CCMRx
  169. PWMB_CCMR2 = 0x60; // Set CC6 PWMB output mode
  170. PWMB_CCER1 |= 0x10; // Enable CC6 channel
  171. PWMB_CCR6 = dutyS / 100.0 * periodS; // Set dutys time
  172. }
  173. else if(channelS == PWM_CHANNEL_7P){
  174. PWMB_PS = (PWMB_PS & 0xCF) | (PWM_PIN_POS_2 << 4); //
  175. PWMB_CCER2 &= 0xF0;
  176. PWMB_CCMR3 = 0x60;
  177. PWMB_CCER2 |= 0x01;
  178. PWMB_CCR7 = dutyS / 100.0 * periodS;
  179. }
  180. else if(channelS == PWM_CHANNEL_8P){
  181. PWMB_PS = (PWMB_PS & 0x3F) | (PWM_PIN_POS_0 << 6); // PWM8 23 TMC_STEP
  182. PWMB_CCER2 &= 0x0F;
  183. PWMB_CCMR4 = 0x60;
  184. PWMB_CCER2 |= 0x10;
  185. PWMB_CCR8 = dutyS / 100.0 * periodS;
  186. }
  187. if((channelS & 0xF0) == 0xF0){ // channel 5-8
  188. PWMB_ARR = periodS; // Set period time
  189. PWMB_ENO |= ((uint8_t)1 << ((channelS & 0x0F) * 2)); // Enable channel output
  190. PWMB_BKR = 0x80; // Enable main output
  191. PWMB_CR1 = 0x01; // Start counter
  192. }
  193. else{ // channel 1-4
  194. PWMA_ARR = periodS;
  195. PWMA_ENO |= channelS;
  196. PWMA_BKR = 0x80;
  197. PWMA_CR1 = 0x01;
  198. }
  199. P_SW2 &= 0x7F;
  200. }
  201. /**
  202. * @brief stop PWM output with specified level
  203. * @param level IO level after stop PWM
  204. * @param channelS channel to start or to change param
  205. * @note Undone, not real stop
  206. */
  207. void PWM_Stop(uint8_t level, uint8_t channelS)
  208. {
  209. PWM_Start(1000, level * 1000, channelS);
  210. }
  211. #elif defined(__STM32_HAL_ATY)
  212. #include "tim.h"
  213. /**
  214. * @brief set pulse period with specified duty
  215. * @param periodS pulse period, us
  216. * @param dutyS pulse duty, 0-100.0 @ %
  217. * @param channelS channel to start or to change param
  218. * @note Undone
  219. */
  220. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  221. {
  222. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
  223. TIM1->CNT = 0;
  224. HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
  225. TIM1->ARR = periodS;
  226. TIM1->CCR4 = dutyS;
  227. TIM1->EGR = TIM_EGR_UG;
  228. }
  229. /**
  230. * @brief stop PWM output with specified level
  231. * @param level IO level after stop PWM
  232. * @note Undone
  233. */
  234. void PWM_Stop(uint8_t level)
  235. {
  236. if(level)
  237. {
  238. TIM1->ARR = 0;
  239. TIM1->CCR4 = 0;
  240. TIM1->EGR = TIM_EGR_UG;
  241. }
  242. else
  243. {
  244. TIM1->ARR = 0;
  245. TIM1->CCR4 = 1;
  246. TIM1->EGR = TIM_EGR_UG;
  247. }
  248. HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
  249. }
  250. #elif defined(__ESP8266_RTOS_ATY)
  251. #include "driver/pwm.h"
  252. #define PWM_CHANNEL_FAN 0
  253. #define PWM_CHANNEL_COLD 1
  254. #define PWM_CHANNEL_WARM 2
  255. const uint32_t* channelPin = {4, 2, 15};
  256. uint8_t* dutiesPercent = {50, 50, 50};
  257. /**
  258. * @see https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/api-reference/peripherals/pwm.html
  259. *
  260. */
  261. void PWM_Init(void)
  262. {
  263. uint32_t period = 1000;
  264. uint32_t duties[3];
  265. duties[0] = dutiesPercent[0] / 100.0 * period;
  266. duties[1] = dutiesPercent[1] / 100.0 * period;
  267. duties[2] = dutiesPercent[2] / 100.0 * period;
  268. pwm_init(period, duties, 3, channelPin);
  269. }
  270. /**
  271. * @brief set pulse period with specified duty
  272. * @param periodS pulse period, us
  273. * @param dutyS pulse duty, 0-100.0 @ %
  274. * @param channelS channel to start or to change param
  275. * @todo not tested
  276. */
  277. void PWM_Start(uint32_t periodS, uint8_t dutyS, uint8_t channelS)
  278. {
  279. extern pwm_obj_t* pwm_obj;
  280. if(periodS = !pwm_obj->period)
  281. pwm_set_period(periodS);
  282. pwm_set_duty(channelS, (uint32_t)((float)(dutyS / 100.0) * periodS));
  283. // pwm_set_period_duties(period, allDuties);
  284. pwm_start();
  285. }
  286. /**
  287. * @brief stop PWM output with specified level
  288. * @param level IO level after stop PWM
  289. * @todo not tested
  290. */
  291. void PWM_Stop(uint8_t level)
  292. {
  293. if(level)
  294. pwm_stop(0x00);
  295. else
  296. pwm_stop(0xFF);
  297. pwm_deinit();
  298. }
  299. #endif /* PLATFORM */
  300. #ifdef __DEBUG_HW_PWM_ATY
  301. void PWM_Test(uint8_t testType)
  302. {
  303. if(testType == 11)
  304. {
  305. static dutyCount = 0;
  306. dutyCount++;
  307. UartSendStr("\r\n");
  308. UartSendByte(dutyCount / 100 + '0');
  309. UartSendByte(((uint8_t)dutyCount % 100) / 10 + '0');
  310. UartSendByte((uint8_t)dutyCount % 10 + '0');
  311. UartSendStr("\r\n");
  312. if(dutyCount <= 100)
  313. PWM_Start(10, dutyCount, 1);
  314. else if(dutyCount > 100 && dutyCount < 200)
  315. PWM_Start(10, 200 - dutyCount, 1);
  316. else
  317. dutyCount = 0;
  318. }
  319. else if(testType == 21)
  320. {
  321. static uint8_t tempa = 10;
  322. PWM_Start(tempa, 50, 1);
  323. if(tempa == 10)
  324. tempa = 21;
  325. else if(tempa == 21)
  326. tempa = 42;
  327. else if(tempa == 42)
  328. tempa = 64;
  329. else if(tempa == 64)
  330. tempa = 85;
  331. else if(tempa == 85)
  332. tempa = 128;
  333. else
  334. tempa = 10;
  335. }
  336. else if(testType == 31)
  337. {
  338. static uint8_t stopLevel = 0;
  339. PWM_Start(10, 50, 1);
  340. stopLevel = !stopLevel;
  341. UartSendByte(stopLevel + '0');
  342. PWM_Stop(stopLevel);
  343. // P36 = stopLevel;
  344. }
  345. else if(testType == 22)
  346. {
  347. static uint32_t freqCount = 0xFFFF;
  348. P37 = 0;
  349. P35 = 0;
  350. if(freqCount > 100)
  351. freqCount -= 100;
  352. else if(freqCount > 0 && freqCount <= 100)
  353. freqCount -= 1;
  354. else if(freqCount == 0)
  355. freqCount = 0xFFFF;
  356. PWM_StartPulse(freqCount, 1);
  357. UartSendStr("\r\n");
  358. UartSendByte(freqCount / 10000 + '0');
  359. UartSendByte(((uint16_t)freqCount % 10000) / 1000 + '0');
  360. UartSendByte(((uint16_t)freqCount % 1000) / 100 + '0');
  361. UartSendByte(((uint16_t)freqCount % 100) / 10 + '0');
  362. UartSendByte((uint16_t)freqCount % 10 + '0');
  363. }
  364. }
  365. #endif /* __DEBUG_HW_PWM_ATY */
  366. /******************************************************************************/
  367. /**
  368. * @brief set pulse frequence with half dutycycle
  369. * @param periodS pulse frequence
  370. */
  371. void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  372. {
  373. PWM_Start(periodS, 50, channelS);
  374. }
  375. // /**
  376. // * @brief set pulse frequence with half period
  377. // * @param periodS pulse frequence
  378. // */
  379. // void PwmFreqSet(uint32_t periodS, uint8_t channelS)
  380. // {
  381. // PWM_Start(periodS, periodS / 2, channelS);
  382. // }
  383. #endif /* __HW_PWM_ATY_C */
  384. /******************************** End Of File *********************************/