-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Move GPIO code from modm to here
- Loading branch information
Showing
9 changed files
with
365 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import functools | ||
|
||
class cached_property(object): | ||
def __init__(self, func): | ||
self.__doc__ = getattr(func, "__doc__") | ||
self.func = func | ||
|
||
def __get__(self, obj, cls): | ||
if obj is None: | ||
return self | ||
value = obj.__dict__[self.func.__name__] = self.func(obj) | ||
return value | ||
|
||
cached_function = functools.lru_cache(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2020, Niklas Hauser | ||
# All rights reserved. | ||
|
||
from collections import defaultdict | ||
from .cache import * | ||
|
||
class Instance: | ||
def __init__(self, driver, instance): | ||
self._instance = instance | ||
self.driver = driver | ||
self.name = self._instance["name"] | ||
self.number = int(self.name) if self.name.isdigit() else self.name | ||
|
||
def features(self, default=[]): | ||
feats = self.driver.features() | ||
feats.extend(self._instance.get("feature", [])) | ||
return feats if len(feats) else default | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class Driver: | ||
def __init__(self, device, driver): | ||
self._driver = driver | ||
self.device = device | ||
self.name = self._driver["name"] | ||
self.type = self._driver["type"] | ||
|
||
def instances(self, default=[]): | ||
if "instance" in self._driver: | ||
return [Instance(self, i) for i in self._driver["instance"]] | ||
return default | ||
|
||
def features(self, default=[]): | ||
if "feature" in self._driver: | ||
return list(self._driver["feature"]) | ||
return default | ||
|
||
def __str__(self): | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2020, Niklas Hauser | ||
# All rights reserved. | ||
|
||
from ..cache import cached_property | ||
from ..driver import Driver | ||
from collections import defaultdict | ||
|
||
class DriverCore(Driver): | ||
def __init__(self, device): | ||
Driver.__init__(self, device, device._find_first_driver("core")) | ||
|
||
|
||
def vectors(self, filterfn=None): | ||
vecs = (v["name"] for v in self._driver["vector"]) | ||
if filterfn is not None: | ||
vecs = filter(filterfn, vecs) | ||
return vecs | ||
|
||
|
||
def instance_irq_map(self, name): | ||
""" | ||
:return: a map from int(instance) to str(name) interrupt starting with {name}. | ||
""" | ||
vector_map = defaultdict(list) | ||
for vector in self.vectors(lambda v: v.startswith(name)): | ||
vrange = sorted(int(d) for d in vector[len(name):].split("_") if d.isdigit()) | ||
if len(vrange) == 2: | ||
vrange = list(range(vrange[0], vrange[1]+1)) | ||
for num in vrange: | ||
# if num in vector_map: | ||
# raise ValueError("Instance '{}' already in '{}' map!".format(str(num), name)) | ||
vector_map[num].append(vector) | ||
return vector_map | ||
|
||
|
||
def shared_irqs(self, name): | ||
""" | ||
:return: a map from str(name) to range(instances) >= 2 for interrupts starting with {name}. | ||
""" | ||
vector_range = {} | ||
for vector in self.vectors(lambda v: v.startswith(name)): | ||
vrange = sorted(int(d) for d in vector[len(name):].split("_") if d.isdigit()) | ||
if len(vrange) <= 1: | ||
continue; | ||
if len(vrange) == 2: | ||
vrange = list(range(vrange[0], vrange[1]+1)) | ||
if vector in vector_range: | ||
raise ValueError("Vector '{}' already in '{}' map!".format(str(vector), name)) | ||
vector_range[vector] = vrange | ||
return vector_range | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2016, Fabian Greif | ||
# Copyright (c) 2016, Niklas Hauser | ||
# All rights reserved. | ||
|
||
from .gpio import DriverGpio | ||
from .core import DriverCore | ||
from ..device import Device | ||
from ..cache import cached_property | ||
from ..access import copy_keys | ||
|
||
|
||
class Stm32Device(Device): | ||
def __init__(self, identifier, device_file): | ||
Device.__init__(self, identifier, device_file) | ||
|
||
@cached_property | ||
def gpio(self): | ||
return DriverGpio(self) | ||
|
||
@cached_property | ||
def core(self): | ||
return DriverCore(self) | ||
|
||
@cached_property | ||
def peripherals(self): | ||
all_peripherals = [] | ||
for s in self.gpio.signals_all: | ||
d = copy_keys(s, "driver", "instance") | ||
if len(d): all_peripherals.append(d); | ||
|
||
# Signals are not enough, since there are peripherals that don't have signals. | ||
# Example: STM32F401RE < 64pins: SPI4 cannot be connected to any pins. | ||
for d in self._properties["driver"]: | ||
driver = d["name"] | ||
if driver in ["gpio", "core"]: | ||
continue | ||
elif "instance" in d: | ||
all_peripherals.extend( {"driver": driver, "instance": int(i["name"])} for i in d["instance"] ) | ||
else: | ||
all_peripherals.append( {"driver": driver} ) | ||
|
||
for r in self.gpio._driver.get("remap", {}): | ||
d = copy_keys(r, "driver", "instance") | ||
if len(d): all_peripherals.append(d); | ||
|
||
return all_peripherals |
Oops, something went wrong.