add Nios II app

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1307 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
wuyangyong 2011-03-04 14:08:36 +00:00
parent 7bc4fe3b74
commit fb9a0952c8
9 changed files with 631 additions and 0 deletions

114
bsp/nios_ii/application.c Normal file
View File

@ -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 <rtthread.h>
#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<<n)
// trun off led n
#define rt_hw_led_off(n) IOWR_ALTERA_AVALON_PIO_DATA(\
LED_BASE,\
IORD_ALTERA_AVALON_PIO_DATA(LED_BASE) & ~(1<<n) )
ALIGN(RT_ALIGN_SIZE)
static char thread_led1_stack[1024];
struct rt_thread thread_led1;
static void rt_thread_entry_led1(void* parameter)
{
unsigned int count=0;
while (1)
{
/* led1 on */
#ifndef RT_USING_FINSH
rt_kprintf("led1 on,count : %d\r\n",count);
#endif
count++;
rt_hw_led_on(1);
/* sleep 0.5 second and switch to other thread */
rt_thread_delay(RT_TICK_PER_SECOND/2);
/* led1 off */
#ifndef RT_USING_FINSH
rt_kprintf("led1 off\r\n");
#endif
rt_hw_led_off(1);
rt_thread_delay(RT_TICK_PER_SECOND/2);
}
}
ALIGN(RT_ALIGN_SIZE)
static char thread_led2_stack[1024];
struct rt_thread thread_led2;
void rt_thread_entry_led2(void* parameter)
{
unsigned int count=0;
while (1)
{
/* led2 on */
#ifndef RT_USING_FINSH
rt_kprintf("led2 on,count : %d\r\n",count);
#endif
count++;
rt_hw_led_on(2);
rt_thread_delay(RT_TICK_PER_SECOND);
/* led2 off */
#ifndef RT_USING_FINSH
rt_kprintf("led2 off\r\n");
#endif
rt_hw_led_off(2);
rt_thread_delay(RT_TICK_PER_SECOND);
}
}
int rt_application_init()
{
// led_init();
//------- init led1 thread
rt_thread_init(&thread_led1,
"led1",
rt_thread_entry_led1,
RT_NULL,
&thread_led1_stack[0],
sizeof(thread_led1_stack),11,5);
rt_thread_startup(&thread_led1);
//------- init led2 thread
rt_thread_init(&thread_led2,
"led2",
rt_thread_entry_led2,
RT_NULL,
&thread_led2_stack[0],
sizeof(thread_led2_stack),12,5);
rt_thread_startup(&thread_led2);
return 0;
}
/*@}*/

87
bsp/nios_ii/board.c Normal file
View File

@ -0,0 +1,87 @@
/*
* File : board.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.
*/
#include <rthw.h>
#include <rtthread.h>
#include <stdio.h>
#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();
}
/*@}*/

22
bsp/nios_ii/board.h Normal file
View File

@ -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 <rthw.h>
extern void rt_hw_board_init(void);
#endif // BOARD_H_INCLUDED

39
bsp/nios_ii/readme_cn.txt Normal file
View File

@ -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 (默认为基本内核)

View File

@ -0,0 +1,2 @@
see readme_cn.txt
^_^

140
bsp/nios_ii/rtconfig.h Normal file
View File

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

99
bsp/nios_ii/startup.c Normal file
View File

@ -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 <rtthread.h>
#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;
}
/*@}*/

122
bsp/nios_ii/uart.c Normal file
View File

@ -0,0 +1,122 @@
#include <rthw.h>
#include <rtthread.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#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 <rtthread.h>
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);
}

6
bsp/nios_ii/uart.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef UART_H_INCLUDED
#define UART_H_INCLUDED
extern void rt_hw_uart_init(void);
#endif // UART_H_INCLUDED