diff options
Diffstat (limited to 'src/uart.c')
-rw-r--r-- | src/uart.c | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -1,6 +1,7 @@ #include <avr/io.h> #include <stdio.h> #include <util/setbaud.h> +#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 |