Skip to content

Commit

Permalink
Import HDZero OSD Driver from iNav
Browse files Browse the repository at this point in the history
Mostly from https://github.com/iNavFlight/inav/pull/7668/files commit
Notable changes:
* And added missing functions to bitarray.h/.c, msp_serial.h/.c
* Used "drivers/max7456_symbols.h" instead of "drivers/osd_symbols.h"
* Used baudRates[portConfig->msp_baudrateIndex] instead of
baudRates[portConfig->peripheral_baudrateIndex]

Additional changes:
serial: serialPortFunction_e now needs 32 bits

How to use:

In CLI, Set FUNCTION_HDZERO_OSD to the appropriate serial port:
Eg. Assuming to use serial port 4, enter the following CLI commands to
enable FUNCTION_HDZERO_OSD in EmuFlight Configurator 3.0.1:
    serial 3 65536 115200 115200 0 115200
    save
  • Loading branch information
saidinesh5 committed Mar 17, 2022
1 parent a631e0d commit c58ea02
Show file tree
Hide file tree
Showing 21 changed files with 632 additions and 25 deletions.
1 change: 1 addition & 0 deletions make/source.mk
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ COMMON_SRC = \
io/dashboard.c \
io/displayport_max7456.c \
io/displayport_msp.c \
io/displayport_hdzero_osd.c \
io/displayport_oled.c \
io/displayport_srxl.c \
io/displayport_crsf.c \
Expand Down
50 changes: 50 additions & 0 deletions src/main/common/bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,53 @@ void bitArrayCopy(void *array, unsigned from, unsigned to) {
bitArrayClr(array, to);
}
}

void bitArrayClrAll(bitarrayElement_t *array, size_t size)
{
memset(array, 0, size);
}

__attribute__((always_inline)) static inline uint8_t __CTZ(uint32_t val)
{
// __builtin_ctz is not defined for zero, since it's arch
// dependant. However, in ARM it gets translated to a
// rbit and then a clz, making it return 32 for zero on ARM.
// For other architectures, explicitely implement the same
// semantics.
#ifdef __arm__
uint8_t zc;
__asm__ volatile ("rbit %1, %1\n\t"
"clz %0, %1"
: "=r" (zc)
: "r" (val) );
return zc;
#else
// __builtin_clz is not defined for zero, since it's arch
// dependant. Make it return 32 like ARM's CLZ.
return val ? __builtin_ctz(val) : 32;
#endif
}

int bitArrayFindFirstSet(const bitarrayElement_t *array, unsigned start, size_t size)
{
const uint32_t *ptr = (uint32_t*)array;
const uint32_t *end = ptr + (size / 4);
const uint32_t *p = ptr + start / (8 * 4);

if (p < end) {
int ret;
// First iteration might need to mask some bits
uint32_t mask = 0xFFFFFFFF << (start % (8 * 4));
if ((ret = __CTZ(*p & mask)) != 32) {
return (((char *)p) - ((char *)ptr)) * 8 + ret;
}
p++;
while (p < end) {
if ((ret = __CTZ(*p)) != 32) {
return (((char *)p) - ((char *)ptr)) * 8 + ret;
}
p++;
}
}
return -1;
}
13 changes: 13 additions & 0 deletions src/main/common/bitarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@

#pragma once

typedef uint32_t bitarrayElement_t;

bool bitArrayGet(const void *array, unsigned bit);
void bitArraySet(void *array, unsigned bit);
void bitArrayClr(void *array, unsigned bit);
void bitArrayXor(void *dest, size_t size, void *op1, void *op2);
void bitArrayCopy(void *array, unsigned from, unsigned to);
void bitArrayClrAll(bitarrayElement_t *array, size_t size);
// Returns the first set bit with pos >= start_bit, or -1 if all bits
// are zero. Note that size must indicate the size of array in bytes.
// In most cases, you should use the BITARRAY_FIND_FIRST_SET() macro
// to call this function.
int bitArrayFindFirstSet(const bitarrayElement_t *array, unsigned start_bit, size_t size);

#define BITARRAY_DECLARE(name, bits) bitarrayElement_t name[(bits + 31) / 32]
#define BITARRAY_SET_ALL(array) bitArraySetAll(array, sizeof(array))
#define BITARRAY_CLR_ALL(array) bitArrayClrAll(array, sizeof(array))
#define BITARRAY_FIND_FIRST_SET(array, start_bit) bitArrayFindFirstSet(array, start_bit, sizeof(array))
11 changes: 11 additions & 0 deletions src/main/drivers/display_font_metadata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "drivers/display_font_metadata.h"
#include "drivers/osd.h"

bool displayFontMetadataUpdateFromCharacter(displayFontMetadata_t *metadata, const osdCharacter_t *chr)
{
if (chr && FONT_CHR_IS_METADATA(chr)) {
metadata->version = chr->data[5];
return true;
}
return false;
}
24 changes: 24 additions & 0 deletions src/main/drivers/display_font_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

typedef struct osdCharacter_s osdCharacter_t;

