www.pudn.com > STM32_SPI_DMA.rar > main.c, change:2012-06-09,size:10826b


/** 
  ****************************************************************************** 
  * @file    USART/DMA_Interrupt/main.c  
  * @author  MCD Application Team 
  * @version V3.5.0 
  * @date    08-April-2011 
  * @brief   Main program body 
  ****************************************************************************** 
  * @attention 
  * 
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 
  * 
  * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> 
  ****************************************************************************** 
  */  
 
/* Includes ------------------------------------------------------------------*/ 
#include "stm32f10x.h" 
#include "platform_config.h" 
 
/** @addtogroup STM32F10x_StdPeriph_Examples 
  * @{ 
  */ 
 
/** @addtogroup USART_DMA_Interrupt 
  * @{ 
  */  
 
/* Private typedef -----------------------------------------------------------*/ 
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; 
 
/* Private define ------------------------------------------------------------*/ 
#define SF_PIN_SCK         GPIO_Pin_13 
#define SF_PIN_MISO        GPIO_Pin_14 
#define SF_PIN_MOSI        GPIO_Pin_15 
 
#define SF_CS             GPIO_Pin_12 
 
#define GPIOx_led		  GPIOD 
#define GPIO_Pin_x_led    GPIO_Pin_6 
 
/* Private macro -------------------------------------------------------------*/ 
#define countof(a)   (sizeof(a) / sizeof(*(a))) 
 
/* Private variables ---------------------------------------------------------*/ 
unsigned int i,j; 
 
unsigned char a[10]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa};  
unsigned char b[10]={0,0,0,0,0,0,0,0,0,0}; 
unsigned char SPI1_TX_Buf[10]={0x55}; 
unsigned char SPI2_RX_Buf[10]; 
 
/* Private function prototypes -----------------------------------------------*/ 
void RCC_Configuration(void); 
void GPIO_Configuration(void); 
void NVIC_Configuration(void); 
void DMA_Configuration(void); 
void SPI2_Configuration(void); 
void SPI1_Configuration(void); 
void DMA1_Configuration(void); 
 
 
/* Private functions ---------------------------------------------------------*/ 
 
/** 
  * @brief  Main program 
  * @param  None 
  * @retval None 
  */ 
int main(void) 
{ 
  /* System Clocks Configuration */ 
  RCC_Configuration(); 
        
  /* NVIC configuration */ 
  //NVIC_Configuration(); 
 
  /* Configure the GPIO ports */ 
  GPIO_Configuration(); 
 
  /* Configure the SPI1 */ 
  SPI1_Configuration();//配置为DMA发送 
 
  /* Configure the SPI2 */ 
  SPI2_Configuration();//配置为DMA接收 
 
  /* Configure the DMA1 */ 
  DMA1_Configuration(); 
 
  while(1) 
  { 
		 
		//发送部分	 
		 
		DMA_ClearFlag(DMA1_IT_TC3);      //清DMA发送完成标志 
		DMA_Cmd(DMA1_Channel3, DISABLE);   //停止DMA 
		DMA1_Channel3->CNDTR = 1;//重设传输长度 
		DMA_Cmd(DMA1_Channel3, ENABLE);    //启动DMA  
 
		for(i=0; i<0x3fffff; i++); 
		 
 
		 
		/* 
		//接收部分 
			 
		if(SPI2_RX_Buf[j] == 0x55) 
		{ 
			GPIO_ResetBits(GPIOx_led, GPIO_Pin_x_led); 
			for(i=0; i<0x3fffff; i++); 
			GPIO_SetBits(GPIOx_led, GPIO_Pin_x_led); 
			for(i=0; i<0x3fffff; i++); 
					 
			j++; 
 
			if(j == 10) 
			{ 
				j = 0;	 
 
			} 
		} 
		*/	  						 
  } 
} 
 
/** 
  * @brief  Configures the different system clocks. 
  * @param  None 
  * @retval None 
  */ 
void RCC_Configuration(void) 
{ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);     
 
  /* Enable GPIO clock */ 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC	| RCC_APB2Periph_GPIOD 
                       | RCC_APB2Periph_GPIOF | RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, ENABLE); 
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); 
} 
 
/** 
  * @brief  Configures the different GPIO ports. 
  * @param  None 
  * @retval None 
  */ 
void GPIO_Configuration(void) 
{ 
    GPIO_InitTypeDef GPIO_InitStructure; 
 
 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_x_led; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOx_led, &GPIO_InitStructure); 
	 
	GPIO_SetBits(GPIOx_led, GPIO_Pin_x_led);  
 
	//SPI1 
	/* 配置SPI引脚SCK、MISO 和 MOSI为复用推挽模式 */ 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //SF_PIN_SCK | SF_PIN_MISO | SF_PIN_MOSI; 
	//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA, &GPIO_InitStructure); 
 
	/* 配置片选口线为推挽输出模式 */ 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_Init(GPIOA, &GPIO_InitStructure); 
	GPIO_ResetBits(GPIOA, GPIO_Pin_4); 
 
	//SPI2 
	/* 配置SPI引脚SCK、MISO 和 MOSI为复用推挽模式 */ 
	GPIO_InitStructure.GPIO_Pin = SF_PIN_SCK | SF_PIN_MISO | SF_PIN_MOSI; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
 
	/* 配置片选口线为推挽输出模式 */ 
	GPIO_InitStructure.GPIO_Pin = SF_CS; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
 
} 
 
