-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add machine/usb package with USB device class support #29
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package midi | ||
|
|
||
| import ( | ||
| "runtime/volatile" | ||
| "github.com/goplus/emb/runtime/volatile" | ||
| ) | ||
|
|
||
| const bufferSize = 128 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package cdc | ||
|
|
||
| import ( | ||
| "runtime/volatile" | ||
| "github.com/goplus/emb/runtime/volatile" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Race Condition in Ring Buffer The Issues:
Recommendation: func (rb *rxRingBuffer) Put(val byte) bool {
state := interrupt.Disable()
defer interrupt.Restore(state)
if rb.Used() != rxRingBufferSize {
rb.head.Set(rb.head.Get() + 1)
rb.buffer[rb.head.Get()%rxRingBufferSize].Set(val)
return true
}
return false
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance: Redundant Volatile Register Reads Each Optimization for embedded systems: newHead := rb.head.Get() + 1
rb.head.Set(newHead)
rb.buffer[newHead%rxRingBufferSize].Set(val)Further optimization: Since const rxRingBufferMask = rxRingBufferSize - 1
rb.buffer[newHead&rxRingBufferMask].Set(val) |
||
| ) | ||
|
|
||
| const rxRingBufferSize = 128 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,9 @@ package cdc | |
|
|
||
| import ( | ||
| "errors" | ||
| "machine" | ||
| "machine/usb" | ||
| "runtime/interrupt" | ||
| "github.com/goplus/emb/machine" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMPORTANT: Silent Error Suppression Errors from Recommendation: for i := 0; i < size; i++ {
v, err := usbcdc.ReadByte()
if err != nil {
return i, err
}
data[i] = v
} |
||
| "github.com/goplus/emb/machine/usb" | ||
| "github.com/goplus/emb/runtime/interrupt" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package descriptor | ||
|
|
||
| import ( | ||
| "internal/binary" | ||
| "github.com/goplus/emb/internal/binary" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package descriptor | ||
|
|
||
| import ( | ||
| "runtime/volatile" | ||
| "github.com/goplus/emb/runtime/volatile" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package descriptor | ||
|
|
||
| import ( | ||
| "internal/binary" | ||
| "github.com/goplus/emb/internal/binary" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package descriptor | ||
|
|
||
| import ( | ||
| "internal/binary" | ||
| "github.com/goplus/emb/internal/binary" | ||
| ) | ||
|
|
||
| /* Endpoint Descriptor | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,8 @@ package descriptor | |
|
|
||
| import ( | ||
| "errors" | ||
| "internal/binary" | ||
| "internal/bytealg" | ||
| "github.com/goplus/emb/internal/binary" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance: Redundant Volatile Register Reads Each call to Optimization for embedded systems: newHead := rb.head.Get() + 1
rb.head.Set(newHead)
rb.buffer[newHead%rxRingBufferSize].Set(val)Additionally, since buffer sizes are powers of 2, use bitwise AND instead of modulo: const rxRingBufferMask = rxRingBufferSize - 1
rb.buffer[newHead&rxRingBufferMask].Set(val) // Much faster on ARM Cortex-M |
||
| "github.com/goplus/emb/internal/bytealg" | ||
| ) | ||
|
|
||
| var configurationCDCHID = [configurationTypeLen]byte{ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package hid | ||
|
|
||
| import ( | ||
| "runtime/volatile" | ||
| "github.com/goplus/emb/runtime/volatile" | ||
| ) | ||
|
|
||
| const bufferSize = 128 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import ( | |
| "encoding/binary" | ||
| "errors" | ||
| "fmt" | ||
| "machine" | ||
| "github.com/goplus/emb/machine" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| package msc | ||
|
|
||
| import ( | ||
| "machine" | ||
| "machine/usb" | ||
| "machine/usb/descriptor" | ||
| "machine/usb/msc/csw" | ||
| "machine/usb/msc/scsi" | ||
| "github.com/goplus/emb/machine" | ||
| "github.com/goplus/emb/machine/usb" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance: Inefficient Busy-Wait Goroutine This goroutine wakes 10,000 times per second to check a single boolean flag. Issues:
Recommendation: type msc struct {
// ... existing fields ...
taskChan chan struct{}
}
func (m *msc) processTasks() {
for range m.taskChan {
cmd := m.cbw.SCSICmd()
// ... process task ...
m.queuedBytes = 0
m.taskQueued = false
machine.AckUsbOutTransfer(usb.MSC_ENDPOINT_OUT)
}
}
// In rxHandler, send signal: select { case m.taskChan <- struct{}{}: default: } |
||
| "github.com/goplus/emb/machine/usb/descriptor" | ||
| "github.com/goplus/emb/machine/usb/msc/csw" | ||
| "github.com/goplus/emb/machine/usb/msc/scsi" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Quality: Global Singleton Without Thread Safety
The global
Midivariable is initialized ininit()without synchronization. While this may work in single-threaded embedded contexts, it's not future-proof for concurrent access.Recommendation:
Either:
sync.Oncefor lazy initialization if concurrent access is possible