zhyinch
2020-11-02 c194f604712122b2640e10b6d7d901cf4ba3712f
Ô´Âë/ºËÐİå/Src/main.c
@@ -11,7 +11,7 @@
#include "global_param.h"
#include "ADC.h"
#define DEBUG_MODE
//#define DEBUG_MODE
void Device_Init(void)
{
@@ -46,6 +46,31 @@
}
u16 tyncpoll_time;
u16 slottime,max_slotpos;
extern u8 module_power;
extern u16 total_slotnum;
void ComMapCheck(void)
{
   if(g_com_map[COM_INTERVAL]<10)
   {
     g_com_map[COM_INTERVAL]=500;
   }
         if(g_com_map[COM_INTERVAL]>1000)
   {
     g_com_map[COM_INTERVAL]=1000;
   }
    if(module_power>MAX_RFPOWER)
      {module_power=MAX_RFPOWER;}
      if(module_power<0)
      {module_power=0;}
      if(g_com_map[MAX_REPORT_ANC_NUM]>10)
      {g_com_map[MAX_REPORT_ANC_NUM]=10;}
      if(g_com_map[MAX_REPORT_ANC_NUM]<1)
      {g_com_map[MAX_REPORT_ANC_NUM]=1;}
      if(g_com_map[IMU_THRES]>10)
      {g_com_map[IMU_THRES]=10;}
      if(g_com_map[IMU_THRES]<1)
      {g_com_map[IMU_THRES]=1;}
}
void Program_Init(void)
{   float temp;
   u16 temp2;
@@ -55,20 +80,30 @@
   //deca_sleep(1000);
   HeartBeatInit();
#ifdef DEBUG_MODE
   g_com_map[DEV_ROLE]=1;
      g_com_map[DEV_ID]=1;
   g_com_map[COM_INTERVAL]=100;
   g_com_map[MAX_REPORT_ANC_NUM]=3;
      g_com_map[NEARBASE_NUM]=1;
      g_com_map[NEARBASE_ID1]=1;
//   g_com_map[DEV_ROLE]=1;
//      g_com_map[DEV_ID]=1;
//   g_com_map[COM_INTERVAL]=100;
//   g_com_map[MAX_REPORT_ANC_NUM]=3;
//      g_com_map[NEARBASE_NUM]=1;
//      g_com_map[NEARBASE_ID1]=2;
//   g_com_map[ANC_FLAG]=1;
//      save_com_map_to_flash();
   g_com_map[BASESYNCSEQ]=1;
   g_com_map[SYNCBASEID]=1;
#endif
   OUT485_ENABLE;
   g_com_map[VERSION] = 0x0107;
   ComMapCheck();
   g_com_map[VERSION] = 0x0208;
   dev_id = g_com_map[DEV_ID];
   slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*4/3);
   slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*SLOT_SCALE)+3;
   max_slotpos=g_com_map[COM_INTERVAL]/slottime;
   tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;   
   module_power = g_com_map[POWER];
   total_slotnum = 1000/g_com_map[COM_INTERVAL];
   anchor_type = dev_id%3;
   
   if(g_com_map[DEV_ROLE])
   {
   printf("标签ID: %d .\r\n",dev_id);
@@ -76,7 +111,6 @@
   printf("单次通讯基站数量: %d个.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
   }else{
   
   anchor_type = dev_id%g_com_map[MAX_REPORT_ANC_NUM];
   printf("基站ID: %x .\r\n",dev_id);
   printf("基站类型: %c .\r\n",anchor_type+0x41);
   printf("单次通讯基站数量: %d个.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
@@ -101,15 +135,29 @@
 *
 * @return none
 */
extern u16 synclost_count;
void HeatBeat(void)
{
{ u16 checksum;
   if(synclost_count>5)
   {
      hbsend[6] = 0;
   }else{
      hbsend[6] = 1;
   }
checksum = Checksum_u16(&hbsend[2],12);
memcpy(&hbsend[14],&checksum,2);
UART_PushFrame(hbsend,16);
}
extern u8 g_start_sync_flag;
u16 heartbeat_timer,poll_timer,sync_timer;
extern u8 g_start_sync_flag,usart_send_flag,anc_report_num;
u16 heartbeat_timer,poll_timer;
int16_t sync_timer;
extern u8 flag_newsecond, tagpos_rec[50], tagpos_send[50],ancidlist_num;
uint32_t tagpos_binary;
extern u16 ancidlist_rec[20],ancidlist_send[20];
extern u16 target_time;
void IdleTask(void)
{
   g_start_sync_flag=0;
         UART_CheckReceive();
      UART_CheckSend();
   if(heartbeat_timer>1000)
@@ -118,7 +166,33 @@
      if(g_com_map[HEARTBEAT]&&g_com_map[DEV_ROLE]==0)
      HeatBeat();
   }
   if(flag_newsecond)
   {
     flag_newsecond=0;
      tagpos_binary=0;
      memcpy(ancidlist_send,ancidlist_rec,ancidlist_num*2);
      ancidlist_num=0;
      for(u8 i=0;i<max_slotpos;i++)
      {
         if(tagpos_rec[i])
            tagpos_binary|=1<<i;
         tagpos_rec[i] = 0;
      }
   }
   if(target_time>1000)
   {target_time-=1000;}
   #ifdef USART_INTEGRATE_OUTPUT
   if(g_com_map[DEV_ROLE]==0&&usart_send_flag)
   {u16 checksum;
            usart_send_flag=0;
            usart_send_anc[2] = 5;//正常模式
            usart_send_anc[3] = anc_report_num*6+2;//正常模式
            checksum = Checksum_u16(&usart_send_anc[2],anc_report_num*6+2);
            memcpy(&usart_send_anc[4+anc_report_num*6],&checksum,2);
            UART_PushFrame(usart_send_anc,6+anc_report_num*6);
            anc_report_num=0;
   }
   #endif
   if(g_com_map[CNT_UPDATE]==1)
   {
   uint32_t result = 0;
@@ -136,6 +210,25 @@
      //   Delay_ms(100);
         SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
   }
         if(g_com_map[CNT_REBOOT]==1)
      {
         g_com_map[CNT_REBOOT]=0;
         g_com_map[MAP_SIGN_INDEX]=0;
         save_com_map_to_flash();
         delay_ms(100);
         SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
      }
      if(g_com_map[CNT_RESTART]==1)
      {
         g_com_map[CNT_RESTART]=0;
         save_com_map_to_flash();
         delay_ms(100);
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
      }
            if(g_com_map[MAP_SIGN_INDEX]!=0x55AA)
      {
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
      }
   }
int main(void)
@@ -150,10 +243,12 @@
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
   usart_send[0]=0x55;
   usart_send[1]=0xAA;
   usart_send_anc[0]=0x55;
   usart_send_anc[1]=0xAA;
  //SendComMap(50, 0);
   while(1)
   {
   g_start_sync_flag=0;
   if(g_start_send_flag)
   {
      g_start_send_flag = 0;      
@@ -162,6 +257,7 @@
      IdleTask();      
if(g_com_map[DEV_ROLE]==0)
      Anchor_App();
   }
}