Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
rm25c512cl.cpp
- Committer:
- ianaherne
- Date:
- 2018-09-12
- Revision:
- 3:e846980a1536
- Parent:
- 2:183d1d96f917
- Child:
- 4:17e602b21ac5
File content as of revision 3:e846980a1536:
#include "rm25c512cl.h"
rm25c512cl::rm25c512cl(PinName MOSI,PinName MISO,PinName SCLK,PinName CS): _spi(MOSI,MISO,SCLK),_cs(CS){
_cs = 1; // always set chip select to initial state of high
}
/**
* @brief write_enable()
* @details Sets write enable latch to 1 to enable writing to eeprom uses the WREN opcode
* @param NA
* @return NA
* @warning Write or erase instructions will be ignored if this instruction is not issued first
*
*/
void rm25c512cl :: write_enable(){
char cmd[1];
char data_buffer[1];// not used here but required for spi.write() as a parameter
cmd[0] = WREN;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],1,&data_buffer[0],0);
_cs = 1;
_spi.unlock();
wait_us(20);
}
/**
* @brief read_status_reg()
* @details Reads the status register of EEprom using the RDSR opcode, the register
* read can be used to determine if a write has completed, or the write protection status
* @param NA
* @return Current eight bit value of register
* @warning
*
*/
char rm25c512cl :: read_status_reg(){
char cmd[1];
char data_buffer[1]; // to store value returned from EEprom status register
cmd[0] = RDSR;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],1,&data_buffer[0],0);
cmd[0] = 0xFF; // write garbage to read something back
_spi.write(&cmd[0],1,&data_buffer[0],1);
_cs = 1;
_spi.unlock();
wait_us(20);
return data_buffer[0];
}
/**
* @brief write_bytes()
* @details Writes multiple bytes to the specified address given.
* @param Starting address to write to.
* @param Pointer to the data array containg bytes to be stored.
* @param Size of the data array in bytes.
* @return NA
* @warning A page write(128 bytes) can take up to 5 ms to complete
*
*/
void rm25c512cl :: write_bytes(uint16_t address, char* data, uint16_t data_size){
char cmd[3];
char data_buffer[1];// not used here but required for spi.write() as a parameter
cmd[0] = WR;
cmd[1] = address >> 8;
cmd[2] = address & 0x00FF;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],3,&data_buffer[0],0);
_spi.write(&data[0],data_size,&data_buffer[0],0);
_cs = 1;
_spi.unlock();
wait_ms(5);
}
/**
* @brief write_byte()
* @details Writes a single byte to EEprom at the given address
* @param Address to write to
* @param Value to write in given address
* @return NA
* @warning Byte write can take up to 100 us to complete
*
*/
void rm25c512cl :: write_byte(uint16_t address,char value){
char cmd[4];
char data_buffer[1]; // not used here but required for spi.write() as a parameter
cmd[0] = WR;
cmd[1] = address >> 8;
cmd[2] = address & 0x00FF;
cmd[3] = value;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],4,&data_buffer[0],0);
_cs =1;
_spi.unlock();
wait_us(100);
}
/**
* @brief read_bytes()
* @details Reads multiple bytes starting at given address.
* @param Page Address to start reading from.
* @param Pointer to data buffer to store multiple bytes read from EEprom.
* @param Size of the data buffer in bytes.
* @return
* @warning Data buffer must be of size 128 bytes to read entire page.
*
*/
void rm25c512cl :: read_bytes(uint16_t address, char* data_buffer, uint16_t data_buffer_size){
char cmd[3];
cmd[0] = READ;
cmd[1] = address >> 8;
cmd[2] = address & 0x00FF;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],3,&data_buffer[0],0);
cmd[0] = 0xFF;
_spi.write(&cmd[0],1,&data_buffer[0],data_buffer_size);
_cs = 1;
_spi.unlock();
}
/**
* @brief read_byte()
* @details Reads a byte of data at given address.
* @param Address to read byte from.
* @return Byte of data read from address.
* @warning
*
*/
char rm25c512cl :: read_byte(uint16_t address){
char cmd[3];
char data_buffer[1];// To store data byte returned from EEprom
cmd[0] = READ;
cmd[1] = address >> 8;
cmd[2] = address & 0x00FF;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],3,&data_buffer[0],0);
cmd[0] = 0xFF;
_spi.write(&cmd[0],1,&data_buffer[0],1);
_cs = 1;
_spi.unlock();
return data_buffer[0];
}
/**
* @brief page_erase()
* @details 128 byte erase at page address
* @param Page address
* @return NA
* @warning write enable must be called prior to calling this function or or
* instruction will be ignored.
*
*/
void rm25c512cl :: page_erase(uint16_t address){
char cmd[3];
char data_buffer[1];// not used here but required as function parameter
cmd[0] = PERS;
cmd[1] = address >> 8;
cmd[2] = address & 0x00FF;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],3,&data_buffer[0],0);
_cs = 1;
_spi.unlock();
}
/**
* @brief chip_erase()
* @details erases all memory on chip
* @param NA
* @return NA
* @warning write enable must be called prior to calling this function or or
* instruction will be ignored.
*
*/
void rm25c512cl :: chip_erase(){
char cmd[1];
char data_buffer[1];// not used here but required as function parameter
cmd[0] = CERS;
_spi.lock();
_cs = 0;
_spi.write(&cmd[0],1,&data_buffer[0],0);
_cs = 1;
_spi.unlock();
}