error_t soclib_fb_init(struct device_s *fb, void *base, uint_t size, uint_t irq) { fb->base = base; fb->irq = irq; fb->type = DEV_CHR; fb->op.dev.open = &fb_open; fb->op.dev.read = &fb_read; fb->op.dev.write = &fb_write; fb->op.dev.close = NULL; fb->op.dev.lseek = &fb_lseek; fb->op.dev.mmap = &fb_mmap; fb->op.dev.munmap = &fb_munmap; fb->op.dev.set_params = NULL; fb->op.dev.get_params = &fb_get_params; fb->op.drvid = SOCLIB_FB_ID; fb->data = (void*)size; sprintk(fb->name, #if CONFIG_ROOTFS_IS_VFAT "FB%d" #else "fb%d" #endif ,fb_count++); metafs_init(&fb->node, fb->name); return 0; }
void hexdump(void *data, int len) { int i = 0; int index = 0; unsigned int base = 0; unsigned char *p = data; unsigned char hex[16 * 3 + 1] = {0, }; unsigned char text[16 + 1] = {0, }; unsigned char c; for (i = 0; i < len; i++) { index = i & 0xf; if (i && index == 0) { /* print the buffer before reset it */ printk("%08x %-48s |%-16s|\n", base, hex, text); base += 0x10; memset(hex, 0, sizeof hex); memset(text, 0, sizeof text); } c = *p++; sprintk((char *)&hex[index * 3], "%02x ", c); if (c < 0x20 || c > 0x7f) text[index] = '.'; else text[index] = c; } /* print the last part */ printk("%08x %-48s |%-16s|\n", base, hex, text); }
error_t soclib_timer_init(struct device_s *timer) { timer->type = DEV_INTERNAL; timer->action.dev = timer; timer->action.irq_handler = &timer_irq_handler; timer->action.data = NULL; sprintk(timer->name, "timer%d", timer_count++); metafs_init(&timer->node, timer->name); timer->op.timer.run = &timer_run; timer->op.timer.stop = &timer_stop; timer->op.timer.set_period = &timer_set_period; timer->op.timer.get_value = &timer_get_value; timer->op.drvid = SOCLIB_TIMER_ID; timer->data = NULL; return 0; }
int printf(char *fmt , ...){ char str[50]; int index_f = 0; //跟随fmt赋值入str int index_s = 0; //跟随着str的位置标示 int insert = 0; //当有参数比如整数出现时转化为字符串,并记录插入的字符个数 int start = 0; int *p = &fmt; p += 1; while(1){ if(*(fmt + index_f) == 0x00){ str[index_s] = 0x00; sprintk(str + start); break; } if(*(fmt + index_f) == '%'){ index_f++; switch(*(fmt + index_f)){ case 'd': insert = itoa(*p , str + index_s); //将参数(整形)转化为字符串,并放入以str+index_s开头的串中 index_s += insert; //index_s需要加上插入的字符串个数insert p += 1; //p指向下一个参数地址 index_f++; //index_f指向fmt的下一个字符 break; case 'c': str[index_s] = (char)*p; p +=1; index_f++; index_s++; break; case 's': insert = strncpy(str + index_s , (char *)*p , 0); //*p为参数字符串的首地址 index_s += insert; p += 1; index_f++; break; default: break; } continue; } if(*(fmt + index_f) == '/'){ index_f++; switch(*(fmt + index_f)){ case 'n': str[index_s] = 0x00; //出现转义字符时,输出之前的部分 sprintk(str + start); index_s++; //重新设置index_s为输出后的部分 start = index_s; //start也要做相应设定 print_ctrl(); //输出回车 index_f++; break; case 't': break; default: break; } continue; } str[index_s] = *(fmt + index_f); index_f++; index_s++; } return index_s; }