zhyinch
2019-11-12 503efc0a0d2ea34213ec87aea5bf63c52c2eb93a
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 
#include "Rcc_Nvic_Systick.h"
#include "Usart.h"
#include "Spi.h"
#include "led.h"
#include "beep.h"
#include "dw_driver.h"
#include "dw_app.h"
#include "stm32f10x_it.h"
#include "serial_at_cmd_app.h"
#include "global_param.h"
#include "ADC.h"
 
#define DEBUG_MODE
 
void Device_Init(void)
{
    RCC_Configuration();
    //SystemInit();
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x5000);
    Nvic_Init();
//    Systick_Init();
    TIM3_Int_Init();
    Led_Init();
    Beep_Init();
    DW_GPIO_Init();
    Uart1_Init();
    Spi_Init();
    ADC_Configuration();
    
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
}
u8 anchor_type;
u32 dev_id;
u8 hbsend[16];
void HeartBeatInit(void)
{
    u16 checksum;
    hbsend[0]=0x55;
    hbsend[1]=0xAA;
    hbsend[2]=0x2;
    hbsend[3]=0xc;
    memcpy(&hbsend[4],&g_com_map[DEV_ID],2);
    checksum = Checksum_u16(&hbsend[2],12);
    memcpy(&hbsend[14],&checksum,2);
}
u16 tyncpoll_time;
u16 slottime,max_slotpos;
void Program_Init(void)
{    float temp;
    u16 temp2;
    uint16_t i;
    Usart1ParseDataCallback = UsartParseDataHandler;
    parameter_init();
    //deca_sleep(1000);
    HeartBeatInit();
#ifdef DEBUG_MODE
    g_com_map[DEV_ROLE]=1;
        g_com_map[DEV_ID]=4;
    g_com_map[COM_INTERVAL]=100;
    g_com_map[MAX_REPORT_ANC_NUM]=3;
#endif
    OUT485_ENABLE;
    g_com_map[VERSION] = 0x0106;    
    dev_id = g_com_map[DEV_ID];
    temp=(float)g_com_map[MAX_REPORT_ANC_NUM]*4/3;    
    temp2=g_com_map[MAX_REPORT_ANC_NUM]*4/3;
    if(temp2<temp)
    {
        temp2++;
    }
    slottime=temp2;
    max_slotpos=g_com_map[COM_INTERVAL]/slottime;
    tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;    
    
    if(g_com_map[DEV_ROLE])
    {
        tyncpoll_time=g_com_map[DEV_ID]*g_com_map[MAX_REPORT_ANC_NUM]*4/3;
    printf("±êÇ©ID: %d .\r\n",dev_id);
    printf("ͨѶ¼ä¸ô: %d ms.\r\n",g_com_map[COM_INTERVAL]);
    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]);
    }
    OUT485_DISABLE;
//    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]);
 
    
    for(i=0;i<255;i++)
    {
        g_Tagdist[i]=0xffff;
    }
}
 
/*! ------------------------------------------------------------------------------------------------------------------
 * @fn main()
 *
 * @brief Application entry point.
 *
 * @param  none
 *
 * @return none
 */
void HeatBeat(void)
UART_PushFrame(hbsend,16);
}
extern u8 g_start_sync_flag;
u16 heartbeat_timer,poll_timer,sync_timer;
void IdleTask(void)
{
    g_start_sync_flag=0;
            UART_CheckReceive();
        UART_CheckSend();
    if(heartbeat_timer>1000)
    {
        heartbeat_timer=0;
        if(g_com_map[HEARTBEAT]&&g_com_map[DEV_ROLE]==0)
        HeatBeat();
    }
 
    if(g_com_map[CNT_UPDATE]==1)
    {
    uint32_t result = 0;
        u16 tmp = 0xAAAA;
    __disable_irq();
    result = FLASH_Prepare(0x8004A38, 2);
    if(result)
        result = FLASH_Write(0x8004A38, (const uint8_t*)&tmp, 2); 
    __enable_irq();
        printf("½øÈëÉý¼¶Ä£Ê½\r\n");
    g_com_map[CNT_UPDATE]=0;
    save_com_map_to_flash();
    delay_ms(100);
        //    STMFLASH_Write_NoCheck(0x8004A38,0xAAAA);
        //    Delay_ms(100);
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader   
    }
 
    }
int main(void)
{
 
    Device_Init();
    Program_Init();
    Dw1000_Init();
  delay_ms(10);    
    Dw1000_App_Init();
    /* Loop forever initiating ranging exchanges. */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    usart_send[0]=0x55;
    usart_send[1]=0xAA;
    
    while(1)
    {
    
    if(g_start_send_flag)
    {
        g_start_send_flag = 0;        
        Tag_App();
    }
        IdleTask();        
if(g_com_map[DEV_ROLE]==0)
        Anchor_App();
    }
}