Skip to content

Commit

Permalink
#379 Added dynamic configurations
Browse files Browse the repository at this point in the history
Identifying the device type based on the device type string and choosing the correct configuration.
  • Loading branch information
krichardsson committed Nov 13, 2018
1 parent 70c2225 commit 8224e28
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 46 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ CFLAGS += -DUSD_RUN_DISKIO_FUNCTION_TESTS
endif

# Crazyflie sources
VPATH += src/init src/hal/src src/modules/src src/utils/src src/drivers/bosch/src src/drivers/src
VPATH_CF2 += src/platform/cf2
VPATH += src/init src/hal/src src/modules/src src/utils/src src/drivers/bosch/src src/drivers/src src/platform

ifeq ($(PLATFORM), CF2)
VPATH +=$(VPATH_CF2)
Expand All @@ -120,7 +119,7 @@ endif

# Init
PROJ_OBJ += main.o
PROJ_OBJ_CF2 += platform_cf2.o platform_info_stm32.o
PROJ_OBJ_CF2 += platform.o platform_cf2.o platform_stm32.o

# Drivers
PROJ_OBJ += exti.o nvic.o motors.o
Expand Down
6 changes: 5 additions & 1 deletion src/init/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
int main()
{
//Initialize the platform.
platformInit();
int err = platformInit();
if (err != 0) {
// The firmware is running on the wrong hardware. Halt
while(1);
}

//Launch the system task that will initialize and start everything
systemLaunch();
Expand Down
4 changes: 2 additions & 2 deletions src/modules/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ void systemInit(void)
usblinkInit();
sysLoadInit();

/* Initialized hear and early so that DEBUG_PRINT (buffered) can be used early */
/* Initialized here so that DEBUG_PRINT (buffered) can be used early */
crtpInit();
consoleInit();

DEBUG_PRINT("----------------------------\n");
DEBUG_PRINT(P_NAME " is up and running!\n");
DEBUG_PRINT("%s is up and running!\n", platformConfigGetDeviceTypeName());
DEBUG_PRINT("Build %s:%s (%s) %s\n", V_SLOCAL_REVISION,
V_SREVISION, V_STAG, (V_MODIFIED)?"MODIFIED":"CLEAN");
DEBUG_PRINT("I am 0x%08X%08X%08X and I have %dKB of flash!\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Crazyflie control firmware
*
* Copyright (C) 2018 Bitcraze AB
* Copyright (C) 2011-2012 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,15 +21,33 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* platform_info_stm32.h - platform information driver for STM32 based platforms
*
* Platform information is used to identify the hardware platform the firmware
* is running on.
* Generic platform functionality
*
*/

#define PLATFORM_INFO_OTP_NR_OF_BLOCKS 16
#define PLATFORM_INFO_OTP_BLOCK_LEN 32
#define PLATFORM_INFO_MAX_PLATFORM_STRING_LEN (PLATFORM_INFO_OTP_BLOCK_LEN + 1)
#include <string.h>
#include "platform.h"

int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType) {
if (deviceTypeString[0] != '0' || deviceTypeString[1] != ';') {
return 1;
}

const int start = 2;
const int last = start + PLATFORM_DEVICE_TYPE_MAX_LEN - 1;
int end = 0;
for (end = start; end <= last; end++) {
if (deviceTypeString[end] == '\0' || deviceTypeString[end] == ';') {
break;
}
}

if (end > last) {
return 1;
}

void platformInfoGetPlatformString(char* buf);
int length = end - start;
memcpy(deviceType, &deviceTypeString[start], length);
deviceType[length] = '\0';
return 0;
}
14 changes: 14 additions & 0 deletions src/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,25 @@
#ifndef PLATFORM_H_
#define PLATFORM_H_

#include <stdbool.h>

#define PLATFORM_DEVICE_TYPE_STRING_MAX_LEN (32 + 1)
#define PLATFORM_DEVICE_TYPE_MAX_LEN (4 + 1)

/**
* Initilizes all platform specific things.
*/
int platformInit(void);

void platformGetDeviceTypeString(char* deviceTypeString);
int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType);

void platformSetLowInterferenceRadioMode(void);


// Functions to read configuration
const char* platformConfigGetPlatformName();
const char* platformConfigGetDeviceTypeName();
// TODO Add relevant config functions

#endif /* PLATFORM_H_ */
88 changes: 79 additions & 9 deletions src/platform/cf2/platform_cf2.c → src/platform/platform_cf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* TODO: Add description
* Platform functionality for the CF2 platform
*/

#define DEBUG_MODULE "PLATFORM"

/* Personal configs */
/* Project includes */
#include <string.h>

#include "platform.h"
#include "exti.h"
#include "nvic.h"
#include "debug.h"
Expand All @@ -38,21 +39,90 @@
#define PLATFORM_NRF51_LOW_INTERFERENCE_TX_POWER_DBM (-12)
#endif

// TODO: Implement!
static int initPlatformConfiguration();
static void initHardware();


typedef struct {
char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN];
char deviceTypeName[14];
} config_t;

static config_t configs[] = {
{
.deviceType = "CF20",
.deviceTypeName = "Crazyflie 2.0",
},
{
.deviceType = "CF21",
.deviceTypeName = "Crazyflie 2.1",
}
};

static config_t* active_config = 0;


int platformInit(void)
{
//Low level init: Clock and Interrupt controller
nvicInit();

//EXTI interrupts
extiInit();
int err = initPlatformConfiguration();
if (err != 0)
{
// This firmware is not compatible, abort init
return 1;
}

initHardware();
return 0;
}


