Skip to content

Commit

Permalink
perf: Reduce DShot decode code
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasvr committed Oct 15, 2020
1 parent cfafdf0 commit b2a7afb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 75 deletions.
67 changes: 28 additions & 39 deletions BLHeli_S.asm
Expand Up @@ -511,16 +511,16 @@ t1_int:

t1_int_frame_time_scaled:
mov A, Temp2
jnz t1_int_msb_fail ; Frame too long
jnz t1_int_frame_fail ; Frame too long
mov A, Temp1
subb A, DShot_Frame_Length_Thr
jc t1_int_msb_fail ; Frame too short
jc t1_int_frame_fail ; Frame too short
subb A, DShot_Frame_Length_Thr
jnc t1_int_msb_fail ; Frame too long
jnc t1_int_frame_fail ; Frame too long

; Check that correct number of pulses is received
mov A, DPL ; Read current pointer
cjne A, #16, t1_int_msb_fail
cjne A, #16, t1_int_frame_fail

; Decode transmitted data
mov Temp5, #0 ; Reset timestamp
Expand All @@ -539,42 +539,45 @@ t1_int_frame_time_scaled:
t1_int_decode:
ajmp t1_int_decode_msb

t1_int_msb_fail:
t1_int_frame_fail:
mov DPTR, #0 ; Set pointer to start
setb IE_EX0 ; Enable int0 interrupts
setb IE_EX1 ; Enable int1 interrupts
ajmp int0_int_outside_range

t1_int_decode_msb:
; Decode DShot data Msb. Use more code space to save time (by not using loop)
Decode_DShot_2Msb
Decode_DShot_2Msb
Decode_DShot_2Msb
Decode_DShot_2Msb
Decode_DShot_2Bit Temp4
Decode_DShot_2Bit Temp4
; Decode DShot data Lsb high nibble
Decode_DShot_2Bit Temp3
Decode_DShot_2Bit Temp3
ajmp t1_int_decode_lsb

t1_int_lsb_fail:
t1_int_decode_fail:
mov DPTR, #0 ; Set pointer to start
setb IE_EX0 ; Enable int0 interrupts
setb IE_EX1 ; Enable int1 interrupts
ajmp int0_int_outside_range

t1_int_decode_lsb:
; Decode DShot data Lsb
Decode_DShot_2Lsb
Decode_DShot_2Lsb
Decode_DShot_2Lsb
Decode_DShot_2Lsb
; Decode DShot data Lsb low nibble
Decode_DShot_2Bit Temp3
Decode_DShot_2Bit Temp3
; Decode DShot data checksum
Decode_DShot_2Bit Temp7
Decode_DShot_2Bit Temp7

; XOR check (in inverted data, which is ok)
mov A, Temp4
mov A, Temp3
swap A
xrl A, Temp4
xrl A, Temp3
anl A, #0F0h
xrl A, Temp4
anl A, #0Fh
mov Temp2, A
mov A, Temp3
swap A
anl A, #0F0h

mov A, Temp7
anl A, #0Fh
clr C
subb A, Temp2
jz t1_int_xor_ok ; XOR check
Expand All @@ -585,23 +588,15 @@ t1_int_decode_lsb:
ajmp int0_int_outside_range

t1_int_xor_ok:
; Swap to be LSB aligned to 12 bits (and invert)
; Invert DShot data
mov A, Temp4
cpl A
swap A
anl A, #0F0h ; Low nibble of high byte
mov Temp2, A
anl A, #0Fh
mov Temp4, A
mov A, Temp3
cpl A
swap A
anl A, #0Fh ; High nibble of low byte
orl A, Temp2
mov Temp3, A
mov A, Temp4 ; High nibble of high byte
cpl A
swap A
anl A, #0Fh
mov Temp4, A

; Subtract 96 (still 12 bits)
clr C
mov A, Temp3
Expand Down Expand Up @@ -780,12 +775,6 @@ ENDIF

ajmp int0_int_pulse_ready

t1_int_frame_fail:
mov DPTR, #0 ; Set pointer to start
setb IE_EX0 ; Enable int0 interrupts
setb IE_EX1 ; Enable int1 interrupts
ajmp int0_int_outside_range


;**** **** **** **** **** **** **** **** **** **** **** **** ****
;
Expand Down
43 changes: 7 additions & 36 deletions Common.inc
Expand Up @@ -230,61 +230,32 @@ Get_Rcp_End:
ENDIF
ENDM

Decode_Dshot_2Msb MACRO
Decode_DShot_2Bit MACRO dest
movx A, @DPTR
mov Temp6, A
clr C
subb A, Temp5 ; Subtract previous timestamp
clr C
subb A, Temp1
jc t1_int_msb_fail ; Check that bit is longer than minimum
jc t1_int_decode_fail ; Check that bit is longer than minimum

subb A, Temp1 ; Check if bit is zero or one
mov A, Temp4 ; Shift bit into data byte
mov A, dest ; Shift bit into data byte
rlc A
mov Temp4, A
mov dest, A
inc DPL ; Next bit
movx A, @DPTR
mov Temp5, A
clr C
subb A, Temp6
clr C
subb A, Temp1
jc t1_int_msb_fail
jc t1_int_decode_fail

subb A, Temp1
mov A, Temp4
mov A, dest
rlc A
mov Temp4, A
inc DPL
ENDM

Decode_Dshot_2Lsb MACRO
movx A, @DPTR
mov Temp6, A
clr C
subb A, Temp5 ; Subtract previous timestamp
clr C
subb A, Temp1
jc t1_int_lsb_fail ; Check that bit is longer than minimum

subb A, Temp1 ; Check if bit is zero or one
mov A, Temp3 ; Shift bit into data byte
rlc A
mov Temp3, A
inc DPL ; Next bit
movx A, @DPTR
mov Temp5, A
clr C
subb A, Temp6
clr C
subb A, Temp1
jc t1_int_lsb_fail

subb A, Temp1
mov A, Temp3
rlc A
mov Temp3, A
mov dest, A
inc DPL
ENDM

Expand Down

0 comments on commit b2a7afb

Please sign in to comment.