uart_lanfeng9.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. ******************************************************************************
  3. * @file uart_lanfeng9.c
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief uart_lanfeng9 V9.0.
  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_lanfeng9.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. Lanfeng9_OBC2MCInfo_Struct_t Lanfeng9_stOBC2MCInfo;
  50. Lanfeng9_OBC2MCConfig_Struct_t Lanfeng9_stOBC2MCConfig;
  51. /******************************
  52. *
  53. * Functions
  54. *
  55. ******************************/
  56. /***************************************************************
  57. Function:
  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 Lanfeng9_OBCSetPara(Lanfeng9OBC_SetInfo1_Struct_t obc_setinfo1, Lanfeng9_OBC2MCConfig_Struct_t obc2mcconfig, UBYTE FrameBegin)
  66. {
  67. UWORD SpeedLimitation = 0;
  68. UWORD WheelPerimeter = 0;
  69. /*参数设定*/
  70. if(FrameBegin == 0x41)
  71. {
  72. /*限速阈值更新*/
  73. SpeedLimitation = (UWORD)(obc_setinfo1.Set_Bit.ucSpeedLimitation + 10);
  74. if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != SpeedLimitation)
  75. {
  76. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = SpeedLimitation;
  77. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = SpeedLimitation + 2;
  78. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = SpeedLimitation;
  79. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  80. MC_UpcInfo.stAssistInfo.uwSaveFlg = 1;
  81. cp_stFlg.ParaSaveEEFlg = TRUE;
  82. cp_stFlg.ParaUpdateFlg = TRUE;
  83. cp_stFlg.ParaAInfoUpdateFlg = TRUE;
  84. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  85. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  86. }
  87. /*轮径*/
  88. /*轮径更新*/
  89. if(obc_setinfo1.Set_Bit.ucWheelDiameterCode <= 5)
  90. {
  91. WheelPerimeter = (obc_setinfo1.Set_Bit.ucWheelDiameterCode*2 + 16) * 8; //3.14*2.54=7.9756=8
  92. }
  93. else if(obc_setinfo1.Set_Bit.ucWheelDiameterCode == 6)
  94. {
  95. WheelPerimeter = 219; //3.14*700mm=2198mm
  96. }
  97. else
  98. {
  99. WheelPerimeter = 223; //3.14*2.54*28
  100. }
  101. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != WheelPerimeter)
  102. {
  103. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = WheelPerimeter;
  104. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  105. cp_stFlg.ParaSaveEEFlg = TRUE;
  106. cp_stFlg.ParaUpdateFlg = TRUE;
  107. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  108. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  109. }
  110. }
  111. else if(FrameBegin == 0x42)
  112. {
  113. /*限速阈值更新*/
  114. if(obc2mcconfig.stSetInfo2.Set_Bit.AssistSpeedLimitMode == 0)
  115. {
  116. SpeedLimitation = 45;
  117. }
  118. else
  119. {
  120. SpeedLimitation = 32;
  121. }
  122. if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != SpeedLimitation)
  123. {
  124. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = SpeedLimitation;
  125. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = SpeedLimitation + 2;
  126. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = SpeedLimitation;
  127. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  128. MC_UpcInfo.stAssistInfo.uwSaveFlg = 1;
  129. cp_stFlg.ParaSaveEEFlg = TRUE;
  130. cp_stFlg.ParaUpdateFlg = TRUE;
  131. cp_stFlg.ParaAInfoUpdateFlg = TRUE;
  132. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  133. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  134. }
  135. /*灯闪更新*/
  136. if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 0)
  137. {
  138. }
  139. else if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 1)
  140. {
  141. }
  142. else if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 2)
  143. {
  144. }
  145. else if(obc2mcconfig.stSetInfo2.Set_Bit.LightMode == 3)
  146. {
  147. }
  148. /*轮径更新*/
  149. if((obc2mcconfig.ucWheelPerimeter > 0) && (obc2mcconfig.ucWheelPerimeter < 255))//数据有效
  150. {
  151. WheelPerimeter = obc2mcconfig.ucWheelPerimeter;
  152. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != WheelPerimeter)
  153. {
  154. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = WheelPerimeter;
  155. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  156. cp_stFlg.ParaSaveEEFlg = TRUE;
  157. cp_stFlg.ParaUpdateFlg = TRUE;
  158. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  159. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  160. }
  161. }
  162. }
  163. }
  164. /***************************************************************
  165. Function: ;
  166. Description: Get OBC GearSt and LightControl information
  167. Call by:
  168. Input Variables: N/A
  169. Output/Return Variables: N/A
  170. Subroutine Call: N/A;
  171. Reference: N/A
  172. ****************************************************************/
  173. void Lanfeng9_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE FrameBegin, UBYTE* Data)
  174. {
  175. if(FrameBegin == 0x41)
  176. {
  177. /*数据获取*/
  178. memcpy(&Lanfeng9_stOBC2MCInfo.stCtrlInfo, &Data[1], 1);
  179. memcpy(&Lanfeng9_stOBC2MCInfo.stSetInfo1, &Data[2], 1);
  180. /*数据更新*/
  181. if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 10)
  182. {
  183. MC_ControlCode.GearSt = MC_GearSt_WALK;
  184. }
  185. else if((Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5) || (Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 6))
  186. {
  187. MC_ControlCode.GearSt = MC_GearSt_SMART;
  188. }
  189. else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 4)
  190. {
  191. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  192. }
  193. else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 3)
  194. {
  195. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  196. }
  197. else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 2)
  198. {
  199. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  200. }
  201. else if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 1)
  202. {
  203. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  204. }
  205. else
  206. {
  207. MC_ControlCode.GearSt = MC_GearSt_OFF;
  208. }
  209. if(Lanfeng9_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.Light == 1) //大灯
  210. {
  211. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  212. }
  213. else
  214. {
  215. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  216. }
  217. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  218. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  219. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  220. // Lanfeng9_OBCSetPara(Lanfeng9_stOBC2MCInfo.stSetInfo1, Lanfeng9_stOBC2MCConfig, FrameBegin);//参数更新
  221. Lanfeng9_SendData_OBC(ptUartTx,FrameBegin);
  222. }
  223. else if(FrameBegin == 0x42) //读取控制器版本
  224. {
  225. memcpy(&Lanfeng9_stOBC2MCConfig.stSetInfo2, &Data[1], 1);
  226. memcpy(&Lanfeng9_stOBC2MCConfig.ucWheelPerimeter, &Data[2], 1);
  227. // Lanfeng9_OBCSetPara(Lanfeng9_stOBC2MCInfo.stSetInfo1, Lanfeng9_stOBC2MCConfig, FrameBegin);//参数更新
  228. }
  229. else if(FrameBegin == 0x43) //读取控制器版本
  230. {
  231. if((Data[1] == 0x56) && (Data[2] == 0x45))
  232. {
  233. Lanfeng9_SendData_OBC(ptUartTx,FrameBegin);
  234. }
  235. }
  236. else if(FrameBegin == 0x44) //读取控制器版本2
  237. {
  238. if((Data[1] == 0x56) && (Data[2] == 0x32))
  239. {
  240. Lanfeng9_SendData_OBC(ptUartTx,FrameBegin);
  241. }
  242. }
  243. }
  244. /***************************************************************
  245. Function:
  246. Description: Send Data to OBC
  247. Call by:
  248. Input Variables: N/A
  249. Output/Return Variables: N/A
  250. Subroutine Call: N/A;
  251. Reference: N/A
  252. ****************************************************************/
  253. void Lanfeng9_SendData_OBC(USART_Buf_TypeDef* ptUartTx, UBYTE FrameBegin)
  254. {
  255. UBYTE i = 0;
  256. UBYTE databuf[10] = {0};
  257. UWORD checkxor = 0;
  258. UBYTE datalen = 10;
  259. UWORD Speed = 0;
  260. UBYTE Current = 0;
  261. /*数据发送*/
  262. databuf[0] = FrameBegin;
  263. if(FrameBegin == 0x41)
  264. {
  265. datalen = 7;
  266. if(1 == MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage)
  267. {
  268. databuf[1] = 0x01;//欠压
  269. }
  270. else
  271. {
  272. databuf[1] = 0x00;
  273. }
  274. Current = (UBYTE)(abs(MC_RunInfo.BusCurrent/1000)*3);//电流
  275. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  276. {
  277. Speed = 0;
  278. }
  279. else
  280. {
  281. Speed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  282. }
  283. databuf[2] = Current;
  284. databuf[3] = (UBYTE)((Speed & 0xFF00)>>8);
  285. databuf[4] = (UBYTE)(Speed & 0xFF);
  286. if(1 == MC_ErrorCode.ERROR_Bit.Protect_OverCurrent)
  287. {
  288. databuf[5] = 0x01;//电流异常
  289. }
  290. else if(1 == MC_ErrorCode.ERROR_Bit.Fault_Throttle)
  291. {
  292. databuf[5] = 0x02;//转把异常
  293. }
  294. else if(1 == MC_ErrorCode.ERROR_Bit.Fault_PhaseLine)
  295. {
  296. databuf[5] = 0x03;//电机缺相
  297. }
  298. else if(1 == MC_ErrorCode.ERROR_Bit.Fault_HallSensor)
  299. {
  300. databuf[5] = 0x04;//电机霍尔异常
  301. }
  302. /*else if(0)
  303. {
  304. databuf[5] = 0x05;//刹车异常
  305. }*/
  306. else if(1 == MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage)
  307. {
  308. databuf[5] = 0x06;//欠压
  309. }
  310. else if(1 == MC_ErrorCode.ERROR_Bit.Protect_LockRotor)
  311. {
  312. databuf[5] = 0x07;//电机堵转
  313. }
  314. /*else if(0)
  315. {
  316. databuf[5] = 0x08;//通讯故障
  317. }*/
  318. }
  319. else if(FrameBegin == 0x43)
  320. {
  321. datalen = 7;
  322. databuf[1] = MC_VerInfo.FW_Version[1] - 48;
  323. databuf[2] = MC_VerInfo.FW_Version[3] - 48;
  324. databuf[3] = MC_VerInfo.FW_Version[5] - 48;
  325. databuf[4] = 0xff;
  326. databuf[5] = 0xff;
  327. }
  328. else if(FrameBegin == 0x44)
  329. {
  330. datalen = 10;
  331. for(i=0;i<8;i++)
  332. {
  333. databuf[i+1] = MC_VerInfo.FW_Version[7+i];
  334. }
  335. }
  336. for(i=1; i<(datalen-1); i++)
  337. {
  338. checkxor ^= databuf[i];
  339. }
  340. databuf[datalen-1] = checkxor;
  341. for(i=0; i<datalen; i++)
  342. {
  343. UART_PutChar(ptUartTx, databuf[i]);
  344. }
  345. }
  346. /***************************************************************
  347. Function:
  348. Description: Get Uart Data following the protocol
  349. Call by:
  350. Input Variables: N/A
  351. Output/Return Variables: N/A
  352. Subroutine Call: N/A;
  353. Reference: N/A
  354. ****************************************************************/
  355. void Lanfeng9_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  356. {
  357. static UBYTE Data[255];
  358. UWORD i;
  359. UBYTE CheckXorResult=0, CheckXorData=0;
  360. UBYTE FrameBegin, FrameEnd;
  361. if(dataCount == 5)
  362. {
  363. //帧头
  364. FrameBegin = buf[0];
  365. if((FrameBegin == 0x41) || (FrameBegin == 0x42) || (FrameBegin == 0x43) || (FrameBegin == 0x44))
  366. {
  367. for(i=0; i<dataCount; i++)//数据
  368. {
  369. Data[i] = buf[i];
  370. }
  371. CheckXorData = Data[dataCount-2];
  372. for(i = 1; i<(dataCount-2); i++)
  373. {
  374. CheckXorResult ^= Data[i];
  375. }
  376. FrameEnd = Data[dataCount-1];
  377. if((CheckXorData == CheckXorResult) && (FrameEnd == 0x0E))// 校验和帧尾正常
  378. {
  379. Lanfeng9_USART_DataProcess(ptUartTx, FrameBegin, &Data[0]);
  380. }
  381. }
  382. }
  383. }
  384. /************************ (C) END OF FILE *********************************/