4. HAL NII52004-1.2 HAL Hardware Abstraction Layer HAL API Nios II HAL ANSI C HAL API ANSI C HAL HAL API ANSI C ANSI C HAL HAL printf() scanf() ANSI C I/O HAL API RTOS ANSI C Nios II IDE HAL Nios II IDE HAL Nios II IDE Altera Corporation 4 1 2004 12 Preliminary
Nios II IDE 4-1 HAL Nios II 4-1. Nios II IDE HAL.c.h.s HAL HAL Nios II IDE Nios II IDE SOPC Builder Nios II.ptf SOPC Builder HAL 4-1 2 Nios II IDE 1 HAL HAL Nios II SOPC Builder.ptf 4 2 Altera Corporation Nios II 2004 12
HAL SOPC Builder.ptf Nios II IDE HAL HAL HAL system.h system.h HAL system.h Nios II system.h C system.h? system.h IRQ system.h system.h HAL Nios II IDE system.h Nios II IDE HAL Nios II IDE Altera Corporation 4 3 2004 12 Nios II
HAL system.h system.h /* * sys_clk_timer configuration * */ #define SYS_CLK_TIMER_NAME "/dev/sys_clk_timer" #define SYS_CLK_TIMER_TYPE "altera_avalon_timer" #define SYS_CLK_TIMER_BASE 0x00920800 #define SYS_CLK_TIMER_IRQ 0 #define SYS_CLK_TIMER_ALWAYS_RUN 0 #define SYS_CLK_TIMER_FIXED_PERIOD 0 /* * jtag_uart configuration * */ #define JTAG_UART_NAME "/dev/jtag_uart" #define JTAG_UART_TYPE "altera_avalon_jtag_uart" #define JTAG_UART_BASE 0x00920820 #define JTAG_UART_IRQ 1 HAL Nios II ANSI C HAL ANSI C alt_type.h HAL 4 1 HAL 4 1. HAL alt_8 alt_u8 alt_16 alt_u16 alt_32 alt_u32 8 8 16 16 32 32 4 4 Altera Corporation Nios II 2004 12
HAL 4 2 GNU Toolchain 4 2. GNU Toolchain char short long int 8 16 32 32 UNIX HAL API UNIX UNIX Nios II HAL HAL ANSI C stdio.h C UNIX _exit() close() fstat() getpid() gettimeofday() ioctl() isatty() kill() lseek() open() read() sbrk() settimeofday() stat() usleep() wait() write() I/O 4 6 Altera Corporation 4 5 2004 12 Nios II
10 1 HAL API HAL newlib C I/O fopen() fclose() fread() HAL UNIX I/O HAL UNIX close() fstat() ioctl() isatty() lseek() open() read() stat() write() 10 1 HAL API HAL zip /mount/zipfs() /mount/zipfs()/myfile fopen() zipfs HAL system.h /dev/ SOPC Builder SOPC Builder UART uart1 system.h /dev/uart1 4 6 Altera Corporation Nios II 2004 12
HAL HAL zip rozipfs #include <stdio.h> #include <stddef.h> #include <stdlib.h> #define BUF_SIZE (10) int main(void) FILE* fp; char buffer[buf_size]; fp = fopen ( /mount/rozipfs/test, r ); if (fp == NULL) printf ( Cannot open file.\n ); exit (1); fread (buffer, BUF_SIZE, 1, fp); fclose (fp); return 0; 10 1 HAL API UART Universal Asynchronous Receiver/Transmitter HAL file.h ANSI C HAL stdio.h I/O Altera Corporation 4 7 2004 12 Nios II
I/O stdin stdoutstderr HAL stdin stdout stderr printf() perror() Nios II IDE Nios II IDE Hello World Nios II IDE stdout Hello World #include <stdio.h> int main () printf ( Hello world! ); return 0; UNIX API unistd.h STDIN_FILENO STDOUT_FILENO STDERR_FILENO stdin stdout stderr stdin stdout stderr uart1 UART UART #include <stdio.h> #include <string.h> int main (void) char* msg = hello world ; FILE* fp; 4 8 Altera Corporation Nios II 2004 12
HAL fp = fopen ( /dev/uart1, w ); if (fp) fprintf(fp, %s,msg); fclose (fp); return 0; C++ HAL C++ C++ API /dev/null /dev/null /dev/null /dev/null I/O HAL C I/O zip I/O /mnt/rozipfs fopen( /mnt/rozipfs/myfile, r ) fopen() fread() file.h C I/O 10 1 HAL API Altera Corporation 4 9 2004 12 Nios II
HAL Nios II HAL API 2 1 1 HAL UNIX gettimeofday() settimeofday() times() HAL API sys/alt_alarm.h sys/alt_timestamp.h 10 1 HAL API HAL HAL Nios II IDE Nios II IDE HAL Nios II HAL 4 10 Altera Corporation Nios II 2004 12
HAL alt_nticks() / alt_ticks_per_second() HAL UNIX gettimeofday() settimeofday() times() times.h HAL alt_alarm_start() int alt_alarm_start (alt_alarm* alarm, alt_u32 nticks, alt_u32 (*callback) (void* context), void* context); callback nticks context callback alarm alt_alarm_start() callback alt_alarm_stop() 6 1 Altera Corporation 4 11 2004 12 Nios II
1 #include <stddef.h> #include <stdio.h> #include sys/alt_alarm.h #include alt_types.h /* * */ alt_u32 my_alarm_callback (void* context) /* 1 */ return alt_ticks_per_second();... /* alt_alarm */ static alt_alarm alarm;... if (alt_alarm_start (&alarm, alt_ticks_per_second(), my_alarm_callback, NULL) < 0) printf ( No system clock available\n ); HAL HAL HAL 1 alt_timestamp_start() alt_timestamp() Nios II IDE 4 12 Altera Corporation Nios II 2004 12
HAL alt_timestamp_start() alt_timestamp() alt_timestamp_start() 2 32 1 alt_timestamp_freq() Nios II alt_timestamp.h #include <stdio.h> #include sys/alt_timestamp.h #include alt_types.h int main (void) alt_u32 time1; alt_u32 time2; alt_u32 time3; if (alt_timestamp_start() < 0) printf ( No timestamp device available\n ); else time1 = alt_timestamp(); func1(); /* */ time2 = alt_timestamp(); func1(); /* 2 */ time3 = alt_timestamp(); printf ( time in func1 = %u ticks\n, (unsigned int) (time2 time1)); printf ( time in func2 = %u ticks\n, (unsigned int) (time3 time2)); printf ( Number of ticks per second = %u\n, (unsigned int)alt_timestamp_freq()); return 0; Altera Corporation 4 13 2004 12 Nios II
HAL HAL API HAL API HAL API EPCS HAL API HAL API 2 API API API sys/alt_flash.h 10 1 HAL API 4 14 Altera Corporation Nios II 2004 12
HAL alt_flash_open_dev() alt_write_flash() alt_read_flash() alt_flash_close_dev() 4 16 API 1 alt_flash_open_dev() system.h alt_write_flash() int alt_write_flash(alt_flash_fd* fd, int offset, const void* src_addr, int length ) fd offset src_addr length alt_read_flash() int alt_read_flash( alt_flash_fd* fd, int offset, void* dest_addr, int length ) fd offset dest_addr length alt_read_flash() alt_flash_close_dev() void alt_flash_close_dev(alt_flash_fd* fd ) Altera Corporation 4 15 2004 12 Nios II
system.h /dev/ext_flash API API #include <stdio.h> #include <string.h> #include sys/alt_flash.h #define BUF_SIZE1024 int main () alt_flash_fd* fd; int ret_code; char source[buf_size]; char dest[buf_size]; /* 0xAA */ memset(source, 0xa, BUF_SIZE); fd = alt_flash_open_dev( /dev/ext_flash ); if (fd) ret_code = alt_write_flash(fd, 0, source, BUF_SIZE); if (!ret_code) ret_code = alt_read_flash(fd, 0, dest, BUF_SIZE); if (!ret_code) /* * * 0xa * dest */ alt_flash_close_dev(fd); else printf( Can t open flash device\n ); return 0; 4 16 Altera Corporation Nios II 2004 12
HAL alt_write_flash() 4 18 4 3 4 3 2 4K 8K 0xAA 5K 0x0000 0xBB2 K 0x1400 t(2) 0xAA 5 K 0xFF2 2 t(3) 0xAA 4 K 0xFF 4 K 2 t(4) 0x1000 2 K 0xFF 4 3. t(0) t(1) t(2) t(3) t(4) 1 1 2 1 2 0x0000 1?? FF AA AA AA 0x0400 1?? FF AA AA AA 0x0800 1?? FF AA AA AA 0x0C00 1?? FF AA AA AA 0x1000 2?? FF AA FF FF (1) 0x1400 2?? FF FF FF BB 0x1800 2?? FF FF FF BB 0x1C00 2?? FF FF FF FF 4 3 (1) 2 FF Altera Corporation 4 17 2004 12 Nios II
3 alt_get_flash_info() alt_erase_flash_block() alt_write_flash_block() 1 1 1 0 0 1 alt_get_flash_info() int alt_get_flash_info( alt_flash_fd* fd, flash_region** info, int* number_of_regions) number_of_regions info 1 flash_region flash_region sys/alt_flash_types.h typedef typedef struct flash_region int offset;/* */ int region_size;/* */ int number_of_blocks;/* */ int block_size;/* */ flash_region; 4 18 Altera Corporation Nios II 2004 12
HAL alt_get_flash_info() alt_erase_flash() int alt_erase_flash_block( alt_flash_fd* fd, int offset, int length) fd offset length alt_write_flash_block() 1 int alt_write_flash_block( alt_flash_fd* fd, int block_offset, int data_offset, const void *data, int length) fd block_offset data length data_offset API #include <string.h> #include "sys/alt_flash.h" #define BUF_SIZE 100 int main (void) flash_region* regions; alt_flash_fd* fd; int number_of_regions; int ret_code; char write_data[buf_size]; Altera Corporation 4 19 2004 12 Nios II
DMA /* write_data 0xa */ memset(write_data, 0xA, BUF_SIZE); fd = alt_flash_open_dev(ext_flash_name); if (fd) ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions); if (number_of_regions && (regions->offset == 0)) /* 1 */ ret_code = alt_erase_flash_block(fd, regions->offset, regions->block_size); if (ret_code) /* * write_data BUF_SIZE 100 * 1 */ ret_code = alt_write_flash_block( fd, regions->offset, regions->offset+0x100, write_data, BUF_SIZE); return 0; DMA HAL DMA Direct Memory Access HAL DMA DMA 2 HAL 2 2 4 20 Altera Corporation Nios II 2004 12
HAL 4-2 DMA 3 DMA DMA 4-2. DMA 3 1. DMA Recieve Channel 2. DMA 3. DMA DMA DMA API sys/alt_dma.h 10 1 HAL API DMA 7 1 DMA DMA DMA alt_dma_txchan_open() system.h 1 Altera Corporation 4 21 2004 12 Nios II
DMA DMA dma_0 DMA #include <stddef.h> #include sys/alt_dma.h int main (void) alt_dma_txchan tx; tx = alt_dma_txchan_open ( /dev/dma_0 ); if (tx == NULL) /* */ else /* */ return 0; alt_dma_txchan_send() typedef void (alt_txchan_done)(void* handle); int alt_dma_txchan_send (alt_dma_txchan dma, const void* from, alt_u32 length, alt_txchan_done* done, void* handle); alt_dma_txchan_send()dma length from DMA handle done DMA 2 alt_dma_txchan_space() alt_dma_txchan_ioctl() alt_dma_txchan_space() alt_dma_txchan_ioctl() 4 22 Altera Corporation Nios II 2004 12
HAL DMA DMA DMA DMA alt_dma_rxchan_open() alt_dma_rxchan_prepare() alt_dma_rxchan_prepare() typedef void (alt_rxchan_done)(void* handle, void* data); int alt_dma_rxchan_prepare (alt_dma_rxchan dma, void* data, alt_u32 length, alt_rxchan_done* done, void* handle); dma legthdata DMA handle done DMA 2 alt_dma_rxchan_depth() alt_dma_rxchan_ioctl() alt_dma_rxchan_depth() alt_dma_rxchan_ioctl() DMA main() DMA #include <stdio.h> #include <stddef.h> #include <stdlib.h> #include sys/alt_dma.h #include alt_types.h /* */ volatile int dma_complete = 0; /* */ void dma_done (void* handle, void* data) Altera Corporation 4 23 2004 12 Nios II
DMA dma_complete = 1; int main (void) alt_u8 buffer[1024]; alt_dma_rxchan rx; /* */ if ((rx = alt_dma_rxchan_open ( /dev/dma_0 )) == NULL) printf ( Error:failed to open device\n ); exit (1); else /* */ if (alt_dma_rxchan_prepare (rx, buffer, 1024, dma_done, NULL) < 0) printf ( Error:failed to post receive request\n ); exit (1); /* */ while (!dma_complete); printf ( Transaction complete\n ); alt_dma_rxchan_close (rx); return 0; DMA DMA DMA DMA #include <stdio.h> #include <stdlib.h> #include "sys/alt_dma.h" #include "system.h" static volatile int rx_done = 0; /* * * */ 4 24 Altera Corporation Nios II 2004 12
HAL static void done (void* handle, void* data) rx_done++; /* * */ int main (int argc, char* argv[], char* envp[]) int rc; alt_dma_txchan txchan; alt_dma_rxchan rxchan; void* tx_data = (void*) 0x901000; /* */ void* rx_buffer = (void*) 0x902000; /* rx */ /* */ if ((txchan = alt_dma_txchan_open("/dev/dma_0")) == NULL) printf ("Failed to open transmit channel\n"); exit (1); /* */ if ((rxchan = alt_dma_rxchan_open("/dev/dma_0")) == NULL) printf ("Failed to open receive channel\n"); exit (1); /* */ if ((rc = alt_dma_txchan_send (txchan, tx_data, 128, NULL, NULL)) < 0) printf ("Failed to post transmit request, reason = %i\n", rc); exit (1); /* */ if ((rc = alt_dma_rxchan_prepare (rxchan, rx_buffer, 128, done, NULL)) < 0) Altera Corporation 4 25 2004 12 Nios II
printf ("Failed to post read request, reason = %i\n", rc); exit (1); /* */ while (!rx_done); printf ("Transfer successful!\n"); return 0; HAL Nios II HAL nios2-elf-gcc -O3 compiler 2 2 HAL Nios II IDE HAL Use Small Footprint Drivers HAL DALT_USE_SMALL_DRIVERS 4 26 Altera Corporation Nios II 2004 12
HAL 4 4 Nios II 4 4. P. UART JTAG UART LCD IRQ IRQ ALT_MAX_FD Nios II IDE 32 10 ALT_MAX_FD /dev/null null /dev/null printf() HAL stdin stdout stderr null stdin stdin stdout stderr Nios II IDE Altera Corporation 4 27 2004 12 Nios II
ANSI C UNIX I/O UNIX I/O ANSI C I/O I/O ANSI C API UNIX I/O API 4 5 UNIX Newlib C ANSI C HAL newlib newlib ANSI C newlib newlib Nios II IDE nios2-elf-gcc -msmallc newlib C Nios II newlib Windows > Altera > Nios II Development Kit > Nios II Documentation 4 28 Altera Corporation Nios II 2004 12
HAL 4 5 newlib C 4 5. newlib C printf() %f %g scanf() FILE * / stdout stderr stdin FILE * stdio.h asprintf() fiprintf() fprintf() iprintf() printf() siprintf() snprintf() sprintf() vasprintf() vfiprintf() vfprintf() vprintf() vsnprintf() vsprintf() fscanf() scanf() sscanf() vfscanf() vscanf() vsscanf() fseek() ftell() fopen() fclose() fdopen() fcloseall() fileno() stdio.h C++ setbuf() setvbuf() setlocale() localeconv() Altera Corporation 4 29 2004 12 Nios II
Nios II IDE HAL HAL FPGA DALT_NO_CFI_FLASH HAL 4 31 _exit() HAL exit() exit() C I/O atexit() exit() main() exit() _exit() Nios II IDE -Dexit=_exit 4 30 Altera Corporation Nios II 2004 12
HAL HAL C ALT_NO_INSTRUCTION_EMULATION / / / -mno-hw-mul mhw-div main() alt_main() main() alt_main() ANSI C main() main() HAL HAL Nios II HAL 1 Altera Corporation 4 31 2004 12 Nios II
ANSI C Nios II alt_main() alt_main() stdout printf() HAL Nios II 4 26 HAL Nios II IDE Nios II Nios II IDE HAL HAL bss_start bss_end BSS BSS.rwdata.rodata/RAM 4 37 alt_main() 4 32 Altera Corporation Nios II 2004 12
HAL alt_main() ALT_OS_INIT() OS HAL HAL alt_fd_list_lock semaphore alt_sys_init() Nios II IDE HAL alt_sys_init.c C I/O stdin stdout stderr _do_ctors() C++ C++ main () exit()main() exit() Nios II alt_main.c Nios II IDE alt_main() HAL alt_main() alt_main() void alt_main (void) Altera Corporation 4 33 2004 12 Nios II
HAL 1 alt_sys_init.c alt_sys_init() 5 1 HAL HAL HAL HAL Nios II IDE system.h on_chip_memory.on_chip_memory Nios II 32 4 34 Altera Corporation Nios II 2004 12
HAL 4-3 0x0 SOPC Builder 0x20 4-3. HAL on_chip_ram 0x0.on_chip_ram 0x0 +0x10 sdram.reset.sdram Nios II RAM RAM Altera Corporation 4 35 2004 12 Nios II
.reset 3.text.rodata.rwdata.text.rodata.rwdata Nios II IDE Nios II IDE C C++ section.on_chip_memory foo.sdram bar() C /* */ int foo attribute ((section (".on_chip_memory"))) = 0; void bar attribute ((section (".sdram"))) (void) foo++;.section on_chip_memory.section.on_chip_memory GNU 4 36 Altera Corporation Nios II 2004 12
HAL.rwdata.rwdata HAL EPCS RAM HAL.text Nios II IDE Altera Flash Programmer _start EPCS.text HAL _reset _reset _start RAM HAL.rwdata.rodata alt_main() alt_load() Altera Corporation 4 37 2004 12 Nios II
HAL HAL HAL HAL Nios II HAL Nios II HAL HAL HAL <Nios II >/components HAL <Nios II >/components/altera_hal/hal/inc/sys #include <Nios II >/components/altera_hal/hal/inc/ DMA #include sys/alt_dma/h system.h HAL Nios II IDE Newlib ANSI C <Nios II >/bin HAL Nios II IDE Nios II IDE HAL 4 38 Altera Corporation Nios II 2004 12