Forum Discussion
UART Help
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!
Rick15 Replies
- Altera_Forum
Honored Contributor
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 - Altera_Forum
Honored Contributor
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! - Altera_Forum
Honored Contributor
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. - Altera_Forum
Honored Contributor
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 - Altera_Forum
Honored Contributor
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 - Altera_Forum
Honored Contributor
I have tried compile/link with and without the small c library.
Rick - Altera_Forum
Honored Contributor
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. - Altera_Forum
Honored Contributor
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 - Altera_Forum
Honored Contributor
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. - Altera_Forum
Honored Contributor
I did try the scanf, but I got another linker error. It couldn't find the scanf function.