Zhongke Bluexun Bluetooth Headset SDK Analysis and Sharing
[Copy link]
SDK Structure1.1
SDK Directory Structure└─app
├─platform
│
├─bsp //Low-level peripheral related│
├─functions //Function related│
├─gui //Display function│
├─header
│ └─libs
└─projects //Calling API
└─earphone
├─display //Display├─message
//Message
processing├─Output //File output│
└─bin //Music file, configuration│
├─res
│ │ ├─en
│ │ ├─eq
│ │ └─zh
│ └─Settings
│ └─Resources
│ ├─S6
│ │ ├─en
│ │ └─zh │
└─TWS
│ ├─en
│ └─zh
├─plugin //plugin
└─port //port
1.1.1 bsp directory
This directory contains some data related to the underlying hardware, function initialization
1.1.2 functions directory
Mainly includes the implementation of Bluetooth functions, most of the functions are provided in the form of library functions
1.1.3 Message Directory
Mainly includes key message processing, which is the directory that often needs to be modified in Bluetooth solutions
1.1.4 Plugin directory
Music files are called and basically will not be modified here
1.1.5 Port Directory
Mainly includes the call of hardware peripherals and mani functions
2 Code running process
2.1 Initialization
//Normal startup Main function
int main(void)
{
printf("Hello AB533X!\n");
bsp_sys_init();
func_run();
return 0;
}
The bsp_sys_init() function mainly includes the initialization of various functions and obtains the configuration of the download tool.
The func_run() function mainly processes Bluetooth messages and hardware messages.
2.2 Mode Cycles
After initialization, enter a FUN function. The FUN function of the Bluetooth headset basically runs func_bt
void func_run(void)
{
printf("%s\n", __func__);
//func_cb.sta = FUNC_AUX;
while (1) {
func_clear();
switch (func_cb.sta) {
#if FUNC_BT_EN
case FUNC_BT:
func_bt();
break;
#endif
#if FUNC_BTHID_EN
case FUNC_BTHID:
func_bthid();
break;
#endif // FUNC_BTHID_EN
#if FUNC_AUX_EN
case FUNC_AUX:
func_aux();
break;
#endif // FUNC_AUX_EN
case FUNC_PWROFF:
func_pwroff(sys_cb.pwrdwn_tone_en);
break;
case FUNC_BT_CBT:
func_bt_cbt();
break;
default:
func_exit();
break;
}
}
}
2.3 Bluetooth Mode
AT(.text.func.bt)
void func_bt(void)
{
printf("func_bt\n");
func_bt_enter();
while (func_cb.sta == FUNC_BT) {
func_bt_process();
func_bt_message(msg_dequeue());
}
func_bt_exit();
}
When the program runs to func_bt();, the SDK leaves the developer with only message processing, battery detection, incoming call detection, etc. The Bluetooth headset receives audio signals and decodes them, which are all blocked.
printf("%s\n", func);
2.3.1 Enter Bluetooth mode
func_bt_enter();
Mainly performs Bluetooth initialization and broadcasts prompt tone
2.3.2 Bluetooth mode big loop
func_bt_process();
including ringing, calling, Bluetooth music
sfunc_bt_ring();
sfunc_bt_call();
func_bt_message(msg_dequeue());
Message processing
2.3.3 Supplement
C language special macro
LINE indicates the line number of the file being compiled
FILE indicates the name of the file being compiled
DATE_ indicates the date string of the compilation time, for example: "25 Dec 2007"
TIME indicates the time string of the compilation time, for example: "12:30:55"
#include <stdio.h>
int main(void)
{
printf("%s\r\n",__FILE__);
printf("%d\r\n",__LINE__);
printf("%s\r\n",__DATE__);
printf("%s\r\n",__TIME__);
return 0;
}
Printing Results
speci_define.c
6
Jul 6 2019
00:46:39
2.4 Message
Parts that often need to be changed
2.4.1 Message processing:
The most modified part in secondary development is the message processing, and the most modified is the key message processing. Key messages include long press, short press, double press, triple press, quadruple press, quintuple press, etc. Take
Func function as an example
AT(.text.func.msg)
void func_message(u16 msg)
{
switch (msg) {
case KL_NEXT_VOL_UP://If you press and hold the NEXT_VOL_UP button
if (sfunc_bt_call_flag) {
…
break;
}
…
}
2.4.2 Source of information:
Taking keystroke as an example
void msg_enqueue(u16 msg); //Message queue
1
executes the key scanning function in the program:
u8 bsp_key_scan(void)
{
u8 key_val = NO_KEY;
u16 key = NO_KEY;
pwrkey_2_hw_pwroff_detect();
if (!get_adc_val()) {
return NO_KEY;
}
#if USER_ADKEY
key_val = get_adkey(adc_cb.key_val, xcfg_cb.user_adkey_en);
#endif // USER_ADKEY
#if USER_ADKEY2
if (key_val == NO_KEY) {
key_val = get_adkey2();
}
#endif // USER_ADKEY2
#if USER_IOKEY
if (key_val == NO_KEY) {
key_val = get_iokey();
}
#endif // USER_IOKEY
#if USER_PWRKEY
if (key_val == NO_KEY) {
key_val = get_pwrkey();
}
#endif // USER_PWRKEY
#if VBAT_DETECT_EN
sys_cb.vbat = get_vbat_val();
#endif // VBAT_DETECT_EN
#if (IRRX_SW_EN || IRRX_HW_EN)
if (key_val == NO_KEY) {
key_val = get_irkey();
}
#endif // (IRRX_SW_EN || IRRX_HW_EN)
#if USER_ADKEY_MUX_SDCLK
//Needs to be processed last, needs to be returned when adc convert is not performedif
(key_val == NO_KEY) {
if (!adc_cb.sdclk_valid) {
return NO_KEY;
}
key_val = get_adkey(adc_cb.sdclk_val, xcfg_cb.user_adkey_mux_sdclk_en);
}
#endif // USER_ADKEY_MUX_SDCLK
key = bsp_key_process(key_val); //Get which key is pressed
if (key != NO_KEY) {
//printf("enqueue: %04x\n", key);
if ((key & KEY_TYPE_MASK) == KEY_LONG_UP) {
msg_queue_detach(key | KEY_HOLD); //Press and hold the key to clear the HOLD key message first
}
#if KEY_MSG_REMAP_EN
key_msg_remap(&key); //Key remapping.
#endif
msg_enqueue(key);//Put the message into the message queue
}
return key_val;
}
2.4.3 Notes on key messages:
The following macros are all key messages:
Take the PLAY key as an example
#define K_PLAY (KEY_PLAY | KEY_SHORT) //Falling edge
#define KU_PLAY (KEY_PLAY | KEY_SHORT_UP) //Rising edge
#define KL_PLAY (KEY_PLAY | KEY_LONG) //Long press
#define KLU_PLAY (KEY_PLAY | KEY_LONG_UP) //Long press on rising edge
#define KH_PLAY (KEY_PLAY | KEY_HOLD) //Long press for about 2 seconds
#define KD_PLAY (KEY_PLAY | KEY_DOUBLE) //Double click
#define KTH_PLAY (KEY_PLAY | KEY_THREE) //Triple click
#define KFO_PLAY (KEY_PLAY | KEY_FOUR) //Fourth click
#define KFI_PLAY (KEY_PLAY | KEY_FIVE) //Fiveth click
Note! Each key press will trigger a falling edge.
Take Bluetooth mode as an example:
the program first makes a judgment in the func_bt_message function. If no consistent case is found in this function, it will run to the public message processing function void func_message(u16 msg) to make another judgment.
AT(.text.func.bt.msg)
void func_bt_message(u16 msg)
{
switch (msg) {
case KU_PLAY:
case KU_PLAY_POWER:
case KU_PLAY_MODE:
bt_music_play_pause();
f_bt.pp_2_unmute = sys_cb.mute;
break;
case KU_PREV_VOL_DOWN:
case KL_VOL_DOWN_PREV:
case KU_PREV:
bt_music_prev();
sys_cb.key2unmute_cnt = 15 * sys_cb.mute;
break;
……
default:
func_message(msg);
break;
2.4.4 应用:1S消息
In the timer, send a message MSG_SYS_1S every second.
msg_enqueue(MSG_SYS_1S);
void usr_tmr5ms_isr(void)
{
//1s timer process
if ((tmr5ms_cnt % 200) == 0) {
msg_enqueue(MSG_SYS_1S);
tmr5ms_cnt = 0;
sys_cb.lpwr_warning_cnt++;
}
}
Then process the Bluetooth message or public message. The commonly used 1-second message processing includes reporting battery power and automatic playback when connecting to Bluetooth.
case MSG_SYS_1S:
bt_send_msg(BT_MSG_HFP_REPORT_BAT);
break;
2.4.5 Bluetooth message function:
There are three states of message processing. The Bluetooth mode is quite special. In addition to one func_bt_message, there are two more: ringing and talking.
Ringing: void sfunc_bt_ring_message(u16 msg)
performs message processing when an incoming call rings, mainly including answering/hanging up the call, power report and key message public processing.
AT(.text.func.btring.msg)
void sfunc_bt_ring_message(u16 msg)
{
switch (msg) {
case KU_PLAY:
case KU_HSF: //接听
case KU_PLAY_POWER:
case KU_PLAY_MODE:
bsp_clr_mute_sta();
bt_call_answer_incoming();
break;
case KD_PLAY:
case KL_HSF:
case KD_HSF:
bsp_clr_mute_sta();
bt_call_terminate(); //挂断
break;
case MSG_SYS_1S:
bt_send_msg(BT_MSG_HFP_REPORT_BAT);
break;
default:
func_message(msg);
break;
}
}
During a call: sfunc_bt_call_message();
Key message processing during a call, mainly including volume adjustment, three-way calling, and battery report
Music: void func_bt_message(u16 msg)
Bluetooth music mode message processing, up and down song switching, pause playback, volume adjustment, battery power reporting, etc.
|