Skip to content

Commit

Permalink
ramp rate and comparator interrupt changes g071 f421
Browse files Browse the repository at this point in the history
  • Loading branch information
AlkaMotors committed Dec 24, 2024
1 parent 3deb564 commit 6669105
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 91 deletions.
1 change: 1 addition & 0 deletions Inc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern uint8_t compute_dshot_flag;
extern uint16_t battery_voltage;
extern int16_t actual_current;
extern uint16_t e_rpm;
extern uint32_t average_interval;


#ifdef STMICRO
Expand Down
12 changes: 6 additions & 6 deletions Inc/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@
#define CURRENT_ADC_CHANNEL LL_ADC_CHANNEL_4
#define CURRENT_ADC_PIN LL_GPIO_PIN_4
#define RAMP_SPEED_LOW_RPM 1
#define RAMP_SPEED_HIGH_RPM 2
#define RAMP_SPEED_HIGH_RPM 1
#define USE_LED_STRIP
#endif

Expand Down Expand Up @@ -1919,11 +1919,11 @@
#endif

#ifndef RAMP_SPEED_STARTUP
#define RAMP_SPEED_STARTUP 1 // adjusted 2.14 to match duty cycle change between mcu targets.
#define RAMP_SPEED_STARTUP 2 // adjusted 2.14 to match duty cycle change between mcu targets.
#endif

#ifndef RAMP_SPEED_LOW_RPM // below commutation interval of 250us
#define RAMP_SPEED_LOW_RPM 4
#define RAMP_SPEED_LOW_RPM 6
#endif

#ifndef RAMP_SPEED_HIGH_RPM
Expand Down Expand Up @@ -3275,9 +3275,9 @@
#endif

#ifdef HARDWARE_GROUP_AT_245
#define PHASE_A_COMP 0x400000F1 // pa2 // works for polling mode
#define PHASE_B_COMP 0x400000C1 // pa4
#define PHASE_C_COMP 0x400000D1 // pa5
#define PHASE_A_COMP 0x400000F5 // pa2 // works for polling mode
#define PHASE_B_COMP 0x400000C5 // pa4
#define PHASE_C_COMP 0x400000D5 // pa5
#endif

#ifdef HARDWARE_GROUP_L4_A
Expand Down
12 changes: 6 additions & 6 deletions Mcu/f051/Inc/comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#endif /* COMPARATOR_H_ */

// Medium speed comparator
#define COMP_PA0 0b1100101
#define COMP_PA4 0b1000101
#define COMP_PA5 0b1010101
//#define COMP_PA0 0b1100101
//#define COMP_PA4 0b1000101
//#define COMP_PA5 0b1010101

// High speed comparator
// #define COMP_PA0 0b1100001
// #define COMP_PA4 0b1000001
// #define COMP_PA5 0b1010001
#define COMP_PA0 0b1100001
#define COMP_PA4 0b1000001
#define COMP_PA5 0b1010001

#include "main.h"

Expand Down
15 changes: 10 additions & 5 deletions Mcu/f421/Src/at32f421_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "ADC.h"
#include "main.h"
#include "targets.h"
#include "common.h"
#include "comparator.h"


extern void transfercomplete();
extern void PeriodElapsedCallback();
extern void interruptRoutine();
Expand All @@ -19,7 +23,6 @@ extern char send_telemetry;
extern char telemetry_done;
extern char servoPwm;
extern char dshot;
extern uint32_t commutation_interval;
int exti_int = 0;

