From 793b2bec21d37c45e498dd38008566f891dde86e Mon Sep 17 00:00:00 2001 From: schodet Date: Tue, 5 Jul 2005 18:43:16 +0000 Subject: Ajout de utils. --- n/avr/modules/utils/Makefile | 5 ++ n/avr/modules/utils/Makefile.module | 1 + n/avr/modules/utils/README | 25 +++++++ n/avr/modules/utils/avrconfig.h | 33 +++++++++ n/avr/modules/utils/byte.h | 100 ++++++++++++++++++++++++++ n/avr/modules/utils/test/Makefile | 18 +++++ n/avr/modules/utils/test/avrconfig.h | 33 +++++++++ n/avr/modules/utils/test/test_byte.c | 39 ++++++++++ n/avr/modules/utils/test/test_utils.c | 49 +++++++++++++ n/avr/modules/utils/utils.avr.h | 132 ++++++++++++++++++++++++++++++++++ n/avr/modules/utils/utils.h | 34 +++++++++ n/avr/modules/utils/utils.host.c | 54 ++++++++++++++ n/avr/modules/utils/utils.host.h | 60 ++++++++++++++++ n/avr/modules/utils/utils.txt | 6 ++ 14 files changed, 589 insertions(+) create mode 100644 n/avr/modules/utils/Makefile create mode 100644 n/avr/modules/utils/Makefile.module create mode 100644 n/avr/modules/utils/README create mode 100644 n/avr/modules/utils/avrconfig.h create mode 100644 n/avr/modules/utils/byte.h create mode 100644 n/avr/modules/utils/test/Makefile create mode 100644 n/avr/modules/utils/test/avrconfig.h create mode 100644 n/avr/modules/utils/test/test_byte.c create mode 100644 n/avr/modules/utils/test/test_utils.c create mode 100644 n/avr/modules/utils/utils.avr.h create mode 100644 n/avr/modules/utils/utils.h create mode 100644 n/avr/modules/utils/utils.host.c create mode 100644 n/avr/modules/utils/utils.host.h create mode 100644 n/avr/modules/utils/utils.txt (limited to 'n/avr') diff --git a/n/avr/modules/utils/Makefile b/n/avr/modules/utils/Makefile new file mode 100644 index 0000000..72c9ce6 --- /dev/null +++ b/n/avr/modules/utils/Makefile @@ -0,0 +1,5 @@ +BASE = ../.. +DOC = utils.html +EXTRACTDOC = utils.h byte.h avrconfig.h + +include $(BASE)/make/Makefile.gen diff --git a/n/avr/modules/utils/Makefile.module b/n/avr/modules/utils/Makefile.module new file mode 100644 index 0000000..935499a --- /dev/null +++ b/n/avr/modules/utils/Makefile.module @@ -0,0 +1 @@ +utils_SOURCES = utils.host.c diff --git a/n/avr/modules/utils/README b/n/avr/modules/utils/README new file mode 100644 index 0000000..ac30e97 --- /dev/null +++ b/n/avr/modules/utils/README @@ -0,0 +1,25 @@ +avr.utils - Utilities AVR module. + +Utilities module for AVR. Provide services like delay or reset. See modules +README for more details about AVR modules. + + +Copyright (C) 2005 Nicolas Schodet + +Robot APB Team/Efrei 2006. + 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. diff --git a/n/avr/modules/utils/avrconfig.h b/n/avr/modules/utils/avrconfig.h new file mode 100644 index 0000000..f3ace72 --- /dev/null +++ b/n/avr/modules/utils/avrconfig.h @@ -0,0 +1,33 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h - Utilities module configuration template. */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +#endif /* avrconfig_h */ diff --git a/n/avr/modules/utils/byte.h b/n/avr/modules/utils/byte.h new file mode 100644 index 0000000..436e005 --- /dev/null +++ b/n/avr/modules/utils/byte.h @@ -0,0 +1,100 @@ +#ifndef byte_h +#define byte_h +/* byte.h */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ + +/** WARNING: Theses functions only works on little endian processor, do not + * use them on other processors, prefer the portable bit shifts and or. This + * file only exist to overide bad code production with avr-gcc in the bit + * shifts idiom. */ + +/* Union needed for byte access. */ +union _utils_byte_access +{ + uint8_t v8[4]; + uint16_t v16[2]; + uint32_t v32; +}; + +/** Byte packing macro, pack 4 bytes into a double word. */ +extern inline uint32_t +v8_to_v32 (uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) +{ + union _utils_byte_access ba; + ba.v8[0] = b0; + ba.v8[1] = b1; + ba.v8[2] = b2; + ba.v8[3] = b3; + return ba.v32; +} + +/** Byte packing macro, pack 2 bytes into a word. */ +extern inline uint16_t +v8_to_v16 (uint8_t b1, uint8_t b0) +{ + union _utils_byte_access ba; + ba.v8[0] = b0; + ba.v8[1] = b1; + return ba.v16[0]; +} + +/** Word packing macro, pack 2 words into a double word. */ +extern inline uint32_t +v16_to_v32 (uint16_t w1, uint16_t w0) +{ + union _utils_byte_access ba; + ba.v16[0] = w0; + ba.v16[1] = w1; + return ba.v32; +} + +/** Byte unpacking macro, extract one specified byte from a double word. */ +extern inline uint8_t +v32_to_v8 (uint32_t d, int pos) +{ + union _utils_byte_access ba; + ba.v32 = d; + return ba.v8[pos]; +} + +/** Byte unpacking macro, extract one specified byte from a word. */ +extern inline uint8_t +v16_to_v8 (uint16_t w, int pos) +{ + union _utils_byte_access ba; + ba.v16[0] = w; + return ba.v8[pos]; +} + +/** Word unpacking macro, extract one specified word from a double word. */ +extern inline uint16_t +v32_to_v16 (uint32_t d, int pos) +{ + union _utils_byte_access ba; + ba.v32 = d; + return ba.v16[pos]; +} + +#endif /* byte_h */ diff --git a/n/avr/modules/utils/test/Makefile b/n/avr/modules/utils/test/Makefile new file mode 100644 index 0000000..c7d685e --- /dev/null +++ b/n/avr/modules/utils/test/Makefile @@ -0,0 +1,18 @@ +BASE = ../../.. +PROGS = test_utils test_byte +test_utils_SOURCES = test_utils.c +test_byte_SOURCES = test_byte.c +DOC = +EXTRACTDOC = +MODULES = utils +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega128 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +DEFS = +LIBS = + +include $(BASE)/make/Makefile.gen diff --git a/n/avr/modules/utils/test/avrconfig.h b/n/avr/modules/utils/test/avrconfig.h new file mode 100644 index 0000000..f1b0e6f --- /dev/null +++ b/n/avr/modules/utils/test/avrconfig.h @@ -0,0 +1,33 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +#endif /* avrconfig_h */ diff --git a/n/avr/modules/utils/test/test_byte.c b/n/avr/modules/utils/test/test_byte.c new file mode 100644 index 0000000..8f4770b --- /dev/null +++ b/n/avr/modules/utils/test/test_byte.c @@ -0,0 +1,39 @@ +/* test_byte.c */ +/* n.avr.utils - AVR utilities module. {{{ + * + * 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. + * + * }}} */ +#include "common.h" +#include "modules/utils/byte.h" + +/* +AutoDec */ +/* -AutoDec */ + +int +main (void) +{ + volatile uint32_t d; + volatile uint8_t b0 = 1, b1 = 2, b2 = 3, b3 = 4; + d = v8_to_v32 (b3, b2, b1, b0); + b2 = v32_to_v8 (d, 2); + return 0; +} diff --git a/n/avr/modules/utils/test/test_utils.c b/n/avr/modules/utils/test/test_utils.c new file mode 100644 index 0000000..5d5408b --- /dev/null +++ b/n/avr/modules/utils/test/test_utils.c @@ -0,0 +1,49 @@ +/* test_utils.c */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ +#include "modules/utils/utils.h" +#include "io.h" + +int +main (void) +{ +#ifndef HOST + /* Test regv, the first one should generate a warning. */ + TCCR0 = regv (FOC0, WGM00, COM01, COM0, WGM01, CS02, CS00, CS01, + 0, 0, 0, 0, 0, 1, 1, 1); + TCCR0 = regv (FOC0, WGM00, COM01, COM0, WGM01, CS02, CS01, CS00, + 0, 0, 0, 0, 0, 1, 1, 1); +#else + printf ("TODO: reset message\n"); +#endif + /* Test delays, use sei to separate loops in assembly listing. */ + sei (); utils_delay_ns (1); + sei (); utils_delay_ns (1000); + sei (); utils_delay_ns (1000000); + sei (); utils_delay (1); + sei (); utils_delay (4); + sei (); + /* Same player, play again. */ + utils_reset (); +} diff --git a/n/avr/modules/utils/utils.avr.h b/n/avr/modules/utils/utils.avr.h new file mode 100644 index 0000000..b9703dd --- /dev/null +++ b/n/avr/modules/utils/utils.avr.h @@ -0,0 +1,132 @@ +#ifndef utils_avr_h +#define utils_avr_h +/* utils.avr.h */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ + +#include + +/** Helper macro to build register value. Call it like this : + * + * TCCR0 = regv (FOC0, WGM00, COM01, COM0, WGM01, CS02, CS01, CS00, + * 0, 0, 0, 0, 0, 1, 1, 1); + * + * The macro check that the bits are given in the right order and compute the + * register value. If the bits are false, the compiler will generate an + * warning about a constant being to large. + */ +#define regv(b7, b6, b5, b4, b3, b2, b1, b0, v7, v6, v5, v4, v3, v2, v1, v0) \ + ((b7) == 7 && (b6) == 6 && (b5) == 5 && (b4) == 4 \ + && (b3) == 3 && (b2) == 2 && (b1) == 1 && (b0) == 0 \ + ? (v7) << 7 | (v6) << 6 | (v5) << 5 | (v4) << 4 \ + | (v3) << 3 | (v2) << 2 | (v1) << 1 | (v0) << 0 \ + : -1024 * 1024) + +/** Delay in seconds. Do not call this function with a variable parameter. + * If you want a variable delay, prefer looping over a fixed delay. + * Maximum is about 4 seconds at 20MHz. */ +extern inline void +utils_delay (double s) __attribute__ ((always_inline)); + +extern inline void +utils_delay (double s) +{ + double cycles = (double) AC_FREQ * s; + if (cycles <= 1.0) + { + /* Delay shorter than 1 instruction. */ + asm volatile ("nop" : :); + } + else if (cycles <= 2.0) + { + /* Delay shorter than 2 instruction. */ + asm volatile ("nop\n\tnop" : :); + } + else if (cycles <= 3.0) + { + /* Delay shorter than 3 instruction. */ + asm volatile ("nop\n\tnop\n\tnop" : :); + } + else if (cycles <= 255 * 3) + { + uint8_t i = cycles; + asm volatile ("1: dec %0\n\tbrne 1b" : "=r" (i) : "0" (i)); + } + else if (cycles <= 65535 * 4) + { + uint16_t i = cycles; + asm volatile ("1: sbiw %0,1\n\tbrne 1b" : "=w" (i) : "0" (i)); + } + else + { + uint8_t j = (cycles + (65535 * 4 - 1)) / (65535 * 4); + uint16_t iv = cycles / (j * 4); + while (j--) + { + uint16_t i = iv; + asm volatile ("1: sbiw %0,1\n\tbrne 1b" : "=w" (i) : "0" (i)); + } + } +} + +/** Shortcut for delay (ns * 1e-9). */ +extern inline void +utils_delay_ns (double ns) __attribute__ ((always_inline)); +extern inline void +utils_delay_ns (double ns) +{ + utils_delay (ns * 1e-9); +} + +/** Shortcut for delay (us * 1e-6). */ +extern inline void +utils_delay_us (double us) __attribute__ ((always_inline)); +extern inline void +utils_delay_us (double us) +{ + utils_delay (us * 1e-6); +} + +/** Shortcut for delay (ms * 1e-3). */ +extern inline void +utils_delay_ms (double ms) __attribute__ ((always_inline)); +extern inline void +utils_delay_ms (double ms) +{ + utils_delay (ms * 1e-3); +} + +extern inline void +utils_reset (void) __attribute__ ((noreturn)); + +/** Reset the avr using the watchdog. */ +extern inline void +utils_reset (void) +{ + wdt_enable (WDTO_15MS); + while (1) + ; +} + +#endif /* utils_avr_h */ diff --git a/n/avr/modules/utils/utils.h b/n/avr/modules/utils/utils.h new file mode 100644 index 0000000..71731b3 --- /dev/null +++ b/n/avr/modules/utils/utils.h @@ -0,0 +1,34 @@ +#ifndef utils_h +#define utils_h +/* utils.h */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ + +#ifndef HOST +# include "utils.avr.h" +#else +# include "utils.host.h" +#endif + +#endif /* utils_h */ diff --git a/n/avr/modules/utils/utils.host.c b/n/avr/modules/utils/utils.host.c new file mode 100644 index 0000000..1173eee --- /dev/null +++ b/n/avr/modules/utils/utils.host.c @@ -0,0 +1,54 @@ +/* utils.host.c */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ +#define _GNU_SOURCE +#include "utils.h" + +#include +#include +#include +#include +#include + +/** Delay in seconds. */ +void +utils_delay (double s) +{ + struct timespec ts; + assert (s > 0.0); + ts.tv_sec = (long int) s; + ts.tv_nsec = (s - (long int) s) * 1000000000l; + while (nanosleep (&ts, &ts) == -1 && errno == EINTR) + ; +} + +/** Reset. */ +void +utils_reset (void) +{ + execlp (program_invocation_name, program_invocation_name, 0); + assert_perror (errno); + abort (); +} + diff --git a/n/avr/modules/utils/utils.host.h b/n/avr/modules/utils/utils.host.h new file mode 100644 index 0000000..44ab62c --- /dev/null +++ b/n/avr/modules/utils/utils.host.h @@ -0,0 +1,60 @@ +#ifndef utils_host_h +#define utils_host_h +/* utils.host.h */ +/* avr.utils - Utilities AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * 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. + * + * }}} */ + +/** Delay in seconds. */ +void +utils_delay (double s); + +/** Shortcut for delay (ns * 1e-9). */ +extern inline void +utils_delay_ns (double ns) +{ + utils_delay (ns * 1e-9); +} + +/** Shortcut for delay (us * 1e-6). */ +extern inline void +utils_delay_us (double us) +{ + utils_delay (us * 1e-6); +} + +/** Shortcut for delay (ms * 1e-3). */ +extern inline void +utils_delay_ms (double ms) +{ + utils_delay (ms * 1e-3); +} + +void +utils_reset (void) __attribute__ ((noreturn)); + +/** Reset. */ +void +utils_reset (void); + +#endif /* utils_host_h */ diff --git a/n/avr/modules/utils/utils.txt b/n/avr/modules/utils/utils.txt new file mode 100644 index 0000000..4aa9a57 --- /dev/null +++ b/n/avr/modules/utils/utils.txt @@ -0,0 +1,6 @@ +*Title: Module AVR Utilitaires +*Author: Ni + +* Doc + +*File: utils.exd -- cgit v1.2.3