AppFunc.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2021-11-09 12:08:17
  4. * @Version : V3.0
  5. * @LastEditors : ChenJie
  6. * @LastEditTime : 2021-11-22 11:12:04
  7. * @Description : file content
  8. * @FilePath : \PLAT\project\ec616_0h00\apps\qx_app\src\AppFunc.c
  9. */
  10. #include "AppFunc.h"
  11. #include "numeric.h"
  12. #include "hal_module_adapter.h"
  13. #include "BCUDisp.h"
  14. /**
  15. * @brief : 保护板保护状态解码
  16. * @param {*}
  17. * @return {*}
  18. */
  19. void BmsProtectStateDecode(UINT8 *DischargeProtect, UINT8 *ChargeProtect)
  20. {
  21. UINT8 ret = 0;
  22. //battProtectState = (dataPtr[(0x03 + BATT_CELL_VOL_NUM) * 2 + 0] << 24) | (dataPtr[(0x04 + BATT_CELL_VOL_NUM) * 2 + 0] << 16) | (dataPtr[(0x04 + BATT_CELL_VOL_NUM) * 2 + 1] << 8) | (dataPtr[(0x05 + BATT_CELL_VOL_NUM) * 2 + 1]);
  23. if (battProtectState == 0x00)
  24. {
  25. *DischargeProtect = 0;
  26. *ChargeProtect = 0;
  27. return;
  28. }
  29. ret = getbit(battProtectState, 0);
  30. if (ret) //放电短路保护--0
  31. {
  32. *DischargeProtect = (*DischargeProtect) + 1;
  33. //printf("protect[%d]\n", __LINE__);
  34. }
  35. ret = getbit(battProtectState, 1);
  36. if (ret) //放电过流保护--1
  37. {
  38. *DischargeProtect = (*DischargeProtect) + 1;
  39. //printf("protect[%d]\n", __LINE__);
  40. }
  41. ret = getbit(battProtectState, 2);
  42. if (ret) //充电过流保护--2
  43. {
  44. *ChargeProtect = (*ChargeProtect) + 1;
  45. //printf("protect[%d]\n", __LINE__);
  46. }
  47. ret = getbit(battProtectState, 8);
  48. if (ret) //电芯欠压--8
  49. {
  50. *DischargeProtect = (*DischargeProtect) + 1;
  51. //printf("protect[%d]\n", __LINE__);
  52. }
  53. ret = getbit(battProtectState, 9);
  54. if (ret) //总压欠压--9
  55. {
  56. *DischargeProtect = (*DischargeProtect) + 1;
  57. //printf("protect[%d]\n", __LINE__);
  58. }
  59. ret = getbit(battProtectState, 16);
  60. if (ret) //充电高温--16
  61. {
  62. *ChargeProtect = (*ChargeProtect) + 1;
  63. //printf("protect[%d]\n", __LINE__);
  64. }
  65. ret = getbit(battProtectState, 17);
  66. if (ret) //充电低温--17
  67. {
  68. *ChargeProtect = (*ChargeProtect) + 1;
  69. //printf("protect[%d]\n", __LINE__);
  70. }
  71. ret = getbit(battProtectState, 18);
  72. if (ret) //mos高温--18
  73. {
  74. *ChargeProtect = (*ChargeProtect) + 1;
  75. *DischargeProtect = (*DischargeProtect) + 1;
  76. //printf("protect[%d]\n", __LINE__);
  77. }
  78. ret = getbit(battProtectState, 20);
  79. if (ret) //放电高温--20
  80. {
  81. *DischargeProtect = (*DischargeProtect) + 1;
  82. //printf("protect[%d]\n", __LINE__);
  83. }
  84. ret = getbit(battProtectState, 21);
  85. if (ret) //放电低温--21
  86. {
  87. *DischargeProtect = (*DischargeProtect) + 1;
  88. //printf("protect[%d]\n", __LINE__);
  89. }
  90. ret = getbit(battProtectState, 24);
  91. if (ret) //电芯过压--24
  92. {
  93. *ChargeProtect = (*ChargeProtect) + 1;
  94. //printf("protect[%d]\n", __LINE__);
  95. }
  96. ret = getbit(battProtectState, 25);
  97. if (ret) //总压过压--25
  98. {
  99. *ChargeProtect = (*ChargeProtect) + 1;
  100. //printf("protect[%d]\n", __LINE__);
  101. }
  102. ret = getbit(battProtectState, 28);
  103. if (ret) //充满保护--28
  104. {
  105. //*ChargeProtect = (*ChargeProtect) + 1;
  106. //printf("protect[%d]\n", __LINE__);
  107. }
  108. }
  109. /**
  110. * @brief : 故障代码处理函数,将故障代码进行排序并输出
  111. * @param {*}
  112. * @return {*}
  113. */
  114. void ErrorNumHandleFunc(void)
  115. {
  116. UINT16 ErrorArrayBuffer[20] = {0};
  117. MEMCPY(ErrorArrayBuffer, sfmv_idx_fltCode, sfmd_num_fltNum * 2);
  118. BOOL isSorted;
  119. UINT16 temp = 0;
  120. for (UINT8 i = 0; i < sfmd_num_fltNum; i++)
  121. {
  122. isSorted = TRUE; //假设剩下的元素已经排序好了
  123. for (UINT8 j = 0; j < sfmd_num_fltNum - i; j++)
  124. {
  125. if (ErrorArrayBuffer[j] < ErrorArrayBuffer[j + 1])
  126. {
  127. temp = ErrorArrayBuffer[j];
  128. ErrorArrayBuffer[j] = ErrorArrayBuffer[j + 1];
  129. ErrorArrayBuffer[j + 1] = temp;
  130. isSorted = FALSE; //一旦需要交换数组元素,就说明剩下的元素没有排序好
  131. }
  132. }
  133. if (isSorted)
  134. break; //如果没有发生交换,说明剩下的元素已经排序好了
  135. }
  136. if (osOK == osMutexAcquire(Error_Mutex, 1))
  137. {
  138. for (UINT8 index = 0; index < 20; index++)
  139. {
  140. if (ErrorArrayBuffer[index] != 0)
  141. {
  142. PutErrorNum((UINT16 *)ErrorNum, ErrorNumLength, (ErrorArrayBuffer[index] % 10000U));
  143. }
  144. }
  145. }
  146. osMutexRelease(Error_Mutex);
  147. }
  148. /**
  149. * @brief : 租期到期日期计算函数
  150. * @param {UINT8} *StartTimeArray
  151. * @param {UINT16} rentalDays
  152. * @param {UINT8} *ExpiryTimeArray
  153. * @return {*}
  154. */
  155. void ExpiryTimeCal(UINT8 *StartTimeArray, UINT16 rentalDays, UINT8 *ExpiryTimeArray)
  156. {
  157. UINT8 lastday = 0;
  158. UINT8 year = 0, month = 0, hour = 0, minute = 0, second = 0;
  159. UINT16 day = 0;
  160. year = *(StartTimeArray + 0);
  161. month = *(StartTimeArray + 1);
  162. day = *(StartTimeArray + 2) + rentalDays;
  163. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  164. {
  165. lastday = 31;
  166. }
  167. else if (month == 4 || month == 6 || month == 9 || month == 11)
  168. {
  169. lastday = 30;
  170. }
  171. else
  172. {
  173. if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
  174. lastday = 29;
  175. else
  176. lastday = 28;
  177. }
  178. while (day > lastday)
  179. {
  180. day -= lastday;
  181. month += 1;
  182. if (month > 12)
  183. {
  184. month -= 12;
  185. year += 1;
  186. }
  187. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  188. {
  189. lastday = 31;
  190. }
  191. else if (month == 4 || month == 6 || month == 9 || month == 11)
  192. {
  193. lastday = 30;
  194. }
  195. else
  196. {
  197. if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
  198. lastday = 29;
  199. else
  200. lastday = 28;
  201. }
  202. }
  203. *(ExpiryTimeArray + 0) = year;
  204. *(ExpiryTimeArray + 1) = month;
  205. *(ExpiryTimeArray + 2) = day;
  206. *(ExpiryTimeArray + 3) = *(StartTimeArray + 3);
  207. *(ExpiryTimeArray + 4) = *(StartTimeArray + 4);
  208. *(ExpiryTimeArray + 5) = *(StartTimeArray + 5);
  209. return;
  210. }
  211. /**
  212. * @brief :租期到期判断函数
  213. * @param {*}
  214. * @return {*}
  215. */
  216. INT8 rentalEndDetectFunc(void)
  217. {
  218. OsaUtcTimeTValue TimeStracture;
  219. UTC8TimeType UTC8Time, ExpiryTime;
  220. UINT16 year;
  221. UINT8 month, day, hour, minute, sec;
  222. appGetSystemTimeUtcSync(&TimeStracture);
  223. year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16;
  224. month = (TimeStracture.UTCtimer1 & 0xff00) >> 8;
  225. day = TimeStracture.UTCtimer1 & 0xff;
  226. hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24;
  227. minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16;
  228. sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8;
  229. UTCToBeijing((UTC8TimeType *)&UTC8Time, year, month, day, hour, minute, sec);
  230. if (UTC8Time.year > 2000)
  231. {
  232. #ifdef USING_PRINTF1
  233. printf("retal Jug:%d %d %d %d,%d %d %d %d\n", UTC8Time.year, UTC8Time.month, UTC8Time.day, UTC8Time.hour, AppDataInfo.ExpiryTimeArray[0], AppDataInfo.ExpiryTimeArray[1], AppDataInfo.ExpiryTimeArray[2], AppDataInfo.ExpiryTimeArray[3]);
  234. #endif
  235. if (((UTC8Time.year - 0x07D0) & 0xFF) == AppDataInfo.ExpiryTimeArray[0]) //年份相等判定月份
  236. {
  237. if (UTC8Time.month == AppDataInfo.ExpiryTimeArray[1]) //月份相等判定日期
  238. {
  239. if (UTC8Time.day == AppDataInfo.ExpiryTimeArray[2]) //日期相等判定小时
  240. {
  241. // if(UTC8Time.hour<=AppDataInfo.ExpiryTimeArray[3])//小时相等不锁定,小时超出锁定
  242. // {
  243. // return 0;
  244. // }
  245. // else
  246. // {
  247. // return 1;
  248. // }
  249. return 0;
  250. }
  251. else if (UTC8Time.day > AppDataInfo.ExpiryTimeArray[2])
  252. {
  253. return 1;
  254. }
  255. else if (UTC8Time.day < AppDataInfo.ExpiryTimeArray[2])
  256. {
  257. return 0;
  258. }
  259. }
  260. else if (UTC8Time.month > AppDataInfo.ExpiryTimeArray[1]) //实际月份大于终止月份
  261. {
  262. return 1;
  263. }
  264. else if (UTC8Time.month < AppDataInfo.ExpiryTimeArray[1]) //实际月份小于终止月份
  265. {
  266. return 0;
  267. }
  268. }
  269. else if (((UTC8Time.year - 0x07D0) & 0xFF) > AppDataInfo.ExpiryTimeArray[0]) //实际年份大于终止年份
  270. {
  271. return 1;
  272. }
  273. else if (((UTC8Time.year - 0x07D0) & 0xFF) < AppDataInfo.ExpiryTimeArray[0]) //年比较,实际年份小于终止年份
  274. {
  275. return 0;
  276. }
  277. }
  278. else
  279. {
  280. return -1; //没有获取到时间返回
  281. }
  282. }
  283. /**
  284. * @brief :电池是否业务锁定判断函数(锁定放电)
  285. * @param {*}
  286. * @return {*}
  287. */
  288. void BattLockFunc(void)
  289. {
  290. if (AppDataInfo.RentalType == 0) //测试模式
  291. {
  292. ;
  293. }
  294. else if (AppDataInfo.RentalType == 1) //零售模式
  295. {
  296. if (AppDataInfo.RentalLock == TRUE)
  297. {
  298. AppDataInfo.UserLock = FALSE;
  299. AppDataInfo.RentalLock = FALSE;
  300. AppDataInfo.appDataModify = TRUE;
  301. }
  302. }
  303. else if (AppDataInfo.RentalType == 2) // 租赁模式
  304. {
  305. INT8 ret = -1;
  306. ret = rentalEndDetectFunc();
  307. if (ret == 1 && AppDataInfo.RentalLock == FALSE) //租期判定是否超期函数
  308. {
  309. #ifdef USING_PRINTF
  310. printf("retal lock,%d\n", ret);
  311. #endif
  312. AppDataInfo.RentalLock = TRUE;
  313. AppDataInfo.appDataModify = TRUE;
  314. }
  315. else if (ret == 0 && AppDataInfo.RentalLock == TRUE)
  316. {
  317. #ifdef USING_PRINTF
  318. printf("retal unlock,%d\n", ret);
  319. #endif
  320. AppDataInfo.RentalLock = FALSE;
  321. AppDataInfo.appDataModify = TRUE;
  322. AppDataInfo.UserLock = FALSE;
  323. }
  324. }
  325. if (AppDataInfo.BattForceLock == FALSE)
  326. {
  327. if (AppDataInfo.RentalLock == FALSE && AppDataInfo.UserLock == FALSE)
  328. {
  329. AppDataInfo.BattLock = FALSE;
  330. AppDataInfo.appDataModify = TRUE;
  331. }
  332. else if (AppDataInfo.RentalLock == TRUE || AppDataInfo.UserLock == TRUE)
  333. {
  334. AppDataInfo.BattLock = TRUE;
  335. AppDataInfo.appDataModify = TRUE;
  336. }
  337. }
  338. else
  339. {
  340. AppDataInfo.BattLock = TRUE;
  341. AppDataInfo.appDataModify = TRUE;
  342. }
  343. return;
  344. }
  345. /**
  346. * @brief : 电池工作模式延时处理函数
  347. * @param {UINT8} battWorkCurrentState
  348. * @return {*}
  349. */
  350. void BattWorkDelayFunc(UINT8 battWorkCurrentState)
  351. {
  352. static UINT16 workDelayCounter = 0;
  353. if (battWorkCurrentState == BATT_IDLE_SYM && BattWorkStateDelay == BATT_DISCHARGE_SYM) //从放电转静置 10s延时
  354. {
  355. workDelayCounter++;
  356. if (workDelayCounter >= 10)
  357. {
  358. workDelayCounter = 0;
  359. BattWorkStateDelay = battWorkCurrentState;
  360. }
  361. }
  362. else if (battWorkCurrentState == BATT_IDLE_SYM && BattWorkStateDelay == BATT_CHARGE_SYM) //从充电转静置10s延时
  363. {
  364. workDelayCounter++;
  365. if (workDelayCounter >= 10)
  366. {
  367. workDelayCounter = 0;
  368. BattWorkStateDelay = battWorkCurrentState;
  369. }
  370. }
  371. else if (battWorkCurrentState == BATT_DISCHARGE_SYM && BattWorkStateDelay != BATT_DISCHARGE_SYM) //从 充电或者静置 转放电 1s延时
  372. {
  373. workDelayCounter++;
  374. if (workDelayCounter >= 1)
  375. {
  376. workDelayCounter = 0;
  377. BattWorkStateDelay = battWorkCurrentState;
  378. }
  379. }
  380. else if (battWorkCurrentState == BATT_CHARGE_SYM && BattWorkStateDelay != BATT_CHARGE_SYM) //从 放电或者静置 转充电 30s延时
  381. {
  382. workDelayCounter++;
  383. if (workDelayCounter >= 30)
  384. {
  385. workDelayCounter = 0;
  386. BattWorkStateDelay = battWorkCurrentState;
  387. }
  388. }
  389. else
  390. {
  391. BattWorkStateDelay = battWorkCurrentState;
  392. workDelayCounter = 0;
  393. }
  394. return;
  395. }
  396. /**
  397. * @brief : R-T查表函数 (需优化)
  398. * @param {UINT32} R_value
  399. * @return {*}
  400. */
  401. UINT8 LookUpRTtable(UINT32 R_value)
  402. {
  403. UINT8 Temp_Table1[23] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22};
  404. UINT8 Temp_Table2[217] = {23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  405. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
  406. 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
  407. 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
  408. 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
  409. 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,
  410. 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222,
  411. 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239};
  412. UINT32 R_Table1[23] = {202269, 191063, 180554, 170694, 161438, 152746, 144580, 136905, 129687, 122898, 116508,
  413. 110493, 104827, 99488, 94455, 89710, 85233, 81008, 77019, 73252, 69693, 66329, 63148};
  414. UINT16 R_Table2[217] = {
  415. 60140, 57293, 54599, 52049, 49633, 47343, 45174, 43117, 41166, 39315, 37558, 35891, 34307, 32802, 31373,
  416. 30014, 28722, 27493, 26324, 25211, 24152, 23144, 22183, 21268, 20395, 19564, 18771, 18015, 17293, 16604,
  417. 15947, 15319, 14720, 14147, 13600, 13077, 12577, 12098, 11641, 11203, 10784, 10383, 10000, 9632, 9280, 8942,
  418. 8619, 8309, 8012, 7727, 7454, 7191, 6940, 6698, 6466, 6244, 6030, 5825, 5627, 5438, 5255, 5080, 4911, 4749, 4593,
  419. 4443, 4299, 4160, 4026, 3898, 3773, 3654, 3539, 3428, 3321, 3218, 3119, 3023, 2931, 2841, 2755, 2672, 2592, 2515,
  420. 2441, 2369, 2299, 2232, 2167, 2104, 2044, 1985, 1928, 1874, 1821, 1769, 1720, 1672, 1626, 1581, 1537, 1495, 1455,
  421. 1415, 1377, 1340, 1305, 1270, 1236, 1204, 1172, 1142, 1112, 1083, 1056, 1029, 1002, 977, 952, 928, 905, 883, 861,
  422. 839, 819, 799, 779, 760, 742, 724, 707, 690, 674, 658, 642, 627, 613, 599, 585, 571, 558, 546, 533, 521, 509, 498, 487,
  423. 476, 466, 455, 445, 436, 426, 417, 408, 399, 391, 382, 374, 366, 359, 351, 344, 337, 330, 323, 316, 310, 304, 298, 292,
  424. 286, 280, 274, 269, 264, 259, 254, 249, 244, 239, 234, 230, 226, 221, 217, 213, 209, 205, 201, 198, 194, 190, 187, 183,
  425. 180, 177, 174, 171, 168, 165, 162, 159, 156, 153, 151, 148, 145, 143, 141, 138, 136, 133, 131, 129, 127, 125};
  426. if (R_value > R_Table2[0])
  427. {
  428. if (R_value <= R_Table1[22]) //判断是否超出表尾
  429. {
  430. return Temp_Table1[22];
  431. }
  432. else if (R_value >= R_Table1[0]) //判断是否超出表头
  433. {
  434. return Temp_Table1[0];
  435. }
  436. else
  437. {
  438. for (int i = 0; i < 23 - 1; i++)
  439. {
  440. if ((R_value <= R_Table1[i]) && (R_value > R_Table1[i + 1])) //中间判断
  441. {
  442. return Temp_Table1[i];
  443. }
  444. else
  445. {
  446. continue;
  447. }
  448. }
  449. }
  450. } //R-1
  451. else
  452. {
  453. if (R_value <= R_Table2[216]) //判断是否超出表尾
  454. {
  455. return Temp_Table2[216];
  456. }
  457. else if (R_value >= R_Table2[0]) //判断是否超出表头
  458. {
  459. return Temp_Table2[0];
  460. }
  461. else
  462. {
  463. for (int i = 0; i < 217 - 1; i++)
  464. {
  465. if ((R_value < R_Table2[i]) && (R_value >= R_Table2[i + 1])) //中间判断
  466. {
  467. return Temp_Table2[i + 1];
  468. }
  469. else
  470. {
  471. continue;
  472. }
  473. }
  474. }
  475. } //R-2
  476. }
  477. /**
  478. * @brief : tcp校验函数
  479. * @param {UINT8} *data
  480. * @param {UINT16} length
  481. * @return {*}
  482. */
  483. UINT8 bcc_chk(UINT8 *data, UINT16 length)
  484. {
  485. UINT8 bcc_chk_return = 0x00;
  486. UINT16 count = 0;
  487. while (count < length)
  488. {
  489. bcc_chk_return ^= data[count];
  490. count++;
  491. }
  492. return bcc_chk_return;
  493. }
  494. /**
  495. * @brief : 加热判断函数,根据当前状态判断是否需要改变状态
  496. * @param {UINT8} *heaterSwitch
  497. * @param {UINT8} HeatForceControl
  498. * @return {*}
  499. */
  500. BOOL BattHeaterSwitch(UINT8 *heaterSwitch, UINT8 HeatForceControl)
  501. {
  502. BOOL isNeedtoSwitch = FALSE;
  503. UINT8 currentSwitchState = 0;
  504. currentSwitchState = battHeatEnableState & 0x01;
  505. if (currentSwitchState == 0) //当前状态为关闭,判断是否应该开启
  506. {
  507. if (tmsd_st_heatAct == 1)
  508. {
  509. *heaterSwitch = 1;
  510. isNeedtoSwitch = true;
  511. }
  512. }
  513. else //当前状态为开启,判断是否应该关闭
  514. {
  515. if (tmsd_st_heatAct == 0)
  516. {
  517. *heaterSwitch = 0;
  518. isNeedtoSwitch = true;
  519. }
  520. }
  521. return isNeedtoSwitch;
  522. }
  523. /**
  524. * @brief : 蜂鸣器控制函数
  525. * @param {UINT8} BuzzerPeriod
  526. * @param {float} DutyRatio
  527. * @return {*}
  528. */
  529. void relayControlFunc(UINT16 BuzzerPeriod, float DutyRatio)
  530. {
  531. static UINT16 BuzzerTimer = 0;
  532. if (BuzzerTimer < (UINT16)(BuzzerPeriod * DutyRatio))
  533. {
  534. relayControl(TRUE);
  535. }
  536. else
  537. {
  538. relayControl(FALSE);
  539. }
  540. if (BuzzerTimer > BuzzerPeriod)
  541. {
  542. BuzzerTimer = 0;
  543. }
  544. else
  545. {
  546. BuzzerTimer = BuzzerTimer + 100;
  547. }
  548. }
  549. /**
  550. * @brief : LED控制函数,根据不同状态进行LED显示
  551. * @param {*}
  552. * @return {*}
  553. */
  554. void LEDDisplay(void)
  555. {
  556. UINT8 LedBattSoc = 0;
  557. LedBattSoc = socd_pct_bcuSoc / 10;
  558. static UINT16 LightTimer = 0;
  559. static UINT16 ErrorLightTimer = 0;
  560. UINT16 LEDFlashPeriod = 1000; //1000ms
  561. float DutyRatio = 0.5;
  562. if (battWorkState == 2)
  563. {
  564. LEDFlashPeriod = 100 * (LedBattSoc / 10 + 1);
  565. }
  566. else
  567. {
  568. LEDFlashPeriod = 1000;
  569. }
  570. if (AppDataInfo.BattLock == TRUE) //电池整处于锁定状态
  571. {
  572. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  573. {
  574. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  575. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  576. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  577. NetSocDisplay(LED_SOC_3, LED_TURN_ON);
  578. FaultDisplay(LED_TURN_ON);
  579. }
  580. else
  581. {
  582. NetSocDisplay(LED_SOC_0, LED_TURN_OFF);
  583. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  584. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  585. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  586. FaultDisplay(LED_TURN_OFF);
  587. }
  588. }
  589. else //电池未处于锁定状态
  590. {
  591. if (UartErrorFlag == 1) //电池未收到UART信息,出现Uart故障
  592. {
  593. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  594. {
  595. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  596. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  597. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  598. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  599. FaultDisplay(LED_TURN_ON);
  600. }
  601. else
  602. {
  603. NetSocDisplay(LED_SOC_0, LED_TURN_OFF);
  604. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  605. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  606. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  607. FaultDisplay(LED_TURN_OFF);
  608. }
  609. }
  610. else if (UartRecvFlag == 1) //电池接收到UART信息,且正确运行
  611. {
  612. if (AppNVMData.EOLState == 0) //未完成下线检测,灭其余灯,闪红灯
  613. {
  614. NetSocDisplay(LED_SOC_0, LED_TURN_OFF);
  615. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  616. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  617. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  618. if (ErrorLightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  619. {
  620. FaultDisplay(LED_TURN_ON);
  621. }
  622. else
  623. {
  624. FaultDisplay(LED_TURN_OFF);
  625. }
  626. }
  627. else
  628. {
  629. if (getbit(sfmd_st_fltAct, 1) == 1) //通过下线检测,有故障亮红灯
  630. {
  631. FaultDisplay(LED_TURN_ON);
  632. }
  633. else
  634. {
  635. FaultDisplay(LED_TURN_OFF);
  636. }
  637. if (battWorkState == 0 || battWorkState == 1) //静置或放电状态
  638. {
  639. if (LedBattSoc <= 10)
  640. {
  641. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  642. {
  643. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  644. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  645. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  646. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  647. }
  648. else
  649. {
  650. NetSocDisplay(LED_SOC_0, LED_TURN_OFF);
  651. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  652. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  653. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  654. }
  655. }
  656. else if (LedBattSoc > 10 && LedBattSoc <= 25)
  657. {
  658. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  659. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  660. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  661. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  662. }
  663. else if (LedBattSoc > 25 && LedBattSoc <= 50)
  664. {
  665. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  666. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  667. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  668. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  669. }
  670. else if (LedBattSoc > 50 && LedBattSoc <= 75)
  671. {
  672. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  673. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  674. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  675. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  676. }
  677. else if (LedBattSoc > 75 && LedBattSoc <= 100)
  678. {
  679. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  680. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  681. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  682. NetSocDisplay(LED_SOC_3, LED_TURN_ON);
  683. }
  684. }
  685. else if (battWorkState == 2)
  686. {
  687. if (LedBattSoc <= 25)
  688. {
  689. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  690. {
  691. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  692. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  693. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  694. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  695. }
  696. else
  697. {
  698. NetSocDisplay(LED_SOC_0, LED_TURN_OFF);
  699. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  700. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  701. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  702. }
  703. }
  704. else if (LedBattSoc > 25 && LedBattSoc <= 50)
  705. {
  706. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  707. {
  708. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  709. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  710. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  711. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  712. }
  713. else
  714. {
  715. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  716. NetSocDisplay(LED_SOC_1, LED_TURN_OFF);
  717. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  718. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  719. }
  720. }
  721. else if (LedBattSoc > 50 && LedBattSoc <= 75)
  722. {
  723. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  724. {
  725. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  726. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  727. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  728. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  729. }
  730. else
  731. {
  732. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  733. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  734. NetSocDisplay(LED_SOC_2, LED_TURN_OFF);
  735. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  736. }
  737. }
  738. else if (LedBattSoc > 75 && LedBattSoc <= 97)
  739. {
  740. if (LightTimer < (UINT16)(LEDFlashPeriod * DutyRatio))
  741. {
  742. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  743. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  744. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  745. NetSocDisplay(LED_SOC_3, LED_TURN_ON);
  746. }
  747. else
  748. {
  749. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  750. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  751. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  752. NetSocDisplay(LED_SOC_3, LED_TURN_OFF);
  753. }
  754. }
  755. else if (LedBattSoc > 97 && LedBattSoc <= 100)
  756. {
  757. NetSocDisplay(LED_SOC_0, LED_TURN_ON);
  758. NetSocDisplay(LED_SOC_1, LED_TURN_ON);
  759. NetSocDisplay(LED_SOC_2, LED_TURN_ON);
  760. NetSocDisplay(LED_SOC_3, LED_TURN_ON);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. if (LightTimer > LEDFlashPeriod)
  767. {
  768. LightTimer = 0;
  769. }
  770. else
  771. {
  772. LightTimer = LightTimer + 100;
  773. }
  774. if (ErrorLightTimer > LEDFlashPeriod)
  775. {
  776. ErrorLightTimer = 0;
  777. }
  778. else
  779. {
  780. ErrorLightTimer = ErrorLightTimer + 100;
  781. }
  782. return;
  783. }
  784. /**
  785. * @brief : 串口数据解码函数
  786. * @param {UINT8} *dataPtr
  787. * @return {*}
  788. */
  789. BOOL uartBattInfoDecode(UINT8 *dataPtr)
  790. {
  791. UINT8 i = 0, temp = 0;
  792. INT8 BattCurrentNegFlag = 1;
  793. UINT8 TEMP_NUM = 0;
  794. UINT16 Batt_current;
  795. UINT8 BATT_CELL_VOL_NUM = 0, BATT_TEMP_NUM = 0;
  796. UINT32 Battsumvoltage = 0;
  797. BATT_CELL_VOL_NUM = (dataPtr[(0x00) * 2] << 8) | (dataPtr[(0x00) * 2 + 1]);
  798. BATT_TEMP_NUM = ((dataPtr[(0x01) * 2] << 8) | (dataPtr[(0x01) * 2 + 1])) - BMS_OTHER_TEMP;
  799. if ((AppDataInfo.BattCellCount != BATT_CELL_VOL_NUM || AppDataInfo.BattTempCount != BATT_TEMP_NUM) && (BATT_CELL_VOL_NUM != 0) && (BATT_TEMP_NUM != 0))
  800. {
  801. AppDataInfo.appDataModify = TRUE;
  802. AppDataInfo.BattCellCount = BATT_CELL_VOL_NUM;
  803. AppDataInfo.BattTempCount = BATT_TEMP_NUM;
  804. }
  805. else
  806. {
  807. BATT_CELL_VOL_NUM = AppDataInfo.BattCellCount;
  808. BATT_TEMP_NUM = AppDataInfo.BattTempCount;
  809. }
  810. #ifdef USING_PRINTF1
  811. printf("BattCellCount:%d,BATT_CELL_VOL_NUM:%d ", AppDataInfo.BattCellCount, BATT_CELL_VOL_NUM);
  812. printf("BattTempCount:%d,BATT_TEMP_NUM:%d ", AppDataInfo.BattTempCount, BATT_TEMP_NUM);
  813. #endif
  814. Batt_current = (dataPtr[(0x02 + BATT_CELL_VOL_NUM) * 2] << 8) | (dataPtr[(0x02 + BATT_CELL_VOL_NUM) * 2 + 1]);
  815. //原始数据:充电为负,放电为正
  816. if (battWorkState == 0x02) //充电过程
  817. {
  818. if (Batt_current > 0x8000) // 数据为负
  819. {
  820. //求补码,结果为负
  821. Batt_current = (UINT16)((UINT16)(~(Batt_current)) + 1);
  822. Batt_current = Batt_current / 10;
  823. BattCurrentNegFlag = -1;
  824. }
  825. else
  826. {
  827. //源码,结果为负
  828. Batt_current = Batt_current / 10;
  829. BattCurrentNegFlag = -1;
  830. }
  831. }
  832. else //放电过程
  833. {
  834. if (Batt_current > 0x8000) // 数据为负
  835. {
  836. //求补码,结果为正
  837. Batt_current = (UINT16)((UINT16)(~(Batt_current)) + 1);
  838. Batt_current = Batt_current / 10;
  839. BattCurrentNegFlag = 1;
  840. }
  841. else
  842. {
  843. //源码,结果为正
  844. Batt_current = Batt_current / 10;
  845. BattCurrentNegFlag = 1;
  846. }
  847. }
  848. battI = Batt_current * BattCurrentNegFlag + 0x2710;
  849. INT16 BattI_Temp = 0;
  850. BattI_Temp = Batt_current * BattCurrentNegFlag;
  851. for (i = 0; i < BATT_CELL_VOL_NUM; i++)
  852. {
  853. battCellU[i] = ((dataPtr[(0x02 + i) * 2] << 8) | dataPtr[(0x02 + i) * 2 + 1]) + (INT16)(cmnm_R_voloffset[i] * BattI_Temp / 10000);
  854. Battsumvoltage = Battsumvoltage + battCellU[i];
  855. }
  856. avrgCellVol = Battsumvoltage / BATT_CELL_VOL_NUM;
  857. battWorkState = (dataPtr[(0x03 + BATT_CELL_VOL_NUM) * 2 + 1]) & 0x03; //电池状态(原始数据),0表示静置,1表示放电,2表示充电
  858. TEMP_NUM = BATT_TEMP_NUM + BMS_OTHER_TEMP;
  859. for (i = 0; i < BATT_TEMP_NUM; i++)
  860. {
  861. battCellTemp[i] = dataPtr[(0x06 + BATT_CELL_VOL_NUM + i) * 2 + 1];
  862. }
  863. MOSTemp = dataPtr[(0x06 + BATT_CELL_VOL_NUM + BATT_TEMP_NUM) * 2 + 1];
  864. packTemp = dataPtr[(0x06 + BATT_CELL_VOL_NUM + BATT_TEMP_NUM + 1) * 2 + 1];
  865. chargerConnectState = (dataPtr[(0x03 + BATT_CELL_VOL_NUM) * 2 + 1] >> 2) & 0x01; //充电器连接状态,0表示未连接,1表示已连接
  866. //bit0 ~ bit31 represent cell0 ~ cell31
  867. battBalanceoInfo = dataPtr[(0x06 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1] | (dataPtr[(0x06 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2] << 8) + (dataPtr[(0x07 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1] << 16) | (dataPtr[(0x07 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2] << 24);
  868. bmsHwVersion = dataPtr[(0x08 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1];
  869. bmsSwVersion = dataPtr[(0x08 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2];
  870. #ifdef USING_PRINTF1
  871. printf("[%d]-%x\n", __LINE__, (dataPtr[(0x09 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1]));
  872. printf("[%d]-%x\n", __LINE__, (dataPtr[(0x1B + BATT_CELL_VOL_NUM + TEMP_NUM) * 2]));
  873. #endif
  874. temp = ((dataPtr[(0x09 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1]) >> 1) & 0x03;
  875. battMOSSwitchState = ((temp & 0x01) << 1) | ((temp & 0x02) >> 1);
  876. if (AppDataInfo.BattLock == TRUE)
  877. {
  878. battMOSSwitchState = battMOSSwitchState | (0x01 << 2);
  879. }
  880. else
  881. {
  882. battMOSSwitchState = battMOSSwitchState | (0x00 << 2);
  883. }
  884. battWarningState = (dataPtr[(0x09 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 0] << 16) | (dataPtr[(0x0A + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 0] << 8) | (dataPtr[(0x0A + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1]);
  885. battProtectState = (dataPtr[(0x03 + BATT_CELL_VOL_NUM) * 2 + 0] << 24) | (dataPtr[(0x04 + BATT_CELL_VOL_NUM) * 2 + 0] << 16) | (dataPtr[(0x04 + BATT_CELL_VOL_NUM) * 2 + 1] << 8) | (dataPtr[(0x05 + BATT_CELL_VOL_NUM) * 2 + 1]);
  886. battSOC = dataPtr[(0x0B + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1];
  887. battSOH = dataPtr[(0x0C + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1];
  888. Battdesigncap = (dataPtr[(0x0E + BATT_CELL_VOL_NUM + TEMP_NUM) * 2]) << 24 | (dataPtr[(0x0E + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1]) << 16 | (dataPtr[(0x0F + BATT_CELL_VOL_NUM + TEMP_NUM) * 2]) << 8 | (dataPtr[(0x0F + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1]);
  889. battPackVol = ((dataPtr[(0x18 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2]) << 8 | (dataPtr[(0x18 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1])) / 10; //uint 100mV
  890. maxCellVol = (dataPtr[(0x19 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2] << 8) | dataPtr[(0x19 + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1];
  891. minCellVol = (dataPtr[(0x1A + BATT_CELL_VOL_NUM + TEMP_NUM) * 2] << 8) | dataPtr[(0x1A + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1];
  892. battHeatEnableState = dataPtr[(0x1C + BATT_CELL_VOL_NUM + TEMP_NUM) * 2 + 1] & 0x01;
  893. maxCellTemp = 0x00;
  894. minCellTemp = 0xFF;
  895. AvgBattTemp = 0;
  896. UINT16 SumBattTemp = 0;
  897. for (i = 0; i < BATT_TEMP_NUM; i++)
  898. {
  899. SumBattTemp = SumBattTemp + battCellTemp[i];
  900. maxCellTemp = max(maxCellTemp, battCellTemp[i]);
  901. minCellTemp = min(minCellTemp, battCellTemp[i]);
  902. }
  903. AvgBattTemp = (SumBattTemp - maxCellTemp - minCellTemp) / (BATT_TEMP_NUM - 2);
  904. nbSwVersion = APPSWVERSION;
  905. nbHwVersion = HWVERSION;
  906. return true;
  907. }
  908. /**
  909. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  910. * @param {UINT16} *ErrorArray
  911. * @param {UINT8} Errorlen
  912. * @return {*}
  913. */
  914. UINT16 GetErrorNum(UINT16 *ErrorArray, UINT8 Errorlen)
  915. {
  916. UINT16 OutNum;
  917. OutNum = *(ErrorArray);
  918. for (UINT8 i = 0; i < Errorlen - 1; i++)
  919. {
  920. *(ErrorArray + i) = *(ErrorArray + i + 1);
  921. if (*(ErrorArray + i + 1) == 0)
  922. break;
  923. }
  924. return OutNum;
  925. }
  926. /**
  927. * @brief : 故障码注入函数,将故障码写入到故障数组中,如果存在则不写入,如果存在则不写入
  928. * @param {UINT16} *ErrorArray
  929. * @param {UINT8} Errorlen
  930. * @param {UINT16} ErrorNum
  931. * @return {*}
  932. */
  933. UINT8 PutErrorNum(UINT16 *ErrorArray, UINT8 Errorlen, UINT16 ErrorNum)
  934. {
  935. for (UINT8 i = 0; i < Errorlen; i++)
  936. {
  937. if (*(ErrorArray + i) == 0)
  938. {
  939. *(ErrorArray + i) = ErrorNum;
  940. return 0;
  941. }
  942. else
  943. {
  944. if (*(ErrorArray + i) == ErrorNum)
  945. {
  946. return 1;
  947. }
  948. else
  949. {
  950. continue;
  951. }
  952. }
  953. }
  954. return 2;
  955. }
  956. /**
  957. * @brief : App运行数据存储函数
  958. * @param {*}
  959. * @return {*}
  960. */
  961. void SaveAppData(void)
  962. {
  963. OSAFILE fp = PNULL;
  964. UINT32 writeCount = 0;
  965. AppDataHeader AppDataHr;
  966. fp = OsaFopen(APP_DATAINFO_FILE_NAME, "wb"); //read & write
  967. if (OsaFseek(fp, 0, SEEK_SET) != 0)
  968. {
  969. #ifdef USING_PRINTF
  970. printf("Seek file failed [%d] \r\n", __LINE__);
  971. #endif
  972. OsaFclose(fp);
  973. return;
  974. }
  975. if (fp == PNULL)
  976. {
  977. #ifdef USING_PRINTF
  978. printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
  979. #endif
  980. return;
  981. }
  982. AppDataHr.fileBodySize = sizeof(AppDataInfo);
  983. AppDataHr.checkSum = OsaCalcCrcValue((UINT8 *)&AppDataInfo, sizeof(AppDataInfo));
  984. writeCount = OsaFwrite(&AppDataHr, sizeof(AppDataHr), 1, fp);
  985. if (writeCount != 1)
  986. {
  987. #ifdef USING_PRINTF
  988. printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
  989. #endif
  990. OsaFclose(fp);
  991. return;
  992. }
  993. AppDataInfo.appDataModify = FALSE;
  994. writeCount = OsaFwrite(&AppDataInfo, sizeof(AppDataInfo), 1, fp);
  995. if (writeCount != 1)
  996. {
  997. #ifdef USING_PRINTF
  998. printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
  999. #endif
  1000. OsaFclose(fp);
  1001. return;
  1002. }
  1003. OsaFclose(fp);
  1004. return;
  1005. }
  1006. /**
  1007. * @brief : bcu数据存储函数
  1008. * @param {*}
  1009. * @return {*}
  1010. */
  1011. void SaveBcuData(void)
  1012. {
  1013. OSAFILE fp = PNULL;
  1014. UINT32 writeCount = 0;
  1015. BcuDataHeader BcuDataHr;
  1016. fp = OsaFopen(BCU_DATAINFO_FILE_NAME, "wb"); //read & write
  1017. if (OsaFseek(fp, 0, SEEK_SET) != 0)
  1018. {
  1019. OsaFclose(fp);
  1020. return;
  1021. }
  1022. if (fp == PNULL)
  1023. {
  1024. return;
  1025. }
  1026. BcuDataHr.fileBodySize = sizeof(BcuDataInfo);
  1027. BcuDataHr.checkSum = OsaCalcCrcValue((UINT8 *)&BcuDataInfo, sizeof(BcuDataInfo));
  1028. writeCount = OsaFwrite(&BcuDataHr, sizeof(BcuDataHr), 1, fp);
  1029. if (writeCount != 1)
  1030. {
  1031. OsaFclose(fp);
  1032. return;
  1033. }
  1034. BcuDataInfo.appDataModify = FALSE;
  1035. writeCount = OsaFwrite(&BcuDataInfo, sizeof(BcuDataInfo), 1, fp);
  1036. if (writeCount != 1)
  1037. {
  1038. OsaFclose(fp);
  1039. return;
  1040. }
  1041. OsaFclose(fp);
  1042. return;
  1043. }
  1044. /**
  1045. * @brief : 运行数据加载函数,将运行数据进行加载,若新增了数据项,则保持前面数据项不动
  1046. * @param {*}
  1047. * @return {*}
  1048. */
  1049. void LoadAppData(void)
  1050. {
  1051. OSAFILE fp = PNULL;
  1052. UINT32 readCount = 0;
  1053. UINT8 crcCheck = 0;
  1054. void *pReadAppConfig = (void *)&AppDataInfo;
  1055. AppDataHeader AppDataHr;
  1056. fp = OsaFopen(APP_DATAINFO_FILE_NAME, "rb"); //read only
  1057. if (fp == PNULL)
  1058. {
  1059. #ifdef USING_PRINTF
  1060. printf(" NVM, can't open NVM: 'qxappData.nvm', use the defult value\n");
  1061. #endif
  1062. setDefaultAppDataInfo();
  1063. SaveAppData();
  1064. return;
  1065. }
  1066. readCount = OsaFread(&AppDataHr, sizeof(AppDataHr), 1, fp);
  1067. UINT8 readtimes = 0;
  1068. while (readCount != 1 && readtimes <= 5)
  1069. {
  1070. readtimes++;
  1071. readCount = OsaFread(&AppDataHr, sizeof(AppDataHr), 1, fp);
  1072. osDelay(10);
  1073. }
  1074. if (readtimes > 5)
  1075. {
  1076. OsaFclose(fp);
  1077. setDefaultAppDataInfo();
  1078. SaveAppData();
  1079. return;
  1080. }
  1081. if (AppDataHr.fileBodySize == sizeof(AppDataInfo)) //结构体数据没变动,可直接读出
  1082. {
  1083. readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
  1084. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1085. readtimes = 0;
  1086. while (readtimes <= 5 && readCount != 1)
  1087. {
  1088. readtimes++;
  1089. readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
  1090. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1091. }
  1092. }
  1093. else
  1094. {
  1095. setDefaultAppDataInfo();
  1096. readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
  1097. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1098. readtimes = 0;
  1099. while (readtimes <= 5 && readCount != 1)
  1100. {
  1101. readtimes++;
  1102. readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
  1103. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1104. }
  1105. SaveAppData();
  1106. #ifdef USING_PRINTF
  1107. printf(" Struct update\n");
  1108. #endif
  1109. }
  1110. OsaFclose(fp);
  1111. return;
  1112. }
  1113. /**
  1114. * @brief : Bcu存储数据加载函数
  1115. * @param {*}
  1116. * @return {*}
  1117. */
  1118. void LoadBcuData(void)
  1119. {
  1120. OSAFILE fp = PNULL;
  1121. UINT32 readCount = 0;
  1122. UINT8 crcCheck = 0;
  1123. void *pReadAppConfig = (void *)&BcuDataInfo;
  1124. BcuDataHeader BcuDataHr;
  1125. fp = OsaFopen(BCU_DATAINFO_FILE_NAME, "rb"); //read only
  1126. if (fp == PNULL)
  1127. {
  1128. #ifdef USING_PRINTF
  1129. printf(" NVM, can't open NVM: 'qxappData.nvm', use the defult value\n");
  1130. #endif
  1131. setDefaultBcuDataInfo();
  1132. SaveBcuData();
  1133. return;
  1134. }
  1135. readCount = OsaFread(&BcuDataHr, sizeof(BcuDataHr), 1, fp);
  1136. UINT8 readtimes = 0;
  1137. while (readCount != 1 && readtimes <= 5)
  1138. {
  1139. readtimes++;
  1140. readCount = OsaFread(&BcuDataHr, sizeof(BcuDataHr), 1, fp);
  1141. osDelay(10);
  1142. }
  1143. if (readtimes > 5)
  1144. {
  1145. OsaFclose(fp);
  1146. setDefaultBcuDataInfo();
  1147. SaveBcuData();
  1148. return;
  1149. }
  1150. if (BcuDataHr.fileBodySize == sizeof(BcuDataInfo)) //结构体数据没变动,可直接读出
  1151. {
  1152. readCount = OsaFread(pReadAppConfig, BcuDataHr.fileBodySize, 1, fp);
  1153. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1154. readtimes = 0;
  1155. while (readtimes <= 5 && readCount != 1)
  1156. {
  1157. readtimes++;
  1158. readCount = OsaFread(pReadAppConfig, BcuDataHr.fileBodySize, 1, fp);
  1159. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1160. }
  1161. }
  1162. else
  1163. {
  1164. setDefaultBcuDataInfo();
  1165. readCount = OsaFread(pReadAppConfig, BcuDataHr.fileBodySize, 1, fp);
  1166. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1167. readtimes = 0;
  1168. while (readtimes <= 5 && readCount != 1)
  1169. {
  1170. readtimes++;
  1171. readCount = OsaFread(pReadAppConfig, BcuDataHr.fileBodySize, 1, fp);
  1172. //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1173. }
  1174. SaveBcuData();
  1175. #ifdef USING_PRINTF
  1176. printf(" Struct update\n");
  1177. #endif
  1178. }
  1179. OsaFclose(fp);
  1180. return;
  1181. }
  1182. /**
  1183. * @brief : App配置文件保存函数
  1184. * @param {*}
  1185. * @return {*}
  1186. */
  1187. void SaveAppConfig(void)
  1188. {
  1189. OSAFILE fp = PNULL;
  1190. UINT32 writeCount = 0;
  1191. AppConfigDataHeader AppConfigHr; //4 bytes
  1192. /*
  1193. * open the NVM file
  1194. */
  1195. AppNVMData.appDataModify = FALSE;
  1196. fp = OsaFopen(APP_CONFIG_FILE_NAME, "wb"); //read & write
  1197. if (OsaFseek(fp, 0, SEEK_SET) != 0)
  1198. {
  1199. #ifdef USING_PRINTF
  1200. printf("Seek file failed [%d] \r\n", __LINE__);
  1201. #endif
  1202. OsaFclose(fp);
  1203. return;
  1204. }
  1205. if (fp == PNULL)
  1206. {
  1207. #ifdef USING_PRINTF
  1208. printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
  1209. #endif
  1210. return;
  1211. }
  1212. /*
  1213. * write the header
  1214. */
  1215. AppConfigHr.fileBodySize = sizeof(AppNVMData);
  1216. AppConfigHr.checkSum = OsaCalcCrcValue((UINT8 *)&AppNVMData, sizeof(AppNVMData));
  1217. writeCount = OsaFwrite(&AppConfigHr, sizeof(AppConfigHr), 1, fp);
  1218. if (writeCount != 1)
  1219. {
  1220. #ifdef USING_PRINTF
  1221. printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
  1222. #endif
  1223. OsaFclose(fp);
  1224. return;
  1225. }
  1226. /*
  1227. * write the file body
  1228. */
  1229. writeCount = OsaFwrite(&AppNVMData, sizeof(AppNVMData), 1, fp);
  1230. if (writeCount != 1)
  1231. {
  1232. #ifdef USING_PRINTF
  1233. printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
  1234. #endif
  1235. }
  1236. OsaFclose(fp);
  1237. return;
  1238. }
  1239. /**
  1240. * @brief : App配置文件加载函数
  1241. * @param {*}
  1242. * @return {*}
  1243. */
  1244. void LoadAppConfig(void)
  1245. {
  1246. OSAFILE fp = PNULL;
  1247. UINT32 readCount = 0;
  1248. AppConfigDataHeader AppConfigHr;
  1249. UINT8 crcCheck = 0;
  1250. void *pReadAppConfig = (void *)&AppNVMData;
  1251. /*
  1252. * open the NVM file
  1253. */
  1254. fp = OsaFopen(APP_CONFIG_FILE_NAME, "rb"); //read only
  1255. if (fp == PNULL)
  1256. {
  1257. #ifdef USING_PRINTF
  1258. printf(" NVM, can't open NVM: 'qxappConfig.nvm', use the defult value\n");
  1259. #endif
  1260. setDefaultAppConfigData();
  1261. SaveAppConfig();
  1262. return;
  1263. }
  1264. /*
  1265. * read the file header
  1266. */
  1267. readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHr), 1, fp);
  1268. UINT8 readtimes = 0;
  1269. while (readCount != 1 && readtimes <= 5)
  1270. {
  1271. #ifdef USING_PRINTF
  1272. printf("NVM: 'qxappconfig.nvm', can't read header, return: %d, use the defult value \n");
  1273. #endif
  1274. readtimes++;
  1275. readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHr), 1, fp);
  1276. osDelay(10);
  1277. }
  1278. if (readtimes > 5)
  1279. {
  1280. OsaFclose(fp);
  1281. setDefaultAppConfigData();
  1282. SaveAppConfig();
  1283. return;
  1284. }
  1285. if (AppConfigHr.fileBodySize != sizeof(AppNVMData)) //file version is the same, but NVM file size not right
  1286. {
  1287. #ifdef USING_PRINTF
  1288. printf("MW NVM: 'midwareconfig.nvm', file body size not right: (%u/%u), use the defult value",
  1289. AppConfigHr.fileBodySize, sizeof(AppNVMData));
  1290. #endif
  1291. OsaFclose(fp);
  1292. setDefaultAppConfigData();
  1293. SaveAppConfig();
  1294. return;
  1295. }
  1296. /*
  1297. * read the file body
  1298. */
  1299. readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
  1300. crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1301. readtimes = 0;
  1302. //while (readtimes<=5 && crcCheck != AppConfigHr.checkSum)
  1303. while (readtimes <= 5 && readCount != 1 && crcCheck != AppConfigHr.checkSum)
  1304. {
  1305. readtimes++;
  1306. readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
  1307. crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
  1308. }
  1309. if (readtimes > 5)
  1310. {
  1311. #ifdef USING_PRINTF
  1312. printf(" NVM: 'qxappconfig.nvm', can't read body, or body not right, (%u/%u), use the defult value\n ",
  1313. crcCheck, AppConfigHr.checkSum);
  1314. #endif
  1315. OsaFclose(fp);
  1316. setDefaultAppConfigData();
  1317. SaveAppConfig();
  1318. return;
  1319. }
  1320. OsaFclose(fp);
  1321. return;
  1322. }
  1323. /**
  1324. * @brief : App配置文件初始化函数
  1325. * @param {*}
  1326. * @return {*}
  1327. */
  1328. static void setDefaultAppConfigData(void)
  1329. {
  1330. UINT8 i = 0;
  1331. memset(&AppNVMData, 0x00, sizeof(AppConfigDataType));
  1332. AppNVMData.appDataModify = TRUE; //数据更改标志位
  1333. memcpy(AppNVMData.battSN, DEFAULT_SN, BATT_SN_LEN);
  1334. AppNVMData.isBattLocked = 0;
  1335. AppNVMData.reserved1 = 0;
  1336. AppNVMData.reserved2 = 0;
  1337. AppNVMData.reserved3 = 0;
  1338. AppNVMData.reserved4 = 0;
  1339. AppNVMData.reserved5 = 0;
  1340. AppNVMData.reserved6 = 0;
  1341. AppNVMData.reserved7 = 0;
  1342. AppNVMData.reserved8 = 0;
  1343. AppNVMData.EOLState = EOLSTATE;
  1344. return;
  1345. }
  1346. /**
  1347. * @brief : App数据初始化函数
  1348. * @param {*}
  1349. * @return {*}
  1350. */
  1351. static void setDefaultAppDataInfo(void)
  1352. {
  1353. memset(&AppDataInfo, 0x00, sizeof(AppDataInfo));
  1354. AppDataInfo.appDataModify = false;
  1355. AppDataInfo.BattCellCount = 17;
  1356. AppDataInfo.BattTempCount = 5;
  1357. AppDataInfo.BattInfoSendFreqHigh = 10;
  1358. AppDataInfo.BattInfoSendFreqNomal = 30;
  1359. AppDataInfo.BattInfoSendFreqLow = 60;
  1360. AppDataInfo.PosInfoSendFreqHigh = 10;
  1361. AppDataInfo.PosInfoSendFreqNormal = 30;
  1362. AppDataInfo.PosInfoSendFreqLow = 60;
  1363. AppDataInfo.AccMileage = 0;
  1364. AppDataInfo.SysReStartCount = 0;
  1365. AppDataInfo.RelayControl = 0;
  1366. AppDataInfo.BattForceLock = 0;
  1367. AppDataInfo.BattLock = 0;
  1368. AppDataInfo.BattStolenFlag = 0;
  1369. AppDataInfo.UserLock = 0; // 用户锁锁定
  1370. AppDataInfo.RentalLock = 0; //租期锁锁定
  1371. AppDataInfo.RentalType = 0; //租期类型
  1372. memset(AppDataInfo.ExpiryTimeArray, 0x00, 6); //超期时间
  1373. AppDataInfo.ErrorMsg = 0;
  1374. return;
  1375. }
  1376. /**
  1377. * @brief : BCU存储量初始化函数
  1378. * @param {*}
  1379. * @return {*}
  1380. */
  1381. static void setDefaultBcuDataInfo(void)
  1382. {
  1383. memset(&BcuDataInfo, 0x00, sizeof(BcuDataInfo));
  1384. BcuDataInfo.appDataModify = false;
  1385. memset(BcuDataInfo.blcv_Q_totalCpE, 0x00, cmnc_num_cellUNumMax * sizeof(UINT32));
  1386. memset(BcuDataInfo.blcv_Q_reqCpE, 0x00, cmnc_num_cellUNumMax * sizeof(UINT16));
  1387. BcuDataInfo.socd_pct_bcuSocE = 1200;
  1388. BcuDataInfo.socd_pct_battSocE = 1200;
  1389. BcuDataInfo.sohd_tm_chrgStartStatE = 0;
  1390. BcuDataInfo.sohd_flg_chrgEndE = 0;
  1391. memset(BcuDataInfo.sohv_Q_cellCapArrE, 0, cmnc_num_cellUNumMax * sizeof(UINT16));
  1392. memset(BcuDataInfo.sohv_V_chrgStartStatE, 0, cmnc_num_cellUNumMax * sizeof(UINT16));
  1393. BcuDataInfo.sohd_Q_chrgE = 0;
  1394. memset(BcuDataInfo.sohv_Q_packCapArrE, 0, 10 * sizeof(UINT16));
  1395. memset(BcuDataInfo.iscv_Q_remainCpEE, 0, cmnc_num_cellUNumMax * sizeof(UINT16));
  1396. BcuDataInfo.iscd_tm_totalEE = 0;
  1397. BcuDataInfo.sfmd_flg_iscFltEE = 0;
  1398. BcuDataInfo.sfmd_flg_mainCirClosFltE = 0;
  1399. BcuDataInfo.sfmd_flg_heatCirClosFltE = 0;
  1400. BcuDataInfo.sfmd_flg_heatCirOpenFltE = 0;
  1401. BcuDataInfo.sfmd_flg_heatRunFltE = 0;
  1402. return;
  1403. }
  1404. /**
  1405. * @brief : 认证加密函数,将明文加密为译文
  1406. * @param {UINT16} plainText
  1407. * @return {*}
  1408. */
  1409. UINT16 encryptionAlgorithm(UINT16 plainText)
  1410. {
  1411. UINT16 cipherText = 1;
  1412. UINT16 privateKeyE = 37507;
  1413. UINT16 privateKeyN = 10961;
  1414. plainText = plainText % privateKeyN;
  1415. while (privateKeyE > 0)
  1416. {
  1417. if (privateKeyE % 2 == 1)
  1418. {
  1419. cipherText = (cipherText * plainText) % privateKeyN;
  1420. }
  1421. privateKeyE = privateKeyE / 2;
  1422. plainText = (plainText * plainText) % privateKeyN;
  1423. }
  1424. return cipherText;
  1425. }
  1426. /**
  1427. * @brief : 认证解密函数,将译文解密为明文
  1428. * @param {UINT16} cipherText
  1429. * @return {*}
  1430. */
  1431. // UINT8 decryptionAlgorithm (UINT16 cipherText)
  1432. // {
  1433. // UINT16 plainText = 1;
  1434. // UINT16 publicKeyD = 43;
  1435. // UINT16 publicKeyN = 10961;
  1436. // cipherText = cipherText % publicKeyN;
  1437. // while(publicKeyD >0)
  1438. // {
  1439. // if(publicKeyD % 2 ==1)
  1440. // {
  1441. // plainText = plainText * cipherText % publicKeyN;
  1442. // }
  1443. // publicKeyD = publicKeyD/2;
  1444. // cipherText = (cipherText * cipherText) % publicKeyN;
  1445. // }
  1446. // return (UINT8)plainText;
  1447. // }
  1448. /**
  1449. * @brief : Fota升级处理函数,将接收的数据进行校验,搬运至升级区域,并进行应答
  1450. * @param {UINT8} *DataPtr
  1451. * @param {INT32} connectId
  1452. * @return {*}
  1453. */
  1454. void Fota_Func(UINT8 *DataPtr, INT32 connectId)
  1455. {
  1456. Fota_Type Fota_S;
  1457. UINT8 Fota_Answer[43];
  1458. UINT8 Fota_Cmd;
  1459. INT8 ret;
  1460. UINT8 *Data_Read_Buffer = PNULL;
  1461. UINT8 Data_Read_Crc;
  1462. if (*(DataPtr + 30) == 0x01)
  1463. {
  1464. Fota_S.Fota_Flash_Addres = FLASH_FOTA_REGION_START;
  1465. Fota_Cmd = *(DataPtr + 31);
  1466. Fota_Answer[0] = TCP_START_SYM1;
  1467. Fota_Answer[1] = TCP_START_SYM2;
  1468. Fota_Answer[2] = TCP_CONCMD_SYM;
  1469. switch (Fota_Cmd)
  1470. {
  1471. case 0x01:
  1472. {
  1473. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  1474. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  1475. if (Fota_S.Fota_All_Data_Len >= (FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START))
  1476. {
  1477. Fota_Answer[3] = 0x02;
  1478. }
  1479. else
  1480. {
  1481. Fota_Answer[3] = 0x01;
  1482. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres, (FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START)); //512k-32k = 480k -> 0x75300 0x78000
  1483. }
  1484. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1485. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1486. Fota_Answer[22] = 0x00;
  1487. Fota_Answer[23] = 0x12;
  1488. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1489. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1490. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1491. break;
  1492. }
  1493. case 0x02:
  1494. {
  1495. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  1496. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  1497. Fota_S.Fota_Recv_Data_Len = *(DataPtr + 41);
  1498. memset(Fota_S.Fota_Recv_Data, 0x00, 100);
  1499. memcpy(Fota_S.Fota_Recv_Data, (DataPtr + 42), *(DataPtr + 41));
  1500. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data, Fota_S.Fota_Recv_Data_Len);
  1501. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
  1502. if (Fota_S.Fota_CRC == *(DataPtr + Fota_S.Fota_Recv_Data_Len + 42) || Data_Read_Buffer != PNULL)
  1503. {
  1504. UINT8 Fota_Recv_Data_Len_4 = 0;
  1505. if (Fota_S.Fota_Recv_Data_Len % 4 != 0)
  1506. {
  1507. Fota_Recv_Data_Len_4 = Fota_S.Fota_Recv_Data_Len + 4 - (Fota_S.Fota_Recv_Data_Len % 4);
  1508. }
  1509. else
  1510. {
  1511. Fota_Recv_Data_Len_4 = Fota_S.Fota_Recv_Data_Len;
  1512. }
  1513. BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data, Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Fota_Recv_Data_Len_4);
  1514. memset(Data_Read_Buffer, 0x00, Fota_S.Fota_Recv_Data_Len);
  1515. BSP_QSPI_Read_Safe(Data_Read_Buffer, Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Fota_S.Fota_Recv_Data_Len);
  1516. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer, Fota_S.Fota_Recv_Data_Len);
  1517. #ifdef USING_PRINTF1
  1518. printf("Data_Read_Buffer:\n");
  1519. for (int i = 0; i < Fota_S.Fota_Recv_Data_Len; i++)
  1520. {
  1521. printf("%x ", *(Data_Read_Buffer + i));
  1522. }
  1523. printf("\n");
  1524. #endif
  1525. if (Data_Read_Crc == Fota_S.Fota_CRC)
  1526. {
  1527. Fota_Answer[3] = 0x01;
  1528. }
  1529. else
  1530. {
  1531. Fota_Answer[3] = 0x02;
  1532. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Fota_Recv_Data_Len_4);
  1533. }
  1534. }
  1535. else //数据校验失败
  1536. {
  1537. Fota_Answer[3] = 0x02;
  1538. }
  1539. if (Data_Read_Buffer != PNULL)
  1540. free(Data_Read_Buffer);
  1541. Data_Read_Buffer = PNULL;
  1542. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1543. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1544. Fota_Answer[22] = 0x00;
  1545. Fota_Answer[23] = 0x12;
  1546. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1547. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1548. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1549. break;
  1550. }
  1551. case 0x03:
  1552. {
  1553. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  1554. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  1555. Fota_Answer[3] = 0x01;
  1556. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1557. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1558. Fota_Answer[22] = 0x00;
  1559. Fota_Answer[23] = 0x12;
  1560. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1561. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1562. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1563. if (Fota_S.Fota_All_Data_Len == Fota_S.Fota_Current_Addres)
  1564. {
  1565. NB_Fota_update_flag = TRUE;
  1566. }
  1567. else
  1568. {
  1569. NB_Fota_update_flag = FALSE;
  1570. }
  1571. break;
  1572. }
  1573. default:
  1574. {
  1575. Fota_Answer[3] = 0x02;
  1576. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1577. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1578. Fota_Answer[22] = 0x00;
  1579. Fota_Answer[23] = 0x12;
  1580. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1581. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1582. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. else if (*(DataPtr + 30) == 0x88) //BMS升级文件存放
  1588. {
  1589. Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR;
  1590. Fota_Cmd = *(DataPtr + 31);
  1591. Fota_Answer[0] = TCP_START_SYM1;
  1592. Fota_Answer[1] = TCP_START_SYM2;
  1593. Fota_Answer[2] = TCP_CONCMD_SYM;
  1594. switch (Fota_Cmd)
  1595. {
  1596. case 0x01:
  1597. {
  1598. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  1599. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  1600. if (Fota_S.Fota_All_Data_Len >= (FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR))
  1601. {
  1602. Fota_Answer[3] = 0x02;
  1603. }
  1604. else
  1605. {
  1606. Fota_Answer[3] = 0x01;
  1607. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres, (FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR)); //512k-32k = 480k -> 0x75300 0x78000
  1608. }
  1609. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1610. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1611. Fota_Answer[22] = 0x00;
  1612. Fota_Answer[23] = 0x12;
  1613. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1614. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1615. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1616. break;
  1617. }
  1618. case 0x02:
  1619. {
  1620. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  1621. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  1622. Fota_S.Fota_Recv_Data_Len = *(DataPtr + 41);
  1623. memset(Fota_S.Fota_Recv_Data, 0x00, 100);
  1624. memcpy(Fota_S.Fota_Recv_Data, (DataPtr + 42), *(DataPtr + 41));
  1625. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data, Fota_S.Fota_Recv_Data_Len);
  1626. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
  1627. if (Fota_S.Fota_CRC == *(DataPtr + Fota_S.Fota_Recv_Data_Len + 42) || Data_Read_Buffer != PNULL)
  1628. {
  1629. UINT8 Fota_Recv_Data_Len_4 = 0;
  1630. if (Fota_S.Fota_Recv_Data_Len % 4 != 0)
  1631. {
  1632. Fota_Recv_Data_Len_4 = Fota_S.Fota_Recv_Data_Len + 4 - (Fota_S.Fota_Recv_Data_Len % 4);
  1633. }
  1634. else
  1635. {
  1636. Fota_Recv_Data_Len_4 = Fota_S.Fota_Recv_Data_Len;
  1637. }
  1638. BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data, Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Fota_Recv_Data_Len_4);
  1639. memset(Data_Read_Buffer, 0x00, Fota_S.Fota_Recv_Data_Len);
  1640. BSP_QSPI_Read_Safe(Data_Read_Buffer, Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Fota_S.Fota_Recv_Data_Len);
  1641. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer, Fota_S.Fota_Recv_Data_Len);
  1642. #ifdef USING_PRINTF1
  1643. printf("\n\n\n");
  1644. UINT8 temp[1];
  1645. for (int i = 0; i < Fota_S.Fota_Recv_Data_Len; i++)
  1646. {
  1647. printf("%x ", *(Data_Read_Buffer + i));
  1648. }
  1649. printf("\n\n\n");
  1650. #endif
  1651. if (Data_Read_Crc == Fota_S.Fota_CRC)
  1652. {
  1653. Fota_Answer[3] = 0x01;
  1654. }
  1655. else
  1656. {
  1657. Fota_Answer[3] = 0x02;
  1658. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Fota_Recv_Data_Len_4);
  1659. }
  1660. }
  1661. else //数据校验失败
  1662. {
  1663. Fota_Answer[3] = 0x02;
  1664. }
  1665. if (Data_Read_Buffer != PNULL)
  1666. free(Data_Read_Buffer);
  1667. Data_Read_Buffer = PNULL;
  1668. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1669. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1670. Fota_Answer[22] = 0x00;
  1671. Fota_Answer[23] = 0x12;
  1672. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1673. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1674. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1675. break;
  1676. }
  1677. case 0x03:
  1678. {
  1679. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  1680. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  1681. Fota_Answer[3] = 0x01;
  1682. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1683. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1684. Fota_Answer[22] = 0x00;
  1685. Fota_Answer[23] = 0x12;
  1686. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1687. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1688. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1689. if (Fota_S.Fota_All_Data_Len == Fota_S.Fota_Current_Addres)
  1690. {
  1691. BMS_Fota_update_flag = TRUE;
  1692. }
  1693. else
  1694. {
  1695. BMS_Fota_update_flag = FALSE;
  1696. }
  1697. break;
  1698. }
  1699. default:
  1700. {
  1701. Fota_Answer[3] = 0x02;
  1702. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  1703. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  1704. Fota_Answer[22] = 0x00;
  1705. Fota_Answer[23] = 0x12;
  1706. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  1707. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  1708. tcpipConnectionSend(connectId, Fota_Answer, 43, 0, 0, 0);
  1709. break;
  1710. }
  1711. }
  1712. }
  1713. }
  1714. /**
  1715. * @brief : fota网络校验函数
  1716. * @param {UINT8} *data
  1717. * @param {UINT8} length
  1718. * @return {*}
  1719. */
  1720. static UINT8 bcc_chk_fota(UINT8 *data, UINT8 length)
  1721. {
  1722. UINT8 bcc_chk_return = 0x00;
  1723. UINT8 count = 0;
  1724. while (count < length)
  1725. {
  1726. bcc_chk_return ^= data[count];
  1727. count++;
  1728. }
  1729. return bcc_chk_return;
  1730. }
  1731. /**
  1732. * @brief : Fota校验函数
  1733. * @param {UINT8} *data
  1734. * @param {UINT8} length
  1735. * @return {*}
  1736. */
  1737. static UINT8 Fota_crc_chk(UINT8 *data, UINT8 length)
  1738. {
  1739. UINT8 reg_crc = 0x00;
  1740. while (length--)
  1741. {
  1742. reg_crc ^= *data++;
  1743. }
  1744. return reg_crc;
  1745. }
  1746. /**
  1747. * @brief : 1000ms 周期姓发送报文,根据调用周期及计时器,每100ms发送2帧报文,报文周期为1000ms
  1748. * @param {*}
  1749. * @return {*}
  1750. */
  1751. void CanMsgTx1000ms()
  1752. {
  1753. static UINT32 timerRecord = 0;
  1754. UINT8 count = 0;
  1755. CAN_Msg_Type canMsg;
  1756. UINT16 COMOutTable[28] = {
  1757. 0x6A0, 0x6A1, 0x6A2, 0x6A3, 0x6A4, 0x6A5, 0x6A6,
  1758. 0x6B0, 0x6BA,
  1759. 0x6C0, 0x6C1, 0x6C2, 0x6C3, 0x6C4,
  1760. 0x7C0, 0x7C1, 0x7C2, 0x7C3, 0x7C4, 0x7C5, 0x7C6, 0x7C7, 0x7C8, 0x7C9, 0x7CA, 0x7CB, 0x7CC, 0x7CD};
  1761. if (TimeCounter - timerRecord > 0)
  1762. {
  1763. timerRecord = TimeCounter;
  1764. count = TimeCounter % 10;
  1765. switch (count)
  1766. {
  1767. case 0: //send cell batt info 0x6A0、0x6A1
  1768. canMsg.DLC = 8;
  1769. canMsg.Id = COMOutTable[count * 2];
  1770. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1771. HAL_Can_Transmit(canMsg);
  1772. canMsg.DLC = 8;
  1773. canMsg.Id = COMOutTable[count * 2 + 1];
  1774. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1775. HAL_Can_Transmit(canMsg);
  1776. break;
  1777. case 1: //send cell batt info 0x6A2、0x6A3
  1778. canMsg.DLC = 8;
  1779. canMsg.Id = COMOutTable[count * 2];
  1780. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1781. HAL_Can_Transmit(canMsg);
  1782. canMsg.DLC = 8;
  1783. canMsg.Id = COMOutTable[count * 2 + 1];
  1784. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1785. HAL_Can_Transmit(canMsg);
  1786. break;
  1787. case 2: //send cell batt info 0x6A4、 0x6A5
  1788. canMsg.DLC = 8;
  1789. canMsg.Id = COMOutTable[count * 2];
  1790. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1791. HAL_Can_Transmit(canMsg);
  1792. canMsg.DLC = 8;
  1793. canMsg.Id = COMOutTable[count * 2 + 1];
  1794. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1795. HAL_Can_Transmit(canMsg);
  1796. break;
  1797. case 3: //send cell batt info 0x6A6、0x6B0
  1798. canMsg.DLC = 8;
  1799. canMsg.Id = COMOutTable[count * 2];
  1800. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1801. HAL_Can_Transmit(canMsg);
  1802. canMsg.DLC = 8;
  1803. canMsg.Id = COMOutTable[count * 2 + 1];
  1804. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1805. HAL_Can_Transmit(canMsg);
  1806. break;
  1807. case 4: //send batt temp info 0x6BA、0x6C0
  1808. canMsg.DLC = 8;
  1809. canMsg.Id = COMOutTable[count * 2];
  1810. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1811. HAL_Can_Transmit(canMsg);
  1812. canMsg.DLC = 8;
  1813. canMsg.Id = COMOutTable[count * 2 + 1];
  1814. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1815. HAL_Can_Transmit(canMsg);
  1816. break;
  1817. case 5: //send batt other info 0x6C1、0x6C2
  1818. canMsg.DLC = 8;
  1819. canMsg.Id = COMOutTable[count * 2];
  1820. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1821. HAL_Can_Transmit(canMsg);
  1822. canMsg.DLC = 8;
  1823. canMsg.Id = COMOutTable[count * 2 + 1];
  1824. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1825. HAL_Can_Transmit(canMsg);
  1826. break;
  1827. case 6: //send batt other info 0x6C3、0x6C4
  1828. canMsg.DLC = 8;
  1829. canMsg.Id = COMOutTable[count * 2];
  1830. CANEncodeFunction(COMOutTable[count * 2], canMsg.Data);
  1831. HAL_Can_Transmit(canMsg);
  1832. canMsg.DLC = 8;
  1833. canMsg.Id = COMOutTable[count * 2 + 1];
  1834. CANEncodeFunction(COMOutTable[count * 2 + 1], canMsg.Data);
  1835. HAL_Can_Transmit(canMsg);
  1836. break;
  1837. default:
  1838. break;
  1839. }
  1840. }
  1841. }
  1842. /**
  1843. * @brief : 周期性报文组包,根据不同的ID,进行组包,并放入指针中
  1844. * @param {UINT32} ID
  1845. * @param {UINT8} *msgData
  1846. * @return {*}
  1847. */
  1848. void CANEncodeFunction(UINT32 ID, UINT8 *msgData)
  1849. {
  1850. memset(msgData, 0xFF, 8);
  1851. switch (ID)
  1852. {
  1853. case 0x6A0:
  1854. *(UINT16 *)(msgData + 0) = ((battCellU[0] >> 8) & 0xFF) | (((battCellU[0] & 0xFF) << 8) & 0xFF00);
  1855. *(UINT16 *)(msgData + 2) = ((battCellU[1] >> 8) & 0xFF) | (((battCellU[1] & 0xFF) << 8) & 0xFF00);
  1856. *(UINT16 *)(msgData + 4) = ((battCellU[2] >> 8) & 0xFF) | (((battCellU[2] & 0xFF) << 8) & 0xFF00);
  1857. *(UINT16 *)(msgData + 6) = ((battCellU[3] >> 8) & 0xFF) | (((battCellU[3] & 0xFF) << 8) & 0xFF00);
  1858. break;
  1859. case 0x6A1:
  1860. *(UINT16 *)(msgData + 0) = ((battCellU[4] >> 8) & 0xFF) | (((battCellU[4] & 0xFF) << 8) & 0xFF00);
  1861. *(UINT16 *)(msgData + 2) = ((battCellU[5] >> 8) & 0xFF) | (((battCellU[5] & 0xFF) << 8) & 0xFF00);
  1862. *(UINT16 *)(msgData + 4) = ((battCellU[6] >> 8) & 0xFF) | (((battCellU[6] & 0xFF) << 8) & 0xFF00);
  1863. *(UINT16 *)(msgData + 6) = ((battCellU[7] >> 8) & 0xFF) | (((battCellU[7] & 0xFF) << 8) & 0xFF00);
  1864. break;
  1865. case 0x6A2:
  1866. *(UINT16 *)(msgData + 0) = ((battCellU[8] >> 8) & 0xFF) | (((battCellU[8] & 0xFF) << 8) & 0xFF00);
  1867. *(UINT16 *)(msgData + 2) = ((battCellU[9] >> 8) & 0xFF) | (((battCellU[9] & 0xFF) << 8) & 0xFF00);
  1868. *(UINT16 *)(msgData + 4) = ((battCellU[10] >> 8) & 0xFF) | (((battCellU[10] & 0xFF) << 8) & 0xFF00);
  1869. *(UINT16 *)(msgData + 6) = ((battCellU[11] >> 8) & 0xFF) | (((battCellU[11] & 0xFF) << 8) & 0xFF00);
  1870. break;
  1871. case 0x6A3:
  1872. *(UINT16 *)(msgData + 0) = ((battCellU[12] >> 8) & 0xFF) | (((battCellU[12] & 0xFF) << 8) & 0xFF00);
  1873. *(UINT16 *)(msgData + 2) = ((battCellU[13] >> 8) & 0xFF) | (((battCellU[13] & 0xFF) << 8) & 0xFF00);
  1874. *(UINT16 *)(msgData + 4) = ((battCellU[14] >> 8) & 0xFF) | (((battCellU[14] & 0xFF) << 8) & 0xFF00);
  1875. *(UINT16 *)(msgData + 6) = ((battCellU[15] >> 8) & 0xFF) | (((battCellU[15] & 0xFF) << 8) & 0xFF00);
  1876. break;
  1877. case 0x6A4:
  1878. *(UINT16 *)(msgData + 0) = ((battCellU[16] >> 8) & 0xFF) | (((battCellU[16] & 0xFF) << 8) & 0xFF00);
  1879. *(UINT16 *)(msgData + 2) = ((battCellU[17] >> 8) & 0xFF) | (((battCellU[17] & 0xFF) << 8) & 0xFF00);
  1880. *(UINT16 *)(msgData + 4) = ((battCellU[18] >> 8) & 0xFF) | (((battCellU[18] & 0xFF) << 8) & 0xFF00);
  1881. *(UINT16 *)(msgData + 6) = ((battCellU[19] >> 8) & 0xFF) | (((battCellU[19] & 0xFF) << 8) & 0xFF00);
  1882. break;
  1883. case 0x6A5:
  1884. *(UINT16 *)(msgData + 0) = ((battCellU[20] >> 8) & 0xFF) | (((battCellU[20] & 0xFF) << 8) & 0xFF00);
  1885. *(UINT16 *)(msgData + 2) = ((battCellU[21] >> 8) & 0xFF) | (((battCellU[21] & 0xFF) << 8) & 0xFF00);
  1886. *(UINT16 *)(msgData + 4) = ((battCellU[22] >> 8) & 0xFF) | (((battCellU[22] & 0xFF) << 8) & 0xFF00);
  1887. *(UINT16 *)(msgData + 6) = ((battCellU[23] >> 8) & 0xFF) | (((battCellU[23] & 0xFF) << 8) & 0xFF00);
  1888. break;
  1889. case 0x6A6:
  1890. *(UINT16 *)(msgData + 0) = ((battCellU[24] >> 8) & 0xFF) | (((battCellU[24] & 0xFF) << 8) & 0xFF00);
  1891. *(UINT16 *)(msgData + 2) = ((battCellU[25] >> 8) & 0xFF) | (((battCellU[25] & 0xFF) << 8) & 0xFF00);
  1892. *(UINT16 *)(msgData + 4) = ((battCellU[26] >> 8) & 0xFF) | (((battCellU[26] & 0xFF) << 8) & 0xFF00);
  1893. *(UINT16 *)(msgData + 6) = ((battCellU[27] >> 8) & 0xFF) | (((battCellU[27] & 0xFF) << 8) & 0xFF00);
  1894. break;
  1895. case 0x6B0:
  1896. *(UINT8 *)(msgData + 0) = battCellTemp[0] & 0xFF;
  1897. *(UINT8 *)(msgData + 1) = battCellTemp[1] & 0xFF;
  1898. *(UINT8 *)(msgData + 2) = battCellTemp[2] & 0xFF;
  1899. *(UINT8 *)(msgData + 3) = battCellTemp[3] & 0xFF;
  1900. *(UINT8 *)(msgData + 4) = battCellTemp[4] & 0xFF;
  1901. *(UINT8 *)(msgData + 5) = battCellTemp[5] & 0xFF;
  1902. *(UINT8 *)(msgData + 6) = battCellTemp[6] & 0xFF;
  1903. *(UINT8 *)(msgData + 7) = battCellTemp[7] & 0xFF;
  1904. break;
  1905. case 0x6BA:
  1906. *(UINT8 *)(msgData + 0) = MOSTemp & 0xFF;
  1907. *(UINT8 *)(msgData + 1) = packTemp & 0xFF;
  1908. *(UINT8 *)(msgData + 2) = fastChargeTemp & 0xFF;
  1909. *(UINT8 *)(msgData + 3) = normalChargeTemp & 0xFF;
  1910. *(UINT8 *)(msgData + 4) = heatTemp1 & 0xFF;
  1911. *(UINT8 *)(msgData + 5) = heatTemp2 & 0xFF;
  1912. *(UINT8 *)(msgData + 6) = nbReservedTemp1 & 0xFF;
  1913. *(UINT8 *)(msgData + 7) = nbReservedTemp2 & 0xFF;
  1914. break;
  1915. case 0x6C0:
  1916. *(UINT16 *)(msgData + 0) = ((minCellVol >> 8) & 0xFF) | (((minCellVol & 0xFF) << 8) & 0xFF00);
  1917. *(UINT16 *)(msgData + 2) = ((maxCellVol >> 8) & 0xFF) | (((maxCellVol & 0xFF) << 8) & 0xFF00);
  1918. *(UINT16 *)(msgData + 4) = ((battPackVol >> 8) & 0xFF) | (((battPackVol & 0xFF) << 8) & 0xFF00);
  1919. *(UINT8 *)(msgData + 6) = maxCellTemp & 0xFF;
  1920. *(UINT8 *)(msgData + 7) = minCellTemp & 0xFF;
  1921. break;
  1922. case 0x6C1:
  1923. *(UINT8 *)(msgData + 0) = battSOC & 0xFF;
  1924. *(UINT8 *)(msgData + 1) = battSOH & 0xFF;
  1925. *(UINT16 *)(msgData + 2) = ((battI >> 8) & 0xFF) | (((battI & 0xFF) << 8) & 0xFF00);
  1926. *(UINT8 *)(msgData + 4) = battWorkState & 0xFF;
  1927. *(UINT32 *)(msgData + 5) = ((battWarningState >> 16) & 0xFF) | ((battWarningState)&0xFF00) | (((battWarningState & 0xFF) << 16) & 0xFF0000);
  1928. break;
  1929. case 0x6C2:
  1930. *(UINT32 *)(msgData + 0) = ((nbSwVersion >> 24) & 0xFF) | ((nbSwVersion >> 8) & 0xFF00) | ((nbSwVersion << 8) & 0xFF0000) | (((nbSwVersion & 0xFF) << 24) & 0xFF000000);
  1931. *(UINT16 *)(msgData + 4) = ((nbHwVersion >> 8) & 0xFF) | (((nbHwVersion & 0xFF) << 8) & 0xFF00);
  1932. *(UINT8 *)(msgData + 6) = bmsSwVersion & 0xFF;
  1933. *(UINT8 *)(msgData + 7) = bmsHwVersion & 0xFF;
  1934. break;
  1935. case 0x6C3:
  1936. *(UINT32 *)(msgData + 0) = ((battBalanceoInfo >> 24) & 0xFF) | ((battBalanceoInfo >> 8) & 0xFF00) | ((battBalanceoInfo << 8) & 0xFF0000) | (((battBalanceoInfo & 0xFF) << 24) & 0xFF000000);
  1937. *(UINT8 *)(msgData + 4) = battMOSSwitchState & 0xFF;
  1938. *(UINT8 *)(msgData + 5) = battHeatEnableState & 0xFF;
  1939. break;
  1940. case 0x6C4:
  1941. reservedSignal1 = battProtectState & 0xFF;
  1942. reservedSignal2 = (battProtectState >> 8) & 0xFF;
  1943. reservedSignal3 = (battProtectState >> 16) & 0xFF;
  1944. reservedSignal4 = (battProtectState >> 24) & 0xFF;
  1945. *(UINT8 *)(msgData + 0) = reservedSignal1 & 0xFF;
  1946. *(UINT8 *)(msgData + 1) = reservedSignal2 & 0xFF;
  1947. *(UINT8 *)(msgData + 2) = reservedSignal3 & 0xFF;
  1948. *(UINT8 *)(msgData + 3) = reservedSignal4 & 0xFF;
  1949. *(UINT8 *)(msgData + 4) = reservedSignal5 & 0xFF;
  1950. *(UINT8 *)(msgData + 5) = reservedSignal6 & 0xFF;
  1951. *(UINT8 *)(msgData + 6) = reservedSignal7 & 0xFF;
  1952. *(UINT8 *)(msgData + 7) = reservedSignal8 & 0xFF;
  1953. break;
  1954. case 0x7C0:
  1955. *(UINT16 *)(msgData + 0) = ((battCellU[0] >> 8) & 0xFF) | (((battCellU[0] & 0xFF) << 8) & 0xFF00);
  1956. *(UINT16 *)(msgData + 2) = ((battCellU[1] >> 8) & 0xFF) | (((battCellU[1] & 0xFF) << 8) & 0xFF00);
  1957. *(UINT16 *)(msgData + 4) = ((battCellU[2] >> 8) & 0xFF) | (((battCellU[2] & 0xFF) << 8) & 0xFF00);
  1958. *(UINT16 *)(msgData + 6) = ((battCellU[3] >> 8) & 0xFF) | (((battCellU[3] & 0xFF) << 8) & 0xFF00);
  1959. break;
  1960. case 0x7C1:
  1961. *(UINT16 *)(msgData + 0) = ((battCellU[4] >> 8) & 0xFF) | (((battCellU[4] & 0xFF) << 8) & 0xFF00);
  1962. *(UINT16 *)(msgData + 2) = ((battCellU[5] >> 8) & 0xFF) | (((battCellU[5] & 0xFF) << 8) & 0xFF00);
  1963. *(UINT16 *)(msgData + 4) = ((battCellU[6] >> 8) & 0xFF) | (((battCellU[6] & 0xFF) << 8) & 0xFF00);
  1964. *(UINT16 *)(msgData + 6) = ((battCellU[7] >> 8) & 0xFF) | (((battCellU[7] & 0xFF) << 8) & 0xFF00);
  1965. break;
  1966. case 0x7C2:
  1967. *(UINT16 *)(msgData + 0) = ((battCellU[8] >> 8) & 0xFF) | (((battCellU[8] & 0xFF) << 8) & 0xFF00);
  1968. *(UINT16 *)(msgData + 2) = ((battCellU[9] >> 8) & 0xFF) | (((battCellU[9] & 0xFF) << 8) & 0xFF00);
  1969. *(UINT16 *)(msgData + 4) = ((battCellU[10] >> 8) & 0xFF) | (((battCellU[10] & 0xFF) << 8) & 0xFF00);
  1970. *(UINT16 *)(msgData + 6) = ((battCellU[11] >> 8) & 0xFF) | (((battCellU[11] & 0xFF) << 8) & 0xFF00);
  1971. break;
  1972. case 0x7C3:
  1973. *(UINT16 *)(msgData + 0) = ((battCellU[12] >> 8) & 0xFF) | (((battCellU[12] & 0xFF) << 8) & 0xFF00);
  1974. *(UINT16 *)(msgData + 2) = ((battCellU[13] >> 8) & 0xFF) | (((battCellU[13] & 0xFF) << 8) & 0xFF00);
  1975. *(UINT16 *)(msgData + 4) = ((battCellU[14] >> 8) & 0xFF) | (((battCellU[14] & 0xFF) << 8) & 0xFF00);
  1976. *(UINT16 *)(msgData + 6) = ((battCellU[15] >> 8) & 0xFF) | (((battCellU[15] & 0xFF) << 8) & 0xFF00);
  1977. break;
  1978. case 0x7C4:
  1979. *(UINT16 *)(msgData + 0) = ((battCellU[16] >> 8) & 0xFF) | (((battCellU[16] & 0xFF) << 8) & 0xFF00);
  1980. *(UINT16 *)(msgData + 2) = ((battCellU[17] >> 8) & 0xFF) | (((battCellU[17] & 0xFF) << 8) & 0xFF00);
  1981. *(UINT16 *)(msgData + 4) = ((battCellU[18] >> 8) & 0xFF) | (((battCellU[18] & 0xFF) << 8) & 0xFF00);
  1982. *(UINT16 *)(msgData + 6) = ((battCellU[19] >> 8) & 0xFF) | (((battCellU[19] & 0xFF) << 8) & 0xFF00);
  1983. break;
  1984. case 0x7C5:
  1985. *(UINT16 *)(msgData + 0) = ((battCellU[20] >> 8) & 0xFF) | (((battCellU[20] & 0xFF) << 8) & 0xFF00);
  1986. *(UINT16 *)(msgData + 2) = ((battCellU[21] >> 8) & 0xFF) | (((battCellU[21] & 0xFF) << 8) & 0xFF00);
  1987. *(UINT16 *)(msgData + 4) = ((battCellU[22] >> 8) & 0xFF) | (((battCellU[22] & 0xFF) << 8) & 0xFF00);
  1988. *(UINT16 *)(msgData + 6) = ((battCellU[23] >> 8) & 0xFF) | (((battCellU[23] & 0xFF) << 8) & 0xFF00);
  1989. break;
  1990. case 0x7C6:
  1991. *(UINT16 *)(msgData + 0) = ((battCellU[24] >> 8) & 0xFF) | (((battCellU[24] & 0xFF) << 8) & 0xFF00);
  1992. *(UINT16 *)(msgData + 2) = ((battCellU[25] >> 8) & 0xFF) | (((battCellU[25] & 0xFF) << 8) & 0xFF00);
  1993. *(UINT16 *)(msgData + 4) = ((battCellU[26] >> 8) & 0xFF) | (((battCellU[26] & 0xFF) << 8) & 0xFF00);
  1994. *(UINT16 *)(msgData + 6) = ((battCellU[27] >> 8) & 0xFF) | (((battCellU[27] & 0xFF) << 8) & 0xFF00);
  1995. break;
  1996. case 0x7C7:
  1997. *(UINT8 *)(msgData + 0) = battCellTemp[0] & 0xFF;
  1998. *(UINT8 *)(msgData + 1) = battCellTemp[1] & 0xFF;
  1999. *(UINT8 *)(msgData + 2) = battCellTemp[2] & 0xFF;
  2000. *(UINT8 *)(msgData + 3) = battCellTemp[3] & 0xFF;
  2001. *(UINT8 *)(msgData + 4) = battCellTemp[4] & 0xFF;
  2002. *(UINT8 *)(msgData + 5) = battCellTemp[5] & 0xFF;
  2003. *(UINT8 *)(msgData + 6) = battCellTemp[6] & 0xFF;
  2004. *(UINT8 *)(msgData + 7) = battCellTemp[7] & 0xFF;
  2005. break;
  2006. case 0x7C8:
  2007. *(UINT8 *)(msgData + 0) = MOSTemp & 0xFF;
  2008. *(UINT8 *)(msgData + 1) = packTemp & 0xFF;
  2009. *(UINT8 *)(msgData + 2) = fastChargeTemp & 0xFF;
  2010. *(UINT8 *)(msgData + 3) = normalChargeTemp & 0xFF;
  2011. *(UINT8 *)(msgData + 4) = heatTemp1 & 0xFF;
  2012. *(UINT8 *)(msgData + 5) = heatTemp2 & 0xFF;
  2013. *(UINT8 *)(msgData + 6) = nbReservedTemp1 & 0xFF;
  2014. *(UINT8 *)(msgData + 7) = nbReservedTemp2 & 0xFF;
  2015. break;
  2016. case 0x7C9:
  2017. *(UINT16 *)(msgData + 0) = ((minCellVol >> 8) & 0xFF) | (((minCellVol & 0xFF) << 8) & 0xFF00);
  2018. *(UINT16 *)(msgData + 2) = ((maxCellVol >> 8) & 0xFF) | (((maxCellVol & 0xFF) << 8) & 0xFF00);
  2019. *(UINT16 *)(msgData + 4) = ((battPackVol >> 8) & 0xFF) | (((battPackVol & 0xFF) << 8) & 0xFF00);
  2020. *(UINT8 *)(msgData + 6) = maxCellTemp & 0xFF;
  2021. *(UINT8 *)(msgData + 7) = minCellTemp & 0xFF;
  2022. break;
  2023. case 0x7CA:
  2024. *(UINT8 *)(msgData + 0) = battSOC & 0xFF;
  2025. *(UINT8 *)(msgData + 1) = battSOH & 0xFF;
  2026. *(UINT16 *)(msgData + 2) = ((battI >> 8) & 0xFF) | (((battI & 0xFF) << 8) & 0xFF00);
  2027. *(UINT8 *)(msgData + 4) = battWorkState & 0xFF;
  2028. *(UINT32 *)(msgData + 5) = ((battWarningState >> 16) & 0xFF) | ((battWarningState)&0xFF00) | (((battWarningState & 0xFF) << 16) & 0xFF0000);
  2029. break;
  2030. case 0x7CB:
  2031. *(UINT32 *)(msgData + 0) = ((nbSwVersion >> 24) & 0xFF) | ((nbSwVersion >> 8) & 0xFF00) | ((nbSwVersion << 8) & 0xFF0000) | (((nbSwVersion & 0xFF) << 24) & 0xFF000000);
  2032. *(UINT16 *)(msgData + 4) = ((nbHwVersion >> 8) & 0xFF) | (((nbHwVersion & 0xFF) << 8) & 0xFF00);
  2033. *(UINT8 *)(msgData + 6) = bmsSwVersion & 0xFF;
  2034. *(UINT8 *)(msgData + 7) = bmsHwVersion & 0xFF;
  2035. break;
  2036. case 0x7CC:
  2037. *(UINT32 *)(msgData + 0) = ((battBalanceoInfo >> 24) & 0xFF) | ((battBalanceoInfo >> 8) & 0xFF00) | ((battBalanceoInfo << 8) & 0xFF0000) | (((battBalanceoInfo & 0xFF) << 24) & 0xFF000000);
  2038. *(UINT8 *)(msgData + 4) = battMOSSwitchState & 0xFF;
  2039. *(UINT8 *)(msgData + 5) = battHeatEnableState & 0xFF;
  2040. break;
  2041. case 0x7CD:
  2042. reservedSignal1 = battProtectState & 0xFF;
  2043. reservedSignal2 = (battProtectState >> 8) & 0xFF;
  2044. reservedSignal3 = (battProtectState >> 16) & 0xFF;
  2045. reservedSignal4 = (battProtectState >> 24) & 0xFF;
  2046. *(UINT8 *)(msgData + 0) = reservedSignal1 & 0xFF;
  2047. *(UINT8 *)(msgData + 1) = reservedSignal2 & 0xFF;
  2048. *(UINT8 *)(msgData + 2) = reservedSignal3 & 0xFF;
  2049. *(UINT8 *)(msgData + 3) = reservedSignal4 & 0xFF;
  2050. *(UINT8 *)(msgData + 4) = reservedSignal5 & 0xFF;
  2051. *(UINT8 *)(msgData + 5) = reservedSignal6 & 0xFF;
  2052. *(UINT8 *)(msgData + 6) = reservedSignal7 & 0xFF;
  2053. *(UINT8 *)(msgData + 7) = reservedSignal8 & 0xFF;
  2054. break;
  2055. case 0x7CE:
  2056. reservedSignal1 = socd_pct_ahSoc & 0xFF;
  2057. reservedSignal2 = (socd_pct_ahSoc >> 8) & 0xFF;
  2058. reservedSignal3 = socd_pct_ekfSoc & 0xFF;
  2059. reservedSignal4 = (socd_pct_ekfSoc >> 8) & 0xFF;
  2060. reservedSignal5 = socd_pct_estSoc & 0xFF;
  2061. reservedSignal6 = (socd_pct_estSoc >> 8) & 0xFF;
  2062. reservedSignal7 = socd_pct_battSoc & 0xFF;
  2063. reservedSignal8 = (socd_pct_battSoc >> 8) & 0xFF;
  2064. *(UINT8 *)(msgData + 0) = reservedSignal1 & 0xFF;
  2065. *(UINT8 *)(msgData + 1) = reservedSignal2 & 0xFF;
  2066. *(UINT8 *)(msgData + 2) = reservedSignal3 & 0xFF;
  2067. *(UINT8 *)(msgData + 3) = reservedSignal4 & 0xFF;
  2068. *(UINT8 *)(msgData + 4) = reservedSignal5 & 0xFF;
  2069. *(UINT8 *)(msgData + 5) = reservedSignal6 & 0xFF;
  2070. *(UINT8 *)(msgData + 6) = reservedSignal7 & 0xFF;
  2071. *(UINT8 *)(msgData + 7) = reservedSignal8 & 0xFF;
  2072. break;
  2073. case 0x7CF:
  2074. reservedSignal1 = socd_pct_bcuSoc & 0xFF;
  2075. reservedSignal2 = (socd_pct_bcuSoc >> 8) & 0xFF;
  2076. reservedSignal3 = test_efkSocMin & 0xFF;
  2077. reservedSignal4 = (test_efkSocMin >> 8) & 0xFF;
  2078. reservedSignal5 = test_efkSocMax & 0xFF;
  2079. reservedSignal6 = (test_efkSocMax >> 8) & 0xFF;
  2080. reservedSignal7 = 0 & 0xFF;
  2081. reservedSignal8 = (0 >> 8) & 0xFF;
  2082. *(UINT8 *)(msgData + 0) = reservedSignal1 & 0xFF;
  2083. *(UINT8 *)(msgData + 1) = reservedSignal2 & 0xFF;
  2084. *(UINT8 *)(msgData + 2) = reservedSignal3 & 0xFF;
  2085. *(UINT8 *)(msgData + 3) = reservedSignal4 & 0xFF;
  2086. *(UINT8 *)(msgData + 4) = reservedSignal5 & 0xFF;
  2087. *(UINT8 *)(msgData + 5) = reservedSignal6 & 0xFF;
  2088. *(UINT8 *)(msgData + 6) = reservedSignal7 & 0xFF;
  2089. *(UINT8 *)(msgData + 7) = reservedSignal8 & 0xFF;
  2090. break;
  2091. case 0x7D0:
  2092. UINT32 temp1 = (UINT32)(test_UpMin * 100000);
  2093. UINT32 temp2 = (UINT32)(test_UpMax * 100000);
  2094. reservedSignal1 = temp1 & 0xFF;
  2095. reservedSignal2 = (temp1 >> 8) & 0xFF;
  2096. reservedSignal3 = (temp1 >> 16) & 0xFF;
  2097. reservedSignal4 = (temp1 >> 24) & 0xFF;
  2098. reservedSignal5 = temp2 & 0xFF;
  2099. reservedSignal6 = (temp2 >> 8) & 0xFF;
  2100. reservedSignal7 = (temp2 >> 16) & 0xFF;
  2101. reservedSignal8 = (temp2 >> 24) & 0xFF;
  2102. *(UINT8 *)(msgData + 0) = reservedSignal1 & 0xFF;
  2103. *(UINT8 *)(msgData + 1) = reservedSignal2 & 0xFF;
  2104. *(UINT8 *)(msgData + 2) = reservedSignal3 & 0xFF;
  2105. *(UINT8 *)(msgData + 3) = reservedSignal4 & 0xFF;
  2106. *(UINT8 *)(msgData + 4) = reservedSignal5 & 0xFF;
  2107. *(UINT8 *)(msgData + 5) = reservedSignal6 & 0xFF;
  2108. *(UINT8 *)(msgData + 6) = reservedSignal7 & 0xFF;
  2109. *(UINT8 *)(msgData + 7) = reservedSignal8 & 0xFF;
  2110. break;
  2111. case 0x7B0:
  2112. *(UINT8 *)(msgData + 0) = (AppNVMData.battSN[0]) & 0xFF;
  2113. *(UINT8 *)(msgData + 1) = (AppNVMData.battSN[1]) & 0xFF;
  2114. *(UINT8 *)(msgData + 2) = (AppNVMData.battSN[2]) & 0xFF;
  2115. *(UINT8 *)(msgData + 3) = (AppNVMData.battSN[3]) & 0xFF;
  2116. *(UINT8 *)(msgData + 4) = (AppNVMData.battSN[4]) & 0xFF;
  2117. *(UINT8 *)(msgData + 5) = (AppNVMData.battSN[5]) & 0xFF;
  2118. *(UINT8 *)(msgData + 6) = (AppNVMData.battSN[6]) & 0xFF;
  2119. *(UINT8 *)(msgData + 7) = (AppNVMData.battSN[7]) & 0xFF;
  2120. break;
  2121. case 0x7B1:
  2122. *(UINT8 *)(msgData + 0) = (AppNVMData.battSN[8]) & 0xFF;
  2123. *(UINT8 *)(msgData + 1) = (AppNVMData.battSN[9]) & 0xFF;
  2124. *(UINT8 *)(msgData + 2) = (AppNVMData.battSN[10]) & 0xFF;
  2125. *(UINT8 *)(msgData + 3) = (AppNVMData.battSN[11]) & 0xFF;
  2126. *(UINT8 *)(msgData + 4) = (AppNVMData.battSN[12]) & 0xFF;
  2127. *(UINT8 *)(msgData + 5) = (AppNVMData.battSN[13]) & 0xFF;
  2128. *(UINT8 *)(msgData + 6) = (AppNVMData.battSN[14]) & 0xFF;
  2129. *(UINT8 *)(msgData + 7) = (AppNVMData.battSN[15]) & 0xFF;
  2130. break;
  2131. case 0x7B2:
  2132. *(UINT8 *)(msgData + 0) = (AppNVMData.battSN[16]) & 0xFF;
  2133. break;
  2134. default:
  2135. break;
  2136. }
  2137. }
  2138. /**
  2139. * @brief : UDS函数,根据报文内容,进行包括读取版本号、读写SN、查询状态,用于完成下线检测和通过CAN接收升级包等操作
  2140. * @param {CAN_Msg_Type} *CanRxMsg
  2141. * @return {*}
  2142. */
  2143. void UDS_Service(CAN_Msg_Type *CanRxMsg)
  2144. {
  2145. UINT8 i, j = 0;
  2146. UINT32 k = 0;
  2147. INT8 ret, errorCount = 0;
  2148. BOOL boolRet = false;
  2149. static UINT8 snFlag = 0;
  2150. static UINT32 sliceCounterFlag = 0;
  2151. UINT32 tempSliceCounter = 0;
  2152. static UINT8 counter = 0;
  2153. CAN_Msg_Type UDSAnswer = {0};
  2154. UINT32 posCode, negCode;
  2155. UINT32 updateDifferDataByteLen = 0;
  2156. static UINT8 updateDataBuffer[100];
  2157. static UINT8 controllerFlag = 0xFF;
  2158. UINT32 flashStartAddr = 0x00;
  2159. static UINT32 updateDifferDataSliceCounter = 0;
  2160. static UINT8 UDSBattSN[BATT_SN_LEN];
  2161. for (i = 0; i < 2; i++)
  2162. {
  2163. UDSAnswer.DLC = 8;
  2164. memset(UDSAnswer.Data, 0, UDSAnswer.DLC);
  2165. UDSService[i] = CanRxMsg[i].Data[0];
  2166. UDSSubService[i] = CanRxMsg[i].Data[1];
  2167. UDSSubServiceActionCode[i] = CanRxMsg[i].Data[2];
  2168. if (CanRxMsg[i].Id == 0x7A0)
  2169. {
  2170. switch (UDSService[i])
  2171. {
  2172. case 0x10:
  2173. if (UDSSubService[i] == 0x01)
  2174. {
  2175. if ((UDSDialogMode == 0x01 || UDSDialogMode == 0x02) || UDSSwitch == 0)
  2176. {
  2177. UDSPositiveAnswer(0x04, i, 0x00);
  2178. UDSSwitch = 0;
  2179. UDSDialogMode = 1;
  2180. }
  2181. else
  2182. {
  2183. UDSNegtiveAnswer(0x05, i, 0xFF);
  2184. }
  2185. }
  2186. else if (UDSSubService[i] == 0x02)
  2187. {
  2188. UDSPositiveAnswer(0x04, i, 0x00);
  2189. UDSSwitch = 1;
  2190. UDSDialogMode = 2;
  2191. }
  2192. else if (UDSSubService[i] == 0x03)
  2193. {
  2194. if (UDSSwitch == 1)
  2195. {
  2196. UDSPositiveAnswer(0x04, i, 0x00);
  2197. UDSSwitch = 1;
  2198. UDSDialogMode = 3;
  2199. }
  2200. else
  2201. {
  2202. UDSNegtiveAnswer(0x05, i, 0xFF);
  2203. }
  2204. }
  2205. else if (UDSSubService[i] == 0x11) //make NB software reset
  2206. {
  2207. if (UDSDialogMode == 2)
  2208. {
  2209. UDSPositiveAnswer(0x04, i, 0x00);
  2210. osDelay(100);
  2211. //SaveAppConfig();
  2212. appSetCFUN(0);
  2213. osDelay(1000);
  2214. EC_SystemReset();
  2215. }
  2216. else
  2217. {
  2218. UDSNegtiveAnswer(0x05, i, 0xFF);
  2219. }
  2220. }
  2221. else
  2222. {
  2223. UDSNegtiveAnswer(0x05, i, 0xEE);
  2224. }
  2225. break;
  2226. case 0x14: //clear the DTC information 14 FF FF FF 00 00 00 00
  2227. if (UDSDialogMode == 2)
  2228. {
  2229. if (UDSSubService[i] == 0xFF)
  2230. {
  2231. if (UDSSubServiceActionCode[i] == 0xFF)
  2232. {
  2233. if (CanRxMsg[i].Data[3] == 0xFF)
  2234. {
  2235. ihd_flg_DTCClear = 0x01;
  2236. posCode = 0xEEEE;
  2237. UDSPositiveAnswer(06, i, posCode);
  2238. }
  2239. else
  2240. {
  2241. negCode = (UDSSubServiceActionCode[i] << 8) | CanRxMsg[i].Data[3];
  2242. UDSNegtiveAnswer(6, i, negCode);
  2243. }
  2244. }
  2245. else
  2246. {
  2247. negCode = (UDSSubServiceActionCode[i] << 8) | CanRxMsg[i].Data[3];
  2248. UDSNegtiveAnswer(6, i, negCode);
  2249. }
  2250. }
  2251. else
  2252. {
  2253. negCode = (UDSSubServiceActionCode[i] << 8) | CanRxMsg[i].Data[3];
  2254. UDSNegtiveAnswer(6, i, negCode);
  2255. }
  2256. }
  2257. else
  2258. {
  2259. UDSNegtiveAnswer(0x05, i, 0xFF);
  2260. }
  2261. break;
  2262. case 0x22:
  2263. if (UDSDialogMode == 2)
  2264. {
  2265. if (UDSSubService[i] == 0x01) //check the sw of NB
  2266. {
  2267. UDSPositiveAnswer(0x08, i, APPSWVERSION);
  2268. }
  2269. else if (UDSSubService[i] == 0x02) //check the hw of NB
  2270. {
  2271. UDSPositiveAnswer(0x06, i, HWVERSION);
  2272. }
  2273. else if (UDSSubService[i] == 0x03) //check the SN number
  2274. {
  2275. switch (UDSSubServiceActionCode[i])
  2276. {
  2277. case 00:
  2278. UDSAnswer.Id = 0x7B0;
  2279. CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
  2280. ret = HAL_Can_Transmit(UDSAnswer);
  2281. break;
  2282. case 01:
  2283. UDSAnswer.Id = 0x7B1;
  2284. CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
  2285. ret = HAL_Can_Transmit(UDSAnswer);
  2286. break;
  2287. case 02:
  2288. UDSAnswer.Id = 0x7B2;
  2289. CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
  2290. ret = HAL_Can_Transmit(UDSAnswer);
  2291. break;
  2292. default:
  2293. break;
  2294. }
  2295. }
  2296. else if (UDSSubService[i] == 0x04) //check the batt message
  2297. {
  2298. UDSAnswer.Id = 0x7C0 + UDSSubServiceActionCode[i];
  2299. CANEncodeFunction(UDSAnswer.Id, UDSAnswer.Data);
  2300. ret = HAL_Can_Transmit(UDSAnswer);
  2301. }
  2302. else if (UDSSubService[i] == 0x05) //check the enviroment temp of NB
  2303. {
  2304. UINT32 temp = ((fastChargeTemp << 24) & 0xFF000000) | ((normalChargeTemp << 16) & 0xFF0000) | ((heatTemp1 << 8) & 0xFF00) | (heatTemp2 & 0xFF);
  2305. UDSPositiveAnswer(0x08, i, temp); //ntcvalue
  2306. }
  2307. else if (UDSSubService[i] == 0x06) //check the tcp link of NB
  2308. {
  2309. UDSPositiveAnswer(0x06, i, TcpSendLen); //TcpconnectStatus
  2310. }
  2311. else if (UDSSubService[i] == 0x07) //check the GPS link of NB
  2312. {
  2313. posCode = GpsFlag;
  2314. UDSPositiveAnswer(0x05, i, posCode); //gps satellite num(uint8), should be modified
  2315. }
  2316. else if (UDSSubService[i] == 0x0E) //read ram data 22 0E xx xx xx xx XX XX
  2317. {
  2318. // UINT16 size = CanRxMsg[i].Data[7] | (CanRxMsg[i].Data[6] << 8);
  2319. // UINT32 retData = 0xFFFFFFFF;
  2320. // if (size == 1)
  2321. // {
  2322. // UINT8 *addr = (UINT8 *)(CanRxMsg[i].Data[5] | (CanRxMsg[i].Data[4] << 8) | (CanRxMsg[i].Data[3] << 16) | (CanRxMsg[i].Data[2] << 24));
  2323. // retData = *(addr);
  2324. // }
  2325. // else if (size == 2)
  2326. // {
  2327. // UINT16 *addr = (UINT16 *)(CanRxMsg[i].Data[5] | (CanRxMsg[i].Data[4] << 8) | (CanRxMsg[i].Data[3] << 16) | (CanRxMsg[i].Data[2] << 24));
  2328. // retData = *(addr);
  2329. // }
  2330. // else if (size == 4)
  2331. // {
  2332. // UINT32 *addr = (UINT32 *)(CanRxMsg[i].Data[5] | (CanRxMsg[i].Data[4] << 8) | (CanRxMsg[i].Data[3] << 16) | (CanRxMsg[i].Data[2] << 24));
  2333. // retData = *(addr);
  2334. // }
  2335. // else if (size == 8)
  2336. // {
  2337. // //double
  2338. // UINT32 *addr = (UINT32 *)(CanRxMsg[i].Data[5] | (CanRxMsg[i].Data[4] << 8) | (CanRxMsg[i].Data[3] << 16) | (CanRxMsg[i].Data[2] << 24));
  2339. // retData = *(addr);
  2340. // UDSPositiveAnswer(8, i, retData); //
  2341. // UINT32 *addr1 = *(addr+4);
  2342. // retData = *(addr1);
  2343. // UDSPositiveAnswer(8, i, retData);
  2344. // }
  2345. // UDSPositiveAnswer(8, i, retData); // 08 78 22 0E xx xx xx xx
  2346. UINT16 size = CanRxMsg[i].Data[7] | (CanRxMsg[i].Data[6] << 8);
  2347. UINT8 *addr = (UINT8 *)(CanRxMsg[i].Data[5] | (CanRxMsg[i].Data[4] << 8) | (CanRxMsg[i].Data[3] << 16) | (CanRxMsg[i].Data[2] << 24));
  2348. for (UINT8 j = 0; j < size; j++)
  2349. {
  2350. UINT32 ret = 0xFF;
  2351. ret = (j << 16) | (*(addr + j));
  2352. UDSPositiveAnswer(8, i, ret);
  2353. }
  2354. }
  2355. else
  2356. {
  2357. UDSNegtiveAnswer(0x05, i, 0xEE);
  2358. }
  2359. }
  2360. else //the service is not surpported in current dialog mode
  2361. {
  2362. UDSNegtiveAnswer(0x05, i, 0xFF);
  2363. }
  2364. break;
  2365. case 0x2E: //write service
  2366. if (UDSDialogMode == 2)
  2367. {
  2368. if (UDSSubService[i] == 0x03) // write the battSN
  2369. {
  2370. if (UDSSubServiceActionCode[i] == 0x00 && snFlag == 0x00)
  2371. {
  2372. for (j = 0; j < 5; j++)
  2373. {
  2374. UDSBattSN[j + 5 * 0] = CanRxMsg[i].Data[j + 3];
  2375. }
  2376. snFlag = snFlag | 0x01;
  2377. }
  2378. else if (UDSSubServiceActionCode[i] == 0x01 && snFlag == 0x01)
  2379. {
  2380. for (j = 0; j < 5; j++)
  2381. {
  2382. UDSBattSN[j + 5 * 1] = CanRxMsg[i].Data[j + 3];
  2383. }
  2384. snFlag = snFlag | 0x02;
  2385. }
  2386. else if (UDSSubServiceActionCode[i] == 0x02 && snFlag == 0x03)
  2387. {
  2388. for (j = 0; j < 5; j++)
  2389. {
  2390. UDSBattSN[j + 5 * 2] = CanRxMsg[i].Data[j + 3];
  2391. }
  2392. snFlag = snFlag | 0x04;
  2393. }
  2394. else if (UDSSubServiceActionCode[i] == 0x03 && snFlag == 0x07)
  2395. {
  2396. for (j = 0; j < 2; j++)
  2397. {
  2398. UDSBattSN[j + 5 * 3] = CanRxMsg[i].Data[j + 3];
  2399. }
  2400. snFlag = snFlag | 0x08;
  2401. }
  2402. if (snFlag == 0x0F)
  2403. {
  2404. snFlag = 0;
  2405. UDSPositiveAnswer(0x04, i, 00);
  2406. MEMCPY(AppNVMData.battSN, UDSBattSN, BATT_SN_LEN);
  2407. AppNVMData.EOLState = 1; //SN号写入完成,表明已经进行过下线配置
  2408. SaveAppConfig();
  2409. }
  2410. else
  2411. {
  2412. UDSAnswer.Id = 0x7A8;
  2413. UDSAnswer.Data[0] = 0x05;
  2414. UDSAnswer.Data[1] = 0x3E;
  2415. UDSAnswer.Data[2] = UDSService[i];
  2416. UDSAnswer.Data[3] = UDSSubService[i];
  2417. UDSAnswer.Data[4] = snFlag;
  2418. ret = HAL_Can_Transmit(UDSAnswer);
  2419. }
  2420. }
  2421. else if (UDSSubService[i] == 0x0F) //write the update config:updateDifferDataByteLen
  2422. {
  2423. updateDifferDataByteLen = (CanRxMsg[i].Data[2] << 16) | (CanRxMsg[i].Data[3] << 8) | CanRxMsg[i].Data[4];
  2424. UDSPositiveAnswer(0x04, i, 00);
  2425. }
  2426. else if (UDSSubService[i] == 0x0E) //write ram data 2E 0E xx xx xx xx XX XX
  2427. {
  2428. UINT8 *addr = (UINT8 *)(CanRxMsg[i].Data[5] | (CanRxMsg[i].Data[4] << 8) | (CanRxMsg[i].Data[3] << 16) | (CanRxMsg[i].Data[2] << 24));
  2429. UINT8 data = CanRxMsg[i].Data[7];
  2430. *(addr) = data;
  2431. UINT8 retData = *(addr);
  2432. UDSPositiveAnswer(8, i, retData); // 08 78 2E 0E xx xx xx xx
  2433. }
  2434. else
  2435. {
  2436. UDSNegtiveAnswer(0x05, i, 0xEE);
  2437. }
  2438. break;
  2439. }
  2440. else
  2441. {
  2442. UDSNegtiveAnswer(0x05, i, 0xFF);
  2443. }
  2444. break;
  2445. case 0x31: //clear the flash service and control heat or relay
  2446. if (UDSDialogMode == 3)
  2447. {
  2448. if (UDSSubService[i] == 0x0F) //clear the fota flash
  2449. {
  2450. if (UDSSubServiceActionCode[i] != 0x00 && UDSSubServiceActionCode[i] != 0xFF)
  2451. {
  2452. //Clear the Flash
  2453. controllerFlag = UDSSubServiceActionCode[i];
  2454. boolRet = UDSClearFotaDownloadRegion(controllerFlag);
  2455. //if ok
  2456. UDSPositiveAnswer(0x05, i, (UINT32)boolRet);
  2457. }
  2458. else
  2459. {
  2460. UDSNegtiveAnswer(0x06, i, UDSSubServiceActionCode[i] << 8 | 0xFF); //重点测试
  2461. }
  2462. }
  2463. else if (UDSSubService[i] == 0x11) //control the heater 31 10 00/01/02
  2464. {
  2465. if (UDSSubServiceActionCode[i] <= 0x02) //case 00/01/02
  2466. {
  2467. HeatForceControl = UDSSubServiceActionCode[i];
  2468. UDSPositiveAnswer(0x05, i, HeatForceControl);
  2469. }
  2470. else
  2471. {
  2472. UDSNegtiveAnswer(0x05, i, UDSSubServiceActionCode[i]);
  2473. }
  2474. }
  2475. else if (UDSSubService[i] == 0x10) //control the main relay 31 11 00/01/02
  2476. {
  2477. if (UDSSubServiceActionCode[i] <= 0x02) //case 00/01/02
  2478. {
  2479. RelayForceControl = UDSSubServiceActionCode[i];
  2480. UDSPositiveAnswer(0x05, i, RelayForceControl);
  2481. }
  2482. else
  2483. {
  2484. UDSNegtiveAnswer(0x05, i, UDSSubServiceActionCode[i]);
  2485. }
  2486. }
  2487. else
  2488. {
  2489. UDSNegtiveAnswer(0x05, i, 0xEE); //the subservice is not surpported
  2490. }
  2491. }
  2492. else
  2493. {
  2494. UDSNegtiveAnswer(0x05, i, 0xFF); //the survie is not surpported in current dialog mode
  2495. }
  2496. break;
  2497. case 0x34: //prepare for some one process
  2498. if (UDSDialogMode == 3)
  2499. {
  2500. if (UDSSubService[i] == 0x0F) //ask for download update data
  2501. {
  2502. boolRet = UDSAskforDownLoadData();
  2503. //if ok
  2504. UDSPositiveAnswer(0x05, i, (UINT32)boolRet);
  2505. }
  2506. else
  2507. {
  2508. UDSNegtiveAnswer(0x05, i, 0xEE);
  2509. }
  2510. }
  2511. else
  2512. {
  2513. UDSNegtiveAnswer(0x05, i, 0xFF);
  2514. }
  2515. break;
  2516. case 0x36: //download the update data
  2517. if (UDSDialogMode == 3)
  2518. {
  2519. if (downloadReady == TRUE)
  2520. {
  2521. counter++; //记录报文数量
  2522. tempSliceCounter = (CanRxMsg[i].Data[1] << 16) | (CanRxMsg[i].Data[2] << 8) | (CanRxMsg[i].Data[3]);
  2523. /*
  2524. #ifdef USING_PRINTF
  2525. printf("tempSliceCounter = %x\n",tempSliceCounter);
  2526. #endif
  2527. */
  2528. if (tempSliceCounter < (updateDifferDataPackageCounter + 1) * 25 && tempSliceCounter >= updateDifferDataPackageCounter * 25)
  2529. {
  2530. updateDifferDataSliceCounter = tempSliceCounter % 25;
  2531. for (j = 0; j < 4; j++)
  2532. {
  2533. updateDataBuffer[updateDifferDataSliceCounter * 4 + j] = CanRxMsg[i].Data[4 + j];
  2534. }
  2535. if (updateDifferDataSliceCounter > 0)
  2536. {
  2537. sliceCounterFlag = sliceCounterFlag | (0x01 << updateDifferDataSliceCounter);
  2538. }
  2539. else
  2540. {
  2541. sliceCounterFlag = sliceCounterFlag | 0x01;
  2542. }
  2543. /*
  2544. #ifdef USING_PRINTF
  2545. printf("sliceCounterFlag = %x counter=%d\n",sliceCounterFlag,counter);
  2546. #endif
  2547. */
  2548. }
  2549. else
  2550. {
  2551. counter--;
  2552. }
  2553. if (counter == 25)
  2554. {
  2555. counter = 0;
  2556. if (sliceCounterFlag == 0x1FFFFFF) //received all the 25 message of current package
  2557. {
  2558. //write the buffer(100 byte) to flash
  2559. if (controllerFlag == 0x01)
  2560. {
  2561. flashStartAddr = FLASH_FOTA_REGION_START;
  2562. }
  2563. else if (controllerFlag == 0x02 || controllerFlag == 0x03)
  2564. {
  2565. flashStartAddr = FLASH_BMS_FOTA_START_ADDR;
  2566. }
  2567. else
  2568. {
  2569. UDSNegtiveAnswer(0x08, i, 0xFFFFFFFF);
  2570. return;
  2571. }
  2572. ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer, flashStartAddr + (updateDifferDataPackageCounter)*100, 100);
  2573. errorCount = 0;
  2574. while (ret != QSPI_OK && errorCount < 3) //try to write most 3 times
  2575. {
  2576. errorCount++;
  2577. BSP_QSPI_Erase_Safe(flashStartAddr + (updateDifferDataPackageCounter)*100, 100);
  2578. ret = (uint8_t)BSP_QSPI_Write_Safe(updateDataBuffer, flashStartAddr + (updateDifferDataPackageCounter)*100, 100);
  2579. }
  2580. if (ret == QSPI_OK) // write successed
  2581. {
  2582. UDSPositiveAnswer(0x08, i, tempSliceCounter);
  2583. memset(updateDataBuffer, 0, 100);
  2584. sliceCounterFlag = 0x0;
  2585. updateDifferDataPackageCounter++;
  2586. }
  2587. else //write fail
  2588. {
  2589. UDSNegtiveAnswer(0x08, i, 0xFFFFFFFF); //failed to write this package to flash
  2590. }
  2591. }
  2592. else
  2593. {
  2594. //received 25 messages, but lose one or more messages
  2595. //in this case, updater will try to send the package again(most 4 times)
  2596. UDSNegtiveAnswer(0x08, i, sliceCounterFlag);
  2597. }
  2598. }
  2599. }
  2600. else
  2601. {
  2602. UDSNegtiveAnswer(0x05, i, 0xEF); //the download process is not ready
  2603. counter = 0;
  2604. }
  2605. }
  2606. else
  2607. {
  2608. UDSNegtiveAnswer(0x05, i, 0xFF); //the service is not surpported in current dialog mode
  2609. }
  2610. break;
  2611. case 0x37: //exit some one process
  2612. if (UDSDialogMode == 3)
  2613. {
  2614. if (UDSSubService[i] == 0x0F) // exit the download
  2615. {
  2616. if (UDSSubServiceActionCode[i] == controllerFlag)
  2617. {
  2618. if (controllerFlag == 0x02 || controllerFlag == 0x03) //update bms
  2619. {
  2620. BMS_Fota_update_flag = true;
  2621. }
  2622. downloadReady = FALSE;
  2623. UDSPositiveAnswer(0x04, i, UDSSubServiceActionCode[i]);
  2624. }
  2625. else
  2626. {
  2627. UDSNegtiveAnswer(0x05, i, 0xEE);
  2628. }
  2629. }
  2630. else
  2631. {
  2632. UDSNegtiveAnswer(0x05, i, 0xEE);
  2633. }
  2634. }
  2635. else
  2636. {
  2637. UDSNegtiveAnswer(0x05, i, 0xFF);
  2638. }
  2639. break;
  2640. default:
  2641. UDSNegtiveAnswer(0x04, i, 0xFF); //the service is not surpported
  2642. break;
  2643. }
  2644. }
  2645. }
  2646. for (i = 0; i < 2; i++)
  2647. {
  2648. UDSService[i] = 0;
  2649. UDSSubService[i] = 0;
  2650. }
  2651. }
  2652. /**
  2653. * @brief : UDS肯定响应,发送肯定响应报文
  2654. * @param {UINT8} answerLen
  2655. * @param {UINT8} messageIndex
  2656. * @param {UINT32} posCode
  2657. * @return {*}
  2658. */
  2659. UINT8 UDSPositiveAnswer(UINT8 answerLen, UINT8 messageIndex, UINT32 posCode)
  2660. {
  2661. CAN_Msg_Type UDSAnswer;
  2662. UINT8 ret;
  2663. UDSAnswer.Id = 0x7A8;
  2664. UDSAnswer.DLC = 8;
  2665. UDSAnswer.Data[0] = answerLen;
  2666. UDSAnswer.Data[1] = 0x78;
  2667. UDSAnswer.Data[2] = UDSService[messageIndex];
  2668. UDSAnswer.Data[3] = UDSSubService[messageIndex];
  2669. if (answerLen == 4)
  2670. {
  2671. UDSAnswer.Data[4] = 0x00;
  2672. UDSAnswer.Data[5] = 0x00;
  2673. UDSAnswer.Data[6] = 0x00;
  2674. UDSAnswer.Data[7] = 0x00;
  2675. }
  2676. else if (answerLen == 5)
  2677. {
  2678. UDSAnswer.Data[4] = posCode;
  2679. UDSAnswer.Data[5] = 0x00;
  2680. UDSAnswer.Data[6] = 0x00;
  2681. UDSAnswer.Data[7] = 0x00;
  2682. }
  2683. else if (answerLen == 6)
  2684. {
  2685. UDSAnswer.Data[4] = posCode >> 8;
  2686. UDSAnswer.Data[5] = posCode;
  2687. UDSAnswer.Data[6] = 0x00;
  2688. UDSAnswer.Data[7] = 0x00;
  2689. }
  2690. else if (answerLen == 7)
  2691. {
  2692. UDSAnswer.Data[4] = posCode >> 16;
  2693. UDSAnswer.Data[5] = posCode >> 8;
  2694. UDSAnswer.Data[6] = posCode;
  2695. UDSAnswer.Data[7] = 0x00;
  2696. }
  2697. else if (answerLen == 8)
  2698. {
  2699. UDSAnswer.Data[4] = posCode >> 24;
  2700. UDSAnswer.Data[5] = posCode >> 16;
  2701. UDSAnswer.Data[6] = posCode >> 8;
  2702. UDSAnswer.Data[7] = posCode;
  2703. }
  2704. ret = HAL_Can_Transmit(UDSAnswer);
  2705. return ret;
  2706. }
  2707. /**
  2708. * @brief : UDS否定响应,发送否定响应报文
  2709. * @param {UINT8} answerLen
  2710. * @param {UINT8} messageIndex
  2711. * @param {UINT32} negCode
  2712. * @return {*}
  2713. */
  2714. UINT8 UDSNegtiveAnswer(UINT8 answerLen, UINT8 messageIndex, UINT32 negCode)
  2715. {
  2716. CAN_Msg_Type UDSAnswer;
  2717. UINT8 ret;
  2718. UDSAnswer.Id = 0x7A8;
  2719. UDSAnswer.DLC = 8;
  2720. UDSAnswer.Data[0] = answerLen;
  2721. UDSAnswer.Data[1] = 0x7F;
  2722. UDSAnswer.Data[2] = UDSService[messageIndex];
  2723. UDSAnswer.Data[3] = UDSSubService[messageIndex];
  2724. if (answerLen == 4)
  2725. {
  2726. UDSAnswer.Data[3] = negCode;
  2727. UDSAnswer.Data[4] = 0x00;
  2728. UDSAnswer.Data[5] = 0x00;
  2729. UDSAnswer.Data[6] = 0x00;
  2730. UDSAnswer.Data[7] = 0x00;
  2731. }
  2732. if (answerLen == 5)
  2733. {
  2734. UDSAnswer.Data[4] = negCode;
  2735. UDSAnswer.Data[5] = 0x00;
  2736. UDSAnswer.Data[6] = 0x00;
  2737. UDSAnswer.Data[7] = 0x00;
  2738. }
  2739. else if (answerLen == 6)
  2740. {
  2741. UDSAnswer.Data[4] = negCode >> 8;
  2742. UDSAnswer.Data[5] = negCode;
  2743. UDSAnswer.Data[6] = 0x00;
  2744. UDSAnswer.Data[7] = 0x00;
  2745. }
  2746. else if (answerLen == 7)
  2747. {
  2748. UDSAnswer.Data[4] = negCode >> 16;
  2749. UDSAnswer.Data[5] = negCode >> 8;
  2750. UDSAnswer.Data[6] = negCode;
  2751. UDSAnswer.Data[7] = 0x00;
  2752. }
  2753. else if (answerLen == 8)
  2754. {
  2755. UDSAnswer.Data[4] = negCode >> 24;
  2756. UDSAnswer.Data[5] = negCode >> 16;
  2757. UDSAnswer.Data[6] = negCode >> 8;
  2758. UDSAnswer.Data[7] = negCode;
  2759. }
  2760. ret = HAL_Can_Transmit(UDSAnswer);
  2761. return ret;
  2762. }
  2763. /**
  2764. * @brief : 清空计数,并准备接收数据包
  2765. * @param {*}
  2766. * @return {*}
  2767. */
  2768. BOOL UDSAskforDownLoadData()
  2769. {
  2770. updateDifferDataPackageCounter = 0;
  2771. downloadReady = true;
  2772. return TRUE;
  2773. }
  2774. /**
  2775. * @brief : 清除数据接收的flash区域
  2776. * @param {UINT8} controllerFlag
  2777. * @return {*}
  2778. */
  2779. BOOL UDSClearFotaDownloadRegion(UINT8 controllerFlag)
  2780. {
  2781. UINT8 ret = FALSE;
  2782. if (controllerFlag == 0x01)
  2783. {
  2784. ret = BSP_QSPI_Erase_Safe(FLASH_FOTA_REGION_START, 0x46000); //512k-32k -200k = 280k -> 0x46000
  2785. }
  2786. else if (controllerFlag == 0x02 || controllerFlag == 0x03)
  2787. {
  2788. ret = BSP_QSPI_Erase_Safe(FLASH_BMS_FOTA_START_ADDR, FLASH_BMS_FOTA_LEN);
  2789. }
  2790. if (ret == QSPI_OK)
  2791. {
  2792. return TRUE;
  2793. }
  2794. else
  2795. {
  2796. return FALSE;
  2797. }
  2798. }