forked from tilaktilak/atmega328P
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uart.c
41 lines (35 loc) · 832 Bytes
/
uart.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
#include "FreeRTOS.h"
#include "task.h"
#include <avr/io.h>
#include <stdio.h>
#include "uart.h"
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#ifndef BAUD
#define BAUD 9600
#endif
#include <util/setbaud.h>
int uart_transmit(char c, FILE *stream);
int uart_receive(FILE *stream);
FILE uart_file = FDEV_SETUP_STREAM(uart_transmit, uart_receive, _FDEV_SETUP_RW);
void uart_init() {
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
int uart_transmit(char c, FILE *stream) {
while (!(UCSR0A & _BV(UDRE0))) taskYIELD();
UDR0 = c;
return 0;
}
int uart_receive(FILE *stream) {
while (!(UCSR0A & _BV(RXC0))) taskYIELD();
return UDR0;
}