可以直接通过配置寄存器实现利用按键来控制LED灯亮灭,源代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#define GPIOC_BASE_ADDRESS (0xC001C000)
#define MAP_SIZE 40
#define GPIOCOUT *(unsigned int *)base
#define GPIOCOUTENB *(unsigned int *)(base+0x04)
#define GPIOCALTFN0 *(unsigned int *)(base+0x20)
#define GPIOCPAD *(unsigned int *)(base+0x18)
static int dev_fd;
static unsigned int base;
typedef void (*ptr_to_func) (int) ;
ptr_to_func signal (int , ptr_to_func );
void handler(int sig){
if(sig==SIGSTOP){
GPIOCOUT &= ~(1<<11);
if(dev_fd){
close(dev_fd);
}
munmap((unsigned int *)base,MAP_SIZE);
}
}
int main(int argc, char *argv[]){
dev_fd = open("/dev/mem", O_RDWR | O_NDELAY);
if (dev_fd < 0){
printf("open(/dev/mem) failed.");
return 0;
}
base = (unsigned int)mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, GPIOC_BASE_ADDRESS );
GPIOCALTFN0 &= ~(3<<22);
GPIOCALTFN0 |= (1<<22);
GPIOCALTFN0 &= ~(3<<14);
GPIOCALTFN0 |= (1<<14);
GPIOCOUTENB |= (1<<11);
GPIOCOUTENB &= ~(1<<7);
signal(SIGSTOP,handler);
while(1){
if((GPIOCPAD & (1<<7)) == 0)
GPIOCOUT |= (1<<11);
else
GPIOCOUT &= ~(1<<11);
}
return 0;
}
直接在ubuntu中对其交叉编译,命令如下:
将exp3.out拷入Nanopi中执行./exp3.out,可以通过黄 {MOD}按钮控制蓝灯的亮灭。
总结:这种方式也比较简单,是比较底层的,和裸机程序比较相似。