improve dfu ble

This commit is contained in:
hathach 2018-05-25 21:33:47 +07:00
parent 52fa190853
commit f5682831ec
5 changed files with 73 additions and 130 deletions

2
.gitignore vendored
View File

@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
src/segger/Output

View File

@ -35,10 +35,11 @@
#define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */
/**< Maximum number of interrupts available. */
#if defined(NRF52832_XXAA)
#define MAX_NUMBER_INTERRUPTS 39
#elif defined(NRF52840_XXAA)
#define MAX_NUMBER_INTERRUPTS 48 /**< Maximum number of interrupts available. */
#define MAX_NUMBER_INTERRUPTS 48
#endif
/**@brief Enumeration for specifying current bootloader status.

View File

@ -42,18 +42,12 @@ enum { BLE_CONN_CFG_HIGH_BANDWIDTH = 1 };
#define DFU_REV_MAJOR 0x00 /** DFU Major revision number to be exposed. */
#define DFU_REV_MINOR 0x08 /** DFU Minor revision number to be exposed. */
#define DFU_REVISION ((DFU_REV_MAJOR << 8) | DFU_REV_MINOR) /** DFU Revision number to be exposed. Combined of major and minor versions. */
//#define ADVERTISING_LED_PIN_NO 17 /**< Is on when device is advertising. */
//#define CONNECTED_LED_PIN_NO 19 /**< Is on when device has connected. */
#define DFU_SERVICE_HANDLE 0x000C /**< Handle of DFU service when DFU service is first service initialized. */
#define BLE_HANDLE_MAX 0xFFFF /**< Max handle value is BLE. */
#define DEVICE_NAME "AdaDFU" // limit of 8 chars /**< Name of device. Will be included in the advertising data. */
#define DIS_MANUFACTURER "Adafruit Industries"
// DIS_MODEL is defined in boards.h
// TODO Update when upgrade bootloader
#define DIS_FIRMWARE "S140 6.0.0, 6.0.0"
// DIS_MODEL is defined in boards.h, DIS_FIRMWARE is defined by makefile
#define MIN_CONN_INTERVAL (uint16_t)(MSEC_TO_UNITS(10, UNIT_1_25_MS)) /**< Minimum acceptable connection interval (11.25 milliseconds). */
#define MAX_CONN_INTERVAL (uint16_t)(MSEC_TO_UNITS(30, UNIT_1_25_MS)) /**< Maximum acceptable connection interval (15 milliseconds). */
@ -63,7 +57,7 @@ enum { BLE_CONN_CFG_HIGH_BANDWIDTH = 1 };
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define APP_ADV_INTERVAL MSEC_TO_UNITS(25, UNIT_0_625_MS) /**< The advertising interval (25 ms.). */
#define APP_ADV_TIMEOUT_IN_SECONDS BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED /**< The advertising timeout in units of seconds. This is set to @ref BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED so that the advertisement is done as long as there there is a call to @ref dfu_transport_close function.*/
#define APP_ADV_TIMEOUT BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED /**< The advertising timeout in units of seconds. This is set to @ref BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED so that the advertisement is done as long as there there is a call to @ref dfu_transport_close function.*/
#define APP_DIRECTED_ADV_TIMEOUT 50 /**< number of direct advertisement (each lasting 1.28seconds). */
#define PEER_ADDRESS_TYPE_INVALID 0xFF /**< Value indicating that no valid peer address exists. This will be the case when a private resolvable address is used in which case there is no address available but instead an IRK is present. */
#define PEER_ADDRESS_TYPE_INVALID 0xFF /**< Value indicating that no valid peer address exists. This will be the case when a private resolvable address is used in which case there is no address available but instead an IRK is present. */
@ -99,7 +93,6 @@ typedef enum
} pkt_type_t;
static ble_gap_sec_params_t m_sec_params; /**< Security requirements for this application. */
static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
static ble_dfu_t m_dfu; /**< Structure used to identify the Device Firmware Update service. */
static pkt_type_t m_pkt_type; /**< Type of packet to be expected from the DFU Controller. */
static uint8_t m_update_mode; /**< Type of update mode specified by the DFU Controller. */
@ -663,74 +656,58 @@ static void advertising_init(ble_data_t* adv_data, uint8_t adv_flags)
*/
static void advertising_start(void)
{
if (!m_is_advertising)
if (!m_is_advertising)
{
uint8_t adv_buf[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
ble_gap_adv_data_t gap_adv =
{
uint32_t err_code;
.adv_data = { .p_data = adv_buf, .len = 0}
};
uint8_t adv_buf[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
ble_gap_adv_data_t gap_adv =
{
.adv_data = { .p_data = adv_buf, .len = 0}
uint8_t adv_flag = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
ble_gap_adv_params_t m_adv_params =
{
.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED,
.p_peer_addr = NULL,
.filter_policy = BLE_GAP_ADV_FP_ANY,
.interval = APP_ADV_INTERVAL,
.duration = APP_ADV_TIMEOUT,
.primary_phy = BLE_GAP_PHY_1MBPS,
};
if (m_ble_peer_data_valid)
{
ble_gap_irk_t empty_irk = {{0}};
if ( memcmp(m_ble_peer_data.irk.irk, empty_irk.irk, sizeof(empty_irk.irk)) == 0 )
{
// No IRK, general discovery
}
else
{
// Bonded previously
m_whitelist[0] = &m_ble_peer_data.addr;
APP_ERROR_CHECK( sd_ble_gap_whitelist_set(m_whitelist, 1) );
ble_gap_id_key_t id_key = {
.id_info = m_ble_peer_data.irk,
.id_addr_info = m_ble_peer_data.addr
};
// Initialize advertising parameters (used when starting advertising).
memset(&m_adv_params, 0, sizeof(m_adv_params));
m_gap_ids[0] = &id_key;
APP_ERROR_CHECK( sd_ble_gap_device_identities_set(m_gap_ids, NULL, 1) );
if (m_ble_peer_data_valid)
{
ble_gap_irk_t empty_irk = {{0}};
if (memcmp(m_ble_peer_data.irk.irk, empty_irk.irk, sizeof(empty_irk.irk)) == 0)
{
advertising_init(&gap_adv.adv_data, BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED_HIGH_DUTY_CYCLE;
m_adv_params.p_peer_addr = &m_ble_peer_data.addr;
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
m_adv_params.interval = 0;
m_adv_params.duration = 0;
}
else
{
m_whitelist[0] = &m_ble_peer_data.addr;
err_code = sd_ble_gap_whitelist_set(m_whitelist, 1);
APP_ERROR_CHECK(err_code);
ble_gap_id_key_t id_key = {
.id_info = m_ble_peer_data.irk,
.id_addr_info = m_ble_peer_data.addr
};
m_gap_ids[0] = &id_key;
err_code = sd_ble_gap_device_identities_set(m_gap_ids, NULL, 1);
APP_ERROR_CHECK(err_code);
advertising_init(&gap_adv.adv_data, BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
m_adv_params.filter_policy = BLE_GAP_ADV_FP_FILTER_CONNREQ;
m_adv_params.interval = APP_ADV_INTERVAL;
m_adv_params.duration = APP_ADV_TIMEOUT_IN_SECONDS;
}
}
else
{
advertising_init(&gap_adv.adv_data, BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
m_adv_params.p_peer_addr = NULL;
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
m_adv_params.interval = APP_ADV_INTERVAL;
m_adv_params.duration = APP_ADV_TIMEOUT_IN_SECONDS;
}
err_code = sd_ble_gap_adv_set_configure(&_adv_handle, &gap_adv, &m_adv_params);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_start(_adv_handle, BLE_CONN_CFG_HIGH_BANDWIDTH);
APP_ERROR_CHECK(err_code);
// led_on(ADVERTISING_LED_PIN_NO);
m_is_advertising = true;
adv_flag = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
m_adv_params.filter_policy = BLE_GAP_ADV_FP_FILTER_BOTH;
}
}
advertising_init(&gap_adv.adv_data, adv_flag);
APP_ERROR_CHECK( sd_ble_gap_adv_set_configure(&_adv_handle, &gap_adv, &m_adv_params) );
APP_ERROR_CHECK( sd_ble_gap_adv_start(_adv_handle, BLE_CONN_CFG_HIGH_BANDWIDTH) );
m_is_advertising = true;
}
}
@ -745,8 +722,6 @@ static void advertising_stop(void)
err_code = sd_ble_gap_adv_stop(_adv_handle);
APP_ERROR_CHECK(err_code);
// led_off(ADVERTISING_LED_PIN_NO);
m_is_advertising = false;
}
}
@ -766,7 +741,6 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
case BLE_GAP_EVT_CONNECTED:
blinky_ota_connected();
led_on(LED_BLUE);
// led_off(ADVERTISING_LED_PIN_NO);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
m_is_advertising = false;
@ -834,7 +808,6 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
break;
case BLE_GAP_EVT_ADV_SET_TERMINATED:
//case BLE_GAP_EVT_TIMEOUT:
if (p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
{
m_is_advertising = false;
@ -947,21 +920,6 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
on_ble_evt(p_ble_evt);
}
/**@brief Function for the LEDs initialization.
*
* @details Initializes all LEDs used by this application.
*/
static void leds_init(void)
{
// Adafruit: already done in bootloader's main
// nrf_gpio_cfg_output(ADVERTISING_LED_PIN_NO);
// nrf_gpio_cfg_output(CONNECTED_LED_PIN_NO);
// led_off(ADVERTISING_LED_PIN_NO);
// led_off(CONNECTED_LED_PIN_NO);
}
/**@brief Function for the GAP initialization.
*
* @details This function will setup all the necessary GAP (Generic Access Profile) parameters of
@ -1034,7 +992,7 @@ static void services_init(void)
ascii_to_utf8(&dis_init.manufact_name_str, DIS_MANUFACTURER);
ascii_to_utf8(&dis_init.model_num_str, DIS_MODEL);
ascii_to_utf8(&dis_init.fw_rev_str, DIS_FIRMWARE);
ascii_to_utf8(&dis_init.fw_rev_str, MK_DIS_FIRMWARE);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&dis_init.dis_attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&dis_init.dis_attr_md.write_perm);
@ -1065,8 +1023,6 @@ uint32_t dfu_transport_ble_update_start(void)
m_tear_down_in_progress = false;
m_pkt_type = PKT_TYPE_INVALID;
leds_init();
dfu_register_callback(dfu_cb_handler);
err_code = hci_mem_pool_open();
@ -1098,8 +1054,8 @@ uint32_t dfu_transport_ble_update_start(void)
services_init();
sec_params_init();
sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, 0, 4); // maximum power
advertising_start();
sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, _adv_handle, 4); // maximum power
return NRF_SUCCESS;
}

View File

@ -226,16 +226,13 @@ void blinky_ota_disconneted(void)
*/
static uint32_t ble_stack_init(bool init_softdevice)
{
uint32_t err_code;
if (init_softdevice)
{
sd_mbr_command_t com = { .command = SD_MBR_COMMAND_INIT_SD };
err_code = sd_mbr_command(&com);
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK( sd_mbr_command(&com) );
}
err_code = sd_softdevice_vector_table_base_set(BOOTLOADER_REGION_START);
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK( sd_softdevice_vector_table_base_set(BOOTLOADER_REGION_START) );
// Enable Softdevice
nrf_clock_lf_cfg_t clock_cfg =
@ -267,34 +264,30 @@ static uint32_t ble_stack_init(bool init_softdevice)
blecfg.gap_cfg.role_count_cfg.periph_role_count = 1;
blecfg.gap_cfg.role_count_cfg.central_role_count = 0;
blecfg.gap_cfg.role_count_cfg.central_sec_count = 0;
err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &blecfg, ram_start);
APP_ERROR_CHECK( sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &blecfg, ram_start) );
// NRF_DFU_BLE_REQUIRES_BONDS
varclr(&blecfg);
blecfg.gatts_cfg.service_changed.service_changed = 1;
err_code = sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &blecfg, ram_start);
VERIFY_SUCCESS(err_code);
APP_ERROR_CHECK( sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &blecfg, ram_start) );
// ATT MTU
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_HIGH_BANDWIDTH;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = BLEGATT_ATT_MTU_MAX;
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start);
VERIFY_SUCCESS ( err_code );
APP_ERROR_CHECK( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start) );
// Event Length + HVN queue + WRITE CMD queue setting affecting bandwidth
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_HIGH_BANDWIDTH;
blecfg.conn_cfg.params.gap_conn_cfg.conn_count = 1;
blecfg.conn_cfg.params.gap_conn_cfg.event_length = BLEGAP_EVENT_LENGTH;
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start);
VERIFY_SUCCESS ( err_code );
APP_ERROR_CHECK( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start) );
// Enable BLE stack.
err_code = sd_ble_enable(&ram_start);
VERIFY_SUCCESS(err_code);
APP_ERROR_CHECK( sd_ble_enable(&ram_start) );
return err_code;
return NRF_SUCCESS;
}
@ -303,8 +296,6 @@ static uint32_t ble_stack_init(bool init_softdevice)
*/
int main(void)
{
uint32_t err_code;
// SD is already Initialized in case of BOOTLOADER_DFU_OTA_MAGIC
bool sd_inited = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_OTA_MAGIC);
@ -350,14 +341,12 @@ int main(void)
if (bootloader_dfu_sd_in_progress())
{
err_code = bootloader_dfu_sd_update_continue();
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK( bootloader_dfu_sd_update_continue() );
ble_stack_init(!sd_inited);
app_timer_start(blinky_timer_id, APP_TIMER_TICKS(LED_BLINK_INTERVAL), NULL);
err_code = bootloader_dfu_sd_update_finalize();
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK( bootloader_dfu_sd_update_finalize() );
}
else
{
@ -393,8 +382,7 @@ int main(void)
if (dfu_start || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START)))
{
// Initiate an update of the firmware.
err_code = bootloader_dfu_start(_ota_update, 0);
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK( bootloader_dfu_start(_ota_update, 0) );
}
else
{
@ -544,7 +532,7 @@ void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
app_error_handler(0xDEADBEEF, line_num, p_file_name);
app_error_handler(0xDEADBEEF, line_num, p_file_name);
}
//--------------------------------------------------------------------+
@ -567,14 +555,6 @@ uint32_t tusb_hal_millis(void)
return ( ( ((uint64_t)app_timer_cnt_get())*1000*(APP_TIMER_CONFIG_RTC_FREQUENCY+1)) / APP_TIMER_CLOCK_FREQ );
}
void ada_ble_hanlder(ble_evt_t* evt)
{
// from dfu_transport_ble
extern void ble_evt_dispatch(ble_evt_t * p_ble_evt);
ble_evt_dispatch(evt);
}
/*------------------------------------------------------------------*/
/* SoftDevice Event handler
*------------------------------------------------------------------*/
@ -589,7 +569,11 @@ uint32_t proc_ble(void)
// Handle valid event, ignore error
if( NRF_SUCCESS == err)
{
ada_ble_hanlder( (ble_evt_t*) ev_buf );
// from dfu_transport_ble
extern void ble_evt_dispatch(ble_evt_t * p_ble_evt);
ble_evt_t* evt = (ble_evt_t*) ev_buf;
ble_evt_dispatch(evt);
}
return err;

View File

@ -23,7 +23,7 @@ TUSB_PATH = ../../lib/tinyusb/tinyusb
SD_NAME = s140
SD_VERSION = 6.0.0
SD_NAME_UPPER = $(subst s,S,$(SD_NAME))
MK_DIS_FIRMWARE = "$(subst s,S,$(SD_NAME)) $(SD_VERSION), $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_REVISION)"
SD_PATH = ../../lib/softdevice/$(SD_VERSION)
SD_HEX = $(SD_PATH)/$(SD_NAME)/hex/$(SD_NAME)_nrf52_$(SD_VERSION)_softdevice.hex
@ -201,8 +201,8 @@ CFLAGS += -DBLE_STACK_SUPPORT_REQD
CFLAGS += -DBSP_DEFINES_ONLY
CFLAGS += -DSWI_DISABLE0
CFLAGS += -DSOFTDEVICE_PRESENT
CFLAGS += -DFLOAT_ABI_HARD
CFLAGS += -DMK_DIS_FIRMWARE='$(MK_DIS_FIRMWARE)'
CFLAGS += -DDFU_APP_DATA_RESERVED=7*4096