Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

UART Help

Altera_Forum
Honored Contributor II
1,285 Views

Was doing well in evaluating the IDE/HAL until now. Hit a brick wall going 100mph. I need some pointers on how to read characters from the UART. I tried the example in the Processor Ref handbook, but it won't link. Cant find fopen, fclose, fgetc even though stdio.h is included. Also tried the altera_alt_avalon_uart_read function, but don't understand how to init the alt_fd pointer. I am not a UNIX programmer and this layer of abstraction is confusing to me. Couldn't find any examples. Any help would be great! 

 

Rick
0 Kudos
15 Replies
Altera_Forum
Honored Contributor II
596 Views

Hi, 

I have used this functions with the uart already and it works fine so far. I have included some more header files and I am not shure, but I thing the "stdlib.h" could be the right one. There are also some properties in the sytem library you can adjust (do not use reduced driver). Take a look in the HAL-driver for the uart, there is a description for a compiler switch you have to use if the uart should work with interrupt. 

 

The other way is to use the headerfiles from the sdk section with the register and bit definitions, switch the std-in/out/error device in the system library to "none" and use the IORD/IOWR functions with the base address of the "system.h" to read and write to the uart directly (w/wo an interrupt handler you supplied). This will also work fine and this is my prefered way but it is not the "new way" from altera. 

 

Best regards 

Siegmar
0 Kudos
Altera_Forum
Honored Contributor II
595 Views

RPolicy I agree about the whole confusing part. Sigiz brought up the stuff about std-in/out/error but did you remember to turn those on? 

 

And if that still doesn't work and you just need printfs to see what's going on, try the jtag uart (it was very simple to use, just include stdio, and use printf(....) just like you would in any other C program) 

 

G-luck!
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

I've submitted a documentation enhancement request for this to be explained better. Thanks for bringing it to our attention. 

If you are still having problems after all the suggestions, please reply to this post.
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

Still have not been able to get the UART input to work. Printfs work fine. Fopen, fclose, getc cannot be linked. I have enabled all drivers and have set the stdio/err to UART1. Still nothing. Would use the alt_avalon_uart_read if I understood how to init the alt_fd pointer. With the printf so simple, why is the input such a pain? Couldn't we just use scanf? Sounds like a simple answer to me.... 

 

Rick
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

An educated guess. If this fails at link time do you have the small C library box ticked, if so untick it. As you will see in table 4.5 of the Nios II Sofwtare Developer's Handbook some functionality is removed in order to reduce the size of the library, fopen, fclose and getc are amongst these functions. 

 

Hope that helps
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

I have tried compile/link with and without the small c library. 

 

Rick
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

what happens? Do you get -1 back all the time or does the program just sit there spinning waiting for something? 

 

There's another topic somewhere in the forum someone posted about using input across the UART, try there, maybe you're answer is already here.
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

When I try to use the fopen/fclose/getc approach, I get a link error. The error message is that there is undefined references (fopen/fclose/getc). Is it possible that something# undef these functions? 

 

Rick
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

Stupid question, but did you remember to include the stuff for the file IO? (take a look at the example code to see what to include). 

 

Would Scanf work for what you're doing? I find printf was pretty easy to use so scanf shouldn't be too bad.
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

I did try the scanf, but I got another linker error. It couldn't find the scanf function.

0 Kudos
Altera_Forum
Honored Contributor II
596 Views

Including stdio, and something else (I forget) should be all that's needed. 

 

But if printf was working then you probably have what you need. Was the link error anything useful or the normal nonsence that I usually get from visual studio? http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

I also thought that the scanf would work because the printf did. The error was the standard unresolved external type of error. In another words, it couldn&#39;t find code for the scanf. It was the same error generated when I tried the fopen/fclose/getc. 

 

Rick
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

Sorry, I&#39;m out of ideas 

 

Are you trying to use scanf for debug or will there be input in the final design? Also what core and debug module (if any) has been added to the system? (drawing at straws, just like you are probably). 

 

Good-luck and hopefully someone else can be of more help.
0 Kudos
Altera_Forum
Honored Contributor II
596 Views

Can you post the linker error you get please.

0 Kudos
Altera_Forum
Honored Contributor II
596 Views

In fact can you post the linker error you get with the small C Library selected, and then the error when you don&#39;t have the small library selected.

0 Kudos
Reply