Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1734 | - | 1 | /** |
2 | * source for the Bluetooth driver |
||
3 | * @file bluetooth.c |
||
4 | * @author Linus Lotz<lotz@in.tum.de> |
||
5 | * @author Salomon Sickert |
||
6 | */ |
||
7 | |||
8 | |||
9 | |||
10 | #include "cpu.h" |
||
11 | #include <string.h> |
||
12 | #include <util/delay.h> |
||
13 | #include <avr/interrupt.h> |
||
14 | #include "bluetooth.h" |
||
15 | #include "main.h" |
||
16 | #ifdef HWVERSION3_9 |
||
17 | #include "uart1.h" |
||
18 | #include "usart.h" |
||
19 | #include "timer.h" |
||
20 | #include "fifo.h" |
||
21 | #include "error.h" |
||
22 | #include "lcd.h" |
||
23 | #include "eeprom.h" |
||
24 | #include "error.h" |
||
25 | #include "setup.h" |
||
26 | #include "bluetooth.h" |
||
27 | #include "tracking.h" |
||
28 | |||
29 | |||
30 | //#define SaveMem |
||
31 | |||
32 | // |
||
33 | // Baudrate for the UART-connection to the BTM-222 on SQUIRREL |
||
34 | // |
||
35 | |||
36 | #define SQUIRREL |
||
37 | |||
38 | #ifdef SQUIRREL |
||
39 | #define UART_BAUD_RATE 19200 |
||
40 | #endif |
||
41 | |||
42 | #ifdef NUT |
||
43 | #define UART_BAUD_RATE 19200 |
||
44 | #endif |
||
45 | |||
46 | |||
47 | typedef enum { |
||
48 | BT_RAW, |
||
49 | BT_DATA, |
||
50 | BT_CMD, |
||
51 | BT_NOECHO, |
||
52 | BT_NOANSWER |
||
53 | } communication_mode_t; |
||
54 | |||
55 | #define BT_CMD_TIMEOUT_MS 2000 |
||
56 | |||
57 | typedef enum { |
||
58 | BT_TEST, // AT |
||
59 | BT_CONNECT, // ATA |
||
60 | BT_DISCONNECT, // ATH |
||
61 | BT_CLEAR_ADDRESS, // ATD0 |
||
62 | BT_SET_ADDRESS, // ATD=_____ |
||
63 | BT_FIND_DEVICES, // ATF? |
||
64 | BT_DISABLE_AUTOCONNECT, // ATO1 |
||
65 | BT_SET_MASTER, // ATR0 |
||
66 | BT_SET_SLAVE, // ATR1 |
||
67 | BT_SET_PIN, // ATP=1234 |
||
68 | BT_SET_2400, // ATL* Baudrate 2400 |
||
69 | BT_SET_9600, // ATL1 Baudrate 9600 |
||
70 | BT_SET_19200, // ATL2 Baudrate 19200 |
||
71 | BT_SET_38400, // ATL3 Baudrate 38400 |
||
72 | BT_SET_57600, // ATL4 Baudrate 57600 |
||
73 | BT_SET_115200, // ATL5 Baudrate 115200 |
||
74 | BT_SET_NOANSWER, // ATQ1 Rückmeldungen aus |
||
75 | BT_SET_NOECHO, // ATE0 ECHO deaktivieren |
||
76 | BT_SET_ANSWER, // ATQ0 Rückmeldungen |
||
77 | BT_SET_ECHO, // ATE1 ECHO aktivieren |
||
78 | BT_SET_DEFAULT, // Defaultwerte setzen |
||
79 | BT_SET_NAME, // Devicename |
||
80 | BT_SET_DISPWRDOWN // disable auto Powerdown |
||
81 | } bt_cmd_t; |
||
82 | |||
83 | |||
84 | #define IN_FIFO_SIZE 512 |
||
85 | |||
86 | |||
87 | static uint8_t bt_buffer[IN_FIFO_SIZE]; |
||
88 | static fifo_t in_fifo; |
||
89 | |||
90 | char rx_buffer[RXD_BUFFER_SIZE]; |
||
91 | volatile uint8_t rx_len; |
||
92 | volatile uint8_t rx_ready = 0; |
||
93 | uint8_t rx_GPS; |
||
94 | static char start = '$'; |
||
95 | static char end = '\n'; |
||
96 | |||
97 | char data_decode[RXD_BUFFER_SIZE]; |
||
98 | volatile uint16_t rx_timeout; |
||
99 | |||
100 | |||
101 | static bt_mode_t bt_mode = BLUETOOTH_SLAVE; |
||
102 | static communication_mode_t comm_mode = BT_CMD; |
||
103 | |||
104 | uint8_t i = 0; |
||
105 | uint8_t NoEcho = 0; |
||
106 | uint8_t NoAnswer = 0; |
||
107 | |||
108 | uint8_t bt_devicecount = 0; |
||
109 | |||
110 | uint8_t bt_rxerror = 0; |
||
111 | |||
112 | device_info device_list[NUTS_LIST]; |
||
113 | |||
114 | // Set a timeout of Y ms and a Conditon X, which have to be true while timeout |
||
115 | #define while_timeout(X, Y) for(uint16_t __timeout = 0; __timeout++ <= Y && (X); Delay_MS(Y ? 1 : 0)) |
||
116 | |||
117 | //-------------------------------------------------------------- |
||
118 | void Delay_MS(int count) |
||
119 | { |
||
120 | for (int i = 0; i < count; i++) |
||
121 | _delay_ms(1); |
||
122 | } |
||
123 | |||
124 | |||
125 | //-------------------------------------------------------------- |
||
126 | void uart_receive(void) |
||
127 | { |
||
128 | unsigned int uart_data; |
||
129 | |||
130 | while (!fifo_is_full(&in_fifo)) |
||
131 | { |
||
132 | uart_data = uart1_getc(); |
||
133 | |||
134 | // USART_puts("."); |
||
135 | |||
136 | switch (uart_data & 0xFF00) { |
||
137 | // Framing Error detected, i.e no stop bit detected |
||
138 | case UART_FRAME_ERROR: |
||
139 | #ifdef DEBUG |
||
140 | warn_pgm(PSTR("FRM ERR")); |
||
141 | #endif |
||
142 | bt_rxerror++; |
||
143 | return; |
||
144 | |||
145 | // Overrun, a character already presend in the UART UDR register was |
||
146 | // not read by the interrupt handler before the next character arrived, |
||
147 | // one or more received characters have been dropped |
||
148 | // |
||
149 | case UART_OVERRUN_ERROR: |
||
150 | #ifdef DEBUG |
||
151 | warn_pgm(PSTR("OVR ERR")); |
||
152 | #endif |
||
153 | bt_rxerror++; |
||
154 | return; |
||
155 | |||
156 | // We are not reading the receive buffer fast enough, |
||
157 | // one or more received character have been dropped |
||
158 | // |
||
159 | case UART_BUFFER_OVERFLOW: |
||
160 | #ifdef DEBUG |
||
161 | warn_pgm(PSTR("BUF ERR")); |
||
162 | #endif |
||
163 | bt_rxerror++; |
||
164 | return; |
||
165 | |||
166 | // UART Inputbuffer empty, nothing to do |
||
167 | case UART_NO_DATA: |
||
168 | return; |
||
169 | |||
170 | default: |
||
171 | { |
||
172 | fifo_write(&in_fifo, uart_data); |
||
173 | #ifdef DEBUG |
||
174 | USART_putc(uart_data); |
||
175 | #endif |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | #ifdef DEBUG |
||
180 | warn_pgm(PSTR("FIFO OVR ERR")); |
||
181 | #endif |
||
182 | } |
||
183 | |||
184 | |||
185 | //-------------------------------------------------------------- |
||
186 | static void uart_send(const char *data, const uint8_t length) |
||
187 | { |
||
188 | #ifdef DEBUG |
||
189 | debug_pgm(PSTR("bt_uart_send")); |
||
190 | #endif |
||
191 | |||
192 | char echo; |
||
193 | |||
194 | lcd_printp_at (i++, 1, PSTR("."), 0); |
||
195 | for (uint8_t i = 0; i < length; i++) |
||
196 | { |
||
197 | |||
198 | #ifdef DEBUG |
||
199 | USART_putc((data[i])); //test |
||
200 | #endif |
||
201 | // debug_pgm(PSTR("bt_init_S")); |
||
202 | |||
203 | if (uart1_putc(data[i]) == 0) |
||
204 | { |
||
205 | #ifdef DEBUG |
||
206 | warn_pgm(PSTR("UART: Remote not ready")); |
||
207 | #endif |
||
208 | return; |
||
209 | } |
||
210 | |||
211 | if (comm_mode == BT_RAW) |
||
212 | _delay_ms(50); |
||
213 | |||
214 | if (comm_mode == BT_DATA) |
||
215 | _delay_ms(1); |
||
216 | |||
217 | if (comm_mode == BT_NOECHO) |
||
218 | _delay_ms(1); |
||
219 | |||
220 | if (comm_mode == BT_CMD) |
||
221 | { |
||
222 | uint8_t x = 0; |
||
223 | for (; x < 3; x++) |
||
224 | { |
||
225 | // // while_timeout(X, Y) for(uint16_t __timeout = 0; __timeout++ <= Y && (X); _delay_ms(Y ? 1 : 0)) |
||
226 | // while_timeout(fifo_is_empty(&in_fifo), 200) |
||
227 | for(uint16_t __timeout = 0; __timeout++ <= 200 && (fifo_is_empty(&in_fifo)); _delay_ms(200 ? 1 : 0)) |
||
228 | { |
||
229 | uart_receive(); |
||
230 | } |
||
231 | |||
232 | fifo_read(&in_fifo, &echo); |
||
233 | |||
234 | if (echo != data[i]) { |
||
235 | if (uart1_putc(data[i]) == 0) |
||
236 | { |
||
237 | warn_pgm(PSTR ("UART: Remote not ready")); |
||
238 | return; |
||
239 | } |
||
240 | } |
||
241 | else |
||
242 | break; |
||
243 | } |
||
244 | |||
245 | if (x == 3) |
||
246 | { |
||
247 | error_putc(data[i]); |
||
248 | error_pgm(PSTR("BT: WRONG ECHO")); |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | |||
255 | //-------------------------------------------------------------- |
||
256 | static uint16_t send_cmd(const bt_cmd_t command, const char *data) |
||
257 | { |
||
258 | uint16_t CommandDelay=0; // nach BTM222 Kommandos verschiedene Verzögerungszeiten bevor es weitergehen kann |
||
259 | |||
260 | // _delay_ms(500); // org 500 300 zu wenig |
||
261 | char full_command[20]; // Maximum command size |
||
262 | |||
263 | switch (command) |
||
264 | { |
||
265 | case BT_SET_PIN: |
||
266 | strcpy_P(full_command, PSTR("ATP=")); |
||
267 | for (uint8_t i = 0; i < bt_pin_length; i++) |
||
268 | { |
||
269 | full_command[i+4] = bt_pin[i]; |
||
270 | } |
||
271 | full_command[(bt_pin_length+4)] =0; |
||
272 | CommandDelay = 100; //100ms |
||
273 | break; |
||
274 | |||
275 | case BT_SET_DEFAULT: |
||
276 | strcpy_P(full_command, PSTR("ATZ0")); |
||
277 | CommandDelay = 1000; |
||
278 | break; |
||
279 | |||
280 | |||
281 | case BT_SET_2400: |
||
282 | strcpy_P(full_command, PSTR("ATL*")); |
||
283 | CommandDelay = 100; |
||
284 | break; |
||
285 | |||
286 | case BT_SET_9600: |
||
287 | strcpy_P(full_command, PSTR("ATL1")); |
||
288 | CommandDelay = 100; |
||
289 | break; |
||
290 | |||
291 | case BT_SET_19200: |
||
292 | strcpy_P(full_command, PSTR("ATL2")); |
||
293 | CommandDelay = 100; |
||
294 | break; |
||
295 | |||
296 | case BT_SET_38400: |
||
297 | strcpy_P(full_command, PSTR("ATL3")); |
||
298 | CommandDelay = 100; |
||
299 | break; |
||
300 | |||
301 | case BT_SET_57600: |
||
302 | strcpy_P(full_command, PSTR("ATL4")); |
||
303 | CommandDelay = 100; |
||
304 | break; |
||
305 | |||
306 | case BT_SET_115200: |
||
307 | strcpy_P(full_command, PSTR("ATL5")); |
||
308 | CommandDelay = 100; |
||
309 | break; |
||
310 | |||
311 | case BT_SET_NOANSWER: |
||
312 | strcpy_P(full_command, PSTR("ATQ1")); |
||
313 | CommandDelay = 100; |
||
314 | break; |
||
315 | |||
316 | case BT_SET_NOECHO: |
||
317 | strcpy_P(full_command, PSTR("ATE0")); |
||
318 | CommandDelay = 100; |
||
319 | break; |
||
320 | |||
321 | case BT_SET_ANSWER: |
||
322 | strcpy_P(full_command, PSTR("ATQ0")); |
||
323 | CommandDelay = 100; |
||
324 | break; |
||
325 | |||
326 | case BT_SET_ECHO: |
||
327 | strcpy_P(full_command, PSTR("ATE1")); |
||
328 | CommandDelay = 100; |
||
329 | break; |
||
330 | |||
331 | case BT_TEST: |
||
332 | strcpy_P(full_command, PSTR("AT")); |
||
333 | CommandDelay = 100; |
||
334 | break; |
||
335 | |||
336 | case BT_CONNECT: |
||
337 | strcpy_P(full_command, PSTR("ATA")); |
||
338 | CommandDelay = 100; |
||
339 | break; |
||
340 | |||
341 | case BT_DISCONNECT: |
||
342 | strcpy_P(full_command, PSTR("ATH")); |
||
343 | CommandDelay = 100; |
||
344 | break; |
||
345 | |||
346 | case BT_CLEAR_ADDRESS: |
||
347 | strcpy_P(full_command, PSTR("ATD0")); |
||
348 | CommandDelay = 100; |
||
349 | break; |
||
350 | |||
351 | case BT_SET_ADDRESS: |
||
352 | strcpy_P(full_command, PSTR("ATD=")); |
||
353 | memcpy((full_command + strlen(full_command)), data, 12); |
||
354 | full_command[16] = 0; |
||
355 | CommandDelay = 100; |
||
356 | break; |
||
357 | |||
358 | case BT_FIND_DEVICES: |
||
359 | strcpy_P(full_command, PSTR("ATF?")); |
||
360 | CommandDelay = 100; |
||
361 | break; |
||
362 | |||
363 | case BT_DISABLE_AUTOCONNECT: |
||
364 | strcpy_P(full_command, PSTR("ATO1")); |
||
365 | CommandDelay = 3500; |
||
366 | break; |
||
367 | |||
368 | case BT_SET_MASTER: |
||
369 | strcpy_P(full_command, PSTR("ATR0")); |
||
370 | CommandDelay = 3000; |
||
371 | break; |
||
372 | |||
373 | case BT_SET_SLAVE: |
||
374 | strcpy_P(full_command, PSTR("ATR1")); |
||
375 | CommandDelay = 3000; |
||
376 | break; |
||
377 | |||
378 | case BT_SET_NAME: |
||
379 | strcpy_P(full_command, PSTR("ATN=")); |
||
380 | for (uint8_t i = 0; i < bt_name_len; i++) |
||
381 | { |
||
382 | full_command[i + 4] = bt_name[i]; |
||
383 | } |
||
384 | full_command[(bt_name_len + 4)] = 0; |
||
385 | CommandDelay = 100; |
||
386 | break; |
||
387 | |||
388 | case BT_SET_DISPWRDOWN: |
||
389 | strcpy_P(full_command, PSTR("ATS1")); |
||
390 | CommandDelay = 100; |
||
391 | break; |
||
392 | |||
393 | default: |
||
394 | warn_pgm(PSTR("CMD UNK")); |
||
395 | return false; |
||
396 | } |
||
397 | |||
398 | strcat_P(full_command, PSTR("\r")); |
||
399 | |||
400 | // throw away your television |
||
401 | uart_receive(); |
||
402 | fifo_clear(&in_fifo); |
||
403 | // debug_pgm(PSTR("bt_init3")); |
||
404 | // send command |
||
405 | uart_send(full_command, strlen(full_command)); |
||
406 | |||
407 | if (command== BT_SET_NOECHO) |
||
408 | { |
||
409 | _delay_ms(CommandDelay); |
||
410 | return true; |
||
411 | } |
||
412 | |||
413 | if (command== BT_SET_NOANSWER) |
||
414 | { |
||
415 | _delay_ms(CommandDelay); |
||
416 | return true; |
||
417 | } |
||
418 | |||
419 | if (command== BT_SET_ECHO) |
||
420 | { |
||
421 | _delay_ms(CommandDelay); |
||
422 | return true; |
||
423 | } |
||
424 | if (command== BT_SET_ANSWER) |
||
425 | { |
||
426 | _delay_ms(CommandDelay); |
||
427 | return true; |
||
428 | } |
||
429 | // get response |
||
430 | while_timeout(true, BT_CMD_TIMEOUT_MS) |
||
431 | { |
||
432 | uart_receive(); |
||
433 | if (fifo_strstr_pgm(&in_fifo, PSTR("OK\r\n"))) |
||
434 | { |
||
435 | info_pgm(PSTR("CMD SEND: OK")); |
||
436 | _delay_ms(CommandDelay); |
||
437 | return true; |
||
438 | } |
||
439 | |||
440 | if (fifo_strstr_pgm(&in_fifo, PSTR("ERROR\r\n"))) |
||
441 | { |
||
442 | #ifdef DEBUG |
||
443 | info_pgm(PSTR("CMD SEND: Error")); |
||
444 | #endif |
||
445 | return false; |
||
446 | } |
||
447 | } |
||
448 | |||
449 | #ifdef DEBUG |
||
450 | if (command != BT_TEST) |
||
451 | warn_pgm(PSTR("CMD SEND: TIMEOUT")); |
||
452 | #endif |
||
453 | |||
454 | |||
455 | return false; |
||
456 | } |
||
457 | |||
458 | |||
459 | //-------------------------------------------------------------- |
||
460 | void test(void) |
||
461 | { |
||
462 | comm_mode = BT_RAW; |
||
463 | for (uint8_t i = 0; i < 2; i++) |
||
464 | if (send_cmd(BT_TEST, NULL)) |
||
465 | break; |
||
466 | comm_mode = BT_CMD; |
||
467 | } |
||
468 | |||
469 | |||
470 | #ifndef SaveMem |
||
471 | |||
472 | //-------------------------------------------------------------- |
||
473 | static void clean_line(void) |
||
474 | { |
||
475 | while_timeout(true, 50) |
||
476 | uart_receive(); |
||
477 | fifo_strstr_pgm(&in_fifo, PSTR("\r\n")); |
||
478 | } |
||
479 | |||
480 | static communication_mode_t update_comm_mode(uint16_t timeout_ms) |
||
481 | { |
||
482 | while_timeout(true, timeout_ms) |
||
483 | { |
||
484 | uart_receive(); |
||
485 | |||
486 | if (fifo_strstr_pgm(&in_fifo, PSTR("DISCONNECT"))) |
||
487 | { |
||
488 | clean_line(); |
||
489 | test(); |
||
490 | comm_mode = BT_CMD; |
||
491 | return comm_mode; |
||
492 | } |
||
493 | |||
494 | if (fifo_strstr_pgm(&in_fifo, PSTR("CONNECT"))) |
||
495 | { |
||
496 | _delay_ms(100); //don't delete this, else there will be no success!!!!!!!!! |
||
497 | comm_mode = BT_DATA; |
||
498 | return comm_mode; |
||
499 | } |
||
500 | |||
501 | if (fifo_strstr_pgm (&in_fifo, PSTR("Time out,Fail to connect!"))) |
||
502 | { |
||
503 | clean_line(); |
||
504 | #ifdef DEBUG |
||
505 | debug_pgm(PSTR("CONNECT FAILED")); |
||
506 | #endif |
||
507 | test(); |
||
508 | comm_mode = BT_CMD; |
||
509 | return comm_mode; |
||
510 | } |
||
511 | } |
||
512 | |||
513 | return comm_mode; |
||
514 | } |
||
515 | #endif |
||
516 | |||
517 | |||
518 | //-------------------------------------------------------------- |
||
519 | uint16_t bt_init(void) |
||
520 | { |
||
521 | uint8_t init_error = false; |
||
522 | uint8_t BT_found = 0; |
||
523 | i = 0; |
||
524 | |||
525 | set_BTOn(); |
||
526 | |||
527 | lcd_cls(); |
||
528 | lcd_printp_at (0, 0, PSTR("BT initialisieren.."), 0); |
||
529 | // _delay_ms(200); |
||
530 | |||
531 | for (uint8_t z = (bt_name_length); z > 0; z--) |
||
532 | { |
||
533 | if (bt_name[z - 1] != ' ') |
||
534 | { |
||
535 | bt_name_len = z; |
||
536 | break; |
||
537 | } |
||
538 | } |
||
539 | |||
540 | uart1_init(UART_BAUD_SELECT(57600, F_CPU)); |
||
541 | fifo_init(&in_fifo, bt_buffer, IN_FIFO_SIZE); |
||
542 | _delay_ms(100); |
||
543 | // debug_pgm(PSTR("bt_init")); |
||
544 | // uart_receive(); |
||
545 | // debug_pgm(PSTR("bt_init1")); |
||
546 | fifo_clear(&in_fifo); |
||
547 | send_cmd(BT_TEST, NULL); |
||
548 | comm_mode = BT_NOECHO; |
||
549 | send_cmd(BT_SET_ECHO, NULL); |
||
550 | send_cmd(BT_SET_ANSWER, NULL); |
||
551 | |||
552 | // debug_pgm(PSTR("bt_init2")); |
||
553 | #ifdef DEBUG |
||
554 | debug_pgm(PSTR("Check with 57600")); |
||
555 | #endif |
||
556 | // send_cmd(BT_TEST, NULL); // Schrott löschen |
||
557 | |||
558 | if (send_cmd(BT_TEST, NULL)) // Test mit 57600 |
||
559 | { |
||
560 | #ifdef DEBUG |
||
561 | debug_pgm(PSTR("BT found 57600 Baud")); |
||
562 | #endif |
||
563 | BT_found = 1; |
||
564 | } |
||
565 | |||
566 | if (BT_found == 0) |
||
567 | { |
||
568 | #ifdef DEBUG |
||
569 | debug_pgm(PSTR("Check with 19200")); |
||
570 | #endif |
||
571 | uart1_init(UART_BAUD_SELECT(19200, F_CPU));// Test mit 19200 |
||
572 | _delay_ms(100); |
||
573 | send_cmd(BT_TEST, NULL); // Schrott löschen |
||
574 | send_cmd(BT_SET_ANSWER, NULL); |
||
575 | send_cmd(BT_SET_ECHO, NULL); |
||
576 | |||
577 | if (send_cmd(BT_TEST, NULL)) |
||
578 | { |
||
579 | debug_pgm(PSTR("19200 OK")); |
||
580 | if (send_cmd(BT_TEST, NULL)) |
||
581 | { |
||
582 | #ifdef DEBUG |
||
583 | debug_pgm(PSTR("BT found 19200 Baud")); |
||
584 | #endif |
||
585 | BT_found = 2; |
||
586 | } |
||
587 | } |
||
588 | } |
||
589 | |||
590 | if (BT_found == 0) |
||
591 | { |
||
592 | #ifdef DEBUG |
||
593 | debug_pgm(PSTR("Check with 9600")); |
||
594 | #endif |
||
595 | uart1_init(UART_BAUD_SELECT(9600, F_CPU));//test mit 9600 |
||
596 | _delay_ms(100); |
||
597 | send_cmd(BT_TEST, NULL); |
||
598 | send_cmd(BT_SET_ANSWER, NULL); |
||
599 | send_cmd(BT_SET_ECHO, NULL); |
||
600 | if (send_cmd(BT_TEST, NULL)); |
||
601 | { |
||
602 | #ifdef DEBUG |
||
603 | debug_pgm(PSTR("9600 OK")); |
||
604 | #endif |
||
605 | if (send_cmd(BT_TEST, NULL)) |
||
606 | { |
||
607 | #ifdef DEBUG |
||
608 | debug_pgm(PSTR("BT found 9600 Baud")); |
||
609 | #endif |
||
610 | BT_found = 3; |
||
611 | } |
||
612 | } |
||
613 | } |
||
614 | |||
615 | |||
616 | if (BT_found == 0) |
||
617 | { |
||
618 | #ifdef DEBUG |
||
619 | debug_pgm(PSTR("Check with 4800")); |
||
620 | #endif |
||
621 | uart1_init(UART_BAUD_SELECT(4800, F_CPU));//test mit 4800 |
||
622 | _delay_ms(100); |
||
623 | send_cmd(BT_TEST, NULL); |
||
624 | send_cmd(BT_SET_ANSWER, NULL); |
||
625 | send_cmd(BT_SET_ECHO, NULL); |
||
626 | if (send_cmd(BT_TEST, NULL)); |
||
627 | { |
||
628 | #ifdef DEBUG |
||
629 | debug_pgm(PSTR("4800 OK")); |
||
630 | #endif |
||
631 | if (send_cmd(BT_TEST, NULL)) |
||
632 | { |
||
633 | #ifdef DEBUG |
||
634 | debug_pgm(PSTR("BT found 4800 Baud")); |
||
635 | #endif |
||
636 | BT_found = 4; |
||
637 | } |
||
638 | } |
||
639 | } |
||
640 | |||
641 | if (BT_found > 0) |
||
642 | { |
||
643 | /* Set comm_mode to CMD */ |
||
644 | comm_mode = BT_CMD; |
||
645 | // test(); |
||
646 | // if (BTIsSlave==false) |
||
647 | // { |
||
648 | /* Set BTM Baudrate */ |
||
649 | if (!(send_cmd(BT_SET_57600, NULL))) |
||
650 | init_error = true; |
||
651 | uart1_init(UART_BAUD_SELECT(57600, F_CPU)); |
||
652 | _delay_ms(100); |
||
653 | // test(); |
||
654 | /* Clear remote address */ |
||
655 | if(!(send_cmd(BT_CLEAR_ADDRESS, NULL))) |
||
656 | init_error = true; |
||
657 | // test(); |
||
658 | /* Set BTM to SLAVE */ |
||
659 | if (!(send_cmd(BT_SET_SLAVE, NULL))) |
||
660 | init_error = true; |
||
661 | // test(); |
||
662 | /* Set BTM PIN */ |
||
663 | if(!(send_cmd(BT_SET_PIN, NULL))) |
||
664 | init_error = true; |
||
665 | // test(); |
||
666 | /* Set BTM Name */ |
||
667 | if(!(send_cmd(BT_SET_NAME, NULL))) |
||
668 | init_error = true; |
||
669 | _delay_ms(300); |
||
670 | // test(); |
||
671 | if(!(send_cmd(BT_SET_DISPWRDOWN, NULL))) |
||
672 | init_error = true; |
||
673 | // } |
||
674 | // test(); |
||
675 | /* Set BTM Echo aus */ |
||
676 | send_cmd(BT_SET_NOECHO, NULL); |
||
677 | // test(); |
||
678 | comm_mode = BT_NOECHO; |
||
679 | /* Set BTM Answer aus */ |
||
680 | send_cmd(BT_SET_NOANSWER, NULL); |
||
681 | // test(); |
||
682 | |||
683 | bt_mode = BLUETOOTH_SLAVE; |
||
684 | |||
685 | set_BTOff(); |
||
686 | |||
687 | |||
688 | if (!init_error) |
||
689 | { |
||
690 | WriteBTInitFlag(); // Init merken |
||
691 | WriteBTSlaveFlag(); |
||
692 | return true; |
||
693 | } |
||
694 | else |
||
695 | return false; |
||
696 | } |
||
697 | else |
||
698 | { |
||
699 | set_BTOff(); |
||
700 | return false; |
||
701 | } |
||
702 | |||
703 | } |
||
704 | |||
705 | |||
706 | #ifndef SaveMem |
||
707 | |||
708 | //-------------------------------------------------------------- |
||
709 | uint16_t bt_set_mode(const bt_mode_t mode) |
||
710 | { |
||
711 | // if (update_comm_mode(0) == BT_DATA) // 30.1.2012 CB |
||
712 | // return false; |
||
713 | |||
714 | if (mode == bt_mode) |
||
715 | return true; |
||
716 | |||
717 | if (mode == BLUETOOTH_MASTER) |
||
718 | if (send_cmd(BT_SET_MASTER, NULL)) |
||
719 | { |
||
720 | bt_mode = BLUETOOTH_MASTER; |
||
721 | // test(); |
||
722 | send_cmd(BT_DISABLE_AUTOCONNECT, NULL); |
||
723 | debug_pgm(PSTR("bt_setmode: Master is set")); |
||
724 | WriteBTMasterFlag(); |
||
725 | |||
726 | |||
727 | } |
||
728 | |||
729 | if (mode == BLUETOOTH_SLAVE) |
||
730 | { |
||
731 | if (send_cmd(BT_SET_SLAVE, NULL)) |
||
732 | { |
||
733 | bt_mode = BLUETOOTH_SLAVE; |
||
734 | debug_pgm(PSTR("bt_setmode: Slave is set")); |
||
735 | } |
||
736 | send_cmd(BT_SET_NOECHO, NULL); |
||
737 | comm_mode = BT_NOECHO; |
||
738 | /* Set BTM Answer aus */ |
||
739 | send_cmd(BT_SET_NOANSWER, NULL); |
||
740 | |||
741 | bt_mode = BLUETOOTH_SLAVE; |
||
742 | WriteBTSlaveFlag(); |
||
743 | } |
||
744 | // test(); |
||
745 | return mode == bt_mode; |
||
746 | } |
||
747 | |||
748 | |||
749 | //-------------------------------------------------------------- |
||
750 | uint16_t bt_receive(void *data, uint8_t length, uint16_t timeout_ms) |
||
751 | { |
||
752 | uint8_t rec_length = 0; |
||
753 | uint8_t i = 0; |
||
754 | |||
755 | // while_timeout(true, timeout_ms); |
||
756 | for(uint16_t __timeout = 0; __timeout++ <= true && (timeout_ms); _delay_ms(true ? 1 : 0)) |
||
757 | { |
||
758 | if (i == length) |
||
759 | return true; |
||
760 | |||
761 | uart_receive(); |
||
762 | |||
763 | if (fifo_is_empty(&in_fifo)) |
||
764 | continue; |
||
765 | |||
766 | if (update_comm_mode(0) != BT_DATA) |
||
767 | { |
||
768 | #ifdef DEBUG |
||
769 | debug_pgm(PSTR("not connected")); |
||
770 | #endif |
||
771 | return false; |
||
772 | } |
||
773 | // We have a connection |
||
774 | if (timeout_ms == 0) |
||
775 | timeout_ms += 2000; |
||
776 | |||
777 | if (fifo_is_empty(&in_fifo)) |
||
778 | continue; |
||
779 | |||
780 | // Find starting point of packet |
||
781 | if (!rec_length) |
||
782 | { |
||
783 | fifo_read(&in_fifo, (char *)&rec_length); |
||
784 | |||
785 | if (rec_length != length) |
||
786 | { |
||
787 | rec_length = 0; |
||
788 | } |
||
789 | else |
||
790 | { |
||
791 | // You've got mail! |
||
792 | timeout_ms += 2000; |
||
793 | } |
||
794 | } |
||
795 | else |
||
796 | { |
||
797 | fifo_read(&in_fifo, (char *)data + i); |
||
798 | i++; |
||
799 | } |
||
800 | } |
||
801 | return false; |
||
802 | } |
||
803 | |||
804 | #endif |
||
805 | |||
806 | #ifndef SaveMem |
||
807 | |||
808 | |||
809 | //-------------------------------------------------------------- |
||
810 | uint16_t bt_send(void *data, const uint8_t length) |
||
811 | { |
||
812 | if (update_comm_mode(0) == BT_CMD) |
||
813 | return false; |
||
814 | |||
815 | uart_send((const char *)&length, 1); |
||
816 | uart_send((char *)data, length); |
||
817 | return (update_comm_mode(0) == BT_DATA); |
||
818 | } |
||
819 | |||
820 | |||
821 | #ifdef SQUIRREL |
||
822 | |||
823 | //-------------------------------------------------------------- |
||
824 | uint16_t bt_connect(const char *address) |
||
825 | { |
||
826 | fifo_init(&in_fifo, bt_buffer, IN_FIFO_SIZE); |
||
827 | uart_receive(); |
||
828 | fifo_clear(&in_fifo); |
||
829 | |||
830 | // Maybe we already disconnected??? |
||
831 | if (BT_DATA == update_comm_mode(0)) |
||
832 | { |
||
833 | #ifdef DEBUG |
||
834 | debug_pgm(PSTR("We are still connected...")); |
||
835 | #endif |
||
836 | return false; |
||
837 | } |
||
838 | test(); |
||
839 | |||
840 | |||
841 | /* |
||
842 | if (!send_cmd(BT_DISABLE_AUTOCONNECT, address)) |
||
843 | return false; |
||
844 | */ |
||
845 | |||
846 | test(); |
||
847 | #ifdef DEBUG |
||
848 | debug_pgm (PSTR ("SET_ADD")); |
||
849 | #endif |
||
850 | |||
851 | if (!send_cmd(BT_SET_ADDRESS, address)) |
||
852 | return false; |
||
853 | |||
854 | test(); |
||
855 | #ifdef DEBUG |
||
856 | debug_pgm (PSTR ("CONNECT")); |
||
857 | #endif |
||
858 | |||
859 | if (!send_cmd(BT_CONNECT, NULL)) |
||
860 | return false; |
||
861 | #ifdef DEBUG |
||
862 | debug_pgm (PSTR ("WAIT FOR COMM")); |
||
863 | #endif |
||
864 | return (BT_DATA == update_comm_mode(60000)); |
||
865 | } |
||
866 | |||
867 | |||
868 | //-------------------------------------------------------------- |
||
869 | uint16_t bt_disconnect(void) |
||
870 | { |
||
871 | |||
872 | |||
873 | if (BT_CMD == update_comm_mode(0)) |
||
874 | { |
||
875 | fifo_clear(&in_fifo); |
||
876 | return true; |
||
877 | } |
||
878 | |||
879 | // Switch to online cmd mode |
||
880 | for (uint8_t i = 0; i < 4; i++) |
||
881 | { |
||
882 | const char plus = '+'; |
||
883 | uart_send(&plus, 1); |
||
884 | _delay_ms(1000); |
||
885 | } |
||
886 | |||
887 | comm_mode = BT_CMD; |
||
888 | |||
889 | if (!send_cmd(BT_DISCONNECT, NULL)) |
||
890 | return false; |
||
891 | |||
892 | // test(); |
||
893 | if (!send_cmd(BT_CLEAR_ADDRESS, NULL)) |
||
894 | return false; |
||
895 | // test(); |
||
896 | |||
897 | if (BT_CMD == update_comm_mode(10000)) |
||
898 | { |
||
899 | fifo_clear(&in_fifo); |
||
900 | return true; |
||
901 | } |
||
902 | #ifdef DEBUG |
||
903 | debug_pgm(PSTR("Still in DATA??")); |
||
904 | #endif |
||
905 | return false; |
||
906 | |||
907 | } |
||
908 | |||
909 | /* |
||
910 | |||
911 | BTM-222 Softwareversion 4.35 |
||
912 | Inquiry Results: |
||
913 | 111111111222222222233333333334 |
||
914 | 01234567890123456789012345678901234567890 |
||
915 | |||
916 | 1 LE091452 0024-2C-BEB0CA |
||
917 | 2 E71 c 0024-7C-3EC9B9 |
||
918 | |||
919 | BTM-222 Softwareversion 6.26 |
||
920 | Inquiry Results: |
||
921 | 1 E71 c 0024-7C-3EC9B9 N.A. |
||
922 | 2 LE091452 0024-2C-BEB0CA N.A. |
||
923 | |||
924 | */ |
||
925 | |||
926 | |||
927 | //-------------------------------------------------------------- |
||
928 | void copy_mac(const char *src, char *dst) |
||
929 | { |
||
930 | uint8_t off = 0; |
||
931 | |||
932 | for (uint8_t i = 0; i < 40; i++) |
||
933 | { |
||
934 | if (src[i] == '-') if (src[i+3] == '-')// MAC Adresse suchen |
||
935 | { |
||
936 | off = i-4; |
||
937 | break; |
||
938 | } |
||
939 | } |
||
940 | |||
941 | for (uint8_t i = 0; i < 14; i++) |
||
942 | { |
||
943 | if (src[i + off] == '-') |
||
944 | off++; |
||
945 | |||
946 | dst[i] = src[i + off]; |
||
947 | } |
||
948 | } |
||
949 | //-------------------------------------------------------------- |
||
950 | void copy_DevName(const char *src, char *dst) |
||
951 | { |
||
952 | uint8_t off = 0; |
||
953 | |||
954 | |||
955 | for (uint8_t i = 0; i < 14; i++) |
||
956 | { |
||
957 | if (src[i] == ' ') if (src[i+1] == ' ') break; // nach zwei Leerzeichen ist der Name zuende |
||
958 | dst[i] = src[i + off]; |
||
959 | } |
||
960 | } |
||
961 | |||
962 | //-------------------------------------------------------------- |
||
963 | uint16_t bt_discover(char result[8][12]) |
||
964 | |||
965 | |||
966 | { |
||
967 | |||
968 | |||
969 | // if (!bt_set_mode(BLUETOOTH_MASTER)) |
||
970 | // return false; |
||
971 | |||
972 | if (!send_cmd(BT_FIND_DEVICES, NULL)) |
||
973 | return false; |
||
974 | |||
975 | char buffer[255]; //oversized, but who cares? |
||
976 | char *bufferhead = buffer; |
||
977 | uint16_t pos = 0; |
||
978 | uint16_t Timeout = 28000; |
||
979 | uint16_t pos1 = 0; |
||
980 | uint16_t posC = 0; |
||
981 | #ifdef DEBUG |
||
982 | debug_pgm(PSTR("discover2")); |
||
983 | #endif |
||
984 | do |
||
985 | { |
||
986 | uart_receive(); |
||
987 | Timeout--; |
||
988 | pos1++; |
||
989 | posC++; |
||
990 | _delay_ms(1); |
||
991 | write_ndigit_number_u(0,5,fifo_getcount(&in_fifo),5,0,0); |
||
992 | if (posC ==1000) |
||
993 | { |
||
994 | lcd_printp_at (i++, 1, PSTR("."), 0); |
||
995 | posC = 0; |
||
996 | |||
997 | } |
||
998 | |||
999 | |||
1000 | if (fifo_is_full(&in_fifo)) break; |
||
1001 | #ifdef DEBUG |
||
1002 | if (fifo_search(&in_fifo, PSTR("Found."))) debug_pgm(PSTR("Suchen ende1")); |
||
1003 | #endif |
||
1004 | } |
||
1005 | |||
1006 | // while (((Timeout > 0) ||(!fifo_strstr_pgm(&in_fifo, PSTR("Inquiry Results:\r\n")))) && (!fifo_strstr_pgm(&in_fifo, PSTR("Found")))); |
||
1007 | while ((Timeout > 0)||(!fifo_strstr_pgm(&in_fifo, PSTR("Inquiry Results:\r\n")))); |
||
1008 | #ifdef DEBUG |
||
1009 | debug_pgm(PSTR("Suchen ende2")); |
||
1010 | |||
1011 | |||
1012 | if (Timeout == 0) debug_pgm(PSTR("Timeout")); |
||
1013 | |||
1014 | if (fifo_is_full(&in_fifo)) debug_pgm(PSTR("Fifo Overrun, zuviele BT Devices")); |
||
1015 | #endif |
||
1016 | while (!fifo_is_empty(&in_fifo)) |
||
1017 | { |
||
1018 | // Get next line |
||
1019 | while (!fifo_cmp_pgm(&in_fifo, PSTR("\r\n"))) |
||
1020 | { |
||
1021 | fifo_read(&in_fifo, bufferhead); |
||
1022 | bufferhead++; |
||
1023 | } |
||
1024 | // terminate string |
||
1025 | *bufferhead = 0; |
||
1026 | |||
1027 | //reset bufferhead |
||
1028 | bufferhead = buffer; |
||
1029 | |||
1030 | if (strlen(buffer) == 0) |
||
1031 | continue; //the empty line before end of inquiry |
||
1032 | |||
1033 | if (strstr_P(buffer, PSTR("Inquiry End"))) |
||
1034 | // if (searchend) |
||
1035 | { |
||
1036 | fifo_clear(&in_fifo); |
||
1037 | // test(); |
||
1038 | return true; |
||
1039 | } |
||
1040 | |||
1041 | |||
1042 | copy_DevName(&buffer[3],device_list[pos].DevName); |
||
1043 | device_list[pos].DevName[14] = 0; // Stringende |
||
1044 | |||
1045 | copy_mac(&buffer[3], device_list[pos].mac); |
||
1046 | |||
1047 | // for (uint16_t i = 0; i < 15; i++) |
||
1048 | // { |
||
1049 | // |
||
1050 | //// USART_putc((device_list[pos].DevName[i])); |
||
1051 | // lcd_print_hex((device_list[pos].DevName[i]),0); |
||
1052 | // } |
||
1053 | // USART_putc('\n'); |
||
1054 | // |
||
1055 | // |
||
1056 | // for (uint16_t i = 0; i < 12; i++) |
||
1057 | // { |
||
1058 | // |
||
1059 | // USART_putc((device_list[pos].mac[i])); |
||
1060 | // |
||
1061 | // } |
||
1062 | // |
||
1063 | // USART_putc('\n'); |
||
1064 | // USART_putc('\r'); |
||
1065 | pos++; |
||
1066 | } |
||
1067 | |||
1068 | return false; |
||
1069 | } |
||
1070 | |||
1071 | device_info device_list[NUTS_LIST]; |
||
1072 | |||
1073 | void bt_downlink_init(void) |
||
1074 | { |
||
1075 | |||
1076 | |||
1077 | fifo_init(&in_fifo, bt_buffer, IN_FIFO_SIZE); |
||
1078 | _delay_ms(100); |
||
1079 | // debug_pgm(PSTR("bt_init")); |
||
1080 | uart_receive(); |
||
1081 | fifo_clear(&in_fifo); |
||
1082 | // send_cmd(BT_TEST, NULL); |
||
1083 | debug_pgm(PSTR("Downlink_init Start")); |
||
1084 | // if (BTIsSlave == true) // nur Init wenn BT ist Slave |
||
1085 | // { |
||
1086 | comm_mode = BT_NOECHO; |
||
1087 | |||
1088 | if (!send_cmd (BT_SET_ECHO,NULL)) { |
||
1089 | debug_pgm(PSTR("Downlink_init:Couldn't set Echo!")); |
||
1090 | |||
1091 | } |
||
1092 | |||
1093 | comm_mode = BT_CMD; |
||
1094 | if (!send_cmd(BT_SET_ANSWER,NULL)) { |
||
1095 | debug_pgm(PSTR("Downlink_init:Couldn't set Answer!")); |
||
1096 | |||
1097 | } |
||
1098 | // send_cmd(BT_TEST, NULL); |
||
1099 | |||
1100 | if (!bt_set_mode(BLUETOOTH_MASTER)==BLUETOOTH_MASTER) |
||
1101 | |||
1102 | { |
||
1103 | // debug_pgm(PSTR("Downlink_init:Couldn't set master!")); |
||
1104 | // return; |
||
1105 | } |
||
1106 | #ifdef DEBUG |
||
1107 | debug_pgm(PSTR("Downlink_init:master set is ")); |
||
1108 | #endif |
||
1109 | |||
1110 | WriteBTMasterFlag(); // Master merken |
||
1111 | |||
1112 | } |
||
1113 | |||
1114 | |||
1115 | void bt_searchDevice(void) //Bluetoothgeräte suchen |
||
1116 | |||
1117 | { |
||
1118 | |||
1119 | char result[8][12]; |
||
1120 | |||
1121 | |||
1122 | |||
1123 | for (uint8_t i = 0; i < 8; i++) // alte Liste löschen |
||
1124 | for (uint8_t j = 0; j < 12; j++) |
||
1125 | result[i][j] = 0; |
||
1126 | #ifdef DEBUG |
||
1127 | debug_pgm(PSTR("Search Device:BT_discover")); |
||
1128 | #endif |
||
1129 | if (bt_discover(result)) |
||
1130 | { |
||
1131 | bt_devicecount = 0; |
||
1132 | #ifdef DEBUG |
||
1133 | debug_pgm(PSTR("Search Device:Search ok")); |
||
1134 | #endif |
||
1135 | for (uint8_t i = 0; i < 8; i++) |
||
1136 | { |
||
1137 | if (valid(i)) |
||
1138 | bt_devicecount++; |
||
1139 | else break; |
||
1140 | } |
||
1141 | } |
||
1142 | #ifdef DEBUG |
||
1143 | else |
||
1144 | |||
1145 | debug_pgm(PSTR("Search Device:Search failed")); |
||
1146 | #endif |
||
1147 | // } |
||
1148 | |||
1149 | } |
||
1150 | |||
1151 | |||
1152 | //-------------------------------------------------------------- |
||
1153 | uint16_t bt_receiveNMEA(void) |
||
1154 | { |
||
1155 | |||
1156 | char received; |
||
1157 | static uint8_t line_flag = 1; |
||
1158 | static char* ptr_write = rx_buffer; |
||
1159 | |||
1160 | uart_receive(); |
||
1161 | |||
1162 | if (fifo_is_empty(&in_fifo)) |
||
1163 | // continue; |
||
1164 | // break; |
||
1165 | return true; |
||
1166 | |||
1167 | |||
1168 | |||
1169 | if (update_comm_mode(0) != BT_DATA) |
||
1170 | { |
||
1171 | #ifdef DEBUG |
||
1172 | debug_pgm(PSTR("not connected")); |
||
1173 | #endif |
||
1174 | return false; |
||
1175 | } |
||
1176 | |||
1177 | // We have a connection |
||
1178 | // if (timeout_ms == 0) |
||
1179 | // timeout_ms += 2000; |
||
1180 | |||
1181 | if (fifo_is_empty(&in_fifo)) |
||
1182 | // continue; |
||
1183 | // break; |
||
1184 | return true; |
||
1185 | |||
1186 | fifo_read(&in_fifo, &received); |
||
1187 | // Find starting point of packet |
||
1188 | if (rx_ready == 0) |
||
1189 | { |
||
1190 | if ((received == start) && line_flag) |
||
1191 | { // start '$' |
||
1192 | line_flag = 0; // New line has begun |
||
1193 | ptr_write = rx_buffer; // Begin at start of buffer |
||
1194 | rx_len = 0; |
||
1195 | } |
||
1196 | if (line_flag == 0) |
||
1197 | { // Are we receiving a line? |
||
1198 | *ptr_write = received; // Add current byte |
||
1199 | rx_len++; |
||
1200 | |||
1201 | // GPS Datensatzende |
||
1202 | |||
1203 | if (received == end) |
||
1204 | { // End of MK-GPS or NMEA-line? |
||
1205 | line_flag = 1; // Yes, start new line |
||
1206 | rx_ready = 1; // Lock buffer until line has been processed |
||
1207 | } |
||
1208 | |||
1209 | |||
1210 | } |
||
1211 | |||
1212 | ptr_write++; |
||
1213 | if(rx_len == RXD_BUFFER_SIZE) line_flag = 1; // Line too long? Try again |
||
1214 | }//if (rx_ready == 0) |
||
1215 | return true; |
||
1216 | } |
||
1217 | // |
||
1218 | // return false; |
||
1219 | //} |
||
1220 | |||
1221 | //#endif |
||
1222 | |||
1223 | |||
1224 | |||
1225 | |||
1226 | #endif |
||
1227 | #endif |
||
1228 | #endif |