一、文件下载与编译;
1:文件下载链接:https://www.dialog-semiconductor.com/products/connectivity/bluetooth-low-energy/smartbond-da14585-and-da14586
2:软件应用程序和示例:使用Python脚本进行设置
1. Python 2.7 is the minimal requirement to run the small example environment generation script 2. If needed then donwload and install python from https://www.python.org/downloads/ 3. Download the example from dialog support portal 4. Extract the zip file 5. open a terminal and run: > cd /project_environment > python dlg_make_keil5_env_v1.002.py sdkpath "" For instance: > python dlg_make_keil5_env_v1.002.py sdkpath "C:\dev\6.1.10.511"
3:点击编译即可通过;
二、文件解释:
1:user_driver :为IIC驱动代码:
2: user_app :主要蓝牙使用和数据发送
3:user_platform : da14585 外设初始化,以及接口;
4:最后两个是DA14585蓝牙广播配置:
三、代码解释:
申明:由于本人也是菜鸟,并且也是初学DA14585 ;错误地方还望留言改正;
1:首先定位到main函数:
硬件初始化流程:main() -> system_init();-> GPIO_init() 和 periph_init();
蓝牙广播初始化:不细说
IIC获取数据和发送数据:
首先定位到usr_app->usr_accelerometer.c->user_catch_rest_hndl函数中:
该函数为蓝牙状态处理函数:
void user_catch_rest_hndl(ke_msg_id_t const msgid,
void const *param,
ke_task_id_t const dest_id,
ke_task_id_t const src_id)
{
switch(msgid)
{
case GAPC_PARAM_UPDATED_IND:
{
// Cast the "param" pointer to the appropriate message structure
struct gapc_param_updated_ind const *msg_param = (struct gapc_param_updated_ind const *)(param);
// Check if updated Conn Params filled to preferred ones
if ((msg_param->con_interval >= user_connection_param_conf.intv_min) &&
(msg_param->con_interval <= user_connection_param_conf.intv_max) &&
(msg_param->con_latency == user_connection_param_conf.latency) &&
(msg_param->sup_to == user_connection_param_conf.time_out))
{
}
} break;
case CUSTS1_VAL_WRITE_IND:
{
struct custs1_val_write_ind const *msg_param = (struct custs1_val_write_ind const *)(param);
switch (msg_param->handle)
{
case SVC1_IDX_ACCEL_X_NTF_CFG:
user_svc1_accel_X_wr_ntf_handler(msg_param);
break;
case SVC1_IDX_ACCEL_Y_NTF_CFG:
user_svc1_accel_Y_wr_ntf_handler(msg_param);
break;
case SVC1_IDX_ACCEL_Z_NTF_CFG:
user_svc1_accel_Z_wr_ntf_handler(msg_param);
break;
case SVC2_IDX_G_NTF_CFG:
user_svc2_g_wr_ntf_handler(msg_param);
break;
default:
break;
}
} break;
case CUSTS1_VAL_NTF_CFM:
{
struct custs1_val_ntf_cfm const *msg_param = (struct custs1_val_ntf_cfm const *)(param);
switch (msg_param->handle)
{
case SVC1_IDX_ACCEL_X_DATA_VAL:
break;
case SVC1_IDX_ACCEL_Y_DATA_VAL:
break;
case SVC1_IDX_ACCEL_Z_DATA_VAL:
break;
default:
break;
}
} break;
default:
break;
}
}
GAPC_PARAM_UPDATED_IND:判断连接参数是否更新,如果更新在这里更新;
CUSTS1_VAL_WRITE_IND :表示已写入要广播的数值;
CUSTS1_VAL_NTF_CFM:接收到消息的相应;
在case CUSTS1_VAL_WRITE_IND 使用user_svc1_accel_X_wr_ntf_handler(msg_param);函数;
首先判断定时器是否工作:X_timer的值;
如果定时器失效;就创建定时器, app_easy_timer(10, user_svc1_accel_X_send_ntf);
如果定时器没有失效,说明之前还有定时任务,结束定时器 app_easy_timer_cancel(X_timer);
user_svc1_accel_X_send_ntf 在此函数中 使用IIC读取数据并写入广播数据;
void user_svc1_accel_X_send_ntf()
{
//Construct the string to send as a notification
//uint8_t string_length = user_int_to_string(read_ADXL345_X() * 3.9, X_string); //Read data and multipy by 3.9 to get acceleration in mg
uint8_t string_length = user_int_to_string(123 * 3.9, X_string); //Read data and multipy by 3.9 to get acceleration in mg
//Allocate a new message
struct custs1_val_ntf_ind_req* req = KE_MSG_ALLOC_DYN(CUSTS1_VAL_NTF_REQ, //Message id
prf_get_task_from_id(TASK_ID_CUSTS1), //Target task
TASK_APP, //Source of the message
custs1_val_ntf_ind_req, //The type of structure in the message,
//This structure should match the ID
//The ID's and strucures are found in custs1_task.h
string_length); //How many bytes of data will be added
req->conidx = 0; //Connection ID to send the data to (this application can only have one connection(0))
req->notification = true; //Data is sent as a notification and not as indication
req->handle = SVC1_IDX_ACCEL_X_DATA_VAL; //The handle of the characteristic we want to write to
req->length = string_length; //Data length in bytes
memcpy(req->value, X_string, string_length);//Copy the string to the message
ke_msg_send(req); //Send the message to the task
X_timer = app_easy_timer(NOTIFICATION_DELAY / 10, user_svc1_accel_X_send_ntf); //Set a timer for 100 ms (10*10)
}
当蓝牙断开后回调以下函数;关闭所有定时器;并标记失效;
void user_app_disconnect(struct gapc_disconnect_ind const *param)