133 lines
5.3 KiB
C
133 lines
5.3 KiB
C
/**
|
|
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
|
|
*
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
* are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
* list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form, except as embedded into a Nordic
|
|
* Semiconductor ASA integrated circuit in a product or a software update for
|
|
* such product, must reproduce the above copyright notice, this list of
|
|
* conditions and the following disclaimer in the documentation and/or other
|
|
* materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
|
|
* contributors may be used to endorse or promote products derived from this
|
|
* software without specific prior written permission.
|
|
*
|
|
* 4. This software, with or without modification, must only be used with a
|
|
* Nordic Semiconductor ASA integrated circuit.
|
|
*
|
|
* 5. Any software provided in binary form under this license must not be reverse
|
|
* engineered, decompiled, modified and/or disassembled.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
#include "usbd_dfu_trigger.h"
|
|
#include "app_usbd.h"
|
|
#include "app_usbd_nrf_dfu_trigger.h"
|
|
#include "nrf_drv_clock.h"
|
|
#include "nrf_gpio.h"
|
|
#include "nrf_log_ctrl.h"
|
|
|
|
#include "app_usbd_serial_num.h"
|
|
#include "app_util.h"
|
|
#include "nrf_bootloader_info.h"
|
|
#include "nrf_pwr_mgmt.h"
|
|
#define NRF_LOG_MODULE_NAME nrf_dfu_trigger_usb
|
|
#include "nrf_log.h"
|
|
NRF_LOG_MODULE_REGISTER();
|
|
|
|
#define DFU_FLASH_PAGE_SIZE (NRF_FICR->CODEPAGESIZE)
|
|
#define DFU_FLASH_PAGE_COUNT (NRF_FICR->CODESIZE)
|
|
|
|
#define APP_NAME ELITE_DEVICE_NAME
|
|
#define APP_VERSION_STRING "1.0.0"
|
|
|
|
static uint8_t m_version_string[] = APP_NAME " " APP_VERSION_STRING; ///< Human-readable version string.
|
|
static app_usbd_nrf_dfu_trigger_nordic_info_t m_dfu_info; ///< Struct with various information about the current firmware.
|
|
|
|
static void dfu_trigger_evt_handler(app_usbd_class_inst_t const *p_inst, app_usbd_nrf_dfu_trigger_user_event_t event)
|
|
{
|
|
UNUSED_PARAMETER(p_inst);
|
|
ret_code_t err_code;
|
|
switch (event)
|
|
{
|
|
case APP_USBD_NRF_DFU_TRIGGER_USER_EVT_DETACH:
|
|
NRF_LOG_INFO("DFU Detach request received. Triggering a pin reset.");
|
|
NRF_LOG_FINAL_FLUSH();
|
|
{
|
|
err_code = sd_power_gpregret_clr(0, 0xFFFFFFFF);
|
|
APP_ERROR_CHECK(err_code);
|
|
err_code = sd_power_gpregret_set(0, BOOTLOADER_DFU_START);
|
|
APP_ERROR_CHECK(err_code);
|
|
// Signal that DFU mode is to be enter to the power management module
|
|
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF(m_app_dfu,
|
|
NRF_DFU_TRIGGER_USB_INTERFACE_NUM,
|
|
&m_dfu_info,
|
|
m_version_string,
|
|
dfu_trigger_evt_handler);
|
|
|
|
static void strings_create(void)
|
|
{
|
|
uint8_t prev_char = 'a'; // Arbitrary valid char, not '-'.
|
|
// Remove characters that are not supported in semantic version strings.
|
|
for (size_t i = strlen(APP_NAME) + 1; i < strlen((char *)m_version_string); i++)
|
|
{
|
|
if (((m_version_string[i] >= 'a') && (m_version_string[i] <= 'z')) || ((m_version_string[i] >= 'A') && (m_version_string[i] <= 'Z')) || ((m_version_string[i] >= '0') && (m_version_string[i] <= '9')) || (m_version_string[i] == '+') || (m_version_string[i] == '.') || (m_version_string[i] == '-'))
|
|
{
|
|
// Valid semantic version character.
|
|
}
|
|
else if (prev_char == '-')
|
|
{
|
|
m_version_string[i] = '0';
|
|
}
|
|
else
|
|
{
|
|
m_version_string[i] = '-';
|
|
}
|
|
prev_char = m_version_string[i];
|
|
}
|
|
}
|
|
|
|
ret_code_t nrf_dfu_trigger_usb_init(void)
|
|
{
|
|
|
|
m_dfu_info.wAddress = CODE_START;
|
|
m_dfu_info.wFirmwareSize = CODE_SIZE;
|
|
m_dfu_info.wVersionMajor = 1;
|
|
m_dfu_info.wVersionMinor = 0;
|
|
m_dfu_info.wFirmwareID = 0;
|
|
m_dfu_info.wFlashPageSize = DFU_FLASH_PAGE_SIZE;
|
|
m_dfu_info.wFlashSize = m_dfu_info.wFlashPageSize * DFU_FLASH_PAGE_COUNT;
|
|
|
|
strings_create();
|
|
|
|
app_usbd_class_inst_t const *class_dfu = app_usbd_nrf_dfu_trigger_class_inst_get(&m_app_dfu);
|
|
ret_code_t ret = app_usbd_class_append(class_dfu);
|
|
|
|
return ret;
|
|
}
|