簡體   English   中英

Python 和 UIO 設備:為什么 mmap.read() 工作而 os.read() 失敗?

[英]Python and UIO devices: Why does mmap.read() work and os.read() fail?

內核版本:4.19
Python版本:3.5.6
平台:Xilinx Ultrascale+ Zynq

我正在開發一些可以讀寫 UIO 設備的 python 代碼。 我找到了一種有效的方法,也找到了一種我無法理解的沒有明顯原因的方法。 我擔心這意味着我錯過了整個方法的某些東西,並且將來會回來咬我。

對於我的初始測試,我正在讀取和寫入 PL 中 5 個 LSB 處於活動狀態的單個寄存器。 這是工作代碼:

>>> import mmap
>>> fid= open('/dev/uio0', 'r+b', 0) # read/write, binary, non-buffered
>>> regs= mmap.mmap(fid.fileno(), 4)
>>> regs.read(4)
b'\x1c\x00\x00\x00'
>>> regs.seek(0)
>>> regs.write(b'\xF3\x00\x00')
>>> regs.seek(0)
>>> regs.read(4)
b'\x13\x00\x00\x00'

這是失敗代碼的兩個示例:

僅使用標准讀取功能失敗

>>> fid= open('/dev/uio0', 'r+b', 0) # read/write, binary, non-buffered
>>> fid.read(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 5] Input/output error

使用 os 也會失敗,但我可以向 mmap 提供文件描述符並且一切正常。

>>> fid= os.open('/dev/uio0', os.O_SYNC | os.O_RDWR)
>>> os.read(fid, 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 5] Input/output error
>>> import mmap
>>> regs= mmap.mmap(fid, 4)
>>> regs.read(4)
b'\x13\x00\x00\x00'
>>> regs.seek(0)
>>> regs.write(b'\x65\x00\x00\x00')
>>> regs.seek(0)
>>> regs.read(4)
b'\x05\x00\x00\x00'

誰能解釋為什么其他兩種方法失敗了? 從我坐的地方看,它們看起來相當。

提前致謝。

根據設計,UIO 使用:

  • mmap()讀寫設備地址空間
  • read()被通知中斷

所以你觀察到的行為是正確的。

請參閱內核文檔中的UIO HOWTO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM