Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f5b8489
usbd: Add USB device drivers implemented in Python.
projectgus Oct 26, 2022
e2a3e45
usbd: Add midi interface definition from @paulhamsh.
projectgus Feb 9, 2023
c8ad6ca
usbd: Major cleanup, refactor.
projectgus Feb 14, 2023
65762f6
Add basic keypad support
turmoni Jun 3, 2023
944e107
Fix report count, remove irrelevant comments
turmoni Jun 3, 2023
5b5871c
Add basic, read-only MSC support, and add LED status to keypad.
turmoni Jun 28, 2023
e8bd164
Actually add the changes methoned in the previous commit message, and…
turmoni Jun 28, 2023
eb47fa0
usbd: Bugfixes around data transfer, support using an AbstractBlockDe…
turmoni Jun 30, 2023
24f7422
usbd: Add USB device drivers implemented in Python.
projectgus Oct 26, 2022
581a662
usbd: Add midi interface definition from @paulhamsh.
projectgus Feb 9, 2023
7472ef5
Merge remote-tracking branch 'upstream/feature/usbd_python' into feat…
turmoni Jul 10, 2023
e24951a
usbd: Add copyright notices (+delete file that has gone from upstream…
turmoni Jul 10, 2023
e85b368
usbd: Run "black" with the right options for the style checker to be …
turmoni Jul 10, 2023
82f1e47
usbd: Use EP_IN_FLAG from utils for mass storage
turmoni Jul 10, 2023
5c51a9e
usbd: Re-run black to fix the missing comma
turmoni Jul 10, 2023
9d4d843
usbd: Add support for configuration open and reset callbacks.
projectgus Jul 25, 2023
29e9185
usbd: Add USB interface functions for endpoint STALL support.
projectgus Jul 25, 2023
9d7ce9f
usbd: Implement SET_REPORT support for OUT direction HID data.
projectgus Jul 26, 2023
92711ea
usbd: Rename ustruct->struct.
projectgus Jul 26, 2023
3765d04
usbd: Add hid keypad example from @turmoni .
projectgus Jul 26, 2023
756d761
usbd: Update hid_keypad example module.
projectgus Jul 26, 2023
bb389e3
usbd: Implement ruff, black linter & formatting fixes.
projectgus Jul 26, 2023
2baaf58
usbd: Add missing manifest file.
projectgus Jul 26, 2023
83364c0
Merge remote-tracking branch 'upstream/feature/usbd_python' into feat…
turmoni Aug 3, 2023
cd4f51c
usbd: Theoretically handle resets and bad CBWs better in msc
turmoni Sep 2, 2023
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
Prev Previous commit
Next Next commit
usbd: Run "black" with the right options for the style checker to be …
…happy
  • Loading branch information
turmoni committed Jul 10, 2023
commit e85b368ace257d22f0d82713273b7b42aae13b23
19 changes: 17 additions & 2 deletions micropython/usbd/keycodes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# Keypad keycodes for use with USB HID
# MIT license; Copyright (c) 2023 Dave Wickham
_KEYPAD_KEYS = [
"<NumLock>", "/", "*", "-", "+", "<Enter>", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "0", "."
"<NumLock>",
"/",
"*",
"-",
"+",
"<Enter>",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
".",
]

KEYPAD_KEYCODES_TO_KEYS = {k + 0x53: v for k, v in enumerate(_KEYPAD_KEYS)}
Expand Down
36 changes: 9 additions & 27 deletions micropython/usbd/msc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class CSW:
STATUS_FAILED = const(1)
STATUS_PHASE_ERROR = const(2)

def __init__(
self, dCSWSignature=0x53425355, dCSWTag=None, dCSWDataResidue=0, bCSWStatus=0
):
def __init__(self, dCSWSignature=0x53425355, dCSWTag=None, dCSWDataResidue=0, bCSWStatus=0):
self.dCSWSignature = dCSWSignature
self.dCSWTag = dCSWTag
self.dCSWDataResidue = dCSWDataResidue
Expand Down Expand Up @@ -199,9 +197,7 @@ def try_to_prepare_cbw(self, args=None):
try:
self.prepare_cbw()
except KeyError:
self.timer.init(
mode=Timer.ONE_SHOT, period=2000, callback=self.try_to_prepare_cbw
)
self.timer.init(mode=Timer.ONE_SHOT, period=2000, callback=self.try_to_prepare_cbw)

def handle_interface_control_xfer(self, stage, request):
"""Handle the interface control transfers; reset and get max lun"""
Expand Down Expand Up @@ -286,9 +282,7 @@ def prepare_cbw(self, args=None):
def receive_cbw_callback(self, ep_addr, result, xferred_bytes):
"""Callback stub to schedule actual CBW processing"""
self.log("receive_cbw_callback")
micropython.schedule(
self.proc_receive_cbw_callback, (ep_addr, result, xferred_bytes)
)
micropython.schedule(self.proc_receive_cbw_callback, (ep_addr, result, xferred_bytes))

def proc_receive_cbw_callback(self, args):
"""Invoke CBW processing"""
Expand Down Expand Up @@ -378,9 +372,7 @@ def proc_transfer_data(self, args):
return micropython.schedule(self.send_csw, None)

# This is the last data we're sending, pad it out
residue = self.cbw.dCBWDataTransferLength - (
self.transferred_length + len(self.data)
)
residue = self.cbw.dCBWDataTransferLength - (self.transferred_length + len(self.data))
if residue:
self.log(f"Adding {residue} bytes of padding for residue")
self.csw.dCSWDataResidue = residue
Expand Down Expand Up @@ -418,9 +410,7 @@ def validate_cbw(self) -> bool:
# Check if this is a valid SCSI command
try:
# The storage layer doesn't know about USB, it'll return True for valid and False for invalid
return not self.storage_device.validate_cmd(
self.cbw.CBWCB[0 : self.cbw.bCBWCBLength]
)
return not self.storage_device.validate_cmd(self.cbw.CBWCB[0 : self.cbw.bCBWCBLength])
except Exception as exc:
self.log(str(exc))
raise
Expand All @@ -442,13 +432,9 @@ def send_csw(self, args):

# If the host sent a command that was expecting more than just a CSW, we may have to send them some nothing in the absence of being able to STALL
if self.transferred_length == 0 and self.csw.dCSWDataResidue != 0:
self.log(
f"Sending {self.csw.dCSWDataResidue} bytes of nothing to pad it out"
)
self.log(f"Sending {self.csw.dCSWDataResidue} bytes of nothing to pad it out")
self.transferred_length = self.csw.dCSWDataResidue
self.submit_xfer(
self.ep_in, bytearray(self.csw.dCSWDataResidue), self.padding_sent
)
self.submit_xfer(self.ep_in, bytearray(self.csw.dCSWDataResidue), self.padding_sent)
# The flow from sending the CSW happens in the callback, not in whatever called us, so we can just return and re-call from the padding callback
return

Expand Down Expand Up @@ -649,9 +635,7 @@ def handle_read10(self, cmd=None):
length = self.long_operation["remaining_length"]
lba = self.long_operation["current_lba"]
else:
(read10, flags, lba, group, length, control) = ustruct.unpack(
">BBLBHB", cmd
)
(read10, flags, lba, group, length, control) = ustruct.unpack(">BBLBHB", cmd)

# Do we have an AbstractBlockDev?
if getattr(self.filesystem, "readblocks", False):
Expand Down Expand Up @@ -719,6 +703,4 @@ def handle_inquiry(self, cmd):
)

self.sense = type(self).INVALID_COMMAND
raise StorageDevice.StorageError(
"EVPD not implemented", status=CSW.STATUS_FAILED
)
raise StorageDevice.StorageError("EVPD not implemented", status=CSW.STATUS_FAILED)