编译环境:MDK5.25
移植例程:pca10040_ble_debug和pca10040_uart_debug
PC端UART升级master:官方的nrfutil和博客Nordic nRF5 串口固件更新例子(C代码)
一、用mdk打开pca10040_ble_debug工程
目录:…\nRF5_SDK_15.3.0_59ac345\examples\dfu\secure_bootloader\pca10040_ble_debug\arm5_no_packs
二、由于bootloader采用了加密的方式进行升级,升级前比对密匙,正确则进行DFU操作,否则返回错误,具体参照前面网友的博客
三、添加文件
在nRF_DFU目录下添加nrf_dfu_serial.c nrf_dfu_serial_uart.c两个文件
在nRF_Drivers目录下添加nrf_drv_uart.c nrf_nvic.c nrf_soc.c nrfx_prs.c nrfx_uart.c nrfx_uarte.c
在nRF_Libraries目录中添加slip.c
在nRF_Log目录中添加nrf_log_backend_uart.c
四、往MDK添加宏定义
MBR_PRESENT DFU_UART_SUPPORT_EN
五、打开sdk_config.h文件,在文件尾部添加一下宏定义开关,有相同的部分可以删掉,由于官方做了非定义识别,所以也可以不删除
//
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 增加Serial DFU(UART DFU)刷机功能部分 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//
#if defined (DFU_UART_SUPPORT_EN)
// SLIP_ENABLED - slip - SLIP encoding and decoding
#ifndef SLIP_ENABLED
#define SLIP_ENABLED 1
#endif
// nrf_dfu_serial_uart - UART DFU transport
//==========================================================
// NRF_DFU_SERIAL_UART_USES_HWFC - HWFC configuration
#ifndef NRF_DFU_SERIAL_UART_USES_HWFC
#define NRF_DFU_SERIAL_UART_USES_HWFC 1
#endif
// NRF_DFU_SERIAL_UART_RX_BUFFERS - Number of RX buffers.
// Number of buffers depends on flash access vs.
// transport throughtput. If value is too low it may lead
// to received packets being dropped.
#ifndef NRF_DFU_SERIAL_UART_RX_BUFFERS
#define NRF_DFU_SERIAL_UART_RX_BUFFERS 3
#endif
// nRF_Drivers
//==========================================================
// NRFX_PRS_ENABLED - nrfx_prs - Peripheral Resource Sharing module
//==========================================================
#ifndef NRFX_PRS_ENABLED
#define NRFX_PRS_ENABLED 1
#endif
// NRFX_PRS_BOX_0_ENABLED - Enables box 0 in the module.
#ifndef NRFX_PRS_BOX_0_ENABLED
#define NRFX_PRS_BOX_0_ENABLED 0
#endif
// NRFX_PRS_BOX_1_ENABLED - Enables box 1 in the module.
#ifndef NRFX_PRS_BOX_1_ENABLED
#define NRFX_PRS_BOX_1_ENABLED 0
#endif
// NRFX_PRS_BOX_2_ENABLED - Enables box 2 in the module.
#ifndef NRFX_PRS_BOX_2_ENABLED
#define NRFX_PRS_BOX_2_ENABLED 0
#endif
// NRFX_PRS_BOX_3_ENABLED - Enables box 3 in the module.
#ifndef NRFX_PRS_BOX_3_ENABLED
#define NRFX_PRS_BOX_3_ENABLED 0
#endif
// NRFX_PRS_BOX_4_ENABLED - Enables box 4 in the module.
#ifndef NRFX_PRS_BOX_4_ENABLED
#define NRFX_PRS_BOX_4_ENABLED 1
#endif
// NRFX_PRS_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef NRFX_PRS_CONFIG_LOG_ENABLED
#define NRFX_PRS_CONFIG_LOG_ENABLED 0
#endif
// NRFX_PRS_CONFIG_LOG_LEVEL - Default Severity level
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#ifndef NRFX_PRS_CONFIG_LOG_LEVEL
#define NRFX_PRS_CONFIG_LOG_LEVEL 3
#endif
// NRFX_PRS_CONFIG_INFO_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_PRS_CONFIG_INFO_COLOR
#define NRFX_PRS_CONFIG_INFO_COLOR 0
#endif
// NRFX_PRS_CONFIG_DEBUG_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_PRS_CONFIG_DEBUG_COLOR
#define NRFX_PRS_CONFIG_DEBUG_COLOR 0
#endif
//
//
// NRFX_UARTE_ENABLED - nrfx_uarte - UARTE peripheral driver
//==========================================================
#ifndef NRFX_UARTE_ENABLED
#define NRFX_UARTE_ENABLED 1
#endif
// NRFX_UARTE0_ENABLED - Enable UARTE0 instance
#ifndef NRFX_UARTE0_ENABLED
#define NRFX_UARTE0_ENABLED 0
#endif
// NRFX_UARTE_DEFAULT_CONFIG_HWFC - Hardware Flow Control
// <0=> Disabled
// <1=> Enabled
#ifndef NRFX_UARTE_DEFAULT_CONFIG_HWFC
#define NRFX_UARTE_DEFAULT_CONFIG_HWFC 0
#endif
// NRFX_UARTE_DEFAULT_CONFIG_PARITY - Parity
// <0=> Excluded
// <14=> Included
#ifndef NRFX_UARTE_DEFAULT_CONFIG_PARITY
#define NRFX_UARTE_DEFAULT_CONFIG_PARITY 0
#endif
// NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE - Default Baudrate
// <323584=> 1200 baud
// <643072=> 2400 baud
// <1290240=> 4800 baud
// <2576384=> 9600 baud
// <3862528=> 14400 baud
// <5152768=> 19200 baud
// <7716864=> 28800 baud
// <8388608=> 31250 baud
// <10289152=> 38400 baud
// <15007744=> 56000 baud
// <15400960=> 57600 baud
// <20615168=> 76800 baud
// <30801920=> 115200 baud
// <61865984=> 230400 baud
// <67108864=> 250000 baud
// <121634816=> 460800 baud
// <251658240=> 921600 baud
// <268435456=> 1000000 baud
#ifndef NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE
#define NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE 30801920
#endif
// NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7
#ifndef NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY
#define NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY 6
#endif
// NRFX_UARTE_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef NRFX_UARTE_CONFIG_LOG_ENABLED
#define NRFX_UARTE_CONFIG_LOG_ENABLED 0
#endif
// NRFX_UARTE_CONFIG_LOG_LEVEL - Default Severity level
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#ifndef NRFX_UARTE_CONFIG_LOG_LEVEL
#define NRFX_UARTE_CONFIG_LOG_LEVEL 3
#endif
// NRFX_UARTE_CONFIG_INFO_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_UARTE_CONFIG_INFO_COLOR
#define NRFX_UARTE_CONFIG_INFO_COLOR 0
#endif
// NRFX_UARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_UARTE_CONFIG_DEBUG_COLOR
#define NRFX_UARTE_CONFIG_DEBUG_COLOR 0
#endif
//
//
// NRFX_UART_ENABLED - nrfx_uart - UART peripheral driver
//==========================================================
#ifndef NRFX_UART_ENABLED
#define NRFX_UART_ENABLED 1
#endif
// NRFX_UART0_ENABLED - Enable UART0 instance
#ifndef NRFX_UART0_ENABLED
#define NRFX_UART0_ENABLED 0
#endif
// NRFX_UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control
// <0=> Disabled
// <1=> Enabled
#ifndef NRFX_UART_DEFAULT_CONFIG_HWFC
#define NRFX_UART_DEFAULT_CONFIG_HWFC 0
#endif
// NRFX_UART_DEFAULT_CONFIG_PARITY - Parity
// <0=> Excluded
// <14=> Included
#ifndef NRFX_UART_DEFAULT_CONFIG_PARITY
#define NRFX_UART_DEFAULT_CONFIG_PARITY 0
#endif
// NRFX_UART_DEFAULT_CONFIG_BAUDRATE - Default Baudrate
// <323584=> 1200 baud
// <643072=> 2400 baud
// <1290240=> 4800 baud
// <2576384=> 9600 baud
// <3866624=> 14400 baud
// <5152768=> 19200 baud
// <7729152=> 28800 baud
// <8388608=> 31250 baud
// <10309632=> 38400 baud
// <15007744=> 56000 baud
// <15462400=> 57600 baud
// <20615168=> 76800 baud
// <30924800=> 115200 baud
// <61845504=> 230400 baud
// <67108864=> 250000 baud
// <123695104=> 460800 baud
// <247386112=> 921600 baud
// <268435456=> 1000000 baud
#ifndef NRFX_UART_DEFAULT_CONFIG_BAUDRATE
#define NRFX_UART_DEFAULT_CONFIG_BAUDRATE 30924800
#endif
// NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7
#ifndef NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY
#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 6
#endif
// NRFX_UART_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef NRFX_UART_CONFIG_LOG_ENABLED
#define NRFX_UART_CONFIG_LOG_ENABLED 0
#endif
// NRFX_UART_CONFIG_LOG_LEVEL - Default Severity level
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#ifndef NRFX_UART_CONFIG_LOG_LEVEL
#define NRFX_UART_CONFIG_LOG_LEVEL 3
#endif
// NRFX_UART_CONFIG_INFO_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_UART_CONFIG_INFO_COLOR
#define NRFX_UART_CONFIG_INFO_COLOR 0
#endif
// NRFX_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_UART_CONFIG_DEBUG_COLOR
#define NRFX_UART_CONFIG_DEBUG_COLOR 0
#endif
//
//
// UART_ENABLED - nrf_drv_uart - UART/UARTE peripheral driver - legacy layer
//==========================================================
#ifndef UART_ENABLED
#define UART_ENABLED 1
#endif
// UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control
// <0=> Disabled
// <1=> Enabled
#ifndef UART_DEFAULT_CONFIG_HWFC
#define UART_DEFAULT_CONFIG_HWFC 0
#endif
// UART_DEFAULT_CONFIG_PARITY - Parity
// <0=> Excluded
// <14=> Included
#ifndef UART_DEFAULT_CONFIG_PARITY
#define UART_DEFAULT_CONFIG_PARITY 0
#endif
// UART_DEFAULT_CONFIG_BAUDRATE - Default Baudrate
// <323584=> 1200 baud
// <643072=> 2400 baud
// <1290240=> 4800 baud
// <2576384=> 9600 baud
// <3862528=> 14400 baud
// <5152768=> 19200 baud
// <7716864=> 28800 baud
// <10289152=> 38400 baud
// <15400960=> 57600 baud
// <20615168=> 76800 baud
// <30801920=> 115200 baud
// <61865984=> 230400 baud
// <67108864=> 250000 baud
// <121634816=> 460800 baud
// <251658240=> 921600 baud
// <268435456=> 1000000 baud
#ifndef UART_DEFAULT_CONFIG_BAUDRATE
#define UART_DEFAULT_CONFIG_BAUDRATE 30801920
#endif
// UART_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7
#ifndef UART_DEFAULT_CONFIG_IRQ_PRIORITY
#define UART_DEFAULT_CONFIG_IRQ_PRIORITY 6
#endif
// UART_EASY_DMA_SUPPORT - Driver supporting EasyDMA
#ifndef UART_EASY_DMA_SUPPORT
#define UART_EASY_DMA_SUPPORT 1
#endif
// UART_LEGACY_SUPPORT - Driver supporting Legacy mode
#ifndef UART_LEGACY_SUPPORT
#define UART_LEGACY_SUPPORT 0
#endif
// UART0_ENABLED - Enable UART0 instance
//==========================================================
#ifndef UART0_ENABLED
#define UART0_ENABLED 1
#endif
// UART0_CONFIG_USE_EASY_DMA - Default setting for using EasyDMA
#ifndef UART0_CONFIG_USE_EASY_DMA
#define UART0_CONFIG_USE_EASY_DMA 1
#endif
//
//
// SLIP_ENABLED - slip - SLIP encoding and decoding
#ifndef SLIP_ENABLED
#define SLIP_ENABLED 1
#endif
//
//==========================================================
// nRF_Segger_RTT
//==========================================================
// segger_rtt - SEGGER RTT
//==========================================================
// SEGGER_RTT_CONFIG_BUFFER_SIZE_UP - Size of upstream buffer.
// Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE
// or this value is actually used. It depends on which one is bigger.
#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 4096
#endif
// SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Maximum number of upstream buffers.
#ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS
#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2
#endif
// SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of downstream buffer.
#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16
#endif
// SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Maximum number of downstream buffers.
#ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS
#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2
#endif
// SEGGER_RTT_CONFIG_DEFAULT_MODE - RTT behavior if the buffer is full.
// The following modes are supported:
// - SKIP - Do not block, output nothing.
// - TRIM - Do not block, output as much as fits.
// - BLOCK - Wait until there is space in the buffer.
// <0=> SKIP
// <1=> TRIM
// <2=> BLOCK_IF_FIFO_FULL
#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE
#define SEGGER_RTT_CONFIG_DEFAULT_MODE 0
#endif
//
//==========================================================
//
//==========================================================
#endif
//
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 增加Serial DFU(UART DFU)刷机功能部分 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//
ctrl+f搜索“NRF_DFU_PROTOCOL_REDUCED”宏定义,将它的值改为0,这个宏定义是定义精简指令的,在uart和ble使用的不一样,这里我们关闭
sdk_config.h切换到wizard界面,找到如下宏定义,将值改为info否则编译的时候提示空间不足
六、打开“nrf_dfu_req_handler.c”文件,找到宏定义“NRF_DFU_PROTOCOL_REDUCED”,在后面添加如下内容,这样修改在切换回ble的时候只修改sdk_config.h文件中的宏定义即可
七、打开“nrf_soc.c”文件,找到“#include “nrf_error.h””然后右键打开该文件,修改如下宏定义,nrf_error.h文件路径为“…\nRF5_SDK_15.3.0_59ac345\components\drivers_nrf\nrf_soc_nosd”
八、添加头文件路径
九、编译改错
十、新建一个文件夹将生成的hex文件拷贝到一起,名字自己起
十一、通过nrfjprog命令将hex文件烧录到芯片
也可以使用j-flash工具进行烧录
十二、制作DFU升级包,本次使用的是ble_app_uart例程,路径…\nRF5_SDK_15.3.0_59ac345\examples\ble_peripheral\ble_app_uart\pca10040\s132\arm5_no_packs
打开工程,增加DFU支持,DFU参照ble_app_buttonless_dfu例程,路径…\nRF5_SDK_15.3.0_59ac345\examples\ble_peripheral\ble_app_buttonless_dfu\pca10040\s132\arm5_no_packs
12.1、添加文件,在“nRF_BLE_Services”目录下添加ble_dfu.c ble_dfu_bonded.c ble_dfu_unbonded.c三个文件,在“nRF_Libraries”目录下添加nrf_dfu_svci.c
12.2、添加头文件路径
12.3、添加MDK宏定义DFU_SUPPORT_EN NRF_DFU_TRANSPORT_BLE=1 BL_SETTINGS_ACCESS_ONLY
DFU_SUPPORT_EN宏定义为了方便裁剪DFU功能而设置,也可以不用
12.4、打开main.c文件,添加dfu相关服务
添加头文件
添加dfu服务处理回调函数
//
//DFU支持
//
#if defined (DFU_SUPPORT_EN)
static void advertising_config_get(ble_adv_modes_config_t * p_config)
{
memset(p_config, 0, sizeof(ble_adv_modes_config_t));
p_config->ble_adv_fast_enabled = true;
p_config->ble_adv_fast_interval = APP_ADV_INTERVAL;
p_config->ble_adv_fast_timeout = APP_ADV_DURATION;
}
static void disconnect(uint16_t conn_handle, void * p_context)
{
UNUSED_PARAMETER(p_context);
ret_code_t err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_WARNING("Failed to disconnect connection. Connection handle: %d Error: %d", conn_handle, err_code);
}
else
{
NRF_LOG_DEBUG("Disconnected connection handle %d", conn_handle);
}
}
// YOUR_JOB: Update this code if you want to do anything given a DFU event (optional).
/**@brief Function for handling dfu events from the Buttonless Secure DFU service
*
* @param[in] event Event from the Buttonless Secure DFU service.
*/
static void ble_dfu_evt_handler(ble_dfu_buttonless_evt_type_t event)
{
switch (event)
{
case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE:
{
NRF_LOG_INFO("Device is preparing to enter bootloader mode.");
// Prevent device from advertising on disconnect.
ble_adv_modes_config_t config;
advertising_config_get(&config);
config.ble_adv_on_disconnect_disabled = true;
ble_advertising_modes_config_set(&m_advertising, &config);
// Disconnect all other bonded devices that currently are connected.
// This is required to receive a service changed indication
// on bootup after a successful (or aborted) Device Firmware Update.
uint32_t conn_count = ble_conn_state_for_each_connected(disconnect, NULL);
NRF_LOG_INFO("Disconnected %d links.", conn_count);
break;
}
case BLE_DFU_EVT_BOOTLOADER_ENTER:
// YOUR_JOB: Write app-specific unwritten data to FLASH, control finalization of this
// by delaying reset by reporting false in app_shutdown_handler
NRF_LOG_INFO("Device will enter bootloader mode.");
break;
case BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED:
NRF_LOG_ERROR("Request to enter bootloader mode failed asynchroneously.");
// YOUR_JOB: Take corrective measures to resolve the issue
// like calling APP_ERROR_CHECK to reset the device.
break;
case BLE_DFU_EVT_RESPONSE_SEND_ERROR:
NRF_LOG_ERROR("Request to send a response to client failed.");
// YOUR_JOB: Take corrective measures to resolve the issue
// like calling APP_ERROR_CHECK to reset the device.
APP_ERROR_CHECK(false);
break;
default:
NRF_LOG_ERROR("Unknown event from ble_dfu_buttonless.");
break;
}
}
#endif
在services_init()函数中添加dfu初始化部分
打开sdk_config.h文件,将BLE_DFU_ENABLED使能
12.5、编译改错
12.6、制作zip升级包,参考前面博客方法
十三、使用nrfutil命令将升级包通过UART刷进芯片,串口号具体看电脑显示,波特率默认115200,也可以修改bootloader程序中相关定义
使用前面网友博客的C代码升级:
将代码下载下来,解压,进入目录,进行编译
编译成功后,可以调用进行升级了,按住开发板的button4按钮,重启开发板,让程序进入bootloader,然后调用指令升级