Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
63 changes: 63 additions & 0 deletions boards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fnmatch
import importlib
import os.path

class Vendor:
def __init__(self, name):
self.name = name
self.boards = []

def load(self, path):
self.boards[:]

vendor = os.path.basename(path)

for path in os.listdir(path):
if fnmatch.fnmatch(path, '*.py'):

if path == '__init__.py':
continue

board = os.path.splitext(path)[0]

name = 'boards.%s.%s' % (vendor, board)
module = importlib.import_module(name)
self.boards.append(module.Board)

return self.boards

class UnsupportedBoardException(Exception):
pass

class Board:
pass

boards = []

'''
Detect the type of board by looking at the compatible string of the device
tree's root node.
'''
def detect():
with open('/sys/firmware/devicetree/base/compatible', 'r') as file:
line = file.read()
if line:
compatible = line.split('\0')[0]

for board in boards:
if compatible == board.__compatible__:
return board()

raise UnsupportedBoardException('Board: %s' % compatible)

raise IOError

path = os.path.dirname(__file__)

for directory in os.listdir(path):
path = os.path.join(path, directory)

if os.path.exists(os.path.join(path, '__init__.py')):
name = 'boards.%s' % directory
module = importlib.import_module(name)
boards.extend(module.vendor.load(path))
3 changes: 3 additions & 0 deletions boards/nvidia/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from boards import Vendor

vendor = Vendor('NVIDIA')
14 changes: 14 additions & 0 deletions boards/nvidia/jetson-tk1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import boards
import tegra

class Board(boards.Board):
__compatible__ = 'nvidia,jetson-tk1'
name = 'NVIDIA Jetson TK1'

def check_devices_mmc(self):
check_device('/dev/mmcblk0') # eMMC
check_device('/dev/mmcblk1') # MMC/SD

def check_devices(self):
super().check_devices()
self.check_devices_mmc()
5 changes: 5 additions & 0 deletions boards/nvidia/p2371.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import boards

class Board(boards.Board):
__compatible__ = 'nvidia,p2371-e01'
name = 'NVIDIA P2371 reference board (E.1)'
9 changes: 9 additions & 0 deletions linux/debugfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import io, os.path

mountpoint = '/sys/kernel/debug'

def exists(path):
return os.path.exists('%s/%s' % (mountpoint, path))

def open(path, *args, **kwargs):
return io.open('%s/%s' % (mountpoint, path), *args, **kwargs)
31 changes: 14 additions & 17 deletions linux/drm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@ def __str__(self):

class libdrm(ctypes.CDLL):
def __init__(self):
ctypes.CDLL.__init__(self, 'libdrm.so')
ctypes.CDLL.__init__(self, 'libdrm.so.2')

self.drmGetVersion.argstype = [ ctypes.c_int ]
self.drmGetVersion.restype = drmVersionPtr

class DRM:
def __init__(self, device):
self.device = device

def __enter__(self):
self.libdrm = libdrm()

if type(device) == str:
self.fd = os.open(device, os.O_RDWR)
elif type(device) == int:
self.fd = device
if type(self.device) == str:
self.fd = os.open(self.device, os.O_RDWR)
elif type(self.device) == int:
self.fd = self.device

return self

def __del__(self):
def __exit__(self, type, value, traceback):
os.close(self.fd)

def GetVersion(self):
Expand All @@ -61,15 +66,7 @@ def GetVersion(self):
self.libdrm.drmFreeVersion(version)
return result

def open(device):
if type(device) == str:
fd = os.open(device, os.O_RDWR)
elif type(device) == int:
fd = device

return DRM(fd)

if __name__ == '__main__':
drm = open(sys.argv[1])
version = drm.GetVersion()
print(version)
with DRM(sys.argv[1]) as drm:
version = drm.GetVersion()
print(version)
19 changes: 19 additions & 0 deletions linux/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys

color = True

def begin(message, end = ''):
print('%s...' % message, end = end)
sys.stdout.flush()

def end(error = None):
if error:
if color:
print('\033[31mfailed\033[0m:', error)
else:
print('failed:', error)
else:
if color:
print('\033[32mdone\033[0m')
else:
print('done')
16 changes: 16 additions & 0 deletions linux/sysfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import io, os.path

