-
Notifications
You must be signed in to change notification settings - Fork 0
/
com.c
181 lines (139 loc) · 3.41 KB
/
com.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "pipe.h"
#include "com.h"
#include <avr/io.h>
#include <avr/interrupt.h>
struct os_com_s
{
volatile uint8_t *data_reg;
volatile uint8_t *cntrl_reg;
uint8_t tx_ready_bit : 3;
volatile uint8_t tx_ready : 1;
uint8_t rx_ready_bit : 3;
volatile uint8_t rx_ready : 1;
uint8_t timeout;
uint16_t baud;
};
#define RX_READY(com) (*((com)->cntrl_reg) & _BV((com)->rx_ready_bit))
#define TX_READY(com) (*((com)->cntrl_reg) & _BV((com)->tx_ready_bit))
#define OS_ERR(a, b)
/* Gets the com pointer for a pipe */
#define GET_COM(fname) \
int interface; \
interface = os_pinterface(p); \
if (interface == -1) \
{ \
OS_ERR(inv_pipe, #fname ": no interface"); \
return -1; \
} \
com = &os_coms[interface];
os_com os_coms[] =
{
{
.cntrl_reg = &UCSR0A,
.tx_ready_bit = UDRE0, .tx_ready = 0,
.rx_ready_bit = RXC0,
.data_reg = &UDR0,
.timeout = 100,
.baud = 9600
},
{
.cntrl_reg = &UCSR1A,
.tx_ready_bit = UDRE1, .tx_ready = 0,
.rx_ready_bit = RXC1,
.data_reg = &UDR1,
.timeout = 100,
.baud = 9600
}
};
ISR(USART0_TX_vect)
{
os_com_transmit(&uart0_tx);
}
ISR(USART1_TX_vect)
{
os_com_transmit(&uart1_tx);
}
ISR(USART0_RX_vect)
{
os_com_recieve(&uart0_rx);
#if defined(OS_DBG)
// os_debug_read(&uart0_rx);
#endif
}
ISR(USART1_RX_vect)
{
os_com_recieve(&uart1_rx);
}
int8_t os_com_recieve(os_pipe *p)
{
os_com *com;
if (!(os_pflags(p) & f_pipe_com_rx))
return -1;
GET_COM(com_recieve);
if (!RX_READY(com))
return -1;
os_write(p, *(com->data_reg));
return 0;
}
int8_t os_com_transmit(os_pipe *p)
{
char c;
os_com *com;
if (!(os_pflags(p) & f_pipe_com_tx))
return -1;
GET_COM(com_transmit);
if (os_peek(p) == -1)
return -1;
if (!TX_READY(com))
return 0;
c = os_read(p);
*(com->data_reg) = c;
return 0;
}
int16_t os_com_rx_blk(os_pipe *p)
{
os_com *com;
if (!(os_pflags(p) & f_pipe_com_rx))
{
OS_ERR(inv_pipe, "rx_blk: pipe not rx");
return -1;
}
GET_COM(com_rx_blk);
while (!RX_READY(com));
return *(com->data_reg);
}
int8_t os_com_tx_blk(os_pipe *p, char c)
{
os_com *com;
if (!(os_pflags(p) & f_pipe_com_tx))
{
OS_ERR(inv_pipe, "tx_blk: pipe not tx");
return -1;
}
GET_COM(com_tx_blk);
while (!TX_READY(com));
*(com->data_reg) = c;
return 0;
}
int8_t os_com_tx_blk_str(os_pipe *p, char *str)
{
while (*str)
{
if (os_com_tx_blk(p, *(str++)) == -1)
return -1;
}
return 0;
}
void os_com_init(void)
{
cli();
UBRR0H = (F_CPU / os_coms[0].baud / 16 - 1) >> 8;
UBRR0L = (F_CPU / os_coms[0].baud / 16 - 1) & 0xff;
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0) | _BV(TXCIE0);
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
UBRR1H = (F_CPU / os_coms[1].baud / 16 - 1) >> 8;
UBRR1L = (F_CPU / os_coms[1].baud / 16 - 1) & 0xff;
UCSR1B = _BV(RXEN1) | _BV(TXEN1);
UCSR1C = _BV(UCSZ10) | _BV(UCSZ11);
sei();
}