diff options
Diffstat (limited to 'src/uart.c')
-rw-r--r-- | src/uart.c | 80 |
1 files changed, 63 insertions, 17 deletions
@@ -1,7 +1,9 @@ #include <avr/io.h> #include <stdio.h> #include <util/setbaud.h> +#include <util/delay.h> #include "pins.h" +#include "debug.h" /** UART notes * UCSR0A: @@ -39,29 +41,18 @@ * **/ -//TODO replace with static void and just use high level functions? -void uart_sendbyte(uint8_t byte) { - 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 +void uart_debug_sendbyte(uint8_t byte) { + loop_until_bit_is_set(UCSR0A, UDRE0); 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_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 +uint8_t uart_debug_recvbyte() { + loop_until_bit_is_set(UCSR0A, RXC0); + UART_PORT |= _BV(UART_RTS); 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 +void uart_debug_init() { UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; @@ -82,3 +73,58 @@ TODO figure out why USE_2X is enable when it shoudn't be UCSR0B = (_BV(RXEN0) | _BV(TXEN0)); } + +#ifdef FLASH_EEPROM +//this uart port recieves from the computer and writes the eeprom +void uart_flash_init() { + //syncronization + UART_DDR |= _BV(UART_RTS); + UART_DDR &= ~(_BV(UART_CTS)); //keep in mind cts/rts is inverted; low is 1 and high is 0 + UART_PORT |= _BV(UART_RTS); //keep in mind cts/rts is inverted; low is 1 and high is 0 + + //for serial speed, we'll just use the same speed for now + UBRR1H = UBRRH_VALUE; + UBRR1L = UBRRL_VALUE; + + UCSR1A &= ~(_BV(U2X1)); + + UCSR1C = ((_BV(UCSZ11)) | _BV(UCSZ10)); // set 8 bit char size, yes the '=' is intentional + UCSR1B = (_BV(RXEN1) | _BV(TXEN1)); + + //when the master initates serial via pyserial, for some reason it pulls our CTS low + //for a brief moment. This hack avoids that + printf("waiting for unwanted initation pulse...\r\n"); + loop_until_bit_is_clear(UART_PIN, UART_CTS); + loop_until_bit_is_set(UART_PIN, UART_CTS); + printf("complete\r\n"); +} + +void uart_flash_sendbyte(uint8_t byte) { + printf("send start\r\n"); + if(UART_PORT & _BV(UART_CTS)) { + printf("send conflict!\r\n"); + debug_crash(); //make sure they're not trying to send + } + UART_PORT &= ~(_BV(UART_RTS)); //us: ready to send + printf("waiting on master\r\n"); + loop_until_bit_is_clear(UART_PIN, UART_CTS); //them: go ahead + printf("waiting for transmit buffer\r\n"); + loop_until_bit_is_set(UCSR1A, UDRE1); //wait until buffer is empty + UDR1 = byte; + UART_PORT |= _BV(UART_RTS); //okay, we be done sending + printf("done splurting out the data, still need to mark end of packet\r\n"); + loop_until_bit_is_set(UART_PIN, UART_CTS); //wait for them to get the message so we don't double dip + printf("we be done tho\r\n"); +} + +uint8_t uart_flash_recvbyte() { + char byte_in; + loop_until_bit_is_clear(UART_PIN, UART_CTS); //wait for their request + UART_PORT &= ~(_BV(UART_RTS)); //hey, you can send now + loop_until_bit_is_set(UCSR1A, RXC1); //wait for them to actually send + byte_in = UDR1; + UART_PORT |= _BV(UART_RTS); //okay, you can no longer send + loop_until_bit_is_set(UART_PIN, UART_CTS); //wait for them to get the message so we don't double dip + return byte_in; +} +#endif |