Skip to content

Commit

Permalink
LCD driver optimization (twice as fast?), ADC driver cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajjjjjjjj committed Nov 1, 2023
1 parent e33b334 commit 23b973e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 55 deletions.
39 changes: 11 additions & 28 deletions radio/src/targets/flysky/adc_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const uint8_t ana_mapping[NUM_ANALOGS] = {3, 2, 1, 0, 6, 7, 4, 5, 8, 9, 10};
#if NUM_PWMANALOGS > 0
#define FIRST_ANALOG_ADC (ANALOGS_PWM_ENABLED() ? NUM_PWMANALOGS : 0)
#define NUM_ANALOGS_ADC (ANALOGS_PWM_ENABLED() ? (NUM_ANALOGS - NUM_PWMANALOGS) : NUM_ANALOGS)
#elif defined(PCBX9E)
#define FIRST_ANALOG_ADC 0
#define NUM_ANALOGS_ADC 10
#define NUM_ANALOGS_ADC_EXT (NUM_ANALOGS - 10)
#elif defined (FLYSKY_GIMBAL)
#define FIRST_ANALOG_ADC 4
#define NUM_ANALOGS_ADC (NUM_ANALOGS - 4)
Expand All @@ -48,12 +44,9 @@ const uint8_t ana_mapping[NUM_ANALOGS] = {3, 2, 1, 0, 6, 7, 4, 5, 8, 9, 10};

uint16_t adcValues[NUM_ANALOGS] __DMA;

#define ADC_DMA_CHANNEL DMA1_Channel1
#define ADC_DMA_TC_FLAG DMA1_FLAG_TC1

static void adc_dma_arm(void)
{
ADC_StartOfConversion(ADC1);
ADC_StartOfConversion(ADC_MAIN);
}

void adcInit()
Expand All @@ -62,15 +55,6 @@ void adcInit()
// ADC CLOCK = 24 / 4 = 6MHz
RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2);

// enable ADC clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

// enable dma clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

// periph clock enable for port
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);

// init gpio
GPIO_InitTypeDef gpio_init;
GPIO_StructInit(&gpio_init);
Expand Down Expand Up @@ -108,30 +92,29 @@ void adcInit()
adc_init.ADC_ScanDirection = ADC_ScanDirection_Upward;

// load structure values to control and status registers
ADC_Init(ADC1, &adc_init);
ADC_Init(ADC_MAIN, &adc_init);

// configure each channel
ADC_ChannelConfig(ADC_MAIN,
#if !defined(FLYSKY_GIMBAL)
ADC_ChannelConfig(ADC1, ADC_Channel_0 | ADC_Channel_1 | ADC_Channel_2 | ADC_Channel_3 |
#else
ADC_ChannelConfig(ADC1,
ADC_Channel_0 | ADC_Channel_1 | ADC_Channel_2 | ADC_Channel_3 |
#endif
ADC_Channel_4 | ADC_Channel_5 | ADC_Channel_6 | ADC_Channel_7 | ADC_Channel_8 | ADC_Channel_9 | ADC_Channel_10, ADC_SampleTime_41_5Cycles);
ADC_Channel_4 | ADC_Channel_5 | ADC_Channel_6 | ADC_Channel_7 | ADC_Channel_8 | ADC_Channel_9 | ADC_Channel_10, ADC_SAMPTIME);


// enable ADC
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC_MAIN, ENABLE);

// enable DMA for ADC
ADC_DMACmd(ADC1, ENABLE);
ADC_DMACmd(ADC_MAIN, ENABLE);

// -- init dma --

DMA_InitTypeDef dma_init;
DMA_StructInit(&dma_init);

// reset DMA1 channe1 to default values
DMA_DeInit(ADC_DMA_CHANNEL);
DMA_DeInit(ADC_DMA_Channel);

// set up dma to convert 2 adc channels to two mem locations:
// channel will be used for memory to memory transfer
Expand All @@ -152,13 +135,13 @@ void adcInit()
// chunk of data to be transfered
dma_init.DMA_BufferSize = NUM_ANALOGS;
// source and destination start addresses
dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC_MAIN->DR;
dma_init.DMA_MemoryBaseAddr = (uint32_t)&adcValues[FIRST_ANALOG_ADC];
// send values to DMA registers
DMA_Init(ADC_DMA_CHANNEL, &dma_init);
DMA_Init(ADC_DMA_Channel, &dma_init);

// enable the DMA1 - Channel1
DMA_Cmd(ADC_DMA_CHANNEL, ENABLE);
DMA_Cmd(ADC_DMA_Channel, ENABLE);

// start conversion:
adc_dma_arm();
Expand Down
15 changes: 7 additions & 8 deletions radio/src/targets/flysky/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@

