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

Help needed - UART

Altera_Forum
Honored Contributor II
1,544 Views

Hello, 

 

We've been trying to do simple serial communication between nios2 hardware and PC. 

We tried some example codes and code on nios forum, but nothing seems to be working or we cant check if its working or not. 

 

We use : UP3 Educational Kit, Windows2000 based PC connected by 9-pin RS232 cable 

We tried sample codes using HAL and used TurboC / VB based programs and HyperTerminal in PC but saw no working result. 

 

Somebody please help us by providing us with the following details: 

 

1. What components should be added in SOPC using SOPCBuilder for UART and what settings should be there. 

2. After generation of system, what changes should be made in BDF file in Quartus 2, including input, output, pin assignments, etc. 

3. What functions can be used to send and receive data on NIOS2 using NIOS2 IDE.  

4. What program or code can be used to send or receive data on the target PC. 

 

Bala
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
338 Views

Hi Bala, 

 

Adding a UART to SOPC is quite simple. Select AvalonComponents\Communication\UART(RS-232 serial port). For simplicity, you might want to start by having a fixed Baud rate, at say 9600 Bd, common settings for the other items are parity=none, 8 data bits and 1 stop bit. Don't include any flow control at this stage as this may hamper your initial attempts to get communicating. You should now have a serial device setup for 9600,N,8,1,no-handshake - which is pretty unambiguous. 

Quartus will now show the two new connections on the NIOS design, rxd_to_the_uart and txd_from_the_uart. These will need to be assigned to external pins using the assignment editor. The rxd pin should default to Vcc. 

 

I don't know what your Altera hardware looks like but if you want to talk to a PC (into COM1: etc) you need to supply the proper RS-232 levels of -12v and +12v. Quite often boards will only present themselves to the outside world at TTL levels of 0v and +5v. Perhaps this is your problem. You need to check this. Also, check which is your Tx and Rx pins, you would be surprised how often stuff doesn't work because Rx is connected to Rx and Tx is connected to Tx. (On PC 9-pin-D Rx [into PC] is pin2 and Tx [out of PC] is pin3). I have also encountered problems trying to communicate with PCs that only have USB and not the traditional DB9 connectors. 

If you don't have proper RS-232 levels from your board you will have to make some kind of interface board to do the level shifting. Try searching for datasheets on devices like MAX232 or DSV14196WM. 

 

That should take care of the hardware. 

 

On the software side, there are various ways to implement a serial channel depending on your needs but this should be enough to make the port send characters out. Hyperterminal on your PC (with the appropriate settings) should be perfectly adequate to make the connection. 

 

 

# include "altera_avalon_uart_regs.h" 

 

void endless_send(void) 

unsigned char ch; 

unsigned int stat; 

 

/* make sure no IRQ enabled */ 

IOWR(UART_BASE, 3, 0x00); 

/* flush any characters sitting in the Rx holding register */ 

i = IORD(UART_BASE, 0); 

i = IORD(UART_BASE, 0); 

 

ch = ' '; 

for (;;) { 

/* get serial status */ 

stat = IORD(UART_BASE, 2); 

/* check for character Rx */ 

if (stat & 0x0080) { 

/* if character is '1' then reset o/p character back to ' ' */ 

if (IORD(UART_BASE, 0) == '1') ch = ' '; 

/* check if able to Tx next character. */ 

if (stat & 0x0040) { 

IOWR(UART_BASE, 1, ch); 

/* increment character but keep in printable range */ 

ch++; 

if (ch==0x80) ch = ' '; 

 

 

 

Once you have established a link you can go back to SOPC and experiment with changing Baud rates etc. 

 

I hope this is of some help. 

 

Banx.
0 Kudos
Altera_Forum
Honored Contributor II
338 Views

Hi banx, 

Thank you very much for ur timely help.. We've been breaking our heads for more than one week and now we've been able to successfully communicate thru serial port. The problem with our approach was we were trying to use the HAL without knowing quite something about HAL and everytime we try to run a HAL based C program we always got "Verify Failed!" 

Thank you. And do you also know how to use HAL for serial communication. 

Bala
0 Kudos
Altera_Forum
Honored Contributor II
338 Views

Hi banx, 

We;ve also tried HAL programs now. The problem with our earlier implementation was due to some wrong and redundant pin assignments. I've seen some programs with interrupts for serial communication on NIOS forum, please explain when one should go for interrupt-driven communication scheme and also provide some code. 

Thanking you. 

Bala
0 Kudos
Altera_Forum
Honored Contributor II
338 Views

Hi Bala, 

I'm happy to hear that you have made progress. To answer you question: no, I don't know much about HAL nor do I use it much. We did a lot of our early development on NIOS (no HAL as such) and had to write our own device drivers. When NIOS-II came along we just updated the code (r/w of devices) and ignored the HAL. 

All the serial stuff on our system is interrupt driven through ring buffers. As far as the application programmer is concerned they just put data into the Tx ring buffer using tx_char() or tx_string() and then they forget about it - there is some code running at a lower level that ensures it all gets transmitted. Similarly, they can use a function rx_char() to pull received characters out of the Rx ring buffer - they aren't concerned with how those characters got there. None of this relies on HAL. Let me know if you think something like this would be useful. 

 

Banx.
0 Kudos
Altera_Forum
Honored Contributor II
338 Views

Check your mailbox. 

Banx.
0 Kudos
Altera_Forum
Honored Contributor II
338 Views

狂顶!!! 

&#22823;&#23478;&#29992;&#20013;&#25991;&#21834; http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif
0 Kudos
Reply