Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup/relative jumps #126

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Bluejay.asm
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,10 @@ setup_dshot:

; Route RCP according to detected DShot signal (normal or inverted)
mov IT01CF, #(80h + (RTX_PIN SHL 4) + RTX_PIN) ; Route RCP input to Int0/1,with Int1 inverted
jnb Flag_Rcp_DShot_Inverted, ($+6)
jnb Flag_Rcp_DShot_Inverted, setup_dshot_clear_flags
mov IT01CF, #(08h + (RTX_PIN SHL 4) + RTX_PIN) ; Route RCP input to Int0/1,with Int0 inverted

setup_dshot_clear_flags:
clr Flag_Demag_Notify ; Clear motor events
clr Flag_Desync_Notify
clr Flag_Stall_Notify
Expand Down Expand Up @@ -1043,9 +1044,10 @@ exit_run_mode_stall_done:
; Extended telemetry flag is important because it is involved in
; EDT safety feature. We don't want to disable EDT arming during
; turtle mode.
jb Flag_User_Reverse_Requested, ($+5)
jb Flag_User_Reverse_Requested, exit_run_mode_stall_done_beep
clr Flag_Ext_Tele

exit_run_mode_stall_done_beep:
; Stalled too many times
clr IE_EA
call beep_motor_stalled
Expand All @@ -1060,9 +1062,10 @@ exit_run_mode_no_stall:
; Extended telemetry flag is important because it is involved in
; EDT safety feature. We don't want to disable EDT arming during
; turtle mode.
jb Flag_User_Reverse_Requested, ($+5)
jb Flag_User_Reverse_Requested, exit_run_mode_no_stall_beep
clr Flag_Ext_Tele

exit_run_mode_no_stall_beep:
; Clear stall counter
mov Startup_Stall_Cnt, #0

Expand Down
8 changes: 6 additions & 2 deletions src/Modules/DShot.asm
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ detect_rcp_level:
mov C, RTX_BIT

detect_rcp_level_read:
jc ($+5)
jc detect_rcp_level_read_check_high_to_low
jb RTX_BIT, detect_rcp_level ; Level changed from low to high - start over
jnc ($+5)

detect_rcp_level_read_check_high_to_low:
jnc detect_rcp_level_check_loop
jnb RTX_BIT, detect_rcp_level ; Level changed from high to low - start over

detect_rcp_level_check_loop:
djnz ACC, detect_rcp_level_read

mov Flag_Rcp_DShot_Inverted, C
Expand Down
4 changes: 2 additions & 2 deletions src/Modules/Eeprom.asm
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ write_eeprom_byte_from_acc:
clr C
mov A, DPH ; Check that address is not in bootloader area
subb A, #BOOTLOADER_OFFSET
jc ($+3)

jc write_eeprom_byte_write
ret

write_eeprom_byte_write:
mov A, Temp8
mov FLKEY, Flash_Key_1 ; First key code
mov FLKEY, Flash_Key_2 ; Second key code
Expand Down
26 changes: 17 additions & 9 deletions src/Modules/Isrs.asm
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ t1_int_decode_lsb:
t1_int_outside_range:
inc Rcp_Outside_Range_Cnt
mov A, Rcp_Outside_Range_Cnt
jnz ($+4)
jnz t1_int_outside_range_check_limit
dec Rcp_Outside_Range_Cnt

t1_int_outside_range_check_limit:
clr C
mov A, Rcp_Outside_Range_Cnt
subb A, #50 ; Allow a given number of outside pulses
Expand Down Expand Up @@ -180,8 +181,10 @@ t1_int_decode_checksum:
xrl A, Temp4
xrl A, Temp5
xrl A, Temp3
jnb Flag_Rcp_DShot_Inverted, ($+4)
jnb Flag_Rcp_DShot_Inverted, t1_int_decode_checksum_xor_check
cpl A ; Invert checksum if using inverted DShot

t1_int_decode_checksum_xor_check:
anl A, #0Fh
jnz t1_int_outside_range ; XOR check

Expand Down Expand Up @@ -236,8 +239,10 @@ t1_int_normal_range:
mov Temp5, A

t1_int_bidir_set:
jnb Flag_Pgm_Dir_Rev, ($+4) ; Check programmed direction
jnb Flag_Pgm_Dir_Rev, t1_int_bidir_set_dir ; Check programmed direction
cpl C ; Reverse direction

t1_int_bidir_set_dir:
mov Flag_Rcp_Dir_Rev, C ; Set rcp direction

clr C ; Multiply throttle value by 2
Expand All @@ -255,7 +260,7 @@ t1_int_not_bidir:
mov Temp3, A

