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.
 
 
 
 

429 lines
18 KiB

#include "port.h"
#include "../zes8p5066lib/adc.h"
#include "../zes8p5066lib/gpio.h"
#include "../zes8p5066lib/pwm.h"
#include "../zes8p5066lib/uart0.h"
#include "pwm.h"
zpwm_t g_ozonepwm;
void port_init() {
// LED-GPIO初始化
zgpio_init_as_output(GPIO_Pin_A3 /* */, zgpio_get_default_output_config(), true);
zgpio_init_as_output(GPIO_Pin_B13 /**/, zgpio_get_default_output_config(), true);
zgpio_init_as_output(GPIO_Pin_A5 /* */, zgpio_get_default_output_config(), true);
zgpio_init_as_output(GPIO_Pin_A6 /* */, zgpio_get_default_output_config(), true);
// rgb
zgpio_init_as_output(GPIO_Pin_A7 /* */, zgpio_get_default_ode_output_config(), true);
zgpio_init_as_output(GPIO_Pin_A8 /* */, zgpio_get_default_ode_output_config(), true);
zgpio_init_as_output(GPIO_Pin_A9 /* */, zgpio_get_default_ode_output_config(), true);
//调试指示灯初始化
zgpio_init_as_output(GPIO_Pin_A22 /**/, zgpio_get_default_output_config(), true);
//风扇控制GPIO初始化
zgpio_init_as_output(GPIO_Pin_B1 /* */, zgpio_get_default_output_config(), false);
//按键GPIO初始化
zgpio_init_as_input(GPIO_Pin_A11 /**/, zgpio_get_default_input_config());
zgpio_init_as_input(GPIO_Pin_A16 /**/, zgpio_get_default_input_config());
zgpio_init_as_input(GPIO_Pin_A13 /**/, zgpio_get_default_input_config());
zgpio_init_as_input(GPIO_Pin_A12 /**/, zgpio_get_default_input_config());
//串口初始化
uart0_init(uart0_port_24and25, 115200);
// ADC初始化
adc_pin_init(GPIO_Pin_A2);
adc_pin_init(GPIO_Pin_B8);
adc_module_init();
// PWM初始化
// t16_pa4_init();
// set_pwm_modbul_freq_duty2(1000,50);
{
g_ozonepwm.T16Nx = T16N0;
g_ozonepwm.T16Nx_PREMAT = 1;
//臭氧PWM控制引脚
g_ozonepwm.C1Enable = true;
g_ozonepwm.C1Pin = GPIO_Pin_A4;
g_ozonepwm.C1PinReuseFunc = GPIO_Reuse_Func2;
g_ozonepwm.C1PinIdleState = false;
g_ozonepwm.C1freq = 1;
g_ozonepwm.CH1_ODE = GPIO_ODE_Output_Disable;
g_ozonepwm.CH1_DS = GPIO_DS_Output_Normal;
g_ozonepwm.CH1_PUE = GPIO_PUE_Input_Disable;
g_ozonepwm.CH1_PDE = GPIO_PUE_Input_Disable;
// g_ozonepwm.C0Enable = ;
// g_ozonepwm.C0Pin =
// g_ozonepwm.C0PinReuseFunc =
// g_ozonepwm.C0PinIdleState =
// g_ozonepwm.C0freq =
zpwm_init(&g_ozonepwm);
zpwm_set_freq(&g_ozonepwm, 1, 1000);
zpwm_set_duty(&g_ozonepwm, 1, 50);
zpwm_start(&g_ozonepwm, 1);
}
while (true) {
}
}
void port_ozone_pwm_set_duty(float freq, uint32_t dutyns) {
zpwm_set_freq(&g_ozonepwm, 0, freq);
zpwm_set_duty_ns(&g_ozonepwm, 0, dutyns);
}
void port_ozone_pwm_start() { zpwm_start(&g_ozonepwm, 0); }
void port_ozone_pwm_stop() { zpwm_stop(&g_ozonepwm, 0); }
/***********************************************************************************************************************
* =======================================================GPIO======================================================== *
***********************************************************************************************************************/
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, !); }
/***********************************************************************************************************************
* ========================================================ADC======================================================== *
***********************************************************************************************************************/
float port_adc_get_fan_voltage() {
float voltage = adc_get_voltage(GPIO_Pin_A2);
return voltage; // u^2/R
}
float port_adc_get_ozone_generator_voltage() {
float voltage = adc_get_voltage(GPIO_Pin_B8);
return voltage; // u^2/R
}
float port_adc_get_fan_power() {
float voltage = adc_get_voltage(GPIO_Pin_A2);
/**
* @brief 采样电阻两端电压/采样电阻(1欧) X 风扇两端电压(12V)
*/
return voltage / 1 * 12; // RU/R * fanU
}
float port_adc_get_ozone_generator_power() {
float voltage = adc_get_voltage(GPIO_Pin_B8);
return voltage * voltage / 0.5; // u^2/R
}
#if 0
/***********************************************************************************************************************
* =======================================================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 zgpio_init_as_output(GPIO_Pin Pin, GPIO_InitSettingType* setting, int initvalue) {
GPIO_Init(Pin, setting);
GPIO_WriteBit(GPIO_Pin_A3, initvalue);
}
static void zgpio_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初始化
zgpio_init_as_output(GPIO_Pin_A3 /* */, &default_output_config, 0);
zgpio_init_as_output(GPIO_Pin_B13 /**/, &default_output_config, 0);
zgpio_init_as_output(GPIO_Pin_A5 /* */, &default_output_config, 0);
zgpio_init_as_output(GPIO_Pin_A6 /* */, &default_output_config, 0);
zgpio_init_as_output(GPIO_Pin_A7 /* */, &default_output_config, 0);
zgpio_init_as_output(GPIO_Pin_A8 /* */, &default_output_config, 0);
zgpio_init_as_output(GPIO_Pin_A9 /* */, &default_output_config, 0);
//调试指示灯初始化
zgpio_init_as_output(GPIO_Pin_A22 /**/, &default_output_config, 0);
//风扇控制GPIO初始化
zgpio_init_as_output(GPIO_Pin_B1 /* */, &default_output_config, 0);
//按键GPIO初始化
zgpio_init_as_input(GPIO_Pin_A11 /**/, &default_input_config);
zgpio_init_as_input(GPIO_Pin_A16 /**/, &default_input_config);
zgpio_init_as_input(GPIO_Pin_A13 /**/, &default_input_config);
zgpio_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
}
#endif