Sonder Design Team / Memory

Memory.cpp

Committer:
ThomasSonderDesign
Date:
2016-12-22
Revision:
6:c11cb6709b8b
Parent:
2:5c70c0334ed0
Child:
7:9d6961dcb7bb
Child:
8:6af25b9563df

File content as of revision 6:c11cb6709b8b:

#include "mbed.h"
#include "Memory.h"


Memory::Memory(PinName chipSelect) : _cs_mem(chipSelect)
{
    _cs_mem=1;
}
/**
 * Reads 'length' elements into a char array starting at the 24bit Address.
 *If length is greater than BufferSize (3840 bytes) the function will terminate
 *and return the start address.
 */
int Memory::readData(SPI my_spi, char value [], int Address, int length)
{
    if(length>bufferSize) {
        printf("\nLength %i exceeds Max Length\n",length);
        return Address;
    }
    _cs_mem = 1;             //Ensure cs is deselected
    wait_us(10);
    _cs_mem = 0;                     //memory is selected
    my_spi.write(0x03);         //Send read command
    my_spi.write(Address>>16);  //Send high address byte
    my_spi.write(Address>>8);   //Send mid address byte
    my_spi.write(Address);      //Send low address byte


    for(int i =0; i <length; i++) {
        value[i]= my_spi.write(dummy);//Send dummy byte to read out value ate Address
        Address++;
    }
    _cs_mem = 1;
    return Address;     //Return the address of the next unread byte
}


/**
 * Sector  Erase, erases everything in the 4KB sector that includes Address
 */
void Memory::sectorErase(SPI my_spi, long Address)
{
    _cs_mem = 1;
    _cs_mem=0;
    my_spi.write(0x06);     //Send Write enable command
    _cs_mem= 1;
    wait_us(5);
    _cs_mem=0;
    my_spi.write(0x20);     //Send sector erase comand
    my_spi.write(Address>>16);  //Send high address byte
    my_spi.write(Address>>8);   //Send mid address byte
    my_spi.write(Address);      //Send low address byte
    _cs_mem=1;
    wait_us(5);

    //Pol the status register untill the Write In Progress bit is no longer set
    _cs_mem=0;
    my_spi.write(05);
    int byte1 = my_spi.write(dummy);
    while(byte1>0) {
        byte1 = my_spi.write(dummy);
    }
    _cs_mem=1;
}


/**
 * Block  Erase, erases everything in a 4KB block that includes Address
 */
int Memory::blockErase(SPI my_spi, int Address)
{
    _cs_mem = 1;
    _cs_mem=0;
    my_spi.write(0x06);     //Send Write enable command
    _cs_mem= 1;
    wait_us(5);
    _cs_mem=0;
    my_spi.write(0xD8);     //Send sector erase comand
    my_spi.write((Address>>16)&0xff);  //Send high address byte
    my_spi.write((Address>>8)&0xff);   //Send mid address byte
    my_spi.write((Address)&0xff);      //Send low address byte
    _cs_mem=1;
    wait_us(5);

    //Pol the status register untill the Write In Progress bit is no longer set
    _cs_mem=0;
    my_spi.write(05);
    int byte1 = my_spi.write(dummy);
    while(byte1>0) {
        byte1 = my_spi.write(dummy);
    }
    _cs_mem=1;
    int returnVal = (Address/0x10000);
    returnVal*=0x10000;
    returnVal+=0x10000;
    return returnVal;
}

/**
 * Writes a char array containg 'length' elements to memory sarting at address.
 *If length is greater than BufferSize (3840 bytes) the function will terminate
 *and return the start address.
 */
int Memory::writeData(SPI my_spi, char buffer[], int address, int length)
{
    if(length>bufferSize) {
        printf("\nLength %i exceeds Max Length\n",length);
        return address;
    }
    for(int i =0; i<length; i++) {
        if(address%256==0) {                //Handle start and end of pages
            _cs_mem=1;
            wait_us(10);
            //wait for the WIP bit to go low
            _cs_mem=0;                           //Selet memory
            my_spi.write(0x05);             //Send read status register command
            int byte1 = my_spi.write(dummy);//Send dummy byte to read status reg
            while ((byte1&1)>0) {
                wait_us(10);
                my_spi.write(0x05);         //Send read status register command
                byte1 = my_spi.write(dummy);//Send dummy byte to read status reg
            }
            _cs_mem=1;

            _cs_mem=0;                           //Selet memory
            my_spi.write(06);               //Set Write Enable flag in the status reg
            _cs_mem=1;
            wait_us(10);

            _cs_mem=0;                           //Selet memory
            my_spi.write(02);               //Send read comand
            my_spi.write(address>>16);      //Send high address byte
            my_spi.write(address>>8);       //Send middle adress byte
            my_spi.write(address);          //Send low address

        }
        my_spi.write(buffer[i]);            //Write the calue of the buffer to memory
        //printf("%i%i%i%i%i%i%i%i",sixtyBytes[i]>>7&&1,buffer[i]>>6&&1,buffer[i]>>5&&1,buffer[i]>>4&&1,buffer[i]>>3&&1,buffer[i]>>2&&1,buffer[i]>>1&&1,buffer[i]&&1);
            
        wait_us(5);
        address=address++;                  //Increment address
    }
    _cs_mem=1;
    return address;
}