2026-04-18 00:13:52 +02:00
|
|
|
#include "uart.h"
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include "mystd.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
|
|
#define BAUD_RATE 115200
|
|
|
|
|
|
|
|
|
|
#define UBRR_VALUE ((F_CPU / (8UL * BAUD_RATE)) - 1)
|
|
|
|
|
|
|
|
|
|
// uart is 115200 baud rate, 8 bits per word, no parrity and 1 stop bit
|
|
|
|
|
// 115200 8N1
|
|
|
|
|
void uart_init(void) {
|
|
|
|
|
// Set baud rate
|
|
|
|
|
UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
|
|
|
|
|
UBRR0L = (uint8_t)(UBRR_VALUE);
|
|
|
|
|
|
2026-04-19 15:38:04 +02:00
|
|
|
UCSR0A |= BV(U2X0);
|
2026-04-18 00:13:52 +02:00
|
|
|
// Enable transmitter
|
2026-04-19 15:38:04 +02:00
|
|
|
UCSR0B = BV(TXEN0) | BV(RXEN0);
|
2026-04-18 00:13:52 +02:00
|
|
|
|
|
|
|
|
// Set frame format: 8 data bits, no parity, 1 stop bit
|
2026-04-19 15:38:04 +02:00
|
|
|
UCSR0C = BV(UCSZ01) | BV(UCSZ00);
|
2026-04-18 00:13:52 +02:00
|
|
|
|
|
|
|
|
// Set TX (PD1) as output
|
2026-04-19 15:38:04 +02:00
|
|
|
DDRD |= BV(PD1);
|
2026-04-18 00:13:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uart_tx(char data) {
|
|
|
|
|
// wait for transmit buffer to be empty
|
2026-04-19 15:38:04 +02:00
|
|
|
while (!(UCSR0A & BV(UDRE0)))
|
2026-04-18 00:13:52 +02:00
|
|
|
;
|
|
|
|
|
// load data into transmit register
|
|
|
|
|
UDR0 = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char uart_rx(void) {
|
2026-04-19 15:38:04 +02:00
|
|
|
while (!(UCSR0A & BV(RXC0)))
|
2026-04-18 00:13:52 +02:00
|
|
|
;
|
|
|
|
|
return UDR0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uart_sendstring(const char* str) {
|
|
|
|
|
if (!str)
|
|
|
|
|
return;
|
|
|
|
|
while (*str) {
|
|
|
|
|
uart_tx(*str);
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void uart_send_u8(uint8_t val) {
|
|
|
|
|
char buf[4] = {0, 0, 0, 0};
|
|
|
|
|
uint8_t idx = 0;
|
|
|
|
|
bool print = false;
|
|
|
|
|
|
|
|
|
|
uint8_t modulus = 100;
|
|
|
|
|
while (modulus) {
|
|
|
|
|
uint8_t digit = val / modulus;
|
|
|
|
|
if (print || digit != 0) {
|
|
|
|
|
print = true;
|
|
|
|
|
buf[idx++] = '0' + digit;
|
|
|
|
|
}
|
|
|
|
|
val %= modulus;
|
|
|
|
|
modulus /= 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uart_sendstring(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uart_send_u16(uint16_t val) {
|
|
|
|
|
char buf[6] = {0, 0, 0, 0, 0, 0};
|
|
|
|
|
uint8_t idx = 0;
|
|
|
|
|
bool print = false;
|
|
|
|
|
|
|
|
|
|
uint16_t modulus = 10000;
|
|
|
|
|
while (modulus) {
|
|
|
|
|
uint8_t digit = val / modulus;
|
|
|
|
|
if (print || digit != 0) {
|
|
|
|
|
print = true;
|
|
|
|
|
buf[idx++] = '0' + digit;
|
|
|
|
|
}
|
|
|
|
|
val %= modulus;
|
|
|
|
|
modulus /= 10;
|
|
|
|
|
}
|
|
|
|
|
uart_sendstring(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uart_send_u8_hex(uint8_t val) {
|
|
|
|
|
char buf[3] = {0, 0, 0};
|
|
|
|
|
buf[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
|
|
|
|
buf[1] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
|
|
|
|
|
|
|
|
|
uart_sendstring(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uart_send_u16_hex(uint16_t val) {
|
|
|
|
|
char buf[5] = {0, 0, 0, 0, 0};
|
|
|
|
|
|
|
|
|
|
buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F];
|
|
|
|
|
buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F];
|
|
|
|
|
buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
|
|
|
|
buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
|
|
|
|
uart_sendstring(buf);
|
|
|
|
|
}
|