yincheng.zhong
2024-02-25 d7f957eb56272e006de3dd404454cdf56de86655
Src/main.c
@@ -38,6 +38,7 @@
#include <stdlib.h>
#include <Lora.h>
#include "radio.h"
#include "dw_mbx_tag.h"
#define NSH1 0x0001
#define GP   0x0002
#define FLASH_HARDWARE_VERSION_MAP      (uint32_t)0x08004F00    //硬件版本号和批次信息位置
@@ -159,23 +160,8 @@
float motor_keeptime;
void IdleTask(void)
{
//if(huart1.Instance->ISR&USART_ISR_FE)
//   {
//   SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
////   }
//    if(du_flag==1)
//    {
//    du_flag=0;
//
//    }
//    if(xie_flag==1)
//    {
//    xie_flag=0;
//    memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
//    }
   if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
   {
        UART_CheckReceive();
      UART_CheckSend();
      nomove_count = 0;
@@ -211,37 +197,6 @@
   }
u16 total_slotnum,current_slotnum;
extern int32_t offsettimeus;
//u16 last_lpcount = 0;
void SetNextPollTime(u16 time)
{
  int32_t lpcount1,lptime1,target_time1;
int32_t last_lpcount1;
//   if(current_slotnum>=total_slotnum)
//      current_slotnum-=total_slotnum;
//
//   target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000;
//   //if(target_time<90000)
//   {
//      lptime=target_time-offsettimeus;
//      lpcount = lptime/LPTIMER_LSB;
//   if(lpcount>LPTIMER_1S_COUNT)
//      lpcount-=LPTIMER_1S_COUNT;
//   if(lpcount<0)
//   {
//   lpcount+=LPTIMER_1S_COUNT;
//   }
    last_lpcount1=HAL_LPTIM_ReadCounter(&hlptim1);
    lpcount1 = last_lpcount1+32.768*time;
    while(lpcount1>LPTIMER_1S_COUNT)
    {
        lpcount1-=LPTIMER_1S_COUNT;
    }
//    last_lpcount = lpcount;
   __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount1+32768);
}
/* 返回ch字符在sign数组中的序号 */
int getIndexOfSigns(char ch)
@@ -279,16 +234,7 @@
    
//u32 Loratx_frequency;
u8 active_flag,nomovesleeptime;
//void GetLoratx_frequency(void)
//{
//Loratx_frequency=470000000;
//srand(HAL_LPTIM_ReadCounter(&hlptim1));
//        Loratx_frequency=((rand()%5)+470)*1000000;
//        if(470000000<=Loratx_frequency&&Loratx_frequency<480000000)
//        g_com_map[NEARBASE_ID1]=1136+(Loratx_frequency/1000000-470);
//        if(480000000<=Loratx_frequency&&Loratx_frequency<490000000)
//        g_com_map[NEARBASE_ID1]=1152+(Loratx_frequency/1000000-480);
//Loratx_frequency=470000000;
//}
void Program_Init(void)
{   float temp;
@@ -305,7 +251,9 @@
    g_com_map[GROUP_ID]=5;
//    g_com_map[COM_INTERVAL] = 1000;
    if(g_com_map[COM_INTERVAL]<1000)
    {g_com_map[COM_INTERVAL] = 1000;}
    {
        g_com_map[COM_INTERVAL] = 1000;
    }
    active_flag = g_com_map[ACTIVE_INDEX];
    module_power = g_com_map[POWER];
    g_com_map[IMU_ENABLE] = 0;
@@ -320,7 +268,7 @@
   slot_startcount=tyncpoll_time*1000/LPTIMER_LSB;
   lastpoll_time=tyncpoll_time;
    tag_frequency = 1000/g_com_map[COM_INTERVAL];
   total_slotnum = 1000/g_com_map[COM_INTERVAL];
    nomovesleeptime = g_com_map[NOMOVESLEEP_TIME];
   if(active_flag==0)
   {   
@@ -334,23 +282,16 @@
     g_com_map[COM_INTERVAL]=1000;
   }
    if(module_power>67)
      {module_power=67;}
    {
        module_power=67;
    }
      if(module_power<0)
      {module_power=0;}
    {
        module_power=0;
    }
   current_slotnum=1;
//   current_time=GetLPTime();
//      if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 0) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
//  {
//    Error_Handler();
//  }
  
//g_com_map[NOMOVESLEEP_TIME]=5;
//    printf("固件版本: 免布线LORA工卡 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
   memcpy(g_com_map2,g_com_map,COM_MAP_SIZE);
//   printf("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]);
//   printf("DEVICE ALARM DISTANCE: 1.%d 2.%d 3.%d .\r\n",g_com_map[ALARM_DISTANCE1],g_com_map[ALARM_DISTANCE2],g_com_map[ALARM_DISTANCE3]);
}
static void SystemPower_Config(void)
@@ -365,13 +306,6 @@
uint32_t temp;
extern uint8_t sleep_flag,m_bEUARTTxEn;
uint16_t waitusart_timer;
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
//{
//  /* Set transmission flag: trasfer complete*/
//  waitusart_timer = tag_frequency*USART_KEEPWAKE_TIME;
//
//}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
      //SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
