summaryrefslogtreecommitdiff
path: root/src/arch/atmega2560/driver/stdout.cc
blob: 58a3e7eb69432a98b944419ef84dba0a74d01e2d (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
/*
 * Copyright 2021 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#include "driver/stdout.h"
#include <avr/io.h>
#include <avr/interrupt.h>

#ifndef BAUD
#define BAUD 115200UL
#endif

#include <util/setbaud.h>

void StandardOutput::setup()
{
	UBRR0H = UBRRH_VALUE;
	UBRR0L = UBRRL_VALUE;

#if USE_2X
	UCSR0A |= _BV(U2X0);
#else
	UCSR0A &= ~_BV(U2X0);
#endif

	UCSR0B |= _BV(RXEN0) | _BV(TXEN0);
	UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
}

void StandardOutput::put(char c)
{
	while (!(UCSR0A & _BV(UDRE0)));
	UDR0 = c;
	if (c == '\n') {
		put('\r');
	}
}

StandardOutput kout;