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

how to send/recieve 32bit int via UART

Altera_Forum
Honored Contributor II
2,853 Views

Hi, 

i have a question what would be the best way to transfer 32-bit integers using UART in nios? 

 

I've allready made a simple nios design with uart connected to on-chip memory. I wrote a base echo uart application, which works great: 

alt_u8 data; while(1) { if( IORD_ALTERA_AVALON_UART_STATUS( UART_BASE ) & 0x80 ) { data = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE ); IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, data ); printf("%c",data); } }  

 

But mainly I'd like to send 32bit integer data from PC to fpga board.  

How should I organise it? Should I use 32bit - DMA connected to uart and read 5bits of data every time? 

or maybe there is a simpler way? 

 

Thanks in advance
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
1,580 Views

Unless you really need the speed (but seeing that you used printf in your code I suspect you don't), why not just read the bytes one by one? 

 

Regards, 

Andreas
0 Kudos
Altera_Forum
Honored Contributor II
1,580 Views

Hi, 

 

maybe you could use the HAL file system as described in the embedded peripherals ip user guide / uart core.  

/* A simple program that recognizes the characters 't' and 'v' */# include <stdio.h># include <string.h> int main () {char* msg = "Detected the character 't'.\n"; FILE* fp; char prompt = 0; fp = fopen ("/dev/uart1", "r+"); //Open file for reading and writing if (fp) {while (prompt != 'v') { // Loop until we receive a 'v'. 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; }It's not an example how to transfer 32 bit, but it shows you how to open a device with the HAL file system and sending/receiving data. I would recommend to use something like fwrite(&buffer32bit,4,1,fp) and fread(&buffer32bit,4,1,fp). 

 

I never tried it, but I guess it should work. 

 

But I also would recommend to use the uart-registers directly (as you did in your example) and read byte by byte. 

 

Regards
0 Kudos
Altera_Forum
Honored Contributor II
1,579 Views

I would be wary about sending binary data over a uart connection. The system software at the host end might act on some byte values (eg for flow control). 

It you are sending 32bit data you also need to be aware of the endianness of the data - do you send the least or most significant 8 bits first. 

You also might want to use a simple data encapsulation protocol so that the reciever knows what data is being sent, and to allow additional requests to be added later.
0 Kudos
Altera_Forum
Honored Contributor II
1,580 Views

If you want to send binary data over a serial line you will have to use/design some kind of communication protocol. Otherwise things might get nasty soon.

0 Kudos
Reply