#include "config.h"

ENTRY(_start)

//Define the flash max size
__max_flash_size = FLASH_CODE_SIZE;

__base = 0x10000000;

// Define program ram size (* SUM: 61KB *)
__data_ram_size     = 11k;
__comm_ram_size     = 18k;
__commEx_ram_size   = COMM_EX_SIZE;
__bram_ram_size     = 32k;

// Define the area size
__cache_stack_vma   = 0x10600;  //for loader
__cache_stack_size  = 512;      //for loader
__stack_vma         = 0x10800;
__stack_ram_size    = 1k;
__data_vma          = 0x10c00;
__comm_vma          = __data_vma   + __data_ram_size;
__commEx_vma        = __comm_vma   + __comm_ram_size;
__bram_vma          = __commEx_vma + __commEx_ram_size;



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

    stack           : org = __stack_vma,        len = __stack_ram_size
    data            : org = __data_vma,         len = __data_ram_size
    bram            : org = __bram_vma,         len = __bram_ram_size
}

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

    .comm : {
        *(.vector)
        *(.com_text*)
        *(.com_rodata*)
        *debug.o(.rodata*)
        *(.com_sleep*)
        *(.com_periph.*)
        *(.data*)
        *(.sdata*)
       
        #if AINS3_EN
        *(.fft_buf*)
        #endif

        #if AINS4_EN
        *(.fft_buf*)
        #endif
        . = ALIGN(512);
    } > comm AT > flash

    .commEx : {
        *(.com_ex*)
        . = ALIGN(512);
    } > commEx AT > flash

    .flash : {
         __code_start_app_proc = .;
        *(.text.app.proc.*)
        . = ALIGN(512);
        __code_end_app_proc = .;

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

        __code_start_stack_send = .;
        *(.text.stack.send.*)
        . = ALIGN(512);
        __code_end_stack_send = .;

        #if AINS3_EN
        __code_start_alg = .;
        . = ALIGN(512);
        __code_end_alg = .;
        *(.ains3_text*)
        *(.fft_rodata*)
        *(.fft_text*)
        #endif

        #if AINS4_EN
        __code_start_alg = .;
        . = ALIGN(512);
        __code_end_alg = .;
        *(.fft_rodata*)
        *(.ains4_rodata*)
        *(.text.ains4)
        *(.text.ains4_proc*)
        *(.ains4_text*)
        *(.fft_text*)
        #endif

        *(.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) : {
         __heap_start = .;
        *(.mem_heap)
        __heap_end = .;

        __bss_start = .;
        *(COMMON)
        *(.bss*)
        *(.sbss*)
        *(.buf*)
        *(.btmem.stack.l2cap)

        #if AINS3_EN
        *(.ains3_buf*)
        #endif 
        __bss_end = .;
    } > data
    __bss_size = __bss_end - __bss_start;
    __heap_size = __heap_end - __heap_start;

    .bram __bram_vma (NOLOAD) : {
        __bram_start = .;
        *(.btmem.bthw)
        *(.btmem*)
        *(.ram_text.*)
        *(.ble_cache*)
        *(.ble_buf*)
        *(.fot_data.*)
        *(.usb_buf*)
        *(.user_buf*)
        *(.bram_com*)

        #if AINS4_EN
        *(.ains4_buf*)
        #endif 
        __bram_end = .;
    } > bram
    __bram_size = __bram_end - __bram_start;

    .bram_dut __bram_vma (NOLOAD) : {
        . = __bram_ram_size - 4k;
        *(.fcc.huart_buf)
        *(.hci.h3*)
        *(.prod_test*)
    } > bram
}

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