Motion_EC_Stm32_archived/Core/Src/usart.c

192 lines
4.9 KiB
C
Raw Normal View History

2020-04-06 21:16:18 +08:00
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
2020-04-06 21:16:18 +08:00
******************************************************************************
* @attention
*
2021-01-06 12:23:39 +08:00
* <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
2020-04-06 21:16:18 +08:00
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
#include "log.h"
2020-04-06 21:16:18 +08:00
/* USER CODE END 0 */
UART_HandleTypeDef huart4;
2020-07-15 18:26:22 +08:00
UART_HandleTypeDef huart1;
2020-04-06 21:16:18 +08:00
/* UART4 init function */
void MX_UART4_Init(void)
{
huart4.Instance = UART4;
2021-01-06 12:23:39 +08:00
huart4.Init.BaudRate = 56000;
2020-04-06 21:16:18 +08:00
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
2020-07-15 18:26:22 +08:00
}
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
2021-01-06 12:23:39 +08:00
huart1.Init.BaudRate = 56000;
2020-07-15 18:26:22 +08:00
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;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
2020-04-06 21:16:18 +08:00
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspInit 0 */
/* USER CODE END UART4_MspInit 0 */
/* UART4 clock enable */
__HAL_RCC_UART4_CLK_ENABLE();
2020-12-16 12:02:47 +08:00
2020-04-06 21:16:18 +08:00
__HAL_RCC_GPIOC_CLK_ENABLE();
2020-12-16 12:02:47 +08:00
/**UART4 GPIO Configuration
2020-04-06 21:16:18 +08:00
PC10 ------> UART4_TX
2020-12-16 12:02:47 +08:00
PC11 ------> UART4_RX
2020-04-06 21:16:18 +08:00
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN UART4_MspInit 1 */
/* USER CODE END UART4_MspInit 1 */
}
2020-07-15 18:26:22 +08:00
else if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
2020-12-16 12:02:47 +08:00
__HAL_RCC_GPIOA_CLK_ENABLE();
2020-12-16 12:02:47 +08:00
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
2020-12-16 12:02:47 +08:00
PA10 ------> USART1_RX
2020-07-15 18:26:22 +08:00
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
2020-07-15 18:26:22 +08:00
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
2020-07-15 18:26:22 +08:00
GPIO_InitStruct.Pin = GPIO_PIN_10;
2020-07-15 18:26:22 +08:00
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
2020-07-15 18:26:22 +08:00
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
2020-04-06 21:16:18 +08:00
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspDeInit 0 */
/* USER CODE END UART4_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_UART4_CLK_DISABLE();
2020-12-16 12:02:47 +08:00
/**UART4 GPIO Configuration
2020-04-06 21:16:18 +08:00
PC10 ------> UART4_TX
2020-12-16 12:02:47 +08:00
PC11 ------> UART4_RX
2020-04-06 21:16:18 +08:00
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);
2020-04-10 19:05:38 +08:00
/* UART4 interrupt Deinit */
HAL_NVIC_DisableIRQ(UART4_IRQn);
2020-04-06 21:16:18 +08:00
/* USER CODE BEGIN UART4_MspDeInit 1 */
/* USER CODE END UART4_MspDeInit 1 */
}
2020-07-15 18:26:22 +08:00
else if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
2020-12-16 12:02:47 +08:00
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
2020-12-16 12:02:47 +08:00
PA10 ------> USART1_RX
2020-07-15 18:26:22 +08:00
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
2020-07-15 18:26:22 +08:00
/* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
2020-07-15 18:26:22 +08:00
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
2020-12-16 12:02:47 +08:00
}
2020-04-06 21:16:18 +08:00
/* USER CODE BEGIN 1 */
2021-01-06 12:23:39 +08:00
//printf redirect
#ifdef __GNUC__
//#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define PUTCHAR_PROTOTYPE int putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
2020-04-10 19:05:38 +08:00
2021-01-06 12:23:39 +08:00
PUTCHAR_PROTOTYPE
{
2021-01-07 19:01:08 +08:00
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,10);
2021-01-06 12:23:39 +08:00
return ch;
}
2020-04-06 21:16:18 +08:00
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/