[6] | 1 | /** |
---|
| 2 | ****************************************************************************** |
---|
| 3 | * File Name : USART.c |
---|
| 4 | * Description : This file provides code for the configuration |
---|
| 5 | * of the USART instances. |
---|
| 6 | ****************************************************************************** |
---|
| 7 | * @attention |
---|
| 8 | * |
---|
| 9 | * <h2><center>© Copyright (c) 2020 STMicroelectronics. |
---|
| 10 | * All rights reserved.</center></h2> |
---|
| 11 | * |
---|
| 12 | * This software component is licensed by ST under BSD 3-Clause license, |
---|
| 13 | * the "License"; You may not use this file except in compliance with the |
---|
| 14 | * License. You may obtain a copy of the License at: |
---|
| 15 | * opensource.org/licenses/BSD-3-Clause |
---|
| 16 | * |
---|
| 17 | ****************************************************************************** |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /* Includes ------------------------------------------------------------------*/ |
---|
| 21 | #include "usart.h" |
---|
| 22 | |
---|
| 23 | /* USER CODE BEGIN 0 */ |
---|
| 24 | |
---|
| 25 | /* USER CODE END 0 */ |
---|
| 26 | |
---|
| 27 | UART_HandleTypeDef huart1; |
---|
| 28 | |
---|
| 29 | /* USART1 init function */ |
---|
| 30 | |
---|
| 31 | void MX_USART1_UART_Init(void) |
---|
| 32 | { |
---|
| 33 | |
---|
| 34 | huart1.Instance = USART1; |
---|
| 35 | huart1.Init.BaudRate = 115200; |
---|
| 36 | huart1.Init.WordLength = UART_WORDLENGTH_9B; |
---|
| 37 | huart1.Init.StopBits = UART_STOPBITS_1; |
---|
| 38 | huart1.Init.Parity = UART_PARITY_EVEN; |
---|
| 39 | huart1.Init.Mode = UART_MODE_TX_RX; |
---|
| 40 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
---|
| 41 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
---|
| 42 | huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; |
---|
| 43 | huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; |
---|
| 44 | huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; |
---|
| 45 | if (HAL_RS485Ex_Init(&huart1, UART_DE_POLARITY_HIGH, 0, 0) != HAL_OK) |
---|
| 46 | { |
---|
| 47 | Error_Handler(); |
---|
| 48 | } |
---|
| 49 | if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) |
---|
| 50 | { |
---|
| 51 | Error_Handler(); |
---|
| 52 | } |
---|
| 53 | if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) |
---|
| 54 | { |
---|
| 55 | Error_Handler(); |
---|
| 56 | } |
---|
| 57 | if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) |
---|
| 58 | { |
---|
| 59 | Error_Handler(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) |
---|
| 65 | { |
---|
| 66 | |
---|
| 67 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
---|
| 68 | if(uartHandle->Instance==USART1) |
---|
| 69 | { |
---|
| 70 | /* USER CODE BEGIN USART1_MspInit 0 */ |
---|
| 71 | |
---|
| 72 | /* USER CODE END USART1_MspInit 0 */ |
---|
| 73 | /* USART1 clock enable */ |
---|
| 74 | __HAL_RCC_USART1_CLK_ENABLE(); |
---|
| 75 | |
---|
| 76 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
---|
| 77 | /**USART1 GPIO Configuration |
---|
| 78 | PB3 ------> USART1_DE |
---|
| 79 | PB6 ------> USART1_TX |
---|
| 80 | PB7 ------> USART1_RX |
---|
| 81 | */ |
---|
| 82 | // GPIO_InitStruct.Pin = GPIO_PIN_3; |
---|
| 83 | // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
---|
| 84 | // GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
| 85 | // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
| 86 | // GPIO_InitStruct.Alternate = GPIO_AF4_USART1; |
---|
| 87 | // HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
---|
| 88 | |
---|
| 89 | GPIO_InitStruct.Pin = RS485_R_Pin; |
---|
| 90 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
---|
| 91 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
---|
| 92 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
| 93 | GPIO_InitStruct.Alternate = GPIO_AF0_USART1; |
---|
| 94 | HAL_GPIO_Init(RS485_R_GPIO_Port, &GPIO_InitStruct); |
---|
| 95 | |
---|
| 96 | GPIO_InitStruct.Pin = RS485_D_Pin; |
---|
| 97 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
---|
| 98 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
| 99 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
| 100 | GPIO_InitStruct.Alternate = GPIO_AF0_USART1; |
---|
| 101 | HAL_GPIO_Init(RS485_D_GPIO_Port, &GPIO_InitStruct); |
---|
| 102 | |
---|
| 103 | /* USART1 interrupt Init */ |
---|
| 104 | HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); |
---|
| 105 | HAL_NVIC_EnableIRQ(USART1_IRQn); |
---|
| 106 | /* USER CODE BEGIN USART1_MspInit 1 */ |
---|
| 107 | |
---|
| 108 | /* USER CODE END USART1_MspInit 1 */ |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) |
---|
| 113 | { |
---|
| 114 | |
---|
| 115 | if(uartHandle->Instance==USART1) |
---|
| 116 | { |
---|
| 117 | /* USER CODE BEGIN USART1_MspDeInit 0 */ |
---|
| 118 | |
---|
| 119 | /* USER CODE END USART1_MspDeInit 0 */ |
---|
| 120 | /* Peripheral clock disable */ |
---|
| 121 | __HAL_RCC_USART1_CLK_DISABLE(); |
---|
| 122 | |
---|
| 123 | /**USART1 GPIO Configuration |
---|
| 124 | PB3 ------> USART1_DE |
---|
| 125 | PB6 ------> USART1_TX |
---|
| 126 | PB7 ------> USART1_RX |
---|
| 127 | */ |
---|
| 128 | HAL_GPIO_DeInit(GPIOB, RS485_R_Pin|RS485_D_Pin); |
---|
| 129 | |
---|
| 130 | /* USART1 interrupt Deinit */ |
---|
| 131 | HAL_NVIC_DisableIRQ(USART1_IRQn); |
---|
| 132 | /* USER CODE BEGIN USART1_MspDeInit 1 */ |
---|
| 133 | |
---|
| 134 | /* USER CODE END USART1_MspDeInit 1 */ |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /* USER CODE BEGIN 1 */ |
---|
| 139 | |
---|
| 140 | /* USER CODE END 1 */ |
---|
| 141 | |
---|
| 142 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
---|