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

Committer:
terence304
Date:
Wed Jan 24 18:22:00 2018 +0000
Revision:
8:bca4f3d51eaa
Child:
9:4c8f32a4044d
add eeprom read and write funcions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terence304 8:bca4f3d51eaa 1 #include "mbed.h"
terence304 8:bca4f3d51eaa 2 #include "eeprom.h"
terence304 8:bca4f3d51eaa 3
terence304 8:bca4f3d51eaa 4 #if defined( TARGET_NUCLEO_L476RG )
terence304 8:bca4f3d51eaa 5 #define DATA_EEPROM_BASE ( ( uint32_t )0x0807F800U )
terence304 8:bca4f3d51eaa 6 #define DATA_EEPROM_END ( ( uint32_t )DATA_EEPROM_BASE + 2048 )
terence304 8:bca4f3d51eaa 7 #elif defined( TARGET_NUCLEO_L152RE )
terence304 8:bca4f3d51eaa 8 #define DATA_EEPROM_BASE ( ( uint32_t )0x08080000U )
terence304 8:bca4f3d51eaa 9 #define DATA_EEPROM_END ( ( uint32_t )0x080807FFU )
terence304 8:bca4f3d51eaa 10 #else
terence304 8:bca4f3d51eaa 11 #error "Please define EEPROM base address and size for your board "
terence304 8:bca4f3d51eaa 12 #endif
terence304 8:bca4f3d51eaa 13
terence304 8:bca4f3d51eaa 14 uint8_t EepromMcuReadBuffer( uint16_t addr, uint8_t *buffer, uint16_t size )
terence304 8:bca4f3d51eaa 15 {
terence304 8:bca4f3d51eaa 16 assert_param( buffer != NULL );
terence304 8:bca4f3d51eaa 17
terence304 8:bca4f3d51eaa 18 // assert_param( addr >= DATA_EEPROM_BASE );
terence304 8:bca4f3d51eaa 19 assert_param( buffer != NULL );
terence304 8:bca4f3d51eaa 20 assert_param( size < ( DATA_EEPROM_END - DATA_EEPROM_BASE ) );
terence304 8:bca4f3d51eaa 21
terence304 8:bca4f3d51eaa 22 memcpy( buffer, ( uint8_t* )DATA_EEPROM_BASE, size );
terence304 8:bca4f3d51eaa 23
terence304 8:bca4f3d51eaa 24 return SUCCESS;
terence304 8:bca4f3d51eaa 25 }
terence304 8:bca4f3d51eaa 26
terence304 8:bca4f3d51eaa 27 uint8_t EepromMcuWriteBuffer( uint16_t addr, uint8_t *buffer, uint16_t size )
terence304 8:bca4f3d51eaa 28 {
terence304 8:bca4f3d51eaa 29 int n = size / 4 + (size % 4 ? 1 : 0);
terence304 8:bca4f3d51eaa 30 uint32_t *flash = ( uint32_t* )buffer;
terence304 8:bca4f3d51eaa 31
terence304 8:bca4f3d51eaa 32 assert_param( buffer != NULL );
terence304 8:bca4f3d51eaa 33 assert_param( size < ( DATA_EEPROM_END - DATA_EEPROM_BASE ) );
terence304 8:bca4f3d51eaa 34
terence304 8:bca4f3d51eaa 35 HAL_FLASH_Unlock( );
terence304 8:bca4f3d51eaa 36
terence304 8:bca4f3d51eaa 37 for( uint32_t i = 0; i < n; i++ )
terence304 8:bca4f3d51eaa 38 {
terence304 8:bca4f3d51eaa 39 HAL_FLASH_Program( 0x02U, DATA_EEPROM_BASE + \
terence304 8:bca4f3d51eaa 40 ( 4 * i ), flash[i] );
terence304 8:bca4f3d51eaa 41 }
terence304 8:bca4f3d51eaa 42
terence304 8:bca4f3d51eaa 43 HAL_FLASH_Lock( );
terence304 8:bca4f3d51eaa 44
terence304 8:bca4f3d51eaa 45 return SUCCESS;
terence304 8:bca4f3d51eaa 46 }