Zephyr: ADC based on STM HAL working
This commit is contained in:
@@ -3,3 +3,13 @@
|
||||
|
||||
# include generic charge controller Kconfig
|
||||
rsource "../../Kconfig"
|
||||
|
||||
# include STM32 Cube HAL (temporary solution)
|
||||
|
||||
mainmenu "STM32_HAL_ADC"
|
||||
|
||||
config STM32_HAL_ADC
|
||||
default y
|
||||
bool
|
||||
select USE_STM32_HAL_ADC
|
||||
select USE_STM32_HAL_ADC_EX
|
||||
|
||||
@@ -35,3 +35,7 @@ CONFIG_CLOCK_STM32_PLL_MULTIPLIER=4
|
||||
CONFIG_CLOCK_STM32_PLL_DIVISOR=2
|
||||
CONFIG_CLOCK_STM32_AHB_PRESCALER=1
|
||||
CONFIG_CLOCK_STM32_APB1_PRESCALER=1
|
||||
|
||||
# include STM32 Cube HAL (temporary solution)
|
||||
CONFIG_USE_STM32_HAL_ADC=y
|
||||
CONFIG_USE_STM32_HAL_ADC_EX=y
|
||||
|
||||
+6
-202
@@ -26,7 +26,7 @@ static float solar_current_offset;
|
||||
static float load_current_offset;
|
||||
|
||||
// for ADC and DMA
|
||||
static volatile uint16_t adc_readings[NUM_ADC_CH] = {};
|
||||
volatile uint16_t adc_readings[NUM_ADC_CH] = {};
|
||||
static volatile uint32_t adc_filtered[NUM_ADC_CH] = {};
|
||||
static volatile AdcAlert adc_alerts_upper[NUM_ADC_CH] = {};
|
||||
static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {};
|
||||
@@ -50,10 +50,12 @@ static inline uint32_t adc_value(uint32_t channel)
|
||||
*
|
||||
* @return voltage in millivolts
|
||||
*/
|
||||
|
||||
static inline float adc_voltage(uint32_t channel, int32_t vcc)
|
||||
{
|
||||
return (adc_value(channel) * vcc) / 4096;
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured current/voltage for ADC channel after average and scaling
|
||||
* @param channel valid ADC channel pos ADC_POS_..., see adc_h.c
|
||||
@@ -140,7 +142,6 @@ void adc_update_value(unsigned int pos)
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void update_measurements()
|
||||
{
|
||||
// reference voltage of 2.5 V at PIN_V_REF
|
||||
@@ -285,204 +286,7 @@ void adc_set_lv_alerts(float upper, float lower)
|
||||
adc_alerts_lower[ADC_POS_V_BAT].callback = low_voltage_alert;
|
||||
}
|
||||
|
||||
#if defined(__MBED__) && !defined(UNIT_TEST)
|
||||
|
||||
void dma_setup()
|
||||
{
|
||||
//__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
/* Enable the peripheral clock on DMA */
|
||||
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
||||
|
||||
/* Enable DMA transfer on ADC and circular mode */
|
||||
ADC1->CFGR1 |= ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG;
|
||||
|
||||
/* Configure the peripheral data register address */
|
||||
DMA1_Channel1->CPAR = (uint32_t)(&(ADC1->DR));
|
||||
|
||||
/* Configure the memory address */
|
||||
DMA1_Channel1->CMAR = (uint32_t)(&(adc_readings[0]));
|
||||
|
||||
/* Configure the number of DMA tranfer to be performed on DMA channel 1 */
|
||||
DMA1_Channel1->CNDTR = NUM_ADC_CH;
|
||||
|
||||
/* Configure increment, size, interrupts and circular mode */
|
||||
DMA1_Channel1->CCR =
|
||||
DMA_CCR_MINC | /* memory increment mode enabled */
|
||||
DMA_CCR_MSIZE_0 | /* memory size 16-bit */
|
||||
DMA_CCR_PSIZE_0 | /* peripheral size 16-bit */
|
||||
DMA_CCR_TEIE | /* transfer error interrupt enable */
|
||||
DMA_CCR_TCIE | /* transfer complete interrupt enable */
|
||||
DMA_CCR_CIRC; /* circular mode enable */
|
||||
/* DIR = 0: read from peripheral */
|
||||
|
||||
/* Enable DMA Channel 1 */
|
||||
DMA1_Channel1->CCR |= DMA_CCR_EN;
|
||||
|
||||
/* Configure NVIC for DMA (priority 2: second-lowest value for STM32L0/F0) */
|
||||
NVIC_SetPriority(DMA1_Channel1_IRQn, 2);
|
||||
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
||||
|
||||
// Trigger ADC conversions
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
extern "C" void DMA1_Channel1_IRQHandler(void)
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
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;
|
||||
#endif
|
||||
|
||||
ADC_HandleTypeDef hadc = {0};
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
// Configure ADC object structures
|
||||
hadc.Instance = ADC1;
|
||||
hadc.State = HAL_ADC_STATE_RESET;
|
||||
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||
hadc.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
hadc.Init.DataAlign = ADC_DATAALIGN_LEFT; // for exponential moving average filter
|
||||
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
hadc.Init.LowPowerAutoWait = DISABLE;
|
||||
hadc.Init.LowPowerAutoPowerOff = DISABLE;
|
||||
hadc.Init.ContinuousConvMode = DISABLE;
|
||||
hadc.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
hadc.Init.DMAContinuousRequests = ENABLE; //DISABLE;
|
||||
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
|
||||
|
||||
if (HAL_ADC_Init(&hadc) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
#if defined(STM32L0)
|
||||
HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);
|
||||
#else
|
||||
HAL_ADCEx_Calibration_Start(&hadc);
|
||||
#endif
|
||||
|
||||
// Configure ADC channel
|
||||
sConfig.Channel = ADC_CHANNEL_0; // can be any channel for initialization
|
||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||
|
||||
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
|
||||
hadc.Instance->CHSELR = 0;
|
||||
|
||||
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
HAL_ADC_Start(&hadc); // Start conversion
|
||||
|
||||
// Read out value one time to finish ADC configuration
|
||||
if (HAL_ADC_PollForConversion(&hadc, 10) == HAL_OK) {
|
||||
HAL_ADC_GetValue(&hadc);
|
||||
}
|
||||
|
||||
// ADC sampling time register
|
||||
// 000: 1.5 ADC clock cycles
|
||||
// 001: 7.5 ADC clock cycles
|
||||
// 010: 13.5 ADC clock cycles
|
||||
// 011: 28.5 ADC clock cycles
|
||||
// 100: 41.5 ADC clock cycles
|
||||
// 101: 55.5 ADC clock cycles
|
||||
// 110: 71.5 ADC clock cycles
|
||||
// 111: 239.5 ADC clock cycles
|
||||
//ADC1->SMPR = ADC_SMPR_SMP_1; // for normal ADC OK
|
||||
ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; // necessary for internal reference and temperature
|
||||
|
||||
// Select ADC channels based on setup in config.h
|
||||
ADC1->CHSELR = ADC_CHSEL;
|
||||
|
||||
// Enable internal voltage reference and temperature sensor
|
||||
// ToDo check sample rate
|
||||
ADC->CCR |= ADC_CCR_TSEN | ADC_CCR_VREFEN;
|
||||
}
|
||||
|
||||
#endif // UNIT_TEST
|
||||
|
||||
#if defined(STM32F0)
|
||||
|
||||
void adc_timer_start(int freq_Hz) // max. 10 kHz
|
||||
{
|
||||
// Enable TIM15 clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM15->PSC = SystemCoreClock / 10000 - 1;
|
||||
|
||||
// Interrupt on timer update
|
||||
TIM15->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM15->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 2 = second-lowest priority of STM32L0/F0
|
||||
NVIC_SetPriority(TIM15_IRQn, 2);
|
||||
NVIC_EnableIRQ(TIM15_IRQn);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM15->CR1 |= TIM_CR1_CEN;
|
||||
}
|
||||
|
||||
extern "C" void TIM15_IRQHandler(void)
|
||||
{
|
||||
TIM15->SR &= ~(1 << 0);
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
#elif defined(STM32L0)
|
||||
|
||||
void adc_timer_start(int freq_Hz) // max. 10 kHz
|
||||
{
|
||||
// Enable TIM6 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM6->PSC = SystemCoreClock / 10000 - 1;
|
||||
|
||||
// Interrupt on timer update
|
||||
TIM6->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM6->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 2 = second-lowest priority of STM32L0/F0
|
||||
NVIC_SetPriority(TIM6_IRQn, 2);
|
||||
NVIC_EnableIRQ(TIM6_IRQn);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM6->CR1 |= TIM_CR1_CEN;
|
||||
}
|
||||
|
||||
extern "C" void TIM6_IRQHandler(void)
|
||||
{
|
||||
TIM6->SR &= ~(1 << 0);
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
#else
|
||||
#if defined(UNIT_TEST)
|
||||
|
||||
#include "../test/adc_dma_stub.h"
|
||||
|
||||
@@ -505,7 +309,7 @@ void prepare_adc_filtered()
|
||||
|
||||
void clear_adc_filtered()
|
||||
{
|
||||
// initialize also filtered values
|
||||
// initialize also filtered values
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_filtered[i] = 0;
|
||||
}
|
||||
@@ -515,4 +319,4 @@ uint32_t get_adc_filtered(uint32_t channel)
|
||||
return adc_value(channel);
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
+29
-9
@@ -4,26 +4,32 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ADC_DMA_H
|
||||
#define ADC_DMA_H
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @brief Reads ADC via DMA and stores data into necessary structs
|
||||
*/
|
||||
|
||||
#include "dcdc.h"
|
||||
#include "load.h"
|
||||
#include "bat_charger.h"
|
||||
#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)() = NULL; ///< Function to be called when limits are exceeded
|
||||
uint16_t limit = 0; ///< ADC reading for lower limit
|
||||
int debounce_ms = 0; ///< Milliseconds delay for triggering alert
|
||||
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.
|
||||
@@ -36,10 +42,20 @@ void calibrate_current_sensors();
|
||||
*/
|
||||
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);
|
||||
@@ -66,4 +82,8 @@ void adc_set_lv_alerts(float upper, float lower);
|
||||
*/
|
||||
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ADC_DMA */
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "adc_dma.h"
|
||||
|
||||
#if defined(__MBED__)
|
||||
|
||||
#include <math.h> // log for thermistor calculation
|
||||
#include <assert.h>
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
#include "mcu.h"
|
||||
#include "main.h"
|
||||
#include "debug.h"
|
||||
#include "pcb.h" // contains defines for pins
|
||||
|
||||
// for ADC and DMA
|
||||
extern uint16_t adc_readings[];
|
||||
extern AnalogOut ref_i_dcdc;
|
||||
|
||||
void dma_setup()
|
||||
{
|
||||
//__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
/* Enable the peripheral clock on DMA */
|
||||
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
||||
|
||||
/* Enable DMA transfer on ADC and circular mode */
|
||||
ADC1->CFGR1 |= ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG;
|
||||
|
||||
/* Configure the peripheral data register address */
|
||||
DMA1_Channel1->CPAR = (uint32_t)(&(ADC1->DR));
|
||||
|
||||
/* Configure the memory address */
|
||||
DMA1_Channel1->CMAR = (uint32_t)(&(adc_readings[0]));
|
||||
|
||||
/* Configure the number of DMA tranfer to be performed on DMA channel 1 */
|
||||
DMA1_Channel1->CNDTR = NUM_ADC_CH;
|
||||
|
||||
/* Configure increment, size, interrupts and circular mode */
|
||||
DMA1_Channel1->CCR =
|
||||
DMA_CCR_MINC | /* memory increment mode enabled */
|
||||
DMA_CCR_MSIZE_0 | /* memory size 16-bit */
|
||||
DMA_CCR_PSIZE_0 | /* peripheral size 16-bit */
|
||||
DMA_CCR_TEIE | /* transfer error interrupt enable */
|
||||
DMA_CCR_TCIE | /* transfer complete interrupt enable */
|
||||
DMA_CCR_CIRC; /* circular mode enable */
|
||||
/* DIR = 0: read from peripheral */
|
||||
|
||||
/* Enable DMA Channel 1 */
|
||||
DMA1_Channel1->CCR |= DMA_CCR_EN;
|
||||
|
||||
/* Configure NVIC for DMA (priority 2: second-lowest value for STM32L0/F0) */
|
||||
NVIC_SetPriority(DMA1_Channel1_IRQn, 2);
|
||||
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
||||
|
||||
// Trigger ADC conversions
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
extern "C" void DMA1_Channel1_IRQHandler(void)
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
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;
|
||||
#endif
|
||||
|
||||
ADC_HandleTypeDef hadc = {0};
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
// Configure ADC object structures
|
||||
hadc.Instance = ADC1;
|
||||
hadc.State = HAL_ADC_STATE_RESET;
|
||||
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||
hadc.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
hadc.Init.DataAlign = ADC_DATAALIGN_LEFT; // for exponential moving average filter
|
||||
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
hadc.Init.LowPowerAutoWait = DISABLE;
|
||||
hadc.Init.LowPowerAutoPowerOff = DISABLE;
|
||||
hadc.Init.ContinuousConvMode = DISABLE;
|
||||
hadc.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
hadc.Init.DMAContinuousRequests = ENABLE; //DISABLE;
|
||||
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
|
||||
|
||||
if (HAL_ADC_Init(&hadc) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
#if defined(STM32L0)
|
||||
HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);
|
||||
#else
|
||||
HAL_ADCEx_Calibration_Start(&hadc);
|
||||
#endif
|
||||
|
||||
// Configure ADC channel
|
||||
sConfig.Channel = ADC_CHANNEL_0; // can be any channel for initialization
|
||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||
|
||||
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
|
||||
hadc.Instance->CHSELR = 0;
|
||||
|
||||
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
HAL_ADC_Start(&hadc); // Start conversion
|
||||
|
||||
// Read out value one time to finish ADC configuration
|
||||
if (HAL_ADC_PollForConversion(&hadc, 10) == HAL_OK) {
|
||||
HAL_ADC_GetValue(&hadc);
|
||||
}
|
||||
|
||||
// ADC sampling time register
|
||||
// 000: 1.5 ADC clock cycles
|
||||
// 001: 7.5 ADC clock cycles
|
||||
// 010: 13.5 ADC clock cycles
|
||||
// 011: 28.5 ADC clock cycles
|
||||
// 100: 41.5 ADC clock cycles
|
||||
// 101: 55.5 ADC clock cycles
|
||||
// 110: 71.5 ADC clock cycles
|
||||
// 111: 239.5 ADC clock cycles
|
||||
//ADC1->SMPR = ADC_SMPR_SMP_1; // for normal ADC OK
|
||||
ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; // necessary for internal reference and temperature
|
||||
|
||||
// Select ADC channels based on setup in config.h
|
||||
ADC1->CHSELR = ADC_CHSEL;
|
||||
|
||||
// Enable internal voltage reference and temperature sensor
|
||||
// ToDo check sample rate
|
||||
ADC->CCR |= ADC_CCR_TSEN | ADC_CCR_VREFEN;
|
||||
}
|
||||
|
||||
#if defined(STM32F0)
|
||||
|
||||
void adc_timer_start(int freq_Hz) // max. 10 kHz
|
||||
{
|
||||
// Enable TIM15 clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM15->PSC = SystemCoreClock / 10000 - 1;
|
||||
|
||||
// Interrupt on timer update
|
||||
TIM15->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM15->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 2 = second-lowest priority of STM32L0/F0
|
||||
NVIC_SetPriority(TIM15_IRQn, 2);
|
||||
NVIC_EnableIRQ(TIM15_IRQn);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM15->CR1 |= TIM_CR1_CEN;
|
||||
}
|
||||
|
||||
extern "C" void TIM15_IRQHandler(void)
|
||||
{
|
||||
TIM15->SR &= ~(1 << 0);
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
#elif defined(STM32L0)
|
||||
|
||||
void adc_timer_start(int freq_Hz) // max. 10 kHz
|
||||
{
|
||||
// Enable TIM6 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM6->PSC = SystemCoreClock / 10000 - 1;
|
||||
|
||||
// Interrupt on timer update
|
||||
TIM6->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM6->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 2 = second-lowest priority of STM32L0/F0
|
||||
NVIC_SetPriority(TIM6_IRQn, 2);
|
||||
NVIC_EnableIRQ(TIM6_IRQn);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM6->CR1 |= TIM_CR1_CEN;
|
||||
}
|
||||
|
||||
extern "C" void TIM6_IRQHandler(void)
|
||||
{
|
||||
TIM6->SR &= ~(1 << 0);
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* __MBED__ */
|
||||
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "adc_dma.h"
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
|
||||
#include <zephyr.h>
|
||||
|
||||
#include "pcb.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
#include <stm32g4xx_ll_system.h>
|
||||
#endif
|
||||
|
||||
// for ADC and DMA
|
||||
extern uint16_t adc_readings[];
|
||||
|
||||
void adc_update_value(unsigned int pos);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
void dma_setup()
|
||||
{
|
||||
//__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
/* Enable the peripheral clock on DMA */
|
||||
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
||||
|
||||
/* Enable DMA transfer on ADC and circular mode */
|
||||
ADC1->CFGR1 |= ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG;
|
||||
|
||||
/* Configure the peripheral data register address */
|
||||
DMA1_Channel1->CPAR = (uint32_t)(&(ADC1->DR));
|
||||
|
||||
/* Configure the memory address */
|
||||
DMA1_Channel1->CMAR = (uint32_t)(&(adc_readings[0]));
|
||||
|
||||
/* Configure the number of DMA tranfer to be performed on DMA channel 1 */
|
||||
DMA1_Channel1->CNDTR = NUM_ADC_CH;
|
||||
|
||||
/* Configure increment, size, interrupts and circular mode */
|
||||
DMA1_Channel1->CCR =
|
||||
DMA_CCR_MINC | /* memory increment mode enabled */
|
||||
DMA_CCR_MSIZE_0 | /* memory size 16-bit */
|
||||
DMA_CCR_PSIZE_0 | /* peripheral size 16-bit */
|
||||
DMA_CCR_TEIE | /* transfer error interrupt enable */
|
||||
DMA_CCR_TCIE | /* transfer complete interrupt enable */
|
||||
DMA_CCR_CIRC; /* circular mode enable */
|
||||
/* DIR = 0: read from peripheral */
|
||||
|
||||
/* Enable DMA Channel 1 */
|
||||
DMA1_Channel1->CCR |= DMA_CCR_EN;
|
||||
|
||||
/* 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);
|
||||
|
||||
// Trigger ADC conversions
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
#endif
|
||||
|
||||
ADC_HandleTypeDef hadc = {0};
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
// Configure ADC object structures
|
||||
hadc.Instance = ADC1;
|
||||
hadc.State = HAL_ADC_STATE_RESET;
|
||||
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||
hadc.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
hadc.Init.DataAlign = ADC_DATAALIGN_LEFT; // for exponential moving average filter
|
||||
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
hadc.Init.LowPowerAutoWait = DISABLE;
|
||||
hadc.Init.LowPowerAutoPowerOff = DISABLE;
|
||||
hadc.Init.ContinuousConvMode = DISABLE;
|
||||
hadc.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
hadc.Init.DMAContinuousRequests = ENABLE; //DISABLE;
|
||||
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
|
||||
|
||||
if (HAL_ADC_Init(&hadc) != HAL_OK) {
|
||||
print_error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
#if defined(STM32L0)
|
||||
HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);
|
||||
#else
|
||||
HAL_ADCEx_Calibration_Start(&hadc);
|
||||
#endif
|
||||
|
||||
// Configure ADC channel
|
||||
sConfig.Channel = ADC_CHANNEL_0; // can be any channel for initialization
|
||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||
|
||||
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
|
||||
hadc.Instance->CHSELR = 0;
|
||||
|
||||
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
|
||||
print_error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
HAL_ADC_Start(&hadc); // Start conversion
|
||||
|
||||
// Read out value one time to finish ADC configuration
|
||||
if (HAL_ADC_PollForConversion(&hadc, 10) == HAL_OK) {
|
||||
HAL_ADC_GetValue(&hadc);
|
||||
}
|
||||
|
||||
// ADC sampling time register
|
||||
// 000: 1.5 ADC clock cycles
|
||||
// 001: 7.5 ADC clock cycles
|
||||
// 010: 13.5 ADC clock cycles
|
||||
// 011: 28.5 ADC clock cycles
|
||||
// 100: 41.5 ADC clock cycles
|
||||
// 101: 55.5 ADC clock cycles
|
||||
// 110: 71.5 ADC clock cycles
|
||||
// 111: 239.5 ADC clock cycles
|
||||
//ADC1->SMPR = ADC_SMPR_SMP_1; // for normal ADC OK
|
||||
ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; // necessary for internal reference and temperature
|
||||
|
||||
// Select ADC channels based on setup in config.h
|
||||
ADC1->CHSELR = ADC_CHSEL;
|
||||
|
||||
// Enable internal voltage reference and temperature sensor
|
||||
// ToDo check sample rate
|
||||
ADC->CCR |= ADC_CCR_TSEN | ADC_CCR_VREFEN;
|
||||
}
|
||||
|
||||
void adc_trigger_conversion(struct k_timer *timer_id)
|
||||
{
|
||||
ADC1->CR |= ADC_CR_ADSTART;
|
||||
}
|
||||
|
||||
#endif // __ZEPHYR__
|
||||
+16
-1
@@ -27,6 +27,7 @@
|
||||
#include "leds.h" // LED switching using charlieplexing
|
||||
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
|
||||
#include "data_objects.h" // for access to internal data via ThingSet
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
|
||||
PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT)
|
||||
|
||||
@@ -96,6 +97,18 @@ void main(void)
|
||||
|
||||
printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD);
|
||||
|
||||
// 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();
|
||||
|
||||
// initialize all extensions and external communication interfaces
|
||||
ext_mgr.enable_all();
|
||||
|
||||
@@ -107,6 +120,9 @@ void main(void)
|
||||
leds_update_1s();
|
||||
leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC));
|
||||
|
||||
update_measurements();
|
||||
printf("Vbat: %f\n", bat_terminal.voltage);
|
||||
|
||||
cnt++;
|
||||
k_sleep(1000);
|
||||
}
|
||||
@@ -122,7 +138,6 @@ void ext_mgr_thread()
|
||||
if (now >= last_call + 1) {
|
||||
last_call = now;
|
||||
ext_mgr.process_1s();
|
||||
printf("%d\n", now);
|
||||
}
|
||||
cnt++;
|
||||
k_sleep(1);
|
||||
|
||||
@@ -93,7 +93,7 @@ extern const int led_pins[CONFIG_NUM_LED_PINS];
|
||||
#endif // MBED or ZEPHYR
|
||||
|
||||
#ifndef LEDS_WRONG_POLARITY
|
||||
static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED1
|
||||
{ PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED2
|
||||
{ PIN_HIGH, PIN_FLOAT, PIN_LOW }, // LED3
|
||||
@@ -102,7 +102,7 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
};
|
||||
#else
|
||||
// LEDs with wrong polarity
|
||||
static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED1
|
||||
{ PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED2
|
||||
{ PIN_LOW, PIN_FLOAT, PIN_HIGH }, // LED3
|
||||
|
||||
@@ -12,6 +12,7 @@ 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/device_status.cpp)
|
||||
|
||||
Reference in New Issue
Block a user