目前有个项目用到stm32f030CCT6,原来都用stm32f030RCT6,在把SPI移过来时只是改了IO口,stm32f030CCT6用到的是PA4,PA5,PA6,PA7用来做SPI,但是一直调不通,总是死在了 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)这里面,看了网上的贴子,换成while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);判断忙信号也不行。虽然不死在里面,但读出来的数据全是0,不知道有哪位大师有用遇到过此类问题,这款芯片还是第一次用,我的初始化如下.此初始化代码在030RCT6是可以的,已经量产。唯一的区别就是改了下IO口#define PIN_SPI1_CSN GPIO_Pin_4
#define PIN_SPI1_SCK GPIO_Pin_5
#define PIN_SPI1_MISO GPIO_Pin_6
#define PIN_SPI1_MOSI GPIO_Pin_7
/* Select SPI FLASH: Chip Select pin low */
#define SPI_FLASH_CS_LOW() GPIO_ResetBits(GPIOA, PIN_SPI1_CSN)
/* Deselect SPI FLASH: Chip Select pin high */
#define SPI_FLASH_CS_HIGH() GPIO_SetBits(GPIOA, PIN_SPI1_CSN)
void spi_flash_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //中速
GPIO_InitStructure.GPIO_Pin = PIN_SPI1_SCK | PIN_SPI1_MOSI | PIN_SPI1_MISO;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
GPIO_InitStructure.GPIO_Pin = PIN_SPI1_CSN; //配置 CSN
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //高速
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_0);
SPI_I2S_DeInit(SPI1); //将寄存器重设为缺省值
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主机模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8 位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //通信空闲时 SCK 为低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //第一个时钟沿捕获
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件控制 NSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //SPI 速度 8 分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从 MSB 开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC 校验
SPI_Init(SPI1, &SPI_InitStructure);
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);//重要,把应答数据位设置为 8 位
SPI_Cmd(SPI1, ENABLE);
}
|