Skip to content

Commit 2d32995

Browse files
aykevldeadprogram
authored andcommitted
ws2812: support high-MHz ARMv6M chips like the RP2040
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.
1 parent 9a98d1b commit 2d32995

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

ws2812/gen-ws2812.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var architectures = map[string]architectureImpl{
5959
maxBaseCyclesT0H: 1 + 3 + 2, // shift + branch (not taken) + store
6060
minBaseCyclesT1H: 1 + 1 + 2, // shift + branch (taken) + store
6161
maxBaseCyclesT1H: 1 + 3 + 2, // shift + branch (taken) + store
62-
minBaseCyclesTLD: 1 + 1 + 2, // subtraction + branch + store (in next cycle)
62+
minBaseCyclesTLD: 1 + 2 + 2, // subtraction + branch x2 + store (in next cycle)
6363
valueTemplate: "uint32(c) << 24",
6464
template: `
6565
1: @ send_bit
@@ -73,7 +73,9 @@ var architectures = map[string]architectureImpl{
7373
str {maskClear}, {portClear} @ [2] T1H -> T1L transition
7474
@DELAY3
7575
subs {i}, #1 @ [1]
76-
bne.n 1b @ [1/3] send_bit
76+
beq.n 3f @ [1/3] end
77+
b 1b @ [1/3] send_bit
78+
3: @ end
7779
`,
7880
},
7981
"tinygoriscv": {

ws2812/ws2812-asm_cortexm.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ func (d Device) writeByte16(c byte) {
5656
nop
5757
nop
5858
nop
59-
nop
6059
subs {i}, #1 @ [1]
61-
bne.n 1b @ [1/3] send_bit
60+
beq.n 3f @ [1/3] end
61+
b 1b @ [1/3] send_bit
62+
3: @ end
6263
`, map[string]interface{}{
6364
"value": value,
6465
"i": 8,
@@ -186,9 +187,10 @@ func (d Device) writeByte48(c byte) {
186187
nop
187188
nop
188189
nop
189-
nop
190190
subs {i}, #1 @ [1]
191-
bne.n 1b @ [1/3] send_bit
191+
beq.n 3f @ [1/3] end
192+
b 1b @ [1/3] send_bit
193+
3: @ end
192194
`, map[string]interface{}{
193195
"value": value,
194196
"i": 8,
@@ -351,9 +353,10 @@ func (d Device) writeByte64(c byte) {
351353
nop
352354
nop
353355
nop
354-
nop
355356
subs {i}, #1 @ [1]
356-
bne.n 1b @ [1/3] send_bit
357+
beq.n 3f @ [1/3] end
358+
b 1b @ [1/3] send_bit
359+
3: @ end
357360
`, map[string]interface{}{
358361
"value": value,
359362
"i": 8,
@@ -638,9 +641,10 @@ func (d Device) writeByte120(c byte) {
638641
nop
639642
nop
640643
nop
641-
nop
642644
subs {i}, #1 @ [1]
643-
bne.n 1b @ [1/3] send_bit
645+
beq.n 3f @ [1/3] end
646+
b 1b @ [1/3] send_bit
647+
3: @ end
644648
`, map[string]interface{}{
645649
"value": value,
646650
"i": 8,
@@ -1032,9 +1036,10 @@ func (d Device) writeByte168(c byte) {
10321036
nop
10331037
nop
10341038
nop
1035-
nop
10361039
subs {i}, #1 @ [1]
1037-
bne.n 1b @ [1/3] send_bit
1040+
beq.n 3f @ [1/3] end
1041+
b 1b @ [1/3] send_bit
1042+
3: @ end
10381043
`, map[string]interface{}{
10391044
"value": value,
10401045
"i": 8,

0 commit comments

Comments
 (0)