/*====================================================================================================*/ void Serial_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef UART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; /* UART Clk ******************************************************************/ UARTx_CLK_ENABLE(); /* UART AF *******************************************************************/ GPIO_PinAFConfig(UARTx_TX_GPIO_PORT, UARTx_TX_SOURCE, UARTx_TX_AF); GPIO_PinAFConfig(UARTx_RX_GPIO_PORT, UARTx_RX_SOURCE, UARTx_RX_AF); /* UART Pin ******************************************************************/ GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin = UARTx_TX_PIN; GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = UARTx_RX_PIN; GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART IT *******************************************************************/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStruct.NVIC_IRQChannel = UARTx_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x000F; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); /* UART Init *****************************************************************/ UART_InitStruct.USART_BaudRate = UARTx_BAUDRATE; UART_InitStruct.USART_WordLength = UARTx_BYTESIZE; UART_InitStruct.USART_StopBits = UARTx_STOPBITS; UART_InitStruct.USART_Parity = UARTx_PARITY; UART_InitStruct.USART_HardwareFlowControl = UARTx_HARDWARECTRL; UART_InitStruct.USART_Mode = UARTx_MODE; USART_Init(UARTx, &UART_InitStruct); /* UART Enable ***************************************************************/ USART_ITConfig(UARTx, USART_IT_RXNE, ENABLE); USART_Cmd(UARTx, ENABLE); USART_ClearFlag(UARTx, USART_FLAG_TC); }
/*====================================================================================================*/ void Serial_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; /* UART Clk ******************************************************************/ UARTx_TX_GPIO_CLK_ENABLE(); UARTx_RX_GPIO_CLK_ENABLE(); UARTx_CLK_ENABLE(); /* UART Pin ******************************************************************/ GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = GPIO_PIN_15; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_7; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = UARTx_TX_PIN; GPIO_InitStruct.Alternate = UARTx_TX_AF; HAL_GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = UARTx_RX_PIN; GPIO_InitStruct.Alternate = UARTx_RX_AF; HAL_GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART Init *****************************************************************/ Serial_HandleStruct.Instance = UARTx; Serial_HandleStruct.Init.BaudRate = UARTx_BAUDRATE; Serial_HandleStruct.Init.WordLength = UARTx_BYTESIZE; Serial_HandleStruct.Init.StopBits = UARTx_STOPBITS; Serial_HandleStruct.Init.Parity = UARTx_PARITY; Serial_HandleStruct.Init.HwFlowCtl = UARTx_HARDWARECTRL; Serial_HandleStruct.Init.Mode = UARTx_MODE; Serial_HandleStruct.Init.OverSampling = UARTx_OVERSAMPLE; HAL_UART_Init(&Serial_HandleStruct); /* UART Enable ***************************************************************/ __HAL_UART_ENABLE(&Serial_HandleStruct); __HAL_UART_CLEAR_FLAG(&Serial_HandleStruct, UART_FLAG_TC); }
/*====================================================================================================*/ void Serial_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; /* UART Clk ******************************************************************/ UARTx_TX_GPIO_CLK_ENABLE(); UARTx_RX_GPIO_CLK_ENABLE(); UARTx_CLK_ENABLE(); /* UART Pin ******************************************************************/ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = UARTx_TX_PIN; HAL_GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = UARTx_RX_PIN; HAL_GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART IT *******************************************************************/ // HAL_NVIC_SetPriority(UARTx_IRQn, 0, 1); // HAL_NVIC_EnableIRQ(UARTx_IRQn); /* UART Init *****************************************************************/ Serial_InitStruct.Instance = UARTx; Serial_InitStruct.Init.BaudRate = UARTx_BAUDRATE; Serial_InitStruct.Init.WordLength = UARTx_BYTESIZE; Serial_InitStruct.Init.StopBits = UARTx_STOPBITS; Serial_InitStruct.Init.Parity = UARTx_PARITY; Serial_InitStruct.Init.HwFlowCtl = UARTx_HARDWARECTRL; Serial_InitStruct.Init.Mode = UARTx_MODE; HAL_UART_Init(&Serial_InitStruct); /* UART Enable ***************************************************************/ // __HAL_UART_ENABLE_IT(&Serial_InitStruct, UART_IT_RXNE); __HAL_UART_ENABLE(&Serial_InitStruct); __HAL_UART_CLEAR_FLAG(&Serial_InitStruct, UART_FLAG_TC); }
/*====================================================================================================*/ void RS232_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; /* UARTx Clk */ UARTx_TX_GPIO_CLK_ENABLE(); UARTx_RX_GPIO_CLK_ENABLE(); UARTx_CLK_ENABLE(); /* UARTx Pin */ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = UARTx_TX_PIN; GPIO_InitStruct.Alternate = UARTx_TX_AF; HAL_GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = UARTx_RX_PIN; GPIO_InitStruct.Alternate = UARTx_RX_AF; HAL_GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART Init *****************************************************************/ UART_HandleStruct.Instance = UARTx; UART_HandleStruct.Init.BaudRate = UARTx_BAUDRATE; UART_HandleStruct.Init.WordLength = UARTx_BYTESIZE; UART_HandleStruct.Init.StopBits = UARTx_STOPBITS; UART_HandleStruct.Init.Parity = UARTx_PARITY; UART_HandleStruct.Init.HwFlowCtl = UARTx_HARDWARECTRL; UART_HandleStruct.Init.Mode = UARTx_MODE; // UART_InitStruct.OverSampling = UARTx_OVERSAMPLE; HAL_UART_Init(&UART_HandleStruct); __HAL_UART_ENABLE(&UART_HandleStruct); __HAL_UART_CLEAR_FLAG(&UART_HandleStruct, UART_FLAG_TC); }