![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Freescale FRDM-K64F development platform with external Macronix Serial NOR Flash used to perform SPI Read, Program, and Erase.
Diff: main.cpp
- Revision:
- 0:b2771f496313
- Child:
- 1:550759f64702
diff -r 000000000000 -r b2771f496313 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jul 08 20:59:56 2015 +0000 @@ -0,0 +1,236 @@ +#include <ctype.h> +#include "mbed.h" +#include "SPI_MX25R.h" +DigitalOut myled3(LED3); //Red +DigitalOut myled1(LED1); //Blue +DigitalOut led_green(LED_GREEN); + +// SPI_MX25R(PinName mosi, PinName miso, PinName sclk, PinName cs) ; +// FRDM-K64F +// D13 SPI0_SCK (PTD1) J2<12> +// D12 SPI0_MISO (PTD3) J2<10> +// D11 SPI0_MOSI (PTD2) J2<8> +// D10 SPI0_PCS0 (PTD0) J2<6> +// Vdd =3.3V = J3<4> +// Gnd =0.0V = J2<14> +SPI_MX25R spi_mem(PTD2, PTD3, PTD1, PTD0) ; + +void usage(void) +{ + printf("==== List of Commands =============================================================\n\r") ; + printf("\n\r") ; + printf("rdid (Read Manu and Dev ID, 9Fh)\n\r") ; + printf("wren (Write Enable, set WEL=1, 06h)\n\r") ; + printf("wrdi (Write Disable, set WEL=0, 04h)\n\r") ; + printf("rdsr (Read Status Register, 05h + 00h)\n\r") ; + printf("reset (ResetEnable + SoftwareReset = 66h + 99h)\n\r") ; + printf("hp (Enter HP mode = WREN=06h + WRSR= 01h 00 00 02h )\n\r") ; + printf("lp (Enter LP mode = WREN=06h + WRSR= 01h 00 00 00h )\n\r") ; + printf("rdcr (Read Config Register, 15h + 00 00h)\n\r") ; + printf("read address (read 1 byte of data, ex: read 0x0000)\n\r") ; + printf("rd4 address (read 4 bytes of data, ex: read 0x0000)\n\r") ; + printf("rd256 address (read 256 bytes of data, ex: read 0x0000)\n\r") ; + printf("pp address (256B Page Program, ex: pp 0x00 <- note: routine sets WEL)\n\r") ; + printf("se address (4KB Sector Erase, ex: be 0x00 <- note: routine sets WEL)\n\r") ; + printf("be address (64KB Block Erase, ex: be 0x00 <- note: routine sets WEL)\n\r") ; + printf("32Kbe address (32KB Block Erase, ex: 32kbe 0x00 <- note: routine sets WEL)\n\r") ; + printf("ce (Chip Erase = C7h, ex: ce <- note: routine sets WEL)\n\r") ; + printf("\n\r") ; + printf("===================================================================================\n\r") ; +} + +void rd256(unsigned long address) +{ + unsigned int i, j ; + unsigned char data[0x10] ; + + printf(" : ") ; + for (i = 0 ; i < 0x10 ; i++ ) { + printf("+%X ",i) ; + } + printf("\n\r") ; + for (j = 0 ; j < 0x10 ; j++ ) { + printf("%06X : ", address + (0x10 * j)) ; + for (i = 0 ; i < 0x10 ; i++ ) { + data[i] = spi_mem.read8(address + 0x10 * j + i) ; + printf("%02X ", data[i]) ; + } + printf(": ") ; + for (i = 0 ; i < 0x10 ; i++ ) { + if (isprint(data[i])) { + printf("%c", data[i]) ; + } else { + printf(".") ; + } + } + printf(" :\n\r") ; + } + printf("\n\r") ; +} + +int main() { + char cmd[32] ; + //int tmp = 0 ; + int i ; + int numData = 256 ; + unsigned long address = 0 ; + unsigned char data[256] ; + + extern serial_t stdio_uart ; + serial_baud(&stdio_uart, 9600) ; + + printf("\n\r") ; + printf("\n\r") ; + printf("\n\r") ; + printf("\n\r") ; +printf("\n\r ####################################################\n\r") ; + printf(" # #\n\r") ; + printf(" # Macronix MX25Rxx35F Serial NOR Flash Testbench #\n\r") ; + printf(" # #\n\r") ; + printf(" ####################################################\n\r") ; + printf("\n\r") ; + usage() ; + +myled3 = 1; +myled1 = 1; +led_green = 0; + +while(1){ + printf("> ") ; + scanf("%s", cmd) ; + if (strcmp(cmd, "read") == 0) { + scanf("%X", &address) ; + data[0] = spi_mem.read8(address) ; + printf("%01X : 0x%02X\n\r", address, data[0]) ; + + } else if (strcmp(cmd, "rd4") == 0) { + scanf("%X", &address) ; + printf(" 4B data = 0x%08X\n\r", spi_mem.rd32(address)) ; + + } else if (strcmp(cmd, "rdsr") == 0) { // Read Status Register + data[0] = spi_mem.readStatus() ; + printf(" status register = 0x%02X\n\r", data[0]) ; + printf("RDSR = 05h done\n\r") ; + led_green = 1; + + } else if (strcmp(cmd, "reset") == 0) { // ResetEnable + Software Reset + spi_mem.resetEnable() ; + spi_mem.reset() ; + printf("Software ResestEnable 66h + Reset 99h completed\n\r") ; + led_green = 1; + + } else if (strcmp(cmd, "rdcr") == 0) { // Read Configuration Register + printf(" configuration register = 0x%04X\n\r", spi_mem.readConfig()) ; + printf("RDCR = 15h done\n\r") ; + led_green = 1; + + } else if (strcmp(cmd, "rdid") == 0) { // Read ID + printf(" ID = 0x%06X\n\r", spi_mem.readID()) ; + printf("RDID = 9Fh done\n\r") ; + led_green = 1; + + } else if (strcmp(cmd, "wren") == 0) { // Write Enable + spi_mem.writeEnable() ; + printf(" WREN = 06h set\n\r") ; + led_green = 1; + + } else if (strcmp(cmd, "wrdi") == 0) { // Write Disable + spi_mem.writeDisable() ; + printf(" done\n\r") ; + + } else if (strcmp(cmd, "pp") == 0) { // Page Program + scanf("%X", &address) ; + printf("writing %d bytes to 0x%X\n\r",numData, address) ; + spi_mem.writeEnable() ; // send WREN 1st + for (i = 0 ; i < numData ; i++ ) { + data[i] = i & 0xFF ; + } + spi_mem.programPage(address, data, numData) ; + data[0] = 0 ; + while((data[0] & 0x01) != 0x01 ) { + printf(".") ; + data[0] = spi_mem.readStatus() ; + wait(0.01) ; + } + printf("\n\r") ; + printf(" PP = 02h done\n\r") ; + led_green = 1; + + } else if (strcmp(cmd, "ce") == 0) { // Chip Erase + spi_mem.writeEnable() ; // send WREN 1st + spi_mem.chipErase() ; + data[0] = 0x01 ; // poll if WIP bit SR<0>=1 + while((data[0] & 0x01) != 0 ) { + printf("wait, chip erase in progress") ; + data[0] = spi_mem.readStatus() ; + printf(" --> status register = 0x%02X\n\r", data[0]) ; + wait(5) ; + } // end poll + printf("CE = C7h done\n\r") ; + led_green = 0; + + } else if (strcmp(cmd, "se") == 0) { // Sector Erase + scanf("%X", &address) ; + printf("Eraseing address 0x%X\n\r", address) ; + spi_mem.writeEnable() ; // send WREN 1st + spi_mem.sectorErase(address) ; + data[0] = 0x01 ; // poll if WIP bit SR<0>=1 + while((data[0] & 0x01) != 0 ) { + printf(".") ; + data[0] = spi_mem.readStatus() ; + wait(0.1) ; + } // end poll + printf("SE = 20h done\n\r") ; + led_green = 0; + + } else if (strcmp(cmd, "be") == 0) { // 64KB Block Erase + scanf("%X", &address) ; + printf("Eraseing address 0x%X\n\r", address) ; + spi_mem.writeEnable() ; // send WREN 1st + spi_mem.blockErase(address) ; + data[0] = 0x01 ; // poll if WIP bit SR<0>=1 + while((data[0] & 0x01) != 0 ) { + printf(".") ; + data[0] = spi_mem.readStatus() ; + wait(0.1) ; + } // end poll + printf("BE = D8h done\n\r") ; + led_green = 0; + + } else if (strcmp(cmd, "32kbe") == 0) { // 32KB Block Erase + scanf("%X", &address) ; + printf("Eraseing address 0x%X\n\r", address) ; + spi_mem.writeEnable() ; // send WREN 1st + spi_mem.blockErase(address) ; + data[0] = 0x01 ; // poll if WIP bit SR<0>=1 + while((data[0] & 0x01) != 0 ) { + printf(".") ; + data[0] = spi_mem.readStatus() ; + wait(0.1) ; + } // end poll + printf("32KBE = 52h done\n\r") ; + led_green = 0; + + } else if (strcmp(cmd, "hp") == 0) { // Enter High Performance Mode + spi_mem.writeEnable() ; // send WREN 1st + spi_mem.hpmode() ; + printf(" You are now in High Performance mode\n\r") ; + led_green = 0; + + } else if (strcmp(cmd, "lp") == 0) { // Enter Low Power Mode + spi_mem.writeEnable() ; // send WREN 1st + spi_mem.lpmode() ; + printf(" You are now in Low Power mode\n\r") ; + led_green = 0; + + } else if (strcmp(cmd, "rd256") == 0) { + scanf("%X", &address) ; + rd256(address) ; + } else if (strcmp(cmd, "help") == 0) { + usage() ; + } else { + printf("Command NOT Recognized [ %s ]\n\r", cmd) ; + usage() ; + } + } +} \ No newline at end of file