Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

printf

printf

2005-03-24 by neptunus1000

Hello,

I'wondering why my classes are not working. I want to build a Com 
class so a can use printf to put something on the uart.

The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the 
main are working good. But the printf is not working. Can some one 
help my.

[code]
//Main.cpp
#include <targets/LPC210x.h>
#include <stdio.h>
#include "Com.h"

int main(void){
  Com com;
  com.UARTInitialize(9600);
  com.delay(1000);
  com.UARTWriteChar('x');
  com.__putchar('\n');
  com.printf("test");
  return 0;
}

//Com.h
#include <targets/LPC210x.h>
#include <stdio.h>

class Com{
  private:
    int OSCILLATOR_CLOCK_FREQUENCY;
  public:
    Com(void);
    ~Com(void);
    unsigned int processorClockFrequency(void);
    unsigned int peripheralClockFrequency(void);
    void delay(int);
    void UARTInitialize(unsigned int);
    void UARTWriteChar(unsigned char);
    unsigned char UARTReadChar(void);
    void __putchar(int);
    void printf(const char *);
};

Com::Com(void){
  OSCILLATOR_CLOCK_FREQUENCY = 14745600;
}

Com::~Com(void){

}

unsigned int Com::processorClockFrequency(void){
  return OSCILLATOR_CLOCK_FREQUENCY * (PLLCON & 1 ? (PLLCFG & 0xF) + 
1 : 1);
}

unsigned int Com::peripheralClockFrequency(void){
  unsigned int divider;
  switch (VPBDIV & 3)
    {
      case 0:
        divider = 4;
        break;
      case 1:
        divider = 1;
        break;
      case 2:
        divider = 2;
        break;
    }
  return processorClockFrequency() / divider;
}

void Com::delay(int n){
  volatile int i;
  for (i = 0; i < n; ++i);
}

void Com::UARTInitialize(unsigned int baud){
  unsigned int divisor = peripheralClockFrequency() / (16 * baud);
  U0LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
  U0DLL = divisor & 0xFF;
  U0DLM = (divisor >> 8) & 0xFF;
  U0LCR &= ~0x80; /* Disable DLAB */
  PINSEL0 = PINSEL0 & ~0xF | 0x5;
  U0FCR = 1;
}

void Com::UARTWriteChar(unsigned char ch){
  while ((U0LSR & 0x20) == 0);
  U0THR = ch;
}

unsigned char Com::UARTReadChar(void){
  while ((U0LSR & 0x01) == 0);
  return U0RBR;
}

void Com::__putchar(int ch){
  if (ch == '\n')
    UARTWriteChar('\r');
  UARTWriteChar(ch);
}

void Com::printf(const char *string){
  printf(string);
}
[/code]

Re: [lpc2000] printf

2005-03-24 by Anton Erasmus

On 24 Mar 2005 at 9:15, neptunus1000 wrote:

> 
> 
> Hello,
> 
> I'wondering why my classes are not working. I want to build a Com
> class so a can use printf to put something on the uart.
> 
> The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the main
> are working good. But the printf is not working. Can some one help my.

{Code snipped]


Getting printf to output using your own routines, is more a question of porting the
standard C library than anything else. Which compiler do you use, and which standard
library ?  If it is newlib, then look at the new-lib_lpc stubs library to see an example of
how to port newlib.

Regards
   Anton Erasmus


-- 
A J Erasmus

Re: printf

2005-03-24 by neptunus1000

I'm use CrossStudio to compile. And a just want to use printf. The 
compiler is always telling my that I hava to implement __putChar. So 
I thought it is handy to build a Com class, so I can use printf. But 
it is not working as a hoped.

Thanks 

ps my there are beter way's

--- In lpc2000@yahoogroups.com, "Anton Erasmus" <antone@s...> wrote:
> On 24 Mar 2005 at 9:15, neptunus1000 wrote:
> 
> > 
> > 
> > Hello,
> > 
> > I'wondering why my classes are not working. I want to build a Com
> > class so a can use printf to put something on the uart.
> > 
> > The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the 
main
> > are working good. But the printf is not working. Can some one 
help my.
> 
> {Code snipped]
> 
> 
> Getting printf to output using your own routines, is more a 
question of porting the
> standard C library than anything else. Which compiler do you use, 
and which standard
> library ?  If it is newlib, then look at the new-lib_lpc stubs 
library to see an example of
Show quoted textHide quoted text
> how to port newlib.
> 
> Regards
>    Anton Erasmus
> 
> 
> -- 
> A J Erasmus

Re: [lpc2000] Re: printf

2005-03-24 by Michael Johnson

The crossstudio library implements printf in terms of a function called 
__putchar - all you have to do is supply an implementation of this 
function e.g.

void __putchar(int ch)
{
    debug_putchar(ch);
}

We supply many variants of printf/scanf and friends which enable the 
functionality (hence code size) to be trimmed down as required. The 
selection of the print/scanf library is determined by the Printf/Scanf 
properties.

Regards
Michael
Show quoted textHide quoted text
>I'm use CrossStudio to compile. And a just want to use printf. The 
>compiler is always telling my that I hava to implement __putChar. So 
>I thought it is handy to build a Com class, so I can use printf. But 
>it is not working as a hoped.
>
>Thanks 
>
>ps my there are beter way's
>
>--- In lpc2000@yahoogroups.com, "Anton Erasmus" <antone@s...> wrote:
>  
>
>>On 24 Mar 2005 at 9:15, neptunus1000 wrote:
>>
>>    
>>
>>>Hello,
>>>
>>>I'wondering why my classes are not working. I want to build a Com
>>>class so a can use printf to put something on the uart.
>>>
>>>The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the 
>>>      
>>>
>main
>  
>
>>>are working good. But the printf is not working. Can some one 
>>>      
>>>
>help my.
>  
>
>>{Code snipped]
>>
>>
>>Getting printf to output using your own routines, is more a 
>>    
>>
>question of porting the
>  
>
>>standard C library than anything else. Which compiler do you use, 
>>    
>>
>and which standard
>  
>
>>library ?  If it is newlib, then look at the new-lib_lpc stubs 
>>    
>>
>library to see an example of
>  
>
>>how to port newlib.
>>
>>Regards
>>   Anton Erasmus
>>
>>
>>-- 
>>A J Erasmus
>>    
>>
>
>
>
>
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>
>  
>

