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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
 *******************************************************************************
 * @file     app_ble.c
 * @create   2023-08-01    
 * @author   Panchip BLE GROUP
 * @note
 * Copyright (c) 2022 Shanghai Panchip Microelectronics Co.,Ltd.
 *
 *******************************************************************************
 */
#include <assert.h>
#include <string.h>
 
#include "pan_ble.h"
#include "app.h"
#include "app_log.h"
#include "soc_api.h"
static void app_ble_set_mac_addr(void)
{
    /** set public address*/
    uint8_t pub_mac[6]={8,2,3,4,5,6};
    
#if CONFIG_USER_CHIP_MAC_ADDR
    pan10x_mac_addr_get(pub_mac);
#endif
    
#if APP_USER_ROLL_MAC_ADDR
    pan10x_roll_mac_addr_get(pub_mac);
#endif
    
    pan_misc_set_bd_addr(pub_mac);
}
 
/**
 * @brief: BLE Stack Enabled call-back.
 */
static void app_ble_enabled_cb(void)
{
#if CONFIG_BT_MAX_NUM_OF_PERIPHERAL
    /* Adv */
//    app_ble_adv_start();
#endif
 
#if CONFIG_BT_MAX_NUM_OF_CENTRAL 
    /* Scan */
    app_ble_scan_start();
#endif
}
 
/**
 * @brief: ble stack pre init call-back
 *         !!!Note: 1. The Bluetooth MAC Address must be set in this callback.
 *                  2. The BLE Services must be registered in this callback.
 *                  3. The SDP initialization must be executed in this callback.
 */
