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

questions about uarts

Altera_Forum
Honored Contributor II
1,401 Views

Hi all, 

 

I'm starting a new development with NIOS II and I have some questions regarding UART usage. 

 

First of all, I will briefly explain my application: 

 

I need 4 UARTs on my system. One (UART_PC) is connected to a PC. The PC sends commands and expects answers from these commands. I also need 3 other UARTs: UART_MATRIZ, UART_VDSL and UART_ADSL. 

 

When UART_PC receives a command from PC, the application needs to analyze it. Depending on the command, it can take actions such as turn on a LED, or a relay, or simply transfer this command to one of the other three UARTS, that are connected to other equipment. 

 

When a response came from one of three UARTS (UART_MATRIZ the UART_VDSL and UART_ADSL) this should simply be transferred to the UART_PC. 

 

 

My question is: How do this in NIOS? The idea of using the STDIO functions such as fopen, fread, fwrite, etc is appealing but I am not sure if I can use then. The examples I saw on Altera documentation are quite simple and didn't help me so much. 

 

Can I open multiple files simultaneously? One for each UART? 

 

Do I need to do file polling using getc () to wait for anything other than EOF? 

 

How do I implement timeout for the messages coming from PC? 

 

Do I need to open files in a non-blocking mode? If yes, how do I do it?. 

 

Do I need to use interrupts to deal with these UARTS? How can I do it? 

 

I implemented something very similar on an atmel microcontroller, but I needed to write everything from scratch (interrupt routines, timeout, etc). Do I need to to that on NIOS? 

 

Thanks!!
0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
311 Views

Yes, you can open multiple files. 

I'm not sure getc() will return EOF when there's no data to read -- it may block instead. 

 

If that way fails, you'll need to perform low level access to the UARTs' registers.
0 Kudos
Altera_Forum
Honored Contributor II
311 Views

I would be tempted to write the uart access code - not even using interrupts. 

It is likely that your main 'idle' loop will be executed often enough to pick up the characters directly from the uart fifos. 

If you avoid the stdio functions the only initialisation instructions needed before running C code are those to setup sp and gp - and they can be added by the linker script!
0 Kudos
Altera_Forum
Honored Contributor II
311 Views

dsl, 

 

Thanks for the answer. What do you exactly mean by "setup sp and gp" ? 

 

 

How can they be added by the linker script? 

 

 

Best Regards,
0 Kudos
Altera_Forum
Honored Contributor II
311 Views

The only registers that need to be initialised before your C code runs are the global pointer (gp) and the stack pointer (sp). You may also need to set the exception temporary (et) if your execption handler does anything (mine just saves the registers into a fixed memory area and loopstops!). 

Clearly the libc stdio and malloc functions do require more initialisation - but for a small embedded system you may not need those at all. 

 

In the linker script you can Build the instructions by hand, something like: 

RA = 32 - 5; RB = RA - 5; RC = RB - 5; IMM16 = 6; SET_GP_HI = 0 << RA | 26 << RB | ((_gp + 32768) >> 16) << IMM16 | 0x34; SET_GP_LO = 26 << RA | 26 << RB | (_gp & 0xffff) << IMM16 | 0x4; /* addi instructions to set %sp and %et from %gp */ SET_SP = 26 << RA | 27 << RB | 0x4; SET_ET = 26 << RA | 24 << RB | 0x4; SECTIONS { /* code section, at reset address */ code : { LONG(SET_GP_HI) LONG(SET_GP_LO) LONG(SET_SP | ((stack_top - _gp) & 0xffff) << IMM16) LONG(SET_ET | ((exception_data - _gp) & 0xffff) << IMM16) LONG(c_code << 4 | 1) /* jmpi c_code() */ 

You need to set the stack pointer because gcc always saves the caller-saved registers - even for functions with __attribute__(noreturn). 

Similar stuff can be done to save/restore registers for the exception handler.
0 Kudos
Altera_Forum
Honored Contributor II
311 Views

Thanks, I'll try that.

0 Kudos
Reply