12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include "stm32l0xx_hal.h"
- #include "usart.h"
- #include <stdio.h>
- #include <string.h>
- #ifdef __GNUC__
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif
- UART_HandleTypeDef UartHandle;
- PUTCHAR_PROTOTYPE
- {
- UartHandle.Instance->TDR = ch & 0x01FF;
- //USART2->TDR = ch & 0x01FF;
- while (__HAL_USART_GET_FLAG(&UartHandle, USART_FLAG_TC) == RESET) {}
- //while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {}
- return ch;
- }
- //
- void usart_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- HAL_UART_DeInit(&UartHandle);
-
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_USART2_CLK_ENABLE();
-
- GPIO_InitStruct.Pin = GPIO_PIN_2;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
-
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_3;
- GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
-
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- UartHandle.Instance = USART2;
- UartHandle.Init.BaudRate = 115200;
- UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
- UartHandle.Init.StopBits = UART_STOPBITS_1;
- UartHandle.Init.Parity = UART_PARITY_NONE;
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- UartHandle.Init.Mode = UART_MODE_TX_RX;
-
-
- HAL_UART_Init(&UartHandle);
- }
- //
- void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
- {
- if (huart->Instance == USART2)
- {
- __HAL_RCC_USART2_FORCE_RESET();
- __HAL_RCC_USART2_RELEASE_RESET();
-
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2);
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_3);
- }
- }
|