; Align to 11 bits
;clr C ; Note: Cleared by div
;clr C ; Note: Cleared by div
rrca Temp5
mov A, Temp4
rrc A
Expand All @@ -267,10 +272,11 @@ t1_int_not_bidir:
mov A, Temp5
addc A, #0
mov Temp5, A
jnb ACC.3, ($+7) ; Limit to 11-bit maximum
jnb ACC.3, t1_int_not_bidir_do_not_boost ; Limit to 11-bit maximum
mov Temp4, #0FFh
mov Temp5, #07h

t1_int_not_bidir_do_not_boost:
; Do not boost when changing direction in bidirectional mode
jb Flag_Motor_Started, t1_int_startup_boosted

Expand Down Expand Up @@ -300,7 +306,7 @@ t1_int_stall_boost:
mov A, Temp5
addc A, #0
mov Temp5, A
jnb ACC.3, ($+7) ; Limit to 11-bit maximum
jnb ACC.3, t1_int_startup_boosted ; Limit to 11-bit maximum
mov Temp4, #0FFh
mov Temp5, #07h

Expand Down Expand Up @@ -328,17 +334,19 @@ t1_int_rcp_not_zero:
t1_int_zero_rcp_checked:
; Decrement outside range counter
mov A, Rcp_Outside_Range_Cnt
jz ($+4)
jz t1_int_zero_rcp_checked_set_limit
dec Rcp_Outside_Range_Cnt

t1_int_zero_rcp_checked_set_limit:
; Set pwm limit
clr C
mov A, Pwm_Limit ; Limit to the smallest
mov Temp6, A ; Store limit in Temp6
subb A, Pwm_Limit_By_Rpm
jc ($+4)
jc t1_int_zero_rcp_checked_check_limit
mov Temp6, Pwm_Limit_By_Rpm

t1_int_zero_rcp_checked_check_limit:
; Check against limit
clr C
mov A, Temp6
Expand Down Expand Up @@ -552,7 +560,7 @@ t2_int_flag_rcp_stop_check:
; Increment Rcp_Stop_Cnt clipping it to 255
mov A, Rcp_Stop_Cnt
inc A
jz ($+4)
jz t2_int_exit
inc Rcp_Stop_Cnt

;**** **** **** **** **** **** **** **** **** **** **** **** ****
Expand Down
4 changes: 3 additions & 1 deletion src/Modules/Macros.asm
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ ENDM

; DShot GCR encoding, adjust time by adding to previous item
GCR_Add_Time MACRO reg
LOCAL gcr_add_time_set
mov B, @reg
mov A, DShot_GCR_Pulse_Time_2
cjne A, B, ($+5)
cjne A, B, gcr_add_time_set
mov A, DShot_GCR_Pulse_Time_3
gcr_add_time_set:
mov @reg, A
ENDM

Expand Down
10 changes: 7 additions & 3 deletions src/Modules/Power.asm
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ set_pwm_limit:
jz set_pwm_limit_low_rpm_exit ; Avoid divide by zero

mov A, #255 ; Divide 255 by Comm_Period4x_H
jnb Flag_Initial_Run_Phase, ($+5) ; More protection for initial run phase
jnb Flag_Initial_Run_Phase, set_pwm_limit_calculate ; More protection for initial run phase
mov A, #127

set_pwm_limit_calculate:
mov B, Comm_Period4x_H
div AB
mov B, Low_Rpm_Pwr_Slope ; Multiply by slope
mul AB
mov Temp1, A ; Set new limit
xch A, B
jz ($+4) ; Limit to max
jz set_pwm_limit_check_limit_to_min ; Limit to max

mov Temp1, #0FFh

set_pwm_limit_check_limit_to_min:
clr C
mov A, Temp1 ; Limit to min
subb A, Pwm_Limit_Beg
Expand Down Expand Up @@ -113,7 +116,8 @@ set_pwm_limit_high_rpm_inc_limit:
inc A

set_pwm_limit_high_rpm_store:
jz ($+4)
jz set_pwm_limit_high_rpm_end
mov Pwm_Limit_By_Rpm, A

set_pwm_limit_high_rpm_end:
ret
6 changes: 4 additions & 2 deletions src/Modules/Settings.asm
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,18 @@ decode_settings:
mov Temp1, #Pgm_Startup_Power_Max
mov A, #80 ; Limit to at most 80
subb A, @Temp1
jnc ($+4)
jnc decode_settings_check_low_rpm
mov @Temp1, #80

