diff options
| author | dmlunar <root@lunar.sh> | 2025-01-22 16:47:21 +0200 |
|---|---|---|
| committer | dmlunar <root@lunar.sh> | 2025-10-15 23:42:50 +0200 |
| commit | 729f2a2c3ebfb2612d873caf453a1d7ca02180d9 (patch) | |
| tree | 7bab2fcc0c7f50eab3013348697bc06ddd71d551 /firmware/src/module/drain.c | |
| download | varpa-729f2a2c3ebfb2612d873caf453a1d7ca02180d9.tar.gz varpa-729f2a2c3ebfb2612d873caf453a1d7ca02180d9.zip | |
varpa: initial public commit
Diffstat (limited to 'firmware/src/module/drain.c')
| -rw-r--r-- | firmware/src/module/drain.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/firmware/src/module/drain.c b/firmware/src/module/drain.c new file mode 100644 index 0000000..1cb6110 --- /dev/null +++ b/firmware/src/module/drain.c @@ -0,0 +1,123 @@ +/** + * + * Author: Dylan Muller + * Copyright (c) 2025 + * All rights reserved. + * + * - Commercial/IP use prohibited. + * - Attribution required. + * See License.txt + * + */ + +#include "setup.h" + +#include "peripheral/timer.h" + +#include "driver/mcp3202.h" +#include "driver/mcp4725.h" +#include "driver/si4468.h" + +#include "module/gate.h" +#include "module/drain.h" + +#include "util.h" +#include "status.h" + +#include <stdlib.h> +#include <stdio.h> +#include <avr/interrupt.h> +#include <util/delay.h> + +extern t_drain_status drain_status; +extern t_system_status system_status; + +uint16_t drain_sweep_pwr( + int16_t target_fw_pwr +) +{ + uint16_t drain_count = 0x0; + uint16_t drain_inc = 0x0; + uint16_t fw_pwr_adc = 0x0; + int16_t fw_pwr = 0x0; + uint16_t test_count = 0x0; + int16_t test_boundary = DRAIN_TEST_BOUNDARY; + + system_status.status = SYSTEM_STATUS_BUSY; + drain_status.status = DRAIN_STATUS_POWER_CONTROL; + + drain_count = dac_read(MCP4725_ID_DRAIN_VOLT, 1); + _delay_ms(TRANSACT_DELAY_MS); + + si4468_tx_mode(); + _delay_ms(TRANSACT_DELAY_MS); + + dac_write(MCP4725_ID_DRAIN_VOLT, drain_count, 1); + _delay_ms(TRANSACT_DELAY_MS); + + fw_pwr_adc = adc_read_n(MCP3202_ID_FW_POWER, 10); + util_count_to_pwr(fw_pwr_adc, &fw_pwr); + + _delay_ms(TRANSACT_DELAY_MS); + + timer_reset(); + uint32_t time_start = timer_millis(); + bool timeout = false; + + while ((test_count < DRAIN_TEST_COUNT) && + (drain_count < DRAIN_PROBE_LIMIT)) + { + if ((timer_millis() - time_start) > DRAIN_SETPOINT_TIMEOUT_MS) + { + timeout = true; + break; + } + + if (abs(fw_pwr - target_fw_pwr) > DRAIN_PROBE_INC_THRESHOLD) + { + drain_inc = DRAIN_PROBE_INC_COARSE; + } + else + { + drain_inc = DRAIN_PROBE_INC_FINE; + } + + if (fw_pwr < (target_fw_pwr)) + { + drain_count += drain_inc; + } + else if (fw_pwr > (target_fw_pwr)) + { + drain_count -= drain_inc; + } + + dac_write(MCP4725_ID_DRAIN_VOLT, drain_count, 1); + _delay_us(DRAIN_LOOP_DELAY_US); + + fw_pwr_adc = adc_read_n(MCP3202_ID_FW_POWER, DRAIN_POWER_SAMPLES); + util_count_to_pwr(fw_pwr_adc, &fw_pwr); + + if (fw_pwr <= (target_fw_pwr + test_boundary) && + fw_pwr >= (target_fw_pwr - test_boundary)) + { + test_count += 1; + } + } + + system_status.d_time = (uint16_t)(timer_millis() - time_start); + + if (timeout) + { + system_status.status = SYSTEM_STATUS_SETPOINT_TIMEOUT; + } + else if(test_count >= DRAIN_TEST_COUNT) + { + system_status.status = SYSTEM_STATUS_SETPOINT_REACHED; + } + else + { + system_status.status = SYSTEM_STATUS_SETPOINT_FAIL; + } + + return test_count; +} |
