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

Handle SPI clock for CPU over clocked environment

This commit is contained in:
jflyper 2017-09-15 11:10:28 +09:00
parent 07d5990395
commit 5a7ed7b686
6 changed files with 119 additions and 2 deletions

View file

@ -24,8 +24,13 @@
#ifdef USE_MAX7456
#include "build/debug.h"
#include "common/printf.h"
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
#include "drivers/bus_spi.h"
#include "drivers/dma.h"
#include "drivers/io.h"
@ -36,6 +41,8 @@
#include "drivers/time.h"
#include "drivers/vcd.h"
#include "fc/config.h" // For systemConfig()
// VM0 bits
#define VIDEO_BUFFER_DISABLE 0x01
#define MAX7456_RESET 0x02
@ -148,6 +155,10 @@
#define NVM_RAM_SIZE 54
#define WRITE_NVR 0xA0
// Device type
#define MAX7456_DEVICE_TYPE_MAX 0
#define MAX7456_DEVICE_TYPE_AT 1
#define CHARS_PER_LINE 30 // XXX Should be related to VIDEO_BUFFER_CHARS_*?
// On shared SPI buss we want to change clock for OSD chip and restore for other devices.
@ -164,6 +175,12 @@
#define DISABLE_MAX7456 IOHi(max7456CsPin)
#endif
#ifndef MAX7456_SPI_CLK
#define MAX7456_SPI_CLK (SPI_CLOCK_STANDARD)
#endif
static uint16_t max7456SpiClock = MAX7456_SPI_CLK;
uint16_t maxScreenSize = VIDEO_BUFFER_CHARS_PAL;
// We write everything in screenBuffer and then compare
@ -193,6 +210,15 @@ static bool max7456Lock = false;
static bool fontIsLoading = false;
static IO_t max7456CsPin = IO_NONE;
static uint8_t max7456DeviceType;
PG_REGISTER_WITH_RESET_TEMPLATE(max7456Config_t, max7456Config, PG_MAX7456_CONFIG, 0);
PG_RESET_TEMPLATE(max7456Config_t, max7456Config,
.clockConfig = MAX7456_CLOCK_CONFIG_OC, // SPI clock based on device type and overclock state
);
static uint8_t max7456Send(uint8_t add, uint8_t data)
{
@ -387,6 +413,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)
{
@ -399,7 +426,43 @@ void max7456Init(const vcdProfile_t *pVcdProfile)
IOConfigGPIO(max7456CsPin, SPI_IO_CS_CFG);
IOHi(max7456CsPin);
spiSetDivisor(MAX7456_SPI_INSTANCE, SPI_CLOCK_STANDARD);
// Detect device type by writing and reading CA[8] bit at CMAL[6].
// Do this at half the speed for safety.
spiSetDivisor(MAX7456_SPI_INSTANCE, MAX7456_SPI_CLK * 2);
max7456Send(MAX7456ADD_CMAL, (1 << 6)); // CA[8] bit
if (max7456Send(MAX7456ADD_CMAL|MAX7456ADD_READ, 0xff) & (1 << 6)) {
max7456DeviceType = MAX7456_DEVICE_TYPE_AT;
} else {
max7456DeviceType = MAX7456_DEVICE_TYPE_MAX;
}
#if defined(STM32F4) && !defined(DISABLE_OVERCLOCK)
// Determine SPI clock divisor based on config and the device type.
switch (max7456Config()->clockConfig) {
case MAX7456_CLOCK_CONFIG_HALF:
max7456SpiClock = MAX7456_SPI_CLK * 2;
break;
case MAX7456_CLOCK_CONFIG_OC:
max7456SpiClock = (systemConfig()->cpu_overclock && (max7456DeviceType == MAX7456_DEVICE_TYPE_MAX)) ? MAX7456_SPI_CLK * 2 : MAX7456_SPI_CLK;
break;
case MAX7456_CLOCK_CONFIG_FULL:
max7456SpiClock = MAX7456_SPI_CLK;
break;
}
// XXX Disable for production
debug[0] = systemConfig()->cpu_overclock;
debug[1] = max7456DeviceType;
debug[2] = max7456SpiClock;
#endif
spiSetDivisor(MAX7456_SPI_INSTANCE, max7456SpiClock);
// force soft reset on Max7456
ENABLE_MAX7456;
max7456Send(MAX7456ADD_VM0, MAX7456_RESET);