WXK
2025-01-16 13e0fafc140c16539691d26afafaca417e7e2fbc
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
#include <stddef.h>
#include "FreeRTOS.h"
#include "task.h"
#include "nimble/nimble_port.h"
#include "nimble_syscfg.h"
#include "nimble/ble.h"
 
#include "nimble/pan107x/nimble_glue_spark.h"
#include "nimble/hci_common.h"
#include "pan_ble_stack.h"
#include "pan_svc_call.h"
#include "comm_prf.h"
 
#include "ble_config.h"
 
#if (configUSE_TICKLESS_IDLE == 1)
extern void UpdateTickAndSch(void);
#endif
 
static TaskHandle_t host_task_h;
static uint8_t ll_enable = false;
 
#define HCI_BUF_SIZE_ALIGN4(payload) MEM_ALIGNED4(4 + payload)
 
#ifdef IP_107x
#define PAN_BLE_CTLR_BUFFER_SIZE    MEM_ALIGNED4(2148 +                                                                  \
                                                (HCI_BUF_SIZE_ALIGN4(MYNEWT_VAL_BLE_TRANSPORT_ACL_SIZE) << 3) +            \
                                                (HCI_BUF_SIZE_ALIGN4(65) << 3) +                                           \
                                                (500+256) * (CONFIG_BT_MAX_NUM_OF_CENTRAL + CONFIG_BT_MAX_NUM_OF_PERIPHERAL) +   \
                                                80 * CONFIG_BLE_CONTROLLER_RESOLVELIST_NUM +                               \
                                                36 * (2 + CONFIG_BT_MAX_NUM_OF_CENTRAL + CONFIG_BT_MAX_NUM_OF_PERIPHERAL)  \
                                                ) 
                                                
 
#elif defined(IP_101x)                                        
#define PAN_BLE_CTLR_BUFFER_SIZE    MEM_ALIGNED4(1024 +                                                                   \
                                                (HCI_BUF_SIZE_ALIGN4(251)*1) +                                            \
                                                (HCI_BUF_SIZE_ALIGN4(65) *1) +                                            \
                                                (500 + 256) * (CONFIG_BT_MAX_NUM_OF_CENTRAL + CONFIG_BT_MAX_NUM_OF_PERIPHERAL) +  \
                                                80 * CONFIG_BLE_CONTROLLER_RESOLVELIST_NUM +                              \
                                                36 * (2 + CONFIG_BT_MAX_NUM_OF_CENTRAL + CONFIG_BT_MAX_NUM_OF_PERIPHERAL) \
                                                )  
#endif
                 
static uint32_t mem_buffer[PAN_BLE_CTLR_BUFFER_SIZE / 4];
static uint32_t mem_pos = 0;
 
static void *mem_alloc(uint32_t size)
{
    void *mem_ret = NULL;
    char *p_mem = (char *)mem_buffer;
 
    if (PAN_BLE_CTLR_BUFFER_SIZE - mem_pos >= size) {
        mem_ret = &p_mem[mem_pos];
        mem_pos += size;
    } else {
    #if CONFIG_UART_LOG_ENABLE
        printf("[E] BLE Heap buffer allocated Failed, need:%d B(%d)\n",size,PAN_BLE_CTLR_BUFFER_SIZE);
    #endif
        configASSERT(0);
    }
    
#if CONFIG_UART_LOG_ENABLE
    printf("BLE Heap size:%d B, remain:%d B\n", PAN_BLE_CTLR_BUFFER_SIZE, PAN_BLE_CTLR_BUFFER_SIZE - mem_pos);
#endif
    return mem_ret;
}
 
 
/**@brief BLE Configuration */
pan_ble_cfg ble_cfg = {
    .sleep_clock_source   = CONFIG_LOW_SPEED_CLOCK_SRC,       
    .sleep_clock_accuracy = CONFIG_BT_CTLR_SCA,
    .max_num_of_states    = 0,                                 /*unused param*/
    .tx_power             = CONFIG_BT_CTLR_TX_POWER_DFT,
 
    .pf_mem_init          = mem_alloc,
    .link_layer_debug     = (CONFIG_BT_CTLR_LINK_LAYER_DEBUG ? true:false),
    .agc_cfg_mode         = 0,
 
    /* BLE Controller Configuration Parameter. */
   #if CONFIG_PM
    .pmEnable    = true,
   #endif
 
    .mstMargin   = CONFIG_BLE_CONTROLLER_MASTER_LINK_MARGIN,
    .wlNum       = CONFIG_BLE_CONTROLLER_WIHTELIST_NUM,
    .rlNum       = CONFIG_BLE_CONTROLLER_RESOLVELIST_NUM,
    
    .maxMst      = CONFIG_BT_MAX_NUM_OF_CENTRAL,
    .maxSlv      = CONFIG_BT_MAX_NUM_OF_PERIPHERAL,
    
    .maxAclLen   = MYNEWT_VAL_BLE_TRANSPORT_ACL_SIZE,
    .numRxBufs   = CONFIG_BLE_CONTROLLER_RF_RX_BUF_NUM,
    .numTxBufs   = CONFIG_BLE_CONTROLLER_RF_TX_BUF_NUM,
    .numMoreData = CONFIG_BLE_CONTROLLER_MORE_DATA_NUM,
    .llEncTime   = CONFIG_BLE_CONTROLLER_LL_ENC_TIME,
};
 
 
CONFIG_RAM_CODE int ll_semphr_cback(void)
{
    NVIC_EnableIRQ(BLE_EVENT_PROC_IRQn);
    NVIC_SetPendingIRQ(BLE_EVENT_PROC_IRQn);
 
    return 0;
}
 
