#include "main.h"
#include "stdlib.h"
#include "string.h"
/* USER CODE BEGIN PM */
uint16_t motor1Faults;
uint16_t rxbuffer[10] = {0};
uint16_t flag = 0;
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
ADC_HandleTypeDef hadc3;
CORDIC_HandleTypeDef hcordic;
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_ADC3_Init(void);
static void MX_CORDIC_Init(void);
static void MX_TIM1_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM2_Init(void);
static void MX_NVIC_Init(void);
void setMotorSpeed(uint16_t target_speed);
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_ADC3_Init();
MX_CORDIC_Init();
MX_TIM1_Init();
MX_USART2_UART_Init();
MX_MotorControl_Init();
MX_TIM2_Init();
/* Initialize interrupts */
MX_NVIC_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim2);
HAL_UART_Receive_DMA(&huart2, (uint8_t*)rxbuffer, 4);
/* USER CODE END 2 */
/* Infinite loop */
while (1)
{
if(flag == 1)
{
MC_StartMotor1();
}
else
{
MC_StopMotor1();
}
}
}
void setMotorSpeed(uint16_t target_speed)
{
static uint16_t previous_speed = DEFAULT_TARGET_SPEED_RPM;
static uint16_t duration = 0;
const uint16_t slope = 3000;
if (target_speed > MAX_APPLICATION_SPEED_RPM)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"Target speed exceeds the max", 29, 100);
return;
}
else
{
if(target_speed < previous_speed)
{
duration = (previous_speed - target_speed) / slope;
duration = duration * 1000;
}
else
{
duration = (target_speed - previous_speed) / slope;
duration = duration * 1000;
}
MC_ProgramSpeedRampMotor1_F(target_speed, duration);
previous_speed = target_speed;
}
}
/**
* @brief NVIC Configuration.
* @retval None
*/
static void MX_NVIC_Init(void)
{
/* USART2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(USART2_IRQn, 3, 1);
HAL_NVIC_EnableIRQ(USART2_IRQn);
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
/* TIM1_BRK_TIM15_IRQn interrupt configuration */
HAL_NVIC_SetPriority(TIM1_BRK_TIM15_IRQn, 4, 1);
HAL_NVIC_EnableIRQ(TIM1_BRK_TIM15_IRQn);
/* TIM1_UP_TIM16_IRQn interrupt configuration */
HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
/* ADC1_2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(ADC1_2_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
/* EXTI15_10_IRQn interrupt configuration */
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
/* TIM2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 1843200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMAMUX1_CLK_ENABLE();
__HAL_RCC_DMA1_CLK_ENABLE();
}
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
motor1Faults = MC_GetOccurredFaultsMotor1();
//motor1Faults = MC_GetCurrentFaultsMotor1();
if(motor1Faults & MC_DURATION)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_DURATION", 12,100);
}
if(motor1Faults & MC_OVER_VOLT)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_OVER_VOLT", 13,100);
}
if(motor1Faults & MC_UNDER_VOLT)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_UNDER_VOLT", 14,100);
}
if(motor1Faults & MC_OVER_TEMP)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_OVER_TEMP", 13,100);
}
if(motor1Faults & MC_START_UP)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_START_UP", 12,100);
}
if(motor1Faults & MC_SPEED_FDBK)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_SPEED_FDBK", 14,100);
}
if(motor1Faults & MC_BREAK_IN)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_BREAK_IN", 12,100);
}
if(motor1Faults & MC_SW_ERROR)
{
HAL_UART_Transmit(&huart2, (uint8_t*)"MC_SW_ERRORS",12,100);
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart2, (uint8_t*)rxbuffer, 4, 100);
HAL_UART_Receive_DMA(&huart2, (uint8_t*)rxbuffer, 4);
if (strncmp((const char*)rxbuffer, "start", sizeof("start") - 1) == 0)
{
flag = 1;
//MC_StartMotor1();
}
else if (strncmp((const char*)rxbuffer, "stop", sizeof("stop") - 1) == 0)
{
flag = 0;
//MC_StopMotor1();
}
else
{
rxbuffer[4] = '\0';
uint16_t rpmValue = atoi((char*)rxbuffer);
if (500 <= rpmValue && rpmValue <= 3000)
{
setMotorSpeed(rpmValue);
}
else
{
HAL_UART_Transmit(&huart2, (uint8_t*)"Invalid command or RPM value\r\n", 30, 100);
}
}