I have coded a project of mine in C on a Windows machine in the software IAR Embedded Workbench IDE. The project compiles and runs fine. I have a couple of printf functions in my code. But the thing is that the project is intended for a microcontroller AT91SAM7X256. I've successfully built my application to run on the sram of the microcontroller, and the application was loaded successfully. But the printf function is being directed to the USART port of the controller (I can only assume), and so I would like to redirect the printf to display the text on my terminal I/O. Does anyone have an idea on how I can do that?
-
3Realize that your program is running on a different system, and connecting its serial output to a serial port or USB-serial adapter on your development system is the traditional way of obtaining such output. However, some (but by no means all) debugger/programmer connections can also be used to a tunnel an output channel. You would have to check if your particular development interface (which you have neglected to mention) supports that or can be extended to do so.– Chris StrattonApr 30, 2014 at 14:55
4 Answers
I use the ARM, AVR32 and MSP430 processor versions of the IAR toolchain. In each of these you have to implement your own low level functionality to handle the stdin and stdout streams. The ARM compiler manual has a section on "Standard streams for input and output" which says that you need to write your own version of the __write() function, and it provides an example version where the data is written to a memory mapped LED display.
Knowing IAR I would expect they will have a similar example for your processor/toolchain combination.
-
-
Do you know how can I write the
__write()function for the AT91SAM7X256? I've searched and really don't know how to. I just want to have theprintf()function to print on the console I/O (i.e my machine).– AdamApr 30, 2014 at 15:36 -
1@JohnSmith - you are going to have to be a lot more specific about how you think the data might get from the embedded board back to your machine. What paths are available in your setup? What does your setup even consist of? Apr 30, 2014 at 16:59
-
@ChrisStratton Well the
printffunction is working because I can see it being executed through the memory (I even tried ascanfbefore it and that worked too). But since there is nothing on the UART port of my embedded board for example, all the program knows is that it executed the function, not knowing if I actually saw anything. I simply want to redirect the output so I can see it on my console I/O.– AdamApr 30, 2014 at 20:27 -
@JohnSmith - just repeating the question isn't going to get you a solution (in fact, it's going to get your question a downvote). Fill in the missing information as requested twice now in comments. Output data will not magically get from your embedded processor to your PC - you need to take stock of available communication channels and take concrete steps towards putting one of them to use, for example, buy yourself a $10 USB-serial or research if your debug pod can pass through output - and tell us which one you are using already Apr 30, 2014 at 20:37
I don't know why your printf is being redirected but to do what you want to do I did the following: First show the console window. (while running debug go to menu View-> Terminal I/O then stopping the application, go to Project->Options and in Linker-> Library check Include C-SPY debugging support
Be warned that this makes the running of the program considerably slower, than it should be. Comment out all printfs later when finishing your debugging
The standard answer is to select "Semihosted" and "Via semihosting" (Or via SWO -- but I have been using the other.) And "buffered terminal output" for speed under library options 1.
However, I am poking about here because I have a demo program from ST that won't printf to the console and I don't know why -- have many applications that work. So if anyone can remember some detail that gets left out of the standard information feel free to say something...
all you have to do is to #include <stdio.h> and use printf() in your code. For this to work with the debugger console you will have to go to Project options -> Linker -> Output (tab) and in the "Format" section where you have selected "Debug information for C-SPY" you need to have "With I/O emulation modules" checked (this requires "with runtime control modules"). This will make the linker use its special low-level I/O routines (putchar() , getchar() ) which will route it to/from the debug terminal I/O console.
When entering debug mode you get the debug console window displayed by selecting View -> Terminal I/O from the top menu. You will probably notice that if you try to printf too much text it will make the output lag a bit. But it can surely be a handy tool for debugging your application anyway.
(For the curious, you can see in the disassembler that the putchar() / getchar() functions linked into the code when using I/O emulation are just NOP instruction dummy functions. The C-SPY debugger will set a hardware breakpoint in this function, and whenever this breakpoint is reached it will read out the register to read/write the character. The communication overhead causes a small delay for each character. A side-effect of using I/O emulation is that the debugger will reserve one hardware breakpoint for each of these two functions (if you are using them), so you will get fewer hardware breakpoints available to use in your other application debugging.)
You can find some more information about "I/O emulation modules" in the IAR user manual: Help -> 8051 Embedded Workbench User Guide.