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

Add 'enums' for PWM frequency and resolution #103 #104

Merged
merged 1 commit into from
Jun 12, 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
4 changes: 2 additions & 2 deletions src/Bluejay.asm
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
;
;**** **** **** **** **** **** **** **** **** **** **** **** ****

$include (Modules\Mcus.asm)
$include (Modules\Enums.asm)

; List of enumerated supported ESCs
; PORT 0 | PORT 1 | PWM COM PWM LED
Expand Down Expand Up @@ -136,7 +136,7 @@ ELSE
IS_MCU_48MHZ EQU 1
ENDIF

IF PWM_FREQ < 3
IF PWM_FREQ == PWM_24 or PWM_FREQ == PWM_48 or PWM_FREQ == PWM_96
; Number of bits in pwm high byte
PWM_BITS_H EQU (2 + IS_MCU_48MHZ - PWM_CENTERED - PWM_FREQ)
ENDIF
Expand Down
22 changes: 21 additions & 1 deletion src/Modules/Mcus.asm → src/Modules/Enums.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,30 @@
;
;**** **** **** **** **** **** **** **** **** **** **** **** ****
;
; MCUs
; Enums
;
; Enumerations to make code more readable
;
;**** **** **** **** **** **** **** **** **** **** **** **** ****

;**** **** **** **** **** **** **** **** **** **** **** **** ****
; MCU Types
;**** **** **** **** **** **** **** **** **** **** **** **** ****
MCU_BB51 EQU 2
MCU_BB2 EQU 1
MCU_BB1 EQU 0

;**** **** **** **** **** **** **** **** **** **** **** **** ****
; PWM frequency and resolution
;**** **** **** **** **** **** **** **** **** **** **** **** ****

; Frequency
PWM_96 EQU 2
PWM_48 EQU 1
PWM_24 EQU 0

; Resolution
PWM_11_BIT EQU 3
PWM_10_BIT EQU 2
PWM_9_BIT EQU 1
PWM_8_BIT EQU 0
18 changes: 9 additions & 9 deletions src/Modules/Isrs.asm
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ t1_int_zero_rcp_checked:
subb A, Temp2 ; 8-bit rc pulse
jnc t1_int_scale_pwm_resolution

IF PWM_BITS_H == 0 ; 8-bit pwm
IF PWM_BITS_H == PWM_8_BIT ; 8-bit pwm
mov A, Temp6
mov Temp2, A
ELSE
Expand All @@ -357,15 +357,15 @@ ENDIF

t1_int_scale_pwm_resolution:
; Scale pwm resolution and invert (duty cycle is defined inversely)
IF PWM_BITS_H == 3 ; 11-bit pwm
IF PWM_BITS_H == PWM_11_BIT
mov A, Temp5
cpl A
anl A, #7
mov Temp3, A
mov A, Temp4
cpl A
mov Temp2, A
ELSEIF PWM_BITS_H == 2 ; 10-bit pwm
ELSEIF PWM_BITS_H == PWM_10_BIT
clr C
mov A, Temp5
rrc A
Expand All @@ -376,7 +376,7 @@ ELSEIF PWM_BITS_H == 2 ; 10-bit pwm
rrc A
cpl A
mov Temp2, A
ELSEIF PWM_BITS_H == 1 ; 9-bit pwm
ELSEIF PWM_BITS_H == PWM_9_BIT
mov B, Temp5
mov A, Temp4
mov C, B.0
Expand All @@ -391,15 +391,15 @@ ELSEIF PWM_BITS_H == 1 ; 9-bit pwm
cpl A
anl A, #1
mov Temp3, A
ELSEIF PWM_BITS_H == 0 ; 8-bit pwm
ELSEIF PWM_BITS_H == PWM_8_BIT
mov A, Temp2 ; Temp2 already 8-bit
cpl A
mov Temp2, A
mov Temp3, #0
ENDIF

; 11-bit effective dithering of 8/9/10-bit pwm
IF PWM_BITS_H < 3
IF PWM_BITS_H == PWM_8_BIT or PWM_BITS_H == PWM_9_BIT or PWM_BITS_H == PWM_10_BIT
jnb Flag_Dithering, t1_int_set_pwm

