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++
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
12748 Discussions

Nios II Project- Problem with Uart

Altera_Forum
Honored Contributor II
1,282 Views

I am at a university in Ohio. I have been assigned to work with the DE2 Board to make a system that can display incoming text from the uart (serial port on my laptop and hyperterminal). My problem is that I am using an interrupt to monitor the onboard switches and the program then waits for an input from the serial. I am using the getc function to monitor the serial port. I have very little coding experience (but learning more everyday, ugh) and I know that my getc is hanging up my program. Is there another function that I can use to not hang the system until it gets an input to the Uart? Another student (who is better at coding) wrote directly to the xmit and receive registers for the rs232 port. I was hoping to avoid this, or have some help in identifying them. My code is below. I have borrowed and modified the counter and hello world that are included with the nios ide. Thanks for any help. 

 

--Craig 

 

 

# include <stdio.h> 

# include <string.h> 

# include <altera_avalon_pio_regs.h> 

# include "system.h" 

# include <time.h> 

# include "alt_types.h" 

# include "sys/alt_irq.h" 

# include <unistd.h> 

/* A "loop counter" variable. */ 

//static alt_u8 count; 

/* A variable to hold the value of the button pio edge capture register. */ 

volatile int edge_capture; 

int enable; 

 

/* Button pio functions */ 

 

/* 

Some simple functions to: 

1. Define an interrupt handler function. 

2. Register this handler in the system. 

*/ 

 

/******************************************************************* 

* static void handle_button_interrupts( void* context, alt_u32 id)* 

* *  

* Handle interrupts from the buttons. * 

* This interrupt event is triggered by a button/switch press. * 

* This handler sets *context to the value read from the button * 

* edge capture register. The button edge capture register * 

* is then cleared and normal program execution resumes. * 

* The value stored in *context is used to control program flow * 

* in the rest of this program's routines. * 

******************************************************************/ 

 

 

 

static void handle_SW_interrupts(void* context, alt_u32 id) 

/* Cast context to edge_capture's type. It is important that this be  

* declared volatile to avoid unwanted compiler optimization. 

*/ 

 

volatile int* edge_capture_ptr = (volatile int*) context; 

/* Store the value in the Button's edge capture register in *context. */ 

*edge_capture_ptr =IORD_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE); 

/* Reset the Button's edge ccapture register. */ 

 

 

 

IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE, 0x00); 

IORD_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE);  

 

 

/* Initialize the button_pio. */ 

 

 

static void init_sw_pio() 

/* Recast the edge_capture pointer to match the alt_irq_register() function 

* prototype. */ 

void* edge_capture_ptr = (void*) &edge_capture; 

/* Enable all 4 button interrupts. */ 

IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_BASE, 0xff); 

/* Reset the edge capture register */ 

IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE, 0x00); 

 

/* Register the interrupt handler. */ 

alt_irq_register( SW_IRQ, edge_capture_ptr, 

handle_SW_interrupts ); 

 

 

 

 

int main() 

 

FILE* fp; 

FILE* fp1; 

char prompt = 0; 

 

 

 

 

 

 

fp1 = fopen("/dev/lcd_0", "r+"); 

fp = fopen("/dev/uart_0", "r+"); 

fprintf(fp, "UART OPEN\n"); 

 

 

 

 

fprintf(fp1,"The system is \nactive");  

usleep(50000); 

fprintf(fp1, "\n\n\n");  

init_sw_pio(); 

 

while(1){  

 

 

 

 

 

 

 

 

if (edge_capture != 0 ){ 

 

 

 

switch(edge_capture){ 

case 0x0: 

 

printf("No switches are pressed"); 

break; 

 

case 0x1: 

printf("Switch 0 is pressed"); 

break; 

 

case 0x2: 

 

printf("Switch 1 is pressed"); 

break; 

 

case 0x4: 

printf("Switch 2 is pressed"); 

break; 

 

case 0x8: 

printf("Switch 3 is pressed"); 

break; 

 

case 0x16: 

printf("Switch 4 is pressed"); 

break; 

 

case 0x32: 

printf("Switch 5 is pressed"); 

break; 

 

case 0x64: 

printf("Switch 6 is pressed"); 

break; 

 

case 0x128: 

printf("Switch 7 is pressed"); 

break; 

 

default: 

printf("More than one switch is pressed");  

edge_capture = 0; 

}  

 

 

 

prompt = getc(fp); 

fprintf(fp1, "[%c] \n", prompt); 

 

 

usleep(2000000); 

fprintf(fp1, "\n \n"); 

 

 

return 0; 

}
0 Kudos
0 Replies
Reply