我正在尝试使用lwIP在嵌入式设备上实现基本应用程序。 我完成了基本项目的设置,重新定位的工作正如预期的那样。
然而,当我将lwIP添加到项目中并调用lwip_init()
时,我没有更多的输出。如果我忽略对lwip_init()
的呼叫,一切都恢复正常。
更有趣的是,如果我调用fopen()
时出现与以前相同的行为,则序列中没有其他输出。如果我使用重新定位的printf()
函数或者使用驱动程序的函数USART_SendData()
,我都不会输出。
我出错了什么?
我在 retarget.c 文件和我的 Main.cpp 下面发布了(如果我取消评论BLOCK 1
或BLOCK 2
,我没有更多的输出)以及初始化代码。
注意:重新定位的函数会将数据发送到USART3
,而USART_SendData()
会将数据发送到USART1。
我也尝试使用ITM端口进行调试,但我也有同样的行为。
的retarget.c :
#include <stdio.h>
#include <LibSTM/stm32f10x_usart.h>
#include <rt_sys.h>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; };
FILE __stdout;
FILE __stdin;
FILE __stderr;
FILE *__aeabi_stdin;
FILE *__aeabi_stdout;
FILE *__aeabi_stderr;
int fputc(int ch, FILE *f)
{
while (!(USART3->SR & 0x0080));
USART3->DR = ch;
return (ch);
}
int ferror(FILE *f)
{
return EOF;
}
void _ttywrch(int ch)
{
fputc(ch, stdout);
}
void _sys_exit(int return_code)
{
label: goto label;
}
//retargeting since otherwise lwIP inclusion produces errors
//http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3844.html
#define DEFAULT_HANDLE 0x100
const char __stdin_name[] = "my_stdin";
const char __stdout_name[] = "my_stdout";
const char __stderr_name[] = "my_stderr";
int _sys_write(FILEHANDLE fh, const unsigned char * buf, unsigned len, int mode){
int i;
for(i=0;i<len;i++)
{
fputc(buf[i], stdout);
}
return 0;
}
FILEHANDLE _sys_open(const char* name, int openmode){
return DEFAULT_HANDLE;
}
int _sys_istty(FILEHANDLE fh){
return 0;
}
int _sys_seek(FILEHANDLE fh, long pos){
return -1;
}
long _sys_flen(FILEHANDLE fh){
return 0;
}
int _sys_close(FILEHANDLE fh){
return 0;
}
int _sys_ensure(FILEHANDLE fh){
return 0;
}
的main.cpp :
#include <HW/HWSetup.h>
#include <stdio.h>
#include "lwip/init.h"
int main()
{
HWSetup();
printf("Start!\r\n");
/* //BLOCK 1
FILE *f;
f = fopen("foobar", "r");
*/
/* //BLOCK 2
lwip_init();
*/
printf("End!\r\n");
while(1){
USART_SendData(USART1, 'x');
}
}
HWSetup.cpp :
#include <HW/HWSetup.h>
#include <stdio.h>
void SetupGPIO()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
}
void SetupTraces()
{
GPIO_InitTypeDef GPIO_InitStructure;
// Trace GPIO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Trace USART
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 921600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
void SetupUSART_RS232()
{
GPIO_InitTypeDef GPIO_InitStructure;
// USART1 GPIO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
}
void HWSetup()
{
SetupGPIO();
SetupUSART_RS232();
SetupTraces();
}
我也试过调试代码,但是在我介入BX R0(我希望让我进入main())后,我只需要停止调试,因为我看不到它的位置,也没有任何其他控件在调试器上:
; Reset handler
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT __main
IMPORT SystemInit
LDR R0, =SystemInit
BLX R0
LDR R0, =__main
BX R0
ENDP
答案 0 :(得分:0)
以下代码适用于带有和不带microlib的LPC1788和μVision5:
//retarget minimum requirements to work with printf
#include <stdio.h>
#include <rt_misc.h>
#ifndef __MICROLIB
#pragma import(__use_no_semihosting_swi) // some projects need this, some don't like it
#endif
#include <rt_sys.h>
extern void $Super$$_sys_open(void);
extern int sendchar(int ch); //needed for printf
extern int getch(void); //needed for scanf
FILEHANDLE $Sub$$_sys_open(const char *name, int openmode)
{
return 1; /* everything goes to the same output */
}
extern void $Super$$_sys_close(void);
int $Sub$$_sys_close(FILEHANDLE fh)
{
return 0;
}
extern void $Super$$_sys_write(void);
int $Sub$$_sys_write(FILEHANDLE fh, const unsigned char *buf,
unsigned len, int mode)
{
//your_device_write(buf, len);
return 0;
}
extern void $Super$$_sys_read(void);
int $Sub$$_sys_read(FILEHANDLE fh, unsigned char *buf,
unsigned len, int mode)
{
return -1; /* not supported */
}
extern void $Super$$_ttywrch(void);
void $Sub$$_ttywrch(int ch)
{
//char c = ch;
//your_device_write(&c, 1);
sendchar(ch);
}
extern void $Super$$_sys_istty(void);
int $Sub$$_sys_istty(FILEHANDLE fh)
{
return 0; /* buffered output */
}
extern void $Super$$_sys_seek(void);
int $Sub$$_sys_seek(FILEHANDLE fh, long pos)
{
return -1; /* not supported */
}
extern void $Super$$_sys_flen(void);
long $Sub$$_sys_flen(FILEHANDLE fh)
{
return -1; /* not supported */
}
extern void $Super$$_sys_exit(void);
long $Sub$$_sys_exit(FILEHANDLE fh)
{
return -1; /* not supported */
}
//eof
希望,它也会帮助你。
不要忘记在main中初始化uart,或者尝试在_sys_open()中初始化