/*******************************************************************************
* BlueNRG-1 generic linker file for IAR
* Main linker variables to control it are:
*
* MEMORY_FLASH_APP_SIZE: define the size of the application in case not all the flash is needed.
* Default value is: 156KB when no OTA firmware upgrade is supported
*
* MEMORY_FLASH_APP_OFFSET: define the offset of the application.
* Default value is: 0 offset
* 
* ST_OTA_HIGHER_APPLICATION: When defined application is built for OTA support
* in the higher part of memory
*
* ST_OTA_LOWER_APPLICATION: When defined application is built for OTA support
* in the lower part of memory
*
* ST_USE_OTA_SERVICE_MANAGER_APPLICATION: When defined application is built for
* OTA firmware upgrade support with separated application for firmware upgrade
*
*******************************************************************************/


/*******************************************************************************
* Memory Definitions
*******************************************************************************/
/*
BlueNRG-1 memory map
+-----------------------+ 0x20005FFF
|  RAM (24K)            |
+-----------------------+ 0x20000000
|                       |
|                       |
+-----------------------+ 0x10067FFF
|                       |
|  FLASH (160K)         |
+-----------------------+ 0x10040000
|                       |
+-----------------------| 0x100007FF
|   ROM (2K)            |
+-----------------------+ 0x10000000
*/

define symbol _MEMORY_RAM_BEGIN_       = 0x20000000;
define symbol _MEMORY_RAM_SIZE_        = 0x6000;             /* 24KB  */
define symbol _MEMORY_RAM_END_         = 0x20005FFF;

define symbol _MEMORY_FLASH_BEGIN_    = 0x10040000;
define symbol _MEMORY_FLASH_SIZE_     = 0x28000;          /* 160KB */ 
define symbol _MEMORY_FLASH_END_      = 0x10067FFF; 

define symbol _MEMORY_ROM_BEGIN_    = 0x10000000;
define symbol _MEMORY_ROM_SIZE_     = 0x800;          /*2KB */ 
define symbol _MEMORY_ROM_END_      = 0x100007FF; 

/* First RAM 32 bit word is reserved for interrupt vector remapping: Second RAM 32 bit word is reserved for jumping to OTA Service Manager */
define symbol RAM_RESERVED             = 8;                  

/* Reserved for BTLE stack non volatile memory */
define symbol FLASH_NVM_DATASIZE       = 4*1024;            

