gpt4 book ai didi

c - 较高的波特率在 STM32F0Discovery 板上不起作用

转载 作者:行者123 更新时间:2023-11-30 19:34:13 24 4
gpt4 key购买 nike

我在使用 USART 时使用 STM32 标准外设库时遇到奇怪的错误。我需要能够在 USART2 上以 4M 波特率传输。虽然该板声称支持高达 6M 波特率,但只有 3M 及以下波特率才能正常工作。

我查看了该板的 SPL 文档和引用手册,发现我的时钟默认为 48MHz,过采样设置为 8,通过调试器中的单步调试代码确认了这一点。为什么3M及以下可以进行标准初始化,而4M、5M、6M则不行?

static void hal_init(void)
{
GPIO_InitTypeDef port_init;
USART_InitTypeDef usart_init;

//GPIO init: USART2 PA2 as OUT, PA3 as IN
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );

/* Connect PA2 to USART2 tx, PA3 to rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);

port_init.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
port_init.GPIO_Mode = GPIO_Mode_AF;
port_init.GPIO_Speed = GPIO_Speed_50MHz;
port_init.GPIO_OType = GPIO_OType_PP;
port_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init( GPIOA, &port_init );

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/* Need oversampling set for higher baud rates */
USART_OverSampling8Cmd(USART2, ENABLE);

//USART init: USART2 500K 8n1
usart_init.USART_BaudRate = USART_BAUD_RATE; // Currently 4M
usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart_init.USART_Parity = USART_Parity_No;
usart_init.USART_StopBits = USART_StopBits_1;
usart_init.USART_WordLength = USART_WordLength_8b;
usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, (USART_InitTypeDef*) &usart_init);

USART_Cmd(USART2, ENABLE);
}

作为引用,该板也使用 FreeRTOS,我正在使用 IAR-EWARM 进行构建和调试。

编辑:通过告诉板连续传输 0xA5(随机选择),然后将主机设置为接收 10 个字节来测试错误行为。在所有 3M 或以下波特率下,如预期接收到一系列值为 0xA5 的字节。对于 4M,如果过采样设置为 16 (OVER8=0),则接收到值 0x00。如果过采样设置为 8 (OVER8=1),则我的 Python 脚本或具有正确设置的 PuTTy 根本不会接收到任何值(因为接收到的波特率较低)。

最佳答案

接收数据时忘记 SPL、HAL 或其他库。对于这样的速度你应该使用DMA。如果是中断例程,请记住您只有大约 160-180 个时钟周期。这很容易实现,但您需要使用裸寄存器编程。

关于c - 较高的波特率在 STM32F0Discovery 板上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com