uart_Lidian2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. ******************************************************************************
  3. * @file uartSpecial.c
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief uartSpecial 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_Lidian2.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. Lidian2_OBC2MCInfo_Struct_t Lidian2_stOBC2MCInfo;
  50. Lidian2_MC2OBCInfo_Struct_t Lidian2_stMC2OBCInfo;
  51. UBYTE ucWalk_cnt;
  52. /******************************
  53. *
  54. * Functions
  55. *
  56. ******************************/
  57. /***************************************************************
  58. Function: SP_OBCSetPara;
  59. Description: OBC set MC parameter(轮径 电流阈值/功率阈值 低压阈值 限速值)
  60. Call by:
  61. Input Variables: N/A
  62. Output/Return Variables: N/A
  63. Subroutine Call: N/A;
  64. Reference: N/A
  65. ****************************************************************/
  66. void Lidian2_OBCSetPara(void)
  67. {
  68. /*参数设定*/
  69. /*轮径*/
  70. #if 0
  71. UWORD tmpdata;
  72. tmpdata = Lidian2_stOBC2MCInfo.uwWheelDiameter*8/10;
  73. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
  74. {
  75. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
  76. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  77. cp_stFlg.ParaSaveEEFlg = TRUE;
  78. cp_stFlg.ParaUpdateFlg = TRUE;
  79. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  80. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  81. }
  82. /*功率限幅阈值更新*/
  83. #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
  84. UWORD uwVolt = 480;
  85. #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
  86. UWORD uwVolt = 360;
  87. #else
  88. UWORD uwVolt = 360;
  89. #endif
  90. if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
  91. {
  92. MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
  93. MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
  94. cp_stFlg.ParaSaveEEFlg = TRUE;
  95. cp_stFlg.ParaUpdateFlg = TRUE;
  96. cp_stFlg.TestParaInfoUpdateFlg = TRUE;
  97. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  98. }
  99. /*低压保护阈值更新*/
  100. if(MC_UpcInfo.stMContorlInfo.uwAlamUVolV != Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation)
  101. {
  102. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation;
  103. MC_UpcInfo.stMContorlInfo.uwSaveFlg = 1;
  104. cp_stFlg.ParaSaveEEFlg = TRUE;
  105. cp_stFlg.ParaUpdateFlg = TRUE;
  106. cp_stFlg.ParaMCInfoUpdateFlg = TRUE;
  107. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  108. }
  109. #endif
  110. /*限速阈值更新*/
  111. if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != Lidian2_stOBC2MCInfo.ucSpeedLimiation)
  112. {
  113. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = Lidian2_stOBC2MCInfo.ucSpeedLimiation;
  114. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = Lidian2_stOBC2MCInfo.ucSpeedLimiation + 2;
  115. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = Lidian2_stOBC2MCInfo.ucSpeedLimiation;
  116. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  117. MC_UpcInfo.stAssistInfo.uwSaveFlg = 1;
  118. cp_stFlg.ParaSaveEEFlg = TRUE;
  119. cp_stFlg.ParaUpdateFlg = TRUE;
  120. cp_stFlg.ParaAInfoUpdateFlg = TRUE;
  121. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  122. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  123. }
  124. }
  125. /***************************************************************
  126. Function: SP_USART_DataProcess;
  127. Description: Get OBC GearSt and LightControl information
  128. Call by:
  129. Input Variables: N/A
  130. Output/Return Variables: N/A
  131. Subroutine Call: N/A;
  132. Reference: N/A
  133. ****************************************************************/
  134. void Lidian2_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
  135. {
  136. /*数据获取*/
  137. Lidian2_stOBC2MCInfo.ucPowerStyle = Data[3];
  138. Lidian2_stOBC2MCInfo.ucGearSt = Data[4];
  139. memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo1, &Data[5], 1);
  140. Lidian2_stOBC2MCInfo.ucSpeedMagnetSteel = Data[6];
  141. Lidian2_stOBC2MCInfo.uwWheelDiameter = (UWORD)((Data[7]<<8)+Data[8]);//0.1inch
  142. Lidian2_stOBC2MCInfo.ucAssistSensitivity = Data[9];
  143. Lidian2_stOBC2MCInfo.ucAssistPowerMode = Data[10];
  144. Lidian2_stOBC2MCInfo.ucInnerSpeedMagnetSteel = Data[11];
  145. Lidian2_stOBC2MCInfo.ucSpeedLimiation = Data[12];//km/h
  146. Lidian2_stOBC2MCInfo.ucCurrentLimiation = Data[13];
  147. Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation = (UWORD)((Data[14]<<8)+Data[15]);
  148. Lidian2_stOBC2MCInfo.uwThrottlePWM = (UWORD)((Data[16]<<8)+Data[17]);
  149. memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo2, &Data[18], 1);
  150. #if 0
  151. /*数据更新*/
  152. /*WALK挡位逻辑为:WALK优先级最高,仪表长按"-"进入WALK档或退出WALK档,每次长按发送10帧bit1为1*/
  153. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
  154. {
  155. ucWalk_cnt++;
  156. }
  157. else
  158. {
  159. ucWalk_cnt = 0;
  160. }
  161. if(ucWalk_cnt >= 8)
  162. {
  163. ucWalk_cnt = 0;
  164. if(MC_ControlCode.GearSt == MC_GearSt_WALK)
  165. {
  166. MC_ControlCode.GearSt = MC_GearSt_OFF;
  167. }
  168. else
  169. {
  170. MC_ControlCode.GearSt = MC_GearSt_WALK;
  171. }
  172. }
  173. #else
  174. //长按-键进入助推,松开退出
  175. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
  176. {
  177. MC_ControlCode.GearSt = MC_GearSt_WALK;
  178. }
  179. else
  180. {
  181. MC_ControlCode.GearSt = MC_GearSt_OFF;
  182. }
  183. #endif
  184. if(MC_ControlCode.GearSt != MC_GearSt_WALK)
  185. {
  186. if(Lidian2_stOBC2MCInfo.ucGearSt == 15)
  187. {
  188. MC_ControlCode.GearSt = MC_GearSt_SMART;
  189. }
  190. else if(Lidian2_stOBC2MCInfo.ucGearSt == 12)
  191. {
  192. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  193. }
  194. else if((Lidian2_stOBC2MCInfo.ucGearSt == 9)||(Lidian2_stOBC2MCInfo.ucGearSt == 10) )
  195. {
  196. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  197. }
  198. else if(Lidian2_stOBC2MCInfo.ucGearSt == 6)
  199. {
  200. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  201. }
  202. else if((Lidian2_stOBC2MCInfo.ucGearSt == 3)||(Lidian2_stOBC2MCInfo.ucGearSt == 5))
  203. {
  204. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  205. }
  206. else
  207. {
  208. MC_ControlCode.GearSt = MC_GearSt_OFF;
  209. }
  210. }
  211. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Light == 1) //大灯
  212. {
  213. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  214. }
  215. else
  216. {
  217. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  218. }
  219. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  220. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  221. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  222. Lidian2_OBCSetPara();//参数更新
  223. Lidian2_SendData_OBC(ptUartTx);//数据回传
  224. }
  225. /***************************************************************
  226. Function: SP_SendData_OBC;
  227. Description: Send Data to OBC
  228. Call by:
  229. Input Variables: N/A
  230. Output/Return Variables: N/A
  231. Subroutine Call: N/A;
  232. Reference: N/A
  233. ****************************************************************/
  234. void Lidian2_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
  235. {
  236. UBYTE i = 0;
  237. UBYTE sendlen = 14;
  238. UBYTE databuf[14] = {0};
  239. if(MC_ControlCode.GearSt == MC_GearSt_WALK)
  240. {
  241. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 1;
  242. }
  243. else
  244. {
  245. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 0;
  246. }
  247. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
  248. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
  249. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
  250. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
  251. if(bikeCruise.CruiseMode == 1)
  252. {
  253. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 1;
  254. }
  255. else
  256. {
  257. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 0;
  258. }
  259. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.BrakeErr = 0;//暂未判断刹把故障
  260. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.RotorLock = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
  261. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Horizontal = 1;//不实现
  262. if(cp_stFlg.RunModelSelect == CadAssist)
  263. {
  264. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.CadenceSen;
  265. }
  266. else if(cp_stFlg.RunModelSelect == TorqAssist)
  267. {
  268. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.TorqSen;
  269. }
  270. else
  271. {
  272. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = 0;
  273. }
  274. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Brake = BikeBrake_blGetstate();
  275. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.CommErr = 0;//暂不判断通信故障
  276. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Charging = 0;//MC无法判断充电
  277. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.SpeedLimited = 0;
  278. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.GearSt = Lidian2_stOBC2MCInfo.ucGearSt;
  279. Lidian2_stMC2OBCInfo.uwCurrent = (UWORD)abs(MC_RunInfo.BusCurrent/1000);
  280. Lidian2_stMC2OBCInfo.ucCurrentRatio = 0;//未知用途
  281. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  282. {
  283. Lidian2_stMC2OBCInfo.uwSpeed = 0;
  284. }
  285. else
  286. {
  287. Lidian2_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  288. }
  289. Lidian2_stMC2OBCInfo.SOC = MC_RunInfo.SOC;//由BMS实现
  290. Lidian2_stMC2OBCInfo.uwRemainDistance = 0;//由BMS实现
  291. databuf[0] = 0x02;
  292. databuf[1] = sendlen;
  293. databuf[2] = 0x01;
  294. memcpy(&databuf[3],&Lidian2_stMC2OBCInfo.stMC_StatusInfo1,4);
  295. databuf[7] = Lidian2_stMC2OBCInfo.ucCurrentRatio;
  296. databuf[8] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed>>8);
  297. databuf[9] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed&0xFF);
  298. databuf[10] = Lidian2_stMC2OBCInfo.SOC;
  299. databuf[11] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance>>8);
  300. databuf[12] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance&0xFF);
  301. databuf[sendlen-1] = 0;
  302. for(i=0; i<(sendlen-1); i++)
  303. {
  304. databuf[sendlen-1] ^= databuf[i];
  305. }
  306. for(i=0; i<sendlen; i++)
  307. {
  308. UART_PutChar(ptUartTx, databuf[i]);
  309. }
  310. }
  311. /***************************************************************
  312. Function: SP_USART_RxData_Process;
  313. Description: Get Uart Data following the protocol
  314. Call by:
  315. Input Variables: N/A
  316. Output/Return Variables: N/A
  317. Subroutine Call: N/A;
  318. Reference: N/A
  319. ****************************************************************/
  320. void Lidian2_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  321. {
  322. static UBYTE Data[255];
  323. //UWORD Cmd, ID;
  324. UWORD i;
  325. UWORD CheckxorResult=0;
  326. if(dataCount >= 20)
  327. {
  328. if(buf[0] == 0x01)//地址
  329. {
  330. if(buf[1] == 20)//帧长
  331. {
  332. if(buf[2] == 0x01)//命令号
  333. {
  334. if(buf[1] == dataCount)
  335. {
  336. for(i=0; i<dataCount; i++)//数据
  337. {
  338. Data[i] = buf[i];
  339. CheckxorResult ^= Data[i];
  340. }
  341. if(CheckxorResult == 0)// 校验和正常
  342. {
  343. Lidian2_USART_DataProcess(ptUartTx, Data);
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. /************************ (C) END OF FILE *********************************/