Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nRF52840: MCU to MCU updates via activation pin and CircuitPython filesystem programming #283

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
33 changes: 24 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,35 @@ ifeq ($(MCU_SUB_VARIANT),nrf52)
DFU_DEV_REV = 0xADAF
CFLAGS += -DNRF52 -DNRF52832_XXAA -DS132
DFU_APP_DATA_RESERVED=7*4096
BOARD_USE_USB = 0
BOARD_USE_UART = 1
else ifeq ($(MCU_SUB_VARIANT),nrf52833)
SD_NAME = s140
DFU_DEV_REV = 52833
CFLAGS += -DNRF52833_XXAA -DS140
DFU_APP_DATA_RESERVED=7*4096
BOARD_USE_USB = 1
else ifeq ($(MCU_SUB_VARIANT),nrf52840)
SD_NAME = s140
DFU_DEV_REV = 52840
CFLAGS += -DNRF52840_XXAA -DS140
DFU_APP_DATA_RESERVED=10*4096
BOARD_USE_USB = 1
else
$(error Sub Variant $(MCU_SUB_VARIANT) is unknown)
endif

DFU_EXTERNAL_FLASH ?= 0
CFLAGS += -DDFU_EXTERNAL_FLASH=$(DFU_EXTERNAL_FLASH)

DFU_PIN_ACTIVATION ?= 0
ifeq ($(DFU_PIN_ACTIVATION),1)
BOARD_USE_UART = 1
endif

BOARD_USE_UART ?= 0
CFLAGS += -DBOARD_USE_UART=$(BOARD_USE_UART)

#------------------------------------------------------------------------------
# SOURCE FILES
#------------------------------------------------------------------------------
Expand All @@ -145,6 +160,11 @@ C_SRC += src/boards/boards.c
# nrfx
C_SRC += $(NRFX_PATH)/drivers/src/nrfx_power.c
C_SRC += $(NRFX_PATH)/drivers/src/nrfx_nvmc.c

ifeq ($(DFU_EXTERNAL_FLASH),1)
C_SRC += $(NRFX_PATH)/drivers/src/nrfx_qspi.c
endif

C_SRC += $(NRFX_PATH)/mdk/system_$(MCU_SUB_VARIANT).c

# SDK 11 files: serial + OTA DFU
Expand All @@ -169,20 +189,18 @@ C_SRC += $(SDK_PATH)/libraries/hci/hci_slip.c
C_SRC += $(SDK_PATH)/libraries/hci/hci_transport.c
C_SRC += $(SDK_PATH)/libraries/util/nrf_assert.c

# UART or USB Serial
ifeq ($(MCU_SUB_VARIANT),nrf52)

ifeq ($(BOARD_USE_UART),1)
C_SRC += $(SDK_PATH)/libraries/uart/app_uart.c
C_SRC += $(SDK_PATH)/drivers_nrf/uart/nrf_drv_uart.c
C_SRC += $(SDK_PATH)/drivers_nrf/common/nrf_drv_common.c

IPATH += $(SDK11_PATH)/libraries/util
IPATH += $(SDK_PATH)/drivers_nrf/common
IPATH += $(SDK_PATH)/drivers_nrf/uart
endif

else

# pinconfig is required for 840 for CF2
ifeq ($(BOARD_USE_USB),1)
# pinconfig is required for CF2
C_SRC += src/boards/$(BOARD)/pinconfig.c

# USB Application ( MSC + UF2 )
Expand All @@ -201,12 +219,9 @@ C_SRC += \
$(TUSB_PATH)/class/cdc/cdc_device.c \
$(TUSB_PATH)/class/msc/msc_device.c \
$(TUSB_PATH)/tusb.c

endif

#------------------------------------------------------------------------------
# Assembly Files
#------------------------------------------------------------------------------
ASM_SRC = $(NRFX_PATH)/mdk/gcc_startup_$(MCU_SUB_VARIANT).S

