nRF52840/52832 低功耗藍牙BLE4.2/5.0入門指南/自學筆記
nRF52840/52832自學筆記
1.1 新手入門------開發環境
項目 | 名稱 |
---|---|
開發環境 | Windows 10 |
開發工具 | MDK-ARM v5.26 |
SDK版本 | nRF5_SDK_15.2.0_9412b96 |
開發板1 | 青風nRF52832開發板(512kFLASH,64kRAM) |
開發板2 | 紅旭無線nRF52840/ESP32 mini開發板 |
J-link | 黑色磚頭J-link |
nRFgo Studio | 下載最新版本的nRFgo Studio(安裝時不要安裝nRF5x-Command-Line-Tools,版本太舊) |
nRF5x-Command-Line-Tools | 安裝最新版本的nRF5x-Command-Line-Tools(安裝時會彈出安裝J-link驅動,安裝即可,如有老版的J-link驅動,請先卸載在安裝) |
Python 2.7.13 | 安裝時請選擇添加到環境變量(小於2.7.9版本好像安裝時沒有選項,需要手動添加環境變量) |
nrfutil | windows命令行輸入pip install nrfutil後回車(如果Python安裝成功並添加環境變量) |
1.2 從機入門實例------ble_app_uart
1.2.1 修改時鐘(一般項目都沒有外部低速時鐘32.768kHz)
在sdk_config.h中修改時鐘宏定義,如下所示:
#ifndef NRF_SDH_CLOCK_LF_SRC
#define NRF_SDH_CLOCK_LF_SRC 0
#endif
#ifndef NRF_SDH_CLOCK_LF_RC_CTIV
#define NRF_SDH_CLOCK_LF_RC_CTIV 16
#endif
#ifndef NRF_SDH_CLOCK_LF_RC_TEMP_CTIV
#define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 2
#endif
#ifndef NRF_SDH_CLOCK_LF_ACCURACY
#define NRF_SDH_CLOCK_LF_ACCURACY 1
#endif
1.2.2 RTT打印顏色和Level修改
修改sdk_config.h打印相關的宏定義,工程配置加入DEBUG項,如下所示:
#ifndef NRF_LOG_DEFAULT_LEVEL
#define NRF_LOG_DEFAULT_LEVEL 4
#endif
#ifndef NRF_LOG_DEFERRED
#define NRF_LOG_DEFERRED 0
#endif
#ifndef NRF_LOG_USES_COLORS
#define NRF_LOG_USES_COLORS 1
#endif
#ifndef NRF_LOG_COLOR_DEFAULT
#define NRF_LOG_COLOR_DEFAULT 7
#endif
#ifndef NRF_SDH_BLE_LOG_LEVEL
#define NRF_SDH_BLE_LOG_LEVEL 4
#endif
#ifndef NRF_SDH_LOG_LEVEL
#define NRF_SDH_LOG_LEVEL 4
#endif
#ifndef NRF_SDH_SOC_LOG_LEVEL
#define NRF_SDH_SOC_LOG_LEVEL 4
#endif
1.2.3 屏蔽UART、Button和LED功能
- 不讓nrf_drv_uart.c,nrfx_uart.c,nrfx_uarte.c,app_uart_fifo.c,retarget.c五個文件進行編譯(對應的文件上右擊鼠標選擇“Options for File “nrf_drv_uart.c”…”選項,去掉"Include int Target Build"選項。
- main.c文件中屏蔽uart_init,uart_event_handle,app_uart_put三個函數的定義和調用
- main.c文件中屏蔽printf("\r\nUART started.\r\n");打印信息
- main.c文件中屏蔽buttons_leds_init,bsp_event_handler兩個函數的定義和調用
- main.c文件中屏蔽bsp_indication_set(包含後面一行的APP_ERROR_CHECK的調用)函數的定義和調用並在對應位置加上相應的打印
1.2.4 修改三分鐘進入sleep_mode_enter報錯,
- 修改廣播模式爲BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE,APP_ADV_DURATION=0爲一直廣播。
#define APP_ADV_DURATION 0 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
static void advertising_init(void)
{
uint32_t err_code;
ble_advertising_init_t init;
memset(&init, 0, sizeof(init));
init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
init.advdata.include_appearance = false;
init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
init.srdata.uuids_complete.p_uuids = m_adv_uuids;
init.config.ble_adv_fast_enabled = true;
init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
init.evt_handler = on_adv_evt;
err_code = ble_advertising_init(&m_advertising, &init);
APP_ERROR_CHECK(err_code);
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}
- main.c文件中屏蔽sleep_mode_enter函數的定義和調用並在對應位置加上相應的打印
1.2.5 連接硬件+燒錄程序
- nRF52832開發板的VCC、SWD、SCK、GND的四根線與J-link相連,J-link通過USB接口與電腦相連,如下圖所示:
- 打開nRFgo Studio整片擦除nRF52832程序,並燒錄目錄\nRF5_SDK_15.2.0_9412b96\components\softdevice\s132\hex\下的132協議棧,如下圖所示:
- 開J-link RTT Viewer程序,如下圖所示:
- 通過MDK-ARM5.26燒錄application程序,查看打印信息,如下圖所示: