mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 04:53:42 +00:00
samd21,samd51,nrf52840: move usbcdc to machine/usb/cdc (#2972)
* samd21,samd51,nrf52840: move usbcdc to machine/usb/cdc
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package cdc
|
||||
|
||||
import (
|
||||
"runtime/volatile"
|
||||
)
|
||||
|
||||
const rxRingBufferSize = 128
|
||||
|
||||
// rxRingBuffer is ring buffer implementation inspired by post at
|
||||
// https://www.embeddedrelated.com/showthread/comp.arch.embedded/77084-1.php
|
||||
type rxRingBuffer struct {
|
||||
buffer [rxRingBufferSize]volatile.Register8
|
||||
head volatile.Register8
|
||||
tail volatile.Register8
|
||||
}
|
||||
|
||||
// NewRxRingBuffer returns a new ring buffer.
|
||||
func NewRxRingBuffer() *rxRingBuffer {
|
||||
return &rxRingBuffer{}
|
||||
}
|
||||
|
||||
// Used returns how many bytes in buffer have been used.
|
||||
func (rb *rxRingBuffer) Used() uint8 {
|
||||
return uint8(rb.head.Get() - rb.tail.Get())
|
||||
}
|
||||
|
||||
// Put stores a byte in the buffer. If the buffer is already
|
||||
// full, the method will return false.
|
||||
func (rb *rxRingBuffer) Put(val byte) bool {
|
||||
if rb.Used() != rxRingBufferSize {
|
||||
rb.head.Set(rb.head.Get() + 1)
|
||||
rb.buffer[rb.head.Get()%rxRingBufferSize].Set(val)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Get returns a byte from the buffer. If the buffer is empty,
|
||||
// the method will return a false as the second value.
|
||||
func (rb *rxRingBuffer) Get() (byte, bool) {
|
||||
if rb.Used() != 0 {
|
||||
rb.tail.Set(rb.tail.Get() + 1)
|
||||
return rb.buffer[rb.tail.Get()%rxRingBufferSize].Get(), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Clear resets the head and tail pointer to zero.
|
||||
func (rb *rxRingBuffer) Clear() {
|
||||
rb.head.Set(0)
|
||||
rb.tail.Set(0)
|
||||
}
|
||||
|
||||
const txRingBufferSize = 8
|
||||
|
||||
// txRingBuffer is ring buffer implementation inspired by post at
|
||||
// https://www.embeddedrelated.com/showthread/comp.arch.embedded/77084-1.php
|
||||
type txRingBuffer struct {
|
||||
buffer [txRingBufferSize]struct {
|
||||
buf [64]byte
|
||||
size int
|
||||
}
|
||||
head volatile.Register8
|
||||
tail volatile.Register8
|
||||
}
|
||||
|
||||
// NewTxRingBuffer returns a new ring buffer.
|
||||
func NewTxRingBuffer() *txRingBuffer {
|
||||
return &txRingBuffer{}
|
||||
}
|
||||
|
||||
// Used returns how many bytes in buffer have been used.
|
||||
func (rb *txRingBuffer) Used() uint8 {
|
||||
return uint8(rb.head.Get() - rb.tail.Get())
|
||||
}
|
||||
|
||||
// Put stores a byte in the buffer. If the buffer is already
|
||||
// full, the method will return false.
|
||||
func (rb *txRingBuffer) Put(val []byte) bool {
|
||||
if rb.Used() == txRingBufferSize {
|
||||
return false
|
||||
}
|
||||
|
||||
if rb.Used() == 0 {
|
||||
rb.head.Set(rb.head.Get() + 1)
|
||||
rb.buffer[rb.head.Get()%txRingBufferSize].size = 0
|
||||
}
|
||||
buf := &rb.buffer[rb.head.Get()%txRingBufferSize]
|
||||
|
||||
for i := 0; i < len(val); i++ {
|
||||
if buf.size == 64 {
|
||||
// next
|
||||
// TODO: Make sure that data is not corrupted even when the buffer is full
|
||||
rb.head.Set(rb.head.Get() + 1)
|
||||
buf = &rb.buffer[rb.head.Get()%txRingBufferSize]
|
||||
rb.buffer[rb.head.Get()%txRingBufferSize].size = 0
|
||||
}
|
||||
buf.buf[buf.size] = val[i]
|
||||
buf.size++
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Get returns a byte from the buffer. If the buffer is empty,
|
||||
// the method will return a false as the second value.
|
||||
func (rb *txRingBuffer) Get() ([]byte, bool) {
|
||||
if rb.Used() != 0 {
|
||||
rb.tail.Set(rb.tail.Get() + 1)
|
||||
size := rb.buffer[rb.tail.Get()%txRingBufferSize].size
|
||||
return rb.buffer[rb.tail.Get()%txRingBufferSize].buf[:size], true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Clear resets the head and tail pointer to zero.
|
||||
func (rb *txRingBuffer) Clear() {
|
||||
rb.head.Set(0)
|
||||
rb.tail.Set(0)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package cdc
|
||||
|
||||
const (
|
||||
cdcEndpointACM = 1
|
||||
cdcEndpointOut = 2
|
||||
cdcEndpointIn = 3
|
||||
)
|
||||
|
||||
// New returns USBCDC struct.
|
||||
func New() *USBCDC {
|
||||
if USB == nil {
|
||||
USB = &USBCDC{
|
||||
rxBuffer: NewRxRingBuffer(),
|
||||
txBuffer: NewTxRingBuffer(),
|
||||
}
|
||||
}
|
||||
return USB
|
||||
}
|
||||
|
||||
const (
|
||||
// bmRequestType
|
||||
usb_REQUEST_HOSTTODEVICE = 0x00
|
||||
usb_REQUEST_DEVICETOHOST = 0x80
|
||||
usb_REQUEST_DIRECTION = 0x80
|
||||
|
||||
usb_REQUEST_STANDARD = 0x00
|
||||
usb_REQUEST_CLASS = 0x20
|
||||
usb_REQUEST_VENDOR = 0x40
|
||||
usb_REQUEST_TYPE = 0x60
|
||||
|
||||
usb_REQUEST_DEVICE = 0x00
|
||||
usb_REQUEST_INTERFACE = 0x01
|
||||
usb_REQUEST_ENDPOINT = 0x02
|
||||
usb_REQUEST_OTHER = 0x03
|
||||
usb_REQUEST_RECIPIENT = 0x1F
|
||||
|
||||
usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE = (usb_REQUEST_DEVICETOHOST | usb_REQUEST_CLASS | usb_REQUEST_INTERFACE)
|
||||
usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE = (usb_REQUEST_HOSTTODEVICE | usb_REQUEST_CLASS | usb_REQUEST_INTERFACE)
|
||||
usb_REQUEST_DEVICETOHOST_STANDARD_INTERFACE = (usb_REQUEST_DEVICETOHOST | usb_REQUEST_STANDARD | usb_REQUEST_INTERFACE)
|
||||
|
||||
// CDC Class requests
|
||||
usb_CDC_SET_LINE_CODING = 0x20
|
||||
usb_CDC_GET_LINE_CODING = 0x21
|
||||
usb_CDC_SET_CONTROL_LINE_STATE = 0x22
|
||||
usb_CDC_SEND_BREAK = 0x23
|
||||
|
||||
usb_CDC_V1_10 = 0x0110
|
||||
usb_CDC_COMMUNICATION_INTERFACE_CLASS = 0x02
|
||||
|
||||
usb_CDC_CALL_MANAGEMENT = 0x01
|
||||
usb_CDC_ABSTRACT_CONTROL_MODEL = 0x02
|
||||
usb_CDC_HEADER = 0x00
|
||||
usb_CDC_ABSTRACT_CONTROL_MANAGEMENT = 0x02
|
||||
usb_CDC_UNION = 0x06
|
||||
usb_CDC_CS_INTERFACE = 0x24
|
||||
usb_CDC_CS_ENDPOINT = 0x25
|
||||
usb_CDC_DATA_INTERFACE_CLASS = 0x0A
|
||||
|
||||
usb_CDC_LINESTATE_DTR = 0x01
|
||||
usb_CDC_LINESTATE_RTS = 0x02
|
||||
)
|
||||
@@ -0,0 +1,189 @@
|
||||
package cdc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"machine"
|
||||
"runtime/interrupt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrBufferEmpty = errors.New("USB-CDC buffer empty")
|
||||
)
|
||||
|
||||
const cdcLineInfoSize = 7
|
||||
|
||||
type cdcLineInfo struct {
|
||||
dwDTERate uint32
|
||||
bCharFormat uint8
|
||||
bParityType uint8
|
||||
bDataBits uint8
|
||||
lineState uint8
|
||||
}
|
||||
|
||||
// Read from the RX buffer.
|
||||
func (usbcdc *USBCDC) Read(data []byte) (n int, err error) {
|
||||
// check if RX buffer is empty
|
||||
size := usbcdc.Buffered()
|
||||
if size == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Make sure we do not read more from buffer than the data slice can hold.
|
||||
if len(data) < size {
|
||||
size = len(data)
|
||||
}
|
||||
|
||||
// only read number of bytes used from buffer
|
||||
for i := 0; i < size; i++ {
|
||||
v, _ := usbcdc.ReadByte()
|
||||
data[i] = v
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// ReadByte reads a single byte from the RX buffer.
|
||||
// If there is no data in the buffer, returns an error.
|
||||
func (usbcdc *USBCDC) ReadByte() (byte, error) {
|
||||
// check if RX buffer is empty
|
||||
buf, ok := usbcdc.rxBuffer.Get()
|
||||
if !ok {
|
||||
return 0, ErrBufferEmpty
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// Buffered returns the number of bytes currently stored in the RX buffer.
|
||||
func (usbcdc *USBCDC) Buffered() int {
|
||||
return int(usbcdc.rxBuffer.Used())
|
||||
}
|
||||
|
||||
// Receive handles adding data to the UART's data buffer.
|
||||
// Usually called by the IRQ handler for a machine.
|
||||
func (usbcdc *USBCDC) Receive(data byte) {
|
||||
usbcdc.rxBuffer.Put(data)
|
||||
}
|
||||
|
||||
// USBCDC is the USB CDC aka serial over USB interface.
|
||||
type USBCDC struct {
|
||||
rxBuffer *rxRingBuffer
|
||||
txBuffer *txRingBuffer
|
||||
waitTxc bool
|
||||
}
|
||||
|
||||
var (
|
||||
// USB is a USB CDC interface.
|
||||
USB *USBCDC
|
||||
|
||||
usbLineInfo = cdcLineInfo{115200, 0x00, 0x00, 0x08, 0x00}
|
||||
)
|
||||
|
||||
// Configure the USB CDC interface. The config is here for compatibility with the UART interface.
|
||||
func (usbcdc *USBCDC) Configure(config machine.UARTConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush flushes buffered data.
|
||||
func (usbcdc *USBCDC) Flush() {
|
||||
mask := interrupt.Disable()
|
||||
if b, ok := usbcdc.txBuffer.Get(); ok {
|
||||
machine.SendUSBInPacket(cdcEndpointIn, b)
|
||||
} else {
|
||||
usbcdc.waitTxc = false
|
||||
}
|
||||
interrupt.Restore(mask)
|
||||
}
|
||||
|
||||
// Write data to the USBCDC.
|
||||
func (usbcdc *USBCDC) Write(data []byte) (n int, err error) {
|
||||
if usbLineInfo.lineState > 0 {
|
||||
mask := interrupt.Disable()
|
||||
usbcdc.txBuffer.Put(data)
|
||||
if !usbcdc.waitTxc {
|
||||
usbcdc.waitTxc = true
|
||||
usbcdc.Flush()
|
||||
}
|
||||
interrupt.Restore(mask)
|
||||
}
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// WriteByte writes a byte of data to the USB CDC interface.
|
||||
func (usbcdc *USBCDC) WriteByte(c byte) error {
|
||||
usbcdc.Write([]byte{c})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (usbcdc *USBCDC) DTR() bool {
|
||||
return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0
|
||||
}
|
||||
|
||||
func (usbcdc *USBCDC) RTS() bool {
|
||||
return (usbLineInfo.lineState & usb_CDC_LINESTATE_RTS) > 0
|
||||
}
|
||||
|
||||
func cdcCallbackRx(b []byte) {
|
||||
for i := range b {
|
||||
USB.Receive(b[i])
|
||||
}
|
||||
}
|
||||
|
||||
func cdcSetup(setup machine.USBSetup) bool {
|
||||
if setup.BmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
||||
if setup.BRequest == usb_CDC_GET_LINE_CODING {
|
||||
var b [cdcLineInfoSize]byte
|
||||
b[0] = byte(usbLineInfo.dwDTERate)
|
||||
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
||||
b[2] = byte(usbLineInfo.dwDTERate >> 16)
|
||||
b[3] = byte(usbLineInfo.dwDTERate >> 24)
|
||||
b[4] = byte(usbLineInfo.bCharFormat)
|
||||
b[5] = byte(usbLineInfo.bParityType)
|
||||
b[6] = byte(usbLineInfo.bDataBits)
|
||||
|
||||
machine.SendUSBInPacket(0, b[:])
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if setup.BmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
||||
if setup.BRequest == usb_CDC_SET_LINE_CODING {
|
||||
b, err := machine.ReceiveUSBControlPacket()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
|
||||
usbLineInfo.bCharFormat = b[4]
|
||||
usbLineInfo.bParityType = b[5]
|
||||
usbLineInfo.bDataBits = b[6]
|
||||
}
|
||||
|
||||
if setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||
usbLineInfo.lineState = setup.WValueL
|
||||
}
|
||||
|
||||
if setup.BRequest == usb_CDC_SET_LINE_CODING || setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||
// auto-reset into the bootloader
|
||||
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
||||
machine.EnterBootloader()
|
||||
} else {
|
||||
// TODO: cancel any reset
|
||||
}
|
||||
machine.SendZlp()
|
||||
}
|
||||
|
||||
if setup.BRequest == usb_CDC_SEND_BREAK {
|
||||
// TODO: something with this value?
|
||||
// breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
|
||||
// return false;
|
||||
machine.SendZlp()
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func EnableUSBCDC() {
|
||||
machine.USBCDC = New()
|
||||
machine.EnableCDC(USB.Flush, cdcCallbackRx, cdcSetup)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user