/******************************************************************************** Copyright (C), Sinowealth Electronic. Ltd. Author: Sino Version: V0.0 Date: 2020/04/26 History: V2.0 2020/04/26 Preliminary ********************************************************************************/ #include "Main.h" BOOL bISPFlg; //ISP升级标志,进入ISP程序 BOOL bUart0ReadFlg; BOOL bUart0WriteFlg; BOOL bUart0SndAckFlg; //UART已经发送ACK给主机 BOOL bUart1ReadFlg; BOOL bUart1WriteFlg; BOOL bUart1SndAckFlg; //UART已经发送ACK给主机 BOOL bUart2ReadFlg; BOOL bUart2WriteFlg; BOOL bUart2SndAckFlg; //UART已经发送ACK给主机 U8 xdata ucSubClassID; U8 xdata ucUart0Buf[150] _at_ 0x400; //UART接收数据的BUF U8 xdata ucUart0BufPT; //UART数据指针 U8 xdata ucUart0SndLength; //UART数据长度 U8 xdata ucUart0TimeoutCnt; //UART通讯定时器计数 U8 xdata ucUart1Buf[150] _at_ 0x4a0; //UART接收数据的BUF U8 xdata ucUart1BufPT; //UART数据指针 U8 xdata ucUart1SndLength; //UART数据长度 U8 xdata ucUart1TimeoutCnt; //UART通讯定时器计数 U8 xdata ucUart2Buf[150] _at_ 0x540; //UART接收数据的BUF U8 xdata ucUart2BufPT; //UART数据指针 U8 xdata ucUart2SndLength; //UART数据长度 U8 xdata ucUart2TimeoutCnt; //UART通讯定时器计数 U16 xdata uiReceCheckSum; //帧头判断正确 U8 xdata ucUartErrCode; /******************************************************************************* Function: Page1WrRdFuncTable() Description: Input: NULL Output: NULL Others: NULL *******************************************************************************/ U16 code Page1WrRdFuncTable[18]= { SYS_PARA_MAP_ADDR, //SubClassID 0x00 SYSINFO_MAP_ADDR, //0x01 CHG_PARA_MAP_ADDR, //0x02 DSG_PARA_MAP_ADDR, //0x03 0x00, //0x04 DSG_PWM_PARA_MAP_ADDR, //0x05 CHG_TEMP_PARA_MAP_ADDR, //0x06 DSG_TEMP_PARA_MAP_ADDR, //0x07 BAL_PARA_MAP_ADDR, //0x08 SOC_PARA_MAP_ADDR, //0x09 AFE_PARA_MAP_ADDR, //0x0A CALI_PARA_MAP_ADDR, //0x0B DataflashCheck_Map_ADDR, //0x0C 0x00, //0x0D 0x00, //0x0E 0x00, //0x0F 0x00, //0x10 0x00, //0x11 }; /******************************************************************************* Function: Page2WrRdFuncTable() Description: Input: NULL Output: NULL Others: NULL *******************************************************************************/ U16 code Page2WrRdFuncTable[18]= { SYS_PARA_MAP_ADDR+32, //SubClassID 0x00 SYSINFO_MAP_ADDR+32, //0x01 CHG_PARA_MAP_ADDR+32, //0x02 DSG_PARA_MAP_ADDR+32, //0x03 0x00, //0x04 DSG_PWM_PARA_MAP_ADDR+32, //0x05 CHG_TEMP_PARA_MAP_ADDR+32, //0x06 DSG_TEMP_PARA_MAP_ADDR+32, //0x07 BAL_PARA_MAP_ADDR+32, //0x08 SOC_PARA_MAP_ADDR+32, //0x09 AFE_PARA_MAP_ADDR+32, //0x0A CALI_PARA_MAP_ADDR+32, //0x0B DataflashCheck_Map_ADDR+32, //0x0C 0x00, //0x0D 0x00, //0x0E 0x00, //0x0F 0x00, //0x10 0x00, //0x11 }; #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0Handshake * 参 数: 无 * 返回值: 无 * 描 述: UART0通讯握手协议 *************************************************************************************************/ void Uart0Handshake(void) { if(ucUart0Buf[2] == 0x00) //Testing equipment is properly { Uart0SendAck(); } else { Uart0SendNack(); } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1Handshake * 参 数: 无 * 返回值: 无 * 描 述: UART1通讯握手协议 *************************************************************************************************/ void Uart1Handshake(void) { if(ucUart1Buf[2] == 0x00) //Testing equipment is properly { Uart1SendAck(); } else { Uart1SendNack(); } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2Handshake * 参 数: 无 * 返回值: 无 * 描 述: UART2通讯握手协议 *************************************************************************************************/ void Uart2Handshake(void) { if(ucUart2Buf[2] == 0x00) //Testing equipment is properly { Uart2SendAck(); } else { Uart2SendNack(); } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0WriteInfo * 参 数: ptr:数据需要写入的起始地址 * 返回值: 无 * 描 述: UART0写数据 *************************************************************************************************/ void Uart0WriteInfo(U8 xdata *ptr) { U8 i; if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { for(i=0; i 140) { ucUart0Buf[UART_LENGTH] = 0; } for(i=0; i 140) { ucUart1Buf[UART_LENGTH] = 0; } for(i=0; i 140) { ucUart2Buf[UART_LENGTH] = 0; } for(i=0; i= 128) { bE2PProcessFlg = 1; bUart0E2PRdData = 1; } else if(ucUart0Buf[UART_CMD_NO] == SUB_PAGE1 && ucSubClassID == RTC_SUBID) { bE2PProcessFlg = 1; bUart0RTCRdTime = 1; } else if(ucUart0Buf[UART_CMD_NO] == SUB_PAGE1) { rdaddr = (U8 xdata *)Page1WrRdFuncTable[ucSubClassID]; Uart0ReadInfo(rdaddr); } else if(ucUart0Buf[UART_CMD_NO] == SUB_PAGE2) { rdaddr = (U8 xdata *)Page2WrRdFuncTable[ucSubClassID]; Uart0ReadInfo(rdaddr); } break; } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0RdCmdProcess * 参 数: 无 * 返回值: 无 * 描 述: UART读命令处理函数 *************************************************************************************************/ void Uart1RdCmdProcess(void) { U8 xdata *rdaddr; switch(ucUart1Buf[UART_CMD_NO]) { case CELL1: case CELL2: case CELL3: case CELL4: case CELL5: case CELL6: case CELL7: case CELL8: case CELL9: case CELL10: Uart1ReadInfo((U8 xdata *)&Info.uiVCell[ucUart1Buf[UART_CMD_NO]-1]); break; case TOTAL_VOLTAGE: Uart1ReadInfo((U8 xdata *)&Info.ulVoltage); break; case CADC_CURRENT: Uart1ReadInfo((U8 xdata *)&Info.slCurr); break; case EXT_TEMP1: Uart1ReadInfo((U8 xdata *)&Info.uiTS[0]); break; case EXT_TEMP2: Uart1ReadInfo((U8 xdata *)&Info.uiTS[1]); break; case DIE_TEMP1: Uart1ReadInfo((U8 xdata *)&Info.uiICTempe[0]); break; case DIE_TEMP2: Uart1ReadInfo((U8 xdata *)&Info.uiICTempe[1]); break; case FULL_CHG_CAP: Uart1ReadInfo((U8 xdata *)&Info.ulFCC); break; case REMAIN_CAP: Uart1ReadInfo((U8 xdata *)&Info.ulRC); break; case R_SOC: Uart1ReadInfo((U8 xdata *)&Info.uiRSOC); break; case CYCLE_COUNT: Uart1ReadInfo((U8 xdata *)&Info.uiCycleCount); break; case PACK_STATUS: Uart1ReadInfo((U8 xdata *)&Info.uiPackStatus); break; case BATTERY_STATUS: Uart1ReadInfo((U8 xdata *)&Info.uiBatStatus); break; case PACK_CONFIG: Uart1ReadInfo((U8 xdata *)&Info.uiPackConfig); break; case MANUFACTURE_COMMAND: Uart1ReadInfo((U8 xdata *)&Info.uiManuCommand); break; default: //read extern EEPRom data if(ucUart1Buf[UART_LENGTH] >= 128) { bE2PProcessFlg = 1; bUart1E2PRdData = 1; } else if(ucUart1Buf[UART_CMD_NO] == SUB_PAGE1 && ucSubClassID == RTC_SUBID) { bE2PProcessFlg = 1; bUart1RTCRdTime = 1; } else if(ucUart1Buf[UART_CMD_NO] == SUB_PAGE1) { rdaddr = (U8 xdata *)Page1WrRdFuncTable[ucSubClassID]; Uart1ReadInfo(rdaddr); } else if(ucUart1Buf[UART_CMD_NO] == SUB_PAGE2) { rdaddr = (U8 xdata *)Page2WrRdFuncTable[ucSubClassID]; Uart1ReadInfo(rdaddr); } break; } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0RdCmdProcess * 参 数: 无 * 返回值: 无 * 描 述: UART读命令处理函数 *************************************************************************************************/ void Uart2RdCmdProcess(void) { U8 xdata *rdaddr; switch(ucUart2Buf[UART_CMD_NO]) { case CELL1: case CELL2: case CELL3: case CELL4: case CELL5: case CELL6: case CELL7: case CELL8: case CELL9: case CELL10: Uart2ReadInfo((U8 xdata *)&Info.uiVCell[ucUart2Buf[UART_CMD_NO]-1]); break; case TOTAL_VOLTAGE: Uart2ReadInfo((U8 xdata *)&Info.ulVoltage); break; case CADC_CURRENT: Uart2ReadInfo((U8 xdata *)&Info.slCurr); break; case EXT_TEMP1: Uart2ReadInfo((U8 xdata *)&Info.uiTS[0]); break; case EXT_TEMP2: Uart2ReadInfo((U8 xdata *)&Info.uiTS[1]); break; case DIE_TEMP1: Uart2ReadInfo((U8 xdata *)&Info.uiICTempe[0]); break; case DIE_TEMP2: Uart2ReadInfo((U8 xdata *)&Info.uiICTempe[1]); break; case FULL_CHG_CAP: Uart2ReadInfo((U8 xdata *)&Info.ulFCC); break; case REMAIN_CAP: Uart2ReadInfo((U8 xdata *)&Info.ulRC); break; case R_SOC: Uart2ReadInfo((U8 xdata *)&Info.uiRSOC); break; case CYCLE_COUNT: Uart2ReadInfo((U8 xdata *)&Info.uiCycleCount); break; case PACK_STATUS: Uart2ReadInfo((U8 xdata *)&Info.uiPackStatus); break; case BATTERY_STATUS: Uart2ReadInfo((U8 xdata *)&Info.uiBatStatus); break; case PACK_CONFIG: Uart2ReadInfo((U8 xdata *)&Info.uiPackConfig); break; case MANUFACTURE_COMMAND: Uart2ReadInfo((U8 xdata *)&Info.uiManuCommand); break; default: //read extern EEPRom data if(ucUart2Buf[UART_LENGTH] >= 128) { bE2PProcessFlg = 1; bUart2E2PRdData = 1; } else if(ucUart2Buf[UART_CMD_NO] == SUB_PAGE1 && ucSubClassID == RTC_SUBID) { bE2PProcessFlg = 1; bUart2RTCRdTime = 1; } else if(ucUart2Buf[UART_CMD_NO] == SUB_PAGE1) { rdaddr = (U8 xdata *)Page1WrRdFuncTable[ucSubClassID]; Uart2ReadInfo(rdaddr); } else if(ucUart2Buf[UART_CMD_NO] == SUB_PAGE2) { rdaddr = (U8 xdata *)Page2WrRdFuncTable[ucSubClassID]; Uart2ReadInfo(rdaddr); } break; } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0CaliCurrent * 参 数: 无 * 返回值: 无 * 描 述: UART0通讯发送校准总电压的数据 *************************************************************************************************/ void Uart0CaliVoltage(void) { if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { ulExtVPack = ((U16)ucUart0Buf[3]<<8)|ucUart0Buf[4]; if(!ulExtVPack) { Uart0SendNack(); } else { bCaliFlg = 1; ucExtcaliSwitch1 |= 0x01; Uart0SendAck(); } } else { Uart0SendNack(); } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1CaliCurrent * 参 数: 无 * 返回值: 无 * 描 述: UART1通讯发送校准总电压的数据 *************************************************************************************************/ void Uart1CaliVoltage(void) { if(ucUart1Buf[3+ucUart1Buf[UART_LENGTH]] == CRC8cal(&ucUart1Buf, ucUart1Buf[UART_LENGTH]+3)) { ulExtVPack = ((U16)ucUart1Buf[3]<<8)|ucUart1Buf[4]; if(!ulExtVPack) { Uart1SendNack(); } else { bCaliFlg = 1; ucExtcaliSwitch1 |= 0x01; Uart1SendAck(); } } else { Uart1SendNack(); } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2CaliCurrent * 参 数: 无 * 返回值: 无 * 描 述: UART2通讯发送校准总电压的数据 *************************************************************************************************/ void Uart2CaliVoltage(void) { if(ucUart2Buf[3+ucUart2Buf[UART_LENGTH]] == CRC8cal(&ucUart2Buf, ucUart2Buf[UART_LENGTH]+3)) { ulExtVPack = ((U16)ucUart2Buf[3]<<8)|ucUart2Buf[4]; if(!ulExtVPack) { Uart2SendNack(); } else { bCaliFlg = 1; ucExtcaliSwitch1 |= 0x01; Uart2SendAck(); } } else { Uart2SendNack(); } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0CaliCurrent、Uart0CaliCurOffset * 参 数: 无 * 返回值: 无 * 描 述: UART0通讯发送校准电流采集增益&Offset的数据 *************************************************************************************************/ void Uart0CaliCurrent(void) { if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { slExtCur = ((U32)ucUart0Buf[3]<<24) | ((U32)ucUart0Buf[4]<<16) | ((U32)ucUart0Buf[5]<<8) | ((U32)ucUart0Buf[6]); if((!AFE.siCurr) || (!slExtCur)) { Uart0SendNack(); } else { bCaliFlg = 1; ucExtcaliSwitch1 |= 0x10; Uart0SendAck(); } } else { Uart0SendNack(); } } void Uart0CaliCurOffset(void) { if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration offset Current ucExtcaliSwitch1 |= 0x20; Uart0SendAck(); } else { Uart0SendNack(); } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1CaliCurrent、Uart1CaliCurOffset * 参 数: 无 * 返回值: 无 * 描 述: UART1通讯发送校准电流采集增益&Offset的数据 *************************************************************************************************/ void Uart1CaliCurrent(void) { if(ucUart1Buf[3+ucUart1Buf[UART_LENGTH]] == CRC8cal(&ucUart1Buf, ucUart1Buf[UART_LENGTH]+3)) { slExtCur = ((U32)ucUart1Buf[3]<<24) | ((U32)ucUart1Buf[4]<<16) | ((U32)ucUart1Buf[5]<<8) | ((U32)ucUart1Buf[6]); if((!AFE.siCurr) || (!slExtCur)) { Uart1SendNack(); } else { bCaliFlg = 1; ucExtcaliSwitch1 |= 0x10; Uart1SendAck(); } } else { Uart1SendNack(); } } void Uart1CaliCurOffset(void) { if(ucUart1Buf[3+ucUart1Buf[UART_LENGTH]] == CRC8cal(&ucUart1Buf, ucUart1Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration offset Current ucExtcaliSwitch1 |= 0x20; Uart1SendAck(); } else { Uart1SendNack(); } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2CaliCurrent、Uart2CaliCurOffset * 参 数: 无 * 返回值: 无 * 描 述: UART2通讯发送校准电流采集增益&Offset的数据 *************************************************************************************************/ void Uart2CaliCurrent(void) { if(ucUart2Buf[3+ucUart2Buf[UART_LENGTH]] == CRC8cal(&ucUart2Buf, ucUart2Buf[UART_LENGTH]+3)) { slExtCur = ((U32)ucUart2Buf[3]<<24) | ((U32)ucUart2Buf[4]<<16) | ((U32)ucUart2Buf[5]<<8) | ((U32)ucUart2Buf[6]); if((!AFE.siCurr) || (!slExtCur)) { Uart2SendNack(); } else { bCaliFlg = 1; ucExtcaliSwitch1 |= 0x10; Uart2SendAck(); } } else { Uart2SendNack(); } } void Uart2CaliCurOffset(void) { if(ucUart2Buf[3+ucUart2Buf[UART_LENGTH]] == CRC8cal(&ucUart2Buf, ucUart2Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration offset Current ucExtcaliSwitch1 |= 0x20; Uart2SendAck(); } else { Uart2SendNack(); } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0CaliTS1、Uart0CaliTS2 * 参 数: 无 * 返回值: 无 * 描 述: UART0通讯发送校准TS1/TS2数据 *************************************************************************************************/ void Uart0CaliTS1(void) { if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration TS1 uiExtTemp1 = ((U16)ucUart0Buf[3]<<8)|ucUart0Buf[4]; ucExtcaliSwitch1 |= 0x04; Uart0SendAck(); } else { Uart0SendNack(); } } void Uart0CaliTS2(void) { if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration TS2 uiExtTemp2 = ((U16)ucUart0Buf[3]<<8)|ucUart0Buf[4]; ucExtcaliSwitch1 |= 0x08; Uart0SendAck(); } else { Uart0SendNack(); } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1CaliTS1、Uart1CaliTS2 * 参 数: 无 * 返回值: 无 * 描 述: UART1通讯发送校准TS1/TS2数据 *************************************************************************************************/ void Uart1CaliTS1(void) { if(ucUart1Buf[3+ucUart1Buf[UART_LENGTH]] == CRC8cal(&ucUart1Buf, ucUart1Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration TS1 uiExtTemp1 = ((U16)ucUart1Buf[3]<<8)|ucUart1Buf[4]; ucExtcaliSwitch1 |= 0x04; Uart1SendAck(); } else { Uart1SendNack(); } } void Uart1CaliTS2(void) { if(ucUart1Buf[3+ucUart1Buf[UART_LENGTH]] == CRC8cal(&ucUart1Buf, ucUart1Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration TS2 uiExtTemp2 = ((U16)ucUart1Buf[3]<<8)|ucUart1Buf[4]; ucExtcaliSwitch1 |= 0x08; Uart1SendAck(); } else { Uart1SendNack(); } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2CaliTS1、Uart2CaliTS2 * 参 数: 无 * 返回值: 无 * 描 述: UART2通讯发送校准TS1/TS2数据 *************************************************************************************************/ void Uart2CaliTS1(void) { if(ucUart2Buf[3+ucUart2Buf[UART_LENGTH]] == CRC8cal(&ucUart2Buf, ucUart2Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration TS1 uiExtTemp1 = ((U16)ucUart2Buf[3]<<8)|ucUart2Buf[4]; ucExtcaliSwitch1 |= 0x04; Uart2SendAck(); } else { Uart2SendNack(); } } void Uart2CaliTS2(void) { if(ucUart2Buf[3+ucUart2Buf[UART_LENGTH]] == CRC8cal(&ucUart2Buf, ucUart2Buf[UART_LENGTH]+3)) { bCaliFlg = 1; //Calibration TS2 uiExtTemp2 = ((U16)ucUart2Buf[3]<<8)|ucUart2Buf[4]; ucExtcaliSwitch1 |= 0x08; Uart2SendAck(); } else { Uart2SendNack(); } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0CaliRTC * 参 数: 无 * 返回值: 无 * 描 述: UART0通讯发送校准RTC数据 *************************************************************************************************/ void Uart0CaliRTC(void) { U8 i; if(ucUart0Buf[3+ucUart0Buf[UART_LENGTH]] == CRC8cal(&ucUart0Buf, ucUart0Buf[UART_LENGTH]+3)) { for(i=0; i<7; i++) { ucExtRTC[i] = ucUart0Buf[3+i]; } bCaliFlg = 1; ucExtcaliSwitch1 |= 0x80; Uart0SendAck(); } else { Uart0SendNack(); } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1CaliRTC * 参 数: 无 * 返回值: 无 * 描 述: UART1通讯发送校准RTC数据 *************************************************************************************************/ void Uart1CaliRTC(void) { U8 i; if(ucUart1Buf[3+ucUart1Buf[UART_LENGTH]] == CRC8cal(&ucUart1Buf, ucUart1Buf[UART_LENGTH]+3)) { for(i=0; i<7; i++) { ucExtRTC[i] = ucUart1Buf[3+i]; } bCaliFlg = 1; ucExtcaliSwitch1 |= 0x80; Uart1SendAck(); } else { Uart1SendNack(); } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2CaliRTC * 参 数: 无 * 返回值: 无 * 描 述: UART2通讯发送校准RTC数据 *************************************************************************************************/ void Uart2CaliRTC(void) { U8 i; if(ucUart2Buf[3+ucUart2Buf[UART_LENGTH]] == CRC8cal(&ucUart2Buf, ucUart2Buf[UART_LENGTH]+3)) { for(i=0; i<7; i++) { ucExtRTC[i] = ucUart2Buf[3+i]; } bCaliFlg = 1; ucExtcaliSwitch1 |= 0x80; Uart2SendAck(); } else { Uart2SendNack(); } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0WrCmdProcess * 参 数: 无 * 返回值: 无 * 描 述: UART0写命令处理函数 *************************************************************************************************/ void Uart0WrCmdProcess(void) { U8 xdata *rdaddr; switch(ucUart0Buf[UART_CMD_NO]) { case MANUFACTURE_COMMAND: Uart0WriteManufacture(); break; case DATA_FLASH_COMMAND: Uart0ReadSubClassID(); //accept command is dataflashcommand 0x77 break; case CALI_CUR_COMMAND: Uart0CaliCurrent(); break; case CALI_VOL_COMMAND: Uart0CaliVoltage(); break; case CALI_ZERO_CUR_COMMAND: Uart0CaliCurOffset(); break; case CALI_TS1_COMMAND: Uart0CaliTS1(); break; case CALI_TS2_COMMAND: Uart0CaliTS2(); break; case CALI_RTC_COMMAND: Uart0CaliRTC(); break; case SUB_PAGE1: rdaddr = (U8 xdata *)Page1WrRdFuncTable[ucSubClassID]; Uart0WriteInfo(rdaddr); break; case SUB_PAGE2: rdaddr = (U8 xdata *)Page2WrRdFuncTable[ucSubClassID]; Uart0WriteInfo(rdaddr); break; case CMD_E2PROM_ERASE: if(ucUart0Buf[UART_DATA] == 0x55) //Testing equipment is properly { bE2PProcessFlg = 1; bE2PErase = 1; Uart0SendAck(); } else { Uart0SendNack(); } break; } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1WrCmdProcess * 参 数: 无 * 返回值: 无 * 描 述: UART1写命令处理函数 *************************************************************************************************/ void Uart1WrCmdProcess(void) { U8 xdata *rdaddr; switch(ucUart1Buf[UART_CMD_NO]) { case MANUFACTURE_COMMAND: Uart1WriteManufacture(); break; case DATA_FLASH_COMMAND: Uart1ReadSubClassID(); //accept command is dataflashcommand 0x77 break; case CALI_CUR_COMMAND: Uart1CaliCurrent(); break; case CALI_VOL_COMMAND: Uart1CaliVoltage(); break; case CALI_ZERO_CUR_COMMAND: Uart1CaliCurOffset(); break; case CALI_TS1_COMMAND: Uart1CaliTS1(); break; case CALI_TS2_COMMAND: Uart1CaliTS2(); break; case CALI_RTC_COMMAND: Uart1CaliRTC(); break; case SUB_PAGE1: rdaddr = (U8 xdata *)Page1WrRdFuncTable[ucSubClassID]; Uart1WriteInfo(rdaddr); break; case SUB_PAGE2: rdaddr = (U8 xdata *)Page2WrRdFuncTable[ucSubClassID]; Uart1WriteInfo(rdaddr); break; case CMD_E2PROM_ERASE: if(ucUart1Buf[UART_DATA] == 0x55) //Testing equipment is properly { bE2PProcessFlg = 1; bE2PErase = 1; Uart1SendAck(); } else { Uart1SendNack(); } break; } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2WrCmdProcess * 参 数: 无 * 返回值: 无 * 描 述: UART2写命令处理函数 *************************************************************************************************/ void Uart2WrCmdProcess(void) { U8 xdata *rdaddr; switch(ucUart2Buf[UART_CMD_NO]) { case MANUFACTURE_COMMAND: Uart2WriteManufacture(); break; case DATA_FLASH_COMMAND: Uart2ReadSubClassID(); //accept command is dataflashcommand 0x77 break; case CALI_CUR_COMMAND: Uart2CaliCurrent(); break; case CALI_VOL_COMMAND: Uart2CaliVoltage(); break; case CALI_ZERO_CUR_COMMAND: Uart2CaliCurOffset(); break; case CALI_TS1_COMMAND: Uart2CaliTS1(); break; case CALI_TS2_COMMAND: Uart2CaliTS2(); break; case CALI_RTC_COMMAND: Uart2CaliRTC(); break; case SUB_PAGE1: rdaddr = (U8 xdata *)Page1WrRdFuncTable[ucSubClassID]; Uart2WriteInfo(rdaddr); break; case SUB_PAGE2: rdaddr = (U8 xdata *)Page2WrRdFuncTable[ucSubClassID]; Uart2WriteInfo(rdaddr); break; case CMD_E2PROM_ERASE: if(ucUart2Buf[UART_DATA] == 0x55) //Testing equipment is properly { bE2PProcessFlg = 1; bE2PErase = 1; Uart2SendAck(); } else { Uart2SendNack(); } break; } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: Uart0IapCheckHandshake * 参 数: 无 * 返回值: 无 * 描 述: IAP和ISP握手协议 *************************************************************************************************/ void Uart0IapCheckHandshake(void) { U8 i, databak; U16 checksum = 0; if(ucUart0BufPT == 2) { if(ucUart0Buf[HEARD2] != 0xA5) { ucUart0BufPT = 0; } else { uiReceCheckSum = 0; //帧头判断正确 ucUartErrCode = 0; } } else { if(ucUart0BufPT < (ucUart0Buf[LENGTH]+9)) { if(ucUart0BufPT <= (ucUart0Buf[LENGTH]+7)) { uiReceCheckSum += ucUart0Buf[ucUart0BufPT-1]; } if(ucUart0BufPT == (TARGET+1)) //检查ID { if(ucUart0Buf[TARGET] != IAP_BMSID) { ucUart0BufPT = 0; } } else if(ucUart0BufPT == (COMMAND+1)) //检查COMMAND { if((ucUart0Buf[COMMAND] != IAP_CMD_HANDSHAKE)) { ucUartErrCode |= IAPERROR_CMD; } } } else { ucUart0BufPT = 0; if(uiReceCheckSum != ((ucUart0Buf[ucUart0Buf[LENGTH]+8]<<8) + ucUart0Buf[ucUart0Buf[LENGTH]+7])) { ucUartErrCode |= IAPERROR_CHECKSUM; } if(ucUartErrCode != 0) //如果有错误代码则不执行命令处理 { ucUart0Buf[INDEXES] = ucUartErrCode; } else { bISPFlg = 1; //跳转到BOOT区 ucUart0Buf[INDEXES] = 0; } ucUart0Buf[LENGTH] = 0; ucUart0Buf[COMMAND] = 0x0B; databak = ucUart0Buf[SOURCE]; //交换源ID和目标ID ucUart0Buf[SOURCE] = ucUart0Buf[TARGET]; ucUart0Buf[TARGET] = databak; for(i=2; i<(ucUart0Buf[LENGTH]+7); i++) { checksum += ucUart0Buf[i]; } ucUart0Buf[7+ucUart0Buf[LENGTH]] = checksum%256; ucUart0Buf[8+ucUart0Buf[LENGTH]] = checksum/256; Uart0SendAck(); } } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: Uart1IapCheckHandshake * 参 数: 无 * 返回值: 无 * 描 述: IAP和ISP握手协议 *************************************************************************************************/ void Uart1IapCheckHandshake(void) { U8 i, databak; U16 checksum = 0; if(ucUart1BufPT == 2) { if(ucUart1Buf[HEARD2] != 0xA5) { ucUart1BufPT = 0; } else { uiReceCheckSum = 0; //帧头判断正确 ucUartErrCode = 0; } } else { if(ucUart1BufPT < (ucUart1Buf[LENGTH]+9)) { if(ucUart1BufPT <= (ucUart1Buf[LENGTH]+7)) { uiReceCheckSum += ucUart1Buf[ucUart1BufPT-1]; } if(ucUart1BufPT == (TARGET+1)) //检查ID { if(ucUart1Buf[TARGET] != IAP_BMSID) { ucUart1BufPT = 0; } } else if(ucUart1BufPT == (COMMAND+1)) //检查COMMAND { if((ucUart1Buf[COMMAND] != IAP_CMD_HANDSHAKE)) { ucUartErrCode |= IAPERROR_CMD; } } } else { ucUart1BufPT = 0; if(uiReceCheckSum != ((ucUart1Buf[ucUart1Buf[LENGTH]+8]<<8) + ucUart1Buf[ucUart1Buf[LENGTH]+7])) { ucUartErrCode |= IAPERROR_CHECKSUM; } if(ucUartErrCode != 0) //如果有错误代码则不执行命令处理 { ucUart1Buf[INDEXES] = ucUartErrCode; } else { bISPFlg = 1; //跳转到BOOT区 ucUart1Buf[INDEXES] = 0; } ucUart1Buf[LENGTH] = 0; ucUart1Buf[COMMAND] = 0x0B; databak = ucUart1Buf[SOURCE]; //交换源ID和目标ID ucUart1Buf[SOURCE] = ucUart1Buf[TARGET]; ucUart1Buf[TARGET] = databak; for(i=2; i<(ucUart1Buf[LENGTH]+7); i++) { checksum += ucUart1Buf[i]; } ucUart1Buf[7+ucUart1Buf[LENGTH]] = checksum%256; ucUart1Buf[8+ucUart1Buf[LENGTH]] = checksum/256; Uart1SendAck(); } } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: Uart2IapCheckHandshake * 参 数: 无 * 返回值: 无 * 描 述: IAP和ISP握手协议 *************************************************************************************************/ void Uart2IapCheckHandshake(void) { U8 i, databak; U16 checksum = 0; if(ucUart2BufPT == 2) { if(ucUart2Buf[HEARD2] != 0xA5) { ucUart2BufPT = 0; } else { uiReceCheckSum = 0; //帧头判断正确 ucUartErrCode = 0; } } else { if(ucUart2BufPT < (ucUart2Buf[LENGTH]+9)) { if(ucUart2BufPT <= (ucUart2Buf[LENGTH]+7)) { uiReceCheckSum += ucUart2Buf[ucUart2BufPT-1]; } if(ucUart2BufPT == (TARGET+1)) //检查ID { if(ucUart2Buf[TARGET] != IAP_BMSID) { ucUart2BufPT = 0; } } else if(ucUart2BufPT == (COMMAND+1)) //检查COMMAND { if((ucUart2Buf[COMMAND] != IAP_CMD_HANDSHAKE)) { ucUartErrCode |= IAPERROR_CMD; } } } else { ucUart2BufPT = 0; if(uiReceCheckSum != ((ucUart2Buf[ucUart2Buf[LENGTH]+8]<<8) + ucUart2Buf[ucUart2Buf[LENGTH]+7])) { ucUartErrCode |= IAPERROR_CHECKSUM; } if(ucUartErrCode != 0) //如果有错误代码则不执行命令处理 { ucUart2Buf[INDEXES] = ucUartErrCode; } else { bISPFlg = 1; //跳转到BOOT区 ucUart2Buf[INDEXES] = 0; } ucUart2Buf[LENGTH] = 0; ucUart2Buf[COMMAND] = 0x0B; databak = ucUart2Buf[SOURCE]; //交换源ID和目标ID ucUart2Buf[SOURCE] = ucUart2Buf[TARGET]; ucUart2Buf[TARGET] = databak; for(i=2; i<(ucUart2Buf[LENGTH]+7); i++) { checksum += ucUart2Buf[i]; } ucUart2Buf[7+ucUart2Buf[LENGTH]] = checksum%256; ucUart2Buf[8+ucUart2Buf[LENGTH]] = checksum/256; Uart2SendAck(); } } } #endif #if (UART0_DEFINE != 0) /************************************************************************************************* * 函数名: InterruptUart0AppRx * 参 数: RxData:Uart0接收的数据,从SBUF0获取 * 返回值: 无 * 描 述: UART接收中断服务程序接口,中断处理函数会调用该函数 ucUart0Buf[0]--Slave Addr ucUart0Buf[1]--CMD No. ucUart0Buf[2]--Offset ucUart0Buf[3]--Data Length ucUart0Buf[4...]--Data *************************************************************************************************/ void InterruptUart0AppRx(U8 RxData) { ucUart0Buf[ucUart0BufPT] = RxData; ucUart0BufPT++; if(ucUart0BufPT >= 140) { ucUart0BufPT = 0; } if(ucUart0BufPT == 1) { if((ucUart0Buf[UART_SLAVE_ADDR]&0xFE) == SADDR) //接收的第一个字节是否和UART地址匹配 { if((ucUart0Buf[UART_SLAVE_ADDR]&0x01)==0) //bit7是R/W标志;0--R, 1--W { bUart0ReadFlg = 1; bUart0WriteFlg = 0; } else { bUart0WriteFlg = 1; bUart0ReadFlg = 0; } } else if(ucUart0Buf[HEARD1] != 0x5A) //判断是否为IAP/ISP的起始帧 { ucUart0BufPT = 0; //如果帧头错误,则复位指针 } } if(bUart0ReadFlg) { if(ucUart0BufPT==3) { Uart0RdCmdProcess(); //Read the command process } } else if(bUart0WriteFlg) { if(ucUart0BufPT > (ucUart0Buf[UART_LENGTH]+3)) //If a write operation, and complete all the data has been received { Uart0WrCmdProcess(); //Write the command peocess bUart0WriteFlg = 0; //PC write MCU communiaction over ucUart0BufPT = 0; } } else //非读非写操作时,即为ISP或者IAP的begin命令 { Uart0IapCheckHandshake(); } bUart0SndAckFlg = 0; ucSleepTimerCnt = 0; //UART正常通讯会清零低功耗计数,UART复位计数 ucPDTimerCnt = 0; ucUart0TimeoutCnt = 0; } /************************************************************************************************* * 函数名: InterruptUart0AppTx * 参 数: 无 * 返回值: 无 * 描 述: UART0发送中断服务程序接口,中断处理函数会调用该函数 *************************************************************************************************/ void InterruptUart0AppTx(void) { if((ucUart0Buf[HEARD1] == 0x5A) && (ucUart0Buf[HEARD2] == 0xA5) || bISPFlg) { if(ucUart0BufPT >= (ucUart0Buf[LENGTH]+8)) { Uart0RxEn(); //Allow UART receive data ucUart0BufPT = 0; bUart0ReadFlg = 0; //PC read MCU communication over ucUart0Buf[0] = 0; ucUart0Buf[1] = 0; ucUart0Buf[2] = 0; ucUart0Buf[3] = 0; bUart0SndAckFlg = 1; } else { ucUart0BufPT++; Uart0TxEn(ucUart0Buf[ucUart0BufPT]); //20220620 } } else if((ucUart0BufPT==0) || (ucUart0BufPT>=ucUart0Buf[UART_LENGTH]+3)) { Uart0RxEn(); //UART允许接收数据 ucUart0BufPT = 0; bUart0ReadFlg = 0; ucUart0Buf[0] = 0; ucUart0Buf[1] = 0; ucUart0Buf[2] = 0; ucUart0Buf[3] = 0; bUart0SndAckFlg = 1; //UART已发送过数据,主要用于需要等待发送ACK才能进一步操作的功能 } else { ucUart0BufPT++; Uart0TxEn(ucUart0Buf[ucUart0BufPT]); } ucSleepTimerCnt = 0; //UART正常通讯会清零低功耗计数,UART复位计数 ucPDTimerCnt = 0; ucUart0TimeoutCnt = 0; } /************************************************************************************************* * 函数名: InterruptUart0AppTx * 参 数: 无 * 返回值: 无 * 描 述: UART0发送中断服务程序接口,中断处理函数会调用该函数 *************************************************************************************************/ void Uart0Check(void) { if(++ucUart0TimeoutCnt >= 4) //如果4*50mS没有UART通讯,则复位指针向量,并切换到接收模式 { ucUart0TimeoutCnt = 0; ucUart0BufPT = 0; Uart0RxEn(); //UART允许接收数据 } } #endif #if (UART1_DEFINE != 0) /************************************************************************************************* * 函数名: InterruptUart1AppRx * 参 数: RxData:Uart1接收的数据,从SBUF1获取 * 返回值: 无 * 描 述: UART1接收中断服务程序接口,中断处理函数会调用该函数 ucUart1Buf[0]--Slave Addr ucUart1Buf[1]--CMD No. ucUart1Buf[2]--Offset ucUart1Buf[3]--Data Length ucUart1Buf[4...]--Data *************************************************************************************************/ void InterruptUart1AppRx(U8 RxData) { ucUart1Buf[ucUart1BufPT] = RxData; ucUart1BufPT++; if(ucUart1BufPT >= 140) { ucUart1BufPT = 0; } if(ucUart1BufPT == 1) { McuBank1Sel(); if((ucUart1Buf[UART_SLAVE_ADDR]&0xFE) == SADDR1) //接收的第一个字节是否和UART地址匹配 { McuBank0Sel(); if((ucUart1Buf[UART_SLAVE_ADDR]&0x01)==0) //bit7是R/W标志;0--R, 1--W { bUart1ReadFlg = 1; bUart1WriteFlg = 0; } else { bUart1WriteFlg = 1; bUart1ReadFlg = 0; } } else if(ucUart1Buf[HEARD1] != 0x5A) //判断是否为IAP/ISP的起始帧 { ucUart1BufPT = 0; //如果帧头错误,则复位指针 } } if(bUart1ReadFlg) { if(ucUart1BufPT==3) { Uart1RdCmdProcess(); //Read the command process } } else if (bUart1WriteFlg) { if(ucUart1BufPT > (ucUart1Buf[UART_LENGTH]+3)) //If a write operation, and complete all the data has been received { Uart1WrCmdProcess(); //Write the command peocess bUart1WriteFlg = 0; //PC write MCU communiaction over ucUart1BufPT = 0; } } else //非读非写操作时,即为ISP或者IAP的begin命令 { Uart1IapCheckHandshake(); } bUart1SndAckFlg = 0; ucSleepTimerCnt = 0; //UART正常通讯会清零低功耗计数,UART复位计数 ucPDTimerCnt = 0; ucUart1TimeoutCnt = 0; } /************************************************************************************************* * 函数名: InterruptUart1AppTx * 参 数: 无 * 返回值: 无 * 描 述: UART1发送中断服务程序接口,中断处理函数会调用该函数 *************************************************************************************************/ void InterruptUart1AppTx(void) { if((ucUart1Buf[HEARD1] == 0x5A) && (ucUart1Buf[HEARD2] == 0xA5) || bISPFlg) { if(ucUart1BufPT >= (ucUart1Buf[LENGTH]+8)) { Uart1RxEn(); //Allow UART receive data ucUart1BufPT = 0; bUart1ReadFlg = 0; //PC read MCU communication over ucUart1Buf[0] = 0; ucUart1Buf[1] = 0; ucUart1Buf[2] = 0; ucUart1Buf[3] = 0; bUart1SndAckFlg = 1; } else { ucUart1BufPT++; Uart1TxEn(ucUart1Buf[ucUart1BufPT]); //20220620 } } else if((ucUart1BufPT==0) || (ucUart1BufPT>=ucUart1Buf[UART_LENGTH]+3)) { Uart1RxEn(); //UART允许接收数据 ucUart1BufPT = 0; bUart1ReadFlg = 0; ucUart1Buf[0] = 0; ucUart1Buf[1] = 0; ucUart1Buf[2] = 0; ucUart1Buf[3] = 0; bUart1SndAckFlg = 1; //UART已发送过数据,主要用于需要等待发送ACK才能进一步操作的功能 } else { ucUart1BufPT++; Uart1TxEn(ucUart1Buf[ucUart1BufPT]); } ucSleepTimerCnt = 0; //UART正常通讯会清零低功耗计数,UART复位计数 ucPDTimerCnt = 0; ucUart1TimeoutCnt = 0; } /************************************************************************************************* * 函数名: InterruptUart1AppTx * 参 数: 无 * 返回值: 无 * 描 述: UART1发送中断服务程序接口,中断处理函数会调用该函数 *************************************************************************************************/ void Uart1Check(void) { if(++ucUart1TimeoutCnt >= 4) //如果4*50mS没有UART通讯,则复位指针向量,并切换到接收模式 { ucUart1TimeoutCnt = 0; ucUart1BufPT = 0; Uart1RxEn(); //UART允许接收数据 } } #endif #if (UART2_DEFINE != 0) /************************************************************************************************* * 函数名: InterruptUart2AppRx * 参 数: RxData:Uart2接收的数据,从SBUF2获取 * 返回值: 无 * 描 述: UART2接收中断服务程序接口,中断处理函数会调用该函数 ucUart2Buf[0]--Slave Addr ucUart2Buf[1]--CMD No. ucUart2Buf[2]--Offset ucUart2Buf[3]--Data Length ucUart2Buf[4...]--Data *************************************************************************************************/ void InterruptUart2AppRx(U8 RxData) { ucUart2Buf[ucUart2BufPT] = RxData; ucUart2BufPT++; if(ucUart2BufPT >= 140) { ucUart2BufPT = 0; } if(ucUart2BufPT == 1) { McuBank1Sel(); if((ucUart2Buf[UART_SLAVE_ADDR]&0xFE) == SADDR2) //接收的第一个字节是否和UART地址匹配 { McuBank0Sel(); if((ucUart2Buf[UART_SLAVE_ADDR]&0x01)==0) //bit7是R/W标志;0--R, 1--W { bUart2ReadFlg = 1; bUart2WriteFlg = 0; } else { bUart2WriteFlg = 1; bUart2ReadFlg = 0; } } else if(ucUart2Buf[HEARD1] != 0x5A) //判断是否为IAP/ISP的起始帧 { ucUart2BufPT = 0; //如果帧头错误,则复位指针 } } if(bUart2ReadFlg) { if(ucUart2BufPT==3) { Uart2RdCmdProcess(); //Read the command process } } else if(bUart2WriteFlg) { if(ucUart2BufPT > (ucUart2Buf[UART_LENGTH]+3)) //If a write operation, and complete all the data has been received { Uart2WrCmdProcess(); //Write the command peocess bUart2WriteFlg = 0; //PC write MCU communiaction over ucUart2BufPT = 0; } } else //非读非写操作时,即为ISP或者IAP的begin命令 { Uart2IapCheckHandshake(); } bUart2SndAckFlg = 0; ucSleepTimerCnt = 0; //UART正常通讯会清零低功耗计数,UART复位计数 ucPDTimerCnt = 0; ucUart2TimeoutCnt = 0; } /************************************************************************************************* * 函数名: InterruptUartAppTx * 参 数: 无 * 返回值: 无 * 描 述: UART发送中断服务程序接口,中断处理函数会调用该函数 *************************************************************************************************/ void InterruptUart2AppTx(void) { if((ucUart2Buf[HEARD1] == 0x5A) && (ucUart2Buf[HEARD2] == 0xA5) || bISPFlg) { if(ucUart2BufPT >= (ucUart2Buf[LENGTH]+8)) { Uart2RxEn(); //Allow UART receive data ucUart2BufPT = 0; bUart2ReadFlg = 0; //PC read MCU communication over ucUart2Buf[0] = 0; ucUart2Buf[1] = 0; ucUart2Buf[2] = 0; ucUart2Buf[3] = 0; bUart2SndAckFlg = 1; } else { ucUart2BufPT++; Uart2TxEn(ucUart2Buf[ucUart2BufPT]); //20220620 } } else if((ucUart2BufPT==0) || (ucUart2BufPT>=ucUart2Buf[UART_LENGTH]+3)) { Uart2RxEn(); //UART允许接收数据 ucUart2BufPT = 0; bUart2ReadFlg = 0; ucUart2Buf[0] = 0; ucUart2Buf[1] = 0; ucUart2Buf[2] = 0; ucUart2Buf[3] = 0; bUart2SndAckFlg = 1; //UART已发送过数据,主要用于需要等待发送ACK才能进一步操作的功能 } else { ucUart2BufPT++; Uart2TxEn(ucUart2Buf[ucUart2BufPT]); } ucSleepTimerCnt = 0; //UART正常通讯会清零低功耗计数,UART复位计数 ucPDTimerCnt = 0; ucUart2TimeoutCnt = 0; } /************************************************************************************************* * 函数名: InterruptUart0AppTx * 参 数: 无 * 返回值: 无 * 描 述: UART发送中断服务程序接口,中断处理函数会调用该函数 *************************************************************************************************/ void Uart2Check(void) { if(++ucUart2TimeoutCnt >= 4) //如果4*50mS没有UART通讯,则复位指针向量,并切换到接收模式 { ucUart2TimeoutCnt = 0; ucUart2BufPT = 0; Uart2RxEn(); //UART允许接收数据 } } #endif