#------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions lib/sdk/components/libraries/hci/hci_mem_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(HCI_MEM_POOL)
#include "app_util_platform.h"
#include "hci_mem_pool.h"
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -134,6 +135,7 @@ uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
}
*pp_buffer = NULL;

NRFX_CRITICAL_SECTION_ENTER();
if (m_rx_buffer_queue.free_window_count != 0)
{
if (length <= HCI_RX_BUF_SIZE)
Expand Down Expand Up @@ -163,6 +165,7 @@ uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
{
err_code = NRF_ERROR_NO_MEM;
}
NRFX_CRITICAL_SECTION_EXIT();

return err_code;
}
Expand Down
64 changes: 53 additions & 11 deletions lib/sdk/components/libraries/hci/hci_slip.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,23 @@
#include <stdlib.h>
#include "app_uart.h"
#include "nrf_error.h"
#include "board.h"

#if USE_RUNTIME_SELECTION
#pragma message "USB and UART enabled"
static bool selectSerial = false; // use USB by default

void useSerialTransport() {
selectSerial = true;
}

bool usingSerialTransport() {
return selectSerial;
}
#endif

// nRF has native usb peripheral
#ifdef NRF_USBD
#ifdef USE_USB
#include "tusb.h"
#endif

Expand Down Expand Up @@ -122,17 +136,24 @@ static uint32_t send_tx_byte_end(void);
*/
uint32_t (*send_tx_byte) (void) = send_tx_byte_default;

#ifdef NRF_USBD

static uint32_t serial_put(char ch)
#if USE_USB && !USE_RUNTIME_SELECTION
uint32_t serial_put(char ch)
{
return tud_cdc_write_char(ch) ? NRF_SUCCESS : NRF_ERROR_NO_MEM;
}

#else
#elif USE_SERIAL && !USE_RUNTIME_SELECTION

#define serial_put app_uart_put

#else

uint32_t serial_put(char ch)
{
return selectSerial ? app_uart_put(ch) : (tud_cdc_write_char(ch) ? NRF_SUCCESS : NRF_ERROR_NO_MEM);
}

#endif

static uint32_t send_tx_byte_end(void)
Expand Down Expand Up @@ -350,9 +371,8 @@ static bool rx_buffer_overflowed(void)
return false;
}

#ifdef NRF_USBD

static uint32_t slip_uart_open(void)
#if USE_USB
static uint32_t slip_uart_open_usb_cdc(void)
{
m_current_state = SLIP_READY;
return NRF_SUCCESS;
Expand All @@ -367,7 +387,9 @@ void tud_cdc_rx_cb(uint8_t port)
}
}

#else
#endif

#if USE_SERIAL

