Skip to content

Commit

Permalink
Enable PA on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Feb 6, 2024
1 parent 6c607c9 commit ceef804
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 18 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions config/custom_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 30 additions & 16 deletions src/crazyflie2_pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include "uart.h"
#include "systick.h"
#include "bootloader.h"
#include "platform.h"

#include "crtp.h"

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
77 changes: 77 additions & 0 deletions src/platform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "platform.h"

#include <string.h>
#include <nrf.h>

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;
}
67 changes: 67 additions & 0 deletions src/platform.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <stdbool.h>

#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();

0 comments on commit ceef804

Please sign in to comment.