#include "parameter.h"
PARAMETER SystemPara = PARA_DEFAULT;

uint32_t LoraChannel = 0;
void LoraParaRead(void)
{

    LoraPara.RxSF = SystemPara.LockID[3]/10 + 0x07;
    LoraPara.RxBW = SystemPara.LockID[3]%10;

    if(LoraPara.RxSF>12)LoraPara.RxSF = 0x07;
    if(LoraPara.RxBW>9)LoraPara.RxBW = 0x07;

    if(SystemPara.LockID[2]>83)SystemPara.LockID[2] = 23;
    LoraChannel = SystemPara.LockID[2] + 410;
    LoraChannel *= 1024*16;
    LoraPara.RxChannel[0] = (uint8_t)(LoraChannel);
    LoraPara.RxChannel[1] = (uint8_t)(LoraChannel>>8);
    LoraChannel = LoraChannel>>8;
    LoraPara.RxChannel[2] = (uint8_t)(LoraChannel>>8);

    LoraPara.TxSF = SystemPara.PcID[3]/10 + 0x07;
    LoraPara.TxBW = SystemPara.PcID[3]%10;
    if(LoraPara.TxSF>12)LoraPara.TxSF = 0x07;
    if(LoraPara.TxBW>9)LoraPara.TxBW = 0x07;

    
    /*********************************************/
    //设置为固定值SF=11,BW=125K
    LoraPara.RxSF = 0x08;
    LoraPara.RxBW = 0x07;
    LoraPara.TxSF = 0x08;
    LoraPara.TxBW = 0x07;
     /*********************************************/
    
    if(SystemPara.PcID[2]>93)SystemPara.PcID[2] = 89;
    LoraChannel = SystemPara.PcID[2] + 410;
    LoraChannel *= 1024*16;
    LoraPara.TxChannel[0] = (uint8_t)(LoraChannel);
    LoraPara.TxChannel[1] = (uint8_t)(LoraChannel>>8);
    LoraChannel = LoraChannel>>8;
    LoraPara.TxChannel[2] = (uint8_t)(LoraChannel>>8);
    
    //由于是网关，所以需要交换一下值
    uint8_t temp[3] = 0;
    memcpy(temp,LoraPara.TxChannel,3*sizeof(uint8_t));
    memcpy(LoraPara.TxChannel,LoraPara.RxChannel,3*sizeof(uint8_t));
    memcpy(LoraPara.RxChannel,temp,3*sizeof(uint8_t));

}
PARAMETER temp4 = PARA_DEFAULT;
void FixedParaDeal(void)
{
    static int n = sizeof(PARAMETER);

    EEPROM_ReadByte(PARAM_ADDR,(uint8_t *)(&SystemPara),n);
    if(SystemPara.FixedPara==0)
    {
        EEPROM_WriteByte(PARAM_ADDR,(uint8_t *)(&temp4),n);
    }
    else
    {
        memcpy(&temp4,&SystemPara,13*sizeof(uint8_t));
        temp4.FixedPara = 1;
        //EEPROM_WriteByte(PARAM_ADDR,(uint8_t *)(&temp4),n);
    }
    LoraParaRead();

}


void FixedParaChange(bool State)//State=1 修改参数,修改完毕后读出，State=0 不变
{
    if(State!=TRUE)return;
    int n = sizeof(PARAMETER),t = (&SystemPara.PhoResBase[3]-&SystemPara.VariablePara);
    PARAMETER temp2;
    EEPROM_ReadByte(PARAM_ADDR,(uint8_t *)(&temp2),n);
    //if((memcmp(&SystemPara.VariablePara,&temp2.VariablePara,t*sizeof(uint8_t)))!=NULL)
    {
        memcpy(&SystemPara.VariablePara,&temp2.VariablePara,t*sizeof(uint8_t));
        EEPROM_WriteByte(PARAM_ADDR,(uint8_t *)(&SystemPara),n);
    }
    EEPROM_ReadByte(PARAM_ADDR,(uint8_t *)(&SystemPara),n);
    LoraParaRead();
}

void EEPROM_ReadByte(u32 addr, u8 *buf, u16 len)
{
    u16 i;
    if (addr<0x00001000 || addr>0x000013FF)
    {
        return;
    }
    for (i=0; i<len; i++, addr++)
    {
        buf[i] = FLASH_ReadByte(addr);
    }
}

void EEPROM_WriteByte(u32 addr, u8 *buf, u16 len)
{
    u16 i;
    if (addr<0x00001000 || addr>0x000013FF)
    {
        return;
    }
    FLASH_Unlock(FLASH_MemType_Data);
    while (!(FLASH->IAPSR & FLASH_IAPSR_DUL))
    {
        ;
    }
    for (i=0; i<len; i++, addr++)
    {
        FLASH_ProgramByte(addr, buf[i]);
         FLASH_WaitForLastOperation(FLASH_MemType_Program);
    }
    FLASH_Lock(FLASH_MemType_Data);
}

void EEPROM_Erase(u32 addr, u16 len)
{
    if (addr<0x00001000 || addr>0x000013FF)
    {
        return;
    }
    FLASH_Unlock(FLASH_MemType_Data);
    while (!(FLASH->IAPSR & FLASH_IAPSR_DUL))
    {
        ;
    }
    for (; len>0; len--, addr++)
    {
        FLASH_EraseByte(addr);
    }
    FLASH_Lock(FLASH_MemType_Data);
}


