01_SDK/nimble/samples/bluetooth/ble_central_periph_ota/src/app_ble.c
@@ -14,7 +14,7 @@
#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*/
@@ -38,7 +38,7 @@
{
#if CONFIG_BT_MAX_NUM_OF_PERIPHERAL
   /* Adv */
   app_ble_adv_start();
//   app_ble_adv_start();
#endif
#if CONFIG_BT_MAX_NUM_OF_CENTRAL 
@@ -55,6 +55,7 @@
 */
TimerHandle_t xTimerUser; 
uint16_t timer;
uint8_t shengji_flag;
extern struct queue //结构体类型
{
uint8_t totalnum;//基站总数
@@ -67,6 +68,9 @@
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)
@@ -74,26 +78,116 @@
    memset(slave_send_buf,0,sizeof(slave_send_buf));
    slave_send_buf[0]=0X55;
    slave_send_buf[1]=0XAA;
    slave_send_buf[2]=blequeue.totalnum*4+2;
    slave_send_buf[3]=blequeue.totalnum;
    memcpy(&slave_send_buf[4],blequeue.id,2*blequeue.totalnum);
    memcpy(&slave_send_buf[4+2*blequeue.totalnum],blequeue.rssi,2*blequeue.totalnum);
    slave_send_buf[4+4*blequeue.totalnum]=0x66;
    slave_send_buf[5+4*blequeue.totalnum]=0xBB;
    HAL_UART_SendData(&UART0_OBJ, slave_send_buf,blequeue.totalnum*4+6,1000);
    blequeue.totalnum=0;
    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));
//    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();
//    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();
@@ -109,6 +203,7 @@
   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 );
@@ -121,4 +216,35 @@
   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);
}