ADC & DMA renamed to DAQ (data acquisition)

This commit is contained in:
Martin Jaeger
2020-01-27 15:13:53 +01:00
parent dc394cc198
commit 05292c12ba
16 changed files with 199 additions and 193 deletions
-89
View File
@@ -1,89 +0,0 @@
/*
* Copyright (c) 2016 Martin Jäger / Libre Solar
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
*
* @brief Reads ADC via DMA and stores data into necessary structs
*/
#ifndef ADC_DMA_H
#define ADC_DMA_H
#ifdef __ZEPHYR__
#include <zephyr.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
/** Struct to definie upper and lower limit alerts for any ADC channel
*/
typedef struct {
void (*callback)(); ///< Function to be called when limits are exceeded
uint16_t limit; ///< ADC reading for lower limit
int debounce_ms; ///< Milliseconds delay for triggering alert
} AdcAlert;
/** Sets offset to actual measured value, i.e. sets zero current point.
*
* All input/output switches and consumers should be switched off before calling this function
*/
void calibrate_current_sensors();
/** Updates structures with data read from ADC
*/
void update_measurements();
#ifdef __ZEPHYR__
/** Restarts ADC sampling sequence (should be called by timer)
*/
void adc_trigger_conversion(struct k_timer *timer_id);
#else
/** Initializes registers and starts ADC timer
*/
void adc_timer_start(int freq_Hz);
#endif /* __ZEPHYR__ */
/** Sets necessary ADC registers
*/
void adc_setup(void);
/** Sets necessary DMA registers
*/
void dma_setup(void);
/** Read, filter and check raw ADC readings stored by DMA controller
*/
void adc_update_value(unsigned int pos);
/** Set lv side (battery) voltage limits where an alert should be triggered
*
* @param upper Upper voltage limit
* @param lower Lower voltage limit
*/
void adc_set_lv_alerts(float upper, float lower);
/** Add an inhibit delay to the alerts to disable it temporarily
*
* @param adc_pos The position of the ADC measurement channel
* @param timeout_ms Timeout in milliseconds
*/
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms);
#ifdef __cplusplus
}
#endif
#endif /* ADC_DMA */
+4 -4
View File
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "adc_dma.h"
#include "daq.h"
#include <math.h> // log for thermistor calculation
#include <assert.h>
@@ -142,7 +142,7 @@ void adc_update_value(unsigned int pos)
}
}
void update_measurements()
void daq_update()
{
// reference voltage of 2.5 V at PIN_V_REF
//int vcc = 2500 * 4096 / (adc_filtered[ADC_POS_V_REF] >> (4 + ADC_FILTER_CONST));
@@ -272,7 +272,7 @@ uint16_t adc_get_alert_limit(float scale, float limit)
// shift 4 bits left to generate left aligned 16bit value
}
void adc_set_lv_alerts(float upper, float lower)
void daq_set_lv_alerts(float upper, float lower)
{
int vcc = VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU);
float scale = ((4096* 1000) / (ADC_GAIN_V_BAT)) / vcc;
@@ -288,7 +288,7 @@ void adc_set_lv_alerts(float upper, float lower)
#if defined(UNIT_TEST)
#include "../test/adc_dma_stub.h"
#include "../test/daq_stub.h"
void prepare_adc_readings(AdcValues values)
{
+78
View File
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2016 Martin Jäger / Libre Solar
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Data Acquisition (DAQ)
*
* Measurements are taken using ADC with DMA. The DAC is used to generate reference voltages
* for bi-directional current measurement. Data is stored in structs, which are afterwards
* used by the control algorithms
*/
#ifndef DAQ_H_
#define DAQ_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
/**
* Struct to definie upper and lower limit alerts for any ADC channel
*/
typedef struct {
void (*callback)(); ///< Function to be called when limits are exceeded
uint16_t limit; ///< ADC reading for lower limit
int debounce_ms; ///< Milliseconds delay for triggering alert
} AdcAlert;
/**
* Set offset vs. actual measured value, i.e. sets zero current point.
*
* All input/output switches and consumers should be switched off before calling this function
*/
void calibrate_current_sensors(void);
/**
* Updates structures with data read from ADC
*/
void daq_update(void);
/**
* Initializes ADC, DAC and DMA
*/
void daq_setup(void);
/**
* Read, filter and check raw ADC readings stored by DMA controller
*/
void adc_update_value(unsigned int pos);
/**
* Set lv side (battery) voltage limits where an alert should be triggered
*
* @param upper Upper voltage limit
* @param lower Lower voltage limit
*/
void daq_set_lv_alerts(float upper, float lower);
/**
* Add an inhibit delay to the alerts to disable it temporarily
*
* @param adc_pos The position of the ADC measurement channel
* @param timeout_ms Timeout in milliseconds
*/
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms);
#ifdef __cplusplus
}
#endif
#endif /* DAQ_H_ */
+16 -2
View File
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "adc_dma.h"
#include "daq.h"
#if defined(__MBED__)
@@ -73,12 +73,15 @@ extern "C" void DMA1_Channel1_IRQHandler(void)
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
}
void adc_setup()
void dac_setup()
{
#ifdef PIN_REF_I_DCDC
ref_i_dcdc = 0.1; // reference voltage for zero current (0.1 for buck, 0.9 for boost, 0.5 for bi-directional)
#endif
}
void adc_setup()
{
#ifdef PIN_V_SOLAR_EN
DigitalOut solar_en(PIN_V_SOLAR_EN);
solar_en = 1;
@@ -218,4 +221,15 @@ extern "C" void TIM6_IRQHandler(void)
#endif
void daq_setup()
{
dac_setup(); // for current sensor references
adc_setup();
dma_setup();
adc_timer_start(1000); // 1 kHz
wait(0.5); // wait for ADC to collect some measurement values
daq_update();
calibrate_current_sensors();
}
#endif /* __MBED__ */
+61 -44
View File
@@ -4,9 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "adc_dma.h"
#include "daq.h"
#if defined(__ZEPHYR__)// && defined(CONFIG_SOC_SERIES_STM32L0X)
#if defined(__ZEPHYR__)
#include <zephyr.h>
#include <drivers/adc.h>
@@ -79,51 +79,13 @@ extern uint16_t adc_readings[];
void adc_update_value(unsigned int pos);
void DMA1_Channel1_IRQHandler(void *args)
static void dac_setup()
{
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1
{
for (unsigned int i = 0; i < NUM_ADC_CH; i++) {
adc_update_value(i);
}
}
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
/* TODO */
}
void dma_setup()
static void adc_setup()
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
LL_DMA_ConfigAddresses(DMA1, LL_DMA_CHANNEL_1,
LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA), // source address
(uint32_t)(&(adc_readings[0])), // destination address
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
// Configure the number of DMA transfers (data length in multiples of size per transfer)
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, NUM_ADC_CH * 2);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1); // transfer error interrupt
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1); // transfer complete interrupt
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
// Configure NVIC for DMA (priority 2: second-lowest value for STM32L0/F0)
IRQ_CONNECT(DMA1_Channel1_IRQn, 2, DMA1_Channel1_IRQHandler, 0, 0);
irq_enable(DMA1_Channel1_IRQn);
LL_ADC_REG_StartConversion(ADC1);
}
void adc_setup()
{
#ifdef PIN_REF_I_DCDC
//ref_i_dcdc = 0.1; // reference voltage for zero current (0.1 for buck, 0.9 for boost, 0.5 for bi-directional)
#endif
#ifdef PIN_V_SOLAR_EN
//DigitalOut solar_en(PIN_V_SOLAR_EN);
//solar_en = 1;
@@ -174,9 +136,64 @@ void adc_setup()
LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
}
void adc_trigger_conversion(struct k_timer *timer_id)
static inline void adc_trigger_conversion(struct k_timer *timer_id)
{
LL_ADC_REG_StartConversion(ADC1);
}
static void DMA1_Channel1_IRQHandler(void *args)
{
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1
{
for (unsigned int i = 0; i < NUM_ADC_CH; i++) {
adc_update_value(i);
}
}
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
}
static void dma_setup()
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
LL_DMA_ConfigAddresses(DMA1, LL_DMA_CHANNEL_1,
LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA), // source address
(uint32_t)(&(adc_readings[0])), // destination address
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
// Configure the number of DMA transfers (data length in multiples of size per transfer)
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, NUM_ADC_CH * 2);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1); // transfer error interrupt
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1); // transfer complete interrupt
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
// Configure NVIC for DMA (priority 2: second-lowest value for STM32L0/F0)
IRQ_CONNECT(DMA1_Channel1_IRQn, 2, DMA1_Channel1_IRQHandler, 0, 0);
irq_enable(DMA1_Channel1_IRQn);
LL_ADC_REG_StartConversion(ADC1);
}
void daq_setup()
{
static struct k_timer adc_trigger_timer;
dac_setup();
adc_setup();
dma_setup();
k_timer_init(&adc_trigger_timer, adc_trigger_conversion, NULL);
k_timer_start(&adc_trigger_timer, K_MSEC(1), K_MSEC(1)); // 1 kHz
k_sleep(500); // wait for ADC to collect some measurement values
daq_update();
calibrate_current_sensors();
}
#endif // __ZEPHYR__
+1 -1
View File
@@ -111,7 +111,7 @@ enum ErrorFlag {
/** Charge controller internal temperature too high
*
* Set and cleared by ADC update_measurements()
* Set and cleared by daq_update()
*/
ERR_INT_OVERTEMP = 1U << 13,
+7 -12
View File
@@ -22,12 +22,12 @@
#include "dcdc.h" // DC/DC converter control (hardware independent)
#include "pwm_switch.h" // PWM charge controller
#include "bat_charger.h" // battery settings and charger state machine
#include "adc_dma.h" // ADC using DMA and conversion to measurement values
#include "ext/ext.h" // communication interfaces, displays, etc. in UEXT connector
#include "daq.h" // ADC using DMA and conversion to measurement values
#include "ext/ext.h" // communication interfaces, displays, etc. in UEXT connector
#include "eeprom.h" // external I2C EEPROM
#include "load.h" // load and USB output management
#include "leds.h" // LED switching using charlieplexing
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "data_objects.h" // for access to internal data via ThingSet
#include "bl_support.h" // Bootloader support from the application side
@@ -106,13 +106,8 @@ int main()
ts.set_user_password(THINGSET_USER_PASSWORD); // passwords defined in config.h (see template)
ts.set_maker_password(THINGSET_MAKER_PASSWORD);
// ADC, DMA and sensor calibration
adc_setup();
dma_setup();
adc_timer_start(1000); // 1 kHz
wait(0.5); // wait for ADC to collect some measurement values
update_measurements();
calibrate_current_sensors();
// Data Acquisition (DAQ) setup
daq_setup();
// initialize all extensions and external communication interfaces
ext_mgr.enable_all();
@@ -189,10 +184,10 @@ void system_control()
static int counter = 0;
// convert ADC readings to meaningful measurement values
update_measurements();
daq_update();
// alerts should trigger only for transients, so update based on actual voltage
adc_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
daq_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
#if CONFIG_HAS_PWM_SWITCH
ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal);
+8 -17
View File
@@ -20,12 +20,12 @@
#include "dcdc.h" // DC/DC converter control (hardware independent)
#include "pwm_switch.h" // PWM charge controller
#include "bat_charger.h" // battery settings and charger state machine
#include "adc_dma.h" // ADC using DMA and conversion to measurement values
#include "ext/ext.h" // communication interfaces, displays, etc. in UEXT connector
#include "daq.h" // ADC using DMA and conversion to measurement values
#include "ext/ext.h" // communication interfaces, displays, etc. in UEXT connector
#include "eeprom.h" // external I2C EEPROM
#include "load.h" // load and USB output management
#include "leds.h" // LED switching using charlieplexing
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
#include "data_objects.h" // for access to internal data via ThingSet
PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT)
@@ -100,17 +100,8 @@ void main(void)
//ts.set_user_password(THINGSET_USER_PASSWORD); // passwords defined in config.h (see template)
//ts.set_maker_password(THINGSET_MAKER_PASSWORD);
// ADC, DMA and sensor calibration
adc_setup();
dma_setup();
struct k_timer adc_trigger_timer;
k_timer_init(&adc_trigger_timer, adc_trigger_conversion, NULL);
k_timer_start(&adc_trigger_timer, K_MSEC(1), K_MSEC(1)); // 1 kHz
k_sleep(500); // wait for ADC to collect some measurement values
update_measurements();
calibrate_current_sensors();
// Data Acquisition (DAQ) setup
daq_setup();
// initialize all extensions and external communication interfaces
ext_mgr.enable_all();
@@ -145,10 +136,10 @@ void control_thread()
while (1) {
// convert ADC readings to meaningful measurement values
update_measurements();
daq_update();
// alerts should trigger only for transients, so update based on actual voltage
adc_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
daq_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
#if CONFIG_HAS_PWM_SWITCH
ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal);
@@ -158,7 +149,7 @@ void control_thread()
#if CONFIG_HAS_DCDC_CONVERTER
ports_update_current_limits(&dcdc_lv_port, &bat_terminal, &load_terminal);
dcdc.control(); // control of DC/DC including MPPT algorithm
//dcdc.control(); // control of DC/DC including MPPT algorithm
leds_set_charging(half_bridge_enabled());
#endif
+1 -1
View File
@@ -8,7 +8,7 @@
#include "config.h"
#include "pcb.h"
#include "adc_dma.h"
#include "daq.h"
#include "debug.h"
#include "helper.h"
+2 -2
View File
@@ -4,8 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ADC_DMA_STUB_H
#define ADC_DMA_STUB_H
#ifndef DAQ_STUB_H_
#define DAQ_STUB_H_
#include "config.h"
+2 -2
View File
@@ -13,7 +13,7 @@
#include "dcdc.h" // DC/DC converter control (hardware independent)
#include "pwm_switch.h" // PWM charge controller
#include "bat_charger.h" // battery settings and charger state machine
#include "adc_dma.h" // ADC using DMA and conversion to measurement values
#include "daq.h" // ADC using DMA and conversion to measurement values
#include "eeprom.h" // external I2C EEPROM
#include "load.h" // load and USB output management
#include "leds.h" // LED switching using charlieplexing
@@ -78,7 +78,7 @@ time_t timestamp; // current unix timestamp (independent of time(NULL), as it
int main()
{
adc_tests();
daq_tests();
bat_charger_tests();
power_port_tests();
half_brigde_tests();
+1 -1
View File
@@ -9,7 +9,7 @@
void bat_charger_tests();
void adc_tests();
void daq_tests();
void power_port_tests();
+10 -10
View File
@@ -5,8 +5,8 @@
*/
#include "tests.h"
#include "adc_dma.h"
#include "adc_dma_stub.h"
#include "daq.h"
#include "daq_stub.h"
#include "main.h"
@@ -73,7 +73,7 @@ void adc_alert_undervoltage_triggering()
{
dev_stat.clear_error(ERR_ANY_ERROR);
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
adc_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
daq_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
prepare_adc_filtered();
adc_update_value(ADC_POS_V_BAT);
@@ -92,7 +92,7 @@ void adc_alert_undervoltage_triggering()
adcval.battery_voltage = 13;
prepare_adc_readings(adcval);
prepare_adc_filtered();
update_measurements();
daq_update();
charger.discharge_control(&bat_conf);
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
@@ -102,7 +102,7 @@ void adc_alert_overvoltage_triggering()
{
dev_stat.clear_error(ERR_ANY_ERROR);
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
adc_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
daq_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
prepare_adc_filtered();
adc_update_value(ADC_POS_V_BAT);
@@ -122,7 +122,7 @@ void adc_alert_overvoltage_triggering()
adcval.battery_voltage = 12;
prepare_adc_readings(adcval);
prepare_adc_filtered();
update_measurements();
daq_update();
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
charger.charge_control(&bat_conf);
@@ -136,12 +136,12 @@ void adc_alert_overflow_prevention()
TEST_ASSERT_EQUAL_HEX((uint16_t)(UINT16_MAX << 4), limit);
}
/** ADC conversion test
/** Data acquisition tests
*
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
* to calculated voltage/current measurements of different DC buses
*/
void adc_tests()
void daq_tests()
{
adcval.bat_temperature = 25;
adcval.battery_voltage = 12;
@@ -155,8 +155,8 @@ void adc_tests()
RUN_TEST(check_filtering);
// call original update_measurements function
update_measurements();
// call original daq_update function
daq_update();
RUN_TEST(check_solar_terminal_readings);
RUN_TEST(check_bat_terminal_readings);
+2 -2
View File
@@ -5,8 +5,8 @@
*/
#include "tests.h"
#include "adc_dma_stub.h"
#include "adc_dma.h"
#include "daq_stub.h"
#include "daq.h"
#include <time.h>
#include <stdio.h>
+4 -4
View File
@@ -5,8 +5,8 @@
*/
#include "tests.h"
#include "adc_dma_stub.h"
#include "adc_dma.h"
#include "daq_stub.h"
#include "daq.h"
#include <time.h>
#include <stdio.h>
@@ -44,7 +44,7 @@ void energy_calculation_init()
// insert values into ADC functions
prepare_adc_readings(adcval);
prepare_adc_filtered();
update_measurements();
daq_update();
for (int i = 0; i < 60*60*sun_hours; i++) {
hv_terminal.energy_balance();
@@ -56,7 +56,7 @@ void energy_calculation_init()
adcval.dcdc_current = 0;
prepare_adc_readings(adcval);
prepare_adc_filtered();
update_measurements();
daq_update();
for (int i = 0; i < 60*60*night_hours; i++) {
hv_terminal.energy_balance();
+2 -2
View File
@@ -12,10 +12,10 @@ add_definitions(-D__ZEPHYR__ -DDEBUG_PRINT_FLAGS=7)
include_directories(../src)
target_sources(app PRIVATE ../src/adc_dma.cpp)
target_sources(app PRIVATE ../src/adc_dma_zephyr.c)
target_sources(app PRIVATE ../src/bat_charger.cpp)
target_sources(app PRIVATE ../src/data_objects.cpp)
target_sources(app PRIVATE ../src/daq.cpp)
target_sources(app PRIVATE ../src/daq_zephyr.c)
target_sources(app PRIVATE ../src/device_status.cpp)
target_sources(app PRIVATE ../src/dcdc.cpp)
target_sources(app PRIVATE ../src/eeprom.cpp)