I can't seem to get printf going on my system. I've declared putchar as extern and written a routine called putchar to output to the serial port, but the application dies whenever I try to printf. I dare say I'm not doing something fairly fundamental!
"It's already configured for the LPC900 devices" Which isn't very helpful for an LPC2100, is it?! ;-) But surely, the ARM tools must also come with appropriate "Hello World" examples!?
Andy, you're right! The lpc900 stuff isn't going to help me. Anyway, there's examples for the atmel arm7 for 'hello world' of which i've tried to mimic to no avail. I didn't previously mention that I'm using UV3 with the gnu toolchain. With the GNU tools being more generic, there's a lot of stuff in the libraries etc which I fail to comprehend. I can understand by overriding the standard putchar that the printf should work but that doesn't seem to be the case. So I'm thinking there's probably some little trick to get it to work. I'll try some more stuff tonight and try using sprintf but I'm hoping someone has done exactly what I want and can share their experience.
Can you get the output working if you leave printf for now and just use your putchar? Can you see in the debugger where it's stopping?
Oops, you're right. Does the following help?
#include <stdio.h> /* I/O Functions */ #include <LPC21XX.H> /* LPC22XX Peripheral Registers */ int main (void) { /* Initialize Serial Interface */ PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */ U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */ U1LCR = 0x03; /* DLAB = 0 */ while (1) { /* Loop forever */ printf ("Hello World\n"); /* Print "Hello World" */ } }
I haven't tried such a simple test, as the code I'm using has interrupt driven comms with a circular buffer. My 'putchar' works as I have a collection of routines that prints hex, prints a string etc. I tried using 'sprintf' and that locked the code up also. I haven't tried the debugger as it complains about not finding the usb. I'll try your simple example tonight just to confirm that printf is working but still the issue remains as to redirecting the printf output to the destination of my choice. Also, why does printf in your example go out uart1 rather than uart0? Is there any doc on printf and how to redirect it's output?
printf uses putchar and _getkey and they are configured to use UART1 (for the MCB2100). Take a look in \KEIL\ARM\LIB for the source for putchar and getkey. They are polled but this is at least a starting point. As for the debugger, are you using ULINK to connect to your hardware? IF not, you can use the simulator. I accurately simulates the on-chip peripherals for the devices we support. It's what I used to test the hello world example. Jon
I had a quick look last night into what you've suggested. Firstly, the \keil\arm\lib directory doesn't exist so maybe this is half my problem! I'm also a little unsure of how to start a project from scratch with issues like the linker. I've figured out the startup file after much grief initially. Pretty much i've started off with the blinkyirq project and added my own code.I've conquered the interrupts,timers and uarts but the issue with printf is still eluding me.FYI I'm using a tinyarm50 board.
can you send me a sample project?
I did a bit more reading and fiddling tonight. After reading a bit more on how to create a project (I had previously added to the blinkyirq sample project), I created a new project, added the same files as I had used previously and sprintf started working. I then tried printf which also worked through my interrupt driven comms. So I'm happy, don't ask me what changed though to make it work apart from creating a new project. I found to get printf working with my code I had to add syscalls.c to the project (I had tried that previously with no luck). Thanks.
Are you using the KEIL compiler or the GNU compiler? Jon
I'n using the GNU toolchain. With the tinyarm board they supply the IAR tools but there was no sample programs and I couldn't get a piece of code to run, so I tried the Keil demo that had the blinky demo. That ran first time so I've stuck with the Keil tools. I'm porting across some of my 8bit code to do a comparison in terms of performance and to get a feel for the ARM tools. Now I've overcome the printf problem, I can forge ahead! Cheers.
There is a generic GNU example that shows printf I/O in the folder: Keil\ARM\GNU\Examples\Hello. It is written for a Atmel device, but can be easily adapted to any other part. Change the following: