EEPROM for SX1272

Dependencies:   X_NUCLEO_IKS01A1 driver_mbed_TH02 LoRaWAN-lib-v1_0_1 SX1272Lib mbed

Fork of Canada-SX1272-LoRaWAN-Bootcamp by Uttam Bhat

Revision:
8:bca4f3d51eaa
Child:
9:4c8f32a4044d
diff -r 1769f863fe49 -r bca4f3d51eaa app/eeprom.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/eeprom.cpp	Wed Jan 24 18:22:00 2018 +0000
@@ -0,0 +1,46 @@
+#include "mbed.h"
+#include "eeprom.h"
+
+#if defined( TARGET_NUCLEO_L476RG )
+#define DATA_EEPROM_BASE    ( ( uint32_t )0x0807F800U )
+#define DATA_EEPROM_END     ( ( uint32_t )DATA_EEPROM_BASE + 2048 )
+#elif defined( TARGET_NUCLEO_L152RE )
+#define DATA_EEPROM_BASE    ( ( uint32_t )0x08080000U ) 
+#define DATA_EEPROM_END     ( ( uint32_t )0x080807FFU )
+#else
+#error "Please define EEPROM base address and size for your board "
+#endif
+
+uint8_t EepromMcuReadBuffer( uint16_t addr, uint8_t *buffer, uint16_t size )
+{
+    assert_param( buffer != NULL );
+
+    // assert_param( addr >= DATA_EEPROM_BASE );
+    assert_param( buffer != NULL );
+    assert_param( size < ( DATA_EEPROM_END - DATA_EEPROM_BASE ) );
+
+    memcpy( buffer, ( uint8_t* )DATA_EEPROM_BASE, size );
+
+    return SUCCESS;
+}
+
+uint8_t EepromMcuWriteBuffer( uint16_t addr, uint8_t *buffer, uint16_t size )
+{
+    int n = size / 4 + (size % 4 ? 1 : 0);
+    uint32_t *flash = ( uint32_t* )buffer;
+
+    assert_param( buffer != NULL );
+    assert_param( size < ( DATA_EEPROM_END - DATA_EEPROM_BASE ) );
+
+    HAL_FLASH_Unlock( );
+
+    for( uint32_t i = 0; i < n; i++ )
+    {
+        HAL_FLASH_Program( 0x02U, DATA_EEPROM_BASE + \
+                           ( 4 * i ), flash[i] );
+    }
+
+    HAL_FLASH_Lock( );
+
+    return SUCCESS;
+}