Browse Source

蓝牙驱动添加

樊春春 2 months ago
parent
commit
1eabe85515

+ 681 - 0
modules/ble/ble.c

@@ -0,0 +1,681 @@
+/************************************************************************************************
+ *                                          Include                                              *
+ ************************************************************************************************/
+
+#include "ble.h"
+#include "ble_cmd.h"
+#include "ble_sn2model.h"
+#include "stm32f4xx_gpio.h"
+#include "systick.h"
+#include "uart.h"
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+/************************************************************************************************
+ *                                          Config                                                *
+ ************************************************************************************************/
+
+uint8_t             zero_sn[];
+extern BLE_CMD_ASYN g_ble_cmd_asyn_table[];
+
+static uint8_t BUSY_IND                     = 0; // 0、空闲;1、繁忙
+static uint8_t AUTH_IND                     = 0; // 0、未认证;1、认证
+static uint8_t BLE_CONN_FLG                 = 0;
+uint8_t        g_ble_mac[MAC_LEN]           = {0};
+uint8_t        g_ble_name[BLE_NAME_LEN + 1] = {0};
+uint8_t        g_ble_adv_data[28]           = {0};
+uint8_t        g_ble_rst                    = 0; // 0、运行;1、复位
+BLE_CMD_CACHE  g_ble_cmd_cache;                  // 命令缓存
+/************************************************************************************************
+ *                                          Data structs                                         *
+ ************************************************************************************************/
+
+void ble_reset(void)
+{
+    GPIO_ResetBits(BT_RST_PORT, BT_RST_PIN);
+    ms_delay(1);
+    GPIO_SetBits(BT_RST_PORT, BT_RST_PIN);
+}
+
+void ble_init(void)
+{
+    usart_config_init(&usart2_context, 9600);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
+
+    GPIO_InitTypeDef GPIO_StructInit;
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_OUT;
+    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
+    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
+    GPIO_StructInit.GPIO_Pin   = BT_RST_PIN;
+    GPIO_Init(BT_RST_PORT, &GPIO_StructInit);
+
+    ble_reset();
+
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
+    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
+    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_StructInit.GPIO_Pin   = BT_BUSY_IND_PIN;
+    GPIO_Init(BT_BUSY_IND_PORT, &GPIO_StructInit);
+
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
+    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
+    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
+    GPIO_StructInit.GPIO_Pin   = BT_CONN_IND_PIN;
+    GPIO_Init(BT_CONN_IND_PORT, &GPIO_StructInit);
+}
+
+// 蓝牙名称
+int32_t ble_cmd08_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
+{
+    RSP_CMD08 *ptr;
+
+    if (status != 0)
+    {
+        return status;
+    }
+    ptr = (RSP_CMD08 *)data;
+    strcpy(g_ble_name, ptr->name);
+    return 0;
+}
+
+// MAC地址
+int32_t ble_cmd18_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
+{
+    RSP_CMD18 *ptr;
+
+    if (status != 0)
+    {
+        return status;
+    }
+    ptr = (RSP_CMD18 *)data;
+    if (ptr->item0.item == 3)
+    {
+        memcpy(g_ble_mac, ptr->item3.mac, sizeof(g_ble_mac));
+        // OPP_SWAP_N(g_ble_mac, mac_len);
+    }
+
+    return 0;
+}
+
+// 广播数据
+int32_t ble_cmd0e_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
+{
+    RSP_CMD0E *ptr;
+
+    if (status != 0)
+    {
+        return status;
+    }
+    ptr = (RSP_CMD0E *)data;
+    memcpy(g_ble_adv_data, ptr->adv_data, sizeof(g_ble_adv_data));
+    return 0;
+}
+
+// 序列初始化
+int32_t ble_cfg_init_asyn(EN_BLE_INIT_CMD cmd)
+{
+    int32_t ret;
+
+    REQ_CMD07 cmd07;
+    REQ_CMD08 cmd08;
+
+    uint8_t SN[SN_LEN]           = {0};
+    uint8_t UPP[SN_LEN]          = {0};
+    uint8_t model[MODEL_LEN]     = {0};
+    uint8_t upp_model[MODEL_LEN] = {0};
+
+    uint8_t buff[64] = {0};
+
+    if (g_ble_cmd_cache.is_used == 1)
+    {
+        return 1;
+    }
+
+    if (cmd == BLE_INIT_CMD_NAME_QRY)
+    {
+        g_ble_cmd_cache.is_used = 1;
+        g_ble_cmd_cache.cmd     = 0x08;
+        g_ble_cmd_cache.len     = sizeof(REQ_CMD08);
+        memcpy(g_ble_cmd_cache.data, &cmd08, sizeof(REQ_CMD08));
+        g_ble_cmd_cache.p_func = ble_cmd08_action_asyn;
+        g_ble_cmd_cache.tick   = 0;
+    }
+
+    if (cmd == BLE_INIT_CMD_NAME_CFG)
+    {
+        // int32_t len = alientek_sn_get(SN);
+        int32_t len = 12;
+        ret         = sn_2_model(SN, 1, model);
+
+        if (ret == 0)
+        {
+            len = sprintf(buff, "%s", model);
+            // upp_sn_get(UPP);
+            if (memcpy(UPP, zero_sn, SN_LEN) != 0)
+            {
+                ret = sn_2_model((uint8_t *)UPP, 1, upp_model);
+                if (ret == 0)
+                {
+                    sprintf(buff + len, " %s", upp_model);
+                }
+            }
+            if (strcpy(g_ble_name, buff) != 0)
+            {
+                strcpy(cmd07.name, buff);
+                g_ble_cmd_cache.cmd = 0x07;
+                g_ble_cmd_cache.len = strlen(buff);
+                strcpy(&g_ble_cmd_cache.data, buff);
+                g_ble_cmd_cache.p_func = NULL;
+                g_ble_cmd_cache.tick   = 0;
+            }
+        }
+    }
+
+    if (cmd == BLE_INIT_CMD_TXWPR_CFG)
+    {
+        REQ_CMD0B req0b;
+        req0b.tx_power = 8;
+        ret            = ble_exec_cmd0b(&req0b);
+
+        if (ret == 0)
+        {
+        }
+    }
+    return 0;
+}
+
+int32_t ble_cfg_init_do_asyn(void)
+{
+    static EN_BLE_INIT_CMD cmd = BLE_INIT_CMD_NAME_QRY;
+
+    int32_t ret;
+
+    ret = ble_cfg_init_asyn(cmd);
+
+    if (ret == 0)
+    {
+        cmd++;
+    }
+
+    if (cmd == BLE_INIT_CMD_MAX)
+    {
+        cmd = BLE_INIT_CMD_NAME_QRY;
+        return 0;
+    }
+    return 1;
+}
+
+int32_t ble_cfg_init(void)
+{
+    int32_t   ret;
+    REQ_CMD07 cmd07;
+    REQ_CMD08 cmd08;
+    RSP_CMD08 rsp08;
+    uint8_t   SN[SN_LEN]       = {0};
+    uint8_t   model[MODEL_LEN] = {0};
+
+    memset(&rsp08, 0, sizeof(RSP_CMD08));
+    ret = ble_exec_cmd08(&cmd08, &rsp08);
+    if (ret == 0)
+    {
+    }
+
+    int32_t len = 12;
+    // int32_t len = alientek_sn_get(SN);
+    ret = sn_2_model(SN, 1, model);
+
+    if (ret == 0)
+    {
+        if (strcmp(rsp08.name, model) != 0)
+        {
+            strcpy(cmd07.name, model);
+            ret = ble_exec_cmd07(&cmd07, CMD_T0);
+            if (ret != 0)
+            {
+            }
+        }
+    }
+    else
+    {
+    }
+
+    return 0;
+}
+
+int32_t ble_cfg_query_asyn(EN_BLE_INIT_CMD cmd)
+{
+    REQ_CMD18 cmd18;
+    REQ_CMD0E cmd0e;
+    uint8_t   buff[64] = {0};
+    int32_t   len      = 0;
+
+    if (g_ble_cmd_cache.is_used == 1)
+    {
+        return 1;
+    }
+
+    if (cmd == BLE_QRY_CMD_ADV_QRY)
+    {
+        g_ble_cmd_cache.is_used = 1;
+        g_ble_cmd_cache.cmd     = 0x0e;
+        g_ble_cmd_cache.len     = sizeof(REQ_CMD0E);
+        memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD0E));
+        g_ble_cmd_cache.p_func = ble_cmd0e_action_asyn;
+        g_ble_cmd_cache.tick   = 0;
+    }
+
+    if (cmd == BLE_QRY_CMD_MAC_QRY)
+    {
+        cmd18.item              = 3;
+        g_ble_cmd_cache.is_used = 1;
+        g_ble_cmd_cache.cmd     = 0x18;
+        g_ble_cmd_cache.len     = sizeof(REQ_CMD18);
+        memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD18));
+        g_ble_cmd_cache.p_func = ble_cmd18_action_asyn;
+        g_ble_cmd_cache.tick   = 0;
+    }
+
+    if (cmd == BLE_QRY_CMD_ADV_CFG)
+    {
+        if (memcpy(g_ble_mac, g_ble_adv_data, MAC_LEN) != 0)
+        {
+            buff[len++] = 1;
+            buff[len++] = 1 + MAC_LEN;
+            buff[len++] = 0xFF;
+            memcpy(&buff[len], g_ble_mac, MAC_LEN);
+            len += MAC_LEN;
+            g_ble_cmd_cache.is_used = 1;
+            g_ble_cmd_cache.cmd     = 0x0d;
+            g_ble_cmd_cache.len     = len;
+            memcpy(g_ble_cmd_cache.data, buff, len);
+            g_ble_cmd_cache.p_func = NULL;
+            g_ble_cmd_cache.tick   = 0;
+        }
+    }
+    return 0;
+}
+
+int32_t ble_cfg_query_do_asyn(void)
+{
+    static EN_BLE_QRY_CMD cmd = BLE_QRY_CMD_ADV_QRY;
+
+    int32_t ret;
+
+    ret = ble_cfg_query_asyn(cmd);
+
+    if (ret == 0)
+    {
+        cmd++;
+    }
+
+    if (cmd == BLE_QRY_CMD_MAX)
+    {
+        cmd = BLE_QRY_CMD_ADV_QRY;
+        return 0;
+    }
+    return 1;
+}
+
+int32_t ble_cfg_query(void)
+{
+    int32_t   ret;
+    REQ_CMD18 cmd18;
+    RSP_CMD18 rsp18;
+    REQ_CMD0A cmd0a;
+    RSP_CMD0A rsp0a;
+    REQ_CMD0C cmd0c;
+    RSP_CMD0C rsp0c;
+    REQ_CMD0E cmd0e;
+    RSP_CMD0E rsp0e;
+    uint8_t   buff[64]      = {0};
+    int32_t   len           = 0;
+    int32_t   adv_data_len  = 0;
+    uint8_t  *adv_data_ptr  = NULL;
+    uint8_t   adv_data_uuid = 0;
+
+    ret = ble_exec_cmd0e(&cmd0e, &rsp0e);
+    if (ret == 0)
+    {
+        adv_data_len  = rsp0e.adv_data[0];
+        adv_data_uuid = rsp0e.adv_data[1];
+        adv_data_ptr  = &rsp0e.adv_data[2];
+
+        // len += sprintf(buff+len)
+    }
+
+    cmd18.item = 3;
+    ret        = ble_exec_cmd18(&cmd18, &rsp18);
+    if (ret == 0)
+    {
+        memcpy(g_ble_mac, rsp18.item3.mac, len);
+        // OPP_SWAP_N(g_ble_mac, mac_len);
+        if (adv_data_len == 0 || adv_data_ptr == NULL || memcmp(rsp18.item3.mac, adv_data_ptr, MAC_LEN) != 0)
+        {
+            ret = ble_exec_cmd0d(1, rsp18.item3.mac, MAC_LEN);
+
+            if (ret != 0)
+            {
+            }
+        }
+    }
+    else
+    {
+    }
+
+    cmd18.item = 5;
+    ret        = ble_exec_cmd18(&cmd18, &rsp18);
+    if (ret == 0)
+    {
+    }
+
+    ret = ble_exec_cmd0a(&cmd0a, &rsp0a);
+    if (ret == 0)
+    {
+    }
+
+    ret = ble_exec_cmd0c(&cmd0c, &rsp0c);
+    if (ret == 0)
+    {
+    }
+}
+
+int32_t BUSY_IND_LOOP(void)
+{
+    static uint32_t tick = 0;
+    static uint32_t cnt  = 0;
+    uint8_t         busy;
+
+    if (tick == 0)
+    {
+        tick = get_systick_ms();
+    }
+
+    if (get_systick_ms() - tick > 50)
+    {
+        if (cnt++ < 2)
+        {
+            return 0;
+        }
+        else
+        {
+            cnt = 0;
+        }
+
+        if (GPIO_ReadInputDataBit(BT_BUSY_IND_PORT, BT_BUSY_IND_PIN) == 0)
+        {
+            busy = 1;
+        }
+        else
+        {
+            busy = 0;
+        }
+
+        if (BUSY_IND != busy)
+        {
+            BUSY_IND = busy;
+        }
+    }
+
+    return 0;
+}
+
+int32_t CONN_IND_LOOP(void)
+{
+    static uint32_t tick = 0;
+    static uint32_t cnt  = 0;
+    uint8_t         conn;
+
+    if (tick == 0)
+    {
+        tick = get_systick_ms();
+    }
+
+    if (get_systick_ms() - tick > 50)
+    {
+        if (cnt++ < 2)
+        {
+            return 0;
+        }
+        else
+        {
+            cnt = 0;
+        }
+
+        if (GPIO_ReadInputDataBit(BT_CONN_IND_PORT, BT_CONN_IND_PIN) == 0)
+        {
+            conn = 1;
+        }
+        else
+        {
+            conn = 0;
+        }
+
+        if (BLE_CONN_FLG != conn)
+        {
+            BLE_CONN_FLG = conn;
+            if (BLE_CONN_FLG == 0)
+            {
+                AUTH_IND = 0;
+            }
+        }
+    }
+
+    return 0;
+}
+
+int32_t auth_ind_set(uint8_t auth)
+{
+    AUTH_IND = auth;
+    return 0;
+}
+
+int32_t ble_is_conn(void)
+{
+    return BLE_CONN_FLG;
+}
+
+int32_t ble_get_busy_port_sts(void)
+{
+    return BUSY_IND;
+}
+
+int32_t ble_is_busy(void)
+{
+    if (BLE_CONN_FLG == 0 || BUSY_IND == 1 || g_ble_cmd_cache.is_used == 1)
+    {
+        return 1;
+    }
+    return 0;
+}
+
+int32_t ble_is_auth(void)
+{
+    return AUTH_IND;
+}
+
+int32_t ble_data_recv(uint8_t data[], uint8_t length, int32_t ms)
+{
+    static uint32_t tick = 0;
+    uint8_t         rdata[BLE_MTU];
+    int32_t         rlen;
+
+    if (tick == 0)
+    {
+        tick = get_systick_ms();
+    }
+
+    do
+    {
+        rlen = usart_read_ble_format(&usart2_context);
+        if (rlen > 0)
+        {
+            usart_read_update(&usart2_context, rlen, (uint8_t *)rdata);
+
+            if (rdata[2] == g_ble_cmd_cache.cmd)
+            {
+                if (g_ble_cmd_cache.p_func != NULL)
+                {
+                    g_ble_cmd_cache.p_func(rdata[2], rdata[4], rdata, rlen);
+                }
+
+                memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
+            }
+
+            if (rdata[2] == 0x42)
+            {
+                // ble_2_app((uint8_t *)&rdata[4], rlen - 4);
+                tick = 0;
+                return 0;
+            }
+            else
+            {
+                memcpy(data, rdata, rlen);
+                tick = 0;
+                return rlen;
+            }
+        }
+        else
+        {
+        }
+    } while (get_systick_ms() - tick < ms);
+    tick = 0;
+    return -1;
+}
+
+int32_t ble_send_data(uint8_t data[], int32_t length)
+{
+    if (length > BLE_MTU)
+    {
+        return 1;
+    }
+    g_ble_cmd_cache.is_used = 1;
+    g_ble_cmd_cache.cmd     = 0x41;
+    g_ble_cmd_cache.len     = length;
+    memcpy(g_ble_cmd_cache.data, data, length);
+    g_ble_cmd_cache.tick   = 0;
+    g_ble_cmd_cache.p_func = NULL;
+    return 0;
+}
+
+int32_t ble_rcv_loop(void)
+{
+    uint8_t rdata[BLE_MTU];
+    ble_data_recv(rdata, sizeof(rdata), 0);
+    return 0;
+}
+
+int32_t ble_name_modify(void)
+{
+    static uint8_t last_sn[SN_LEN] = {0};
+
+    if (ble_is_conn())
+    {
+        return 0;
+    }
+}
+
+int32_t ble_tx_loop(void)
+{
+    static uint8_t count = 0;
+
+    if (g_ble_cmd_cache.is_used == 1)
+    {
+        if (g_ble_cmd_cache.tick == 0)
+        {
+            g_ble_cmd_cache.tick = get_systick_ms();
+            count                = 0;
+        }
+
+        if (count == 0 || get_systick_ms() - g_ble_cmd_cache.tick > 3000)
+        {
+            for (int32_t i = 0; i < countof_asyn_cmd_table(); i++)
+            {
+                if (g_ble_cmd_asyn_table[i].cmd == g_ble_cmd_cache.cmd && g_ble_cmd_asyn_table[i].p_func != NULL)
+                {
+                    g_ble_cmd_asyn_table[i].p_func(g_ble_cmd_cache.cmd, g_ble_cmd_cache.data, g_ble_cmd_cache.len);
+                }
+            }
+
+            g_ble_cmd_cache.tick = get_systick_ms();
+            count++;
+        }
+
+        if (count > 10)
+        {
+            ble_reset();
+            g_ble_rst = 1;
+            memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
+            count = 0;
+        }
+    }
+
+    return 0;
+}
+
+int32_t ble_loop(void)
+{
+    static uint8_t  init  = 0;
+    static uint32_t tick  = 0;
+    static uint8_t  init1 = 0;
+    static uint32_t tick1 = 0;
+    int32_t         ret;
+
+    if (g_ble_rst == 1)
+    {
+        init      = 0;
+        init1     = 0;
+        g_ble_rst = 0;
+    }
+
+    if (!init)
+    {
+        if (tick == 0)
+            tick = get_systick_ms();
+
+        if (get_systick_ms() - tick > BLE_CFG_DELAY_MS)
+        {
+            ret = ble_cfg_init_do_asyn();
+            if (ret == 0)
+            {
+                init = 1;
+            }
+        }
+    }
+
+    if (init && !init1)
+    {
+        if (tick1 == 0)
+            tick1 = get_systick_ms();
+
+        if (get_systick_ms() - tick1 > BLE_QRY_DELAY_MS)
+        {
+            ret = ble_cfg_query_do_asyn();
+            if (ret == 0)
+            {
+                init1 = 1;
+            }
+        }
+    }
+
+    ble_rcv_loop();
+
+    if (init && init1)
+    {
+        BUSY_IND_LOOP();
+        CONN_IND_LOOP();
+        ble_name_modify();
+    }
+
+    ble_tx_loop();
+
+    return 0;
+}
+/************************************************************************************************
+ *                                          implements                                           *
+ ************************************************************************************************/

