Basic read/write from flash.

Dependencies:   mbed-modifed

Committer:
Dilsher
Date:
Mon Nov 09 19:17:13 2015 +0000
Revision:
1:397846763432
Parent:
0:cb78df310c5f
Child:
2:118a2f154b78
Updates ResetADC and ReadEEPROM

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
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 }
Dilsher 1:397846763432 61 roffset += nbytes;
ptcrews 0:cb78df310c5f 62 return;
ptcrews 0:cb78df310c5f 63 }
ptcrews 0:cb78df310c5f 64
ptcrews 0:cb78df310c5f 65 //Found this online and it claims that it resets ADC to work after deepsleep \_O_/
ptcrews 0:cb78df310c5f 66 void resetADC()
ptcrews 0:cb78df310c5f 67 {
Dilsher 1:397846763432 68 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Dilsher 1:397846763432 69 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Dilsher 1:397846763432 70
Dilsher 1:397846763432 71 /* Enable Power Control clock */
Dilsher 1:397846763432 72 __PWR_CLK_ENABLE();
Dilsher 1:397846763432 73
Dilsher 1:397846763432 74 /* The voltage scaling allows optimizing the power consumption when the device is
Dilsher 1:397846763432 75 clocked below the maximum system frequency, to update the voltage scaling value
Dilsher 1:397846763432 76 regarding system frequency refer to product datasheet. */
Dilsher 1:397846763432 77 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Dilsher 1:397846763432 78
Dilsher 1:397846763432 79 /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
Dilsher 1:397846763432 80 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
Dilsher 1:397846763432 81
Dilsher 1:397846763432 82 /* Get the Oscillators configuration according to the internal RCC registers */
Dilsher 1:397846763432 83 HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
Dilsher 1:397846763432 84
Dilsher 1:397846763432 85 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
Dilsher 1:397846763432 86 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
Dilsher 1:397846763432 87 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
Dilsher 1:397846763432 88 RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
Dilsher 1:397846763432 89 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Dilsher 1:397846763432 90 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
Dilsher 1:397846763432 91 //RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Dilsher 1:397846763432 92 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
Dilsher 1:397846763432 93 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
Dilsher 1:397846763432 94 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
Dilsher 1:397846763432 95 {
Dilsher 1:397846763432 96 //Error_Handler();
Dilsher 1:397846763432 97 }
Dilsher 1:397846763432 98
Dilsher 1:397846763432 99 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
Dilsher 1:397846763432 100 clocks dividers */
Dilsher 1:397846763432 101 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
Dilsher 1:397846763432 102 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Dilsher 1:397846763432 103 if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
Dilsher 1:397846763432 104 {
Dilsher 1:397846763432 105 //Error_Handler();
Dilsher 1:397846763432 106 }
ptcrews 0:cb78df310c5f 107 }
ptcrews 0:cb78df310c5f 108
ptcrews 0:cb78df310c5f 109 int main(){
ptcrews 0:cb78df310c5f 110 resetEEPROM();
ptcrews 0:cb78df310c5f 111 pcSerial.printf("testing:\n");
ptcrews 0:cb78df310c5f 112 uint8_t test[] = "testing byte write and retrieval\0";
ptcrews 0:cb78df310c5f 113 uint8_t len = strlen((char*)test);
ptcrews 0:cb78df310c5f 114 writeEEPROMbytes(test, len);
ptcrews 0:cb78df310c5f 115 pcSerial.printf("write success\n");
ptcrews 0:cb78df310c5f 116 uint8_t fullStr[offset+1];
Dilsher 1:397846763432 117 getEEPROM(fullStr, 10);
ptcrews 0:cb78df310c5f 118 pcSerial.printf("String: %s\n", fullStr);
ptcrews 0:cb78df310c5f 119 }