123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /**
- ******************************************************************************
- * @file uart_lanfeng9.c
- * @author
- * @version V1.0.0
- * @date 23-12-2015
- * @brief uart_lanfeng9 V9.0.
- ******************************************************************************
- *
- * 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_lanfeng9.h"
- #include "string.h"
- #include "usart.h"
- #include "canAppl.h"
- #include "syspar.h"
- #include "bikebrake.h"
- #include "bikethrottle.h"
- /******************************
- *
- * Parameter
- *
- ******************************/
- Lanfeng9_OBC2MCInfo_Struct_t Lanfeng9_stOBC2MCInfo;
- Lanfeng9_OBC2MCConfig_Struct_t Lanfeng9_stOBC2MCConfig;
- /******************************
- *
- * Functions
- *
- ******************************/
- /***************************************************************
- Function:
- Description: OBC set MC parameter(车轮周长 限速值)
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lanfeng9_OBCSetPara(Lanfeng9OBC_SetInfo1_Struct_t obc_setinfo1, Lanfeng9_OBC2MCConfig_Struct_t obc2mcconfig, UBYTE FrameBegin)
- {
- UWORD SpeedLimitation = 0;
- UWORD WheelPerimeter = 0;
- /*参数设定*/
-
- if(FrameBegin == 0x41)
- {
- /*限速阈值更新*/
- SpeedLimitation = (UWORD)(obc_setinfo1.Set_Bit.ucSpeedLimitation + 10);
- if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != SpeedLimitation)
- {
- MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = SpeedLimitation;
- MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = SpeedLimitation + 2;
- MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = SpeedLimitation;
- 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;
- }
-
- /*轮径*/
- /*轮径更新*/
- if(obc_setinfo1.Set_Bit.ucWheelDiameterCode <= 5)
- {
- WheelPerimeter = (obc_setinfo1.Set_Bit.ucWheelDiameterCode*2 + 16) * 8; //3.14*2.54=7.9756=8
- }
- else if(obc_setinfo1.Set_Bit.ucWheelDiameterCode == 6)
- {
- WheelPerimeter = 219; //3.14*700mm=2198mm
- }
- else
- {
- WheelPerimeter = 223; //3.14*2.54*28
- }
- if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != WheelPerimeter)
- {
- MC_UpcInfo.stBikeInfo.uwWheelPerimeter = WheelPerimeter;
- MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
- cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
- }
- }
- else if(FrameBegin == 0x42)
- {
- /*限速阈值更新*/
- if(obc2mcconfig.stSetInfo2.Set_Bit.AssistSpeedLimitMode == 0)
- {
- SpeedLimitation = 45;
- }
- else
- {
- SpeedLimitation = 32;
- }
- if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != SpeedLimitation)
- {
- MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = SpeedLimitation;
- MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = SpeedLimitation + 2;
- MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = SpeedLimitation;
- 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;
- }
-
- /*灯闪更新*/
- if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 0)
- {
-
- }
- else if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 1)
- {
-
- }
- else if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 2)
- {
-
- }
- else if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 3)
- {
-
- }
-
- /*轮径更新*/
- if((obc2mcconfig.ucWheelPerimeter > 0) && (obc2mcconfig.ucWheelPerimeter < 255))//数据有效
- {
- WheelPerimeter = obc2mcconfig.ucWheelPerimeter;
- if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != WheelPerimeter)
- {
- MC_UpcInfo.stBikeInfo.uwWheelPerimeter = WheelPerimeter;
- MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
- cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
- }
- }
- }
- }
- /***************************************************************
- Function: ;
- Description: Get OBC GearSt and LightControl information
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lanfeng9_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE FrameBegin, UBYTE* Data)
- {
- if(FrameBegin == 0x41)
- {
- /*数据获取*/
- memcpy(&Lanfeng9_stOBC2MCInfo.stCtrlInfo, &Data[1], 1);
- memcpy(&Lanfeng9_stOBC2MCInfo.stSetInfo1, &Data[2], 1);
-
- /*数据更新*/
- if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 10)
- {
- MC_ControlCode.GearSt = MC_GearSt_WALK;
- }
- else if((Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5) || (Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 6))
- {
- MC_ControlCode.GearSt = MC_GearSt_SMART;
- }
- else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 4)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
- }
- else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 3)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
- }
- else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 2)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
- }
- else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 1)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
- }
- else
- {
- MC_ControlCode.GearSt = MC_GearSt_OFF;
- }
-
- if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.Light == 1) //大灯
- {
- MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
- }
- else
- {
- MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
- }
-
- MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
- MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
- ulOBC_ComTimeOutCount = cp_ulSystickCnt;
-
- // Lanfeng9_OBCSetPara(Lanfeng9_stOBC2MCInfo.stSetInfo1, Lanfeng9_stOBC2MCConfig, FrameBegin);//参数更新
-
- Lanfeng9_SendData_OBC(ptUartTx,FrameBegin);
- }
- else if(FrameBegin == 0x42) //读取控制器版本
- {
- memcpy(&Lanfeng9_stOBC2MCConfig.stSetInfo2, &Data[1], 1);
- memcpy(&Lanfeng9_stOBC2MCConfig.ucWheelPerimeter, &Data[2], 1);
-
- // Lanfeng9_OBCSetPara(Lanfeng9_stOBC2MCInfo.stSetInfo1, Lanfeng9_stOBC2MCConfig, FrameBegin);//参数更新
- }
- else if(FrameBegin == 0x43) //读取控制器版本
- {
- if((Data[1] == 0x56) && (Data[2] == 0x45))
- {
- Lanfeng9_SendData_OBC(ptUartTx,FrameBegin);
- }
- }
- else if(FrameBegin == 0x44) //读取控制器版本2
- {
- if((Data[1] == 0x56) && (Data[2] == 0x32))
- {
- Lanfeng9_SendData_OBC(ptUartTx,FrameBegin);
- }
- }
- }
- /***************************************************************
- Function:
- Description: Send Data to OBC
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lanfeng9_SendData_OBC(USART_Buf_TypeDef* ptUartTx, UBYTE FrameBegin)
- {
- UBYTE i = 0;
- UBYTE databuf[10] = {0};
- UWORD checkxor = 0;
- UBYTE datalen = 10;
- UWORD Speed = 0;
- UBYTE Current = 0;
-
- /*数据发送*/
- databuf[0] = FrameBegin;
- if(FrameBegin == 0x41)
- {
- datalen = 7;
- if(1 == MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage)
- {
- databuf[1] = 0x01;//欠压
- }
- else
- {
- databuf[1] = 0x00;
- }
-
- Current = (UBYTE)(abs(MC_RunInfo.BusCurrent/1000)*3);//电流
-
- if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
- {
- Speed = 0;
- }
- else
- {
- Speed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
- }
- databuf[2] = Current;
- databuf[3] = (UBYTE)((Speed & 0xFF00)>>8);
- databuf[4] = (UBYTE)(Speed & 0xFF);
-
- if(1 == MC_ErrorCode.ERROR_Bit.Protect_OverCurrent)
- {
- databuf[5] = 0x01;//电流异常
- }
- else if(1 == MC_ErrorCode.ERROR_Bit.Fault_Throttle)
- {
- databuf[5] = 0x02;//转把异常
- }
- else if(1 == MC_ErrorCode.ERROR_Bit.Fault_PhaseLine)
- {
- databuf[5] = 0x03;//电机缺相
- }
- else if(1 == MC_ErrorCode.ERROR_Bit.Fault_HallSensor)
- {
- databuf[5] = 0x04;//电机霍尔异常
- }
- /*else if(0)
- {
- databuf[5] = 0x05;//刹车异常
- }*/
- else if(1 == MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage)
- {
- databuf[5] = 0x06;//欠压
- }
- else if(1 == MC_ErrorCode.ERROR_Bit.Protect_LockRotor)
- {
- databuf[5] = 0x07;//电机堵转
- }
- /*else if(0)
- {
- databuf[5] = 0x08;//通讯故障
- }*/
- }
- else if(FrameBegin == 0x43)
- {
- datalen = 7;
- databuf[1] = MC_VerInfo.FW_Version[1] - 48;
- databuf[2] = MC_VerInfo.FW_Version[3] - 48;
- databuf[3] = MC_VerInfo.FW_Version[5] - 48;
- databuf[4] = 0xff;
- databuf[5] = 0xff;
- }
- else if(FrameBegin == 0x44)
- {
- datalen = 10;
- for(i=0;i<8;i++)
- {
- databuf[i+1] = MC_VerInfo.FW_Version[7+i];
- }
- }
-
- for(i=1; i<(datalen-1); i++)
- {
- checkxor ^= databuf[i];
- }
-
- databuf[datalen-1] = checkxor;
-
- for(i=0; i<datalen; i++)
- {
- UART_PutChar(ptUartTx, databuf[i]);
- }
- }
- /***************************************************************
- Function:
- Description: Get Uart Data following the protocol
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lanfeng9_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
- {
- static UBYTE Data[255];
- UWORD i;
- UBYTE CheckXorResult=0, CheckXorData=0;
- UBYTE FrameBegin, FrameEnd;
-
- if(dataCount == 5)
- {
- //帧头
- FrameBegin = buf[0];
- if((FrameBegin == 0x41) || (FrameBegin == 0x42) || (FrameBegin == 0x43) || (FrameBegin == 0x44))
- {
- for(i=0; i<dataCount; i++)//数据
- {
- Data[i] = buf[i];
- }
- CheckXorData = Data[dataCount-2];
- for(i = 1; i<(dataCount-2); i++)
- {
- CheckXorResult ^= Data[i];
- }
-
- FrameEnd = Data[dataCount-1];
-
- if((CheckXorData == CheckXorResult) && (FrameEnd == 0x0E))// 校验和帧尾正常
- {
- Lanfeng9_USART_DataProcess(ptUartTx, FrameBegin, &Data[0]);
- }
- }
- }
- }
- /************************ (C) END OF FILE *********************************/
|