Refactored adc_dma.cpp for readability (#76)

- replaced the lengthy adc value formulas with respective functions
  to ease reading
- removed unecessary casts for alert callbacks
- added static to the internal global variables in order to hide them from the
  other compilation units
This commit is contained in:
hogthrob
2019-11-09 23:13:00 +01:00
committed by Martin Jäger
parent 5cc4921cd2
commit ba7d379225
7 changed files with 124 additions and 106 deletions
-43
View File
@@ -1,43 +0,0 @@
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "pcb.h"
#include "adc_dma.h"
#include "adc_dma_stub.h"
#include <stdint.h>
#include <stdio.h>
extern uint16_t adc_readings[NUM_ADC_CH];
extern uint32_t adc_filtered[NUM_ADC_CH];
void prepare_adc_readings(AdcValues values)
{
adc_readings[ADC_POS_VREF_MCU] = (uint16_t)(1.224 / 3.3 * 4096) << 4;
adc_readings[ADC_POS_V_SOLAR] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_V_BAT] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_I_DCDC] = (uint16_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_I_LOAD] = (uint16_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << 4;
}
void prepare_adc_filtered()
{
// initialize also filtered values
for (int i = 0; i < NUM_ADC_CH; i++) {
adc_filtered[i] = adc_readings[i] << ADC_FILTER_CONST;
}
}
+5
View File
@@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ADC_DMA_STUB_H
#define ADC_DMA_STUB_H
#include "config.h"
@@ -30,3 +32,6 @@ typedef struct {
void prepare_adc_readings(AdcValues values);
void prepare_adc_filtered();
void clear_adc_filtered();
uint32_t get_adc_filtered(uint32_t channel);
#endif
+5 -6
View File
@@ -14,8 +14,7 @@ extern AdcAlert adc_alerts[NUM_ADC_CH];
void check_filtering()
{
// reset values
adc_filtered[ADC_POS_VREF_MCU] = 0;
adc_filtered[ADC_POS_V_SOLAR] = 0;
clear_adc_filtered();
// collect 1000 samples to update filtered values
for (int s = 0; s < 1000; s++) {
@@ -25,14 +24,14 @@ void check_filtering()
}
uint32_t adc_filtered_bak[NUM_ADC_CH];
adc_filtered_bak[ADC_POS_VREF_MCU] = adc_filtered[ADC_POS_VREF_MCU];
adc_filtered_bak[ADC_POS_V_SOLAR] = adc_filtered[ADC_POS_V_SOLAR];
adc_filtered_bak[ADC_POS_VREF_MCU] = get_adc_filtered(ADC_POS_VREF_MCU);
adc_filtered_bak[ADC_POS_V_SOLAR] = get_adc_filtered(ADC_POS_V_SOLAR);
// overwrite filtered values
prepare_adc_filtered();
TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_VREF_MCU], adc_filtered_bak[ADC_POS_VREF_MCU]);
TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_V_SOLAR], adc_filtered_bak[ADC_POS_V_SOLAR]);
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_VREF_MCU), adc_filtered_bak[ADC_POS_VREF_MCU]);
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_V_SOLAR), adc_filtered_bak[ADC_POS_V_SOLAR]);
}
void check_solar_terminal_readings()