Compare commits

...

2 commits

Author SHA1 Message Date
Stephan I. Böttcher
f692d97cf0 POWER_LED and POWER_SYDBY 2024-04-11 21:22:53 +02:00
Stephan I. Böttcher
be5c18ca52 uart: fix uart_busy(), we were powering down too early 2024-04-11 21:22:05 +02:00
3 changed files with 36 additions and 14 deletions

View file

@ -410,7 +410,11 @@ int main()
sleep_enable(); sleep_enable();
sei(); sei();
send_str("\nV Turbo Weather V0.03\n"); uint8_t reset_source = RSTCTRL.RSTFR;
RSTCTRL.RSTFR = reset_source;
send_str("\nV Turbo Weather V0.04\nR ");
send_hex_byte(reset_source);
send_char('\n');
uint8_t test_calib = config.calib_test; uint8_t test_calib = config.calib_test;
if (test_calib > N_TESTDATA) if (test_calib > N_TESTDATA)
@ -467,11 +471,15 @@ int main()
} }
} }
if (!(trigger & config.triggers)) { if (!(trigger & config.triggers)) {
if (config.power & POWER_DOWN) { if (config.power & (POWER_DOWN|POWER_STDBY)) {
mclk(0); mclk(0);
if (config.power & POWER_DOWN) {
if (config.power & POWER_DOWN_CLI) if (config.power & POWER_DOWN_CLI)
cli(); cli();
set_sleep_mode(SLEEP_MODE_PWR_DOWN); set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
else
set_sleep_mode(SLEEP_MODE_STANDBY);
sleep_enable(); sleep_enable();
sleep_cpu(); sleep_cpu();
set_sleep_mode(SLEEP_MODE_IDLE); set_sleep_mode(SLEEP_MODE_IDLE);
@ -480,12 +488,12 @@ int main()
} }
rfen(1); rfen(1);
if (config.power & POWER_LED)
led(1); led(1);
if (!mclk_status()) { if (!mclk_status())
mclk(1);
mclk_delay = config.mclk_delay; mclk_delay = config.mclk_delay;
} mclk(1);
if (!tick) if (!tick)
continue; continue;
@ -503,10 +511,10 @@ int main()
if (config.send & SEND_ADC) if (config.send & SEND_ADC)
start_adc(); start_adc();
if (config.send & SEND_CLOCK) {
cli(); cli();
uint32_t time = clock; uint32_t time = clock;
sei(); sei();
if (config.send & SEND_CLOCK) {
send_str("T 0x"); send_str("T 0x");
send_hex_long(time); send_hex_long(time);
send_char('\n'); send_char('\n');

View file

@ -36,6 +36,8 @@ enum power_flags {
POWER_DOWN = 0x01, POWER_DOWN = 0x01,
POWER_DOWN_CLI = 0x02, POWER_DOWN_CLI = 0x02,
STOP_MCLK = 0x04, STOP_MCLK = 0x04,
POWER_LED = 0x08,
POWER_STDBY = 0x10,
}; };
enum send_flags { enum send_flags {
SEND_CONFIG = 0x01, SEND_CONFIG = 0x01,

View file

@ -56,6 +56,7 @@ uint8_t uart_tick()
uint8_t uart_tx[128]; uint8_t uart_tx[128];
volatile uint8_t uart_tx_w; volatile uint8_t uart_tx_w;
volatile uint8_t uart_tx_r; volatile uint8_t uart_tx_r;
volatile uint8_t uart_tx_b;
static const uint8_t uart_tx_m = sizeof(uart_tx) - 1; static const uint8_t uart_tx_m = sizeof(uart_tx) - 1;
static inline void tx() static inline void tx()
@ -65,6 +66,8 @@ static inline void tx()
while (r != uart_tx_w) { while (r != uart_tx_w) {
if (!(USART0.STATUS & USART_DREIF_bm)) { if (!(USART0.STATUS & USART_DREIF_bm)) {
USART0.CTRLA |= USART_DREIE_bm; USART0.CTRLA |= USART_DREIE_bm;
USART0.STATUS = USART_TXCIF_bm;
uart_tx_b = USART_TXCIF_bm;
uart_tx_r = r; uart_tx_r = r;
return; return;
} }
@ -84,11 +87,20 @@ ISR(USART0_DRE_vect)
__attribute__ ((noinline, noclone)) __attribute__ ((noinline, noclone))
uint8_t uart_busy() uint8_t uart_busy()
{ {
// How to test if we can power down?
// `uart_tx_b` is set to `TXC` when a char is loaded for transmission
// and the TXCIF is cleared.
// When `uart_tx_b` is zero, we are good,
// else when `TXCIF` is set we are also good.
// When `uart_tx_b` is set but the `TXCIF` not yet, we are busy,
//returning TXC.
cli(); cli();
tx(); tx();
uint8_t r = uart_tx_w - uart_tx_r; uint8_t b = uart_tx_b;
if (!b || USART0.STATUS & b)
b = uart_tx_b = 0;
sei(); sei();
return r; return b;
} }
__attribute__ ((noinline, noclone)) __attribute__ ((noinline, noclone))