#include "config.h"

ENTRY(_start)

//Define the flash max size
__max_flash_size = FLASH_CODE_SIZE;

__base = 0x10000000;

// Define the area size
__cache_stack_vma   = 0x10600;  //for loader
__cache_stack_size  = 512;      //for loader
__stack_vma         = 0x10800;
__stack_ram_size    = 1k;
__heap_vma          = 0x10c00;
__heap_ram_size     = 12k;
__data_vma          = 0x13c00;
__data_ram_size     = 13k;
__comm_vma          = 0x17000;
__comm_ram_size     = 36k;
__bram_vma          = 0x20000;
__bram_ram_size     = 48k;
__cram_vma          = 0x2c000;
__cram_ram_size     = 80k;

//Define the area
__aram_vma = 0x50000;
__eram_vma = 0x54000;
__dram_vma = 0x58000;
__rram_vma = 0x55800;  //for rnn input/output

MEMORY
{
    init            : org = __base,             len = 512
    flash(rx)       : org = __base + 512,       len = __max_flash_size
    comm(rx)        : org = __comm_vma,         len = __comm_ram_size

    stack           : org = __stack_vma,        len = __stack_ram_size
    heap            : org = __heap_vma,         len = __heap_ram_size
    data            : org = __data_vma,         len = __data_ram_size
    bram            : org = __bram_vma,         len = __bram_ram_size
    cram            : org = __cram_vma,         len = __cram_ram_size
    aram            : org = __aram_vma,         len = 16k
    dram            : org = __dram_vma,         len = 0x4500
    eram            : org = __eram_vma,         len = 0xa00
    rram            : org = __rram_vma,         len = 608
}

SECTIONS
{
    .init : {
        *(.reset)
    } > init

    .comm : {
        *(.vector)
        *(.plt)
        *(.com_text*)
        *(.com_text.stack.handler)
        *(.com_rodata*)
        *debug.o(.rodata*)
        *(.data*)
        *(.sdata*)
        *(.load_text)
        *(.load_rodata)
        . = ALIGN(512);
    } > comm AT > flash

    .flash : {
        __code_start_sniff = .;
        *(.sniff*)
        . = ALIGN(512);
        __code_end_sniff = .;

        __code_start_usbdev = .;
        *(.usbdev*)
        . = ALIGN(512);
        __code_end_usbdev = .;

        __code_start_sleep = .;
        *(.saradc_text*)
        *(.sleep_text*)
        . = ALIGN(512);
        __code_end_sleep = .;

        __code_start_charge = .;
        *(.charge_text*)
        *(.text.bsp.charge)
        *(.text.bsp.tkey)
        *(.text.vhouse)
        . = ALIGN(512);
        __code_end_charge = .;

        __code_start_pwrsave = .;
        *(.text.pwroff*)
        . = ALIGN(512);
        __code_end_pwrsave = .;

        __code_start_bb_test = .;
        *(.bb_test*)
        *(.bb_test_rodata*)
        . = ALIGN(512);
        __code_end_bb_test = .;

        __code_start_update = .;
        *(.text.update*)
        . = ALIGN(512);
        __code_end_update = .;

        __code_start_huart = .;
        *(.com_huart*)
        . = ALIGN(512);
        __code_end_huart = .;

        __code_start_fota = .;
        *(.text.fot.cache*)
        . = ALIGN(512);
        __code_end_fota = .;

        __code_start_bts = .;
        *(.text.stk.tws.txreq*)
        *(.text.stk.l2cap.txreq*)
        . = ALIGN(512);
        *(.text.stk.tws.send*)
        . = ALIGN(512);
        *(.text.stk.tws.recv*)
        . = ALIGN(512);
        *(.text.stk.hci.tx*)
        . = ALIGN(512);
        *(.text.bts.hci.rx*)
        . = ALIGN(512);
        *(.text.stk.bt.acl.tx*)
        . = ALIGN(512);
        __code_end_bts = .;

        *(.irq_init.aligned)
        . = ALIGN(512);
        *(.text.ble.chmap*)
        . = ALIGN(512);
        *(.text*)
        *(.rodata*)
        *(.srodata*)
        *(.rela*)
        LONG(0)
        . = ALIGN(512);

    } > flash

    .stack (NOLOAD) : {
        __irq_stack_start = .;
        . = 0x400;
        __irq_stack = .;
    } > stack
    __irq_stack_size = __irq_stack - __irq_stack_start;

    .bss (NOLOAD) : {
        __bss_start = .;
        *(COMMON)
        *(.bss*)
        *(.sbss*)
        *(.buf*)
        *(.dac_obuf)
        *(.btmem.stack.sdp)
        *(.btmem.stack.spp)
        *(.btmem.stack.l2cap)
        *(.btmem.stack.hid)
        *(.qtest_buf)
        __bss_end = .;
    } > data
    __bss_size = __bss_end - __bss_start;

    .heap (NOLOAD) : {
        *(.mem_heap)
    } > heap

    .bram __bram_vma (NOLOAD) : {
        *(.btmem.bthw)
        *(.btmem*)

        *(.ble_cache*)
        *(.ble_buf*)
        *(.spp_tx_buff.*)
        *(.ble_tx_buff.*)
    } > bram

    .aram __aram_vma (NOLOAD) : {
        *(.usb_buf*)
        . = 0x1000;
    } > aram

    .fcc_buf __aram_vma (NOLOAD) : {
        . = 0x1000;
        *(.fcc_buf.huart)
    } > aram

    .aram_msc_stm __aram_vma (NOLOAD) : {
        *(.stm_buf)
        . = 0x1000;
    } > aram

    .cram_user __cram_vma (NOLOAD) : {
        *(.user.*)
    } > cram

    .dram_upd __dram_vma (NOLOAD) : {
        . = 0x3800;
        *(.upd_ota*)
        *(.upd_buf*)
        . = 0x4500;
    } > dram

    .eram_fot __eram_vma (NOLOAD) : {
        *(.fot_data.*)
        . = 0xa00;
    } > eram
}

//Calc the lma
__bank_size = SIZEOF(.flash);
__comm_lma = LOADADDR(.comm);
__comm_size = SIZEOF(.comm);
