From 898db117e526828a87a0aac8e1200b03da4586d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B7=E5=BA=8A=E5=B0=B1=E7=8A=AF=E5=9B=B0?= <11730503+psx123456@user.noreply.gitee.com> Date: Fri, 9 May 2025 16:40:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- APP/README.md | 32 - APP/application/Src/start.c | 4 +- APP/businessLogic/Inc/bl_usart.h | 2 - APP/businessLogic/Inc/inFlash.h | 8 +- APP/businessLogic/Inc/parameter.h | 1 + APP/businessLogic/Src/Init.c | 4 +- APP/businessLogic/Src/bl_chargControl.c | 28 +- APP/businessLogic/Src/bl_usart.c | 275 ++++++-- APP/businessLogic/Src/inFlash.c | 47 +- APP/businessLogic/Src/parameter.c | 8 +- APP/businessLogic/Src/soc.c | 4 +- APP/businessLogic/Src/task.c | 15 +- APP/functionalModule/Src/capture.c | 3 +- APP/hardwareDriver/Src/HD_ADC.c | 11 +- Core/Src/main.c | 32 + Core/Src/system_stm32g4xx.c | 620 +++++------------- .../STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c | 37 ++ EWARM/chargeController.ewd | 2 +- EWARM/chargeController.ewp | 8 +- EWARM/stm32g431xx_flash -APP.icf | 36 + EWARM/stm32g431xx_flash -APP1.icf | 36 + EWARM/stm32g431xx_flash_upgrade.icf | 36 + tools/TimeSliceOffset/timeSliceOffset.c | 1 - 24 files changed, 659 insertions(+), 594 deletions(-) delete mode 100644 APP/README.md create mode 100644 EWARM/stm32g431xx_flash -APP.icf create mode 100644 EWARM/stm32g431xx_flash -APP1.icf create mode 100644 EWARM/stm32g431xx_flash_upgrade.icf diff --git a/.gitignore b/.gitignore index 5ccb1f7..93bdaae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ EWARM/chargeController/ -EWARM/settings/ \ No newline at end of file +EWARM/settings/ +EWARM/upgrade/ \ No newline at end of file diff --git a/APP/README.md b/APP/README.md deleted file mode 100644 index 91b5920..0000000 --- a/APP/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# application应用层 - - - - - - - -# businessLogic业务逻辑层 - - - - - - - - - - - -# functionalModule功能模块层 - - - - - - - - - -# hardwareDriver硬件驱动层 - diff --git a/APP/application/Src/start.c b/APP/application/Src/start.c index 209558a..a7e0b76 100644 --- a/APP/application/Src/start.c +++ b/APP/application/Src/start.c @@ -72,11 +72,11 @@ void start(void) // HAL_Delay(5000); // while (1) { - // cfgTest(); + // cfgTest(); // HAL_Delay(1000); // } /* 启动事件 */ - // insertEventsOrderRecord(startEvent); + insertEventsOrderRecord(startEvent); TimeSliceOffset_Start(); } diff --git a/APP/businessLogic/Inc/bl_usart.h b/APP/businessLogic/Inc/bl_usart.h index bca9d1e..7572b05 100644 --- a/APP/businessLogic/Inc/bl_usart.h +++ b/APP/businessLogic/Inc/bl_usart.h @@ -12,8 +12,6 @@ - - /* SL协议读取寄存器最大地址 */ #define maxReadRegAddrMacro 0x0150 /* SL协议读取寄存器最小地址 */ diff --git a/APP/businessLogic/Inc/inFlash.h b/APP/businessLogic/Inc/inFlash.h index 709302b..02cc0d9 100644 --- a/APP/businessLogic/Inc/inFlash.h +++ b/APP/businessLogic/Inc/inFlash.h @@ -87,7 +87,7 @@ typedef struct _config_info{ uint8_t uniqueDeviceID[7]; /* 设备唯一ID */ uint32_t gw485_Baud; /* 串口波特率 */ uint32_t bat485_Baud; /* 串口波特率,为0代表bms不支持通信 */ - uint8_t powerBoxType; /* 是否只充当电源板:0x00:不是;0x01:是*/ + uint8_t powerBoxType; /* 是否只充当电源板:0x00:不是;0xff:是*/ float constantVoltageV; /* 恒压充电阈值电压(V) */ float floatI; /* 浮充充电阈值电流(A) */ float startSolarOpenCircuitV; /* 启动充电太阳能板开路电压(V) */ @@ -115,6 +115,12 @@ typedef struct _config_info{ uint16_t collectOpenCircuitVoltageTime; /* 充电时采集开路电压的间隔时间 */ float reverseChargProtectionCurr; /* 反向充电保护电流 */ + float softStartVolt; /* 软启动阈值电压 */ + float MPPTConstantVoltage; /* MPPT恒定输出电压 */ + float MPPTReduceConstantVoltage; /* MPPT降功率运行时控制输入电压稳定值 */ + float underVoltageProtection; /* 电池欠压保护电压 */ + + uint16_t crc; /* 校验 */ }config_info; #define CONFIG_INFO_SIZE (sizeof(config_info)) diff --git a/APP/businessLogic/Inc/parameter.h b/APP/businessLogic/Inc/parameter.h index dc34d0b..d88b4bd 100644 --- a/APP/businessLogic/Inc/parameter.h +++ b/APP/businessLogic/Inc/parameter.h @@ -44,6 +44,7 @@ typedef struct _config_parameter{ float FloatChargeV; /* 浮充充电时的输出电压(V) */ uint16_t collectOpenCircuitVoltageTime; /* 充电时采集开路电压的间隔时间 */ float reverseChargProtectionCurr; /* 反向充电保护电流 */ + float softStartVolt; /* 软启动阈值电压 */ float MPPTConstantVoltage; /* MPPT恒定输出电压 */ float MPPTReduceConstantVoltage; /* MPPT降功率运行时控制输入电压稳定值 */ diff --git a/APP/businessLogic/Src/Init.c b/APP/businessLogic/Src/Init.c index d1fa58e..218be3d 100644 --- a/APP/businessLogic/Src/Init.c +++ b/APP/businessLogic/Src/Init.c @@ -21,7 +21,6 @@ void Init(void) { // HAL_Delay(10000); - config_info_start(); Init_debug_uart(); @@ -31,9 +30,10 @@ void Init(void) g_cfgParameter.firstStageProtectionValue = setfirstStageProtectionValue(g_cfgParameter.firstStageProtectionCurr); - FM_GPIO_Init(); tim_Init(); FM_RTC_Init(); + + FM_GPIO_Init(); Init_BAT485_uart(g_cfgParameter.bat485_Baud); Init_GW485_uart(g_cfgParameter.gw485_Baud); diff --git a/APP/businessLogic/Src/bl_chargControl.c b/APP/businessLogic/Src/bl_chargControl.c index 247b4ad..6c5cfdb 100644 --- a/APP/businessLogic/Src/bl_chargControl.c +++ b/APP/businessLogic/Src/bl_chargControl.c @@ -49,7 +49,7 @@ void setPIControlStep(float *PI_step) */ void mppt_constantVoltage(float InVoltage) { - static float kp = 0.005f; + static float kp = 0.0005f; // static float ki = 0.00001; // static float ki = 0.1f; static float ki = 10.0f; @@ -85,7 +85,7 @@ void mppt_constantVoltage(float InVoltage) */ void mppt_constantVoltageNoBatteryO(float OutVoltage) { - static float kp = 0.005f; + static float kp = 0.002f; // static float ki = 0.00001; // static float ki = 0.1f; static float ki = 10.0f; @@ -116,7 +116,7 @@ void mppt_constantVoltageO(float OutVoltage) // static float lastVolt = 0; // static float lastStepPwm = 0; static float lastDutyRatio = 0; - static float kp = 0.005f; + static float kp = 0.002f; // static float ki = 0.00001; // static float ki = 0.1f; static float ki = 10.0f; @@ -432,7 +432,7 @@ void mppt_readJust(void) static uint8_t currMinFlag1 = 0; // if (getChargCurrent() < 0.8f) { - if (totalChargeCurr < 120) { + if (totalChargeCurr < 100) { // hysteresisValue1 = getChargCurrent() * 1.7f; // hysteresisValue2 = getChargCurrent() * 12; currMinFlag++; @@ -444,7 +444,7 @@ void mppt_readJust(void) return; } - + // else if (getChargCurrent() < 3 && currMinFlag1) { else if (totalChargeCurr < 150 && currMinFlag1) { // currMinFlag1 = 0; @@ -452,14 +452,24 @@ void mppt_readJust(void) return; } - // else if (getChargCurrent() < 7) { - else if (totalChargeCurr < 350) { + // else if (getChargCurrent() < 5) { + else if (totalChargeCurr < 250) { currMinFlag1 = 0; currMinFlag = 0; // hysteresisValue1 = getChargCurrent() * 1.1f; // hysteresisValue2 = getChargCurrent() * 10; - hysteresisValue1 = totalChargeCurr / 40.0f; - hysteresisValue2 = totalChargeCurr / 4.0f; + hysteresisValue1 = totalChargeCurr / 35.0f; + hysteresisValue2 = totalChargeCurr / 3.5f; + } + + // else if (getChargCurrent() < 13) { + else if (totalChargeCurr < 650) { + currMinFlag1 = 0; + currMinFlag = 0; + // hysteresisValue1 = getChargCurrent() * 1.1f; + // hysteresisValue2 = getChargCurrent() * 10; + hysteresisValue1 = totalChargeCurr / 45.0f; + hysteresisValue2 = totalChargeCurr / 4.5f; } // else if (getChargCurrent() < 20) { diff --git a/APP/businessLogic/Src/bl_usart.c b/APP/businessLogic/Src/bl_usart.c index 2898964..587c2c4 100644 --- a/APP/businessLogic/Src/bl_usart.c +++ b/APP/businessLogic/Src/bl_usart.c @@ -132,8 +132,6 @@ typedef enum { // checkCodeHY, /* 接收到HY校验位 */ // endFlagHY, /* 接收到HY帧尾 */ #endif - - } uartStateMachine; /* 功能码 */ @@ -246,10 +244,15 @@ typedef enum { float_ChargeV = 0x0118, /* (2字节)浮充充电时的输出电压(*10再强转u16(V)) */ collect_OpenCircuit_Voltage_Time = 0x0119, /* (2字节)充电时采集开路电压的间隔时间(S) */ reverse_Charge_Protection_Curr = 0x011A, /* (2字节)反向充电保护电流(A) */ + + soft_Start_Volt = 0x011B, /* (2字节)软启动电压(*10再强转u16(V)) */ + MPPT_Constant_Voltage = 0x011C, /* (2字节)MPPT恒定输出电压(*10再强转u16(V)) */ + MPPT_Reduce_Constant_Voltage = 0x011D, /* (2字节)MPPT降功率运行时控制输入电压稳定值(*10再强转u16(V)) */ + under_Voltage_Protection = 0x011E, /* (2字节)欠压保护电压(*10再强转u16(V)) */ }cfgFileType; #define gw485RxBufferSize 256 - +#define regLength 19 /* 计时参数,1min后没解析整个配置文件,丢掉当前数据 */ static uint32_t gw485CfgTime = 0; @@ -739,7 +742,8 @@ BOOL analysisWait(void) if (gw485RxBufferIndex < maxLen) { return FALSE; - } + } + log_error("analysisWait \n"); state = wait; gw485RxBufferIndex--; memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); @@ -784,6 +788,7 @@ BOOL analysisStartFlagSL(void) return FALSE; } + log_error("analysisStartFlagSL \n"); state = wait; gw485RxBufferIndex--; memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); @@ -840,6 +845,7 @@ BOOL analysisAddressSL(void) if (gw485RxBufferIndex < maxLen) { return FALSE; } + log_error("analysisAddressSL \n"); state = wait; gw485RxBufferIndex--; memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); @@ -934,6 +940,7 @@ BOOL analysisFunctionCodeSL(void) if (gw485RxBufferIndex < maxLen) { return FALSE; } + log_error("analysisFunctionCodeSL \n"); state = wait; gw485RxBufferIndex--; memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); @@ -964,6 +971,7 @@ BOOL analysisReadRegStartAddressSL(void) if (gw485RxBufferIndex < maxLen) { return FALSE; } + log_error("analysisReadRegStartAddressSL \n"); state = wait; gw485RxBufferIndex--; memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); @@ -1016,6 +1024,7 @@ BOOL analysisCrcCheckBitSL(void) } } + log_error("analysisCrcCheckBitSL \n"); state = wait; gw485RxBufferIndex--; memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); @@ -1064,6 +1073,7 @@ void analysisEndFlagSL(device_handle device) SL_MsgProcFunc_Read_SOE(device, gw485RxBuffer, frameLength); } + // log_error("analysisEndFlagSL \n"); state = wait; gw485RxBufferIndex = 0; // memcpy(gw485RxBuffer, gw485RxBuffer + gw485RxBufferIndex, ); @@ -1198,7 +1208,7 @@ BOOL analysisRegStatusSL(void) // gw485RxBufferIndex--; // memcpy(gw485RxBuffer, gw485RxBuffer + 1, gw485RxBufferIndex); // return FALSE; - + return checkCrcSl(); } @@ -1303,9 +1313,6 @@ BOOL analysisReadSOELength(void) return checkCrcSl(); } - - - /* 读取寄存器 */ /** * @brief 读取太阳能开路电压 @@ -1314,7 +1321,7 @@ BOOL analysisReadSOELength(void) */ uint16_t SL_ReadRegisterSolarOpenCircuitVoltage(void *pMsg) { - return (uint16_t)(getSolarOpenCircuitVoltage() * floatMagnification); + return (uint16_t)(getSolarOpenCircuitVoltage() * floatMagnification + 0.5f); } /** @@ -1324,7 +1331,7 @@ uint16_t SL_ReadRegisterSolarOpenCircuitVoltage(void *pMsg) */ uint16_t SL_ReadRegisterSolarInputVoltage(void *pMsg) { - return (uint16_t)(getSolarInCircuitVoltage() * floatMagnification); + return (uint16_t)(getSolarInCircuitVoltage() * floatMagnification + 0.5f); } /** @@ -1334,7 +1341,7 @@ uint16_t SL_ReadRegisterSolarInputVoltage(void *pMsg) */ uint16_t SL_ReadRegisterOutputVoltage(void *pMsg) { - return (uint16_t)(getOutputVoltage() * floatMagnification); + return (uint16_t)(getOutputVoltage() * floatMagnification + 0.5f); } /** @@ -1344,7 +1351,7 @@ uint16_t SL_ReadRegisterOutputVoltage(void *pMsg) */ uint16_t SL_ReadRegisterBatteryVoltage(void *pMsg) { - return (uint16_t)(getBatteryVoltage() * floatMagnification); + return (uint16_t)(getBatteryVoltage() * floatMagnification + 0.5f); } /** @@ -1354,7 +1361,7 @@ uint16_t SL_ReadRegisterBatteryVoltage(void *pMsg) */ uint16_t SL_ReadRegisterChargCurrent(void *pMsg) { - return (uint16_t)(getChargCurrent() * floatMagnification); + return (uint16_t)(getChargCurrent() * floatMagnification + 0.5f); } /** @@ -1364,7 +1371,7 @@ uint16_t SL_ReadRegisterChargCurrent(void *pMsg) */ uint16_t SL_ReadRegisterDischargCurrent(void *pMsg) { - return (uint16_t)(getDischargCurrent() * floatMagnification); + return (uint16_t)(getDischargCurrent() * floatMagnification + 0.5f); } /** @@ -1374,7 +1381,7 @@ uint16_t SL_ReadRegisterDischargCurrent(void *pMsg) */ uint16_t SL_ReadRegisterHighSideMosTemperature(void *pMsg) { - return (uint16_t)(getHighSideMosTemperature() * floatMagnification); + return (uint16_t)(getHighSideMosTemperature() * floatMagnification + 0.5f); } /** @@ -1414,7 +1421,7 @@ uint16_t SL_ReadRegisterChargState(void *pMsg) uint16_t SL_ReadRegisterTotalChargCapacity(void *pMsg) { volatile uint16_t temp = 0; - temp = (uint16_t)(getTotalChargCapacity() * floatMagnification * 1000); + temp = (uint16_t)(getTotalChargCapacity() * floatMagnification * 1000 + 0.5f); totalChargCapacityInt(0); return temp; } @@ -1427,7 +1434,7 @@ uint16_t SL_ReadRegisterTotalChargCapacity(void *pMsg) uint16_t SL_ReadRegisterTotalElectricityConsumption(void *pMsg) { volatile uint16_t temp = 0; - temp = (uint16_t)(getTotalElectricityConsumption() * floatMagnification * 1000); + temp = (uint16_t)(getTotalElectricityConsumption() * floatMagnification * 1000 + 0.5f); totalElectricityConsumptionInt(0); return temp; } @@ -1724,7 +1731,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 恒压充电阈值电压 */ else if (dataType == constant_Voltage_V) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 14.4f || tempFloat < 13.5f) { return NULL; } @@ -1735,7 +1742,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 浮充充电阈值电流 */ else if (dataType == float_I) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 0.3f || tempFloat < 0.01f) { return NULL; } @@ -1746,8 +1753,8 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 启动充电太阳能板开路电压 */ else if (dataType == start_Solar_Open_Circuit_V) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; - if (tempFloat > 18.0f || tempFloat < 15.5f) { + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + if (tempFloat > 20.0f || tempFloat < 16.5f) { return NULL; } @@ -1757,7 +1764,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 关闭充电太阳能板输出电压 */ else if (dataType == stop_Solar_Output_Circuit_V) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 16 || tempFloat < 15) { return NULL; } @@ -1818,7 +1825,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) else if (dataType == first_Stage_Protection_Delay) { uint16_t tempU16 = (*(pMsg + 2) << 8) | *(pMsg + 3); - if (tempU16 > 100 || tempU16 < 1) { + if (tempU16 > 1000 || tempU16 < 1) { return NULL; } @@ -1828,7 +1835,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 软件第一段保护阈值电流 */ else if (dataType == first_Stage_Protection_Curr) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 60 || tempFloat < 1) { return NULL; } @@ -1839,7 +1846,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 软件第二段保护延时 */ else if (dataType == second_Stage_Protection_Delay) { - uint16_t tempU16 = (*(pMsg + 2) << 8) | *(pMsg + 3); + uint16_t tempU16 = (*(pMsg + 2) << 8) | *(pMsg + 3); if (tempU16 > 60000 || tempU16 < 10) { return NULL; } @@ -1850,7 +1857,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 软件第二段保护阈值电流 */ else if (dataType == second_Stage_Protection_Curr) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 50 || tempFloat < 1) { return NULL; } @@ -1863,8 +1870,8 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 软件第三段保护延时 */ else if (dataType == third_Stage_Protection_Delay) { uint32_t tempU32 = (*(pMsg + 2) << 24) | (*(pMsg + 3) << 16) | (*(pMsg + 4) << 8) | *(pMsg + 5); - if (tempU32 > 1000000 || tempU32 < 10) { - // debug_printf("third_Stage_Protection_Delay = %d\n", tempU32); + if (tempU32 > 10000000 || tempU32 < 10) { + log_info("third_Stage_Protection_Delay = %d\n", tempU32); return NULL; } @@ -1874,9 +1881,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 软件第三段保护阈值电流 */ else if (dataType == third_Stage_Protection_Curr) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 50 || tempFloat < 1) { - // debug_printf("third_Stage_Protection_Curr\n"); + log_info("third_Stage_Protection_Curr\n"); return NULL; } @@ -1888,7 +1895,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) else if (dataType == input_Power_Low_Detection_Delay) { uint16_t tempU16 = (*(pMsg + 2) << 8) | *(pMsg + 3); if (tempU16 > 1000 || tempU16 < 2) { - // debug_printf("input_Power_Low_Detection_Delay\n"); + log_info("input_Power_Low_Detection_Delay\n"); return NULL; } @@ -1898,9 +1905,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 前端输入功率不足检测电压 */ else if (dataType == input_Power_Low_Detection_Volt) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; - if (tempFloat > 13 || tempFloat < 8) { - // debug_printf("input_Power_Low_Detection_Volt\n"); + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + if (tempFloat > 15 || tempFloat < 8) { + log_info("input_Power_Low_Detection_Volt\n"); return NULL; } @@ -1910,9 +1917,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 最大太阳能板输出电压 */ else if (dataType == max_Open_Solar_Output_Circuit_V) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 27 || tempFloat < 22) { - // debug_printf("max_Open_Solar_Output_Circuit_V\n"); + log_info("max_Open_Solar_Output_Circuit_V\n"); return NULL; } @@ -1922,9 +1929,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 最大充电电流 */ else if (dataType == max_Charg_Curr) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 40 || tempFloat < 20) { - // debug_printf("max_Charg_Curr\n"); + log_info("max_Charg_Curr\n"); return NULL; } // debug_printf("max_Charg_Curr : %f\n", tempFloat); @@ -1935,9 +1942,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 检测回路阻抗时的最小充电电流 */ else if (dataType == min_Check_Loop_Impedance_Charg_Curr) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 20 || tempFloat < 2) { - // debug_printf("min_Check_Loop_Impedance_Charg_Curr\n"); + log_info("min_Check_Loop_Impedance_Charg_Curr\n"); return NULL; } @@ -1947,9 +1954,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 满功率输出温度 */ else if (dataType == full_Power_Output_Temperature) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 100 || tempFloat < 30) { - // debug_printf("full_Power_Output_Temperature\n"); + log_info("full_Power_Output_Temperature\n"); return NULL; } @@ -1959,9 +1966,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 降功率输出温度 */ else if (dataType == reduce_Power_Output_Temperature) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 100 || tempFloat < 30) { - // debug_printf("reduce_Power_Output_Temperature\n"); + log_info("reduce_Power_Output_Temperature\n"); return NULL; } @@ -1971,9 +1978,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 停止输出温度 */ else if (dataType == stop_PowerOutput_Temperature) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 100 || tempFloat < 30) { - // debug_printf("stop_PowerOutput_Temperature\n"); + log_info("stop_PowerOutput_Temperature\n"); return NULL; } @@ -1983,9 +1990,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 恒压充电输出电压 */ else if (dataType == constant_Voltage_Charge_V) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 14.5f || tempFloat < 14) { - // debug_printf("constant_Voltage_Charge_V\n"); + log_info("constant_Voltage_Charge_V\n"); return NULL; } @@ -1995,8 +2002,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 浮充充电输出电压 */ else if (dataType == float_ChargeV) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 14.5f || tempFloat < 13) { + log_info("cfg float_ChargeV error\n"); return NULL; } @@ -2008,6 +2016,7 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) else if (dataType == collect_OpenCircuit_Voltage_Time) { uint16_t tempU16 = (*(pMsg + 2) << 8) | *(pMsg + 3); if (tempU16 > 10000 || tempU16 < 100) { + log_info("cfg collect_OpenCircuit_Voltage_Time error\n"); return NULL; } @@ -2017,8 +2026,9 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) /* 配置 反向充电保护电流 */ else if (dataType == reverse_Charge_Protection_Curr) { - float tempFloat = ((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; if (tempFloat > 12.0f || tempFloat < 0.1f) { + log_info("cfg reverse_Charge_Protection_Curr error\n"); return NULL; } @@ -2026,6 +2036,55 @@ uint8_t *analysisDistributionProfile(uint8_t *pMsg, config_info *temp) return (pMsg + 4); } + /* 配置 软启动电压 */ + else if (dataType == soft_Start_Volt) { + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + if (tempFloat < 18.0f || tempFloat > 22.0f) { +// log_info("cfg soft_Start_Volt error : %f\n", tempFloat); + return NULL; + } + + temp->softStartVolt = tempFloat; + return (pMsg + 4); + } + + /* 读取 MPPT恒定输出电压 */ + else if (dataType == MPPT_Constant_Voltage) { + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + if (tempFloat < 15.0f || tempFloat > 19.0f) { + log_info("cfg MPPT_Constant_Voltage error\n"); + return NULL; + } + + temp->MPPTConstantVoltage = tempFloat; + return (pMsg + 4); + } + + /* 读取 MPPT降功率运行时控制输入电压稳定值 */ + else if (dataType == MPPT_Reduce_Constant_Voltage) { + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + if (tempFloat < 12.0f || tempFloat > 22.0f) { + log_info("cfg MPPT_Reduce_Constant_Voltage error\n"); + return NULL; + } + + temp->MPPTReduceConstantVoltage = tempFloat; + return (pMsg + 4); + } + + /* 读取 欠压保护电压 */ + else if (dataType == under_Voltage_Protection) { + float tempFloat = (float)((*(pMsg + 2) << 8) | *(pMsg + 3)) / floatMagnification; + if (tempFloat > 15.0f) { + log_info("cfg under_Voltage_Protection error\n"); + return NULL; + } + + temp->underVoltageProtection = tempFloat; + return (pMsg + 4); + } + + log_info("cfg file error\n"); return NULL; } @@ -2409,7 +2468,47 @@ uint8_t *analysisReadProfile(uint8_t *pMsg, uint8_t **outData, uint16_t *dataLen return (pMsg + 2); } - // debug_printf("dataType error : %d\n", dataType); + /* 读取 软启动电压 */ + else if (dataType == soft_Start_Volt) { + uint16_t tempU16 = (uint16_t)(g_cfgParameter.softStartVolt * floatMagnification); + *(*outData + 2) = (uint8_t)(tempU16 >> 8); + *(*outData + 3) = (uint8_t)(tempU16 & 0x00FF); + *outData += 4; + *dataLen += 4; + return (pMsg + 2); + } + + /* 读取 MPPT恒定输出电压 */ + else if (dataType == MPPT_Constant_Voltage) { + uint16_t tempU16 = (uint16_t)(g_cfgParameter.MPPTConstantVoltage * floatMagnification); + *(*outData + 2) = (uint8_t)(tempU16 >> 8); + *(*outData + 3) = (uint8_t)(tempU16 & 0x00FF); + *outData += 4; + *dataLen += 4; + return (pMsg + 2); + } + + /* 读取 MPPT降功率运行时控制输入电压稳定值 */ + else if (dataType == MPPT_Reduce_Constant_Voltage) { + uint16_t tempU16 = (uint16_t)(g_cfgParameter.MPPTReduceConstantVoltage * floatMagnification); + *(*outData + 2) = (uint8_t)(tempU16 >> 8); + *(*outData + 3) = (uint8_t)(tempU16 & 0x00FF); + *outData += 4; + *dataLen += 4; + return (pMsg + 2); + } + + /* 读取 欠压保护电压 */ + else if (dataType == under_Voltage_Protection) { + uint16_t tempU16 = (uint16_t)(g_cfgParameter.underVoltageProtection * floatMagnification); + *(*outData + 2) = (uint8_t)(tempU16 >> 8); + *(*outData + 3) = (uint8_t)(tempU16 & 0x00FF); + *outData += 4; + *dataLen += 4; + return (pMsg + 2); + } + + debug_printf("dataType error : %d\n", dataType); return NULL; } @@ -2530,7 +2629,61 @@ void SL_MsgProcFunc_Write_Register(device_handle device, void *pMsg, uint32_t Ms */ void SL_MsgProcFunc_Broadcast_Scan(device_handle device, void *pMsg, uint32_t MsgLen) { - debug_printf("SL_MsgProcFunc_Broadcast_Scan\n"); + // debug_printf("SL_MsgProcFunc_Broadcast_Scan\n"); + uint8_t *replay_pack = getInsertData(); + + /* 起始标志 */ + *(replay_pack) = g_cfgParameter.startFlagSL[0]; + *(replay_pack + 1) = g_cfgParameter.startFlagSL[1]; + + /* 地址 */ + replay_pack += 2; + *(replay_pack) = 0xFF; + *(replay_pack + 1) = 0xFF; + *(replay_pack + 2) = 0xFF; + *(replay_pack + 3) = 0xFF; + *(replay_pack + 4) = 0xFF; + *(replay_pack + 5) = 0xFF; + *(replay_pack + 6) = 0xFF; + + /* 功能码 */ + replay_pack += 7; + *replay_pack = SL_Function_Code_Registration_request; + + /* 寄存器长度 */ + replay_pack += 1; + *replay_pack = (regLength >> 8) & 0xFF; + *replay_pack += 1; + *replay_pack = regLength & 0xFF; + + /* 注册状态 */ + replay_pack += 3; + + /* 接入节点ID */ + *(replay_pack) = g_cfgParameter.uniqueDeviceID[0]; + *(replay_pack + 1) = g_cfgParameter.uniqueDeviceID[1]; + *(replay_pack + 2) = g_cfgParameter.uniqueDeviceID[2]; + *(replay_pack + 3) = g_cfgParameter.uniqueDeviceID[3]; + *(replay_pack + 4) = g_cfgParameter.uniqueDeviceID[4]; + *(replay_pack + 5) = g_cfgParameter.uniqueDeviceID[5]; + *(replay_pack + 6) = g_cfgParameter.uniqueDeviceID[6]; + + /* 接入节点类型 */ + replay_pack += 8; + *replay_pack = 1; + + /* 校验位 */ + replay_pack += 1; + uint16_t crc_temp = checkModebusCrc(getInsertData(), 23); + *replay_pack = (uint8_t)(crc_temp >> 8); + replay_pack += 1; + *replay_pack = (uint8_t)crc_temp; + + /* 结束标志 */ + replay_pack += 1; + *replay_pack = g_cfgParameter.endFlagSL; + + uart_insertDataSend(device, 26); } /** @@ -2542,7 +2695,7 @@ void SL_MsgProcFunc_Broadcast_Scan(device_handle device, void *pMsg, uint32_t Ms */ void SL_MsgProcFunc_Registration_request(device_handle device, void *pMsg, uint32_t MsgLen) { - debug_printf("SL_MsgProcFunc_Registration_request\n"); + // debug_printf("SL_MsgProcFunc_Registration_request\n"); } /** @@ -2662,14 +2815,14 @@ wholePackageCorrect: cfgInfo.crc = checkModebusCrc((uint8_t *)&cfgInfo, CONFIG_INFO_SIZE - 2); saveConfigInfo(&cfgInfo); - float tempF; - tempF = getTotalElectricityConsumption(); - savetotalElectricityConsumption(&tempF); - tempF = getTotalChargCapacity(); - savetotalChargCapacity(&tempF); - timeInfo time; - time = getLastTime(); - saveTime(&time); + // float tempF; + // tempF = getTotalElectricityConsumption(); + // savetotalElectricityConsumption(&tempF); + // tempF = getTotalChargCapacity(); + // savetotalChargCapacity(&tempF); + // timeInfo time; + // time = getLastTime(); + // saveTime(&time); return; diff --git a/APP/businessLogic/Src/inFlash.c b/APP/businessLogic/Src/inFlash.c index 585292d..e4fb56f 100644 --- a/APP/businessLogic/Src/inFlash.c +++ b/APP/businessLogic/Src/inFlash.c @@ -152,45 +152,50 @@ void readFlashContent(config_info *configInfo) configInfo->uniqueDeviceID[5] = 0x11; configInfo->uniqueDeviceID[6] = 0x11; - configInfo->gw485_Baud = 115200; - configInfo->bat485_Baud = 115200; + configInfo->gw485_Baud = 9600; + configInfo->bat485_Baud = 9600; configInfo->powerBoxType = 0xFF; configInfo->constantVoltageV = 14; - configInfo->floatI = 0.1f; - configInfo->startSolarOpenCircuitV = 17; + configInfo->floatI = 0.2f; + configInfo->startSolarOpenCircuitV = 19; configInfo->stopSolarOutputCircuitV = 15; configInfo->checkCanStartTime = 5; configInfo->shortCircuitJudgmentDelay = 10; - configInfo->inputPowerLowJudgmentDelay = 30; + configInfo->inputPowerLowJudgmentDelay = 60; configInfo->inputPowerLowAgainOutputDelay = 1800; - configInfo->firstStageProtectionDelay = 2; + configInfo->firstStageProtectionDelay = 200; configInfo->firstStageProtectionCurr = 50; - configInfo->secondStageProtectionDelay = 50000; + configInfo->secondStageProtectionDelay = 20000; configInfo->secondStageProtectionCurr = 35; configInfo->thirdStageProtectionDelay = 600000; configInfo->thirdStageProtectionCurr = 30; configInfo->inputPowerLowDetectionDelay = 10; - configInfo->inputPowerLowDetectionVolt = 10.0f; + configInfo->inputPowerLowDetectionVolt = 14.0f; configInfo->maxOpenSolarOutputCircuitV = 25; - configInfo->maxChargCurr = 35; + configInfo->maxChargCurr = 30; configInfo->minCheckLoopImpedanceChargCurr = 5; - configInfo->stopPowerOutputTemperature = 100; - configInfo->reducePowerOutputTemperature = 90; - configInfo->fullPowerOutputTemperature = 50; + configInfo->stopPowerOutputTemperature = 90; + configInfo->reducePowerOutputTemperature = 70; + configInfo->fullPowerOutputTemperature = 60; configInfo->constantVoltageChargeV = 14.4f; configInfo->FloatChargeV = 14.2f; configInfo->collectOpenCircuitVoltageTime = 1800; - configInfo->reverseChargProtectionCurr = 2; + configInfo->reverseChargProtectionCurr = 1; + + configInfo->softStartVolt = 18.5f; + configInfo->MPPTConstantVoltage = 17.2f; + configInfo->MPPTReduceConstantVoltage = 20.0f; + configInfo->underVoltageProtection = 11.0f; // configInfo->firstStageProtectionCurr = firstStageProtectionCurrMacro; // configInfo->firstStageProtectionDelay = firstStageProtectionDelayMacro; @@ -234,7 +239,7 @@ void config_info_start(void) // g_cfgParameter.bat485_Baud = 115200; // static volatile uint32_t tempBatBaud; // tempBatBaud = temp_configInfo.bat485_Baud; - + g_cfgParameter.uniqueDeviceID[0] = temp_configInfo.uniqueDeviceID[0]; g_cfgParameter.uniqueDeviceID[1] = temp_configInfo.uniqueDeviceID[1]; g_cfgParameter.uniqueDeviceID[2] = temp_configInfo.uniqueDeviceID[2]; @@ -278,11 +283,15 @@ void config_info_start(void) g_cfgParameter.collectOpenCircuitVoltageTime= temp_configInfo.collectOpenCircuitVoltageTime; g_cfgParameter.reverseChargProtectionCurr = temp_configInfo.reverseChargProtectionCurr; - g_cfgParameter.softStartVolt = 18.5f; - g_cfgParameter.MPPTConstantVoltage = 17.0f; - g_cfgParameter.MPPTReduceConstantVoltage = 20.0f; - g_cfgParameter.underVoltageProtection = 12.0f; - + // g_cfgParameter.softStartVolt = 18.5f; + // g_cfgParameter.MPPTConstantVoltage = 17.5f; + // g_cfgParameter.MPPTReduceConstantVoltage = 20.0f; + // g_cfgParameter.underVoltageProtection = 11.0f; + g_cfgParameter.softStartVolt = temp_configInfo.softStartVolt; + g_cfgParameter.MPPTConstantVoltage = temp_configInfo.MPPTConstantVoltage; + g_cfgParameter.MPPTReduceConstantVoltage = temp_configInfo.MPPTReduceConstantVoltage; + g_cfgParameter.underVoltageProtection = temp_configInfo.underVoltageProtection; + /* 读取的回路阻抗无效则回路阻抗设置为0 */ float fTemp; // fTemp = 0.01f; diff --git a/APP/businessLogic/Src/parameter.c b/APP/businessLogic/Src/parameter.c index 4bfaca5..89e8cad 100644 --- a/APP/businessLogic/Src/parameter.c +++ b/APP/businessLogic/Src/parameter.c @@ -212,7 +212,13 @@ void setOutputVoltage(void) */ float getChargCurrent(void) { - return g_otherParameter.Charg_Current; + if (getChargMosState() == TRUE) { + return g_otherParameter.Charg_Current; + } + else { + return 0; + } + } /** diff --git a/APP/businessLogic/Src/soc.c b/APP/businessLogic/Src/soc.c index 22c1307..8367d2c 100644 --- a/APP/businessLogic/Src/soc.c +++ b/APP/businessLogic/Src/soc.c @@ -65,10 +65,10 @@ */ const float ocv_table_4s[21] = { // 0-9% (单节2.80V~3.16V → 四节11.20V~12.64V) - 12.00, 12.64, // 0-9% + 11.2, 12.64, // 0-9% // 10-19% (单节3.18V~3.21V → 四节12.72V~12.84V) - 12.80, 12.84, // 10-19% + 12.72, 12.84, // 10-19% // 20-29% (单节3.22V~3.23V → 四节12.88V~12.92V) 12.88, 12.92, // 20-29% diff --git a/APP/businessLogic/Src/task.c b/APP/businessLogic/Src/task.c index 349994a..28d32e8 100644 --- a/APP/businessLogic/Src/task.c +++ b/APP/businessLogic/Src/task.c @@ -196,6 +196,8 @@ void chargRunLed(uint8_t mode) } } +#include "adc.h" + /** * @brief 喂狗任务 * @param None @@ -205,6 +207,14 @@ void Task_wdi(void) { feedDog(); + // debug_printf("adc1_7 : %d \n", HAL_ADCEx_Calibration_GetValue(&hadc1, ADC_CHANNEL_7)); + // debug_printf("adc1_8 : %d \n", HAL_ADCEx_Calibration_GetValue(&hadc1, ADC_CHANNEL_8)); + // debug_printf("adc1_11 : %d \n", HAL_ADCEx_Calibration_GetValue(&hadc1, ADC_CHANNEL_11)); + // debug_printf("adc1_15 : %d \n", HAL_ADCEx_Calibration_GetValue(&hadc1, ADC_CHANNEL_15)); + + // debug_printf("adc2_1 : %d \n", HAL_ADCEx_Calibration_GetValue(&hadc1, ADC_CHANNEL_1)); + + // debug_printf("chargCurrent:%f \n", getChargCurrent()); // debug_printf("outputVoltage:%f \n", getOutputVoltage()); // debug_printf("BatteryVoltage:%f \n", getBatteryVoltage()); @@ -367,7 +377,9 @@ void Task_refreshJudgeData(void) /* 连续两次电池电压过低,则关闭输出 */ static uint8_t numLow = 0; static uint8_t volageLowFlag = 0; - if (getBatteryState() && getBatteryVoltage() < g_cfgParameter.underVoltageProtection) { + if (getBatteryState() + && (getBatteryVoltage() < g_cfgParameter.underVoltageProtection) + && (getChargBatteryCurrent() < 0.5f)) { numLow++; } else { @@ -383,7 +395,6 @@ void Task_refreshJudgeData(void) setPowerOutput(TRUE); volageLowFlag = 0; } - } /** diff --git a/APP/functionalModule/Src/capture.c b/APP/functionalModule/Src/capture.c index 4664a77..878f40d 100644 --- a/APP/functionalModule/Src/capture.c +++ b/APP/functionalModule/Src/capture.c @@ -187,7 +187,8 @@ void proportionalInt(int mode) /* 仅充当电源盒 */ if (mode) { /* 光伏充电输出电流比例,放大倍数*电阻 */ - P_CHG_CURR = (1.0 / (50 * (1 / (1 / 0.01 + 1 / 0.002)))) * Proportion; + // P_CHG_CURR = (1.0 / (50 * (1 / (1 / 0.01 + 1 / 0.002)))) * Proportion; + P_CHG_CURR = (1.0 / (50 * (1 / (1 / 0.001 + 1 / 0.002)))) * Proportion; /* 充电控制盒输出电压比例,分压系数 */ P_PV_VOLT_OUT = ((56.0 + 10.0) / 10.0) * Proportion; /* 放电电流采集电流倍数 */ diff --git a/APP/hardwareDriver/Src/HD_ADC.c b/APP/hardwareDriver/Src/HD_ADC.c index 3d4bfce..bb74395 100644 --- a/APP/hardwareDriver/Src/HD_ADC.c +++ b/APP/hardwareDriver/Src/HD_ADC.c @@ -6,9 +6,18 @@ void HD_adc_Init(void) { MX_DMA_Init(); - MX_TIM6_Init(); MX_ADC1_Init(); MX_ADC2_Init(); + // HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); + // for (size_t i = 0; i < 1000; i++) { + // __NOP; + // } + // HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED); + // for (size_t i = 0; i < 1000; i++) { + // __NOP; + // } + + MX_TIM6_Init(); } /** diff --git a/Core/Src/main.c b/Core/Src/main.c index 12d8b20..f4dc0bc 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -137,6 +137,38 @@ int main(void) */ void SystemClock_Config(void) { +// RCC_OscInitTypeDef RCC_OscInitStruct = {0}; +// RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + +// /** Configure the main internal regulator output voltage +// */ +// HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); + +// /** Initializes the RCC Oscillators according to the specified parameters +// * in the RCC_OscInitTypeDef structure. +// */ +// RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; +// RCC_OscInitStruct.HSIState = RCC_HSI_ON; +// RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; +// RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; +// if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) +// { +// Error_Handler(); +// } + +// /** Initializes the CPU, AHB and APB buses clocks +// */ +// RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK +// |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; +// RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; +// RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; +// RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; +// RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + +// if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) +// { +// Error_Handler(); +// } RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; diff --git a/Core/Src/system_stm32g4xx.c b/Core/Src/system_stm32g4xx.c index 624bc6d..a02a813 100644 --- a/Core/Src/system_stm32g4xx.c +++ b/Core/Src/system_stm32g4xx.c @@ -1,6 +1,3 @@ -// #define REMOTE_UPDATE // 控制是否远程升级 - -#ifdef REMOTE_UPDATE /** ****************************************************************************** * @file system_stm32g4xx.c @@ -122,454 +119,171 @@ #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. This value must be a multiple of 0x200. */ #else -#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x000088B8U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ -#endif /* VECT_TAB_SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -/******************************************************************************/ -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Macros - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Variables - * @{ - */ - /* The SystemCoreClock variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetHCLKFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ - uint32_t SystemCoreClock = HSI_VALUE; - - const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; - const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Functions - * @{ - */ - -/** - * @brief Setup the microcontroller system. - * @param None - * @retval None - */ - -void SystemInit(void) -{ - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ - #endif - - /* Configure the Vector Table location add offset address ------------------*/ -#if defined(USER_VECT_TAB_ADDRESS) - SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -} - -/** - * @brief Update SystemCoreClock variable according to Clock Register Values. - * The SystemCoreClock variable contains the core clock (HCLK), it can - * be used by the user application to setup the SysTick timer or configure - * other parameters. - * - * @note Each time the core clock (HCLK) changes, this function must be called - * to update SystemCoreClock variable value. Otherwise, any configuration - * based on this variable will be incorrect. - * - * @note - The system frequency computed by this function is not the real - * frequency in the chip. It is calculated based on the predefined - * constant and the selected clock source: - * - * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) - * - * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) - * - * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) - * or HSI_VALUE(*) multiplied/divided by the PLL factors. - * - * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 16 MHz) but the real value may vary depending on the variations - * in voltage and temperature. - * - * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 24 MHz), user has to ensure that HSE_VALUE is same as the real - * frequency of the crystal used. Otherwise, this function may - * have wrong result. - * - * - The result of this function could be not correct when using fractional - * value for HSE crystal. - * - * @param None - * @retval None - */ -void SystemCoreClockUpdate(void) -{ - uint32_t tmp, pllvco, pllr, pllsource, pllm; - - /* Get SYSCLK source -------------------------------------------------------*/ - switch (RCC->CFGR & RCC_CFGR_SWS) - { - case 0x04: /* HSI used as system clock source */ - SystemCoreClock = HSI_VALUE; - break; - - case 0x08: /* HSE used as system clock source */ - SystemCoreClock = HSE_VALUE; - break; - - case 0x0C: /* PLL used as system clock source */ - /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN - SYSCLK = PLL_VCO / PLLR - */ - pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); - pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; - if (pllsource == 0x02UL) /* HSI used as PLL clock source */ - { - pllvco = (HSI_VALUE / pllm); - } - else /* HSE used as PLL clock source */ - { - pllvco = (HSE_VALUE / pllm); - } - pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); - pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; - SystemCoreClock = pllvco/pllr; - break; - - default: - break; - } - /* Compute HCLK clock frequency --------------------------------------------*/ - /* Get HCLK prescaler */ - tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; - /* HCLK clock frequency */ - SystemCoreClock >>= tmp; -} - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -#else - -/** - ****************************************************************************** - * @file system_stm32g4xx.c - * @author MCD Application Team - * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File - * - * This file provides two functions and one global variable to be called from - * user application: - * - SystemInit(): This function is called at startup just after reset and - * before branch to main program. This call is made inside - * the "startup_stm32g4xx.s" file. - * - * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used - * by the user application to setup the SysTick - * timer or configure other parameters. - * - * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must - * be called whenever the core clock is changed - * during program execution. - * - * After each device reset the HSI (16 MHz) is used as system clock source. - * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to - * configure the system clock before to branch to main program. - * - * This file configures the system clock as follows: - *============================================================================= - *----------------------------------------------------------------------------- - * System Clock source | HSI - *----------------------------------------------------------------------------- - * SYSCLK(Hz) | 16000000 - *----------------------------------------------------------------------------- - * HCLK(Hz) | 16000000 - *----------------------------------------------------------------------------- - * AHB Prescaler | 1 - *----------------------------------------------------------------------------- - * APB1 Prescaler | 1 - *----------------------------------------------------------------------------- - * APB2 Prescaler | 1 - *----------------------------------------------------------------------------- - * PLL_M | 1 - *----------------------------------------------------------------------------- - * PLL_N | 16 - *----------------------------------------------------------------------------- - * PLL_P | 7 - *----------------------------------------------------------------------------- - * PLL_Q | 2 - *----------------------------------------------------------------------------- - * PLL_R | 2 - *----------------------------------------------------------------------------- - * Require 48MHz for RNG | Disabled - *----------------------------------------------------------------------------- - *============================================================================= - ****************************************************************************** - * @attention - * - * Copyright (c) 2019 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ - -/** @addtogroup CMSIS - * @{ - */ - -/** @addtogroup stm32g4xx_system - * @{ - */ - -/** @addtogroup STM32G4xx_System_Private_Includes - * @{ - */ - -#include "stm32g4xx.h" - -#if !defined (HSE_VALUE) - #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSI_VALUE) - #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_TypesDefinitions - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Defines - * @{ - */ - -/************************* Miscellaneous Configuration ************************/ -/* Note: Following vector table addresses must be defined in line with linker - configuration. */ -/*!< Uncomment the following line if you need to relocate the vector table - anywhere in Flash or Sram, else the vector table is kept at the automatic - remap of boot address selected */ -/* #define USER_VECT_TAB_ADDRESS */ - -#if defined(USER_VECT_TAB_ADDRESS) -/*!< Uncomment the following line if you need to relocate your vector Table - in Sram else user remap will be done in Flash. */ -/* #define VECT_TAB_SRAM */ -#if defined(VECT_TAB_SRAM) -#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ -#else -#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ -#endif /* VECT_TAB_SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -/******************************************************************************/ -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Macros - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Variables - * @{ - */ - /* The SystemCoreClock variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetHCLKFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ - uint32_t SystemCoreClock = HSI_VALUE; - - const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; - const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32G4xx_System_Private_Functions - * @{ - */ - -/** - * @brief Setup the microcontroller system. - * @param None - * @retval None - */ - -void SystemInit(void) -{ - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ - #endif - - /* Configure the Vector Table location add offset address ------------------*/ -#if defined(USER_VECT_TAB_ADDRESS) - SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -} - -/** - * @brief Update SystemCoreClock variable according to Clock Register Values. - * The SystemCoreClock variable contains the core clock (HCLK), it can - * be used by the user application to setup the SysTick timer or configure - * other parameters. - * - * @note Each time the core clock (HCLK) changes, this function must be called - * to update SystemCoreClock variable value. Otherwise, any configuration - * based on this variable will be incorrect. - * - * @note - The system frequency computed by this function is not the real - * frequency in the chip. It is calculated based on the predefined - * constant and the selected clock source: - * - * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) - * - * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) - * - * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) - * or HSI_VALUE(*) multiplied/divided by the PLL factors. - * - * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 16 MHz) but the real value may vary depending on the variations - * in voltage and temperature. - * - * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 24 MHz), user has to ensure that HSE_VALUE is same as the real - * frequency of the crystal used. Otherwise, this function may - * have wrong result. - * - * - The result of this function could be not correct when using fractional - * value for HSE crystal. - * - * @param None - * @retval None - */ -void SystemCoreClockUpdate(void) -{ - uint32_t tmp, pllvco, pllr, pllsource, pllm; - - /* Get SYSCLK source -------------------------------------------------------*/ - switch (RCC->CFGR & RCC_CFGR_SWS) - { - case 0x04: /* HSI used as system clock source */ - SystemCoreClock = HSI_VALUE; - break; - - case 0x08: /* HSE used as system clock source */ - SystemCoreClock = HSE_VALUE; - break; - - case 0x0C: /* PLL used as system clock source */ - /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN - SYSCLK = PLL_VCO / PLLR - */ - pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); - pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; - if (pllsource == 0x02UL) /* HSI used as PLL clock source */ - { - pllvco = (HSI_VALUE / pllm); - } - else /* HSE used as PLL clock source */ - { - pllvco = (HSE_VALUE / pllm); - } - pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); - pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; - SystemCoreClock = pllvco/pllr; - break; - - default: - break; - } - /* Compute HCLK clock frequency --------------------------------------------*/ - /* Get HCLK prescaler */ - tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; - /* HCLK clock frequency */ - SystemCoreClock >>= tmp; -} - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - - +#define VECT_TAB_BASE_ADDRESS FLASH_BASE + +#if defined(OTA) +#define VECT_TAB_OFFSET 0x0000A000U +#else +#define VECT_TAB_OFFSET 0x00000000U #endif + +#endif /* VECT_TAB_SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ +/******************************************************************************/ +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Variables + * @{ + */ + /* The SystemCoreClock variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. + */ + uint32_t SystemCoreClock = HSI_VALUE; + + const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; + const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Functions + * @{ + */ + +/** + * @brief Setup the microcontroller system. + * @param None + * @retval None + */ + +void SystemInit(void) +{ + /* FPU settings ------------------------------------------------------------*/ + #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ + #endif + + /* Configure the Vector Table location add offset address ------------------*/ +#if defined(USER_VECT_TAB_ADDRESS) + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ +} + +/** + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 16 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 24 MHz), user has to ensure that HSE_VALUE is same as the real + * frequency of the crystal used. Otherwise, this function may + * have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ +void SystemCoreClockUpdate(void) +{ + uint32_t tmp, pllvco, pllr, pllsource, pllm; + + /* Get SYSCLK source -------------------------------------------------------*/ + switch (RCC->CFGR & RCC_CFGR_SWS) + { + case 0x04: /* HSI used as system clock source */ + SystemCoreClock = HSI_VALUE; + break; + + case 0x08: /* HSE used as system clock source */ + SystemCoreClock = HSE_VALUE; + break; + + case 0x0C: /* PLL used as system clock source */ + /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN + SYSCLK = PLL_VCO / PLLR + */ + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; + if (pllsource == 0x02UL) /* HSI used as PLL clock source */ + { + pllvco = (HSI_VALUE / pllm); + } + else /* HSE used as PLL clock source */ + { + pllvco = (HSE_VALUE / pllm); + } + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; + SystemCoreClock = pllvco/pllr; + break; + + default: + break; + } + /* Compute HCLK clock frequency --------------------------------------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; +} + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + diff --git a/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c b/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c index 563b90d..a280c3e 100644 --- a/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c +++ b/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c @@ -147,6 +147,43 @@ uint32_t uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */ */ HAL_StatusTypeDef HAL_Init(void) { + +// HAL_StatusTypeDef status = HAL_OK; +// /* Configure Flash prefetch, Instruction cache, Data cache */ +// /* Default configuration at reset is: */ +// /* - Prefetch disabled */ +// /* - Instruction cache enabled */ +// /* - Data cache enabled */ +// #if (INSTRUCTION_CACHE_ENABLE == 0U) +// __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); +// #endif /* INSTRUCTION_CACHE_ENABLE */ + +// #if (DATA_CACHE_ENABLE == 0U) +// __HAL_FLASH_DATA_CACHE_DISABLE(); +// #endif /* DATA_CACHE_ENABLE */ + +// #if (PREFETCH_ENABLE != 0U) +// __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); +// #endif /* PREFETCH_ENABLE */ + +// /* Set Interrupt Group Priority */ +// HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + +// /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is HSI) */ +// if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK) +// { +// status = HAL_ERROR; +// } +// else +// { +// /* Init the low level hardware */ +// HAL_MspInit(); +// } + +// /* Return function status */ +// return status; + + HAL_StatusTypeDef status = HAL_OK; /* Configure Flash prefetch, Instruction cache, Data cache */ /* Default configuration at reset is: */ diff --git a/EWARM/chargeController.ewd b/EWARM/chargeController.ewd index e7ab616..fd4c67f 100644 --- a/EWARM/chargeController.ewd +++ b/EWARM/chargeController.ewd @@ -1155,7 +1155,7 @@