-
Notifications
You must be signed in to change notification settings - Fork 34
periph: add a PeripheralInfo class for metadata. #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,59 @@ | ||
from .memory import MemoryMap | ||
from . import event | ||
|
||
|
||
__all__ = ["PeripheralInfo"] | ||
|
||
|
||
class PeripheralInfo: | ||
"""Peripheral metadata. | ||
|
||
A unified description of the local resources of a peripheral. It may be queried in order to | ||
recover its memory windows, CSR registers and event sources. | ||
|
||
Parameters | ||
---------- | ||
memory_map : :class:`MemoryMap` | ||
Memory map of the peripheral. | ||
irq : :class:`event.Source` | ||
IRQ line of the peripheral. Optional. | ||
""" | ||
def __init__(self, *, memory_map, irq=None): | ||
if not isinstance(memory_map, MemoryMap): | ||
raise TypeError("Memory map must be an instance of MemoryMap, not {!r}" | ||
.format(memory_map)) | ||
memory_map.freeze() | ||
self._memory_map = memory_map | ||
|
||
if irq is not None and not isinstance(irq, event.Source): | ||
raise TypeError("IRQ line must be an instance of event.Source, not {!r}" | ||
.format(irq)) | ||
self._irq = irq | ||
|
||
@property | ||
def memory_map(self): | ||
"""Memory map. | ||
|
||
Return value | ||
------------ | ||
A :class:`MemoryMap` describing the local address space of the peripheral. | ||
""" | ||
return self._memory_map | ||
|
||
@property | ||
def irq(self): | ||
"""IRQ line. | ||
|
||
Return value | ||
------------ | ||
An :class:`event.Source` used by the peripheral to request interrupts. If provided, its | ||
event map describes local events. | ||
|
||
Exceptions | ||
---------- | ||
Raises :exn:`NotImplementedError` if the peripheral info does not have an IRQ line. | ||
""" | ||
if self._irq is None: | ||
raise NotImplementedError("Peripheral info does not have an IRQ line" | ||
.format(self)) | ||
return self._irq |
This file contains hidden or 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,50 @@ | ||
import unittest | ||
|
||
from ..periph import PeripheralInfo | ||
from ..memory import MemoryMap | ||
from .. import event | ||
|
||
|
||
class PeripheralInfoTestCase(unittest.TestCase): | ||
def test_memory_map(self): | ||
memory_map = MemoryMap(addr_width=1, data_width=8) | ||
info = PeripheralInfo(memory_map=memory_map) | ||
self.assertIs(info.memory_map, memory_map) | ||
|
||
def test_memory_map_frozen(self): | ||
memory_map = MemoryMap(addr_width=1, data_width=8) | ||
info = PeripheralInfo(memory_map=memory_map) | ||
with self.assertRaisesRegex(ValueError, | ||
r"Memory map has been frozen. Address width cannot be extended further"): | ||
memory_map.add_resource("a", size=3, extend=True) | ||
|
||
def test_memory_map_wrong(self): | ||
with self.assertRaisesRegex(TypeError, | ||
r"Memory map must be an instance of MemoryMap, not 'foo'"): | ||
info = PeripheralInfo(memory_map="foo") | ||
|
||
def test_irq(self): | ||
memory_map = MemoryMap(addr_width=1, data_width=8) | ||
irq = event.Source() | ||
info = PeripheralInfo(memory_map=memory_map, irq=irq) | ||
self.assertIs(info.irq, irq) | ||
|
||
def test_irq_none(self): | ||
memory_map = MemoryMap(addr_width=1, data_width=8) | ||
info = PeripheralInfo(memory_map=memory_map, irq=None) | ||
with self.assertRaisesRegex(NotImplementedError, | ||
r"Peripheral info does not have an IRQ line"): | ||
info.irq | ||
|
||
def test_irq_default(self): | ||
memory_map = MemoryMap(addr_width=1, data_width=8) | ||
info = PeripheralInfo(memory_map=memory_map) | ||
with self.assertRaisesRegex(NotImplementedError, | ||
r"Peripheral info does not have an IRQ line"): | ||
info.irq | ||
|
||
def test_irq_wrong(self): | ||
memory_map = MemoryMap(addr_width=1, data_width=8) | ||
with self.assertRaisesRegex(TypeError, | ||
r"IRQ line must be an instance of event.Source, not 'foo'"): | ||
info = PeripheralInfo(memory_map=memory_map, irq="foo") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.