AppFuncLib.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-10-27
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \S32K146_4G\code\app\lib\AppFuncLib.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppFuncLib.h"
  11. #include "AppGlobalVar.h"
  12. #include "Hal_Fls.h"
  13. #include "AppTaskUart1.h"
  14. /**
  15. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  16. * @param {UINT16} *ErrorArray
  17. * @param {UINT8} Errorlen
  18. * @return {*}
  19. */
  20. static uint8 bcc_chk_fota(uint8 *data, uint8 length);
  21. static uint8 Fota_crc_chk(uint8 *data, uint8 length);
  22. uint16 GetErrorNum(uint16 *ErrorArray, uint8 Errorlen)
  23. {
  24. uint16 OutNum;
  25. OutNum = *(ErrorArray);
  26. for (uint8 i = 0; i < Errorlen - 1; i++)
  27. {
  28. *(ErrorArray + i) = *(ErrorArray + i + 1);
  29. if (*(ErrorArray + i + 1) == 0)
  30. break;
  31. }
  32. return OutNum;
  33. }
  34. uint8 PutErrorNum(uint16 *ErrorArray, uint8 Errorlen, uint16 ErrorNum)
  35. {
  36. for (uint8 i = 0; i < Errorlen; i++)
  37. {
  38. if (*(ErrorArray + i) == 0)
  39. {
  40. *(ErrorArray + i) = ErrorNum;
  41. return 0;
  42. }
  43. else
  44. {
  45. if (*(ErrorArray + i) == ErrorNum)
  46. {
  47. return 1;
  48. }
  49. else
  50. {
  51. continue;
  52. }
  53. }
  54. }
  55. return 2;
  56. }
  57. uint16 ATstrdel(char *str)
  58. {
  59. char *p = str;
  60. bool flag = false;
  61. while (*str)
  62. {
  63. if (*str > 0x20)
  64. {
  65. *(p) = *str;
  66. p = p + 1;
  67. flag = false;
  68. }
  69. else
  70. {
  71. if (!flag)
  72. {
  73. *(p) = ',';
  74. p = p + 1;
  75. flag = true;
  76. }
  77. }
  78. str++;
  79. }
  80. *p = '\0';
  81. return 0;
  82. }
  83. uint16 mstrlen(const char *s)
  84. {
  85. uint16 out = 0;
  86. const char *ss = s;
  87. while (*ss)
  88. ss++;
  89. out = (ss - s);
  90. return out;
  91. }
  92. int mstrncmp(const char *s1, const char *s2, int n)
  93. {
  94. const unsigned char *c1 = (const unsigned char *)s1;
  95. const unsigned char *c2 = (const unsigned char *)s2;
  96. unsigned char ch;
  97. int d = 0;
  98. while (n--)
  99. {
  100. d = (int)(ch = *c1++) - (int)*c2++;
  101. if (d || !ch)
  102. break;
  103. }
  104. return d;
  105. }
  106. unsigned char HexToChar(unsigned char bHex)
  107. {
  108. if ((bHex >= 0) && (bHex <= 9))
  109. bHex += 0x30;
  110. else if ((bHex >= 10) && (bHex <= 15)) //大写字母
  111. bHex += 0x37;
  112. else
  113. bHex = 0xff;
  114. return bHex;
  115. }
  116. unsigned char CharToHex(unsigned char bChar)
  117. {
  118. if ((bChar >= 0x30) && (bChar <= 0x39))
  119. bChar -= 0x30;
  120. else if ((bChar >= 0x41) && (bChar <= 0x46)) //大写字母
  121. bChar -= 0x37;
  122. else if ((bChar >= 0x61) && (bChar <= 0x66)) //小写字母
  123. bChar -= 0x57;
  124. else
  125. bChar = 0xff;
  126. return bChar;
  127. }
  128. uint8 AtStrCompare(const char *a, const char *b)
  129. {
  130. uint8 out = 1;
  131. while (1)
  132. {
  133. if (*a == '\0' || *b == '\0') //判断其中是否有字符串结束
  134. {
  135. if (strlen(a) == strlen(b))
  136. {
  137. out = 1;
  138. break;
  139. }
  140. else
  141. {
  142. out = 0;
  143. break;
  144. }
  145. }
  146. else
  147. {
  148. if (*a != *b)
  149. {
  150. out = 0;
  151. break;
  152. }
  153. else if (*a == '=' && *b == '=')
  154. {
  155. out = 1;
  156. break;
  157. }
  158. }
  159. a++;
  160. b++;
  161. }
  162. return out;
  163. }
  164. unsigned short CRC16_Modbus(unsigned char *pdata, int len)
  165. {
  166. unsigned short crc = 0xFFFF;
  167. int i, j;
  168. for (j = 0; j < len; j++)
  169. {
  170. crc = crc ^ pdata[j];
  171. for (i = 0; i < 8; i++)
  172. {
  173. if ((crc & 0x0001) > 0)
  174. {
  175. crc = crc >> 1;
  176. crc = crc ^ 0xa001;
  177. }
  178. else
  179. crc = crc >> 1;
  180. }
  181. }
  182. return crc;
  183. }
  184. char *Myitoa(int value, char *result, int base)
  185. {
  186. // check that the base if valid
  187. if (base < 2 || base > 36)
  188. {
  189. *result = '\0';
  190. return result;
  191. }
  192. char *ptr = result, *ptr1 = result, tmp_char;
  193. int tmp_value;
  194. do
  195. {
  196. tmp_value = value;
  197. value /= base;
  198. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  199. } while (value);
  200. // Apply negative sign
  201. if (tmp_value < 0)
  202. *ptr++ = '-';
  203. *ptr-- = '\0';
  204. while (ptr1 < ptr)
  205. {
  206. tmp_char = *ptr;
  207. *ptr-- = *ptr1;
  208. *ptr1++ = tmp_char;
  209. }
  210. return result;
  211. }
  212. /************************************************************************
  213. * @brief 整数转字符串
  214. * @param[in] num 整数
  215. * @param[out] buf 字符串
  216. * @return 返回字符串长度
  217. ************************************************************************/
  218. inline int _itoa(int num, char buf[32])
  219. {
  220. return _i2a(num, buf, 10);
  221. }
  222. /************************************************************************
  223. * @brief 整数转字符串
  224. * @param[in] num 整数
  225. * @param[out] buf 字符串
  226. * @param[in] radix 进位制整数
  227. * @return 返回字符串长度
  228. ************************************************************************/
  229. int _i2a(int num, char buf[32], int radix)
  230. {
  231. static const char s[] = "0123456789abcdef";
  232. int n = num, R = radix;
  233. char *dst = buf;
  234. if (n < 0)
  235. {
  236. *dst++ = '-';
  237. n = -n;
  238. }
  239. if (n < 10)
  240. {
  241. *dst++ = s[n];
  242. *dst = 0;
  243. }
  244. else
  245. {
  246. char tmp[32], *p = tmp;
  247. while (n)
  248. {
  249. *p++ = s[n % R];
  250. n /= R;
  251. }
  252. while (--p != tmp)
  253. *dst++ = *p;
  254. *dst++ = *tmp;
  255. *dst = 0;
  256. }
  257. return dst - buf;
  258. }
  259. /************************************************************************
  260. * @brief 浮点数转字符串
  261. * @param[in] val 浮点数
  262. * @param[out] buf 字符串
  263. * @param[in] eps 精度(小数位)
  264. * @return 返回字符串长度
  265. ************************************************************************/
  266. int _ftoa(double val, char buf[32], int eps)
  267. {
  268. double f = val;
  269. char *p = buf;
  270. if (val < 0)
  271. {
  272. *p++ = '-';
  273. f = -f;
  274. }
  275. int n = f;
  276. int len = _itoa(n, p);
  277. return len + __ftoa(f - n, p + len, eps);
  278. }
  279. /************************************************************************
  280. * @brief 浮点数转字符串:范围(-1, 1)
  281. * @param[in] val 浮点数
  282. * @param[out] buf 字符串
  283. * @param[in] eps 精度(小数位)
  284. * @return 返回字符串长度
  285. ************************************************************************/
  286. int __ftoa(double val, char buf[32], int eps)
  287. {
  288. double f = val;
  289. char *p = buf;
  290. static const char s[] = "0123456789";
  291. if (f < 0)
  292. {
  293. *p++ = '-';
  294. f = -f;
  295. }
  296. *p++ = '.';
  297. for (int i = eps + 1, n; --i; ++p, f -= n)
  298. *p = s[n = f *= 10.0];
  299. *p = 0;
  300. return p - buf;
  301. }
  302. /************************************************************************
  303. * @brief 替换sprintf
  304. * @ref 可变长参数列表误区与陷阱——va_arg不可接受的类型
  305. * http://www.cppblog.com/ownwaterloo/archive/2009/04/21/80655.aspx
  306. ************************************************************************/
  307. int _sprintf(char *dst, const char *format, ...)
  308. {
  309. char *s = dst;
  310. const char *f = format;
  311. va_list ap, another;
  312. va_start(ap, format);
  313. va_copy(another, ap);
  314. while (*f)
  315. {
  316. int n = 1;
  317. if ('%' != *f)
  318. {
  319. *s = *f;
  320. }
  321. else
  322. {
  323. ++f;
  324. switch (*f)
  325. {
  326. case 's': // 字符串
  327. {
  328. const char *p = va_arg(ap, char *);
  329. n = strlen(p);
  330. memcpy(s, p, n);
  331. }
  332. break;
  333. case 'd':
  334. case 'u': // 整数
  335. {
  336. char buf[32];
  337. n = _itoa(va_arg(ap, int), buf);
  338. memcpy(s, buf, n);
  339. }
  340. break;
  341. case 'f': // 浮点数
  342. {
  343. char buf[32];
  344. n = _ftoa(va_arg(ap, double), buf, 6);
  345. memcpy(s, buf, n);
  346. }
  347. break;
  348. case 'x': // 16进制数
  349. {
  350. char buf[32];
  351. n = _i2a(va_arg(ap, int), buf, 16);
  352. memcpy(s, buf, n);
  353. }
  354. break;
  355. case 'c': // 字符
  356. {
  357. *s = va_arg(ap, int);
  358. }
  359. break;
  360. case '%': // 百分号
  361. {
  362. *s = '%';
  363. }
  364. break;
  365. default:
  366. {
  367. va_end(ap);
  368. int x = vsprintf(dst, format, another);
  369. va_end(another);
  370. return x;
  371. }
  372. break;
  373. }
  374. }
  375. ++f;
  376. s += n;
  377. }
  378. *s = 0;
  379. va_end(ap);
  380. return s - dst;
  381. }
  382. uint8 bcc_chk(uint8 *data, uint16 length)
  383. {
  384. uint8 bcc_chk_return = 0x00;
  385. uint16 count = 0;
  386. while (count < length)
  387. {
  388. bcc_chk_return ^= data[count];
  389. count++;
  390. }
  391. return bcc_chk_return;
  392. }
  393. uint16 crc_chk(uint8 *data, uint8 length)
  394. {
  395. uint8 j;
  396. uint16 reg_crc = 0xFFFF;
  397. while (length--)
  398. {
  399. reg_crc ^= *data++;
  400. for (j = 0; j < 8; j++)
  401. {
  402. if (reg_crc & 0x01)
  403. {
  404. reg_crc = (reg_crc >> 1) ^ 0xA001;
  405. }
  406. else
  407. {
  408. reg_crc = reg_crc >> 1;
  409. }
  410. }
  411. }
  412. return reg_crc;
  413. }