123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- /**
- ******************************************************************************
- * @file uart_gainianzhihui.c
- * @author
- * @version V1.0.0
- * @date 23-12-2015
- * @brief uart_gainianzhihui 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_gainianzhihui.h"
- #include "string.h"
- #include "usart.h"
- #include "canAppl.h"
- #include "syspar.h"
- #include "bikebrake.h"
- #include "bikethrottle.h"
- /******************************
- *
- * Parameter
- *
- ******************************/
- GNZH_OBC2MCInfo_Struct_t GNZH_stOBC2MCInfo;
- GNZH_MC2OBCInfo_Struct_t GNZH_stMC2OBCInfo;
- /******************************
- *
- * Functions
- *
- ******************************/
- /***************************************************************
- Function: SP_OBCSetPara;
- Description: OBC set MC parameter(轮径 电流阈值/功率阈值 限速值)
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void GNZH_OBCSetPara(void)
- {
- /*参数设定*/
-
- /*限速阈值更新*/
- if(GNZH_stOBC2MCInfo.ucSpeedLimiation != 0)
- {
- if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != GNZH_stOBC2MCInfo.ucSpeedLimiation)
- {
- MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = GNZH_stOBC2MCInfo.ucSpeedLimiation;
- MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = GNZH_stOBC2MCInfo.ucSpeedLimiation + 2;
- MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = GNZH_stOBC2MCInfo.ucSpeedLimiation;
- 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(GNZH_stOBC2MCInfo.ucCurrentLimiation != 0)
- {
- #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
- UWORD uwVolt = 480;
- #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
- UWORD uwVolt = 360;
- #else
- UWORD uwVolt = 360;
- #endif
- if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((GNZH_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
- {
- MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((GNZH_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
- MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.TestParaInfoUpdateFlg = TRUE;
- cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
- }
- }
-
- /*轮径*/
- UWORD tmpdata;
- tmpdata = GNZH_stOBC2MCInfo.ucWheelDiameter*8;
- if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
- {
- MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
- MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
- cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
- }
- }
- /***************************************************************
- Function: SP_USART_DataProcess;
- 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 GNZH_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
- {
- /*数据获取*/
- memcpy(&GNZH_stOBC2MCInfo.stCtrlInfo, &Data[1], 9);
- GNZH_stOBC2MCInfo.ucSpeedLimiation = Data[2];
- GNZH_stOBC2MCInfo.ucCurrentLimiation = Data[3];
- GNZH_stOBC2MCInfo.ucWheelDiameter = Data[4];
- GNZH_stOBC2MCInfo.ucCuriseEnable = Data[5];
- GNZH_stOBC2MCInfo.RS1 = Data[6];
- GNZH_stOBC2MCInfo.RS2 = Data[7];
- GNZH_stOBC2MCInfo.uwBrakeReg = (UWORD)(Data[8] + (Data[9]<<8));
-
- /*数据更新*/
- /*if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5)
- {
- MC_ControlCode.GearSt = MC_GearSt_SMART;
- }
- else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 4)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
- }
- else */
- if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 8)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
- }
- else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
- }
- else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 2)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
- }
- else
- {
- MC_ControlCode.GearSt = MC_GearSt_OFF;
- }
- if(GNZH_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;
-
- //GNZH_OBCSetPara();//参数更新
-
- GNZH_SendData_OBC(ptUartTx);//数据回传
- }
- /***************************************************************
- Function: SP_SendData_OBC;
- Description: Send Data to OBC
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void GNZH_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
- {
- UBYTE i = 0;
- UBYTE databuf[13] = {0};
- UWORD checkxor = 0;
-
- /*更新数据发送结构体*/
- if(MC_RunInfo.BusCurrent>25500)
- {
- GNZH_stMC2OBCInfo.ucCurrent = 255;
- }
- else
- {
- GNZH_stMC2OBCInfo.ucCurrent = abs(MC_RunInfo.BusCurrent/100);
- }
-
- if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
- {
- GNZH_stMC2OBCInfo.uwSpeed = 0;
- }
- else
- {
- GNZH_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
- }
-
- if(bikeCruise.CruiseMode == 1)
- {
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Curise = 1;
- }
- else
- {
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Curise = 0;
- }
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.MotorErr = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Brake = BikeBrake_blGetstate();
- //GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Glide = 0;//滑行
- GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
-
- /*数据发送*/
- databuf[0] = 0xA5;
- databuf[1] = GNZH_stMC2OBCInfo.ucCurrent;
- databuf[2] = (UBYTE)(GNZH_stMC2OBCInfo.uwSpeed & 0xFF);
- databuf[3] = (UBYTE)(GNZH_stMC2OBCInfo.uwSpeed >> 8);
- memcpy(&databuf[4],&GNZH_stMC2OBCInfo.stMC_StatusInfo,1);
-
- databuf[5] = 0;
- databuf[6] = 0;
- databuf[7] = 0;
- databuf[8] = 0;
- databuf[9] = 0;
-
- for(i=0; i<10; i=i+2)
- {
- checkxor ^= (UWORD)((databuf[i]<<8) + databuf[i+1]);
- }
-
- databuf[10] = (UBYTE)(checkxor & 0xFF);
- databuf[11] = (UBYTE)(checkxor >> 8);
- databuf[12] = 0xAD;
-
- for(i=0; i<13; i++)
- {
- UART_PutChar(ptUartTx, databuf[i]);
- }
- }
- /***************************************************************
- Function: SP_USART_RxData_Process;
- 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 GNZH_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
- {
- static UBYTE Data[255];
- //UWORD Cmd, ID;
- UWORD i;
- UWORD CheckXorResult=0, CheckXorData=0;
-
- if(dataCount == 13)
- {
- if(buf[0] == 0x55)//帧头
- {
- if(buf[dataCount-1] == 0x0D)//帧尾
- {
- for(i=0; i<dataCount; i++)//数据
- {
- Data[i] = buf[i];
- }
- CheckXorData = (UWORD)(buf[dataCount-3] + (buf[dataCount-2] << 8));
-
- for(i = 0; i<10; i=i+2)
- {
- CheckXorResult ^= (UWORD)((Data[i]<<8) + (Data[i+1]));
- }
- if(CheckXorData == CheckXorResult)// 校验和正常
- {
- GNZH_USART_DataProcess(ptUartTx, &Data[0]);
- }
- }
- }
- }
- }
- /************************ (C) END OF FILE *********************************/
|