Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
1,226 Views

NIOS-II Software Writing

I have write the Nios-II Code (Below) with the help of ready made examples. 

please someone clarify the bold underline portion of the code what acutely they are doing.or from where i can get the help related to such type of keywords? 

#include <stdio.h> 

#include"system.h" 

#include"altera_avalon_pio_regs.h" 

static alt_u8 count; 

#define ESC 27 

#define ESC_TOP_LEFT "[1;0H" 

#define ESC_COL2_INDENT5 "[2;5H" 

#define ESC_COL1_INDENT1 "[1;1H" 

#define ESC_COL1_INDENT5 "[1;5H" 

#define ESC_COL2_INDENT1 "[2;1H" 

#define ESC_COL2_INDENT5 "[2;5H" 

#define ESC_COL2_INDENT10 "[2;10H" 

#define ESC_COL2_INDENT14 "[2;14H" 

#define ESC_CLEAR "K" 

#ifdef lcd_display_base staticvoid lcd_init( FILE *lcd ) 

fprintf(lcd, "%c%s Video Coding ", ESC, ESC_TOP_LEFT); #endif 

 

 

staticvoid initial_message() 

printf("* Hello from Nios II! *\n"); 

}  

 

#ifdef seven_seg_pio_base 

staticvoid sevenseg_set_hex(int hex) 

static alt_u8 segments[16] = { 

0x81, 0xCF, 0x92, 0x86, 0xCC, 0xA4, 0xA0, 0x8F, 0x80, 0x84, 

0x88, 0xE0, 0xF2, 0xC2, 0xB0, 0xB8 };  

unsignedint data = segments[hex & 15] | (segments[(hex >> 4) & 15] << 8); 

iowr_altera_avalon_pio_data(seven_seg_pio_base, data); 

#endif 