void ble_hs_thread_entry(void *parameter)
{
    nimble_port_run();
}
 
void hs_thread_init(void)
{
    xTaskCreate(ble_hs_thread_entry, "host", MYNEWT_VAL(BLE_HOST_THREAD_STACK_SIZE),
            NULL, configMAX_PRIORITIES - os_priority_low, &host_task_h);
}
 
void vApplicationUserHook(void)
{
    if(ll_enable){
        pan_update_stimer();
    }
}
 
int host_copydata(void *from, void *dst, uint16_t len)
{
    struct os_mbuf *om = from;
 
    return os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), dst);
}
 
void ble_hci_evt_ll_to_host_cbk(uint8_t *p_evt, uint16_t evt_len)
{
    void *buf;
 
    if (p_evt[2] == BLE_HCI_LE_SUBEV_ADV_RPT) {
        buf = ble_transport_alloc_evt(1);
    } else {
        buf = ble_transport_alloc_evt(0);
    }
 
    if (buf) {
        memcpy(buf, p_evt, evt_len);
        ble_transport_to_hs_evt(buf);
    }
}
 
void ble_hci_acl_ll_to_host_cbk(uint8_t *p_acl, uint16_t acl_len)
{
    int ret;
    struct os_mbuf *om;
 
    om = ble_transport_alloc_acl_from_ll();
 
    if (om) {
        ret = os_mbuf_append(om, p_acl, acl_len);
        if (ret) {
            os_mbuf_free_chain(om);
        } else {
            ble_transport_to_hs_acl(om);
        }
    }
    else{
        APP_TRACK_ERR("Host transport alloc ACL buffer failed\n");
    }
}
 
void ll_init(void)
{
    //printf("LL Spark Controller Version:%07x\n", pan_ble_get_version()->commit_id);
 
    pan_ll_register_hostcopy_cb(host_copydata);
    pan_ll_register_semphr_cback(ll_semphr_cback);
    pan_ble_hci_init(ble_hci_evt_ll_to_host_cbk, ble_hci_acl_ll_to_host_cbk);
    pan_ble_init(&ble_cfg);
    
    ll_enable = true;
}
 
int ble_transport_to_ll_acl_impl(struct os_mbuf *om)
{
    int rc = 0;
 
    rc = pan_ble_hci_acl_nimble_handle((void *)om, OS_MBUF_PKTLEN(om));
 
    os_mbuf_free_chain(om);
 
    return (rc != 0) ? BLE_ERR_MEM_CAPACITY : 0;
}
 
int ble_transport_to_ll_cmd_impl(void *buf)
{
    int rc;
    struct ble_hci_cmd *cmd = buf;
 
    pan_ble_hci_cmd_handle(buf, sizeof(struct ble_hci_cmd) + cmd->length, 0, 0);
 
    ble_transport_free(cmd);
 
    return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0;
}
 
void TRIM_IRQHandler(void)
{
    printf("trim\n");
}
 
CONFIG_RAM_CODE void BLE_EVENT_PROC_IRQ(void)
{
    pan_ble_handle();
 
#if (configUSE_TICKLESS_IDLE == 1)
    UpdateTickAndSch();
#endif
}
 
CONFIG_RAM_CODE void LL_IRQHandler(void)
{
#ifdef PRF_BLE_DUAL_MODE
    if (panchip_prf_ble_handler()) {
        return;
    }
#endif
    pan_ble_irq();
}
 
uint32_t db_set_bd_address(uint8_t *bd_addr)
{
    pan_misc_set_bd_addr(bd_addr);
    return 0;
}