(原創) 如何使用硬體 + μC/OS-II 的方式『播放SD卡內wav檔音樂』? (IC Design) (DE2) (Quartus II) (Nios II) (μC/OS-II)

Abstract
之前已經討論過使用硬體的方式『播放SD卡內wav檔音樂』,本文將討論硬體 + μC/OS-II這種軟硬體合作的方式播放SD卡內wav檔音樂。

使用環境 : Quartus II 6.0 + Nios II 6.0

Introduction
μC/OS-II為一嵌入式real time OS,本文希望能透過OS以軟體的方式控制硬體,播放SD卡內wav檔音樂。

Solution
Step 1:
下載DE2_System_v1.5
友晶科技http://www.terasic.com/downloads/cd-rom/de2/下載DE2_System_v1.5.zip

Step 2:
建立DE2_SD_Card_Audio_MicroCOS2目錄
在c:\DE2\下建立[DE2_SD_Card_Audio_MicroCOS2]目錄,將下載的DE2_System_v1.5解壓縮到c:\,將c:\DE2_System_v1.5\DE2_demonstrations\DE2_SD_Card_Audio\下所有檔案複製到c:\DE2\DE2_SD_Card_Audio_MicroCOS2下

Step 3:
將硬體燒進DE2
開啟Quartus II後,載入c:\DE2\DE2_SD_Card_Audio_MicroCOS2\DE2_SD_Card_Audio.qpf,並開啟Programmer,將c:\DE2\DE2_SD_Card_Audio_MicroCOS2\DE2_SD_Card_Audio.sof燒進DE2

Step 4:
將舊的workspace和project刪除
將c:\DE2\DE2_SD_Card_Audio_MicroCOS2\的三個目錄刪除
[.metadata]
[hello_led_0]
[hello_led_0_syslib]

.metadata紀錄舊有workspace資訊,hello_led_0和hello_led_0_syslib則為原來DE2_SD_Card_Audio所建立的專案目錄

Step 5:
切換workspace
開啟Nios II後,切換workspace到c:\DE2\DE2_SD_Card_Audio_MicroCOS2,此時沒有任何專案,因為已經被刪除了。

Step 6:
建立一個新的Nios II C/C++ Application
sdplayeros01.jpg



Step 7:
選擇Hello MicroC/OS-II這個project template,,並選取正確路徑的SOPC Builder System。
sdplayeros02.jpg


Step 8:
建立一個新的System Library
sdplayeros03.jpg

Step 9:
修改hello_ucosii.c
將DE2_System_v1.5.zip中,純硬體的c:\DE2_System_v1.5\DE2_demonstrations\DE2_SD_Card_Audio\hello_led_0\hello_led.c內

#include "basic_io.h"
#include 
"LCD.h"
#include 
"SD_Card.h"

int main(void)
{
  UINT16 i
=0,Tmp1=0,Tmp2=0;
  UINT32 j
=720;
  BYTE Buffer[
512]={0};
  
  
while(SD_card_init())
  usleep(
500000);
  LCD_Test();
  
  
while(1)
  
{
    SD_read_lba(Buffer,j,
1);
    
while(i<512)
    
{
      
if(!IORD(AUDIO_0_BASE,0))
      
{
        Tmp1
=(Buffer[i+1]<<8)|Buffer[i];
        IOWR(AUDIO_0_BASE,
0,Tmp1);
        i
+=2;
      }

    }

    
if(j%64==0)
    
{
      Tmp2
=Tmp1*Tmp1;
      IOWR(LED_RED_BASE,
0,Tmp2);
      IOWR(LED_GREEN_BASE,
0,Tmp1);
    }

    IOWR(SEG7_DISPLAY_BASE,
0,j);
    j
++;
    i
=0;
  }

 
  
return 0;
}


的int main(void) 內所有的程式碼(不含最後一行return 0)複製到
hello_ucosii.c的void task1(void* pdata)內

將 OS_STK    task2_stk[TASK_STACKSIZE]; 整行刪除

將 #define TASK2_PRIORITY      2 整行刪除

將void task2(void* pdata)整個function刪除

/* Prints "Hello World" and sleeps for three seconds */
void task2(void* pdata)
{
  
while (1)
  

    printf(
"Hello from task2\n");
    OSTimeDlyHMSM(
0030);
  }

}


將int main(void)內的