mov A, Temp4 ; 11-bit low byte
Expand All @@ -418,7 +418,7 @@ IF PWM_BITS_H < 3
add A, #1
mov Temp2, A
jnz t1_int_set_pwm
IF PWM_BITS_H != 0
IF PWM_BITS_H != PWM_8_BIT
mov A, Temp3
addc A, #0
mov Temp3, A
Expand Down Expand Up @@ -466,7 +466,7 @@ ENDIF

; Note: Interrupts are not explicitly disabled
; Assume higher priority interrupts (Int0, Timer0) to be disabled at this point
IF PWM_BITS_H != 0
IF PWM_BITS_H != PWM_8_BIT
; Set power pwm auto-reload registers
Set_Power_Pwm_Reg_L Temp2
Set_Power_Pwm_Reg_H Temp3
Expand All @@ -476,7 +476,7 @@ ENDIF

IF DEADTIME != 0
; Set damp pwm auto-reload registers
IF PWM_BITS_H != 0
IF PWM_BITS_H != PWM_8_BIT
Set_Damp_Pwm_Reg_L Temp4
Set_Damp_Pwm_Reg_H Temp5
ELSE
Expand Down
14 changes: 7 additions & 7 deletions src/Modules/Settings.asm
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ decode_temp_done:

mov Temp1, #Pgm_Braking_Strength ; Read programmed braking strength setting
mov A, @Temp1
IF PWM_BITS_H == 3 ; Scale braking strength to pwm resolution
IF PWM_BITS_H == PWM_11_BIT ; Scale braking strength to pwm resolution
; Note: Added for completeness
; Currently 11-bit pwm is only used on targets with built-in dead time insertion
rl A
Expand All @@ -180,7 +180,7 @@ IF PWM_BITS_H == 3 ; Scale braking strength to pwm resoluti
mov A, Temp2
anl A, #0F8h
mov Pwm_Braking_L, A
ELSEIF PWM_BITS_H == 2
ELSEIF PWM_BITS_H == PWM_10_BIT
rl A
rl A
mov Temp2, A
Expand All @@ -189,15 +189,15 @@ ELSEIF PWM_BITS_H == 2
mov A, Temp2
anl A, #0FCh
mov Pwm_Braking_L, A
ELSEIF PWM_BITS_H == 1
ELSEIF PWM_BITS_H == PWM_9_BIT
rl A
mov Temp2, A
anl A, #01h
mov Pwm_Braking_H, A
mov A, Temp2
anl A, #0FEh
mov Pwm_Braking_L, A
ELSEIF PWM_BITS_H == 0
ELSEIF PWM_BITS_H == PWM_8_BIT
mov Pwm_Braking_H, #0
mov Pwm_Braking_L, A
ENDIF
Expand All @@ -210,17 +210,17 @@ decode_pwm_dithering:
add A, #0FFh ; Carry set if A is not zero
mov Flag_Dithering, C ; Set dithering enabled

IF PWM_BITS_H == 2 ; Initialize pwm dithering bit patterns
IF PWM_BITS_H == PWM_10_BIT ; Initialize pwm dithering bit patterns
mov Temp1, #Dithering_Patterns ; 1-bit dithering (10-bit to 11-bit)
mov @Temp1, #00h ; 00000000
imov Temp1, #55h ; 01010101
ELSEIF PWM_BITS_H == 1
ELSEIF PWM_BITS_H == PWM_9_BIT
mov Temp1, #Dithering_Patterns ; 2-bit dithering (9-bit to 11-bit)
mov @Temp1, #00h ; 00000000
imov Temp1, #11h ; 00010001
imov Temp1, #55h ; 01010101
imov Temp1, #77h ; 01110111
ELSEIF PWM_BITS_H == 0
ELSEIF PWM_BITS_H == PWM_8_BIT
mov Temp1, #Dithering_Patterns ; 3-bit dithering (8-bit to 11-bit)
mov @Temp1, #00h ; 00000000
imov Temp1, #01h ; 00000001
Expand Down