Skip to content
This repository was archived by the owner on Dec 14, 2025. It is now read-only.

Commit 247dd58

Browse files
committed
[BT] Add DBG for latency tests
1 parent 88ebc79 commit 247dd58

6 files changed

Lines changed: 217 additions & 0 deletions

File tree

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
idf_component_register(SRCS "main.c"
2+
"stats.c"
23
"adapter/adapter.c"
34
"adapter/config.c"
45
"adapter/hid_parser.c"
@@ -21,6 +22,7 @@ idf_component_register(SRCS "main.c"
2122
"adapter/parallel_2p.c"
2223
"adapter/parallel_auto.c"
2324
"bluetooth/host.c"
25+
"bluetooth/debug.c"
2426
"bluetooth/hci.c"
2527
"bluetooth/l2cap.c"
2628
"bluetooth/sdp.c"

main/bluetooth/debug.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2019-2020, Jacques Gagnon
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdint.h>
8+
#include <xtensa/hal.h>
9+
#include <driver/gpio.h>
10+
#include "sdkconfig.h"
11+
#include "../stats.h"
12+
13+
static uint8_t type;
14+
static uint32_t counter;
15+
static uint32_t start, end;
16+
17+
//#define REPORT_INTEVAL_STATS
18+
//#define MIN_LATENCY_TEST
19+
void bt_dbg_init(uint8_t dev_type) {
20+
end = 0;
21+
start = xthal_get_ccount();
22+
counter = 0;
23+
type = dev_type;
24+
}
25+
26+
void bt_dbg(uint8_t *data, uint16_t len) {
27+
#ifdef REPORT_INTEVAL_STATS
28+
float average, max, min, std_dev;
29+
uint32_t interval;
30+
31+
end = xthal_get_ccount();
32+
counter++;
33+
34+
if (end > start) {
35+
interval = (end - start)/CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
36+
}
37+
else {
38+
interval = ((0xFFFFFFFF - start) + end)/CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
39+
}
40+
start = end;
41+
42+
average = getAverage(interval)/1000;
43+
max = getMax(interval)/1000;
44+
min = getMin(interval)/1000;
45+
std_dev = getStdDev(interval)/1000;
46+
printf("\e[1;1H\e[2J");
47+
48+
printf("Samples: %d\n", counter);
49+
printf("Average: %.6f ms\n", average);
50+
printf("Max: %.6f ms\n", max);
51+
printf("Min: %.6f ms\n", min);
52+
printf("Std Dev: %.6f ms\n", std_dev);
53+
#endif
54+
#ifdef MIN_LATENCY_TEST
55+
//#define PS3
56+
//#define PS4
57+
//#define PS5
58+
//#define WIIU
59+
//#define XB1
60+
//#define SW
61+
#ifdef PS3
62+
if ((*(uint32_t *)&data[11]) & 0x01FFFF00) {
63+
GPIO.out = 0xFBFFFFFF;
64+
}
65+
else {
66+
GPIO.out = 0xFFFFFFFF;
67+
}
68+
#else
69+
#ifdef PS4
70+
if ((*(uint32_t *)&data[6+11]) & 0x0003FFF0) {
71+
GPIO.out = 0xFBFFFFFF;
72+
}
73+
else {
74+
GPIO.out = 0xFFFFFFFF;
75+
}
76+
#else
77+
#ifdef PS5
78+
if ((*(uint32_t *)&data[8+11]) & 0x0003FFF0) {
79+
GPIO.out = 0xFBFFFFFF;
80+
}
81+
else {
82+
GPIO.out = 0xFFFFFFFF;
83+
}
84+
#else
85+
#ifdef WIIU
86+
if (~(*(uint32_t *)&data[13+11]) & 0x0003FFFE) {
87+
GPIO.out = 0xFBFFFFFF;
88+
}
89+
else {
90+
GPIO.out = 0xFFFFFFFF;
91+
}
92+
#else
93+
#ifdef XB1
94+
if ((*(uint32_t *)&data[13+11]) & 0x000003FF) {
95+
GPIO.out = 0xFBFFFFFF;
96+
}
97+
else {
98+
GPIO.out = 0xFFFFFFFF;
99+
}
100+
#else
101+
#ifdef SW
102+
if ((*(uint16_t *)&data[+11])) {
103+
GPIO.out = 0xFBFFFFFF;
104+
}
105+
else {
106+
GPIO.out = 0xFFFFFFFF;
107+
}
108+
#endif
109+
#endif
110+
#endif
111+
#endif
112+
#endif
113+
#endif
114+
#endif
115+
}
116+

main/bluetooth/debug.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2019-2020, Jacques Gagnon
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef _BT_DBG_H_
7+
#define _BT_DBG_H_
8+
9+
void bt_dbg_init(uint8_t dev_type);
10+
void bt_dbg(uint8_t *data, uint16_t len);
11+
12+
#endif /* _BT_DBG_H_ */

main/bluetooth/host.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include "sdp.h"
1919
#include "att.h"
2020
#include "../util.h"
21+
#include "debug.h"
2122

2223
//#define H4_TRACE /* Display packet dump that can be parsed by wireshark/text2pcap */
24+
//#define BT_DBG /* Run bt_host_dbg function after HID channels are setup */
2325

2426
#define BT_TX 0
2527
#define BT_RX 1
@@ -32,6 +34,7 @@ enum {
3234
/* BT CTRL flags */
3335
BT_CTRL_READY,
3436
BT_HOST_DISCONN_SW_INHIBIT,
37+
BT_HOST_DBG_MODE,
3538
};
3639

3740
struct bt_host_link_keys {
@@ -316,6 +319,12 @@ static int bt_host_rx_pkt(uint8_t *data, uint16_t len) {
316319
bt_h4_trace(data, len, BT_RX);
317320
#endif /* H4_TRACE */
318321

322+
#ifdef BT_DBG
323+
if (atomic_test_bit(&bt_flags, BT_HOST_DBG_MODE)) {
324+
bt_dbg(data, len);
325+
}
326+
else {
327+
#endif
319328
switch(bt_hci_pkt->h4_hdr.type) {
320329
case BT_HCI_H4_TYPE_ACL:
321330
bt_host_acl_hdlr(bt_hci_pkt, len);
@@ -327,6 +336,9 @@ static int bt_host_rx_pkt(uint8_t *data, uint16_t len) {
327336
printf("# %s unsupported packet type: 0x%02X\n", __FUNCTION__, bt_hci_pkt->h4_hdr.type);
328337
break;
329338
}
339+
#ifdef BT_DBG
340+
}
341+
#endif
330342

331343
return 0;
332344
}
@@ -406,6 +418,16 @@ int32_t bt_host_init(void) {
406418
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
407419
gpio_config(&io_conf);
408420

421+
#ifdef BT_DBG
422+
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
423+
io_conf.mode = GPIO_MODE_OUTPUT;
424+
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
425+
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
426+
io_conf.pin_bit_mask = 1ULL << 26;
427+
gpio_config(&io_conf);
428+
gpio_set_level(26, 1);
429+
#endif
430+
409431
bt_host_load_bdaddr_from_file();
410432

411433
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
@@ -478,6 +500,10 @@ int32_t bt_host_store_link_key(struct bt_hci_evt_link_key_notify *link_key_notif
478500
}
479501

480502
void bt_host_bridge(struct bt_dev *device, uint8_t report_id, uint8_t *data, uint32_t len) {
503+
#ifdef BT_DBG
504+
atomic_set_bit(&bt_flags, BT_HOST_DBG_MODE);
505+
bt_dbg_init(device->type);
506+
#else
481507
if (device->type == HID_GENERIC) {
482508
uint32_t i = 0;
483509
for (; i < REPORT_MAX; i++) {
@@ -499,4 +525,5 @@ void bt_host_bridge(struct bt_dev *device, uint8_t report_id, uint8_t *data, uin
499525
adapter_bridge(&bt_adapter.data[device->id]);
500526
}
501527
bt_adapter.data[device->id].report_cnt++;
528+
#endif
502529
}

main/stats.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Functions to calculate Average, Min, Max, and Std Dev */
2+
/* Taken from Arduino lag test code by @jorge_: https://pastebin.com/ktqn7izx */
3+
4+
#include <stdint.h>
5+
#include <math.h>
6+
7+
float getAverage(uint32_t newNum){
8+
static uint32_t numSamples = 1;
9+
static float curAvg;
10+
11+
curAvg = curAvg + (newNum - curAvg) / numSamples;
12+
numSamples++;
13+
14+
return curAvg;
15+
}
16+
17+
float getMax(uint32_t newNum){
18+
static uint32_t maxVal = 0;
19+
20+
if(newNum > maxVal){
21+
maxVal = newNum;
22+
}
23+
24+
return maxVal;
25+
}
26+
27+
float getMin(uint32_t newNum){
28+
static uint32_t minVal = 0xFFFFFFFF;
29+
30+
if(newNum < minVal){
31+
minVal = newNum;
32+
}
33+
34+
return minVal;
35+
}
36+
37+
float getStdDev(uint32_t newNum){
38+
static float M = 0.0;
39+
static float S = 0.0;
40+
static uint32_t i = 1;
41+
42+
float tmpM = M;
43+
M += (newNum - tmpM) / i;
44+
S += (newNum - tmpM) * (newNum - M);
45+
i++;
46+
47+
return sqrt(S/(i-2));
48+
}

main/stats.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Functions to calculate Average, Min, Max, and Std Dev */
2+
/* Taken from Arduino lag test code by @jorge_: https://pastebin.com/ktqn7izx */
3+
4+
#ifndef _STATS_H_
5+
#define _STATS_H_
6+
7+
float getAverage(uint32_t newNum);
8+
float getMax(uint32_t newNum);
9+
float getMin(uint32_t newNum);
10+
float getStdDev(uint32_t newNum);
11+
12+
#endif /* _STATS_H_ */

0 commit comments

Comments
 (0)