OSTaskCreateExt(task2,
                  NULL,
                  (
void *)&task2_stk[TASK_STACKSIZE],
                  TASK2_PRIORITY,
                  TASK2_PRIORITY,
                  task2_stk,
                  TASK_STACKSIZE,
                  NULL,
                  
0);


刪除

檔頭加上

#include "basic_io.h"
#include 
"LCD.h"
#include 
"SD_Card.h"


最後完整程式如下

/*************************************************************************
* 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 
"includes.h"

#include 
"basic_io.h"
#include 
"LCD.h"
#include 
"SD_Card.h"


/* Definition of Task Stacks */
#define   TASK_STACKSIZE       2048
OS_STK    task1_stk[TASK_STACKSIZE];

/* Definition of Task Priorities */

#define TASK1_PRIORITY      1

/* Prints "Hello World" and sleeps for three seconds */
void task1(void* pdata)
{
  UINT16 i
=0,Tmp1=0,Tmp2=0;
  UINT32 j
=720;
  BYTE Buffer[
512]={0};
  
  
while(SD_card_init())
  usleep(
500000);
  LCD_Test();
  
  
while(1)
  
{
    SD_read_lba(Buffer,j,
1);
    
while(i<512)
    
{
      
if(!IORD(AUDIO_0_BASE,0))
      
{
        Tmp1
=(Buffer[i+1]<<8)|Buffer[i];
        IOWR(AUDIO_0_BASE,
0,Tmp1);
        i
+=2;
      }

    }

    
if(j%64==0)
    
{
      Tmp2
=Tmp1*Tmp1;
      IOWR(LED_RED_BASE,
0,Tmp2);
      IOWR(LED_GREEN_BASE,
0,Tmp1);
    }

    IOWR(SEG7_DISPLAY_BASE,
0,j);
    j
++;
    i
=0;
  }

}


/* The main function creates two task and starts multi-tasking */
int main(void)
{
  
  OSTaskCreateExt(task1,
                  NULL,
                  (
void *)&task1_stk[TASK_STACKSIZE],
                  TASK1_PRIORITY,
                  TASK1_PRIORITY,
                  task1_stk,
                  TASK_STACKSIZE,
                  NULL,
                  
0);
              
               
  OSStart();
  
return 0;
}


/******************************************************************************
*                                                                             *
* License Agreement                                                           *
*                                                                             *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           *
* All rights reserved.                                                        *
*                                                                             *
* Permission is hereby granted, free of charge, to any person obtaining a     *
* copy of this software and associated documentation files (the "Software"),  *
* to deal in the Software without restriction, including without limitation   *
* the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
* and/or sell copies of the Software, and to permit persons to whom the       *
* Software is furnished to do so, subject to the following conditions:        *
*                                                                             *
* The above copyright notice and this permission notice shall be included in  *
* all copies or substantial portions of the Software.                         *
*                                                                             *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
* DEALINGS IN THE SOFTWARE.                                                   *
*                                                                             *
* This agreement shall be governed in all respects by the laws of the State   *
* of California and by the laws of the United States of America.              *
* Altera does not recommend, suggest or require that this reference design    *
* file be used in conjunction or combination with any other product.          *
*****************************************************************************
*/


將DE2_System_v1.5.zip中,純硬體的c:\DE2_System_v1.5\DE2_demonstrations\DE2_SD_Card_Audio\hello_led_0\下的
basic_io.h
LCD.c
LCD.h
SD_Card.h
四個檔複製到
c:\DE2\DE2_SD_Card_Audio_MicroCOS2\hello_ucosii_0\下
最後將hello_ucosii_0 refresh載入這四個檔
sdplayeros04.jpg

Step 10:
執行hello_ucosii_0
將hello_ucosii_0專案Run As Nios II Hardware,就可聽到SD內wav檔的音樂,不過音質似乎不是很好。

Step 11:
將hello_ucosii_0改成release模式
選定hello_ucosii_0,按右鍵選Properties
sdplayeros05.jpg


右側選C/C++ Build,將左側的Configuration改為Release
sdplayeros06.jpg

因為Debug模式下,compiler並未啟動最佳化,因此執行速度較慢,所以音質較差。重新將hello_ucosii_0專案Run As Hardware,就可得到如MP3般的音質了。

Conclusion
藉由這個範例,我們學到了若要將純硬體的系統,改成由μC/OS-II控制硬體,可以透過task的方式驅動。

posted on 2007-11-01 13:52  真 OO无双  阅读(5205)  评论(11编辑  收藏  举报

导航