-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from RoboMaster-Club/20-serial-debug-uart-dma
add serial debug dma support
- Loading branch information
Showing
6 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
#ifndef BSP_SERIAL | ||
#define BSP_SERIAL | ||
#include <stdio.h> | ||
#include "robot_param.h" | ||
#include "usart.h" | ||
|
||
#ifdef DEBUG | ||
#define DEBUG_PRINTF(huart, format, ...) debug_printf((huart), (format), ##__VA_ARGS__) | ||
#else | ||
#define DEBUG_PRINTF(huart, format, ...) ((void)0) | ||
#endif | ||
|
||
void debug_printf(UART_HandleTypeDef *huart, const char *format, ...); | ||
extern int _write(int file, char *ptr, int len); | ||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,45 @@ | ||
#include "bsp_serial.h" | ||
|
||
#include "usart.h" | ||
#include "stdarg.h" | ||
#include "stdio.h" | ||
#include "string.h" | ||
|
||
#define MAX_STRING_LENGTH 1028 | ||
|
||
#ifdef DEBUG | ||
#define DEBUG_PRINTF(huart, format, ...) debug_printf((huart), (format), ##__VA_ARGS__) | ||
#else | ||
#define DEBUG_PRINTF(huart, format, ...) ((void)0) | ||
#endif | ||
char buffer[MAX_STRING_LENGTH]; | ||
|
||
/** | ||
* @brief Non-blocking printf function using DMA | ||
*/ | ||
void debug_printf(UART_HandleTypeDef *huart, const char *format, ...) { | ||
va_list args; | ||
va_start(args, format); | ||
|
||
// Use vsnprintf instead of vsprintf to avoid buffer overflow | ||
vsnprintf(buffer, MAX_STRING_LENGTH, format, args); | ||
va_end(args); | ||
|
||
// Send the string over UART using DMA | ||
HAL_UART_Transmit_DMA(huart, (uint8_t*)buffer, strlen(buffer)); | ||
while (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) == RESET) { | ||
// Optionally, insert a timeout mechanism here to break from the loop if needed | ||
} | ||
} | ||
|
||
/** | ||
* Obsolete function required for printf to work | ||
* this function calls HAL_UART_Transmit, which will block the program | ||
* until all data is sent. | ||
* @ref debug_printf for a non-blocking version using DMA | ||
*/ | ||
int _write(int file, char *ptr, int len) { | ||
HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, HAL_MAX_DELAY); | ||
HAL_UART_Transmit(&huart6, (uint8_t *)ptr, len, HAL_MAX_DELAY); | ||
//while(!huart6.Instance->SR & UART_FLAG_IDLE); | ||
return len; | ||
} |