void HardFault_Handler(void)
Expand Down Expand Up @@ -177,12 +180,14 @@ void DMA1_Channel5_4_IRQHandler(void)
*/
void ADC1_CMP_IRQHandler(void)
{
if ((EXINT->intsts & EXTI_LINE) != (uint32_t)RESET) {
EXINT->intsts = EXTI_LINE;
if((INTERVAL_TIMER->cval) > (commutation_interval >> 1)){
if((INTERVAL_TIMER->cval) > ((average_interval>>1))){
EXINT->intsts = EXTI_LINE;
interruptRoutine();
}else{
if (getCompOutputLevel() == rising){
EXINT->intsts = EXTI_LINE;
}
}
}
}

/**
Expand Down
33 changes: 9 additions & 24 deletions Mcu/g071/Src/comparator.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
///*
// * comparator.c
// *
// * Created on: Sep. 26, 2020
// * Author: Alka
// */
//
// #include "comparator.h"
// #include "targets.h"
//
//
// void maskPhaseInterrupts(){
// EXTI->IMR1 &= ~(1 << 18);
// EXTI->RPR1 = EXTI_LINE;
// EXTI->FPR1 = EXTI_LINE;
//// LL_EXTI_ClearRisingFlag_0_31(EXTI_LINE);
//// LL_EXTI_ClearFallingFlag_0_31(EXTI_LINE);
//}
//
// void enableCompInterrupts(){
// EXTI->IMR1 |= (1 << 18);
//}
//

/*
* comparator.c
*
Expand All @@ -36,6 +12,7 @@

COMP_TypeDef* active_COMP = COMP2;
uint32_t current_EXTI_LINE = LL_EXTI_LINE_18;
uint8_t medium_speed_set;

uint8_t getCompOutputLevel() { return (active_COMP->CSR >> 30 & 1); }

Expand All @@ -55,6 +32,14 @@ void enableCompInterrupts() { EXTI->IMR1 |= current_EXTI_LINE; }

void changeCompInput()
{
if((average_interval < 400) && medium_speed_set){
LL_COMP_SetPowerMode(active_COMP, LL_COMP_POWERMODE_HIGHSPEED);
medium_speed_set = 0;
}
if((average_interval > 600) && !medium_speed_set){
LL_COMP_SetPowerMode(active_COMP, LL_COMP_POWERMODE_MEDIUMSPEED);
medium_speed_set = 1;
}
if (step == 1 || step == 4) { // c floating
#ifdef N_VARIANT
current_EXTI_LINE = PHASE_C_EXTI_LINE;
Expand Down
8 changes: 4 additions & 4 deletions Mcu/g071/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ void MX_COMP1_Init(void)
COMP_InitStruct.InputMinus = LL_COMP_INPUT_MINUS_IO3;
COMP_InitStruct.InputHysteresis = LL_COMP_HYSTERESIS_NONE;
COMP_InitStruct.OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
COMP_InitStruct.OutputBlankingSource = LL_COMP_BLANKINGSRC_TIM1_OC5;
COMP_InitStruct.OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
LL_COMP_Init(COMP1, &COMP_InitStruct);
LL_COMP_SetPowerMode(COMP1, LL_COMP_POWERMODE_MEDIUMSPEED);
LL_COMP_SetPowerMode(COMP1, LL_COMP_POWERMODE_HIGHSPEED);
LL_COMP_SetCommonWindowMode(__LL_COMP_COMMON_INSTANCE(COMP1),
LL_COMP_WINDOWMODE_DISABLE);
LL_COMP_SetCommonWindowOutput(__LL_COMP_COMMON_INSTANCE(COMP1),
Expand Down Expand Up @@ -200,9 +200,9 @@ void MX_COMP2_Init(void)
COMP_InitStruct.InputMinus = LL_COMP_INPUT_MINUS_IO3;
COMP_InitStruct.InputHysteresis = LL_COMP_HYSTERESIS_NONE;
COMP_InitStruct.OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
COMP_InitStruct.OutputBlankingSource = LL_COMP_BLANKINGSRC_TIM1_OC5;
COMP_InitStruct.OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
LL_COMP_Init(COMP2, &COMP_InitStruct);
LL_COMP_SetPowerMode(COMP2, LL_COMP_POWERMODE_MEDIUMSPEED);
LL_COMP_SetPowerMode(COMP2, LL_COMP_POWERMODE_HIGHSPEED);
LL_COMP_SetCommonWindowMode(__LL_COMP_COMMON_INSTANCE(COMP2),
LL_COMP_WINDOWMODE_DISABLE);
LL_COMP_SetCommonWindowOutput(__LL_COMP_COMMON_INSTANCE(COMP2),
Expand Down
75 changes: 49 additions & 26 deletions Mcu/g071/Src/stm32g0xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include "IO.h"
#include "WS2812.h"
#include "targets.h"
#include "comparator.h"
#include "common.h"

/* USER CODE END Includes */

/* Private typedef
Expand Down Expand Up @@ -84,7 +87,6 @@ extern char dshot_telemetry;
extern char armed;
extern char out_put;
extern uint8_t compute_dshot_flag;
extern uint32_t commutation_interval;

/* USER CODE END EV */

Expand Down Expand Up @@ -234,35 +236,56 @@ void DMA1_Channel2_3_IRQHandler(void)
*/
void ADC1_COMP_IRQHandler(void)
{
if (LL_EXTI_IsActiveFallingFlag_0_31(LL_EXTI_LINE_18)) {
LL_EXTI_ClearFallingFlag_0_31(LL_EXTI_LINE_18);
if((INTERVAL_TIMER->CNT) > (commutation_interval >> 1)){
interruptRoutine();
}
return;
}

if (LL_EXTI_IsActiveRisingFlag_0_31(LL_EXTI_LINE_18)) {
LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_18);
if((INTERVAL_TIMER->CNT) > (commutation_interval >> 1)){
interruptRoutine();
if (LL_EXTI_IsActiveFallingFlag_0_31(LL_EXTI_LINE_18)) {
if((INTERVAL_TIMER->CNT) > (average_interval >> 1)){
LL_EXTI_ClearFallingFlag_0_31(LL_EXTI_LINE_18);
interruptRoutine();
}else{
if(getCompOutputLevel() == rising){
LL_EXTI_ClearFallingFlag_0_31(LL_EXTI_LINE_18);
return;
}
}
return;
}
if (LL_EXTI_IsActiveFallingFlag_0_31(LL_EXTI_LINE_17)) {
LL_EXTI_ClearFallingFlag_0_31(LL_EXTI_LINE_17);
if((INTERVAL_TIMER->CNT) > (commutation_interval >> 1)){
interruptRoutine();
return;
}

if (LL_EXTI_IsActiveRisingFlag_0_31(LL_EXTI_LINE_18)) {
if((INTERVAL_TIMER->CNT) > (average_interval >> 1)){
LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_18);
interruptRoutine();
}else{
if(getCompOutputLevel() == rising){
LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_18);
return;
}
}
return;
return;
}
if (LL_EXTI_IsActiveFallingFlag_0_31(LL_EXTI_LINE_17)) {
if((INTERVAL_TIMER->CNT) > (average_interval >> 1)){
LL_EXTI_ClearFallingFlag_0_31(LL_EXTI_LINE_17);
interruptRoutine();
}else{
if(getCompOutputLevel() == rising){
LL_EXTI_ClearFallingFlag_0_31(LL_EXTI_LINE_17);
return;
}
}
if (LL_EXTI_IsActiveRisingFlag_0_31(LL_EXTI_LINE_17)) {
LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_17);
if((INTERVAL_TIMER->CNT) > (commutation_interval >> 1)){
interruptRoutine();
}
return;
return;
}

if (LL_EXTI_IsActiveRisingFlag_0_31(LL_EXTI_LINE_17)) {
if((INTERVAL_TIMER->CNT) > (average_interval >> 1)){
LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_17);
interruptRoutine();
}else{
if(getCompOutputLevel() == rising){
LL_EXTI_ClearRisingFlag_0_31(LL_EXTI_LINE_17);
return;
}
}
return;
}
}