/** 
  * @brief  Configures the nested vectored interrupt controller. 
  * @param  None 
  * @retval None 
  */ 
void NVIC_Configuration(void) 
{ 
   NVIC_InitTypeDef NVIC_InitStructure; 
 
  /* Enable the USART1 Interrupt */ 
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure); 
 
  /* Enable the USARTz Interrupt */ 
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure); 
} 
 
 
/** 
  * @brief  Configures the SPI2. 
  * @param  None 
  * @retval None 
  */ 
void SPI2_Configuration(void) 
{ 
	SPI_InitTypeDef  SPI_InitStructure; 
 
	/* 配置SPI硬件参数 */ 
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;	/* 数据方向:2线全双工 */ 
	SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;		/* STM32的SPI工作模式 :主机模式 */ 
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;	/* 数据位长度 : 8位 */ 
	/* SPI_CPOL和SPI_CPHA结合使用决定时钟和数据采样点的相位关系、 
	   本例配置: 总线空闲是高电平,第2个边沿(上升沿采样数据) 
	*/ 
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;			/* 时钟上升沿采样数据 */ 
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;		/* 时钟的第2个边沿采样数据 */ 
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;			/* 片选控制方式:软件控制 */ 
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;	/* 波特率预分频系数:4分频 */ 
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	/* 数据位传输次序:高位先传 */ 
	SPI_InitStructure.SPI_CRCPolynomial = 7;			/* CRC多项式寄存器,复位后为7。本例程不用 */ 
	SPI_Init(SPI2, &SPI_InitStructure); 
 
    // Enable DMA1 Channel4 // 
    DMA_Cmd(DMA1_Channel4, ENABLE); 
 
	SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); 
 
	SPI_Cmd(SPI2, ENABLE);		/* 使能SPI  */ 
 
} 
 
/** 
  * @brief  Configures the SPI1. 
  * @param  None 
  * @retval None 
  */ 
void SPI1_Configuration(void) 
{ 
	SPI_InitTypeDef  SPI_InitStructure; 
 
	/* 配置SPI硬件参数 */ 
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;	/* 数据方向:2线全双工 */ 
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		/* STM32的SPI工作模式 :主机模式 */ 
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;	/* 数据位长度 : 8位 */ 
	/* SPI_CPOL和SPI_CPHA结合使用决定时钟和数据采样点的相位关系、 
	   本例配置: 总线空闲是高电平,第2个边沿(上升沿采样数据) 
	*/ 
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;			/* 时钟上升沿采样数据 */ 
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;		/* 时钟的第2个边沿采样数据 */ 
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;			/* 片选控制方式:软件控制 */ 
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;	/* 波特率预分频系数:4分频 */ 
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	/* 数据位传输次序:高位先传 */ 
	SPI_InitStructure.SPI_CRCPolynomial = 7;			/* CRC多项式寄存器,复位后为7。本例程不用 */ 
	SPI_Init(SPI1, &SPI_InitStructure); 
 
    // Enable DMA1 Channel3 // 
    DMA_Cmd(DMA1_Channel3, ENABLE); 
 
	SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE); 
 
	SPI_Cmd(SPI1, ENABLE);		/* 使能SPI  */ 
 
 
 
} 
 
/** 
  * @brief  Configures the SPI1. 
  * @param  None 
  * @retval None 
  */ 
void DMA1_Configuration(void) 
{ 
	DMA_InitTypeDef  DMA_InitStructure; 
 
	//发送通道 
	DMA_DeInit(DMA1_Channel3); 
	DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&SPI1->DR;//SPI1的DR寄存器地址 
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI1_TX_Buf; 
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  //外设地址是目的地 
	DMA_InitStructure.DMA_BufferSize = 1; 
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  //DMA内存地址自动增加模式 
	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(DMA1_Channel3, &DMA_InitStructure); 
 
	DMA_Cmd(DMA1_Channel3,ENABLE); 
 
	//接收通道 
	DMA_DeInit(DMA1_Channel4); 
	DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&SPI2->DR;//SPI2的DR寄存器地址 
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI2_RX_Buf; 
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  //外设地址是目的地 
	DMA_InitStructure.DMA_BufferSize = 10; 
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  //DMA内存地址自动增加模式 
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;    //循环模式 
	DMA_InitStructure.DMA_Priority = DMA_Priority_High; 
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 
	DMA_Init(DMA1_Channel4, &DMA_InitStructure); 
 
	DMA_Cmd(DMA1_Channel4,ENABLE); 
} 
/** 
  * @} 
  */  
 
/** 
  * @} 
  */  
 
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/