inverter2/my_src/inverter.c

134 lines
3.8 KiB
C
Raw Normal View History

2016-10-25 17:14:58 +02:00
/*
* inverter.c
*
* Created on: 25.10.2016
* Author: dehottgw
*/
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "stm32f1xx_hal.h"
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
#define NUM_OF_SINE_SLOT 30
uint16_t freqOut = 100;
const uint32_t FREQ_IN = 72E6;
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];
typedef struct {
uint8_t slotCnt;
bool running;
GPIO_TypeDef *bridgePolarityPort;
uint16_t bridgePolarityPin;
TIM_HandleTypeDef *handle;
} timerSupport_t;
#define NUM_OF_TIMER 3
timerSupport_t timerSupport[NUM_OF_TIMER];
void inverterBegin() {
for (uint8_t i = 0; i < NUM_OF_SINE_SLOT; i++) {
float angle = i * slotAngle;
sineValues[i] = sinf(angle / 180 * PI);
}
// TODO Configure pins for all three bridges
timerSupport[0].handle = &htim2;
timerSupport[0].running = false;
timerSupport[0].slotCnt = 0;
timerSupport[1].handle = &htim3;
timerSupport[1].running = false;
timerSupport[1].slotCnt = 0;
timerSupport[2].handle = &htim4;
timerSupport[2].running = false;
timerSupport[2].slotCnt = 0;
__HAL_TIM_ENABLE_IT(&htim2, TIM_IT_UPDATE);
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
__HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
__HAL_TIM_ENABLE_IT(&htim4, TIM_IT_UPDATE);
HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM4_IRQn);
HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
}
void inverterSetFrequency(uint8_t freqOut) {
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_OC_Stop_IT(&htim1, TIM_CHANNEL_1);
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_2);
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_3);
for (uint8_t i = 0; i < NUM_OF_TIMER; i++){
HAL_TIM_PWM_Stop_DMA(timerSupport[i].handle, TIM_CHANNEL_1);
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);
}
// TODO Set the 120<32> values for the channels
HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_1);
HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_2);
HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_3);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
for (uint8_t i = 0; i < NUM_OF_TIMER; i++) {
if (htim == timerSupport[i].handle) {
timerSupport[i].slotCnt++;
if (timerSupport[i].slotCnt == NUM_OF_SINE_SLOT + 2) {
timerSupport[i].slotCnt = 2;
HAL_GPIO_TogglePin(timerSupport[i].bridgePolarityPort, timerSupport[i].bridgePolarityPin);
}
}
}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef * htim) {
if (htim->Instance == TIM1) {
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
HAL_GPIO_TogglePin(LED2_PIN_GPIO_Port, LED2_PIN_Pin);
if (! timerSupport[0].running) {
timerSupport[0].running = true;
HAL_TIM_PWM_Start_DMA(timerSupport[0].handle, TIM_CHANNEL_1, (uint32_t*)IV, NUM_OF_SINE_SLOT);
}
} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
HAL_GPIO_TogglePin(LED3_PIN_GPIO_Port, LED3_PIN_Pin);
if (! timerSupport[1].running) {
timerSupport[1].running = true;
HAL_TIM_PWM_Start_DMA(timerSupport[1].handle, TIM_CHANNEL_1, (uint32_t*)IV, NUM_OF_SINE_SLOT);
}
} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
HAL_GPIO_TogglePin(LED4_PIN_GPIO_Port, LED4_PIN_Pin);
if (! timerSupport[2].running) {
timerSupport[2].running = true;
HAL_TIM_PWM_Start_DMA(timerSupport[2].handle, TIM_CHANNEL_1, (uint32_t*)IV, NUM_OF_SINE_SLOT);
}
}
}
}