void platformSetLowInterferenceRadioMode(void)
{
// Decrease the nRF51 Tx power to reduce interference
radiolinkSetPowerDbm(PLATFORM_NRF51_LOW_INTERFERENCE_TX_POWER_DBM);
DEBUG_PRINT("Low interference mode. NRF51 TX power offset by %ddb.\r\n", PLATFORM_NRF51_LOW_INTERFERENCE_TX_POWER_DBM);
}


static int initPlatformConfiguration()
{
char deviceTypeString[PLATFORM_DEVICE_TYPE_STRING_MAX_LEN];
char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN];

platformGetDeviceTypeString(deviceTypeString);
platformParseDeviceTypeString(deviceTypeString, deviceType);

for (int i = 0; i < (sizeof(configs) / sizeof(config_t)); i++) {
config_t* config = &configs[i];
if (strcmp(config->deviceType, deviceType) == 0) {
active_config = config;
return 0;
}
}

return 1;
}

static void initHardware()
{
//Low level init: Clock and Interrupt controller
nvicInit();

//EXTI interrupts
extiInit();
}


// Config functions ------------------------

const char* platformConfigGetPlatformName()
{
return "cf2";
}

const char* platformConfigGetDeviceTypeName()
{
return active_config->deviceTypeName;
}


Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Crazyflie control firmware
*
* Copyright (C) 2018 Bitcraze AB
* Copyright (C) 2011-2012 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,18 +21,23 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* platform_info_stm32.c - platform information driver for STM32 based platforms
*
* Platform information is used to identify the hardware platform the firmware
* is running on.
* Platform functionality for platforms using the STM32
*
*/

#include <string.h>
#include "platform_info_stm32.h"
#include "platform.h"

#define PLATFORM_INFO_OTP_NR_OF_BLOCKS 16
#define PLATFORM_INFO_OTP_BLOCK_LEN 32
#if PLATFORM_DEVICE_TYPE_STRING_MAX_LEN < (PLATFORM_INFO_OTP_BLOCK_LEN + 1)
#error
#endif


#define DEFAULT_PLATFORM_STRING "0;CF20"


#ifndef UNIT_TEST_MODE
static char* getAddressOfOtpMemoryBlock(int blockNr) {
return (char*)(0x1fff7800 + blockNr * 0x20);
Expand All @@ -45,7 +50,7 @@ static char* getAddressOfOtpMemoryBlock(int blockNr) {



void platformInfoGetPlatformString(char* buf) {
void platformGetDeviceTypeString(char* deviceTypeString) {
char* block = 0;

for (int i = 0; i < PLATFORM_INFO_OTP_NR_OF_BLOCKS; i++) {
Expand All @@ -60,6 +65,6 @@ void platformInfoGetPlatformString(char* buf) {
block = DEFAULT_PLATFORM_STRING;
}

strncpy(buf, block, PLATFORM_INFO_OTP_BLOCK_LEN);
buf[PLATFORM_INFO_OTP_BLOCK_LEN] = '\0';
strncpy(deviceTypeString, block, PLATFORM_INFO_OTP_BLOCK_LEN);
deviceTypeString[PLATFORM_INFO_OTP_BLOCK_LEN] = '\0';
}
85 changes: 85 additions & 0 deletions test/platform/Test_platform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// File under test platform.c
#include "platform.h"

#include "unity.h"
#include <string.h>


static char actualDeviceType[100];

void setUp(void) {
strcpy(actualDeviceType, "abcdefghijklmn");
}

void tearDown(void) {
// Empty
}

void testThatDeviceTypeStringIsReturned() {
// Fixture
const char* deviceTypeString = "0;CF20";

// Test
int actual = platformParseDeviceTypeString(deviceTypeString, actualDeviceType);

// Assert
TEST_ASSERT_EQUAL_STRING("CF20", actualDeviceType);
TEST_ASSERT_EQUAL(0, actual);
}

void testThatDeviceTypeStringIsReturnedWithTrailingSemiColonAndKeyValues() {
// Fixture
const char* deviceTypeString = "0;CF21;R=C";

// Test
int actual = platformParseDeviceTypeString(deviceTypeString, actualDeviceType);

// Assert
TEST_ASSERT_EQUAL_STRING("CF21", actualDeviceType);
TEST_ASSERT_EQUAL(0, actual);
}

void testThatDeviceTypeStringIsReturnedWhenTypeIdentifierIsShorterThan4Chars() {
// Fixture
const char* deviceTypeString = "0;AB;R=C";

// Test
int actual = platformParseDeviceTypeString(deviceTypeString, actualDeviceType);

// Assert
TEST_ASSERT_EQUAL_STRING("AB", actualDeviceType);
TEST_ASSERT_EQUAL(0, actual);
}

void testThatDeviceTypeIsNotReturnedWhenTypeIdentifierIsTooLong() {
// Fixture
const char* deviceTypeString = "0;CF21XXX;R=C";

// Test
int actual = platformParseDeviceTypeString(deviceTypeString, actualDeviceType);

// Assert
TEST_ASSERT_NOT_EQUAL(0, actual);
}

void testThatDeviceTypeIsNotReturnedIfVersionIfNot0() {
// Fixture
const char* deviceTypeString = "1;CF21";

// Test
int actual = platformParseDeviceTypeString(deviceTypeString, actualDeviceType);

// Assert
TEST_ASSERT_NOT_EQUAL(0, actual);
}

void testThatDeviceTypeIsNotReturnedIfSecondCharIsNotSemicolon() {
// Fixture
const char* deviceTypeString = "0+CF21";

// Test
int actual = platformParseDeviceTypeString(deviceTypeString, actualDeviceType);

// Assert
TEST_ASSERT_NOT_EQUAL(0, actual);
}
Loading

0 comments on commit 8224e28

Please sign in to comment.