本帖最后由 dukelec 于 2025-9-19 09:16 编辑
flash 有限的时候,譬如 bootloader 里面,我是用自己整理的 dprintf 来打印,只支持十六进制打印解析,真正的最小占用flash
但凡支持十进制打印,占用flash小不了
dprint.h 头文件:
[C] 纯文本查看 复制代码
/*
* Software License Agreement (MIT License)
*
* Copyright (c) 2017, DUKELEC, Inc.
* All rights reserved.
*
* Author: Duke Fong <[email]d@d-l.io[/email]>
*/
#ifndef __DPRINTF__
#define __DPRINTF__
#include "stdio.h"
#include "string.h"
#include "stdint.h"
#include <stddef.h>
#include "stdbool.h"
#include <stdlib.h>
#include <stdarg.h>
//void dputc(char c);
void dputs(char *str);
void dputh16(uint16_t val);
void dputh32(uint32_t val);
void _dprintf(char *fmt, ...);
#define printf _dprintf
#endif
dprintf.c 文件:
[C] 纯文本查看 复制代码
/*
* Software License Agreement (MIT License)
*
* Copyright (c) 2017, DUKELEC, Inc.
* All rights reserved.
*
* Author: Duke Fong <[email]d@d-l.io[/email]>
*/
#include "main.h"
#include "dprintf.h"
void dputs(char *str)
{
while (*str != '\0')
dputc(*str++);
}
void dputh16(uint16_t val)
{
const char tlb[] = "0123456789abcdef";
int i;
for (i = 0; i < 4; i++) {
dputc(tlb[val >> 12]);
val <<= 4;
}
}
void dputh32(uint32_t val)
{
dputh16(val >> 16);
dputh16(val & 0xffff);
}
void _dprintf(char *fmt, ...)
{
va_list va;
char ch;
va_start(va,fmt);
while ((ch = *(fmt++))) {
if (ch != '%') {
dputc(ch);
continue;
}
ch = *(fmt++);
switch (ch) {
case 'x':
dputh16(va_arg(va, uint32_t));
break;
case 'X':
dputh32(va_arg(va, uint32_t));
break;
#if 0
case 's' :
dputs(va_arg(va, char *));
break;
case '%' :
dputc('%');
default:
break;
#endif
}
}
va_end(va);
}
#if 0 打开可以额外支持 %s 字符串解析和打印 % 符号本身
|