/********************************************************************************************************* ** Function name : flash_write_enable ** Descriptions : Enable the flash memory to write ** Input parameters : none ** Output parameters : none ** Returned value : none *********************************************************************************************************/ static void flash_write_enable (void) { while (flash_read_status() & 0x01 != 0x00); /* Wait for flash free */ SPI_FLASH_CS_LOW(); /* P0.2--0,CS = 0 选中SPI Flash */ Send_Byte(0x06); SPI_FLASH_CS_HIGH(); /* P0.2--1,CS = 1 释放SPI Flash */ while (flash_read_status() & 0x03 != 0x02); /* Wait for operation complete */ }
/********************************************************************************************************* ** Function name : flash_write_sector ** Descriptions : Write flash memory , just in one page memory ** Input parameters : WAddr -- the start address to write ** Output parameters : buf -- the buffer to write the data ** RLength -- the length of the data to write ** Returned value : The operation result. 1 -- sucess, 0 -- false *********************************************************************************************************/ uint8_t flash_write_sector (uint32_t WAddr, uint8_t *buf, uint32_t WLength) { uint32_t i; if (WLength == 0) { return 0; } flash_write_enable(); /* Write enable */ SPI_FLASH_CS_LOW(); /* P0.2--0,CS = 0 选中SPI Flash */ Send_Byte(0x02); Send_Byte((WAddr & 0xFF0000) >> 16); Send_Byte((WAddr & 0x00FF00) >> 8); Send_Byte((WAddr & 0x0000FF)); for (i=0; i<WLength; i++) { Send_Byte(buf[i]); } SPI_FLASH_CS_HIGH(); /* P0.2--1,CS = 1 释放SPI Flash */ while (flash_read_status() & 0x01 != 0x00); return 1; }
/********************************************************************************************************* ** Function name : flash_read_data ** Descriptions : Read flash memory ** Input parameters : RAddr -- the start address to read ** Output parameters : buf -- the buffer to receive the read data ** RLength -- the length of the data to read ** Returned value : The operation result. 1 -- sucess, 0 -- false *********************************************************************************************************/ uint8_t flash_read_data (uint32_t RAddr, uint8_t *buf, uint32_t RLength) { uint8_t Temp; uint32_t i; if (RLength == 0) { return 0; } /* * Check the state register. If it's busy , wait until it's free */ while(1) { Temp = flash_read_status( ); Temp &= 0x01; if(Temp == 0x00) break; for(i=0; i<10; i++); } SPI_FLASH_CS_LOW(); /* P0.2--0,CS = 0 选中SPI Flash */ Send_Byte(0x03); Send_Byte((RAddr & 0xFF0000) >> 16); Send_Byte((RAddr & 0x00FF00) >> 8); Send_Byte((RAddr & 0x0000FF)); for (i=0; i<RLength; i++) { buf[i] = Send_Byte(0xff); } SPI_FLASH_CS_HIGH(); /* P0.2--1,CS = 1 释放SPI Flash */ return 1; }
/********************************************************************************************************* ** Function name : flash_all_erase ** Descriptions : Erase the whole flash ** Input parameters : None ** Output parameters : None ** Returned value : The operation result. 1 -- sucess, 0 -- false *********************************************************************************************************/ uint8_t flash_whole_erase( void ) { flash_write_enable(); /* Write enable */ SPI_FLASH_CS_LOW(); /* P0.2--0,CS = 0 选中SPI Flash */ Send_Byte(0xC7); SPI_FLASH_CS_HIGH(); /* P0.2--1,CS = 1 释放SPI Flash */ while (flash_read_status() & 0x01 != 0x00); /* Wait for the flash free */ return 1; }
int flash_read_registers(uint8_t *buf) { int ret = 0; ret = flash_read_status(buf); if (ret == 0) { ret = flash_read_config(&(buf[1])); } if (ret == 0) { ret = flash_read_status2(&(buf[2])); } return ret; }
uint8_t flash_write_word (uint32_t WAddr, uint16_t *buf, uint32_t WLength) { uint32_t i; if (WLength == 0) { return 0; } if(WLength%2!=0) { return 0; } WLength/=2; WREN(); SPI_FLASH_CS_LOW(); Send_Byte(SPI_FLASH_CMD_SST_AAI_WORD_PROGRAM); /* send AAI command */ Send_Byte(((WAddr & 0xFFFFFF) >> 16)); /* send 3 address bytes */ Send_Byte(((WAddr & 0xFFFF) >> 8)); Send_Byte(WAddr & 0xFF); for (i=0; i<WLength; i++) { // APP_TRACE("%d ",i); Send_Byte(buf[i] & 0xFF); Send_Byte(((buf[i] & 0xFFFF) >> 8)); SPI_FLASH_CS_HIGH(); while (flash_read_status() & 0x01 != 0x00); if(i<WLength-1) { SPI_FLASH_CS_LOW(); Send_Byte(SPI_FLASH_CMD_SST_AAI_WORD_PROGRAM); /* send AAI command */ } } WRDI(); while (flash_read_status() & 0x01 != 0x00); return 1; }
// wait until the flash program/erase operation is finished static int flash_wait_until_done(uint32_t timeout_ms) { int ret; while (timeout_ms-- > 0) { uint8_t status; ret = flash_read_status(&status); if (ret != 0 || (status & STATUS_BUSY) == 0) { return ret; } flash_os_sleep_ms(1); } return -1; }
/********************************************************************************************************* ** Function name : flash_sector_erase ** Descriptions : Sector erase ** Input parameters : addr -- sector address ** Output parameters : None ** Returned value : The operation result. 1 -- sucess, 0 -- false *********************************************************************************************************/ uint8_t flash_sector_erase (uint32_t addr) { flash_write_enable(); /* Write enable */ SPI_FLASH_CS_LOW(); /* P0.2--0,CS = 0 选中SPI Flash */ Send_Byte(0x20); Send_Byte((addr & 0xFF0000) >> 16); Send_Byte((addr & 0x00FF00) >> 8); Send_Byte(addr & 0x0000FF); SPI_FLASH_CS_HIGH(); /* P0.2--1,CS = 1 释放SPI Flash */ while (flash_read_status() & 0x01 != 0x00); /* Wait for the flash free */ return 1; }
void flash_init( void ) { DDRB |= MOSI | SCK | CS; flash_read_status( ); }
void flash_wait_complete(){ while(flash_read_status() & 0x1); }