zoukankan      html  css  js  c++  java
  • Examples

    记录NRF52840 添加LED service的流程,以及遇到的问题。

    由于SDK中已经有了led service的.c和.h文件,因此只需要添加文件,并且调用相关函数即可。

     

    注:编译调试环境为keil5

     

    1. 在main.c中包含ble_lbs.h头文件

    #include "ble_lbs.h" 

     

    2. 将源文件添加到工程中。

    工程窗口左边,在nRF_BLE_Services目录左键点击,选择"Add Existing Files To Group  nRF_BLE_Services", 选择ble_lbs.c

     

    3. sdk_config.h 中使能LBS 服务

    打开sdk_config.h, 设置

    #define BLE_LBS_ENABLED 1

     

    4. 在 main.c中添加服务的数据结构作为全局静态变量:

    BLE_LBS_DEF(m_lbs);

     

    5. 在services_init()中添加 lbs_init()

    static void services_init(void)
    {
        qwr_init();
        dis_init();
        bas_init();
        hids_init();
        lbs_init(); 
    }

     

    6. 实现lbs_init()函数

    static void lbs_init(void)
    {
        
        ret_code_t     err_code;
        ble_lbs_init_t lbs_init_obj={0};
         
        memset(&lbs_init_obj, 0, sizeof(lbs_init_obj));
    
        lbs_init_obj.led_write_handler = ipl_led_write_handler;
    
        err_code = ble_lbs_init(&m_lbs, &lbs_init_obj);
        APP_ERROR_CHECK(err_code);
        
    }

     

    static void ipl_led_write_handler( uint16_t handle, ble_lbs_t *p_lbs, unsigned char led_state )
    {
            NRF_LOG_INFO( "handle = %x, state=%d
    ",handle,led_state );
            if( led_state )
            {
                    nrf_gpio_pin_set( LED_3 );
            }
            else
            {
                    nrf_gpio_pin_clear( LED_3 );
            }
    }

     

    7. 编译工程,下载调试。

     

    8. 调试打印,出现开发板一直重复RESET。

    添加打印,发现是lbs_init()中出错, err_code = 4 (NRF_ERROR_NO_MEM)

     

     err_code = ble_lbs_init(&m_lbs, &lbs_init_obj);
     NRF_LOG_INFO("ble_lbs_init = %d
    ", err_code); 
     APP_ERROR_CHECK(err_code);

     

    该问题是由于系统没有分配足够的空间给服务引起的。需要修改私有服务的数量以及GATT_ATTR_TAB的大小

    -> static void ble_stack_init(void)
    
    -> ret_code_t nrf_sdh_ble_default_cfg_set(uint8_t conn_cfg_tag, uint32_t * p_ram_start)
    ->
    
    // Configure number of custom UUIDS. 设置定制的私有UUID数目
    memset(&ble_cfg, 0, sizeof(ble_cfg));
    ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = NRF_SDH_BLE_VS_UUID_COUNT;    //修改私有UUID数量, SIG定义的共有任务不计入其中
    
    ret_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, *p_ram_start);
    if (ret_code != NRF_SUCCESS)
    {
         NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_COMMON_CFG_VS_UUID.",
         nrf_strerror_get(ret_code));
    }
    
    // Configure the GATTS attribute table.
    memset(&ble_cfg, 0x00, sizeof(ble_cfg));
    ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE;  //修改GATTS_ATTR_TAB_SIZE大小
    
    ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, *p_ram_start);
    if (ret_code != NRF_SUCCESS)
    {
        NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_ATTR_TAB_SIZE.",
        nrf_strerror_get(ret_code));
    }

     

    sdk_config.h中修改

    // <o> NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE - Attribute Table size in bytes. The size must be a multiple of 4. 
    #ifndef NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
    #define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 2048//1408
    #endif
    
    // <o> NRF_SDH_BLE_VS_UUID_COUNT - The number of vendor-specific UUIDs. 
    #ifndef NRF_SDH_BLE_VS_UUID_COUNT
    #define NRF_SDH_BLE_VS_UUID_COUNT 5
    #endif

     

    9. 重新编译,下载调试。

    还是会一直RESET,并打印如下内容:

    <warning> nrf_sdh_ble:Insufficient RAM allocated for the softDevice.

    <warning> nrf_sdh_ble: Change the RAM start location from 0x20000010 to 0x200024E0

     

    原因是,增加了UUID服务后,ram使用的空间也要增加,RAM的地址也要修改。一个ram大约是0x10,因此需要按照打印设置RAM的起始位置以及大小

     

    方法:

    工程窗口左边,在工程根目录左键点击,选择“Options for Target xxxxxx”,设置ram的起始地址和大小

    ram原起始地址和大小为:

     

    修改为打印希望修改的值:

    注:SEGGER中修改方法:修改Options->Linker->Section Placement Macros(双击)->错误代码中建议的内存值,保存并Rebuild

    该图片取自:https://blog.csdn.net/qq_26957203/article/details/90647808

    10. 重新编译下载调试。

    配对手机,打开nRF Connect APP, 可以观察到添加的LED服务,并且可以通过手机控制开发板的led 亮灭

  • 相关阅读:
    使用Vue做个简单的评论 + localstorage存储
    使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provide the "name" option.(未注册组件)的原因之一
    自定义Vue组件
    HBuilder X 中使用模拟器进行App开发
    使用ajax上传图片,并且使用canvas实现出上传进度效果
    js 实现图片预览的两种方式
    破解百度云下载速度限制的方法
    图形开发语言 工具
    EC20的低功耗模式
    STM32单片机的软件重启和远程重启
  • 原文地址:https://www.cnblogs.com/hjj801006/p/13534822.html
Copyright © 2011-2022 走看看