mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 16:25:31 +03:00
Refactor OSD code
This commit is contained in:
parent
bec109d366
commit
aea5666cfb
6 changed files with 1361 additions and 1438 deletions
|
@ -37,7 +37,7 @@
|
|||
#include "max7456.h"
|
||||
#include "max7456_symbols.h"
|
||||
|
||||
//on shared SPI buss we want to change clock for OSD chip and restore for other devices
|
||||
// On shared SPI bus we want to change clock for OSD chip and restore for other devices
|
||||
#ifdef MAX7456_SPI_CLK
|
||||
#define ENABLE_MAX7456 {spiSetDivisor(MAX7456_SPI_INSTANCE, MAX7456_SPI_CLK);IOLo(max7456CsPin);}
|
||||
#else
|
||||
|
@ -50,30 +50,29 @@
|
|||
#define DISABLE_MAX7456 IOHi(max7456CsPin)
|
||||
#endif
|
||||
|
||||
uint16_t max_screen_size = VIDEO_BUFFER_CHARS_PAL;
|
||||
uint16_t maxScreenSize = VIDEO_BUFFER_CHARS_PAL;
|
||||
// we write everything in SCREEN_BUFFER and then comapre
|
||||
// SCREEN_BUFFER with SHADOW_BUFFER to upgrade only changed chars
|
||||
// this solution is faster then redraw all screen
|
||||
|
||||
static uint8_t SCREEN_BUFFER[VIDEO_BUFFER_CHARS_PAL+40]; //for faster writes we use memcpy so we need some space to don't overwrite buffer
|
||||
static uint8_t SCREEN_BUFFER[VIDEO_BUFFER_CHARS_PAL + 40]; //for faster writes we use memcpy so we need some space to don't overwrite buffer
|
||||
static uint8_t SHADOW_BUFFER[VIDEO_BUFFER_CHARS_PAL];
|
||||
|
||||
//max chars to update in one idle
|
||||
#define MAX_CHARS2UPDATE 100
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
volatile uint8_t dma_transaction_in_progress = 0;
|
||||
#endif
|
||||
volatile uint8_t dmaTxInProgress = 0;
|
||||
#endif // MAX7456_DMA_CHANNEL_TX
|
||||
|
||||
static uint8_t spi_buff[MAX_CHARS2UPDATE*6];
|
||||
static uint8_t spiBuffer[MAX_CHARS2UPDATE * 6];
|
||||
|
||||
static uint8_t video_signal_cfg = 0;
|
||||
static uint8_t video_signal_reg = VIDEO_MODE_PAL | OSD_ENABLE; //PAL by default
|
||||
static uint8_t max7456_lock = 0;
|
||||
static IO_t max7456CsPin = IO_NONE;
|
||||
static bool fontIsLoading = false;
|
||||
static uint8_t videoSignalCfg = 0;
|
||||
static uint8_t videoSignalReg = VIDEO_MODE_PAL | OSD_ENABLE; //PAL by default
|
||||
static uint8_t max7456Lock = 0;
|
||||
static IO_t max7456CsPin = IO_NONE;
|
||||
static bool fontIsLoading = false;
|
||||
|
||||
static uint8_t max7456_send(uint8_t add, uint8_t data)
|
||||
{
|
||||
static uint8_t max7456_send(uint8_t add, uint8_t data) {
|
||||
spiTransferByte(MAX7456_SPI_INSTANCE, add);
|
||||
return spiTransferByte(MAX7456_SPI_INSTANCE, data);
|
||||
}
|
||||
|
@ -83,15 +82,15 @@ static void max7456_send_dma(void* tx_buffer, void* rx_buffer, uint16_t buffer_s
|
|||
DMA_InitTypeDef DMA_InitStructure;
|
||||
#ifdef MAX7456_DMA_CHANNEL_RX
|
||||
static uint16_t dummy[] = {0xffff};
|
||||
#else
|
||||
#else // MAX7456_DMA_CHANNEL_RX
|
||||
UNUSED(rx_buffer);
|
||||
#endif
|
||||
while (dma_transaction_in_progress); // Wait for prev DMA transaction
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
while (dmaTxInProgress); // Wait for prev DMA transaction
|
||||
|
||||
DMA_DeInit(MAX7456_DMA_CHANNEL_TX);
|
||||
#ifdef MAX7456_DMA_CHANNEL_RX
|
||||
DMA_DeInit(MAX7456_DMA_CHANNEL_RX);
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
|
||||
// Common to both channels
|
||||
DMA_StructInit(&DMA_InitStructure);
|
||||
|
@ -108,24 +107,24 @@ static void max7456_send_dma(void* tx_buffer, void* rx_buffer, uint16_t buffer_s
|
|||
#ifdef STM32F4
|
||||
DMA_InitStructure.DMA_Memory0BaseAddr = rx_buffer ? (uint32_t)rx_buffer : (uint32_t)(dummy);
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
|
||||
#else
|
||||
#else // STM32F4
|
||||
DMA_InitStructure.DMA_MemoryBaseAddr = rx_buffer ? (uint32_t)rx_buffer : (uint32_t)(dummy);
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
||||
#endif
|
||||
#endif // STM32F4
|
||||
DMA_InitStructure.DMA_MemoryInc = rx_buffer ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
|
||||
|
||||
DMA_Init(MAX7456_DMA_CHANNEL_RX, &DMA_InitStructure);
|
||||
DMA_Cmd(MAX7456_DMA_CHANNEL_RX, ENABLE);
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
// Tx channel
|
||||
|
||||
#ifdef STM32F4
|
||||
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)tx_buffer; //max7456_screen;
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
|
||||
#else
|
||||
#else // STM32F4
|
||||
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)tx_buffer; //max7456_screen;
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
||||
#endif
|
||||
#endif // STM32F4
|
||||
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
||||
|
||||
DMA_Init(MAX7456_DMA_CHANNEL_TX, &DMA_InitStructure);
|
||||
|
@ -133,18 +132,18 @@ static void max7456_send_dma(void* tx_buffer, void* rx_buffer, uint16_t buffer_s
|
|||
|
||||
#ifdef MAX7456_DMA_CHANNEL_RX
|
||||
DMA_ITConfig(MAX7456_DMA_CHANNEL_RX, DMA_IT_TC, ENABLE);
|
||||
#else
|
||||
#else // MAX7456_DMA_CHANNEL_RX
|
||||
DMA_ITConfig(MAX7456_DMA_CHANNEL_TX, DMA_IT_TC, ENABLE);
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
|
||||
// Enable SPI TX/RX request
|
||||
ENABLE_MAX7456;
|
||||
dma_transaction_in_progress = 1;
|
||||
dmaTxInProgress = 1;
|
||||
|
||||
SPI_I2S_DMACmd(MAX7456_SPI_INSTANCE,
|
||||
#ifdef MAX7456_DMA_CHANNEL_RX
|
||||
SPI_I2S_DMAReq_Rx |
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
SPI_I2S_DMAReq_Tx, ENABLE);
|
||||
}
|
||||
|
||||
|
@ -153,7 +152,7 @@ void max7456_dma_irq_handler(dmaChannelDescriptor_t* descriptor) {
|
|||
if (DMA_GET_FLAG_STATUS(descriptor, DMA_IT_TCIF)) {
|
||||
#ifdef MAX7456_DMA_CHANNEL_RX
|
||||
DMA_Cmd(MAX7456_DMA_CHANNEL_RX, DISABLE);
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
// make sure spi dmd transfer is complete
|
||||
while (SPI_I2S_GetFlagStatus (MAX7456_SPI_INSTANCE, SPI_I2S_FLAG_TXE) == RESET) {};
|
||||
while (SPI_I2S_GetFlagStatus (MAX7456_SPI_INSTANCE, SPI_I2S_FLAG_BSY) == SET) {};
|
||||
|
@ -171,11 +170,11 @@ void max7456_dma_irq_handler(dmaChannelDescriptor_t* descriptor) {
|
|||
SPI_I2S_DMACmd(MAX7456_SPI_INSTANCE,
|
||||
#ifdef MAX7456_DMA_CHANNEL_RX
|
||||
SPI_I2S_DMAReq_Rx |
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_RX
|
||||
SPI_I2S_DMAReq_Tx, DISABLE);
|
||||
|
||||
DISABLE_MAX7456;
|
||||
dma_transaction_in_progress = 0;
|
||||
dmaTxInProgress = 0;
|
||||
}
|
||||
|
||||
if (DMA_GET_FLAG_STATUS(descriptor, DMA_IT_HTIF)) {
|
||||
|
@ -185,12 +184,10 @@ void max7456_dma_irq_handler(dmaChannelDescriptor_t* descriptor) {
|
|||
DMA_CLEAR_FLAG(descriptor, DMA_IT_TEIF);
|
||||
}
|
||||
}
|
||||
#endif // MAX7456_DMA_CHANNEL_TX
|
||||
|
||||
#endif
|
||||
|
||||
uint8_t max7456_get_rows_count(void)
|
||||
{
|
||||
if (video_signal_reg & VIDEO_MODE_PAL)
|
||||
uint8_t max7456_get_rows_count(void) {
|
||||
if (videoSignalReg & VIDEO_MODE_PAL)
|
||||
return VIDEO_LINES_PAL;
|
||||
|
||||
return VIDEO_LINES_NTSC;
|
||||
|
@ -198,8 +195,7 @@ uint8_t max7456_get_rows_count(void)
|
|||
|
||||
//because MAX7456 need some time to detect video system etc. we need to wait for a while to initialize it at startup
|
||||
//and in case of restart we need to reinitialize chip
|
||||
void max7456_init2(void)
|
||||
{
|
||||
void max7456_init2(void) {
|
||||
uint8_t max_screen_rows;
|
||||
uint8_t srdata = 0;
|
||||
uint16_t x;
|
||||
|
@ -211,24 +207,24 @@ void max7456_init2(void)
|
|||
|
||||
ENABLE_MAX7456;
|
||||
|
||||
switch(video_signal_cfg) {
|
||||
switch(videoSignalCfg) {
|
||||
case PAL:
|
||||
video_signal_reg = VIDEO_MODE_PAL | OSD_ENABLE;
|
||||
videoSignalReg = VIDEO_MODE_PAL | OSD_ENABLE;
|
||||
break;
|
||||
case NTSC:
|
||||
video_signal_reg = VIDEO_MODE_NTSC | OSD_ENABLE;
|
||||
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
|
||||
break;
|
||||
default:
|
||||
srdata = max7456_send(MAX7456ADD_STAT, 0x00);
|
||||
if ((0x02 & srdata) == 0x02)
|
||||
video_signal_reg = VIDEO_MODE_NTSC | OSD_ENABLE;
|
||||
videoSignalReg = VIDEO_MODE_NTSC | OSD_ENABLE;
|
||||
}
|
||||
|
||||
if (video_signal_reg & VIDEO_MODE_PAL) { //PAL
|
||||
max_screen_size = VIDEO_BUFFER_CHARS_PAL;
|
||||
if (videoSignalReg & VIDEO_MODE_PAL) { //PAL
|
||||
maxScreenSize = VIDEO_BUFFER_CHARS_PAL;
|
||||
max_screen_rows = VIDEO_LINES_PAL;
|
||||
} else { // NTSC
|
||||
max_screen_size = VIDEO_BUFFER_CHARS_NTSC;
|
||||
maxScreenSize = VIDEO_BUFFER_CHARS_NTSC;
|
||||
max_screen_rows = VIDEO_LINES_NTSC;
|
||||
}
|
||||
|
||||
|
@ -238,26 +234,23 @@ void max7456_init2(void)
|
|||
}
|
||||
|
||||
// make sure the Max7456 is enabled
|
||||
max7456_send(VM0_REG, video_signal_reg);
|
||||
max7456_send(VM0_REG, videoSignalReg);
|
||||
max7456_send(DMM_REG, CLEAR_DISPLAY);
|
||||
DISABLE_MAX7456;
|
||||
|
||||
//clear shadow to force redraw all screen in non-dma mode
|
||||
memset(SHADOW_BUFFER, 0, max_screen_size);
|
||||
if (first_init)
|
||||
{
|
||||
memset(SHADOW_BUFFER, 0, maxScreenSize);
|
||||
if (first_init) {
|
||||
max7456_refresh_all();
|
||||
first_init = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//here we init only CS and try to init MAX for first time
|
||||
void max7456_init(uint8_t video_system)
|
||||
{
|
||||
void max7456_init(uint8_t video_system) {
|
||||
#ifdef MAX7456_SPI_CS_PIN
|
||||
max7456CsPin = IOGetByTag(IO_TAG(MAX7456_SPI_CS_PIN));
|
||||
#endif
|
||||
#endif // MAX7456_SPI_CS_PIN
|
||||
IOInit(max7456CsPin, OWNER_OSD, RESOURCE_SPI_CS, 0);
|
||||
IOConfigGPIO(max7456CsPin, SPI_IO_CS_CFG);
|
||||
|
||||
|
@ -266,115 +259,107 @@ void max7456_init(uint8_t video_system)
|
|||
ENABLE_MAX7456;
|
||||
max7456_send(VM0_REG, MAX7456_RESET);
|
||||
DISABLE_MAX7456;
|
||||
video_signal_cfg = video_system;
|
||||
videoSignalCfg = video_system;
|
||||
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
dmaSetHandler(MAX7456_DMA_IRQ_HANDLER_ID, max7456_dma_irq_handler, NVIC_PRIO_MAX7456_DMA, 0);
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_TX
|
||||
//real init will be made letter when driver idle detect
|
||||
}
|
||||
|
||||
//just fill with spaces with some tricks
|
||||
void max7456_clear_screen(void)
|
||||
{
|
||||
uint16_t x;
|
||||
uint32_t *p = (uint32_t*)&SCREEN_BUFFER[0];
|
||||
for (x = 0; x < VIDEO_BUFFER_CHARS_PAL/4; x++)
|
||||
p[x] = 0x20202020;
|
||||
void max7456_clear_screen(void) {
|
||||
uint16_t x;
|
||||
uint32_t *p = (uint32_t*)&SCREEN_BUFFER[0];
|
||||
for (x = 0; x < VIDEO_BUFFER_CHARS_PAL/4; x++)
|
||||
p[x] = 0x20202020;
|
||||
}
|
||||
|
||||
uint8_t* max7456_get_screen_buffer(void) {
|
||||
return SCREEN_BUFFER;
|
||||
}
|
||||
|
||||
void max7456_write_char(uint8_t x, uint8_t y, uint8_t c)
|
||||
{
|
||||
SCREEN_BUFFER[y*30+x] = c;
|
||||
void max7456_write_char(uint8_t x, uint8_t y, uint8_t c) {
|
||||
SCREEN_BUFFER[y*30+x] = c;
|
||||
}
|
||||
|
||||
void max7456_write(uint8_t x, uint8_t y, char *buff)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
for (i = 0; *(buff+i); i++)
|
||||
if (x+i < 30) //do not write over screen
|
||||
SCREEN_BUFFER[y*30+x+i] = *(buff+i);
|
||||
void max7456_write(uint8_t x, uint8_t y, char *buff) {
|
||||
uint8_t i = 0;
|
||||
for (i = 0; *(buff+i); i++)
|
||||
if (x+i < 30) //do not write over screen
|
||||
SCREEN_BUFFER[y*30+x+i] = *(buff+i);
|
||||
}
|
||||
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
uint8_t max7456_dma_in_progres(void)
|
||||
{
|
||||
return dma_transaction_in_progress;
|
||||
uint8_t max7456_dma_in_progres(void) {
|
||||
return dmaTxInProgress;
|
||||
}
|
||||
#endif
|
||||
#endif // MAX7456_DMA_CHANNEL_TX
|
||||
|
||||
void max7456_draw_screen(void) {
|
||||
uint8_t check;
|
||||
static uint16_t pos = 0;
|
||||
int k = 0, buff_len=0;
|
||||
|
||||
if (!max7456_lock && !fontIsLoading) {
|
||||
if (!max7456Lock && !fontIsLoading) {
|
||||
//-----------------detect MAX7456 fail, or initialize it at startup when it is ready--------
|
||||
max7456_lock = 1;
|
||||
max7456Lock = 1;
|
||||
ENABLE_MAX7456;
|
||||
check = max7456_send(VM0_REG | 0x80, 0x00);
|
||||
DISABLE_MAX7456;
|
||||
|
||||
if ( check != video_signal_reg )
|
||||
if ( check != videoSignalReg )
|
||||
max7456_init2();
|
||||
|
||||
//------------ end of (re)init-------------------------------------
|
||||
|
||||
for (k=0; k< MAX_CHARS2UPDATE; k++)
|
||||
{
|
||||
if (SCREEN_BUFFER[pos] != SHADOW_BUFFER[pos])
|
||||
{
|
||||
spi_buff[buff_len++] = MAX7456ADD_DMAH;
|
||||
spi_buff[buff_len++] = pos >> 8;
|
||||
spi_buff[buff_len++] = MAX7456ADD_DMAL;
|
||||
spi_buff[buff_len++] = pos & 0xff;
|
||||
spi_buff[buff_len++] = MAX7456ADD_DMDI;
|
||||
spi_buff[buff_len++] = SCREEN_BUFFER[pos];
|
||||
for (k=0; k< MAX_CHARS2UPDATE; k++) {
|
||||
if (SCREEN_BUFFER[pos] != SHADOW_BUFFER[pos]) {
|
||||
spiBuffer[buff_len++] = MAX7456ADD_DMAH;
|
||||
spiBuffer[buff_len++] = pos >> 8;
|
||||
spiBuffer[buff_len++] = MAX7456ADD_DMAL;
|
||||
spiBuffer[buff_len++] = pos & 0xff;
|
||||
spiBuffer[buff_len++] = MAX7456ADD_DMDI;
|
||||
spiBuffer[buff_len++] = SCREEN_BUFFER[pos];
|
||||
SHADOW_BUFFER[pos] = SCREEN_BUFFER[pos];
|
||||
k++;
|
||||
}
|
||||
|
||||
if (++pos >= max_screen_size) {
|
||||
if (++pos >= maxScreenSize) {
|
||||
pos = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (buff_len) {
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
if (buff_len > 0)
|
||||
max7456_send_dma(spi_buff, NULL, buff_len);
|
||||
#else
|
||||
max7456_send_dma(spiBuffer, NULL, buff_len);
|
||||
#else
|
||||
ENABLE_MAX7456;
|
||||
for (k=0; k < buff_len; k++)
|
||||
spiTransferByte(MAX7456_SPI_INSTANCE, spi_buff[k]);
|
||||
spiTransferByte(MAX7456_SPI_INSTANCE, spiBuffer[k]);
|
||||
DISABLE_MAX7456;
|
||||
#endif // MAX7456_DMA_CHANNEL_TX
|
||||
#endif // MAX7456_DMA_CHANNEL_TX
|
||||
}
|
||||
max7456_lock = 0;
|
||||
max7456Lock = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// this funcktion refresh all and should not be used when copter is armed
|
||||
void max7456_refresh_all(void)
|
||||
{
|
||||
if (!max7456_lock) {
|
||||
void max7456_refresh_all(void) {
|
||||
if (!max7456Lock) {
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
while (dma_transaction_in_progress);
|
||||
while (dmaTxInProgress);
|
||||
#endif
|
||||
uint16_t xx;
|
||||
max7456_lock = 1;
|
||||
max7456Lock = 1;
|
||||
ENABLE_MAX7456;
|
||||
max7456_send(MAX7456ADD_DMAH, 0);
|
||||
max7456_send(MAX7456ADD_DMAL, 0);
|
||||
max7456_send(MAX7456ADD_DMM, 1);
|
||||
|
||||
for (xx = 0; xx < max_screen_size; ++xx)
|
||||
{
|
||||
for (xx = 0; xx < maxScreenSize; ++xx) {
|
||||
max7456_send(MAX7456ADD_DMDI, SCREEN_BUFFER[xx]);
|
||||
SHADOW_BUFFER[xx] = SCREEN_BUFFER[xx];
|
||||
}
|
||||
|
@ -382,7 +367,7 @@ void max7456_refresh_all(void)
|
|||
max7456_send(MAX7456ADD_DMDI, 0xFF);
|
||||
max7456_send(MAX7456ADD_DMM, 0);
|
||||
DISABLE_MAX7456;
|
||||
max7456_lock = 0;
|
||||
max7456Lock = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -390,10 +375,10 @@ void max7456_write_nvm(uint8_t char_address, uint8_t *font_data) {
|
|||
uint8_t x;
|
||||
|
||||
#ifdef MAX7456_DMA_CHANNEL_TX
|
||||
while (dma_transaction_in_progress);
|
||||
while (dmaTxInProgress);
|
||||
#endif
|
||||
while (max7456_lock);
|
||||
max7456_lock = 1;
|
||||
while (max7456Lock);
|
||||
max7456Lock = 1;
|
||||
|
||||
ENABLE_MAX7456;
|
||||
// disable display
|
||||
|
@ -407,9 +392,9 @@ void max7456_write_nvm(uint8_t char_address, uint8_t *font_data) {
|
|||
max7456_send(MAX7456ADD_CMDI, font_data[x]);
|
||||
#ifdef LED0_TOGGLE
|
||||
LED0_TOGGLE;
|
||||
#else
|
||||
#else // LED0_TOGGLE
|
||||
LED1_TOGGLE;
|
||||
#endif
|
||||
#endif // LED0_TOGGLE
|
||||
}
|
||||
|
||||
// transfer 54 bytes from shadow ram to NVM
|
||||
|
@ -420,8 +405,7 @@ void max7456_write_nvm(uint8_t char_address, uint8_t *font_data) {
|
|||
|
||||
DISABLE_MAX7456;
|
||||
|
||||
max7456_lock = 0;
|
||||
max7456Lock = 0;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif // USE_MAX7456
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue