| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- #include "tool.h"
- #include "includes.h"
- #define CRC16_INIT_VALUE 0x0000
- //CRC校验
- //CRC16-IBM
- uint16_t CRC16_get(uint8_t *_buff,uint32_t _len)
- {
- uint32_t i,j;
- uint16_t crc;
- uint16_t temp;
- crc=(uint16_t)CRC16_INIT_VALUE;
- for(i=0;i<_len;i++) {
- temp=_buff[i];
- temp &=0x00FF;
- crc^=temp;
- for(j=0;j<8;j++) {
- if((crc&0x0001)!=0x00) {
- crc>>=1;
- crc^=0xA001;
- } else {
- crc>>=1;
- }
- }
- }
- return crc;
- }
- //报税口明文协议校验
- uint8_t _crc_get_gw(uint8_t *data, uint8_t size)
- {
- uint8_t i, crc = 0;
-
- for(i = 0;i < size;i++){
- crc ^=data[i];
- }
-
- return crc;
- }
- //crc32校验
- uint32_t crc_block_data_calculate(uint32_t *buf, uint32_t len)
- {
- uint32_t i,data;
- for(i = 0; i < len; i++)
- {
- CRC_FeedData32(CRC0, buf[i]);
- }
- data = CRC_ReadData32(CRC0);
- return data;
- }
- //异或和计算
- uint8_t get_xor(uint8_t *buf, uint16_t len)
- {
- uint16_t i;
- uint8_t data = 0;
- for(i = 0; i < len; i++)
- {
- data ^= buf[i];
- }
- return data;
- }
- /*转义*/
- void _ytsf_data_code(uint8_t *buff, uint8_t *len)
- {
- uint8_t buff_code[128];
- uint8_t i, tlen = 0;
- /* 帧中除了帧头为 BB 外,不再出现 BB,如果出现 BB 则转义为
- BA 01;如果出现 BA 则转义为 BA 00;帧长度和校验码都按转义前
- 来计算。*/
- for(i = 0;i < *len; i ++){
- if(buff[i] == 0xba){
- buff_code[tlen++] = 0xba;
- buff_code[tlen++] = 0x00;
- } else if(i&&buff[i] == 0xbb){
- buff_code[tlen++] = 0xba;
- buff_code[tlen++] = 0x01;
- }
- else{
- buff_code[tlen++] = buff[i];
- }
- }
- memcpy(buff, buff_code, tlen);
- *len = tlen;
- }
- /*反转义*/
- void _ytsf_data_decode(uint8_t *buff, uint8_t *len)
- {
- uint8_t buff_code[128];
- uint8_t i, tlen = 0;
- /* 帧中除了帧头为 BB 外,不再出现 BB,如果出现 BB 则转义为
- BA 01;如果出现 BA 则转义为 BA 00;帧长度和校验码都按转义前
- 来计算。*/
- for(i = 0;i < *len; i ++){
- if((buff[i] == 0xba)&&(buff[i+1] == 0x00)) {
- buff_code[tlen++] = 0xba;
- i += 1;
- } else if((buff[i] == 0xba)&&(buff[i+1] == 0x01)){
- buff_code[tlen++] = 0xbb;
- i += 1;
- }
- else{
- buff_code[tlen++] = buff[i];
- }
- }
- memcpy(buff, buff_code, tlen);
- *len = tlen;
- }
- //十六进制字符串转十进制数
- long HextoDec(char *s)
- {
- int i,t;
- long sum=0;
- for(i=0;s[i];i++)
- {
- if(s[i]<='9')
- t=s[i]-'0';
- else
- t=s[i]-'A'+10;
- sum=sum*16+t;
- }
- return sum;
- }
- unsigned char HexToChar(unsigned char bChar)
- {
- if((bChar>=0x30)&&(bChar<=0x39))
- {
- bChar -= 0x30;
- }
- else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
- {
- bChar -= 0x37;
- }
- else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
- {
- bChar -= 0x57;
- }
- else
- {
- bChar = 0xff;
- }
- return bChar;
- }
- //ascii转十六进制
- int asciitohex(char *data, uint8_t *out_data, int len)
- {
- int i,slen=0;
- uint8_t temp1,temp2;
- for(i = 0; i < len; i+=2)
- {
- temp1 = HexToChar(data[i]);
- temp2 = HexToChar(data[i+1]);
- out_data[slen] = (temp1<<4) | temp2;
- slen++;
- }
- return slen;
- }
- //字符串转十六进制数
- uint32_t strtohex(char *data, uint8_t len)
- {
- uint8_t i = 0;;
- uint32_t p_data = 0;
- uint8_t temp;
- for(i = 0; i < len; i++)
- {
- temp = HexToChar(data[i]);
- if(temp == 0xff)
- break;
- p_data = (p_data<<4)|temp;
- }
- return p_data;
- }
- //ascii转bcd码
- static uint8_t ascii2bcd1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- static uint8_t ascii2bcd2[6] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
- /*
- 返回值:0 输入数据全部是字母(a~f、A~F)或数字;1 输入数据有不是字母(a~f、A~F)和数字的数
- */
- uint8_t ascii_to_bcd(uint8_t *data, uint8_t *res, uint16_t len)
- {
- uint8_t ret=0;
- uint8_t temp;
- uint8_t index;
- uint16_t i;
- if(len < 2)
- return 1;
- for(i = 0; i < len/2; i++)
- {
- //first BCD
- if(data[2*i]>='0' && data[2*i]<='9')
- {
- index = data[2*i] - '0';
- temp = ascii2bcd1[index]<<4;
- }
- else if(data[2*i]>='A' && data[2*i]<='F')
- {
- index = data[2*i] - 'A';
- temp = ascii2bcd2[index]<<4;
- }
- else if(data[2*i]>='a' && data[2*i]<='f')
- {
- index = data[2*i] - 'a';
- temp = ascii2bcd2[index]<<4;
- }
- else
- {
- temp = 0;
- ret = 1;
- }
- //second BCD
- if(data[2*i+1]>='0' && data[2*i+1]<='9')
- {
- index = data[2*i+1] - '0';
- temp |= ascii2bcd1[index];
- }
- else if(data[2*i+1]>='A' && data[2*i+1]<='F')
- {
- index = data[2*i+1] - 'A';
- temp |= ascii2bcd2[index];
- }
- else if(data[2*i+1]>='a' && data[2*i+1]<='f')
- {
- index = data[2*i+1] - 'a';
- temp |= ascii2bcd2[index];
- }
- else
- {
- temp |= 0;
- ret = 1;
- }
- res[i] = temp;
- }
- return ret;
- }
- //获取当前时间
- uint64_t get_real_time(void)
- {
- // return UTIL_GetMcycle();
- return Systickcount;
- }
- /**
- * @brief 设置定时器
- * @par param[timeout_t] *tt
- * @par param[uint32_t] val,延时ms
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- void timeout_setValue(timeout_t *tt,uint32_t val,uint8_t flg){
- tt->flag = flg;
- tt->count = get_real_time();
- tt->timeout = val;//UTIL_UsToMcycle(val);
- };
- /**
- * @brief 启动定时器
- * @par param[timeout_t] *tt
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- /**
- * @brief 启动定时器
- * @param tt 定时器指针
- * @param flg 是否开启
- */
- void timeout_start(timeout_t *tt,uint8_t flg){
- tt->flag = flg;
- if(flg){
- tt->count = get_real_time();
- }
- };
- /**
- * @brief 停止定时器
- * @param tt 定时器指针
- */
- void timeout_stop(timeout_t *tt){
- tt->flag = 0;
- };
- /**
- * @brief 返回定时器是否超时
- * @param tt 定时器指针
- * @return uint8_t 1:超时,0:未超时
- */
- uint8_t timeout_isOut(timeout_t *tt){
- volatile uint64_t time;
- if(tt->flag){
- time = get_real_time();
- if((time - tt->count) >= tt->timeout){
- tt->count = time;
- if(tt->flag == 1){
- tt->flag = 0;
- }
- return 1;
- }
- else{
- return 0;
- }
- }
- else{
- return 0;
- }
- };
- /**************************************************************************
- 以下定时器均由timer定时器驱动,Tickcount_us
- ***************************************************************************/
- /**
- * @brief 设置定时器
- * @par param[timeout_t] *tt
- * @par param[uint32_t] val,延时10us
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- void timeout_setValue_us(timeout_t *tt,uint32_t val,uint8_t flg){
- tt->flag = flg;
- tt->count = Get_Time();
- tt->timeout = val;
- };
- /**
- * @brief 启动定时器
- * @par param[timeout_t] *tt
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- /**
- * @brief 启动定时器
- * @param tt 定时器指针
- * @param flg 是否开启
- */
- void timeout_start_us(timeout_t *tt,uint8_t flg){
- tt->flag = flg;
- if(flg){
- tt->count = Get_Time();
- }
- };
- /**
- * @brief 停止定时器
- * @param tt 定时器指针
- */
- void timeout_stop_us(timeout_t *tt){
- tt->flag = 0;
- };
- /**
- * @brief 返回定时器是否超时
- * @param tt 定时器指针
- * @return uint8_t 1:超时,0:未超时
- */
- uint8_t timeout_isOut_us(timeout_t *tt){
- if(tt->flag){
- if((Get_Time() - tt->count) >= tt->timeout){
- tt->count = Get_Time();
- if(tt->flag == 1){
- tt->flag = 0;
- }
- return 1;
- }
- else{
- return 0;
- }
- }
- else{
- return 0;
- }
- };
- /**************************************************************************
- 以上定时器均由timer定时器驱动,Tickcount_us
- ***************************************************************************/
- //数组比较
- //返回,0:相同;1:不同
- uint8_t buff_compare(uint8_t *buf1, uint8_t *buf2, uint16_t len)
- {
- uint8_t ret = 0;
- uint16_t i;
- for(i = 0; i < len; i++)
- {
- if(buf1[i] != buf2[i])
- {
- ret = 1;
- break;
- }
- }
- return ret;
- }
- //设置标记高位取反
- //将一个数设置为高八位是低八位的取反
- uint16_t set_flag_not(uint16_t data)
- {
- uint16_t temp_data,ret_data = 0;
- uint16_t temp_h,temp_l;
- temp_data = data;
- temp_h = ((~temp_data)<<8)&0xff00;
- temp_l = temp_data&0x00ff;
- ret_data = temp_h | temp_l;
- return ret_data;
- }
- //标记高位取反判断
- //0 错误,1 正确
- uint8_t flag_not_cmp(uint16_t data)
- {
- uint8_t ret = 0;
- uint16_t temp_data;
- uint8_t temp_h,temp_l;
- temp_data = data;
- temp_h = (uint8_t)((temp_data&0xff00)>>8);
- temp_l = (uint8_t)(temp_data&0x00ff);
- if((temp_h^temp_l) == 0xff)
- {
- ret = 1;
- }
- return ret;
- }
|