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.
Diff: rm25c512cl.cpp
- Revision:
- 7:c562fe4d48de
- Parent:
- 6:0434e89c2d68
- Child:
- 8:d71c8068ace2
--- a/rm25c512cl.cpp Thu Sep 13 12:17:25 2018 +0000
+++ b/rm25c512cl.cpp Thu Sep 13 16:14:06 2018 +0000
@@ -84,7 +84,7 @@
_spi.unlock();
- wait_us(20);
+
return data_buffer[0];
@@ -97,7 +97,7 @@
* @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 Sucess or failure of write
+* @return if a timeout has occured i.e. device is busy for too long
* @warning A page write(128 bytes) can take up to 5 ms to complete
*
*/
@@ -134,16 +134,16 @@
i--;
- }while(status == 1 && i>0);
+ }while(status == BUSY && i>0);
/* if timeout elapses*/
- if( i < 1){
+ if( i < 1 && status == BUSY){
- return FAILURE;
+ return TIMEOUT_ERROR;
}
else {
- return SUCCESS;
+ return TIMEOUT_OK;
}
}
@@ -153,15 +153,17 @@
* @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
+* @return if a timeout has occured i.e. device is busy for too long
* @warning Byte write can take up to 100 us to complete
*
*/
-void rm25c512cl :: write_byte(uint16_t address,char value){
+bool 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
+ char status;
+ uint32_t i = TIMEOUT_BYTE;
cmd[0] = WR;
cmd[1] = address >> 8;
@@ -178,7 +180,28 @@
_spi.unlock();
- wait_us(100);
+ /* keeps checking the WEL bit i status reg to make sure write completes*/
+
+ do{
+
+ status = read_status_reg();
+ status = status & 0x1;
+
+ i--;
+
+ }while(status == BUSY && i>0);
+
+ /* if timeout elapses*/
+
+ if( i < 1 && status == BUSY){
+
+ return TIMEOUT_ERROR;
+ }
+ else {
+ return TIMEOUT_OK;
+ }
+
+
}
@@ -260,17 +283,19 @@
* @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
+* @return if a timeout has occured i.e. device is busy for too long.
+* @warning Write enable must be called prior to calling this function or
* instruction will be ignored.
*
*/
-void rm25c512cl :: page_erase(uint16_t address){
+bool rm25c512cl :: page_erase(uint16_t address){
char cmd[3];
char data_buffer[1];// not used here but required as function parameter
+ char status;
+ uint32_t i = TIMEOUT_BYTES;
cmd[0] = PERS;
cmd[1] = address >> 8;
@@ -286,24 +311,45 @@
_spi.unlock();
-
+ /* keeps checking the WEL bit i status reg to make sure erase completes*/
+
+ do{
+
+ status = read_status_reg();
+ status = status & 0x1;
+
+ i--;
+
+ }while(status == BUSY && i>0);
+ /* if timeout elapses*/
+
+ if( i < 1 && status == BUSY){
+
+ return TIMEOUT_ERROR;
+ }
+ else {
+ return TIMEOUT_OK;
+ }
+
}
/**
* @brief chip_erase()
* @details erases all memory on chip
* @param NA
-* @return NA
+* @return if a timeout has occured i.e. device is busy for too long
* @warning write enable must be called prior to calling this function or or
-* instruction will be ignored.
+* instruction will be ignored.
*
*/
-void rm25c512cl :: chip_erase(){
+bool rm25c512cl :: chip_erase(){
char cmd[1];
char data_buffer[1];// not used here but required as function parameter
+ char status;
+ uint32_t i = TIMEOUT_BYTES;
cmd[0] = CERS;
@@ -317,6 +363,27 @@
_spi.unlock();
+ /* keeps checking the WEL bit i status reg to make sure erase completes*/
+
+ do{
+
+ status = read_status_reg();
+ status = status & 0x1;
+
+ i--;
+
+ }while(status == BUSY && i>0);
+
+ /* if timeout elapses*/
+
+ if( i < 1 && status == BUSY){
+
+ return TIMEOUT_ERROR;
+ }
+ else {
+ return TIMEOUT_OK;
+ }
+
}