泰凌微 使用指北(四)
续前面的
上一节说到了main的主循环和初始化的函数 这些函数都在app.c里面有写 所以接下来继续介绍代码
app.c
//这里是信号发射强度
#define MY_RF_POWER_INDEX RF_POWER_P2p87dBm
//设备的地址
#define BLE_DEVICE_ADDRESS_TYPE BLE_DEVICE_ADDRESS_PUBLIC
//这里我赛了串口用到的东西
//module spp Tx / Rx fifo
u8 spp_rx_fifo_b[SPP_RXFIFO_SIZE * SPP_RXFIFO_NUM] = {0};
my_fifo_t spp_rx_fifo = {
SPP_RXFIFO_SIZE,
SPP_RXFIFO_NUM,
0,
0,
spp_rx_fifo_b,};
u8 spp_tx_fifo_b[SPP_TXFIFO_SIZE * SPP_TXFIFO_NUM] = {0};
my_fifo_t spp_tx_fifo = {
SPP_TXFIFO_SIZE,
SPP_TXFIFO_NUM,
0,
0,
spp_tx_fifo_b,};
#define UART_TX_BUSY ( (spp_tx_fifo.rptr != spp_tx_fifo.wptr) || Tr_isUartTxDone() )
#define UART_RX_BUSY (spp_rx_fifo.rptr != spp_rx_fifo.wptr)
1.user_init_normal(void)
这个是这样的用户初始化代码段
他是这样的 先初始化数据链路层ll,然后到hci通讯事件,然后到我自己的串口分析,然后到gap层
代码如下(示例):
random_generator_init(); //this is must 随机数生成器
blc_readFlashSize_autoConfigCustomFlashSector(); //读取flash大小并自动配置
/* attention that this function must be called after "blc_readFlashSize_autoConfigCustomFlashSector" !!!*/
blc_app_loadCustomizedParameters_normal(); //读取flash 进行rf频率偏移电容校准
//////////////////////////// BLE stack Initialization Begin //////////////////////////////////
u8 mac_public[6]; //创建了一个地址
u8 mac_random_static[6]; //创建了一个随机的值
blc_initMacAddress(flash_sector_mac_address, mac_public, mac_random_static);//这个是初始化mac地址的函数 从前面的flash读取 到后面的本地地址和随机地址两个
//数据链路层作为主机的状态机配置,在手册里面58页
////// Controller Initialization //////////
blc_ll_initBasicMCU();//mcu初始化 里面没东西
blc_ll_initStandby_module(mac_public); //mandatory
blc_ll_initScanning_module(); //scan module: mandatory for BLE master,
blc_ll_initMasterRole_module();//master module: mandatory for BLE master,
blc_ll_setAclConnMaxOctetsNumber(ACL_CONN_MAX_RX_OCTETS, ACL_CONN_MAX_TX_OCTETS);
blc_ll_initAclConnTxFifo(app_acl_txfifo, ACL_TX_FIFO_SIZE, ACL_TX_FIFO_NUM);
blc_ll_initAclConnRxFifo(app_acl_rxfifo, ACL_RX_FIFO_SIZE, ACL_RX_FIFO_NUM);
//////////// LinkLayer Initialization End /////////////////////////
//////////// HCI Initialization Begin /////////////////////////
//bluetooth event
blc_hci_setEventMask_cmd (HCI_EVT_MASK_DISCONNECTION_COMPLETE | HCI_EVT_MASK_ENCRYPTION_CHANGE);
//bluetooth low energy(LE) event//bluetooth low energy(LE) event //蓝牙低功耗//接收连接完成的事件,接收广播报告,连接更新完成,物理层接收完成,连接建立
blc_hci_le_setEventMask_cmd( HCI_LE_EVT_MASK_CONNECTION_COMPLETE \
| HCI_LE_EVT_MASK_ADVERTISING_REPORT \
| HCI_LE_EVT_MASK_CONNECTION_UPDATE_COMPLETE \
| HCI_LE_EVT_MASK_PHY_UPDATE_COMPLETE \
| HCI_LE_EVT_MASK_CONNECTION_ESTABLISH ); //connection establish: telink private event
blc_hci_registerControllerEventHandler(controller_event_callback);
//////////// HCI Initialization End /////////////////////////
//////////// Controller Initialization End /////////////////////////
//////////// Host Initialization Begin /////////////////////////
/* GAP initialization must be done before any other host feature initialization !!! */
/*GAP初始化必须在任何其他主机功能初始化之前完成*/
blc_gap_central_init(); //gap initialization
blc_gap_registerHostEventHandler( app_host_event_callback );
blc_gap_setEventMask(GAP_EVT_MASK_ATT_EXCHANGE_MTU);
blc_att_setRxMtuSize(MTU_SIZE_SETTING);
/* L2CAP Initialization */
blc_l2cap_register_handler(app_l2cap_handler);//注册这个l2cap回调函数
blc_l2cap_initRxDataBuffer(app_l2cap_rx_fifo, L2CAP_RX_BUFF_SIZE);
//配置rx fifo和最大长度
//到这里运行了一次 然后controller_event_callback没运行过
/* SMP Initialization */
/* SMP Initialization may involve flash write/erase(when one sector stores too much information,
* is about to exceed the sector threshold, this sector must be erased, and all useful information
* should re_stored) , so it must be done after battery check */
* /*SMP初始化可能涉及闪存写入/擦除(当一个扇区存储了太多信息时,
*即将超过扇区阈值,必须擦除此扇区,并删除所有有用信息
*应重新储存),因此必须在电池检查后进行*/
#if (BLE_HOST_SMP_ENABLE)
blm_smp_configPairingSecurityInfoStorageAddr(flash_sector_master_pairing);//配置存储配对安全信息的地址
blm_smp_registerSmpFinishCb(app_host_smp_finish);//注册一个完成smp的回调函数
blc_smp_central_init();
//SMP trigger by master//主控SMP触发 开启的事件是主设备首次配对和主设备初次连接
//SMP trigger by master
blm_host_smp_setSecurityTrigger(MASTER_TRIGGER_SMP_FIRST_PAIRING | MASTER_TRIGGER_SMP_AUTO_CONNECT);
#else
blc_smp_setSecurityLevel(No_Security);
#if (ACL_CENTRAL_CUSTOM_PAIR_ENABLE)
user_master_host_pairing_management_init();
#endif
#endif
#if (ACL_CENTRAL_SIMPLE_SDP_ENABLE)
host_att_register_idle_func (main_idle_loop);//这里是注册了一个sdp在mainloop中循环的sdp函数
#endif
////////////////// SPP initialization ///////////////////////////////////
//note: dma addr must be set first before any other uart initialization!
//先设置接收的fifo地址
u8 *uart_rx_addr = (spp_rx_fifo_b + (spp_rx_fifo.wptr & (spp_rx_fifo.num-1)) * spp_rx_fifo.size);
//初始化接收的fifo地址
uart_recbuff_init(UART_CONVERT((unsigned char *) uart_rx_addr, spp_rx_fifo.size));
//初始化串口引脚
uart_gpio_set(UART_CONVERT(UART_TX_PIN, UART_RX_PIN));
//重启串口
uart_reset(UART_NUM); //will reset uart digital registers from 0x90 ~ 0x9f, so uart setting must set after this reset
//baud rate: 9600
//配置串口波特率
uart_init_baudrate(UART_CONVERT(UART_BAUD_RATE, CLOCK_SYS_CLOCK_HZ, PARITY_NONE, STOP_BIT_ONE));
//开启串口dma
uart_dma_enable(UART_CONVERT(1,1)); //uart data in hardware buffer moved by dma, so we need enable them first
//这个是开启dma的irq
irq_enable_type(FLD_IRQ_DMA_EN);// uart_rx use dma_rx irq
//这个是开启dma的串口接收的功能
dma_chn_irq_enable(FLD_DMA_CHN_UART_RX, 1); //uart Rx dma irq enable
//启用发送完成中断
uart_mask_tx_done_irq_enable(UART_NUM);
//启用发送错误中断
uart_mask_error_irq_enable(UART_NUM);// open uart_error_mask,when stop bit error or parity error,it will enter error_interrupt.
//开启串口的tx中断
irq_enable_type(FLD_IRQ_UART_EN);// uart_tx use uart_txdone irq
//设置串口的发送完成的处理
Tr_SetUartTxDone();
extern int rx_from_uart_cb (void);
extern int tx_to_uart_cb (void);
//注册两个串口的回调函数
blc_register_hci_handler(rx_from_uart_cb, tx_to_uart_cb); //customized uart handler
//extern int controller_event_handler(u32 h, u8 *para, int n);
//注册处理来自ble控制器的事件回调函数
//blc_hci_registerControllerEventHandler(controller_event_handler); //register event callback
//设置事件掩码
//bls_hci_mod_setEventMask_cmd(0xfffff); //enable all 18 events,event list see ll.h
//偷偷塞一个led调试进来
/*led初始化*/
gpio_set_func(LED_RED, AS_GPIO); //设置为io
gpio_set_input_en(LED_RED, 0); //关闭io输入
gpio_set_output_en(LED_RED, 1); //设置为io输出
gpio_write(LED_RED, LED_OFF_LEVAL); //关闭LED
//////////////////////////// User Configuration for BLE application ////////////////////////////
/* set rf power index, user must set it after every suspend wakeup, cause relative setting will be reset in suspend */
rf_set_power_level_index (MY_RF_POWER_INDEX);
/* set scan parameter and scan enable */
blc_ll_setCentralScanParameter(SCAN_TYPE_PASSIVE, SCAN_INTERVAL_100MS, SCAN_INTERVAL_100MS, \
OWN_ADDRESS_PUBLIC, SCAN_FP_ALLOW_ADV_ANY);
blc_ll_setScanEnable (BLC_SCAN_ENABLE, DUP_FILTER_DISABLE);
/* Check if any Stack(Controller & Host) Initialization error after all BLE initialization done.
* attention that code will stuck in "while(1)" if any error detected in initialization, user need find what error happens and then fix it */
blc_app_checkControllerHostInitialization();
2.main_loop()
void main_loop(void)
{
main_idle_loop ();
#if (ACL_CENTRAL_SIMPLE_SDP_ENABLE)
simple_sdp_loop();//简单空任务 没东西的
#endif
}
3.main_idle_loop
int main_idle_loop (void)
{
////////////////////////////////////// BLE entry /////////////////////////////////
blm_sdk_main_loop(); //系统sdk的循环 动不了
//当ll处于Idle state空闲状态时候,上面的循环就不起作用了 直接执行下面的
//链路层有扫描 空闲 发起 连接 具体图要看58页
host_pair_unpair_proc(); //这个是用来持续检测断开的函数
////////////////////////////////////// UI entry /////////////////////////////////
#if (UI_LED_ENABLE)
gpio_write(GPIO_LED_GREEN,1);
#endif
#if (UI_KEYBOARD_ENABLE)
proc_keyboard (0, 0, 0);
#endif
//proc conn param update
if(host_update_conn_param_req){
host_update_conn_proc(); //这个函数是用来持续连接更新蓝牙
}
return 0;
}
4.app.h
这里面基本没什么改的所以就不写了
2687

被折叠的 条评论
为什么被折叠?



