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.

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