summaryrefslogtreecommitdiff
path: root/2004/n/asserv/src/serial.c
diff options
context:
space:
mode:
authorschodet2003-12-13 16:43:44 +0000
committerschodet2003-12-13 16:43:44 +0000
commitbbd4ad1a1034dd4776db3d364b5fa268923124c1 (patch)
treea7cb96c51d2ecc38c0741f5187cc7a69d6ae8704 /2004/n/asserv/src/serial.c
parent7852fb953115ea9363d8e40dfa90fa869c257bf8 (diff)
Implémentation des files de priorités séries.
Diffstat (limited to '2004/n/asserv/src/serial.c')
-rw-r--r--2004/n/asserv/src/serial.c172
1 files changed, 151 insertions, 21 deletions
diff --git a/2004/n/asserv/src/serial.c b/2004/n/asserv/src/serial.c
index a72f7db..0c113e7 100644
--- a/2004/n/asserv/src/serial.c
+++ b/2004/n/asserv/src/serial.c
@@ -29,18 +29,26 @@
#define HEXDIGIT(a) ((a) <= 9 ? (a) + '0' : (a) + ('a' - 10))
#define SERIAL2INT(a, b) (HEX (a) << 4 | HEX (b))
-#define CRLF "\r"
+#define CRLF '\r'
+#define EOB '\0'
/* Tampon de reception. */
-char serial_recv_buf[SERIAL_RECV_BUF_LEN];
-int serial_recv_buf_n;
+/* 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, serial_send_working;
+short serial_send_full;
+
+/* Tampon d'emmision non fiable. */
+#define SERIAL_UNRELIABLE_SEND_BUF_LEN 20
+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
@@ -51,8 +59,11 @@ serial_init (void)
serial_recv_working = 0;
serial_send_buf_n = 0;
serial_send_full = 0;
- serial_send_working = 0;
- printf ("!z" CRLF);
+ serial_unreliable_send_buf_n = 0;
+ serial_unreliable_send_full = 0;
+ serial_send_char ('!');
+ serial_send_char ('z');
+ serial_send_char (CRLF);
}
/* Recois des caractres. */
@@ -73,6 +84,53 @@ serial_recv ()
serial_recv_buf[serial_recv_buf_n++] = c;
}
+/* Envois des caractres. */
+#int_TBE
+serial_send ()
+{
+ char c;
+ if (serial_send_full)
+ {
+ /* Efface le tampon non-fiable. */
+ if (serial_unreliable_send_full)
+ {
+ serial_unreliable_send_buf_n = 0;
+ serial_unreliable_send_full = 0;
+ }
+ /* Envoie le caractre fiable. */
+ c = serial_send_buf[serial_send_buf_n++];
+ putc (c);
+ /* Si fin de tampon, on le libre. */
+ 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 caractre non-fiable. */
+ c = serial_unreliable_send_buf[serial_unreliable_send_buf_n++];
+ /* Si fin de tampon, on le libre. */
+ 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_send_buf_n < SERIAL_SEND_BUF_LEN); */
+ }
+ }
+}
+
/* Traite la commande recue. */
void
serial_parse (void)
@@ -107,43 +165,115 @@ serial_parse (void)
void
serial_send_error (char c)
{
- printf ("!e%c" CRLF, c);
+ serial_send_char ('!');
+ serial_send_char ('e');
+ serial_send_char (c);
+ serial_send_char (CRLF);
}
/* Envoie un code ok. */
void
serial_send_ok (char c)
{
- printf ("!o%c" CRLF, c);
+ serial_send_char ('!');
+ serial_send_char ('o');
+ serial_send_char (c);
+ 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)
{
- printf ("!m%c", side);
- serial_send_int (vacc);
- putc (',');
- serial_send_long (e);
- putc (',');
- serial_send_long (pwm);
- puts (CRLF);
+ 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);
+}
+
+/* Envoie un caractre. */
+void
+serial_send_char (char c)
+{
+ /* Bloque jusqu' fin d'emmission. */
+ while (serial_send_full)
+ ;
+ /* Ajoute le caractre. */
+ serial_send_buf[serial_send_buf_n++] = c;
+ /* Si fin de ligne, on envois. */
+ if (c == CRLF)
+ {
+ serial_send_full = 1;
+ serial_send_buf_n = 0;
+ c = serial_send_buf[serial_send_buf_n++];
+ enable_interrupts (INT_TBE);
+ putc (c);
+ }
+}
+
+/* Envoie un caractre non-fiable. */
+void
+serial_unreliable_send_char (char c)
+{
+ /* Drop si une transmission est en cours. */
+ if (serial_send_full || serial_unreliable_send_full) return;
+ /* Ajoute le caractre. */
+ serial_send_buf[serial_send_buf_n++] = c;
+ /* Si fin de buffer, on envois. */
+ if (c == EOB)
+ {
+ serial_unreliable_send_full = 1;
+ serial_unreliable_send_buf_n = 0;
+ c = serial_unreliable_send_buf[serial_unreliable_send_buf_n++];
+ enable_interrupts (INT_TBE);
+ putc (c);
+ }
}
/* Envoie un long. */
void
serial_send_long (unsigned long &value)
{
- putc (HEXDIGIT ((make8 (value, 1) >> 4) & 0xf));
- putc (HEXDIGIT (make8 (value, 1) & 0xf));
- putc (HEXDIGIT ((make8 (value, 0) >> 4) & 0xf));
- putc (HEXDIGIT (make8 (value, 0) & 0xf));
+ 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)
{
- putc (HEXDIGIT ((value >> 4) & 0xf));
- putc (HEXDIGIT (value & 0xf));
+ 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);
}