AnsweredAssumed Answered

multiple UARTs Interrupt Usage

Question asked by Ehsan Behravan on Jun 7, 2017
Latest reply on Jun 18, 2017 by Ehsan Behravan

Hi

I'm using STM32F745VG custom board. Two UARTs are used with interrupt but just one of them is working. 

UART8 : 38400 buad rate. working

UART7 : 9600 buad rate. not working!!!

i'm implement interrupt with Callback function and when i omit UART8 from callback function, UART7 is working fine.

it seems callback function can not handle two uarts.

my code is:

 

 

UART_HandleTypeDef huart7;
UART_HandleTypeDef huart8;


uint8_t dataRDY=0;
uint8_t Start=0;
uint8_t startMessage[5]="start";
uint8_t data_rx[5];
uint8_t data_rx_debug[5];
uint8_t data_gps[100];
uint8_t welcome[2]="OK";
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_UART7_Init(void);
static void MX_UART8_Init(void);
static void MX_NVIC_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

if (huart->Instance==UART8) {

if (data_rx[0]==startMessage[0]
&&data_rx[1]==startMessage[1]
&&data_rx[2]==startMessage[2]
&&data_rx[3]==startMessage[3]
&&data_rx[4]==startMessage[4])
{
Start=1;
}
data_rx_debug[0]=data_rx[0];
data_rx_debug[1]=data_rx[1];
data_rx_debug[2]=data_rx[2];
data_rx_debug[3]=data_rx[3];
data_rx_debug[4]=data_rx[4];

HAL_UART_Receive_IT(&huart8,data_rx,5);
}

else if (huart->Instance==UART7) {

dataRDY=1;
HAL_UART_Receive_IT(&huart7,data_gps,100);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_9,GPIO_PIN_SET);

}

}
/* USER CODE END 0 */

int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* Enable I-Cache-------------------------------------------------------------*/
SCB_EnableICache();

/* Enable D-Cache-------------------------------------------------------------*/
SCB_EnableDCache();

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

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

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

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_UART7_Init();
MX_UART8_Init();

/* Initialize interrupts */
MX_NVIC_Init();

/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart8,data_rx,5);
while(Start!=1)
{
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_8);
HAL_Delay(300);

}
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_8,GPIO_PIN_SET);
HAL_UART_Transmit_IT(&huart8,welcome,2);
HAL_UART_Receive_IT(&huart7,data_gps,100);

/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if (dataRDY==1){
dataRDY=0;
HAL_UART_Transmit_IT(&huart8,data_gps,100);
HAL_Delay(50);
//HAL_UART_Receive_IT(&huart7,data_gps,5);

}
/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

/**Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_UART7|RCC_PERIPHCLK_UART8;
PeriphClkInitStruct.Uart7ClockSelection = RCC_UART7CLKSOURCE_PCLK1;
PeriphClkInitStruct.Uart8ClockSelection = RCC_UART8CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}

/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/** NVIC Configuration
*/
static void MX_NVIC_Init(void)
{
/* UART7_IRQn interrupt configuration */
HAL_NVIC_SetPriority(UART7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART7_IRQn);
/* UART8_IRQn interrupt configuration */
HAL_NVIC_SetPriority(UART8_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART8_IRQn);
}

/* UART7 init function */
static void MX_UART7_Init(void)
{

huart7.Instance = UART7;
huart7.Init.BaudRate = 9600;
huart7.Init.WordLength = UART_WORDLENGTH_8B;
huart7.Init.StopBits = UART_STOPBITS_1;
huart7.Init.Parity = UART_PARITY_NONE;
huart7.Init.Mode = UART_MODE_TX_RX;
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
huart7.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart7.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart7) != HAL_OK)
{
Error_Handler();
}

}

/* UART8 init function */
static void MX_UART8_Init(void)
{

huart8.Instance = UART8;
huart8.Init.BaudRate = 38400;
huart8.Init.WordLength = UART_WORDLENGTH_8B;
huart8.Init.StopBits = UART_STOPBITS_1;
huart8.Init.Parity = UART_PARITY_NONE;
huart8.Init.Mode = UART_MODE_TX_RX;
huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart8.Init.OverSampling = UART_OVERSAMPLING_16;
huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart8) != HAL_OK)
{
Error_Handler();
}

}

/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{

GPIO_InitTypeDef GPIO_InitStruct;

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_RESET);

/*Configure GPIO pins : PD8 PD9 PD10 PD11 */
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler */
}

#ifdef USE_FULL_ASSERT

/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */

}

#endif

/**
* @}
*/

/**
* @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

 

 

Start variable is working fine. it means Start is equal 1 but UART7 not working.

Thanks for your reply.

Outcomes