Need assistance with defining my own putch( char ) function

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm just getting started with the AVR32. I used the ATMega series several years ago, mostly with the GCC toolchain. However, I've never used printf with GCC before but even if I had I probably would have forgotten how by now.

Most of my time in the last several years has been spent dealing with PICs. In the compilers I used for it, the putch / _mon_putc functions were declared weakly. All I had to do was write my own putch function, or the compiler would use the built in function. No fuss to it.

I've written a function to load a byte into a circular buffer and enable interrupts named Put_Tx_Char_Uart_1( Uint8 c ); What I'd like to do now is

void putch( char c )
{
   Put_Tx_Char_Uart_1( (Uint8)c );
}

I've perused this document: http://gcc.gnu.org/onlinedocs/gc...
I've searched the forums for previous posts and the best I could come up with were several posts stating to write a "initstdio()" function... but that doesn't help as I don't know what to put in there.
I've something here that suggests there is a function named FDEV_SETUP_STREAM that I can use to change the function that printf calls to output a character.
At the end of stdio.h, I found this line of code:

#define	putchar(x)	putc(x, stdout)

That makes me think that printf calls putchar(x) to output a character, and all I need to do is redirect stdout to Put_Tx_Char_Uart_1, but for the life of me I can't figure out how to do that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Got it!

int _write(int file, char * ptr, int len)
{ 
	while (len--)
		Put_Tx_Char_Uart_1(*ptr++); 
  
	return len; 
}

I suspect I can check the "file" variable to determine if I want to redirect the data to a different uart if I use fprintf. I've never used that before though, but it does seem like it could be very beneficial.