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;
}
} |
|