Skip to content

Commit

Permalink
ws2812: support high-MHz ARMv6M chips like the RP2040
Browse files Browse the repository at this point in the history
The possible branch distance is a lot shorter on ARMv6M (Cortex-M and
Cortex-M0+) for conditional branches. Therefore, convert this long
conditional branch into an unconditional branch.

This probably makes the code a little bit slower but because it is in
the low period of the WS2812 signal it shouldn't matter for the
protocol. And it avoids difficult workarounds specifically for the
RP2040.
  • Loading branch information
aykevl authored and deadprogram committed Mar 10, 2022
1 parent 9a98d1b commit 2d32995
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
6 changes: 4 additions & 2 deletions ws2812/gen-ws2812.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var architectures = map[string]architectureImpl{
maxBaseCyclesT0H: 1 + 3 + 2, // shift + branch (not taken) + store
minBaseCyclesT1H: 1 + 1 + 2, // shift + branch (taken) + store
maxBaseCyclesT1H: 1 + 3 + 2, // shift + branch (taken) + store
minBaseCyclesTLD: 1 + 1 + 2, // subtraction + branch + store (in next cycle)
minBaseCyclesTLD: 1 + 2 + 2, // subtraction + branch x2 + store (in next cycle)
valueTemplate: "uint32(c) << 24",
template: `
1: @ send_bit
Expand All @@ -73,7 +73,9 @@ var architectures = map[string]architectureImpl{
str {maskClear}, {portClear} @ [2] T1H -> T1L transition
@DELAY3
subs {i}, #1 @ [1]
bne.n 1b @ [1/3] send_bit
beq.n 3f @ [1/3] end
b 1b @ [1/3] send_bit
3: @ end
`,
},
"tinygoriscv": {
Expand Down
25 changes: 15 additions & 10 deletions ws2812/ws2812-asm_cortexm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func (d Device) writeByte16(c byte) {
nop
nop
nop
nop
subs {i}, #1 @ [1]
bne.n 1b @ [1/3] send_bit
beq.n 3f @ [1/3] end
b 1b @ [1/3] send_bit
3: @ end
`, map[string]interface{}{
"value": value,
"i": 8,
Expand Down Expand Up @@ -186,9 +187,10 @@ func (d Device) writeByte48(c byte) {
nop
nop
nop
nop
subs {i}, #1 @ [1]
bne.n 1b @ [1/3] send_bit
beq.n 3f @ [1/3] end
b 1b @ [1/3] send_bit
3: @ end
`, map[string]interface{}{
"value": value,
"i": 8,
Expand Down Expand Up @@ -351,9 +353,10 @@ func (d Device) writeByte64(c byte) {
nop
nop
nop
nop
subs {i}, #1 @ [1]
bne.n 1b @ [1/3] send_bit
beq.n 3f @ [1/3] end
b 1b @ [1/3] send_bit
3: @ end
`, map[string]interface{}{
"value": value,
"i": 8,
Expand Down Expand Up @@ -638,9 +641,10 @@ func (d Device) writeByte120(c byte) {
nop
nop
nop
nop
subs {i}, #1 @ [1]
bne.n 1b @ [1/3] send_bit
beq.n 3f @ [1/3] end
b 1b @ [1/3] send_bit
3: @ end
`, map[string]interface{}{
"value": value,
"i": 8,
Expand Down Expand Up @@ -1032,9 +1036,10 @@ func (d Device) writeByte168(c byte) {
nop
nop
nop
nop
subs {i}, #1 @ [1]
bne.n 1b @ [1/3] send_bit
beq.n 3f @ [1/3] end
b 1b @ [1/3] send_bit
3: @ end
`, map[string]interface{}{
"value": value,
"i": 8,
Expand Down

0 comments on commit 2d32995

Please sign in to comment.