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.

418 lines
18 KiB

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
4 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
4 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
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
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
  1. #include "port.h"
  2. #include "../zes8p5066lib/adc.h"
  3. #include "../zes8p5066lib/gpio.h"
  4. #include "../zes8p5066lib/pwm.h"
  5. #include "../zes8p5066lib/systicket.h"
  6. #include "../zes8p5066lib/uart0.h"
  7. zpwm_t g_ozonepwm;
  8. void port_init() {
  9. // LED-GPIO初始化
  10. zgpio_init_as_output(GPIO_Pin_A3 /* */, zgpio_get_default_output_config(), true);
  11. zgpio_init_as_output(GPIO_Pin_B13 /**/, zgpio_get_default_output_config(), true);
  12. zgpio_init_as_output(GPIO_Pin_A5 /* */, zgpio_get_default_output_config(), true);
  13. zgpio_init_as_output(GPIO_Pin_A6 /* */, zgpio_get_default_output_config(), true);
  14. // rgb
  15. zgpio_init_as_output(GPIO_Pin_A7 /* */, zgpio_get_default_ode_output_config(), true);
  16. zgpio_init_as_output(GPIO_Pin_A8 /* */, zgpio_get_default_ode_output_config(), true);
  17. zgpio_init_as_output(GPIO_Pin_A9 /* */, zgpio_get_default_ode_output_config(), true);
  18. //调试指示灯初始化
  19. zgpio_init_as_output(GPIO_Pin_A22 /**/, zgpio_get_default_output_config(), true);
  20. //风扇控制GPIO初始化
  21. zgpio_init_as_output(GPIO_Pin_B1 /* */, zgpio_get_default_output_config(), false);
  22. //按键GPIO初始化
  23. zgpio_init_as_input(GPIO_Pin_A11 /**/, zgpio_get_default_input_config());
  24. zgpio_init_as_input(GPIO_Pin_A16 /**/, zgpio_get_default_input_config());
  25. zgpio_init_as_input(GPIO_Pin_A13 /**/, zgpio_get_default_input_config());
  26. zgpio_init_as_input(GPIO_Pin_A12 /**/, zgpio_get_default_input_config());
  27. //串口初始化
  28. uart0_init(uart0_port_24and25, 115200);
  29. // ADC初始化
  30. adc_pin_init(GPIO_Pin_A2);
  31. adc_pin_init(GPIO_Pin_B8);
  32. adc_module_init();
  33. // PWM初始化
  34. {
  35. g_ozonepwm.T16Nx = T16N0;
  36. g_ozonepwm.T16Nx_PREMAT = 1;
  37. //臭氧PWM控制引脚
  38. g_ozonepwm.C1Enable = true; //
  39. g_ozonepwm.C1Pin = GPIO_Pin_A4; //
  40. g_ozonepwm.C1PinReuseFunc = GPIO_Reuse_Func2; //
  41. g_ozonepwm.C1PinIdleState = false; //
  42. g_ozonepwm.C1freq = 1; //
  43. g_ozonepwm.CH1_ODE = GPIO_ODE_Output_Disable; //
  44. g_ozonepwm.CH1_DS = GPIO_DS_Output_Normal; //
  45. g_ozonepwm.CH1_PUE = GPIO_PUE_Input_Disable; //
  46. g_ozonepwm.CH1_PDE = GPIO_PDE_Input_Disable; //
  47. zpwm_init(&g_ozonepwm);
  48. // zpwm_set_freq(&g_ozonepwm, 1, 30000);
  49. // zpwm_set_duty_ns(&g_ozonepwm, 1, 10000);
  50. // zpwm_start(&g_ozonepwm, 1);
  51. // while (true)
  52. // ;
  53. }
  54. }
  55. void port_ozone_pwm_set_duty(float freq, uint32_t dutyns) {
  56. zpwm_set_freq(&g_ozonepwm, 1, freq);
  57. zpwm_set_duty_ns(&g_ozonepwm, 1, dutyns);
  58. }
  59. void port_ozone_pwm_start() { zpwm_start(&g_ozonepwm, 1); }
  60. void port_ozone_pwm_stop() { zpwm_stop(&g_ozonepwm, 1); }
  61. /***********************************************************************************************************************
  62. * =======================================================GPIO======================================================== *
  63. ***********************************************************************************************************************/
  64. void port_debug_set(bool state) { GPIO_SET(A, 22, !, state); }
  65. bool port_fan_get() { return GPIO_GET(B, 1, !!); }
  66. void port_fan_set(bool state) {
  67. if (port_fan_get() == state) {
  68. return;
  69. }
  70. GPIO_SET(B, 1, !!, state);
  71. }
  72. void port_led0_set(bool state) { GPIO_SET(A, 3, !, state); }
  73. void port_led1_set(bool state) { GPIO_SET(B, 13, !, state); }
  74. void port_led2_set(bool state) { GPIO_SET(A, 5, !, state); }
  75. void port_led3_set(bool state) { GPIO_SET(A, 6, !, state); }
  76. void port_led_r_set(bool state) { GPIO_SET(A, 7, !, state); }
  77. void port_led_g_set(bool state) { GPIO_SET(A, 8, !, state); }
  78. void port_led_b_set(bool state) { GPIO_SET(A, 9, !, state); }
  79. bool port_gpio_get_timer_key_state(void) { return GPIO_GET(A, 11, !!); }
  80. bool port_gpio_get_level_key_state(void) { return GPIO_GET(A, 16, !!); }
  81. bool port_gpio_get_power_key_state(void) { return GPIO_GET(A, 12, !!); }
  82. bool port_gpio_get_interval_key_state(void) { return GPIO_GET(A, 13, !!); }
  83. bool port_led0_get_state(void) { return GPIO_GET(A, 3, !); }
  84. bool port_led1_get_state(void) { return GPIO_GET(B, 13, !); }
  85. bool port_led2_get_state(void) { return GPIO_GET(A, 5, !); }
  86. bool port_led3_get_state(void) { return GPIO_GET(A, 6, !); }
  87. /***********************************************************************************************************************
  88. * ========================================================ADC======================================================== *
  89. ***********************************************************************************************************************/
  90. float port_adc_get_fan_voltage() {
  91. float voltage = adc_get_voltage(GPIO_Pin_A2);
  92. return voltage; // u^2/R
  93. }
  94. float port_adc_get_ozone_generator_voltage() {
  95. float voltage = adc_get_voltage(GPIO_Pin_B8);
  96. return voltage; // u^2/R
  97. }
  98. float port_adc_get_fan_power() {
  99. float voltage = adc_get_voltage(GPIO_Pin_A2);
  100. /**
  101. * @brief /(1) X (12V)
  102. */
  103. return voltage / 1 * 12; // RU/R * fanU
  104. }
  105. float port_adc_get_ozone_generator_power() {
  106. float voltage = adc_get_voltage(GPIO_Pin_B8);
  107. return voltage * voltage / 0.5; // u^2/R
  108. }
  109. #if 0
  110. /***********************************************************************************************************************
  111. * =======================================================GPIO======================================================== *
  112. ***********************************************************************************************************************/
  113. static GPIO_InitSettingType default_input_config = {
  114. default_input_config.Signal = GPIO_Pin_Signal_Digital, //
  115. default_input_config.Dir = GPIO_Direction_Input, //
  116. default_input_config.Func = GPIO_Reuse_Func0, //
  117. default_input_config.PUE = GPIO_PUE_Input_Disable, //
  118. default_input_config.PDE = GPIO_PDE_Input_Disable //
  119. };
  120. static GPIO_InitSettingType default_output_config = {
  121. default_output_config.Signal = GPIO_Pin_Signal_Digital, //
  122. default_output_config.Dir = GPIO_Direction_Output, //
  123. default_output_config.Func = GPIO_Reuse_Func0, //
  124. default_output_config.ODE = GPIO_ODE_Output_Disable, //
  125. default_output_config.DS = GPIO_DS_Output_Normal, //
  126. default_output_config.PUE = GPIO_PUE_Input_Disable, //
  127. default_output_config.PDE = GPIO_PDE_Input_Enable //
  128. };
  129. // default_input_config
  130. static void zgpio_init_as_output(GPIO_Pin Pin, GPIO_InitSettingType* setting, int initvalue) {
  131. GPIO_Init(Pin, setting);
  132. GPIO_WriteBit(GPIO_Pin_A3, initvalue);
  133. }
  134. static void zgpio_init_as_input(GPIO_Pin Pin, GPIO_InitSettingType* setting) { GPIO_Init(Pin, setting); }
  135. /***********************************************************************************************************************
  136. * ========================================================ADC======================================================== *
  137. ***********************************************************************************************************************/
  138. extern void ADC_Set_CH(ADC_TYPE_CHS AdcCH);
  139. ADC_TYPE_CHS adc_get_chnum(GPIO_Pin gpiopin) {
  140. /**
  141. * @brief
  142. * :http://192.168.1.3:3000/manufacturer_eastsoft/ES8P5066_res/raw/branch/master/ES8P5066_Datasheet_C%20V1.1.pdf
  143. * Page25:1. 5. 2
  144. *
  145. * | **PIN NAME (FUNO(D))** | **FUN4(A)** |
  146. * | ------------------------- | ----------- |
  147. * | PA1 | AVREFP/AIN7 |
  148. * | PA2 | AIN8 |
  149. * | PA3 | AIN9 |
  150. * | PA4 | AIN10 |
  151. * | PA5 | AIN11 |
  152. * | PA6 | AIN12 |
  153. * | PA7 | AIN13 |
  154. * | PM | AIN14 |
  155. * | PA9 | AIN15 |
  156. * | PA10 | |
  157. * | PA11 | AIN1 |
  158. * | PAl2 | AIN2 |
  159. * | PA13 | AIN6 |
  160. * | PA14(ISCK) | AIN16 |
  161. * | PA15(ISDA) | AIN17 |
  162. * | PA16 | |
  163. * | PA22 | |
  164. * | PA23 | |
  165. * | PA24 | AIN19 |
  166. * | PA25 | |
  167. * | PA27 | |
  168. * | PA28 | |
  169. * | PBO | |
  170. * | PB1 | |
  171. * | PB8 | AIN3 |
  172. * | PB9 | AIN4 |
  173. * | PB10 | OSCI |
  174. * | PB11 | OSCO |
  175. * | PB12(MRSTN) | AIN0 |
  176. * | PB13 | AIN5 |
  177. */
  178. if (gpiopin == GPIO_Pin_A1 /* */) return ADC_CHS_AIN7;
  179. if (gpiopin == GPIO_Pin_A2 /* */) return ADC_CHS_AIN8;
  180. if (gpiopin == GPIO_Pin_A3 /* */) return ADC_CHS_AIN9;
  181. if (gpiopin == GPIO_Pin_A4 /* */) return ADC_CHS_AIN10;
  182. if (gpiopin == GPIO_Pin_A5 /* */) return ADC_CHS_AIN11;
  183. if (gpiopin == GPIO_Pin_A6 /* */) return ADC_CHS_AIN12;
  184. if (gpiopin == GPIO_Pin_A7 /* */) return ADC_CHS_AIN13;
  185. if (gpiopin == GPIO_Pin_A14 /**/) return ADC_CHS_AIN14;
  186. if (gpiopin == GPIO_Pin_A9 /* */) return ADC_CHS_AIN15;
  187. if (gpiopin == GPIO_Pin_A11 /**/) return ADC_CHS_AIN1;
  188. if (gpiopin == GPIO_Pin_A12 /**/) return ADC_CHS_AIN2;
  189. if (gpiopin == GPIO_Pin_A13 /**/) return ADC_CHS_AIN6;
  190. if (gpiopin == GPIO_Pin_A14 /**/) return ADC_CHS_AIN16;
  191. if (gpiopin == GPIO_Pin_A15 /**/) return ADC_CHS_AIN17;
  192. if (gpiopin == GPIO_Pin_A24 /**/) return ADC_CHS_AIN19;
  193. if (gpiopin == GPIO_Pin_B8 /* */) return ADC_CHS_AIN3;
  194. if (gpiopin == GPIO_Pin_B9 /* */) return ADC_CHS_AIN4;
  195. if (gpiopin == GPIO_Pin_B12 /**/) return ADC_CHS_AIN0;
  196. if (gpiopin == GPIO_Pin_B13 /**/) return ADC_CHS_AIN5;
  197. ZASSERT(0);
  198. return (ADC_TYPE_CHS)-1;
  199. }
  200. uint32_t adc_get_value(ADC_TYPE_CHS adc_ch) {
  201. ADC_Set_CH(adc_ch);
  202. ADC_SoftStart();
  203. port_delay_ms(1);
  204. ADC_SoftStop();
  205. while (ADC_GetIFStatus(ADC_IF) == RESET)
  206. ;
  207. uint32_t adcv = ADC_GetConvValue();
  208. ADC_ClearIFStatus(ADC_IF);
  209. return adcv;
  210. }
  211. /***********************************************************************************************************************
  212. * =======================================================UART======================================================== *
  213. ***********************************************************************************************************************/
  214. // 1、keil勾选Use MicroLIB 2、IAR/keil #define __PRINTF_USE_UART0__
  215. void uart0_init(void) {
  216. /**
  217. * @brief 0115200
  218. * Fpclk
  219. */
  220. GPIO_InitSettingType InitSet;
  221. InitSet.Dir = GPIO_Direction_Output;
  222. InitSet.DS = GPIO_DS_Output_Strong;
  223. InitSet.Func = GPIO_Reuse_Func2;
  224. InitSet.ODE = GPIO_ODE_Output_Disable;
  225. InitSet.PDE = GPIO_PDE_Input_Disable;
  226. InitSet.PUE = GPIO_PUE_Input_Enable;
  227. InitSet.Signal = GPIO_Pin_Signal_Digital;
  228. GPIO_Init(UART0_TXD0_PIN, &InitSet);
  229. InitSet.Dir = GPIO_Direction_Input;
  230. InitSet.DS = GPIO_DS_Output_Strong;
  231. InitSet.Func = GPIO_Reuse_Func2;
  232. InitSet.ODE = GPIO_ODE_Output_Disable;
  233. InitSet.PDE = GPIO_PDE_Input_Disable;
  234. InitSet.PUE = GPIO_PUE_Input_Disable;
  235. InitSet.Signal = GPIO_Pin_Signal_Digital;
  236. GPIO_Init(UART0_RXD0_PIN, &InitSet);
  237. UART_InitStruType UART_InitStruct;
  238. UART_InitStruct.UART_BaudRate = 115200; //波特率
  239. UART_InitStruct.UART_ClockSet = UART_Clock_1; //时钟选择不分频
  240. UART_InitStruct.UART_RxMode = UART_DataMode_8; // 8数据位无奇偶校验位
  241. //标准极性端口数据和传输数据相同
  242. UART_InitStruct.UART_RxPolar = UART_Polar_Normal;
  243. UART_InitStruct.UART_StopBits = UART_StopBits_1; //一个停止位
  244. UART_InitStruct.UART_TxMode = UART_DataMode_8; // 8数据位无奇偶校验位
  245. UART_InitStruct.UART_TxPolar = UART_Polar_Normal; //标准usart极性
  246. UART_Init(UART0, &UART_InitStruct);
  247. UART_ITConfig(UART0, UART_IT_RB, Enable); /* UART0接收中断使能 */
  248. /* UART0发送缓冲区空中断模式: 全空中断 */
  249. UART_TBIMConfig(UART0, UART_TBIM_Byte);
  250. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  251. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  252. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  253. NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_1, Enable); //使能串口0中断
  254. UART0_TxEnable();
  255. UART0_RxEnable();
  256. }
  257. /***********************************************************************************************************************
  258. * =======================================================PORT-GPIO======================================================== *
  259. ***********************************************************************************************************************/
  260. void port_unused_gpio_init(void) {
  261. /**
  262. * @brief
  263. * http://192.168.1.3:3000/manufacturer_eastsoft/ES8P5066_res/raw/branch/master/AN142_应用笔记_ES8P5066%20V1.0.pdf
  264. * Page8:1. 13 使GPIO端口处理
  265. * 使 GPIO
  266. *
  267. */
  268. // GPIO_InitSettingType x;
  269. // x.Signal = GPIO_Pin_Signal_Digital;
  270. // x.Dir = GPIO_Direction_Output; //输出模式
  271. // x.Func = GPIO_Reuse_Func0;
  272. // x.ODE = GPIO_ODE_Output_Disable;
  273. // x.DS = GPIO_DS_Output_Normal;
  274. // x.PUE = GPIO_PUE_Input_Disable;
  275. // x.PDE = GPIO_PDE_Input_Enable;
  276. // GPIO_Init(GPIO_Pin_A10, &x);
  277. // GPIO_Init(GPIO_Pin_A23, &x);
  278. // GPIO_Init(GPIO_Pin_A27, &x); // 自己原理图上是26
  279. // GPIO_Init(GPIO_Pin_A28, &x); // 自己原理图上是27
  280. // GPIO_Init(GPIO_Pin_B0, &x);
  281. // //三个adc
  282. // GPIO_Init(GPIO_Pin_B8, &x);
  283. // GPIO_Init(GPIO_Pin_B9, &x);
  284. // GPIO_Init(GPIO_Pin_A2, &x);
  285. // GPIO_WriteBit(GPIO_Pin_A10, 0);
  286. // GPIO_WriteBit(GPIO_Pin_A23, 0);
  287. // GPIO_WriteBit(GPIO_Pin_A27, 0);
  288. // GPIO_WriteBit(GPIO_Pin_A28, 0);
  289. // GPIO_WriteBit(GPIO_Pin_B0, 0);
  290. // GPIO_WriteBit(GPIO_Pin_B8, 0);
  291. // GPIO_WriteBit(GPIO_Pin_B9, 0);
  292. // GPIO_WriteBit(GPIO_Pin_A2, 0);
  293. }
  294. void port_gpio_init(void) {
  295. // LED-GPIO初始化
  296. zgpio_init_as_output(GPIO_Pin_A3 /* */, &default_output_config, 0);
  297. zgpio_init_as_output(GPIO_Pin_B13 /**/, &default_output_config, 0);
  298. zgpio_init_as_output(GPIO_Pin_A5 /* */, &default_output_config, 0);
  299. zgpio_init_as_output(GPIO_Pin_A6 /* */, &default_output_config, 0);
  300. zgpio_init_as_output(GPIO_Pin_A7 /* */, &default_output_config, 0);
  301. zgpio_init_as_output(GPIO_Pin_A8 /* */, &default_output_config, 0);
  302. zgpio_init_as_output(GPIO_Pin_A9 /* */, &default_output_config, 0);
  303. //调试指示灯初始化
  304. zgpio_init_as_output(GPIO_Pin_A22 /**/, &default_output_config, 0);
  305. //风扇控制GPIO初始化
  306. zgpio_init_as_output(GPIO_Pin_B1 /* */, &default_output_config, 0);
  307. //按键GPIO初始化
  308. zgpio_init_as_input(GPIO_Pin_A11 /**/, &default_input_config);
  309. zgpio_init_as_input(GPIO_Pin_A16 /**/, &default_input_config);
  310. zgpio_init_as_input(GPIO_Pin_A13 /**/, &default_input_config);
  311. zgpio_init_as_input(GPIO_Pin_A12 /**/, &default_input_config);
  312. }
  313. void port_debug_set(bool state) { GPIO_SET(A, 22, !, state); }
  314. bool port_fan_get() { return GPIO_GET(B, 1, !!); }
  315. void port_fan_set(bool state) {
  316. if (port_fan_get() == state) {
  317. return;
  318. }
  319. GPIO_SET(B, 1, !!, state);
  320. }
  321. void port_led0_set(bool state) { GPIO_SET(A, 3, !, state); }
  322. void port_led1_set(bool state) { GPIO_SET(B, 13, !, state); }
  323. void port_led2_set(bool state) { GPIO_SET(A, 5, !, state); }
  324. void port_led3_set(bool state) { GPIO_SET(A, 6, !, state); }
  325. void port_led_r_set(bool state) { GPIO_SET(A, 7, !, state); }
  326. void port_led_g_set(bool state) { GPIO_SET(A, 8, !, state); }
  327. void port_led_b_set(bool state) { GPIO_SET(A, 9, !, state); }
  328. bool port_gpio_get_timer_key_state(void) { return GPIO_GET(A, 11, !!); }
  329. bool port_gpio_get_level_key_state(void) { return GPIO_GET(A, 16, !!); }
  330. bool port_gpio_get_power_key_state(void) { return GPIO_GET(A, 12, !!); }
  331. bool port_gpio_get_interval_key_state(void) { return GPIO_GET(A, 13, !!); }
  332. bool port_led0_get_state(void) { return GPIO_GET(A, 3, !); }
  333. bool port_led1_get_state(void) { return GPIO_GET(B, 13, !); }
  334. bool port_led2_get_state(void) { return GPIO_GET(A, 5, !); }
  335. bool port_led3_get_state(void) { return GPIO_GET(A, 6, !); }
  336. /***********************************************************************************************************************
  337. * =====================================================PORT-ADC====================================================== *
  338. ***********************************************************************************************************************/
  339. void ADCInit(void) {}
  340. static void adcinit() {
  341. // ADC_GPIO_INIT
  342. {
  343. GPIO_InitSettingType y;
  344. y.Signal = GPIO_Pin_Signal_Analog;
  345. y.Dir = GPIO_Direction_Input;
  346. y.Func = GPIO_Reuse_Func0;
  347. GPIO_Init(GPIO_Pin_A2, &y);
  348. }
  349. {
  350. GPIO_InitSettingType y;
  351. y.Signal = GPIO_Pin_Signal_Analog;
  352. y.Dir = GPIO_Direction_Input;
  353. y.Func = GPIO_Reuse_Func0;
  354. GPIO_Init(GPIO_Pin_B8, &y);
  355. }
  356. // ADCINIT
  357. ADC_InitStruType x;
  358. x.CLKS = ADC_CLKS_PCLK;
  359. x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
  360. x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */
  361. x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,*/
  362. x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */
  363. x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */
  364. x.ST = 10; /*采样时间st*2+1(个Tadclk)=1.743us AD硬件采样时间选择 */
  365. x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */
  366. x.CHS = ADC_CHS_AIN0; /*这里使用通道零作为默认,当没有使能ADC采集时,这个通道数关系不大*/
  367. ADC_Init(&x);
  368. }
  369. float port_adc_get_fan_power() {
  370. uint32_t adcraw = adc_get_value(adc_get_chnum(GPIO_Pin_A2));
  371. float voltage = adcraw * 3.3 / 4096;
  372. return voltage * voltage / 1; // u^2/R
  373. }
  374. float port_adc_get_ozone_generator_power() {
  375. uint32_t adcv = adc_get_value(adc_get_chnum(GPIO_Pin_B8));
  376. float voltage = adcv * 3.3 / 4096;
  377. return voltage * voltage / 0.5; // u^2/R
  378. }
  379. #endif