staticvoid count_sevenseg() { 

#ifdef seven_seg_pio_base 

sevenseg_set_hex(count); 

#endif 

staticvoid count_lcd( void* arg ) { 

#ifdef lcd_display_name 

FILE *lcd = (FILE*) arg; 

fprintf(lcd, "%c%s 0x%x\n", ESC, ESC_COL2_INDENT10, count); 

#endif 

int main() 

FILE * lcd; 

int delay; 

#ifdef lcd_display_name lcd = fopen("/dev/lcd_display", "w"); 

#endif 

#ifdef lcd_display_name 

lcd_init( lcd ); 

#endif 

initial_message(); 

while(1) { 

usleep(100000); 

iowr_altera_avalon_pio_data(led_pio_base, count & 0x01); 

delay = 0; 

while(delay < 20000) 

delay++; 

count_sevenseg(); 

count_lcd(lcd); 

count++; 

fclose(lcd); 

return 0; 

}
0 Kudos
6 Replies
Altera_Forum
Honored Contributor I
167 Views

hi! 

 

these keywords come up from the altera HAL delivered with your dev tools. 

 

they come up from the system.h and altera_avalon_pio_regs.h 

 

the IOWR_ALTERA_AVALON...-macros come from the second include file. they write data in the register of the pio, which base-address is defined in the system.h file. also they care about caches and write directly to the registers. 

 

most of the# ifdef-lines ask for macros defined in the system.h include file. there are all peripherals listed which you have included in your niosII-system (build in the sopc builder). in system.h there are also bae adresses, memory spans and so on. take a look at them and you will see pretty fast where they come from. 

 

schafrichter
Altera_Forum
Honored Contributor I
167 Views

Sentences that have# prefix are called pre-proccessor directives. If you have included in your SoPC Builder system a LCD controller, the system.h file defines LCD_DISPLAY_BASE macro , so code below conditional compile sentence is included, if macro is not defined, code is not included. 

 

 

 

# ifdef LCD_DISPLAY_BASE 

staticvoid lcd_init( FILE *lcd ) 

fprintf(lcd, "%c%s Video Coding ", ESC, ESC_TOP_LEFT); 

# endif 

 

staticvoid initial_message() 

printf("* Hello from Nios II! *\n"); 

# endif
Altera_Forum
Honored Contributor I
167 Views

thanx for quick replay.... 

please let me know when to use ....these macros...i mean what are the priority or sequence for using them (below) or they are need base....?  

#define LCD_DISPLAY_NAME  

 

#define LCD_DISPLAY_TYPE  

 

#define LCD_DISPLAY_SPAN  

 

I just wanted to know when to use all of them (below)...or is there software litrature/Guide which can guide me... 

 

 

#define SEVEN_SEG_PIO_NAME "/dev/seven_seg_pio" 

#define SEVEN_SEG_PIO_TYPE "altera_avalon_pio" 

#define SEVEN_SEG_PIO_BASE 0x02120890 

#define SEVEN_SEG_PIO_SPAN 16 

#define SEVEN_SEG_PIO_DO_TEST_BENCH_WIRING 0 

#define SEVEN_SEG_PIO_DRIVEN_SIM_VALUE 0x0000 

#define SEVEN_SEG_PIO_HAS_TRI 0 

#define SEVEN_SEG_PIO_HAS_OUT 1 

#define SEVEN_SEG_PIO_HAS_IN 0 

#define SEVEN_SEG_PIO_CAPTURE 0 

#define SEVEN_SEG_PIO_EDGE_TYPE "NONE" 

#define SEVEN_SEG_PIO_IRQ_TYPE "NONE" 

#define SEVEN_SEG_PIO_FREQ 50000000  

 

* lcd_display configuration 

 

#define LCD_DISPLAY_NAME "/dev/lcd_display" 

#define LCD_DISPLAY_TYPE "altera_avalon_lcd_16207" 

#define LCD_DISPLAY_BASE 0x02120880 

#define LCD_DISPLAY_SPAN 16  

 

* led_pio configuration  

 

#define LED_PIO_NAME "/dev/led_pio" 

#define LED_PIO_TYPE "altera_avalon_pio" 

#define LED_PIO_BASE 0x02120870 

#define LED_PIO_SPAN 16 

#define LED_PIO_DO_TEST_BENCH_WIRING 0 

#define LED_PIO_DRIVEN_SIM_VALUE 0x0000 

#define LED_PIO_HAS_TRI 0 

#define LED_PIO_HAS_OUT 1 

#define LED_PIO_HAS_IN 0 

#define LED_PIO_CAPTURE 0 

#define LED_PIO_EDGE_TYPE "NONE" 

#define LED_PIO_IRQ_TYPE "NONE" 

#define LED_PIO_FREQ 50000000  

 

* button_pio configuration  

 

#define BUTTON_PIO_NAME "/dev/button_pio" 

#define BUTTON_PIO_TYPE "altera_avalon_pio" 

#define BUTTON_PIO_BASE 0x02120860 

#define BUTTON_PIO_SPAN 16 

#define BUTTON_PIO_IRQ 2 

#define BUTTON_PIO_DO_TEST_BENCH_WIRING 1 

#define BUTTON_PIO_DRIVEN_SIM_VALUE 0x000F 

#define BUTTON_PIO_HAS_TRI 0 

#define BUTTON_PIO_HAS_OUT 0 

#define BUTTON_PIO_HAS_IN 1 

#define BUTTON_PIO_CAPTURE 1 

#define BUTTON_PIO_EDGE_TYPE "ANY" 

#define BUTTON_PIO_IRQ_TYPE "EDGE" 

#define BUTTON_PIO_FREQ 50000000  

 

 

* lan91c111 configuration  

 

#define LAN91C111_NAME "/dev/lan91c111" 

#define LAN91C111_TYPE "altera_avalon_lan91c111" 

#define LAN91C111_BASE 0x02110000 

#define LAN91C111_SPAN 65536 

#define LAN91C111_IRQ 6 

#define LAN91C111_IS_ETHERNET_MAC 1 

#define LAN91C111_LAN91C111_REGISTERS_OFFSET 0x0300 

#define LAN91C111_LAN91C111_DATA_BUS_WIDTH 32
Altera_Forum
Honored Contributor I
167 Views

What is the sequence to use them..i mean when to use  

#define LCD_DISPLAY_BASE  

 

and  

 

#define LCD_DISPLAY_NAME (it use most of the time whenever function call occure)
Altera_Forum
Honored Contributor I
167 Views

The defines that you show are automatically generated during the software build. You shouldn't (and don't need to) modify are create them. They are generated according to the SOPC configuration. 

 

You can use the ifdefs though, if you want to generate some code only if a specific component is available.
Altera_Forum
Honored Contributor I
167 Views

you can read about it here: 

http://www.altera.com/literature/hb/nios2/n2sw_nii5v2.pdf 

 

and here: 

http://www.altera.com/literature/hb/nios2/n2cpu_nii5v3.pdf 

 

the whole bunch of macros is defined with the help of your sopc system and the altera tools. 

 

the XXX_BASE macros are for base addresses of the components, 

XXX_SPAN gives you memory span which this device occupies and so on. 

 

the docs mentioned above will help you to understand the system.
Reply