Skip to content

Commit 4e556ab

Browse files
authored
Add next gen profile and configurations (#67)
* fix: crash in picture animation * ble: add next gen profile * force read header from flash anyway * fix: bmlist: mem leak * feat: add reset option after legacy transfers
1 parent 5121346 commit 4e556ab

File tree

29 files changed

+911
-242
lines changed

29 files changed

+911
-242
lines changed

CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_sys.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -369,28 +369,3 @@ void mDelaymS(uint16_t t)
369369
}
370370
}
371371

372-
#ifdef DEBUG
373-
int _write(int fd, char *buf, int size)
374-
{
375-
int i;
376-
for(i = 0; i < size; i++)
377-
{
378-
#if DEBUG == Debug_UART0
379-
while(R8_UART0_TFC == UART_FIFO_SIZE); /* 等待数据发送 */
380-
R8_UART0_THR = *buf++; /* 发送数据 */
381-
#elif DEBUG == Debug_UART1
382-
while(R8_UART1_TFC == UART_FIFO_SIZE); /* 等待数据发送 */
383-
R8_UART1_THR = *buf++; /* 发送数据 */
384-
#elif DEBUG == Debug_UART2
385-
while(R8_UART2_TFC == UART_FIFO_SIZE); /* 等待数据发送 */
386-
R8_UART2_THR = *buf++; /* 发送数据 */
387-
#elif DEBUG == Debug_UART3
388-
while(R8_UART3_TFC == UART_FIFO_SIZE); /* 等待数据发送 */
389-
R8_UART3_THR = *buf++; /* 发送数据 */
390-
#endif
391-
}
392-
return size;
393-
}
394-
395-
#endif
396-

CH5xx_ble_firmware_library/StdPeriphDriver/inc/CH583SFR.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,6 @@ typedef volatile unsigned long long *PUINT64V;
123123
#define max(a,b) (((a) > (b)) ? (a) : (b))
124124
#endif
125125

126-
#ifdef DEBUG
127-
#define PRINT(X...) printf(X)
128-
#else
129-
#define PRINT(X...)
130-
#endif
131-
132126
/* Calculate the byte offset of a field in a structure of type */
133127
#define FIELD_OFFSET(Type, Field) ((UINT16)&(((Type *)0)->Field))
134128

CH5xx_ble_firmware_library/StdPeriphDriver/inc/CH58x_common.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@
3636
#endif
3737
#endif
3838

39-
#define Debug_UART0 0
40-
#define Debug_UART1 1
41-
#define Debug_UART2 2
42-
#define Debug_UART3 3
43-
44-
#ifdef DEBUG
45-
#include <stdio.h>
46-
#endif
47-
4839
/**
4940
* @brief 系统主频时钟(Hz)
5041
*/

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,27 @@ CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_clk.c \
4747
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_uart0.c \
4848
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_timer1.c \
4949
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_pwm.c \
50-
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usbhostClass.c \
5150
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_adc.c \
52-
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usbhostBase.c \
5351
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_timer3.c \
5452
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_timer0.c \
55-
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2hostClass.c \
5653
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_flash.c \
5754
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_uart1.c \
5855
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2dev.c \
59-
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2hostBase.c \
6056
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_spi1.c \
6157
CH5xx_ble_firmware_library/RVMSIS/core_riscv.c \
6258
src/main.c \
59+
src/debug.c \
6360
src/leddrv.c \
6461
src/button.c \
6562
src/bmlist.c \
6663
src/ble/profile/legacy.c \
6764
src/ble/profile/batt.c \
6865
src/ble/profile/devinfo.c \
66+
src/ble/profile/ng.c \
67+
src/config.c \
68+
src/legacyctrl.c \
69+
src/util/crc.c \
70+
src/ngctrl.c \
6971
src/ble/setup.c \
7072
src/ble/peripheral.c \
7173
src/data.c \

src/animation.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "xbm.h"
44
#include "leddrv.h"
55
#include "bmlist.h"
6+
#include "debug.h"
67

