initial commit for source
authorNIIBE Yutaka <gniibe@fsij.org>
Fri, 8 Jul 2011 08:37:00 +0000 (17:37 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Fri, 8 Jul 2011 08:37:00 +0000 (17:37 +0900)
12 files changed:
boards/STBEE_MINI/board.c [new file with mode: 0644]
boards/STBEE_MINI/board.h [new file with mode: 0644]
boards/STBEE_MINI/mcuconf.h [new file with mode: 0644]
boards/common/board-common.c [new file with mode: 0644]
boards/common/mcuconf-common.h [new file with mode: 0644]
src/Makefile.in [new file with mode: 0644]
src/chconf.h [new file with mode: 0644]
src/config.h.in [new file with mode: 0644]
src/configure [new file with mode: 0755]
src/halconf.h [new file with mode: 0644]
src/main.c [new file with mode: 0644]
src/neug.ld.in [new file with mode: 0644]

diff --git a/boards/STBEE_MINI/board.c b/boards/STBEE_MINI/board.c
new file mode 100644 (file)
index 0000000..25461c4
--- /dev/null
@@ -0,0 +1,34 @@
+#include "config.h"
+#include "ch.h"
+#include "hal.h"
+
+/*
+ * Board-specific initialization code.
+ */
+void boardInit(void)
+{
+  /*
+   * Disable JTAG and SWD, done after hwinit1_common as HAL resets AFIO
+   */
+  AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_DISABLE;
+  /* We use LED2 as optional "error" indicator */
+  palSetPad (IOPORT1, GPIOA_LED2);
+}
+
+void
+USB_Cable_Config (FunctionalState NewState)
+{
+  if (NewState != DISABLE)
+    palSetPad (IOPORT1, GPIOA_USB_ENABLE);
+  else
+    palClearPad (IOPORT1, GPIOA_USB_ENABLE);
+}
+
+void
+set_led (int value)
+{
+  if (value)
+    palClearPad (IOPORT1, GPIOA_LED1);
+  else
+    palSetPad (IOPORT1, GPIOA_LED1);
+}
diff --git a/boards/STBEE_MINI/board.h b/boards/STBEE_MINI/board.h
new file mode 100644 (file)
index 0000000..39f379d
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+    This file is part of ChibiOS/RT.
+
+    ChibiOS/RT 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 3 of the License, or
+    (at your option) any later version.
+
+    ChibiOS/RT 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, see <http://www.gnu.org/licenses/>.
+
+                                      ---
+
+    A special exception to the GPL can be applied should you wish to distribute
+    a combined work that includes ChibiOS/RT, without being obliged to provide
+    the source code for any proprietary components. See the file exception.txt
+    for full details of how and when the exception can be applied.
+*/
+
+#ifndef _BOARD_H_
+#define _BOARD_H_
+
+#include "config.h"
+/*
+ * Setup for the STBee Mini board.
+ */
+
+/*
+ * Board identifier.
+ */
+#define BOARD_STBEE_MINI
+#define BOARD_NAME "STBee Mini"
+
+#if defined(PINPAD_SUPPORT)
+#define HAVE_7SEGLED   1
+#endif
+
+/*
+ * Board frequencies.
+ */
+#define STM32_LSECLK            32768
+#define STM32_HSECLK            12000000
+
+/*
+ * MCU type, this macro is used by both the ST library and the ChibiOS/RT
+ * native STM32 HAL.
+ */
+#define STM32F10X_MD
+
+/*
+ * IO pins assignments.
+ */
+#define GPIOA_LED1              13
+#define GPIOA_USB_ENABLE        14
+#define GPIOA_LED2              15
+
+#define GPIOC_BUTTON            13
+
+/*
+ * I/O ports initial setup, this configuration is established soon after reset
+ * in the initialization code.
+ *
+ * The digits have the following meaning:
+ *   0 - Analog input.
+ *   1 - Push Pull output 10MHz.
+ *   2 - Push Pull output 2MHz.
+ *   3 - Push Pull output 50MHz.
+ *   4 - Digital input.
+ *   5 - Open Drain output 10MHz.
+ *   6 - Open Drain output 2MHz.
+ *   7 - Open Drain output 50MHz.
+ *   8 - Digital input with PullUp or PullDown resistor depending on ODR.
+ *   9 - Alternate Push Pull output 10MHz.
+ *   A - Alternate Push Pull output 2MHz.
+ *   B - Alternate Push Pull output 50MHz.
+ *   C - Reserved.
+ *   D - Alternate Open Drain output 10MHz.
+ *   E - Alternate Open Drain output 2MHz.
+ *   F - Alternate Open Drain output 50MHz.
+ * Please refer to the STM32 Reference Manual for details.
+ */
+
+/*
+ * Port A setup.
+ * PA11 - input with pull-up (USBDM)
+ * PA12 - input with pull-up (USBDP)
+ * Everything input with pull-up except:
+ * PA13 - Open Drain output (LED1 0:ON 1:OFF)
+ * PA14 - Push pull output  (USB ENABLE 0:DISABLE 1:ENABLE)
+ * PA15 - Open Drain output (LED2 0:ON 1:OFF)
+ */
+#define VAL_GPIOACRL            0x88888888      /*  PA7...PA0 */
+#define VAL_GPIOACRH            0x63688888      /* PA15...PA8 */
+#define VAL_GPIOAODR            0xFFFFFFFF
+
+/* Port B setup. */
+/* Everything input with pull-up */
+#define VAL_GPIOBCRL            0x88888888      /*  PB7...PB0 */
+#define VAL_GPIOBCRH            0x88888888      /* PB15...PB8 */
+#define VAL_GPIOBODR            0xFFFFFFFF
+
+/*
+ * Port C setup.
+ * Everything input with pull-up except:
+ * PC13 - Normal input.
+ * PC14 - Normal input.
+ * PC15 - Normal input.
+ */
+#define VAL_GPIOCCRL            0x88888888      /*  PC7...PC0 */
+#define VAL_GPIOCCRH            0x44488888      /* PC15...PC8 */
+#define VAL_GPIOCODR            0xFFFFFFFF
+
+/*
+ * Port D setup.
+ * Everything input with pull-up except:
+ * PD0  - Normal input (XTAL).
+ * PD1  - Normal input (XTAL).
+ */
+#define VAL_GPIODCRL            0x88888844      /*  PD7...PD0 */
+#define VAL_GPIODCRH            0x88888888      /* PD15...PD8 */
+#define VAL_GPIODODR            0xFFFFFFFF
+
+/*
+ * Port E setup.
+ * Everything input with pull-up except:
+ */
+#define VAL_GPIOECRL            0x88888888      /*  PE7...PE0 */
+#define VAL_GPIOECRH            0x88888888      /* PE15...PE8 */
+#define VAL_GPIOEODR            0xFFFFFFFF
+
+#if !defined(_FROM_ASM_)
+#ifdef __cplusplus
+extern "C" {
+#endif
+  void boardInit(void);
+#ifdef __cplusplus
+}
+#endif
+#endif /* _FROM_ASM_ */
+
+#endif /* _BOARD_H_ */
diff --git a/boards/STBEE_MINI/mcuconf.h b/boards/STBEE_MINI/mcuconf.h
new file mode 100644 (file)
index 0000000..fdcd62c
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ * HAL driver system settings.
+ */
+#define STM32_SW                    STM32_SW_PLL
+#define STM32_PLLSRC                STM32_PLLSRC_HSE
+#define STM32_PLLXTPRE              STM32_PLLXTPRE_DIV1
+#define STM32_PLLMUL_VALUE          6
+#define STM32_HPRE                  STM32_HPRE_DIV1
+#define STM32_PPRE1                 STM32_PPRE1_DIV2
+#define STM32_PPRE2                 STM32_PPRE2_DIV2
+#define STM32_ADCPRE                STM32_ADCPRE_DIV4
+#define STM32_USBPRE                STM32_USBPRE_DIV1P5
+#define STM32_MCO                   STM32_MCO_NOCLOCK
diff --git a/boards/common/board-common.c b/boards/common/board-common.c
new file mode 100644 (file)
index 0000000..ed98441
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+    This file is part of ChibiOS/RT.
+
+    ChibiOS/RT 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 3 of the License, or
+    (at your option) any later version.
+
+    ChibiOS/RT 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, see <http://www.gnu.org/licenses/>.
+
+                                      ---
+
+    A special exception to the GPL can be applied should you wish to distribute
+    a combined work that includes ChibiOS/RT, without being obliged to provide
+    the source code for any proprietary components. See the file exception.txt
+    for full details of how and when the exception can be applied.
+*/
+
+#include "ch.h"
+#include "hal.h"
+
+/**
+ * @brief   PAL setup.
+ * @details Digital I/O ports static configuration as defined in @p board.h.
+ *          This variable is used by the HAL when initializing the PAL driver.
+ */
+#if HAL_USE_PAL || defined(__DOXYGEN__)
+const PALConfig pal_default_config =
+{
+  {VAL_GPIOAODR, VAL_GPIOACRL, VAL_GPIOACRH},
+  {VAL_GPIOBODR, VAL_GPIOBCRL, VAL_GPIOBCRH},
+  {VAL_GPIOCODR, VAL_GPIOCCRL, VAL_GPIOCCRH},
+  {VAL_GPIODODR, VAL_GPIODCRL, VAL_GPIODCRH},
+  {VAL_GPIOEODR, VAL_GPIOECRL, VAL_GPIOECRH},
+};
+#endif
+
+/*
+ * Early initialization code.
+ * This initialization must be performed just after stack setup and before
+ * any other initialization.
+ */
+void
+__early_init(void)
+{
+#ifdef DFU_SUPPORT
+  SCB->VTOR = 0x08003000;
+#endif
+
+  stm32_clock_init();
+}
+
+const uint8_t *
+unique_device_id (void)
+{
+  /* STM32F103 has 96-bit unique device identifier */
+  const uint8_t *addr = (const uint8_t *)0x1ffff7e8;
+
+  return addr;
+}
diff --git a/boards/common/mcuconf-common.h b/boards/common/mcuconf-common.h
new file mode 100644 (file)
index 0000000..cbb2001
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+    This file is part of ChibiOS/RT.
+
+    ChibiOS/RT 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 3 of the License, or
+    (at your option) any later version.
+
+    ChibiOS/RT 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, see <http://www.gnu.org/licenses/>.
+
+                                      ---
+
+    A special exception to the GPL can be applied should you wish to distribute
+    a combined work that includes ChibiOS/RT, without being obliged to provide
+    the source code for any proprietary components. See the file exception.txt
+    for full details of how and when the exception can be applied.
+*/
+
+/*
+ * STM32 drivers configuration.
+ * The following settings override the default settings present in
+ * the various device driver implementation headers.
+ * Note that the settings for each driver only have effect if the driver
+ * is enabled in halconf.h.
+ *
+ * IRQ priorities:
+ * 15...0       Lowest...Highest.
+ *
+ * DMA priorities:
+ * 0...3        Lowest...Highest.
+ */
+
+/*
+ * ADC driver system settings.
+ */
+#define USE_STM32_ADC1              TRUE
+#define STM32_ADC1_DMA_PRIORITY     3
+#define STM32_ADC1_IRQ_PRIORITY     5
+#define STM32_ADC1_DMA_ERROR_HOOK() chSysHalt()
+
+/*
+ * CAN driver system settings.
+ */
+#define USE_STM32_CAN1              FALSE
+#define STM32_CAN1_IRQ_PRIORITY     11
+
+/*
+ * PWM driver system settings.
+ */
+#define USE_STM32_PWM1              FALSE
+#define USE_STM32_PWM2              FALSE
+#define USE_STM32_PWM3              FALSE
+#define USE_STM32_PWM4              FALSE
+#define STM32_PWM1_IRQ_PRIORITY     7
+#define STM32_PWM2_IRQ_PRIORITY     7
+#define STM32_PWM3_IRQ_PRIORITY     7
+#define STM32_PWM4_IRQ_PRIORITY     7
+
+/*
+ * SERIAL driver system settings.
+ */
+#define USE_STM32_USART1            FALSE
+#define USE_STM32_USART2            FALSE
+#define USE_STM32_USART3            FALSE
+#if defined(STM32F10X_HD) || defined(STM32F10X_CL)
+#define USE_STM32_UART4             FALSE
+#define USE_STM32_UART5             FALSE
+#endif
+#define STM32_USART1_PRIORITY       12
+#define STM32_USART2_PRIORITY       12
+#define STM32_USART3_PRIORITY       12
+#if defined(STM32F10X_HD) || defined(STM32F10X_CL)
+#define STM32_UART4_PRIORITY        12
+#define STM32_UART5_PRIORITY        12
+#endif
+
+/*
+ * SPI driver system settings.
+ */
+#define USE_STM32_SPI1              FALSE
+#define USE_STM32_SPI2              FALSE
+#define STM32_SPI1_DMA_PRIORITY     2
+#define STM32_SPI2_DMA_PRIORITY     2
+#define STM32_SPI1_IRQ_PRIORITY     10
+#define STM32_SPI2_IRQ_PRIORITY     10
+#define STM32_SPI1_DMA_ERROR_HOOK() chSysHalt()
+
+/*
+ * USB driver system settings.
+ */
+#define STM32_USB_USE_USB1                  TRUE
+#define STM32_USB_LOW_POWER_ON_SUSPEND      FALSE
+#define STM32_USB_USB1_HP_IRQ_PRIORITY      6
+#define STM32_USB_USB1_LP_IRQ_PRIORITY      14
diff --git a/src/Makefile.in b/src/Makefile.in
new file mode 100644 (file)
index 0000000..dda0dba
--- /dev/null
@@ -0,0 +1,185 @@
+# Makefile for NeuG
+
+BOARD_DIR=@BOARD_DIR@
+
+##############################################################################
+# Build global options
+# NOTE: Can be overridden externally.
+#
+
+# Compiler options here.
+ifeq ($(USE_OPT),)
+  USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16
+endif
+
+# C++ specific options here (added to USE_OPT).
+ifeq ($(USE_CPPOPT),)
+  USE_CPPOPT = -fno-rtti
+endif
+
+# Enable this if you want the linker to remove unused code and data
+ifeq ($(USE_LINK_GC),)
+  USE_LINK_GC = yes
+endif
+
+# If enabled, this option allows to compile the application in THUMB mode.
+ifeq ($(USE_THUMB),)
+  USE_THUMB = yes
+endif
+
+# Enable register caching optimization (read documentation).
+ifeq ($(USE_CURRP_CACHING),)
+  USE_CURRP_CACHING = no
+endif
+
+#
+# Build global options
+##############################################################################
+
+##############################################################################
+# Architecture or project specific options
+#
+
+# Enable this if you really want to use the STM FWLib.
+ifeq ($(USE_FWLIB),)
+  USE_FWLIB = no
+endif
+
+#
+# Architecture or project specific options
+##############################################################################
+
+##############################################################################
+# Project, sources and paths
+#
+
+# Define project name here
+PROJECT = neug
+
+# Define linker script file here
+LDSCRIPT= neug.ld
+
+# Imported source files
+CHIBIOS = ../chibios
+include $(CHIBIOS)/os/hal/platforms/STM32/platform.mk
+include $(CHIBIOS)/os/hal/hal.mk
+include $(CHIBIOS)/os/ports/GCC/ARMCMx/STM32/port.mk
+include $(CHIBIOS)/os/kernel/kernel.mk
+
+# C sources that can be compiled in ARM or THUMB mode depending on the global
+# setting.
+CSRC = $(PORTSRC) \
+       $(KERNSRC) \
+       $(HALSRC) \
+       $(PLATFORMSRC) \
+       $(BOARDSRC) \
+       ../boards/common/board-common.c \
+       $(BOARD_DIR)/board.c \
+       main.c
+
+# List ASM source files here
+ASMSRC = $(PORTASM)
+
+INCDIR = $(CRYPTINCDIR) $(STMUSBINCDIR) $(VCOMDIR) \
+        $(PORTINC) $(KERNINC) $(TESTINC) \
+         $(HALINC) $(PLATFORMINC) ../boards/common $(BOARD_DIR) \
+         $(CHIBIOS)/os/various
+
+#
+# Project, sources and paths
+##############################################################################
+
+##############################################################################
+# Compiler settings
+#
+
+MCU  = cortex-m3
+
+#TRGT = arm-elf-
+TRGT = arm-none-eabi-
+CC   = $(TRGT)gcc
+CPPC = $(TRGT)g++
+# Enable loading with g++ only if you need C++ runtime support.
+# NOTE: You can use C++ even without C++ support if you are careful. C++
+#       runtime support makes code size explode.
+LD   = $(TRGT)gcc
+#LD   = $(TRGT)g++
+CP   = $(TRGT)objcopy
+AS   = $(TRGT)gcc -x assembler-with-cpp
+OD   = $(TRGT)objdump
+HEX  = $(CP) -O ihex
+BIN  = $(CP) -O binary
+
+# ARM-specific options here
+AOPT =
+
+# THUMB-specific options here
+TOPT = -mthumb -DTHUMB
+
+# Define C warning options here
+CWARN = -Wall -Wextra -Wstrict-prototypes
+
+# Define C++ warning options here
+CPPWARN = -Wall -Wextra
+
+#
+# Compiler settings
+##############################################################################
+
+##############################################################################
+# Start of default section
+#
+
+# List all default C defines here, like -D_DEBUG=1
+DDEFS =
+
+# List all default ASM defines here, like -D_DEBUG=1
+DADEFS =
+
+# List all default directories to look for include files here
+DINCDIR =
+
+# List the default directory to look for the libraries here
+DLIBDIR =
+
+# List all default libraries here
+DLIBS =
+
+#
+# End of default section
+##############################################################################
+
+##############################################################################
+# Start of user section
+#
+
+# List all user C define here, like -D_DEBUG=1
+UDEFS =
+
+# Define ASM defines here
+UADEFS =
+
+# List all user directories here
+UINCDIR =
+
+# List the user directory to look for the libraries here
+ULIBDIR =
+
+# List all user libraries here
+ULIBS =
+
+#
+# End of user defines
+##############################################################################
+
+ifeq ($(USE_FWLIB),yes)
+  include $(CHIBIOS)/ext/stm32lib/stm32lib.mk
+  CSRC += $(STM32SRC)
+  INCDIR += $(STM32INC)
+  USE_OPT += -DUSE_STDPERIPH_DRIVER
+endif
+
+include $(CHIBIOS)/os/ports/GCC/ARMCMx/rules.mk
+
+distclean: clean
+       -rm -f Makefile neug.ld config.h
diff --git a/src/chconf.h b/src/chconf.h
new file mode 100644 (file)
index 0000000..13fd71e
--- /dev/null
@@ -0,0 +1,63 @@
+/* ChibiOS/RT configuration file */
+
+#ifndef _CHCONF_H_
+#define _CHCONF_H_
+
+#define CH_FREQUENCY                    1000
+#define CH_TIME_QUANTUM                 20
+#define CH_USE_NESTED_LOCKS             FALSE
+#define CH_MEMCORE_SIZE                 0 /* Whole RAM */
+#define CH_NO_IDLE_THREAD               FALSE
+#define CH_OPTIMIZE_SPEED               TRUE
+#define CH_USE_REGISTRY                 TRUE
+#define CH_USE_WAITEXIT                 TRUE
+#define CH_USE_SEMAPHORES               TRUE
+#define CH_USE_SEMAPHORES_PRIORITY      FALSE
+#define CH_USE_SEMSW                    FALSE
+#define CH_USE_MUTEXES                  TRUE
+#define CH_USE_CONDVARS                 TRUE
+#define CH_USE_CONDVARS_TIMEOUT         TRUE
+#define CH_USE_EVENTS                   TRUE /* We use this! */
+#define CH_USE_EVENTS_TIMEOUT           TRUE /* We use this! */
+#define CH_USE_MESSAGES                 FALSE
+#define CH_USE_MESSAGES_PRIORITY        FALSE
+#define CH_USE_MAILBOXES                FALSE
+#define CH_USE_QUEUES                   FALSE
+#define CH_USE_MEMCORE                  TRUE
+#define CH_USE_HEAP                     TRUE
+#define CH_USE_MALLOC_HEAP              FALSE
+#define CH_USE_MEMPOOLS                 FALSE
+#define CH_USE_DYNAMIC                  TRUE
+
+/* Debug options */
+#define CH_DBG_ENABLE_CHECKS            FALSE
+#define CH_DBG_ENABLE_ASSERTS           FALSE
+#define CH_DBG_ENABLE_TRACE             FALSE
+#define CH_DBG_ENABLE_STACK_CHECK       TRUE
+#define CH_DBG_FILL_THREADS             FALSE
+#define CH_DBG_THREADS_PROFILING        FALSE
+
+#define THREAD_EXT_FIELDS                                               \
+  /* Add threads custom fields here.*/                                  \
+
+#define THREAD_EXT_INIT(tp) {                                           \
+  /* Add threads initialization code here.*/                            \
+}
+
+#define THREAD_EXT_EXIT(tp) {                                           \
+  /* Add threads finalization code here.*/                              \
+}
+
+#define IDLE_LOOP_HOOK() {                                              \
+  /* Idle loop code here.*/                                             \
+}
+
+#define SYSTEM_TICK_EVENT_HOOK() {                                          \
+  /* System tick event code here.*/                                         \
+}
+
+#define SYSTEM_HALT_HOOK() {                                                \
+  /* System halt code here.*/                                               \
+}
+
+#endif  /* _CHCONF_H_ */
diff --git a/src/config.h.in b/src/config.h.in
new file mode 100644 (file)
index 0000000..0f15d78
--- /dev/null
@@ -0,0 +1 @@
+@DFU_DEFINE@
diff --git a/src/configure b/src/configure
new file mode 100755 (executable)
index 0000000..f067809
--- /dev/null
@@ -0,0 +1,132 @@
+#! /bin/bash
+
+#
+# This file is *NOT* generated by GNU Autoconf, but written by NIIBE Yutaka
+#
+# Copyright (C) 2010, 2011 Free Software Initiative of Japan
+#
+# This file is a part of NeuG, a Random Number Generator implementation.
+#
+# NeuG 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 3 of the License, or
+# (at your option) any later version.
+#
+# NeuG 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, see <http://www.gnu.org/licenses/>.
+
+# Default settings
+help=no
+target=STBEE_MINI
+verbose=no
+with_dfu=default
+debug=no
+
+# Process each option
+for option; do
+  case $option in
+  *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
+  *)   optarg=yes ;;
+  esac
+
+  case $option in
+  -h | --help)
+    help=yes ;;
+  --target=*)
+    target=$optarg ;;
+  -v | --verbose)
+    verbose=yes ;;
+  --with-dfu)
+    with_dfu=yes ;;
+  --without-dfu)
+    with_dfu=no ;;
+  *)
+    echo "Unrecognized option \`$option'" >&2
+    echo "Try \`$0 --help' for more information." >&2
+    exit 1
+    ;;
+  esac
+done
+
+if test "$help" = "yes"; then
+  cat <<EOF
+Usage: $0 [OPTION]...
+
+Defaults for the options are specified in brackets.
+
+Configuration:
+  -h, --help           display this help and exit      [no]
+  --target=TARGET      specify target                  [OLIMEX_STM32_H103]
+                       supported targes are:
+                          OLIMEX_STM32_H103
+                          STM32_PRIMER2
+                          CQ_STARM
+                          STBEE_MINI
+                          STM8S_DISCOVERY
+                          STBEE
+  --with-dfu           build image for DFU             [<target specific>]
+EOF
+  exit 0
+fi
+
+BOARD_DIR=../boards/$target
+if test -d $BOARD_DIR; then
+  echo "Configured for target: $target"
+else
+  echo "Unsupported target \`$target'" >&2
+  exit 1
+fi
+
+# Flash page size in byte
+FLASH_PAGE_SIZE=1024
+# Flash memory size in KB
+FLASH_SIZE=128
+
+# Settings for TARGET
+case $target in
+CQ_STARM|STBEE_MINI)
+  if test "$with_dfu" = "default"; then
+    with_dfu=yes;
+  fi  ;;
+STM32_PRIMER2)
+  FLASH_PAGE_SIZE=2048
+  FLASH_SIZE=512
+  ;;
+STBEE)
+  FLASH_PAGE_SIZE=2048
+  FLASH_SIZE=512
+  if test "$with_dfu" = "default"; then
+    with_dfu=yes;
+  fi  ;;
+STM8S_DISCOVERY)
+  FLASH_SIZE=64
+  ;;
+*)
+  ;;
+esac
+
+# --with-dfu option
+if test "$with_dfu" = "yes"; then
+  echo "Configured for DFU"
+  ORIGIN=0x08003000
+  FLASH_SIZE=`expr $FLASH_SIZE - 12`
+  DFU_DEFINE="#define DFU_SUPPORT 1"
+else
+  echo "Configured for bare system (no-DFU)"
+  ORIGIN=0x08000000
+  DFU_DEFINE="#undef DFU_SUPPORT"
+fi
+
+sed -e "s%@BOARD_DIR@%$BOARD_DIR%" \
+       < Makefile.in > Makefile
+sed -e "s/@ORIGIN@/$ORIGIN/" -e "s/@FLASH_SIZE@/$FLASH_SIZE/" \
+    -e "s/@FLASH_PAGE_SIZE@/$FLASH_PAGE_SIZE/" \
+       < neug.ld.in > neug.ld
+sed -e "s/@DFU_DEFINE@/$DFU_DEFINE/" \
+       < config.h.in > config.h
+exit 0
diff --git a/src/halconf.h b/src/halconf.h
new file mode 100644 (file)
index 0000000..a5d9d45
--- /dev/null
@@ -0,0 +1,31 @@
+/* HAL configuration file for ChibiOS/RT */
+
+#ifndef _HALCONF_H_
+#define _HALCONF_H_
+
+#include "mcuconf.h"
+
+#define HAL_USE_PAL              TRUE
+#define HAL_USE_ADC              TRUE
+#define HAL_USE_CAN              FALSE
+#define HAL_USE_GPT              FALSE
+#define HAL_USE_I2C              FALSE
+#define HAL_USE_ICU              FALSE
+#define HAL_USE_MAC              FALSE
+#define HAL_USE_MMC_SPI          FALSE
+#define HAL_USE_PWM              FALSE
+#define HAL_USE_SDC              FALSE
+#define HAL_USE_SERIAL           FALSE
+#define HAL_USE_SERIAL_USB       TRUE
+#define HAL_USE_SPI              FALSE
+#define HAL_USE_UART             FALSE
+#define HAL_USE_USB              TRUE
+
+#define ADC_USE_WAIT             FALSE
+#define ADC_USE_MUTUAL_EXCLUSION FALSE
+
+#define SERIAL_BUFFERS_SIZE         16
+
+
+
+#endif /* _HALCONF_H_ */
diff --git a/src/main.c b/src/main.c
new file mode 100644 (file)
index 0000000..ed38507
--- /dev/null
@@ -0,0 +1,692 @@
+/*
+ * main.c - main routine of neug
+ *
+ * Copyright (C) 2011 Free Software Initiative of Japan
+ * Author: NIIBE Yutaka <gniibe@fsij.org>
+ *
+ * This file is a part of NeuG, a Random Number Generator
+ * implementation.
+ *
+ * NeuG 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnuk 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "config.h"
+
+#include "ch.h"
+#include "hal.h"
+#include "usb_cdc.h"
+
+#include "hw_config.h"
+
+/*
+ * USB Driver structure.
+ */
+static SerialUSBDriver SDU1;
+
+/*
+ * USB Device Descriptor.
+ */
+static const uint8_t vcom_device_descriptor_data[18] = {
+  USB_DESC_DEVICE       (0x0200,        /* bcdUSB (2.0).                    */
+                         0x02,          /* bDeviceClass (CDC).              */
+                         0x00,          /* bDeviceSubClass.                 */
+                         0x00,          /* bDeviceProtocol.                 */
+                         0x40,          /* bMaxPacketSize.                  */
+                         0x234b,        /* idVendor (FSIJ).                   */
+                         0x0001,        /* idProduct.                       */
+                         0x0100,        /* bcdDevice.                       */
+                         1,             /* iManufacturer.                   */
+                         2,             /* iProduct.                        */
+                         3,             /* iSerialNumber.                   */
+                         1)             /* bNumConfigurations.              */
+};
+
+/*
+ * Device Descriptor wrapper.
+ */
+static const USBDescriptor vcom_device_descriptor = {
+  sizeof vcom_device_descriptor_data,
+  vcom_device_descriptor_data
+};
+
+/* Configuration Descriptor tree for a CDC.*/
+static const uint8_t vcom_configuration_descriptor_data[67] = {
+  /* Configuration Descriptor.*/
+  USB_DESC_CONFIGURATION(67,            /* wTotalLength.                    */
+                         0x02,          /* bNumInterfaces.                  */
+                         0x01,          /* bConfigurationValue.             */
+                         0,             /* iConfiguration.                  */
+                         0x80,          /* bmAttributes (bus powered).      */
+                         50),           /* bMaxPower (100mA).               */
+  /* Interface Descriptor.*/
+  USB_DESC_INTERFACE    (0x00,          /* bInterfaceNumber.                */
+                         0x00,          /* bAlternateSetting.               */
+                         0x01,          /* bNumEndpoints.                   */
+                         0x02,          /* bInterfaceClass (Communications
+                                           Interface Class, CDC section
+                                           4.2).                            */
+                         0x02,          /* bInterfaceSubClass (Abstract
+                                         Control Model, CDC section 4.3).   */
+                         0x01,          /* bInterfaceProtocol (AT commands,
+                                           CDC section 4.4).                */
+                         0),            /* iInterface.                      */
+  /* Header Functional Descriptor (CDC section 5.2.3).*/
+  USB_DESC_BYTE         (5),            /* bLength.                         */
+  USB_DESC_BYTE         (0x24),         /* bDescriptorType (CS_INTERFACE).  */
+  USB_DESC_BYTE         (0x00),         /* bDescriptorSubtype (Header
+                                           Functional Descriptor.           */
+  USB_DESC_BCD          (0x0110),       /* bcdCDC.                          */
+  /* Call Management Functional Descriptor. */
+  USB_DESC_BYTE         (5),            /* bFunctionLength.                 */
+  USB_DESC_BYTE         (0x24),         /* bDescriptorType (CS_INTERFACE).  */
+  USB_DESC_BYTE         (0x01),         /* bDescriptorSubtype (Call Management
+                                           Functional Descriptor).          */
+  USB_DESC_BYTE         (0x00),         /* bmCapabilities (D0+D1).          */
+  USB_DESC_BYTE         (0x01),         /* bDataInterface.                  */
+  /* ACM Functional Descriptor.*/
+  USB_DESC_BYTE         (4),            /* bFunctionLength.                 */
+  USB_DESC_BYTE         (0x24),         /* bDescriptorType (CS_INTERFACE).  */
+  USB_DESC_BYTE         (0x02),         /* bDescriptorSubtype (Abstract
+                                           Control Management Descriptor).  */
+  USB_DESC_BYTE         (0x02),         /* bmCapabilities.                  */
+  /* Union Functional Descriptor.*/
+  USB_DESC_BYTE         (5),            /* bFunctionLength.                 */
+  USB_DESC_BYTE         (0x24),         /* bDescriptorType (CS_INTERFACE).  */
+  USB_DESC_BYTE         (0x06),         /* bDescriptorSubtype (Union
+                                           Functional Descriptor).          */
+  USB_DESC_BYTE         (0x00),         /* bMasterInterface (Communication
+                                           Class Interface).                */
+  USB_DESC_BYTE         (0x01),         /* bSlaveInterface0 (Data Class
+                                           Interface).                      */
+  /* Endpoint 2 Descriptor.*/
+  USB_DESC_ENDPOINT     (INTERRUPT_REQUEST_EP|0x80, /* bEndpointAddress.    */
+                         0x03,          /* bmAttributes (Interrupt).        */
+                         0x0008,        /* wMaxPacketSize.                  */
+                         0xFF),         /* bInterval.                       */
+  /* Interface Descriptor.*/
+  USB_DESC_INTERFACE    (0x01,          /* bInterfaceNumber.                */
+                         0x00,          /* bAlternateSetting.               */
+                         0x02,          /* bNumEndpoints.                   */
+                         0x0A,          /* bInterfaceClass (Data Class
+                                           Interface, CDC section 4.5).     */
+                         0x00,          /* bInterfaceSubClass (CDC section
+                                           4.6).                            */
+                         0x00,          /* bInterfaceProtocol (CDC section
+                                           4.7).                            */
+                         0x00),         /* iInterface.                      */
+  /* Endpoint 3 Descriptor.*/
+  USB_DESC_ENDPOINT     (DATA_AVAILABLE_EP,         /* bEndpointAddress.    */
+                         0x02,          /* bmAttributes (Bulk).             */
+                         0x0040,        /* wMaxPacketSize.                  */
+                         0x00),         /* bInterval.                       */
+  /* Endpoint 1 Descriptor.*/
+  USB_DESC_ENDPOINT     (DATA_REQUEST_EP|0x80,      /* bEndpointAddress.    */
+                         0x02,          /* bmAttributes (Bulk).             */
+                         0x0040,        /* wMaxPacketSize.                  */
+                         0x00)          /* bInterval.                       */
+};
+
+
+/*
+ * Configuration Descriptor wrapper.
+ */
+static const USBDescriptor vcom_configuration_descriptor = {
+  sizeof vcom_configuration_descriptor_data,
+  vcom_configuration_descriptor_data
+};
+
+/*
+ * U.S. English language identifier.
+ */
+static const uint8_t vcom_string0[] = {
+  USB_DESC_BYTE(4),                     /* bLength.                         */
+  USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType.                 */
+  USB_DESC_WORD(0x0409)                 /* wLANGID (U.S. English).          */
+};
+
+/*
+ * Vendor string.
+ */
+static const uint8_t vcom_string1[] = {
+  USB_DESC_BYTE(68),                    /* bLength.                         */
+  USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType.                 */
+  /* Manufacturer: "Free Software Initiative of Japan" */
+  'F', 0, 'r', 0, 'e', 0, 'e', 0, ' ', 0, 'S', 0, 'o', 0, 'f', 0,
+  't', 0, 'w', 0, 'a', 0, 'r', 0, 'e', 0, ' ', 0, 'I', 0, 'n', 0,
+  'i', 0, 't', 0, 'i', 0, 'a', 0, 't', 0, 'i', 0, 'v', 0, 'e', 0,
+  ' ', 0, 'o', 0, 'f', 0, ' ', 0, 'J', 0, 'a', 0, 'p', 0, 'a', 0,
+  'n', 0
+};
+
+/*
+ * Device Description string.
+ */
+static const uint8_t vcom_string2[] = {
+  USB_DESC_BYTE(18),                    /* bLength.                         */
+  USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType.                 */
+  /* Product name: "NeuG RNG" */
+  'N', 0, 'e', 0, 'u', 0, 'G', 0, ' ', 0, 'R', 0, 'N', 0, 'G', 0,
+};
+
+/*
+ * Serial Number string.
+ */
+static const uint8_t vcom_string3[] = {
+  USB_DESC_BYTE(8),                     /* bLength.                         */
+  USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType.                 */
+  '0' , 0,  '.' , 0,  '1' , 0
+};
+
+/*
+ * Strings wrappers array.
+ */
+static const USBDescriptor vcom_strings[] = {
+  {sizeof vcom_string0, vcom_string0},
+  {sizeof vcom_string1, vcom_string1},
+  {sizeof vcom_string2, vcom_string2},
+  {sizeof vcom_string3, vcom_string3}
+};
+
+/*
+ * Handles the GET_DESCRIPTOR callback. All required descriptors must be
+ * handled here.
+ */
+static const USBDescriptor *get_descriptor(USBDriver *usbp,
+                                           uint8_t dtype,
+                                           uint8_t dindex,
+                                           uint16_t lang) {
+
+  (void)usbp;
+  (void)lang;
+  switch (dtype) {
+  case USB_DESCRIPTOR_DEVICE:
+    return &vcom_device_descriptor;
+  case USB_DESCRIPTOR_CONFIGURATION:
+    return &vcom_configuration_descriptor;
+  case USB_DESCRIPTOR_STRING:
+    if (dindex < 4)
+      return &vcom_strings[dindex];
+  }
+  return NULL;
+}
+
+/**
+ * @brief   EP1 initialization structure (IN only).
+ */
+static const USBEndpointConfig ep1config = {
+  USB_EP_MODE_TYPE_BULK | USB_EP_MODE_PACKET,
+  NULL,
+  sduDataTransmitted,
+  NULL,
+  0x0040,
+  0x0000,
+  NULL,
+  NULL
+};
+
+/**
+ * @brief   EP2 initialization structure (IN only).
+ */
+static const USBEndpointConfig ep2config = {
+  USB_EP_MODE_TYPE_INTR | USB_EP_MODE_PACKET,
+  NULL,
+  sduInterruptTransmitted,
+  NULL,
+  0x0010,
+  0x0000,
+  NULL,
+  NULL
+};
+
+/**
+ * @brief   EP3 initialization structure (OUT only).
+ */
+static const USBEndpointConfig ep3config = {
+  USB_EP_MODE_TYPE_BULK | USB_EP_MODE_PACKET,
+  NULL,
+  NULL,
+  sduDataReceived,
+  0x0000,
+  0x0040,
+  NULL,
+  NULL
+};
+
+/*
+ * Handles the USB driver global events.
+ */
+static void usb_event(USBDriver *usbp, usbevent_t event) {
+
+  switch (event) {
+  case USB_EVENT_RESET:
+    return;
+  case USB_EVENT_ADDRESS:
+    return;
+  case USB_EVENT_CONFIGURED:
+    /* Enables the endpoints specified into the configuration.
+       Note, this callback is invoked from an ISR so I-Class functions
+       must be used.*/
+    chSysLockFromIsr();
+    usbInitEndpointI(usbp, DATA_REQUEST_EP, &ep1config);
+    usbInitEndpointI(usbp, INTERRUPT_REQUEST_EP, &ep2config);
+    usbInitEndpointI(usbp, DATA_AVAILABLE_EP, &ep3config);
+    chSysUnlockFromIsr();
+    return;
+  case USB_EVENT_SUSPEND:
+    return;
+  case USB_EVENT_WAKEUP:
+    return;
+  case USB_EVENT_STALLED:
+    return;
+  }
+  return;
+}
+
+
+/*
+ * Serial over USB driver configuration.
+ */
+static const SerialUSBConfig serusbcfg = {
+  &USBD1,
+  {
+    usb_event,
+    get_descriptor,
+    sduRequestsHook,
+    NULL
+  }
+};
+\f
+struct stdout {
+  Mutex m;
+  CondVar start_cnd;
+  CondVar finish_cnd;
+  const char *str;
+  int size;
+};
+
+static struct stdout stdout;
+
+static void
+stdout_init (void)
+{
+  chMtxInit (&stdout.m);
+  chCondInit (&stdout.start_cnd);
+  chCondInit (&stdout.finish_cnd);
+  stdout.size = 0;
+  stdout.str = NULL;
+}
+
+void
+write (const char *s, int size)
+{
+  if (size == 0)
+    return;
+
+  chMtxLock (&stdout.m);
+  while (stdout.str)
+    chCondWait (&stdout.finish_cnd);
+  stdout.str = s;
+  stdout.size = size;
+  chCondSignal (&stdout.start_cnd);
+  chCondWait (&stdout.finish_cnd);
+  chMtxUnlock ();
+}
+
+Thread *stdout_thread;
+static uint32_t count_in;
+static uint8_t buffer_in[VIRTUAL_COM_PORT_DATA_SIZE];
+
+static WORKING_AREA(waSTDOUTthread, 128);
+
+static msg_t
+STDOUTthread (void *arg)
+{
+  (void)arg;
+  stdout_thread = chThdSelf ();
+
+ again:
+
+  while (1)
+    {
+      if (bDeviceState == CONFIGURED)
+       break;
+
+      chThdSleepMilliseconds (100);
+    }
+
+  while (1)
+    {
+      const char *p;
+      int len;
+
+      if (bDeviceState != CONFIGURED)
+       break;
+
+      chMtxLock (&stdout.m);
+      if (stdout.str == NULL)
+       chCondWait (&stdout.start_cnd);
+
+      p = stdout.str;
+      len = stdout.size;
+      while (len > 0)
+       {
+         int i;
+
+         if (len < VIRTUAL_COM_PORT_DATA_SIZE)
+           {
+             for (i = 0; i < len; i++)
+               buffer_in[i] = p[i];
+             count_in = len;
+             len = 0;
+           }
+         else
+           {
+             for (i = 0; i < VIRTUAL_COM_PORT_DATA_SIZE; i++)
+               buffer_in[i] = p[i];
+             len -= VIRTUAL_COM_PORT_DATA_SIZE;
+             count_in = VIRTUAL_COM_PORT_DATA_SIZE;
+             p += count_in;
+           }
+
+         chEvtClear (EV_TX_READY);
+
+         USB_SIL_Write (EP3_IN, buffer_in, count_in);
+         SetEPTxValid (ENDP3);
+
+         chEvtWaitOne (EV_TX_READY);
+       }
+
+      stdout.str = NULL;
+      stdout.size = 0;
+      chCondBroadcast (&stdout.finish_cnd);
+      chMtxUnlock ();
+    }
+
+  goto again;
+  return 0;
+}
+
+
+static WORKING_AREA(waUSBthread, 128);
+extern msg_t USBthread (void *arg);
+
+/*
+ * main thread does 1-bit LED display output
+ */
+#define LED_TIMEOUT_INTERVAL   MS2ST(100)
+#define LED_TIMEOUT_ZERO       MS2ST(50)
+#define LED_TIMEOUT_ONE                MS2ST(200)
+#define LED_TIMEOUT_STOP       MS2ST(500)
+
+
+#define ID_OFFSET 12
+static void
+device_initialize_once (void)
+{
+  const uint8_t *p = &gnukStringSerial[ID_OFFSET];
+
+  if (p[0] == 0xff && p[1] == 0xff && p[2] == 0xff && p[3] == 0xff)
+    {
+      /*
+       * This is the first time invocation.
+       * Setup serial number by unique device ID.
+       */
+      const uint8_t *u = unique_device_id ();
+      int i;
+
+      for (i = 0; i < 4; i++)
+       {
+         uint8_t b = u[i];
+         uint8_t nibble; 
+
+         nibble = (b >> 4);
+         nibble += (nibble >= 10 ? ('A' - 10) : '0');
+         flash_put_data_internal (&p[i*4], nibble);
+         nibble = (b & 0x0f);
+         nibble += (nibble >= 10 ? ('A' - 10) : '0');
+         flash_put_data_internal (&p[i*4+2], nibble);
+       }
+    }
+}
+
+
+Thread *main_thread;
+\f
+/* Total number of channels to be sampled by a single ADC operation.*/
+#define ADC_GRP1_NUM_CHANNELS   2
+/* Depth of the conversion buffer, channels are sampled one time each.*/
+#define ADC_GRP1_BUF_DEPTH      4
+/*
+ * ADC samples buffer.
+ */
+static adcsample_t samp[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
+#define ADC_SAMPLE_1P5          0   /**< @brief 1.5 cycles sampling time.   */
+#define ADC_SAMPLE_13P5         2   /**< @brief 13.5 cycles sampling time.  */
+#define ADC_SAMPLE_239P5        7   /**< @brief 239.5 cycles sampling time. */
+
+static void adccb (adcsample_t *buffer, size_t n);
+
+/*
+ * ADC conversion group.
+ * Mode:        Linear buffer, 4 samples of 2 channels, SW triggered.
+ * Channels:    Vref   (239.5 cycles sample time)
+ *              Sensor (239.5 cycles sample time)
+ */
+static const ADCConversionGroup adcgrpcfg = {
+  FALSE,
+  ADC_GRP1_NUM_CHANNELS,
+  0,
+  ADC_CR2_EXTSEL_SWSTART | ADC_CR2_TSVREFE | ADC_CR2_CONT,
+  ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_1P5) | ADC_SMPR1_SMP_VREF(ADC_SAMPLE_1P5),
+  0,
+  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
+  0,
+  ADC_SQR3_SQ2_N(ADC_CHANNEL_SENSOR) | ADC_SQR3_SQ1_N(ADC_CHANNEL_VREFINT)
+};
+
+/*
+ * ADC end conversion callback.
+ */
+static void adccb (adcsample_t *buffer, size_t n)
+{
+  ADCDriver *adcp = &ADCD1;
+
+  (void) buffer; (void) n;
+
+  chSysLockFromIsr();
+  if (adcp->ad_state == ADC_COMPLETE)
+    chEvtSignalI (main_thread, (eventmask_t)1);
+  chSysUnlockFromIsr();
+}
+\f
+static volatile uint8_t fatal_code;
+
+#define TMT_MAT1 0x8f7011ee
+#define TMT_MAT2 0xfc78ff1f
+#define TMT_TMAT 0x3793fdff
+
+static uint32_t tmt[4] = { 0x56781234, TMT_MAT1, TMT_MAT2, TMT_TMAT };
+
+#define TMT_CALC_LASTWORD(y,v) (y^v)
+
+static void tmt_one_step (uint32_t v)
+{
+  uint32_t x, y;
+
+  y = tmt[3];
+  x = (tmt[0] & 0x7fffffff) ^ tmt[1] ^ tmt[2];
+  x ^= (x << 1);
+  y ^= (y >> 1) ^ x;
+  tmt[0] = tmt[1];
+  tmt[1] = tmt[2];
+  tmt[2] = x ^ (y << 10);
+  tmt[3] = TMT_CALC_LASTWORD(y, v);
+  if ((y & 1))
+    {
+      tmt[1] ^= TMT_MAT1;
+      tmt[2] ^= TMT_MAT2;
+    }
+}
+
+static uint32_t tmt_value (void)
+{
+  uint32_t t0, t1;
+
+  t0 = tmt[3];
+  t1 = tmt[0] + (tmt[2] >> 8);
+  t0 ^= t1;
+  if ((t1 & 1))
+    t0 ^= TMT_TMAT;
+  return t0;
+}
+
+/* 8 parallel CRC-16 shift registers */
+static uint8_t epool[16];      /* Big-endian */
+static uint8_t ep_count;
+
+static void ep_add (uint8_t bits)
+{
+  uint8_t v = epool[ep_count];
+
+  /* CRC-16-CCITT's Polynomial is: x^16 + x^12 + x^5 + 1 */
+  epool[ep_count] ^= bits;
+  epool[(ep_count - 5)& 0x0f] ^= v;
+  epool[(ep_count - 12)& 0x0f] ^= v;
+
+  ep_count = (ep_count + 1) & 0x0f;
+}
+
+static uint32_t ep_value (void)
+{
+  unsigned int v = (epool[ep_count] << 24)
+                   | (epool[(ep_count + 1) & 0x0f] << 16)
+                   | (epool[(ep_count + 2) & 0x0f] << 8)
+                   | epool[(ep_count + 3) & 0x0f];
+  return v;
+}
+
+
+
+/*
+ * Entry point.
+ *
+ * NOTE: the main function is already a thread in the system on entry.
+ *       See the hwinit1_common function.
+ */
+int
+main (int argc, char **argv)
+{
+  int count = 0;
+
+  (void)argc;
+  (void)argv;
+
+  halInit();
+  chSysInit();
+
+
+  /*
+   * Activates the USB driver and then the USB bus pull-up on D+.
+   */
+  sduObjectInit(&SDU1);
+  sduStart(&SDU1, &serusbcfg);
+  USB_Cable_Config (ENABLE);
+
+  main_thread = chThdSelf ();
+
+  device_initialize_once ();
+
+  adcStart (&ADCD1, NULL);
+  adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH, adccb);
+
+  stdout_init ();
+
+  /*
+   * Creates 'stdout' thread.
+   */
+  chThdCreateStatic (waSTDOUTthread, sizeof(waSTDOUTthread),
+                    NORMALPRIO, STDOUTthread, NULL);
+
+  chThdCreateStatic (waUSBthread, sizeof(waUSBthread),
+                    NORMALPRIO, USBthread, NULL);
+
+  while (1)
+    {
+      eventmask_t m;
+
+      count++;
+
+      if (bDeviceState != CONFIGURED)
+       continue;
+
+      m = chEvtWaitOne (ALL_EVENTS);
+
+      if (m == (eventmask_t)1)
+       {
+         static uint8_t round;
+         uint8_t b;
+
+         if ((round & 1))
+           b = (((samp[0] & 0x01) << 0) | ((samp[1] & 0x01) << 1)
+                | ((samp[2] & 0x01) << 2) | ((samp[3] & 0x01) << 3)
+                | ((samp[4] & 0x01) << 4) | ((samp[5] & 0x01) << 5)
+                | ((samp[6] & 0x01) << 6) | ((samp[7] & 0x01) << 7));
+         else
+           b = (((samp[0] & 0x01) << 1) | ((samp[1] & 0x01) << 0)
+                | ((samp[2] & 0x01) << 3) | ((samp[3] & 0x01) << 2)
+                | ((samp[4] & 0x01) << 5) | ((samp[5] & 0x01) << 4)
+                | ((samp[6] & 0x01) << 7) | ((samp[7] & 0x01) << 6));
+
+         adcStartConversion (&ADCD1, &adcgrpcfg, samp,
+                             ADC_GRP1_BUF_DEPTH, adccb);
+
+         ep_add (b);
+         if (++round >= 7)
+           {
+             static uint8_t r;
+             char s[32];
+             uint32_t x = ep_value ();
+
+             tmt_one_step (((SysTick->VAL) & 6) == 0);
+             x ^= tmt_value ();
+
+             memcpy (s + (r&7)*4, (const char *)&x, 4);
+             r = (r + 1) & 7;
+             if (r == 0)
+               {
+                 static uint8_t led;
+
+                 led++;
+                 set_led ((led & 0x80) == 0);
+                 _write (s, 32);
+               }
+
+             round = 0;
+           }
+       }
+    }
+
+  return 0;
+}
+
+void
+fatal (uint8_t code)
+{
+  fatal_code = code;
+  for (;;);
+}
diff --git a/src/neug.ld.in b/src/neug.ld.in
new file mode 100644 (file)
index 0000000..d211203
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+    This file is part of ChibiOS/RT.
+
+    ChibiOS/RT 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 3 of the License, or
+    (at your option) any later version.
+
+    ChibiOS/RT 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, see <http://www.gnu.org/licenses/>.
+
+                                      ---
+
+    A special exception to the GPL can be applied should you wish to distribute
+    a combined work that includes ChibiOS/RT, without being obliged to provide
+    the source code for any proprietary components. See the file exception.txt
+    for full details of how and when the exception can be applied.
+*/
+
+/*
+ * ST32F103 memory setup.
+ */
+__main_stack_size__     = 0x0400;
+__stacks_total_size__   = __main_stack_size__;
+
+MEMORY
+{
+    flash : org = @ORIGIN@, len = @FLASH_SIZE@k
+    ram : org = 0x20000000, len = 20k
+}
+
+__ram_start__           = ORIGIN(ram);
+__ram_size__            = LENGTH(ram);
+__ram_end__             = __ram_start__ + __ram_size__;
+
+SECTIONS
+{
+    . = 0;
+
+    .text : ALIGN(16) SUBALIGN(16)
+    {
+        _text = .;
+        KEEP(*(vectors))
+        *(.text)
+        *(.text.*)
+        *(.rodata)
+        *(.rodata.*)
+        *(.glue_7t)
+        *(.glue_7)
+        *(.gcc*)
+    } > flash
+
+    .ctors :
+    {
+        PROVIDE(_ctors_start_ = .);
+        KEEP(*(SORT(.ctors.*)))
+        KEEP(*(.ctors))
+        PROVIDE(_ctors_end_ = .);
+    } > flash
+
+    .dtors :
+    {
+        PROVIDE(_dtors_start_ = .);
+        KEEP(*(SORT(.dtors.*)))
+        KEEP(*(.dtors))
+        PROVIDE(_dtors_end_ = .);
+    } > flash
+
+    .ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)}
+
+    __exidx_start = .;
+    .ARM.exidx : {*(.ARM.exidx* .gnu.linkonce.armexidx.*)} > flash
+    __exidx_end = .;
+
+    .eh_frame_hdr : {*(.eh_frame_hdr)}
+
+    .eh_frame : ONLY_IF_RO {*(.eh_frame)}
+
+    . = ALIGN(4);
+    _etext = .;
+    _textdata = _etext;
+
+    .data :
+    {
+        _data = .;
+        *(.data)
+        . = ALIGN(4);
+        *(.data.*)
+        . = ALIGN(4);
+        *(.ramtext)
+        . = ALIGN(4);
+        _edata = .;
+    } > ram AT > flash
+
+    .bss :
+    {
+        _bss_start = .;
+        *(.bss)
+        . = ALIGN(4);
+        *(.bss.*)
+        . = ALIGN(4);
+        *(COMMON)
+        . = ALIGN(4);
+        _bss_end = .;
+    } > ram
+}
+
+PROVIDE(end = .);
+_end            = .;
+
+__heap_base__   = _end;
+__heap_end__    = __ram_end__ - __stacks_total_size__;