Skip to content

Commit

Permalink
Improved ADC performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Vostrenkov committed Mar 7, 2020
1 parent 25b2c54 commit 09eb58d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
4 changes: 1 addition & 3 deletions Inc/analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
#define DEADBAND_BUF_SIZE 8
#define DEADBAND_HOLD_VALUE 2000

#define ADC_PERIOD_MS 2
#define SENSORS_PERIOD_MS 2

#define PREBUF_SIZE 8

extern tle_t sensors[MAX_AXIS_NUM];

Expand Down
2 changes: 1 addition & 1 deletion Inc/common_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

//#define DEBUG

#define FIRMWARE_VERSION 0x1413 // v1.4.1b3
#define FIRMWARE_VERSION 0x1414 // v1.4.1b4
#define USED_PINS_NUM 30 // constant for BluePill and BlackPill boards
#define MAX_AXIS_NUM 8 // max 8
#define MAX_BUTTONS_NUM 128 // power of 2, max 128
Expand Down
2 changes: 1 addition & 1 deletion Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

static const dev_config_t init_config =
{
.firmware_version = 0x1413, // do not change
.firmware_version = 0x1414, // do not change

/*
Name of device in devices dispatcher
Expand Down
Binary file modified MDK-ARM/FreeJoy.bin
Binary file not shown.
21 changes: 20 additions & 1 deletion Src/analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ analog_data_t input_data[MAX_AXIS_NUM];
analog_data_t scaled_axis_data[MAX_AXIS_NUM];
analog_data_t raw_axis_data[MAX_AXIS_NUM];
analog_data_t out_axis_data[MAX_AXIS_NUM];
analog_data_t tmp_axis_data[PREBUF_SIZE][MAX_AXIS_NUM];

analog_data_t FILTER_LEVEL_1_COEF[FILTER_BUF_SIZE] = {40, 30, 15, 10, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
analog_data_t FILTER_LEVEL_2_COEF[FILTER_BUF_SIZE] = {30, 20, 10, 10, 10, 6, 6, 4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Expand Down Expand Up @@ -462,7 +463,7 @@ void AxesInit (dev_config_t * p_dev_config)
if (p_dev_config->pins[i] == AXIS_ANALOG) // Configure ADC channels
{
/* ADC1 regular channel configuration */
ADC_RegularChannelConfig(ADC1, channel_config[i].channel, i+1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, channel_config[i].channel, i+1, ADC_SampleTime_71Cycles5);
axis_num++;
}

Expand Down Expand Up @@ -506,8 +507,12 @@ void AxesInit (dev_config_t * p_dev_config)
*/
void ADC_Conversion (void)
{
static uint8_t num_of_conv = 0;
analog_data_t tmp = 0;

if (adc_cnt > 0)
{
DMA1_Channel1->CMAR = (uint32_t) &tmp_axis_data[num_of_conv++];
DMA_SetCurrDataCounter(DMA1_Channel1, MAX_AXIS_NUM);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
Expand All @@ -519,6 +524,20 @@ void ADC_Conversion (void)

ADC_Cmd(ADC1, DISABLE);
DMA_Cmd(DMA1_Channel1, DISABLE);

if (num_of_conv > PREBUF_SIZE - 1)
{
num_of_conv = 0;
for (uint8_t i=0; i<MAX_AXIS_NUM; i++)
{
tmp = 0;
for (uint8_t k=0; k<PREBUF_SIZE; k++)
{
tmp += tmp_axis_data[k][i];
}
input_data[i] = tmp/PREBUF_SIZE;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Src/periphery.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Timers_Init(dev_config_t * p_dev_config)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_Prescaler = RCC_Clocks.PCLK1_Frequency/100000 - 1;
TIM_TimeBaseInitStructure.TIM_Period = 400 - 1; // 2ms, 500Hz
TIM_TimeBaseInitStructure.TIM_Period = 200 - 1; // 1ms, 1000Hz
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
Expand Down
26 changes: 18 additions & 8 deletions Src/stm32f10x_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/

#define ADC_PERIOD_MS 2
#define SENSORS_PERIOD_MS 2
#define ENCODER_PERIOD_MS 1

/* Private variables ---------------------------------------------------------*/
volatile int32_t millis =0, joy_millis=0, adc_millis=100, sensors_millis=101;

volatile int32_t millis =0, joy_millis=0, encoder_millis = 0, adc_millis=100, sensors_millis=101;
extern dev_config_t dev_config;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

Expand Down Expand Up @@ -215,16 +222,15 @@ void TIM1_UP_IRQHandler(void)
if (millis - adc_millis >= ADC_PERIOD_MS)
{
adc_millis = millis;



AxesProcess(&dev_config);

// Disable periphery before ADC conversion
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,DISABLE);

ADC_Conversion();
for (uint8_t i=0; i<PREBUF_SIZE; i++) ADC_Conversion();

// Enable periphery after ADC conversion
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
Expand All @@ -247,8 +253,12 @@ void TIM1_UP_IRQHandler(void)
}
}
}

EncoderProcess(buttons_state, &dev_config);
// encoders polling
if (millis - encoder_millis >= ENCODER_PERIOD_MS)
{
encoder_millis = millis;
EncoderProcess(buttons_state, &dev_config);
}

}
}
Expand Down

0 comments on commit 09eb58d

Please sign in to comment.