From a12ab13462cea5b7ab4610180364d9abca73018f Mon Sep 17 00:00:00 2001 From: Bruce Luckcuck Date: Sat, 9 Nov 2019 14:26:08 -0500 Subject: [PATCH] Prevent font upload if max7456 is not detected Prevents wedge if a font upload is attempted but the max7456 device wasn't detected during initialization. --- src/main/drivers/max7456.c | 19 +++++++++++++++---- src/main/drivers/max7456.h | 3 ++- src/main/msp/msp.c | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/drivers/max7456.c b/src/main/drivers/max7456.c index 1bd4bb39c1..1a52e5a1e9 100644 --- a/src/main/drivers/max7456.c +++ b/src/main/drivers/max7456.c @@ -193,6 +193,7 @@ static displayPortLayer_e activeLayer = DISPLAYPORT_LAYER_FOREGROUND; busDevice_t max7456BusDevice; busDevice_t *busdev = &max7456BusDevice; +static bool max7456DeviceDetected = false; static uint16_t max7456SpiClock = MAX7456_SPI_CLK; uint16_t maxScreenSize = VIDEO_BUFFER_CHARS_PAL; @@ -450,6 +451,8 @@ void max7456PreInit(const max7456Config_t *max7456Config) bool max7456Init(const max7456Config_t *max7456Config, const vcdProfile_t *pVcdProfile, bool cpuOverclock) { + max7456DeviceDetected = false; + // initialize all layers for (unsigned i = 0; i < MAX7456_SUPPORTED_LAYER_COUNT; i++) { max7456ClearLayer(i); @@ -484,15 +487,15 @@ bool max7456Init(const max7456Config_t *max7456Config, const vcdProfile_t *pVcdP uint8_t osdm = max7456Send(MAX7456ADD_OSDM|MAX7456ADD_READ, 0xff); + __spiBusTransactionEnd(busdev); + if (osdm != 0x1B) { IOConfigGPIO(busdev->busdev_u.spi.csnPin, IOCFG_IPU); return false; } - __spiBusTransactionEnd(busdev); - // At this point, we can claim the ownership of the CS pin - + max7456DeviceDetected = true; IOInit(busdev->busdev_u.spi.csnPin, OWNER_OSD_CS, 0); // Detect device type by writing and reading CA[8] bit at CMAL[6]. @@ -822,8 +825,11 @@ void max7456RefreshAll(void) max7456DrawScreenSlow(); } -void max7456WriteNvm(uint8_t char_address, const uint8_t *font_data) +bool max7456WriteNvm(uint8_t char_address, const uint8_t *font_data) { + if (!max7456DeviceDetected) { + return false; + } #ifdef MAX7456_DMA_CHANNEL_TX while (dmaTransactionInProgress); #endif @@ -854,6 +860,7 @@ void max7456WriteNvm(uint8_t char_address, const uint8_t *font_data) while ((max7456Send(MAX7456ADD_STAT, 0x00) & STAT_NVR_BUSY) != 0x00); __spiBusTransactionEnd(busdev); + return true; } #ifdef MAX7456_NRST_PIN @@ -877,4 +884,8 @@ void max7456HardwareReset(void) #endif } +bool max7456IsDeviceDetected(void) +{ + return max7456DeviceDetected; +} #endif // USE_MAX7456 diff --git a/src/main/drivers/max7456.h b/src/main/drivers/max7456.h index d1fc867468..a92d364392 100644 --- a/src/main/drivers/max7456.h +++ b/src/main/drivers/max7456.h @@ -37,7 +37,7 @@ bool max7456Init(const struct max7456Config_s *max7456Config, const struct vc void max7456Invert(bool invert); void max7456Brightness(uint8_t black, uint8_t white); void max7456DrawScreen(void); -void max7456WriteNvm(uint8_t char_address, const uint8_t *font_data); +bool max7456WriteNvm(uint8_t char_address, const uint8_t *font_data); uint8_t max7456GetRowsCount(void); void max7456Write(uint8_t x, uint8_t y, const char *buff); void max7456WriteChar(uint8_t x, uint8_t y, uint8_t c); @@ -48,3 +48,4 @@ bool max7456BuffersSynced(void); bool max7456LayerSupported(displayPortLayer_e layer); bool max7456LayerSelect(displayPortLayer_e layer); bool max7456LayerCopy(displayPortLayer_e destLayer, displayPortLayer_e sourceLayer); +bool max7456IsDeviceDetected(void); diff --git a/src/main/msp/msp.c b/src/main/msp/msp.c index b9cf361895..efdb41d175 100644 --- a/src/main/msp/msp.c +++ b/src/main/msp/msp.c @@ -44,6 +44,7 @@ #include "common/streambuf.h" #include "common/utils.h" +#include "config/config.h" #include "config/config_eeprom.h" #include "config/feature.h" @@ -67,7 +68,6 @@ #include "drivers/vtx_table.h" #include "fc/board_info.h" -#include "config/config.h" #include "fc/controlrate_profile.h" #include "fc/core.h" #include "fc/rc.h" @@ -110,6 +110,7 @@ #include "pg/beeper.h" #include "pg/board.h" #include "pg/gyrodev.h" +#include "pg/max7456.h" #include "pg/motor.h" #include "pg/rx.h" #include "pg/rx_spi.h" @@ -796,13 +797,19 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce #define OSD_FLAGS_RESERVED_1 (1 << 2) #define OSD_FLAGS_RESERVED_2 (1 << 3) #define OSD_FLAGS_OSD_HARDWARE_MAX_7456 (1 << 4) +#define OSD_FLAGS_MAX7456_DETECTED (1 << 5) uint8_t osdFlags = 0; #if defined(USE_OSD) osdFlags |= OSD_FLAGS_OSD_FEATURE; #endif #ifdef USE_MAX7456 - osdFlags |= OSD_FLAGS_OSD_HARDWARE_MAX_7456; + if (max7456Config()->csTag && max7456Config()->spiDevice) { + osdFlags |= OSD_FLAGS_OSD_HARDWARE_MAX_7456; + if (max7456IsDeviceDetected()) { + osdFlags |= OSD_FLAGS_MAX7456_DETECTED; + } + } #endif sbufWriteU8(dst, osdFlags); @@ -3281,7 +3288,9 @@ static mspResult_e mspCommonProcessInCommand(mspDescriptor_t srcDesc, uint8_t cm font_data[i] = sbufReadU8(src); } // !!TODO - replace this with a device independent implementation - max7456WriteNvm(addr, font_data); + if (!max7456WriteNvm(addr, font_data)) { + return MSP_RESULT_ERROR; + } } break; #else