uart_lanfeng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. ******************************************************************************
  3. * @file uart_lanfeng.c
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief uart_lanfeng 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_lanfeng.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. Lanfeng_OBC2MCInfo_Struct_t Lanfeng_stOBC2MCInfo;
  50. /******************************
  51. *
  52. * Functions
  53. *
  54. ******************************/
  55. /***************************************************************
  56. Function:
  57. Description: OBC set MC parameter(轮径 电流阈值/功率阈值 限速值)
  58. Call by:
  59. Input Variables: N/A
  60. Output/Return Variables: N/A
  61. Subroutine Call: N/A;
  62. Reference: N/A
  63. ****************************************************************/
  64. void Lanfeng_OBCSetPara(LanfengOBC_SetInfo_Struct_t obc_setinfo)
  65. {
  66. UWORD SpeedLimitation = 0;
  67. UWORD WheelPerimeter = 0;
  68. /*参数设定*/
  69. /*限速阈值更新*/
  70. SpeedLimitation = (UWORD)(obc_setinfo.Set_Bit.ucSpeedLimitation + 12);
  71. if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != SpeedLimitation)
  72. {
  73. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = SpeedLimitation;
  74. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = SpeedLimitation + 2;
  75. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = SpeedLimitation;
  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(obc_setinfo.Set_Bit.ucWheelDiameterCode <= 5)
  87. {
  88. WheelPerimeter = (obc_setinfo.Set_Bit.ucWheelDiameterCode*2 + 16) * 8; //3.14*2.54=7.9756=8
  89. }
  90. else if(obc_setinfo.Set_Bit.ucWheelDiameterCode == 6)
  91. {
  92. WheelPerimeter = 219; //3.14*700mm=2198mm
  93. }
  94. else
  95. {
  96. WheelPerimeter = 223; //3.14*2.54*28
  97. }
  98. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != WheelPerimeter)
  99. {
  100. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = WheelPerimeter;
  101. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  102. cp_stFlg.ParaSaveEEFlg = TRUE;
  103. cp_stFlg.ParaUpdateFlg = TRUE;
  104. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  105. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  106. }
  107. }
  108. /***************************************************************
  109. Function: ;
  110. Description: Get OBC GearSt and LightControl information
  111. Call by:
  112. Input Variables: N/A
  113. Output/Return Variables: N/A
  114. Subroutine Call: N/A;
  115. Reference: N/A
  116. ****************************************************************/
  117. void Lanfeng_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE Cmd, UBYTE* Data)
  118. {
  119. if(Cmd == 0x2C)
  120. {
  121. /*数据获取*/
  122. memcpy(&Lanfeng_stOBC2MCInfo.stCtrlInfo1, &Data[2], 1);
  123. memcpy(&Lanfeng_stOBC2MCInfo.stCtrlInfo2, &Data[3], 1);
  124. memcpy(&Lanfeng_stOBC2MCInfo.stSetInfo, &Data[4], 1);
  125. /*数据更新*/
  126. if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.Walk == 1)
  127. {
  128. MC_ControlCode.GearSt = MC_GearSt_WALK;
  129. }
  130. else if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ucGearSt == 5)
  131. {
  132. MC_ControlCode.GearSt = MC_GearSt_SMART;
  133. }
  134. else if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ucGearSt == 4)
  135. {
  136. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  137. }
  138. else if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ucGearSt == 3)
  139. {
  140. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  141. }
  142. else if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ucGearSt == 2)
  143. {
  144. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  145. }
  146. else if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ucGearSt == 1)
  147. {
  148. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  149. }
  150. else
  151. {
  152. MC_ControlCode.GearSt = MC_GearSt_OFF;
  153. }
  154. if(Lanfeng_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.Light == 1) //大灯
  155. {
  156. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  157. }
  158. else
  159. {
  160. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  161. }
  162. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  163. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  164. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  165. //Lanfeng_OBCSetPara(Lanfeng_stOBC2MCInfo.stSetInfo);//参数更新
  166. Lanfeng_SendData_OBC(ptUartTx,Cmd);
  167. }
  168. else if(Cmd == 0x2D) //读取控制器版本
  169. {
  170. if((Data[2] == 0x56) && (Data[3] == 0x45) && (Data[4] == 0x52))
  171. {
  172. Lanfeng_SendData_OBC(ptUartTx,Cmd);
  173. }
  174. }
  175. }
  176. /***************************************************************
  177. Function:
  178. Description: Send Data to OBC
  179. Call by:
  180. Input Variables: N/A
  181. Output/Return Variables: N/A
  182. Subroutine Call: N/A;
  183. Reference: N/A
  184. ****************************************************************/
  185. void Lanfeng_SendData_OBC(USART_Buf_TypeDef* ptUartTx, UBYTE Cmd)
  186. {
  187. UBYTE i = 0;
  188. UBYTE databuf[10] = {0};
  189. UWORD checksum = 0;
  190. UBYTE datalen = 10;
  191. UWORD Speed = 0;
  192. UBYTE Current = 0;
  193. /*数据发送*/
  194. databuf[0] = 0x3A;
  195. databuf[1] = Cmd;
  196. if(Cmd == 0x2C)
  197. {
  198. Current = (UBYTE)(abs(MC_RunInfo.BusCurrent/1000)*3);//电流
  199. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  200. {
  201. Speed = 0;
  202. }
  203. else
  204. {
  205. Speed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  206. }
  207. databuf[2] = Current;
  208. databuf[3] = (UBYTE)(Speed & 0xFF);
  209. databuf[4] = (UBYTE)((Speed & 0xFF00)>>8);
  210. if(curSpeed_state.state == ClzLoop)
  211. {
  212. databuf[5] = 0x10;
  213. }
  214. else
  215. {
  216. databuf[5] = 0x00;
  217. }
  218. if(1 == MC_ErrorCode.ERROR_Bit.Protect_OverCurrent)
  219. {
  220. databuf[5] |= 0x01;//电流异常
  221. }
  222. else if(1 == MC_ErrorCode.ERROR_Bit.Fault_Throttle)
  223. {
  224. databuf[5] |= 0x02;//转把异常
  225. }
  226. else if(1 == MC_ErrorCode.ERROR_Bit.Fault_PhaseLine)
  227. {
  228. databuf[5] |= 0x03;//电机缺相
  229. }
  230. else if(1 == MC_ErrorCode.ERROR_Bit.Fault_HallSensor)
  231. {
  232. databuf[5] |= 0x04;//电机霍尔异常
  233. }
  234. else if(1 == alm_unBikeCode.bit.Brake)
  235. {
  236. databuf[5] |= 0x05;//刹车异常
  237. }
  238. else if(1 == MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage)
  239. {
  240. databuf[5] |= 0x06;//欠压
  241. }
  242. else if(1 == MC_ErrorCode.ERROR_Bit.Protect_LockRotor)
  243. {
  244. databuf[5] |= 0x07;//电机堵转
  245. }
  246. /*else if(0)
  247. {
  248. databuf[5] |= 0x08;//通讯故障
  249. }*/
  250. }
  251. else if(Cmd == 0x2D)
  252. {
  253. databuf[2] = MC_VerInfo.FW_Version[1] - 48;
  254. databuf[3] = MC_VerInfo.FW_Version[3] - 48;
  255. databuf[4] = MC_VerInfo.FW_Version[5] - 48;
  256. databuf[5] = 0;
  257. }
  258. for(i=1; i<(datalen-4); i++)
  259. {
  260. checksum = (UWORD)(databuf[i] + checksum);
  261. }
  262. databuf[datalen-4] = (UBYTE)(checksum & 0xFF);
  263. databuf[datalen-3] = (UBYTE)(checksum >> 8);
  264. databuf[datalen-2] = 0x0D;
  265. databuf[datalen-1] = 0x0A;
  266. for(i=0; i<datalen; i++)
  267. {
  268. UART_PutChar(ptUartTx, databuf[i]);
  269. }
  270. }
  271. /***************************************************************
  272. Function:
  273. Description: Get Uart Data following the protocol
  274. Call by:
  275. Input Variables: N/A
  276. Output/Return Variables: N/A
  277. Subroutine Call: N/A;
  278. Reference: N/A
  279. ****************************************************************/
  280. void Lanfeng_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  281. {
  282. UBYTE Cmd;
  283. static UBYTE Data[255];
  284. UWORD i;
  285. UWORD CheckSumResult=0, CheckSumData=0;
  286. UBYTE FrameBegin, FrameEnd1, FrameEnd2;
  287. if(dataCount == 9)
  288. {
  289. //帧头
  290. FrameBegin = buf[0];
  291. if(FrameBegin == 0x3A)
  292. {
  293. //命令字
  294. Cmd = buf[1];
  295. if((Cmd == 0x2C) || (Cmd == 0x2D)) //命令字
  296. {
  297. for(i=0; i<dataCount; i++)//数据
  298. {
  299. Data[i] = buf[i];
  300. }
  301. CheckSumData = (UWORD)(Data[dataCount-4] + (Data[dataCount-3] << 8));
  302. for(i = 1; i<(dataCount-4); i++)
  303. {
  304. CheckSumResult = (UWORD)(Data[i] + CheckSumResult);
  305. }
  306. FrameEnd1 = Data[dataCount-2];
  307. FrameEnd2 = Data[dataCount-1];
  308. if((CheckSumData == CheckSumResult) && (FrameEnd1 == 0x0D) && (FrameEnd2 == 0x0A))// 校验和帧尾正常
  309. {
  310. Lanfeng_USART_DataProcess(ptUartTx, Cmd, &Data[0]);
  311. }
  312. }
  313. }
  314. }
  315. }
  316. /************************ (C) END OF FILE *********************************/