cancel
Showing results for 
Search instead for 
Did you mean: 

UART receive, timertick, reset

soundarya_h_s
Associate III
#include "main.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "ctype.h"
#include "stdio.h"
#include "speed_torq_ctrl.h"
#include "ramp_ext_mngr.h"

#define USART_IT_ENABLE 1

uint8_t rxbuffer[10] = {0};
uint16_t motor1Faults;
volatile uint8_t flag = 0;
volatile uint8_t rcflag = 0;
volatile uint8_t fault_flag = 0;
volatile uint8_t startflag = 0;
volatile uint8_t stopflag = 0;
volatile uint8_t txflag = 0;
char TempBuffer[40] = "Invalid command or RPM value\r\n";
uint8_t rxindex = 0;
volatile uint8_t rpmflag = 0;
volatile uint8_t rpm = 0;
uint32_t rpmValue = 0;
char buffer[50] = {0};
uint32_t rampStartTime = 0;

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;

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* 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();
  MX_TIM3_Init();

  /* Initialize interrupts */
  
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim2);
 #if (USART_IT_ENABLE)
   LL_USART_DisableDMAReq_RX (USART2);
   HAL_UART_Receive_IT(&huart2,(uint8_t*)rxbuffer, 1);
 #else
   HAL_UART_Receive_DMA(&huart2,(uint8_t*)rxbuffer,1);
 #endif
  /* USER CODE END 2 */


   MC_ProgramSpeedRampMotor1(500,20000);
  
   //MC_StartMotor1();
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	if(fault_flag == 1)
	{
	   handlefaults();
	   fault_flag = 0;
	}
	if(flag == 1)
	{
	  MC_StartMotor1();
	}
	if(flag == 2)
	{
	  MC_StopMotor1();
	}

	if(rpm == 2)
	{
	  HAL_UART_Transmit(&huart2, (uint8_t*)TempBuffer, strlen(TempBuffer), 100);
	}
        if(rcflag == 1 && (HAL_GetTick() - rampStartTime >= 30000))
	{
		HAL_UART_Transmit(&huart2, (uint8_t*)"Motor is running at RPM ", 23, 100);
		sprintf(buffer," %d",rpmValue);
		HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), 100);
	}
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
   //if (htim->Instance == TIM2)
	//{
		fault_flag = 1;
	//}
	//if (htim->Instance == TIM3)
	//{
//		rcflag = 1;
//	    handleflags();
//		__HAL_TIM_SET_COUNTER(&htim3, 0);
//		HAL_TIM_Base_Stop_IT(&htim3);
//	}
}

void handlefaults()
{
	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);
	}
	fault_flag = 0;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	int cmd_ok = 0;
	if(rxbuffer[rxindex] == '#')
	{
		if(rxindex > 0 && rxbuffer[rxindex - 1] == '#')
		{
			if(strstr((const char*)rxbuffer, "start##") != NULL)
			{
				flag = 1;
				startflag = 1;
			}
			else if(strstr((const char*)rxbuffer, "stop##") != NULL)
			{
				flag = 2;
				stopflag = 1;
			}
			else
			{
				rxbuffer[rxindex] = '\0';
				rxbuffer[rxindex - 1] = '\0';
				rpmValue = atoi((char*)rxbuffer);
				if (rpmValue >= 100 && rpmValue <= 3000)
				{
					MC_ProgramSpeedRampMotor1((uint32_t)rpmValue,30000);
                                        rampStartTime = HAL_GetTick(); // Record ramp start time
					rpmflag = 1;
                                        
				}
				else
				{
					rpm = 2;
				}
			}
			cmd_ok = 1;
		}
	}
	if(rxindex > 9)
	{
		cmd_ok = 1;
		txflag = 1;
	}
	if(cmd_ok == 1)
		{
			memset(rxbuffer, 0, sizeof(rxbuffer));
			cmd_ok = 0;
			rxindex = 0;
		}
		else
		{
			rxindex++;
		}
	handleflags();
	HAL_UART_Receive_IT(&huart2, ((uint8_t *)rxbuffer) + strlen(rxbuffer), 1);

}

void handleflags(void)
{
	if(startflag == 1)
	{
		HAL_UART_Transmit(&huart2, (uint8_t*)"Motor Started\n\r", 13, 100);
		startflag = 0;
	}
	if(stopflag == 1)
	{
		HAL_UART_Transmit(&huart2, (uint8_t*)"Motor Stopped\n\r", 13, 100);
		stopflag = 0;
	}
	if(rpmflag == 1 )
	{
		HAL_UART_Transmit(&huart2, (uint8_t*)"Ramp execution started\r\n", 22, 100);
		rpmflag = 0;
		rcflag = 1;
	}
}
static void MX_TIM2_Init(void)
{

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 1699;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 49999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */

  /* USER CODE END TIM2_Init 2 */

}
static void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 16999;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 29999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */

}

soundarya_h_s_0-1710833127974.pngsoundarya_h_s_1-1710833176955.png

Issue 1: after completion of first ramp execution i am getting message from UART "Motor is running at RPM 650" . Thereafter other commands are not working

Issue 2: I have set the timer interrupt for 5s to check error conditions, i should receive error messages once every 5s
but that is not happening here
clock frequency is 170MHz

0 REPLIES 0