Skip to content

Commit

Permalink
Closes #25: Implement automatic support for CF20/CF21 using the same bin
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Nov 16, 2018
1 parent 94bf2ec commit 707a725
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 97 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CLOAD_SCRIPT ?= ../crazyflie-clients-python/bin/cfloader
S110 ?= 1 # SoftDevice flashed or not
BLE ?= 1 # BLE mode activated or not. If disabled, CRTP mode is active

PLATFORM ?= cf2

CROSS_COMPILE?=arm-none-eabi-

CC=$(CROSS_COMPILE)gcc
Expand All @@ -22,7 +24,7 @@ OPENOCD_TARGET ?= target/nrf51_stlink.tcl
OPENOCD_CMDS ?=


POWER_MANAGEMENT ?= cf2
POWER_MANAGEMENT ?= $(PLATFORM)

NRF51_SDK ?= nrf51_sdk/nrf51822
NRF_S110 ?= s110
Expand All @@ -35,7 +37,7 @@ PERSONAL_DEFINES ?=

PROCESSOR = -mcpu=cortex-m0 -mthumb
NRF= -DNRF51
PROGRAM=cf2_nrf
PROGRAM=$(PLATFORM)_nrf

CFLAGS+=$(PROCESSOR) $(NRF) $(PERSONAL_DEFINES) $(INCLUDES) $(CONFIG) $(BUILD_OPTION)
ASFLAGS=$(PROCESSOR)
Expand Down Expand Up @@ -85,7 +87,7 @@ OBJS += src/main.o gcc_startup_nrf51.o system_nrf51.o src/uart.o \
src/syslink.o src/pm_$(POWER_MANAGEMENT).o src/systick.o src/button.o src/swd.o src/ow.o \
src/ow/owlnk.o src/ow/ownet.o src/ow/owtran.o \
src/ow/crcutil.o src/ds2431.o src/ds28e05.o src/esb.o src/memory.o \
src/platform_info.o
src/platform.o src/platform_$(PLATFORM).o

all: $(PROGRAM).elf $(PROGRAM).bin $(PROGRAM).hex
$(SIZE) $(PROGRAM).elf
Expand All @@ -99,6 +101,7 @@ ifeq ($(strip $(BLE)),1)
else
@echo "BLE Disabled"
endif
@echo "Built for platform $(PLATFORM)"

$(PROGRAM).hex: $(PROGRAM).elf
$(OBJCOPY) $^ -O ihex $@
Expand Down
45 changes: 45 additions & 0 deletions interface/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <stdbool.h>

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

/**
* 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.
*/
int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType);

/**
* 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();

// ************** 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();
11 changes: 0 additions & 11 deletions interface/platform_info.h

This file was deleted.

8 changes: 7 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <stdio.h>
#include <string.h>

#include "platform.h"

#include "uart.h"
#include "esb.h"
#include "syslink.h"
Expand All @@ -38,7 +40,6 @@
#include "pm.h"
#include "pinout.h"
#include "systick.h"
#include "uart.h"

#include "memory.h"
#include "ownet.h"
Expand Down Expand Up @@ -74,6 +75,11 @@ static void handleBootloaderCmd(struct esbPacket_s *packet);

int main()
{
// Stop early if the platform is not supported
if (platformInit() != 0) {
while(1);
}

systickInit();
memoryInit();

Expand Down
41 changes: 41 additions & 0 deletions src/platform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "platform.h"

#include <string.h>

static const char *defaultDeviceType = "0;CF20;R=D";

static volatile unsigned char *deviceTypeStringLocation = 0x3FFE0;

void platformGetDeviceTypeString(char *deviceTypeString)
{
if (deviceTypeStringLocation[0] == 0xffu) {
strncpy(deviceTypeString, defaultDeviceType, 32);
deviceTypeString[32] = 0;
} else {
strncpy(deviceTypeString, deviceTypeStringLocation, 32);
deviceTypeString[32] = 0;
}
}

int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType) {
char *state;
char *tok;

// first token is the version, must be "0"
tok = strtok_r(deviceTypeString, ";", &state);
if (tok == NULL || strcmp(tok, "0")) {
return 1;
}

// Second token is the platform name
tok = strtok_r(NULL, ";", &state);
if (tok == NULL) {
return 1;
}
strncpy(deviceType, tok, PLATFORM_DEVICE_TYPE_MAX_LEN);
deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN-1] = '\0';

// Next tokens are KEY=VALUE pairs, ignored for now

return 0;
}
31 changes: 31 additions & 0 deletions src/platform_cf2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "platform.h"

#include <string.h>

bool has_rfx2411n = false;

bool platformHasRfx2411n()
{
return has_rfx2411n;
}

int platformInit()
{
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 (!strcmp(deviceType, "CF20")) {
has_rfx2411n = false;
} else if (!strcmp(deviceType, "CF21")) {
has_rfx2411n = true;
} else {
return 1;
}

return 0;
}
18 changes: 0 additions & 18 deletions src/platform_info.c

This file was deleted.

6 changes: 6 additions & 0 deletions src/platform_rzr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "platform.h"

int platformInit()
{
return 0;
}
75 changes: 38 additions & 37 deletions src/pm_cf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "led.h"
#include "systick.h"
#include "uart.h"
#include "platform.h"

#define TICK_BETWEEN_STATE 2
#define TICK_BETWEEN_ADC_MEAS 5
Expand Down Expand Up @@ -115,13 +116,13 @@ static void pmNrfPower(bool enable)
//stop NRF
LED_OFF();
// Turn off PA
#ifdef HAS_RFX2411N
nrf_gpio_pin_clear(RADIO_PA_RX_EN);
nrf_gpio_pin_clear(RADIO_PA_MODE);
nrf_gpio_pin_clear(RADIO_PA_ANT_SW);
#else
nrf_gpio_pin_clear(RADIO_PAEN_PIN);
#endif
if (platformHasRfx2411n) {
nrf_gpio_pin_clear(RADIO_PA_RX_EN);
nrf_gpio_pin_clear(RADIO_PA_MODE);
nrf_gpio_pin_clear(RADIO_PA_ANT_SW);
} else {
nrf_gpio_pin_clear(RADIO_PAEN_PIN);
}
// Disable 1-wire pull-up
nrf_gpio_pin_clear(OW_PULLUP_PIN);
// CE, EN1 and EN2 externally pulled low. Put low to not draw any current.
Expand Down Expand Up @@ -207,37 +208,37 @@ static void pmRunSystem(bool enable)
nrf_gpio_pin_clear(PM_CHG_EN);
#endif

#ifdef HAS_RFX2411N
// 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 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
if (platformHasRfx2411n()) {
// Enable RF power amplifier
nrf_gpio_cfg_output(RADIO_PAEN_PIN);

#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
#endif
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 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 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
}

#ifdef HAS_BAT_SINK
// Sink battery divider
Expand Down
27 changes: 0 additions & 27 deletions src/pm_cf21.c

This file was deleted.

0 comments on commit 707a725

Please sign in to comment.