+ 102 - 0
modules/ble/ble.h

@@ -0,0 +1,102 @@
+#ifndef __BLE_H
+#define __BLE_H
+
+#include "ble_core.h"
+#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __BLE_H_GLOBAL
+#define __BLE_H_EXTERN
+#else
+#define __BLE_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+#define BT_BUSY_IND_PORT GPIOB
+#define BT_BUSY_IND_PIN  GPIO_Pin_12
+#define BT_CONN_IND_PORT GPIOB
+#define BT_CONN_IND_PIN  GPIO_Pin_13
+#define BT_RST_PORT      GPIOC
+#define BT_RST_PIN       GPIO_Pin_2
+
+#define BLE_NAME             "ALIENTEK"
+#define BLE_CODE             0
+#define BLE_CFG_DELAY_MS     1020
+#define BLE_START_DELAY_MS   500
+#define BLE_QRY_DELAY_MS     500
+#define BLE_RESTORE_DELAY_MS 1000
+
+#define MAC_LEN   6
+#define SN_LEN    10
+#define MODEL_LEN 6
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+#pragma pack(1)
+typedef enum
+{
+    BLE_INIT_CMD_NAME_QRY = 0,
+    BLE_INIT_CMD_NAME_CFG,
+    BLE_INIT_CMD_CODE_CFG,
+    BLE_INIT_CMD_TXWPR_CFG,
+    BLE_INIT_CMD_MAX,
+} EN_BLE_INIT_CMD;
+
+typedef enum
+{
+    BLE_QRY_CMD_ADV_QRY = 0,
+    BLE_QRY_CMD_MAC_QRY,
+    BLE_QRY_CMD_ADV_CFG,
+    BLE_QRY_CMD_MAX,
+} EN_BLE_QRY_CMD;
+
+#define BLE_MTU 100
+
+typedef struct
+{
+    uint8_t is_used;
+    uint8_t cmd;
+    uint8_t len;
+    uint8_t data[BLE_MTU];
+    int32_t (*p_func)(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len);
+    uint32_t tick;
+} BLE_CMD_CACHE;
+
+#pragma pack(0)
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void    ble_init(void);
+int32_t ble_loop(void);
+int32_t ble_send_data(uint8_t data[], int32_t length);
+int32_t ble_data_recv(uint8_t data[], uint8_t length, int32_t ms);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __BLE_H

