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.

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