uart_gainianzhihui.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. UWORD uwVolt= cp_stMotorPara.swRUdcV;
  96. if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((GNZH_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
  97. {
  98. MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((GNZH_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
  99. MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
  100. cp_stFlg.ParaSaveEEFlg = TRUE;
  101. cp_stFlg.ParaUpdateFlg = TRUE;
  102. cp_stFlg.TestParaInfoUpdateFlg = TRUE;
  103. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  104. }
  105. }
  106. /*轮径*/
  107. UWORD tmpdata;
  108. tmpdata = GNZH_stOBC2MCInfo.ucWheelDiameter*8;
  109. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
  110. {
  111. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
  112. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  113. cp_stFlg.ParaSaveEEFlg = TRUE;
  114. cp_stFlg.ParaUpdateFlg = TRUE;
  115. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  116. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  117. }
  118. }
  119. /***************************************************************
  120. Function: SP_USART_DataProcess;
  121. Description: Get OBC GearSt and LightControl information
  122. Call by:
  123. Input Variables: N/A
  124. Output/Return Variables: N/A
  125. Subroutine Call: N/A;
  126. Reference: N/A
  127. ****************************************************************/
  128. void GNZH_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
  129. {
  130. /*数据获取*/
  131. memcpy(&GNZH_stOBC2MCInfo.stCtrlInfo, &Data[1], 9);
  132. GNZH_stOBC2MCInfo.ucSpeedLimiation = Data[2];
  133. GNZH_stOBC2MCInfo.ucCurrentLimiation = Data[3];
  134. GNZH_stOBC2MCInfo.ucWheelDiameter = Data[4];
  135. GNZH_stOBC2MCInfo.ucCuriseEnable = Data[5];
  136. GNZH_stOBC2MCInfo.RS1 = Data[6];
  137. GNZH_stOBC2MCInfo.RS2 = Data[7];
  138. GNZH_stOBC2MCInfo.uwBrakeReg = (UWORD)(Data[8] + (Data[9]<<8));
  139. /*数据更新*/
  140. /*if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5)
  141. {
  142. MC_ControlCode.GearSt = MC_GearSt_SMART;
  143. }
  144. else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 4)
  145. {
  146. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  147. }
  148. else */
  149. if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 8)
  150. {
  151. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  152. }
  153. else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5)
  154. {
  155. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  156. }
  157. else if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 2)
  158. {
  159. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  160. }
  161. else
  162. {
  163. MC_ControlCode.GearSt = MC_GearSt_OFF;
  164. }
  165. if(GNZH_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.Light == 1) //大灯
  166. {
  167. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  168. }
  169. else
  170. {
  171. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  172. }
  173. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  174. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  175. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  176. //GNZH_OBCSetPara();//参数更新
  177. GNZH_SendData_OBC(ptUartTx);//数据回传
  178. }
  179. /***************************************************************
  180. Function: SP_SendData_OBC;
  181. Description: Send Data to OBC
  182. Call by:
  183. Input Variables: N/A
  184. Output/Return Variables: N/A
  185. Subroutine Call: N/A;
  186. Reference: N/A
  187. ****************************************************************/
  188. void GNZH_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
  189. {
  190. UBYTE i = 0;
  191. UBYTE databuf[13] = {0};
  192. UWORD checkxor = 0;
  193. /*更新数据发送结构体*/
  194. if(MC_RunInfo.BusCurrent>25500)
  195. {
  196. GNZH_stMC2OBCInfo.ucCurrent = 255;
  197. }
  198. else
  199. {
  200. GNZH_stMC2OBCInfo.ucCurrent = abs(MC_RunInfo.BusCurrent/100);
  201. }
  202. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  203. {
  204. GNZH_stMC2OBCInfo.uwSpeed = 0;
  205. }
  206. else
  207. {
  208. GNZH_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  209. }
  210. if(bikeCruise.CruiseMode == 1)
  211. {
  212. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Curise = 1;
  213. }
  214. else
  215. {
  216. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Curise = 0;
  217. }
  218. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
  219. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.MotorErr = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
  220. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
  221. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
  222. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Brake = BikeBrake_blGetstate();
  223. //GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.Glide = 0;//滑行
  224. GNZH_stMC2OBCInfo.stMC_StatusInfo.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
  225. /*数据发送*/
  226. databuf[0] = 0xA5;
  227. databuf[1] = GNZH_stMC2OBCInfo.ucCurrent;
  228. databuf[2] = (UBYTE)(GNZH_stMC2OBCInfo.uwSpeed & 0xFF);
  229. databuf[3] = (UBYTE)(GNZH_stMC2OBCInfo.uwSpeed >> 8);
  230. memcpy(&databuf[4],&GNZH_stMC2OBCInfo.stMC_StatusInfo,1);
  231. databuf[5] = 0;
  232. databuf[6] = 0;
  233. databuf[7] = 0;
  234. databuf[8] = 0;
  235. databuf[9] = 0;
  236. for(i=0; i<10; i=i+2)
  237. {
  238. checkxor ^= (UWORD)((databuf[i]<<8) + databuf[i+1]);
  239. }
  240. databuf[10] = (UBYTE)(checkxor & 0xFF);
  241. databuf[11] = (UBYTE)(checkxor >> 8);
  242. databuf[12] = 0xAD;
  243. for(i=0; i<13; i++)
  244. {
  245. UART_PutChar(ptUartTx, databuf[i]);
  246. }
  247. }
  248. /***************************************************************
  249. Function: SP_USART_RxData_Process;
  250. Description: Get Uart Data following the protocol
  251. Call by:
  252. Input Variables: N/A
  253. Output/Return Variables: N/A
  254. Subroutine Call: N/A;
  255. Reference: N/A
  256. ****************************************************************/
  257. void GNZH_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  258. {
  259. static UBYTE Data[255];
  260. //UWORD Cmd, ID;
  261. UWORD i;
  262. UWORD CheckXorResult=0, CheckXorData=0;
  263. if(dataCount == 13)
  264. {
  265. if(buf[0] == 0x55)//帧头
  266. {
  267. if(buf[dataCount-1] == 0x0D)//帧尾
  268. {
  269. for(i=0; i<dataCount; i++)//数据
  270. {
  271. Data[i] = buf[i];
  272. }
  273. CheckXorData = (UWORD)(buf[dataCount-3] + (buf[dataCount-2] << 8));
  274. for(i = 0; i<10; i=i+2)
  275. {
  276. CheckXorResult ^= (UWORD)((Data[i]<<8) + (Data[i+1]));
  277. }
  278. if(CheckXorData == CheckXorResult)// 校验和正常
  279. {
  280. GNZH_USART_DataProcess(ptUartTx, &Data[0]);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. /************************ (C) END OF FILE *********************************/