Skip to content

Commit

Permalink
bitcraze#700 Check lighthouse bitstream using CRC
Browse files Browse the repository at this point in the history
Removes the lighthouse deck bitstream from the fimware and check if the
bitstream in the deck is supported using CRC instread of a direct
byte comparison. This allows to remove the DISABLE_LIGHTHOUSE_DRIVER
macro and so to have the listhouse always enabled.

This firmware is now incapable of flashing a new bitstream if needed.
Implementation of a flashing over CRTP is needed to make it fonctional
again.
  • Loading branch information
ataffanel committed Feb 18, 2021
1 parent 2f439d5 commit d628df1
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 96 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ jobs:
# Build Bigquad deck with all config options
- >
"EXTRA_CFLAGS=-DENABLE_BQ_DECK -DBQ_DECK_ENABLE_OSD -DBQ_DECK_ENABLE_PM"
# Build with the lighthouse driver
- >
"EXTRA_CFLAGS=-DDISABLE_LIGHTHOUSE_DRIVER=0"
env:
FEATURE: ${{ matrix.features }}

Expand Down
Binary file removed blobs/lighthouse.bin
Binary file not shown.
4 changes: 0 additions & 4 deletions docs/functional-areas/lighthouse/setting_up.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ Clone (or download) the [crazyflie-firmware](https://github.com/bitcraze/crazyfl
git clone https://github.com/bitcraze/crazyflie-firmware
```

### Enable lighthouse support

To compile the firmware with lighthouse support, use the ```DISABLE_LIGHTHOUSE_DRIVER=0``` compile flag. For instance this can be done by adding ```CFLAGS += -DDISABLE_LIGHTHOUSE_DRIVER=0``` to your config.mk file.

### System identification

The Crayflie will make an educated guess in run time based on the light signals it receives to decide if the system uses lighthouse V1 or V2 base stations. Unfortunately this sometimes fails, and a better option is to comile the firmware with a compile flag to force it to V1 or V2 instead. Add ```CFLAGS += -DLIGHTHOUSE_FORCE_TYPE=1``` or ```CFLAGS += -DLIGHTHOUSE_FORCE_TYPE=2``` depending on your system.
Expand Down
3 changes: 0 additions & 3 deletions examples/demos/swarm_demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ APP_STACKSIZE=300
## Weight of the Crazyflie + Qi + Lighthouse deck
CFLAGS += -DCF_MASS=0.036f

## To build with lighthouse support
CFLAGS += -DDISABLE_LIGHTHOUSE_DRIVER=0

## Force lighthouse 2
CFLAGS += -DLIGHTHOUSE_FORCE_TYPE=2

Expand Down
6 changes: 1 addition & 5 deletions src/deck/drivers/src/ledring12.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,7 @@ static void rssiEffect(uint8_t buffer[][3], bool reset)
*/
static void lighthouseEffect(uint8_t buffer[][3], bool reset)
{
#if DISABLE_LIGHTHOUSE_DRIVER == 1
uint16_t validAngles = 0;
#else
uint16_t validAngles = pulseProcessorAnglesQuality();
#endif
uint16_t validAngles = pulseProcessorAnglesQuality();

for (int i = 0; i < NBR_LEDS; i++) {
buffer[i][0] = LIMIT(LINSCALE(0.0f, 255.0f, 100.0f, 0.0f, validAngles)); // Red (small validAngles)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/interface/lighthouse/lighthouse_deck_flasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@

#pragma once

void lighthouseDeckFlasherCheckVersionAndBoot();
bool lighthouseDeckFlasherCheckVersionAndBoot();
7 changes: 6 additions & 1 deletion src/modules/src/lighthouse/lighthouse_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ void lighthouseCoreTask(void *param) {
lighthouseStorageInitializeGeoDataFromStorage();
lighthouseStorageInitializeCalibDataFromStorage();

lighthouseDeckFlasherCheckVersionAndBoot();
if (lighthouseDeckFlasherCheckVersionAndBoot() == false) {
DEBUG_PRINT("FPGA not booted. Lighthouse disabled!\n");
while(1) {
vTaskDelay(portMAX_DELAY);
}
}
deckIsFlashed = true;


Expand Down
105 changes: 29 additions & 76 deletions src/modules/src/lighthouse/lighthouse_deck_flasher.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,17 @@
#include "debug.h"
#include "lh_bootloader.h"
#include "lighthouse_deck_flasher.h"
#include "crc32.h"

#ifdef LH_FLASH_BOOTLOADER
#include "lh_flasher.h"
#endif

#define BITSTREAM_CRC 0xe2889216
#define BITSTREAM_SIZE 104092

// Uncomment if you want to force the Crazyflie to reflash the FPGA binary to the deck at each startup
// #define FORCE_FLASH_FPGA true

#ifndef FORCE_FLASH_FPGA
#define FORCE_FLASH_FPGA false
#endif


#ifndef DISABLE_LIGHTHOUSE_DRIVER
#define DISABLE_LIGHTHOUSE_DRIVER 1
#endif


#if DISABLE_LIGHTHOUSE_DRIVER == 1
void lighthouseDeckFlasherCheckVersionAndBoot() {
DEBUG_PRINT("No Lighthouse support in FW, deck not initialized\n");
}
#else

#define STR2(x) #x
#define STR(x) STR2(x)

#define INCBIN(name, file) \
__asm__(".section .rodata\n" \
".global incbin_" STR(name) "_start\n" \
".align 4\n" \
"incbin_" STR(name) "_start:\n" \
".incbin \"" file "\"\n" \
\
".global incbin_" STR(name) "_end\n" \
".align 1\n" \
"incbin_" STR(name) "_end:\n" \
".byte 0\n" \
".align 4\n" \
STR(name) "Size:\n" \
".int incbin_" STR(name) "_end - incbin_" STR(name) "_start\n" \
); \
extern const __attribute__((aligned(4))) void* incbin_ ## name ## _start; \
extern const void* incbin_ ## name ## _end; \
extern const int name ## Size; \
static const __attribute__((used)) unsigned char* name = (unsigned char*) & incbin_ ## name ## _start; \

INCBIN(bitstream, BLOBS_LOC"lighthouse.bin");


void lighthouseDeckFlasherCheckVersionAndBoot() {
bool lighthouseDeckFlasherCheckVersionAndBoot() {
lhblInit(I2C1_DEV);

#ifdef LH_FLASH_BOOTLOADER
Expand All @@ -94,51 +53,45 @@ void lighthouseDeckFlasherCheckVersionAndBoot() {
#endif

uint8_t bootloaderVersion = 0;
lhblGetVersion(&bootloaderVersion);
if (lhblGetVersion(&bootloaderVersion) == false) {
DEBUG_PRINT("Cannot communicate with lighthouse bootloader, aborting!\n");
return false;
}
DEBUG_PRINT("Lighthouse bootloader version: %d\n", bootloaderVersion);

// Wakeup mem
lhblFlashWakeup();
vTaskDelay(M2T(1));

// Checking the bitstreams are identical
// Also decoding bitstream version for console
// Decoding bitstream version for console
static char deckBitstream[65];
lhblFlashRead(LH_FW_ADDR, 64, (uint8_t*)deckBitstream);
deckBitstream[64] = 0;
int deckVersion = strtol(&deckBitstream[2], NULL, 10);
int embeddedVersion = strtol((char*)&bitstream[2], NULL, 10);

bool identical = true;
for (int i=0; i<=bitstreamSize; i+=64) {
int length = ((i+64)<bitstreamSize)?64:bitstreamSize-i;
// Checking that the bitstream has the right checksum
crc32Context_t crcContext;
crc32ContextInit(&crcContext);

for (int i=0; i<=BITSTREAM_SIZE; i+=64) {
int length = ((i+64)<BITSTREAM_SIZE)?64:BITSTREAM_SIZE-i;
lhblFlashRead(LH_FW_ADDR + i, length, (uint8_t*)deckBitstream);
if (memcmp(deckBitstream, &bitstream[i], length)) {
DEBUG_PRINT("Fail comparing firmware\n");
identical = false;
break;
}
crc32Update(&crcContext, deckBitstream, length);
}

if (identical == false || FORCE_FLASH_FPGA) {
DEBUG_PRINT("Deck has version %d and we embeed version %d\n", deckVersion, embeddedVersion);
DEBUG_PRINT("Updating deck with embedded version!\n");

// Erase LH deck FW
lhblFlashEraseFirmware();

// Flash LH deck FW
if (lhblFlashWriteFW((uint8_t*)bitstream, bitstreamSize)) {
DEBUG_PRINT("FW updated [OK]\n");
deckVersion = embeddedVersion;
} else {
DEBUG_PRINT("FW updated [FAILED]\n");
}
}
uint32_t crc = crc32Out(&crcContext);
bool pass = crc == BITSTREAM_CRC;
DEBUG_PRINT("Bitstream CRC32: %x %s\n", (int)crc, pass?"[PASS]":"[FAIL]");

// Launch LH deck FW
DEBUG_PRINT("Firmware version %d verified, booting deck!\n", deckVersion);
lhblBootToFW();
if (pass) {
DEBUG_PRINT("Firmware version %d verified, booting deck!\n", deckVersion);
lhblBootToFW();
} else {
DEBUG_PRINT("The deck bitstream does not match the required bitstream.\n");
DEBUG_PRINT("We require lighthouse bitstream of size %d and CRC32 %x.\n", BITSTREAM_SIZE, BITSTREAM_CRC);
DEBUG_PRINT("Leaving the deck in bootloader mode ...\n");
}

return pass;
}

#endif
3 changes: 0 additions & 3 deletions tools/make/config.mk.example
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@
## SDCard test configuration ------------------------------------
# FATFS_DISKIO_TESTS = 1 # Set to 1 to enable FatFS diskio function tests. Erases card.

## To build with lighthouse support
# CFLAGS += -DDISABLE_LIGHTHOUSE_DRIVER=0

## Force lighthouse V1 or V2 type system
# Lighthouse V1
# CFLAGS += -DLIGHTHOUSE_FORCE_TYPE=1
Expand Down

0 comments on commit d628df1

Please sign in to comment.