Basic read/write from flash.
Dependencies: mbed-modifed
main.cpp
- Committer:
- Dilsher
- Date:
- 2015-11-09
- Revision:
- 1:397846763432
- Parent:
- 0:cb78df310c5f
- Child:
- 2:118a2f154b78
File content as of revision 1:397846763432:
#include "mbed.h" //define where the EEPROM begins #define stadr 0x08080000 Serial pcSerial(USBTX, USBRX); //Our current offset in the EEPROM int offset = 0; int roffset = 0; //This function writes a byte of data to the EEPROM at the appropriate location. //This is called by my writeEEPROMbytes. //Use this if you want to write a single byte. HAL_StatusTypeDef writeEEPROMByte(uint8_t data) { HAL_StatusTypeDef status; int address = offset + stadr; offset++; HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data); HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM return status; } //This function takes an array of bytes and its size and writes them to the EEPROM //Use this if you want to write a lot of bytes. void writeEEPROMbytes(uint8_t* data, uint8_t size) { for(int i = 0; i < size; i++) { writeEEPROMByte(data[i]); } } //This function reads a byte of data from the EEPROM at the offset passed in. //This is called by my getEEPROM function uint8_t readEEPROMByte(uint32_t off) { uint8_t tmp = 0; off = off + stadr; tmp = *(__IO uint32_t*)off; return tmp; } //Call this function when you have sent all the data in the EEPROM through GSM //It just resets the offset to 0 so we start writing from the start. void resetEEPROM() { offset = 0; } //This returns an array of bytes with size offset, that contains the entire EEPROM //Call this function when you want to send the data over GSM void getEEPROM(uint8_t *ptr, int nbytes) { for(int i = 0; i < nbytes; i++) { ptr[i] = readEEPROMByte(roffset+i); } roffset += nbytes; return; } //Found this online and it claims that it resets ADC to work after deepsleep \_O_/ void resetADC() { RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitTypeDef RCC_OscInitStruct = {0}; /* Enable Power Control clock */ __PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */ while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {}; /* Get the Oscillators configuration according to the internal RCC registers */ HAL_RCC_GetOscConfig(&RCC_OscInitStruct); /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSIState = RCC_HSI_OFF; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; //RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { //Error_Handler(); } /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { //Error_Handler(); } } int main(){ resetEEPROM(); pcSerial.printf("testing:\n"); uint8_t test[] = "testing byte write and retrieval\0"; uint8_t len = strlen((char*)test); writeEEPROMbytes(test, len); pcSerial.printf("write success\n"); uint8_t fullStr[offset+1]; getEEPROM(fullStr, 10); pcSerial.printf("String: %s\n", fullStr); }