TinyFanControl-Firmware/src/temp_sensor.c

211 lines
5.8 KiB
C

#include <math.h>
#include "temp_sensor.h"
#include "fxp.h"
#include <libopencm3/cm3/common.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/dma.h>
#define ADC_NUM_CHANNELS 1
static volatile int16_t adc_values[ADC_NUM_CHANNELS];
static fxp_t m_temperature_internal = 0;
static fxp_t m_temperature_ntc = 0;
/* Temperature sensor calibration value address */
#define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
#define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
#define VDD_CALIB ((int32_t) (330)) /* calibration voltage = 3,30V - DO NOT CHANGE */
#define VDD_APPLI ((int32_t) (330)) /* actual supply voltage */
/* function for temperature conversion */
static fxp_t calc_temperature(uint16_t adc_val)
{
int32_t temperature_raw = ((int32_t)adc_val * VDD_APPLI / VDD_CALIB)
- (int32_t)(*TEMP30_CAL_ADDR);
fxp_t temperature = FXP_FROM_INT(temperature_raw);
fxp_t scale_dividend = FXP_FROM_INT(110 - 30);
fxp_t scale_divisor = FXP_FROM_INT((int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR));
fxp_t scale = fxp_div(scale_dividend, scale_divisor);
return fxp_add(fxp_mult(temperature, scale), FXP_FROM_INT(30));
}
static fxp_t adc_val_to_pin_voltage(uint16_t adc_val)
{
return fxp_div(
fxp_mult(FXP_FROM_INT((int32_t)adc_val), fxp_div(FXP_FROM_INT(33), FXP_FROM_INT(10))),
FXP_FROM_INT(4096));
}
static fxp_t calc_temperature_ntc(uint16_t adc_val)
{
// note: all resistor values in kΩ! The factor 1000 is removed from the numbers!
static const fxp_t ln_r_ntc_nom = 252323; // ln(47 kΩ) converted to fxp_t with 16 fractional bits
static const fxp_t r1 = FXP_FROM_INT(10);
static const fxp_t b_constant = FXP_FROM_INT(4450);
static const fxp_t ntc_temp_nom_inv = 220; // 1/(273.15+25) * 2**16
static const fxp_t celsius2kelvin = 17901158; // (273.15) * 2**16
fxp_t v_r1 = adc_val_to_pin_voltage(adc_val);
fxp_t v_ref = fxp_div(FXP_FROM_INT(33), FXP_FROM_INT(10));
fxp_t r_ntc = fxp_div(fxp_mult(fxp_sub(v_ref, v_r1), r1), v_r1);
fxp_t ln_r_ntc = fxp_from_float(logf(fxp_to_float(r_ntc)));
fxp_t temp_k = fxp_div(FXP_FROM_INT(1),
fxp_add(
fxp_div(
fxp_sub(
ln_r_ntc,
ln_r_ntc_nom),
b_constant),
ntc_temp_nom_inv));
return fxp_sub(temp_k, celsius2kelvin);
}
void temp_sensor_init(void)
{
uint8_t channels[ADC_NUM_CHANNELS] = {
0, // The NTC is connected to ADC_IN0
//ADC_CHANNEL_TEMP // Temperature sensor
};
// Prepare the ADC
adc_power_off(ADC1);
// enable the temperature sensor
adc_enable_temperature_sensor();
// configure ADC
//adc_enable_scan_mode(ADC1);
//adc_set_clk_source(ADC1, ADC_CLKSOURCE_PCLK_DIV2); // -> 1.05 MHz @ 2.1 MHz ABP
ADC_CFGR2(ADC1) = ADC_CFGR2_CKMODE_PCLK_DIV2;
adc_set_single_conversion_mode(ADC1);
adc_set_resolution(ADC1, ADC_CFGR1_RES_12_BIT);
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_12DOT5CYC);
//adc_disable_external_trigger_regular(ADC1);
adc_set_right_aligned(ADC1);
adc_set_regular_sequence(ADC1, ADC_NUM_CHANNELS, channels);
adc_calibrate(ADC1);
// configure DMA for ADC
//nvic_enable_irq(NVIC_DMA1_CHANNEL1_IRQ);
dma_channel_reset(DMA1, DMA_CHANNEL1);
dma_set_priority(DMA1, DMA_CHANNEL1, DMA_CCR_PL_LOW);
dma_set_memory_size(DMA1, DMA_CHANNEL1, DMA_CCR_MSIZE_16BIT);
dma_set_peripheral_size(DMA1, DMA_CHANNEL1, DMA_CCR_PSIZE_16BIT);
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL1);
dma_enable_circular_mode(DMA1, DMA_CHANNEL1);
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL1);
dma_set_peripheral_address(DMA1, DMA_CHANNEL1, (uint32_t) &ADC_DR(ADC1));
/* The array adc_values[] is filled with the waveform data to be output */
dma_set_memory_address(DMA1, DMA_CHANNEL1, (uint32_t) adc_values);
dma_set_number_of_data(DMA1, DMA_CHANNEL1, ADC_NUM_CHANNELS);
//dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL1);
dma_enable_channel(DMA1, DMA_CHANNEL1);
adc_enable_dma(ADC1);
adc_power_on(ADC1);
}
void temp_sensor_trigger_update(void)
{
// start the ADC conversion sequency. The result will be transferred to RAM
// by the DMA.
adc_start_conversion_regular(ADC1);
}
bool temp_sensor_has_new_values(void)
{
if(dma_get_interrupt_flag(DMA1, DMA_CHANNEL1, DMA_TCIF)) {
dma_clear_interrupt_flags(DMA1, DMA_CHANNEL1, DMA_TCIF);
// calculate temperature values and cache them.
m_temperature_ntc = calc_temperature_ntc(adc_values[0]);
//m_temperature_internal = calc_temperature(adc_values[1]);
return true;
} else {
return false;
}
}
fxp_t temp_sensor_get_ntc_temperature(void)
{
return m_temperature_ntc;
}
fxp_t temp_sensor_get_internal_temperature(void)
{
return m_temperature_internal;
}
// BEGIN temporary import from libopencm3
void adc_set_regular_sequence(uint32_t adc, uint8_t length, uint8_t channel[])
{
uint32_t reg32 = 0;
uint8_t i = 0;
bool stepup = false, stepdn = false;
if (length == 0) {
ADC_CHSELR(adc) = 0;
return;
}
reg32 |= (1 << channel[0]);
for (i = 1; i < length; i++) {
reg32 |= (1 << channel[i]);
stepup |= channel[i-1] < channel[i];
stepdn |= channel[i-1] > channel[i];
}
/* Check, if the channel list is in order */
if (stepup && stepdn) {
//cm3_assert_not_reached();
}
/* Update the scan direction flag */
if (stepdn) {
ADC_CFGR1(adc) |= ADC_CFGR1_SCANDIR;
} else {
ADC_CFGR1(adc) &= ~ADC_CFGR1_SCANDIR;
}
ADC_CHSELR(adc) = reg32;
}
/*---------------------------------------------------------------------------*/
/** @brief ADC Set the Sample Time for All Channels
*
* The sampling time can be selected in ADC clock cycles from 1.5 to 239.5,
* same for all channels.
*
* @param[in] adc Unsigned int32. ADC base address (@ref adc_reg_base)
* @param[in] time Unsigned int8. Sampling time selection (@ref adc_api_smptime)
*/
void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
{
ADC_SMPR1(adc) = time;
}
// END temporary import from libopencm3