Line data Source code
1 : /** 2 : * @file stm32f4_uart.c 3 : * @brief Provides serial communication over UART1 and UART2. 4 : * 5 : * Copyright (c) 2025 Cory McKiel. 6 : * Licensed under the MIT License. See LICENSE file in the project root. 7 : */ 8 : #include "uart.h" 9 : #include "stm32f4_uart1.h" 10 : #include "stm32f4_uart2.h" 11 : 12 0 : int __io_putchar(int ch) 13 : { 14 0 : uint8_t data[1] = { 0 }; 15 0 : size_t bytes_written = 0; 16 : 17 0 : data[0] = ch; 18 0 : hal_uart_write(HAL_UART2, data, sizeof(data), &bytes_written); 19 : 20 0 : return ch; 21 : } 22 : 23 61 : hal_status_t hal_uart_init(hal_uart_t uart) 24 : { 25 61 : hal_status_t hal_status = HAL_STATUS_ERROR; 26 : 27 61 : if (uart == HAL_UART1) 28 : { 29 33 : hal_status = stm32f4_uart1_init(); 30 : } 31 28 : else if (uart == HAL_UART2) 32 : { 33 25 : hal_status = stm32f4_uart2_init(); 34 : } 35 : 36 61 : return hal_status; 37 : } 38 : 39 97 : hal_status_t hal_uart_deinit(hal_uart_t uart) 40 : { 41 97 : hal_status_t hal_status = HAL_STATUS_ERROR; 42 : 43 97 : if (uart == HAL_UART1) 44 : { 45 51 : hal_status = stm32f4_uart1_deinit(); 46 : } 47 46 : else if (uart == HAL_UART2) 48 : { 49 46 : hal_status = stm32f4_uart2_deinit(); 50 : } 51 : 52 97 : return hal_status; 53 : } 54 : 55 32 : hal_status_t hal_uart_read(hal_uart_t uart, uint8_t *data, size_t len, size_t *bytes_read) 56 : { 57 32 : hal_status_t hal_status = HAL_STATUS_ERROR; 58 : 59 32 : if (uart == HAL_UART1) 60 : { 61 16 : hal_status = stm32f4_uart1_read(data, len, bytes_read); 62 : } 63 16 : else if (uart == HAL_UART2) 64 : { 65 13 : hal_status = stm32f4_uart2_read(data, len, bytes_read); 66 : } 67 : 68 32 : return hal_status; 69 : } 70 : 71 37 : hal_status_t hal_uart_write(hal_uart_t uart, const uint8_t *data, size_t len, size_t *bytes_written) 72 : { 73 37 : hal_status_t hal_status = HAL_STATUS_ERROR; 74 : 75 37 : if (uart == HAL_UART1) 76 : { 77 21 : hal_status = stm32f4_uart1_write(data, len, bytes_written); 78 : } 79 16 : else if (uart == HAL_UART2) 80 : { 81 14 : hal_status = stm32f4_uart2_write(data, len, bytes_written); 82 : } 83 : 84 37 : return hal_status; 85 : }