TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
uart.c
Go to the documentation of this file.
1
#include <avr/interrupt.h>
2
#include <avr/io.h>
3
#include <stdint.h>
4
// #include <stdio.h>
5
#include <util/delay.h>
6
7
#include "
uart.h
"
8
9
char
receive_buf
= 0;
10
// ISR(USART0_RXC_vect) { USART_Receive(); }
11
12
// static FILE stdout_uart =
13
// FDEV_SETUP_STREAM(USART_Transmit, USART_Receive, _FDEV_SETUP_RW);
14
15
int
USART_init
(
struct
USART_config
*
config
) {
16
uint16_t ubrr = (
config
->fosc / 16 /
config
->baud) - 1;
17
UBRR0H = (
unsigned
char)(ubrr >> 8);
18
UBRR0L = (
unsigned
char)ubrr;
19
20
// Enable RX & TX & RXC interrupt
21
UCSR0B = (1 << RXEN0) | (1 << TXEN0)
/*| (1 << RXCIE0)*/
;
22
23
// Format 9600 8N1
24
UCSR0C = (1 << URSEL0) | (0 << USBS0) | (3 << UCSZ00);
25
26
// sei();
27
// fdevopen(USART_Transmit, USART_Receive);
28
29
return
0;
30
}
31
32
int
USART_endl
(
void
) {
33
int
status = 0;
34
status =
USART_Transmit
(
'\x0D'
);
35
status =
USART_Transmit
(
'\x0A'
);
36
return
status;
37
}
38
39
int
USART_Transmit
(
unsigned
char
data) {
40
// Wait for empty transmit buffer
41
while
(!(UCSR0A & (1 << UDRE0)))
42
;
43
44
// Put data into buffer, sends the data
45
UDR0 = data;
46
47
return
0;
48
}
49
50
// This function is a big memory nono!
51
int
USART_SendString
(
char
*data) {
52
int
status;
53
int
counter = 0;
54
while
(data[counter] !=
'\0'
) {
55
if
(data[counter] ==
'\n'
) {
56
status =
USART_Transmit
(
'\x0D'
);
57
status =
USART_Transmit
(
'\x0A'
);
58
}
else
{
59
status =
USART_Transmit
(data[counter]);
60
}
61
counter++;
62
}
63
return
status;
64
}
65
66
int
USART_Receive
(
void
) {
67
// Get and return received data from buffer
68
receive_buf
= UDR0;
69
70
return
0;
71
}
72
73
int
USART_ReceiveHandler
() {
74
char
cmd[32] = {0};
75
int
cmd_count = 0;
76
77
// Echo
78
if
(
receive_buf
) {
79
USART_Transmit
(
receive_buf
);
80
if
(
receive_buf
== 0x0D) {
81
USART_Transmit
(0x0A);
82
}
83
84
cmd[cmd_count] =
receive_buf
;
85
cmd_count++;
86
receive_buf
= 0;
87
}
88
89
// CMD Handler
90
if
(cmd[cmd_count] == 0x0D) {
91
USART_SendString
(cmd);
92
cmd[cmd_count] = 0;
93
cmd_count = 0;
94
}
95
96
return
0;
97
}
USART_endl
int USART_endl(void)
Transmit CRLF as end-of-line.
Definition
uart.c:32
USART_Transmit
int USART_Transmit(unsigned char data)
Transmit a single byte via USART0.
Definition
uart.c:39
USART_init
int USART_init(struct USART_config *config)
Initialize USART0 with the given configuration.
Definition
uart.c:15
USART_Receive
int USART_Receive(void)
Receive one byte from USART0. (Minimal implementation.).
Definition
uart.c:66
USART_ReceiveHandler
int USART_ReceiveHandler()
Handle incoming received bytes and simple command echoing.
Definition
uart.c:73
config
struct USART_config config
Definition
main.c:49
USART_config
USART configuration structure.
Definition
uart.h:22
receive_buf
char receive_buf
Definition
uart.c:9
USART_SendString
int USART_SendString(char *data)
Definition
uart.c:51
uart.h
UART communication driver.
drivers
uart.c
Generated by
1.15.0