Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
refactor: cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorBel committed Mar 31, 2021
1 parent e56f251 commit 725e08f
Showing 1 changed file with 25 additions and 168 deletions.
193 changes: 25 additions & 168 deletions src/bldc-sensored/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,58 +122,23 @@ void tim1_init()
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// // Time Base configuration
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_TimeBaseStructure.TIM_Prescaler = 0;
// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// TIM_TimeBaseStructure.TIM_Period = BLDC_CHOPPER_PERIOD;
// TIM_TimeBaseStructure.TIM_ClockDivision = 0;
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
// TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

// Channel 1, 2, 3 – set to PWM mode - all 6 outputs
// TIM_OCInitTypeDef TIM_OCInitStructure;
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
// TIM_OCInitStructure.TIM_Pulse = (int)(BLDC_CHOPPER_PERIOD * 0.1);

// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
// TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; /// !!!
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; /// !!!

// TIM_OC1Init(TIM1, &TIM_OCInitStructure);
// TIM_OC2Init(TIM1, &TIM_OCInitStructure);
// TIM_OC3Init(TIM1, &TIM_OCInitStructure);

// TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
// TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
// TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
// TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;

// DeadTime[ns] = value * (1/SystemCoreFreq) (on 72MHz: 7 is 98ns)
// TIM_BDTRInitStructure.TIM_DeadTime = 0;

// TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;

// // Break functionality (overload input)
// TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
// TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;

// TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

// TIM_Cmd(TIM1, ENABLE);
// // enable motor timer main output (the bridge signals)
// TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM1->SMCR = b15 + b4 + b5 + b6; // make ETR input active low
TIM1->CR2 = 0;
TIM1->CCR1 = 1350;
TIM1->CCR2 = 1350;
TIM1->CCR3 = 1350;
// TIM1->CCR4 = 1100;
TIM1->ARR = BLDC_CHOPPER_PERIOD;
TIM1->CR1 = 0x0001;

// note: b15 b7 and b7 are to enable ETR based current limit
TIM1->CCMR1 = 0x6868 + b15 + b7;
TIM1->CCMR2 = 0x6868 + b7;
// // b4 for cc4 and b7 for brk interrupt
// //TIM1->DIER = b4+b7; // enable cc4 interrupt
TIM1->DIER = b6;
}

void tim4_init(void)
{
uint8_t res6_7 = (uint8_t)((GPIO_ReadInputData(GPIOB) & (GPIO_Pin_6 | GPIO_Pin_7)) >> 5);
uint8_t res4 = (uint8_t)((GPIO_ReadInputData(GPIOB) & GPIO_Pin_4) >> 4);
uint8_t res = res6_7 | res4;
return res;
void HallSensorsGetPosition(void)
{
// uint8_t res6_7 = (uint8_t)((GPIO_ReadInputData(GPIOB) & (GPIO_Pin_6 | GPIO_Pin_7)) >> 5);
Expand Down Expand Up @@ -254,21 +219,6 @@ void EXTI9_5_IRQHandler(void)
}
}

// void EXTI4_IRQHandler(void)
// {
// if (EXTI_GetITStatus(EXTI_Line4) != RESET)
// {
// // Clear interrupt flags
// EXTI_ClearITPendingBit(EXTI_Line4);
// EXTI_ClearITPendingBit(EXTI_Line6);
// EXTI_ClearITPendingBit(EXTI_Line7);

// // Commutation
// HallSensorsGetPosition();
// commutate();
// }
// }

