WXK
2023-11-21 d8dd7c6e256bc607904ef1c1b1a36471c99db873
Src/radio/Lora.c
@@ -13,9 +13,14 @@
static RadioEvents_t RadioEvents;
#define USE_MODEM_LORA
//#define USE_MODEM_FSK
#define REGION_CN779
#define REGION_CN779
uint32_t RxDutyCycle_RX_time   =128;
uint32_t RxDutyCycle_SLEEP_time=256;
#define LORA_FREQUENCY0 470200000
#define LORA_FREQUENCY1 471200000
#define LORA_FREQUENCY2 472200000
#define LORA_FREQUENCY3 473200000
#define LORA_FREQUENCY4 474200000
#if defined( REGION_AS923 )
#define RF_FREQUENCY                                923000000 // Hz
@@ -140,6 +145,7 @@
void OnRxError( void );
uint8_t  lora_chongfuyingda_flag;
uint8_t Lora_rx_open_flag;
extern u32 Loratx_frequency;
void LedToggle(void)
{
@@ -147,8 +153,10 @@
}
void Lora_tx_mode( void )
{
            Radio.SetChannel( 470200000 );
            Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 0,
            Radio.SetChannel( Loratx_frequency );
            Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2,
            7, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON,
            false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 );
@@ -186,6 +194,7 @@
}
void OnTxDone( void )
{
    LED_TB_OFF;
    if(lora_yingda_num>1000)
    {
    lora_yingda_num=0;
@@ -198,20 +207,61 @@
    {}
            
}
extern uint32_t lp_time;
uint32_t lora_txtime_old;
extern uint8_t frame_seq_nb2,rec_nearbase_num,ancidlist_num;
uint16_t RX_TIMEOUTs;
extern LPTIM_HandleTypeDef hlptim1;
uint16_t current_time,start_time,end_time;
    uint16_t lora_recv_devid=0;
u32 id;
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
extern uint8_t Lora_tx_ancnum;
extern uint16_t Lora_tx_ancid[50];
extern uint16_t Lora_tx_ancdist[50];
uint8_t power_and_key=0;
extern u8 battary,button;
u8 lora_seq_nb2;
extern uint8_t stationary_flag;
extern uint8_t anjian_flag;
void Lora_tx_zubao( void )
{
    uint16_t checksum;
                    usart_send[2] = 0x22;//正常模式
               usart_send[3] = 9+Lora_tx_ancnum*4;//数据段长度
                    memcpy(&usart_send[4],&dev_id,2);
               usart_send[6] = lora_seq_nb2;//包序
                    power_and_key=stationary_flag<<1;//把按键跟静止2合1,发出去
                    power_and_key=power_and_key+anjian_flag;;
                    memcpy(usart_send+7,&bat_percent,1);
                    memcpy(usart_send+8,&power_and_key,1);
                    usart_send[9] = lora_yingda_flag;//lora等待应答位
                    usart_send[10] = Lora_tx_ancnum;
//                    usart_send[9] = lora_yingda_flag;
               memcpy(&usart_send[11],&Lora_tx_ancid,2*Lora_tx_ancnum);
               memcpy(&usart_send[11+Lora_tx_ancnum*2],&Lora_tx_ancdist,2*Lora_tx_ancnum);
               checksum = Checksum_u16(&usart_send[2],8+4*Lora_tx_ancnum);
               memcpy(&usart_send[11+4*Lora_tx_ancnum],&checksum,2);
}
uint16_t current_count;
u16 Lora_recevcount;
extern uint8_t no_data_chongqi_num;
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    Lora_recevcount=HAL_LPTIM_ReadCounter(&hlptim1);
    Lora_recevcount=Lora_recevcount+197;//197代表6ms
    if(Lora_recevcount>=32768)
    {
    Lora_recevcount -=32768;
    }
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    BufferSize = size;
    memcpy( RX_Buffer, payload, BufferSize );
    RssiValue = rssi;
    SnrValue = snr;
    no_data_chongqi_num=0;
    Radio.Standby();
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    if(lora_yingda_flag)
    {
    memcpy(&lora_recv_devid,&RX_Buffer[4],2);
@@ -228,10 +278,10 @@
//       Delay_Ms(10);
//        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
        uint16_t i=0;
        for(i=0;i<BufferSize;i++)
        {printf("RXD %d:\r\n",RX_Buffer[i]);}
//        for(i=0;i<BufferSize;i++)
        {printf("R %d:\r\n",RX_Buffer[3]);}
        RX_TIMEOUTs++;
        printf("RX_num :%d\r\n",RX_TIMEOUTs);
//        printf("RX_num :%d\r\n",RX_TIMEOUTs);
//        start_time=HAL_LPTIM_ReadCounter(&hlptim1);
//        end_time=start_time+((32768/1000)*20);
//        if(end_time>=32768)
@@ -251,34 +301,57 @@
        }
    }
////////            printf("Anchor_App\r\n");
        current_count=HAL_LPTIM_ReadCounter(&hlptim1);
      while(current_count<Lora_recevcount||current_count>Lora_recevcount+16384)
      {
         current_count=HAL_LPTIM_ReadCounter(&hlptim1);
         if(current_count<Lora_recevcount-300)
         {
            break;
         }
      }
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
            Anchor_App();
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
            HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效
            delay_ms(10);
            Lora_tx_mode();
            Radio.Send( usart_send, 12+rec_nearbase_num*4);
            lora_yingda_num++;
//              printf("RX_TIMEOUTs :%d\r\n",RX_TIMEOUTs);
//        Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//              printf("RX_TIMEOUTs :XXXX\r\n");
//                 Radio.Rx( RX_TIMEOUT_VALUE );
//            printf("Slave TXD XXXX\r\n");
            delay_ms(1);
//            if(lp_time-lora_txtime_old>=1)
//            {
//            LED_TB_ON;
//            lora_txtime_old=lp_time;
//            lora_seq_nb2++;
//            lora_yingda_num++;
//            Lora_tx_zubao();
//            Lora_tx_mode();
//            Radio.Send( usart_send, 12+Lora_tx_ancnum*4);
//            Lora_tx_ancnum=0;
//            }
//            else
//            {
            Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//            }
    }
}
void OnTxTimeout( void )
{
       Radio.Standby();
        Lora_Init();
    Lora_rx_open_flag=0;
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
}
void OnRxTimeout( void )
{
    Radio.Standby();
        Lora_Init();
    Lora_rx_open_flag=0;
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
    printf("slave OnRxTimeout\r\n");
//    printf("slave OnRxTimeout\r\n");
    
}
@@ -286,31 +359,9 @@
{
    Radio.Standby();
    if(EnableMaster)
    {
        TX_Buffer[0] = 'P';
        TX_Buffer[1] = 'I';
        TX_Buffer[2] = 'N';
        TX_Buffer[3] = 'G';
//        crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
//        TX_Buffer[4]=crc_value>>8;
//        TX_Buffer[5]=crc_value;
        //Radio.Send( TX_Buffer, 6);
           Radio.Send( TX_Buffer, 4);
//           HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_SET);
//           HAL_Delay_nMS( 1 );
//           HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_RESET);
           printf("Master TXD PING!\r\n");
    }
    else
    {
      Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        printf("slave OnRxError\r\n");
//                 Radio.Rx( RX_TIMEOUT_VALUE );
    }
        Lora_Init();
    Lora_rx_open_flag=0;
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
}
void Lora_Init(void)