/* serial.c */ /* APBTasserv - asservissement Robot 2004 {{{ * * Copyright (C) 2003 Nicolas Schodet * * Robot APB Team/Efrei 2004. * Web: http://assos.efrei.fr/robot/ * Mail: robot AT efrei DOT fr * * This program 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 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ #include "serial.h" #include "motor.h" /* Macros de lecture du tampon. */ #define HEXDIGIT(a) ((a) <= 9 ? (a) + '0' : (a) + ('a' - 10)) #define SERIAL2INT(a, b) (HEX (a) << 4 | HEX (b)) #define CRLF '\r' #define EOB '\0' /* Tampon de reception. */ /* Compilateur de merde ! * char serial_recv_buf[SERIAL_RECV_BUF_LEN]; * int serial_recv_buf_n; */ short serial_recv_full, serial_recv_working; /* Tampon d'emmission. */ #define SERIAL_SEND_BUF_LEN 20 char serial_send_buf[SERIAL_SEND_BUF_LEN]; int serial_send_buf_n; short serial_send_full; /* Tampon d'emmision non fiable. */ #define SERIAL_UNRELIABLE_SEND_BUF_LEN 34 char serial_unreliable_send_buf[SERIAL_UNRELIABLE_SEND_BUF_LEN]; int serial_unreliable_send_buf_n; short serial_unreliable_send_full; /* Initialise la reception serie. */ void serial_init (void) { serial_recv_buf_n = 0; serial_recv_full = 0; serial_recv_working = 0; serial_send_buf_n = 0; serial_send_full = 0; serial_unreliable_send_buf_n = 0; serial_unreliable_send_full = 0; } /* Recois des caractères. */ #int_RDA serial_recv () { char c; c = getc (); if (serial_recv_full) return; if (c == '\n' || c == '\r') { /* Fin de commande. */ if (serial_recv_buf_n) serial_recv_full = 1; } else { if (c == '!' || serial_recv_buf_n >= SERIAL_RECV_BUF_LEN) /* Début de commande ou buffer overflow. */ serial_recv_buf_n = 0; serial_recv_buf[serial_recv_buf_n++] = c; } } /* Envois des caractères. */ #int_TBE serial_send () { char c; if (serial_send_full) { /* Efface le tampon non-fiable. */ serial_unreliable_send_buf_n = 0; serial_unreliable_send_full = 0; /* Envoie le caractère fiable. */ c = serial_send_buf[serial_send_buf_n++]; putc (c); /* Si fin de tampon, on le libère. */ if (c == CRLF) { serial_send_buf_n = 0; serial_send_full = 0; disable_interrupts (INT_TBE); } /* On ne fait pas le test, normalement ne doit pas arriver. * assert (serial_send_buf_n < SERIAL_SEND_BUF_LEN); */ } else if (serial_unreliable_send_full) { /* Envoie le caractère non-fiable. */ c = serial_unreliable_send_buf[serial_unreliable_send_buf_n++]; /* Si fin de tampon, on le libère. */ if (c == EOB) { serial_unreliable_send_buf_n = 0; serial_unreliable_send_full = 0; disable_interrupts (INT_TBE); } else putc (c); /* On ne fait pas le test, normalement ne doit pas arriver. * assert (serial_unreliable_send_buf_n < * SERIAL_UNRELIABLE_SEND_BUF_LEN); */ } else { /* Ne devrait pas arriver, mais soyons prudent. */ disable_interrupts (INT_TBE); } } /* Traite la commande recue. */ void serial_parse (void) { restart_wdt (); /* Si le tampon n'est pas plein, bye, bye. */ if (!serial_recv_full || serial_recv_working) return; serial_recv_working = 1; /* Vérifie la présence de caractère de début de séquence et la taille du * tampon. */ if (serial_recv_buf_n > 1 && serial_recv_buf[0] == '!') { /* Est-ce une commande moteur. */ if (!motor_parse ()) { switch (serial_recv_com ()) { case 'z': reset_cpu (); break; default: serial_send_error ('?'); break; } } } else serial_send_error ('?'); serial_recv_buf_n = 0; serial_recv_working = 0; serial_recv_full = 0; } /* Envoie un code rezet. */ void serial_send_rezet (void) { serial_send_char ('!'); serial_send_char ('z'); switch (restart_cause ()) { case WDT_TIMEOUT: serial_send_char ('w'); break; case MCLR_FROM_SLEEP: serial_send_char ('m'); break; case BROWNOUT_RESTART: serial_send_char ('b'); break; case NORMAL_POWER_UP: break; default: serial_send_char ('?'); break; } serial_send_char (CRLF); } /* Envoie un code d'erreur. */ void serial_send_error (char c) { serial_send_char ('!'); serial_send_char ('E'); serial_send_char (c); serial_send_char (CRLF); } /* Acquitte la trame en la renvoyant à l'expediteur. */ void serial_send_ok (void) { int i; for (i = 0; i < serial_recv_buf_n; i++) serial_send_char (serial_recv_buf[i]); serial_send_char (CRLF); } /* Envoie un rapport d'un moteur. */ void serial_send_motor_stat (char side, unsigned int vacc, unsigned long &e, unsigned long &pwm) { serial_unreliable_send_char ('!'); serial_unreliable_send_char ('M'); serial_unreliable_send_char (side); serial_unreliable_send_int (vacc); serial_unreliable_send_char (','); serial_unreliable_send_long (e); serial_unreliable_send_char (','); serial_unreliable_send_long (pwm); serial_unreliable_send_char (CRLF); } /* Envois les compteurs moteurs. */ void serial_send_motor_cpt (signed long &g, signed long &d) { serial_unreliable_send_char ('!'); serial_unreliable_send_char ('C'); serial_unreliable_send_long (g); serial_unreliable_send_char (','); serial_unreliable_send_long (d); serial_unreliable_send_char (CRLF); } /* Envoie un signal de fin de ttl. */ void serial_send_motor_ttl (void) { serial_send_char ('!'); serial_send_char ('T'); serial_send_char (CRLF); } /* Envois la valeur des entrées gpi. */ void serial_send_gpi (int gpi) { serial_send_char ('!'); serial_send_char ('H'); serial_send_int (gpi); serial_send_char (CRLF); } /* Envoie un caractère. */ void serial_send_char (char c) { /* Bloque jusqu'à fin d'emmission. */ while (serial_send_full) ; /* Ajoute le caractère. */ serial_send_buf[serial_send_buf_n++] = c; /* Si fin de ligne, on envois. */ if (c == CRLF) { disable_interrupts (INT_TBE); serial_send_full = 1; serial_send_buf_n = 0; enable_interrupts (INT_TBE); } } /* Envoie un caractère non-fiable. */ void serial_unreliable_send_char (char c) { /* Drop si une transmission est en cours ou si une transmission fiable est * entamée. */ if (serial_send_buf_n || serial_unreliable_send_full) return; /* Ajoute le caractère. */ serial_unreliable_send_buf[serial_unreliable_send_buf_n++] = c; /* Si fin de buffer, on envois. */ if (c == EOB) { disable_interrupts (INT_TBE); serial_unreliable_send_full = 1; serial_unreliable_send_buf_n = 0; enable_interrupts (INT_TBE); } } /* Envoie un long. */ void serial_send_long (unsigned long &value) { serial_send_char (HEXDIGIT ((make8 (value, 1) >> 4) & 0xf)); serial_send_char (HEXDIGIT (make8 (value, 1) & 0xf)); serial_send_char (HEXDIGIT ((make8 (value, 0) >> 4) & 0xf)); serial_send_char (HEXDIGIT (make8 (value, 0) & 0xf)); } /* Envoie un long non-fiable. */ void serial_unreliable_send_long (unsigned long &value) { serial_unreliable_send_char (HEXDIGIT ((make8 (value, 1) >> 4) & 0xf)); serial_unreliable_send_char (HEXDIGIT (make8 (value, 1) & 0xf)); serial_unreliable_send_char (HEXDIGIT ((make8 (value, 0) >> 4) & 0xf)); serial_unreliable_send_char (HEXDIGIT (make8 (value, 0) & 0xf)); } /* Envoie un int. */ void serial_send_int (unsigned int &value) { serial_send_char (HEXDIGIT ((value >> 4) & 0xf)); serial_send_char (HEXDIGIT (value & 0xf)); } /* Envoie un int non-fiable. */ void serial_unreliable_send_int (unsigned int &value) { serial_unreliable_send_char (HEXDIGIT ((value >> 4) & 0xf)); serial_unreliable_send_char (HEXDIGIT (value & 0xf)); } /* Termine le bloc non-fiable. */ void serial_unreliable_send_eob (void) { serial_unreliable_send_char (EOB); }