gpt4 book ai didi

c - 如何使用 DMA 将源阵列复制到目标阵列(使用 CMSIS CORE)?

转载 作者:行者123 更新时间:2023-12-02 09:13:58 28 4
gpt4 key购买 nike

我在 stm32f103rc 中尝试 DMA。我按照这个教程https://letanphuc.net/2014/06/how-to-use-stm32-dma/并使用 CMSIS CORE 编写了我自己的代码。在这里,我采用两个数组,其中一个是“sourceArr”,其中存储一个随机值,并在 DMA 的帮助下将该数组复制到“destArr”。

DMA 相关函数定义和变量

volatile uint32_t status = 0;
uint32_t sourceArr[ARRAYSIZE];
uint32_t destArr[ARRAYSIZE];

#define DMA1_CLOCK_EN() (RCC->AHBENR = RCC_AHBENR_DMA1EN)
#define DMA1_CHANNEL1_EN() (DMA1_Channel1->CCR |= DMA_CCR1_EN) //((uint16_t)0x0001)

void DMA1_Channel1_IRQHandler(void)
{
if (DMA1->ISR & DMA_ISR_TCIF1)
{
status = 1;
Blink_Led(5000);
DMA1->IFCR |= DMA_IFCR_CTCIF1;
}
}

void NVIC_Init()
{
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
}

void DMA_Init(void)
{
DMA1_Channel1->CCR |= DMA_CCR1_MEM2MEM; /*Memory to memory transfer enable ((uint16_t)0x4000)*/
DMA1_Channel1->CCR &= ~DMA_CCR1_CIRC; /* circular mode* ((uint16_t)0x0020) */
/* Priroty set as medium PL[1:0] = 01 */
DMA1_Channel1->CCR |= DMA_CCR1_PL_0; //((uint16_t)0x1000)
/* source and destination data size set as 32bit */
DMA1_Channel1->CCR |= DMA_CCR1_MSIZE_1; //((uint16_t)0x0800)
DMA1_Channel1->CCR |= DMA_CCR1_PSIZE_1; //((uint16_t)0x0200)
/* Auto increment of memory enabled for source and destination */
DMA1_Channel1->CCR |= DMA_CCR1_PINC; //((uint16_t)0x0040)
DMA1_Channel1->CCR |= DMA_CCR1_MINC; //((uint16_t)0x0080)
/* Data transfer direction set as read from peripheral*/
DMA1_Channel1->CCR &= ~DMA_CCR1_DIR; //((uint16_t)0x0010)
/* source and destination start addresses */
DMA1_Channel1->CPAR = (uint32_t)&sourceArr;
DMA1_Channel1->CMAR = (uint32_t)&destArr;
/* Enable DMA1 Channel Transfer Complete interrupt */
DMA1_Channel1->CCR |= DMA_CCR1_TCIE; //((uint16_t)0x0002)

}

void DMA_DeInit(void)
{
DMA1_Channel1->CCR &= (uint16_t)~DMA_CCR1_EN; /* Disable the selected DMAy Channelx */
DMA1_Channel1->CCR = 0; /* Reset DMAy Channelx control register */
DMA1_Channel1->CNDTR = 0; /* Reset DMAy Channelx remaining bytes register */
DMA1_Channel1->CPAR = 0; /* Reset DMAy Channelx peripheral address register */
DMA1_Channel1->CMAR = 0; /* Reset DMAy Channelx memory address register */
DMA1->IFCR |= DMA_IFCR_CGIF1 | DMA_IFCR_CTCIF1 | DMA_IFCR_CHTIF1 | DMA_IFCR_CTEIF1; /* Reset interrupt pending bits for DMA1 Channel1 */

}

主要

int main(void)
{
int i;
for (i=0;i<ARRAYSIZE;i++)
sourceArr[i] = i;
Led_Init();
Blink_Led(1000);
DMA1_CLOCK_EN(); //enable clock for DMA
DMA_DeInit();
DMA_Init();
NVIC_Init();
Blink_Led(1000);
status = 0;
__enable_irq();
DMA1_CHANNEL1_EN(); //Enable DMA1 Channel 1 transfer
while(status==0);
Blink_Led(1000);
for (i=0; i<ARRAYSIZE;i++)
{
destArr[i]=sourceArr[i];
}
Blink_Led(1000);

while (1)
{

}
}

如果一切正常,则会闪烁 5 次。主函数中有 4 个,IRQ 中有 1 个。

但我只眨眼两次。

任何建议都会非常有帮助。

提前致谢

最佳答案

您应该设置要传输的数据数量

DMA1_Channel1->CNDTR = ARRAYSIZE;

在使用 DMA1_CHANNEL1_EN() 启用 channel 之前。

关于c - 如何使用 DMA 将源阵列复制到目标阵列(使用 CMSIS CORE)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974648/

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