⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msemmain.c

📁 UCOSII V2.52 在三星SNDS4510开发板(国内基本所有的4510b的板子都是抄袭此公板设计的)上移植的成功例子
💻 C
字号:
/*
 * File:        main.c
 *
 * uC/OS Real-time multitasking kernel for the ARM processor.
 * Semphore testing program
 * function:
 * Task1,2,3 print their own ID number every 10ms,
 * in addition, Task2 flash a LED after it print its own ID number
 * the Semphore, LED_sem=1
 * If Task1 read a valid key, then Take up LED_sem for 80ms and rotate LEDs left
 * This will block Task2 from flashing the LED and printing its ID for 100ms,
 * 100ms later,Task1 will release LED_sem and Task2 can take up 
 * the LED_sem again for flashing the LED.  										  * 
 *    
 * 真正好用的信号量测试主程序,目的:
 * 正常情况Task1,2,3都打印各自的ID号,Task2每隔10ms来闪动一次小灯
 * LED_sem的初值为1
 * Task1来读取一个按键,
 * 如果有一个按键被按下的话,则Task1占用LED_sem信号量80ms并占用LED资源,控制LED小灯Rotate Left
 * Task2被阻塞,导致100ms内无法占用LED_sem,无法继续闪动小灯和打印自身ID号,
 * 100ms后Task1释放LED_sem信号量,Task2得以每10ms占用LED_sem一次,
 * 来闪动小灯。
 *
 * Modified by ygm(ygm@hlju.edu.cn).
 *
 */

#include "includes.h"               /* uC/OS interface */

#include "s3c4510.h"
#include "serial.h"
#include "timer.h"
#include "string.h"

//#include "lcd.h"

/* allocate memory for tasks' stacks */

#define	STACKSIZE	256		//256*4 Bytes

OS_STK Stack1[STACKSIZE];
OS_STK Stack2[STACKSIZE];
OS_STK Stack3[STACKSIZE];


/* semaphore event control blocks */
OS_EVENT	*LED_sem;

char PassMsg[] = "0123456789";

U8 ReadKey(void)  // Read Button from HJARM4510b board and return te KEY NUMBER
{
	U8	KeyBuffer=0x00;  // KeyBuffer store the Key number
	U32 KeyValue=0x0000;
    KeyValue=inport(IOPDATA);
	KeyValue&=0x0000FF00;
	if(KeyValue!=NOKEY)
	  {
        KeyBuffer=0x01;
        KeyValue=KeyValue>>9; // +1 because My board's LSB KEY is not work             
        while((KeyValue&0x01)!=0) 
        {
          KeyBuffer=KeyBuffer+1;
          KeyValue=KeyValue>>1;
        }

      }
    else
      {
      	KeyBuffer=0;
      }
      return KeyBuffer;
}

/*
 * Task running at the lowest priority. 
 * Wait for a message in the Readkey mailbox and post
 * a messages to the third mailbox.
 */
void Task1(void *Id)
{

	U8 err,i;
    U8	KeyValue;	// KeyValue is Key's  scancode 
	DBGMSG(UART0,"Task1 Called!\r\n");  	


    for (;;)
    {
    
        DBGMSG(UART0,"%c", *(char *)Id);
        OSTimeDly(10);  
	    if((KeyValue=ReadKey())!=NO_PRESS)
	    {
	      OSSemPend(LED_sem,0,&err);            
			for(i=1;i<8;i++)
			{
			  outport(IOPDATA, 1<<i);	
	          DBGMSG(UART0,"-");			  
			  OSTimeDly(10);	// in order to block Task2!				
			}
		  OSSemPost(LED_sem);	    
		}
	 }
}

void Task2(void *Id)
{
   //char *Msg;
    INT8U err,Led;
	DBGMSG(UART0,"Task2 Called!\r\n");  
	for (;;)
    {	
    OSSemPend(LED_sem,10000,&err);            
		Led ^= 1;
		outport(IOPDATA, Led);			
		OSTimeDly(10);  
        DBGMSG(UART0,"%c", *(char *)Id);		
	OSSemPost(LED_sem);	
	}

}


void Task3(void *Id)
{

//    INT8U err;	
    
    DBGMSG(UART0,"Task3() called\r\n");   

    for (;;)
    {
        /* wait for a message from the input mailbox */


        /* print task's id */
        DBGMSG(UART0,"%c", *(char *)Id);
//        OSSemPost(LED_sem);
		OSTimeDly(10);          

    }
}

OS_STK SysTimerStk[STACKSIZE];
void SysTimerTask(void *Id)
{
	//U8	err;
	//static U8 Led = 0;

	
	InitTimer(0, 1000, 0);
	InitTimer(1, 100, 0);	
	
	outport(INTMSK, ~(1<<INT_TIMER1|1<<INT_GLOBAL));

	DBGMSG(UART0,"Begin Run...\r\n");	
	
	for(;;)
	{
		DBGMSG(UART0,".");				
		OSTimeDly(10);  		
	}
}
////////////////////////////////////////////////////////


/* Main function. */
int main(void)
//#pragma import(__use_no_semihosting_swi)
{
	char Id1 = '1';
	char Id2 = '2';
	char Id3 = '3';	

	DBGMSG(UART0,"S3C4510b UCOSII MultiTasks DEMO 2005-12-2.\r\n");
	DBGMSG(UART0,"http://mes.hlju.edu.cn.\r\n");

	DBGMSG(UART0,"System Initialized.\r\n");
	DBGMSG(UART0,"Build Date:%s -- Time:%s.\r\n", __DATE__, __TIME__);
	

    /* needed by uC/OS */
    OSInit();
    
   	LED_sem=OSSemCreate(1);
	


    /* 
     * create the tasks in uC/OS and assign increasing
     * priorities to them so that Task3 at the end of
     * the pipeline has the highest priority.
     */
     
	OSTaskCreate(Task1, (void *)&Id1, Stack1+STACKSIZE, 4);
	OSTaskCreate(Task2, (void *)&Id2, Stack2+STACKSIZE, 3);
	OSTaskCreate(Task3, (void *)&Id3, Stack3+STACKSIZE, 2);
	
	OSTaskCreate(SysTimerTask, (void *)0, SysTimerStk+STACKSIZE, 1);    
   
    /* start the game */
    OSStart();

    /* never reached */
    return	0;
}                               /* main */

void IrqStart(void);
PrVoid IrqFinish(void);

PrVoid pIrqStart = IrqStart, pIrqHandler = OSTimeTick;
PrPrVoid pIrqFinish = IrqFinish;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -