无法读/写winbond W25x16

时间:2018-06-15 18:17:12

标签: c arm stm32

我从winbond w25x16使用stm32f103rct6进行SPI Flash。 编程我正在使用CubeMX - HAL和Keil v5

以下是我关注的链接:

https://electronics.stackexchange.com/questions/51229/how-do-i-write-to-spi-flash-memory

来自Jeff Walther的

以及示例程序的此链接

https://github.com/mhollands/Winbond-W25Q32JV/blob/master/main.cpp

这是我的代码:

uint8_t Check_Status_Flag()
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);       //CS Low
    HAL_Delay(10);

    uint8_t txBuff[1],rxBuff[1];
    txBuff[0] = 0x05;               //command for checking status
    HAL_SPI_Transmit(&hspi1, txBuff, 1, 100);           //send command
    txBuff[0] = 0x00;
    HAL_SPI_Transmit(&hspi1, txBuff, 1, 100);           //send dummy value
    HAL_SPI_Receive(&hspi1, rxBuff, 1, 100);            //receive status

    return rxBuff[0];
}

void Write_Enable()
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);       //CS Low
    HAL_Delay(10);

    uint8_t txBuff[1],rxBuff[1];
    txBuff[0] = 0x06;           //command for write enable
    HAL_SPI_Transmit(&hspi1, txBuff, 1, 100);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);     //CS High
    HAL_Delay(10);


    while ((Check_Status_Flag() & 0x02) != 0x00);
}


void Sector_Erase(uint8_t addr1, uint8_t addr2, uint8_t addr3)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);       //CS Low
    HAL_Delay(10);

    uint8_t txBuff[4],rxBuff[4];
    txBuff[0] = 0x20;           //command for sector erase
    txBuff[1] = addr1;      //MSB of the address
    txBuff[2] = addr2;      //middle address
    txBuff[3] = addr3;          //LSB of the address
    HAL_SPI_Transmit(&hspi1, txBuff, 4, 100);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);     //CS High
    HAL_Delay(10);

    while ((Check_Status_Flag() & 0x01) != 0x00);
}

void Write_Data(uint8_t addr1, uint8_t addr2, uint8_t addr3, uint8_t data)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);       //CS Low
    HAL_Delay(10);

    uint8_t txBuff[5],rxBuff[5];
    txBuff[0] = 0x02;           //command for write data
    txBuff[1] = addr1;      //MSB of the address
    txBuff[2] = addr2;      //middle address
    txBuff[3] = addr3;          //LSB of the address
    txBuff[4] = data;
    HAL_SPI_Transmit(&hspi1, txBuff, 5, 100);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);     //CS High
    HAL_Delay(10);

    while ((Check_Status_Flag() & 0x01) != 0x00);
}

void Read_Data(uint8_t addr1, uint8_t addr2, uint8_t addr3)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);       //CS Low
    HAL_Delay(10);

    uint8_t txBuff[5],rxBuff[5];
    txBuff[0] = 0x03;           //command for read data
    txBuff[1] = addr1;      //MSB of the address
    txBuff[2] = addr2;      //middle address
    txBuff[3] = addr3;          //LSB of the address
    HAL_SPI_Transmit(&hspi1, txBuff, 4, 100);
    HAL_SPI_Receive(&hspi1, rxBuff, 4, 100);        //receiving 4 garbage

    txBuff[0] = 0x00;

    HAL_SPI_Transmit(&hspi1, txBuff, 1, 100);       //transmitting dummy
    HAL_SPI_Receive(&hspi1, rxBuff, 1, 100);        //receiving the data 
    uint8_t data = rxBuff[0];                                       //storing the data
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);     //CS High
    HAL_Delay(10);

    while ((Check_Status_Flag() & 0x01) != 0x00);
}

这是主要的

int main(void)
{
  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_SPI1_Init();


  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);       //CS High
  HAL_Delay(10);
  Write_Enable();
  Sector_Erase(0x00, 0x10, 0x00);
  Write_Enable();
  Write_Data(0x00, 0x00, 0x01, 0x11);
  Read_Data(0x00, 0x00, 0x01);

  while (1)
  {

  }

}

SPI init

static void MX_SPI1_Init(void)
{

  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

当我进入调试模式并在监视窗口下放置'data'(在Read_Data函数内)时,我没有得到我在该特定寄存器中写入的值。

错误说:无法评估

我对调试部分知之甚少,我的代码可能存在一些问题。

如果有人可以指导我如何在stm32中使用调试模式并指出我的代码中的错误,那将非常有帮助。

提前致谢。

0 个答案:

没有答案