我有一个 C 函数,它可以通过打开设备描述符(nf10)完美地读/写硬件寄存器。我正在尝试使用 Python 做同样的事情。我能够读取寄存器,但无法写入寄存器。为什么我不能写?有没有更好的方法来读/写硬件中的寄存器?
相关Python代码:
#! /usr/bin/env python
import os
from fcntl import *
from struct import *
SIOCDEVPRIVATE = 35312
NF10_IOCTL_CMD_READ_STAT = SIOCDEVPRIVATE + 0
NF10_IOCTL_CMD_WRITE_REG = SIOCDEVPRIVATE + 1
NF10_IOCTL_CMD_READ_REG = SIOCDEVPRIVATE + 2
def rdaxi(addr):
f = open("/dev/nf10", "r+")
arg = pack("q", int(addr, 16))
value = ioctl(f, NF10_IOCTL_CMD_READ_REG, arg)
value = unpack("q", value)
value = value[0]
value = hex(value & int("0xffffffff", 16))
f.close()
return value
def wraxi(addr, value):
f = open("/dev/nf10", "r+")
arg = (int(addr, 16) << 32) + int(value, 16)
arg = pack("q", arg)
ioctl(f, NF10_IOCTL_CMD_WRITE_REG, arg)
f.close()
相关C代码
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define NF10_IOCTL_CMD_READ_STAT (SIOCDEVPRIVATE+0)
#define NF10_IOCTL_CMD_WRITE_REG (SIOCDEVPRIVATE+1)
#define NF10_IOCTL_CMD_READ_REG (SIOCDEVPRIVATE+2)
int main(int argc, char* argv[]){
int f;
uint64_t v;
uint64_t addr;
uint64_t val;
if(argc < 3){
printf("usage: rdaxi reg_addr(in hex) reg_val(in_hex)\n\n");
return 0;
}
else{
sscanf(argv[1], "%llx", &addr);
sscanf(argv[2], "%llx", &val);
}
//----------------------------------------------------
//-- open nf10 file descriptor for all the fun stuff
//----------------------------------------------------
f = open("/dev/nf10", O_RDWR);
if(f < 0){
perror("/dev/nf10");
return 0;
}
printf("\n");
// High 32 bits are the AXI address,
// low 32 bits are the value written to that address
v = (addr << 32) + val;
if(ioctl(f, NF10_IOCTL_CMD_WRITE_REG, v) < 0){
perror("nf10 ioctl failed");
return 0;
}
printf("\n");
close(f);
return 0;
}