Basic read/write from flash.

Dependencies:   mbed-modifed

Committer:
Dilsher
Date:
Mon Nov 09 19:52:31 2015 +0000
Revision:
3:50a42705f3ea
Parent:
2:118a2f154b78
Created struct for reading

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptcrews 0:cb78df310c5f 1 #include "mbed.h"
ptcrews 0:cb78df310c5f 2
ptcrews 0:cb78df310c5f 3 //define where the EEPROM begins
ptcrews 0:cb78df310c5f 4 #define stadr 0x08080000
ptcrews 0:cb78df310c5f 5
ptcrews 0:cb78df310c5f 6 Serial pcSerial(USBTX, USBRX);
ptcrews 0:cb78df310c5f 7
ptcrews 0:cb78df310c5f 8 //Our current offset in the EEPROM
ptcrews 0:cb78df310c5f 9 int offset = 0;
Dilsher 1:397846763432 10 int roffset = 0;
ptcrews 0:cb78df310c5f 11
Dilsher 3:50a42705f3ea 12 struct reading {
Dilsher 3:50a42705f3ea 13 float latitude; //Signed positive if N, negative if S
Dilsher 3:50a42705f3ea 14 float longitude; //Signed positive if W, negative if E
Dilsher 3:50a42705f3ea 15 float temperature;
Dilsher 3:50a42705f3ea 16 float pH;
Dilsher 3:50a42705f3ea 17 uint8_t day;
Dilsher 3:50a42705f3ea 18 uint8_t month;
Dilsher 3:50a42705f3ea 19 uint8_t year;
Dilsher 3:50a42705f3ea 20 uint8_t hour;
Dilsher 3:50a42705f3ea 21 uint8_t minutes;
Dilsher 3:50a42705f3ea 22 };
Dilsher 3:50a42705f3ea 23
ptcrews 0:cb78df310c5f 24 //This function writes a byte of data to the EEPROM at the appropriate location.
ptcrews 0:cb78df310c5f 25 //This is called by my writeEEPROMbytes.
ptcrews 0:cb78df310c5f 26 //Use this if you want to write a single byte.
ptcrews 0:cb78df310c5f 27 HAL_StatusTypeDef writeEEPROMByte(uint8_t data)
ptcrews 0:cb78df310c5f 28 {
ptcrews 0:cb78df310c5f 29 HAL_StatusTypeDef status;
ptcrews 0:cb78df310c5f 30 int address = offset + stadr;
ptcrews 0:cb78df310c5f 31 offset++;
ptcrews 0:cb78df310c5f 32 HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing
ptcrews 0:cb78df310c5f 33 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
ptcrews 0:cb78df310c5f 34 HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM
ptcrews 0:cb78df310c5f 35 return status;
ptcrews 0:cb78df310c5f 36 }
ptcrews 0:cb78df310c5f 37
ptcrews 0:cb78df310c5f 38 //This function takes an array of bytes and its size and writes them to the EEPROM
ptcrews 0:cb78df310c5f 39 //Use this if you want to write a lot of bytes.
ptcrews 0:cb78df310c5f 40 void writeEEPROMbytes(uint8_t* data, uint8_t size)
ptcrews 0:cb78df310c5f 41 {
ptcrews 0:cb78df310c5f 42 for(int i = 0; i < size; i++)
ptcrews 0:cb78df310c5f 43 {
ptcrews 0:cb78df310c5f 44 writeEEPROMByte(data[i]);
ptcrews 0:cb78df310c5f 45 }
ptcrews 0:cb78df310c5f 46 }
ptcrews 0:cb78df310c5f 47
ptcrews 0:cb78df310c5f 48 //This function reads a byte of data from the EEPROM at the offset passed in.
ptcrews 0:cb78df310c5f 49 //This is called by my getEEPROM function
ptcrews 0:cb78df310c5f 50 uint8_t readEEPROMByte(uint32_t off) {
ptcrews 0:cb78df310c5f 51 uint8_t tmp = 0;
ptcrews 0:cb78df310c5f 52 off = off + stadr;
ptcrews 0:cb78df310c5f 53 tmp = *(__IO uint32_t*)off;
ptcrews 0:cb78df310c5f 54
ptcrews 0:cb78df310c5f 55 return tmp;
ptcrews 0:cb78df310c5f 56 }
ptcrews 0:cb78df310c5f 57
ptcrews 0:cb78df310c5f 58 //Call this function when you have sent all the data in the EEPROM through GSM
ptcrews 0:cb78df310c5f 59 //It just resets the offset to 0 so we start writing from the start.
ptcrews 0:cb78df310c5f 60 void resetEEPROM()
ptcrews 0:cb78df310c5f 61 {
ptcrews 0:cb78df310c5f 62 offset = 0;
ptcrews 0:cb78df310c5f 63 }
ptcrews 0:cb78df310c5f 64
ptcrews 0:cb78df310c5f 65 //This returns an array of bytes with size offset, that contains the entire EEPROM
ptcrews 0:cb78df310c5f 66 //Call this function when you want to send the data over GSM
Dilsher 1:397846763432 67 void getEEPROM(uint8_t *ptr, int nbytes)
ptcrews 0:cb78df310c5f 68 {
Dilsher 1:397846763432 69 for(int i = 0; i < nbytes; i++)
ptcrews 0:cb78df310c5f 70 {
Dilsher 1:397846763432 71 ptr[i] = readEEPROMByte(roffset+i);
ptcrews 0:cb78df310c5f 72 }
ptcrews 0:cb78df310c5f 73 return;
ptcrews 0:cb78df310c5f 74 }
ptcrews 0:cb78df310c5f 75
ptcrews 0:cb78df310c5f 76 int main(){
ptcrews 0:cb78df310c5f 77 resetEEPROM();
ptcrews 0:cb78df310c5f 78 pcSerial.printf("testing:\n");
ptcrews 0:cb78df310c5f 79 uint8_t test[] = "testing byte write and retrieval\0";
ptcrews 0:cb78df310c5f 80 uint8_t len = strlen((char*)test);
ptcrews 0:cb78df310c5f 81 writeEEPROMbytes(test, len);
ptcrews 0:cb78df310c5f 82 pcSerial.printf("write success\n");
ptcrews 0:cb78df310c5f 83 uint8_t fullStr[offset+1];
Dilsher 1:397846763432 84 getEEPROM(fullStr, 10);
ptcrews 0:cb78df310c5f 85 pcSerial.printf("String: %s\n", fullStr);
ptcrews 0:cb78df310c5f 86 }