// ADC
#define ADC_MAIN ADC1
#define ADC_DMA DMA2
#define ADC_DMA_SxCR_CHSEL 0
#define ADC_DMA_Stream DMA2_Stream4
#define ADC_SET_DMA_FLAGS() ADC_DMA->HIFCR = (DMA_HIFCR_CTCIF4 | DMA_HIFCR_CHTIF4 | DMA_HIFCR_CTEIF4 | DMA_HIFCR_CDMEIF4 | DMA_HIFCR_CFEIF4)
#define ADC_TRANSFER_COMPLETE() (ADC_DMA->HISR & DMA_HISR_TCIF4)
#define ADC_SAMPTIME 2 // sample time = 28 cycles
#define ADC_DMA_Channel DMA1_Channel1
// #define ADC_SET_DMA_FLAGS() ADC_DMA->HIFCR = (DMA_HIFCR_CTCIF4 | DMA_HIFCR_CHTIF4 | DMA_HIFCR_CTEIF4 | DMA_HIFCR_CDMEIF4 | DMA_HIFCR_CFEIF4)
// #define ADC_TRANSFER_COMPLETE() (ADC_DMA->HISR & DMA_HISR_TCIF4)
#define ADC_DMA_TC_FLAG DMA1_FLAG_TC1
#define ADC_SAMPTIME ADC_SampleTime_41_5Cycles

#define ADC_RCC_AHB1Periph (RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_DMA1)
#define ADC_RCC_APB1Periph 0
Expand Down Expand Up @@ -581,8 +580,8 @@ F072 IRQs
#define MIXER_SCHEDULER_TIMER_IRQHandler TIM17_IRQHandler

//all used RCC goes here
#define RCC_AHB1_LIST (I2C_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph | LCD_RCC_AHB1Periph | KEYS_RCC_AHB1Periph | BUZZER_RCC_AHBPeriph | EXTMODULE_RCC_AHBPeriph | CRC_RCC_AHB1Periph | TELEMETRY_RCC_AHB1Periph | AUX_SERIAL_RCC_AHB1Periph | AUX2_SERIAL_RCC_AHB1Periph)
#define RCC_AHB1_LIST (I2C_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph | LCD_RCC_AHB1Periph | KEYS_RCC_AHB1Periph | BUZZER_RCC_AHBPeriph | EXTMODULE_RCC_AHBPeriph | CRC_RCC_AHB1Periph | TELEMETRY_RCC_AHB1Periph | AUX_SERIAL_RCC_AHB1Periph | AUX2_SERIAL_RCC_AHB1Periph | ADC_RCC_AHB1Periph)
#define RCC_APB1_LIST (I2C_RCC_APB1Periph | INTERRUPT_xMS_RCC_APB1Periph | TIMER_2MHz_RCC_APB1Periph | TELEMETRY_RCC_APB1Periph | BACKLIGHT_RCC_APB1Periph | RCC_APB1Periph_USB | AUX2_SERIAL_RCC_APB1Periph)
#define RCC_APB2_LIST (MIXER_SCHEDULER_TIMER_RCC_APB2Periph | PWM_RCC_APB2Periph | INTMODULE_RCC_APB2Periph | EXTMODULE_RCC_APB2Periph | AUX_SERIAL_RCC_APB2Periph)
#define RCC_APB2_LIST (MIXER_SCHEDULER_TIMER_RCC_APB2Periph | PWM_RCC_APB2Periph | INTMODULE_RCC_APB2Periph | EXTMODULE_RCC_APB2Periph | AUX_SERIAL_RCC_APB2Periph | ADC_RCC_APB2Periph)

#endif // _HAL_H_
31 changes: 12 additions & 19 deletions radio/src/targets/flysky/lcd_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,19 @@ const static unsigned char lcdInitSequence[] =

static void lcdSendCtl(uint8_t data) {
LCD_RS_LO(); //command
LCD_RW_LO();
LCD_CS_LO();
LCD_DATA_SET(data);
LCD_RD_HI();
LCD_RD_LO();
LCD_CS_HI();
}

static void lcdSendGFX(uint8_t data) {
LCD_RS_HI(); //gfx
LCD_RW_LO();
LCD_CS_LO();
// LCD_RS_HI(); //gfx, done on transaction start
LCD_DATA_SET(data);
LCD_RD_HI();
LCD_RD_LO();
LCD_CS_HI();
}

void lcdInit() {
//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOB, ENABLE);
//RCC_AHBPeriphClockCmd(LCD_RCC_AHB1Periph, ENABLE);
GPIO_InitTypeDef gpio_init;
// set all gpio directions to output
gpio_init.GPIO_Mode = GPIO_Mode_OUT;
Expand All @@ -159,8 +151,8 @@ void lcdInit() {
GPIO_Init(LCD_RD_CS_GPIO, &gpio_init);

LCD_RST_LO();
LCD_CS_HI();
LCD_RW_LO();
LCD_CS_LO(); // Enable access to LCD
LCD_RW_LO(); // Enable write mode
LCD_RD_HI();
LCD_RS_HI();
lcdReset();
Expand All @@ -169,18 +161,19 @@ void lcdInit() {
}

void lcdRefresh() {
uint8_t *p = displayBuf;
int page = 0xB0;
do {
uint8_t *p = displayBuf;
int page = 0xB0;
do {
uint8_t line = LCD_W;
lcdSendCtl(page); // page selection
lcdSendCtl(4); // start at line 4 -> controller is 132 lines while lcd 128
lcdSendCtl(0x10u);// column address 0
do {
lcdSendGFX(*p++);
} while (--line);
++page;
} while (page < 0xB8);
LCD_RS_HI(); // start gfx data
do {
lcdSendGFX(*p++);
} while (--line);
++page;
} while (page < 0xB8);
}

void lcdOff() {
Expand Down

0 comments on commit 23b973e

Please sign in to comment.