samd21,samd51,nrf52840: move usbcdc to machine/usb/cdc (#2972)

* samd21,samd51,nrf52840: move usbcdc to machine/usb/cdc
This commit is contained in:
sago35
2022-07-10 18:33:52 +09:00
committed by GitHub
parent 56780c2691
commit d434058aef
15 changed files with 662 additions and 839 deletions
+14 -2
View File
@@ -14,6 +14,9 @@ var (
const (
hidEndpoint = 4
usb_SET_REPORT_TYPE = 33
usb_SET_IDLE = 10
)
type hidDevicer interface {
@@ -27,7 +30,7 @@ var size int
// calls machine.EnableHID for USB configuration
func SetCallbackHandler(d hidDevicer) {
if size == 0 {
machine.EnableHID(callback)
machine.EnableHID(callback, nil, callbackSetup)
}
devices[size] = d
@@ -45,7 +48,16 @@ func callback() {
}
}
func callbackSetup(setup machine.USBSetup) bool {
ok := false
if setup.BmRequestType == usb_SET_REPORT_TYPE && setup.BRequest == usb_SET_IDLE {
machine.SendZlp()
ok = true
}
return ok
}
// SendUSBPacket sends a HIDPacket.
func SendUSBPacket(b []byte) {
machine.SendUSBHIDPacket(hidEndpoint, b)
machine.SendUSBInPacket(hidEndpoint, b)
}
+14 -2
View File
@@ -41,7 +41,8 @@ type keyboard struct {
// wideChar holds high bits for the UTF-8 decoder.
wideChar uint16
buf *hid.RingBuffer
buf *hid.RingBuffer
waitTxc bool
}
// decodeState represents a state in the UTF-8 decode state machine.
@@ -74,13 +75,24 @@ func newKeyboard() *keyboard {
}
func (kb *keyboard) Callback() bool {
kb.waitTxc = false
if b, ok := kb.buf.Get(); ok {
kb.waitTxc = true
hid.SendUSBPacket(b)
return true
}
return false
}
func (kb *keyboard) tx(b []byte) {
if kb.waitTxc {
kb.buf.Put(b)
} else {
kb.waitTxc = true
hid.SendUSBPacket(b)
}
}
func (kb *keyboard) ready() bool {
return true
}
@@ -214,7 +226,7 @@ func (kb *keyboard) Press(c Keycode) error {
}
func (kb *keyboard) sendKey(consumer bool, b []byte) bool {
kb.buf.Put(b)
kb.tx(b)
return true
}
+18 -6
View File
@@ -15,8 +15,9 @@ const (
)
type mouse struct {
buf *hid.RingBuffer
button Button
buf *hid.RingBuffer
button Button
waitTxc bool
}
func init() {
@@ -38,13 +39,24 @@ func newMouse() *mouse {
}
func (m *mouse) Callback() bool {
m.waitTxc = false
if b, ok := m.buf.Get(); ok {
m.waitTxc = true
hid.SendUSBPacket(b[:5])
return true
}
return false
}
func (m *mouse) tx(b []byte) {
if m.waitTxc {
m.buf.Put(b)
} else {
m.waitTxc = true
hid.SendUSBPacket(b)
}
}
// Move is a function that moves the mouse cursor.
func (m *mouse) Move(vx, vy int) {
if vx == 0 && vy == 0 {
@@ -65,7 +77,7 @@ func (m *mouse) Move(vx, vy int) {
vy = 127
}
m.buf.Put([]byte{
m.tx([]byte{
0x01, byte(m.button), byte(vx), byte(vy), 0x00,
})
}
@@ -79,7 +91,7 @@ func (m *mouse) Click(btn Button) {
// Press presses the given mouse buttons.
func (m *mouse) Press(btn Button) {
m.button |= btn
m.buf.Put([]byte{
m.tx([]byte{
0x01, byte(m.button), 0x00, 0x00, 0x00,
})
}
@@ -87,7 +99,7 @@ func (m *mouse) Press(btn Button) {
// Release releases the given mouse buttons.
func (m *mouse) Release(btn Button) {
m.button &= ^btn
m.buf.Put([]byte{
m.tx([]byte{
0x01, byte(m.button), 0x00, 0x00, 0x00,
})
}
@@ -105,7 +117,7 @@ func (m *mouse) Wheel(v int) {
v = 127
}
m.buf.Put([]byte{
m.tx([]byte{
0x01, byte(m.button), 0x00, 0x00, byte(v),
})
}