mirror of
https://github.com/markqvist/OpenModem.git
synced 2025-05-21 15:50:32 -04:00
Initial commit
This commit is contained in:
commit
05d62b594e
18 changed files with 1551 additions and 0 deletions
47
hardware/Serial.c
Normal file
47
hardware/Serial.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "Serial.h"
|
||||
#include <util/setbaud.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void serial_init(Serial *serial) {
|
||||
memset(serial, 0, sizeof(*serial));
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
|
||||
#if USE_2X
|
||||
UCSR0A |= _BV(U2X0);
|
||||
#else
|
||||
UCSR0A &= ~(_BV(U2X0));
|
||||
#endif
|
||||
|
||||
// Set to 8-bit data, enable RX and TX
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
|
||||
|
||||
FILE uart0_fd = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW);
|
||||
|
||||
serial->uart0 = uart0_fd;
|
||||
}
|
||||
|
||||
bool serial_available(uint8_t index) {
|
||||
if (index == 0) {
|
||||
if (UCSR0A & _BV(RXC0)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void uart0_putchar(char c) {
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
}
|
||||
|
||||
char uart0_getchar(void) {
|
||||
loop_until_bit_is_set(UCSR0A, RXC0);
|
||||
return UDR0;
|
||||
}
|
||||
|
||||
char uart0_getchar_nowait(void) {
|
||||
if (!(UCSR0A & _BV(RXC0))) return EOF;
|
||||
return UDR0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue