1. 打開S+core IDE 以eCos創造一個專案
2. 於專案目錄下,找到default_install資料夾將它刪除
3. 打開eCos Configuration Tool
4. 點選Build > packages
5. 增加 file IO跟IOB support for Score SPCE3200還有IOA support for Score SPCE3200三個packages
6. 命名為default.ecc存於eCos專案目錄下編譯
7. 運行程式下載至開發版上觀察結果
#include <stdio.h>
#include <unistd.h>
#include <cyg/fileio/fileio.h>
#include <cyg/kernel/kapi.h>
#include <cyg/infra/testcase.h> // test macros
#include <cyg/io/spce3200_iob.h>
#include <cyg/io/spce3200_ioa.h>
#define C_IOB2_OUT_EN 0x00000400 // P_IOB_GPIO_SETUP(LED4)
#define C_IOB3_OUT_EN 0x00000800 // P_IOB_GPIO_SETUP(LED3)
#define C_IOB4_OUT_EN 0x00001000 // P_IOB_GPIO_SETUP(LED2)
#define C_IOB5_OUT_EN 0x00002000 // P_IOB_GPIO_SETUP(LED1)
#define C_IOB0_PULL_DOWN 0x01000000 // P_IOB_GPIO_SETUP(KEY2)
#define C_IOB0_NOPULL_UP 0x00010000 // P_IOB_GPIO_SETUP
#define C_IOB1_PULL_DOWN 0x02000000 // P_IOB_GPIO_SETUP(KEY1)
#define C_IOB1_NOPULL_UP 0x00020000 // P_IOB_GPIO_SETUP
#define C_IOA0_PULL_DOWN 0x01000000 // P_IOB_GPIO_SETUP(KEY4)
#define C_IOA0_NOPULL_UP 0x00010000 // P_IOB_GPIO_SETUP
#define C_IOA1_PULL_DOWN 0x02000000 // P_IOA_GPIO_SETUP(KEY3)
#define C_IOA1_NOPULL_UP 0x00020000 // P_IOA_GPIO_SETUP
//====================================================
// 功能: 主執行緒,掃描按鍵使對應的LED亮
//====================================================
void ex2_KeyLED(cyg_addrword_t data)
{
cyg_uint32 Key;
int fd_KeyB = open("/dev/iob", O_RDWR ); //打開GPIOB設備
int fd_KeyA = open("/dev/ioa", O_RDWR ); //打開GPIOA設備
Key = C_IOB2_OUT_EN //GPIOB配置參數
| C_IOB3_OUT_EN
| C_IOB4_OUT_EN
| C_IOB5_OUT_EN
| C_IOB0_PULL_DOWN | C_IOB0_NOPULL_UP
| C_IOB1_PULL_DOWN | C_IOB1_NOPULL_UP
;
cyg_fs_fsetinfo(fd_KeyB, CYGNUM_DEVS_IOB_SPCE3200_ATTRIB, &Key, 4);//GPIOB初始化
Key = C_IOA0_PULL_DOWN | C_IOA0_NOPULL_UP //GPIOA配置參數
| C_IOA1_PULL_DOWN | C_IOA1_NOPULL_UP
;
cyg_fs_fsetinfo(fd_KeyA, CYGNUM_DEVS_IOA_SPCE3200_ATTRIB, &Key, 4); //GPIOA初始化
while(1)
{
cyg_uint32 KeyValue; //用來存鍵值
read(fd_KeyA, &Key, 1); //讀取IOA值
Key = Key & 0x03; //取IOA0~IOA1值
KeyValue = Key; //將IOA值存入鍵值
read(fd_KeyB, &Key, 1); //讀取IOB值
Key = Key & 0x03; //取IOB0~IOB1值
KeyValue += Key << 2; //將IOB值存入鍵值
if(KeyValue > 0)
{
int i;
for(i=0x01; i!=0x10; i<<=1)
if(KeyValue & i)
printf("●");
else
printf("○");
printf("\n");
}
KeyValue <<= 2;
write(fd_KeyB, &KeyValue, 1); //寫IOB端口,點亮LED
}
}
cyg_uint8 stack[4096];
cyg_handle_t h_ex3_KeyLED;
cyg_thread ex3_KeyLED_s;
//====================================================
// 功能: 創建主執行緒
//====================================================
void cyg_user_start(void)
{
cyg_thread_create(0, // 執行緒優先級
ex2_KeyLED, // 執行緒函數
(cyg_addrword_t)0, // 執行緒參數
"ex3_KeyLED_thread", // 執行緒名稱
(void *)stack, // 執行緒堆疊基址
4096, // 執行緒堆疊大小
&h_ex3_KeyLED, // 返回執行緒控制碼
&ex3_KeyLED_s); // 返回執行緒信息結構體
cyg_thread_resume(h_ex3_KeyLED); // 啟動執行緒
cyg_scheduler_start();
}