Hi, I used printf for serial print by this function:
struct __FILE { int handle;} ; //FILE __stdout; //FILE __stdin; //FILE __stderr;
int fputc(int ch, FILE *f) { while(!USART_GetFlagStatus(COM3,USART_FLAG_TXE));
USART_SendData(COM3,ch);
return ch; }
This worked but when I want to create a file and write on it with fwrite function, data send to serial!! How can I tell to micro fwrite don't use my fputc function and write data to file not serial? When I remove my fputc function, data write to file but I want to use printf for debug and I don't want to remove it.
I hope I've been able to figure out what I mean.
Regards, Alireza
You'd have to add code to discriminate the destination based on the FILE handle passed to you, and implement a file system, etc.
This is embedded, you have to build the OS
Thanks,
I change it to:
#define WriteToUART3 0 #define WriteToFile 1
int fputc(int ch, FILE *f) { int ret = EOF; switch( f->handle ) { case WriteToUART3 : while(!USART_GetFlagStatus(COM3,USART_FLAG_TXE)); USART_SendData(COM3,ch); ret = ch ; break ; case WriteToFile : ???????? ret = ch ; break ; default : break ; } return ret ; }
What I must write instead of ???????? ?! I know that I must call original fputc function, but how?! Thanks
Where do you expect this data to go?
You'll likely need an SD card with some SPI or SDIO peripheral driver, and a file system like FATFS layered on top of that. Does your board have such features?
I would generally avoid doing byte level IO as this is hugely inefficient on a block storage device, and you don't have a lot of memory for buffering or caching.
Yes, I do this with SD and spi protocol. This work for me without my fputc function, but I want to have serial debug too. For write to SD I use from fwrite function, but it seem fwrite use from fputc function!