TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
UART

Data Structures

struct  USART_config
 USART configuration structure. More...

Functions

int USART_init (struct USART_config *config)
 Initialize USART0 with the given configuration.
int USART_endl (void)
 Transmit CRLF as end-of-line.
int USART_Transmit (unsigned char data)
 Transmit a single byte via USART0.
int USART_SendString (char data[])
 Send a zero-terminated string over USART0.
int USART_Receive (void)
 Receive one byte from USART0. (Minimal implementation.).
int USART_ReceiveHandler (void)
 Handle incoming received bytes and simple command echoing.

Detailed Description

Function Documentation

◆ USART_endl()

int USART_endl ( void )

#include <uart.h>

Transmit CRLF as end-of-line.

Returns
Status code from last character transmitted.

Definition at line 32 of file uart.c.

32 {
33 int status = 0;
34 status = USART_Transmit('\x0D');
35 status = USART_Transmit('\x0A');
36 return status;
37}
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)

#include <uart.h>

Initialize USART0 with the given configuration.

Enables TX and RX with frame format 8N1. TODO: Verify...

Parameters
[in]configPointer to configuration containing baud rate and clock.
Returns
0 on success, non-zero on error.

Definition at line 15 of file uart.c.

15 {
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}
struct USART_config config
Definition main.c:49

Referenced by main().

◆ USART_Receive()

int USART_Receive ( void )

#include <uart.h>

Receive one byte from USART0. (Minimal implementation.).

Stores the received byte into a global buffer.

Returns
0 on success.

Definition at line 66 of file uart.c.

66 {
67 // Get and return received data from buffer
68 receive_buf = UDR0;
69
70 return 0;
71}
char receive_buf
Definition uart.c:9

◆ USART_ReceiveHandler()

int USART_ReceiveHandler ( void )

#include <uart.h>

Handle incoming received bytes and simple command echoing.

Echoes characters and echos back the full command line when CR is received.

Returns
0 on success.

Definition at line 73 of file uart.c.

73 {
74 char cmd[32] = {0};
75 int cmd_count = 0;
76
77 // Echo
78 if (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) {
92 cmd[cmd_count] = 0;
93 cmd_count = 0;
94 }
95
96 return 0;
97}
int USART_SendString(char *data)
Definition uart.c:51

◆ USART_SendString()

int USART_SendString ( char data[])

#include <uart.h>

Send a zero-terminated string over USART0.

Newlines ('\n') are converted to CRLF.

Parameters
[in]dataPointer to zero-terminated string.
Returns
Status code of the last transmitted character.

Referenced by utils_joystick_print_pos(), and utils_joystick_print_raw().

◆ USART_Transmit()

int USART_Transmit ( unsigned char data)

#include <uart.h>

Transmit a single byte via USART0.

Blocks until the transmit buffer is ready.

Parameters
[in]dataByte to send.
Returns
0 on success.

Definition at line 39 of file uart.c.

39 {
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}

Referenced by _log_print_P(), USART_endl(), USART_ReceiveHandler(), and USART_SendString().