gpt4 book ai didi

arm - STM32H7xx 尽可能快地切换 IO

转载 作者:行者123 更新时间:2023-12-04 13:59:48 25 4
gpt4 key购买 nike

我正在尝试尽快切换 STM32H743 上的 IO。我正在使用外部 10MHz 时钟,供电电压为 3.3V,我确信我的主时钟运行在 400MHz,与 GPIO (AHB4) 通信的总线时钟运行在 200MHz。这是我用来配置芯片和切换 IO 的一些示例代码:

RCC_ClkInitTypeDef clock;
RCC_OscInitTypeDef osc;

MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
// External 10.000MHz oscillator
osc.OscillatorType = RCC_OSCILLATORTYPE_HSE;
osc.HSEState = RCC_HSE_ON;
osc.HSIState = RCC_HSI_OFF;
osc.CSIState = RCC_CSI_OFF;
osc.PLL.PLLState = RCC_PLL_ON;
osc.PLL.PLLSource = RCC_PLLSOURCE_HSE;
// ((10 / 5) * 400 ) / 2 = 400
osc.PLL.PLLM = 5;
osc.PLL.PLLN = 400;
osc.PLL.PLLP = 2;
osc.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
osc.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
HAL_RCC_OscConfig(&osc);

// Sysclk source is PLL
clock.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);

clock.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clock.SYSCLKDivider = RCC_SYSCLK_DIV1;
// HCLK = SYSCLK / 2 = 200
clock.AHBCLKDivider = RCC_HCLK_DIV2;
clock.APB3CLKDivider = RCC_APB3_DIV2;
clock.APB1CLKDivider = RCC_APB1_DIV2;
clock.APB2CLKDivider = RCC_APB2_DIV2;
clock.APB4CLKDivider = RCC_APB4_DIV2;
HAL_RCC_ClockConfig(&clock, FLASH_LATENCY_4);
__HAL_RCC_CSI_ENABLE();
__HAL_RCC_SYSCFG_CLK_ENABLE();
HAL_EnableCompensationCell();

// GPIOC0 is pin I'm toggling
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef gpio;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOC, &gpio);

// Configure GPIOC9 as SYSCLK/2 (200MHz)
gpio.Mode = GPIO_MODE_AF_PP;
gpio.Alternate = GPIO_AF0_MCO;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOC, &gpio);
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2);

while (1)
{
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
GPIOC->ODR = 0;
GPIOC->ODR = 1;
}

当我在示波器上查看时,我在 200MHz 下正确地看到了我的 SYSCLK/2,但切换引脚每 60nS 只切换一次。

scope

