aboutsummaryrefslogtreecommitdiff
path: root/hw/thefreeman
diff options
context:
space:
mode:
authorSelene ToyKeeper2023-11-21 02:52:00 -0700
committerSelene ToyKeeper2023-11-21 02:52:00 -0700
commitfdb47b96e86924bf81852205b2b3078b8b21d040 (patch)
treeed8337b4619bb86ffb40c63767a32ae58e9b4dcc /hw/thefreeman
parent1.55V AA battery should not show as "white" voltage color, only purple (diff)
downloadanduril-fdb47b96e86924bf81852205b2b3078b8b21d040.tar.gz
anduril-fdb47b96e86924bf81852205b2b3078b8b21d040.tar.bz2
anduril-fdb47b96e86924bf81852205b2b3078b8b21d040.zip
got ADC voltage+temp working on avrdd... but broke all other builds/MCUs
This patch changes the ADC code to use two internal standard units, and everything else must convert to these units: - FSM Volts: centiVolts << 6 (range 0 to 10.24 V per cell) - FSM Kelvin: Kelvin << 6 (range 0 to 1024 K) UI-level voltage is still "Volts * 10", and temperature is still Celsius. FSM expects functions to be provided, to convert from the hardware's raw ADC measurements to these internal units: `voltage_raw2cooked()` and `temp_raw2cooked()`. Defaults will be provided by arch/*.[ch] for each MCU type, or the hwdef can make its own. Anyway, gotta go fix all the other MCUs and builds now. :(
Diffstat (limited to 'hw/thefreeman')
-rw-r--r--hw/thefreeman/avr32dd20-devkit/hwdef.c13
-rw-r--r--hw/thefreeman/avr32dd20-devkit/hwdef.h12
2 files changed, 22 insertions, 3 deletions
diff --git a/hw/thefreeman/avr32dd20-devkit/hwdef.c b/hw/thefreeman/avr32dd20-devkit/hwdef.c
index e5b347f..133baca 100644
--- a/hw/thefreeman/avr32dd20-devkit/hwdef.c
+++ b/hw/thefreeman/avr32dd20-devkit/hwdef.c
@@ -103,3 +103,16 @@ bool gradual_tick_main(uint8_t gt) {
return false; // not done yet
}
+
+uint16_t voltage_raw2cooked(uint16_t measurement) {
+ // In : 65535 * BATTLVL pin / 1.024 Vref
+ // Out: 65535 * (Vbat / 10) / 1.024V (i.e. FSM Volt units)
+ // BATTLVL = Vbat * (100.0/(330+100)) = Vbat / 4.3
+ // So, Out = In * 4.3 / 10.24
+ // (plus 1.5% based on measured hardware)
+ // (plus a fudge factor of +0.04V to round up to nearest 1/10th Volt)
+ uint16_t result = ((uint32_t)measurement * 436 / 1024)
+ + (65535 * 4 / 1024);
+ return result;
+}
+
diff --git a/hw/thefreeman/avr32dd20-devkit/hwdef.h b/hw/thefreeman/avr32dd20-devkit/hwdef.h
index 7e093f9..7e1ad3d 100644
--- a/hw/thefreeman/avr32dd20-devkit/hwdef.h
+++ b/hw/thefreeman/avr32dd20-devkit/hwdef.h
@@ -35,7 +35,7 @@
* and low value Rsense (high current range, pin high)
* IN- NFET : pull up after BST enable to eliminate startup flash, pull down otherwise
* CH senses the status of the onboard charger
- * LVL : ??? (unused?)
+ * BATT LVL : Vbat * (100.0/(330+100))
* LVB is for OTSM firmware, not used here
*/
@@ -109,8 +109,14 @@ enum CHANNEL_MODES {
// TODO: define stuff for the voltage divider
// AVR datasheet table 3.1 I/O Multiplexing, PA6 ADC0 = AIN26
-//#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is regulated
+#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is regulated
#define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN26_gc
+#define DUAL_VOLTAGE_FLOOR 21 // for AA/14500 boost drivers, don't indicate low voltage if below this level
+#define DUAL_VOLTAGE_LOW_LOW 7 // the lower voltage range's danger zone 0.7 volts (NiMH)
+// convert BATT LVL pin readings to FSM volt units
+#undef voltage_raw2cooked
+uint16_t voltage_raw2cooked(uint16_t measurement);
+
// average drop across diode on this hardware
#ifndef VOLTAGE_FUDGE_FACTOR
@@ -148,7 +154,7 @@ inline void hwdef_setup() {
//PORTA.PIN3CTRL = PORT_PULLUPEN_bm; // CH
PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
PORTA.PIN5CTRL = PORT_PULLUPEN_bm;
- PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
+ //PORTA.PIN6CTRL = PORT_PULLUPEN_bm; // BATT LVL
//PORTA.PIN7CTRL = PORT_PULLUPEN_bm; // HDR
//PORTC.PIN0CTRL = PORT_PULLUPEN_bm; // doesn't exist
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.