78
#define ANI_ANIMATION_STEPS (5) // steps
89
#define ANI_FIXED_STEPS (LED_COLS) // steps
@@ -404,7 +405,7 @@ static void picture(bm_t *bm, uint16_t *fb, int step, int frame)
404405
int hc = LED_COLS / 2;
405406
int range = mod(step - 1, LED_COLS);
406407

407-
if (range > LED_COLS/2) {
408+
if (range > hc) {
408409
still(bm, fb, frame);
409410
return;
410411
}
@@ -418,15 +419,10 @@ static void picture(bm_t *bm, uint16_t *fb, int step, int frame)
418419
0 : bm->buf[hc - i + frame];
419420
}
420421

421-
if (i >= LED_COLS)
422+
if (i >= hc)
422423
return;
423424
fb[hc + i - 1] = -1;
424425
fb[hc - i] = -1;
425-
426-
for (i++; i< LED_COLS; i++) {
427-
fb[hc + i - 1] = 0;
428-
fb[hc - i] = 0;
429-
}
430426
}
431427

432428
static void picture_out(bm_t *bm, uint16_t *fb, int step)
@@ -442,7 +438,7 @@ static void picture_out(bm_t *bm, uint16_t *fb, int step)
442438
fb[hc + i - 1] = 0;
443439
fb[hc - i] = 0;
444440
}
445-
if (i >= LED_COLS)
441+
if (i >= hc)
446442
return;
447443
fb[hc + i - 1] = -1;
448444
fb[hc - i] = -1;

src/ble/peripheral.c

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CH58xBLE_LIB.h"
22
#include "setup.h"
3+
#include "../config.h"
34

45
#define ADV_UUID (0xFEE0)
56

@@ -24,29 +25,6 @@ typedef struct
2425

2526
#define CONN_TIMEOUT 100 // Supervision timeout (units of 10ms)
2627

27-
// GAP - SCAN RSP data (max size = 31 bytes)
28-
static uint8 scanRspData[] = {
29-
// complete name
30-
16, // length of this section
31-
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
32-
'L', 'E','D', ' ',
33-
'B', 'a','d', 'g', 'e', ' ',
34-
'M', 'a','g', 'i', 'c',
35-
36-
// connection interval range
37-
0x05, // length of this section
38-
GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
39-
LO_UINT16(MIN_CONN_INTERVAL),
40-
HI_UINT16(MIN_CONN_INTERVAL),
41-
LO_UINT16(MAX_CONN_INTERVAL),
42-
HI_UINT16(MAX_CONN_INTERVAL),
43-
44-
// Tx power level
45-
0x02, // length of this data
46-
GAP_ADTYPE_POWER_LEVEL,
47-
9 // 9dBm
48-
};
49-
5028
// GAP - Advertisement data (max size = 31 bytes)
5129
// keep short, save energy, save the planet
5230
static uint8 advertData[] = {
@@ -61,9 +39,6 @@ static uint8 advertData[] = {
6139
HI_UINT16(ADV_UUID)
6240
};
6341

64-
// GAP GATT Attributes
65-
static uint8 devName[GAP_DEVICE_NAME_LEN] = "LED Badge Magic";
66-
6742
// Connection item list
6843
static peripheralConnItem_t conn_list;
6944

@@ -210,21 +185,58 @@ void ble_disable_advertise()
210185
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8), &e);
211186
}
212187

