Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmiriuta committed Jul 15, 2019
1 parent 5ccb154 commit 00a39d7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/main/drivers/input.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "input.h"

TIM_HandleTypeDef inputTimerHandle;
DMA_HandleTypeDef inputTimerDmaHandle;

bool inputArmed, inputDataValid;
uint8_t inputProtocol;
uint32_t inputData;
uint32_t inputNormed, outputPwm;
uint32_t inputArmCounter, inputTimeoutCounter;
uint32_t inputBufferDMA[INPUT_BUFFER_DMA_SIZE];

extern TIM_HandleTypeDef inputTimerHandle;
extern uint8_t motorDirection;

void inputArmCheck(void) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/drivers/motor.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "motor.h"

TIM_HandleTypeDef motorPwmTimerHandle, motorCommutationTimerHandle;
COMP_HandleTypeDef motorBemfComparatorHandle;

bool motorBemfRising;
Expand Down Expand Up @@ -224,7 +225,7 @@ void motorStart() {

motorCommutate();

TIM3->CNT = 0xffff;
motorCommutationTimerHandle.Instance->CNT = 0xffff;
motorBemfCounter = 0;
motorRunning = true;
HAL_COMP_Start_IT(&motorBemfComparatorHandle);
Expand All @@ -236,15 +237,15 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) {
uint32_t motorTimestamp;

__disable_irq();
motorTimestamp = TIM3->CNT;
motorTimestamp = motorCommutationTimerHandle.Instance->CNT;

if ((!motorRunning) || (!motorStartup)) {
HAL_COMP_Stop_IT(&motorBemfComparatorHandle);
__enable_irq();
return;
}

while ((TIM3->CNT - motorTimestamp) < motorBemfFilterDelay);
while ((motorCommutationTimerHandle.Instance->CNT - motorTimestamp) < motorBemfFilterDelay);

for (int i = 0; i < motorBemfFilterLevel; i++) {
if ((motorBemfRising && HAL_COMP_GetOutputLevel(&motorBemfComparatorHandle) == COMP_OUTPUTLEVEL_HIGH) ||
Expand All @@ -260,7 +261,7 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) {
#endif

HAL_COMP_Stop_IT(&motorBemfComparatorHandle);
TIM3->CNT = 0xffff;
motorCommutationTimerHandle.Instance->CNT = 0xffff;

motorBemfCounter++;
motorBemfZeroCounterTimeout = 0;
Expand All @@ -269,7 +270,7 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) {
// ToDo
//if ((motorCommutationDelay > 31) && (motorCommutationDelay < 613) && (outputPwm > 37) && (outputPwm < 707)) {
if (motorCommutationDelay > 17) {
while (TIM3->CNT < motorCommutationDelay) {
while (motorCommutationTimerHandle.Instance->CNT < motorCommutationDelay) {
#if (defined(_DEBUG_) && defined(MOTOR_TIMING))
LED_TOGGLE(BLUE);
#endif
Expand Down
24 changes: 12 additions & 12 deletions src/main/drivers/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern ADC_HandleTypeDef adcHandle;
extern COMP_HandleTypeDef motorBemfComparatorHandle;
extern TIM_HandleTypeDef motorPwmTimerHandle, timer3Handle, inputTimerHandle;
extern TIM_HandleTypeDef motorPwmTimerHandle, motorCommutationTimerHandle, inputTimerHandle;

extern uint32_t adcValue[3];
extern uint32_t inputBufferDMA[INPUT_BUFFER_DMA_SIZE];
Expand Down Expand Up @@ -168,26 +168,26 @@ void systemMotorPwmTimerInit(void) {
//while (HAL_TIM_OC_Start_IT(&motorPwmTimerHandle, TIM_CHANNEL_4) != HAL_OK);
}

void systemTimer3Init(void) {
void systemMotorCommutationTimerInit(void) {
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

timer3Handle.Instance = TIM3;
timer3Handle.Init.Prescaler = 7;
timer3Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
timer3Handle.Init.Period = 65535;
timer3Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timer3Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
while (HAL_TIM_Base_Init(&timer3Handle) != HAL_OK);
motorCommutationTimerHandle.Instance = TIM3;
motorCommutationTimerHandle.Init.Prescaler = 7;
motorCommutationTimerHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
motorCommutationTimerHandle.Init.Period = 65535;
motorCommutationTimerHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
motorCommutationTimerHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
while (HAL_TIM_Base_Init(&motorCommutationTimerHandle) != HAL_OK);

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
while (HAL_TIM_ConfigClockSource(&timer3Handle, &sClockSourceConfig) != HAL_OK);
while (HAL_TIM_ConfigClockSource(&motorCommutationTimerHandle, &sClockSourceConfig) != HAL_OK);

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
while (HAL_TIMEx_MasterConfigSynchronization(&timer3Handle, &sMasterConfig) != HAL_OK);
while (HAL_TIMEx_MasterConfigSynchronization(&motorCommutationTimerHandle, &sMasterConfig) != HAL_OK);

while (HAL_TIM_Base_Start(&timer3Handle) != HAL_OK);
while (HAL_TIM_Base_Start(&motorCommutationTimerHandle) != HAL_OK);
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/drivers/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ void systemDmaInit(void);
void systemAdcInit(void);
void systemBemfComparatorInit(void);
void systemMotorPwmTimerInit(void);
void systemTimer3Init(void);
void systemMotorCommutationTimerInit(void);
void systemInputTimerInit(void);
11 changes: 5 additions & 6 deletions src/main/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#include "main.h"

extern COMP_HandleTypeDef motorBemfComparatorHandle;

TIM_HandleTypeDef motorPwmTimerHandle, timer3Handle, inputTimerHandle;
DMA_HandleTypeDef inputTimerDmaHandle;

// ADC
kalman_t adcCurrentFilterState;
extern uint32_t adcVoltageRaw, adcCurrentRaw, adcTemperatureRaw;
Expand All @@ -13,6 +8,8 @@ extern uint32_t adcVoltage, adcCurrent, adcTemperature;
// motor
kalman_t motorCommutationIntervalFilterState;
uint32_t motorCommutationInterval, motorCommutationDelay;
extern COMP_HandleTypeDef motorBemfComparatorHandle;
extern TIM_HandleTypeDef motorPwmTimerHandle, motorCommutationTimerHandle;
extern bool motorStartup, motorRunning;
extern bool motorDirection, motorSlowDecay, motorBrakeActiveProportional;
extern uint32_t motorBemfZeroCrossTimestamp;
Expand All @@ -21,6 +18,8 @@ extern uint32_t motorDutyCycle, motorBemfCounter;
extern uint32_t motorBemfZeroCounterTimeout, motorBemfZeroCounterTimeoutThreshold;

// input
extern TIM_HandleTypeDef inputTimerHandle;
extern DMA_HandleTypeDef inputTimerDmaHandle;
extern bool inputArmed, inputDataValid;
extern uint8_t inputProtocol;
extern uint32_t inputData;
Expand All @@ -39,7 +38,7 @@ int main(void) {
systemBemfComparatorInit();
systemAdcInit();
systemMotorPwmTimerInit();
systemTimer3Init();
systemMotorCommutationTimerInit();
systemInputTimerInit();

// init
Expand Down
5 changes: 3 additions & 2 deletions src/main/target/stm32f0xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

extern DMA_HandleTypeDef adcDmaHandle;
extern DMA_HandleTypeDef inputTimerDmaHandle;
extern TIM_HandleTypeDef motorPwmTimerHandle, motorCommutationTimerHandle;

void HAL_MspInit(void) {
__HAL_RCC_SYSCFG_CLK_ENABLE();
Expand Down Expand Up @@ -78,7 +79,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) {
//HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 0);
//HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
}
else if(htim_base->Instance == TIM3) {
else if(htim_base->Instance == motorCommutationTimerHandle.Instance) {
__HAL_RCC_TIM3_CLK_ENABLE();
}
else if(htim_base->Instance == TIM15) {
Expand Down Expand Up @@ -143,7 +144,7 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) {

HAL_NVIC_DisableIRQ(TIM1_CC_IRQn);
}
else if(htim_base->Instance == TIM3) {
else if(htim_base->Instance == motorCommutationTimerHandle.Instance) {
__HAL_RCC_TIM3_CLK_DISABLE();
}
else if(htim_base->Instance == TIM15) {
Expand Down

0 comments on commit 00a39d7

Please sign in to comment.