@@ -420,11 +354,6 @@
    if(GPIO_Pin == GPIO_PIN_3)
    {
        SystemClock_Config();
//        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
//        HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效
//        l_bIsVibration = HIDO_TRUE;
//        nomove_time=0;
//        printf("RXD :77777\r\n");
        irq_num++;
        RadioIrqProcess();
    }
@@ -440,17 +369,7 @@
        stationary_num=0;
    }
}
extern void Lora_tx_zubao( void );
extern void Lora_tx_mode( void );
extern     uint8_t Lora_tx_ancnum;
extern   u8 lora_seq_nb2;
extern uint16_t lora_yingda_num;
uint8_t lora_tx_flag;
uint8_t lora_txanchor_power_flag;
extern uint16_t Lora_tx_ancid[50];
extern uint16_t Lora_tx_ancdist[50];
extern uint8_t Lora_tx_anc_electricity[50];
u8 lora_seq_nb1;
static uint8_t send_frame1[EUART_RX_BUF_SIZE];
#define BleRxBufferSize 1
uint8_t aRxBuffer[1],group_id,GPSRxBuffer[BleRxBufferSize];
@@ -518,41 +437,10 @@
void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
{
        SystemClock_Config();
// HAL_UART_Transmit(&hlpuart1,(uint8_t*)ble_rxdate111,3,1000);
////  huart->RxState = HAL_UART_STATE_BUSY_RX;
//        ble_rxzhongzhuandate[ble_zhongzhuannum]=BleRxBuffer[0];
//    ble_zhongzhuannum++;
//    if(ble_rxzhongzhuandate[ble_zhongzhuannum-1]==0x0A&&ble_rxzhongzhuandate[ble_zhongzhuannum-2]==0x0D)
//    {
//        ble_zhongzhuannum=0;
//    memcpy(ble_rxdate,ble_rxzhongzhuandate,50);
//    memset(ble_rxzhongzhuandate,0,sizeof(ble_rxzhongzhuandate));
//    }
//        if(lp_time>3)
    fangzhijinrushuimian_flag=1;
    HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)GPSRxBuffer, 1);
//    HAL_UART_Transmit(&hlpuart1,ble_rxdate,2,1000);
}
void PowerDownDetect(void)
{
 static uint32_t l_u32QXpowerTick = 0;
static uint32_t powercount=0;
    if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12))
      {
          delay_ms(1000);
          if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12))
          {
              while(1)
              {
                if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)==0)
                {
                    delay_ms(100);
                    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
                }
            }
          }
      }
}
uint8_t usart5_state,flag_onesecond;
void OneSecondTask(void)
{
@@ -586,28 +474,26 @@
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */
    MX_DMA_Init();
  /* USER CODE END SysInit */
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_SPI1_Init();
  MX_ADC_Init();
  MX_DMA_Init();
//  MX_IWDG_Init();
  MX_TIM2_Init();
  MX_LPUART1_UART_Init();
   // MX_LPUART1_UART_Init();
  MX_LPTIM1_Init();
  /* USER CODE BEGIN 2 */
//    LED_LG_ON;
    LED2_TB_ON;
    Program_Init();
    LIS3DH_Data_Init();   
//    Uwb_Lora_Switch(1,0);
    Dw1000_Init();
    Delay_Ms(10);
//    Uwb_Lora_Switch(0,0);
    //   HardWareTypeDiffConfig();
    dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
    dwt_entersleep();
     if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
@@ -615,20 +501,18 @@
     Error_Handler();
    }
    Delay_Ms(10);
