You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
8.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <stdbool.h> //定义布尔
  2. #include <string.h>
  3. #include "board.h"
  4. //
  5. #include "../zes8p5066lib/basic.h"
  6. #include "../zes8p5066lib/gpio.h"
  7. #include "../zes8p5066lib/key.h"
  8. #include "../zes8p5066lib/systicket.h"
  9. #include "../zes8p5066lib/uart0.h"
  10. #include "test.h"
  11. #if 0
  12. #define PWM_FREQHZ 25000 //硬件pwm频率
  13. /***********************************************************************************************************************
  14. * =========================================================================================================== *
  15. ***********************************************************************************************************************/
  16. void onkey(zkey_t* key, zkey_state_t key_state);
  17. /***********************************************************************************************************************
  18. * =========================================================================================================== *
  19. ***********************************************************************************************************************/
  20. void onkey(zkey_t* key, zkey_state_t key_state) {
  21. if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) {
  22. ozone_pwm_control_update();
  23. printf("onkey2\n");
  24. }
  25. if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) {
  26. ozone_pwm_control_update();
  27. printf("onkey0\n");
  28. }
  29. if (key == &s_keys[1] /*左2*/ && zks_falling_edge == key_state) {
  30. }
  31. if (key == &s_keys[3] /*左2*/ && zks_falling_edge == key_state) {
  32. printf("onkey3\n");
  33. ozone_pwm_control_update();
  34. }
  35. }
  36. /***********************************************************************************************************************
  37. * =================================================INTERNAL_FUNCTION================================================= *
  38. ***********************************************************************************************************************/
  39. void UART0_IRQHandler(void) {
  40. /**
  41. * @brief
  42. *
  43. */
  44. // rx_data = UART_RecByte(UART0);
  45. // if (!s_rxbuf_is_ready) {
  46. // if (rx_count < sizeof(rx_buffer) - 1) {
  47. // rx_buffer[rx_count++] = rx_data;
  48. // uart0_lastreceive_ticket = get_sys_ticket();
  49. // }
  50. // }
  51. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  52. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  53. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  54. }
  55. /**
  56. * @brief
  57. *
  58. * @param freq
  59. * @param us
  60. */
  61. float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
  62. float period = 1000000.0 / freq;
  63. float duty = us * 100 / period;
  64. return duty;
  65. }
  66. void ozone_pwm_control_update() {
  67. printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
  68. set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
  69. }
  70. #define DO_IT_EACH_MS(time) \
  71. { \
  72. static uint32_t __ticket = 0; \
  73. if (port_haspassedms(__ticket) > (time)) { \
  74. __ticket = get_sys_ticket();
  75. #define END() \
  76. } \
  77. }
  78. /***********************************************************************************************************************
  79. * =============================================================================================================
  80. ***********************************************************************************************************************/
  81. bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
  82. void process_rx_order() {
  83. if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
  84. freqhz += 50;
  85. ozone_pwm_control_update();
  86. } else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
  87. freqhz -= 50;
  88. if (freqhz <= 0) freqhz = 1;
  89. ozone_pwm_control_update();
  90. } else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
  91. freqhz += 1000;
  92. ozone_pwm_control_update();
  93. } else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
  94. freqhz -= 1000;
  95. if (freqhz <= 0) freqhz = 1;
  96. ozone_pwm_control_update();
  97. } else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
  98. dutyus += 1;
  99. ozone_pwm_control_update();
  100. } else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
  101. dutyus -= 1;
  102. if (dutyus < 0) dutyus = 0;
  103. ozone_pwm_control_update();
  104. } else if (strcontains("AT+PWM=", rx_buffer)) {
  105. /**
  106. * AT+PWM=25000,8
  107. */
  108. int _dutyus = 0;
  109. int _freq = 0;
  110. sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
  111. if (_dutyus < 0 || _freq < 10000) {
  112. printf("Error\n");
  113. return;
  114. }
  115. dutyus = _dutyus;
  116. freqhz = _freq;
  117. ozone_pwm_control_update();
  118. } else if (strcontains("AT+MAT2=", rx_buffer)) {
  119. /**
  120. * AT+PWM=25000,8
  121. */
  122. int mat2 = 0;
  123. sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
  124. set_pwm_mat2(mat2);
  125. } else if (strcontains("AT+FAN=", rx_buffer)) {
  126. /**
  127. * AT+PWM=25000,8
  128. */
  129. int mat2 = 0;
  130. sscanf(rx_buffer, "AT+FAN=%d", &mat2);
  131. if (mat2 > 0) {
  132. port_fan_set(true);
  133. } else {
  134. port_fan_set(false);
  135. }
  136. }
  137. }
  138. void try_process_rx_order() {
  139. if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
  140. if (rx_count != 0) {
  141. s_rxbuf_is_ready = true;
  142. process_rx_order();
  143. rx_count = 0;
  144. memset(rx_buffer, 0, sizeof(rx_buffer));
  145. s_rxbuf_is_ready = false;
  146. }
  147. }
  148. }
  149. float power_filter(float nowpower, float factor) {
  150. static float last = 0;
  151. float now_raw = nowpower;
  152. float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
  153. last = now;
  154. return now;
  155. }
  156. uint16_t capture(int timeoff);
  157. #endif
  158. void onkey(zkey_t* key, zkey_state_t key_state);
  159. int a = 34;
  160. static zkey_t s_keys[] = {
  161. ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
  162. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  163. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  164. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  165. };
  166. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  167. void onkey(zkey_t* key, zkey_state_t key_state) {
  168. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  169. printf("on %s \n", key->name);
  170. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  171. printf("on %s \n", key->name);
  172. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  173. printf("on %s \n", key->name);
  174. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  175. printf("on %s \n", key->name);
  176. }
  177. }
  178. void systicket_do_debug_light_state(void) {
  179. static uint32_t lastprocess = 0;
  180. static uint8_t debug_led_state = 1;
  181. if (systicket_haspassedms(lastprocess) > 300) {
  182. lastprocess = systicket_get_now_ms();
  183. debug_led_state = !debug_led_state;
  184. port_debug_set(debug_led_state);
  185. }
  186. }
  187. void init_all_subdevice_state() {
  188. port_debug_set(false);
  189. port_fan_set(false);
  190. port_led0_set(false);
  191. port_led1_set(false);
  192. port_led2_set(false);
  193. port_led3_set(false);
  194. port_led_r_set(false);
  195. port_led_g_set(false);
  196. port_led_b_set(false);
  197. }
  198. int main(void) {
  199. SystemInit(); //配置系统时钟
  200. DeviceClockAllEnable(); //打开所有外设时钟
  201. systicket_init();
  202. //
  203. zgpio_init_all_gpio();
  204. port_init();
  205. printf("==========OZONE_GENERATOR==========\n");
  206. printf("= manufactor: iflytop\n");
  207. printf("= version : %s\n", VERSION);
  208. printf("=\n");
  209. /**
  210. * @brief
  211. */
  212. zkey_init(&key_module);
  213. /**
  214. * @brief
  215. */
  216. init_all_subdevice_state();
  217. //
  218. port_fan_set(true);
  219. port_ozone_pwm_set_duty(30000, 10 * 1000);
  220. port_ozone_pwm_start();
  221. while (true) {
  222. //调试指示灯逻辑
  223. systicket_do_debug_light_state();
  224. //按键扫描逻辑
  225. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  226. END();
  227. #if 1
  228. //打印功率
  229. DO_IT_EACH_MS(300) { //
  230. // printf("v :fan:%f,ozone:%f\n", port_adc_get_fan_voltage(), port_adc_get_ozone_generator_voltage());
  231. printf("power:fan:%f,ozone:%f\n", port_adc_get_fan_power(), port_adc_get_ozone_generator_power());
  232. }
  233. END();
  234. test_all_light();
  235. #endif
  236. // test_all_light();
  237. }
  238. }