要使DMA与外设建立有效连接,这不是DMA自身的事情,是各个外设的事情,每个外设都有 一个xxx_DMACmd(XXXx,Enable )函数,如果使DMA与SPI建立有效联系,就使用SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Rx,ENABLE);
[size=12.7272720336914px]具体代码如下:
- void DMA_Configuration_SPI1_RX(u16 bufsize)
- {
- DMA_DeInit(DMA1_Channel2);
- DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&SPI1->DR;;
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)Usart_TX_Buf;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA单向传输
- DMA_InitStructure.DMA_BufferSize = bufsize;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //DMA内存地址自动增加模式
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel2, &DMA_InitStructure);
- }
- /* 使用软件模拟SPI读写时序,进行初始化 */
- void ADS8345_PORT_INIT( void )
- {
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- /*spi1 port_init*/
- /* Configure SPI1 pins: SCK, NSS and MOSI ---------------------------------*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 ;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- /*miso*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure) ;
- /* Configure SPI1 pins: NSS ---------------------------------*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA,GPIO_Pin_4);
- /* SPI1 Configuration (Master Tx, 14 MBaud) --------------------------------*/
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI1, &SPI_InitStructure);
- /* Enable SPI1 */
- SPI_Cmd(SPI1, ENABLE);
- }
-
- void RCC_Configuration(void)
- {
- /* 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)
- {
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* ADCCLK = PCLK2/4 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
-
- /* PLLCLK = 8MHz * 7 = 56 MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);
- /* 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)
- {
- }
- }
- /* Enable DMA clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- /* Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_SPI1, ENABLE);
- }
复制代码
在main中:
- RCC_Configuration();
- /*System tick init*/
- delay_init(72);
- ADS8345_PORT_INIT();
- /*SPI RX DMA enable*/
- DMA_Configuration_SPI1_R(2);
- // NVIC_Configuration();
- DMA_Cmd(DMA1_Channel2,ENABLE);
- SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Rx,ENABLE);
- DMA_ITConfig(DMA1_Channel2, DMA_IT_TC | DMA_IT_HT, ENABLE);
复制代码
然后直接就可以去Usart_TX_Buf中读数了! 谈谈DMA的基本结构和和常用函数两篇文就是讲— STM32 SPI DMA读取ADS8345数据
0
|
|
|
|