c - Raspberry PI,原始读取 C 中的寄存器值

标签 c raspberry-pi raspbian

谁能给我一些关于如何从 Raspberry Pi 寄存器中读取值的示例? 我需要检查 AUX_MU_LSR_REG (0x7E21 5054) 中的字节,但我不知道如何在不使用任何其他库的情况下执行此操作。 我尝试过了: (这里我修改了原贴)

********* *****************开始代码的

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>



#define BCM2708     0x20000000
#define UART_BASE   (BCM2708+0x215000)
#define PAGE_SIZE   (4*1024)
#define BLOCK_SIZE  (4*1024)



int mem_fd;                 
void *uart_map;                 
volatile unsigned *uart;            




int main()
{

printf("start\n");

if((mem_fd=open("/dev/mem",O_RDWR|O_SYNC))<0)
{
printf("can't open /dev/mem \n");
exit(-1);
}
else printf("stream_file open OK \n");

if((uart_map=malloc(BLOCK_SIZE))==0)
printf("malloc fail\n");
else printf("GPIO_mallocation OK %d \n", (int)uart_map );


uart_map=mmap   (NULL,          //any address in our space
BLOCK_SIZE,         //map length
PROT_READ|PROT_WRITE,   //Enable reading and writing to maped memmory
MAP_SHARED,     //Shares with other processes
mem_fd,         //File to map
UART_BASE       //Offset toUART peripheral
);

if(uart_map==MAP_FAILED)
{
printf("mmap error %d", (int)uart_map);
exit(-1);
}
else printf("GPIO mapping OK \n");
uart=(volatile unsigned* )uart_map;
int i;

for(i=0;i<32;i++)
{
printf("adress:%x ",uart+i);
printf("value:%x\n",*(uart+i));
}





printf("stop\n");
return 0;
}

` ***********************代码结束 现在我真的不记得我是如何显示 0 的,但在上面的代码中我遇到了指针冲突。

uart 指向什么?理论上它应该显示值,但几周后我无法让它运行。

希望你能以某种方式帮助我

最佳答案

我已经成功编译了示例 here并且能够通过对该代码进行少许修改来从 GPIO 引脚读取和写入数字值。

您基本上正朝着正确的方向前进:mmap() 数据表中为您的特定 GPIO、SPI 或您需要的任何寄存器指定的范围,然后从该地址读取和写入。这是 RPi 上所有 GPIO 编程的基础。

有一些您应该注意的特殊位域,但正如我提到的,这些都在 elinux.org 上的 Pi 数据表中

注意:向mmap() 系统注册您需要以 super 用户身份运行。

关于c - Raspberry PI,原始读取 C 中的寄存器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445071/

相关文章:

c - 使用 strt_ok 后释放变量时出现段错误

linux - Raspberry Pi 3-无法更改分辨率

python - 如何将变音符号发送到带有 GSM 模块的智能手机?

为 Raspberry Pi 交叉编译 C++11 代码

java - raspbian : Cannot execute binary file: Exec format error 上的 Flyway 问题

node.js - 通过React.js中的网站控制树莓派?

c - 我不会写这个阶乘代码

c - 在 C 中使用按引用传递的结构变量

c - 错误的 Malloc() 三层结构?

linux - 尽管 ping 有效,但 apt-get 在树莓派上失败