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++
12686 Discussions

Nios 2 uart initialization problem

kpainter
Beginner
784 Views

I am using Quartus Lite 22.1.2.  I am using the lightweight uart core.  It is initialized by the alt_sys_init function and declares an object called uart_0.  My problem is this object seems to be getting optimized out.  In my main function, this line:

extern intel_lw_uart_state uart_0;
 
The uart is working.  Printf works, I can see characters in the rx_buf if I break in the rx isr and look at the sp variable but the uart_0 variable is not found.  From the log file:
/media/kdp/d_drive/intelFPGA_lite/22.1std/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/../lib/gcc/nios2-elf/12.2.1/../../../../../H-x86_64-pc-linux-gnu/nios2-elf/bin/ld: obj/default/UserFlash.o:/home/kdp/FPGA_Dev/Eval_board/software/UserFlashTest/UserFlash.c:886: more undefined references to `uart_0' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:1077: UserFlashTest.elf] Error 1
 
I am wondering what could be going wrong?
0 Kudos
12 Replies
JingyangTeh
Employee
735 Views

Hi


For the lightweight UART, it is not meant to use the device instantiation directly.

It should be used to use through calling the Standard C Library Function as mention in the link below:

https://www.intel.com/content/www/us/en/docs/programmable/683130/22-3/uart-api-34890.html


Regards

Jingyang, Teh


0 Kudos
kpainter
Beginner
680 Views

Thank you for the quick answer.  I had wondered about that so we tried the avalon uart and the same thing happens.  Let me explain what we are trying to do and maybe there is a better way.  We are trying to use this embedded console found here:
https://github.com/bradschl/embedded-c-debug-console

 We have a getc function defined as below:

int console_getc(void * console_hint)
{
(void) console_hint;
int ret = ECDC_GETC_EOF;

if (uart_0.rx_start != uart_0.rx_end)
{
ret = intel_lw_uart_read(&uart_0, xferbuf, 1, 0x4000);
ret = xferbuf[0];
}
return ret;
}
 
Here is where the uart_0 is not recognized as it is being removed.  This is happening with the avalon uart as well as I mentioned.  I can see that the uart_0 struct is being filled with received characters from the isr but I am not able to get them out.  Perhaps there is a better method of doing what I am attempting?  Thank you for the assistance!
 
Regards,
Kelly Painter
0 Kudos
JingyangTeh
Employee
655 Views

Hi


I have not tried using the LightWeight UART IP before.

But from the user guide you would need to enable the small driver option in the BSP.


In your case have you initialized the called the initialize the UART in your code using the function "intel_lw_uart_ioctl_fd (alt_fd* fd, int req, void* arg);"

You would need to declare your own FD and pass it through the init function.


Could you try sharing your project folder for me to understand better?


Regards

Jingyang, Teh


0 Kudos
JingyangTeh
Employee
575 Views

Hi


Do you have any follow up question on this?


Regards

Jingyang, Teh


0 Kudos
kpainter
Beginner
539 Views

Hi Jingyang,

Yes, I have been trying to use the standard c library functions as you suggested with no luck.   I found this page which seems like pretty standard stuff:

https://www.intel.com/content/www/us/en/docs/programmable/683130/24-3/hal-system-library-support-28025.html

However, the Nios 2 compiler doesn't like any of it.  Specifically, I am trying to do this:

FILE* file;
file = fopen("/dev/uart_0", "w+");
fd = fileno(file);

This generates a couple of errors:

conflicting types for 'file'; have 'int' UserFlash.c /UserFlashTest line 861 C/C++ Problem
conflicting types for 'fopen'; have 'FILE *(int, const char *)' UserFlashTest line 339, external location: /home/seahawk/apps/intelFPGA_lite/22.1std/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/nios2-elf/include/stdio.h C/C++ Problem
Invalid redeclaration of 'file' UserFlash.c /UserFlashTest line 861 Semantic Error

 

I tried the same thing using gcc on Ubuntu with no problems

#inclu

de <stdio.h>

int main() {
FILE *file = fopen("log.txt", "a");
int fd;

if (file == NULL) {
perror("Error opening file");
return 1;
}

const char *log_entry = "Log entry: Application started";
fprintf(file, "%s\n", log_entry);
fd = fileno(file);
fprintf(file, "fd = %d\n", fd);

fclose(file);
printf("Done");
return 0;
}

 

The first thing I am not sure about is the name parameter as pointed out from that website above, it is in system.h  Here is mine with respect to uart_0.  I am not sure if the name is /dev/uart_0 or UART_0_NAME.  Neither one works.

 

/*
* uart_0 configuration
*
*/

#define ALT_MODULE_CLASS_uart_0 intel_lw_uart
#define UART_0_BASE 0x142380
#define UART_0_BAUD 115200
#define UART_0_DATA_BITS 8
#define UART_0_FIXED_BAUD 1
#define UART_0_FREQ 90000000
#define UART_0_IRQ 7
#define UART_0_IRQ_INTERRUPT_CONTROLLER_ID 0
#define UART_0_NAME "/dev/uart_0"
#define UART_0_PARITY 'N'
#define UART_0_READ_DEPTH 1024
#define UART_0_SIM_TRUE_BAUD 0
#define UART_0_SPAN 32
#define UART_0_STOP_BITS 1
#define UART_0_SYNC_REG_DEPTH 2
#define UART_0_TYPE "intel_lw_uart"
#define UART_0_USE_CTS_RTS 0
#define UART_0_USE_EOP_REGISTER 0
#define UART_0_WRITE_DEPTH 1024

 

I did ask my management about sharing my project folder and they really don't want to do that, unfortunately.

Hopefully, you will have a nugget of wisdom for me that will solve this problem.

 

Regards,

Kelly Painter

 

0 Kudos
kpainter
Beginner
513 Views

I have been playing around with this more and have tried to open the uart in this way:

int fd;

fd = open(UART_0_NAME, O_RDWR);

This generates two errors:

Description Resource Path Location Type
conflicting types for 'fopen'; have 'FILE *(int, const char *)' UserFlashTest line 339, external location: /home/user/apps/intelFPGA_lite/22.1std/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/nios2-elf/include/stdio.h C/C++ Problem
initializer element is not constant UserFlash.c /UserFlashTest line 861 C/C++ Problem

Functions that call read and write with fd don't generate errors.  I think if I could get past the open call, I would be good.  Any suggestions as what might be wrong?

 

Regards,

Kelly Painter

0 Kudos
JingyangTeh
Employee
398 Views

Hi


It seems weird that it is recognizing the FILE as an int type.

Could you try including these headers into your project and recompiling?

#include <stdio.h>

#include <stddef.h>

#include <stdlib.h>


Regards

Jingyang, Teh


0 Kudos
JingyangTeh
Employee
349 Views

Hi


Do you have any update on this case?

After adding the header files did you manage to solve the error?


Regards

Jingyang, Teh


0 Kudos
kpainter
Beginner
328 Views

Hi Jingyang,

I added those includes and I still am getting the same errors. 

Description Resource Path Location Type
conflicting types for 'fd'; have 'int' UserFlash.c /UserFlashTest line 859 C/C++ Problem
Invalid redeclaration of 'fd' UserFlash.c /UserFlashTest line 859 Semantic Error

 

Regards,

Kelly Painter

0 Kudos
JingyangTeh
Employee
282 Views

Hi


Do you have problem compiling a simple hello world example on your end?

Is it possible that you could zip up your project and share it to me to have a look?


Regards

Jingyang, Teh


0 Kudos
JingyangTeh
Employee
199 Views

Hi


Did you managed to try the hello world example?

Did you face the same issue?


Regards

Jingyang, Teh


0 Kudos
JingyangTeh
Employee
153 Views

Hi


As we do not receive any response from you on the previous question/reply/answer that we have provided. Please login to ‘https://supporttickets.intel.com/s/?language=en_US’, view details of the desire request, and post a feed/response within the next 15 days to allow me to continue to support you. After 15 days, this thread will be transitioned to community support. The community users will be able to help you on your follow-up questions.


Regards

Jingyang, Teh


0 Kudos
Reply