From fb9a0952c8b33b73f711fabb9e2367e70fb84eab Mon Sep 17 00:00:00 2001 From: wuyangyong Date: Fri, 4 Mar 2011 14:08:36 +0000 Subject: [PATCH] add Nios II app git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1307 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/nios_ii/application.c | 114 +++++++++++++++++++++++++++++++ bsp/nios_ii/board.c | 87 +++++++++++++++++++++++ bsp/nios_ii/board.h | 22 ++++++ bsp/nios_ii/readme_cn.txt | 39 +++++++++++ bsp/nios_ii/readme_en.txt | 2 + bsp/nios_ii/rtconfig.h | 140 ++++++++++++++++++++++++++++++++++++++ bsp/nios_ii/startup.c | 99 +++++++++++++++++++++++++++ bsp/nios_ii/uart.c | 122 +++++++++++++++++++++++++++++++++ bsp/nios_ii/uart.h | 6 ++ 9 files changed, 631 insertions(+) create mode 100644 bsp/nios_ii/application.c create mode 100644 bsp/nios_ii/board.c create mode 100644 bsp/nios_ii/board.h create mode 100644 bsp/nios_ii/readme_cn.txt create mode 100644 bsp/nios_ii/readme_en.txt create mode 100644 bsp/nios_ii/rtconfig.h create mode 100644 bsp/nios_ii/startup.c create mode 100644 bsp/nios_ii/uart.c create mode 100644 bsp/nios_ii/uart.h diff --git a/bsp/nios_ii/application.c b/bsp/nios_ii/application.c new file mode 100644 index 000000000..eeca09a9f --- /dev/null +++ b/bsp/nios_ii/application.c @@ -0,0 +1,114 @@ +/* + * File : application.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006-2011, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2011-02-14 aozima first implementation for Nios II. + * 2011-03-04 aozima add led. + */ + +#include +#include "board.h" + +/** + * @addtogroup NIOS_II + */ + +/*@{*/ + +#include "system.h" +#include "altera_avalon_pio_regs.h" + +// trun on led n +#define rt_hw_led_on(n) IOWR_ALTERA_AVALON_PIO_DATA(\ + LED_BASE,\ + IORD_ALTERA_AVALON_PIO_DATA(LED_BASE) | 1< +#include + +#include +#include "system.h" +#include "sys/alt_irq.h" +#include "altera_avalon_timer_regs.h" + +#include "uart.h" + +extern int alt_irq_register (alt_u32 id, + void* context, + void (*alt_isr_func)(void* isr_context, alt_u32 id) ); + +/** + * @addtogroup NIOS_II + */ + +/*@{*/ + +/** + * This is the timer interrupt service routine. + * + */ +void rt_hw_timer_handler(void * context,unsigned long id) +{ + void* base = (void*)TIMER_BASE; + + /* clear the interrupt */ + IOWR_ALTERA_AVALON_TIMER_STATUS (base, 0); + + /* enter interrupt */ + rt_interrupt_enter(); + + rt_tick_increase(); + + /* leave interrupt */ + rt_interrupt_leave(); +} + +void sysTick_config(void) +{ + void* base = (void*)TIMER_BASE; + + IOWR_ALTERA_AVALON_TIMER_CONTROL (base, + ALTERA_AVALON_TIMER_CONTROL_ITO_MSK | + ALTERA_AVALON_TIMER_CONTROL_CONT_MSK | + ALTERA_AVALON_TIMER_CONTROL_START_MSK); + + alt_irq_register (TIMER_IRQ, NULL, rt_hw_timer_handler); +} + +static void rt_hw_show_info(void) +{ + rt_kprintf("\r\n\r\n---------- board info ----------\r\n"); + rt_kprintf("ALT_DEVICE_FAMILY: %s\r\n",ALT_DEVICE_FAMILY); + rt_kprintf("ALT_CPU_ARCHITECTURE: %s\r\n",ALT_CPU_ARCHITECTURE); + rt_kprintf("ALT_CPU_CPU_FREQ: %u\r\n",ALT_CPU_CPU_FREQ); + rt_kprintf("memory size: at 0x%08X 0x%08X byte\r\n",SDRAM_BASE,SDRAM_SPAN); +} + +void rt_hw_board_init(void) +{ + rt_hw_uart_init(); + rt_console_set_device("uart"); + rt_hw_show_info(); + + sysTick_config(); +} + +/*@}*/ + diff --git a/bsp/nios_ii/board.h b/bsp/nios_ii/board.h new file mode 100644 index 000000000..a9a7faeff --- /dev/null +++ b/bsp/nios_ii/board.h @@ -0,0 +1,22 @@ +/* + * File : board.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006-2011, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2011-02-14 aozima first implementation for Nios II. + */ + +#ifndef BOARD_H_INCLUDED +#define BOARD_H_INCLUDED + +#include + +extern void rt_hw_board_init(void); + +#endif // BOARD_H_INCLUDED diff --git a/bsp/nios_ii/readme_cn.txt b/bsp/nios_ii/readme_cn.txt new file mode 100644 index 000000000..87b97705a --- /dev/null +++ b/bsp/nios_ii/readme_cn.txt @@ -0,0 +1,39 @@ +1. 按常规方法建立一个空白Nios II工程. +2. 添加本目录的所有源程序和头文件,并添加RT-Thread内核及Nios II CPU所需要的文件. +─rt-thread + │ + ├─include + │ rtdef.h + │ rthw.h + │ rtm.h + │ rtthread.h + │ + ├─libcpu + │ └─nios + │ └─nios_ii + │ context_gcc.S + │ interrupt.c + │ stack.c + │ vector.S + │ + └─src + clock.c + device.c + idle.c + ipc.c + irq.c + kservice.c + kservice.h + mem.c + mempool.c + module.c + module.h + object.c + rtm.c + scheduler.c + SConscript + slab.c + thread.c + timer.c +3. 添加头文件搜索路径 +4. 根据需要修改rtconfig.h (默认为基本内核) diff --git a/bsp/nios_ii/readme_en.txt b/bsp/nios_ii/readme_en.txt new file mode 100644 index 000000000..b5a4eda5a --- /dev/null +++ b/bsp/nios_ii/readme_en.txt @@ -0,0 +1,2 @@ +see readme_cn.txt +^_^ \ No newline at end of file diff --git a/bsp/nios_ii/rtconfig.h b/bsp/nios_ii/rtconfig.h new file mode 100644 index 000000000..9248602ab --- /dev/null +++ b/bsp/nios_ii/rtconfig.h @@ -0,0 +1,140 @@ +/* RT-Thread config file */ +#ifndef __RTTHREAD_CFG_H__ +#define __RTTHREAD_CFG_H__ + +/* RT_NAME_MAX*/ +#define RT_NAME_MAX 8 + +/* RT_ALIGN_SIZE*/ +#define RT_ALIGN_SIZE 4 + +/* PRIORITY_MAX */ +#define RT_THREAD_PRIORITY_MAX 32 + +/* Tick per Second */ +/* TIMER_TICKS_PER_SEC define in system.h */ +#define RT_TICK_PER_SECOND 1000 //TIMER_TICKS_PER_SEC + +/* SECTION: RT_DEBUG */ +/* Thread Debug */ +#define RT_DEBUG +//#define THREAD_DEBUG +//#define SCHEDULER_DEBUG +//#define IRQ_DEBUG + +#define IDLE_THREAD_STACK_SIZE 1024 + +#define RT_USING_OVERFLOW_CHECK + +/* Using Hook */ +#define RT_USING_HOOK + +/* Using Software Timer */ +/* #define RT_USING_TIMER_SOFT */ +#define RT_TIMER_THREAD_PRIO 4 +#define RT_TIMER_THREAD_STACK_SIZE 512 +#define RT_TIMER_TICK_PER_SECOND 10 + +/* SECTION: IPC */ +/* Using Semaphore*/ +#define RT_USING_SEMAPHORE + +/* Using Mutex */ +#define RT_USING_MUTEX + +/* Using Event */ +#define RT_USING_EVENT + +/* Using MailBox */ +#define RT_USING_MAILBOX + +/* Using Message Queue */ +#define RT_USING_MESSAGEQUEUE + +/* SECTION: Memory Management */ +/* Using Memory Pool Management*/ +#define RT_USING_MEMPOOL + +/* Using Dynamic Heap Management */ +#define RT_USING_HEAP + +/* Using Small MM */ +#define RT_USING_SMALL_MEM + +/* SECTION: Device System */ +/* Using Device System */ +#define RT_USING_DEVICE +#define RT_USING_UART1 + +/* SECTION: Console options */ +/* the buffer size of console*/ +#define RT_CONSOLEBUF_SIZE 128 + +#define RT_USING_NEWLIB + +/* SECTION: finsh, a C-Express shell */ +#define RT_USING_FINSH +/* Using symbol table */ +#define FINSH_USING_SYMTAB +#define FINSH_USING_DESCRIPTION +#define __fsymtab_start _alt_partition_FSymTab_start +#define __fsymtab_end _alt_partition_FSymTab_end +#define __vsymtab_start _alt_partition_VSymTab_start +#define __vsymtab_end _alt_partition_VSymTab_end + +/* SECTION: device filesystem */ +//#define RT_USING_DFS +#define RT_USING_DFS_ELMFAT + +/* the max number of mounted filesystem */ +#define DFS_FILESYSTEMS_MAX 2 +/* the max number of opened files */ +#define DFS_FD_MAX 4 +/* the max number of cached sector */ +#define DFS_CACHE_MAX_NUM 4 + +/* SECTION: lwip, a lighwight TCP/IP protocol stack */ +//#define RT_USING_LWIP +/* LwIP uses RT-Thread Memory Management */ +#define RT_LWIP_USING_RT_MEM +/* Enable ICMP protocol*/ +#define RT_LWIP_ICMP +/* Enable UDP protocol*/ +#define RT_LWIP_UDP +/* Enable TCP protocol*/ +#define RT_LWIP_TCP +/* Enable DNS */ +#define RT_LWIP_DNS + +/* the number of simulatenously active TCP connections*/ +#define RT_LWIP_TCP_PCB_NUM 5 + +/* ip address of target*/ +#define RT_LWIP_IPADDR0 192 +#define RT_LWIP_IPADDR1 168 +#define RT_LWIP_IPADDR2 1 +#define RT_LWIP_IPADDR3 30 + +/* gateway address of target*/ +#define RT_LWIP_GWADDR0 192 +#define RT_LWIP_GWADDR1 168 +#define RT_LWIP_GWADDR2 1 +#define RT_LWIP_GWADDR3 1 + +/* mask address of target*/ +#define RT_LWIP_MSKADDR0 255 +#define RT_LWIP_MSKADDR1 255 +#define RT_LWIP_MSKADDR2 255 +#define RT_LWIP_MSKADDR3 0 + +/* tcp thread options */ +#define RT_LWIP_TCPTHREAD_PRIORITY 12 +#define RT_LWIP_TCPTHREAD_MBOX_SIZE 4 +#define RT_LWIP_TCPTHREAD_STACKSIZE 1024 + +/* ethernet if thread options */ +#define RT_LWIP_ETHTHREAD_PRIORITY 15 +#define RT_LWIP_ETHTHREAD_MBOX_SIZE 4 +#define RT_LWIP_ETHTHREAD_STACKSIZE 512 + +#endif diff --git a/bsp/nios_ii/startup.c b/bsp/nios_ii/startup.c new file mode 100644 index 000000000..2f5a6a27b --- /dev/null +++ b/bsp/nios_ii/startup.c @@ -0,0 +1,99 @@ +/* + * File : startup.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006-2011, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2011-02-14 aozima first implementation for Nios II. + * 2011-03-04 aozima add HEAP and finsh support . + */ + +#include +#include "system.h" +#include "board.h" + +/** + * @addtogroup NIOS_II + */ + +/*@{*/ + +extern int rt_application_init(void); + +#ifdef RT_USING_FINSH +extern void finsh_system_init(void); +extern void finsh_set_device(const char* device); +#endif + +#ifdef RT_USING_HEAP +extern int _alt_partition_sdram_load_addr; +#endif + +/** + * This function will startup RT-Thread RTOS. + */ +void rtthread_startup(void) +{ + /* init board */ + rt_hw_board_init(); + + /* show version */ + rt_show_version(); + + /* init tick */ + rt_system_tick_init(); + + /* init kernel object */ + rt_system_object_init(); + + /* init timer system */ + rt_system_timer_init(); + +#ifdef RT_USING_HEAP + rt_system_heap_init( &_alt_partition_sdram_load_addr, (void*)(SDRAM_BASE + SDRAM_SPAN) ); +#endif + + /* init scheduler system */ + rt_system_scheduler_init(); + + /* init application */ + rt_application_init(); + + /* init timer thread */ + rt_system_timer_thread_init(); + +#ifdef RT_USING_FINSH + /* init finsh */ + finsh_system_init(); + finsh_set_device("uart"); +#endif + + /* init idle thread */ + rt_thread_idle_init(); + + /* start scheduler */ + rt_system_scheduler_start(); + + /* never reach here */ + return ; +} + +int main(void) +{ + rt_uint32_t UNUSED level; + + /* disable interrupt first */ + level = rt_hw_interrupt_disable(); + + /* startup RT-Thread RTOS */ + rtthread_startup(); + + return 0; +} + +/*@}*/ diff --git a/bsp/nios_ii/uart.c b/bsp/nios_ii/uart.c new file mode 100644 index 000000000..463b02e34 --- /dev/null +++ b/bsp/nios_ii/uart.c @@ -0,0 +1,122 @@ +#include +#include + + +#include +#include +#include + +#include "system.h" +#include "sys/alt_irq.h" +#include "altera_avalon_uart_regs.h" + +extern int alt_irq_register (alt_u32 id, + void* context, + void (*alt_isr_func)(void* isr_context, alt_u32 id) ); + +static void set_baudrate(unsigned int baudrate) +{ + IOWR_ALTERA_AVALON_UART_DIVISOR(RS232_BASE, + (unsigned int)(ALT_CPU_FREQ/baudrate+0.5) ); +} + +/********* rt-thread *********/ +#include + +struct rt_device uart_device; +uint8_t rx_buf[100]; +uint32_t rx_put_index; +uint32_t rx_get_index; + +static rt_err_t rt_uart_init (rt_device_t dev) +{ + set_baudrate(115200); + + IOWR_ALTERA_AVALON_UART_CONTROL(RS232_BASE, 0x80);//接收中断使能 + IOWR_ALTERA_AVALON_UART_STATUS(RS232_BASE, 0x0); // clean status + + rx_put_index = 0; + rx_get_index = 0; + + return RT_EOK; +} + +static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag) +{ + return RT_EOK; +} + +static rt_err_t rt_uart_close(rt_device_t dev) +{ + return RT_EOK; +} + +static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) +{ + if( rx_get_index ) + { + *(uint8_t *)buffer = rx_buf[0]; + rx_get_index--; + return size; + } + return 0; +} + +static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) +{ + const char * write_point = buffer; + while(size--) + { + if(*write_point == '\n') + { + IOWR_ALTERA_AVALON_UART_TXDATA(RS232_BASE,'\r'); + while( !(IORD_ALTERA_AVALON_UART_STATUS(RS232_BASE)&(1<<6)) ); // status bit6 : TRDY + } + IOWR_ALTERA_AVALON_UART_TXDATA(RS232_BASE,*write_point); + write_point++; + while( !(IORD_ALTERA_AVALON_UART_STATUS(RS232_BASE)&(1<<6)) ); // status bit6 : TRDY + } + + return size; +} + +static rt_err_t rt_uart_control(rt_device_t dev, rt_uint8_t cmd, void *args) +{ + return RT_EOK; +} + +static void uart_isr(void * context,alt_u32 id) +{ + rx_buf[rx_get_index] = IORD_ALTERA_AVALON_UART_RXDATA(RS232_BASE); + rx_get_index++; + if (uart_device.rx_indicate != RT_NULL) + { + uart_device.rx_indicate(&uart_device, 1); + } +} + +void rt_hw_uart_init(void) +{ + // init uart + set_baudrate(115200); + IOWR_ALTERA_AVALON_UART_CONTROL(RS232_BASE, 0x80);//接收中断使能 + IOWR_ALTERA_AVALON_UART_STATUS(RS232_BASE, 0x0); // clean status + alt_irq_register(RS232_IRQ, NULL, uart_isr); + + // register device + uart_device.type = RT_Device_Class_Char; + /* device interface */ + uart_device.init = rt_uart_init; + uart_device.open = rt_uart_open; + uart_device.close = rt_uart_close; + uart_device.read = rt_uart_read; + uart_device.write = rt_uart_write; + uart_device.control = rt_uart_control; + + uart_device.user_data = RT_NULL; + uart_device.rx_indicate = RT_NULL; + uart_device.tx_complete = RT_NULL; + + rt_device_register(&uart_device, + "uart", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX); +} diff --git a/bsp/nios_ii/uart.h b/bsp/nios_ii/uart.h new file mode 100644 index 000000000..8fa5cb976 --- /dev/null +++ b/bsp/nios_ii/uart.h @@ -0,0 +1,6 @@ +#ifndef UART_H_INCLUDED +#define UART_H_INCLUDED + +extern void rt_hw_uart_init(void); + +#endif // UART_H_INCLUDED -- GitLab