Skip to content

Commit 630c498

Browse files
authored
nrf52840: implement USB-CDC (#883)
* machine/nrf52840: usb-cdc implementation
1 parent d11abb3 commit 630c498

10 files changed

+603
-4
lines changed

src/machine/board_circuitplay_bluefruit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ const (
5656
UART_RX_PIN = P0_30 // PORTB
5757
)
5858

59+
// UART0 is the USB device
60+
var (
61+
UART0 = USB
62+
)
63+
5964
// I2C pins
6065
const (
6166
SDA_PIN = P0_05 // I2C0 external

src/machine/board_clue_alpha.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ const (
103103
UART_TX_PIN = D1
104104
)
105105

106+
// UART0 is the USB device
107+
var (
108+
UART0 = USB
109+
)
110+
106111
// I2C pins
107112
const (
108113
SDA_PIN = D20 // I2C0 external

src/machine/board_nrf52840-mdk.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const (
1818
UART_RX_PIN Pin = 19
1919
)
2020

21+
// UART0 is the USB device
22+
var (
23+
UART0 = USB
24+
)
25+
2126
// I2C pins (unused)
2227
const (
2328
SDA_PIN = NoPin
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// +build nrf52840,pca10056
2+
3+
package machine
4+
5+
// UART0 is the NRF UART
6+
var (
7+
UART0 = NRF_UART0
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// +build nrf52840,reelboard
2+
3+
package machine
4+
5+
// UART0 is the NRF UART
6+
var (
7+
UART0 = NRF_UART0
8+
)

src/machine/machine_nrf.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ type UART struct {
6666

6767
// UART
6868
var (
69-
// UART0 is the hardware serial port on the NRF.
70-
UART0 = UART{Buffer: NewRingBuffer()}
69+
// NRF_UART0 is the hardware UART on the NRF SoC.
70+
NRF_UART0 = UART{Buffer: NewRingBuffer()}
7171
)
7272

7373
// Configure the UART.
@@ -88,7 +88,7 @@ func (uart UART) Configure(config UARTConfig) {
8888
nrf.UART0.INTENSET.Set(nrf.UART_INTENSET_RXDRDY_Msk)
8989

9090
// Enable RX IRQ.
91-
intr := interrupt.New(nrf.IRQ_UART0, UART0.handleInterrupt)
91+
intr := interrupt.New(nrf.IRQ_UART0, NRF_UART0.handleInterrupt)
9292
intr.SetPriority(0xc0) // low priority
9393
intr.Enable()
9494
}

src/machine/machine_nrf51.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"device/nrf"
77
)
88

9+
var (
10+
UART0 = NRF_UART0
11+
)
12+
913
func CPUFrequency() uint32 {
1014
return 16000000
1115
}

src/machine/machine_nrf52.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"unsafe"
88
)
99

10+
var (
11+
UART0 = NRF_UART0
12+
)
13+
1014
func CPUFrequency() uint32 {
1115
return 64000000
1216
}

src/machine/usb.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build sam
1+
// +build sam nrf52840
22

33
package machine
44

@@ -376,6 +376,23 @@ type cdcLineInfo struct {
376376
lineState uint8
377377
}
378378

379+
// strToUTF16LEDescriptor converts a utf8 string into a string descriptor
380+
// note: the following code only converts ascii characters to UTF16LE. In order
381+
// to do a "proper" conversion, we would need to pull in the 'unicode/utf16'
382+
// package, which at the time this was written added 512 bytes to the compiled
383+
// binary.
384+
func strToUTF16LEDescriptor(in string) []byte {
385+
size := (len(in) << 1) + 2
386+
out := make([]byte, size)
387+
out[0] = byte(size)
388+
out[1] = 0x03
389+
for i, rune := range in {
390+
out[(i<<1)+2] = byte(rune)
391+
out[(i<<1)+3] = 0
392+
}
393+
return out
394+
}
395+
379396
var (
380397
// TODO: allow setting these
381398
usb_STRING_LANGUAGE = [2]uint16{(3 << 8) | (2 + 2), 0x0409} // English

0 commit comments

Comments
 (0)