From 3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 23 Aug 2018 17:08:59 +0200 Subject: o Initial import. --- .../bootloader/serial_dfu/nrf_dfu_serial.c | 265 +++++++++++++++ .../bootloader/serial_dfu/nrf_dfu_serial.h | 109 ++++++ .../bootloader/serial_dfu/nrf_dfu_serial_uart.c | 235 +++++++++++++ .../bootloader/serial_dfu/nrf_dfu_serial_usb.c | 370 +++++++++++++++++++++ 4 files changed, 979 insertions(+) create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.c create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.h create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_uart.c create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_usb.c (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu') diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.c new file mode 100644 index 0000000..8e14b05 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.c @@ -0,0 +1,265 @@ +/** + * Copyright (c) 2017 - 2018, 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 "nrf_dfu_serial.h" +#include "nrf_dfu_req_handler.h" +#include "nrf_dfu_handling_error.h" + +#define NRF_LOG_MODULE_NAME nrf_dfu_serial +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +#define NRF_SERIAL_OPCODE_SIZE (sizeof(uint8_t)) + + + +static uint32_t response_ext_err_payload_add(uint8_t * p_buffer, uint32_t buf_offset) +{ + p_buffer[buf_offset] = ext_error_get(); + (void) ext_error_set(NRF_DFU_EXT_ERROR_NO_ERROR); + + return 1; +} + + +static void response_send(nrf_dfu_serial_t * p_transport, + nrf_dfu_response_t const * p_response) +{ + uint8_t index = 0; + uint8_t * p_serialized_rsp = p_transport->p_rsp_buf; + + NRF_LOG_DEBUG("Sending Response: [0x%01x, 0x%01x]", p_response->request, p_response->result); + + p_serialized_rsp[index++] = NRF_DFU_OP_RESPONSE; + p_serialized_rsp[index++] = p_response->request; + p_serialized_rsp[index++] = (uint8_t)(p_response->result); + + if (p_response->result == NRF_DFU_RES_CODE_SUCCESS) + { + switch (p_response->request) + { + case NRF_DFU_OP_PROTOCOL_VERSION: + { + p_serialized_rsp[index] = p_response->protocol.version; + index += sizeof(uint8_t); + } break; + + case NRF_DFU_OP_HARDWARE_VERSION: + { + index += uint32_encode(p_response->hardware.part, &p_serialized_rsp[index]); + index += uint32_encode(p_response->hardware.variant, &p_serialized_rsp[index]); + index += uint32_encode(p_response->hardware.memory.rom_size, &p_serialized_rsp[index]); + index += uint32_encode(p_response->hardware.memory.ram_size, &p_serialized_rsp[index]); + index += uint32_encode(p_response->hardware.memory.rom_page_size, &p_serialized_rsp[index]); + } break; + + case NRF_DFU_OP_FIRMWARE_VERSION: + { + p_serialized_rsp[index++] = p_response->firmware.type; + index += uint32_encode(p_response->firmware.version, &p_serialized_rsp[index]); + index += uint32_encode(p_response->firmware.addr, &p_serialized_rsp[index]); + index += uint32_encode(p_response->firmware.len, &p_serialized_rsp[index]); + } break; + + case NRF_DFU_OP_CRC_GET: + index += uint32_encode(p_response->crc.offset, &p_serialized_rsp[index]); + index += uint32_encode(p_response->crc.crc, &p_serialized_rsp[index]); + break; + + case NRF_DFU_OP_OBJECT_SELECT: + index += uint32_encode(p_response->select.max_size, &p_serialized_rsp[index]); + index += uint32_encode(p_response->select.offset, &p_serialized_rsp[index]); + index += uint32_encode(p_response->select.crc, &p_serialized_rsp[index]); + break; + + case NRF_DFU_OP_MTU_GET: + index += uint16_encode(p_response->mtu.size, &p_serialized_rsp[index]); + break; + + case NRF_DFU_OP_PING: + p_serialized_rsp[index] = p_response->ping.id; + index += sizeof(uint8_t); + break; + + default: + // no implementation + break; + } + } + else if (p_response->result == NRF_DFU_RES_CODE_EXT_ERROR) + { + index += response_ext_err_payload_add(p_serialized_rsp, index); + } + + if (index > NRF_SERIAL_MAX_RESPONSE_SIZE) + { + NRF_LOG_ERROR("Message is larger than expected."); + } + + // Send response. + if (p_transport->rsp_func((uint8_t const *)(p_serialized_rsp), index) != NRF_SUCCESS) + { + NRF_LOG_ERROR("Failed to send data over serial interface!"); + } +} + + +void dfu_req_handler_rsp_clbk(nrf_dfu_response_t * p_res, void * p_context) +{ + nrf_dfu_serial_t * p_transport = (nrf_dfu_serial_t *)(p_context); + + if (p_res->result != NRF_DFU_RES_CODE_SUCCESS) + { + NRF_LOG_WARNING("DFU request completed with result: 0x%x", p_res->result); + } + + switch (p_res->request) + { + default: + /* Reply normally. + * Make sure to reply to NRF_DFU_OP_OBJECT_CREATE when running DFU over serial, + * otherwise the transfer might run very slow, without an apparent reason. + */ + break; + + case NRF_DFU_OP_OBJECT_WRITE: + { + p_transport->pkt_notif_target_count--; + + if ( (p_transport->pkt_notif_target == 0) + || (p_transport->pkt_notif_target_count != 0)) + { + /* Do not reply to _OBJECT_WRITE messages. */ + return; + } + + /* Reply with a CRC message and reset the packet counter. */ + p_transport->pkt_notif_target_count = p_transport->pkt_notif_target; + + p_res->request = NRF_DFU_OP_CRC_GET; + p_res->crc.offset = p_res->write.offset; + p_res->crc.crc = p_res->write.crc; + } break; + } + + response_send(p_transport, p_res); +} + + +void nrf_dfu_serial_on_packet_received(nrf_dfu_serial_t * p_transport, + uint8_t const * p_data, + uint32_t length) +{ + uint8_t const * p_payload = &p_data[NRF_SERIAL_OPCODE_SIZE]; + uint16_t const payload_len = (length - NRF_SERIAL_OPCODE_SIZE); + + nrf_dfu_request_t request = + { + .request = (nrf_dfu_op_t)(p_data[0]), + .callback.response = dfu_req_handler_rsp_clbk, + .p_context = p_transport + }; + + bool buf_free = true; + + switch (request.request) + { + case NRF_DFU_OP_FIRMWARE_VERSION: + { + request.firmware.image_number = p_payload[0]; + } break; + + case NRF_DFU_OP_RECEIPT_NOTIF_SET: + { + NRF_LOG_DEBUG("Set receipt notif target: %d", p_transport->pkt_notif_target); + + p_transport->pkt_notif_target = uint16_decode(&p_payload[0]); + p_transport->pkt_notif_target_count = p_transport->pkt_notif_target; + } break; + + case NRF_DFU_OP_OBJECT_SELECT: + { + request.select.object_type = p_payload[0]; + } break; + + case NRF_DFU_OP_OBJECT_CREATE: + { + // Reset the packet receipt notification on create object + p_transport->pkt_notif_target_count = p_transport->pkt_notif_target; + + request.create.object_type = p_payload[0]; + request.create.object_size = uint32_decode(&p_payload[1]); + } break; + + case NRF_DFU_OP_OBJECT_WRITE: + { + // Buffer will be freed asynchronously + buf_free = false; + + request.write.p_data = p_payload; + request.write.len = payload_len; + request.callback.write = p_transport->payload_free_func; + } break; + + case NRF_DFU_OP_MTU_GET: + { + NRF_LOG_DEBUG("Received serial mtu"); + request.mtu.size = p_transport->mtu; + } break; + + case NRF_DFU_OP_PING: + { + NRF_LOG_DEBUG("Received ping %d", p_payload[0]); + request.ping.id = p_payload[0]; + } break; + + default: + /* Do nothing. */ + break; + } + + if (buf_free) + { + p_transport->payload_free_func((void *)(p_payload)); + } + + ret_code_t ret_code = nrf_dfu_req_handler_on_req(&request); + ASSERT(ret_code == NRF_SUCCESS); +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.h new file mode 100644 index 0000000..c14ee40 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.h @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2017 - 2018, 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. + * + */ + +#ifndef NRF_DFU_SERIAL_H__ +#define NRF_DFU_SERIAL_H__ + +#include +#include "sdk_errors.h" +#include "nrf_dfu_req_handler.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/**@file + * + * @defgroup nrf_dfu_serial DFU Serial transports shared part + * @{ + * @ingroup nrf_dfu + * @brief Shared part of Device Firmware Update (DFU) transport layers using serial interface (UART, USB CDC ACM). + * + * @defgroup nrf_dfu_serial_uart DFU Serial UART transport + * @ingroup nrf_dfu_serial + * @brief Configuration for Device Firmware Update (DFU) transport layer using UART. + * + * @defgroup nrf_dfu_serial_usb DFU Serial USB CDC ACM transport + * @ingroup nrf_dfu_serial + * @brief Configuration for Device Firmware Update (DFU) transport layer using USB CDC ACM. + * + */ + +#define NRF_SERIAL_MAX_RESPONSE_SIZE (sizeof(nrf_dfu_response_t)) + +/** + * Prototype for function for sending response over serial DFU transport. + */ +typedef ret_code_t (*nrf_serial_rsp_func_t)(uint8_t const * p_data, uint32_t length); + +/** + * Prototype for function for freeing RX buffer. + * + * Function is called when input data is processed. + */ +typedef void (*nrf_serial_rx_buf_free_func_t)(void * p_buf); + + +/**@brief DFU serial transport layer state. + * + * @details This structure contains status information related to the serial transport layer type. + */ +typedef struct +{ + uint16_t pkt_notif_target; + uint16_t pkt_notif_target_count; + nrf_serial_rsp_func_t rsp_func; + nrf_serial_rx_buf_free_func_t payload_free_func; + uint32_t mtu; + uint8_t * p_rsp_buf; +} nrf_dfu_serial_t; + +void nrf_dfu_serial_on_packet_received(nrf_dfu_serial_t * p_transport, + uint8_t const * p_data, + uint32_t length); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif // NRF_DFU_SERIAL_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_uart.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_uart.c new file mode 100644 index 0000000..3d0b783 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_uart.c @@ -0,0 +1,235 @@ +/** + * Copyright (c) 2016 - 2018, 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 "nrf_dfu_serial.h" + +#include +#include "boards.h" +#include "app_util_platform.h" +#include "nrf_dfu_transport.h" +#include "nrf_dfu_req_handler.h" +#include "slip.h" +#include "nrf_balloc.h" +#include "nrf_drv_uart.h" + +#define NRF_LOG_MODULE_NAME nrf_dfu_serial_uart +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +/**@file + * + * @defgroup nrf_dfu_serial_uart DFU Serial UART transport + * @ingroup nrf_dfu + * @brief Device Firmware Update (DFU) transport layer using UART. + */ + +#define NRF_SERIAL_OPCODE_SIZE (sizeof(uint8_t)) +#define NRF_UART_MAX_RESPONSE_SIZE_SLIP (2 * NRF_SERIAL_MAX_RESPONSE_SIZE + 1) +#define RX_BUF_SIZE (64) //to get 64bytes payload +#define OPCODE_OFFSET (sizeof(uint32_t) - NRF_SERIAL_OPCODE_SIZE) +#define DATA_OFFSET (OPCODE_OFFSET + NRF_SERIAL_OPCODE_SIZE) +#define UART_SLIP_MTU (2 * (RX_BUF_SIZE + 1) + 1) +#define BALLOC_BUF_SIZE ((CEIL_DIV((RX_BUF_SIZE+OPCODE_SIZE),sizeof(uint32_t))*sizeof(uint32_t))) + +NRF_BALLOC_DEF(m_payload_pool, (UART_SLIP_MTU + 1), NRF_DFU_SERIAL_UART_RX_BUFFERS); + +static nrf_drv_uart_t m_uart = NRF_DRV_UART_INSTANCE(0); +static uint8_t m_rx_byte; + +static nrf_dfu_serial_t m_serial; +static slip_t m_slip; +static uint8_t m_rsp_buf[NRF_UART_MAX_RESPONSE_SIZE_SLIP]; +static bool m_active; + +static nrf_dfu_observer_t m_observer; + +static uint32_t uart_dfu_transport_init(nrf_dfu_observer_t observer); +static uint32_t uart_dfu_transport_close(nrf_dfu_transport_t const * p_exception); + +DFU_TRANSPORT_REGISTER(nrf_dfu_transport_t const uart_dfu_transport) = +{ + .init_func = uart_dfu_transport_init, + .close_func = uart_dfu_transport_close, +}; + +static void payload_free(void * p_buf) +{ + uint8_t * p_buf_root = (uint8_t *)p_buf - DATA_OFFSET; //pointer is shifted to point to data + nrf_balloc_free(&m_payload_pool, p_buf_root); +} + +static ret_code_t rsp_send(uint8_t const * p_data, uint32_t length) +{ + uint32_t slip_len; + (void) slip_encode(m_rsp_buf, (uint8_t *)p_data, length, &slip_len); + + return nrf_drv_uart_tx(&m_uart, m_rsp_buf, slip_len); +} + +static __INLINE void on_rx_complete(nrf_dfu_serial_t * p_transport, uint8_t * p_data, uint8_t len) +{ + ret_code_t ret_code; + + ret_code = slip_decode_add_byte(&m_slip, p_data[0]); + (void) nrf_drv_uart_rx(&m_uart, &m_rx_byte, 1); + + if (ret_code == NRF_SUCCESS) + { + // Activity detected on current transport, close all except active one. + UNUSED_RETURN_VALUE(nrf_dfu_transports_close(&uart_dfu_transport)); + + nrf_dfu_serial_on_packet_received(p_transport, + (uint8_t const *)m_slip.p_buffer, + m_slip.current_index); + + uint8_t * p_rx_buf = nrf_balloc_alloc(&m_payload_pool); + if (p_rx_buf == NULL) + { + NRF_LOG_ERROR("Failed to allocate buffer"); + return; + } + NRF_LOG_INFO("Allocated buffer %x", p_rx_buf); + // reset the slip decoding + m_slip.p_buffer = &p_rx_buf[OPCODE_OFFSET]; + m_slip.current_index = 0; + m_slip.state = SLIP_STATE_DECODING; + } + +} + +static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context) +{ + switch (p_event->type) + { + case NRF_DRV_UART_EVT_RX_DONE: + on_rx_complete((nrf_dfu_serial_t*)p_context, + p_event->data.rxtx.p_data, + p_event->data.rxtx.bytes); + break; + + case NRF_DRV_UART_EVT_ERROR: + APP_ERROR_HANDLER(p_event->data.error.error_mask); + break; + + default: + // No action. + break; + } +} + +static uint32_t uart_dfu_transport_init(nrf_dfu_observer_t observer) +{ + uint32_t err_code = NRF_SUCCESS; + + if (m_active) + { + return err_code; + } + + NRF_LOG_DEBUG("serial_dfu_transport_init()"); + + m_observer = observer; + + err_code = nrf_balloc_init(&m_payload_pool); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + uint8_t * p_rx_buf = nrf_balloc_alloc(&m_payload_pool); + + m_slip.p_buffer = &p_rx_buf[OPCODE_OFFSET]; + m_slip.current_index = 0; + m_slip.buffer_len = UART_SLIP_MTU; + m_slip.state = SLIP_STATE_DECODING; + + m_serial.rsp_func = rsp_send; + m_serial.payload_free_func = payload_free; + m_serial.mtu = UART_SLIP_MTU; + m_serial.p_rsp_buf = &m_rsp_buf[NRF_UART_MAX_RESPONSE_SIZE_SLIP - + NRF_SERIAL_MAX_RESPONSE_SIZE]; + + nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG; + + uart_config.pseltxd = TX_PIN_NUMBER; + uart_config.pselrxd = RX_PIN_NUMBER; + uart_config.pselcts = CTS_PIN_NUMBER; + uart_config.pselrts = RTS_PIN_NUMBER; + uart_config.hwfc = NRF_DFU_SERIAL_UART_USES_HWFC ? + NRF_UART_HWFC_ENABLED : NRF_UART_HWFC_DISABLED; + uart_config.p_context = &m_serial; + + err_code = nrf_drv_uart_init(&m_uart, &uart_config, uart_event_handler); + if (err_code != NRF_SUCCESS) + { + NRF_LOG_ERROR("Failed initializing uart"); + return err_code; + } + + err_code = nrf_drv_uart_rx(&m_uart, &m_rx_byte, 1); + if (err_code != NRF_SUCCESS) + { + NRF_LOG_ERROR("Failed initializing rx"); + } + + NRF_LOG_DEBUG("serial_dfu_transport_init() completed"); + + m_active = true; + + if (m_observer) + { + m_observer(NRF_DFU_EVT_TRANSPORT_ACTIVATED); + } + + return err_code; +} + + +static uint32_t uart_dfu_transport_close(nrf_dfu_transport_t const * p_exception) +{ + if ((m_active == true) && (p_exception != &uart_dfu_transport)) + { + nrf_drv_uart_uninit(&m_uart); + m_active = false; + } + + return NRF_SUCCESS; +} + diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_usb.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_usb.c new file mode 100644 index 0000000..2b45643 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_usb.c @@ -0,0 +1,370 @@ +/** + * Copyright (c) 2016 - 2018, 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 +#include "nrf_dfu_req_handler.h" +#include "nrf_dfu_transport.h" +#include "slip.h" +#include "nrf_balloc.h" +#include "nrf_drv_power.h" +#include "nrf_drv_clock.h" +#include "nrf_drv_usbd.h" +#include "nrf_dfu_serial.h" +#include "app_scheduler.h" +#include "app_usbd.h" +#include "app_usbd_cdc_acm.h" +#include "app_usbd_core.h" +#include "app_usbd_string_desc.h" +#include "app_util_platform.h" +#include "app_usbd_serial_num.h" + +#define NRF_LOG_MODULE_NAME nrf_dfu_serial_usb +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +/**@file + * + * @defgroup nrf_dfu_serial_usb DFU Serial USB CDC ACM transport + * @ingroup nrf_dfu + * @brief Device Firmware Update (DFU) transport layer using USB CDC ACM. + */ + +#define NRF_SERIAL_OPCODE_SIZE (sizeof(uint8_t)) + +#define NRF_USB_MAX_RESPONSE_SIZE_SLIP (2 * NRF_SERIAL_MAX_RESPONSE_SIZE + 1) + +#define RX_BUF_SIZE (1024) +#define SLIP_MTU (2 * (RX_BUF_SIZE + 1) + 1) +#define OPCODE_OFFSET (sizeof(uint32_t) - NRF_SERIAL_OPCODE_SIZE) +#define DATA_OFFSET (OPCODE_OFFSET + NRF_SERIAL_OPCODE_SIZE) + +#define CDC_ACM_COMM_INTERFACE 0 +#define CDC_ACM_COMM_EPIN NRF_DRV_USBD_EPIN2 +#define CDC_ACM_DATA_INTERFACE 1 +#define CDC_ACM_DATA_EPIN NRF_DRV_USBD_EPIN1 +#define CDC_ACM_DATA_EPOUT NRF_DRV_USBD_EPOUT1 + +/** + * @brief Enable power USB detection + * + * Configure if example supports USB port connection + */ +#ifndef USBD_POWER_DETECTION +#define USBD_POWER_DETECTION true +#endif + +/** + * @brief Interfaces list passed to @ref APP_USBD_CDC_ACM_GLOBAL_DEF + * */ +#define CDC_ACM_INTERFACES_CONFIG() \ + APP_USBD_CDC_ACM_CONFIG(CDC_ACM_COMM_INTERFACE, \ + CDC_ACM_COMM_EPIN, \ + CDC_ACM_DATA_INTERFACE, \ + CDC_ACM_DATA_EPIN, \ + CDC_ACM_DATA_EPOUT) + +/*lint -save -e26 -e64 -e505 -e651 */ +static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_cdc_acm_user_event_t event); + +/**@brief CDC_ACM class instance. */ +APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm, + cdc_acm_user_ev_handler, + CDC_ACM_COMM_INTERFACE, + CDC_ACM_DATA_INTERFACE, + CDC_ACM_COMM_EPIN, + CDC_ACM_DATA_EPIN, + CDC_ACM_DATA_EPOUT, + APP_USBD_CDC_COMM_PROTOCOL_NONE); +/*lint -restore */ + +NRF_BALLOC_DEF(m_payload_pool, (SLIP_MTU+1), NRF_DFU_SERIAL_USB_RX_BUFFERS); + +static nrf_dfu_serial_t m_serial; +static slip_t m_slip; +static uint8_t m_rsp_buf[NRF_USB_MAX_RESPONSE_SIZE_SLIP]; +static uint8_t m_rx_buf[NRF_DRV_USBD_EPSIZE]; + +static nrf_dfu_observer_t m_observer; + +static uint32_t usb_dfu_transport_init(nrf_dfu_observer_t observer); +static uint32_t usb_dfu_transport_close(nrf_dfu_transport_t const * p_exception); + + +DFU_TRANSPORT_REGISTER(nrf_dfu_transport_t const usb_dfu_transport) = +{ + .init_func = usb_dfu_transport_init, + .close_func = usb_dfu_transport_close, +}; + + +static void payload_free(void * p_buf) +{ + uint8_t * p_buf_root = ((uint8_t *)(p_buf)) - DATA_OFFSET; //pointer is shifted to point to data + nrf_balloc_free(&m_payload_pool, p_buf_root); +} + + +static ret_code_t rsp_send(uint8_t const * p_data, uint32_t length) +{ + ASSERT(p_data); + ASSERT(length != 0); + + uint32_t slip_len; + + // Cannot fail if inputs are non-NULL. + (void) slip_encode(m_rsp_buf, (uint8_t *)(p_data), length, &slip_len); + + return app_usbd_cdc_acm_write(&m_app_cdc_acm, m_rsp_buf, slip_len); +} + + +static void on_rx_complete(nrf_dfu_serial_t * p_transport, uint8_t * p_data, uint8_t len) +{ + ret_code_t ret_code; + + for (uint32_t i = 0; i < len; i++) + { + ret_code = slip_decode_add_byte(&m_slip, p_data[i]); + if (ret_code != NRF_SUCCESS) + { + continue; + } + + // Activity detected on current transport, close all except active one. + (void) nrf_dfu_transports_close(&usb_dfu_transport); + + nrf_dfu_serial_on_packet_received(p_transport, + (uint8_t const *)(m_slip.p_buffer), + m_slip.current_index); + + uint8_t * p_rx_buf = nrf_balloc_alloc(&m_payload_pool); + if (p_rx_buf == NULL) + { + NRF_LOG_ERROR("Failed to allocate buffer!"); + return; + } + + NRF_LOG_DEBUG("Allocated buffer %x", p_rx_buf); + // reset the slip decoding + m_slip.p_buffer = &p_rx_buf[OPCODE_OFFSET]; + m_slip.current_index = 0; + m_slip.state = SLIP_STATE_DECODING; + } +} + +/** + * @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t (headphones) + * */ +static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_cdc_acm_user_event_t event) +{ + ret_code_t ret_code; + + switch (event) + { + case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN: + { + ret_code = app_usbd_cdc_acm_read(&m_app_cdc_acm, m_rx_buf, 1); + NRF_LOG_WARNING("Could not read from CDC. Error: 0x%x.", ret_code); + } break; + + case APP_USBD_CDC_ACM_USER_EVT_RX_DONE: + { + do + { + on_rx_complete(&m_serial, m_rx_buf, 1); + ret_code = app_usbd_cdc_acm_read(&m_app_cdc_acm, m_rx_buf, 1); + } while (ret_code == NRF_SUCCESS); + } break; + + default: + break; + } +} + + +static void usbd_dfu_transport_ev_handler(app_usbd_event_type_t event) +{ + switch (event) + { + case APP_USBD_EVT_STOPPED: + app_usbd_disable(); + break; + + case APP_USBD_EVT_POWER_DETECTED: + NRF_LOG_INFO("USB power detected"); + if (!nrf_drv_usbd_is_enabled()) + { + app_usbd_enable(); + } + + if (m_observer) + { + m_observer(NRF_DFU_EVT_TRANSPORT_ACTIVATED); + } + break; + + case APP_USBD_EVT_POWER_REMOVED: + NRF_LOG_INFO("USB power removed"); + app_usbd_stop(); + if (m_observer) + { + m_observer(NRF_DFU_EVT_TRANSPORT_DEACTIVATED); + } + break; + + case APP_USBD_EVT_POWER_READY: + NRF_LOG_INFO("USB ready"); + app_usbd_start(); + break; + + default: + break; + } +} + + +static void usbd_sched_event_handler(void * p_event_data, uint16_t event_size) +{ + app_usbd_event_execute(p_event_data); +} + + +static void usbd_event_handler(app_usbd_internal_evt_t const * const p_event) +{ + ret_code_t ret_code; + if (p_event->type == APP_USBD_EVT_DRV_SOF) + { + app_usbd_event_execute(p_event); + } + else + { + ret_code = app_sched_event_put(p_event, + sizeof(app_usbd_internal_evt_t), + usbd_sched_event_handler); + + if (ret_code != NRF_SUCCESS) + { + NRF_LOG_ERROR("Could not schedule USB event!"); + } + } +} + + +static uint32_t usb_dfu_transport_init(nrf_dfu_observer_t observer) +{ + uint32_t err_code; + + /* Execute event directly in interrupt handler */ + static const app_usbd_config_t usbd_config = + { + .ev_handler = usbd_event_handler, + .ev_state_proc = usbd_dfu_transport_ev_handler + }; + + (void) nrf_balloc_init(&m_payload_pool); //Result is checked by checking result of _alloc(). + + m_observer = observer; + + uint8_t * p_rx_buf = nrf_balloc_alloc(&m_payload_pool); + if (p_rx_buf == NULL) + { + NRF_LOG_ERROR("Could not allocate payload pool."); + return NRF_ERROR_INTERNAL; + } + + m_slip.p_buffer = &p_rx_buf[OPCODE_OFFSET]; + m_slip.current_index = 0; + m_slip.buffer_len = SLIP_MTU; + m_slip.state = SLIP_STATE_DECODING; + + m_serial.rsp_func = rsp_send; + m_serial.payload_free_func = payload_free; + m_serial.mtu = SLIP_MTU; + m_serial.p_rsp_buf = &m_rsp_buf[NRF_USB_MAX_RESPONSE_SIZE_SLIP - + NRF_SERIAL_MAX_RESPONSE_SIZE]; + + + NRF_LOG_DEBUG("Initializing drivers."); + + err_code = nrf_drv_clock_init(); + if (err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED) + { + VERIFY_SUCCESS(err_code); + } + + err_code = nrf_drv_power_init(NULL); + VERIFY_SUCCESS(err_code); + + app_usbd_serial_num_generate(); + + err_code = app_usbd_init(&usbd_config); + VERIFY_SUCCESS(err_code); + + app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm); + err_code = app_usbd_class_append(class_cdc_acm); + VERIFY_SUCCESS(err_code); + + NRF_LOG_DEBUG("Starting USB"); + + if (USBD_POWER_DETECTION) + { + err_code = app_usbd_power_events_enable(); + VERIFY_SUCCESS(err_code); + } + else + { + NRF_LOG_DEBUG("No USB power detection enabled, starting USB now"); + + app_usbd_enable(); + app_usbd_start(); + } + + NRF_LOG_DEBUG("USB Transport initialized"); + + return err_code; +} + + +static uint32_t usb_dfu_transport_close(nrf_dfu_transport_t const * p_exception) +{ + return NRF_SUCCESS; +} -- cgit v1.2.3