/** @brief Function for handling the UART module event. It parses events from the UART when
* bytes are received/transmitted.
Expand All @@ -390,7 +412,7 @@ static void slip_uart_eventhandler(app_uart_evt_t * uart_event)

/** @brief Function for enabling the UART module when the SLIP layer is opened.
*/
static uint32_t slip_uart_open(void)
static uint32_t slip_uart_open_serial(void)
{
uint32_t err_code;

Expand Down Expand Up @@ -419,6 +441,27 @@ static uint32_t slip_uart_open(void)

#endif

#if !USE_SERIAL && !USE_USB
#error at least one of USE_SERIAL and USE_USB must be enabled
#endif

static uint32_t slip_uart_open(void) {
#if USE_RUNTIME_SELECTION
if (selectSerial) {
return slip_uart_open_serial();
}
else {
return slip_uart_open_usb_cdc();
}
#elif USE_SERIAL
return slip_uart_open_serial();
#else
return slip_uart_open_usb_cdc();
#endif

}


uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler)
{
m_slip_event_handler = event_handler;
Expand Down Expand Up @@ -447,7 +490,7 @@ uint32_t hci_slip_close()
{
m_current_state = SLIP_OFF;

#ifdef NRF_USBD
#if USE_USB
return NRF_SUCCESS;
#else
uint32_t err_code = app_uart_close();
Expand All @@ -456,7 +499,6 @@ uint32_t hci_slip_close()

}


uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length)
{
if (p_buffer == NULL)
Expand Down
20 changes: 20 additions & 0 deletions lib/sdk/components/libraries/hci/hci_slip.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,31 @@
#define HCI_SLIP_H__

#include <stdint.h>
#include "boards.h"

// NRF_USBD
#ifdef NRF_USBD
#define USE_USB 1
#else
#define USE_USB 0
#endif

/**
* @brief Enable UART interface with DFU_ACTIVATION present for MCU to MCU transfers
*/
#define USE_SERIAL (PIN_DFU_ACTIVATE_PRESENT || BOARD_USE_UART)
#define USE_RUNTIME_SELECTION (USE_USB && USE_SERIAL)


#ifdef __cplusplus
extern "C" {
#endif

#if USE_RUNTIME_SELECTION
void useSerialTransport(void);
bool usingSerialTransport(void);
#endif

/**@brief Event types from the SLIP Layer. */
typedef enum
{
Expand Down
11 changes: 9 additions & 2 deletions lib/sdk/components/libraries/hci/hci_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(HCI_TRANSPORT)
#include "app_util_platform.h"
#include "hci_transport.h"
#include "hci_slip.h"
#include "crc16.h"
Expand Down Expand Up @@ -286,6 +287,7 @@ static void rx_vendor_specific_pkt_type_handle(const uint8_t * p_buffer, uint32_
err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer);
APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM));


// If memory pool RX buffer produce succeeded we register that buffer to slip layer
// otherwise we register the internal acknowledgement buffer.
err_code = hci_slip_rx_buffer_register(
Expand Down Expand Up @@ -781,7 +783,9 @@ uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length)
if (m_is_slip_decode_ready)
{
m_is_slip_decode_ready = false;
CRITICAL_REGION_ENTER();
err_code = hci_mem_pool_rx_extract(pp_buffer, &length);
CRITICAL_REGION_EXIT();
length -= (PKT_HDR_SIZE + PKT_CRC_SIZE);

*p_length = (uint16_t)length;
Expand All @@ -800,9 +804,12 @@ uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length)
return err_code;
}


uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer)
{
return (hci_mem_pool_rx_consume(p_buffer - PKT_HDR_SIZE));
uint32_t r;
CRITICAL_REGION_ENTER();
r = hci_mem_pool_rx_consume(p_buffer - PKT_HDR_SIZE);
CRITICAL_REGION_EXIT();
return r;
}
#endif //NRF_MODULE_ENABLED(HCI_TRANSPORT)
1 change: 0 additions & 1 deletion lib/sdk11/components/libraries/bootloader_dfu/bootloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms, bool cancel_timeout
* The SoftDevice vector table base for interrupt forwarding will be set the application
* address.
*
* @param[in] app_addr Address to the region where the application is stored.
*/
void bootloader_app_start(void);

Expand Down
12 changes: 12 additions & 0 deletions lib/sdk11/components/libraries/bootloader_dfu/dfu.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ uint32_t dfu_sd_image_swap(void);
*/
uint32_t dfu_init_pkt_complete(void);


#if DFU_EXTERNAL_FLASH
uint32_t dfu_external_begin_pkt_handle(dfu_update_packet_t* p_packet);

uint32_t dfu_erase_pkt_handle(dfu_erase_packet_t* p_packet);
uint32_t dfu_write_pkt_handle(dfu_write_packet_t* p_packet);
uint32_t dfu_checksum_pkt_handle(dfu_checksum_packet_t* p_packet);

uint32_t dfu_external_end_pkt_handle(dfu_update_packet_t* p_packet);

#endif

#endif // DFU_H__

/** @} */
Loading