From ceef80408cd2034a7243afb3c7f6e7136035f678 Mon Sep 17 00:00:00 2001 From: Arnaud Taffanel Date: Tue, 6 Feb 2024 15:00:32 +0100 Subject: [PATCH] Enable PA on all platforms --- Makefile | 1 + config/custom_board.h | 3 ++ src/crazyflie2_pm.c | 46 +++++++++++++++++--------- src/main.c | 7 ++-- src/platform.c | 77 +++++++++++++++++++++++++++++++++++++++++++ src/platform.h | 67 +++++++++++++++++++++++++++++++++++++ 6 files changed, 183 insertions(+), 18 deletions(-) create mode 100644 src/platform.c create mode 100644 src/platform.h diff --git a/Makefile b/Makefile index 0626cd7..d6ec3d9 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,7 @@ SRC_FILES += \ $(PROJ_DIR)/uart.c \ $(PROJ_DIR)/bootloader.c \ $(PROJ_DIR)/crc.c \ + $(PROJ_DIR)/platform.c \ $(SDK_ROOT)/external/segger_rtt/RTT_Syscalls_GCC.c \ $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT.c \ $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_printf.c \ diff --git a/config/custom_board.h b/config/custom_board.h index 280276c..50c41da 100644 --- a/config/custom_board.h +++ b/config/custom_board.h @@ -50,6 +50,9 @@ #define STM_NRST_PIN 21 +#define RADIO_PAEN_PIN 19 +#define RADIO_PATX_DIS_PIN 20 + // Pins if RFX2411N is used #define RADIO_PA_RX_EN 20 #define RADIO_PA_ANT_SW 18 diff --git a/src/crazyflie2_pm.c b/src/crazyflie2_pm.c index 8bbf36d..38551e5 100644 --- a/src/crazyflie2_pm.c +++ b/src/crazyflie2_pm.c @@ -3,6 +3,7 @@ #include "nrf.h" #include "nrf_gpio.h" #include "boards.h" +#include "platform.h" static pm_state_t m_state = PM_STATE_OFF; @@ -28,24 +29,37 @@ void crazyflie2_pm_init(void) { // todo: setup analog measurement // Setup radio PA - nrf_gpio_cfg_output(RADIO_PA_RX_EN); - nrf_gpio_cfg_output(RADIO_PA_MODE); - nrf_gpio_cfg_output(RADIO_PA_ANT_SW); + if (platformHasRfx2411n()) { + // Enable RF power amplifier + nrf_gpio_cfg_output(RADIO_PA_RX_EN); + nrf_gpio_cfg_output(RADIO_PA_MODE); + nrf_gpio_cfg_output(RADIO_PA_ANT_SW); + #ifdef USE_EXT_ANTENNA + // Select u.FL antenna + nrf_gpio_pin_clear(RADIO_PA_ANT_SW); + #else + // Select chip antenna + nrf_gpio_pin_set(RADIO_PA_ANT_SW); + #endif -#ifdef USE_EXT_ANTENNA - // Select u.FL antenna - nrf_gpio_pin_clear(RADIO_PA_ANT_SW); -#else - // Select chip antenna - nrf_gpio_pin_set(RADIO_PA_ANT_SW); -#endif + #ifdef RFX2411N_BYPASS_MODE + nrf_gpio_pin_set(RADIO_PA_MODE); + #else + nrf_gpio_pin_set(RADIO_PA_RX_EN); + nrf_gpio_pin_clear(RADIO_PA_MODE); + #endif + } else { + // Enable RF power amplifier + nrf_gpio_cfg_output(RADIO_PAEN_PIN); -#ifdef RFX2411N_BYPASS_MODE - nrf_gpio_pin_set(RADIO_PA_MODE); -#else - nrf_gpio_pin_set(RADIO_PA_RX_EN); - nrf_gpio_pin_clear(RADIO_PA_MODE); -#endif + #ifdef DISABLE_PA + nrf_gpio_pin_clear(RADIO_PAEN_PIN); + nrf_gpio_cfg_output(RADIO_PATX_DIS_PIN); + nrf_gpio_pin_clear(RADIO_PATX_DIS_PIN); + #else + nrf_gpio_pin_set(RADIO_PAEN_PIN); + #endif + } crazyflie2_pm_set_state(PM_STATE_SYSTEM_OFF); } diff --git a/src/main.c b/src/main.c index ad25ea1..dd0c23e 100644 --- a/src/main.c +++ b/src/main.c @@ -78,6 +78,7 @@ #include "uart.h" #include "systick.h" #include "bootloader.h" +#include "platform.h" #include "crtp.h" @@ -549,6 +550,8 @@ int main(void) uint32_t err_code; static char address[5]; + platformInitByDeviceType(); + sd_mbr_command(&startSdCmd); sd_softdevice_vector_table_base_set(BOOTLOADER_ADDRESS); @@ -585,8 +588,8 @@ int main(void) // err_code = syslinkInit(); // APP_ERROR_CHECK(err_code); - // crazyflie2_pm_init(); - // crazyflie2_pm_set_state(PM_STATE_SYSTEM_ON); + crazyflie2_pm_init(); + crazyflie2_pm_set_state(PM_STATE_SYSTEM_ON); err_code = app_mailbox_create(&m_uplink); APP_ERROR_CHECK(err_code); diff --git a/src/platform.c b/src/platform.c new file mode 100644 index 0000000..2dab0d1 --- /dev/null +++ b/src/platform.c @@ -0,0 +1,77 @@ +#include "platform.h" + +#include +#include + +static const char *defaultDeviceType = "0;CF20;R=D"; + +static char *deviceTypeStringLocation = (void*)PLATFORM_DEVICE_DATA_FLASH_POS; + +static bool has_rfx2411n = false; + +void platformGetDeviceTypeString(char *deviceTypeString) +{ + if (deviceTypeStringLocation[0] == 0xffu) { + strncpy(deviceTypeString, defaultDeviceType, 32); + deviceTypeString[32] = 0; + } else { + strncpy(deviceTypeString, deviceTypeStringLocation, 32); + deviceTypeString[32] = 0; + } +} + +/** + * Parse deviceType string to extract the deviceType + * + * Ignores the key=value sections. + * + * \param [in] deviceTypeString deviceTypeString extracted from the hardware + * \param [out] deviceType Buffer of at least PLATFORM_DEVICE_TYPE_MAX_LEN + * bytes where the device type will be stored + * \return 0 in case of success, 1 in case of failure. + */ +static int platformParseDeviceTypeString(char* deviceTypeString, char* deviceType) { + // char *state; + + memcpy(deviceType, deviceTypeString+2, 4); + deviceType[4] = '\0'; + + return 0; +} + +// This function configures the platform in runtime based on the device type. +// The main reason to not move it into the platform files is that if a user +// flashes the wrong binary in the NRF we still want it to start up. +int platformInitByDeviceType() { + static char deviceTypeString[PLATFORM_DEVICE_TYPE_STRING_MAX_LEN]; + static char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN]; + + platformGetDeviceTypeString(deviceTypeString); + if (platformParseDeviceTypeString(deviceTypeString, deviceType) != 0) { + return 1; + } + + if (0 == strcmp(deviceType, "CF20")) { + has_rfx2411n = false; + + } else if (0 == strcmp(deviceType, "CF21")) { + has_rfx2411n = true; + + } else if (0 == strcmp(deviceType, "RR10")) { + has_rfx2411n = true; + + } else if ((0 == strcmp(deviceType, "RZ10")) || + (0 == strcmp(deviceType, "CB10")) || + (0 == strcmp(deviceType, "CB11"))) { + has_rfx2411n = true; + + } else { + has_rfx2411n = false; + } + + return 0; +} + +bool platformHasRfx2411n() { + return has_rfx2411n; +} diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 0000000..e99dfc4 --- /dev/null +++ b/src/platform.h @@ -0,0 +1,67 @@ +/** + * || ____ _ __ + * +------+ / __ )(_) /_______________ _____ ___ + * | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ + * +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ + * || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ + * + * Crazyflie 2.0 NRF Firmware + * Copyright (c) 2024, Bitcraze AB, All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +#pragma once + +#include +#include + +#define PLATFORM_DEVICE_DATA_FLASH_POS 0x3FFE0 +#define PLATFORM_DEVICE_TYPE_STRING_MAX_LEN 33 +#define PLATFORM_DEVICE_TYPE_MAX_LEN 31 + +/** + * Fetch deviceType string from flash + * + * This function defaults to "0;CF20;R=D" if no string is present in the + * hardware + * + * \param [out] deviceTypeString Buffer of at least + * PLATFORM_DEVICE_TYPE_STRING_MAX_LEN bytes + * where the platform string will be stored + */ +void platformGetDeviceTypeString(char *deviceTypeString); + +/** + * Initialize the platform + * + * Initialize the platform discovering capabilities and returns if it has been successful + * + * \return 0 in case of success, 1 in case of failure. + */ +int platformInit(); + +/** + * Initialize the platform based on device type + * + * Generic initialization based on device type + * + * \return 0 in case of success, 1 in case of failure. + */ +int platformInitByDeviceType(); + +// ************** Capabilities functions ************** +// The following functions can be implemented by different platform to give +// access to the current device capabilities. Not all platform has to implement +// all functions +bool platformHasRfx2411n();