Hello program for the FM25W256 library

Dependencies:   FM25W256 mbed

main.cpp

Committer:
MACRUM
Date:
2016-03-04
Revision:
0:3efb18460968
Child:
1:be908b1aafe6

File content as of revision 0:3efb18460968:

#include "mbed.h"

Serial pc(USBTX, USBRX);
SPI fram(dp2, dp1, dp6);
DigitalOut _cs(dp18);

#define CMD_WREN   0x06
#define CMD_WRDI   0x04
#define CMD_RDSR   0x05
#define CMD_WRSR   0x01
#define CMD_READ   0x03
#define CMD_WRITE  0x02

void write_byte(uint16_t address, uint8_t data)
{
    _cs = 0;
    fram.write(CMD_WREN);
    _cs = 1;

    _cs = 0;
    fram.write(CMD_WRITE);
    fram.write(address >> 8);
    fram.write(address & 0xFF);
    fram.write(data & 0xFF);
    _cs = 1;
}

uint8_t read_byte(uint16_t address)
{
    uint8_t data;

    _cs = 0;
    fram.write(CMD_READ);
    fram.write(address >> 8);
    fram.write(address & 0xFF);
    data = fram.write(0);
    _cs = 1;

    return data;
}

void read_bytes(uint16_t address, uint8_t *buf, uint16_t size)
{
    _cs = 0;
    fram.write(CMD_READ);
    fram.write(address >> 8);
    fram.write(address & 0xFF);
    while (size--) {
        *buf++ = fram.write(0);
    }
    _cs = 1;
}


void read_test()
{
    uint16_t adrs = 0;
    for(int i=0; i<16; i++) {
        pc.printf("0x%04X : ", i * 16);
        for(int j=0; j<16; j++) {
            //pc.printf("%02X ", fram.write(0));
            pc.printf("%02X ", read_byte(adrs++));
        }
        pc.printf("\n");
    }
}
    
void write_test()
{
    for(int i=0; i<256; i++) {
        write_byte(i, i);
    }
}

int main()
{
    fram.format(8, 0);
    fram.frequency(20000000);
    _cs = 1;

    pc.printf("\nFM25W256 test program - write test? (y/n) ");
    if (pc.getc() == 'y') {
        write_test();
    }

    pc.printf("\nFM25W256 test program - read test? (y/n) ");
    if (pc.getc() == 'y') {
        pc.printf("\n");
        read_test();
    }

    while(1) {
    }
}