cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use multiple uarts with put char / get char using prtinf?

Study Yahoo
Associate III
Posted on November 27, 2017 at 02:15

Good morning

I want to use uart in multiple ways as shown below.

I want to set prtinf tx and rx it with getchar.

How do I use multiple uarts with put char / get char?

void UART_Init(void)

{

huart1.Instance = USART1;

huart1.Init.BaudRate = 9600;

huart1.Init.WordLength = UART_WORDLENGTH_8B;

huart1.Init.StopBits = UART_STOPBITS_1;

huart1.Init.Parity = UART_PARITY_NONE;

huart1.Init.Mode = UART_MODE_TX_RX;

huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart1.Init.OverSampling = UART_OVERSAMPLING_16;

huart2.Instance = USART2;

huart2.Init.BaudRate = 9600;

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;

huart3.Instance = USART3;

huart3.Init.BaudRate = 9600;

huart3.Init.WordLength = UART_WORDLENGTH_8B;

huart3.Init.StopBits = UART_STOPBITS_1;

huart3.Init.Parity = UART_PARITY_NONE;

huart3.Init.Mode = UART_MODE_TX_RX;

huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart3.Init.OverSampling = UART_OVERSAMPLING_16;

huart6.Instance = USART6;

huart6.Init.BaudRate = 9600;

huart6.Init.WordLength = UART_WORDLENGTH_8B;

huart6.Init.StopBits = UART_STOPBITS_1;

huart6.Init.Parity = UART_PARITY_NONE;

huart6.Init.Mode = UART_MODE_TX_RX;

huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart6.Init.OverSampling = UART_OVERSAMPLING_16;

}

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(huart->Instance==USART1)

{

/* USER CODE BEGIN USART1_MspInit 0 */

/* USER CODE END USART1_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART1_CLK_ENABLE();

/**USART1 GPIO Configuration

PA9 ------> USART1_TX

PA10 ------> USART1_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USART1 interrupt Init */

HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART1_IRQn);

/* USER CODE BEGIN USART1_MspInit 1 */

/* USER CODE END USART1_MspInit 1 */

}

else if(huart->Instance==USART2)

{

/* USER CODE BEGIN USART2_MspInit 0 */

/* USER CODE END USART2_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART2_CLK_ENABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USART2 interrupt Init */

HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART2_IRQn);

/* USER CODE BEGIN USART2_MspInit 1 */

/* USER CODE END USART2_MspInit 1 */

}

else if(huart->Instance==USART3)

{

/* USER CODE BEGIN USART3_MspInit 0 */

/* USER CODE END USART3_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART3_CLK_ENABLE();

/**USART3 GPIO Configuration

PB10 ------> USART3_TX

PB11 ------> USART3_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USART3 interrupt Init */

HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART3_IRQn);

/* USER CODE BEGIN USART3_MspInit 1 */

/* USER CODE END USART3_MspInit 1 */

}

else if(huart->Instance==USART6)

{

/* USER CODE BEGIN USART6_MspInit 0 */

/* USER CODE END USART6_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART6_CLK_ENABLE();

/**USART6 GPIO Configuration

PC6 ------> USART6_TX

PC7 ------> USART6_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF8_USART6;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/* USART6 interrupt Init */

HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART6_IRQn);

/* USER CODE BEGIN USART6_MspInit 1 */

/* USER CODE END USART6_MspInit 1 */

}

}

6 REPLIES 6
Posted on November 27, 2017 at 02:25

Use sprintf() to a buffer, or use fprintf() and manage the FILE handle at the fputc() level in Keil

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 27, 2017 at 02:42

I do not understand well.

Could you show me the sample code?

Posted on November 27, 2017 at 02:59

/* Implementation of putchar (also used by printf function to output data)    */

void SendChar(USART_TypeDef* USARTx, int ch)                    /* Write character to Serial Port     */

{

  while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); // Wait for Empty

  USART_SendData(USARTx, ch); // Send Char

}

int fputc(int ch, FILE *f)

{

  if (f == (FILE *)USART1)

    SendChar(USART1, ch);

  else if (f == (FILE *)USART2)

    SendChar(USART2, ch);

  else

    ITM_SendChar(ch);

  return(ch);

}

fprintf((void *)USART1, 'Output USART1\n');

fprintf((void *)USART2, 'Output USART2\n');
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 27, 2017 at 03:07

Thank you for your reply.

Excuse me, can I please sample rx?

Posted on November 27, 2017 at 04:26

Think about it a little, and consider how you can do similar things with fgetc()

Creativity is not about cut-n-pasting other peoples code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 27, 2017 at 06:00

Thank you for answer.

I've received a lot of help from your information.

Due to the short project deadline, I have asked many questions.

Have a nice day!