if( isdefinedsymbol(ST_OTA_HIGHER_APPLICATION) ) 
{
  /* This configuration is intended for application supporting OTA firmware upgrade with 2-app scheme (app in the upper part of memory map) */
  /*
     BlueNRG-1 higher OTA firmware upgrade support higher application memory map
     +-----------------------+ 0x20005FFF
     |  RAM (24K-4)          |
     +-----------------------+ 0x20000004
     |                       |
     |                       |
     +-----------------------+ 0x10068000
     |                       |
     |  NVM(6K)              |
     +-----------------------+ 0x10066800
     |                       |
     |  Higher app (76K)     |
     +-----------------------+ 0x10053800
     |                       |
     |  Lower app (76K)      |
     +-----------------------| 0x10040800
     |   Reset Manager (2K)  |
     +-----------------------+ 0x10040000
     |                       |
     +-----------------------| 0x100007FF
     |   ROM (2K)            |
     +-----------------------+ 0x10000000
  */

  define symbol RESET_MANAGER_SIZE       = 0x800;
  define symbol MEMORY_FLASH_APP_SIZE   = (_MEMORY_FLASH_SIZE_ - RESET_MANAGER_SIZE - FLASH_NVM_DATASIZE - 2048)/2;
  define symbol _MEMORY_FLASH_OFFSET_   = RESET_MANAGER_SIZE + MEMORY_FLASH_APP_SIZE;
} else if ( isdefinedsymbol(ST_OTA_LOWER_APPLICATION) ) {
  /* This configuration is intended for application supporting OTA firmware upgrade with 2-app scheme (app in the lower part of memory map) */
  /*
     BlueNRG-1 higher OTA firmware upgrade support lower application memory map
     +-----------------------+ 0x20005FFF
     |  RAM (24K-4)          |
     +-----------------------+ 0x20000004
     |                       |
     |                       |
     +-----------------------+ 0x10068000
     |                       |
     |  NVM(6K)              |
     +-----------------------+ 0x10066800
     |                       |
     |  Higher app (76K)     |
     +-----------------------+ 0x10053800
     |                       |
     |  Lower app (76K)      |
     +-----------------------| 0x10040800
     |   Reset Manager (2K)  |
     +-----------------------+ 0x10040000
     |                       |
     +-----------------------| 0x100007FF
     |   ROM (2K)            |
     +-----------------------+ 0x10000000
  */

  define symbol RESET_MANAGER_SIZE       = 0x800;
  define symbol MEMORY_FLASH_APP_SIZE   = (_MEMORY_FLASH_SIZE_ - RESET_MANAGER_SIZE - FLASH_NVM_DATASIZE - 2048)/2;
  define symbol _MEMORY_FLASH_OFFSET_   = RESET_MANAGER_SIZE;
} else if ( isdefinedsymbol(ST_USE_OTA_SERVICE_MANAGER_APPLICATION) ) {
  /* This configuration is intended for application supporting OTA firmware upgrade with independent OTA firmware upgrade service manager
  (app in the upper part of memory map) */
  /*
     BlueNRG-1 higher OTA firmware upgrade support higher application memory map
     +-----------------------+ 0x20005FFF
     |  RAM (24K-4)          |
     +-----------------------+ 0x20000004
     |                       |
     |                       |
     +-----------------------+ 0x10068000
     |                       |
     |  NVM(4K)              |
     +-----------------------+ 0x10067000
     |                       |
     |  User app (92K)       |
     +-----------------------+ 0x10050000
     | Reset Manager (2K)    |
     | + OTA S. Manager (62K)|
     +-----------------------+ 0x10040000
     |                       |
     +-----------------------| 0x100007FF
     |   ROM (2K)            |
     +-----------------------+ 0x10000000
  */

  define symbol RESET_SM_MANAGER_SIZE   = 0x10000; /* Including OTA firmware upgrade service manager application */
  define symbol MEMORY_FLASH_APP_SIZE   = _MEMORY_FLASH_SIZE_ - RESET_SM_MANAGER_SIZE - FLASH_NVM_DATASIZE;
  define symbol _MEMORY_FLASH_OFFSET_   = RESET_SM_MANAGER_SIZE;
} else {
  /* This configuration is intended for application not supporting OTA firmware upgrade */
  /*
  BlueNRG-1 standard application memory map
  +-----------------------+ 0x20005FFF
  |  RAM (24K)            |
  +-----------------------+ 0x20000000
  |                       |
  |                       |
  +-----------------------+ 0x10068000
  |                       |
  |  NVM(4K)              |
  +-----------------------+ 0x10067000
  |                       |
  |  User app (156K)      |
  +-----------------------+ 0x10040000
  |                       |
  +-----------------------| 0x100007FF
  |   ROM (2K)            |
  +-----------------------+ 0x10000000
*/
  if( !isdefinedsymbol( MEMORY_FLASH_APP_SIZE) ) {
    define symbol MEMORY_FLASH_APP_SIZE   = _MEMORY_FLASH_SIZE_ - FLASH_NVM_DATASIZE;
  }
  if( isdefinedsymbol( MEMORY_FLASH_APP_OFFSET) ) {
    define symbol _MEMORY_FLASH_OFFSET_   = MEMORY_FLASH_APP_OFFSET;
  } else {
    define symbol _MEMORY_FLASH_OFFSET_ = 0;
  }
}

