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
337 lines
15 KiB
#include "port.h"
|
|
|
|
/***********************************************************************************************************************
|
|
* ======================================================ticket======================================================= *
|
|
***********************************************************************************************************************/
|
|
uint32_t g_sys_sick = 0;
|
|
void SysTick_IRQHandler(void) { g_sys_sick++; }
|
|
void port_systicket_init(void) {
|
|
SYSTICK_InitStruType x;
|
|
// x.SysTick_Value = SystemCoreClock / 10000;//重装载值4800(SystemCoreClock=48M)
|
|
// x.SysTick_ClkSource = SysTick_ClkS_Cpu;//时钟48M
|
|
// x.SysTick_ITEnable = Enable;
|
|
// SysTick_Init(&x);//现在滴答定时器时钟用的48M,重装载值4800 t=1s/f(f=48M/4800)
|
|
x.SysTick_Value = SystemCoreClock / 1000; //
|
|
x.SysTick_ClkSource = SysTick_ClkS_Cpu; //时钟48M
|
|
x.SysTick_ITEnable = Enable; //
|
|
SysTick_Init(&x); //现在滴答定时器时钟用的48M,重装载值4800 t=1s/f(f=48M/48000)
|
|
SysTick_Disable(); //
|
|
SysTick_Enable();
|
|
}
|
|
/**
|
|
* @brief ticket辅助方法
|
|
*/
|
|
uint32_t port_get_now_ms(void) { return g_sys_sick; }
|
|
uint32_t port_haspassedms(uint32_t ticket) {
|
|
uint32_t nowticket = port_get_now_ms();
|
|
if (nowticket >= ticket) {
|
|
return nowticket - ticket;
|
|
}
|
|
return UINT32_MAX - ticket + nowticket;
|
|
}
|
|
void port_delay_ms(__IO uint32_t nTime) {
|
|
uint32_t TimingDelay = 0;
|
|
TimingDelay = port_get_now_ms();
|
|
// printf("nTime%d\r\n",nTime);
|
|
while (port_haspassedms(TimingDelay) < nTime)
|
|
;
|
|
}
|
|
void port_do_debug_light_state(void) {
|
|
static uint32_t lastprocess = 0;
|
|
static uint8_t debug_led_state = 1;
|
|
if (port_haspassedms(lastprocess) > 1000) {
|
|
lastprocess = port_get_now_ms();
|
|
debug_led_state = !debug_led_state;
|
|
port_debug_set(debug_led_state);
|
|
}
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* =======================================================GPIO======================================================== *
|
|
***********************************************************************************************************************/
|
|
static GPIO_InitSettingType default_input_config = {
|
|
default_input_config.Signal = GPIO_Pin_Signal_Digital, //
|
|
default_input_config.Dir = GPIO_Direction_Input, //
|
|
default_input_config.Func = GPIO_Reuse_Func0, //
|
|
default_input_config.PUE = GPIO_PUE_Input_Disable, //
|
|
default_input_config.PDE = GPIO_PDE_Input_Disable //
|
|
};
|
|
static GPIO_InitSettingType default_output_config = {
|
|
default_output_config.Signal = GPIO_Pin_Signal_Digital, //
|
|
default_output_config.Dir = GPIO_Direction_Output, //
|
|
default_output_config.Func = GPIO_Reuse_Func0, //
|
|
default_output_config.ODE = GPIO_ODE_Output_Disable, //
|
|
default_output_config.DS = GPIO_DS_Output_Normal, //
|
|
default_output_config.PUE = GPIO_PUE_Input_Disable, //
|
|
default_output_config.PDE = GPIO_PDE_Input_Enable //
|
|
};
|
|
|
|
// default_input_config
|
|
|
|
static void gpio_init_as_output(GPIO_Pin Pin, GPIO_InitSettingType* setting, int initvalue) {
|
|
GPIO_Init(Pin, setting);
|
|
GPIO_WriteBit(GPIO_Pin_A3, initvalue);
|
|
}
|
|
static void gpio_init_as_input(GPIO_Pin Pin, GPIO_InitSettingType* setting) { GPIO_Init(Pin, setting); }
|
|
|
|
/***********************************************************************************************************************
|
|
* ========================================================ADC======================================================== *
|
|
***********************************************************************************************************************/
|
|
extern void ADC_Set_CH(ADC_TYPE_CHS AdcCH);
|
|
ADC_TYPE_CHS adc_get_chnum(GPIO_Pin gpiopin) {
|
|
/**
|
|
* @brief
|
|
* 参考:http://192.168.1.3:3000/manufacturer_eastsoft/ES8P5066_res/raw/branch/master/ES8P5066_Datasheet_C%20V1.1.pdf
|
|
* Page25:1. 5. 2 管脚对照表
|
|
*
|
|
* | **PIN NAME (FUNO(D))** | **FUN4(A)** |
|
|
* | ------------------------- | ----------- |
|
|
* | PA1 | AVREFP/AIN7 |
|
|
* | PA2 | AIN8 |
|
|
* | PA3 | AIN9 |
|
|
* | PA4 | AIN10 |
|
|
* | PA5 | AIN11 |
|
|
* | PA6 | AIN12 |
|
|
* | PA7 | AIN13 |
|
|
* | PM | AIN14 |
|
|
* | PA9 | AIN15 |
|
|
* | PA10 | |
|
|
* | PA11 | AIN1 |
|
|
* | PAl2 | AIN2 |
|
|
* | PA13 | AIN6 |
|
|
* | PA14(ISCK) | AIN16 |
|
|
* | PA15(ISDA) | AIN17 |
|
|
* | PA16 | |
|
|
* | PA22 | |
|
|
* | PA23 | |
|
|
* | PA24 | AIN19 |
|
|
* | PA25 | |
|
|
* | PA27 | |
|
|
* | PA28 | |
|
|
* | PBO | |
|
|
* | PB1 | |
|
|
* | PB8 | AIN3 |
|
|
* | PB9 | AIN4 |
|
|
* | PB10 | OSCI |
|
|
* | PB11 | OSCO |
|
|
* | PB12(MRSTN) | AIN0 |
|
|
* | PB13 | AIN5 |
|
|
*/
|
|
if (gpiopin == GPIO_Pin_A1 /* */) return ADC_CHS_AIN7;
|
|
if (gpiopin == GPIO_Pin_A2 /* */) return ADC_CHS_AIN8;
|
|
if (gpiopin == GPIO_Pin_A3 /* */) return ADC_CHS_AIN9;
|
|
if (gpiopin == GPIO_Pin_A4 /* */) return ADC_CHS_AIN10;
|
|
if (gpiopin == GPIO_Pin_A5 /* */) return ADC_CHS_AIN11;
|
|
if (gpiopin == GPIO_Pin_A6 /* */) return ADC_CHS_AIN12;
|
|
if (gpiopin == GPIO_Pin_A7 /* */) return ADC_CHS_AIN13;
|
|
if (gpiopin == GPIO_Pin_A14 /**/) return ADC_CHS_AIN14;
|
|
if (gpiopin == GPIO_Pin_A9 /* */) return ADC_CHS_AIN15;
|
|
if (gpiopin == GPIO_Pin_A11 /**/) return ADC_CHS_AIN1;
|
|
if (gpiopin == GPIO_Pin_A12 /**/) return ADC_CHS_AIN2;
|
|
if (gpiopin == GPIO_Pin_A13 /**/) return ADC_CHS_AIN6;
|
|
if (gpiopin == GPIO_Pin_A14 /**/) return ADC_CHS_AIN16;
|
|
if (gpiopin == GPIO_Pin_A15 /**/) return ADC_CHS_AIN17;
|
|
if (gpiopin == GPIO_Pin_A24 /**/) return ADC_CHS_AIN19;
|
|
if (gpiopin == GPIO_Pin_B8 /* */) return ADC_CHS_AIN3;
|
|
if (gpiopin == GPIO_Pin_B9 /* */) return ADC_CHS_AIN4;
|
|
if (gpiopin == GPIO_Pin_B12 /**/) return ADC_CHS_AIN0;
|
|
if (gpiopin == GPIO_Pin_B13 /**/) return ADC_CHS_AIN5;
|
|
ZASSERT(0);
|
|
return (ADC_TYPE_CHS)-1;
|
|
}
|
|
|
|
uint32_t adc_get_value(ADC_TYPE_CHS adc_ch) {
|
|
ADC_Set_CH(adc_ch);
|
|
ADC_SoftStart();
|
|
port_delay_ms(1);
|
|
ADC_SoftStop();
|
|
while (ADC_GetIFStatus(ADC_IF) == RESET)
|
|
;
|
|
uint32_t adcv = ADC_GetConvValue();
|
|
ADC_ClearIFStatus(ADC_IF);
|
|
return adcv;
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* =======================================================UART======================================================== *
|
|
***********************************************************************************************************************/
|
|
// 1、keil勾选Use MicroLIB 2、IAR/keil #define __PRINTF_USE_UART0__
|
|
|
|
void uart0_init(void) {
|
|
/**
|
|
* @brief 配置串口0的波特率为115200
|
|
* 时钟为系统时钟 Fpclk 为系统时钟频率
|
|
*/
|
|
GPIO_InitSettingType InitSet;
|
|
InitSet.Dir = GPIO_Direction_Output;
|
|
InitSet.DS = GPIO_DS_Output_Strong;
|
|
InitSet.Func = GPIO_Reuse_Func2;
|
|
InitSet.ODE = GPIO_ODE_Output_Disable;
|
|
InitSet.PDE = GPIO_PDE_Input_Disable;
|
|
InitSet.PUE = GPIO_PUE_Input_Enable;
|
|
InitSet.Signal = GPIO_Pin_Signal_Digital;
|
|
GPIO_Init(UART0_TXD0_PIN, &InitSet);
|
|
|
|
InitSet.Dir = GPIO_Direction_Input;
|
|
InitSet.DS = GPIO_DS_Output_Strong;
|
|
InitSet.Func = GPIO_Reuse_Func2;
|
|
InitSet.ODE = GPIO_ODE_Output_Disable;
|
|
InitSet.PDE = GPIO_PDE_Input_Disable;
|
|
InitSet.PUE = GPIO_PUE_Input_Disable;
|
|
InitSet.Signal = GPIO_Pin_Signal_Digital;
|
|
GPIO_Init(UART0_RXD0_PIN, &InitSet);
|
|
|
|
UART_InitStruType UART_InitStruct;
|
|
|
|
UART_InitStruct.UART_BaudRate = 115200; //波特率
|
|
UART_InitStruct.UART_ClockSet = UART_Clock_1; //时钟选择不分频
|
|
UART_InitStruct.UART_RxMode = UART_DataMode_8; // 8数据位无奇偶校验位
|
|
//标准极性端口数据和传输数据相同
|
|
UART_InitStruct.UART_RxPolar = UART_Polar_Normal;
|
|
UART_InitStruct.UART_StopBits = UART_StopBits_1; //一个停止位
|
|
UART_InitStruct.UART_TxMode = UART_DataMode_8; // 8数据位无奇偶校验位
|
|
UART_InitStruct.UART_TxPolar = UART_Polar_Normal; //标准usart极性
|
|
UART_Init(UART0, &UART_InitStruct);
|
|
UART_ITConfig(UART0, UART_IT_RB, Enable); /* UART0接收中断使能 */
|
|
/* UART0发送缓冲区空中断模式: 全空中断 */
|
|
UART_TBIMConfig(UART0, UART_TBIM_Byte);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_TB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_RB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_FE);
|
|
NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_1, Enable); //使能串口0中断
|
|
UART0_TxEnable();
|
|
UART0_RxEnable();
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* =======================================================PORT-GPIO======================================================== *
|
|
***********************************************************************************************************************/
|
|
void port_unused_gpio_init(void) {
|
|
/**
|
|
* @brief
|
|
* http://192.168.1.3:3000/manufacturer_eastsoft/ES8P5066_res/raw/branch/master/AN142_应用笔记_ES8P5066%20V1.0.pdf
|
|
* Page8:1. 13 未使用和未封装的GPIO端口处理
|
|
* 系统中未使用和未封装出来的 GPIO 端口建议设置为输出固定电平并悬空,若设置为输入则不
|
|
* 可悬空,须加上拉或下拉电阻接到电源或地。
|
|
*/
|
|
|
|
// GPIO_InitSettingType x;
|
|
// x.Signal = GPIO_Pin_Signal_Digital;
|
|
// x.Dir = GPIO_Direction_Output; //输出模式
|
|
// x.Func = GPIO_Reuse_Func0;
|
|
// x.ODE = GPIO_ODE_Output_Disable;
|
|
// x.DS = GPIO_DS_Output_Normal;
|
|
// x.PUE = GPIO_PUE_Input_Disable;
|
|
// x.PDE = GPIO_PDE_Input_Enable;
|
|
// GPIO_Init(GPIO_Pin_A10, &x);
|
|
// GPIO_Init(GPIO_Pin_A23, &x);
|
|
// GPIO_Init(GPIO_Pin_A27, &x); // 自己原理图上是26
|
|
// GPIO_Init(GPIO_Pin_A28, &x); // 自己原理图上是27
|
|
// GPIO_Init(GPIO_Pin_B0, &x);
|
|
// //三个adc
|
|
// GPIO_Init(GPIO_Pin_B8, &x);
|
|
// GPIO_Init(GPIO_Pin_B9, &x);
|
|
// GPIO_Init(GPIO_Pin_A2, &x);
|
|
// GPIO_WriteBit(GPIO_Pin_A10, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_A23, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_A27, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_A28, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_B0, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_B8, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_B9, 0);
|
|
// GPIO_WriteBit(GPIO_Pin_A2, 0);
|
|
}
|
|
|
|
void port_gpio_init(void) {
|
|
// LED-GPIO初始化
|
|
gpio_init_as_output(GPIO_Pin_A3 /* */, &default_output_config, 0);
|
|
gpio_init_as_output(GPIO_Pin_B13 /**/, &default_output_config, 0);
|
|
gpio_init_as_output(GPIO_Pin_A5 /* */, &default_output_config, 0);
|
|
gpio_init_as_output(GPIO_Pin_A6 /* */, &default_output_config, 0);
|
|
gpio_init_as_output(GPIO_Pin_A7 /* */, &default_output_config, 0);
|
|
gpio_init_as_output(GPIO_Pin_A8 /* */, &default_output_config, 0);
|
|
gpio_init_as_output(GPIO_Pin_A9 /* */, &default_output_config, 0);
|
|
//调试指示灯初始化
|
|
gpio_init_as_output(GPIO_Pin_A22 /**/, &default_output_config, 0);
|
|
//风扇控制GPIO初始化
|
|
gpio_init_as_output(GPIO_Pin_B1 /* */, &default_output_config, 0);
|
|
//按键GPIO初始化
|
|
gpio_init_as_input(GPIO_Pin_A11 /**/, &default_input_config);
|
|
gpio_init_as_input(GPIO_Pin_A16 /**/, &default_input_config);
|
|
gpio_init_as_input(GPIO_Pin_A13 /**/, &default_input_config);
|
|
gpio_init_as_input(GPIO_Pin_A12 /**/, &default_input_config);
|
|
}
|
|
|
|
void port_debug_set(bool state) { GPIO_SET(A, 22, !, state); }
|
|
bool port_fan_get() { return GPIO_GET(B, 1, !!); }
|
|
void port_fan_set(bool state) {
|
|
if (port_fan_get() == state) {
|
|
return;
|
|
}
|
|
GPIO_SET(B, 1, !!, state);
|
|
}
|
|
void port_led0_set(bool state) { GPIO_SET(A, 3, !, state); }
|
|
void port_led1_set(bool state) { GPIO_SET(B, 13, !, state); }
|
|
void port_led2_set(bool state) { GPIO_SET(A, 5, !, state); }
|
|
void port_led3_set(bool state) { GPIO_SET(A, 6, !, state); }
|
|
void port_led_r_set(bool state) { GPIO_SET(A, 7, !, state); }
|
|
void port_led_g_set(bool state) { GPIO_SET(A, 8, !, state); }
|
|
void port_led_b_set(bool state) { GPIO_SET(A, 9, !, state); }
|
|
|
|
bool port_gpio_get_timer_key_state(void) { return GPIO_GET(A, 11, !!); }
|
|
bool port_gpio_get_level_key_state(void) { return GPIO_GET(A, 16, !!); }
|
|
bool port_gpio_get_power_key_state(void) { return GPIO_GET(A, 12, !!); }
|
|
bool port_gpio_get_interval_key_state(void) { return GPIO_GET(A, 13, !!); }
|
|
|
|
bool port_led0_get_state(void) { return GPIO_GET(A, 3, !); }
|
|
bool port_led1_get_state(void) { return GPIO_GET(B, 13, !); }
|
|
bool port_led2_get_state(void) { return GPIO_GET(A, 5, !); }
|
|
bool port_led3_get_state(void) { return GPIO_GET(A, 6, !); }
|
|
|
|
/***********************************************************************************************************************
|
|
* =====================================================PORT-ADC====================================================== *
|
|
***********************************************************************************************************************/
|
|
|
|
void ADCInit(void) {}
|
|
static void adcinit() {
|
|
// ADC_GPIO_INIT
|
|
{
|
|
GPIO_InitSettingType y;
|
|
y.Signal = GPIO_Pin_Signal_Analog;
|
|
y.Dir = GPIO_Direction_Input;
|
|
y.Func = GPIO_Reuse_Func0;
|
|
GPIO_Init(GPIO_Pin_A2, &y);
|
|
}
|
|
|
|
{
|
|
GPIO_InitSettingType y;
|
|
y.Signal = GPIO_Pin_Signal_Analog;
|
|
y.Dir = GPIO_Direction_Input;
|
|
y.Func = GPIO_Reuse_Func0;
|
|
GPIO_Init(GPIO_Pin_B8, &y);
|
|
}
|
|
|
|
// ADCINIT
|
|
ADC_InitStruType x;
|
|
x.CLKS = ADC_CLKS_PCLK;
|
|
x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
|
|
x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */
|
|
x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,*/
|
|
x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */
|
|
x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */
|
|
x.ST = 10; /*采样时间st*2+1(个Tadclk)=1.743us AD硬件采样时间选择 */
|
|
x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */
|
|
x.CHS = ADC_CHS_AIN0; /*这里使用通道零作为默认,当没有使能ADC采集时,这个通道数关系不大*/
|
|
ADC_Init(&x);
|
|
}
|
|
|
|
float port_adc_get_fan_power() {
|
|
uint32_t adcraw = adc_get_value(adc_get_chnum(GPIO_Pin_A2));
|
|
float voltage = adcraw * 3.3 / 4096;
|
|
return voltage * voltage / 1; // u^2/R
|
|
}
|
|
float port_adc_get_ozone_generator_power() {
|
|
uint32_t adcv = adc_get_value(adc_get_chnum(GPIO_Pin_B8));
|
|
float voltage = adcv * 3.3 / 4096;
|
|
return voltage * voltage / 0.5; // u^2/R
|
|
}
|