[開發記錄] 函式庫調用 - GPIO控制 之一

# 學習 Embedded linux 函式庫調用
先嘗試控制 GPIO 部分:
每一腳都有六種功能,須由MUX設定,並且經由記憶體對應的位置控制IO。

#include <stdio.h>	// C語言標準函式庫-檔案處理
#include <stdlib.h>	// C語言標準函式庫-通用工具
#include <sys/types.h>	// linux 系統函式庫
#include <sys/stat.h>	// linux 系統函式庫
#include <unistd.h>	// POSIX標準的系統調用函式庫
#include <fcntl.h>	// POSIX標準的文件控制函式庫
#include <sys/mman.h>	// POSIX標準的記憶體管理函式庫

#define P9Port_FF (0x000000FF << 2)
int main()
{
//---------------GPIO 測試程式---------------

//O_SYNC makes the memory uncacheable
int fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd < 0) 
	{
	sprintf(stderr,"Could not open memory\n");
	return 0;
	}

// Pad configuration
volatile ulong *pinconf;
pinconf = (ulong*) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x48000000);
if (pinconf == MAP_FAILED) 
	{
	sprintf(stderr,"Pinconf Mapping failed\n");
	close(fd);
	return 0;
	}

// IO control is at physical address 0x48002000 = 0x48000000+0x2000
// Set 8 pin of GPIO bank 5 to GPIO MODE => use BBxM P9 odd 7 to 21 (8bit)
pinconf[0x2158/4] = 0x00040004;	//GPIO 131 & 130
pinconf[0x215C/4] = 0x00040004;	//GPIO 133 & 132
pinconf[0x2160/4] = 0x00040004;	//GPIO 135 & 134
pinconf[0x2164/4] = 0x00040004;	//GPIO 137 & 136
close(fd);

fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd < 0) 
	{
	sprintf(stderr,"Could not open memory\n");
	return 0;
	}

// GPIO Configuration: configure are input
volatile ulong *gpio;
gpio = (ulong*) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x49050000);
if (gpio == MAP_FAILED) {
	printf("Gpio Mapping failed\n");
	close(fd);
	return 0;
}

// GPIO bank 5 is at physical address 0x49056000 = 0x49050000+0x6000

// First set all output on bank5 to high
// GPIO Bank 5 is GPIO[159:128],used [37:30] as a 8bit port
// (set_data_out has offset 0x94)
gpio[0x6094/4]=0xFFFFFFFF;

// GPIO Output enable (GPIO_OE) is offset by 0x34 for each bank
// (set low for output)
gpio[0x6034/4] = 0x00000000;	//~(P9Port_FF);

// Also disable the wakeupenable and irqenable intertupts
// GPIO clear_Wakeupenable is offset by 0x80 for each bank
gpio[0x6080/4] = 0x0000FFFF;	//P9Port_FF;

// GPIO clear_irqenable1 is offset by 0x60 for each bank
gpio[0x6060/4] = 0x0000FFFF;	//P9Port_FF;

// GPIO clear_irqenable2 is offset by 0x70 for each bank
gpio[0x6070/4] = 0x0000FFFF;	//P9Port_FF;


// clear_data_out has offset 0x90
gpio[0x6090/4]= 0x0000FFFF;	//P9Port_FF;
usleep(500);	//delay 500 us

// set_data_out has offset 0x94
int i;
for(i=0;i<25000;i=i+1)
{
gpio[0x6094/4]=0x0000FFFF; //P9Port_FF;
usleep(500);
}


/*

int i;
unsigned char j;
unsigned long k;


//blinky
for(i=0;i<20;i=i+1)
{
// clear_data_out has offset 0x90
gpio[0x6090/4]=P9Port_FF;
sleep(500);

// set_data_out has offset 0x94
gpio[0x6094/4]=P9Port_FF;
sleep(500);
}

//Scrolling light
j=1;k=0;
for(i=0;i<50;i=i+1)
{
j=j<<1;
k=(unsigned long)j << 2;
// clear_data_out has offset 0x90
gpio[0x6090/4]=P9Port_FF;
usleep(500);	//delay 500us

// set_data_out has offset 0x94
gpio[0x6094/4]=k;
sleep(500);		//display 500ms

}
*/
close(fd);
//---------------GPIO 測試程式---------------

}


以上IO控制內容參考來源( http://x4350.blogspot.tw/2011/01/digital-io-on-beagleboard-using-gpio.html )

稍微做一些修正後,僅出現警告訊息,需測試程式控制的正確性。


編輯程式後,行會導致整個系統當機,

目前大致理解程式碼動作,但將/dev/mem開啟為fd的同時,是否會將整個記憶體鎖住,導致系統整個停住不動,此部分仍要再研究研究....



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值