STM32F103之SPI FLASH问题

[复制链接]
7053|7
 楼主| dontium 发表于 2011-5-24 19:00 | 显示全部楼层 |阅读模式
本帖最后由 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;
}
sjnh 发表于 2011-5-25 08:41 | 显示全部楼层
1:操作SST25VF016B的时序不对,参照手册,
2:SPI_DR读写是不同的值
yinyangdianzi 发表于 2011-5-25 09:56 | 显示全部楼层
NOR FLASH  SST的不错。
 楼主| dontium 发表于 2011-5-25 16:30 | 显示全部楼层
本帖最后由 dontium 于 2011-5-25 17:21 编辑

谢谢sjnh

我仔细看了存储器资料,程序也改了。还是没能读到ID

我用示波器看各脚波形,发现:
MOSI 有信号
CLOCK 恒低
nSS   恒高

看来,STM32的设置还是有问题。
再烦请大家帮忙
谢谢
hotpower 发表于 2011-5-25 17:26 | 显示全部楼层
学习了
 楼主| dontium 发表于 2011-5-25 18:35 | 显示全部楼层
PA15用两种设置也都试过了的:AF_PP, OUT_PP
 楼主| dontium 发表于 2011-5-25 18:54 | 显示全部楼层
本帖最后由 dontium 于 2011-5-25 18:57 编辑

终于搞定了

You are mapping SPI1 to JTAG pins, you will need to disable the JTAG function.

Refer to the STM32 Reference Manual (RM0008) Chapter on SPI. PB.3 is JTDO, PB.4 is JNTRST, and PB.5 is not 5V tolerant.

"Warning: Since some SPI3/I2S3 pins are shared with JTAG pins
(SPI3_NSS/I2S3_WS with JTDI and SPI3_SCK/I2S3_CK with
JTDO), they are not controlled by the I/O controller and are
reserved for JTAG usage (after each Reset).
For this purpose prior to configure the SPI3/I2S3 pins, the
user has to disable the JTAG and use the SWD interface
(when debugging the application), or disable both JTAG/SWD
interfaces (for standalone application). For more information
on the configuration of JTAG/SWD interface pins, please refer
to Section 7.3.4: JTAG/SWD alternate function remapping."


https://my.st.com/public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=https%3a%2f%2fmy%2est%2ecom%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fARM%20CortexM3%20STM32%2fBIG%20PROBLEM%20WITH%20SPI%20Interface%20in%20STM32%20completly%20disaster%20of%20ST&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E&currentviews=1458
 楼主| dontium 发表于 2011-5-25 18:59 | 显示全部楼层
谢谢sjnh,也谢谢大家。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

151

主题

1176

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部