aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/debug.c
blob: 2bd9c781e80e0acc2b2a7b584e3af3dfce307977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Debugging interface
 *
 * Copyright (c) 2020 Michael Buesch <m@bues.ch>
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "compat.h"
#include "debug.h"
#include "main.h"
#include "util.h"
#include "uart.h"

#include <stdio.h>

#if DEBUG && IS_ATMEGAx8


static struct {
	bool initialized;
	uint8_t ringbuf[256];
	uint16_t ringbuf_in;
	uint16_t ringbuf_out;
	uint16_t ringbuf_used;
} dbg;


static void tx_next_byte(void)
{
	if (dbg.ringbuf_used) {
		uart_tx_byte(dbg.ringbuf[dbg.ringbuf_out],
			     UART_CHAN_7BIT);
		if (dbg.ringbuf_out >= ARRAY_SIZE(dbg.ringbuf) - 1u)
			dbg.ringbuf_out = 0u;
		else
			dbg.ringbuf_out++;
		dbg.ringbuf_used--;
	}
	if (!dbg.ringbuf_used)
		uart_tx_enable(false, UART_CHAN_7BIT);
}

static void debug_tx_ready_handler(void)
{
	tx_next_byte();
}

static void debug_ringbuf_putbyte(uint8_t data)
{
	uint8_t irq_state;

	if (!dbg.initialized)
		return;

	irq_state = irq_disable_save();

	if (dbg.ringbuf_used < ARRAY_SIZE(dbg.ringbuf)) {
		dbg.ringbuf[dbg.ringbuf_in] = data;
		if (dbg.ringbuf_in >= ARRAY_SIZE(dbg.ringbuf) - 1u)
			dbg.ringbuf_in = 0u;
		else
			dbg.ringbuf_in++;
		dbg.ringbuf_used++;
	}

	uart_tx_enable(true, UART_CHAN_7BIT);
	if (uart_tx_is_ready(UART_CHAN_7BIT))
		tx_next_byte();

	irq_restore(irq_state);
}

static int debug_stream_putchar(char c, FILE *stream)
{
	debug_ringbuf_putbyte((uint8_t)c);
	return 0;
}

static FILE debug_fstream = FDEV_SETUP_STREAM(debug_stream_putchar,
					      NULL,
					      _FDEV_SETUP_WRITE);

void dfprintf(const char __flash *fmt, ...)
{
	va_list args;

	if (!dbg.initialized)
		return;

	va_start(args, fmt);
	vfprintf_P(&debug_fstream, (const char * _cast_force)fmt, args);
	va_end(args);
}

void debug_prepare_deep_sleep(void)
{
	if (!dbg.initialized)
		return;

	dprintf("Entering deep sleep\r\n");
	while (dbg.ringbuf_used) {
		if (uart_tx_is_ready(UART_CHAN_7BIT))
			tx_next_byte();
	}
	_delay_us(300);
}

void debug_init(void)
{
	uart_register_callbacks(debug_tx_ready_handler, NULL, UART_CHAN_7BIT);

	stdout = &debug_fstream;
	stderr = &debug_fstream;

	memory_barrier();
	dbg.initialized = true;
}

#endif /* DEBUG && IS_ATMEGAx8 */
bues.ch cgit interface