常用小函数

打印功能

print_int(“value:”,12344,‘D’);

void print_int(char *str, unsigned int ivalue, char format)
{
	char buf[11] = {0};
	unsigned int range ,i ,temp_val;
	
	printf("%s", str);
	if (ivalue == 0) {
		printf("0x00\n");
		return;
	}
	switch (format) {
	case 'D' :
		 range = 1000000000;
		i = 0;
		for(;;)
		{
			temp_val = ivalue/ range;
			if (temp_val != 0) {
				break;
			}
			ivalue = ivalue % range;
			range = range / 10;
		}
		for(;;)
		{
			temp_val = ivalue/ range;
			buf[i++] = temp_val + '0';
			if (range == 1) {
				break;
			}
			ivalue = ivalue % range;
			range = range / 10;
		}
		break;
	case 'X' :
	    range = 0x10000000;
		i = 0;
		for(;;)
		{
			temp_val = ivalue/ range;
			if (temp_val != 0) {
				break;
			}
			ivalue = ivalue % range;
			range = range / 16;
			
		}
		for(;;)
		{
			temp_val = ivalue/ range;
			if (temp_val > 9) {
				buf[i++] = temp_val % 10 + 'A';
			} else {
				buf[i++] = temp_val + '0';
			}
			if (range == 1) {
				break;
			}
			ivalue = ivalue % range;
			range = range / 16;
			
		}
		printf("0X");
		break;
	default:
		printf("errp\n");
	}
	printf("%s\n", buf);
}

mac 字符和数据之间的转化

static inline void mac2str(char *dest, uint8_t dest_size, uint8_t *mac)
{
	snprintf(dest, dest_size, "%02x%02x%02x%02x%02x%02x", 
							mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
static inline void str2mac(uint8_t *mac_str, uint8_t *mac)
{
	uint32_t _mac[BLE_MAC_LEN] = {0};
	int i = 0;
	sscanf(mac_str, "%2x%2x%2x%2x%2x%2x", 
			&_mac[0], &_mac[1],&_mac[2], &_mac[3], &_mac[4], &_mac[5]); 
	for (i = 0; i < 6; i++)
	{
		mac[i] = _mac[i];
	}
}

hex转字符

//16个char 数据变字符就变32个字符了
void arrtostr(char *str,char *arr,int arrlen){
	int i=0;
	for(i=0;i<arrlen;i++){
		str[i*2]=(arr[i]>>4)+((arr[i]>>4)>9 ? 'W':'0');//'7'+10=刚好等于字母‘A’-- 'W' -- a
		str[i*2+1]=(arr[i]&0x0f)+(((arr[i])&0x0f)>9 ? 'W':'0');
	}
	str[arrlen*2]='\0';
	MY_DEBUG("zhuan: %s\r\n",str);
}

unsigned char decrypt[16]; 
unsigned char dexrypestr[33];	
arrtostr((char*)dexrypestr,(char*)decrypt,16);

定义内核的寄存器结构

通过union访问其比特位
struct pwm_reg_info {
	union{
		unsigned int pwm_ctr;
		struct {
			unsigned int cycle:16;
			unsigned int type:1;
			unsigned int ms_dir:1;
			unsigned int reserve1:2;
			unsigned int ms_threshold_en:1;
			unsigned int reserve2:3;
			unsigned int ms_threshold_val:7;
			unsigned int reserve3:1;
		}ctr_bit;
	};
	unsigned int pwm_period;
};
struct pwm_info {
	unsigned int  __iomem *pwm_reg_base;
	struct pwm_reg_info *pwm_n_reg;
	
};

checksum

unsigned int check_byte_sum(char *buf, int len)
{
   unsigned int sum = 0;
   int i;

   for(i=0; i<len; i++)
   {
   	sum += ((unsigned char)buf[i]) & 0xff;
   }

   return sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值