decode_settings_check_low_rpm:
; Check low rpm power slope
mov Temp1, #Pgm_Rpm_Power_Slope
mov A, #13 ; Limit to at most 13
subb A, @Temp1
jnc ($+4)
jnc decode_settings_set_low_rpm
mov @Temp1, #13

decode_settings_set_low_rpm:
mov Low_Rpm_Pwr_Slope, @Temp1

; Decode demag compensation
Expand Down
29 changes: 19 additions & 10 deletions src/Modules/Timing.asm
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ calc_next_comm_period:
mov Temp1, TMR2L ; Load Timer2 value
mov Temp2, TMR2H
mov Temp3, Timer2_X
jnb TMR2CN0_TF2H, ($+4) ; Check if interrupt is pending
jnb TMR2CN0_TF2H,calc_next_comm_period_enable_timer2 ; Check if interrupt is pending
inc Temp3 ; If it is pending,then timer has already wrapped

calc_next_comm_period_enable_timer2:
setb TMR2CN0_TR2 ; Timer2 enabled
setb IE_EA

Expand Down Expand Up @@ -220,7 +222,7 @@ calc_next_comm_done:
clr C
mov A, Comm_Period4x_H
subb A, #2 ; Is Comm_Period4x_H below 2? (above ~160k erpm)
jnc ($+4)
jnc calc_next_comm_15deg
setb Flag_High_Rpm ; Yes - Set high rpm flag

calc_next_comm_15deg:
Expand Down Expand Up @@ -290,9 +292,10 @@ calc_next_comm_period_fast:

clr C
subb A, #2 ; Is Comm_Period4x_H 2 or more? (below ~160k erpm)
jc ($+4)
jc calc_next_comm_period_fast_div_comm_perio4x_by_16
clr Flag_High_Rpm ; Yes - Clear high rpm bit

calc_next_comm_period_fast_div_comm_perio4x_by_16:
mov A, Temp4 ; Divide Comm_Period4x by 16 and store in Temp4/3
swap A
mov Temp7, A
Expand Down Expand Up @@ -373,14 +376,15 @@ adjust_comm_timing:
inc Temp8 ; Increase timing (if metric 130 or above)

subb A, #30
jc ($+3)
jc adjust_comm_timing_limit_to_max

inc Temp8 ; Increase timing again (if metric 160 or above)

adjust_comm_timing_limit_to_max:
clr C
mov A, Temp8 ; Limit timing to max
subb A, #6
jc ($+4)
jc load_comm_timing_done

mov Temp8, #5 ; Set timing to max (if timing 6 or above)

Expand Down Expand Up @@ -603,9 +607,10 @@ comp_start:
mov Temp4, #(1 SHL IS_MCU_48MHZ) ; Max number of readings required
jb Flag_High_Rpm, comp_check_timeout ; Branch if high rpm

jnb Flag_Initial_Run_Phase, ($+5)
jnb Flag_Initial_Run_Phase, comp_start_check_startup_phase
clr Flag_Demag_Detected ; Clear demag detected flag if start phases

comp_start_check_startup_phase:
jnb Flag_Startup_Phase, comp_not_startup
mov Temp3, #(27 SHL IS_MCU_48MHZ) ; Set many samples during startup,approximately one pwm period
mov Temp4, #(27 SHL IS_MCU_48MHZ)
Expand All @@ -620,12 +625,14 @@ IF MCU_TYPE == MCU_BB1
clr C
rrc A
ENDIF
jnz ($+3)
jnz comp_not_startup_check_ok_readings
inc A ; Minimum 1

comp_not_startup_check_ok_readings:
mov Temp3, A
clr C
subb A, #(20 SHL IS_MCU_48MHZ)
jc ($+4)
jc comp_check_timeout
mov Temp3, #(20 SHL IS_MCU_48MHZ) ; Maximum 20

comp_check_timeout:
Expand Down Expand Up @@ -679,9 +686,10 @@ comp_read_wrong_startup:
clr C
mov A, Temp3
subb A, Temp4 ; If above initial requirement - do not increment further
jc ($+3)
jc comp_read_wrong_startup_jump ; TODO: Skip this jump to optimize
dec Temp3

comp_read_wrong_startup_jump:
sjmp comp_check_timeout ; Continue to look for good ones

comp_read_wrong_extend_timeout:
Expand Down Expand Up @@ -809,9 +817,10 @@ wait_for_comm_demag_event_added:
mov Demag_Detected_Metric, A
clr C
subb A, #120 ; Limit to minimum 120
jnc ($+5)
jnc wait_for_comm_update_demag_metric_max
mov Demag_Detected_Metric, #120

wait_for_comm_update_demag_metric_max:
; Update demag metric max
clr C
mov A, Demag_Detected_Metric
Expand Down