/** ****************************************************************************** * @file uart_bafang.c * @author * @version V1.0.0 * @date 23-12-2015 * @brief uart_bafang function. ****************************************************************************** * * COPYRIGHT(c) 2015 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "uart_bafang.h" #include "string.h" #include "usart.h" #include "canAppl.h" #include "syspar.h" #include "bikebrake.h" #include "power.h" /****************************** * * Parameter * ******************************/ /****************************** * * Functions * ******************************/ /*************************************************************** Function: Bafang_USART_RxData_Process; Description: Call by: Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void Bafang_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount) { UWORD i; UBYTE databuf[20]; UBYTE checksum=0; if(dataCount > 2) { for(i=0;i<(dataCount-1);i++) { checksum = (UBYTE)(checksum + buf[i]); } } if(buf[0] == 0x11)//读 { if((dataCount == 2) || ((dataCount > 2) && (checksum == buf[dataCount-1]))) { switch (buf[1]) { case 0x08: //读状态 { if(MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor == 1) { databuf[0] = 0x25; } else if(MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor == 1) { databuf[0] = 0x21; } else if(MC_ErrorCode.ERROR_Bit.Fault_PCBNTC == 1) { databuf[0] = 0x15; } else if(MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp == 1) { databuf[0] = 0x11; } else if(MC_ErrorCode.ERROR_Bit.Fault_PhaseLine == 1) { databuf[0] = 0x09; } else if(MC_ErrorCode.ERROR_Bit.Fault_HallSensor == 1) { databuf[0] = 0x08; } else if(MC_ErrorCode.ERROR_Bit.Protect_OverVoltage == 1) { databuf[0] = 0x07; } else if(MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage == 1) { databuf[0] = 0x06; } else if(MC_ErrorCode.ERROR_Bit.Fault_Throttle == 1) { databuf[0] = 0x05; } else if(BikeBrake_blGetstate() == TRUE) { databuf[0] = 0x03; } else { databuf[0] = 0x01;//正常状态 } UART_PutChar(ptUartTx, databuf[0]); break; } case 0x0A: //读瞬时电流 { databuf[0] = (UBYTE)abs(MC_RunInfo.BusCurrent/500);//0.5A UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[0]); break; } case 0x0B: //读档位 { databuf[0] = 0x00;//0x00-仪表, 0x01-蓝牙 databuf[1] = MC_RunInfo.GearSt; databuf[2] = (UBYTE)(databuf[0] + databuf[1]); UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[1]); UART_PutChar(ptUartTx, databuf[2]); break; } case 0x0C: //读档位(控制器带蓝牙双向控制) { databuf[0] = 0x00;//0x00-仪表开机, 0x01-仪表关机 UART_PutChar(ptUartTx, databuf[0]); break; } case 0x11: //读电池容量 { //Bafang_SendData_BMS(&UART_TxBuff_Struct_BMS, 0x16, 0x0D, 0x01); if(blBMSCommFault == TRUE) { databuf[0] = MC_RunInfo.SOC; UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[0]); } break; } case 0x1B: //读大灯开启/关闭 { databuf[0] = 0x00;//0x00-仪表, 0x01-蓝牙 if(MC_RunInfo.LightSwitch == 0xF1) { databuf[1] = 0x01; } else { databuf[1] = 0x00; } databuf[2] = (UBYTE)(databuf[0] + databuf[1]); UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[1]); UART_PutChar(ptUartTx, databuf[2]); break; } case 0x20: //读转速 { UWORD uwSpeed = (UWORD)((UQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * FBASE * 60 >>20);//s databuf[0] = (UBYTE)(uwSpeed>>8); databuf[1] = (UBYTE)(uwSpeed); //databuf[2] = (UBYTE)(databuf[0] + databuf[1]); databuf[2] = (UBYTE)(0x20 + databuf[0] + databuf[1]); UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[1]); UART_PutChar(ptUartTx, databuf[2]); break; } case 0x21: //读踏频 { databuf[0] = (UBYTE)(MC_RunInfo.Cadence>>8); databuf[1] = (UBYTE)(MC_RunInfo.Cadence); databuf[2] = (UBYTE)(databuf[0] + databuf[1]); UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[1]); UART_PutChar(ptUartTx, databuf[2]); break; } case 0x22: //读剩余里程 { databuf[0] = (UBYTE)(MC_RunInfo.RemainDistance>>8); databuf[1] = (UBYTE)(MC_RunInfo.RemainDistance); databuf[2] = (UBYTE)(databuf[0] + databuf[1]); UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[1]); UART_PutChar(ptUartTx, databuf[2]); break; } case 0x23: { break; } case 0x24: { break; } case 0x25: { break; } case 0x26: { break; } case 0x27: { break; } case 0x31: //读工作状态 { if( power_stPowStateOut.powerstate == POWER_ON_END ) { if ((cp_stBikeRunInfoPara.BikeSpeedKmH <= 10)&&(MC_RunInfo.MotorSpeed <= 100) &&(curSpeed_state.state!=ClzLoop)) { databuf[0] = 0x30;//0x31-工作中,0x30-不工作 } else { databuf[0] = 0x31; } } else { databuf[0] = 0x30; } UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[0]); break; } case 0x51: //波特率跳转 { break; } case 0x60: { break; } case 0x61: { break; } case 0x62: { break; } case 0x63: { break; } case 0x64: { break; } case 0x65: { break; } case 0x66: { break; } case 0x67: { break; } case 0x90: //读协议版本 { UART_PutChar(ptUartTx, 0x90); UART_PutChar(ptUartTx, 0x40); UART_PutChar(ptUartTx, 0xD0); break; } default: break; } } } else if(buf[0] == 0x16)//写 { if((buf[1] == 0x0B) && (dataCount == 4) && (checksum == buf[dataCount-1])) //写档位 { ulOBC_ComTimeOutCount = cp_ulSystickCnt; if(buf[2] == 3) { MC_ControlCode.GearSt = MC_GearSt_SMART; } else if(buf[2] == 23) { MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO; } else if(buf[2] == 21) { MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT; } else if(buf[2] == 13) { MC_ControlCode.GearSt = MC_GearSt_Torque_NORM; } else if(buf[2] == 11) { MC_ControlCode.GearSt = MC_GearSt_Torque_ECO; } else if(buf[2] == 6) { MC_ControlCode.GearSt = MC_GearSt_WALK; } else { MC_ControlCode.GearSt = MC_GearSt_OFF; } MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新 } else if((buf[1] == 0x1A) && (dataCount == 3)) //写大灯开启/关闭 { if(buf[2] == 0xF1) //大灯 { MC_ControlCode.LightSwitch = MC_LightSwitch_ON; } else { MC_ControlCode.LightSwitch = MC_LightSwitch_OFF; } MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新 } else if((buf[1] == 0x1F) && (dataCount == 5) && (checksum == buf[dataCount-1])) //写限速 { // UWORD tmpdata = 0; // tmpdata = (UWORD)((buf[2]<<8) + buf[3]); // tmpdata= (UWORD)((ULONG)tmpdata*MC_UpcInfo.stBikeInfo.uwWheelPerimeter*36/60000);//rpm->km/h // //限速阈值更新 // if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != tmpdata) // { // MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = tmpdata; // MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = tmpdata + 2; // MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = tmpdata; // MC_UpcInfo.stBikeInfo.uwSaveFlg = 1; // MC_UpcInfo.stAssistInfo.uwSaveFlg = 1; // cp_stFlg.ParaSaveEEFlg = TRUE; // cp_stFlg.ParaUpdateFlg = TRUE; // cp_stFlg.ParaAInfoUpdateFlg = TRUE; // cp_stFlg.ParaBikeInfoUpdateFlg = TRUE; // cp_stFlg.ParaAssistUpdateFinishFlg = TRUE; // } } else if((buf[1] == 0x0C) && (dataCount == 4) && (checksum == buf[dataCount-1])) //写ECO/SPORT { if(buf[2] == 0x02) { //ECO模式 } else if(buf[2] == 0x04) { //SPORT模式 } UART_PutChar(ptUartTx, buf[0]); UART_PutChar(ptUartTx, buf[1]); UART_PutChar(ptUartTx, buf[2]); UART_PutChar(ptUartTx, buf[3]); } else if((buf[1] == 0x23) && (dataCount == 3) && (checksum == buf[dataCount-1])) //清除错误代码 { databuf[0] = 0x23; UART_PutChar(ptUartTx, databuf[0]); UART_PutChar(ptUartTx, databuf[0]); } } } /*************************************************************** Function: Bafang_SendData_BMS; Description: Call by: Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void Bafang_SendData_BMS(USART_Buf_TypeDef* ptUartTx, UBYTE Addr, UBYTE Cmd, UBYTE Length) { #if 0// (SIMULATION == 1) return; #else UBYTE databuf[255] = {0}; UBYTE checksum=0; UBYTE i = 0; databuf[0] = 0x3A; databuf[1] = Addr; databuf[2] = Cmd; databuf[3] = Length; for(i=1; i<(Length+4); i++) { checksum = (UWORD)(databuf[i] + checksum); } databuf[4+Length] = (UBYTE)checksum; databuf[5+Length] = (UBYTE)(checksum>>8); databuf[6+Length] = 0x0D; databuf[7+Length] = 0x0A; for(i=0; i<(Length+8); i++) { UART_PutChar(ptUartTx, databuf[i]); } #endif } /************************ (C) END OF FILE *********************************/