小学生
最后登录1970-1-1
在线时间 小时
注册时间2017-12-13
|
本帖最后由 walnut666 于 2017-12-13 16:31 编辑
我设置开发板的SP1为从模式,SP2为主模式。并且用杜邦线将sp1与sp2连起来,SP2循环发送发送0x01-0x20,但是sp1中断收不到数据,并且中断里SPI_IT_MODF会变成SET。已经卡了好几天了,实在找不出问题出在哪里。求好心人帮忙。
代码如下:
main文件:- int main(void)
- {
- //嵌套向量终端控制器组选择
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- BASIC_TIM_Init();
- LED_GPIO_Config();
- USART_Config();
- Usart_SendByte(DEBUG_USARTx,88);
- SPI_M_Init();
- SPI_S_Init();
-
- u8 SPI_M_Buffer_Tx[32] = /* 定义待 SPI-master 传输数据 */
- {
- 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 SPI_M_Buffer_Rx[32];
- u8 RxSP=0;
- while(1)
- {
- M_SPI_CS_LOW();
- for(int i=0;i<32;i++)
- {
-
- SPI_M_Buffer_Rx[i]=M_SPI_SendByte(SPI_M_Buffer_Tx[i]);
-
- }
- M_SPI_CS_HIGH();
- SOFT_DELAY;
- }
- while(1)
- {
-
- LED1_ON;
- SOFT_DELAY;
- LED1_OFF;
- LED3_ON;
- SOFT_DELAY
- LED3_OFF;
- LED2_ON;
- SOFT_DELAY
- LED2_OFF;
- };
- }
复制代码
配置spi的.h文件
- #ifndef __SPI_H
- #define __SPI_H
- #include "stm32f10x.h"
- /*等待超时时间*/
- #define SPIT_FLAG_TIMEOUT ((uint32_t)0x1000)
- #define SPIT_LONG_TIMEOUT ((uint32_t)(10 * SPIT_FLAG_TIMEOUT))
- /*spi主发设备。使用SPI2*/
- //--------start--------
- #define M_SPI SPI2
- #define M_SPI_APBxClock_FUN RCC_APB1PeriphClockCmd
- #define M_SPI_CLK RCC_APB1Periph_SPI2
- //中断
- #define M_SPI_IRQ SPI2_IRQn
- #define M_SPI_IRQHandler SPI2_IRQHandler
- //CS(NSS)引脚 片选选普通 GPIO 即可
- #define M_SPI_CS_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define M_SPI_CS_CLK RCC_APB2Periph_GPIOB
- #define M_SPI_CS_PORT GPIOB
- #define M_SPI_CS_PIN GPIO_Pin_12
- //SCK 引脚
- #define M_SPI_SCK_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define M_SPI_SCK_CLK RCC_APB2Periph_GPIOB
- #define M_SPI_SCK_PORT GPIOB
- #define M_SPI_SCK_PIN GPIO_Pin_13
- //MISO 引脚
- #define M_SPI_MISO_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define M_SPI_MISO_CLK RCC_APB2Periph_GPIOB
- #define M_SPI_MISO_PORT GPIOB
- #define M_SPI_MISO_PIN GPIO_Pin_14
- //MOSI 引脚
- #define M_SPI_MOSI_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define M_SPI_MOSI_CLK RCC_APB2Periph_GPIOB
- #define M_SPI_MOSI_PORT GPIOB
- #define M_SPI_MOSI_PIN GPIO_Pin_15
- #define M_SPI_CS_LOW() GPIO_ResetBits(M_SPI_CS_PORT, M_SPI_CS_PIN)
- #define M_SPI_CS_HIGH() GPIO_SetBits(M_SPI_CS_PORT, M_SPI_CS_PIN)
- //-------end--------
- /*SPI 接口定义-开头****************************/
- #define S_SPI SPI1
- #define S_SPI_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define S_SPI_CLK RCC_APB2Periph_SPI1
- //CS(NSS)引脚 片选选普通 GPIO 即可
- #define S_SPI_CS_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define S_SPI_CS_CLK RCC_APB2Periph_GPIOA
- #define S_SPI_CS_PORT GPIOA
- #define S_SPI_CS_PIN GPIO_Pin_4
- //SCK 引脚
- #define S_SPI_SCK_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define S_SPI_SCK_CLK RCC_APB2Periph_GPIOA
- #define S_SPI_SCK_PORT GPIOA
- #define S_SPI_SCK_PIN GPIO_Pin_5
- //MISO 引脚
- #define S_SPI_MISO_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define S_SPI_MISO_CLK RCC_APB2Periph_GPIOA
- #define S_SPI_MISO_PORT GPIOA
- #define S_SPI_MISO_PIN GPIO_Pin_6
- //MOSI 引脚
- #define S_SPI_MOSI_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define S_SPI_MOSI_CLK RCC_APB2Periph_GPIOA
- #define S_SPI_MOSI_PORT GPIOA
- #define S_SPI_MOSI_PIN GPIO_Pin_7
- #define S_SPIxIRQ SPI1_IRQn
- #define S_SPI_IRQHandler SPI1_IRQHandler
- /*SPI 接口定义-结尾****************************/
- //spi_T初始化
- void SPI_M_Init(void);
- void SPI_S_Init(void);
- u8 M_SPI_SendByte(u8 byte);
- #endif
复制代码 配置SPI的.c文件- #include "bsp_spi.h"
- static __IO uint32_t SPITimeout = SPIT_LONG_TIMEOUT;
- void SPI_M_Init(void)
- {
-
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* 使能SPI时钟 */
- M_SPI_APBxClock_FUN ( M_SPI_CLK, ENABLE );
-
- /* 使能SPI引脚相关的时钟 */
- M_SPI_CS_APBxClock_FUN ( M_SPI_CS_CLK|M_SPI_SCK_CLK|
- M_SPI_MISO_PIN|M_SPI_MOSI_PIN, ENABLE );
-
- /* 配置SPI的 CS引脚,普通IO即可 */
- GPIO_InitStructure.GPIO_Pin = M_SPI_CS_PIN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(M_SPI_CS_PORT, &GPIO_InitStructure);
-
- /* 配置SPI的 SCK引脚*/
- GPIO_InitStructure.GPIO_Pin = M_SPI_SCK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(M_SPI_SCK_PORT, &GPIO_InitStructure);
- /* 配置SPI的 MISO引脚*/
- GPIO_InitStructure.GPIO_Pin = M_SPI_MISO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
- GPIO_Init(M_SPI_MISO_PORT, &GPIO_InitStructure);
- /* 配置SPI的 MOSI引脚*/
- GPIO_InitStructure.GPIO_Pin = M_SPI_MOSI_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(M_SPI_MOSI_PORT, &GPIO_InitStructure);
- /* 停止信号 FLASH: CS引脚高电平*/
- M_SPI_CS_HIGH();
- /* SPI 模式配置 */
- // FLASH芯片 支持SPI模式0及模式3,据此设置CPOL CPHA
- 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_High;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 0;
- SPI_Init(M_SPI , &SPI_InitStructure);
- /* 使能 SPI */
- SPI_Cmd(M_SPI , ENABLE);
- }
- u8 M_SPI_SendByte(u8 byte)
- {
- SPITimeout = SPIT_FLAG_TIMEOUT;
- /* 等待发送缓冲区为空,TXE事件 */
- while (SPI_I2S_GetFlagStatus(M_SPI , SPI_I2S_FLAG_TXE) == RESET)
- {
- if((SPITimeout--) == 0)
- return 255;
- }
- /* 写入数据寄存器,把要写入的数据写入发送缓冲区 */
- SPI_I2S_SendData(M_SPI , byte);
- SPITimeout = SPIT_FLAG_TIMEOUT;
- /* 等待接收缓冲区非空,RXNE事件 */
- while (SPI_I2S_GetFlagStatus(M_SPI , SPI_I2S_FLAG_RXNE) == RESET)
- {
- if((SPITimeout--) == 0)
- return 255;
- }
- /* 读取数据寄存器,获取接收缓冲区数据 */
- return SPI_I2S_ReceiveData(M_SPI);
- }
- void SPI_S_Init(void)
- {
-
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- //使能spi时钟
- S_SPI_APBxClock_FUN(S_SPI_CLK, ENABLE);
- //使能spi引脚相关时钟
- S_SPI_CS_APBxClock_FUN( S_SPI_CS_CLK |S_SPI_SCK_CLK |
- S_SPI_MISO_PIN | S_SPI_MOSI_PIN,
- ENABLE);
-
-
-
- //配置spi的cs引脚
- GPIO_InitStructure.GPIO_Pin = S_SPI_CS_PIN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//下拉输入
- GPIO_Init(S_SPI_CS_PORT, &GPIO_InitStructure);
-
- //配置spi的sck引脚
- GPIO_InitStructure.GPIO_Pin = S_SPI_SCK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//时钟设置为悬浮输入
- GPIO_Init(S_SPI_SCK_PORT, &GPIO_InitStructure);
- //配置spi的mosi引脚
- GPIO_InitStructure.GPIO_Pin=S_SPI_MOSI_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
- GPIO_Init(S_SPI_MOSI_PORT,&GPIO_InitStructure);
-
- //配置spi的miso引脚
- GPIO_InitStructure.GPIO_Pin=S_SPI_MISO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
- GPIO_Init(S_SPI_MISO_PORT,&GPIO_InitStructure);
-
- //停止信号
- //T_SPI_CS_HIGH();
- //SPI模式配置
- //配置CPOL,CPHA
- SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
- SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
- SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS=SPI_NSS_Hard;
- SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256;
- SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial=0;
-
- SPI_Init(S_SPI,&SPI_InitStructure);
-
- //配置中断
- //配置USART为中断源
- NVIC_InitStructure.NVIC_IRQChannel = S_SPIxIRQ;
- //抢断优先级位1
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- //子优先级为1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- //使能中断
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- //初始化配置NVIC
- NVIC_Init(&NVIC_InitStructure);
- //使能spi接收、发送中断
- SPI_I2S_ITConfig(S_SPI,SPI_I2S_IT_RXNE,ENABLE);
- SPI_I2S_ITConfig(S_SPI,SPI_I2S_IT_TXE,ENABLE);
- SPI_I2S_SendData(S_SPI,0xff);
- //使能spi
- SPI_Cmd(S_SPI,ENABLE);
- }
复制代码
|
|