/* * Simple norFlash driver * Copyright (C) 2006 Rick Huang * Revision History: * First release: Dec 9, 2006 * * This program is free software; you can redistribute it and/or modify * anyway you wish, as long as this statement is kept intact. * */ #include "uart.h" #include "spi.h" #include "gpio.h" /* Return 0x80 for no error */ /* bit 6 - Erase suspend * bit 5, 4 - 01 - Program error * 10 - Erase error * 11 - Cmd sequence error * bit 3 - Vpen error * bit 2 - Program suspend * bit 1 - Block lock */ char SF_ReadStatusReg() { char a; S2W_WriteByte(0x10000000, 0x70); // SF read status command a = S2W_ReadByte(0x10000000); S2W_WriteByte(0x10000000, 0xFF); // Read Array mode, normal operation return a; } /* If return != 0x80, error has occured */ /* bit 6 - Erase suspend * bit 5, 4 - 01 - Program error * 10 - Erase error * 11 - Cmd sequence error * bit 3 - Vpen error * bit 2 - Program suspend * bit 1 - Block lock */ char SF_ProgramChar(unsigned addr, char data) { char a; int i; S2W_WriteByte(addr + 0x10000000, 0x10); // SF single byte program S2W_WriteByte(addr + 0x10000000, data); // SF single byte program for(i=0;i<1000;i++) { a = S2W_ReadByte(addr + 0x10000000); // Read status; if(a == 0x80)break; // FLASH is ready } S2W_WriteByte(addr + 0x10000000, 0xff); // SF read array mode return a; } /* If return != 0x80, error has occured */ /* Each block is 128K byte, 0x0 ~ 0x1ffff */ char SF_BlockErase(unsigned addr) { char a; int i; S2W_WriteByte(addr + 0x10000000, 0x20); // Erase block, command 1 S2W_WriteByte(addr + 0x10000000, 0xd0); // Erase block, command 2 for(i=0;i<10000;i++) // Erase can take up to 3 sec { Delay500uS(); a = S2W_ReadByte(addr + 0x10000000); // Read status; if(a == 0x80)break; // FLASH is ready } S2W_WriteByte(addr + 0x10000000, 0xff); // SF read array mode return a; } void TestWrite() { int i; for(i=0;i<255;i++) { SF_ProgramChar(0x400000+i, i); } } void DumpROM() { char a; unsigned addr; UART1_puts("\r\n"); for(addr=0x11000000; addr<0x10000000 ; addr++) { a = S2W_ReadByte(addr); UART1_putch(a); } }