-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
uart_hw.c
266 lines (209 loc) · 6.73 KB
/
uart_hw.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mcu_periph/uart.h"
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "serial_port.h"
#if USE_UART0
volatile uint16_t uart0_rx_insert_idx, uart0_rx_extract_idx;
uint8_t uart0_rx_buffer[UART0_RX_BUFFER_SIZE];
volatile uint16_t uart0_tx_insert_idx, uart0_tx_extract_idx;
volatile bool_t uart0_tx_running;
uint8_t uart0_tx_buffer[UART0_TX_BUFFER_SIZE];
struct SerialPort* fmssp0;
int uart0_fd;
extern uint8_t portnum;
//This function will close our UART and reopen with the new baud rate
#ifdef GPS_CONFIGURE
void uart0_init_param( uint16_t baud, uint8_t mode, uint8_t fmode) {
//serial_port_flush_output(fmssp0);
serial_port_close(fmssp0);
fmssp0 = serial_port_new();
if (portnum == 0) {
printf("opening ttyUSB0 on uart0 at %d\n",GPS_BAUD);
serial_port_open_raw(fmssp0,"/dev/ttyUSB0",GPS_BAUD);
}
if (portnum == 1) {
printf("opening ttyUSB1 on uart0 at %d\n",GPS_BAUD);
serial_port_open_raw(fmssp0,"/dev/ttyUSB1",GPS_BAUD);
}
uart0_fd = (int)fmssp0->fd;
// initialize the transmit data queue
uart0_tx_extract_idx = 0;
uart0_tx_insert_idx = 0;
uart0_tx_running = FALSE;
// initialize the receive data queue
uart0_rx_extract_idx = 0;
uart0_rx_insert_idx = 0;
}
#endif
void uart0_init( void ) {
fmssp0 = serial_port_new();
//TODO: set device name in application and pass as argument
if (portnum == 0) {
printf("opening ttyUSB0 on uart0 at %d\n",UART0_BAUD);
serial_port_open_raw(fmssp0,"/dev/ttyUSB0",UART0_BAUD);
}
if (portnum == 1) {
printf("opening ttyUSB1 on uart0 at %d\n",UART0_BAUD);
serial_port_open_raw(fmssp0,"/dev/ttyUSB1",UART0_BAUD);
}
uart0_fd = (int)fmssp0->fd;
// initialize the transmit data queue
uart0_tx_extract_idx = 0;
uart0_tx_insert_idx = 0;
uart0_tx_running = FALSE;
// initialize the receive data queue
uart0_rx_extract_idx = 0;
uart0_rx_insert_idx = 0;
}
void uart0_transmit( uint8_t data ) {
uint16_t temp = (uart0_tx_insert_idx + 1) % UART0_TX_BUFFER_SIZE;
if (temp == uart0_tx_extract_idx)
return; // no room
// check if in process of sending data
if (uart0_tx_running) { // yes, add to queue
uart0_tx_buffer[uart0_tx_insert_idx] = data;
uart0_tx_insert_idx = temp;
}
else { // no, set running flag and write to output register
uart0_tx_running = TRUE;
write(uart0_fd,&data,1);
//printf("w %x\n",data);
}
}
bool_t uart0_check_free_space( uint8_t len) {
int16_t space = uart0_tx_extract_idx - uart0_tx_insert_idx;
if (space <= 0)
space += UART0_TX_BUFFER_SIZE;
return (uint16_t)(space - 1) >= len;
}
void uart0_handler(void) {
unsigned char c='D';
// check if more data to send
if (uart0_tx_insert_idx != uart0_tx_extract_idx) {
write(uart0_fd,&uart0_tx_buffer[uart0_tx_extract_idx],1);
//printf("w %x\n",uart0_tx_buffer[uart0_tx_extract_idx]);
uart0_tx_extract_idx++;
uart0_tx_extract_idx %= UART0_TX_BUFFER_SIZE;
}
else {
uart0_tx_running = FALSE; // clear running flag
}
if(read(uart0_fd,&c,1) > 0){
//printf("r %x %c\n",c,c);
uint16_t temp = (uart0_rx_insert_idx + 1) % UART0_RX_BUFFER_SIZE;
uart0_rx_buffer[uart0_rx_insert_idx] = c;
// check for more room in queue
if (temp != uart0_rx_extract_idx)
uart0_rx_insert_idx = temp; // update insert index
}
}
#endif /* USE_UART0 */
#if USE_UART1
volatile uint16_t uart1_rx_insert_idx, uart1_rx_extract_idx;
uint8_t uart1_rx_buffer[UART1_RX_BUFFER_SIZE];
volatile uint16_t uart1_tx_insert_idx, uart1_tx_extract_idx;
volatile bool_t uart1_tx_running;
uint8_t uart1_tx_buffer[UART1_TX_BUFFER_SIZE];
struct SerialPort* fmssp1;
int uart1_fd;
void uart1_init( void ) {
fmssp1 = serial_port_new();
if (portnum == 0) {
printf("opening ttyUSB1 on uart1 at %d\n",UART1_BAUD);
serial_port_open_raw(fmssp1,"/dev/ttyUSB1",UART1_BAUD);
}
if (portnum == 1) {
printf("opening ttyUSB0 on uart1 at %d\n",UART1_BAUD);
serial_port_open_raw(fmssp1,"/dev/ttyUSB0",UART1_BAUD);
}
uart1_fd = (int)fmssp1->fd;
// initialize the transmit data queue
uart1_tx_extract_idx = 0;
uart1_tx_insert_idx = 0;
uart1_tx_running = FALSE;
// initialize the receive data queue
uart1_rx_extract_idx = 0;
uart1_rx_insert_idx = 0;
}
void uart1_transmit( uint8_t data ) {
uint16_t temp = (uart1_tx_insert_idx + 1) % UART1_TX_BUFFER_SIZE;
if (temp == uart1_tx_extract_idx)
return; // no room
// check if in process of sending data
if (uart1_tx_running) { // yes, add to queue
uart1_tx_buffer[uart1_tx_insert_idx] = data;
uart1_tx_insert_idx = temp;
}
else { // no, set running flag and write to output register
uart1_tx_running = TRUE;
//printf("z %x\n",data);
write(uart1_fd,&data,1);
}
}
bool_t uart1_check_free_space( uint8_t len) {
int16_t space = uart1_tx_extract_idx - uart1_tx_insert_idx;
if (space <= 0)
space += UART1_TX_BUFFER_SIZE;
return (uint16_t)(space - 1) >= len;
}
void uart1_handler(void) {
unsigned char c='D';
// check if more data to send
if (uart1_tx_insert_idx != uart1_tx_extract_idx) {
write(uart1_fd,&uart1_tx_buffer[uart1_tx_extract_idx],1);
//printf("z %x\n",uart1_tx_buffer[uart1_tx_extract_idx]);
uart1_tx_extract_idx++;
uart1_tx_extract_idx %= UART1_TX_BUFFER_SIZE;
}
else {
uart1_tx_running = FALSE; // clear running flag
}
if(read(uart1_fd,&c,1) > 0){
//printf("s %x %c\n",c,c);
uint16_t temp = (uart1_rx_insert_idx + 1) % UART1_RX_BUFFER_SIZE;;
uart1_rx_buffer[uart1_rx_insert_idx] = c;
// check for more room in queue
if (temp != uart1_rx_extract_idx)
uart1_rx_insert_idx = temp; // update insert index
}
}
#endif /* USE_UART1 */
void uart_init( void )
{
#if USE_UART0
uart0_init();
#endif
#if USE_UART1
uart1_init();
#endif
//TODO: add uart2 and greater
#if USE_UART2
uart2_init();
#endif
#if USE_UART3
uart3_init();
#endif
}