uart_gainianzhihui.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. ******************************************************************************
  3. * @file uart_gainianzhihui.c
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief uart_gainianzhihui function.
  8. ******************************************************************************
  9. *
  10. * COPYRIGHT(c) 2015 STMicroelectronics
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "uart_gainianzhihui.h"
  38. #include "string.h"
  39. #include "usart.h"
  40. #include "canAppl.h"
  41. #include "syspar.h"
  42. #include "bikebrake.h"
  43. #include "bikethrottle.h"
  44. /******************************
  45. *
  46. * Parameter
  47. *
  48. ******************************/
  49. GNZH_OBC2MCInfo_Struct_t GNZH_stOBC2MCInfo;
  50. GNZH_MC2OBCInfo_Struct_t GNZH_stMC2OBCInfo;
  51. /******************************
  52. *
  53. * Functions
  54. *
  55. ******************************/
  56. /***************************************************************
  57. Function: SP_OBCSetPara;
  58. Description: OBC set MC parameter(轮径 电流阈值/功率阈值 限速值)
  59. Call by:
  60. Input Variables: N/A
  61. Output/Return Variables: N/A
  62. Subroutine Call: N/A;
  63. Reference: N/A
  64. ****************************************************************/
  65. void GNZH_OBCSetPara(void)
  66. {
  67. /*参数设定*/
  68. /*限速阈值更新*/
  69. if(GNZH_stOBC2MCInfo.ucSpeedLimiation != 0)
  70. {
  71. if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != GNZH_stOBC2MCInfo.ucSpeedLimiation)
  72. {
  73. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = GNZH_stOBC2MCInfo.ucSpeedLimiation;
  74. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = GNZH_stOBC2MCInfo.ucSpeedLimiation + 2;
  75. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = GNZH_stOBC2MCInfo.ucSpeedLimiation;
  76. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  77. MC_UpcInfo.stAssistInfo.uwSaveFlg = 1;
  78. cp_stFlg.ParaSaveEEFlg = TRUE;
  79. cp_stFlg.ParaUpdateFlg = TRUE;
  80. cp_stFlg.ParaAInfoUpdateFlg = TRUE;
  81. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  82. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  83. }
  84. }
  85. /*功率限幅阈值更新*/
  86. if(GNZH_stOBC2MCInfo.ucCurrentLimiation != 0)
  87. {
  88. #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
  89. UWORD uwVolt = 480;
  90. #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
  91. UWORD uwVolt = 360;
  92. #else
  93. UWORD uwVolt = 360;
  94. #endif
  95. if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((GNZH_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
  96. {
  97. MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((GNZH_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
  98. MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
  99. cp_stFlg.ParaSaveEEFlg = TRUE;
  100. cp_stFlg.ParaUpdateFlg = TRUE;
  101. cp_stFlg.TestParaInfoUpdateFlg = TRUE;
  102. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  103. }
  104. }
  105. /*轮径*/
  106. UWORD tmpdata;
  107. tmpdata = GNZH_stOBC2MCInfo.ucWheelDiameter*8;
  108. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
  109. {
  110. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
  111. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  112. cp_stFlg.ParaSaveEEFlg = TRUE;
  113. cp_stFlg.ParaUpdateFlg = TRUE;
  114. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  115. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  116. }
  117. }
  118. /***************************************************************
  119. Function: SP_USART_DataProcess;
  120. Description: Get OBC GearSt and LightControl information
  121. Call by:
  122. Input Variables: N/A
  123. Output/Return Variables: N/A
  124. Subroutine Call: N/A;
  125. Reference: N/A
  126. ****************************************************************/
  127. void GNZH_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
  128. {
  129. /*数据获取*/
  130. memcpy(&GNZH_stOBC2MCInfo.stCtrlInfo, &Data[1], 9);
  131. GNZH_stOBC2MCInfo.ucSpeedLimiation = Data[2];
  132. GNZH_stOBC2MCInfo.ucCurrentLimiation = Data[3];
  133. GNZH_stOBC2MCInfo.ucWheelDiameter = Data[4];
  134. GNZH_stOBC2MCInfo.ucCuriseEnable = Data[5];
  135. GNZH_stOBC2MCInfo.RS1 = Data[6];
  136. GNZH_stOBC2MCInfo.RS2 = Data[7];
  137. GNZH_stOBC2MCInfo.uwBrakeReg = (UWORD)(Data[8] + (Data[9]<<8));
  138. /*数据更新*/
  139. /*if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5)
  140. {
  141. MC_ControlCode.GearSt = MC_GearSt_SMART;
  142. }
  143. else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 4)
  144. {
  145. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  146. }
  147. else */
  148. if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 8)
  149. {
  150. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  151. }
  152. else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5)
  153. {
  154. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  155. }
  156. else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 2)
  157. {
  158. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  159. }
  160. else
  161. {
  162. MC_ControlCode.GearSt = MC_GearSt_OFF;
  163. }
  164. if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.Light == 1) //大灯
  165. {
  166. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  167. }
  168. else
  169. {
  170. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  171. }
  172. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  173. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  174. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  175. //GNZH_OBCSetPara();//参数更新
  176. GNZH_SendData_OBC(ptUartTx);//数据回传
  177. }
  178. /***************************************************************
  179. Function: SP_SendData_OBC;
  180. Description: Send Data to OBC
  181. Call by:
  182. Input Variables: N/A
  183. Output/Return Variables: N/A
  184. Subroutine Call: N/A;
  185. Reference: N/A
  186. ****************************************************************/
  187. void GNZH_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
  188. {
  189. UBYTE i = 0;
  190. UBYTE databuf[13] = {0};
  191. UWORD checkxor = 0;
  192. /*更新数据发送结构体*/
  193. if(MC_RunInfo.BusCurrent>25500)
  194. {
  195. GNZH_stMC2OBCInfo.ucCurrent = 255;
  196. }
  197. else
  198. {
  199. GNZH_stMC2OBCInfo.ucCurrent = abs(MC_RunInfo.BusCurrent/100);
  200. }
  201. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  202. {
  203. GNZH_stMC2OBCInfo.uwSpeed = 0;
  204. }
  205. else
  206. {
  207. GNZH_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  208. }
  209. if(bikeCruise.CruiseMode == 1)
  210. {
  211. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Curise = 1;
  212. }
  213. else
  214. {
  215. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Curise = 0;
  216. }
  217. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
  218. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.MotorErr = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
  219. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
  220. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
  221. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Brake = BikeBrake_blGetstate();
  222. //GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Glide = 0;//滑行
  223. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
  224. /*数据发送*/
  225. databuf[0] = 0xA5;
  226. databuf[1] = GNZH_stMC2OBCInfo.ucCurrent;
  227. databuf[2] = (UBYTE)(GNZH_stMC2OBCInfo.uwSpeed & 0xFF);
  228. databuf[3] = (UBYTE)(GNZH_stMC2OBCInfo.uwSpeed >> 8);
  229. memcpy(&databuf[4],&GNZH_stMC2OBCInfo.stMC_StatusInfo,1);
  230. databuf[5] = 0;
  231. databuf[6] = 0;
  232. databuf[7] = 0;
  233. databuf[8] = 0;
  234. databuf[9] = 0;
  235. for(i=0; i<10; i=i+2)
  236. {
  237. checkxor ^= (UWORD)((databuf[i]<<8) + databuf[i+1]);
  238. }
  239. databuf[10] = (UBYTE)(checkxor & 0xFF);
  240. databuf[11] = (UBYTE)(checkxor >> 8);
  241. databuf[12] = 0xAD;
  242. for(i=0; i<13; i++)
  243. {
  244. UART_PutChar(ptUartTx, databuf[i]);
  245. }
  246. }
  247. /***************************************************************
  248. Function: SP_USART_RxData_Process;
  249. Description: Get Uart Data following the protocol
  250. Call by:
  251. Input Variables: N/A
  252. Output/Return Variables: N/A
  253. Subroutine Call: N/A;
  254. Reference: N/A
  255. ****************************************************************/
  256. void GNZH_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  257. {
  258. static UBYTE Data[255];
  259. //UWORD Cmd, ID;
  260. UWORD i;
  261. UWORD CheckXorResult=0, CheckXorData=0;
  262. if(dataCount == 13)
  263. {
  264. if(buf[0] == 0x55)//帧头
  265. {
  266. if(buf[dataCount-1] == 0x0D)//帧尾
  267. {
  268. for(i=0; i<dataCount; i++)//数据
  269. {
  270. Data[i] = buf[i];
  271. }
  272. CheckXorData = (UWORD)(buf[dataCount-3] + (buf[dataCount-2] << 8));
  273. for(i = 0; i<10; i=i+2)
  274. {
  275. CheckXorResult ^= (UWORD)((Data[i]<<8) + (Data[i+1]));
  276. }
  277. if(CheckXorData == CheckXorResult)// 校验和正常
  278. {
  279. GNZH_USART_DataProcess(ptUartTx, &Data[0]);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. /************************ (C) END OF FILE *********************************/