-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.h
49 lines (39 loc) · 1.28 KB
/
debug.h
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
/* Author: Steve Gunn
* Licence: This work is licensed under the Creative Commons Attribution License.
* View this license at http://creativecommons.org/about/licenses/
* Notes: F_CPU must be defined to match the clock frequency
* Usage: Include in your main file and call init_debug_uart0() from the beginning of main
* to initialise redirection of stdout, stderr and stdin to UART0.
*/
#define __ASSERT_USE_STDERR
#include <assert.h>
#include <stdio.h>
#include <avr/io.h>
#define DEBUG_BAUD 9600
int uputchar0(char c, FILE *stream)
{
if (c == '\n') uputchar0('\r', stream);
while (!(UCSR0A & _BV(UDRE0)));
UDR0 = c;
return c;
}
int ugetchar0(FILE *stream)
{
while(!(UCSR0A & _BV(RXC0)));
return UDR0;
}
void init_debug_uart0(void)
{
/* Configure UART0 baud rate, one start bit, 8-bit, no parity and one stop bit */
UBRR0H = (F_CPU/(DEBUG_BAUD*16L)-1) >> 8;
UBRR0L = (F_CPU/(DEBUG_BAUD*16L)-1);
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
/* Setup new streams for input and output */
static FILE uout = FDEV_SETUP_STREAM(uputchar0, NULL, _FDEV_SETUP_WRITE);
static FILE uin = FDEV_SETUP_STREAM(NULL, ugetchar0, _FDEV_SETUP_READ);
/* Redirect all standard streams to UART0 */
stdout = &uout;
stderr = &uout;
stdin = &uin;
}