- #include "stm32f10x.h"
- #include "stdio.h"
- #include "string.h"
- #define PRINTF_ON 1
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void USART_Configuration(void);
- void SPI_Configuration(void);
- #define BufferSize 32
- #define delay() for(i=0;i<200;i++)
- SPI_InitTypeDef SPI_InitStructure;
- u8 SPI1_Buffer_Tx[BufferSize] =
- {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,
- 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
- 0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
- };
- u8 SPI2_Buffer_Tx[BufferSize] =
- {
- 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,
- 0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,
- 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,
- 0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70
- };
- u8 SPI1_Buffer_Rx[BufferSize+1] = {0};
- u8 SPI2_Buffer_Rx[BufferSize] = {0};
- u8 Tx_Counter = 0;
- u8 Rx_Counter = 0;
- u8 k=0,i=0;
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- USART_Configuration();
- SPI_Configuration();
- while(Tx_Counter < BufferSize)
- {
- SPI_I2S_SendData(SPI2,SPI2_Buffer_Tx[Tx_Counter]); //必须从机先发送数据
- //while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET); //如果spi2 还有发送缓存则等待发送完成
- SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Tx_Counter]);
-
- while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == RESET); //没有接收缓存则等待
- SPI2_Buffer_Rx[Rx_Counter] = SPI_I2S_ReceiveData(SPI2);
-
- while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) ==RESET);
-
- SPI1_Buffer_Rx[Rx_Counter] = SPI_I2S_ReceiveData(SPI1);
- Tx_Counter++;
- Rx_Counter++;
- }
- printf("\r\n The SPI1 has sended data below : \r\n");
- while(k<BufferSize)
- {
- printf(" %0.2d \r ",SPI1_Buffer_Tx[k]);
- k++;
- }
- printf("\r\n The SPI2 has received data below : \r\n");
- k=0;
- while(k<BufferSize)
- {
- printf(" %0.2d \r ",SPI2_Buffer_Rx[k]);
- k++;
- }
- k=0;
- printf("\r\n The SPI2 has sended data below : \r\n");
- while(k<BufferSize)
- {
- printf(" %0.2d \r ",SPI2_Buffer_Tx[k]);
- k++;
- }
- printf("\r\n The SPI1 has received data below : \r\n");
- k=0;
- while(k<BufferSize)
- {
- printf(" %0.2d \r ",SPI1_Buffer_Rx[k]);
- k++;
- }
- while(1);
- }
- void SPI_Configuration(void)
- {
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- 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_4;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_Init(SPI1,&SPI_InitStructure);
-
- SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
- SPI_Init(SPI2,&SPI_InitStructure);
- SPI_Cmd(SPI1,ENABLE);
- SPI_Cmd(SPI2,ENABLE);
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- }
- void RCC_Configuration(void)
- {
- /* 定义枚举类型变量 HSEStartUpStatus */
- ErrorStatus HSEStartUpStatus;
- /* 复位系统时钟设置*/
- RCC_DeInit();
- /* 开启HSE*/
- RCC_HSEConfig(RCC_HSE_ON);
- /* 等待HSE起振并稳定*/
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- /* 判断HSE起是否振成功,是则进入if()内部 */
- if(HSEStartUpStatus == SUCCESS)
- {
- /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* 设置FLASH延时周期数为2 */
- FLASH_SetLatency(FLASH_Latency_2);
- /* 使能FLASH预取缓存 */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* 使能PLL */
- RCC_PLLCmd(ENABLE);
- /* 等待PLL输出稳定 */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* 选择SYSCLK时钟源为PLL */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* 等待PLL成为SYSCLK时钟源 */
- while(RCC_GetSYSCLKSource() != 0x08);
- }
- /* 打开APB2总线上的GPIOA时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1|RCC_APB2Periph_SPI1, ENABLE);
- //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
- //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP|RCC_APB1Periph_WWDG, ENABLE);
-
- }
-
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- USART_ClockInit(USART1 , &USART_ClockInitStructure);
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART1,&USART_InitStructure);
- USART_Cmd(USART1,ENABLE);
- }
- #if PRINTF_ON
- int fputc(int ch,FILE *f)
- {
- USART_SendData(USART1,(u8) ch);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
- return ch;
- }
- #endif