/* memory space definition */
define memory mem with size = 4G;   /* This is mandatory for the IAR linker.  */
                                    /* It creates a memory space "mem" with   */
                                    /* 4 gigabytes. 32-bit data bus always    */
                                    /* means 4G memory space.                 */

/* region definitions */
define region REGION_RAM_RESERVED   = mem:[from _MEMORY_RAM_BEGIN_
                                           to   _MEMORY_RAM_BEGIN_ + RAM_RESERVED];
define region REGION_RAM   = mem:[from _MEMORY_RAM_BEGIN_ +  RAM_RESERVED
                                   to   _MEMORY_RAM_END_];
                                   
define region REGION_FLASH_BOOTLOADER = mem:[from _MEMORY_FLASH_BEGIN_
                                  to  _MEMORY_FLASH_BEGIN_ + _MEMORY_FLASH_OFFSET_];
                                   
define region REGION_FLASH = mem:[from _MEMORY_FLASH_BEGIN_ + _MEMORY_FLASH_OFFSET_
                                  to  _MEMORY_FLASH_BEGIN_ + _MEMORY_FLASH_OFFSET_ + MEMORY_FLASH_APP_SIZE - 1];

define region REGION_FLASH_NVM = mem:[from _MEMORY_FLASH_END_- FLASH_NVM_DATASIZE + 1
                                  to   _MEMORY_FLASH_END_];
define region REGION_ROM = mem:[from _MEMORY_ROM_BEGIN_ to  _MEMORY_ROM_END_];
/*******************************************************************************
* ROM Sections
*******************************************************************************/
define block ROM_INFO with alignment = 8, size = 0x800 { section .rom_info};
keep {section .rom_info};
place in REGION_ROM { block ROM_INFO, };

/*******************************************************************************
* BlueNRG-1 STACK Sections
*******************************************************************************/
/**
* The last 2KB sector of FLASH is reserved for firmware to use. The BLE Host
* stores its security database in this area. The linker needs to make sure this
* 2KB sector is left empty.
*/
define block BLOCK_STACKLIB_FLASH_DATA with alignment = 2048, fixed order
{
    section .noinit.stacklib_flash_data,
    section .noinit.stacklib_stored_device_id_data
};                  
place at start of REGION_FLASH_NVM {block BLOCK_STACKLIB_FLASH_DATA};
do not initialize { section .noinit.stacklib_flash_data, 
                    section .noinit.stacklib_stored_device_id_data};


define block BLUE with alignment = 8, size = 0x20C  { section .bss.__blue_RAM}; // Radio Global configuration data
keep {section .bss.__blue_RAM};
place at address mem: 0x200000C0 { block BLUE, };

/*******************************************************************************
* FLASH Sections
*******************************************************************************/

place at start of REGION_FLASH_BOOTLOADER { readonly section .bootloader};
place at start of REGION_FLASH { readonly section .intvec };
keep {section .intvec};

/*******************************************************************************
* RAM Sections
*******************************************************************************/
define block CSTACK     with alignment = 8, size = 0xC00  { };
do not initialize { section CSTACK };

initialize by copy { readwrite };
do not initialize  {section .noinit};

/* Crash Handler Storage Information Block */
keep {section .bss.__crash_RAM};

/* Updater BLUE flag Ram Block - Blue flag Ram for DTM updater software activation */             
keep {section .bss.__blueflag_RAM};

keep {section .app_base};
keep {section .bss.savedMSP};
keep {section .bss.wakeupFromSleepFlag};

define block RAM_preamble with alignment = 8, fixed order
{
    section .bss.savedMSP,
    section .bss.wakeupFromSleepFlag,
    section .app_base,
    section .bss.__crash_RAM,
    section .bss.__blueflag_RAM,
};                  
place at start of REGION_RAM { block RAM_preamble, };
/*******************************************************************************
* Sections placement
*******************************************************************************/
place in REGION_FLASH { readonly };
place in REGION_RAM { readwrite };
place at end of REGION_RAM {block CSTACK };

/*******************************************************************************
* End of File
*******************************************************************************/
