////////////////////////////////////////////////////////////////////////////////
// 版权:     利尔达科技集团股份有限公司
// 文件名:   main_tx/rx
// 版本：    v1.0
// 工作环境: IAR  v5.30
// 作者:     于海波
// 生成日期: 2013.12
// 功能:      A发送包书记给B，黄灯闪烁，B模块接到数据红灯闪烁，B将数据回传给A模块，
//            A接到数据后红灯闪烁， 循环次操作，
// 相关文件: 
// 修改日志：
////////////////////////////////////////////////////////////////////////////////

#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "driver.h"
#include "sx1276-f4152-Hal.h"
#include "LSD_RF_SX1276.h"
#include "LSD_RF_APPrf.h"
#include "clock.h"
//===================================定义变量===================================================
long SysTick = 0;
uint8_t TXbuffer[30]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
uint8_t WakeAddr[8]={5,6,7,8,9,10,11,7};
uint8_t Rxbuffer[64];
//===================================函数声明===================================================

//===============================================================================================
////////////////////////////////////////////////////////////////////////////////
// 功能描述 : 主函数
// 输入参数 : 无
// 返回参数 : 无
// 说明     : 
////////////////////////////////////////////////////////////////////////////////
typedef enum{
    Nope,
    B300bps,//BW = 500KHz  SF = 7  CR = 4_6
    B1080bps,//BW = 125KHz    SF = 9  CR = 4_6
    B4000bps,//BW = 125KHz  SF = 7  CR = 4_5
}t_Baudrate;
t_Baudrate Baudrate = Nope;

unsigned char *point;
unsigned char Reg_PKTSNR = 0;
unsigned char Reg_PKTRssi = 0;
unsigned char Reg_Rssi = 0;

