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.

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