forked from drFerg/DCCCommander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cprint.c
41 lines (36 loc) · 1.09 KB
/
cprint.c
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
#include "cprint.h"
#include <Arduino.h>
#include <stdio.h>
/* Uno, Duemillenove, Mega etc. */
#if defined(__AVR_ATmega168__) ||defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) \
|| defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define UCSRXA UCSR0A
#define UDREX UDRE0
#define UDRX UDR0
#define RXCX RXC0
#elif defined(__AVR_ATmega32U4__) /* Leonardo, Esplora, Yun etc. */
#define UCSRXA UCSR1A
#define UDREX UDRE1
#define UDRX UDR1
#define RXCX RXC1
#endif
int USARTSendByte(char byte, FILE *stream) {
if(byte == '\n') USARTSendByte('\r', 0);
/* Wait while previous byte is completed */
while(!(UCSRXA & (1 << UDREX))){};
UDRX = byte; /* Send byte */
return 0;
}
int USARTReceiveByte(FILE *stream) {
uint8_t byte;
/* Wait until a byte is received */
while (!(UCSRXA & (1 << RXCX))){};
byte = UDRX; /* Copy byte */
USARTSendByte(byte,stream); /* Echo byte */
return byte;
}
/* set stream pointer */
FILE usart_str = FDEV_SETUP_STREAM(USARTSendByte, USARTReceiveByte, _FDEV_SETUP_RW);
void cprint_init() {
stdin = stdout = &usart_str;
}