//    Uwb_Lora_Switch(0,0);
//    printf("RXD :66666\r\n");
    usart_send[0]=0x55;
    usart_send[1]=0xaa;
    bat_percent=Get_VDDVlotage();
     HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)GPSRxBuffer, 1);
     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); //开GPS电源
    LoraInit();
    Delay_Ms(1000);
    HAL_UART_Transmit(&hlpuart1,Close_RMC,22,1000);
    HAL_UART_Transmit(&hlpuart1,Close_GSA,22,1000);
    HAL_UART_Transmit(&hlpuart1,Close_GSV,22,1000);
    HAL_UART_Transmit(&hlpuart1,Close_GLL,22,1000);
    HAL_UART_Transmit(&hlpuart1,Close_TXT,22,1000);
//    Delay_Ms(1000);
//    HAL_UART_Transmit(&hlpuart1,Close_RMC,22,1000);
//    HAL_UART_Transmit(&hlpuart1,Close_GSA,22,1000);
//    HAL_UART_Transmit(&hlpuart1,Close_GSV,22,1000);
//    HAL_UART_Transmit(&hlpuart1,Close_GLL,22,1000);
//    HAL_UART_Transmit(&hlpuart1,Close_TXT,22,1000);
  if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 0) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
  {
    Error_Handler();
@@ -648,17 +532,7 @@
    #ifndef DEBUG_MODE   
    if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))   
  {
      if(fangzhijinrushuimian_flag==0)
          {
    Selection.WakeUpEvent = UART_WAKEUP_ON_STARTBIT;
    HAL_UARTEx_EnableClockStopMode(&hlpuart1);
    HAL_UARTEx_EnableStopMode( &hlpuart1 );
    HAL_UARTEx_StopModeWakeUpSourceConfig( &hlpuart1, Selection );
    __HAL_UART_ENABLE_IT(&hlpuart1, UART_IT_WUF);
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
          }
  }
    #endif
    
  }
@@ -1156,60 +1030,40 @@
float key_keeptime;
extern float freqlost_count,range_lost_time;
extern uint8_t lora_yingda_flag;
extern uint32_t wg_lost_count;
uint8_t lora_yingdatime;
extern uint8_t lora_chongfuyingda_flag;
extern uint8_t lora_chongfuyingda_flag,report_ancnum;
uint16_t no_data_chongqi_num;
uint16_t anchor_send_num;
uint8_t no_yingdatime;
void LedTask(void)
{
    if(report_ancnum>0)
    {
        LED2_TB_ON;
    }
//    if(wg_lost_count<=1)
//    {
//        LED_TB_ON;
//    }
    if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
        if(bat_percent<15)
        {
            LED_TR_ON;
        }
}
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
//    uint8_t pinlv;
    SystemClock_Config();
    lp_time++;
    anchor_send_num++;
    no_data_chongqi_num++;
    stationary_num++;
    if(Beepchixutime>0)
    {
        Beepchixutime--;
        if(Beepchixutime==0)
        {Beep_Off();}
    }
    if(no_yingdaflag==1)
    {
    no_yingdatime++;
        if(no_yingdatime>=5)
        {
            no_yingdaflag=0;
            no_yingdatime=0;
        }
    }
    if( no_data_chongqi_num>600)
    {NVIC_SystemReset();}
    if(lp_time%(g_com_map[COM_INTERVAL]/1000)==0)
    {
    LedTask();
    LoraReportPoll();
    flag_onesecond = 1;
//    lora_tx_flag=1;
//    lora_txanchor_power_flag=0;
//    }
//    if(anchor_send_num>=3600)
//    {anchor_send_num=0;
//    lora_txanchor_power_flag=1;
//        lora_tx_flag=0;
//        bat_percent=Get_VDDVlotage();
//    }
    }
    if(stationary_num>=10)
    {stationary_num=0;
    stationary_flag=1;
    }
    LED_TB_OFF;
    LED_TR_OFF;
    LED2_TB_OFF;
    LED2_TR_OFF;
}
/* USER CODE END 4 */