从0开始
0----下载SDK
nRF5 SDK downloads - nordicsemi.com
1---验证板子SDK \examples\peripheral\blinky\pca10056\blank\arm5_no_packs
错误笔记: Software Pack 'NordicSemiconductor.nRF_DeviceFamilyPack.8.40.3' is not installed
NordicSemiconductor.nRF_DeviceFamilyPack.#.#.#.pack下载方法_竹叶木木的博客-CSDN博客
1、打开
http://developer.nordicsemi.com/nRF5_SDK/pieces/nRF_DeviceFamilyPack/
2、找到自己需要的软件包,下载
3、打开以下路径,将下载好的压缩包保存,如下载了NordicSemiconductor.nRF_DeviceFamilyPack.8.24.1pack.zip,解压,重命名为8.24.1。
4、完成
5、若在工程中还是找不到该软件包,重新打开keil工程即可。
————————————————
版权声明:本文为CSDN博主「竹叶木木」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41430066/article/details/104125524
开发板效果是4个LED流水点亮 没有LOG输出
+++++++++++++++++++++run其他
examples\ble_peripheral\ble_app_blinky\pca10056
不要run center里面的 它是主机 去扫描的 我的话自己有手机 选择板子去广播的案例
+++++++++++++++++++++
2---fanstal的网关的话 修改板子代码
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\components\boards\pca10056.h
//#define LEDS_NUMBER 4
//#define LED_1 NRF_GPIO_PIN_MAP(0,13)
//#define LED_2 NRF_GPIO_PIN_MAP(0,14)
#define LED_3 NRF_GPIO_PIN_MAP(0,15)
#define LED_4 NRF_GPIO_PIN_MAP(0,16)
#define LED_START LED_1
//#define LED_STOP LED_4
#define LEDS_ACTIVE_STATE 0
//#define LEDS_LIST { LED_1, LED_2, LED_3, LED_4 }
#define LEDS_INV_MASK LEDS_MASK
#define LEDS_NUMBER 2
#define LED_1 NRF_GPIO_PIN_MAP(1, 0)
#define LED_2 NRF_GPIO_PIN_MAP(0, 11)//11
#define LED_STOP LED_2
#define LEDS_LIST { LED_1, LED_2}
1--原理图
2--烧录
保存固件
+++++++++++++++++++
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\examples\ble_peripheral\ble_app_uart\pca10056\s140\arm5_no_packs
这就是NUS
先最为从机 我手机APP扫描到它 然后它发消息过来!
#define DEVICE_NAME "Nordic_UART"
届时 它的广播名字
services_init
nus_data_handler
注意一下
发广播的是从机-它是ble_nus.c--它是发送函数 ble_nus_data_send
去扫描的是主机-它是ble_nus_c.c--它是发送函数 ble_nus_c_string_send
我现在不要UART 我是手机APP点击以后 就从机一直不停的应答
它是在
on_write
p_client->is_notification_enabled = true;
evt.type = BLE_NUS_EVT_COMM_STARTED;
注意这个枚举
typedef enum
{
BLE_NUS_EVT_RX_DATA, /**< Data received. */
BLE_NUS_EVT_TX_RDY, /**< Service is ready to accept new data to be transmitted. */
BLE_NUS_EVT_COMM_STARTED, /**< Notification has been enabled. */
BLE_NUS_EVT_COMM_STOPPED, /**< Notification has been disabled. */
} ble_nus_evt_type_t;
主函数只是处理了 从机收到消息以后怎么办
static void nus_data_handler(ble_nus_evt_t * p_evt)
{
if (p_evt->type == BLE_NUS_EVT_RX_DATA)
{
uint32_t err_code;
NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
{
do
{
err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
{
NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_BUSY);
}
if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
{
while (app_uart_put('\n') == NRF_ERROR_BUSY);
}
}
}
而前面的 从机收到使能CCCD该怎么办 主函数没有处理 我就在这里写即可
static void nus_data_handler(ble_nus_evt_t * p_evt)
{
if (p_evt->type == BLE_NUS_EVT_RX_DATA)
{
uint32_t err_code;
NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
{
do
{
err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
{
NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_BUSY);
}
if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
{
while (app_uart_put('\n') == NRF_ERROR_BUSY);
}
}
else if (p_evt->type == BLE_NUS_EVT_COMM_STARTED){
uint32_t err_code;
err_code = app_timer_start(tx_timer_id, APP_TIMER_TICKS(1000), NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("BLE_NUS_EVT_COMM_STARTED.");
}
else if (p_evt->type == BLE_NUS_EVT_COMM_STOPPED){
uint32_t err_code;
err_code = app_timer_stop(tx_timer_id);
APP_ERROR_CHECK(err_code);
num='0';
NRF_LOG_INFO("BLE_NUS_EVT_COMM_STOPPED.");
}
}
前面这个就增加定时器
APP_TIMER_DEF(tx_timer_id);
static uint8_t num='0';
static void data_output_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
uint32_t err_code;
uint16_t numlen=1;
err_code = ble_nus_data_send(&m_nus, &num, &numlen, m_conn_handle);
if ((err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_NOT_FOUND))
{
APP_ERROR_CHECK(err_code);
}
num++;
}
static void timers_init(void)
{
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
err_code = app_timer_create(&tx_timer_id,
APP_TIMER_MODE_REPEATED,
data_output_timeout_handler);
// err_code = app_timer_start(tx_timer_id, APP_TIMER_TICKS(100), NULL);
// APP_ERROR_CHECK(err_code);
}
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\examples\ble_peripheral\ble_app_uart\pca10056\s140\arm5_no_packs\_build
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\components\softdevice\s140\hex
测试效果和预计一样
手机点击使能以后手机看到数据在变化!
而且直接可以看LOG的 说明我的修改可以
看一下UUID
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\components\ble\ble_services\ble_nus\ble_nus.c(64) :
#define NUS_BASE_UUID {{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E}} /**< Used vendor specific UUID. */
ble_nus_init
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
标准NUS的样子
++++++++++++++++++++
继续看
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\examples\ble_central\ble_app_uart_c\pca10056\s140\arm5_no_packs
nus的主机 这就是GW
我希望它扫到一个然后连接然后断开
源码
static void scan_init(void)
{
ret_code_t err_code;
nrf_ble_scan_init_t init_scan;
memset(&init_scan, 0, sizeof(init_scan));
init_scan.connect_if_match = true;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER, false);
APP_ERROR_CHECK(err_code);
}
它是通过UUID筛选设备的 我通过名字就修改一下
我的从机 的名字是GKOSON
#define NAME_FILTER "GKOSON"
static void scan_init(void)
{
ret_code_t err_code;
nrf_ble_scan_init_t init_scan;
memset(&init_scan, 0, sizeof(init_scan));
init_scan.connect_if_match = true;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);
// err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
// APP_ERROR_CHECK(err_code);
// err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER, false);
// APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, NAME_FILTER);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
APP_ERROR_CHECK(err_code);
}
那么我主动使能以后 就马上TX一下
然后我收到ACK以后 就马上断开连接-ble_nus_chars_received_uart_print
/**@snippet [Handling events from the ble_nus_c module] */
static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_ble_nus_evt)
{
ret_code_t err_code;
switch (p_ble_nus_evt->evt_type)
{
case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
NRF_LOG_INFO("Discovery complete.");
err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
APP_ERROR_CHECK(err_code);
err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Connected to device with Nordic UART Service.");
//使能以后 马上TX消息出去
static uint8_t data_array[4]={"HAHA"};
err_code = ble_nus_c_string_send(&m_ble_nus_c, data_array, 4);
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
{
NRF_LOG_ERROR("Failed sending NUS message. Error 0x%x. ", err_code);
APP_ERROR_CHECK(err_code);
}
break;
case BLE_NUS_C_EVT_NUS_TX_EVT:
ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
//收到从机的消息以后 主动断开
sd_ble_gap_disconnect(p_ble_nus_c->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
break;
case BLE_NUS_C_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected.");
scan_start();
break;
}
}
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\examples\ble_central\ble_app_uart_c\pca10056\s140\arm5_no_packs\_build
D:\git\DeviceDownload\nRF5_SDK_17.1.0_ddde560\components\softdevice\s140\hex
死机 调式一次
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, NAME_FILTER);
这里不走了
和配置有关系
..\..\..\main.c(88): warning: #177-D: variable "m_nus_uuid" was declared but never referenced
放弃UUID过滤的
开始测试
可以找到我的从机 断开一般是0X16 (我自己主机发的)但是也有0X13(从机发起的)
效果还可以 因为我的从机的逻辑是收到主机消息HAHA以后马上ACK 然后主动断开连接了