+ 214 - 0
modules/ble/ble_cmd.c

@@ -0,0 +1,214 @@
+#include "ble_cmd.h"
+#include "ble.h"
+#include "uart.h"
+#include <stdint.h>
+#include <string.h>
+
+BLE_CMD_ASYN g_ble_cmd_asyn_table[] = {
+    {0x05, ble_exec_cmd_asyn},
+    {0x07, ble_exec_cmd07_asyn},
+    {0x08, ble_exec_cmd_asyn},
+    {0x0d, ble_exec_cmd_asyn},
+    {0x0e, ble_exec_cmd_asyn},
+    {0x18, ble_exec_cmd_asyn},
+    {0x41, ble_exec_cmd_asyn},
+};
+
+int32_t ble_send_to_ec95(uint8_t cmd, uint8_t data[], uint8_t length)
+{
+    uint8_t s_data[EC95_MTU + 4] = {0};
+    int32_t len                  = 0;
+
+    if (length > EC95_MTU)
+    {
+        return 2;
+    }
+
+    s_data[len++] = 0x01;
+    s_data[len++] = 0xFC;
+    s_data[len++] = cmd;
+    s_data[len++] = length;
+
+    for (int32_t i = 0; i < length; i++)
+    {
+        s_data[len++] = data[i];
+    }
+
+    usart_send_it(&usart2_context, s_data, len);
+    return 0;
+}
+
+int32_t ble_cmd_exec(uint8_t cmd, uint8_t idata[], int32_t ilen, uint8_t odata[], int32_t *olen, int32_t ms)
+{
+    int32_t ret;
+    ret = ble_send_to_ec95(cmd, idata, ilen);
+    if (ret != 0)
+    {
+        return 1;
+    }
+    ret = ble_data_recv(odata, *olen, ms);
+
+    if (ret <= 0)
+    {
+        return 2;
+    }
+    return 0;
+}
+
+int32_t ble_exec_cmd05(REQ_CMD05 *req)
+{
+    RSP_COMM_CMD rsp;
+    int32_t      ret;
+    int32_t      len = sizeof(RSP_COMM_CMD);
+
+    ret = ble_cmd_exec(0x05, (uint8_t *)req, sizeof(REQ_CMD05), (uint8_t *)&rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp.status;
+}
+
+int32_t ble_exec_cmd07(REQ_CMD07 *req, int32_t timeout)
+{
+    RSP_COMM_CMD rsp;
+    int32_t      ret;
+    int32_t      len = sizeof(RSP_COMM_CMD);
+
+    ret = ble_cmd_exec(0x07, (uint8_t *)req->name, strlen(req->name), (uint8_t *)&rsp, &len, timeout);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp.status;
+}
+
+int32_t ble_exec_cmd08(REQ_CMD08 *req, RSP_CMD08 *rsp)
+{
+    int32_t ret;
+    int32_t len = sizeof(RSP_CMD08);
+
+    ret = ble_cmd_exec(0x08, (uint8_t *)req, sizeof(REQ_CMD08), (uint8_t *)rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp->hdr.status;
+}
+
+int32_t ble_exec_cmd0a(REQ_CMD0A *req, RSP_CMD0A *rsp)
+{
+    int32_t ret;
+    int32_t len = sizeof(RSP_CMD0A);
+
+    ret = ble_cmd_exec(0x0A, (uint8_t *)req, sizeof(REQ_CMD0A), (uint8_t *)rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp->hdr.status;
+}
+
+int32_t ble_exec_cmd0b(REQ_CMD0B *req)
+{
+    RSP_COMM_CMD rsp;
+    int32_t      ret;
+    int32_t      len = sizeof(RSP_COMM_CMD);
+
+    ret = ble_cmd_exec(0x0B, (uint8_t *)req, sizeof(REQ_CMD0B), (uint8_t *)&rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp.status;
+}
+
+int32_t ble_exec_cmd0c(REQ_CMD0C *req, RSP_CMD0C *rsp)
+{
+    int32_t ret;
+    int32_t len = sizeof(RSP_CMD0C);
+
+    ret = ble_cmd_exec(0x0C, (uint8_t *)req, sizeof(REQ_CMD0C), (uint8_t *)rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp->hdr.status;
+}
+
+int32_t ble_exec_cmd0d(uint8_t save_flg, uint8_t *adv_data, uint8_t adv_data_len)
+{
+    RSP_COMM_CMD rsp;
+    int32_t      ret;
+    uint8_t      req[24] = {0};
+    int32_t      len     = 0;
+
+    req[len++] = save_flg;
+    req[len++] = 1 + adv_data_len;
+    req[len++] = 0xFF;
+    memcpy(&req[len], adv_data, adv_data_len);
+    len += adv_data_len;
+
+    ret = ble_cmd_exec(0x0D, (uint8_t *)req, len, (uint8_t *)&rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return adv_data_len;
+}
+
+int32_t ble_exec_cmd0e(REQ_CMD0E *req, RSP_CMD0E *rsp)
+{
+    int32_t ret;
+    int32_t len = sizeof(RSP_CMD0E);
+
+    ret = ble_cmd_exec(0x0E, (uint8_t *)req, sizeof(REQ_CMD0E), (uint8_t *)rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp->hdr.status;
+}
+
+int32_t ble_exec_cmd18(REQ_CMD18 *req, RSP_CMD18 *rsp)
+{
+    int32_t ret;
+    int32_t len = sizeof(RSP_CMD18);
+
+    ret = ble_cmd_exec(0x0E, (uint8_t *)req, sizeof(REQ_CMD18), (uint8_t *)rsp, &len, CMD_T0);
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return rsp->hdr.status;
+}
+
+int32_t ble_exec_cmd_asyn(uint8_t cmd, uint8_t *data, uint8_t length)
+{
+    int32_t ret;
+    ret = ble_send_to_ec95(cmd, (uint8_t *)data, length);
+
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return 0;
+}
+
+int32_t ble_exec_cmd07_asyn(uint8_t cmd, uint8_t *data, uint8_t length)
+{
+    int32_t    ret;
+    REQ_CMD07 *req = (REQ_CMD07 *)data;
+    ret            = ble_send_to_ec95(0x07, (uint8_t *)req->name, strlen(req->name));
+
+    if (ret != 0)
+    {
+        return -1;
+    }
+    return 0;
+}
+
+int32_t countof_asyn_cmd_table(void)
+{
+    return sizeof(g_ble_cmd_asyn_table) / sizeof(BLE_CMD_ASYN);
+}

+ 223 - 0
modules/ble/ble_cmd.h

@@ -0,0 +1,223 @@
+#ifndef __BLE_CMD_H
+#define __BLE_CMD_H
+
+#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __BLE_CMD_H_GLOBAL
+#define __BLE_CMD_H_EXTERN
+#else
+#define __BLE_CMD_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                           *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                         *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                             *
+ ************************************************************************************************/
+
+#include "uart.h"
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+#define EC95_MTU     252
+#define BLE_NAME_LEN 20
+#define BLE_CODE_LEN 4
+
+#define CMD_T0   40
+#define CMD18_T0 80
+#define CMD19_T0 180
+#define CMD41_T0 180
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+#pragma pack(1)
+
+typedef struct
+{
+    uint8_t head[2];
+    uint8_t cmd;
+    uint8_t len;
+    uint8_t status;
+} RSP_COMM_CMD;
+
+typedef struct
+{
+    uint8_t password[BLE_CODE_LEN];
+} REQ_CMD05;
+
+typedef struct
+{
+    uint8_t name[BLE_NAME_LEN + 1];
+} REQ_CMD07;
+
+typedef struct
+{
+} REQ_CMD08;
+
+typedef struct
+{
+} REQ_CMD0A;
+
+typedef struct
+{
+    uint8_t tx_power; // 发射功率
+} REQ_CMD0B;
+
+typedef struct
+{
+} REQ_CMD0C;
+
+typedef struct
+{
+} REQ_CMD0E;
+
+typedef struct
+{
+    uint8_t state; // 0,关闭蓝牙,1,开启蓝牙 2、切换到广播
+} REQ_CMD13;
+
+typedef struct
+{
+} REQ_CMD14;
+
+typedef struct
+{
+    uint8_t state; // 0,自动更新MTU,关闭更新MTU
+} REQ_CMD16;
+
+typedef struct
+{
+    uint8_t item; // 0,产品型号, 1,固件版本 2,源码版本
+} REQ_CMD18;
+
+typedef struct
+{
+    uint8_t item; // 0,复位重启, 1,恢复出厂设置
+} REQ_CMD19;
+
+typedef struct
+{
+    RSP_COMM_CMD hdr;
+    uint8_t      name[BLE_NAME_LEN + 1];
+} RSP_CMD08;
+
+typedef struct
+{
+    RSP_COMM_CMD hdr;
+    uint16_t     adv_inter_val; // 广播间隔
+} RSP_CMD0A;
+
+typedef struct
+{
+    RSP_COMM_CMD hdr;
+    uint8_t      tx_power;
+} RSP_CMD0C;
+
+typedef struct
+{
+    RSP_COMM_CMD hdr;
+    uint8_t      adv_data[28]; // 自动广播数据
+} RSP_CMD0E;
+
+typedef struct
+{
+    RSP_COMM_CMD hdr;
+    uint8_t      state; // 0,空闲,1,广播
+} RSP_CMD14;
+
+typedef struct
+{
+    uint8_t item;
+    uint8_t model[17]; // 产品型号
+} RSP_CMD18_ITEM0;
+
+typedef struct
+{
+    uint8_t item;
+    uint8_t fwv[5]; // 固件版本
+} RSP_CMD18_ITEM1;
+
+typedef struct
+{
+    uint8_t item;
+    uint8_t ver[16]; // 源码版本
+} RSP_CMD18_ITEM2;
+
+typedef struct
+{
+    uint8_t item;
+    uint8_t mac[6]; // mac地址
+} RSP_CMD18_ITEM3;
+
+typedef struct
+{
+    uint8_t item;
+    uint8_t reason; // 最后一次断开原因
+} RSP_CMD18_ITEM4;
+
+typedef struct
+{
+    uint8_t item;
+    uint8_t mtu; // mtu
+} RSP_CMD18_ITEM5;
+
+typedef struct
+{
+    RSP_COMM_CMD hdr;
+    union
+    {
+        RSP_CMD18_ITEM0 item0;
+        RSP_CMD18_ITEM1 item1;
+        RSP_CMD18_ITEM2 item2;
+        RSP_CMD18_ITEM3 item3;
+        RSP_CMD18_ITEM4 item4;
+        RSP_CMD18_ITEM5 item5;
+    };
+} RSP_CMD18;
+
+typedef struct
+{
+    uint8_t cmd;
+    int32_t (*p_func)(uint8_t cmd, uint8_t *data, uint8_t len);
+} BLE_CMD_ASYN;
+
+#pragma pack(0)
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+int32_t ble_exec_cmd_asyn(uint8_t cmd, uint8_t *data, uint8_t length);
+int32_t ble_exec_cmd07_asyn(uint8_t cmd, uint8_t *data, uint8_t length);
+int32_t ble_exec_cmd05(REQ_CMD05 *req);
+int32_t ble_exec_cmd07(REQ_CMD07 *req, int32_t timeout);
+int32_t ble_exec_cmd08(REQ_CMD08 *req, RSP_CMD08 *rsp);
+int32_t ble_exec_cmd0a(REQ_CMD0A *req, RSP_CMD0A *rsp);
+int32_t ble_exec_cmd0b(REQ_CMD0B *req);
+int32_t ble_exec_cmd0c(REQ_CMD0C *req, RSP_CMD0C *rsp);
+int32_t ble_exec_cmd0d(uint8_t save_flg, uint8_t *adv_data, uint8_t adv_data_len);
+int32_t ble_exec_cmd0e(REQ_CMD0E *req, RSP_CMD0E *rsp);
+int32_t ble_exec_cmd18(REQ_CMD18 *req, RSP_CMD18 *rsp);
+int32_t countof_asyn_cmd_table(void);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __BLE_CMD_H

+ 0 - 63
modules/ble/ble_core.c

@@ -1,63 +0,0 @@
-/************************************************************************************************
- *                                          Include                                              *
- ************************************************************************************************/
-#include "ble_core.h"
-#include "stm32f4xx_gpio.h"
-#include "systick.h"
-#include "uart.h"
-#include <stdint.h>
-/************************************************************************************************
- *                                          Config                                                *
- ************************************************************************************************/
-
-/************************************************************************************************
- *                                          Data structs                                         *
- ************************************************************************************************/
-void ble_init(void)
-{
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
-
-    GPIO_InitTypeDef GPIO_StructInit;
-    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_OUT;
-    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
-    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
-    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = BT_RST_PIN;
-    GPIO_Init(BT_RST_PORT, &GPIO_StructInit);
-
-    GPIO_ResetBits(BT_RST_PORT, BT_RST_PIN);
-    ms_delay(1);
-    GPIO_SetBits(BT_RST_PORT, BT_RST_PIN);
-
-    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
-    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
-    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
-    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_NOPULL;
-    GPIO_StructInit.GPIO_Pin   = BT_BUSY_IND_PIN;
-    GPIO_Init(BT_BUSY_IND_PORT, &GPIO_StructInit);
-
-    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
-    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
-    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
-    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = BT_CONN_IND_PIN;
-    GPIO_Init(BT_CONN_IND_PORT, &GPIO_StructInit);
-}
-uint8_t conn_sta = 0;
-uint8_t busy_sta = 0;
-void    ble_send(void)
-{
-    conn_sta     = GPIO_ReadInputDataBit(BT_CONN_IND_PORT, BT_CONN_IND_PIN);
-    busy_sta     = GPIO_ReadInputDataBit(BT_BUSY_IND_PORT, BT_BUSY_IND_PIN);
-    uint8_t a[5] = {0};
-    a[0]         = 0x01;
-    a[1]         = 0xFC;
-    a[2]         = 0x00;
-    a[3]         = 0x01;
-    a[4]         = 0x01;
-    usart_send_it(&usart2_context, a, 5);
-}
-/************************************************************************************************
- *                                          implements                                           *
- ************************************************************************************************/

+ 9 - 9
modules/ble/ble_core.h

@@ -31,15 +31,15 @@ extern "C" {
 /************************************************************************************************
 /************************************************************************************************
  *                                          Defines                                              *
  *                                          Defines                                              *
  ************************************************************************************************/
  ************************************************************************************************/
-#define BT_BUSY_IND_PORT GPIOB
-#define BT_BUSY_IND_PIN  GPIO_Pin_12
-#define BT_CONN_IND_PORT GPIOB
-#define BT_CONN_IND_PIN  GPIO_Pin_13
-#define BT_RST_PORT      GPIOC
-#define BT_RST_PIN       GPIO_Pin_2
-
-void ble_init(void);
-void ble_send(void);
+// #define BT_BUSY_IND_PORT GPIOB
+// #define BT_BUSY_IND_PIN  GPIO_Pin_12
+// #define BT_CONN_IND_PORT GPIOB
+// #define BT_CONN_IND_PIN  GPIO_Pin_13
+// #define BT_RST_PORT      GPIOC
+// #define BT_RST_PIN       GPIO_Pin_2
+
+// void ble_init(void);
+// void ble_send(void);
 /************************************************************************************************
 /************************************************************************************************
  *                                          Defines                                              *
  *                                          Defines                                              *
  ************************************************************************************************/
  ************************************************************************************************/

+ 88 - 0
modules/ble/ble_sn2model.c

@@ -0,0 +1,88 @@
+#include "ble_sn2model.h"
+#include "ble.h"
+#include <ctype.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+DEV_MODEL model_tbl[] = SN2MODEL_MAP_TBL;
+
+void hex_to_str(const uint8_t *src, uint8_t *dest, int32_t src_len)
+{
+    int32_t i;
+    uint8_t tmp[3];
+
+    for (i = 0; i < src_len; i++)
+    {
+        sprintf(tmp, "%02X", src[i]);
+        memcpy(&dest[i * 2], tmp, 2);
+    }
+
+    return;
+}
+
+void str_to_hex(const uint8_t *src, uint8_t *dest, int32_t src_len)
+{
+    int32_t i;
+    uint8_t high_byte, low_byte;
+
+    for (i = 0; i < src_len; i += 2)
+    {
+        high_byte = toupper(src[i]);
+        low_byte  = tolower(src[i + 1]);
+
+        if (high_byte > 0x39)
+        {
+            high_byte -= 0x37;
+        }
+        else
+        {
+            high_byte -= 0x30;
+        }
+
+        if (low_byte > 0x39)
+        {
+            low_byte -= 0x37;
+        }
+        else
+        {
+            low_byte -= 0x30;
+        }
+
+        dest[i / 2] = (high_byte << 4) | low_byte;
+    }
+
+    return;
+}
+
+int32_t str_sn_2_model(uint8_t *str_sn, uint8_t *model)
+{
+    for (int32_t i = 0; i < sizeof(model_tbl) / sizeof(DEV_MODEL); i++)
+    {
+        if (strncmp(model_tbl[i].prefix, &str_sn[4], MODEL_LEN) == 0)
+        {
+            strcpy(model, model_tbl[i].model);
+            return 0;
+        }
+    }
+    return 1;
+}
+
+int32_t hex_sn_2_model(uint8_t *hex_sn, uint8_t *model)
+{
+    uint8_t str_sn[SN_LEN * 2 + 1] = {0};
+    hex_to_str(hex_sn, str_sn, SN_LEN);
+    return str_sn_2_model(str_sn, model);
+}
+
+int32_t sn_2_model(uint8_t *sn, int32_t sn_type, uint8_t *model)
+{
+    if (sn_type == 0)
+    {
+        return str_sn_2_model(sn, model);
+    }
+    else
+    {
+        return hex_sn_2_model(sn, model);
+    }
+}

+ 66 - 0
modules/ble/ble_sn2model.h

@@ -0,0 +1,66 @@
+#ifndef __BLE_SN2MODEL_H
+#define __BLE_SN2MODEL_H
+
+#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __BLE_SN2MODEL_H_GLOBAL
+#define __BLE_SN2MODEL_H_EXTERN
+#else
+#define __BLE_SN2MODEL_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+#define ALIENTEK_SN2MODEL_MAP_TBL \
+    {"000000", "ALIENTEK"},
+
+#define UPPER_SN2MODEL_MAP_TBL \
+    {"111001", "UPPER"},
+
+#define SN2MODEL_MAP_TBL {    \
+    ALIENTEK_SN2MODEL_MAP_TBL \
+        UPPER_SN2MODEL_MAP_TBL}
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+#pragma pack(1)
+typedef struct
+{
+    uint8_t *prefix;
+    uint8_t *model;
+} DEV_MODEL;
+#pragma pack()
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+int32_t sn_2_model(uint8_t *sn, int32_t sn_type, uint8_t *model);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __BLE_SN2MODEL_H

+ 47 - 0
modules/ble/ble_task.c

@@ -0,0 +1,47 @@
+
+
+#include "ble.h"
+#include "systick.h"
+#include <stdint.h>
+#include <stdio.h>
+
+uint32_t cunt = 0;
+
+void run_info(void)
+{
+    static uint32_t tick = 0;
+    if (tick == 0)
+    {
+        tick = get_systick_ms();
+    }
+
+    if (get_systick_ms() - tick > 10000)
+    {
+        printf("test");
+        tick = 0;
+    }
+}
+
+void ble_main_loop()
+{
+    ble_loop();
+}
+
+#define BLE_EN 1
+
+void ble_task(void)
+{
+    static uint32_t cnt = 0;
+
+#if (BLE_EN == 1)
+    run_info();
+    ble_main_loop();
+#endif
+    cnt++;
+
+    if (cnt >= 200)
+    {
+        cunt++;
+        cnt = 0;
+    }
+}

+ 49 - 0
modules/ble/ble_task.h

@@ -0,0 +1,49 @@
+#ifndef __BLE_TASK_H
+#define __BLE_TASK_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __BLE_TASK_H_GLOBAL
+#define __BLE_TASK_H_EXTERN
+#else
+#define __BLE_TASK_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+#define BLE_TASK_INIT() ble_init()
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void ble_task(void);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __BLE_TASK_H

+ 130 - 0
modules/esp8266/esp8266.c

@@ -0,0 +1,130 @@
+
+
+// #include "esp8266.h"
+// #include "esp8266_core.h"
+// #include "systick.h"
+// #include <cstddef>
+// #include <stdint.h>
+// #include <stdio.h>
+// #include <string.h>
+// #include <sys/signal.h>
+
+// extern esp8266_cmd_cache g_esp8266_cache;
+
+// /*----------------------------------------
+// * @函数名: esp8266_cfg_init_do_asyn
+// * @描述:
+// * @入参:
+// * @出参: *pArr
+// * @返回值: NONE
+// * @调用:
+// * @被调用:
+// ----------------------------------------
+// * 作者     创建时间          操作
+//  樊春春   2024/06/11    Create/Modify
+// ----------------------------------------*/
+// int32_t esp8266_cfg_init_do_asyn(void)
+// {
+//     static ESP8266_INIT_CMD cmd = ESP8266_INIT_CMD_START;
+//     int32_t                 ret;
+//     ret = esp8266_cfg_init();
+
+//     if (ret == 0)
+//     {
+//         ret++;
+//     }
+
+//     if (cmd == ESP8266_INIT_CMD_START)
+//     {
+//         cmd = ESP8266_INIT_CMD_START;
+//         return 0;
+//     }
+//     return 1;
+// }
+
+// /*----------------------------------------
+// * @函数名: esp8266_cfg_init
+// * @描述: ESP8266初始化
+// * @入参:
+// * @出参: *pArr
+// * @返回值: NONE
+// * @调用:
+// * @被调用:
+// ----------------------------------------
+// * 作者     创建时间          操作
+//  樊春春   2024/06/11    Create/Modify
+// ----------------------------------------*/
+// int esp8266_cfg_init(void)
+// {
+//     int32_t ret;
+
+//     return 0;
+// }
+
+// /*----------------------------------------
+// * @函数名: esp8266_send_data
+// * @描述:
+// * @入参:
+// * @出参: *pArr
+// * @返回值: NONE
+// * @调用:
+// * @被调用:
+// ----------------------------------------
+// * 作者     创建时间          操作
+//  樊春春   2024/06/11    Create/Modify
+// ----------------------------------------*/
+// int esp8266_send_data(uint8_t data[], uint32_t len)
+// {
+//     uint8_t buf[32];
+//     if (len > 2048)
+//     {
+//         return 1;
+//     }
+//     sprintf(buf, "AT+CIPSEND=1,%d\r\n", len);
+//     g_esp8266_cache.is_used = 1;
+//     g_esp8266_cache.cmd     = buf;
+//     memcpy(g_esp8266_cache.data, data, len);
+//     g_esp8266_cache.tick   = 0;
+//     g_esp8266_cache.p_func = NULL;
+//     return 0;
+// }
+
+// /*----------------------------------------
+// * @函数名: esp8366_tx_loop
+// * @描述: 异步发送AT指令
+// * @入参:
+// * @出参: *pArr
+// * @返回值: NONE
+// * @调用:
+// * @被调用:
+// ----------------------------------------
+// * 作者     创建时间          操作
+//  樊春春   2024/06/11    Create/Modify
+// ----------------------------------------*/
+// int esp8366_tx_loop(void)
+// {
+//     static uint8_t count = 0;
+//     if (g_esp8266_cache.is_used == 1)
+//     {
+//         if (g_esp8266_cache.tick == 0)
+//         {
+//             g_esp8266_cache.tick = get_systick_ms();
+//             count                = 0;
+//         }
+
+//         if (count == 0 || get_systick_ms() - g_esp8266_cache.tick > 3000)
+//         {
+//             for (uint32_t i = 0; i < countof_aysn_cmd_table(); i++)
+//             {
+//                 if (g_esp8266_cmd_asyn_table[i].cmd == g_esp8266_cache.cmd && g_esp8266_cmd_asyn_table[i].p_func != NULL)
+//                 {
+//                     g_esp8266_cmd_asyn_table[i].p_func(g_esp8266_cache.cmd, g_esp8266_cache.data, g_esp8266_cache.len);
+//                 }
+//             }
+//             g_esp8266_cache.tick = get_systick_ms();
+//             count++;
+//         }
+//     }
+
+//     return 0;
+// }

+ 48 - 0
modules/esp8266/esp8266.h

@@ -0,0 +1,48 @@
+
+// #ifndef __ESP8266_H
+// #define __ESP8266_H
+
+// #ifdef __cplusplus
+// extern "C" {
+// #endif
+// #ifdef __ESP8266_H_GLOBAL
+// #define __ESP8266_H_EXTERN
+// #else
+// #define __ESP8266_H_EXTERN extern
+// #endif
+
+// /************************************************************************************************
+//  *                                          Version                                             *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          How to use                                          *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Enable config                                        *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Includes                                            *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Defines                                              *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Typedefs                                            *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Interfaces                                          *
+//  ************************************************************************************************/
+
+// int esp8266_cfg_init(void);
+
+// #ifdef __cplusplus
+// };
+// #endif
+
+// #endif // __ESP8266_H

+ 57 - 24
modules/esp8266/esp8266_core.c

@@ -1,7 +1,8 @@
 
 
 
 
 // #include "esp8266_core.h"
 // #include "esp8266_core.h"
-// #include "hal_systick.h"
+// #include "systick.h"
+// #include "uart.h"
 // #include <stdint.h>
 // #include <stdint.h>
 // #include <stdio.h>
 // #include <stdio.h>
 // #include <string.h>
 // #include <string.h>
@@ -19,38 +20,70 @@
 
 
 // esp8266_cmd_cache g_esp8266_cache;
 // esp8266_cmd_cache g_esp8266_cache;
 
 
-// uint8_t esp8266_send_cmd(uint8_t *cmd, uint8_t *ack, uint32_t wait_time)
+// uint8_t esp8266_send_cmd(char *cmd)
 // {
 // {
-//     g_esp8266_cache.is_used = 1;
-//     g_esp8266_cache.cmd     = cmd;
-//     memcpy(g_esp8266_cache.cmd, cmd, sizeof(cmd));
-//     g_esp8266_cache.tick   = 0;
-//     g_esp8266_cache.p_func = 0;
-//     return 0;
+//     usart_send_it(&usart3_context, (uint8_t *)cmd, strlen(cmd));
+//     usart_send_it(&usart3_context, "\r\n", 2);
 // }
 // }
 
 
-// uint8_t esp8266_tx_loop(void)
+// void esp8266_cfg_query_init(ESP8266_INIT_CMD cmd)
 // {
 // {
-//     static uint8_t count = 0;
+//     // static ESP8266_INIT_CMD cmd = ESP8266_INIT_CMD_START;
 
 
-//     if (g_esp8266_cache.is_used == 1)
+//     static uint32_t tick = 0;
+//     if (tick == 0)
 //     {
 //     {
-//         if (g_esp8266_cache.tick == 0)
-//         {
-//             g_esp8266_cache.tick = get_systick_ms();
-//             count                = 0;
-//         }
-
-//         if (count == 0 || get_systick_ms() - g_esp8266_cache.tick > 3000)
-//         {
-//             for (uint8_t i = 0; i < 10; i++)
-//             {
-//                 if (g_)
-//             }
-//         }
+//         tick = get_systick_ms();
 //     }
 //     }
+//     if (cmd == ESP8266_INIT_CMD_START)
+//     {
+//         esp8266_send_cmd(cmd_ok);
+//         cmd++;
+//     }
+//     usart_receive_read(&usart3_context, &ch, 1);
+// }
+
+// void esp8266_init(void)
+// {
+//     static ESP8266_INIT_CMD cmd = ESP8266_INIT_CMD_START;
+
+//     uint32_t tick = 0;
+//     if (tick == 0)
+//     {
+//         tick = get_systick_ms();
+//     }
+//     if (cmd == ESP8266_INIT_CMD_START)
+//     {
+//         esp8266_send_cmd(cmd_ok);
+//         cmd++;
+//     }
+//     usart_receive_read(&usart3_context, &ch, 1);
 // }
 // }
 
 
+// // int32_t esp8266_cfg_init_asyn()
+
+// // uint8_t esp8266_tx_loop(void)
+// // {
+// //     static uint8_t count = 0;
+
+// //     if (g_esp8266_cache.is_used == 1)
+// //     {
+// //         if (g_esp8266_cache.tick == 0)
+// //         {
+// //             g_esp8266_cache.tick = get_systick_ms();
+// //             count                = 0;
+// //         }
+
+// //         if (count == 0 || get_systick_ms() - g_esp8266_cache.tick > 3000)
+// //         {
+// //             for (uint8_t i = 0; i < 10; i++)
+// //             {
+// //                 if (g_)
+// //             }
+// //         }
+// //     }
+// // }
+
 // void esp8266_task(void)
 // void esp8266_task(void)
 // {
 // {
 //     static uint8_t status = 0;
 //     static uint8_t status = 0;

+ 48 - 31
modules/esp8266/esp8266_core.h

@@ -1,10 +1,8 @@
 // #ifndef __ESP8266_CORE_H
 // #ifndef __ESP8266_CORE_H
 // #define __ESP8266_CORE_H
 // #define __ESP8266_CORE_H
 
 
-// #include <stdint.h>
 // #ifdef __cplusplus
 // #ifdef __cplusplus
-// extern 'C'
-// {
+// extern "C" {
 // #endif
 // #endif
 // #ifdef __ESP8266_CORE_H_GLOBAL
 // #ifdef __ESP8266_CORE_H_GLOBAL
 // #define __ESP8266_CORE_H_EXTERN
 // #define __ESP8266_CORE_H_EXTERN
@@ -12,37 +10,56 @@
 // #define __ESP8266_CORE_H_EXTERN extern
 // #define __ESP8266_CORE_H_EXTERN extern
 // #endif
 // #endif
 
 
-//     /************************************************************************************************
-//      *                                          Version                                              *
-//      ************************************************************************************************/
-
-//     /************************************************************************************************
-//      *                                          How to use                                           *
-//      ************************************************************************************************/
-
-//     /************************************************************************************************
-//      *                                          Enable config                                         *
-//      ************************************************************************************************/
-
-//     /************************************************************************************************
-//      *                                          Includes                                             *
-//      ************************************************************************************************/
-
-//     /************************************************************************************************
-//      *                                          Defines                                              *
-//      ************************************************************************************************/
-//     typedef struct
-//     {
-//         uint8_t  is_used; // 0 未使用,1,使用
-//         uint8_t *cmd;
-//         uint8_t *ack;
-//         uint32_t tick;
-//         uint16_t wait_time;
-//         int32_t (*p_func)(uint8_t *cmd, uint8_t *ack, uint16_t wait_time)
-//     } esp8266_cmd_cache;
+// /************************************************************************************************
+//  *                                          Version                                             *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          How to use                                          *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Enable config                                        *
+//  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Includes                                            *
+//  ************************************************************************************************/
+
+// #include <stdint.h>
+
 // /************************************************************************************************
 // /************************************************************************************************
 //  *                                          Defines                                              *
 //  *                                          Defines                                              *
 //  ************************************************************************************************/
 //  ************************************************************************************************/
+
+// /************************************************************************************************
+//  *                                          Typedefs                                            *
+//  ************************************************************************************************/
+
+// typedef enum
+// {
+//     ESP8266_INIT_CMD_START = 0,
+//     ESP8266_INIT_CMD_MODE,
+//     ESP8266_INIT_CMD_JOIN,
+//     ESP8266_INIT_CMD_MUX,
+//     ESP8266_INIT_CMD_SERVER,
+//     ESP8266_INIT_CMD_SEND
+// } ESP8266_INIT_CMD;
+
+// typedef struct
+// {
+//     uint8_t  is_used; // 0 未使用,1,使用
+//     uint8_t *cmd;
+//     uint8_t *ack;
+//     uint32_t tick;
+//     uint16_t wait_time;
+//     int32_t (*p_func)(uint8_t *cmd, uint8_t *ack, uint16_t wait_time)
+// } esp8266_cmd_cache;
+
+// /************************************************************************************************
+//  *                                          Interfaces                                          *
+//  ************************************************************************************************/
+
 // #ifdef __cplusplus
 // #ifdef __cplusplus
 // };
 // };
 // #endif
 // #endif

+ 2 - 2
modules/task/app_task.c

@@ -1,7 +1,7 @@
 #include "app_task.h"
 #include "app_task.h"
 #include "J1939.H"
 #include "J1939.H"
 #include "app_eeprom.h"
 #include "app_eeprom.h"
-#include "ble_core.h"
+#include "ble_task.h"
 #include "can_task.h"
 #include "can_task.h"
 #include "cli.h"
 #include "cli.h"
 #include "dev_at24cxx.h"
 #include "dev_at24cxx.h"
@@ -34,6 +34,7 @@ static void app_task_5ms(void)
     key_button_task();
     key_button_task();
     can_process();
     can_process();
     cli_loop();
     cli_loop();
+    ble_task();
 }
 }
 
 
 /*----------------------------------------
 /*----------------------------------------
@@ -73,7 +74,6 @@ static void app_task_50ms(void)
     if (key_button.key_state.bits.key_up_state_flag == 1)
     if (key_button.key_state.bits.key_up_state_flag == 1)
     {
     {
         buf[0] = 10;
         buf[0] = 10;
-        ble_send();
         app_eeprom_write_50_zhuzu();
         app_eeprom_write_50_zhuzu();
         key_button.key_state.bits.key_up_state_flag = 0;
         key_button.key_state.bits.key_up_state_flag = 0;
     }
     }

+ 3 - 3
project/main.c

@@ -1,5 +1,5 @@
 #include "app_task.h"
 #include "app_task.h"
-#include "ble_core.h"
+#include "ble_task.h"
 #include "can.h"
 #include "can.h"
 #include "gpio.h"
 #include "gpio.h"
 #include "i2c1.h"
 #include "i2c1.h"
@@ -8,6 +8,7 @@
 #include "misc.h"
 #include "misc.h"
 #include "pwm.h"
 #include "pwm.h"
 #include "systick.h"
 #include "systick.h"
+#include "uart.h"
 #include "uart4.h"
 #include "uart4.h"
 
 
 #define APP_FLASH_OFFSET 0x8000
 #define APP_FLASH_OFFSET 0x8000
@@ -28,10 +29,9 @@ int main(void)
     can_init();
     can_init();
     can_network_init();
     can_network_init();
     usart_config_init(&usart1_context, 115200);
     usart_config_init(&usart1_context, 115200);
-    usart_config_init(&usart2_context, 9600);
     usart_config_init(&usart3_context, 115200);
     usart_config_init(&usart3_context, 115200);
     uart4_init();
     uart4_init();
-    ble_init();
+    BLE_TASK_INIT();
     i2c1_init();
     i2c1_init();
     key_button_init();
     key_button_init();
     set_firmver();
     set_firmver();