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.

286 lines
13 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
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. * =======================================================PORT-GPIO======================================================== *
  150. ***********************************************************************************************************************/
  151. void port_unused_gpio_init(void) {
  152. /**
  153. * @brief
  154. * http://192.168.1.3:3000/manufacturer_eastsoft/ES8P5066_res/raw/branch/master/AN142_应用笔记_ES8P5066%20V1.0.pdf
  155. * Page8:1. 13 使GPIO端口处理
  156. * 使 GPIO
  157. *
  158. */
  159. // GPIO_InitSettingType x;
  160. // x.Signal = GPIO_Pin_Signal_Digital;
  161. // x.Dir = GPIO_Direction_Output; //输出模式
  162. // x.Func = GPIO_Reuse_Func0;
  163. // x.ODE = GPIO_ODE_Output_Disable;
  164. // x.DS = GPIO_DS_Output_Normal;
  165. // x.PUE = GPIO_PUE_Input_Disable;
  166. // x.PDE = GPIO_PDE_Input_Enable;
  167. // GPIO_Init(GPIO_Pin_A10, &x);
  168. // GPIO_Init(GPIO_Pin_A23, &x);
  169. // GPIO_Init(GPIO_Pin_A27, &x); // 自己原理图上是26
  170. // GPIO_Init(GPIO_Pin_A28, &x); // 自己原理图上是27
  171. // GPIO_Init(GPIO_Pin_B0, &x);
  172. // //三个adc
  173. // GPIO_Init(GPIO_Pin_B8, &x);
  174. // GPIO_Init(GPIO_Pin_B9, &x);
  175. // GPIO_Init(GPIO_Pin_A2, &x);
  176. // GPIO_WriteBit(GPIO_Pin_A10, 0);
  177. // GPIO_WriteBit(GPIO_Pin_A23, 0);
  178. // GPIO_WriteBit(GPIO_Pin_A27, 0);
  179. // GPIO_WriteBit(GPIO_Pin_A28, 0);
  180. // GPIO_WriteBit(GPIO_Pin_B0, 0);
  181. // GPIO_WriteBit(GPIO_Pin_B8, 0);
  182. // GPIO_WriteBit(GPIO_Pin_B9, 0);
  183. // GPIO_WriteBit(GPIO_Pin_A2, 0);
  184. }
  185. void port_gpio_init(void) {
  186. // LED-GPIO初始化
  187. gpio_init_as_output(GPIO_Pin_A3 /* */, &default_output_config, 0);
  188. gpio_init_as_output(GPIO_Pin_B13 /**/, &default_output_config, 0);
  189. gpio_init_as_output(GPIO_Pin_A5 /* */, &default_output_config, 0);
  190. gpio_init_as_output(GPIO_Pin_A6 /* */, &default_output_config, 0);
  191. gpio_init_as_output(GPIO_Pin_A7 /* */, &default_output_config, 0);
  192. gpio_init_as_output(GPIO_Pin_A8 /* */, &default_output_config, 0);
  193. gpio_init_as_output(GPIO_Pin_A9 /* */, &default_output_config, 0);
  194. //调试指示灯初始化
  195. gpio_init_as_output(GPIO_Pin_A22 /**/, &default_output_config, 0);
  196. //风扇控制GPIO初始化
  197. gpio_init_as_output(GPIO_Pin_B1 /* */, &default_output_config, 0);
  198. //按键GPIO初始化
  199. gpio_init_as_input(GPIO_Pin_A11 /**/, &default_input_config);
  200. gpio_init_as_input(GPIO_Pin_A16 /**/, &default_input_config);
  201. gpio_init_as_input(GPIO_Pin_A13 /**/, &default_input_config);
  202. gpio_init_as_input(GPIO_Pin_A12 /**/, &default_input_config);
  203. }
  204. void port_debug_set(bool state) { GPIO_SET(A, 22, !, state); }
  205. bool port_fan_get() { return GPIO_GET(B, 1, !!); }
  206. void port_fan_set(bool state) {
  207. if (port_fan_get() == state) {
  208. return;
  209. }
  210. GPIO_SET(B, 1, !!, state);
  211. }
  212. void port_led0_set(bool state) { GPIO_SET(A, 3, !, state); }
  213. void port_led1_set(bool state) { GPIO_SET(B, 13, !, state); }
  214. void port_led2_set(bool state) { GPIO_SET(A, 5, !, state); }
  215. void port_led3_set(bool state) { GPIO_SET(A, 6, !, state); }
  216. void port_led_r_set(bool state) { GPIO_SET(A, 7, !, state); }
  217. void port_led_g_set(bool state) { GPIO_SET(A, 8, !, state); }
  218. void port_led_b_set(bool state) { GPIO_SET(A, 9, !, state); }
  219. bool port_gpio_get_timer_key_state(void) { return GPIO_GET(A, 11, !!); }
  220. bool port_gpio_get_level_key_state(void) { return GPIO_GET(A, 16, !!); }
  221. bool port_gpio_get_power_key_state(void) { return GPIO_GET(A, 12, !!); }
  222. bool port_gpio_get_interval_key_state(void) { return GPIO_GET(A, 13, !!); }
  223. bool port_led0_get_state(void) { return GPIO_GET(A, 3, !); }
  224. bool port_led1_get_state(void) { return GPIO_GET(B, 13, !); }
  225. bool port_led2_get_state(void) { return GPIO_GET(A, 5, !); }
  226. bool port_led3_get_state(void) { return GPIO_GET(A, 6, !); }
  227. /***********************************************************************************************************************
  228. * =====================================================PORT-ADC====================================================== *
  229. ***********************************************************************************************************************/
  230. void ADCInit(void) {}
  231. static void adcinit() {
  232. // ADC_GPIO_INIT
  233. {
  234. GPIO_InitSettingType y;
  235. y.Signal = GPIO_Pin_Signal_Analog;
  236. y.Dir = GPIO_Direction_Input;
  237. y.Func = GPIO_Reuse_Func0;
  238. GPIO_Init(GPIO_Pin_A2, &y);
  239. }
  240. {
  241. GPIO_InitSettingType y;
  242. y.Signal = GPIO_Pin_Signal_Analog;
  243. y.Dir = GPIO_Direction_Input;
  244. y.Func = GPIO_Reuse_Func0;
  245. GPIO_Init(GPIO_Pin_B8, &y);
  246. }
  247. // ADCINIT
  248. ADC_InitStruType x;
  249. x.CLKS = ADC_CLKS_PCLK;
  250. x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
  251. x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */
  252. x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,*/
  253. x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */
  254. x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */
  255. x.ST = 10; /*采样时间st*2+1(个Tadclk)=1.743us AD硬件采样时间选择 */
  256. x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */
  257. x.CHS = ADC_CHS_AIN0; /*这里使用通道零作为默认,当没有使能ADC采集时,这个通道数关系不大*/
  258. ADC_Init(&x);
  259. }
  260. float port_adc_get_fan_power() {
  261. uint32_t adcraw = adc_get_value(adc_get_chnum(GPIO_Pin_A2));
  262. float voltage = adcraw * 3.3 / 4096;
  263. return voltage * voltage / 1; // u^2/R
  264. }
  265. float port_adc_get_ozone_generator_power() {
  266. uint32_t adcv = adc_get_value(adc_get_chnum(GPIO_Pin_B8));
  267. float voltage = adcv * 3.3 / 4096;
  268. return voltage * voltage / 0.5; // u^2/R
  269. }