[STM32L4] 读写不对

[复制链接]
 楼主| xxmmi 发表于 2020-12-8 23:50 | 显示全部楼层 |阅读模式
int main(void)
{
        uint16_t i = 0;
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_RTC_Init();
  MX_SPI1_Init();

User_W25Reset();
HAL_Delay(100);
User_W25WriteStaus(0xA0, 0x00);
User_W25BlockErase(0x00);
for(i = 0; i < 2048; i++)
{
        gUSER_SPI1_WriteBuffer[i] = 0xA0;
}
User_W25WriteData(0x00);
User_W25ReadPage(0x00);
  while (1)
  {        
        HAL_Delay(100);
        User_W25ReadStaus(0xC0);
  }

}

void User_W25Reset()
{
        uint8_t nData = 0xFF;
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);        
        HAL_SPI_Transmit(&hspi1, &nData, 1, 1000);        
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
}

void User_W25EnableORNo(uint8_t nEnable)
{
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);
        HAL_SPI_Transmit(&hspi1, &nEnable, 1, 1000);        
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
}

void User_W25ReadStaus(uint8_t naddr)
{
        uint8_t nData[9];
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);
        nData[0] = 0x05;
        nData[1] = naddr;
        HAL_SPI_Transmit(&hspi1, nData, 2, 1000);
        nData[0] = 0x00;
        nData[1] = 0x00;
  HAL_SPI_TransmitReceive(&hspi1, nData, gUSER_SPI1_ReadBuffer, 8,1000);
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
}

void User_W25WriteStaus(uint8_t naddr, uint8_t nCode)
{
        uint8_t nData[4];
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);
        nData[0] = 0x01;
        nData[1] = naddr;
        nData[2] = nCode;
          HAL_SPI_Transmit(&hspi1, nData, 3, 1000);
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
}

void User_W25BlockErase(uint16_t naddr)
{
        uint8_t nData[5];
        User_W25EnableORNo(0x06);
        while(1)
        {
                User_W25ReadStaus(0xC0);
                if((gUSER_SPI1_ReadBuffer[0] & 0x01) == 0)
                        break;
        }
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);
        nData[0] = 0xD8;
        nData[1] = 0x00;
        nData[2] = (naddr >> 8) & 0xFF;
        nData[3] = naddr & 0xFF;
        HAL_SPI_Transmit(&hspi1, nData, 4, 1000);
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
        while(1)
        {
                User_W25ReadStaus(0xC0);
                if((gUSER_SPI1_ReadBuffer[0] & 0x01) == 0)        //这里读回的数据是0x20,是不是就说明擦除正确完成了呢?
                        break;
        }
}

void User_W25ReadPage(uint8_t naddr)
{
        uint8_t nData[5];
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);
        nData[0] = 0x13;
        nData[1] = 0x00;
        nData[2] = (naddr >> 8) & 0xFF;
        nData[3] = naddr & 0xFF;
        HAL_SPI_Transmit(&hspi1, nData, 4, 1000);
        User_W25ReadStaus(0xC0);
        while(1)
        {
                User_W25ReadStaus(0xC0);
                if((gUSER_SPI1_ReadBuffer[0] & 0x01) == 0)//这里返回的是0x20;
                        break;
        }
        nData[0] = 0x0B;
        nData[1] = 0x00;        
        nData[2] = 0x00;
        nData[3] = 0x00;
        HAL_SPI_Transmit(&hspi1, nData, 4, 1000);

        HAL_SPI_TransmitReceive(&hspi1, gUSER_SPI1_WriteBuffer, gUSER_SPI1_ReadBuffer, 2112,1000);//这里返回的数据都是0x00;
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
}

void User_W25WriteData(uint16_t naddr)
{
        uint8_t nData[5];
        User_W25EnableORNo(0x06);

        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_RESET);
        HAL_Delay(1);
        nData[0] = 0x02;
        nData[1] = 0x00;
        nData[2] = 0x00;
        HAL_SPI_Transmit(&hspi1, nData, 3, 1000);
        HAL_SPI_Transmit(&hspi1, gUSER_SPI1_WriteBuffer, 2048, 1000);
        
        nData[0] = 0x10;
        nData[1] = 0x00;        
        nData[2] = (naddr >> 8) & 0xFF;
        nData[3] = naddr & 0xFF;
        HAL_SPI_Transmit(&hspi1, nData, 4, 1000);
        
        
        HAL_Delay(1);
        HAL_GPIO_WritePin(W25_CS_GPIO_Port, W25_CS_Pin, GPIO_PIN_SET);
        while(1)
        {
                User_W25ReadStaus(0xC0);
                if((gUSER_SPI1_ReadBuffer[0] & 0x01) == 0)        //这里返回的数据是22
                        break;
        }
}
zhuhuis 发表于 2020-12-8 23:53 | 显示全部楼层
怎么个不对?
llljh 发表于 2020-12-8 23:56 | 显示全部楼层
能不能读取?
kingkits 发表于 2020-12-9 08:33 | 显示全部楼层
写入是要按页写入的,如果没有重新输入页地址,则写入操作只在当前页里循环
yangxiaor520 发表于 2020-12-9 08:42 | 显示全部楼层
什么不对?
734774645 发表于 2020-12-9 15:36 | 显示全部楼层
这种没头没脑的问题,谁知道啥情况。
734774645 发表于 2020-12-9 15:39 | 显示全部楼层
不对就先找个对的,然后在怼的基础改嘛
llljh 发表于 2020-12-9 17:45 | 显示全部楼层
那你就把他修改对。
houcs 发表于 2020-12-9 17:49 | 显示全部楼层
串口打印下数据,看有没有输出
 楼主| xxmmi 发表于 2020-12-9 17:53 | 显示全部楼层
也可以用ST-Link单步调试
hanwe 发表于 2020-12-9 17:58 | 显示全部楼层
关注一下status register 2里的BUF标志,BUF=1和BUF=0,对应的读指令是不同的。。芯片型号的最后一位是G或T,对应的BUF默认值不同
wangzsa 发表于 2020-12-9 18:03 | 显示全部楼层
调试状态下看看寄存器
guoyt 发表于 2020-12-9 18:34 | 显示全部楼层
怎么个不对的现象啊
zwll 发表于 2020-12-9 18:38 | 显示全部楼层
大面上看不出来的 只能用示波器卡卡 看看修改一下延时时间啥的
houcs 发表于 2020-12-9 18:41 | 显示全部楼层
对不对,你自己不知道?
 楼主| xxmmi 发表于 2020-12-9 18:44 | 显示全部楼层

哦,那我就知道怎么回事了,多谢大家
nawu 发表于 2021-1-6 12:27 | 显示全部楼层
能详细说说现象吗
qcliu 发表于 2021-1-6 12:29 | 显示全部楼层
光看这段程序看不出来什么问题
tfqi 发表于 2021-1-6 12:53 | 显示全部楼层
读操作和写操作都不行吗
wiba 发表于 2021-1-6 12:55 | 显示全部楼层
单步运行可以吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

369

主题

4278

帖子

2

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

369

主题

4278

帖子

2

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