TimerHandle_t xTimerUser; 
uint16_t timer;
uint8_t shengji_flag;
extern struct queue //½á¹¹ÌåÀàÐÍ
{
uint8_t totalnum;//»ùÕ¾×ÜÊý
uint8_t flag_newanchor[anchor_max_num];//ÐÂÀÏ»ùÕ¾±íʾ
uint16_t id[anchor_max_num]; /*ID*/
uint16_t time[anchor_max_num];   /*ʱ¼ä*/
uint16_t fail_num[anchor_max_num]; //uwb½ÓÊÜʧ°ÜµÄ´ÎÊý
uint16_t rssi[anchor_max_num];//À¶ÑÀ²â³öµÄ¾àÀë
uint8_t uwb_tongxun_flag[anchor_max_num];//ÓëuwbͨѶ³É¹¦Ê§°ÜµÄ±ê־λ
uint16_t u16id[anchor_max_num]; /*u16ID*/    
}blequeue;
uint8_t slave_send_buf[100];
uint16_t shengji_time;
uint8_t only_one;
uint8_t send_flag;
void Spi_Zubao_Send(void)
{   
    if(blequeue.totalnum>10)
    {blequeue.totalnum=10;}
    memset(slave_send_buf,0,sizeof(slave_send_buf));
    slave_send_buf[0]=0X55;
    slave_send_buf[1]=0XAA;
    slave_send_buf[2]=0X41;
    
    slave_send_buf[3]=blequeue.totalnum*4+2;
    slave_send_buf[4]=blequeue.totalnum;
    memcpy(&slave_send_buf[5],blequeue.id,2*blequeue.totalnum);
    memcpy(&slave_send_buf[5+2*blequeue.totalnum],blequeue.rssi,2*blequeue.totalnum);
    slave_send_buf[5+4*blequeue.totalnum]=0x66;
    slave_send_buf[6+4*blequeue.totalnum]=0xBB;
//    HAL_UART_SendData(&UART0_OBJ, slave_send_buf,blequeue.totalnum*4+7,1000);
    HAL_UART_SendData_DMA(&UART0_OBJ, slave_send_buf,blequeue.totalnum*4+7);
//    blequeue.totalnum=0;
 
//    memset(blequeue.id,0,sizeof(blequeue.id));
//    memset(blequeue.u16id,0,sizeof(blequeue.u16id));
//    memset(blequeue.rssi,0,sizeof(blequeue.rssi));
}
void vTimerCallback( TimerHandle_t xTimer )
{    
    uint16_t aa,bb;
timer++;
//    Spi_Zubao_Send();
    if (shengji_flag==1) 
    {
    shengji_time++;
        if(shengji_time>600)
        {
        NVIC_SystemReset();
        }
    }
    for(aa=0;aa<blequeue.totalnum;aa++)
    {
    blequeue.time[aa]++;
        if(blequeue.time[aa]>3)
        {
           for(bb=aa;bb<blequeue.totalnum;bb++)
            {
                blequeue.id[bb]=blequeue.id[bb+1];
                blequeue.rssi[bb]=blequeue.rssi[bb+1];
                blequeue.time[bb]=blequeue.time[bb+1];
            }
        }
    }
}
static TaskHandle_t xTaskToNotify = NULL;
static void gpio_p02_input_callback(HAL_GPIO_IntMode intMode)
{
    BaseType_t xHigherPriorityTaskWoken = pdTRUE;
 
    /* Notify the task that KEY1 is pressed (P06 falling edge detected). */
//    vTaskNotifyGiveFromISR(xTaskToNotify, &xHigherPriorityTaskWoken);
    if (shengji_flag==0) 
        {
            if (HAL_GPIO_ReadPin(P0_2) == HAL_GPIO_LEVEL_LOW) 
                {
//                APP_LOG_INFO("==== HAL_GPIO_LEVEL_LOW ,½øÈëÉý¼¶Ä£Ê½====\n");
                } 
                else 
                {
//                APP_LOG_INFO("==== HAL_GPIO_LEVEL_HIGH ,½øÈëÉý¼¶Ä£Ê½====\n");
                }
//                shengji_flag=1;
//                only_one=1;
                #if CONFIG_BT_MAX_NUM_OF_PERIPHERAL
                    /* Adv */
//                    app_ble_adv_start();
//                shengji_start();
                #endif
                send_flag=1;
                
        }
}
static void gpio_init(void)
{
    /* Set pinmux of P06 to GPIO (default) */
    SYS_SET_MFP(P0, 2, GPIO);
 
    /* Init GPIO P06 to digital input mode */
    HAL_GPIO_InitTypeDef GPIO_InitStruct = {
        .mode   = HAL_GPIO_MODE_INPUT_DIGITAL,
        .pull   = HAL_GPIO_PULL_UP,
    };
    HAL_GPIO_Init(P0_2, &GPIO_InitStruct);
 
    /* Wait a while for internal pull-up resistor stable to avoid later interrupt false triggering */
    soc_busy_wait(10000);
 
    /* Enable input interrupt of GPIO P06 */
    HAL_GPIO_IntInitTypeDef GPIO_IntInitStruct = {
        .intMode        = HAL_GPIO_INT_FALLING,  // Change this option to see different interrupt behaviors
        .callbackFunc   = gpio_p02_input_callback,
        .debounce       = ENABLE,
    };
    HAL_GPIO_InterruptInit(P0_2, &GPIO_IntInitStruct);
 
    /* Enable NVIC GPIO0 IRQn */
    NVIC_EnableIRQ(GPIO0_IRQn);
}
void app_ble_pre_init_cb(void)
{
     /*
     * Configure clock of GPIO interrupt debouce
     * NOTE:
     * - This configuration affects all GPIOs with debounce enabled
     * - This API should only be called once at app init flow for safe
     */
    GPIO_SetDebounceTime(GPIO_DBCTL_DBCLKSRC_RCL, GPIO_DBCTL_DBCLKSEL_32);    // Set debounce time to 32 cycles of 32K Clock (about 1ms)
 
    /* Init P06 to digital input mode with interrupt and debounce enabled */
    gpio_init();
    
    /* Set MAC address. */
    app_ble_set_mac_addr();
 
    /* BLE Peripheral initilaization. */
    app_ble_periph_init();
 
    /* BLE Central initilaization. */
    app_ble_central_init();
    
    /* BLE Services initilaization. */
    app_ble_svc_init();
    
    img_mgmt_module_init();
    smp_ble_pkg_init();
    
    app_ble_thread_init();
    xTimerUser = xTimerCreate("Timer's name",pdMS_TO_TICKS(900),pdTRUE,( void * ) 0,vTimerCallback);                 
    if( xTimerUser != NULL ) {
    xTimerStart( xTimerUser, 0 );
    }
}
 
void app_ble_init(void)
{
    /* BLE Stack Initialization. */
    pan_ble_stack_init(app_ble_pre_init_cb, app_ble_enabled_cb);
}
 
void POLL(void)
{
    if(shengji_flag&&only_one)
    {
        app_ble_adv_start();
        only_one=0;
    }
    if(send_flag)
    {
    Spi_Zubao_Send();
    send_flag=0;    
    }
 
}
#define MYNEWT_VAL_APP_BLE_THREAD_STACK_SIZE        128
static TaskHandle_t app_ble_task_h;
void app_ble_thread_entry(void *parameter)
{
        while(1)
        {
            POLL();
            vTaskDelay((pdMS_TO_TICKS(1000)));
        }
vTaskDelete(NULL);
 
}
void app_ble_thread_init(void)
{
    xTaskCreate(app_ble_thread_entry, "app", MYNEWT_VAL(APP_BLE_THREAD_STACK_SIZE),
            NULL, configMAX_PRIORITIES - 3, &app_ble_task_h);
 
}