From bbd4ad1a1034dd4776db3d364b5fa268923124c1 Mon Sep 17 00:00:00 2001 From: schodet Date: Sat, 13 Dec 2003 16:43:44 +0000 Subject: Implémentation des files de priorités séries. --- 2004/n/asserv/src/main.c | 4 +- 2004/n/asserv/src/motor.h | 8 ++- 2004/n/asserv/src/serial.c | 172 +++++++++++++++++++++++++++++++++++++++------ 2004/n/asserv/src/serial.h | 12 +++- 4 files changed, 168 insertions(+), 28 deletions(-) (limited to '2004/n/asserv/src') diff --git a/2004/n/asserv/src/main.c b/2004/n/asserv/src/main.c index 1dd817e..7f3732c 100644 --- a/2004/n/asserv/src/main.c +++ b/2004/n/asserv/src/main.c @@ -57,7 +57,7 @@ A1-5 B2-7 C4-5 D2-6 E0-2 #fuses HS,WDT,WDT128,PUT,NOBROWNOUT,NOLVP #use delay(clock=20000000) #use rs232(baud=115200,xmit=PIN_C6,parity=N,rcv=PIN_C7,bits=8) -#priority EXT,EXT1,RDA,TIMER2 +#priority EXT,EXT1,RDA,TBE,TIMER2 #include "motor.c" #include "serial.c" @@ -98,8 +98,6 @@ main_init (void) enable_interrupts (INT_TIMER2); enable_interrupts (INT_RDA); enable_interrupts (GLOBAL); - // Heu, on s'en servait pour le bus... - //output_low (PIN_B2); } void diff --git a/2004/n/asserv/src/motor.h b/2004/n/asserv/src/motor.h index 92b1680..be29c03 100644 --- a/2004/n/asserv/src/motor.h +++ b/2004/n/asserv/src/motor.h @@ -27,7 +27,7 @@ /* +AutoDec */ -/* Initialise le modue moteur. */ +/* Initialise le module moteur. */ void motor_init (void); @@ -35,6 +35,10 @@ motor_init (void); signed long safe_add_long (signed long &a, signed long &b); +/* Soustraction limitée à 32000. */ +signed long +safe_sub_long (signed long &a, signed long &b); + /* Limite un entier entre -MAX et MAX. */ signed long boundary (signed long &n, signed long &max); @@ -84,7 +88,7 @@ motor_parse (void); /* Démarre l'asservissement. */ void -motor_toggle_asservi (short fl); +motor_toggle_asservi (void); /* -AutoDec */ 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 caractères. */ @@ -73,6 +84,53 @@ serial_recv () 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. */ + if (serial_unreliable_send_full) + { + 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_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 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) + { + 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 caractère 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 caractère. */ + 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); } diff --git a/2004/n/asserv/src/serial.h b/2004/n/asserv/src/serial.h index 6595039..e954c75 100644 --- a/2004/n/asserv/src/serial.h +++ b/2004/n/asserv/src/serial.h @@ -27,8 +27,8 @@ /* Tampon de reception. */ #define SERIAL_RECV_BUF_LEN 20 -extern char serial_recv_buf[SERIAL_RECV_BUF_LEN]; -extern int serial_recv_buf_n; +char serial_recv_buf[SERIAL_RECV_BUF_LEN]; +int serial_recv_buf_n; /* Macros de récupération de la commande recu. */ #define HEXBIN(a) (((a) >= '0' && (a) <= '9') ? (a) - '0' : (a) - ('a' - 10)) @@ -57,6 +57,10 @@ serial_init (void); #int_RDA serial_recv (); +/* Envois des caractères. */ +#int_TBE +serial_send (); + /* Traite la commande recue. */ void serial_parse (void); @@ -73,6 +77,10 @@ serial_send_ok (char c); void serial_send_motor_stat (char side, unsigned int vacc, unsigned long &e, unsigned long &pwm); +/* Envoie un caractère. */ +void +serial_send_char (char c); + /* Envoie un long. */ void serial_send_long (unsigned long &value); -- cgit v1.2.3