/**
Expand Down
33 changes: 13 additions & 20 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,8 @@ void commutate()
#endif
bemfcounter = 0;
zcfound = 0;
commutation_intervals[step - 1] = commutation_interval; // just used to calulate average
commutation_intervals[step - 1] = commutation_interval; // just used to calulate average
e_com_time = ((commutation_intervals[0] + commutation_intervals[1] + commutation_intervals[2] + commutation_intervals[3] + commutation_intervals[4] + commutation_intervals[5]) + 4) >> 1; // COMMUTATION INTERVAL IS 0.5US INCREMENTS
#ifdef USE_PULSE_OUT
if(rising){
GPIOB->scr = GPIO_PINS_8;
Expand All @@ -833,7 +834,7 @@ void PeriodElapsedCallback()
{
DISABLE_COM_TIMER_INT(); // disable interrupt
commutate();
commutation_interval = (3 * commutation_interval + thiszctime) >> 2;
commutation_interval = ((commutation_interval)+((lastzctime + thiszctime) >> 1))>>1;
if (!eepromBuffer.auto_advance) {
advance = (commutation_interval >> 3) * temp_advance; // 60 divde 8 7.5 degree increments
} else {
Expand Down Expand Up @@ -872,8 +873,9 @@ void interruptRoutine()
}
}
__disable_irq();
maskPhaseInterrupts();
thiszctime = INTERVAL_TIMER_COUNT;
maskPhaseInterrupts();
lastzctime = thiszctime;
thiszctime = INTERVAL_TIMER_COUNT;
SET_INTERVAL_TIMER_COUNT(0);
SET_AND_ENABLE_COM_INT(waitTime+1); // enable COM_TIMER interrupt
__enable_irq();
Expand Down Expand Up @@ -1779,7 +1781,7 @@ if(zero_crosses < 5){
min_bemf_counts_down = TARGET_MIN_BEMF_COUNTS;
}
RELOAD_WATCHDOG_COUNTER();
e_com_time = ((commutation_intervals[0] + commutation_intervals[1] + commutation_intervals[2] + commutation_intervals[3] + commutation_intervals[4] + commutation_intervals[5]) + 4) >> 1; // COMMUTATION INTERVAL IS 0.5US INCREMENTS

if (eepromBuffer.variable_pwm) {
tim1_arr = map(commutation_interval, 96, 200, TIMER1_MAX_ARR / 2,
TIMER1_MAX_ARR);
Expand Down Expand Up @@ -1875,7 +1877,6 @@ if(zero_crosses < 5){
}
}
#endif

average_interval = e_com_time / 3;
if (desync_check && zero_crosses > 10) {
if ((getAbsDif(last_average_interval, average_interval) > average_interval >> 1) && (average_interval < 2000)) { // throttle resitricted before zc 20.
Expand Down Expand Up @@ -1989,28 +1990,20 @@ if(zero_crosses < 5){
}

if (degrees_celsius > eepromBuffer.limits.temperature) {
duty_cycle_maximum = map(degrees_celsius, eepromBuffer.limits.temperature - 10, eepromBuffer.limits.temperature + 10,
throttle_max_at_high_rpm / 2, 1);
duty_cycle_maximum = map(degrees_celsius, eepromBuffer.limits.temperature - 10, eepromBuffer.limits.temperature + 10,
throttle_max_at_high_rpm / 2, 1);
}
if (zero_crosses < 100 && commutation_interval > 500) {
#ifdef MCU_G071
TIM1->CCR5 = 1; // comparator blanking
filter_level = 8;
#else
filter_level = 12;
#endif
filter_level = 12;
} else {
#ifdef MCU_G071
TIM1->CCR5 = 10;
#endif
filter_level = map(average_interval, 100, 500, 3, 12);
filter_level = map(average_interval, 100, 500, 3, 12);
}
if (commutation_interval < 50) {
filter_level = 2;
filter_level = 2;
}

if (eepromBuffer.auto_advance) {
auto_advance_level = map(duty_cycle, 100, 2000, 13, 23);
auto_advance_level = map(duty_cycle, 100, 2000, 13, 23);
}

/**************** old routine*********************/
Expand Down

0 comments on commit 6669105

Please sign in to comment.