-
-
Save Voha888/eaa3f7c8c0c0d056417f807a71dc96ed to your computer and use it in GitHub Desktop.
Вывод отладочных сообщений через USART STM32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Вывод отладочных сообщений через USART STM32. | |
| * Работает как минимум на STM32F0, STM32F1, STM32F4, STM32F7. | |
| * Работает как функция printf() стандартной библиотеки Си. */ | |
| #include <stdio.h> | |
| #include <stdarg.h> | |
| void debug(const char * format, ...); | |
| void debug(const char * format, ...) | |
| { | |
| char str[200]; | |
| int i; | |
| int str_len; | |
| va_list args; | |
| va_start(args, format); | |
| str_len = vsnprintf(str, 200, format, args); | |
| va_end(args); | |
| for(i = 0; i < str_len; i++) | |
| { | |
| USART_SendData(USART1, (uint8_t)(str[i])); | |
| while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); | |
| } | |
| } | |
| /* Требуется библиотека STM32Cube. | |
| * Имеется ограничение в 1024 байта на конечную строку. */ | |
| void print(const char * format, ...) | |
| { | |
| const size_t maxStringLength = 1024; | |
| static char str[maxStringLength]; | |
| va_list args; | |
| va_start(args, format); | |
| const int stringLength = vsnprintf(str, maxStringLength, format, args); | |
| va_end(args); | |
| HAL_UART_Transmit(&huart, (uint8_t *)(str), stringLength, 1000); | |
| } | |
| /* Требуется библиотека STM32Cube. | |
| * Конечная строка ограничена размером кучи. */ | |
| void print(const char * format, ...) | |
| { | |
| char * strp; | |
| va_list args; | |
| va_start(args, format); | |
| const int stringLength = vasprintf(&strp, format, args); | |
| va_end(args); | |
| if (stringLength < 0) { | |
| return; | |
| } | |
| HAL_UART_Transmit(&huart, (uint8_t *)(strp), stringLength, 1000); | |
| free(strp); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment