diff --git a/my_src/inverter.c b/my_src/inverter.c index 756c561..aba996b 100644 --- a/my_src/inverter.c +++ b/my_src/inverter.c @@ -20,12 +20,12 @@ extern TIM_HandleTypeDef htim5; #define NUM_OF_SINE_SLOT 30 -uint16_t freqOut = 100; const uint32_t FREQ_IN = 1E6; const float PI = 3.14159; float slotAngle = 180.0 / NUM_OF_SINE_SLOT; float sineValues[NUM_OF_SINE_SLOT]; uint16_t IV[NUM_OF_SINE_SLOT]; + volatile uint32_t timer1Cnt; typedef struct { @@ -33,6 +33,8 @@ typedef struct { bool running; GPIO_TypeDef *bridgePolarityPort; uint16_t bridgePolarityPin; + GPIO_TypeDef *startMarkPort; + uint16_t startMarkPin; IRQn_Type irqType; TIM_HandleTypeDef *handle; uint32_t channel; @@ -40,6 +42,7 @@ typedef struct { #define NUM_OF_TIMER 3 volatile timerSupport_t timerSupport[NUM_OF_TIMER]; +volatile uint8_t phaseOrder[NUM_OF_TIMER]; @@ -56,6 +59,9 @@ void inverterBegin() { timerSupport[0].bridgePolarityPin = BridgePolarity0_Pin; timerSupport[0].irqType = TIM2_IRQn; timerSupport[0].channel = TIM_CHANNEL_1; + timerSupport[0].startMarkPort = LED2_PIN_GPIO_Port; + timerSupport[0].startMarkPin = LED2_PIN_Pin; + timerSupport[1].handle = &htim5; timerSupport[1].running = false; timerSupport[1].slotCnt = 0; @@ -63,6 +69,9 @@ void inverterBegin() { timerSupport[1].bridgePolarityPin = BridgePolarity1_Pin; timerSupport[1].irqType = TIM5_IRQn; timerSupport[1].channel = TIM_CHANNEL_2; + timerSupport[1].startMarkPort = LED3_PIN_GPIO_Port; + timerSupport[1].startMarkPin = LED3_PIN_Pin; + timerSupport[2].handle = &htim4; timerSupport[2].running = false; timerSupport[2].slotCnt = 0; @@ -70,38 +79,66 @@ void inverterBegin() { timerSupport[2].bridgePolarityPin = BridgePolarity2_Pin; timerSupport[2].irqType = TIM4_IRQn; timerSupport[2].channel = TIM_CHANNEL_1; + timerSupport[2].startMarkPort = LED4_PIN_GPIO_Port; + timerSupport[2].startMarkPin = LED4_PIN_Pin; } -void inverterSetFrequency(uint8_t freqOut) { +void inverterStart(uint8_t freqOut, direction_t direction) { + inverterStop(); + uint16_t slotWidth = (FREQ_IN / (freqOut * NUM_OF_SINE_SLOT * 4)); for (uint8_t i = 0; i < NUM_OF_SINE_SLOT; i++) { IV[i] = (uint16_t)(sineValues[i] * 0.9 * slotWidth); } - HAL_TIM_Base_Stop(&htim1); - __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); + if (direction == CLOCKWISE) { + phaseOrder[0] = 0; + phaseOrder[1] = 1; + phaseOrder[2] = 2; + } else { + phaseOrder[0] = 0; + phaseOrder[1] = 2; + phaseOrder[2] = 1; + } for (uint8_t i = 0; i < NUM_OF_TIMER; i++){ - HAL_TIM_PWM_Stop_DMA(timerSupport[i].handle, timerSupport[i].channel); - __HAL_TIM_DISABLE_IT(timerSupport[i].handle, TIM_IT_UPDATE); - timerSupport[i].slotCnt = 0; timerSupport[i].running = false; HAL_GPIO_WritePin(timerSupport[i].bridgePolarityPort, timerSupport[i].bridgePolarityPin, GPIO_PIN_RESET); __HAL_TIM_SET_AUTORELOAD(timerSupport[i].handle, slotWidth); } - HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET); - HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); - HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET); - timer1Cnt = 0; __HAL_TIM_SET_AUTORELOAD(&htim1, slotWidth); HAL_TIM_Base_Start_IT(&htim1); __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); } +void inverterStop() { + HAL_TIM_Base_Stop(&htim1); + __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); + + for (uint8_t i = 0; i < NUM_OF_TIMER; i++){ + HAL_TIM_PWM_Stop_DMA(timerSupport[i].handle, timerSupport[i].channel); + __HAL_TIM_DISABLE_IT(timerSupport[i].handle, TIM_IT_UPDATE); + HAL_GPIO_WritePin(timerSupport[i].startMarkPort, timerSupport[i].startMarkPin, GPIO_PIN_RESET); + } +} + + +static void startPhase(uint8_t phaseIdx) { + uint8_t timerIdx = phaseOrder[phaseIdx]; + if (! timerSupport[timerIdx].running) { + HAL_GPIO_WritePin(timerSupport[timerIdx].startMarkPort, + timerSupport[timerIdx].startMarkPin, GPIO_PIN_SET); + HAL_TIM_PWM_Start_DMA(timerSupport[timerIdx].handle, timerSupport[timerIdx].channel, + (uint32_t*)IV, NUM_OF_SINE_SLOT); + __HAL_TIM_ENABLE_IT(timerSupport[timerIdx].handle, TIM_IT_UPDATE); + timerSupport[timerIdx].running = true; + } +} + void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { for (uint8_t i = 0; i < NUM_OF_TIMER; i++) { if (htim == timerSupport[i].handle) { @@ -112,29 +149,16 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { } } } + if (htim == &htim1) { HAL_GPIO_TogglePin(Sync_GPIO_Port, Sync_Pin); + if (timer1Cnt == 0) { - if (! timerSupport[0].running) { - HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET); - timerSupport[0].running = true; - __HAL_TIM_ENABLE_IT(timerSupport[0].handle, TIM_IT_UPDATE); - HAL_TIM_PWM_Start_DMA(timerSupport[0].handle, timerSupport[0].channel, (uint32_t*)IV, NUM_OF_SINE_SLOT); - } + startPhase(0); } else if (timer1Cnt == ((NUM_OF_SINE_SLOT * 2) / 3)) { - if (! timerSupport[1].running) { - HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET); - timerSupport[1].running = true; - HAL_TIM_PWM_Start_DMA(timerSupport[1].handle, timerSupport[1].channel, (uint32_t*)IV, NUM_OF_SINE_SLOT); - __HAL_TIM_ENABLE_IT(timerSupport[1].handle, TIM_IT_UPDATE); - } + startPhase(1); } else if (timer1Cnt == ((NUM_OF_SINE_SLOT * 2) * 2 / 3)) { - if (! timerSupport[2].running) { - HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); - timerSupport[2].running = true; - HAL_TIM_PWM_Start_DMA(timerSupport[2].handle, timerSupport[2].channel, (uint32_t*)IV, NUM_OF_SINE_SLOT); - __HAL_TIM_ENABLE_IT(timerSupport[2].handle, TIM_IT_UPDATE); - } + startPhase(2); } timer1Cnt++; diff --git a/my_src/inverter.h b/my_src/inverter.h index 89bc7e1..dc29e28 100644 --- a/my_src/inverter.h +++ b/my_src/inverter.h @@ -9,8 +9,11 @@ #define INVERTER_H_ +typedef enum { CLOCKWISE, COUNTERCLOCKWISE } direction_t; + void inverterBegin(); -void inverterSetFrequency(uint8_t freqOut); +void inverterStart(uint8_t freqOut, direction_t direction); +void inverterStop(); #endif /* INVERTER_H_ */ diff --git a/my_src/main2.c b/my_src/main2.c index 675c32c..ff02653 100644 --- a/my_src/main2.c +++ b/my_src/main2.c @@ -45,16 +45,16 @@ void my_errorHandler() { } void testSwitchFrequency1(void *handle) { - inverterSetFrequency(100); + inverterStart(100, CLOCKWISE); } void testSwitchFrequency2(void *handle) { - inverterSetFrequency(50); + inverterStart(50, CLOCKWISE); } void my_setup_2() { inverterBegin(); - inverterSetFrequency(50); + inverterStart(50, CLOCKWISE); schAdd(testSwitchFrequency1, NULL, 5000, 0); schAdd(testSwitchFrequency2, NULL, 10000, 0); schAdd(testSwitchFrequency1, NULL, 15000, 0);