Basic read/write from flash.
Dependencies: mbed-modifed
main.cpp@2:118a2f154b78, 2015-11-09 (annotated)
- Committer:
- Dilsher
- Date:
- Mon Nov 09 19:44:16 2015 +0000
- Revision:
- 2:118a2f154b78
- Parent:
- 1:397846763432
- Child:
- 3:50a42705f3ea
Removed ResetADC
Who changed what in which revision?
User | Revision | Line number | New 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 | |
ptcrews | 0:cb78df310c5f | 12 | //This function writes a byte of data to the EEPROM at the appropriate location. |
ptcrews | 0:cb78df310c5f | 13 | //This is called by my writeEEPROMbytes. |
ptcrews | 0:cb78df310c5f | 14 | //Use this if you want to write a single byte. |
ptcrews | 0:cb78df310c5f | 15 | HAL_StatusTypeDef writeEEPROMByte(uint8_t data) |
ptcrews | 0:cb78df310c5f | 16 | { |
ptcrews | 0:cb78df310c5f | 17 | HAL_StatusTypeDef status; |
ptcrews | 0:cb78df310c5f | 18 | int address = offset + stadr; |
ptcrews | 0:cb78df310c5f | 19 | offset++; |
ptcrews | 0:cb78df310c5f | 20 | HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing |
ptcrews | 0:cb78df310c5f | 21 | status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data); |
ptcrews | 0:cb78df310c5f | 22 | HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM |
ptcrews | 0:cb78df310c5f | 23 | return status; |
ptcrews | 0:cb78df310c5f | 24 | } |
ptcrews | 0:cb78df310c5f | 25 | |
ptcrews | 0:cb78df310c5f | 26 | //This function takes an array of bytes and its size and writes them to the EEPROM |
ptcrews | 0:cb78df310c5f | 27 | //Use this if you want to write a lot of bytes. |
ptcrews | 0:cb78df310c5f | 28 | void writeEEPROMbytes(uint8_t* data, uint8_t size) |
ptcrews | 0:cb78df310c5f | 29 | { |
ptcrews | 0:cb78df310c5f | 30 | for(int i = 0; i < size; i++) |
ptcrews | 0:cb78df310c5f | 31 | { |
ptcrews | 0:cb78df310c5f | 32 | writeEEPROMByte(data[i]); |
ptcrews | 0:cb78df310c5f | 33 | } |
ptcrews | 0:cb78df310c5f | 34 | } |
ptcrews | 0:cb78df310c5f | 35 | |
ptcrews | 0:cb78df310c5f | 36 | //This function reads a byte of data from the EEPROM at the offset passed in. |
ptcrews | 0:cb78df310c5f | 37 | //This is called by my getEEPROM function |
ptcrews | 0:cb78df310c5f | 38 | uint8_t readEEPROMByte(uint32_t off) { |
ptcrews | 0:cb78df310c5f | 39 | uint8_t tmp = 0; |
ptcrews | 0:cb78df310c5f | 40 | off = off + stadr; |
ptcrews | 0:cb78df310c5f | 41 | tmp = *(__IO uint32_t*)off; |
ptcrews | 0:cb78df310c5f | 42 | |
ptcrews | 0:cb78df310c5f | 43 | return tmp; |
ptcrews | 0:cb78df310c5f | 44 | } |
ptcrews | 0:cb78df310c5f | 45 | |
ptcrews | 0:cb78df310c5f | 46 | //Call this function when you have sent all the data in the EEPROM through GSM |
ptcrews | 0:cb78df310c5f | 47 | //It just resets the offset to 0 so we start writing from the start. |
ptcrews | 0:cb78df310c5f | 48 | void resetEEPROM() |
ptcrews | 0:cb78df310c5f | 49 | { |
ptcrews | 0:cb78df310c5f | 50 | offset = 0; |
ptcrews | 0:cb78df310c5f | 51 | } |
ptcrews | 0:cb78df310c5f | 52 | |
ptcrews | 0:cb78df310c5f | 53 | //This returns an array of bytes with size offset, that contains the entire EEPROM |
ptcrews | 0:cb78df310c5f | 54 | //Call this function when you want to send the data over GSM |
Dilsher | 1:397846763432 | 55 | void getEEPROM(uint8_t *ptr, int nbytes) |
ptcrews | 0:cb78df310c5f | 56 | { |
Dilsher | 1:397846763432 | 57 | for(int i = 0; i < nbytes; i++) |
ptcrews | 0:cb78df310c5f | 58 | { |
Dilsher | 1:397846763432 | 59 | ptr[i] = readEEPROMByte(roffset+i); |
ptcrews | 0:cb78df310c5f | 60 | } |
ptcrews | 0:cb78df310c5f | 61 | return; |
ptcrews | 0:cb78df310c5f | 62 | } |
ptcrews | 0:cb78df310c5f | 63 | |
ptcrews | 0:cb78df310c5f | 64 | int main(){ |
ptcrews | 0:cb78df310c5f | 65 | resetEEPROM(); |
ptcrews | 0:cb78df310c5f | 66 | pcSerial.printf("testing:\n"); |
ptcrews | 0:cb78df310c5f | 67 | uint8_t test[] = "testing byte write and retrieval\0"; |
ptcrews | 0:cb78df310c5f | 68 | uint8_t len = strlen((char*)test); |
ptcrews | 0:cb78df310c5f | 69 | writeEEPROMbytes(test, len); |
ptcrews | 0:cb78df310c5f | 70 | pcSerial.printf("write success\n"); |
ptcrews | 0:cb78df310c5f | 71 | uint8_t fullStr[offset+1]; |
Dilsher | 1:397846763432 | 72 | getEEPROM(fullStr, 10); |
ptcrews | 0:cb78df310c5f | 73 | pcSerial.printf("String: %s\n", fullStr); |
ptcrews | 0:cb78df310c5f | 74 | } |