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 on DE2

Altera_Forum
Honored Contributor II
1,322 Views

Hi, I'm using DE2 (old kit) and my application has a UART to exchange data with PC. I'm using the kit's RS232 connection (not the JTAG). I created the system (including the UART at 9600bps) in Qsys and my Nios code initially is the same as Example 7-2 of Embedded Peripherals IP User Guide. When I run the code in Nios II I observe (through terminal on Windows) that I can send data from DE2 (fwrite or fprintf) to PC but I can't read data from PC. Does anybody has any tip to solve my problem?

0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
265 Views

Hi, could you post your piece of code (more comfortable for us to help you) ? 

 

I personally use 2 file descriptors : 1 for sending, 1 for receiving.
0 Kudos
Altera_Forum
Honored Contributor II
265 Views

This is my code: 

 

# include <stdio.h> 

# include <string.h> 

 

int main () 

char* msg = "Detected the character 't'.\n"; 

 

FILE* fp; 

char prompt = 0; 

 

fp = fopen ("/dev/rs232", "r+"); //Open file for reading and writing 

 

if (fp) 

 

while (prompt != 'v') 

{ // Loop until we receive a 'v'. 

fprintf(fp, "Entered while.\n"); 

prompt = getc(fp); // Get a character from the UART. 

 

if (prompt == 't') 

{ // Print a message if character is 't'. 

fwrite (msg, strlen (msg), 1, fp); 

 

 

fprintf(fp, "Closing the UART file.\n"); 

fclose (fp); 

return 0; 

 

 

I have made some changes after I wrote the post and now I'm able to send and receive characters. Although, it isn't suitable for my application to use polling to receive characters, so I need to implement interrupt at least for reception, but I have no idea how to do this. Any tip?
0 Kudos
Altera_Forum
Honored Contributor II
265 Views

Hi, look at how to write an interrupt in NIOS II (Altera Nios II software design handbook). 

If you employ interrupt, you will forget the file pointer and functions like fwrite(), getc() 

and need to employ lower layer functions like iord() 

 

For my project, I have tried interrupts to recieve data but it missed a few.
0 Kudos
Altera_Forum
Honored Contributor II
265 Views

So, if I decide to use interrupts I have to forget the HAL facilities? Do I really have to write low level code? I thought there was a way to use interrupts through HAL.

0 Kudos
Altera_Forum
Honored Contributor II
265 Views

It is quite simple to use interrupts : look at Nios II examples. You just need few functions like  

// Read status and compare to RRDY_MSK if (IORD_ALTERA_AVALON_UART_STATUS(UART1_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK) { donnee = IORD_ALTERA_AVALON_UART_RXDATA(UART1_BASE); } //...  

 

By doing this way, your main program will be interrupted at every character received (of course). 

Be careful about blocking situations. 

Good Luck
0 Kudos
Reply