unsigned char LCD_p[9] = {0,0,0,0,0,8,0,1,0xff};
int T_Cnt = 0;
int R_Cnt = 0;
bool RESET_Flag = false;
void main( void )
{
    WDTCTL = WDTPW + WDTHOLD;            // 关闭看门狗
    platform_init();                     //测试底板初始化
    LEDONBAND(LEDALL);                   //初始化过程点亮所有LED灯
    KEYBOARD_DIR &=~ (KEY_S1+KEY_S2);
    KEYBOARD_IFG &=~ (KEY_S1+KEY_S2);
    KEYBOARD_IES |= (KEY_S1+KEY_S2);
    KEYBOARD_IE |= (KEY_S1+KEY_S2);
    
    point = (unsigned char *)(&(G_LoRaConfig.BandWidth));
    flash_read(FLASH_ADDRESS_D,point,3*2);
    if(G_LoRaConfig.BandWidth>BW500KHZ){
        G_LoRaConfig.BandWidth=BW125KHZ;
        G_LoRaConfig.SpreadingFactor = SF09;
        G_LoRaConfig.CodingRate = CR_4_6;
        flash_seg_clear(FLASH_ADDRESS_D);//写之前必清除
        flash_write(FLASH_ADDRESS_D,point,3*2);
    }
    if(G_LoRaConfig.SpreadingFactor==SF11){
        Baudrate = B300bps;
        LCD_p[8] = 1;
    }
    else{
        if(G_LoRaConfig.SpreadingFactor==SF07){
            Baudrate = B4000bps;
            LCD_p[8] = 3;
        }
        else{
            Baudrate = B1080bps;
            LCD_p[8] = 2;
        }
    }
    LCD_p[7] = 0xff;LCD_p[6] = 0;LCD_p[5] = 0;LCD_p[4] = 0;
    lcd_init();
    LCD_Disp_ALL_Num(LCD_p);
    //配置各个参数
    G_LoRaConfig.LoRa_Freq = 474600000;   //中心频点470MHz
    //G_LoRaConfig.BandWidth = BW125KHZ;    //BW = 125KHz
    //G_LoRaConfig.SpreadingFactor = SF09;  //SF = 9
    //G_LoRaConfig.CodingRate = CR_4_6;     //CR = 4/6
    G_LoRaConfig.PowerCfig = 15;          //19±dBm
    G_LoRaConfig.MaxPowerOn = true;       
    G_LoRaConfig.CRCON = true;            //CRC开启
    G_LoRaConfig.ExplicitHeaderOn = true; //Header开启
    G_LoRaConfig.PayloadLength = 20;      //数据包长度
    
    if(SX127x_init()!=NORMAL) WDTCTL=0;       //无线模块初始化失败复位
    KEYBOARD_IE |= (KEY_S1+KEY_S2);
    _EINT();                              //开总中断
    LEDOFFBIT(LEDALL);                   //初始化完毕关闭所有LED灯
    ON_Timerout();                        //开启定时器，1s一发数据
    while(1)
    {
        LPM3;
        if(RESET_Flag==true){
            flash_seg_clear(FLASH_ADDRESS_D);//写之前必清除
            flash_write(FLASH_ADDRESS_D,point,3*2);
            WDTCTL = 0;//RESET
        }
        
        LSD_RF_RXmode();  //每次发送一包数据后，进入接收状态，等待B模块应答
        /****RSSI*****/
        
        //中断中已经算过符号
        LCD_p[6] = Reg_PKTRssi/100%10;
        LCD_p[5] = Reg_PKTRssi/10%10;
        LCD_p[4] = Reg_PKTRssi%10;
        
        LCD_p[0] = T_Cnt%10;
        LCD_p[1] = T_Cnt/10%10;
        LCD_p[2] = R_Cnt%10;
        LCD_p[3] = R_Cnt/10%10;
        /****Cnt *****/
        //LCD_p[0] = T_Cnt%10;
        //LCD_p[1] = T_Cnt/10%10;
        //LCD_p[2] = R_Cnt%10;
        //LCD_p[3] = R_Cnt/10%10;
        LCD_ClrLcd_ALL();
        LCD_Disp_ALL_Num(LCD_p);
        if(T_Cnt>=100)
            T_Cnt = 0;
        if(R_Cnt>=100)
            R_Cnt = 0;
    }   
}
////////////////////////////////////////////////////////////////////////////////
// 功能描述 : 无线接收数据中断入口
// 输入参数 : 无
// 返回参数 : 无
// 说明     : 
////////////////////////////////////////////////////////////////////////////////
#pragma vector = PORT1_VECTOR
__interrupt void port1_isr(void)
{  
    if(KEYBOARD_IFG&(KEY_ALL)){
        char Keys_Flag = KEYBOARD_IFG&(KEY_ALL);
        switch(Keys_Flag&(KEY_S1|KEY_S2)){
        case KEY_S1:
            switch(Baudrate){
            case B1080bps:
                G_LoRaConfig.BandWidth = BW125KHZ;    //BW = 125KHz
                G_LoRaConfig.SpreadingFactor = SF07;  //SF = 9
                G_LoRaConfig.CodingRate = CR_4_6;     //CR = 4/6
                Baudrate = B4000bps;
                break;
            case B4000bps:
                G_LoRaConfig.BandWidth = BW125KHZ;    //BW = 125KHz
                G_LoRaConfig.SpreadingFactor = SF11;  //SF = 9
                G_LoRaConfig.CodingRate = CR_4_8;     //CR = 4/6  
                Baudrate = B300bps;
                break;
            case B300bps:
            default:
                G_LoRaConfig.BandWidth = BW125KHZ;    //BW = 125KHz
                G_LoRaConfig.SpreadingFactor = SF09;  //SF = 9
                G_LoRaConfig.CodingRate = CR_4_6;     //CR = 4/6
                Baudrate = B1080bps;
                break;
            }
            RESET_Flag = true;
            LPM3_EXIT;
            break;
        case KEY_S2:
            T_Cnt = 0;
            R_Cnt = 0;
            break;
        default:
            break;
        }
        KEYBOARD_IFG &= ~KEY_ALL;
        return;
    }
    if(DIO0_IFG)                                      //判断是否是DIO0引起的中断
    {
        DIO0_IFG = 0;                                  //清除DIO0中断标志位
        
        LSD_RF_RxPacket(Rxbuffer);   //接收数据包
#include "sx1276-LoRa.h"
        signed int temp = 0,test;
        SX1276Read( REG_LR_PKTSNRVALUE,&Reg_PKTSNR);
        test = (signed char)Reg_PKTSNR;
        if((Reg_PKTSNR&0x80)!=0){//负数
            //LCD_p[3] = 10;//Reg_PKTSNR
            Reg_PKTSNR = ~(Reg_PKTSNR)+1;
            temp = -Reg_PKTSNR;
        }
        else{
            //LCD_p[3] = 0xff;//Reg_PKTSNR
            temp = Reg_PKTSNR;
        }
        test++;
        SX1276Read( REG_LR_PKTRSSIVALUE,&Reg_PKTRssi);
        if(temp>0){
            temp = -164+Reg_PKTRssi*16/15;
            //if(temp>-50)
                //temp = -137+Reg_PKTRssi;
        }
        else{
             temp = -164+Reg_PKTRssi+(signed char)(temp*0.25+0.5);
        }
        if(temp<0){
            LCD_p[7] = 10;
            Reg_PKTRssi = -temp;
        }
        else{
            LCD_p[7] = 0xff;
            Reg_PKTRssi = temp;
        }
        //SX1276Read( REG_LR_RSSIVALUE,&Reg_Rssi);
        //Reg_Rssi
        if(memcmp(Rxbuffer,TXbuffer,20)==0)
        {
            LEDONBIT(LED1);                                //状态指示灯 
            DelayMs(60);
            LEDOFFBIT(LED1);                               //状态指示灯
            R_Cnt++;
        }
        LPM3_EXIT;  
    }
    P1IFG=0;
}
////////////////////////////////////////////////////////////////////////////////
// 功能描述 : 超时定时器
// 输入参数 : 无
// 返回参数 : 无
// 说明     : 用于误唤醒，或唤醒失败后超时时间到后重新进入WOR
////////////////////////////////////////////////////////////////////////////////
#pragma vector = TIMER1_A0_VECTOR
__interrupt void Timer1_A0_ISR()
{
     static uint8 t=0;
    t++;
    if(Baudrate >= B1080bps)
        t = 5;
    if(Baudrate == B300bps)
        t++;
    if(t >= 5)
    {
    LEDONBIT(LED3);//发送状态指示灯
    G_LoRaConfig.PayloadLength = 20;
    SX1276Reset();
     if(SX127x_init()!=NORMAL) WDTCTL=0; 
    LSD_RF_SendPacket(TXbuffer);//发送30个字节数据测试 
    T_Cnt++;
    LEDOFFBIT(LED3);//发送状态指示灯
    LPM3_EXIT;
    t = 0;
    }
}