RE: [lpc2000] Re: printf

2005-03-24 by Paul Curtis

Hi,

I think that should be: 

int __putchar(int ch)
{
  debug_putchar(ch);
  return ch;
}

This is all described in the CrossWorks manual--look under "Customizing
the runtime system".

Regards,

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors
Show quoted textHide quoted text
> -----Original Message-----
> From: Michael Johnson 
> Sent: 24 March 2005 11:19
> To: lpc2000@yahoogroups.com
> Subject: Re: [lpc2000] Re: printf
> 
> 
> The crossstudio library implements printf in terms of a 
> function called __putchar - all you have to do is supply an 
> implementation of this function e.g.
> 
> void __putchar(int ch)
> {
>     debug_putchar(ch);
> }
> 
> We supply many variants of printf/scanf and friends which 
> enable the functionality (hence code size) to be trimmed down 
> as required. The selection of the print/scanf library is 
> determined by the Printf/Scanf properties.
> 
> Regards
> Michael
> 
> >I'm use CrossStudio to compile. And a just want to use printf. The 
> >compiler is always telling my that I hava to implement 
> __putChar. So I 
> >thought it is handy to build a Com class, so I can use 
> printf. But it 
> >is not working as a hoped.
> >
> >Thanks
> >
> >ps my there are beter way's
> >
> >--- In lpc2000@yahoogroups.com, "Anton Erasmus" <antone@s...> wrote:
> >  
> >
> >>On 24 Mar 2005 at 9:15, neptunus1000 wrote:
> >>
> >>    
> >>
> >>>Hello,
> >>>
> >>>I'wondering why my classes are not working. I want to build a Com 
> >>>class so a can use printf to put something on the uart.
> >>>
> >>>The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the
> >>>      
> >>>
> >main
> >  
> >
> >>>are working good. But the printf is not working. Can some one
> >>>      
> >>>
> >help my.
> >  
> >
> >>{Code snipped]
> >>
> >>
> >>Getting printf to output using your own routines, is more a
> >>    
> >>
> >question of porting the
> >  
> >
> >>standard C library than anything else. Which compiler do you use,
> >>    
> >>
> >and which standard
> >  
> >
> >>library ?  If it is newlib, then look at the new-lib_lpc stubs
> >>    
> >>
> >library to see an example of
> >  
> >
> >>how to port newlib.
> >>
> >>Regards
> >>   Anton Erasmus
> >>
> >>
> >>--
> >>A J Erasmus
> >>    
> >>
> >
> >
> >
> >
> >
> > 
> >Yahoo! Groups Links
> >
> >
> >
> > 
> >
> >
> >
> >  
> >
> 
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
>

Re: [lpc2000] printf

2005-04-01 by Leonardo Santos

Could you elaborate on that? I'm using GCC 3.4.3 + newlib 1.13 I downloaded 
from http://rod.info/arm.html. I've been able to compile FreeRTOS for a 
LPC2106 target and played a little with it. But I really miss the printf 
function. I think this question might be off topic, but if I could get some 
pointers, I'd be very grateful.

Lesson:
Never take stdio.h for granted!

Thanks!

On Thursday 24 March 2005 07:24, Anton Erasmus wrote:
> On 24 Mar 2005 at 9:15, neptunus1000 wrote:
> > Hello,
> >
> > I'wondering why my classes are not working. I want to build a Com
> > class so a can use printf to put something on the uart.
> >
> > The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the main
> > are working good. But the printf is not working. Can some one help my.
>
> {Code snipped]
>
>
> Getting printf to output using your own routines, is more a question of
> porting the standard C library than anything else. Which compiler do you
> use, and which standard library ?  If it is newlib, then look at the
> new-lib_lpc stubs library to see an example of how to port newlib.
>
> Regards
>    Anton Erasmus

-- 
This is Linux country.
On a quiet night, you can hear Windows reboot.

Leonardo Pereira Santos
Engenheiro de Projetos
PD3 Tecnologia
av. ParĂ¡ 330/202
(51) 3337 1237

Today Fortune tells us:
"I never let my schooling get in the way of my education."
-- Mark Twain

Re: [lpc2000] printf

2005-04-01 by Robert Adsett

At 01:49 PM 4/1/05 -0300, Leonardo Santos wrote:
>Could you elaborate on that? I'm using GCC 3.4.3 + newlib 1.13 I downloaded
>from http://rod.info/arm.html. I've been able to compile FreeRTOS for a
>LPC2106 target and played a little with it. But I really miss the printf
>function. I think this question might be off topic, but if I could get some
>pointers, I'd be very grateful.
>
>Lesson:
>Never take stdio.h for granted!

The newlib-lpc layer http://www.aeolusdevelopment.com provides the stub 
functions needed for printf etc.. to work on a LPC.  You can take a look at 
it as an example or just use it.  There is also a link on the site to Bill 
Gatliff's articles on porting newlib which is a quick useful overview.

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,   be 
they legal, genetic, or physical.  If you don't believe me, try to chew a 
radio signal. "  -- Kelvin Throop, III
http://www.aeolusdevelopment.com/

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.