Skip to content

Commit

Permalink
Wireless charge current limits impl. and other optimizes (#37)
Browse files Browse the repository at this point in the history
* wireless charge current limit

* legacy dfu use legacy uart driver

* cmake config improvements

---------

Signed-off-by: Adam BZH <[email protected]>
  • Loading branch information
424778940z authored May 31, 2024
1 parent c1da542 commit 79aab2a
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 17 deletions.
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.22.1)

if(NOT DEFINED ${CMAKE_BUILD_TYPE})
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
if(NOT DEFINED CMAKE_CONFIGURATION_TYPES OR "${CMAKE_BUILD_TYPE}" STREQUAL "")
if(NOT DEFINED CMAKE_BUILD_TYPE OR "${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
endif()
endif()

###############################
Expand Down Expand Up @@ -78,9 +80,10 @@ ExternalProject_Add(
SOURCE_DIR ${PROJECT_SOURCE_DIR}/dfu_legacy
INSTALL_DIR ${PROJECT_SOURCE_DIR}/artifacts
CMAKE_GENERATOR "Ninja"
BUILD_ALWAYS YES
CMAKE_ARGS "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
CMAKE_ARGS "-DTOOL_CHAIN_PREFIX=${TOOL_CHAIN_PREFIX} "
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:STRING=${PROJECT_SOURCE_DIR}/artifacts "
CMAKE_ARGS "-DTOOL_CHAIN_PREFIX=${TOOL_CHAIN_PREFIX}"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:STRING=${PROJECT_SOURCE_DIR}/artifacts"
)

# dfu payload
Expand All @@ -103,9 +106,10 @@ ExternalProject_Add(
SOURCE_DIR ${PROJECT_SOURCE_DIR}/app
INSTALL_DIR ${PROJECT_SOURCE_DIR}/artifacts
CMAKE_GENERATOR "Ninja"
BUILD_ALWAYS YES
CMAKE_ARGS "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
CMAKE_ARGS "-DTOOL_CHAIN_PREFIX=${TOOL_CHAIN_PREFIX} "
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:STRING=${PROJECT_SOURCE_DIR}/artifacts "
CMAKE_ARGS "-DTOOL_CHAIN_PREFIX=${TOOL_CHAIN_PREFIX}"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:STRING=${PROJECT_SOURCE_DIR}/artifacts"
DEPENDS ${CMAKE_PROJECT_NAME}_DFU_UPGRADE_PAYLOAD
)

Expand Down
3 changes: 2 additions & 1 deletion dfu_legacy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ set(
${NRF_SDK_ROOT}/components/libraries/atomic_fifo/nrf_atfifo.c
${NRF_SDK_ROOT}/components/libraries/balloc/nrf_balloc.c
${NRF_SDK_ROOT}/components/libraries/bootloader/serial_dfu/nrf_dfu_serial.c
${NRF_SDK_ROOT}/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_uart.c
# ${NRF_SDK_ROOT}/components/libraries/bootloader/serial_dfu/nrf_dfu_serial_uart.c
${PROJECT_SOURCE_DIR}/nrf_dfu_serial_uart.c # use old version instead
${NRF_SDK_ROOT}/components/libraries/bootloader/dfu/dfu-cc.pb.c
${NRF_SDK_ROOT}/components/libraries/bootloader/dfu/nrf_dfu.c
${NRF_SDK_ROOT}/components/libraries/bootloader/dfu/nrf_dfu_flash.c
Expand Down
272 changes: 272 additions & 0 deletions dfu_legacy/nrf_dfu_serial_uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/**
* Copyright (c) 2016 - 2019, 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 <string.h>
// #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 RX_PIN_NUMBER 11
#define TX_PIN_NUMBER 12
#define CTS_PIN_NUMBER NRF_UARTE_PSEL_DISCONNECTED
#define RTS_PIN_NUMBER NRF_UARTE_PSEL_DISCONNECTED

#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 = NRF_ERROR_TIMEOUT;

// Check if there is byte to process. Zero length transfer means that RXTO occured.
if (len)
{
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)
{
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];
m_serial.p_low_level_transport = &uart_dfu_transport;

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;
}

uint32_t uart_battery_transport_init(void)
{
uint32_t err_code = NRF_SUCCESS;

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;
}

return err_code;
}

ret_code_t battery_percent_send(uint8_t const * p_data, uint32_t length)
{
return nrf_drv_uart_tx(&m_uart, p_data, length);
}

20 changes: 19 additions & 1 deletion drivers/pmu/axp2101.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ static bool axp2101_config_battery(void)
// CRITICAL -> 5%
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_GAUGE_THLD, 0x55));

// charge setting
// normarl charge current 500ma
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_ICC_CFG, 0b00001011));

// voltage
// bit 7:3 zero
// bit 2:0 0b100 -> 4.35v
Expand Down Expand Up @@ -169,6 +173,16 @@ static bool axp2101_config_common(void)
return true;
}

static bool axp2101_charge_current_sel(bool low_current_mode)
{
uint8_t reg_val = 0;
EC_E_BOOL_R_BOOL(axp2101_reg_read(AXP2101_ICC_CFG, &reg_val));
reg_val &= 0b11100000; // clear current bits [4:0]
reg_val |= (low_current_mode ? 0b00001001 : 0b00001011); // set current 300ma or 500ma
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_ICC_CFG, reg_val));
return true;
}

// function public

Power_Error_t axp2101_init(void)
Expand Down Expand Up @@ -401,7 +415,7 @@ Power_Error_t axp2101_pull_status(void)
status_temp.chargeFinished = ((hlbuff.u8_low & 0b00000111) == 0b00000100); // bit 2:0 = 100 charge done

if ( (hlbuff.u8_low & 0b01100000) == 0b00100000 || // bit 6:5 = 01 battery current charge
status_temp.chargeFinished // still try get power source
status_temp.chargeFinished // still try get power source
)
{
// TODO: find a batter way to check GPIO?
Expand All @@ -413,8 +427,12 @@ Power_Error_t axp2101_pull_status(void)
EC_E_BOOL_R_PWR_ERR(pmu_interface_p->GPIO.Config(8, PWR_GPIO_Config_UNUSED));

status_temp.wirelessCharge = !gpio_high_low; // low is wireless

// if not wireless charging then it's wired
status_temp.wiredCharge = !status_temp.wirelessCharge;

// wireless charge current limit to 300ma
EC_E_BOOL_R_PWR_ERR(axp2101_charge_current_sel(status_temp.wirelessCharge));
}
else
{
Expand Down
Loading

0 comments on commit 79aab2a

Please sign in to comment.