本帖最后由 dontium 于 2011-5-24 19:05 编辑
我用IAR5.5编译STM32F103VBT6,存储器用SST25VF016B,使用PB3、4、5及PA15。
现在只是试一下读FLASH ID,结果读不到,
调试时发现,向SPI_DR写数据时,根本改变不了它的值,始终为SPI_DR=0。其程序如下,请帮忙指导一下,先谢谢了!
RCC部分:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE , ENABLE);//LED
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI初始化部分:
/* Configure SPI pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_PIN_SCK | SPI_FLASH_PIN_MOSI | SPI_FLASH_PIN_MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_FLASH_GPIO, &GPIO_InitStructure);
/* Configure I/O for Flash Chip select */
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPI_FLASH_CS_GPIO, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
//SPI_FLASH_CS_HIGH();
//GPIO_SetBits(GPIOA,GPIO_Pin_15);
GPIOA->BSRR = GPIO_Pin_15;
AFIO->MAPR |= 1; //use remap function,PB3,4,5 Remap to SPI1 MOSI,MISO,SCK
//GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE);
/* disable the SPI */
SPI_Cmd(SPI_FLASH, DISABLE);//***
/* SPI configuration */
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_Low; //SPI_CPOL_High
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //SPI_CPHA_2Edge
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(SPI_FLASH, &SPI_InitStructure);
/* Enable the SPI */
SPI_Cmd(SPI_FLASH, ENABLE);
这个是读ID部分:
uint32_t SPI_FLASH_ReadID(void)
{
uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "RDID " instruction */
SPI_FLASH_SendByte(0x90);
/* Read a byte from the FLASH */
Temp0 = SPI_FLASH_SendByte(0x00);
/* Read a byte from the FLASH */
Temp1 = SPI_FLASH_SendByte(0x00);
/* Read a byte from the FLASH */
Temp2 = SPI_FLASH_SendByte(0x00);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
return Temp;
}
其中,上面读ID的过程SPI_FLASH_SendByte为:
uint8_t SPI_FLASH_SendByte(uint8_t byte)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI_FLASH, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI_FLASH, byte);
//SPI1->DR = (u16) (0xff & byte);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI_FLASH, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
//return SPI_I2S_ReceiveData(SPI_FLASH);
return SPI1->DR;
} |