some changes
This commit is contained in:
		
							
								
								
									
										133
									
								
								my_src/inverter.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								my_src/inverter.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,133 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								my_src/inverter.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								my_src/inverter.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
/*
 | 
			
		||||
 * inverter.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 25.10.2016
 | 
			
		||||
 *      Author: dehottgw
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef INVERTER_H_
 | 
			
		||||
#define INVERTER_H_
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void inverterInit();
 | 
			
		||||
void inverterSetFrequency(uint8_t freqOut);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif /* INVERTER_H_ */
 | 
			
		||||
@@ -10,108 +10,43 @@
 | 
			
		||||
 * 2. insert my_setup_1(); at USER CODE 1 in main.c
 | 
			
		||||
 * 3. insert my_setup_2(); at USER CODE 2 in main.c
 | 
			
		||||
 * 4. insert my_loop(); at USER CODE 3 in main.c
 | 
			
		||||
 * 5. insert my_errorHandler(); at USER CODE BEGIN Error_Handler in main.c
 | 
			
		||||
 * All this is done by the script insertMyCode.sh in tools
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <math.h>
 | 
			
		||||
#include <PontCoopScheduler.h>
 | 
			
		||||
#include "stm32f1xx_hal.h"
 | 
			
		||||
#include "inverter.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];
 | 
			
		||||
volatile uint8_t slotCnt;
 | 
			
		||||
 | 
			
		||||
void blink(void *handle) {
 | 
			
		||||
	HAL_GPIO_TogglePin(LED_PIN_GPIO_Port, LED_PIN_Pin);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void my_setup_1() {
 | 
			
		||||
	for (uint8_t i = 0; i < NUM_OF_SINE_SLOT; i++) {
 | 
			
		||||
		float angle = i * slotAngle;
 | 
			
		||||
		sineValues[i] = sinf(angle / 180 * PI);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	schInit();
 | 
			
		||||
	schAdd(blink, NULL, 0, 100);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void my_setup_2() {
 | 
			
		||||
	uint16_t myArr = (FREQ_IN / (freqOut * NUM_OF_SINE_SLOT * 4));
 | 
			
		||||
 | 
			
		||||
	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);
 | 
			
		||||
 | 
			
		||||
	HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 0);
 | 
			
		||||
	HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
 | 
			
		||||
 | 
			
		||||
	__HAL_TIM_SET_AUTORELOAD(&htim3, myArr);
 | 
			
		||||
	HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
 | 
			
		||||
	HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	uint16_t slotWidth = __HAL_TIM_GET_AUTORELOAD(&htim2);
 | 
			
		||||
	for (uint8_t i = 0; i < NUM_OF_SINE_SLOT; i++) {
 | 
			
		||||
		IV[i] = (uint16_t)(sineValues[i] * 0.9 * slotWidth);
 | 
			
		||||
	}
 | 
			
		||||
	slotCnt = 0;
 | 
			
		||||
	__HAL_TIM_SET_AUTORELOAD(&htim2, myArr);
 | 
			
		||||
	HAL_GPIO_WritePin(BridgePolarity_GPIO_Port, BridgePolarity_Pin, GPIO_PIN_RESET);
 | 
			
		||||
	HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, (uint32_t*)IV, NUM_OF_SINE_SLOT);
 | 
			
		||||
 | 
			
		||||
	__HAL_TIM_ENABLE_IT(&htim2, TIM_IT_UPDATE);
 | 
			
		||||
	HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
 | 
			
		||||
	HAL_NVIC_EnableIRQ(TIM2_IRQn);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void my_errorHandler() {
 | 
			
		||||
	HAL_GPIO_WritePin(ERROR_PIN_GPIO_Port, ERROR_PIN_Pin, GPIO_PIN_SET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void my_loop() {
 | 
			
		||||
	schExec();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void HAL_SYSTICK_Callback() {
 | 
			
		||||
	schUpdate();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
 | 
			
		||||
	if (htim->Instance == TIM2) {
 | 
			
		||||
		slotCnt++;
 | 
			
		||||
		if (slotCnt == NUM_OF_SINE_SLOT + 2) {
 | 
			
		||||
			slotCnt = 2;
 | 
			
		||||
			HAL_GPIO_TogglePin(BridgePolarity_GPIO_Port, BridgePolarity_Pin);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
void my_errorHandler() {
 | 
			
		||||
	HAL_GPIO_WritePin(ERROR_PIN_GPIO_Port, ERROR_PIN_Pin, GPIO_PIN_SET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
		} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
 | 
			
		||||
			HAL_GPIO_TogglePin(LED3_PIN_GPIO_Port, LED3_PIN_Pin);
 | 
			
		||||
		} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
 | 
			
		||||
			HAL_GPIO_TogglePin(LED4_PIN_GPIO_Port, LED4_PIN_Pin);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
void my_setup_2() {
 | 
			
		||||
	inverterBegin();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user