在最开始几年开发调试telink 8266、8267芯片的时候,是无法进行printf打印调试的,都是通过EVK查看内存变化,或者通过led灯闪烁等方法调试程序运行状况,非常不方便。后来,telink SDK中提供了一些可以通过串口printf来实现调试。
废话不多说,直接上调试方法。
我当前使用的调试方法是gpio模拟串口进行调试。方法如下:
- 配置某个gpio为串口TX引脚。可以在app_config.h中配置,也可以在初始化中调用接口配置。我当前是在app_config.h中配置。
- 检测u_printf.h中的宏是否打开。
- 修改putchar.c中的串口输出函数。
具体代码如下:
//app_config.h
//当前使用sws烧写引脚作为串口输出引脚,并不影响EVK烧写firmware
#if IPL_SWS_UART_ENABLE
#define UART_PRINT_DEBUG_ENABLE 1
#define PRINT_BAUD_RATE 1000000//1M
#define DEBUG_INFO_TX_PIN GPIO_PA7
#define PULL_WAKEUP_SRC_PA7 PM_PIN_PULLUP_10K
#define PA7_OUTPUT_ENABLE 1
#define PA7_DATA_OUT 1 //must
#define PA7_FUNC AS_GPIO
#endif
确定u_printf.h中打印函数是否有效:
//u_printf.h
//确定#define printf u_printf 有效
#if (UART_PRINT_DEBUG_ENABLE || USB_PRINT_DEBUG_ENABLE)//print info by a gpio or usb printer
int u_printf(const char *fmt, ...);
int u_sprintf(char* s, const char *fmt, ...);
void u_array_printf(unsigned char*data, unsigned int len);
int elk_porting_dprintf(const char *fmt, ...);
#define printf u_printf
#define sprintf u_sprintf
#define array_printf u_array_printf
#else
#define printf
#define sprintf
#define array_printf
#endif
修改putchar.c中的串口输出函数:
//putchar.c
//修改uart_putc()函数,主要是打开函数中的中断设置,否则会出现乱码
#if (UART_PRINT_DEBUG_ENABLE)
#ifndef BIT_INTERVAL
#define BIT_INTERVAL (16000000/PRINT_BAUD_RATE)
#endif
_attribute_ram_code_
int uart_putc(char byte) //GPIO simulate uart print func
{
unsigned char j = 0;
unsigned int t1 = 0,t2 = 0;
REG_ADDR8(0x582+((DEBUG_INFO_TX_PIN>>8)<<3)) &= ~(DEBUG_INFO_TX_PIN & 0xff) ;//Enable output
unsigned int pcTxReg = (0x583+((DEBUG_INFO_TX_PIN>>8)<<3));//register GPIO output
unsigned char tmp_bit0 = read_reg8(pcTxReg) & (~(DEBUG_INFO_TX_PIN & 0xff));
unsigned char tmp_bit1 = read_reg8(pcTxReg) | (DEBUG_INFO_TX_PIN & 0xff);
unsigned char bit[10] = {0};
bit[0] = tmp_bit0;
bit[1] = (byte & 0x01)? tmp_bit1 : tmp_bit0;
bit[2] = ((byte>>1) & 0x01)? tmp_bit1 : tmp_bit0;
bit[3] = ((byte>>2) & 0x01)? tmp_bit1 : tmp_bit0;
bit[4] = ((byte>>3) & 0x01)? tmp_bit1 : tmp_bit0;
bit[5] = ((byte>>4) & 0x01)? tmp_bit1 : tmp_bit0;
bit[6] = ((byte>>5) & 0x01)? tmp_bit1 : tmp_bit0;
bit[7] = ((byte>>6) & 0x01)? tmp_bit1 : tmp_bit0;
bit[8] = ((byte>>7) & 0x01)? tmp_bit1 : tmp_bit0;
bit[9] = tmp_bit1;
unsigned char r = irq_disable();
t1 = read_reg32(0x740);
for(j = 0;j<10;j++)
{
t2 = t1;
while(t1 - t2 < BIT_INTERVAL){
t1 = read_reg32(0x740);
}
write_reg8(pcTxReg,bit[j]); //send bit0
}
irq_restore(r);
return byte;
}
#endif
int put_usb(int c)
{
reg_usb_ep_dat(8) = (unsigned char)c;
#if 0
reg_usb_ep_ctrl( 0 ) = ( 1<<(7) );
#endif
}
int putchar(int c)
{
#if USB_PRINT_DEBUG_ENABLE
return usb_putc((char)c);
#endif
#if (UART_PRINT_DEBUG_ENABLE)
return uart_putc((char)c);
#elif (USB_PRINT_DEBUG_ENABLE)
if(reg_usb_host_conn){
swire_is_init = 0; // should re-init swire if connect swire again
return usb_putc((char)c);
}else{
return swire_putc((char)c);
}
#endif
return 0;
}
这样,在SDK中就可以调用printf进行打印输出了。但是注意只有TX,并没有RX功能。如果需要RX功能,可以尝试使用硬件UART而非gpio模拟gpio进行log输出和输入。