From 94304b11e7220f060dbc345de5fa1952d0465016 Mon Sep 17 00:00:00 2001 From: Brett Weiland Date: Tue, 27 Sep 2022 16:14:28 -0500 Subject: working on serial syncronization for programming eeprom --- src/uart.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/uart.c') diff --git a/src/uart.c b/src/uart.c index 3ba75db..ca1e587 100644 --- a/src/uart.c +++ b/src/uart.c @@ -1,6 +1,7 @@ #include #include #include +#include "pins.h" /** UART notes * UCSR0A: @@ -35,27 +36,36 @@ * ____________________________________________________________ * UBRR0(H,L): baudrates * + * **/ //TODO replace with static void and just use high level functions? void uart_sendbyte(uint8_t byte) { - if(byte == '\n') uart_sendbyte('\r'); - loop_until_bit_is_set(UCSR0A, UDRE0); + UART_PORT &= ~(_BV(UART_RTS)); //us: ready to send + loop_until_bit_is_clear(UART_PORT, UART_CTS); //them: go ahead + //also wait for buffer to be empty, should never be a problem + loop_until_bit_is_set(UCSR0A, UDRE0); //done recieving UDR0 = byte; + UART_PORT |= _BV(UART_RTS); //keep in mind cts is inverted; low is 1 and high is 0 } //TODO replace with static void and just use high level functions? uint8_t uart_recvbyte() { - loop_until_bit_is_set(UCSR0A, RXC0); + loop_until_bit_is_clear(UART_PORT, UART_CTS); //wait for their request + UART_PORT &= ~(_BV(UART_RTS)); //hey, you can send now + loop_until_bit_is_set(UCSR0A, RXC0); //wait for them to actually send + UART_PORT |= _BV(UART_RTS); //okay, you can no longer send return UDR0; } void uart_init() { //set baud rate + UART_DDR |= _BV(UART_RTS); + UART_PORT |= _BV(UART_RTS); //keep in mind cts is inverted; low is 1 and high is 0 + UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; - /** TODO figure out why USE_2X is enable when it shoudn't be //set baud rate -- cgit v1.2.3