typedef struct displayFontMetadata_s {
uint8_t version;
uint16_t charCount;
} displayFontMetadata_t;

// 'I', 'N', 'A', 'V'
#define FONT_CHR_IS_METADATA(chr) ((chr)->data[0] == 'I' && \
(chr)->data[1] == 'N' && \
(chr)->data[2] == 'A' && \
(chr)->data[3] == 'V')

#define FONT_METADATA_CHR_INDEX 255
// Used for runtime detection of display drivers that might
// support 256 or 512 characters.
#define FONT_METADATA_CHR_INDEX_2ND_PAGE 256

bool displayFontMetadataUpdateFromCharacter(displayFontMetadata_t *metadata, const osdCharacter_t *chr);
2 changes: 1 addition & 1 deletion src/main/drivers/serial_softserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ uint32_t softSerialTxBytesFree(const serialPort_t *instance) {
return 0;
}
softSerial_t *s = (softSerial_t *)instance;
uint8_t bytesUsed = (s->port.txBufferHead - s->port.txBufferTail) & (s->port.txBufferSize - 1);
uint32_t bytesUsed = (s->port.txBufferHead - s->port.txBufferTail) & (s->port.txBufferSize - 1);
return (s->port.txBufferSize - 1) - bytesUsed;
}

Expand Down
24 changes: 19 additions & 5 deletions src/main/fc/fc_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
#include "rx/spektrum.h"

#include "io/beeper.h"
#include "io/displayport_hdzero_osd.h"
#include "io/displayport_max7456.h"
#include "io/displayport_srxl.h"
#include "io/serial.h"
Expand Down Expand Up @@ -508,12 +509,25 @@ void init(void) {
if (feature(FEATURE_OSD)) {
#if defined(USE_OSD_BEESIGN)
// If there is a beesign for the OSD then use it
osdDisplayPort = beesignDisplayPortInit(vcdProfile());
#elif defined(USE_MAX7456)
if (!osdDisplayPort) {
osdDisplayPort = beesignDisplayPortInit(vcdProfile());
}
#endif
#if defined(USE_HDZERO_OSD)
if (!osdDisplayPort) {
osdDisplayPort = hdzeroOsdDisplayPortInit();
}
#endif
#if defined(USE_MAX7456)
// If there is a max7456 chip for the OSD then use it
osdDisplayPort = max7456DisplayPortInit(vcdProfile());
#elif defined(USE_CMS) && defined(USE_MSP_DISPLAYPORT) && defined(USE_OSD_OVER_MSP_DISPLAYPORT) // OSD over MSP; not supported (yet)
osdDisplayPort = displayPortMspInit();
if (!osdDisplayPort) {
osdDisplayPort = max7456DisplayPortInit(vcdProfile());
}
#endif
#if defined(USE_CMS) && defined(USE_MSP_DISPLAYPORT) && defined(USE_OSD_OVER_MSP_DISPLAYPORT) // OSD over MSP; not supported (yet)
if (!osdDisplayPort) {
osdDisplayPort = displayPortMspInit();
}
#endif
// osdInit will register with CMS by itself.
osdInit(osdDisplayPort);
Expand Down
6 changes: 6 additions & 0 deletions src/main/fc/fc_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "io/gps.h"
#include "io/ledstrip.h"
#include "io/osd.h"
#include "io/displayport_hdzero_osd.h"
#include "io/piniobox.h"
#include "io/serial.h"
#include "io/transponder_ir.h"
Expand Down Expand Up @@ -159,6 +160,11 @@ static void taskHandleSerial(timeUs_t currentTimeUs) {
bool evaluateMspData = osdSlaveIsLocked ? MSP_SKIP_NON_MSP_DATA : MSP_EVALUATE_NON_MSP_DATA;;
#endif
mspSerialProcess(evaluateMspData, mspFcProcessCommand, mspFcProcessReply);

#ifdef USE_HDZERO_OSD
// Capture HDZero messages to determine if VTX is connected
hdzeroOsdSerialProcess(mspFcProcessCommand);
#endif
}

static void taskBatteryAlerts(timeUs_t currentTimeUs) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/interface/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ static void cliSerial(char *cmdline) {
ptr = nextArg(ptr);
if (ptr) {
val = atoi(ptr);
portConfig.functionMask = val & 0xFFFF;
portConfig.functionMask = val & 0xFFFFFFFF;
validArgumentCount++;
}
for (int i = 0; i < 4; i ++) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/interface/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static const char * const lookupTableRxSpi[] = {
"FLYSKY",
"FLYSKY_2A",
"KN",
"SFHSS",
"SFHSS",
"REDPINE"
};
#endif
Expand Down Expand Up @@ -355,7 +355,7 @@ static const char * const lookupTableRescueSanityType[] = {

#ifdef USE_MAX7456
static const char * const lookupTableVideoSystem[] = {
"AUTO", "PAL", "NTSC"
"AUTO", "PAL", "NTSC", "HDZERO"
};
#endif // USE_MAX7456

Expand Down
Loading

0 comments on commit c58ea02

Please sign in to comment.