summaryrefslogtreecommitdiff
path: root/src/uart.c
blob: 3ba75db8ea82ee23b261fd2e5f4c73100d497a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <avr/io.h>
#include <stdio.h>
#include <util/setbaud.h>

/** UART notes
 * UCSR0A:
 *  0: MP communication mode
 *  1: U2X (double trans speed)
 *  2: USART parity error
 *  3: Data overrun (udr0 not read before next frame)
 *  4: Frame error
 *  5: Data register empty (new data can be transmitted)
 *  6: USART transmit complete
 *  7: USART recieve complete
 * ____________________________________________________________
 * UCSR0B:
 *  0: transmit data bit 8
 *  1: recieve data bit 8
 *  2: USART char size 0 (used with UCSZ01 and UCSZ00 to set data frame size)
 *  3: Transmitter enable
 *  4: Reciever  enable
 *  5: USART data reg empty interrupt table
 *  6: TX complete int enable
 *  7: RX complete int enable
 * ____________________________________________________________
 * UCSR0C:
 *  0: Clock polarity (1: falling edge trasmit, recieve rising edge)
 *  1: USART char size 0
 *  2: USART char size 1
 *  3: USART stop bit select (1: 1 stop bit, 0: 2 stop bits)
 *  4: USART parity mode (00: async, 01: sync)
 *  5: USART Parity mode (02: master SPI)
 *  6: USART mode select (00: async 01: sync)
 *  7: USART mode select (02: master SPI)
 * ____________________________________________________________
 * 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); 
  UDR0 = byte; 
}

//TODO replace with static void and just use high level functions?
uint8_t uart_recvbyte() {
  loop_until_bit_is_set(UCSR0A, RXC0);
  return UDR0;
}

void uart_init() {
  //set baud rate
  UBRR0H = UBRRH_VALUE;
  UBRR0L = UBRRL_VALUE;


/**
TODO figure out why USE_2X is enable when it shoudn't be
   //set baud rate
#ifdef USE_2X //is set by header
  UCSR0A |= _BV(U2X0);
#else
  UCSR0A &= ~(_BV(U2X0));
#endif
**/ 

  UCSR0A &= ~(_BV(U2X0));

  UCSR0C = ((_BV(UCSZ01)) | _BV(UCSZ00)); // set 8 bit char size, yes the '=' is intentional
  UCSR0B = (_BV(RXEN0) | _BV(TXEN0));

}