diff --git a/src/main.c b/src/main.c index 2301604..0f99867 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,7 @@ enum OperState { volatile int wait_frame = 1; -#define ADC_NUM_CHANNELS 3 +#define ADC_NUM_CHANNELS 4 volatile int16_t adc_values[ADC_NUM_CHANNELS]; static void unlock_rtc_access(void) @@ -198,11 +198,15 @@ static void init_adc(void) uint8_t channels[ADC_NUM_CHANNELS] = { 0, // VInSense 1, // VOutSense - 2 // CurrentSense + 2, // CurrentSense + 16 // Temperature sensor }; adc_power_off(ADC1); + // enable the temperature sensor + adc_enable_temperature_sensor(); + // configure ADC //adc_enable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); @@ -325,11 +329,32 @@ static void init_systick(int freq) } #endif +/* 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 ((uint16_t) (330)) /* calibration voltage = 3,30V - DO NOT CHANGE */ +#define VDD_APPLI ((uint16_t) (330)) /* actual supply voltage */ + +/* function for temperature conversion */ +static fxp_t calc_temperature(uint16_t adc_val) +{ + fxp_t temperature = fxp_from_int( + ((int32_t)adc_val * VDD_APPLI / VDD_CALIB) - + (int32_t)*TEMP30_CAL_ADDR); + + temperature = fxp_mult(temperature, fxp_from_int(110 - 30)); + temperature = fxp_div(temperature, fxp_from_int(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR)); + + return fxp_add(temperature, fxp_from_int(30)); +} + struct PowerState { fxp_t vin, vin_avg; fxp_t vout, vout_avg; fxp_t current, current_avg; + fxp_t temp, temp_avg; // junction temperature + fxp_t power_avg; }; @@ -358,6 +383,10 @@ static void report_status(struct PowerState *power_state, fxp_format_int((int32_t)operState, number); debug_send_string(number); + debug_send_string(":"); + + fxp_format(power_state->temp_avg, number, 1); + debug_send_string(number); debug_send_string("\r\n"); } @@ -505,7 +534,7 @@ static void load_off(void) int main(void) { - uint32_t cpuload = 0; + //uint32_t cpuload = 0; uint64_t timebase_ms = 0; uint32_t time_in_state = 0; char msg[128]; @@ -577,6 +606,7 @@ int main(void) power_state.vin_avg = 0; power_state.vout_avg = 0; power_state.current_avg = 0; + power_state.temp_avg = fxp_from_int(-999); mpp_state.mppMaxPWM = CONV_PWM_MAX; @@ -624,6 +654,13 @@ int main(void) lcd_send_string(msg); lcd_send_string("V "); + lcd_set_cursor_pos(0, 0); + + fxp_format(power_state.temp_avg, number, 1); + fxp_right_align(number, msg, 4, ' '); + lcd_send_string(msg); + lcd_send_string("C"); + lcd_set_cursor_pos(0, 10); fxp_format(power_state.power_avg, number, 2); @@ -633,19 +670,6 @@ int main(void) force_display_update_time += 500; } - - if((timebase_ms % 1000) == 10) { - cpuload /= 1000; - - lcd_set_cursor_pos(0, 0); - - fxp_format_int((int32_t)cpuload, number); - fxp_right_align(number, msg, 3, '0'); - lcd_send_string("L:"); - lcd_send_string(msg); - - cpuload = 0; - } } @@ -674,9 +698,12 @@ int main(void) power_state.current = fxp_add(fxp_mult(fxp_from_int(adc_values[2]), ADC2CURRENT_M), ADC2CURRENT_T); } + power_state.temp = calc_temperature(adc_values[3]); + power_state.vin_avg = fxp_add(fxp_mult(power_state.vin, AVG_FACT), fxp_mult(power_state.vin_avg, AVG_FACT_INV)); power_state.vout_avg = fxp_add(fxp_mult(power_state.vout, AVG_FACT), fxp_mult(power_state.vout_avg, AVG_FACT_INV)); power_state.current_avg = fxp_add(fxp_mult(power_state.current, AVG_FACT), fxp_mult(power_state.current_avg, AVG_FACT_INV)); + power_state.temp_avg = fxp_add(fxp_mult(power_state.temp, AVG_FACT), fxp_mult(power_state.temp_avg, AVG_FACT_INV)); power_state.power_avg = fxp_mult(power_state.vout_avg, power_state.current_avg); @@ -972,8 +999,18 @@ int main(void) report_status(&power_state, pwm, operState); } + /* + if((timebase_ms % 1000) == 10) { + cpuload /= 1000; + + // use CPU load values here + + cpuload = 0; + } + // cpu load = timer1 value after main loop operations cpuload += timer_get_counter(TIM3); + */ timebase_ms++;