forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.c
103 lines (96 loc) · 4.27 KB
/
fifo.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
/************************************************************************/
/* XBoot Extensible AVR Bootloader */
/* */
/* FT245/2232 asynchronous Fifo Module */
/* */
/* fifo.c */
/* */
/* Uwe Bonnes [email protected] */
/* */
/* Copyright (c) 2011 Uwe Bonnes */
/* */
/* 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 "fifo.h"
/* As discussed in
* http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=41613
* Accessing a bitrev table in bootloader flash will not be faster, as
* a our character needs to be added to the table converted to a far address
* and probaly also the NVM needs to be cared about
*/
#define REVERSE(a) do \
{ \
a=((a>>1)&0x55)|((a<<1)&0xaa); \
a=((a>>2)&0x33)|((a<<2)&0xcc); \
asm volatile("swap %0":"=r"(a):"0"(a)); \
} while(0)
// Initialize FIFO
void __attribute__ ((always_inline)) fifo_init(void)
{
#ifdef __AVR_XMEGA__
FIFO_DATA_PORT.DIR = 0;
FIFO_CTL_PORT.OUTSET = FIFO_RD_N_bm | FIFO_WR_N_bm;
FIFO_CTL_PORT.DIRSET = FIFO_RD_N_bm | FIFO_WR_N_bm;
#endif // __AVR_XMEGA__
}
// Shut down UART
void __attribute__ ((always_inline)) fifo_deinit(void)
{
#ifdef __AVR_XMEGA__
FIFO_DATA_PORT.DIR = 0xff;
FIFO_CTL_PORT.OUTCLR = FIFO_RD_N_bm | FIFO_WR_N_bm;
FIFO_CTL_PORT.DIRCLR = FIFO_RD_N_bm | FIFO_WR_N_bm;
#endif // __AVR_XMEGA__
}
uint8_t __attribute__ ((always_inline)) fifo_cur_char(void)
{
uint8_t ret;
FIFO_CTL_PORT.OUTCLR = FIFO_RD_N_bm;
ret = FIFO_DATA_PORT.IN;
#ifdef FIFO_BIT_REVERSE
REVERSE(ret);
#endif
FIFO_CTL_PORT.OUTSET = FIFO_RD_N_bm;
return ret;
}
void __attribute__ ((always_inline)) fifo_send_char(uint8_t c)
{
if ((FIFO_CTL_PORT.IN & FIFO_TXE_N_bm) != FIFO_TXE_N_bm)
{
FIFO_DATA_PORT.DIR = 0xff;
#ifdef FIFO_BIT_REVERSE
REVERSE(c);
#endif
FIFO_DATA_PORT.OUT = c;
FIFO_DATA_PORT.DIR = 0xff;
FIFO_CTL_PORT.OUTCLR = FIFO_WR_N_bm;
FIFO_DATA_PORT.DIR = 0;
FIFO_CTL_PORT.OUTSET = FIFO_WR_N_bm;
}
}
void __attribute__ ((always_inline)) fifo_send_char_blocking(uint8_t c)
{
while(FIFO_CTL_PORT.IN & FIFO_TXE_N_bm)
{
};
fifo_send_char(c);
}