#include "include.h"

#define CM_SIZE         0x5000          //Parameter area at least 20k
#define CM_START        (FLASH_SIZE - CM_SIZE)


#if SYS_PARAM_RTCRAM
    #define param_read8(a, b)       rtcram_read(a, (u8 *)&b, 1)
    #define param_write8(a, b)      rtcram_write(a, (u8 *)&b, 1)
    #define param_read16(a, b)      rtcram_read(a, (u8 *)&b, 2)
    #define param_write16(a, b)     rtcram_write(a, (u8 *)&b, 2)
    #define param_read32(a, b)      rtcram_read((a, (u8 *)&b, 4)
    #define param_write32(a, b)     rtcram_write(a, (u8 *)&b, 4)
    #define param_read(a, b, c)     rtcram_read(a, b, c)
    #define param_write(a, b, c)    rtcram_write(a, b, c)
    #define param_sync_do()
#else
    #define param_read8(a, b)       b = cm_read8(PAGE0(a))
    #define param_write8(a, b)      cm_write8(PAGE0(a), b)
    #define param_read16(a, b)      b = cm_read16(PAGE0(a))
    #define param_write16(a, b)     cm_write16(PAGE0(a), b)
    #define param_read32(a, b)      b = cm_read32(PAGE0(a))
    #define param_write32(a, b)     cm_write32(PAGE0(a), b)
    #define param_read(a, b, c)     cm_read(a, PAGE0(b), c)
    #define param_write(a, b, c)    cm_write(a, PAGE0(b), c)
    #define param_sync_do()         cm_sync()
#endif


AT(.text.bsp.param)
void param_init(bool reset)
{
    cm_init(MAX_CM_PAGE, CM_START, CM_SIZE);
    //printf("CM: %x\n", cm_read8(PAGE0(0)));
    //printf("CM: %x\n", cm_read8(PAGE1(0)));

    sys_cb.rand_seed = sys_get_rand_key();
}

AT(.text.bsp.param)
void bsp_param_write(u8 *buf, u32 addr, uint len)
{
    param_write(buf, addr, len);
}

AT(.text.bsp.param)
void bsp_param_read(u8 *buf, u32 addr, uint len)
{
    param_read(buf, addr, len);
}

AT(.text.bsp.param)
void bsp_param_sync(void)
{
    param_sync_do();
}

#if BT_LOCAL_ADDR
AT(.text.bsp.param)
void param_random_key_read(u8 *key)
{
    if (key == NULL) {
        return;
    }
    param_read(key, PARAM_RANDOM_KEY, 4);
}

AT(.text.bsp.param)
void param_random_key_write(void)
{
    u32 key;
    param_random_key_read((u8*)&key);
    if (key == 0 || key == UINT_MAX) {
        key = sys_cb.rand_seed;
        param_write((u8 *)&key, PARAM_RANDOM_KEY, 4);
    }
}
#endif // BT_LOCAL_ADDR

AT(.text.bsp.param)
void param_sync(void)
{
    param_sync_do();
}

AT(.text.bsp.param)
u32 param_get_xosc_addr(void)
{
    return PARAM_BT_XOSC_CAP;
}

#if FOT_EN

AT(.text.bsp.param)
void param_fot_addr_write(u8 *param)
{
    param_write((u8 *)param, PARAM_FOT_ADDR, 4);
    param_sync();
}

AT(.text.bsp.param)
void param_fot_addr_read(u8 *param)
{
    param_read(param, PARAM_FOT_ADDR, 4);
}

AT(.text.bsp.param)
void param_fot_remote_ver_write(u8 *param)
{
    param_write((u8 *)param, PARAM_FOT_REMOTE_VER, 2);
    param_sync();
}

AT(.text.bsp.param)
void param_fot_remote_ver_read(u8 *param)
{
    param_read(param, PARAM_FOT_REMOTE_VER, 2);
}

AT(.text.bsp.param)
void param_fot_head_info_write(u8 *param)
{
    param_write((u8 *)param, PARAM_FOT_HEAD_INFO, 8);
    param_sync();
}

AT(.text.bsp.param)
void param_fot_head_info_read(u8 *param)
{
    param_read(param, PARAM_FOT_HEAD_INFO, 8);
}

AT(.text.bsp.param)
void param_fot_hash_write(u8 *param)
{
    param_write((u8 *)param, PARAM_FOT_HASH, 4);
    param_sync();
}

AT(.text.bsp.param)
void param_fot_hash_read(u8 *param)
{
    param_read(param, PARAM_FOT_HASH, 4);
}
#endif

AT(.text.bsp.param)
void param_qcheck_code_write(u8* param)
{
    param_write((u8 *)param, PARAM_QCHECK_CODE, 4);
    param_sync();
}

AT(.text.bsp.param)
void param_qcheck_code_read(u8* param)
{
    param_read((u8 *)param, PARAM_QCHECK_CODE, 4);

}
//wav
AT(.text.bsp.param)
void param_sys_vol_write(void)
{
    param_write((u8 *)&sys_cb.vol, PARAM_SYS_VOL, 1);
}
AT(.text.bsp.param)
void param_sys_vol_read(void)
{
    param_read((u8 *)&sys_cb.vol, PARAM_SYS_VOL, 1);
}
