uart.h using atmel studio

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

Hi,

 

I am developing a program dealing with uart on at90can128 MCU and I use Atmel studio 6.2 compiler. That's why I need to use uart.h header file but I can't find it.

If I add it #include "uart.h", I get this error Error :  "   uart.h: No such file or directory "

Where can I find it and how can I add it to the project in order to use it ?

 

Thanks! 

 

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

For Atmel Studio 6.2, there are files uart.c and uart.h in the stdio demo.

 

On my computer it is located at:

C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1061\avr8-gnu-toolchain\doc\avr-libc\examples\stdiodemo

 

Typically, you would copy these two files to your project directory and add the .c file to the project.

 

 

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

What do you want to be IN uart.h?

All the hardware register definitions will get included via <avr/io.h>

Typically "uart.h" would be user-provided code that define higher-level functions like uart_getchar(), uart_putstring(), and so on.

(I understand that some of the more commercial compilers DO provide a bunch of higher-level libraries that might provide this sort of thing, but the default AS 6.2 compiler (avr-gcc) does not)

 

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

Point is: you do not need uart.h

 

All register and bit names are already  defined  in <avr/io.h>

 

There may be some defines for baud rates and such, but  that is nearly trivial.

 

Any  functions it might refer to such as getchar() are ones that you will have to provide the code for, anyway.

 

Suggestion: Make your own. For all practical purposes, you would have to do  that, anyway.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Thanks Chuck99 I found it.

Acctually, I need this header file in order to communicate with the uart port in my board via at90can128 MCU.

ka7ehk & westfw why wouldn't I use it in my code for communication ? it has usefull functions !

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

kobidon wrote:
I need this header file ... it has usefull functions !

 

No, it doesn't!

 

A so-called "header" file, .h,  just provides the function prototypes (declarations) - it would be the associated .c file(s) that actually provide(s) the executable bodies of those functions (definitions).

 

http://c-faq.com/decl/decldef.html

 

http://www.github.com/abcminiuse... - a corrected & updated version of https://www.avrfreaks.net/forum/t...

 

The point is that those functions are going to be specific to the particular hardware; so it's not going to be a case of dropping it in and having it "just work" - you will need to do some work yourself!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Aug 6, 2015 - 06:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

...(Yes I undersand that I have to make some changes in the uart.c file in order to adjust it to my hardware (def of BaudRate, etc

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

The point is that avr-gcc/AVR-LibC, the compiler and library that come with Studio don't really have library code for things like UART, ADC, SPI, TWI and other AVR peripherals. Yes the user manual has one example of UART but it's not a generic example that would work on all 300 models of AVR. It is written very specifically for one model of AVR (atmega16 in fact) and it's just a complete fluke if the same code happens to work for CAN128 too.

 

If you want peripheral library code for the avr-gcc compiler that has been designed to work for many of the AVRs then Google third party libraries such as "pascal stang avr", "peter fleury avr", "martin thomas avr" as those guys have all written libraries supporting a number of the AVR peripherals and designed to work on a lot of the (older) models of AVR.

 

Unlike CodeVision, Imagecraft or whatever, avr-gcc is just the compiler and, as I say, does not include (apart from a few limited examples) peripheral support library code. Pay for the commercial compilers if you want that handed to you on a plate.

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

clawson wrote:
Pay for the commercial compilers if you want that handed to you on a plate.

+1

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the same topic, a question about uart_putchar function in the uart.c :

What is the *stream argument stands for ? I mean, what should I need to put in the function in this argument when I call it ? 

 

 

int uart_putchar(char c, FILE *stream)
{

 

 

  if (c == '\a')
    {
      fputs("*ring*\n", stderr);
      return 0;
    }

 

 

  if (c == '\n')
    uart_putchar('\r', stream);
  loop_until_bit_is_set(UCSRA, UDRE); // Make sure UART is ready to transmit new data. wait until USART Data Register Empty UDRE flag is set.
  UDR = c; // transmit data to UART by writing a byte to USART Data Register UDR0

 

 

  return 0;
}

 

 

 

thanks!

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

kobidon wrote:

In the same topic, a question about uart_putchar function in the uart.c :

What is the *stream argument stands for ? I mean, what should I need to put in the function in this argument when I call it ?

As I stated in post#2, uart.c and uart.h are part of the stdio demo, as described here.

Part of the demo shows how to set up streams.  Take a look at stdiodemo.c (Link).

 

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

FILE lcd_str = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE);

 

 

main(void)
{

   ...

   stdout = stdin = &uart_str; 

   stderr = &lcd_str;

   ...

  

   fprintf(stderr, "Hello world!\n");  // this goes to the LCD

   ...

  

   printf("Unknown command: %s\n", buf);  // this goes to the UART

 

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

Thanks for the fast reply but you didn't explain me what is this file I pointed to by the *stream pointer ? I want to understand the essence of nessecery using this pointer to this file.

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

kobidon wrote:

Thanks for the fast reply but you didn't explain me what is this file I pointed to by the *stream pointer ? I want to understand the essence of nessecery using this pointer to this file.

I thought it was rather obvious.  Guess I was wrong.

 

By my reading of it, you would use stdout for stream if you want to send chars out through the uart.

See my post #11 to see how stdout is defined.

 

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

C functions like printf() are defined to do IO to "files."   avr-libc has a lightweight implementation of "files" which they refer to as "streams" (no "file system", but there are devices that can do both input and output, and support some of the standard operations.)  This is described here: http://www.nongnu.org/avr-libc/u...

 

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

PS:  If you're programming an ARM or other high-end microcontroller, and it uses newlib for libc, using printf() causes a much more complete "file" implementation to be sucked into your image, perhaps doubling the code-space impact of "printf" (which was already pretty high.)   It gives you a new appreciation for avr-libc...

 

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

Thanks for the fast reply but you didn't explain me what is this file I pointed to by the *stream pointer ? I want to understand the essence of nessecery using this pointer to this file.

The simple answer is: Ignore it.

 

The API happens to dictate that the implementing routine must take that parameter but in reality, in 99.9999% of all implementations no programmer ever actually uses it.

 

The reason it is there is that you might well have your uart_stream defined and attached to both stdout and stderr (say). When data is sent to the routine you could use the second parameter to determine which of those two steams generated the data and you might, for example, choose to output ANSI color codes to turn anything from stderr red (say). This is just an example and it's a bit contrived but it hopefully shows why the parameter is there. It's actually quite tricky to think of a real world example of why you would really use it which is, I guess, why 99.9999% of people don't!