1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-24 00:35:39 +03:00

MAX7456 code tidy

This commit is contained in:
Martin Budden 2017-11-23 05:31:53 +00:00
parent 07855aaf12
commit 7be1d09e50
8 changed files with 89 additions and 96 deletions

View file

@ -17,7 +17,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "platform.h"
@ -26,8 +25,6 @@
#include "build/debug.h"
#include "common/printf.h"
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
@ -41,7 +38,6 @@
#include "drivers/time.h"
#include "drivers/vcd.h"
#include "fc/config.h" // For systemConfig()
// DEBUG_MAX7456_SIGNAL
#define DEBUG_MAX7456_SIGNAL_MODEREG 0
@ -217,7 +213,7 @@ static uint8_t displayMemoryModeReg = 0;
static uint8_t hosRegValue; // HOS (Horizontal offset register) value
static uint8_t vosRegValue; // VOS (Vertical offset register) value
static bool max7456Lock = false;
static bool max7456Lock = false;
static bool fontIsLoading = false;
static IO_t max7456CsPin = IO_NONE;
@ -366,26 +362,26 @@ void max7456ReInit(void)
ENABLE_MAX7456;
switch (videoSignalCfg) {
case VIDEO_SYSTEM_PAL:
videoSignalReg = VIDEO_MODE_PAL | OSD_ENABLE;
break;
case VIDEO_SYSTEM_PAL:
videoSignalReg = VIDEO_MODE_PAL | OSD_ENABLE;
break;
case VIDEO_SYSTEM_NTSC:
case VIDEO_SYSTEM_NTSC:
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
break;
case VIDEO_SYSTEM_AUTO:
srdata = max7456Send(MAX7456ADD_STAT, 0x00);
if (VIN_IS_NTSC(srdata)) {
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
break;
case VIDEO_SYSTEM_AUTO:
srdata = max7456Send(MAX7456ADD_STAT, 0x00);
if (VIN_IS_NTSC(srdata)) {
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
} else if (VIN_IS_PAL(srdata)) {
videoSignalReg = VIDEO_MODE_PAL | OSD_ENABLE;
} else {
// No valid input signal, fallback to default (XXX NTSC for now)
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
}
break;
} else if (VIN_IS_PAL(srdata)) {
videoSignalReg = VIDEO_MODE_PAL | OSD_ENABLE;
} else {
// No valid input signal, fallback to default (XXX NTSC for now)
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
}
break;
}
if (videoSignalReg & VIDEO_MODE_PAL) { //PAL
@ -394,9 +390,9 @@ void max7456ReInit(void)
maxScreenSize = VIDEO_BUFFER_CHARS_NTSC;
}
/* Set all rows to same charactor black/white level. */
// Set all rows to same charactor black/white level
max7456Brightness(0, 2);
/* Re-enable MAX7456 (last function call disables it) */
// Re-enable MAX7456 (last function call disables it)
ENABLE_MAX7456;
// Make sure the Max7456 is enabled
@ -409,8 +405,7 @@ void max7456ReInit(void)
// Clear shadow to force redraw all screen in non-dma mode.
memset(shadowBuffer, 0, maxScreenSize);
if (firstInit)
{
if (firstInit) {
max7456RefreshAll();
firstInit = false;
}
@ -420,7 +415,7 @@ void max7456ReInit(void)
// Here we init only CS and try to init MAX for first time.
// Also detect device type (MAX v.s. AT)
void max7456Init(const vcdProfile_t *pVcdProfile)
void max7456Init(const vcdProfile_t *pVcdProfile, bool cpuOverclock)
{
max7456HardwareReset();
@ -452,7 +447,7 @@ void max7456Init(const vcdProfile_t *pVcdProfile)
break;
case MAX7456_CLOCK_CONFIG_OC:
max7456SpiClock = (systemConfig()->cpu_overclock && (max7456DeviceType == MAX7456_DEVICE_TYPE_MAX)) ? MAX7456_SPI_CLK * 2 : MAX7456_SPI_CLK;
max7456SpiClock = (cpuOverclock && (max7456DeviceType == MAX7456_DEVICE_TYPE_MAX)) ? MAX7456_SPI_CLK * 2 : MAX7456_SPI_CLK;
break;
case MAX7456_CLOCK_CONFIG_FULL:
@ -460,9 +455,11 @@ void max7456Init(const vcdProfile_t *pVcdProfile)
break;
}
DEBUG_SET(DEBUG_MAX7456_SPICLOCK, DEBUG_MAX7456_SPICLOCK_OVERCLOCK, systemConfig()->cpu_overclock);
DEBUG_SET(DEBUG_MAX7456_SPICLOCK, DEBUG_MAX7456_SPICLOCK_OVERCLOCK, cpuOverclock);
DEBUG_SET(DEBUG_MAX7456_SPICLOCK, DEBUG_MAX7456_SPICLOCK_DEVTYPE, max7456DeviceType);
DEBUG_SET(DEBUG_MAX7456_SPICLOCK, DEBUG_MAX7456_SPICLOCK_DIVISOR, max7456SpiClock);
#else
UNUSED(cpuOverclock);
#endif
spiSetDivisor(MAX7456_SPI_INSTANCE, max7456SpiClock);
@ -489,10 +486,11 @@ void max7456Init(const vcdProfile_t *pVcdProfile)
*/
void max7456Invert(bool invert)
{
if (invert)
if (invert) {
displayMemoryModeReg |= INVERT_PIXEL_COLOR;
else
} else {
displayMemoryModeReg &= ~INVERT_PIXEL_COLOR;
}
ENABLE_MAX7456;
max7456Send(MAX7456ADD_DMM, displayMemoryModeReg);
@ -507,7 +505,7 @@ void max7456Invert(bool invert)
*/
void max7456Brightness(uint8_t black, uint8_t white)
{
uint8_t reg = (black << 2) | (3 - white);
const uint8_t reg = (black << 2) | (3 - white);
ENABLE_MAX7456;
for (int i = MAX7456ADD_RB0; i <= MAX7456ADD_RB15; i++) {
@ -519,13 +517,11 @@ void max7456Brightness(uint8_t black, uint8_t white)
//just fill with spaces with some tricks
void max7456ClearScreen(void)
{
uint16_t x;
uint32_t *p = (uint32_t*)&screenBuffer[0];
for (x = 0; x < VIDEO_BUFFER_CHARS_PAL/4; x++)
p[x] = 0x20202020;
memset(screenBuffer, 0x20, VIDEO_BUFFER_CHARS_PAL);
}
uint8_t* max7456GetScreenBuffer(void) {
uint8_t* max7456GetScreenBuffer(void)
{
return screenBuffer;
}
@ -536,10 +532,11 @@ void max7456WriteChar(uint8_t x, uint8_t y, uint8_t c)
void max7456Write(uint8_t x, uint8_t y, const char *buff)
{
uint8_t i = 0;
for (i = 0; *(buff+i); i++)
if (x+i < CHARS_PER_LINE) // Do not write over screen
for (int i = 0; *(buff+i); i++) {
if (x+i < CHARS_PER_LINE) {// Do not write over screen
screenBuffer[y*CHARS_PER_LINE+x+i] = *(buff+i);
}
}
}
bool max7456DmaInProgress(void)
@ -553,13 +550,9 @@ bool max7456DmaInProgress(void)
void max7456DrawScreen(void)
{
uint8_t stallCheck;
uint8_t videoSense;
static uint32_t lastSigCheckMs = 0;
uint32_t nowMs;
static uint32_t videoDetectTimeMs = 0;
static uint16_t pos = 0;
int k = 0, buff_len=0;
static uint16_t reInitCount = 0;
@ -569,10 +562,10 @@ void max7456DrawScreen(void)
max7456Lock = true;
ENABLE_MAX7456;
stallCheck = max7456Send(MAX7456ADD_VM0|MAX7456ADD_READ, 0x00);
const uint8_t stallCheck = max7456Send(MAX7456ADD_VM0|MAX7456ADD_READ, 0x00);
DISABLE_MAX7456;
nowMs = millis();
const timeMs_t nowMs = millis();
if (stallCheck != videoSignalReg) {
max7456ReInit();
@ -583,7 +576,7 @@ void max7456DrawScreen(void)
// Adjust output format based on the current input format.
ENABLE_MAX7456;
videoSense = max7456Send(MAX7456ADD_STAT, 0x00);
const uint8_t videoSense = max7456Send(MAX7456ADD_STAT, 0x00);
DISABLE_MAX7456;
DEBUG_SET(DEBUG_MAX7456_SIGNAL, DEBUG_MAX7456_SIGNAL_MODEREG, videoSignalReg & VIDEO_MODE_MASK);
@ -612,7 +605,8 @@ void max7456DrawScreen(void)
//------------ end of (re)init-------------------------------------
for (k=0; k< MAX_CHARS2UPDATE; k++) {
int buff_len = 0;
for (int k = 0; k < MAX_CHARS2UPDATE; k++) {
if (screenBuffer[pos] != shadowBuffer[pos]) {
spiBuff[buff_len++] = MAX7456ADD_DMAH;
spiBuff[buff_len++] = pos >> 8;
@ -631,15 +625,13 @@ void max7456DrawScreen(void)
}
if (buff_len) {
#ifdef MAX7456_DMA_CHANNEL_TX
if (buff_len > 0)
max7456SendDma(spiBuff, NULL, buff_len);
#else
#ifdef MAX7456_DMA_CHANNEL_TX
max7456SendDma(spiBuff, NULL, buff_len);
#else
ENABLE_MAX7456;
for (k=0; k < buff_len; k++)
spiTransferByte(MAX7456_SPI_INSTANCE, spiBuff[k]);
spiTransfer(MAX7456_SPI_INSTANCE, spiBuff, NULL, buff_len);
DISABLE_MAX7456;
#endif // MAX7456_DMA_CHANNEL_TX
#endif // MAX7456_DMA_CHANNEL_TX
}
max7456Lock = false;
}
@ -651,17 +643,15 @@ void max7456RefreshAll(void)
{
if (!max7456Lock) {
#ifdef MAX7456_DMA_CHANNEL_TX
while (dmaTransactionInProgress);
while (dmaTransactionInProgress);
#endif
uint16_t xx;
max7456Lock = true;
ENABLE_MAX7456;
max7456Send(MAX7456ADD_DMAH, 0);
max7456Send(MAX7456ADD_DMAL, 0);
max7456Send(MAX7456ADD_DMM, displayMemoryModeReg | 1);
for (xx = 0; xx < maxScreenSize; ++xx)
{
for (int xx = 0; xx < maxScreenSize; ++xx) {
max7456Send(MAX7456ADD_DMDI, screenBuffer[xx]);
shadowBuffer[xx] = screenBuffer[xx];
}
@ -675,8 +665,6 @@ void max7456RefreshAll(void)
void max7456WriteNvm(uint8_t char_address, const uint8_t *font_data)
{
uint8_t x;
#ifdef MAX7456_DMA_CHANNEL_TX
while (dmaTransactionInProgress);
#endif
@ -690,7 +678,7 @@ void max7456WriteNvm(uint8_t char_address, const uint8_t *font_data)
max7456Send(MAX7456ADD_CMAH, char_address); // set start address high
for (x = 0; x < 54; x++) {
for (int x = 0; x < 54; x++) {
max7456Send(MAX7456ADD_CMAL, x); //set start address low
max7456Send(MAX7456ADD_CMDI, font_data[x]);
#ifdef LED0_TOGGLE
@ -734,4 +722,4 @@ void max7456HardwareReset(void)
#endif
}
#endif
#endif // USE_MAX7456