Printf and serial library

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

Hello everyone!

I realize, this might have been discussed a lot of times but a quick search didn't answer the questions I had.

Basically, what I want to do is to implement a serial communication on my atmega168 which has the following functionality:
- supports big enough RX and TX buffers (32 or 64 bytes)
- non-blocking during write
- supports some sort of printf for at least string, hex and decimal.

I read some documentation on stdio.h, which seems to already have printf, but I am not sure to understand the FILE datatype and how to link it to the circular buffers from the uart.

Examples or links are more than welcome!
Thanks!

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

Did you have a look at C:\WinAVR-20100110\doc\avr-libc\examples\stdiodemo ? (Provided you have winAvr installed)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Ok, so if I understand correctly,

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

and

stdout = stdin = &uart_str;

means that whenever I call fprintf(&uart_str, ...) it will execute uart_putchar for every char that is generated by fprintf?
In the case of fgets, I am not too sure.

For the non-blocking uart communication and the circular buffer, do i need to implement that myself with interrupts and all that fun?

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

Quote:
For the non-blocking uart communication and the circular buffer, do i need to implement that myself with interrupts and all that fun?

That's right. FDEV_SETUP_STREAM is just a method to hide the details of the physical method of doing i/o from the c programmer. What exactly is working under the hood, is completely up to you. In case of a (non-blocking) serial i/o interface via an UART, you have to code that "with interrupts and all that fun" yourself - or do a search for several existing implementations.

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

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

Quote:
whenever I call fprintf
Or any other stdio output function AFAIK.

As you can see uart.c has already an input buffer, not sure how you need to change it to suit your needs, but lots of other C nuts will be able to help out. :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Ok, I guess I will just code all that myself. I remember actually coding some sort of circular input buffer for the uart in assembly a while ago, so writing it in c shouldn't be problematic. I just thought that with the amount of people using serial communication, there would be some nice public libraries available, to spare me the trouble.

Although, honestly I think all I really needed and absolutely didn't feel like coding was the printf, so thanks for sorting that one out :).

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

If you want an example of input and output FIFOs that work to Tx and Rx using interrupts then download ffsample.zip from the FatFs website and in the AVR directory look at uart.c

The way Chan has implemented that would make it easy to wire it into FDEV_SETUP_STREAM.

By the way, once you have done:

stdout = stdin = &uart_str; 

then you don't need to use fprintf() - though you can if you want. You can just use printf("Hello") and it should work because printf() is effectively shorthand for fprintf(stdout, ...)

BTW as this is GCC specific I'll move it to that forum.

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

clawson wrote:
If you want an example of input and output FIFOs that work to Tx and Rx using interrupts then download ffsample.zip from the FatFs website and in the AVR directory look at uart.c

Awesome, that's EXACTLY what I needed. Thanks

Seems like the original website is gone, so I've found a copy on some chineese-ish site.
Here it is for future references:
http://read.pudn.com/downloads83/sourcecode/embed/320211/avr/uart.c__.htm

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

Quote:
Seems like the original website is gone,
It's not gone but there seems to be some issues with browsers or something. There was a thread a while ago about that.

edit I just accessed the site without problems.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly