找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3292|回复: 0
打印 上一主题 下一主题
收起左侧

ARM9之2440之os-ii

[复制链接]
跳转到指定楼层
楼主
ID:82781 发表于 2015-6-13 16:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "config.h"

  2. extern char Image$RW$Limit[];
  3. void *mallocPt=Image$RW$Limit;

  4. //***************************[ SYSTEM ]***************************************************
  5. static int delayLoopCount;

  6. void Delay(int time)
  7. {
  8.       // time=0: adjust the Delay function by WatchDog timer.
  9.       // time>0: the number of loop time
  10.       // resolution of time is 100us.
  11.         int i, adjust=0;

  12.     if(time==0)
  13.     {
  14.                time   = 200;
  15.         adjust = 1;
  16.         delayLoopCount = 800;

  17.             //PCLK/1M,Watch-dog disable,1/64,interrupt disable,reset disable
  18.         rWTCON = ((PCLK/1000000-1)<<8)|(2<<3);
  19.         rWTDAT = 0xffff;                              //for first update
  20.         rWTCNT = 0xffff;                              //resolution=64us @any PCLK
  21. rWTCON = ((PCLK/1000000-1)<<8)|(2<<3)|(1<<5); //Watch-dog timer start
  22.     }

  23.     for(;time>0;time--)
  24.                for(i=0;i<delayLoopCount;i++);

  25.     if(adjust==1)
  26.     {
  27.         rWTCON = ((PCLK/1000000-1)<<8)|(2<<3); //Watch-dog timer stop
  28.         i = 0xffff - rWTCNT;                     //1count->64us, 200*800 cycle runtime = 64*i us

  29. //Uart_Printf("\nrWTCNT=%x ", rWTCNT);
  30. //Uart_Printf("\ni (0xffff -rWTCNT)=%d", i);
  31.         delayLoopCount = 16000000/(i*64);         //200*800:64*i=1*x:100 -> x=160000*100/(64*i)   
  32.         //Uart_Printf("\ndelayLoopCount=%d", delayLoopCount);
  33.     }
  34.    
  35. }


  36. //***************************[ PORTS ]****************************************************
  37. void Port_Init(void)
  38. {
  39.     //CAUTION:Follow the configuration order for setting the ports.
  40.     // 1) setting value(GPnDAT)
  41.     // 2) setting control register  (GPnCON)
  42.     // 3) configure pull-up resistor(GPnUP)  

  43.     //32bit data bus configuration  
  44.     //*** PORT A GROUP
  45.     //Ports  : GPA22 GPA21  GPA20 GPA19 GPA18 GPA17 GPA16 GPA15 GPA14 GPA13 GPA12  
  46.     //Signal : nFCE nRSTOUT nFRE   nFWE  ALE   CLE  nGCS5 nGCS4 nGCS3 nGCS2 nGCS1
  47.     //Binary :  1     1      1  , 1   1   1    1   ,  1     1     1     1
  48.     //Ports  : GPA11   GPA10  GPA9   GPA8   GPA7   GPA6   GPA5   GPA4   GPA3   GPA2   GPA1  GPA0
  49.     //Signal : ADDR26 ADDR25 ADDR24 ADDR23 ADDR22 ADDR21 ADDR20 ADDR19 ADDR18 ADDR17 ADDR16 ADDR0
  50.     //Binary :  1       1      1      1   , 1       1      1      1   ,  1       1     1      1         
  51.     rGPACON = 0x7fffff;

  52.     //**** PORT B GROUP
  53.     //Ports  : GPB10    GPB9    GPB8    GPB7    GPB6     GPB5    GPB4   GPB3   GPB2     GPB1      GPB0
  54.     //Signal : nXDREQ0 nXDACK0 nXDREQ1 nXDACK1 nSS_KBD nDIS_OFF L3CLOCK L3DATA L3MODE nIrDATXDEN Keyboard
  55.     //Setting: OUTPUT  OUTPUT   OUTPUT  OUTPUT   OUTPUT   OUTPUT   OUTPUT OUTPUT OUTPUT   OUTPUT    OUTPUT
  56.     //Binary :   01  ,  01       01  ,   01      01   ,  01       01  ,   01     01   ,  01        01  
  57.     rGPBCON = 0x155555;
  58.     rGPBUP  = 0x7ff;     // The pull up function is disabled GPB[10:0]

  59.     //*** PORT C GROUP
  60.     //Ports  : GPC15 GPC14 GPC13 GPC12 GPC11 GPC10 GPC9 GPC8  GPC7   GPC6   GPC5 GPC4 GPC3  GPC2  GPC1 GPC0
  61.     //Signal : VD7   VD6   VD5   VD4   VD3   VD2   VD1  VD0 LCDVF2 LCDVF1 LCDVF0 VM VFRAME VLINE VCLK LEND  
  62.     //Binary :  10   10  , 10    10  , 10    10  , 10   10  , 10     10  ,  10   10 , 10     10 , 10   10
  63.     rGPCCON = 0xaaaaaaaa;      
  64.     rGPCUP  = 0xffff;     // The pull up function is disabled GPC[15:0]

  65.     //*** PORT D GROUP
  66.     //Ports  : GPD15 GPD14 GPD13 GPD12 GPD11 GPD10 GPD9 GPD8 GPD7 GPD6 GPD5 GPD4 GPD3 GPD2 GPD1 GPD0
  67.     //Signal : VD23  VD22  VD21  VD20  VD19  VD18  VD17 VD16 VD15 VD14 VD13 VD12 VD11 VD10 VD9  VD8
  68.     //Binary : 10    10  , 10    10  , 10    10  , 10   10 , 10   10 , 10   10 , 10   10 ,10   10
  69.     rGPDCON = 0xaaaaaaaa;      
  70.     rGPDUP  = 0xffff;     // The pull up function is disabled GPD[15:0]

  71.     //*** PORT E GROUP
  72.     //Ports  : GPE15  GPE14 GPE13   GPE12   GPE11   GPE10   GPE9    GPE8     GPE7  GPE6  GPE5   GPE4  
  73.     //Signal : IICSDA IICSCL SPICLK SPIMOSI SPIMISO SDDATA3 SDDATA2 SDDATA1 SDDATA0 SDCMD SDCLK I2SSDO
  74.     //Binary :  10     10  ,  10      10  ,  10      10   ,  10      10   ,   10    10  , 10     10  ,     
  75.     //-------------------------------------------------------------------------------------------------------
  76.     //Ports  :  GPE3   GPE2  GPE1    GPE0   
  77.     //Signal : I2SSDI CDCLK I2SSCLK I2SLRCK     
  78.     //Binary :  10     10  ,  10      10
  79.     rGPECON = 0xaaaaaaaa;      
  80.     rGPEUP  = 0xffff;     // The pull up function is disabled GPE[15:0]

  81.     //*** PORT F GROUP
  82.     //Ports  : GPF7   GPF6   GPF5   GPF4      GPF3     GPF2  GPF1   GPF0
  83.     //Signal : nLED_8 nLED_4 nLED_2 nLED_1 nIRQ_PCMCIA EINT2 KBDINT EINT0
  84.     //Setting: Output Output Output Output    EINT3    EINT2 EINT1  EINT0
  85.     //Binary :  01      01 ,  01     01  ,     10       10  , 10     10
  86.     rGPFCON = 0x55aa;
  87.     rGPFUP  = 0xff;     // The pull up function is disabled GPF[7:0]

  88.     //*** PORT G GROUP
  89.     //Ports  : GPG15 GPG14 GPG13 GPG12 GPG11    GPG10    GPG9     GPG8     GPG7      GPG6   
  90.     //Signal : nYPON  YMON nXPON XMON  EINT19 DMAMODE1 DMAMODE0 DMASTART KBDSPICLK KBDSPIMOSI
  91.     //Setting: nYPON  YMON nXPON XMON  EINT19  Output   Output   Output   SPICLK1    SPIMOSI1
  92.     //Binary :   11    11 , 11    11  , 10      01    ,   01       01   ,    11         11
  93.     //-----------------------------------------------------------------------------------------
  94.     //Ports  :    GPG5       GPG4    GPG3    GPG2    GPG1    GPG0   
  95.     //Signal : KBDSPIMISO LCD_PWREN EINT11 nSS_SPI IRQ_LAN IRQ_PCMCIA
  96.     //Setting:  SPIMISO1  LCD_PWRDN EINT11   nSS0   EINT9    EINT8
  97.     //Binary :     11         11   ,  10      11  ,  10        10
  98.     rGPGCON = 0xff95ffba;
  99.     rGPGUP  = 0xffff;    // The pull up function is disabled GPG[15:0]

  100.     //*** PORT H GROUP
  101.     //Ports  :  GPH10    GPH9  GPH8 GPH7  GPH6  GPH5 GPH4 GPH3 GPH2 GPH1  GPH0
  102.     //Signal : CLKOUT1 CLKOUT0 UCLK nCTS1 nRTS1 RXD1 TXD1 RXD0 TXD0 nRTS0 nCTS0
  103.     //Binary :   10   ,  10     10 , 11    11  , 10   10 , 10   10 , 10    10
  104.     rGPHCON = 0x2afaaa;
  105.     rGPHUP  = 0x7ff;    // The pull up function is disabled GPH[10:0]

  106.     // Added for S3C2440, DonGo
  107.     //PORT J GROUP
  108.     //Ports        :  GPJ12    GPJ11     GPJ10        GPJ9  GPJ8      GPJ7        GPJ6  GPJ5        GPJ4  GPJ3  GPJ2  GPJ1  GPJ0
  109.     //Signal : CAMRESET CAMCLKOUT CAMHREF CAMVS CAMPCLKIN CAMD7 CAMD6 CAMD5 CAMD4 CAMD3 CAMD2 CAMD1 CAMD0
  110.     //Setting: Out      Out      CAMHREF CAMVS CAMPCLKIN CAMD7 CAMD6 CAMD5 CAMD4 CAMD3 CAMD2 CAMD1 CAMD0
  111.     //Binary : 01         01        10      10    10        10    10    10    10    10    10    10    10
  112.     //PU_OFF : 1         0 1           1     1         1     1     1        1        1     1     1     1
  113.     //---------------------------------------------------------------------------------------
  114.     rGPJDAT = (1<<12)|(0<<11);
  115.     rGPJCON = 0x016aaaa;
  116.     rGPJUP        = ~((0<<12)|(1<<11));

  117.     rGPJDAT = (0<<12)|(0<<11);
  118.     rGPJCON = 0x016aaaa;
  119.     rGPJUP        = 0x1fff;//~((1<<12)|(1<<11));

  120.     //rGPJCON = 0x02aaaaaa;
  121.     //rGPJUP  = 0x1fff;    // The pull up function is disabled GPH[10:0]

  122.     //External interrupt will be falling edge triggered.
  123.     rEXTINT0 = 0x22222222;    // EINT[7:0]
  124.     rEXTINT1 = 0x22222222;    // EINT[15:8]
  125.     rEXTINT2 = 0x22222222;    // EINT[23:16]
  126. }


  127. //***************************[ UART ]******************************
  128. static int whichUart=0;

  129. void Uart_Init(int pclk,int baud)
  130. {
  131.     int i;
  132.     if(pclk == 0)
  133.     pclk    = PCLK;
  134.     rUFCON0 = 0x0;   //UART channel 0 FIFO control register, FIFO disable
  135.     rUFCON1 = 0x0;   //UART channel 1 FIFO control register, FIFO disable
  136.     rUFCON2 = 0x0;   //UART channel 2 FIFO control register, FIFO disable
  137.     rUMCON0 = 0x0;   //UART chaneel 0 MODEM control register, AFC disable
  138.     rUMCON1 = 0x0;   //UART chaneel 1 MODEM control register, AFC disable
  139. //UART0
  140.     rULCON0 = 0x3;   //Line control register : Normal,No parity,1 stop,8 bits
  141.      //    [10]       [9]     [8]        [7]        [6]      [5]         [4]           [3:2]        [1:0]
  142.      // Clock Sel,  Tx Int,  Rx Int, Rx Time Out, Rx err, Loop-back, Send break,  Transmit Mode, Receive Mode
  143.      //     0          1       0    ,     0          1        0           0     ,       01          01
  144.      //   PCLK       Level    Pulse    Disable    Generate  Normal      Normal        Interrupt or Polling
  145.     rUCON0  = 0x245;   // Control register
  146.     rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 );   //Baud rate divisior register 0
  147. //UART1
  148.     rULCON1 = 0x3;
  149.     rUCON1  = 0x245;
  150.     rUBRDIV1=( (int)(pclk/16./baud+0.5) -1 );
  151. //UART2
  152.     rULCON2 = 0x3;
  153.     rUCON2  = 0x245;
  154.     rUBRDIV2=( (int)(pclk/16./baud+0.5) -1 );   

  155. Uart_TxEmpty(whichUart);
  156.     //for(i=0;i<100;i++);
  157. }

  158. //===================================================================
  159. void Uart_Select(int ch)
  160. {
  161.     whichUart = ch;
  162. }

  163. //===================================================================
  164. void Uart_TxEmpty(int ch)
  165. {
  166.     if(ch==0)
  167.         while(!(rUTRSTAT0 & 0x4)); //Wait until tx shifter is empty.
  168.          
  169.     else if(ch==1)
  170.         while(!(rUTRSTAT1 & 0x4)); //Wait until tx shifter is empty.
  171.         
  172.     else if(ch==2)
  173.         while(!(rUTRSTAT2 & 0x4)); //Wait until tx shifter is empty.
  174. }

  175. //=====================================================================
  176. char Uart_Getch(void)
  177. {
  178.     if(whichUart==0)
  179.     {      
  180.         while(!(rUTRSTAT0 & 0x1)); //Receive data ready
  181.         return RdURXH0();
  182.     }
  183.     else if(whichUart==1)
  184.     {      
  185.         while(!(rUTRSTAT1 & 0x1)); //Receive data ready
  186.         return RdURXH1();
  187.     }
  188.     else if(whichUart==2)
  189.     {
  190.         while(!(rUTRSTAT2 & 0x1)); //Receive data ready
  191.         return RdURXH2();
  192.     }
  193. }

  194. //====================================================================
  195. char Uart_GetKey(void)
  196. {
  197.     if(whichUart==0)
  198.     {      
  199.         if(rUTRSTAT0 & 0x1)    //Receive data ready
  200.             return RdURXH0();
  201.         else
  202.             return 0;
  203.     }
  204.     else if(whichUart==1)
  205.     {
  206.         if(rUTRSTAT1 & 0x1)    //Receive data ready
  207.             return RdURXH1();
  208.         else
  209.             return 0;
  210.     }
  211.     else if(whichUart==2)
  212.     {      
  213.         if(rUTRSTAT2 & 0x1)    //Receive data ready
  214.             return RdURXH2();
  215.         else
  216.             return 0;
  217.     }   
  218. }

  219. //====================================================================
  220. void Uart_GetString(char *string)
  221. {
  222.     char *string2 = string;
  223.     char c;
  224.     while((c = Uart_Getch())!='\r')
  225.     {
  226.         if(c=='\b')
  227.         {
  228.             if( (int)string2 < (int)string )
  229.             {
  230.                 Uart_Printf("\b \b");
  231.                 string--;
  232.             }
  233.         }
  234.         else
  235.         {
  236.             *string++ = c;
  237.             Uart_SendByte(c);
  238.         }
  239.     }
  240.     *string='\0';
  241.     Uart_SendByte('\n');
  242. }

  243. //=====================================================================
  244. int Uart_GetIntNum(void)
  245. {
  246.     char str[30];
  247.     char *string = str;
  248.     int base     = 10;
  249.     int minus    = 0;
  250.     int result   = 0;
  251.     int lastIndex;   
  252.     int i;
  253.    
  254.     Uart_GetString(string);
  255.    
  256.     if(string[0]=='-')
  257.     {
  258.         minus = 1;
  259.         string++;
  260.     }
  261.    
  262.     if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
  263.     {
  264.         base    = 16;
  265.         string += 2;
  266.     }
  267.    
  268.     lastIndex = strlen(string) - 1;
  269.    
  270.     if(lastIndex<0)
  271.         return -1;
  272.    
  273.     if(string[lastIndex]=='h' || string[lastIndex]=='H' )
  274.     {
  275.         base = 16;
  276.         string[lastIndex] = 0;
  277.         lastIndex--;
  278.     }

  279.     if(base==10)
  280.     {
  281.         result = atoi(string);
  282.         result = minus ? (-1*result):result;
  283.     }
  284.     else
  285.     {
  286.         for(i=0;i<=lastIndex;i++)
  287.         {
  288.             if(isalpha(string[i]))
  289.             {
  290.                 if(isupper(string[i]))
  291.                     result = (result<<4) + string[i] - 'A' + 10;
  292.                 else
  293.                     result = (result<<4) + string[i] - 'a' + 10;
  294.             }
  295.             else
  296.                 result = (result<<4) + string[i] - '0';
  297.         }
  298.         result = minus ? (-1*result):result;
  299.     }
  300.     return result;
  301. }

  302. //=====================================================================
  303. void Uart_SendByte(int data)
  304. {
  305.     if(whichUart==0)
  306.     {
  307.         if(data=='\n')
  308.         {
  309.             while(!(rUTRSTAT0 & 0x2));
  310.             Delay(10);                 //because the slow response of hyper_terminal
  311.             WrUTXH0('\r');
  312.         }
  313.         while(!(rUTRSTAT0 & 0x2));   //Wait until THR is empty.
  314.         Delay(10);
  315.         WrUTXH0(data);
  316.     }
  317.     else if(whichUart==1)
  318.     {
  319.         if(data=='\n')
  320.         {
  321.             while(!(rUTRSTAT1 & 0x2));
  322.             Delay(10);                 //because the slow response of hyper_terminal
  323.             rUTXH1 = '\r';
  324.         }
  325.         while(!(rUTRSTAT1 & 0x2));   //Wait until THR is empty.
  326.         Delay(10);
  327.         rUTXH1 = data;
  328.     }   
  329.     else if(whichUart==2)
  330.     {
  331.         if(data=='\n')
  332.         {
  333.             while(!(rUTRSTAT2 & 0x2));
  334.             Delay(10);                 //because the slow response of hyper_terminal
  335.             rUTXH2 = '\r';
  336.         }
  337.         while(!(rUTRSTAT2 & 0x2));   //Wait until THR is empty.
  338.         Delay(10);
  339.         rUTXH2 = data;
  340.     }      
  341. }               

  342. //====================================================================
  343. void Uart_SendString(char *pt)
  344. {
  345.     while(*pt)
  346.         Uart_SendByte(*pt++);
  347. }

  348. #if !USE_MAIN
  349. //If you don't use vsprintf(), the code size is reduced very much.
  350. void Uart_Printf(const char *fmt,...)
  351. {
  352.     va_list ap;
  353.     char string[50];

  354.     va_start(ap,fmt);
  355.     vsprintf(string,fmt,ap);
  356.     va_end(ap);
  357.     Uart_SendString(string);
  358. }
  359. #endif

  360. //*************************[ Timer ]********************************
  361. void Timer_Start(int divider)  //0:16us,1:32us 2:64us 3:128us
  362. {
  363.     rWTCON = ((PCLK/1000000-1)<<8)|(divider<<3);  //Watch-dog timer control register
  364.     rWTDAT = 0xffff;  //Watch-dog timer data register
  365.     rWTCNT = 0xffff;  //Watch-dog count register

  366.       // Watch-dog timer enable & interrupt  disable
  367.     rWTCON = (rWTCON & ~(1<<5) & ~(1<<2)) |(1<<5);
  368. }

  369. //=================================================================
  370. int Timer_Stop(void)
  371. {
  372.     rWTCON = ((PCLK/1000000-1)<<8);
  373.     return (0xffff - rWTCNT);
  374. }


  375. //*************************[ MPLL ]*******************************
  376. void ChangeMPllValue(int mdiv,int pdiv,int sdiv)
  377. {
  378.     rMPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
  379. }


  380. //************************[ HCLK, PCLK ]***************************
  381. void ChangeClockDivider(int hdivn_val,int pdivn_val)
  382. {
  383. int hdivn=2, pdivn=0;
  384.      // hdivn_val (FCLK:HCLK)ratio hdivn
  385.      // 11           1:1       (0)
  386.      // 12           1:2       (1)
  387.      // 13           1:3       (3)
  388.      // 14           1:4       (2)
  389.      // pdivn_val (HCLK:PCLK)ratio pdivn
  390.      // 11           1:1       (0)
  391.      // 12           1:2       (1)
  392. switch(hdivn_val) {
  393. case 11: hdivn=0; break;
  394. case 12: hdivn=1; break;
  395. case 13: hdivn=3; break;
  396. case 16: hdivn=3; break;
  397. case 14: hdivn=2; break;
  398. case 18: hdivn=2; break;
  399. }
  400. switch(pdivn_val) {
  401. case 11: pdivn=0; break;
  402. case 12: pdivn=1; break;
  403. }
  404. //Uart_Printf("Clock division change [hdiv:%x, pdiv:%x]\n", hdivn, pdivn);
  405. rCLKDIVN = (hdivn<<1) | pdivn;
  406. //Uart_Printf("rCLKDIVN:%x]\n", rCLKDIVN);

  407. switch(hdivn_val) {
  408. case 16:        // when 1, HCLK=FCLK/6.
  409. rCAMDIVN = (rCAMDIVN & ~(3<<8)) | (1<<8);
  410. break;
  411. case 18: // when 1, HCLK=FCLK/8.
  412. rCAMDIVN = (rCAMDIVN & ~(3<<8)) | (1<<9);
  413. break;
  414. }
  415. //Uart_Printf("rCAMDIVN:%x]\n", rCAMDIVN);
  416.     if(hdivn!=0)
  417.         MMU_SetAsyncBusMode();
  418.     else
  419.         MMU_SetFastBusMode();
  420. }



  421. //**************************[ UPLL ]*******************************
  422. void ChangeUPllValue(int mdiv,int pdiv,int sdiv)
  423. {
  424.     rUPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
  425. }

  426. /************************【实时时钟】*********************************/
  427. void Rtc_Init(void)
  428. {
  429. int wYear, wMonth,wDay,wDayOfWeek,wHour,wMinute,wSecond;

  430. wYear = 2009;
  431. wMonth = 3;
  432. wDay = 5;
  433. wDayOfWeek = 4;
  434. wHour = 12;
  435. wMinute = 00;
  436. wSecond = 00;

  437. rRTCCON = 1 ;        //RTC read and write enable

  438. rBCDYEAR = (unsigned char)TO_BCD(wYear%100);        //年
  439. rBCDMON  = (unsigned char)TO_BCD(wMonth);        //月
  440. rBCDDAY        = (unsigned char)TO_BCD(wDay);        //日       
  441. rBCDDATE = wDayOfWeek+1;        //星期
  442. rBCDHOUR = (unsigned char)TO_BCD(wHour);        //小时
  443. rBCDMIN  = (unsigned char)TO_BCD(wMinute);        //分
  444. rBCDSEC  = (unsigned char)TO_BCD(wSecond);        //秒

  445. rRTCCON &= ~1 ;        //RTC read and write disable
  446. }

  447. /*****************************【蜂鸣器】************************************/
  448. void Buzzer_Freq_Set( U32 freq )
  449. {
  450. rGPBCON &= ~3;        //set GPB0 as tout0, pwm output
  451. rGPBCON |= 2;

  452. rTCFG0 &= ~0xff;
  453. rTCFG0 |= 15;        //prescaler = 15+1
  454. rTCFG1 &= ~0xf;
  455. rTCFG1 |= 2;        //mux = 1/8
  456. rTCNTB0 = (PCLK>>7)/freq;
  457. rTCMPB0 = rTCNTB0>>1;        // 50%
  458. rTCON &= ~0x1f;
  459. rTCON |= 0xb;        //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
  460. rTCON &= ~2;        //clear manual update bit
  461. }

  462. void Buzzer_Stop( void )
  463. {
  464. rGPBCON &= ~3;        //set GPB0 as output
  465. rGPBCON |= 1;
  466. rGPBDAT &= ~1;
  467. }

  468. void Beep(U32 freq, U32 ms)
  469. {
  470. Buzzer_Freq_Set( freq ) ;
  471. Delay( ms ) ;
  472. Buzzer_Stop() ;
  473. }

  474. #include "config.h"

  475. extern void Lcd_Display(void);

  476. void Task_LCD(void *p);

  477. /*****************【任务相关自定义变量】*****************/
  478. OS_STK  MainTaskStk[MainTaskStkLengh];
  479. OS_STK        Task0Stk [Task0StkLengh];       // Define the Task0 stack
  480. OS_STK        Task1Stk [Task1StkLengh];       // Define the Task1 stack
  481. OS_STK        Task2Stk [Task2StkLengh];       // Define the Task2 stack

  482. OS_EVENT *Semp;                         //Creat semp

  483. U8 err;
  484. int rYear, rMonth,rDay,rDayOfWeek,rHour,rMinute,rSecond;
  485. //建立一个任务用变量,实现该任务第一次运行时,就需要初始化,之后再运行该任务时则不需要进行初始化
  486. int user_task0_firstboot = 1;
  487. int user_task1_firstboot = 1;
  488. int user_task2_firstboot = 1;

  489. int Main(int argc, char **argv)
  490. {
  491. //初始化目标板
  492. TargetInit();

  493. //初始化uCOS-II
  494. OSInit ();         

  495. //初始化系统时基
  496. OSTimeSet(0);

  497. //创建系统初始任务
  498. OSTaskCreate (MainTask,(void *)0, &MainTaskStk[MainTaskStkLengh - 1], MainTaskPrio);

  499. //开始任务
  500. OSStart ();

  501. return 0;
  502. }

  503. void MainTask(void *pdata) //Main Task create taks0 and task1
  504. {
  505. #if OS_CRITICAL_METHOD == 3        /* Allocate storage for CPU status register */
  506. OS_CPU_SR  cpu_sr;
  507. #endif
  508. OS_ENTER_CRITICAL();

  509. Timer0Init();        //initial timer0 for ucos time tick
  510. ISRInit();        //initial interrupt prio or enable or disable

  511. OS_EXIT_CRITICAL();

  512. OSPrintfInit();        //use task to print massage to Uart

  513. OSStatInit();
  514. OSTaskCreate (Task0,(void *)0, &Task0Stk[Task0StkLengh - 1], Task0Prio);       
  515. OSTaskCreate (Task1,(void *)0, &Task1Stk[Task1StkLengh - 1], Task1Prio);       
  516. OSTaskCreate (Task2,(void *)0, &Task2Stk[Task2StkLengh - 1], Task2Prio);         
  517. while(1)
  518. {
  519. OSPrintf("\nEnter Main Task\n");
  520. OSTimeDly(OS_TICKS_PER_SEC*100);
  521. }
  522. }

  523. void Task0(void *pdata)        //任务0,打印CPU占用率
  524. {
  525. while (1)
  526. {
  527. OSPrintf("\nEnter Task0\n");
  528. OSPrintf("CPU Usage: %d%%\n",OSCPUUsage); //打印CPU占用率,调用系统变量实现

  529. OSTimeDly(OS_TICKS_PER_SEC);
  530. }
  531. }

  532. void Task1(void *pdata)        //任务1,控制LED灯亮,开启蜂鸣器同时鸣叫
  533. {
  534. U16 task1Cnt=0;

  535. if(user_task1_firstboot == 1)
  536. {
  537. // RTC初始化
  538. Rtc_Init();

  539. user_task1_firstboot = 0;
  540. }

  541. while (1)
  542. {
  543. task1Cnt++;
  544. OSPrintf("\nEnter Task1\n");
  545. OSPrintf("uC/OS Version:V%4.2f\n",(float)OSVersion()*0.01);//打印uC/OS的版本号

  546. //实现流水灯
  547. if((task1Cnt%5) == 0)
  548. rGPBDAT = 0x1E0;        //全灭
  549. else
  550. rGPBDAT = rGPBDAT - (0x10<<(task1Cnt%5));        //流水灯

  551. //蜂鸣器鸣叫
  552. Beep(3000, 60);
  553. Beep(2500, 60);
  554. Beep(2000, 60);
  555. Beep(1500, 60);
  556. Beep(1000, 60);
  557. Beep(900, 60);
  558. OSTimeDly(OS_TICKS_PER_SEC*15);
  559. }
  560. }

  561. void Task2(void *pdata)
  562. {
  563. unsigned int  x, m, n, k, y;
  564. static unsigned char i=0;
  565. int tmp,key;         

  566. int width = 10;
  567. int height = 100;

  568. if(user_task2_firstboot == 1)
  569. {
  570. //LCD 初始化
  571. Lcd_Display();

  572. user_task2_firstboot = 0;
  573. }

  574. while(1)
  575. {
  576. i++;
  577. if(i>60)i=0;

  578. if(rBCDYEAR == 0x99)
  579. rYear = 1999;
  580. else
  581. rYear    = (2000 + rBCDYEAR);
  582. rMonth   = FROM_BCD(rBCDMON & 0x1f);
  583. rDay        = FROM_BCD(rBCDDAY & 0x03f);
  584. rDayOfWeek = rBCDDATE - 1;
  585. rHour    = FROM_BCD(rBCDHOUR & 0x3f);
  586. rMinute     = FROM_BCD(rBCDMIN & 0x7f);
  587. rSecond     = FROM_BCD(rBCDSEC & 0x7f);

  588. OSTimeDly( 5 );
  589. OSPrintf("\nEnter Task2\n");       
  590. OSPrintf("LCD display Test!\n\n");       

  591. //在LCD上打印日期,星期,时间
  592. Lcd_printf(0,65,RGB( 0xFF,0xFF,0xFF),RGB( 0x00,0x00,0x00),0,"时间:%4d-%02d-%02d 星期%d  %02d:%02d:%02d\n",
  593.              rYear, rMonth, rDay,rDayOfWeek, rHour, rMinute, rSecond);

  594. Lcd_printf(84,92,RGB( 0xFF,0xFF,0xFF),RGB( 0x00,0x00,0x00),1,"OS计数器任务");
  595. Lcd_printf(89,122,RGB( 0xFF,0xFF,0xFF),RGB( 0x00,0x00,0x00),0,"任务 计数中:%02d" , i);

  596. OSTimeDly(OS_TICKS_PER_SEC*5);
  597. }
  598. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表