OSDN Git Service

M系列信号による乱数発生器を追加。
authorShinichiro Nakamura <shinta.main.jp@gmail.com>
Sun, 26 Aug 2012 08:37:52 +0000 (17:37 +0900)
committerShinichiro Nakamura <shinta.main.jp@gmail.com>
Sun, 26 Aug 2012 08:37:52 +0000 (17:37 +0900)
firm/bare_metal/common/effect.c
firm/bare_metal/common/effect.h
firm/bare_metal/common/tinyrand.c [new file with mode: 0644]
firm/bare_metal/common/tinyrand.h [new file with mode: 0644]
firm/bare_metal/dynamic_menu/Makefile
firm/bare_metal/karaoke_machine/Makefile
firm/bare_metal/noise_generator/Makefile [new file with mode: 0644]
firm/bare_metal/noise_generator/main.c [new file with mode: 0644]
firm/bare_metal/simple_through/Makefile

index 941f080..743a8fb 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <stdlib.h>
 #include "effect.h"
+#include "tinyrand.h"
 
 typedef struct {
     double left;
@@ -42,6 +43,7 @@ typedef struct {
 } volume_t;
 
 volume_t volume;
+tinyrand_t tinyrand;
 
 void effect_through(
         UZURA *p,
@@ -81,10 +83,33 @@ void effect_karaoke(
     }
 }
 
+void effect_noise(
+        UZURA *p,
+        const int32_t *src, int32_t *des, int32_t count)
+{
+    int32_t i;
+    uint32_t vl, vr;
+    UZURA_LEVEL_INPUT(p).left = 0;
+    UZURA_LEVEL_INPUT(p).right = 0;
+    UZURA_LEVEL_OUTPUT(p).left = 0;
+    UZURA_LEVEL_OUTPUT(p).right = 0;
+    for (i = 0; i < count; i+=2) {
+        tinyrand_pop(&tinyrand, 32, &vl);
+        tinyrand_pop(&tinyrand, 32, &vr);
+        *(des + i + 0) = vl;
+        *(des + i + 1) = vr;
+        UZURA_LEVEL_INPUT(p).left += abs(*(src + i + 0)) >> 1;
+        UZURA_LEVEL_INPUT(p).right += abs(*(src + i + 1)) >> 1;
+        UZURA_LEVEL_OUTPUT(p).left += abs(*(des + i + 0)) >> 1;
+        UZURA_LEVEL_OUTPUT(p).right += abs(*(des + i + 1)) >> 1;
+    }
+}
+
 void effect_param_init(void)
 {
     volume.left = 1.0;
     volume.right = 1.0;
+    tinyrand_init(&tinyrand, 0x512367);
 }
 
 void effect_param_volume(const double left, const double right)
index 376c15a..edbd7d3 100644 (file)
@@ -45,6 +45,9 @@ void effect_through(
 void effect_karaoke(
         UZURA *p,
         const int32_t *src, int32_t *des, int32_t count);
+void effect_noise(
+        UZURA *p,
+        const int32_t *src, int32_t *des, int32_t count);
 
 void effect_param_init(void);
 void effect_param_volume(const double left, const double right);
diff --git a/firm/bare_metal/common/tinyrand.c b/firm/bare_metal/common/tinyrand.c
new file mode 100644 (file)
index 0000000..692582c
--- /dev/null
@@ -0,0 +1,129 @@
+/**
+ * @file tinyrand.c
+ * @author Shinichiro Nakamura
+ * @brief NTMが使用する乱数生成器の実装。
+ */
+
+/*
+ * ===============================================================
+ *  Natural Tiny Monitor (NT-Monitor)
+ * ===============================================================
+ * Copyright (c) 2011-2012 Shinichiro Nakamura
+ * Inspired by M, Murakami
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ * ===============================================================
+ */
+
+#include "tinyrand.h"
+
+/**
+ * @brief ビットポジションを得る。
+ *
+ * @param N ビット。
+ *
+ * @return ビットポジション。
+ */
+#define BP(N)   (1 << ((N) - 1))
+
+/**
+ * @brief 乱数生成モジュールを初期化する。
+ * @details
+ * この乱数生成モジュールは24ビットM系列信号によって乱数を生成する。
+ *
+ * @param p 乱数生成用ハンドラ。
+ * @param seed 乱数の種。(0以外の値を指定しなければならない)
+ *
+ * @return 0 成功。
+ * @return !0 失敗。
+ */
+int tinyrand_init(tinyrand_t *p, const uint32_t seed)
+{
+    if (seed == 0) {
+        return -1;
+    }
+
+    p->seed = seed;
+    p->reg = seed;
+    return 0;
+}
+
+/**
+ * @brief 乱数生成モジュールから1ビットの乱数を取り出す。
+ * @details
+ * この関数が乱数生成モジュールの心臓部分である。
+ * 24ビットM系列信号を発生させるモジュールである。
+ *
+ * @param p 乱数生成用ハンドラ。
+ *
+ * @return 1ビットの乱数。
+ */
+static int tinyrand_update(tinyrand_t *p)
+{
+    const uint32_t mask = BP(1) | BP(3) | BP(4);
+    const uint32_t bits = BP(24);
+    if ((p->reg & bits) == bits) {
+        p->reg = ((p->reg ^ mask) << 1) | 1;
+        return 1;
+    } else {
+        p->reg = p->reg << 1;
+        return 0;
+    }
+}
+
+/**
+ * @brief 乱数を取り出す。
+ * @details
+ * このインターフェースは、指定したビット幅に収まる値を返す。
+ *
+ * @param p 乱数生成用ハンドラ。
+ * @param nbits 生成する乱数のビット幅。
+ * @param value 値格納先。
+ *
+ * @return 0 成功。
+ * @return !0 失敗。
+ */
+int tinyrand_pop(tinyrand_t *p, const uint8_t nbits, uint32_t *value)
+{
+    int i;
+
+    /*
+     * 値を初期化する。
+     */
+    *value = 0;
+
+    /*
+     * 不正なパラメータの場合、何もしない。
+     */
+    if (nbits == 0) {
+        return -1;
+    }
+
+    /*
+     * nbitsの幅を持つ値を生成する。
+     */
+    for (i = 0; i < nbits; i++) {
+        *value |= tinyrand_update(p) ? (1 << i) : 0;
+    }
+    return 0;
+}
+
diff --git a/firm/bare_metal/common/tinyrand.h b/firm/bare_metal/common/tinyrand.h
new file mode 100644 (file)
index 0000000..267b5ca
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+ * @file tinyrand.h
+ * @author Shinichiro Nakamura
+ * @brief NTMが使用する乱数生成器の定義。
+ */
+
+/*
+ * ===============================================================
+ *  Natural Tiny Monitor (NT-Monitor)
+ * ===============================================================
+ * Copyright (c) 2011-2012 Shinichiro Nakamura
+ * Inspired by M, Murakami
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ * ===============================================================
+ */
+
+#ifndef TINYRAND_H
+#define TINYRAND_H
+
+#include <stdint.h>
+
+/**
+ * @brief 乱数生成用ハンドラ。
+ */
+typedef struct {
+    uint32_t seed;
+    uint32_t reg;
+} tinyrand_t;
+
+/**
+ * @brief 乱数生成モジュールを初期化する。
+ * @details
+ * この乱数生成モジュールは24ビットM系列信号によって乱数を生成する。
+ *
+ * @param p 乱数生成用ハンドラ。
+ * @param seed 乱数の種。(0以外の値を指定しなければならない)
+ *
+ * @return 0 成功。
+ * @return !0 失敗。
+ */
+int tinyrand_init(tinyrand_t *p, const uint32_t seed);
+
+/**
+ * @brief 乱数を取り出す。
+ * @details
+ * このインターフェースは、指定したビット幅に収まる値を返す。
+ *
+ * @param p 乱数生成用ハンドラ。
+ * @param nbits 生成する乱数のビット幅。
+ * @param value 値格納先。
+ *
+ * @return 0 成功。
+ * @return !0 失敗。
+ */
+int tinyrand_pop(tinyrand_t *p, const uint8_t nbits, uint32_t *value);
+
+#endif
+
index 27845bd..1b6598c 100644 (file)
@@ -11,6 +11,7 @@ CSRC   += mmc.c pff.c
 CSRC   += twi.c uart.c
 CSRC   += ssm2603.c at24c.c
 CSRC   += bfin_util.c uzura.c
+CSRC   += tinyrand.c
 CSRC   += effect.c system.c main.c
 ASRC   +=
 VPATH   = ../common/
index 27845bd..1b6598c 100644 (file)
@@ -11,6 +11,7 @@ CSRC   += mmc.c pff.c
 CSRC   += twi.c uart.c
 CSRC   += ssm2603.c at24c.c
 CSRC   += bfin_util.c uzura.c
+CSRC   += tinyrand.c
 CSRC   += effect.c system.c main.c
 ASRC   +=
 VPATH   = ../common/
diff --git a/firm/bare_metal/noise_generator/Makefile b/firm/bare_metal/noise_generator/Makefile
new file mode 100644 (file)
index 0000000..1b6598c
--- /dev/null
@@ -0,0 +1,229 @@
+#
+# Project name (also used for output file name)
+#
+PROJECT = bluetank
+
+#
+# Source files and search directories
+#
+CSRC   += lcd.c led.c rotenc.c
+CSRC   += mmc.c pff.c
+CSRC   += twi.c uart.c
+CSRC   += ssm2603.c at24c.c
+CSRC   += bfin_util.c uzura.c
+CSRC   += tinyrand.c
+CSRC   += effect.c system.c main.c
+ASRC   +=
+VPATH   = ../common/
+
+#
+# Optimization level (0, 1, 2, 3, 4 or s)
+#
+OPTIMIZE = s
+
+#
+# C Standard level (c89, gnu89, c99 or gnu99)
+#
+CSTD = gnu89
+
+#
+# Processor type for CSRC/ASRC files.
+#
+CPU = bf592-any
+
+#
+# Linker script for the target MCU
+#
+LINKSCRIPT =
+
+#
+# Output file format (ihex, bin or both) and debugger type
+#
+OUTPUT  = ihex
+DEBUG   = dwarf-2
+
+#
+# Include dirs, library dirs and definitions
+#
+LIBS    =
+LIBDIRS =
+INCDIRS = ../common/
+DEFS    =
+ADEFS   =
+
+#
+# Warning contorls
+#
+WARNINGS = all extra
+
+#
+# Object output directory
+#
+OBJDIR = obj
+
+#
+# Programs to build porject
+#
+CC      = bfin-elf-gcc
+OBJCOPY = bfin-elf-objcopy
+OBJDUMP = bfin-elf-objdump
+SIZE    = bfin-elf-size
+NM      = bfin-elf-nm
+
+#
+# Define all object files
+#
+COBJ      = $(CSRC:.c=.o)
+AOBJ      = $(ASRC:.S=.o)
+COBJ      := $(addprefix $(OBJDIR)/,$(COBJ))
+AOBJ      := $(addprefix $(OBJDIR)/,$(AOBJ))
+PROJECT   := $(OBJDIR)/$(PROJECT)
+
+#
+# Flags for C files
+#
+CFLAGS += -std=$(CSTD)
+CFLAGS += -g$(DEBUG)
+CFLAGS += -O$(OPTIMIZE)
+CFLAGS += -fmessage-length=0
+CFLAGS += $(addprefix -W,$(WARNINGS))
+CFLAGS += $(addprefix -I,$(INCDIRS))
+CFLAGS += $(addprefix -D,$(DEFS))
+CFLAGS += -Wp,-M,-MP,-MT,$(OBJDIR)/$(*F).o,-MF,$(OBJDIR)/$(*F).d
+
+#
+# Assembler flags
+#
+ASFLAGS += $(addprefix -D,$(ADEFS)) -Wa,-g$(DEBUG)
+
+#
+# Linker flags
+#
+LDFLAGS += -Wl,-Map=$(PROJECT).map,--cref,--gc-sections
+LDFLAGS += -lc -lgcc
+LDFLAGS += $(patsubst %,-L%,$(LIBDIRS)) $(patsubst %,-l%,$(LIBS))
+#LDFLAGS += -T$(LINKSCRIPT)
+#LDFLAGS += -nostartfiles
+#LDFLAGS += -nodefaultlibs
+#LDFLAGS += -nostdlib
+#LDFLAGS += -fno-builtin
+
+# Combine all necessary flags and optional flags.
+# Add target processor to flags.
+ALL_CFLAGS  = -mcpu=$(CPU) -I. $(CFLAGS)
+ALL_ASFLAGS = -mcpu=$(CPU) -I. -x assembler-with-cpp $(ASFLAGS)
+
+#
+# Default target.
+#
+all: version build size
+
+ifeq ($(OUTPUT),ihex)
+build: elf hex lst sym
+hex: $(PROJECT).hex
+else
+ifeq ($(OUTPUT),binary)
+build: elf bin lst sym
+bin: $(PROJECT).bin
+else
+ifeq ($(OUTPUT),both)
+build: elf hex bin lst sym
+hex: $(PROJECT).hex
+bin: $(PROJECT).bin
+else
+$(error "Invalid format: $(OUTPUT)")
+endif
+endif
+endif
+
+elf: $(PROJECT).elf
+lst: $(PROJECT).lst
+sym: $(PROJECT).sym
+
+#
+# Display compiler version information.
+#
+version :
+       @$(CC) --version
+
+#
+# Create final output file (.hex or .bin) from ELF output file.
+#
+%.hex: %.elf
+       @echo
+       $(OBJCOPY) -O ihex $< $@
+
+%.bin: %.elf
+       @echo
+       $(OBJCOPY) -O binary $< $@
+
+#
+# Create extended listing file from ELF output file.
+#
+%.lst: %.elf
+       @echo
+       $(OBJDUMP) -h -S -C $< > $@
+
+#
+# Create a symbol table from ELF output file.
+#
+%.sym: %.elf
+       @echo
+       $(NM) -n $< > $@
+
+#
+# Display size of file.
+#
+size:
+       @echo
+       $(SIZE) -A $(PROJECT).elf
+
+#
+# Link: create ELF output file from object files.
+#
+%.elf:  $(AOBJ) $(COBJ)
+       @echo
+       @echo Linking...
+       $(CC) $(ALL_CFLAGS) $(AOBJ) $(COBJ) --output $@ $(LDFLAGS)
+
+#
+# Compile: create object files from C source files.
+#
+$(COBJ) : $(OBJDIR)/%.o : %.c
+       @echo
+       @echo $< :
+       $(CC) -c $(ALL_CFLAGS) $< -o $@
+
+#
+# Assemble: create object files from assembler source files.
+#
+$(AOBJ) : $(OBJDIR)/%.o : %.S
+       @echo
+       @echo $< :
+       $(CC) -c $(ALL_ASFLAGS) $< -o $@
+
+#
+# Target: clean project.
+#
+clean:
+       @echo
+       rm -f -r $(OBJDIR) | exit 0
+
+#
+# Load: Load ldr for Blackfin
+#
+load:
+       bfin-elf-ldr -T BF592 -c $(PROJECT).ldr $(PROJECT).elf
+       bfin-elf-ldr -l $(PROJECT).ldr /dev/ttyUSB0 -b 57600 -C
+
+#
+# Console: Console
+#
+console:
+       minicom -b 57600 -D /dev/ttyUSB0
+
+#
+# Include the dependency files.
+#
+-include $(shell mkdir $(OBJDIR) 2>/dev/null) $(wildcard $(OBJDIR)/*.d)
+
diff --git a/firm/bare_metal/noise_generator/main.c b/firm/bare_metal/noise_generator/main.c
new file mode 100644 (file)
index 0000000..bd9056e
--- /dev/null
@@ -0,0 +1,56 @@
+/**
+ * @file main.c
+ * @author Copyright(C) 2012 Shinichiro Nakamura
+ * @brief BlueTank ACB-BF592 Application Sample Codes.
+ */
+
+/*
+ * ===============================================================
+ *  BlueTank
+ * ===============================================================
+ * Copyright (c) 2012 Shinichiro Nakamura
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ * ===============================================================
+ */
+
+#include "uzura.h"
+#include "effect.h"
+#include "system.h"
+
+typedef struct {
+    UZURA uzura;
+} work_t;
+
+int main(void)
+{
+    work_t w;
+
+    effect_param_init();
+    uzura_init(&w.uzura, &w);
+    uzura_set_effect(&w.uzura, effect_noise);
+    uzura_set_system(&w.uzura, system_default);
+    uzura_execute(&w.uzura);
+
+    return 0;
+}
+
index 27845bd..1b6598c 100644 (file)
@@ -11,6 +11,7 @@ CSRC   += mmc.c pff.c
 CSRC   += twi.c uart.c
 CSRC   += ssm2603.c at24c.c
 CSRC   += bfin_util.c uzura.c
+CSRC   += tinyrand.c
 CSRC   += effect.c system.c main.c
 ASRC   +=
 VPATH   = ../common/