void commutate(void)
{
// Hall_CBA
Expand Down Expand Up @@ -400,63 +350,22 @@ int main(void)

// // tim1 setup
tim1_init();
TIM1->SMCR = b15 + b4 + b5 + b6; // make ETR input active low
TIM1->CR2 = 0;
TIM1->CCR1 = 1350;
TIM1->CCR2 = 1350;
TIM1->CCR3 = 1350;
// TIM1->CCR4 = 1100;
TIM1->ARR = BLDC_CHOPPER_PERIOD;
TIM1->CR1 = 0x0001;

// note: b15 b7 and b7 are to enable ETR based current limit
TIM1->CCMR1 = 0x6868 + b15 + b7;
TIM1->CCMR2 = 0x6868 + b7;
// // b4 for cc4 and b7 for brk interrupt
// //TIM1->DIER = b4+b7; // enable cc4 interrupt
TIM1->DIER = b6;

motorstartinit();

while (1)
{ // backgroung loop
//TIM1->BDTR= b15+b11+b10+b12+b13; // set MOE

if ((4095 - potvalue) > 200)
run = 255;
if ((4095 - potvalue) < 100)
run = 0;

// if ((TIM1->BDTR & b15) == 0) // overcurrent fault condition
// {
// run = 0;
// while (1)
// {
// ledon;
// delay(1000000);
// ledoff;
// delay(1000000);
// if ((4095 - potvalue) < 100)
// {
// TIM1->BDTR = b15 + b11 + b10 + b12 + b13; // set MOE
// break;
// }

// } //end of LED flash loop

// } // end of if overcurrent

// //TIM1->BDTR= b15+b11+b10+b12+b13; // set MOE

// if ((4095 - potvalue) > 200)
// run = 255;
// if ((4095 - potvalue) < 100)
// run = 0;

// if (run)
// ledon;
// if (run == 0)
// ledoff;

// runningdc = ((4095 - potvalue) * 1200) >> 12;
// if (runningdc > 1195)
// runningdc = 1300;
// if (dutycyclehold && (runningdc > 600))
// runningdc = 600;
// if (run)
// ledon;
// if (run == 0)
// ledoff;
} // end of backgroung loop

} // end of main
Expand Down Expand Up @@ -499,57 +408,6 @@ int main(void)
// } // end of phase switch statement
// } // end of commutate function

// void commutate2(void)
// {

// //TIM1->CCER=b10+b8+b6+b4+b2+b0; // enable all 6
// switch (phase)
// {
// case 0: // phase AB
// // enable all 6 except AN
// // invert AN
// TIM1->CCER = b10 + b8 + b6 + b4 + b0 + b3;
// TIM1->CCMR1 = 0x4868 + b15 + b7; // B low, A PWM
// TIM1->CCMR2 = 0x6858 + b7; // force C ref high (phc en low)
// break;
// case 1: // phase AC
// // enable all 6 except AN
// // invert AN
// TIM1->CCER = b10 + b8 + b6 + b4 + b0 + b3;
// TIM1->CCMR1 = 0x5868 + b15 + b7; // force B high and A PWM
// TIM1->CCMR2 = 0x6848 + b7; // force C ref low
// break;
// case 2: // phase BC
// // enable all 6 except BN
// // invert BN
// TIM1->CCER = b10 + b8 + b4 + b2 + b0 + b7;
// TIM1->CCMR1 = 0x6858 + b15 + b7; // force B PWM and A high
// TIM1->CCMR2 = 0x6848 + b7; // force C ref low
// break;
// case 3: // phase BA
// // enable all 6 except BN
// // invert BN
// TIM1->CCER = b10 + b8 + b4 + b2 + b0 + b7;
// TIM1->CCMR1 = 0x6848 + b15 + b7; // force B PWM and A ref low
// TIM1->CCMR2 = 0x6858 + b7; // force C ref high
// break;
// case 4: // phase CA
// // enable all 6 except CN
// // invert CN
// TIM1->CCER = b8 + b6 + b4 + b2 + b0 + b11; // enable all 6 except CN
// TIM1->CCMR1 = 0x5848 + b15 + b7; // force B high and A ref low
// TIM1->CCMR2 = 0x6868 + b7; // force C PWM
// break;
// case 5: // phase CB
// // enable all 6 except CN
// // invert CN
// TIM1->CCER = b8 + b6 + b4 + b2 + b0 + b11; // enable all 6 except CN
// TIM1->CCMR1 = 0x4858 + b15 + b7; // force B low and A high
// TIM1->CCMR2 = 0x6868 + b7; // force C PWM
// break;
// } // end of phase switch statement
// } // end of commutate2 function

void delay(unsigned long time) // 1000 is 200 usec
{
while (time > 0)
Expand All @@ -558,7 +416,6 @@ void delay(unsigned long time) // 1000 is 200 usec

// unsigned short readadc(unsigned char chnl)
// {

// ADC1->CHSELR = (1 << chnl); // set ADC MUX
// ADC1->CR = b2; // start adc conversion

Expand Down

0 comments on commit 725e08f

Please sign in to comment.