Basic read/write from flash.

Dependencies:   mbed-modifed

Committer:
ptcrews
Date:
Mon Nov 09 19:04:19 2015 +0000
Revision:
0:cb78df310c5f
Child:
1:397846763432
Basic read/write from flash.

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;
ptcrews 0:cb78df310c5f 10
ptcrews 0:cb78df310c5f 11 //This function writes a byte of data to the EEPROM at the appropriate location.
ptcrews 0:cb78df310c5f 12 //This is called by my writeEEPROMbytes.
ptcrews 0:cb78df310c5f 13 //Use this if you want to write a single byte.
ptcrews 0:cb78df310c5f 14 HAL_StatusTypeDef writeEEPROMByte(uint8_t data)
ptcrews 0:cb78df310c5f 15 {
ptcrews 0:cb78df310c5f 16 HAL_StatusTypeDef status;
ptcrews 0:cb78df310c5f 17 int address = offset + stadr;
ptcrews 0:cb78df310c5f 18 offset++;
ptcrews 0:cb78df310c5f 19 HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing
ptcrews 0:cb78df310c5f 20 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
ptcrews 0:cb78df310c5f 21 HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM
ptcrews 0:cb78df310c5f 22 return status;
ptcrews 0:cb78df310c5f 23 }
ptcrews 0:cb78df310c5f 24
ptcrews 0:cb78df310c5f 25 //This function takes an array of bytes and its size and writes them to the EEPROM
ptcrews 0:cb78df310c5f 26 //Use this if you want to write a lot of bytes.
ptcrews 0:cb78df310c5f 27 void writeEEPROMbytes(uint8_t* data, uint8_t size)
ptcrews 0:cb78df310c5f 28 {
ptcrews 0:cb78df310c5f 29 for(int i = 0; i < size; i++)
ptcrews 0:cb78df310c5f 30 {
ptcrews 0:cb78df310c5f 31 writeEEPROMByte(data[i]);
ptcrews 0:cb78df310c5f 32 }
ptcrews 0:cb78df310c5f 33 }
ptcrews 0:cb78df310c5f 34
ptcrews 0:cb78df310c5f 35 //This function reads a byte of data from the EEPROM at the offset passed in.
ptcrews 0:cb78df310c5f 36 //This is called by my getEEPROM function
ptcrews 0:cb78df310c5f 37 uint8_t readEEPROMByte(uint32_t off) {
ptcrews 0:cb78df310c5f 38 uint8_t tmp = 0;
ptcrews 0:cb78df310c5f 39 off = off + stadr;
ptcrews 0:cb78df310c5f 40 tmp = *(__IO uint32_t*)off;
ptcrews 0:cb78df310c5f 41
ptcrews 0:cb78df310c5f 42 return tmp;
ptcrews 0:cb78df310c5f 43 }
ptcrews 0:cb78df310c5f 44
ptcrews 0:cb78df310c5f 45 //Call this function when you have sent all the data in the EEPROM through GSM
ptcrews 0:cb78df310c5f 46 //It just resets the offset to 0 so we start writing from the start.
ptcrews 0:cb78df310c5f 47 void resetEEPROM()
ptcrews 0:cb78df310c5f 48 {
ptcrews 0:cb78df310c5f 49 offset = 0;
ptcrews 0:cb78df310c5f 50 }
ptcrews 0:cb78df310c5f 51
ptcrews 0:cb78df310c5f 52 //This returns an array of bytes with size offset, that contains the entire EEPROM
ptcrews 0:cb78df310c5f 53 //Call this function when you want to send the data over GSM
ptcrews 0:cb78df310c5f 54 void getEEPROM(uint8_t *ptr)
ptcrews 0:cb78df310c5f 55 {
ptcrews 0:cb78df310c5f 56 for(int i = 0; i < offset; i++)
ptcrews 0:cb78df310c5f 57 {
ptcrews 0:cb78df310c5f 58 ptr[i] = readEEPROMByte(i);
ptcrews 0:cb78df310c5f 59 }
ptcrews 0:cb78df310c5f 60 return;
ptcrews 0:cb78df310c5f 61 }
ptcrews 0:cb78df310c5f 62
ptcrews 0:cb78df310c5f 63 //Found this online and it claims that it resets ADC to work after deepsleep \_O_/
ptcrews 0:cb78df310c5f 64 void resetADC()
ptcrews 0:cb78df310c5f 65 {
ptcrews 0:cb78df310c5f 66 // Enable the HSI (to clock the ADC)
ptcrews 0:cb78df310c5f 67 RCC_OscInitTypeDef RCC_OscInitStruct;
ptcrews 0:cb78df310c5f 68 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
ptcrews 0:cb78df310c5f 69 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
ptcrews 0:cb78df310c5f 70 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
ptcrews 0:cb78df310c5f 71 HAL_RCC_OscConfig(&RCC_OscInitStruct);
ptcrews 0:cb78df310c5f 72
ptcrews 0:cb78df310c5f 73 }
ptcrews 0:cb78df310c5f 74
ptcrews 0:cb78df310c5f 75 int main(){
ptcrews 0:cb78df310c5f 76 resetEEPROM();
ptcrews 0:cb78df310c5f 77 pcSerial.printf("testing:\n");
ptcrews 0:cb78df310c5f 78 uint8_t test[] = "testing byte write and retrieval\0";
ptcrews 0:cb78df310c5f 79 uint8_t len = strlen((char*)test);
ptcrews 0:cb78df310c5f 80 writeEEPROMbytes(test, len);
ptcrews 0:cb78df310c5f 81 pcSerial.printf("write success\n");
ptcrews 0:cb78df310c5f 82 uint8_t fullStr[offset+1];
ptcrews 0:cb78df310c5f 83 getEEPROM(fullStr);
ptcrews 0:cb78df310c5f 84 pcSerial.printf("String: %s\n", fullStr);
ptcrews 0:cb78df310c5f 85 }