diff options
author | Martin Braun <martin.braun@ettus.com> | 2017-04-10 10:53:21 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2017-12-22 15:03:45 -0800 |
commit | ba4fad345d7489b40a7dab83842ac21d13c4f460 (patch) | |
tree | 9de140350ad90a52e95143d8ca37911cadbc1b83 | |
parent | 701904882cb63202f277af18c7ef11ac6cc3a581 (diff) | |
download | uhd-ba4fad345d7489b40a7dab83842ac21d13c4f460.tar.gz uhd-ba4fad345d7489b40a7dab83842ac21d13c4f460.tar.bz2 uhd-ba4fad345d7489b40a7dab83842ac21d13c4f460.zip |
mpm: Added uio control class
-rw-r--r-- | mpm/python/usrp_mpm/uio.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/mpm/python/usrp_mpm/uio.py b/mpm/python/usrp_mpm/uio.py new file mode 100644 index 000000000..18a894e9c --- /dev/null +++ b/mpm/python/usrp_mpm/uio.py @@ -0,0 +1,68 @@ +# +# Copyright 2017 Ettus Research (National Instruments) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +""" +Access to UIO mapped memory. +""" + +import struct +import os +import mmap + +class uio(object): + """ + Provides peek/poke interfaces for uio-mapped memory. + + Arguments: + path -- Path to UIO device, e.g. '/dev/uio0' + length -- Number of bytes in the address space (is passed to mmap.mmap). + Must be at least one page size + read_only -- Boolean, True == ro, False == rw + offset -- Passed to mmap.mmap + """ + def __init__(self, path, length, read_only=True, offset=None): + # Python can't tell the size of a uio device + assert length >= mmap.PAGESIZE + offset = offset or 0 + self._path = path + self._read_only = read_only + self._fd = os.open(path, os.O_RDONLY if read_only else os.O_RDWR) + self._mm = mmap.mmap( + self._fd, + length, + flags=mmap.MAP_SHARED, + prot=mmap.PROT_READ | (0 if read_only else mmap.PROT_WRITE), + offset=offset, + ) + + def peek32(self, addr): + """ + Returns the 32-bit value starting at address addr as an integer + """ + return struct.unpack('@I', self._mm[addr:addr+4])[0] + + def poke32(self, addr, val): + """ + Writes the 32-bit value val to address starting at addr. + Will throw if read_only was set to True. + A value that exceeds 32 bits will be truncated to 32 bits. + """ + assert not self._read_only + self._mm[addr:addr+4] = struct.pack( + '@I', + (val & 0xFFFFFFFF), + ) + |