打印功能
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;
}