#ifndef __USART_NRF_H #define __USART_NRF_H #ifdef DTU3PRO #include "stm32f4xx.h" #define UsartNrf USART3 #else #include "stm32f10x.h" #define UsartNrf USART2 #endif #include #include #include #include #include #include #include "rtc.h" #include "SysTick.h" #define DTUID_READ_CNT 5 /* Polling, polling software version, upgrade program, command, anti-theft polling, anti-theft settings, lock MI, unlock MI, limit power, configure grid-connected file, switch air baud rate*/ #ifdef DTU3PRO #define PORT_LEN 100//500 #else #define PORT_LEN 100 #endif #define EVERY_PORT_POWER 300 #define STX 0x7e #define ETX 0x7f #define UART_LEN 35 #define MAX_PRO_PACKAGE_ROW 21 #define MAX_SUB_DATA_LEN 64 #define BROADCAST 0x02 //Query network terminal device ID #define ANSWER_REQHOST (BROADCAST | 0x80 | 0x40) #define ANSWER_BROADCAST (BROADCAST | 0x80) #define BROADCAST_ON 0x18 //Query the status of terminal A channel #define ANSWER_BROADCAST_ON (BROADCAST_ON | 0x80) #define REQ_NUB_BROADCAST 0x03 // Request the number of broadcast commands #define ANSWER_REQ_NUB_BROADCAST (REQ_NUB_BROADCAST | 0x80) //83 #define REQ_RF_SVERSISON 0x06 //Query the terminal RF ID software and hardware version number #define ANSWER_REQ_RF_SVERSISON (REQ_RF_SVERSISON | 0x80) //0x86 #define REQ_RF_RVERSISON 0x07 // #define ANSWER_REQ_RF_RVERSISON (REQ_RF_RVERSISON | 0x80) //0x87 //#define REQ_A_EVENT 0x08 //Query A-side events #define REQ_A_DAT 0x09 //Query A-side data #define ANSWER_REQ_A_DAT (REQ_A_DAT | 0x80) //89 #define ANSWER_REQ_A_EVENT (0x08 | 0x80) //88 #define REQ_GRIDFILE 0X10 #define ANSWER_REQ_GRIDFILE REQ_GRIDFILE|0X80 //Polling the grid-connected protection file #define DOWN_DAT 0x0a //Download the grid-connected configuration file #define ANSWER_DOWN_DAT (DOWN_DAT | 0x80) //8A #define ANSWER_GDFAULT (GDFAULT | 0x80) //8D #define DOWN_PRO 0x0e //download program command #define ANSWER_DOWN_PRO (DOWN_PRO | 0x80) //8E #define REQ_VERSISON 0x0f //Query terminal software and hardware information #define ANSWER_REQ_VERSISON (REQ_VERSISON | 0x80) //8F// #define REQ_PARA_VERSION 0x10 //Query the name and version number of the grid-connected configuration file #define ANSWER_REQ_PARA_VERSION (REQ_PARA_VERSION | 0x80) //90 #define REQ_B_DAT 0x11 //Query B-side data #define ANSWER_REQ_B_DAT (REQ_B_DAT | 0x80) //91 #define ANSWER_REQ_B_EVENT (0x12 | 0x80) //92 #define ANSWER_WRITE_SEND_RFID (WRITE_SEND_RFID | 0x80) //9a #define ANSWER_WRITE_RECV_RFID (WRITE_RECV_RFID | 0x80) //9b #define CONTROL_LOCK_MI__LIMIT_POWER_ONOFF 0x51 //Lock & limit power #define CONTROL_LOCK_MI_SUB 0xa5a5 #define CONTROL_LIMIT_POWER_SUB 0x5a5a #define CONTROL_ON_SUB 0x55aa #define CONTROL_OFF_SUB 0xaa55 #define CONTROL_LOCK_SUB1 0X6699 #define CONTROL_LOCK_SUB2 0X9966 #define CONTROL_UNLOCK_SUB 0X6969 #define ANSWER_CONTROL_LOCK_MI__LIMIT_POEWR_ONOFF (CONTROL_LOCK_MI__LIMIT_POWER_ONOFF | 0x80) #define RE_FISRT__CONFIRM_SET_ENERGY 0x52 #define RE_FISRT_SET_ENERGY_SUB 0xa5 //Set the power generation quantum command for the first time #define RE_CONFIRM_SET_ENERGY_SUB 0x5a //Reset power generation quantum command #define ANSWER_SET__CONFIRM_ENERGY (RE_FISRT_SET_ENERGY_SUB | 0x80) #define CLEAR_GFDI 0X0D //Clear GFDI #define CLEAR_GFDI_SUB 0X00 #define ANSWER_CLEAR_GFDI (CLEAR_GFDI | 0x80) #define CHANGE_MOD_2M_250K 0x01 #define ANSWER_CHANGE_MOD_2M_250K (CHANGE_MOD_2M_250K | 0x80) #define REQ_A1_DAT_1000 0X36 #define ANSWER_REQ_A1_DAT_1000 (REQ_A1_DAT_1000 | 0x80) #define REQ_B1_DAT_1000 0X38 #define ANSWER_REQ_B1_DAT_1000 (REQ_B1_DAT_1000 | 0x80) #define REQ_A2_DAT_1000 0X37 #define ANSWER_REQ_A2_DAT_1000 (REQ_A2_DAT_1000 | 0x80) #define REQ_B2_DAT_1000 0X39 #define ANSWER_REQ_B2_DAT_1000 (REQ_B2_DAT_1000 | 0x80) #define REQ_GET_DTU_PARA_SUB 0X01 #define REQ_GET_DTU_PARA 0X30 #define ANSWER_REQ_GET_DTU_PARA (REQ_GET_DTU_PARA| 0X80) #define ANSWER_REQ_GET_DTU_PARA_SUB 0x01 typedef enum { InitInverterType = 0, Inverter_250 = 1, Inverter_500 = 2, Inverter_1000 = 4, Inverter_Pro = 7, Inverter_HM_OneToOne = 8, Inverter_HM_OneToTwo = 9, Inverter_HM_OneToFour = 10, } InverterType; typedef enum { Device_250K = 1, Device_2M = 0, Device_INIT_BAUD = 3 } BaudType; typedef enum { RF = 0, REPEAT433 = 1 } DataSourceType; typedef struct { vu8 tm_year; vu8 tm_mon; vu8 tm_mday; vu8 tm_hour; vu8 tm_min; vu8 tm_sec; } RTC_TIME; typedef enum { NET_NOCMD = 0, //no network command NET_EXECUTION_COMPLETED = 100, //There is a network command execution completed NET_NOT_EXECUTED = 200, //There are network commands not executed NET_EXECUTION_FAILURE = 201, //A network command failed to execute LOCAL_NOT_EXECUTED = 202, //1-99Ϊ��������ִ�н��� } NETSTATUS; typedef enum { NET_INIT = 0, NET_TURN_ON = 1, // turn on NET_TURN_OFF = 2, //shutdown NET_LIMIT_POEWR = 3, // limit power NET_SET_PASSWORD = 4, // Anti-theft setting NET_ZERO_EXPORT = 5, //Anti-backflow GF NET_DOWNLOAD_DAT = 6, //Download the grid-connected protection file NET_DOWNLOAD_PRO = 7, //download rf program NET_CLEAN_GFDI = 8, //Clear ground fault NET_SEARCH_ID = 9, //Search ID NET_SET_ENERGY = 10, //set power generation NET_WAIT_DOWNLOAD_DAT = 11, //Waiting to download the grid-connected protection file NET_WAIT_DOWNLOAD_PRO = 12, //waiting to download rf program NET_CANCEL_GUARD = 13, //Cancel anti-theft NET_LOCK = 14, //lock NET_UNLOCK = 15, //unlock NET_TERMINAL_INFOR = 16, // terminal device information NET_INVERTER_HW_INFOR = 17, //Inverter hardware information NET_RESTART = 18, // restart NET_POLL_GRID_ON_FILE = 19, NET_ABORTCONTROL = 20, //Abort command execution NET_LIMIT_ACTIVE_POEWR = 24, NET_LIMIT_REACTIVE_POWER = 25, NET_PF_SET = 26, NET_CLEAR_ALARM = 27, //dong 2020-06-15 Self-check NET_SELF_INSPECTION = 28, //Micro-inverse self- check //NET_POLL_GRID_ON_PROFILE = 28, NET_ELE_ENERGY = 29, //NET_ANTI_THERFT_PARA = 30, NET_SELF_STAUS = 90, //Micro-inverse self-check status NET_GET_LOSS_RATE = 91, NET_POLL = 92, //polling NET_NOPORT = 93, //No micro-inverse id NET_ALARM_DATA = 94, //After polling, clear the completion flag like version polling NET_RECORD_DATA = 95, NET_ALARM_UPDATE = 96, NET_DTU_HW_INFOR = 97, //DTU hardware information NET_SWITCH_BAUD = 98, // NET_DTU_HW_INFOR_RES = 99, NET_REGISTER_ID = 100, //Registration ID NET_MI_VERSION = 101, } NetCmdType; typedef union { struct { vu8 PVVol[2]; // PV voltage vu8 PVCur[2]; //PV current vu8 GridVol[2]; // grid voltage vu8 Freque[2]; //Frequency vu8 Power[2]; //Power vu8 Temper[2]; //Temperature vu8 Collec_Time; //Number of consecutive data not collected vu8 LinkState; //Link state vu8 Run_Status[2]; //Micro-inverse running status vu8 Fault_Code[2]; //Fault code vu8 Fault_Num[2]; //Number of faults volatile NetCmdType NetCmd; //Network command vu32 Real_Time; //Sampling time hours, minutes and seconds vu8 NetStatus; //Network command status vu16 DailyEnergy; //Daily power generation vu16 HistoryEnergyH; //History total power generation high vu16 HistoryEnergyL; //The low level of historical total power generation vu16 DataVer[2]; // vu8 PVPower[2]; // vu8 GridActivePower[2]; // grid active power vu8 GridReactivePower[2]; //Grid reactive power vu8 GridCurrent[2]; //Grid current vu8 PowerFactor[2]; // power factor + vu8 BusVol[2]; // } Data; vu8 DataMsg[52]; } InverterReal; typedef enum { MI_NO = 0, MI_250W = 0x01, MI_500W_A = 0x02, MI_500W_B = 0x03, MI_1000W_A = 0x04, MI_1000W_B = 0x05, MI_1000W_C = 0x06, MI_1000W_D = 0x07, Pro_A = 0x08, Pro_B = 0x09, Pro_C = 0x0a, Pro_D = 0x0b, HM_250W = 0x0C, HM_500W_A = 0x0D, HM_500W_B = 0x0E, HM_1000W_A = 0x0F, HM_1000W_B = 0x10, HM_1000W_C = 0x11, HM_1000W_D = 0x12, } PortType; typedef union { struct { vu16 USFWBuild_VER; //bootloader version vu16 AppFWBuild_VER; // application version vu16 AppFWBuild_YYYY; //App creation time vu16 AppFWBuild_MMDD; vu16 AppFWBuild_HHMM; vu16 AppFW_PNH; //software part number vu16 AppFW_PNL; vu16 HW_VER; //hardware version vu16 HW_PNH; //Hardware part number vu16 HW_PNL; vu16 HW_FB_TLmValue; //The value of the excitation inductance vu16 HW_FB_ReSPRT; //Resonance period vu16 HW_GridSamp_ResValule; //Use the resistance value vu16 HW_ECapValue; //Electrolytic capacitor value vu16 Matching_AppFW_PNH; //The software part number for hardware matching vu16 Matching_AppFW_PNL; vu16 AppFW_MINVER; //The minimum version number of the software required by the hardware vu8 MIMod; //Micro inverse model vu16 Country_Std; //Configured country vu16 Default_Version; //The default version of the configuration and the saved version vu16 Save_Version; //The version number of the saved version vu8 NRF_SoftVersion[4]; //Inverter NRF software and hardware version number vu8 NRF_HardVersion[4]; //Inverter NRF software and hardware version number vu16 HW_CRCValue; //Check value vu16 AppFWPN; //App firmware part number vu16 HWSPECVER; //Hardware specification version vu16 PDSPECL; //Product specification parameter version vu16 GPFCode; //Grid protection file code vu16 GPFVer; //The version of the grid protection file vu16 ReservedPara; vu16 CRC_ALL; vu8 rule_id; } Property; vu8 PropertyMsg[68]; } InverterDetail; #pragma pack(1) typedef struct { vu8 SetValut[2]; vu8 Desc[2]; } PowerPFDevType; typedef struct { vu8 WCode[2]; } ClearAlarmDevType; typedef struct { vu8 Info[2]; vu8 Data[16]; } EletricSetType; typedef struct { vu8 PWO[4]; vu8 PWN[4]; vu8 ATTime[2]; } PassWordSetType; //dong 2020-06-15 Grid-connected protection file self-check parameters typedef struct { u8 Ver[2]; u8 GPF[2]; } SelfCheckParaType; typedef union { PowerPFDevType PowerPFDev; ClearAlarmDevType ClearAlarmDev; EletricSetType EletricSet; PassWordSetType PassWordSet; vu8 Data[18]; //Parameter setting } PassValueType; typedef union { struct { vu8 Pre_Id[2]; // vu8 Id[4]; //Network command specified Id + polling Id+Search Id PortType Port; //Port type vu8 Id_In_Phase; //The phase where the ID is located 1: A 2: B 3: C vu8 Acq_Switch; //Micro-inverse acquisition switch vu16 Power_Limit; //Power limit PassValueType Pass; } Property; //After executing the current inverter network command, the polling of the inverter will be executed vu8 PropertyMsg[30]; } InverterMajor; #pragma pack() typedef union { struct// when booting { //dtu connection server mode vu8 access_model; //Old Password vu8 LockOldPassword[4]; //new password vu8 LockNewPassword[4]; // Anti-theft setting time and time vu8 Lock_Time[2]; // prevent backflow vu8 Zero_Export_Switch; // wireless transmission address vu8 Reflux_WirelessSenderAddr[3]; //Server upload interval vu16 Server_SendTimePeriod; //power value vu32 LimitPower_MyPower[3]; //set time offset vs32 timezone; //Number of inverters vu16 InverterNum; //number of components vu16 PortNum; //DTU current total power generation vu16 TotalPower; //DTU historical total power generation high vu16 HistoryPowerH; //DTU historical total power generation is low vu16 HistoryPowerL; //The real-time total power of components under this dtu vu16 Reflux_AllPanelPower; //Number of A-phase components under DTU vu16 Reflux_DtuPanelNum_A; //Number of B-phase components under DTU vu16 Reflux_DtuPanelNum_B; //Number of C-phase components under DTU vu16 Reflux_DtuPanelNum_C; //Total number of components in the system vu16 Reflux_SysPanelNum; //Number of A-phase components in the system vu16 Reflux_SysPanelNum_A; //Number of B-phase components under the system vu16 Reflux_SysPanelNum_B; //Number of C-phase components in the system vu16 Reflux_SysPanelNum_C; // anti-theft switch vu8 Anti_Theft_Switch; vu8 CurMIRealTimePoll; vu8 LedState; //HasNotPolledVer = false; indicates that the version number is being polled HasNotPolledVer = true: indicates that the polling version number is over vu8 PolledVerState; //Anti-backflow grid type vu8 GridType; //Remaining power online switch vu8 SurplusSwitch; //Remaining power grid limit power vs32 SurplusPowerA; //Remaining power grid limit power vs32 SurplusPowerB; //Remaining power grid limit power vs32 SurplusPowerC; //overall control 0: overall control 1: split-phase control vu8 OverallControl; //storage location vu8 StorageLocat; #ifdef DTU3PRO //DHCP switch vu8 DHCP_Switch; //set IP address vu8 IP_ADDR[4]; //submask vu8 subnet_mask[4]; //default gateway vu8 default_gateway[4]; //RS485 mode vu8 RS485Mode; //RS485 address vu8 RS485Addr; //DRM power limit vu8 DRM_Limit_Switch; // phase balance control vu8 Phase_Balance_Switch; //The interphase tolerance value unit is w vu16 Tolerance_Between_Phases; //SunSpec mode vu8 SunSpec_Switch; //SunSpec start address vu8 SunSpec_Addr_Start; } Property; vu8 PropertyMsg[108]; #else } Property; vu8 PropertyMsg[84]; #endif } DtuDetail; typedef union { struct { vu8 Pre_Id[2]; vu8 Id[4]; //DTU hardware version vu8 DtuHw_Ver[2]; //DTU software version vu8 DtuSw_Ver[2]; //DTU RF hardware version number vu8 RfHw_Ver[4]; //DTU RF software version vu8 RfFw_Ver[4]; //GPRS version number vu8 Gprs_Ver[20]; //GPRS card number vu8 Gprs_SIMNum[28]; //gprs signal strength vu8 GPRS_CSQ; //wifi version number vu8 Wifi_Vsn[10]; //Router SSID vu8 wifi_ssid[40]; //router password vu8 wifi_passward[30]; //wifi signal strength vu8 wifi_RSSI; //server domain name vu8 ServerDomainName[40]; //APN vu8 APN[20]; //Whether the server mode/app mode is successful vu8 LinkMode; vu8 DtuToolAddr[4]; vu8 wifi_ssid_save_set; //Server upload interval vu8 server_send_time; // Internet mode selection vu8 netmode_select; vu8 rule_id; //server port number vu16 ServerPort; //APN2 vu8 APN2[20]; } Property; vu8 PropertyMsg[240]; } DtuMajor; extern vu8 Uart_SendBuffer[UART_LEN]; //Send buffer extern volatile InverterDetail MIDetail; //Micro inverter details extern volatile InverterReal MIReal[PORT_LEN]; //Inverter real-time data extern volatile InverterMajor MIMajor[PORT_LEN]; //Micro inversion component extern volatile DtuMajor Dtu3Major; //DTU main information extern volatile DtuDetail Dtu3Detail; //DTU details extern vu16 PortNO; //component number bool UsartNrf_NrfInit(void); //Initialize nrf void UsartNrf_MemInit(void); ///Initialize related structures and Bia Niang void UsartNrf_SendProcessLoop(void); //Send and receive polling function void UsartNrf_ClearMIReal(void); //Clear the MIReal structure void UsartNrf_ClearMI_DailyEnergy(void); //Clear the day's power generation void UsartNrf_ClearInverterMajor(void); //Clear the InverterMajor structure void UsartNrf_Process_LedShow(bool PollRealTimeState, u8 NoConllectTime); u16 UsartNrf_Get_crc_xor16(u16 *b_hex, u16 len); u8 Get_crc_xor(u8 *b_hex, u16 len); u8 ForwardSubstitution(u8 *result, u8 *source, u8 length); u8 UsartNrf_Send_PackUpdateGridOnProFile(u8 *target_adr, u8 *rout_adr); void UsartNrf3_Send_ConfigGridOnPre(void); void UsartNrf_SendLoop_SetPackageTimeOut(bool Stop); void UsartNrf_Send_PackUpdateMiProgram(u8 *target_adr, u8 *rout_adr); u8 UsartNrf_Send_MiProgram_SigleFrame(u8 *target_adr, u8 *router_adr, u8 *dat, u8 nub, u8 len); bool UsartNrf_HasSetCurrentInverterOk(void); bool UsartNrf_SendLoop_GetNextSetCmd(void); u8 UsartNrf_Backward_substitution1(u8 *result, u8 *source, u8 length); void UsartNrf_GetDtu3PortNum(void); void UsartNrf_InitPVPannelToMIMajor(void); void UsartNrf_ClearMI_DailyEnergy(void); //Clear the day's power generation extern char *string; //Grid protection file array extern bool HasTempRegister; extern NetCmdType CurNetCmd; extern vu8 Uart_CurSendMainCmd; //The current network command sends the main command extern vu16 Uart_CurSendSubCmd; //The current network command sends the command extern vu8 Uart_CurRecMainCmd; //Receipt main command of current network command extern vu16 Uart_CurRecSubCmd; //Receipt subcommand of current network command extern vu8 Uart_SendBufferLen; //Currently sending bytes extern BaudType Uart_CurrentDtuBaud; //Current DTU air baud rate extern vu32 Uart_CurrentReplyState; //current receipt state extern vu16 SubCmd; extern vu8 MainCmd; extern vu8 Uart_CurrentReplyAddr[4]; //Download receipt address extern vu8 Uart_CurSendAddr[4]; extern vu32 Index_Pro; extern vu32 TotalIndex_Pro; extern bool RightHexFile; //Whether it is hex file stealing extern bool RightGridOnProFile; extern u8 *CurRowData_Pro; extern u8 *CurRowData_Dat; extern vu32 LocalTime_TimeOut; extern vu16 TotalIndex_Dat; extern vu16 Index_Dat; extern vu16 Uart_ReceiveBufferLen; //Currently received bytes extern BaudType GetInveterBaudType(u8 *DeviceId); extern u8 UsartNrf_Send_PackSetNrfBaudRateInTheAir(u8 *target_adr, u8 *rout_adr, u8 Cmd, BaudType Baud); extern InverterType UsartNrf_GetInvterType(u8 *pId); extern u8 UsartNrf_Send_PackGridOnProFile_SigleFrame(u8 *target_adr, u8 *router_adr, u8 *dat, u8 nub, u8 len); extern u16 UsartNrf_Send_CRC16_Work(u8 *CRC_Buf, u16 CRC_Leni); extern void UsartNrf_ClearVersionActionState(void); extern void UsartNrf_Process_Version_InitInverterRf(void); extern bool UsartNrf_Process_ExceptionFilter(u8 *pBuffer, u8 Head, u8 Tail, u32 MaxValue, s32 MinValue); #define PORT_NUMBER_CONFIRMATION(x) (MIMajor[x].Property.Port == MI_250W) || (MIMajor[x].Property.Port == MI_500W_A) || (MIMajor[x].Property.Port == MI_1000W_A) ||(MIMajor[x].Property.Port == Pro_A)||(MIMajor[x].Property.Port == HM_250W)||(MIMajor[x].Property.Port == HM_500W_A)||(MIMajor[x].Property.Port == HM_1000W_A) #define PORT_NUMBER_CONFIRMATION1(x) (MIMajor[x].Property.Port == MI_500W_B) || (MIMajor[x].Property.Port == MI_1000W_B) || (MIMajor[x].Property.Port == MI_1000W_C)|| (MIMajor[x].Property.Port == MI_1000W_D) #define PORT_NUMBER_CONFIRMATION2(x) (MIMajor[x].Property.Port == MI_250W)||(MIMajor[x].Property.Port == MI_500W_B) || (MIMajor[x].Property.Port == MI_1000W_D) || (MIMajor[x].Property.Port == Pro_D)|| (MIMajor[x].Property.Port == HM_250W)||(MIMajor[x].Property.Port == HM_500W_B)||(MIMajor[x].Property.Port == HM_1000W_D) #endif