forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uart.c
121 lines (115 loc) · 4.66 KB
/
uart.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/************************************************************************/
/* XBoot Extensible AVR Bootloader */
/* */
/* UART Module */
/* */
/* uart.c */
/* */
/* Alex Forencich <[email protected]> */
/* */
/* Copyright (c) 2010 Alex Forencich */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files(the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "uart.h"
// Interrupts
#ifdef USE_INTERRUPTS
#ifdef USE_UART
ISR(UART_DEVICE_RXC_ISR)
{
if (comm_mode == MODE_UNDEF)
{
comm_mode = MODE_UART;
#ifdef USE_I2C
#ifdef __AVR_XMEGA__
// disable I2C interrupt
I2C_DEVICE.SLAVE.CTRLA = 0;
#endif // __AVR_XMEGA__
#endif // USE_I2C
}
if (rx_char_cnt == 0)
{
rx_buff0 = UART_DEVICE.DATA;
rx_char_cnt = 1;
}
else
{
rx_buff1 = UART_DEVICE.DATA;
rx_char_cnt = 2;
}
}
ISR(UART_DEVICE_TXC_ISR)
{
tx_char_cnt = 0;
}
#endif // USE_UART
#endif // USE_INTERRUPTS
// Initialize UART
void uart_init(void)
{
#ifdef __AVR_XMEGA__
UART_PORT.DIRSET = (1 << UART_TX_PIN);
#ifdef UART_REMAP
UART_PORT.REMAP |= PORT_USART0_bm;
#endif // UART_REMAP
UART_DEVICE.BAUDCTRLA = (UART_BSEL_VALUE & USART_BSEL_gm);
UART_DEVICE.BAUDCTRLB = ((UART_BSCALE_VALUE << USART_BSCALE_gp) & USART_BSCALE_gm) | ((UART_BSEL_VALUE >> 8) & ~USART_BSCALE_gm);
#if UART_CLK2X
UART_DEVICE.CTRLB = USART_RXEN_bm | USART_CLK2X_bm | USART_TXEN_bm;
#else
UART_DEVICE.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
#endif // UART_CLK2X
#ifdef USE_INTERRUPTS
UART_DEVICE.CTRLA = USART_RXCINTLVL0_bm | USART_TXCINTLVL0_bm;
#endif // USE_INTERRUPTS
#else // __AVR_XMEGA__
UART_UBRR = UART_BRV;
#ifdef UART_U2X
UART_UCSRA = _BV(U2X0);
#else
UART_UCSRA = 0;
#endif
UART_UCSRB = _BV(RXEN0) | _BV(TXEN0);
UART_UCSRC = _BV(UCSZ01) | _BV(UCSZ00);
#endif // __AVR_XMEGA__
}
// Shut down UART
void uart_deinit(void)
{
#ifdef __AVR_XMEGA__
UART_DEVICE.CTRLB = 0;
#ifdef USE_INTERRUPTS
UART_DEVICE.CTRLA = 0;
#endif // USE_INTERRUPTS
UART_DEVICE.BAUDCTRLA = 0;
UART_DEVICE.BAUDCTRLB = 0;
#ifdef UART_REMAP
UART_PORT.REMAP &= ~PORT_USART0_bm;
#endif // UART_REMAP
UART_PORT.DIRCLR = (1 << UART_TX_PIN);
#else // __AVR_XMEGA__
UART_UCSRA = 0;
UART_UCSRB = 0;
UART_UCSRC = _BV(UCSZ01) | _BV(UCSZ00);
UART_UBRR = 0;
#endif // __AVR_XMEGA__
}