tool.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include "tool.h"
  2. #include "includes.h"
  3. #define CRC16_INIT_VALUE 0x0000
  4. //CRC校验
  5. //CRC16-IBM
  6. uint16_t CRC16_get(uint8_t *_buff,uint32_t _len)
  7. {
  8. uint32_t i,j;
  9. uint16_t crc;
  10. uint16_t temp;
  11. crc=(uint16_t)CRC16_INIT_VALUE;
  12. for(i=0;i<_len;i++) {
  13. temp=_buff[i];
  14. temp &=0x00FF;
  15. crc^=temp;
  16. for(j=0;j<8;j++) {
  17. if((crc&0x0001)!=0x00) {
  18. crc>>=1;
  19. crc^=0xA001;
  20. } else {
  21. crc>>=1;
  22. }
  23. }
  24. }
  25. return crc;
  26. }
  27. //报税口明文协议校验
  28. uint8_t _crc_get_gw(uint8_t *data, uint8_t size)
  29. {
  30. uint8_t i, crc = 0;
  31. for(i = 0;i < size;i++){
  32. crc ^=data[i];
  33. }
  34. return crc;
  35. }
  36. //crc32校验
  37. uint32_t crc_block_data_calculate(uint32_t *buf, uint32_t len)
  38. {
  39. uint32_t i,data;
  40. for(i = 0; i < len; i++)
  41. {
  42. CRC_FeedData32(CRC0, buf[i]);
  43. }
  44. data = CRC_ReadData32(CRC0);
  45. return data;
  46. }
  47. //异或和计算
  48. uint8_t get_xor(uint8_t *buf, uint16_t len)
  49. {
  50. uint16_t i;
  51. uint8_t data = 0;
  52. for(i = 0; i < len; i++)
  53. {
  54. data ^= buf[i];
  55. }
  56. return data;
  57. }
  58. /*转义*/
  59. void _ytsf_data_code(uint8_t *buff, uint8_t *len)
  60. {
  61. uint8_t buff_code[128];
  62. uint8_t i, tlen = 0;
  63. /* 帧中除了帧头为 BB 外,不再出现 BB,如果出现 BB 则转义为
  64. BA 01;如果出现 BA 则转义为 BA 00;帧长度和校验码都按转义前
  65. 来计算。*/
  66. for(i = 0;i < *len; i ++){
  67. if(buff[i] == 0xba){
  68. buff_code[tlen++] = 0xba;
  69. buff_code[tlen++] = 0x00;
  70. } else if(i&&buff[i] == 0xbb){
  71. buff_code[tlen++] = 0xba;
  72. buff_code[tlen++] = 0x01;
  73. }
  74. else{
  75. buff_code[tlen++] = buff[i];
  76. }
  77. }
  78. memcpy(buff, buff_code, tlen);
  79. *len = tlen;
  80. }
  81. /*反转义*/
  82. void _ytsf_data_decode(uint8_t *buff, uint8_t *len)
  83. {
  84. uint8_t buff_code[128];
  85. uint8_t i, tlen = 0;
  86. /* 帧中除了帧头为 BB 外,不再出现 BB,如果出现 BB 则转义为
  87. BA 01;如果出现 BA 则转义为 BA 00;帧长度和校验码都按转义前
  88. 来计算。*/
  89. for(i = 0;i < *len; i ++){
  90. if((buff[i] == 0xba)&&(buff[i+1] == 0x00)) {
  91. buff_code[tlen++] = 0xba;
  92. i += 1;
  93. } else if((buff[i] == 0xba)&&(buff[i+1] == 0x01)){
  94. buff_code[tlen++] = 0xbb;
  95. i += 1;
  96. }
  97. else{
  98. buff_code[tlen++] = buff[i];
  99. }
  100. }
  101. memcpy(buff, buff_code, tlen);
  102. *len = tlen;
  103. }
  104. //十六进制字符串转十进制数
  105. long HextoDec(char *s)
  106. {
  107. int i,t;
  108. long sum=0;
  109. for(i=0;s[i];i++)
  110. {
  111. if(s[i]<='9')
  112. t=s[i]-'0';
  113. else
  114. t=s[i]-'A'+10;
  115. sum=sum*16+t;
  116. }
  117. return sum;
  118. }
  119. unsigned char HexToChar(unsigned char bChar)
  120. {
  121. if((bChar>=0x30)&&(bChar<=0x39))
  122. {
  123. bChar -= 0x30;
  124. }
  125. else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
  126. {
  127. bChar -= 0x37;
  128. }
  129. else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
  130. {
  131. bChar -= 0x57;
  132. }
  133. else
  134. {
  135. bChar = 0xff;
  136. }
  137. return bChar;
  138. }
  139. //ascii转十六进制
  140. int asciitohex(char *data, uint8_t *out_data, int len)
  141. {
  142. int i,slen=0;
  143. uint8_t temp1,temp2;
  144. for(i = 0; i < len; i+=2)
  145. {
  146. temp1 = HexToChar(data[i]);
  147. temp2 = HexToChar(data[i+1]);
  148. out_data[slen] = (temp1<<4) | temp2;
  149. slen++;
  150. }
  151. return slen;
  152. }
  153. //字符串转十六进制数
  154. uint32_t strtohex(char *data, uint8_t len)
  155. {
  156. uint8_t i = 0;;
  157. uint32_t p_data = 0;
  158. uint8_t temp;
  159. for(i = 0; i < len; i++)
  160. {
  161. temp = HexToChar(data[i]);
  162. if(temp == 0xff)
  163. break;
  164. p_data = (p_data<<4)|temp;
  165. }
  166. return p_data;
  167. }
  168. //ascii转bcd码
  169. static uint8_t ascii2bcd1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  170. static uint8_t ascii2bcd2[6] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  171. /*
  172. 返回值:0 输入数据全部是字母(a~f、A~F)或数字;1 输入数据有不是字母(a~f、A~F)和数字的数
  173. */
  174. uint8_t ascii_to_bcd(uint8_t *data, uint8_t *res, uint16_t len)
  175. {
  176. uint8_t ret=0;
  177. uint8_t temp;
  178. uint8_t index;
  179. uint16_t i;
  180. if(len < 2)
  181. return 1;
  182. for(i = 0; i < len/2; i++)
  183. {
  184. //first BCD
  185. if(data[2*i]>='0' && data[2*i]<='9')
  186. {
  187. index = data[2*i] - '0';
  188. temp = ascii2bcd1[index]<<4;
  189. }
  190. else if(data[2*i]>='A' && data[2*i]<='F')
  191. {
  192. index = data[2*i] - 'A';
  193. temp = ascii2bcd2[index]<<4;
  194. }
  195. else if(data[2*i]>='a' && data[2*i]<='f')
  196. {
  197. index = data[2*i] - 'a';
  198. temp = ascii2bcd2[index]<<4;
  199. }
  200. else
  201. {
  202. temp = 0;
  203. ret = 1;
  204. }
  205. //second BCD
  206. if(data[2*i+1]>='0' && data[2*i+1]<='9')
  207. {
  208. index = data[2*i+1] - '0';
  209. temp |= ascii2bcd1[index];
  210. }
  211. else if(data[2*i+1]>='A' && data[2*i+1]<='F')
  212. {
  213. index = data[2*i+1] - 'A';
  214. temp |= ascii2bcd2[index];
  215. }
  216. else if(data[2*i+1]>='a' && data[2*i+1]<='f')
  217. {
  218. index = data[2*i+1] - 'a';
  219. temp |= ascii2bcd2[index];
  220. }
  221. else
  222. {
  223. temp |= 0;
  224. ret = 1;
  225. }
  226. res[i] = temp;
  227. }
  228. return ret;
  229. }
  230. //获取当前时间
  231. uint64_t get_real_time(void)
  232. {
  233. // return UTIL_GetMcycle();
  234. return Systickcount;
  235. }
  236. /**
  237. * @brief 设置定时器
  238. * @par param[timeout_t] *tt
  239. * @par param[uint32_t] val,延时ms
  240. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  241. */
  242. void timeout_setValue(timeout_t *tt,uint32_t val,uint8_t flg){
  243. tt->flag = flg;
  244. tt->count = get_real_time();
  245. tt->timeout = val;//UTIL_UsToMcycle(val);
  246. };
  247. /**
  248. * @brief 启动定时器
  249. * @par param[timeout_t] *tt
  250. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  251. */
  252. /**
  253. * @brief 启动定时器
  254. * @param tt 定时器指针
  255. * @param flg 是否开启
  256. */
  257. void timeout_start(timeout_t *tt,uint8_t flg){
  258. tt->flag = flg;
  259. if(flg){
  260. tt->count = get_real_time();
  261. }
  262. };
  263. /**
  264. * @brief 停止定时器
  265. * @param tt 定时器指针
  266. */
  267. void timeout_stop(timeout_t *tt){
  268. tt->flag = 0;
  269. };
  270. /**
  271. * @brief 返回定时器是否超时
  272. * @param tt 定时器指针
  273. * @return uint8_t 1:超时,0:未超时
  274. */
  275. uint8_t timeout_isOut(timeout_t *tt){
  276. volatile uint64_t time;
  277. if(tt->flag){
  278. time = get_real_time();
  279. if((time - tt->count) >= tt->timeout){
  280. tt->count = time;
  281. if(tt->flag == 1){
  282. tt->flag = 0;
  283. }
  284. return 1;
  285. }
  286. else{
  287. return 0;
  288. }
  289. }
  290. else{
  291. return 0;
  292. }
  293. };
  294. /**************************************************************************
  295. 以下定时器均由timer定时器驱动,Tickcount_us
  296. ***************************************************************************/
  297. /**
  298. * @brief 设置定时器
  299. * @par param[timeout_t] *tt
  300. * @par param[uint32_t] val,延时10us
  301. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  302. */
  303. void timeout_setValue_us(timeout_t *tt,uint32_t val,uint8_t flg){
  304. tt->flag = flg;
  305. tt->count = Get_Time();
  306. tt->timeout = val;
  307. };
  308. /**
  309. * @brief 启动定时器
  310. * @par param[timeout_t] *tt
  311. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  312. */
  313. /**
  314. * @brief 启动定时器
  315. * @param tt 定时器指针
  316. * @param flg 是否开启
  317. */
  318. void timeout_start_us(timeout_t *tt,uint8_t flg){
  319. tt->flag = flg;
  320. if(flg){
  321. tt->count = Get_Time();
  322. }
  323. };
  324. /**
  325. * @brief 停止定时器
  326. * @param tt 定时器指针
  327. */
  328. void timeout_stop_us(timeout_t *tt){
  329. tt->flag = 0;
  330. };
  331. /**
  332. * @brief 返回定时器是否超时
  333. * @param tt 定时器指针
  334. * @return uint8_t 1:超时,0:未超时
  335. */
  336. uint8_t timeout_isOut_us(timeout_t *tt){
  337. if(tt->flag){
  338. if((Get_Time() - tt->count) >= tt->timeout){
  339. tt->count = Get_Time();
  340. if(tt->flag == 1){
  341. tt->flag = 0;
  342. }
  343. return 1;
  344. }
  345. else{
  346. return 0;
  347. }
  348. }
  349. else{
  350. return 0;
  351. }
  352. };
  353. /**************************************************************************
  354. 以上定时器均由timer定时器驱动,Tickcount_us
  355. ***************************************************************************/
  356. //数组比较
  357. //返回,0:相同;1:不同
  358. uint8_t buff_compare(uint8_t *buf1, uint8_t *buf2, uint16_t len)
  359. {
  360. uint8_t ret = 0;
  361. uint16_t i;
  362. for(i = 0; i < len; i++)
  363. {
  364. if(buf1[i] != buf2[i])
  365. {
  366. ret = 1;
  367. break;
  368. }
  369. }
  370. return ret;
  371. }
  372. //设置标记高位取反
  373. //将一个数设置为高八位是低八位的取反
  374. uint16_t set_flag_not(uint16_t data)
  375. {
  376. uint16_t temp_data,ret_data = 0;
  377. uint16_t temp_h,temp_l;
  378. temp_data = data;
  379. temp_h = ((~temp_data)<<8)&0xff00;
  380. temp_l = temp_data&0x00ff;
  381. ret_data = temp_h | temp_l;
  382. return ret_data;
  383. }
  384. //标记高位取反判断
  385. //0 错误,1 正确
  386. uint8_t flag_not_cmp(uint16_t data)
  387. {
  388. uint8_t ret = 0;
  389. uint16_t temp_data;
  390. uint8_t temp_h,temp_l;
  391. temp_data = data;
  392. temp_h = (uint8_t)((temp_data&0xff00)>>8);
  393. temp_l = (uint8_t)(temp_data&0x00ff);
  394. if((temp_h^temp_l) == 0xff)
  395. {
  396. ret = 1;
  397. }
  398. return ret;
  399. }