summaryrefslogtreecommitdiff
path: root/n
diff options
context:
space:
mode:
Diffstat (limited to 'n')
-rw-r--r--n/avr/proto/Makefile2
-rw-r--r--n/avr/proto/avrconfig.h6
-rw-r--r--n/avr/proto/proto.c138
-rw-r--r--n/avr/proto/proto.h43
-rw-r--r--n/avr/proto/proto.txt1
-rw-r--r--n/avr/proto/proto_inline.c159
-rw-r--r--n/avr/proto/test_proto.c15
7 files changed, 210 insertions, 154 deletions
diff --git a/n/avr/proto/Makefile b/n/avr/proto/Makefile
index 9855cda..050f564 100644
--- a/n/avr/proto/Makefile
+++ b/n/avr/proto/Makefile
@@ -1,7 +1,7 @@
PROGS = test_proto
SOURCES = test_proto.c proto.c
DOC = proto.html
-EXTRACTDOC = avrconfig.h proto.h
+EXTRACTDOC = avrconfig.h proto.h proto_inline.c
MODULES = n/avr/rs232
CONFIGFILE = testconfig.h
# atmega8, atmega8535, atmega128...
diff --git a/n/avr/proto/avrconfig.h b/n/avr/proto/avrconfig.h
index 4bf8392..7935ea3 100644
--- a/n/avr/proto/avrconfig.h
+++ b/n/avr/proto/avrconfig.h
@@ -25,9 +25,7 @@
* }}} */
/* proto - Protocol module. */
-/** Maximum argument number. */
-#define AC_PROTO_MAX_ARGS 3
-/** Protocol arguments type. */
-#define AC_PROTO_ARG_TYPE uint8_t
+/** Maximum argument size. */
+#define AC_PROTO_ARGS_MAX_SIZE 8
#endif /* avrconfig_h */
diff --git a/n/avr/proto/proto.c b/n/avr/proto/proto.c
index a426c41..b23c6b4 100644
--- a/n/avr/proto/proto.c
+++ b/n/avr/proto/proto.c
@@ -35,42 +35,29 @@ proto_accept_digit (uint8_t c);
static void
proto_hex (uint8_t h);
-/* Send a arg. */
+/* Send a byte. */
inline static void
-proto_arg (proto_arg_t a);
+proto_arg (uint8_t a);
/* -AutoDec */
static uint8_t cmd;
-static proto_arg_t argv[AC_PROTO_MAX_ARGS];
-static uint8_t argc;
+static uint8_t size;
+static uint8_t args[AC_PROTO_ARGS_MAX_SIZE];
/** Step of decoding:
* - 0: nothing received.
* - 1: bang received.
* - 2: command received.
- * - 3, 4, 5: command received, and processing a number. */
+ * - 3: command received, and processing a number. */
static uint8_t step;
-static proto_cb_f func_cb;
-static proto_putc_f func_putc;
-
-/** Initialize and set the callback function. */
-void
-proto_init (proto_cb_f f_cb, proto_putc_f f_putc)
-{
- func_cb = f_cb;
- func_putc = f_putc;
-}
-
/** Accept a new character. */
void
proto_accept (uint8_t c)
{
if (c == '!')
- {
step = 1;
- }
else
{
switch (step)
@@ -83,12 +70,12 @@ proto_accept (uint8_t c)
if (isalpha (c))
{
cmd = c;
+ size = 0;
step = 2;
- argc = 0;
}
else
{
- func_cb ('?', 0, 0);
+ proto_callback ('?', 0, 0);
step = 0;
}
break;
@@ -96,21 +83,16 @@ proto_accept (uint8_t c)
/* Command received yet. */
if (c == '\r')
{
- func_cb (cmd, argc, argv);
+ proto_callback (cmd, size, args);
step = 0;
}
- else if (c == ',')
- {
- }
else
{
- step += sizeof (proto_arg_t) * 2 - 1;
+ step = 3;
proto_accept_digit (c);
}
break;
case 3:
- case 4:
- case 5:
step--;
proto_accept_digit (c);
break;
@@ -123,9 +105,9 @@ static void
proto_accept_digit (uint8_t c)
{
/* Test for argument list overflow. */
- if (argc >= AC_PROTO_MAX_ARGS)
+ if (size >= AC_PROTO_ARGS_MAX_SIZE)
{
- func_cb ('?', 0, 0);
+ proto_callback ('?', 0, 0);
step = 0;
return;
}
@@ -138,111 +120,39 @@ proto_accept_digit (uint8_t c)
c -= 'A' - 10;
else
{
- func_cb ('?', 0, 0);
+ proto_callback ('?', 0, 0);
return;
}
/* Add digit. */
- argv[argc] <<= 4;
- argv[argc] |= c;
+ args[size] <<= 4;
+ args[size] |= c;
if (step == 2)
- argc++;
+ size++;
}
/* Send a hex digit. */
static void
proto_hex (uint8_t h)
{
- func_putc (h >= 10 ? h - 10 + 'a' : h + '0');
+ proto_putc (h >= 10 ? h - 10 + 'a' : h + '0');
}
-/* Send a arg. */
+/* Send a byte. */
inline static void
-proto_arg (proto_arg_t a)
+proto_arg (uint8_t a)
{
- if (sizeof (proto_arg_t) == 2)
- {
- proto_hex (a >> 12);
- proto_hex ((a >> 8) & 0xf);
- }
proto_hex ((a >> 4) & 0xf);
proto_hex ((a >> 0) & 0xf);
}
/** Send a command, generic function. */
void
-proto_send (uint8_t cmd, uint8_t argc, proto_arg_t *argv)
-{
- func_putc ('!');
- func_putc (cmd);
- while (argc-- > 1)
- {
- proto_arg (*argv++);
- func_putc (',');
- }
- if (!argc)
- proto_arg (*argv);
- func_putc ('\r');
-}
-
-/** Send a command, no arg. */
-void
-proto_send0 (uint8_t cmd)
-{
- func_putc ('!');
- func_putc (cmd);
- func_putc ('\r');
-}
-
-/** Send a command, one arg. */
-void
-proto_send1 (uint8_t cmd, proto_arg_t a0)
-{
- func_putc ('!');
- func_putc (cmd);
- proto_arg (a0);
- func_putc ('\r');
-}
-
-/** Send a command, two arg. */
-void
-proto_send2 (uint8_t cmd, proto_arg_t a0, proto_arg_t a1)
-{
- func_putc ('!');
- func_putc (cmd);
- proto_arg (a0);
- func_putc (',');
- proto_arg (a1);
- func_putc ('\r');
-}
-
-/** Send a command, three arg. */
-void
-proto_send3 (uint8_t cmd, proto_arg_t a0, proto_arg_t a1, proto_arg_t a2)
-{
- func_putc ('!');
- func_putc (cmd);
- proto_arg (a0);
- func_putc (',');
- proto_arg (a1);
- func_putc (',');
- proto_arg (a2);
- func_putc ('\r');
-}
-
-/** Send a command, four arg. */
-void
-proto_send4 (uint8_t cmd, proto_arg_t a0, proto_arg_t a1, proto_arg_t a2,
- proto_arg_t a3)
+proto_send (uint8_t cmd, uint8_t size, uint8_t *args)
{
- func_putc ('!');
- func_putc (cmd);
- proto_arg (a0);
- func_putc (',');
- proto_arg (a1);
- func_putc (',');
- proto_arg (a2);
- func_putc (',');
- proto_arg (a3);
- func_putc ('\r');
+ proto_putc ('!');
+ proto_putc (cmd);
+ while (size--)
+ proto_arg (*args++);
+ proto_putc ('\r');
}
diff --git a/n/avr/proto/proto.h b/n/avr/proto/proto.h
index 2bfe7b8..5d6319f 100644
--- a/n/avr/proto/proto.h
+++ b/n/avr/proto/proto.h
@@ -26,49 +26,28 @@
#include <inttypes.h>
-typedef AC_PROTO_ARG_TYPE proto_arg_t;
-
-/** Protocol callback type. Take the command and the arguments. */
-typedef void (*proto_cb_f) (uint8_t cmd, uint8_t argc, proto_arg_t argv[]);
+/** Protocol callback function. Take the command and the arguments. Must be
+ * defined by the user. */
+void
+proto_callback (uint8_t cmd, uint8_t size, uint8_t *args);
-/** Protocol putc function type. Take a char to send. */
-typedef void (*proto_putc_f) (uint8_t c);
+/** Protocol putc function. Take a char to send. Must be defined by the
+ * user. */
+void
+proto_putc (uint8_t c);
/* +AutoDec */
-/** Initialize and set the callback function. */
-void
-proto_init (proto_cb_f f_cb, proto_putc_f f_putc);
-
/** Accept a new character. */
void
proto_accept (uint8_t c);
/** Send a command, generic function. */
void
-proto_send (uint8_t cmd, uint8_t argc, proto_arg_t *argv);
-
-/** Send a command, no arg. */
-void
-proto_send0 (uint8_t cmd);
-
-/** Send a command, one arg. */
-void
-proto_send1 (uint8_t cmd, proto_arg_t a0);
-
-/** Send a command, two arg. */
-void
-proto_send2 (uint8_t cmd, proto_arg_t a0, proto_arg_t a1);
-
-/** Send a command, three arg. */
-void
-proto_send3 (uint8_t cmd, proto_arg_t a0, proto_arg_t a1, proto_arg_t a2);
-
-/** Send a command, four arg. */
-void
-proto_send4 (uint8_t cmd, proto_arg_t a0, proto_arg_t a1, proto_arg_t a2,
- proto_arg_t a3);
+proto_send (uint8_t cmd, uint8_t size, uint8_t *args);
/* -AutoDec */
+#include "proto_inline.c"
+
#endif /* proto_h */
diff --git a/n/avr/proto/proto.txt b/n/avr/proto/proto.txt
index 1c8b84d..33f09f9 100644
--- a/n/avr/proto/proto.txt
+++ b/n/avr/proto/proto.txt
@@ -48,3 +48,4 @@ commandes, on utilise les fonctions |proto_send...|.
* Doc
*File: proto.exd
+*File: proto_inline.exd
diff --git a/n/avr/proto/proto_inline.c b/n/avr/proto/proto_inline.c
new file mode 100644
index 0000000..2919c50
--- /dev/null
+++ b/n/avr/proto/proto_inline.c
@@ -0,0 +1,159 @@
+/* proto_inline.c */
+/* {{{
+ *
+ * Copyright (C) 2005 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: 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.
+ *
+ * }}} */
+
+/* Send a command with no argument. */
+extern inline void
+proto_send0 (uint8_t cmd)
+{
+ proto_send (cmd, 0, 0);
+}
+
+/* Send a command with 1 byte argument. */
+extern inline void
+proto_send1b (uint8_t cmd, uint8_t arg0)
+{
+ uint8_t args[1];
+ args[0] = arg0;
+ proto_send (cmd, 1, args);
+}
+
+/* Send a command with 1 word argument. */
+extern inline void
+proto_send1w (uint8_t cmd, uint16_t arg0)
+{
+ uint8_t args[1 * 2];
+ args[0] = (arg0 >> 0) & 0xff;
+ args[0] = (arg0 >> 8) & 0xff;
+ proto_send (cmd, 1 * 2, args);
+}
+
+/* Send a command with 1 double word argument. */
+extern inline void
+proto_send1d (uint8_t cmd, uint32_t arg0)
+{
+ uint8_t args[1 * 4];
+ args[0] = arg0;
+ proto_send (cmd, 1 * 4, args);
+}
+
+/* Send a command with 2 bytes arguments. */
+extern inline void
+proto_send2b (uint8_t cmd, uint8_t arg0, uint8_t arg1)
+{
+ uint8_t args[2];
+ args[0] = arg0;
+ args[1] = arg1;
+ proto_send (cmd, 2, args);
+}
+
+/* Send a command with 2 words arguments. */
+extern inline void
+proto_send2w (uint8_t cmd, uint16_t arg0, uint16_t arg1)
+{
+ uint8_t args[2 * 2];
+ args[0] = arg0;
+ args[1] = arg1;
+ proto_send (cmd, 2 * 2, args);
+}
+
+/* Send a command with 2 double words arguments. */
+extern inline void
+proto_send2d (uint8_t cmd, uint32_t arg0, uint32_t arg1)
+{
+ uint8_t args[2 * 4];
+ args[0] = arg0;
+ args[1] = arg1;
+ proto_send (cmd, 2 * 4, args);
+}
+
+/* Send a command with 3 bytes arguments. */
+extern inline void
+proto_send3b (uint8_t cmd, uint8_t arg0, uint8_t arg1, uint8_t arg2)
+{
+ uint8_t args[3];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ proto_send (cmd, 3, args);
+}
+
+/* Send a command with 3 words arguments. */
+extern inline void
+proto_send3w (uint8_t cmd, uint16_t arg0, uint16_t arg1, uint16_t arg2)
+{
+ uint8_t args[3 * 2];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ proto_send (cmd, 3 * 2, args);
+}
+
+/* Send a command with 3 double words arguments. */
+extern inline void
+proto_send3d (uint8_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2)
+{
+ uint8_t args[3 * 4];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ proto_send (cmd, 3 * 4, args);
+}
+
+/* Send a command with 4 bytes arguments. */
+extern inline void
+proto_send4b (uint8_t cmd, uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t arg3)
+{
+ uint8_t args[4];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ args[3] = arg3;
+ proto_send (cmd, 4, args);
+}
+
+/* Send a command with 4 words arguments. */
+extern inline void
+proto_send4w (uint8_t cmd, uint16_t arg0, uint16_t arg1, uint16_t arg2, uint16_t arg3)
+{
+ uint8_t args[4 * 2];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ args[3] = arg3;
+ proto_send (cmd, 4 * 2, args);
+}
+
+/* Send a command with 4 double words arguments. */
+extern inline void
+proto_send4d (uint8_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
+{
+ uint8_t args[4 * 4];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ args[3] = arg3;
+ proto_send (cmd, 4 * 4, args);
+}
+
diff --git a/n/avr/proto/test_proto.c b/n/avr/proto/test_proto.c
index fa8f48a..1ce75f1 100644
--- a/n/avr/proto/test_proto.c
+++ b/n/avr/proto/test_proto.c
@@ -28,19 +28,28 @@
/* -AutoDec */
void
-test_callback (uint8_t c, uint8_t argc, proto_arg_t argv[])
+proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
- proto_send (c, argc, argv);
+ proto_send (cmd, size, args);
+}
+
+void
+proto_putc (uint8_t c)
+{
+ rs232_putc (c);
}
int
main (void)
{
rs232_init ();
- proto_init (test_callback, rs232_putc);
rs232_putc ('!');
rs232_putc ('z');
rs232_putc ('\r');
+ proto_send0 ('h');
+ proto_send1b ('e', 0xf0);
+ proto_send2w ('l', 0x1234, 0x5678);
+ proto_send1d ('o', 0x12345678);
while (1)
{
uint8_t c = rs232_getc ();