188+
// len should not exceed 20 chars excluding null-terminate char. Otherwise it
189+
// will be trimmed off and return -1
190+
static int setup_scan_rsp(char *name, uint8_t len)
191+
{
192+
int ret = 0;
193+
// GAP - SCAN RSP data (max size = 31 bytes)
194+
uint8_t scanRspData[31] = {
195+
// connection interval range
196+
0x05, // length of this section
197+
GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
198+
LO_UINT16(MIN_CONN_INTERVAL),
199+
HI_UINT16(MIN_CONN_INTERVAL),
200+
LO_UINT16(MAX_CONN_INTERVAL),
201+
HI_UINT16(MAX_CONN_INTERVAL),
202+
203+
// Tx power level
204+
0x02, // length of this data
205+
GAP_ADTYPE_POWER_LEVEL,
206+
9, // 9dBm
207+
208+
len + 1,
209+
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
210+
};
211+
212+
if (len < (31 - 11)) {
213+
tmos_memcpy(&scanRspData[11], name, len);
214+
ret = -1;
215+
} else {
216+
tmos_memcpy(&scanRspData[11], name, 20);
217+
}
218+
int total_len = 11 + len;
219+
tmos_memset(&scanRspData[total_len], 0, 31 - total_len);
220+
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, total_len, scanRspData);
221+
return ret;
222+
}
223+
213224
static void gap_init()
214225
{
215226
GAPRole_PeripheralInit();
216227

217-
static uint16 desired_min_interval = 6;
218-
static uint16 desired_max_interval = 500;
228+
uint16_t min_interval = 6;
229+
uint16_t max_interval = 500;
219230

220231
// Set the GAP Role Parameters
221-
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
232+
setup_scan_rsp(badge_cfg.ble_devname, 20);
222233
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
223-
GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16), &desired_min_interval);
224-
GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16), &desired_max_interval);
234+
GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16), &min_interval);
235+
GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16), &max_interval);
225236

226237
// Set the GAP Characteristics
227-
GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, devName);
238+
GGS_SetParameter(GGS_DEVICE_NAME_ATT, 20,
239+
badge_cfg.ble_devname);
228240
GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, MIN_ADV_INTERVAL);
229241
GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, MAX_ADV_INTERVAL);
230242

src/ble/profile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
int legacy_registerService();
55
int devInfo_registerService();
66
int batt_registerService();
7+
int ng_registerService();
8+
9+
bStatus_t ng_notify(uint8_t *pValue, uint8_t len);
710

811
#endif /* __BLE_UART_SERVICE_H__ */

src/ble/profile/legacy.c

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "utils.h"
22

3-
#include "../../data.h"
4-
#include "../../power.h"
5-
#include "../../leddrv.h"
3+
#include "../../legacyctrl.h"
64

75
static const uint16_t ServiceUUID = 0xFEE0;
86
static const gattAttrType_t service = {2, (uint8_t *)&ServiceUUID};
@@ -18,39 +16,6 @@ static gattAttribute_t attr_table[] = {
1816
CHAR_VAL_DECLAR(&RxCharUUID, 2, GATT_PERMIT_WRITE, RxCharVal),
1917
};
2018

21-
static bStatus_t receive(uint8_t *val, uint16_t len)
22-
{
23-
static uint16_t c, data_len, n;
24-
static uint8_t *data;
25-
if (len != LEGACY_TRANSFER_WIDTH) {
26-
return ATT_ERR_INVALID_VALUE_SIZE;
27-
}
28-
if (c == 0) {
29-
if (memcmp(val, "wang\0\0", 6)) {
30-
return ATT_ERR_INVALID_VALUE;
31-
} else {
32-
data = malloc(sizeof(data_legacy_t));
33-
}
34-
}
35-
36-
memcpy(data + c * len, val, len);
37-
38-
if (c == 1) {
39-
data_legacy_t *d = (data_legacy_t *)data;
40-
n = bigendian16_sum(d->sizes, 8);
41-
data_len = LEGACY_HEADER_SIZE + LED_ROWS * n;
42-
data = realloc(data, data_len);
43-
}
44-
45-
if (c > 2 && ((c+1) * LEGACY_TRANSFER_WIDTH) >= data_len) {
46-
data_flatSave(data, data_len);
47-
reset_jump();
48-
}
49-
50-
c++;
51-
return SUCCESS;
52-
}
53-
5419
static bStatus_t write_handler(uint16 connHandle, gattAttribute_t *pAttr,
5520
uint8 *pValue, uint16 len, uint16 offset, uint8 method)
5621
{
@@ -60,7 +25,10 @@ static bStatus_t write_handler(uint16 connHandle, gattAttribute_t *pAttr,
6025

6126
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
6227
if(uuid == RxCharUUID) {
63-
return receive(pValue, len);
28+
if (legacy_ble_rx(pValue, len)) {
29+
return ATT_ERR_UNLIKELY;
30+
}
31+
return SUCCESS;
6432
}
6533
return ATT_ERR_ATTR_NOT_FOUND;
6634
}

0 commit comments

Comments
 (0)