3楼正确,的确存在竟争
谢谢浪淘沙的文档,的确很及时 ^o^
DMA 控制器和 Cortex-M3 核共享系统数据线执行直接存储器数据传输。因此,1 个 DMA 请求占用至少 2个周期的 CPU访问系统总线时间。为了保证 Cortex-M3核的代码执行的最 小带宽,DMA控制器总是在 2个连续的DMA请求间释放系统时钟至少1个周期。
可是spi相对指命来说,是很慢的设备,问题肯定不在这里。我贴出代码,浪淘沙可有空帮忙解决? xixi
代码是demo中的,只在Demo_Init函数未尾增加了SPI DMA的代码。
int main(void) { #ifdef DEBUG debug(); #endif
/* Initialize the Demo */ Demo_Init(); while(1); }
void Demo_Init(void) { SPI_InitTypeDef SPI_InitStructure; DMA_InitTypeDef DMA_InitStructure; unsigned char SPI1_Buffer_Rx[128]; unsigned char SPI1_Buffer_Tx[128];
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/ /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2);
/* ADCCLK = PCLK2/6 */ // RCC_ADCCLKConfig(RCC_PCLK2_Div6);
/* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */ RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
/* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1, ENABLE); /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
/* TIM1 Periph clock enable */ // RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* SPI2 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* TIM2 and TIM4 clocks enable */ // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM4, ENABLE);
/* USBCLK = PLLCLK/1.5 */ // RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable USB clock */ // RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
/*------------------- Resources Initialization -----------------------------*/ /* GPIO Configuration */ GPIO_Config();
/* Interrupt Configuration */ InterruptConfig();
/* Configure the systick */ SysTick_Config();
/*------------------- Drivers Initialization -------------------------------*/ /* Initialize the LEDs toogling */ LedShow_Init();
/* Initialize the Low Power application */ LowPower_Init();
/* If HSE is not detected at program startup */ if(HSEStartUpStatus == ERROR) { /* Generate NMI exception */ NVIC_SetSystemHandlerPendingBit(SystemHandler_NMI); }
/* Initialize the SPI FLASH driver */ SPI_FLASH_Init(); // MSD_Init();
#if 1 /* Test for SPI DMA function */ #define CRCPolynomial 7 #define SPI1_DR_Address 0x4001300C #define BufferSize 128 /* 初始化SPI1 ------------------------------------------------------*/ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = CRCPolynomial; SPI_Init(SPI1, &SPI_InitStructure); /* 初始化DMA Channel2 ----------------------------------------------*/ DMA_DeInit(DMA_Channel2); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI1_Buffer_Rx; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = BufferSize; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA_Channel2, &DMA_InitStructure); /* DMA Channel3 configuration ----------------------------------------------*/ DMA_DeInit(DMA_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI1_Buffer_Tx; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(DMA_Channel3, &DMA_InitStructure); /* Enable SPI1 DMA Rx request */ SPI_DMACmd(SPI1, SPI_DMAReq_Tx, ENABLE); SPI_DMACmd(SPI1, SPI_DMAReq_Rx, ENABLE); /* Enable SPI1 CRC calculation */ SPI_CalculateCRC(SPI1, ENABLE); /* Enable SPI1 */ SPI_Cmd(SPI1, ENABLE); DMA_Cmd(DMA_Channel2, ENABLE); DMA_Cmd(DMA_Channel3, ENABLE); /* Transfer complete */ while(!DMA_GetFlagStatus(DMA_FLAG_TC2)); #endif /* Enable Leds toggling */ LedShow(ENABLE);
} |
|