Forum Discussion
Altera_Forum
Honored Contributor
19 years agoexamples application code:
/************************************************************************* * Copyright ? 2004 Altera Corporation, San Jose, California, USA. * * All rights reserved. All use of this software and documentation is * * subject to the License Agreement located at the end of this file below.* ************************************************************************** * Description: * * The following is a simple hello world program running MicroC/OS-II.The * * purpose of the design is to be a very simple application that just * * demonstrates MicroC/OS-II running on NIOS II.The design doesn't account* * for issues such as checking system call return codes. etc. * * * * Requirements: * * -Supported Example Hardware Platforms * * Standard * * Full Featured * * Low Cost * * -Supported Development Boards * * Nios II Development Board, Stratix II Edition * * Nios Development Board, Stratix Professional Edition * * Nios Development Board, Stratix Edition * * Nios Development Board, Cyclone Edition * * -System Library Settings * * RTOS Type - MicroC/OS-II * * Periodic System Timer * * -Know Issues * * If this design is run on the ISS, terminal output will take several* * minutes per iteration. * **************************************************************************/ # include <stdio.h># include <io.h># include <sys/types.h># include <sys/stat.h># include <fcntl.h># include <unistd.h># include "includes.h"# include "altera_avalon_spi_regs.h" /*这两个头文件必不可少*/# include "altera_avalon_spi.h" /* Definition of Task Stacks */# define TASK_STACKSIZE 2048 OS_STK task1_stk[TASK_STACKSIZE]; OS_STK task2_stk[TASK_STACKSIZE]; OS_STK task3_stk[TASK_STACKSIZE]; /* Definition of Task Priorities */ # define TASK1_PRIORITY 1# define TASK2_PRIORITY 2# define TASK3_PRIORITY 3# define BUFLEN 256 int master_fd; /* Prints "Hello World" and sleeps for three seconds */ void task1(void* pdata) { alt_u8 sendbuf[BUFLEN]; alt_u16 writelen,i; for(i=0;i<BUFLEN;i++) sendbuf = i;alt_u16 value=0;
ioctl(master_fd,spi_master_ioctl_select_slave,&value);
// return;
while (1)
{
writelen = write(master_fd,sendbuf,sizeof(sendbuf));
// printf("write %d\n",writelen);
ostimedlyhmsm(0, 0, 0, 50);
}
}
void task3(void* pdata)
{
alt_u8 recvbuf[buflen];
alt_u16 readlen,i,wi;
wi = 0;
while (1)
{
readlen = read(master_fd,recvbuf,sizeof(recvbuf));
for(i=0;i<readlen;i++){
printf("%2.2x ",recvbuf); wi = (wi+1)%16; if(wi==0) printf("\n"); } } } void task2(void* pdata) { alt_u8 recvbuf[BUFLEN]; alt_u16 readlen,i,wi; printf("Hello from task2\n"); int fd; if ((fd=open(SPI_SLAVE_NAME, O_RDWR, 0)) < 0) { perror("open spi"); return; } printf("fd=%d\n",fd); wi=0; while (1) { readlen = read(fd,recvbuf,sizeof(recvbuf)); write(fd,recvbuf,readlen);# if 0 for(i=0;i<readlen;i++){ printf("%2.2x ",recvbuf[i]); wi = (wi+1)%16; if(wi==0) printf("\n"); }# endif } close(fd); } /* The main function creates two task and starts multi-tasking */ int main(void) { if ((master_fd=open(SPI_MASTER_NAME, O_RDWR, 0)) < 0) { perror("open spi"); return; } OSTaskCreateExt(task1, NULL, (void *)&task1_stk[TASK_STACKSIZE], TASK1_PRIORITY, TASK1_PRIORITY, task1_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task3, NULL, (void *)&task3_stk[TASK_STACKSIZE], TASK3_PRIORITY, TASK3_PRIORITY, task3_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task2, NULL, (void *)&task2_stk[TASK_STACKSIZE], TASK2_PRIORITY, TASK2_PRIORITY, task2_stk, TASK_STACKSIZE, NULL, 0); OSStart(); return 0; }