void SST25_WriteFlash2(u32 addr, u8 *readbuff, u16 length) { u32 i = 0; Write_CMD(); Write_EN(); Select_Flash(); SPI_Flash_SendByte(0xad); SPI_Flash_SendByte((addr&0xffffff)>>16); SPI_Flash_SendByte((addr&0xffff)>>8); SPI_Flash_SendByte(addr&0xff); SPI_Flash_SendByte(readbuff[0]); SPI_Flash_SendByte(readbuff[1]); NotSelect_Flash(); i = 2; while (i < length) { Select_Flash(); SPI_Flash_SendByte(0xad); SPI_Flash_SendByte(readbuff[i++]); SPI_Flash_SendByte(readbuff[i++]); NotSelect_Flash(); } Write_DIS(); Select_Flash(); Busy_Test(); }
void FlashPageWrite(u16 page,u8 *Data) //写一整页,页范围0-4095 { unsigned int i; FlashWaitBusy(); Select_Flash(); SPI_Flash_SendByte(BUFFER_2_WRITE); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); for (i = 0;i < 528; i++) { SPI_Flash_SendByte(Data[i]); } NotSelect_Flash(); if ( page < 4096) { Select_Flash(); SPI_Flash_SendByte(B2_TO_MM_PAGE_PROG_WITH_ERASE); SPI_Flash_SendByte((u8)(page>>6)); SPI_Flash_SendByte((u8)(page<<2)); SPI_Flash_SendByte(0x00); NotSelect_Flash(); FlashWaitBusy(); }
void Write_CMD(void) { Select_Flash(); // SPI CS 使能 SPI_Flash_SendByte(0x50); NotSelect_Flash(); // SPI CS 禁用 Select_Flash(); // SPI CS 使能 SPI_Flash_SendByte(0x01); SPI_Flash_SendByte(0x00); NotSelect_Flash(); // SPI CS 禁用 Busy_Test(); // 忙检测 }
void Write_DIS(void) { Select_Flash(); // SPI CS 使能 SPI_Flash_SendByte(0x04); // 写禁止指令 NotSelect_Flash(); // SPI CS 禁用 Busy_Test(); // 忙检测 }
void FlashReadID(u8 *Data) { u8 i; Select_Flash(); SPI_Flash_SendByte(0x9F); for(i = 0; i < 4; i++) { Data[i] = SPI_Flash_ReadByte(); } NotSelect_Flash(); }
void FlashPageEarse(u16 page) { FlashWaitBusy(); Select_Flash(); SPI_Flash_SendByte(PAGE_ERASE); SPI_Flash_SendByte((u8)(page >> 6)); SPI_Flash_SendByte((u8)(page << 2)); SPI_Flash_SendByte(Dummy_Byte); //|-23-|-22-|21|20|19|18|17|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0| //|2个无关位|------------12位页地址-------------|----10个无关位-----| NotSelect_Flash(); }
void Page_Clr(u32 a1) { Write_CMD(); Write_EN(); Select_Flash(); SPI_Flash_SendByte(0x20); SPI_Flash_SendByte((a1&0xffffff)>>16); SPI_Flash_SendByte((a1&0xffff)>>8); SPI_Flash_SendByte(a1&0xff); NotSelect_Flash(); Busy_Test(); }
void FlashReadID(void) { Select_Flash(); SPI_Flash_SendByte(0x90); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); fac_id = SPI_Flash_ReadByte(); // BFH: 工程码SST dev_id = SPI_Flash_ReadByte(); // 41H: 器件型号SST25VF016B NotSelect_Flash(); }
u8 Read_CMD(void) { u8 busy; Select_Flash(); SPI_Flash_SendByte(0x05); busy = SPI_Flash_ReadByte(); NotSelect_Flash(); return (busy); }
void SST25_WriteFlash(u32 addr, u8 *readbuff, u16 length) { u32 i = 0, a2; Page_Clr(addr); // 删除页 Write_CMD(); Write_EN(); Select_Flash(); SPI_Flash_SendByte(0xad); SPI_Flash_SendByte((addr&0xffffff)>>16); SPI_Flash_SendByte((addr&0xffff)>>8); SPI_Flash_SendByte(addr&0xff); SPI_Flash_SendByte(readbuff[0]); SPI_Flash_SendByte(readbuff[1]); NotSelect_Flash(); i = 2; while (i < length) { a2 = 120; while (a2 > 0) a2--; Select_Flash(); SPI_Flash_SendByte(0xad); SPI_Flash_SendByte(readbuff[i++]); SPI_Flash_SendByte(readbuff[i++]); NotSelect_Flash(); } a2 = 100; while (a2 > 0) a2--; Write_DIS(); Select_Flash(); Busy_Test(); }
void FlashPageRead(u16 page,u8 *Data) { u16 i; FlashWaitBusy(); Select_Flash(); SPI_Flash_SendByte(PAGE_READ); SPI_Flash_SendByte((u8)(page >> 6)); SPI_Flash_SendByte((u8)(page << 2)); SPI_Flash_SendByte(0x00);//3个字节 SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); for (i = 0;i < 528; i++) { Data[i] = SPI_Flash_ReadByte(); } NotSelect_Flash(); }
void SST25_ReadFlash(u32 addr, u8 *readbuff, u16 length) { u32 i = 0; Select_Flash(); SPI_Flash_SendByte(0x0b); SPI_Flash_SendByte((addr&0xffffff)>>16); SPI_Flash_SendByte((addr&0xffff)>>8); SPI_Flash_SendByte(addr&0xff); SPI_Flash_SendByte(0); while (i < length) { readbuff[i] = SPI_Flash_ReadByte(); i++; } NotSelect_Flash(); }
/******************************************************************************* * Function Name : SPI_FLASH_Init * Description : Initializes the peripherals used by the SPI FLASH driver. * Input : None * Output : None * Return : None *******************************************************************************/ void SPI_Flash_Init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI1 GPIOB clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Configure SPI1 pins: SCK, MISO and MOSI */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure PA.12 as Output push-pull, used as Flash Chip select */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Deselect the FLASH: Chip Select high */ NotSelect_Flash(); /* SPI1 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(SPI1, ENABLE); }
void Write_EN(void) { Select_Flash(); // SPI CS 使能 SPI_Flash_SendByte(0x06); // 写使能指令 NotSelect_Flash(); // SPI CS 禁用 }
void SST25_Init(void) { MYSPI_Init(); NotSelect_Flash(); }