当我查看此代码的反汇编(编译为“-O3”)时,切换被编译为单个 STR 指令(r1 = 0、r2 = 1、r3 = &GPIOC->ODR):
...
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
str r2, [r3, #20]
str r1, [r3, #20]
...

我找不到 Cortex-M7 处理器的周期信息,但是当我查看 Cortex-M4 处理器的周期时间 ( http://infocenter.arm.com/help/topic/com.arm.doc.ddi0439b/DDI0439B_cortex_m4_r0p0_trm.pdf) 表 3-1 时,我看到 STR 应该需要两个时钟周期才能执行。我希望看到我的 IO 每 10 纳秒(或 200MHz AHB4 总线上的每两个时钟周期)aproimately 切换一次。

我试过从 FLASH 和 SRAM 运行代码,但 IO 速度没有区别。

为什么我的 IO 不是每两个时钟周期切换一次?

编辑:

在引用手册 (st.com/resource/en/reference_manual/dm00314099.pdf) 的 10.2 节中,它说:“能够每两个时钟周期改变一次的快速切换。”

编辑:

所以一些评论提到处理器并不是真的要直接与 GPIO 对话。所以我重写了代码以使用 DMA(我必须使用 BDMA 并专门分配 RAM_D3 中的数据以使其运行速度与软件循环一样快)。最终,我想将计算出的数据从 RAM 输出到整个 GPIO 端口。如何按照引用手册的建议每两个时钟周期输出一次数据?

在 NUCLEO-H743ZI 上运行的 DMA 代码:
#include <stm32h7xx_hal.h>
#include <stdint.h>

DMA_HandleTypeDef dma;

void SysTick_Handler(void)
{
HAL_IncTick();
}

static void ClockConfig(void)
{
RCC_ClkInitTypeDef clock;
RCC_OscInitTypeDef osc;
MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

osc.OscillatorType = RCC_OSCILLATORTYPE_CSI;
osc.CSIState = RCC_CSI_ON;
osc.CSICalibrationValue = 16;
osc.PLL.PLLState = RCC_PLL_ON;
osc.PLL.PLLSource = RCC_PLLSOURCE_CSI;
osc.PLL.PLLM = 1;
osc.PLL.PLLN = 200;
osc.PLL.PLLP = 2;
osc.PLL.PLLQ = 2;
osc.PLL.PLLR = 2;
osc.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
osc.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
osc.PLL.PLLFRACN = 0;
HAL_RCC_OscConfig(&osc);

clock.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);
clock.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clock.SYSCLKDivider = RCC_SYSCLK_DIV1;
clock.AHBCLKDivider = RCC_HCLK_DIV2;
clock.APB3CLKDivider = RCC_APB3_DIV2;
clock.APB1CLKDivider = RCC_APB1_DIV2;
clock.APB2CLKDivider = RCC_APB2_DIV2;
clock.APB4CLKDivider = RCC_APB4_DIV2;
HAL_RCC_ClockConfig(&clock, FLASH_LATENCY_4);
__HAL_RCC_CSI_ENABLE();
__HAL_RCC_SYSCFG_CLK_ENABLE();
HAL_EnableCompensationCell();

HAL_SYSTICK_Config(SystemCoreClock / 1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

void GpioConfig(void)
{
__GPIOC_CLK_ENABLE();
GPIO_InitTypeDef gpio;
gpio.Pin = GPIO_PIN_0;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &gpio);
gpio.Pin = GPIO_PIN_1;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &gpio);
gpio.Mode = GPIO_MODE_AF_PP;
gpio.Alternate = GPIO_AF0_MCO;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FAST;
gpio.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOC, &gpio);
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2);
}

uint8_t complete = 0;
void DmaComplete(DMA_HandleTypeDef *handle)
{
complete = 1;
}

void DmaError(DMA_HandleTypeDef *handle)
{
complete = 1;
}

void DmaConfig(void)
{
__HAL_RCC_BDMA_CLK_ENABLE();
dma.Instance = BDMA_Channel0;
dma.Init.Request = DMA_REQUEST_MEM2MEM;
dma.Init.Direction = DMA_MEMORY_TO_MEMORY;
dma.Init.PeriphInc = DMA_PINC_ENABLE;
dma.Init.MemInc = DMA_MINC_DISABLE;
dma.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
dma.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
dma.Init.Mode = DMA_NORMAL;
dma.Init.Priority = DMA_PRIORITY_VERY_HIGH;
HAL_DMA_Init(&dma);
HAL_DMA_RegisterCallback(&dma, HAL_DMA_XFER_CPLT_CB_ID, DmaComplete);
HAL_DMA_RegisterCallback(&dma, HAL_DMA_XFER_ERROR_CB_ID, DmaError);
HAL_NVIC_SetPriority(BDMA_Channel0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(BDMA_Channel0_IRQn);
}

void BDMA_Channel0_IRQHandler(void)
{
HAL_DMA_IRQHandler(&dma);
}

void HardFault_Handler(void)
{
__NOP();
}

__attribute__((section(".ram_d3")))
static uint16_t src[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int main(void)
{
HAL_Init();
ClockConfig();
GpioConfig();
DmaConfig();
while (1)
{
complete = 0;
HAL_DMA_Start_IT(&dma, (uint32_t)&src, (uint32_t)&GPIOC->ODR, 10);
while (complete == 0) ;
}
}

scope2

最佳答案

你的假设是错误的。 GPIO 速度不是由核心指令时序决定的,而是由外设的物理特性决定的。请参阅 DS 中的表格,其中静态和动态 GPIO 端口特性。最大速度取决于许多参数。

关于arm - STM32H7xx 尽可能快地切换 IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51736591/

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