From fbcac59563c32f14de4861449c1b267c247b5b78 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Sun, 16 Apr 2023 18:21:29 -0600 Subject: reduced ROM by ~600 bytes by moving all eeprom config values to a "cfg" struct (this also made some parts of the code cleaner) --- spaghetti-monster/fsm-adc.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'spaghetti-monster/fsm-adc.h') diff --git a/spaghetti-monster/fsm-adc.h b/spaghetti-monster/fsm-adc.h index db2bb7b..77f625a 100644 --- a/spaghetti-monster/fsm-adc.h +++ b/spaghetti-monster/fsm-adc.h @@ -63,9 +63,14 @@ void adc_deferred(); // do the actual ADC-related calculations static inline void ADC_voltage_handler(); uint8_t voltage = 0; #ifdef USE_VOLTAGE_CORRECTION -// same 0.05V units as fudge factor, -// but 7 is neutral, and the expected range is from 1 to 13 -uint8_t voltage_correction = 7; + #ifdef USE_CFG + #define VOLT_CORR cfg.voltage_correction + #else + // same 0.05V units as fudge factor, + // but 7 is neutral, and the expected range is from 1 to 13 + uint8_t voltage_correction = 7; + #define VOLT_CORR voltage_correction + #endif #endif #ifdef USE_LVP void low_voltage(); @@ -98,8 +103,15 @@ void battcheck(); #endif // temperature now, in C (ish) int16_t temperature; -uint8_t therm_ceil = DEFAULT_THERM_CEIL; -int8_t therm_cal_offset = 0; +#ifdef USE_CFG + #define TH_CEIL cfg.therm_ceil + #define TH_CAL cfg.therm_cal_offset +#else + #define TH_CEIL therm_ceil + #define TH_CAL therm_cal_offset + uint8_t therm_ceil = DEFAULT_THERM_CEIL; + int8_t therm_cal_offset = 0; +#endif static inline void ADC_temperature_handler(); #endif // ifdef USE_THERMAL_REGULATION -- cgit v1.2.3 From 583854e37efde7f461e073e735a1736b02d28c70 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Mon, 17 Apr 2023 00:08:32 -0600 Subject: switched the rest of FSM + Anduril to use SPDX license headers instead of full GPL headers (or all too often, nothing at all) There are a few "FIXME" entries where I'm not sure about the correct copyright. --- spaghetti-monster/fsm-adc.h | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'spaghetti-monster/fsm-adc.h') diff --git a/spaghetti-monster/fsm-adc.h b/spaghetti-monster/fsm-adc.h index 77f625a..16666f9 100644 --- a/spaghetti-monster/fsm-adc.h +++ b/spaghetti-monster/fsm-adc.h @@ -1,25 +1,8 @@ -/* - * fsm-adc.h: ADC (voltage, temperature) functions for SpaghettiMonster. - * - * Copyright (C) 2017 Selene ToyKeeper - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef FSM_ADC_H -#define FSM_ADC_H +// fsm-adc.h: ADC (voltage, temperature) functions for SpaghettiMonster. +// Copyright (C) 2017-2023 Selene ToyKeeper +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once #if defined(USE_LVP) || defined(USE_THERMAL_REGULATION) // use raw value instead of lowpassed value for the next N measurements @@ -120,5 +103,3 @@ inline void ADC_on(); inline void ADC_off(); inline void ADC_start_measurement(); - -#endif -- cgit v1.2.3 From 847485ca96a6ef99abd19984ec025cf1d6841d3d Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Tue, 25 Apr 2023 23:28:30 -0600 Subject: fixed bug behind K93_LOCKOUT_KLUDGE which could exit lockout in solid aux mode (also reduced avg standby power by about 15 uA) (also fixed oscillating voltage mode colors, I think) The bug happened because sometimes sleep LVP would get triggered, because the ADC would read zero under some conditions. This in turn happened because the ADC needs the MCU to be at least partially awake in order to finish a measurement. So in standby mode, with the MCU only waking up very briefly to send a sleep tick and go back to sleep, the ADC required several cycles (like 375ms to 625ms) to finish a single measurement. This varied depending on how many instructions the MCU executed while it was awake. In the single-color mode, so few instructions were being executed that the ADC seemed to time out and abort its measurement, returning a zero. Then a low voltage warning was sent, which knocked the light back into "Off" mode. Adding no-op instructions inside the single-color clause was sufficient to prevent the ADC from timing out, because it kept the MCU awake just barely long enough. But it was a kludge, and it still took like half a second to finish a measurement, and the measurements were noisy. It also used more power, because it required keeping the ADC powered on far too long. This fix puts the MCU into "ADC Noise Reduction" mode instead, when a voltage measurement is needed during sleep. It reduces noise to make measurements more stable... but more importantly, it lets the measurement finish in like 0.5ms instead of 500ms. So it uses less power and isn't dependent on the number of calculations the MCU does during each "sleep tick". As a bonus, this can also measure voltage much more often, while still using less total energy than before. It was once every 8 seconds, and now it's once per second. Avg power use in aux low mode, on a D4Sv2: (avg of 30k samples each) - before: 101 uA - after: 86 uA --- spaghetti-monster/fsm-adc.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'spaghetti-monster/fsm-adc.h') diff --git a/spaghetti-monster/fsm-adc.h b/spaghetti-monster/fsm-adc.h index 16666f9..b25b650 100644 --- a/spaghetti-monster/fsm-adc.h +++ b/spaghetti-monster/fsm-adc.h @@ -32,6 +32,9 @@ volatile uint8_t adc_reset = 2; #endif #endif +#ifdef TICK_DURING_STANDBY +volatile uint8_t adc_active_now = 0; // sleep LVP needs a different sleep mode +#endif volatile uint8_t irq_adc = 0; // ADC interrupt happened? uint8_t adc_sample_count = 0; // skip the first sample; it's junk uint8_t adc_channel = 0; // 0=voltage, 1=temperature -- cgit v1.2.3 From aa4c627377b07cc07cd1e904f9f7d2f24c1155a4 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Wed, 26 Apr 2023 01:34:53 -0600 Subject: made sleep voltage work on attiny1616 again (oops, it has no "ADC Noise Reduction" mode... needs different setup code) --- spaghetti-monster/fsm-adc.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spaghetti-monster/fsm-adc.h') diff --git a/spaghetti-monster/fsm-adc.h b/spaghetti-monster/fsm-adc.h index b25b650..1bb67ed 100644 --- a/spaghetti-monster/fsm-adc.h +++ b/spaghetti-monster/fsm-adc.h @@ -106,3 +106,7 @@ inline void ADC_on(); inline void ADC_off(); inline void ADC_start_measurement(); +#ifdef TICK_DURING_STANDBY +inline void adc_sleep_mode(); +#endif + -- cgit v1.2.3