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.

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