我正在为 STM32 微控制器开发一个功能,该功能通过 uart 端口发送给定长度的字符串。为了处理 uart 通信,我创建了一个 Serial 类,该类具有一个在中断处理程序中弹出和传输的传输和接收缓冲区。我目前正在处理的函数实际上是我之前编写的有效函数的重载。下面是工作函数:
void Serial::sendString(char* str) {
// Writes a string to txBuffer. If Transmit interrupts are enabled, and
// the Data register is empty, the txBuffer will be popped into the DR to
// prime the interrupts.
__HAL_UART_DISABLE_IT(uart, UART_IT_TXE); // Keeps our spaghetti straightened out...
while (*str != '\0') { // While char is not a null terminator...
txBuffer->push(*str); // Push first char into queue as we know it is valid
str++; // Pointer goes to next char in string
}
uint32_t isrflags = READ_REG(uart->Instance->SR); // Reads the flags and control register
//uint32_t cr1its = READ_REG(uart->Instance->CR1); // Into variables
// If the DR is empty and Transmission interrupts are disabled...
if ((isrflags & USART_SR_TXE) != RESET) {
uart->Instance->DR = txBuffer->pop(); // Reenable interrupts and prime the DR
}
__HAL_UART_ENABLE_IT(uart, UART_IT_TXE); // Alright, time to cook the pasta
}
重载是我遇到问题的功能。出于某种原因,调试器显示变量“i”以值“14”初始化,并且在使用调试器单步执行时不会递增。事实上,调试器根本不允许我进入 for 循环。这是重载:
void Serial::sendString(char* str, unsigned int len) {
// Writes a string to txBuffer. If Transmit interrupts are enabled, and
// the Data register is empty, the txBuffer will be popped into the DR to
// prime the interrupts.
// Rather than being terminated by a null character, this method instead
// sends each char in an array of a specified length. Note that this overload
// MUST be used in any situation that a null terminator might appear in a char
// array!
__HAL_UART_DISABLE_IT(uart, UART_IT_TXE); // Keeps our spaghetti straightened out...
for (unsigned int i = 0; i < len; i++) { // While char is not a null terminator...
txBuffer->push(str[i]); // Push first char into queue as we know it is valid
//str++; // Pointer goes to next char in string
}
uint32_t isrflags = READ_REG(uart->Instance->SR); // Reads the flags and control register
// uint32_t cr1its = READ_REG(uart->Instance->CR1); // Into variables
// If the DR is empty...
if ((isrflags & USART_SR_TXE) != RESET) {
uart->Instance->DR = txBuffer->pop();
}
__HAL_UART_ENABLE_IT(uart, UART_IT_TXE); // Alright, time to cook the pasta
}
这些函数在 main 中的终端 while 循环中调用。调试时,问题立即发生;我根本无法承受超载。我的代码似乎只是在这个位置停止了。
我之前已经能够成功运行过载。仅当我试图解决函数中的另一个错误时,该错误才出现,即字符串中的第一个字符仅在一半时间内被传输。我设置了一个断点并开始调试,现在它根本不起作用......