成功!我是用的stm32f411re和mbed, 步骤如下:
1. serial_api.c中init_uart:
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_RTS_CTS;
2. 初始化UART1的CTS/RTS引脚:
PinMap rts = {PA_12, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)};
PinMap cts = {PA_11, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)};
pin_function(rts.pin, rts.function);
pin_function(cts.pin, cts.function);
pin_mode(rts.pin, PullNone); //output, uart request peer to send
pin_mode(cts.pin, PullUp); // input, peer send 0 to allow uart send
以上代码写好后, 只有把PA_11也就是CTS拉低才能往uart写数据。下面是测试代码:
#include "mbed_stm_demo.h"
#include "BreathLed/BreathLed.h"
#include "PeripheralPins.h"
using namespace steeven;
BreathLed led1(LED1);
Serial pc(USBTX, USBRX);
Serial uart1(PA_9, PA_10);
int main() {
char ch;
int i = 0;
int j = 0;
led1.loop(0.5, 0.2);
pc.baud(115200);
PinMap rts = {PA_12, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)};
PinMap cts = {PA_11, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)};
pin_function(rts.pin, rts.function);
pin_function(cts.pin, cts.function);
pin_mode(rts.pin, PullNone); //output, uart request peer to send
pin_mode(cts.pin, PullUp); // input, peer send 0 to allow uart send
uart1.baud(115200);
pc.printf("hello world!\n");
while (1) {
if (pc.readable()) {
i++;
ch = pc.getc();
if (uart1.writeable()) {
uart1.putc(ch);
} else {
pc.printf("%d/%d ", i,j); //if
}
if (i%50 == 49){
pc.printf("\r\n%d lost: %d \r\n", i,i-j);
}
}
if (uart1.readable()) {
ch = uart1.getc();
j++;
}
}
}
用串口调试工具不停地定时发送数据,可以看到:
如果cts悬空, 不停地打印 x/y, 表示数据丢失
如果cts接地,只有满50才打印一次,没丢 |