mountpoint = '/sys'

def exists(path):
return os.path.exists('%s/%s' % (mountpoint, path))

def open(path, *args, **kwargs):
return io.open('%s/%s' % (mountpoint, path), *args, **kwargs)

class Object:
def __init__(self, path):
self.path = path

def open(self, path, *args, **kwargs):
return open('%s/%s' % (self.path, path), *args, **kwargs)
75 changes: 65 additions & 10 deletions linux/system.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#!/usr/bin/python

import io
import ctypes
import os
import sys
import time

from . import ioctl, libc, sysfs

'''
Represents one CPU present in the system.
'''
class CPU():
def __init__(self, num):
self.path = '/sys/devices/system/cpu/cpu%u' % num
self.sysfs = sysfs.Object('devices/system/cpu/cpu%u' % num)
self.hotpluggable = True
self.num = num

with io.open(os.path.join(self.path, 'online'), 'r') as cpu:
online = cpu.readline().strip()
with self.sysfs.open('online', 'r') as file:
online = file.readline().strip()
if online == '0':
self.online = False
else:
Expand All @@ -24,13 +27,13 @@ def __init__(self, num):
Bring the CPU online or take it offline.
'''
def set_online(self, online):
with io.open(os.path.join(self.path, 'online'), 'w') as cpu:
with self.sysfs.open('online', 'w') as file:
if online:
cpu.write('1')
file.write('1')
else:
cpu.write('0')
file.write('0')

cpu.online = online
self.online = online

'''
Return the CPU mask.
Expand All @@ -52,7 +55,7 @@ def __str__(self):
'''
class CPUSet():
def __init__(self):
with io.open('/sys/devices/system/cpu/present', 'r') as file:
with sysfs.open('devices/system/cpu/present', 'r') as file:
present = file.readline().rstrip()
self.start, self.end = map(int, present.split('-'))

Expand Down Expand Up @@ -144,6 +147,22 @@ def apply_mask(self, mask):
def __iter__(self):
return iter(self.cpus)

'''
Provides access to a realtime clock device in the system.
'''
class RTC:
def __init__(self, name = 'rtc0'):
self.sysfs = sysfs.Object('class/rtc/%s' % name)

'''
Set the RTC to raise an alarm a given number of seconds from now.
'''
def set_alarm_relative(self, alarm):
alarm = int(time.time()) + alarm

with self.sysfs.open('wakealarm', 'w') as file:
file.write('%u' % alarm)

'''
Provides access to the system and system wide controls.
'''
Expand All @@ -152,5 +171,41 @@ def __init__(self):
pass

def suspend(self):
with io.open('/sys/power/state', 'w') as file:
with sysfs.open('power/state', 'w') as file:
file.write('mem')

'''
Provides access to a watchdog device in the system.
'''
class Watchdog():
WDIOC_SETOPTIONS = ioctl.IOR(ord('W'), 4, 4)
WDIOC_SETTIMEOUT = ioctl.IOWR(ord('W'), 6, 4)

WDIOS_DISABLECARD = 0x0001
WDIOS_ENABLECARD = 0x0002

def __init__(self, path):
self.path = path

def __enter__(self):
self.fd = os.open(self.path, os.O_RDWR)

def disable(self):
options = ctypes.pointer(ctypes.c_uint(Watchdog.WDIOS_DISABLECARD))

libc.ioctl(self.fd, Watchdog.WDIOC_SETOPTIONS, options)

def enable(self):
options = ctypes.pointer(ctypes.c_uint(Watchdog.WDIOS_ENABLECARD))

libc.ioctl(self.fd, Watchdog.WDIOC_SETOPTIONS, options)

def set_timeout(self, timeout):
timeout = ctypes.pointer(ctypes.c_uint(timeout))

libc.ioctl(self.fd, Watchdog.WDIOC_SETTIMEOUT, timeout)

def __exit__(self, type, value, traceback):
options = ctypes.pointer(ctypes.c_uint(Watchdog.WDIOS_DISABLECARD))
libc.ioctl(self.fd, Watchdog.WDIOC_SETOPTIONS, options)
os.close(self.fd)
35 changes: 0 additions & 35 deletions linux/watchdog.py

This file was deleted.

Loading