diff --git a/.gitignore b/.gitignore index 0ff1ebfa2..5cdd4b63b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ *.ihex *.pyc *~ -obj_* +build/* Makefile.target Makefile.*.defines tools/doxygen/html @@ -36,7 +36,7 @@ COOJA.testlog *.simplelink *.sky *.firmware -*.srf06-cc26xx +*.cc26x0-cc13x0 *.zoul # do not ignore platform makefiles @@ -47,7 +47,7 @@ COOJA.testlog !Makefile.nrf52dk !Makefile.openmote-cc2538 !Makefile.sky -!Makefile.srf06-cc26xx +!Makefile.cc26x0-cc13x0 !Makefile.zoul # other nRF52 build artifacts diff --git a/.gitmodules b/.gitmodules index ed5ea056d..6481bbb93 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,10 +2,10 @@ path = tools/cc2538-bsl url = https://github.com/JelmerT/cc2538-bsl.git [submodule "arch/cpu/cc26xx-cc13xx/lib/cc26xxware"] - path = arch/cpu/cc26xx-cc13xx/lib/cc26xxware + path = arch/cpu/cc26x0-cc13x0/lib/cc26xxware url = https://github.com/contiki-ng/cc26xxware.git [submodule "arch/cpu/cc26xx-cc13xx/lib/cc13xxware"] - path = arch/cpu/cc26xx-cc13xx/lib/cc13xxware + path = arch/cpu/cc26x0-cc13x0/lib/cc13xxware url = https://github.com/contiki-ng/cc13xxware.git [submodule "tools/sensniff"] path = tools/sensniff @@ -26,5 +26,5 @@ path = arch/cpu/simplelink-cc13xx-cc26xx/lib/coresdk_cc13xx_cc26xx url = https://github.com/contiki-ng/coresdk_cc13xx_cc26xx.git [submodule "arch/cpu/cc26xx-cc13xx/lib/cc2640r2-sdk"] - path = arch/cpu/cc26xx-cc13xx/lib/cc2640r2-sdk + path = arch/cpu/cc26x0-cc13x0/lib/cc2640r2-sdk url = https://github.com/contiki-ng/cc2640r2-sdk.git diff --git a/.travis.yml b/.travis.yml index f5a58c609..29e853d0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,14 +30,26 @@ before_install: docker push $DOCKER_IMG; fi fi - # Build Cooja - - ant -q -f $CNG_HOST_PATH/tools/cooja/build.xml jar + # Build Cooja conditionally + - if [ ${BUILD_COOJA:-false} = true ] ; then + ant -q -f $CNG_HOST_PATH/tools/cooja/build.xml jar ; + fi + + # Create a directory for out of tree tests and clone the test repo therein + # The directory will need created unconditionally so we can always chgrp and + # mount it, even if empty. Checkout a pre-defined version. + - mkdir -p $OUT_OF_TREE_TEST_PATH + - if [ ${TEST_NAME} = "out-of-tree-build" ] ; then + git clone --depth 1 https://github.com/contiki-ng/out-of-tree-tests $OUT_OF_TREE_TEST_PATH && + cd $OUT_OF_TREE_TEST_PATH && + git checkout $OUT_OF_TREE_TEST_VER ; + fi # Set permissions for Docker mount - - sudo chgrp -hR 1000 $CNG_HOST_PATH + - sudo chgrp -hR 1000 $CNG_HOST_PATH $OUT_OF_TREE_TEST_PATH # The test script for each build script: - - docker run --privileged -v $CNG_HOST_PATH:/home/user/contiki-ng -ti $DOCKER_IMG bash --login -c "make -C tests/??-$TEST_NAME"; + - docker run --privileged -v $OUT_OF_TREE_TEST_PATH:/home/user/out-of-tree-tests -v $CNG_HOST_PATH:/home/user/contiki-ng -ti $DOCKER_IMG bash --login -c "make -C tests/??-$TEST_NAME"; # Check outcome of the test - $CNG_HOST_PATH/tests/check-test.sh $CNG_HOST_PATH/tests/??-$TEST_NAME; exit $?; @@ -47,6 +59,8 @@ env: global: - DOCKER_IMG='contiker/contiki-ng' - CNG_HOST_PATH=`pwd` + - OUT_OF_TREE_TEST_PATH=$HOME/out-of-tree-tests + - OUT_OF_TREE_TEST_VER=2869ae7 # Encrypted environment variables. # Only available on builds of contiki-ng/contiki-ng branches, not PRs or forks. - secure: 0nrV5yjpT2kE19Hlm7t619Qbmyjx/G7bSUI1c+U3kZbyuxnRlASjVcDN5uPBoimIfGiBRI0nRq690BogAJt4EKwbC1Dy8kC1XD8mRtQ2AIZ6PHaUoG9iS5sBhFBQK0XkB83bwh6omRn/04O0uuX74ooSWT7fDrWxi/y5+0ysXK6gRtOhdrJ3FU5OkNVewX8NeCdx3pOWhMOtXWdFkMIi1XRdDnvMM5/hHlHMkdXXtaZQX9UsK3Q3DSjPRLZjKRiOlcx9MIg2ebh9ITmd2Du2p2q/LKtoutJckvhbKQPWcZi/B+1ZTSff0FHBIg+EYxf6TeFuia7XSTWH7sr2CDCCtcvSR9bB5yW6jdmGfa8Af8I1TCBuqoSUo0Re50BZBZF7COleEh+IojbjXn2CIDMg5rT4Sh3qcMGvFn9OW1cz5h5UNSOk7EIAXXPcI7Aloxh2sBo4/DrvvbfIsKrvxV9Fx4bdyNtR7dZ7xsoOw6L0zttC3K9naf3VAOeBAyjBiRwm0tWxJC/buhTsKlYrthhyUrwLtYAFL4UHcazvz57hY/cEzR2X6F//9Hp7HFoNtn1E36doX3ZfeI22yxHMo9SYW7O69C45wbhJ29lAA9XXbYVyGBKFkY8C1NCZ0Xckt9H8/Ow5Sz8HmW/NNBJCn0Fsx+jezdGc4ED5naugNbLAyNg= @@ -55,14 +69,16 @@ env: - TEST_NAME='compile-base' - TEST_NAME='compile-arm-ports-01' - TEST_NAME='compile-arm-ports-02' - - TEST_NAME='rpl-lite' - - TEST_NAME='rpl-classic' - - TEST_NAME='tun-rpl-br' + - TEST_NAME='rpl-lite' BUILD_COOJA=true + - TEST_NAME='rpl-classic' BUILD_COOJA=true + - TEST_NAME='tun-rpl-br' BUILD_COOJA=true - TEST_NAME='coap-lwm2m' - - TEST_NAME='simulation-base' - - TEST_NAME='ieee802154' + - TEST_NAME='simulation-base' BUILD_COOJA=true + - TEST_NAME='ieee802154' BUILD_COOJA=true - TEST_NAME='compile-nxp-ports' - TEST_NAME='documentation' - TEST_NAME='compile-tools' - TEST_NAME='native-runs' - - TEST_NAME='ipv6' + - TEST_NAME='ipv6' BUILD_COOJA=true + - TEST_NAME='ipv6-nbr' BUILD_COOJA=true + - TEST_NAME='out-of-tree-build' diff --git a/Makefile.embedded b/Makefile.embedded index 4654626c4..e6841926b 100644 --- a/Makefile.embedded +++ b/Makefile.embedded @@ -3,6 +3,9 @@ # Future extensions to the build system that are of a similar nature (for # embedded devices only), can be achieved by extending this Makefile here. +### +### Targets using the tools/serial-io +### RLWRAPGOALS = login serialdump serialview .PHONY: $(RLWRAPGOALS) @@ -31,3 +34,14 @@ serialview: $(SERIAL_DUMP_BIN) login: $(SERIAL_DUMP_BIN) $(SERIALDUMP) -b$(BAUDRATE) $(PORT) + +### +### Targets using tools/motelist +### +CONTIKI_NG_MOTELIST_DIR = $(TOOLS_DIR)/motelist +CONTIKI_NG_MOTELIST = python $(CONTIKI_NG_MOTELIST_DIR)/motelist.py + +.PHONY: motelist-all + +motelist-all: + $(CONTIKI_NG_MOTELIST) diff --git a/Makefile.help b/Makefile.help new file mode 100644 index 000000000..d8de20c00 --- /dev/null +++ b/Makefile.help @@ -0,0 +1,29 @@ +usage: + @echo "Usage:" + @echo " make [TARGET=(TARGET)] [BOARD=(BOARD)] [DEFINES=(DEFINES)] [PORT=(PORT)] [target]" + @echo "" + @echo "Typical usage:" + @echo " make [TARGET=(TARGET)] [BOARD=(BOARD)] [all]" + @echo "" + @echo " Will build Contiki-NG firmware(s) from the current example dir" + @echo " for platform TARGET, board BOARD." + @echo "" + @echo "Miscellaneous targets:" + @echo " targets Prints list of supported platforms" + @echo " boards Prints a list of supported boards for TARGET" + @echo " savetarget Saves TARGET and BOARD for future invocations of make" + @echo " savedefines Saves DEFINES for future invocations of make" + @echo " clean Removes all compiled files for TARGET" + @echo " distclean Removes all compiled files for all TARGETs" + @echo " viewconf Prints Contiki-NG build configuration for TARGET" + @echo " %.flashprof Shows a Flash/ROM profile of a given firmware (e.g. hello-world.flashprof)" + @echo " %.ramprof Shows a RAM profile of a given firmware (e.g. hello-world.ramprof)" + @echo " %.o Produces an object file from a given source file (e.g. hello-world.o)" + @echo " %.e Produces the pre-processed version of a given source file (e.g. hello-world.e)" + @echo " %.s Produces an assembly file from a given source file (e.g. hello-world.s)" + @echo " login View the serial output of the device connected to PORT" + @echo " serialview Same as login, but prepend serial output with a unix timestamp" + @echo " serialdump same as serialview, but also save the output to a file" + @echo " motelist-all Prints a list of connected devices" + +help: usage diff --git a/Makefile.include b/Makefile.include index 319ae29d7..930378c01 100644 --- a/Makefile.include +++ b/Makefile.include @@ -13,8 +13,6 @@ include $(CONTIKI)/Makefile.identify-target ### Include Makefile.tools to pull in targets that allow us to build tools dir include $(CONTIKI)/Makefile.tools -CONTIKI_NG_TARGET_LIB = contiki-ng-$(TARGET).a - ifeq ($(DEFINES),) -include Makefile.$(TARGET).defines ifneq ($(DEFINES),) @@ -39,18 +37,18 @@ ifdef CI endif endif -OBJECTDIR = obj_$(TARGET) +BUILD_DIR = build +BUILD_DIR_TARGET = $(BUILD_DIR)/$(TARGET) +BUILD_DIR_BOARD = $(BUILD_DIR_TARGET)/$(BOARD)/$(BUILD_DIR_CONFIG) +OBJECTDIR = $(BUILD_DIR_BOARD)/obj + +CONTIKI_NG_TARGET_LIB = $(BUILD_DIR_BOARD)/contiki-ng-$(TARGET).a LOWERCASE = -abcdefghijklmnopqrstuvwxyz/ UPPERCASE = _ABCDEFGHIJKLMNOPQRSTUVWXYZ_ TARGET_UPPERCASE := ${strip ${shell echo $(TARGET) | sed y!$(LOWERCASE)!$(UPPERCASE)!}} CFLAGS += -DCONTIKI=1 -DCONTIKI_TARGET_$(TARGET_UPPERCASE)=1 CFLAGS += -DCONTIKI_TARGET_STRING=\"$(TARGET)\" -ifneq ($(BOARD),) -TARGET_BOARD_UPPERCASE := ${strip ${shell echo $(BOARD) | sed y!$(LOWERCASE)!$(UPPERCASE)!}} -CFLAGS += -DCONTIKI_BOARD_$(TARGET_BOARD_UPPERCASE)=1 -CFLAGS += -DCONTIKI_BOARD_STRING=\"$(BOARD)\" -endif CFLAGS += -Wno-unused-const-variable @@ -80,15 +78,14 @@ endef CONTIKI_OBJECTFILES = ${addprefix $(OBJECTDIR)/,${call oname, $(CONTIKI_SOURCEFILES)}} PROJECT_OBJECTFILES = ${addprefix $(OBJECTDIR)/,${call oname, $(PROJECT_SOURCEFILES)}} -# Provide way to create $(OBJECTDIR) if it has been removed by make clean -$(OBJECTDIR): - mkdir $@ - uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1))) ### Include target makefile (TODO Unsafe?) -target_makefile := $(wildcard $(CONTIKI)/arch/platform/$(TARGET)/Makefile.$(TARGET) ${foreach TDIR, $(TARGETDIRS), $(TDIR)/$(TARGET)/Makefile.$(TARGET)}) +# Configurable arch path +ARCH_PATH ?= $(CONTIKI)/arch + +target_makefile := $(wildcard $(ARCH_PATH)/platform/$(TARGET)/Makefile.$(TARGET) ${foreach TDIR, $(TARGETDIRS), $(TDIR)/$(TARGET)/Makefile.$(TARGET)}) # Check if the target makefile exists, and create the object directory if necessary. ifeq ($(strip $(target_makefile)),) @@ -129,6 +126,17 @@ endif # $(BOARD) not empty PLATFORM_ACTION ?= build +# Provide way to create $(OBJECTDIR) if it has been removed by make clean +$(OBJECTDIR): + $(TRACE_MKDIR) + $(Q)mkdir -p $@ + +ifneq ($(BOARD),) + TARGET_BOARD_UPPERCASE := ${strip ${shell echo $(BOARD) | sed y!$(LOWERCASE)!$(UPPERCASE)!}} + CFLAGS += -DCONTIKI_BOARD_$(TARGET_BOARD_UPPERCASE)=1 + CFLAGS += -DCONTIKI_BOARD_STRING=\"$(BOARD)\" +endif + # Configure MAC layer # The different options @@ -240,12 +248,20 @@ ifeq ($(V),1) TRACE_LD = TRACE_AR = TRACE_AS = + TRACE_OBJCOPY = + TRACE_OBJDUMP = + TRACE_MKDIR = + TRACE_CP = Q= else TRACE_CC = @echo " CC " $< TRACE_LD = @echo " LD " $@ TRACE_AR = @echo " AR " $@ TRACE_AS = @echo " AS " $< + TRACE_OBJCOPY = @echo " OBJCOPY " $< "-->" $@ + TRACE_OBJDUMP = @echo " OBJDUMP " $< "-->" $@ + TRACE_MKDIR = @echo " MKDIR " $@ + TRACE_CP = @echo " CP " $< "-->" $@ Q=@ endif @@ -302,24 +318,24 @@ endef ### Harmonize filename of a .map file, if the platform's build system wants ### to create one -CONTIKI_NG_PROJECT_MAP = $(addsuffix -$(TARGET).map, $(basename $@)) +CONTIKI_NG_PROJECT_MAP = $(BUILD_DIR_BOARD)/$(basename $(notdir $@)).map .PHONY: clean distclean usage help targets boards savetarget savedefines viewconf clean: - -$(Q)rm -f *.d *.e *.o $(CONTIKI_NG_TARGET_LIB) $(CLEAN) - -$(Q)rm -rf $(OBJECTDIR) - -$(Q)rm -f $(addsuffix -$(TARGET).map, $(CONTIKI_PROJECT)) + -$(Q)rm -f *.d *.e *.o $(CLEAN) + -$(Q)rm -rf $(BUILD_DIR_TARGET) -$(Q)rm -f $(addsuffix .$(TARGET), $(CONTIKI_PROJECT)) @echo Target $(TARGET) cleaned distclean: - @for TARG in `ls $(CONTIKI)/arch/platform $(TARGETDIRS)`; do \ + @for TARG in `ls $(ARCH_PATH)/platform $(TARGETDIRS)`; do \ echo Running: $(MAKE) TARGET=$$TARG clean; \ $(MAKE) TARGET=$$TARG clean; \ done + -$(Q)rm -rf $(BUILD_DIR) --include $(CONTIKI)/arch/platform/$(TARGET)/Makefile.customrules-$(TARGET) +-include $(ARCH_PATH)/platform/$(TARGET)/Makefile.customrules-$(TARGET) ifndef CUSTOM_RULE_C_TO_OBJECTDIR_O $(OBJECTDIR)/%.o: %.c | $(OBJECTDIR) @@ -382,49 +398,26 @@ ifndef LD endif ifndef CUSTOM_RULE_LINK -%.$(TARGET): %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) +$(BUILD_DIR_BOARD)/%.$(TARGET): %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) $(TRACE_LD) $(Q)$(LD) $(LDFLAGS) $(TARGET_STARTFILES) ${filter-out %.a,$^} \ ${filter %.a,$^} $(TARGET_LIBFILES) -o $@ endif +%.$(TARGET): $(BUILD_DIR_BOARD)/%.$(TARGET) + $(TRACE_CP) + $(Q)cp $< $@ + %.ramprof: %.$(TARGET) $(NM) -S -td --size-sort $< | grep -i " [abdrw] " | cut -d' ' -f2,4 %.flashprof: %.$(TARGET) $(NM) -S -td --size-sort $< | grep -i " [t] " | cut -d' ' -f2,4 -usage: - @echo "Usage:" - @echo " make [TARGET=(TARGET)] [BOARD=(BOARD)] [DEFINES=(DEFINES)] [PORT=(PORT)] [target]" - @echo "" - @echo "Typical usage:" - @echo " make [TARGET=(TARGET)] [BOARD=(BOARD)] [all]" - @echo "" - @echo " Will build Contiki-NG firmware(s) from the current example dir" - @echo " for platform TARGET, board BOARD." - @echo "" - @echo "Miscellaneous targets:" - @echo " targets Prints list of supported platforms" - @echo " boards Prints a list of supported boards for TARGET" - @echo " savetarget Saves TARGET and BOARD for future invocations of make" - @echo " savedefines Saves DEFINES for future invocations of make" - @echo " clean Removes all compiled files for TARGET" - @echo " distclean Removes all compiled files for all TARGETs" - @echo " viewconf Prints Contiki-NG build configuration for TARGET" - @echo " %.flashprof Shows a Flash/ROM profile of a given firmware (e.g. hello-world.flashprof)" - @echo " %.ramprof Shows a RAM profile of a given firmware (e.g. hello-world.ramprof)" - @echo " %.o Produces an object file from a given source file (e.g. hello-world.o)" - @echo " %.e Produces the pre-processed version of a given source file (e.g. hello-world.e)" - @echo " %.s Produces an assembly file from a given source file (e.g. hello-world.s)" - @echo " login View the serial output of the device connected to PORT" - @echo " serialview Same as login, but prepend serial output with a unix timestamp" - @echo " serialdump same as serialview, but also save the output to a file" - -help: usage +include $(CONTIKI)/Makefile.help targets: - @ls $(CONTIKI)/arch/platform $(TARGETDIRS) + @ls $(ARCH_PATH)/platform $(TARGETDIRS) boards: ifdef BOARD @@ -472,9 +465,9 @@ ifeq ($(findstring $(TARGET),native cooja),) include $(CONTIKI)/Makefile.embedded endif -# Don't treat %.$(TARGET) as an intermediate file because it is -# in fact the primary target. -.PRECIOUS: %.$(TARGET) +# Don't treat $(BUILD_DIR_BOARD)/%.$(TARGET) and $(TARGET) as intermediate +# files because for many platforms they are in fact the primary target. +.PRECIOUS: $(BUILD_DIR_BOARD)/%.$(TARGET) %.$(TARGET) # Cancel the predefined implict rule for compiling and linking # a single C source into a binary to force GNU make to consider @@ -485,6 +478,8 @@ ifeq ($(PLATFORM_ACTION),skip) # Skip this target. $(CONTIKI_PROJECT): @echo "Skipping $@: not for the '$(TARGET)/$(BOARD)' platform!" +%.$(TARGET): + @echo "Skipping $@: not for the '$(TARGET)/$(BOARD)' platform!" else # Build this target. # Match-anything pattern rule to allow the project makefiles to diff --git a/README.md b/README.md index e9a1005ef..03a980a0d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Contiki-NG is an open-source, cross-platform operating system for Next-Generation IoT devices. It focuses on dependable (secure and reliable) low-power communication and standard protocols, such as IPv6/6LoWPAN, 6TiSCH, RPL, and CoAP. Contiki-NG comes with extensive documentation, tutorials, a roadmap, release cycle, and well-defined development flow for smooth integration of community contributions. -Unless excplicitly stated otherwise, Contiki-NG sources are distributed under +Unless explicitly stated otherwise, Contiki-NG sources are distributed under the terms of the [3-clause BSD license](LICENSE.md). This license gives everyone the right to use and distribute the code, either in binary or source code format, as long as the copyright license is retained in diff --git a/arch/cpu/arm/Makefile.arm b/arch/cpu/arm/Makefile.arm index 83f9b8a36..e8befdb86 100644 --- a/arch/cpu/arm/Makefile.arm +++ b/arch/cpu/arm/Makefile.arm @@ -1,3 +1,11 @@ +### Verbosity control. Use make V=1 to get verbose builds. +### Extends what we already have in the top-level Makefile +ifeq ($(V),1) + TRACE_SREC_CAT = +else + TRACE_SREC_CAT = @echo " SREC_CAT " $< "-->" $@ +endif + CC = arm-none-eabi-gcc CPP = arm-none-eabi-cpp LD = arm-none-eabi-gcc @@ -43,22 +51,32 @@ endif ### CPU-dependent cleanup files CLEAN += *.elf *.bin *.lst *.hex *.i16hex +OUT_HEX = $(BUILD_DIR_BOARD)/%.hex +OUT_I16HEX = $(BUILD_DIR_BOARD)/%.i16hex +OUT_BIN = $(BUILD_DIR_BOARD)/%.bin +OUT_LST = $(BUILD_DIR_BOARD)/%.lst + ### Don't treat the following files as intermediate -.PRECIOUS: %.elf %.hex %.bin +.PRECIOUS: $(OUT_ELF) $(OUT_HEX) $(OUT_BIN) -%.i16hex: %.elf - $(OBJCOPY) -O ihex $< $@ +$(OUT_I16HEX): $(OUT_ELF) + $(TRACE_OBJCOPY) + $(Q)$(OBJCOPY) -O ihex $< $@ -%.hex: %.i16hex - $(SREC_CAT) $< -intel -o $@ -intel +$(OUT_HEX): $(OUT_I16HEX) + $(TRACE_SREC_CAT) + $(Q)$(SREC_CAT) $< -intel -o $@ -intel -%.bin: %.elf - $(OBJCOPY) -O binary $(OBJCOPY_FLAGS) $< $@ +$(OUT_BIN): $(OUT_ELF) + $(TRACE_OBJCOPY) + $(Q)$(OBJCOPY) -O binary $(OBJCOPY_FLAGS) $< $@ -%.lst: %.elf - $(OBJDUMP) $(OBJDUMP_FLAGS) $< > $@ +$(OUT_LST): $(OUT_ELF) + $(TRACE_OBJDUMP) + $(Q)$(OBJDUMP) $(OBJDUMP_FLAGS) $< > $@ ### We don't really need the .hex and .bin for the .$(TARGET) but let's make ### sure they get built -%.$(TARGET): %.elf %.hex %.bin - cp $< $@ +$(BUILD_DIR_BOARD)/%.$(TARGET): $(OUT_ELF) $(OUT_HEX) $(OUT_BIN) + $(TRACE_CP) + $(Q)cp $< $@ diff --git a/arch/cpu/arm/cortex-m/Makefile.cortex-m b/arch/cpu/arm/cortex-m/Makefile.cortex-m index 444df05cd..3f14ead41 100644 --- a/arch/cpu/arm/cortex-m/Makefile.cortex-m +++ b/arch/cpu/arm/cortex-m/Makefile.cortex-m @@ -1,3 +1,28 @@ CONTIKI_ARM_DIRS += cortex-m cortex-m/CMSIS -include $(CONTIKI)/arch/cpu/arm/Makefile.arm +### Build syscalls for newlib +MODULES += os/lib/newlib + +LDFLAGS += -T $(LDSCRIPT) +LDFLAGS += -Wl,--gc-sections,--sort-section=alignment +LDFLAGS += -Wl,-Map=$(CONTIKI_NG_PROJECT_MAP),--cref,--no-warn-mismatch + +OBJCOPY_FLAGS += --gap-fill 0xff + +CPU_STARTFILES = ${addprefix $(OBJECTDIR)/,${call oname, $(CPU_START_SOURCEFILES)}} + +### Resolve any potential circular dependencies between the linked libraries +### See: https://stackoverflow.com/questions/5651869/gcc-what-are-the-start-group-and-end-group-command-line-options/5651895 +TARGET_LIBFLAGS := -Wl,--start-group $(TARGET_LIBFILES) -Wl,--end-group + +CUSTOM_RULE_LINK = 1 + +OUT_ELF = $(BUILD_DIR_BOARD)/%.elf + +.SECONDEXPANSION: + +$(OUT_ELF): $(CPU_STARTFILES) $$(CONTIKI_OBJECTFILES) %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(LDSCRIPT) $(TARGET_LIBS) + $(TRACE_LD) + $(Q)$(LD) $(LDFLAGS) ${filter-out $(LDSCRIPT) %.a,$^} ${filter %.a,$^} $(TARGET_LIBFLAGS) -o $@ + +include $(ARCH_PATH)/cpu/arm/Makefile.arm diff --git a/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 b/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 index 6bd76135c..35051a0f7 100644 --- a/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 +++ b/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 @@ -3,28 +3,7 @@ CONTIKI_ARM_DIRS += cortex-m/cm3 CFLAGS += -mcpu=cortex-m3 LDFLAGS += -mcpu=cortex-m3 -nostartfiles -LDFLAGS += -T $(LDSCRIPT) -LDFLAGS += -Wl,--gc-sections,--sort-section=alignment -LDFLAGS += -Wl,-Map=$(CONTIKI_NG_PROJECT_MAP),--cref,--no-warn-mismatch -OBJCOPY_FLAGS += --gap-fill 0xff +TARGET_LIBFILES += -lm -### Build syscalls for newlib -MODULES += os/lib/newlib - -CPU_STARTFILES = ${addprefix $(OBJECTDIR)/,${call oname, $(CPU_START_SOURCEFILES)}} - -### Compilation rules -CUSTOM_RULE_LINK = 1 - -### Resolve any potential circular dependencies between the linked libraries -### See: https://stackoverflow.com/questions/5651869/gcc-what-are-the-start-group-and-end-group-command-line-options/5651895 -TARGET_LIBFLAGS := -Wl,--start-group $(TARGET_LIBFILES) -lm -Wl,--end-group - -.SECONDEXPANSION: - -%.elf: $(CPU_STARTFILES) $$(CONTIKI_OBJECTFILES) %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(LDSCRIPT) - $(TRACE_LD) - $(Q)$(LD) $(LDFLAGS) ${filter-out $(LDSCRIPT) %.a,$^} ${filter %.a,$^} $(TARGET_LIBFLAGS) -o $@ - -include $(CONTIKI)/arch/cpu/arm/cortex-m/Makefile.cortex-m +include $(ARCH_PATH)/cpu/arm/cortex-m/Makefile.cortex-m diff --git a/arch/cpu/arm/cortex-m/cm4/Makefile.cm4 b/arch/cpu/arm/cortex-m/cm4/Makefile.cm4 index 110e0697b..dc470601f 100644 --- a/arch/cpu/arm/cortex-m/cm4/Makefile.cm4 +++ b/arch/cpu/arm/cortex-m/cm4/Makefile.cm4 @@ -3,26 +3,5 @@ CONTIKI_ARM_DIRS += cortex-m/cm4 CFLAGS += -mcpu=cortex-m4 LDFLAGS += -mcpu=cortex-m4 -LDFLAGS += -T $(LDSCRIPT) -LDFLAGS += -Wl,--gc-sections,--sort-section=alignment -LDFLAGS += -Wl,-Map=$(CONTIKI_NG_PROJECT_MAP),--cref,--no-warn-mismatch -OBJCOPY_FLAGS += --gap-fill 0xff - -### Build syscalls for newlib -MODULES += os/lib/newlib - -CPU_STARTFILES = ${addprefix $(OBJECTDIR)/,${call oname, $(CPU_START_SOURCEFILES)}} - -### Compilation rules -CUSTOM_RULE_LINK = 1 - -### Resolve any potential circular dependencies between the linked libraries -### See: https://stackoverflow.com/questions/5651869/gcc-what-are-the-start-group-and-end-group-command-line-options/5651895 -TARGET_LIBFLAGS := -Wl,--start-group $(TARGET_LIBFILES) -Wl,--end-group - -%.elf: $(CPU_STARTFILES) %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) $(TARGET_LIBS) - $(TRACE_LD) - $(Q)$(LD) $(LDFLAGS) ${filter %.o %.a,$^} $(TARGET_LIBFLAGS) -o $@ - -include $(CONTIKI)/arch/cpu/arm/cortex-m/Makefile.cortex-m +include $(ARCH_PATH)/cpu/arm/cortex-m/Makefile.cortex-m diff --git a/arch/cpu/cc2538/Makefile.cc2538 b/arch/cpu/cc2538/Makefile.cc2538 index 51a5c40c7..85f435320 100644 --- a/arch/cpu/cc2538/Makefile.cc2538 +++ b/arch/cpu/cc2538/Makefile.cc2538 @@ -52,4 +52,4 @@ $(LDSCRIPT): $(SOURCE_LDSCRIPT) FORCE | $(OBJECTDIR) $(TRACE_CC) $(Q)$(CC) $(LDGENFLAGS) $< | grep -v '^\s*#\s*pragma\>' > $@ -include $(CONTIKI)/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 +include $(ARCH_PATH)/cpu/arm/cortex-m/cm3/Makefile.cm3 diff --git a/arch/cpu/cc2538/cc2538-def.h b/arch/cpu/cc2538/cc2538-def.h index 163456a14..defa4ecb0 100644 --- a/arch/cpu/cc2538/cc2538-def.h +++ b/arch/cpu/cc2538/cc2538-def.h @@ -36,11 +36,13 @@ /*---------------------------------------------------------------------------*/ #define RTIMER_ARCH_SECOND 32768 /*---------------------------------------------------------------------------*/ +#define CC2538_PHY_OVERHEAD 3 +#define CC2538_BYTE_AIR_TIME 32 /* 352us from calling transmit() until the SFD byte has been sent */ -#define CC2538_DELAY_BEFORE_TX ((unsigned)US_TO_RTIMERTICKS(352)) +#define CC2538_DELAY_BEFORE_TX ((unsigned)US_TO_RTIMERTICKS(352)) /* 192us as in datasheet but ACKs are not always received, so adjusted to 250us */ -#define CC2538_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(250)) -#define CC2538_DELAY_BEFORE_DETECT 0 +#define CC2538_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(250)) +#define CC2538_DELAY_BEFORE_DETECT 0 /* Frame filtering done in software */ #define TSCH_CONF_HW_FRAME_FILTERING 0 diff --git a/arch/cpu/cc2538/dev/cc2538-rf.c b/arch/cpu/cc2538/dev/cc2538-rf.c index bddfcc942..e3a548067 100644 --- a/arch/cpu/cc2538/dev/cc2538-rf.c +++ b/arch/cpu/cc2538/dev/cc2538-rf.c @@ -140,6 +140,8 @@ static const output_config_t output_power[] = { {-24, 0x00 }, }; +static radio_result_t get_value(radio_param_t param, radio_value_t *value); + #define OUTPUT_CONFIG_COUNT (sizeof(output_power) / sizeof(output_config_t)) /* Max and Min Output Power in dBm */ @@ -394,6 +396,48 @@ get_sfd_timestamp(void) return RTIMER_NOW() - RADIO_TO_RTIMER(timer_val - sfd); } /*---------------------------------------------------------------------------*/ +/* Enable or disable radio test mode emmiting modulated or unmodulated + * (carrier) signal. See User's Guide pages 719 and 741. + */ +static uint32_t prev_FRMCTRL0, prev_MDMTEST1; +static uint8_t was_on; + +static void +set_test_mode(uint8_t enable, uint8_t modulated) +{ + radio_value_t mode; + get_value(RADIO_PARAM_POWER_MODE, &mode); + + if(enable) { + if(mode == RADIO_POWER_MODE_CARRIER_ON) { + return; + } + was_on = (mode == RADIO_POWER_MODE_ON); + off(); + prev_FRMCTRL0 = REG(RFCORE_XREG_FRMCTRL0); + /* This constantly transmits random data */ + REG(RFCORE_XREG_FRMCTRL0) = 0x00000042; + if(!modulated) { + prev_MDMTEST1 = REG(RFCORE_XREG_MDMTEST1); + /* ...adding this we send an unmodulated carrier instead */ + REG(RFCORE_XREG_MDMTEST1) = 0x00000018; + } + CC2538_RF_CSP_ISTXON(); + } else { + if(mode != RADIO_POWER_MODE_CARRIER_ON) { + return; + } + CC2538_RF_CSP_ISRFOFF(); + REG(RFCORE_XREG_FRMCTRL0) = prev_FRMCTRL0; + if(!modulated) { + REG(RFCORE_XREG_MDMTEST1) = prev_MDMTEST1; + } + if(was_on) { + on(); + } + } +} +/*---------------------------------------------------------------------------*/ /* Netstack API radio driver functions */ /*---------------------------------------------------------------------------*/ static int @@ -806,8 +850,12 @@ get_value(radio_param_t param, radio_value_t *value) switch(param) { case RADIO_PARAM_POWER_MODE: - *value = (REG(RFCORE_XREG_RXENABLE) && RFCORE_XREG_RXENABLE_RXENMASK) == 0 - ? RADIO_POWER_MODE_OFF : RADIO_POWER_MODE_ON; + if((REG(RFCORE_XREG_RXENABLE) & RFCORE_XREG_RXENABLE_RXENMASK) == 0) { + *value = RADIO_POWER_MODE_OFF; + } else { + *value = (REG(RFCORE_XREG_FRMCTRL0) & RFCORE_XREG_FRMCTRL0_TX_MODE) == 0 + ? RADIO_POWER_MODE_ON : RADIO_POWER_MODE_CARRIER_ON; + } return RADIO_RESULT_OK; case RADIO_PARAM_CHANNEL: *value = (radio_value_t)get_channel(); @@ -896,6 +944,11 @@ set_value(radio_param_t param, radio_value_t value) off(); return RADIO_RESULT_OK; } + if(value == RADIO_POWER_MODE_CARRIER_ON || + value == RADIO_POWER_MODE_CARRIER_OFF) { + set_test_mode((value == RADIO_POWER_MODE_CARRIER_ON), 0); + return RADIO_RESULT_OK; + } return RADIO_RESULT_INVALID_VALUE; case RADIO_PARAM_CHANNEL: if(value < CC2538_RF_CHANNEL_MIN || diff --git a/arch/cpu/cc2538/dev/spi-arch.c b/arch/cpu/cc2538/dev/spi-arch.c index d8c6437d5..aae0ed797 100644 --- a/arch/cpu/cc2538/dev/spi-arch.c +++ b/arch/cpu/cc2538/dev/spi-arch.c @@ -281,27 +281,6 @@ spi_arch_close_and_unlock(const spi_device_t *dev) return SPI_DEV_STATUS_OK; } /*---------------------------------------------------------------------------*/ -spi_status_t -spi_arch_select(const spi_device_t *dev) -{ - - if(!spi_arch_has_lock(dev)) { - return SPI_DEV_STATUS_BUS_NOT_OWNED; - } - - SPIX_CS_CLR(PIN_TO_PORT(dev->pin_spi_cs), PIN_TO_NUM(dev->pin_spi_cs)); - - return SPI_DEV_STATUS_OK; -} -/*---------------------------------------------------------------------------*/ -spi_status_t -spi_arch_deselect(const spi_device_t *dev) -{ - SPIX_CS_SET(PIN_TO_PORT(dev->pin_spi_cs), PIN_TO_NUM(dev->pin_spi_cs)); - - return SPI_DEV_STATUS_OK; -} -/*---------------------------------------------------------------------------*/ /* Assumes that checking dev and bus is not NULL before calling this */ spi_status_t spi_arch_transfer(const spi_device_t *dev, diff --git a/arch/cpu/cc26x0-cc13x0/Makefile.cc13x0 b/arch/cpu/cc26x0-cc13x0/Makefile.cc13x0 new file mode 100644 index 000000000..c29ee7270 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/Makefile.cc13x0 @@ -0,0 +1,7 @@ +TI_XXWARE_PATH = lib/cc13xxware + +CONTIKI_CPU_SOURCEFILES += smartrf-settings.c prop-mode.c prop-mode-tx-power.c cc13xx-50kbps-tsch.c + +CFLAGS += -DCPU_FAMILY_CC13X0=1 -DCPU_FAMILY_CC13XX=1 + +include $(CONTIKI_CPU)/Makefile.cc26x0-cc13x0 diff --git a/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0 b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0 new file mode 100644 index 000000000..51db3f205 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0 @@ -0,0 +1,5 @@ +TI_XXWARE_PATH = lib/cc26xxware + +CFLAGS += -DCPU_FAMILY_CC26X0=1 -DCPU_FAMILY_CC26XX=1 + +include $(CONTIKI_CPU)/Makefile.cc26x0-cc13x0 diff --git a/arch/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 similarity index 92% rename from arch/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx rename to arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 index 00f9ae232..d3900b9fb 100644 --- a/arch/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx +++ b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 @@ -1,8 +1,8 @@ -CPU_ABS_PATH = arch/cpu/cc26xx-cc13xx +CPU_ABS_PATH = arch/cpu/cc26x0-cc13x0 TI_XXWARE = $(CONTIKI_CPU)/$(TI_XXWARE_PATH) -ifeq (,$(wildcard $(TI_XXWARE))) - $(warning $(TI_XXWARE) does not exist.) +ifeq (,$(wildcard $(TI_XXWARE)/*)) + $(warning $(TI_XXWARE) does not exist or is empty.) $(warning Did you run 'git submodule update --init' ?) $(error "") endif @@ -24,7 +24,7 @@ CFLAGS += -I$(TI_XXWARE) -I$(CONTIKI)/$(TI_XXWARE_SRC) CFLAGS += -I$(TI_XXWARE)/inc MODULES += $(TI_XXWARE_SRC) -LDSCRIPT = $(CONTIKI_CPU)/cc26xx.ld +LDSCRIPT ?= $(CONTIKI_CPU)/cc26xx.ld ### If the user-specified a Node ID, pass a define ifdef NODEID @@ -44,6 +44,7 @@ CONTIKI_CPU_SOURCEFILES += rf-core.c rf-ble.c ieee-mode.c CONTIKI_CPU_SOURCEFILES += ble-cc2650.c ble-hal-cc26xx.c ble-addr.c rf-ble-cmd.c CONTIKI_CPU_SOURCEFILES += random.c soc-trng.c int-master.c CONTIKI_CPU_SOURCEFILES += spi-arch.c +CONTIKI_CPU_SOURCEFILES += cc26xx-aes.c CONTIKI_SOURCEFILES += $(CONTIKI_CPU_SOURCEFILES) @@ -81,8 +82,10 @@ STACK_SIZE = 0 @$(SIZE) -A $< | egrep "data|bss" | awk '{s+=$$2} END {s=s+$(STACK_SIZE); f=$(RAM_SIZE)-s; printf "[RAM] used %6d, free %6d\n",s,f;}' @$(SIZE) -A $< | egrep "text|isr_vector" | awk '{s+=$$2} END {f=$(FLASH_SIZE)-s; printf "[Flash] used %6d, free %6d\n",s,f;}' +include $(ARCH_PATH)/cpu/arm/cortex-m/cm3/Makefile.cm3 + ifeq ($(BOARD_SUPPORTS_BSL),1) -%.upload: %.bin +%.upload: $(OUT_BIN) ifeq ($(wildcard $(BSL)), ) @echo "ERROR: Could not find the cc2538-bsl script. Did you run 'git submodule update --init' ?" else @@ -95,5 +98,3 @@ endif ### For the login etc targets BAUDRATE = 115200 - -include $(CONTIKI)/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 diff --git a/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0r2f b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0r2f new file mode 100644 index 000000000..5118cd6a8 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0r2f @@ -0,0 +1,5 @@ +TI_XXWARE_PATH = lib/cc2640r2-sdk + +CFLAGS += -DCPU_FAMILY_CC26X0R2=1 -DCPU_FAMILY_CC26XXR2=1 + +include $(CONTIKI_CPU)/Makefile.cc26x0-cc13x0 diff --git a/arch/cpu/cc26xx-cc13xx/ble-addr.c b/arch/cpu/cc26x0-cc13x0/ble-addr.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/ble-addr.c rename to arch/cpu/cc26x0-cc13x0/ble-addr.c diff --git a/arch/cpu/cc26xx-cc13xx/ble-addr.h b/arch/cpu/cc26x0-cc13x0/ble-addr.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/ble-addr.h rename to arch/cpu/cc26x0-cc13x0/ble-addr.h diff --git a/arch/cpu/cc26xx-cc13xx/cc13x0-cc26x0-cm3.h b/arch/cpu/cc26x0-cc13x0/cc13x0-cc26x0-cm3.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/cc13x0-cc26x0-cm3.h rename to arch/cpu/cc26x0-cc13x0/cc13x0-cc26x0-cm3.h diff --git a/arch/cpu/cc26xx-cc13xx/cc13xx-cc26xx-conf.h b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h similarity index 97% rename from arch/cpu/cc26xx-cc13xx/cc13xx-cc26xx-conf.h rename to arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h index e7d9dacd7..0b553fb90 100644 --- a/arch/cpu/cc26xx-cc13xx/cc13xx-cc26xx-conf.h +++ b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h @@ -64,11 +64,11 @@ * project has specified otherwise. Depending on the final mode, determine a * default channel (again, if unspecified) and configure RDC params */ -#if CPU_FAMILY_CC13XX +#if CPU_FAMILY_CC13X0 #ifndef CC13XX_CONF_PROP_MODE #define CC13XX_CONF_PROP_MODE 1 #endif /* CC13XX_CONF_PROP_MODE */ -#endif /* CPU_FAMILY_CC13XX */ +#endif /* CPU_FAMILY_CC13X0 */ #if CC13XX_CONF_PROP_MODE #ifndef NETSTACK_CONF_RADIO @@ -102,6 +102,11 @@ #define NETSTACK_RADIO_MAX_PAYLOAD_LEN 125 +/* Platform-specific (H/W) AES implementation */ +#ifndef AES_128_CONF +#define AES_128_CONF cc26xx_aes_128_driver +#endif /* AES_128_CONF */ + /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/cpu/cc26xx-cc13xx/cc13xx-cc26xx-def.h b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-def.h similarity index 79% rename from arch/cpu/cc26xx-cc13xx/cc13xx-cc26xx-def.h rename to arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-def.h index 47dbc4eb5..97b188e15 100644 --- a/arch/cpu/cc26xx-cc13xx/cc13xx-cc26xx-def.h +++ b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-def.h @@ -36,12 +36,43 @@ /*---------------------------------------------------------------------------*/ /* TSCH related defines */ +/* 2 bytes header, 4 bytes CRC */ +#define CC13XX_RADIO_PHY_OVERHEAD 6 +/* 3 bytes preamble, 3 bytes sync */ +#define CC13XX_RADIO_PHY_HEADER_LEN 6 +/* The default data rate is 50 kbps */ +#define CC13XX_RADIO_BIT_RATE 50000 + /* 1 len byte, 2 bytes CRC */ -#define RADIO_PHY_OVERHEAD 3 -/* 250kbps data rate. One byte = 32us */ -#define RADIO_BYTE_AIR_TIME 32 +#define CC26XX_RADIO_PHY_OVERHEAD 3 +/* 4 bytes preamble, 1 byte sync */ +#define CC26XX_RADIO_PHY_HEADER_LEN 5 +/* The fixed data rate is 250 kbps */ +#define CC26XX_RADIO_BIT_RATE 250000 + +#if CPU_FAMILY_CC13XX +#define RADIO_PHY_HEADER_LEN CC13XX_RADIO_PHY_HEADER_LEN +#define RADIO_PHY_OVERHEAD CC13XX_RADIO_PHY_OVERHEAD +#define RADIO_BIT_RATE CC13XX_RADIO_BIT_RATE + +/* The TSCH default slot length of 10ms is too short, use custom one instead */ +#ifndef TSCH_CONF_DEFAULT_TIMESLOT_TIMING +#define TSCH_CONF_DEFAULT_TIMESLOT_TIMING tsch_timing_cc13xx_50kbps +#endif /* TSCH_CONF_DEFAULT_TIMESLOT_TIMING */ + +/* Symbol for the custom TSCH timeslot timing template */ +#define TSCH_CONF_ARCH_HDR_PATH "rf-core/cc13xx-50kbps-tsch.h" + +#else +#define RADIO_PHY_HEADER_LEN CC26XX_RADIO_PHY_HEADER_LEN +#define RADIO_PHY_OVERHEAD CC26XX_RADIO_PHY_OVERHEAD +#define RADIO_BIT_RATE CC26XX_RADIO_BIT_RATE +#endif + +#define RADIO_BYTE_AIR_TIME (1000000 / (RADIO_BIT_RATE / 8)) + /* Delay between GO signal and SFD */ -#define RADIO_DELAY_BEFORE_TX ((unsigned)US_TO_RTIMERTICKS(81)) +#define RADIO_DELAY_BEFORE_TX ((unsigned)US_TO_RTIMERTICKS(RADIO_PHY_HEADER_LEN * RADIO_BYTE_AIR_TIME)) /* Delay between GO signal and start listening. * This value is so small because the radio is constantly on within each timeslot. */ #define RADIO_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(15)) @@ -56,9 +87,6 @@ #define RADIO_TO_RTIMER(X) ((uint32_t)(((uint64_t)(X) * (RTIMER_SECOND / 256)) / (RADIO_TIMER_SECOND / 256))) #define USEC_TO_RADIO(X) ((X) * 4) -/* The PHY header (preamble + SFD, 4+1 bytes) duration is equivalent to 10 symbols */ -#define RADIO_IEEE_802154_PHY_HEADER_DURATION_USEC 160 - /* Do not turn off TSCH within a timeslot: not enough time */ #define TSCH_CONF_RADIO_ON_DURING_TIMESLOT 1 diff --git a/arch/cpu/cc26xx-cc13xx/cc26xx.ld b/arch/cpu/cc26x0-cc13x0/cc26xx.ld similarity index 98% rename from arch/cpu/cc26xx-cc13xx/cc26xx.ld rename to arch/cpu/cc26x0-cc13x0/cc26xx.ld index 649da32cf..4ea422558 100644 --- a/arch/cpu/cc26xx-cc13xx/cc26xx.ld +++ b/arch/cpu/cc26x0-cc13x0/cc26xx.ld @@ -96,6 +96,8 @@ SECTIONS /* These symbols are used by the stack check library. */ _stack = .; _stack_origin = ORIGIN(SRAM) + LENGTH(SRAM); + _heap = _stack; + _eheap = _stack_origin; .ccfg : { diff --git a/arch/cpu/cc26xx-cc13xx/ccxxware-conf.h b/arch/cpu/cc26x0-cc13x0/ccxxware-conf.h similarity index 98% rename from arch/cpu/cc26xx-cc13xx/ccxxware-conf.h rename to arch/cpu/cc26x0-cc13x0/ccxxware-conf.h index bc26f1171..c101deb08 100644 --- a/arch/cpu/cc26xx-cc13xx/ccxxware-conf.h +++ b/arch/cpu/cc26x0-cc13x0/ccxxware-conf.h @@ -36,7 +36,7 @@ * @{ * * \file - * CCxxware-specific configuration for the cc26xx-cc13xx CPU family + * CCxxware-specific configuration for the cc26x0-cc13x0 CPU family */ #ifndef CCXXWARE_CONF_H_ #define CCXXWARE_CONF_H_ diff --git a/arch/cpu/cc26xx-cc13xx/clock.c b/arch/cpu/cc26x0-cc13x0/clock.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/clock.c rename to arch/cpu/cc26x0-cc13x0/clock.c diff --git a/arch/cpu/cc26xx-cc13xx/dbg.c b/arch/cpu/cc26x0-cc13x0/dbg.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dbg.c rename to arch/cpu/cc26x0-cc13x0/dbg.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/adc-sensor.c b/arch/cpu/cc26x0-cc13x0/dev/adc-sensor.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/adc-sensor.c rename to arch/cpu/cc26x0-cc13x0/dev/adc-sensor.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/adc-sensor.h b/arch/cpu/cc26x0-cc13x0/dev/adc-sensor.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/adc-sensor.h rename to arch/cpu/cc26x0-cc13x0/dev/adc-sensor.h diff --git a/arch/cpu/cc26xx-cc13xx/dev/aux-ctrl.c b/arch/cpu/cc26x0-cc13x0/dev/aux-ctrl.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/aux-ctrl.c rename to arch/cpu/cc26x0-cc13x0/dev/aux-ctrl.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/aux-ctrl.h b/arch/cpu/cc26x0-cc13x0/dev/aux-ctrl.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/aux-ctrl.h rename to arch/cpu/cc26x0-cc13x0/dev/aux-ctrl.h diff --git a/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.c b/arch/cpu/cc26x0-cc13x0/dev/batmon-sensor.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.c rename to arch/cpu/cc26x0-cc13x0/dev/batmon-sensor.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h b/arch/cpu/cc26x0-cc13x0/dev/batmon-sensor.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h rename to arch/cpu/cc26x0-cc13x0/dev/batmon-sensor.h diff --git a/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c new file mode 100755 index 000000000..cacdfc61f --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/** + * \addtogroup cc26xx-aes + * @{ + * + * \file + * Implementation of the AES driver for the CC26x0/CC13x0 SoC + * \author + * Atis Elsts + */ +#include "contiki.h" +#include "dev/cc26xx-aes.h" +#include "ti-lib.h" +/*---------------------------------------------------------------------------*/ +#include "sys/log.h" +#define LOG_MODULE "cc26xx-aes" +#define LOG_LEVEL LOG_LEVEL_MAIN +/*---------------------------------------------------------------------------*/ +static uint32_t skey[AES_128_KEY_LENGTH / sizeof(uint32_t)]; + +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_set_key(const uint8_t *key) +{ + memcpy(skey, key, AES_128_KEY_LENGTH); +} +/*---------------------------------------------------------------------------*/ +static void +encrypt_decrypt(uint8_t *plaintext_and_result, bool do_encrypt) +{ + uint32_t result[AES_128_BLOCK_SIZE / sizeof(uint32_t)]; + unsigned status; + int i; + + /* First, make sure the PERIPH PD is on */ + ti_lib_prcm_power_domain_on(PRCM_DOMAIN_PERIPH); + while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) + != PRCM_DOMAIN_POWER_ON)); + + /* Enable CRYPTO peripheral */ + ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_CRYPTO); + ti_lib_prcm_load_set(); + while(!ti_lib_prcm_load_get()); + + status = ti_lib_crypto_aes_load_key(skey, CRYPTO_KEY_AREA_0); + if(status != AES_SUCCESS) { + LOG_WARN("load key failed: %u\n", status); + } else { + + status = ti_lib_crypto_aes_ecb((uint32_t *)plaintext_and_result, result, CRYPTO_KEY_AREA_0, do_encrypt, false); + if(status != AES_SUCCESS) { + LOG_WARN("ecb failed: %u\n", status); + } else { + + for(i = 0; i < 100; ++i) { + ti_lib_cpu_delay(10); + status = ti_lib_crypto_aes_ecb_status(); + if(status != AES_DMA_BSY) { + break; + } + } + + ti_lib_crypto_aes_ecb_finish(); + + if(status != AES_SUCCESS) { + LOG_WARN("ecb get result failed: %u\n", status); + } + } + } + + ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_CRYPTO); + ti_lib_prcm_load_set(); + while(!ti_lib_prcm_load_get()); + + if(status == AES_SUCCESS) { + memcpy(plaintext_and_result, result, AES_128_BLOCK_SIZE); + } else { + /* corrupt the result */ + plaintext_and_result[0] ^= 1; + } +} +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_encrypt(uint8_t *plaintext_and_result) +{ + encrypt_decrypt(plaintext_and_result, true); +} +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_decrypt(uint8_t *cyphertext_and_result) +{ + encrypt_decrypt(cyphertext_and_result, false); +} +/*---------------------------------------------------------------------------*/ +const struct aes_128_driver cc26xx_aes_128_driver = { + cc26xx_aes_set_key, + cc26xx_aes_encrypt +}; + +/** @} */ diff --git a/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h new file mode 100755 index 000000000..a979cc562 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/** + * \addtogroup cc26xx + * @{ + * + * \defgroup cc26xx-aes CC26x0/CC13x0 AES-128 + * + * AES-128 driver for the CC26x0/CC13x0 SoC + * @{ + * + * \file + * Header file of the AES-128 driver for the CC26xx SoC + * \author + * Atis Elsts + */ +#ifndef CC2538_AES_H_ +#define CC2538_AES_H_ + +#include "lib/aes-128.h" + +/** + * \brief Set a key to use in subsequent encryption & decryption operations. + * \param key The key to use + * + * The size of the key must be AES_128_KEY_LENGTH. + */ +void cc26xx_aes_set_key(const uint8_t *key); + +/** + * \brief Encrypt a message using the SoC AES-128 hardware implementation + * \param plaintext_and_result In: message to encrypt, out: the encrypted message. + * + * The size of the message must be AES_128_BLOCK_SIZE. + * The key to use in the encryption must be set before calling this function. + */ +void cc26xx_aes_encrypt(uint8_t *plaintext_and_result); + +/** + * \brief Decrypt a message using the SoC AES-128 hardware implementation + * \param cyphertext_and_result In: message to decrypt, out: the decrypted message. + * + * The size of the message must be AES_128_BLOCK_SIZE. + * The key to use in the decryption must be set before calling this function. + */ +void cc26xx_aes_decrypt(uint8_t *cyphertext_and_result); + +extern const struct aes_128_driver cc26xx_aes_128_driver; + +#endif /* CC2538_AES_H_ */ +/** + * @} + * @} + */ diff --git a/arch/cpu/cc26xx-cc13xx/dev/cc26xx-uart.c b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/cc26xx-uart.c rename to arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/cc26xx-uart.h b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/cc26xx-uart.h rename to arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h diff --git a/arch/cpu/cc26xx-cc13xx/dev/contiki-watchdog.c b/arch/cpu/cc26x0-cc13x0/dev/contiki-watchdog.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/contiki-watchdog.c rename to arch/cpu/cc26x0-cc13x0/dev/contiki-watchdog.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/gpio-hal-arch.c b/arch/cpu/cc26x0-cc13x0/dev/gpio-hal-arch.c similarity index 94% rename from arch/cpu/cc26xx-cc13xx/dev/gpio-hal-arch.c rename to arch/cpu/cc26x0-cc13x0/dev/gpio-hal-arch.c index 6516c22cd..4fd4f7688 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/gpio-hal-arch.c +++ b/arch/cpu/cc26x0-cc13x0/dev/gpio-hal-arch.c @@ -39,7 +39,6 @@ /*---------------------------------------------------------------------------*/ #include "contiki.h" #include "ti-lib.h" -#include "ti-lib-rom.h" #include "dev/gpio-hal.h" #include @@ -53,11 +52,7 @@ gpio_hal_arch_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg) gpio_hal_pin_cfg_t tmp; /* Clear settings that we are about to change, keep everything else */ -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated config = ti_lib_ioc_port_configure_get(pin); -#else - config = ti_lib_rom_ioc_port_configure_get(pin); -#endif config &= ~CONFIG_MASK; tmp = cfg & GPIO_HAL_PIN_CFG_EDGE_BOTH; @@ -87,7 +82,7 @@ gpio_hal_arch_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg) config |= IOC_INT_ENABLE; } - ti_lib_rom_ioc_port_configure_set(pin, IOC_PORT_GPIO, config); + ti_lib_ioc_port_configure_set(pin, IOC_PORT_GPIO, config); } /*---------------------------------------------------------------------------*/ gpio_hal_pin_cfg_t @@ -98,11 +93,7 @@ gpio_hal_arch_pin_cfg_get(gpio_hal_pin_t pin) uint32_t config; cfg = 0; -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated config = ti_lib_ioc_port_configure_get(pin); -#else - config = ti_lib_rom_ioc_port_configure_get(pin); -#endif /* Pull */ tmp = config & IOC_IOPULL_M; diff --git a/arch/cpu/cc26xx-cc13xx/dev/gpio-hal-arch.h b/arch/cpu/cc26x0-cc13x0/dev/gpio-hal-arch.h similarity index 89% rename from arch/cpu/cc26xx-cc13xx/dev/gpio-hal-arch.h rename to arch/cpu/cc26x0-cc13x0/dev/gpio-hal-arch.h index 42a912674..3997c1062 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/gpio-hal-arch.h +++ b/arch/cpu/cc26x0-cc13x0/dev/gpio-hal-arch.h @@ -49,7 +49,6 @@ /*---------------------------------------------------------------------------*/ #include "contiki.h" #include "ti-lib.h" -#include "ti-lib-rom.h" #include /*---------------------------------------------------------------------------*/ @@ -57,15 +56,10 @@ #define gpio_hal_arch_interrupt_enable(p) interrupt_enable(p) -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated #define gpio_hal_arch_interrupt_disable(p) ti_lib_ioc_int_disable(p) #define gpio_hal_arch_pin_set_input(p) ti_lib_ioc_pin_type_gpio_input(p) #define gpio_hal_arch_pin_set_output(p) ti_lib_ioc_pin_type_gpio_output(p) -#else -#define gpio_hal_arch_interrupt_disable(p) ti_lib_rom_ioc_int_disable(p) -#define gpio_hal_arch_pin_set_input(p) ti_lib_rom_ioc_pin_type_gpio_input(p) -#define gpio_hal_arch_pin_set_output(p) ti_lib_rom_ioc_pin_type_gpio_output(p) -#endif + #define gpio_hal_arch_set_pin(p) ti_lib_gpio_set_dio(p) #define gpio_hal_arch_clear_pin(p) ti_lib_gpio_clear_dio(p) #define gpio_hal_arch_toggle_pin(p) ti_lib_gpio_toggle_dio(p) @@ -80,11 +74,7 @@ static inline void interrupt_enable(gpio_hal_pin_t pin) { ti_lib_gpio_clear_event_dio(pin); -#ifndef ThisLibraryIsFor_CC26x0R2_HaltIfViolated - ti_lib_rom_ioc_int_enable(pin); -#else ti_lib_ioc_int_enable(pin); -#endif } /*---------------------------------------------------------------------------*/ #endif /* GPIO_HAL_ARCH_H_ */ diff --git a/arch/cpu/cc26xx-cc13xx/dev/gpio-interrupt.c b/arch/cpu/cc26x0-cc13x0/dev/gpio-interrupt.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/gpio-interrupt.c rename to arch/cpu/cc26x0-cc13x0/dev/gpio-interrupt.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/oscillators.c b/arch/cpu/cc26x0-cc13x0/dev/oscillators.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/oscillators.c rename to arch/cpu/cc26x0-cc13x0/dev/oscillators.c diff --git a/arch/cpu/cc26xx-cc13xx/dev/oscillators.h b/arch/cpu/cc26x0-cc13x0/dev/oscillators.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/oscillators.h rename to arch/cpu/cc26x0-cc13x0/dev/oscillators.h diff --git a/arch/cpu/cc26xx-cc13xx/dev/soc-rtc.c b/arch/cpu/cc26x0-cc13x0/dev/soc-rtc.c similarity index 99% rename from arch/cpu/cc26xx-cc13xx/dev/soc-rtc.c rename to arch/cpu/cc26x0-cc13x0/dev/soc-rtc.c index 8de930a9c..2309026b6 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/soc-rtc.c +++ b/arch/cpu/cc26x0-cc13x0/dev/soc-rtc.c @@ -99,7 +99,7 @@ soc_rtc_init(void) ti_lib_aon_rtc_channel_enable(AON_RTC_CH1); ti_lib_aon_rtc_enable(); - ti_lib_rom_int_enable(INT_AON_RTC_COMB); + ti_lib_int_enable(INT_AON_RTC_COMB); /* Re-enable interrupts */ if(!interrupts_disabled) { diff --git a/arch/cpu/cc26xx-cc13xx/dev/soc-rtc.h b/arch/cpu/cc26x0-cc13x0/dev/soc-rtc.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/soc-rtc.h rename to arch/cpu/cc26x0-cc13x0/dev/soc-rtc.h diff --git a/arch/cpu/cc26xx-cc13xx/dev/soc-trng.c b/arch/cpu/cc26x0-cc13x0/dev/soc-trng.c similarity index 94% rename from arch/cpu/cc26xx-cc13xx/dev/soc-trng.c rename to arch/cpu/cc26x0-cc13x0/dev/soc-trng.c index 2fea8418d..30a558640 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/soc-trng.c +++ b/arch/cpu/cc26x0-cc13x0/dev/soc-trng.c @@ -71,7 +71,7 @@ static void disable_number_ready_interrupt(void) { ti_lib_trng_int_disable(TRNG_NUMBER_READY); - ti_lib_rom_int_disable(INT_TRNG_IRQ); + ti_lib_int_disable(INT_TRNG_IRQ); } /*---------------------------------------------------------------------------*/ static void @@ -79,14 +79,14 @@ enable_number_ready_interrupt(void) { ti_lib_trng_int_clear(TRNG_NUMBER_READY); ti_lib_trng_int_enable(TRNG_NUMBER_READY); - ti_lib_rom_int_enable(INT_TRNG_IRQ); + ti_lib_int_enable(INT_TRNG_IRQ); } /*---------------------------------------------------------------------------*/ static bool accessible(void) { /* First, check the PD */ - if(ti_lib_rom_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) + if(ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) != PRCM_DOMAIN_POWER_ON) { return false; } @@ -104,12 +104,12 @@ static void power_up(void) { /* First, make sure the PERIPH PD is on */ - ti_lib_rom_prcm_power_domain_on(PRCM_DOMAIN_PERIPH); - while((ti_lib_rom_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) + ti_lib_prcm_power_domain_on(PRCM_DOMAIN_PERIPH); + while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) != PRCM_DOMAIN_POWER_ON)); /* Enable clock in active mode */ - ti_lib_rom_prcm_peripheral_run_enable(PRCM_PERIPH_TRNG); + ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_TRNG); ti_lib_prcm_load_set(); while(!ti_lib_prcm_load_get()); } @@ -136,7 +136,7 @@ static uint64_t read_number(void) { uint64_t ran = (uint64_t)HWREG(TRNG_BASE + TRNG_O_OUT1) << 32; - ran += ti_lib_rom_trng_number_get(TRNG_LOW_WORD); + ran += ti_lib_trng_number_get(TRNG_LOW_WORD); return ran; } @@ -237,7 +237,7 @@ soc_trng_rand_asynchronous(uint32_t samples, soc_trng_callback_t cb) ti_lib_trng_int_clear(TRNG_NUMBER_READY); /* Enable clock in sleep mode and register with LPM */ - ti_lib_rom_prcm_peripheral_sleep_enable(PRCM_PERIPH_TRNG); + ti_lib_prcm_peripheral_sleep_enable(PRCM_PERIPH_TRNG); ti_lib_prcm_load_set(); while(!ti_lib_prcm_load_get()); @@ -271,7 +271,7 @@ PROCESS_THREAD(soc_trng_process, ev, data) } /* Disable clock in sleep mode */ - ti_lib_rom_prcm_peripheral_sleep_disable(PRCM_PERIPH_TRNG); + ti_lib_prcm_peripheral_sleep_disable(PRCM_PERIPH_TRNG); ti_lib_prcm_load_set(); while(!ti_lib_prcm_load_get()); diff --git a/arch/cpu/cc26xx-cc13xx/dev/soc-trng.h b/arch/cpu/cc26x0-cc13x0/dev/soc-trng.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/dev/soc-trng.h rename to arch/cpu/cc26x0-cc13x0/dev/soc-trng.h diff --git a/arch/cpu/cc26xx-cc13xx/dev/spi-arch.c b/arch/cpu/cc26x0-cc13x0/dev/spi-arch.c similarity index 82% rename from arch/cpu/cc26xx-cc13xx/dev/spi-arch.c rename to arch/cpu/cc26x0-cc13x0/dev/spi-arch.c index d42c06af9..242cfa503 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/spi-arch.c +++ b/arch/cpu/cc26x0-cc13x0/dev/spi-arch.c @@ -123,7 +123,7 @@ spi_arch_lock_and_open(const spi_device_t *dev) != PRCM_DOMAIN_POWER_ON)) ; /* Enable clock in active mode */ - ti_lib_rom_prcm_peripheral_run_enable(spi_controller[dev->spi_controller].prcm_periph); + ti_lib_prcm_peripheral_run_enable(spi_controller[dev->spi_controller].prcm_periph); ti_lib_prcm_load_set(); while(!ti_lib_prcm_load_get()) ; @@ -131,17 +131,14 @@ spi_arch_lock_and_open(const spi_device_t *dev) ti_lib_ssi_int_disable(spi_controller[dev->spi_controller].ssi_base, SSI_RXOR | SSI_RXFF | SSI_RXTO | SSI_TXFF); ti_lib_ssi_int_clear(spi_controller[dev->spi_controller].ssi_base, SSI_RXOR | SSI_RXTO); -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated - ti_lib_ssi_config_set_exp_clk(spi_controller[dev->spi_controller].ssi_base, ti_lib_sys_ctrl_clock_get(), - get_mode(dev), SSI_MODE_MASTER, dev->spi_bit_rate, 8); - ti_lib_ioc_pin_type_ssi_master(spi_controller[dev->spi_controller].ssi_base, dev->pin_spi_miso, - dev->pin_spi_mosi, IOID_UNUSED, dev->pin_spi_sck); -#else - ti_lib_rom_ssi_config_set_exp_clk(spi_controller[dev->spi_controller].ssi_base, ti_lib_sys_ctrl_clock_get(), - get_mode(dev), SSI_MODE_MASTER, dev->spi_bit_rate, 8); - ti_lib_rom_ioc_pin_type_ssi_master(spi_controller[dev->spi_controller].ssi_base, dev->pin_spi_miso, - dev->pin_spi_mosi, IOID_UNUSED, dev->pin_spi_sck); -#endif + ti_lib_ssi_config_set_exp_clk(spi_controller[dev->spi_controller].ssi_base, + ti_lib_sys_ctrl_clock_get(), + get_mode(dev), SSI_MODE_MASTER, + dev->spi_bit_rate, 8); + ti_lib_ioc_pin_type_ssi_master(spi_controller[dev->spi_controller].ssi_base, + dev->pin_spi_miso, + dev->pin_spi_mosi, IOID_UNUSED, + dev->pin_spi_sck); ti_lib_ssi_enable(spi_controller[dev->spi_controller].ssi_base); @@ -159,7 +156,7 @@ spi_arch_close_and_unlock(const spi_device_t *dev) } /* Power down SSI */ - ti_lib_rom_prcm_peripheral_run_disable(spi_controller[dev->spi_controller].prcm_periph); + ti_lib_prcm_peripheral_run_disable(spi_controller[dev->spi_controller].prcm_periph); ti_lib_prcm_load_set(); while(!ti_lib_prcm_load_get()) ; @@ -213,39 +210,12 @@ spi_arch_transfer(const spi_device_t *dev, for(i = 0; i < totlen; i++) { c = i < wlen ? write_buf[i] : 0; ti_lib_ssi_data_put(spi_controller[dev->spi_controller].ssi_base, (uint8_t)c); -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated ti_lib_ssi_data_get(spi_controller[dev->spi_controller].ssi_base, &c); -#else - ti_lib_rom_ssi_data_get(spi_controller[dev->spi_controller].ssi_base, &c); -#endif if(i < rlen) { inbuf[i] = (uint8_t)c; } } -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated while(ti_lib_ssi_data_get_non_blocking(spi_controller[dev->spi_controller].ssi_base, &c)) ; -#else - while(ti_lib_rom_ssi_data_get_non_blocking(spi_controller[dev->spi_controller].ssi_base, &c)) ; -#endif return SPI_DEV_STATUS_OK; } /*---------------------------------------------------------------------------*/ -spi_status_t -spi_arch_select(const spi_device_t *dev) -{ - - if(!spi_arch_has_lock(dev)) { - return SPI_DEV_STATUS_BUS_NOT_OWNED; - } - - ti_lib_gpio_clear_dio(dev->pin_spi_cs); - - return SPI_DEV_STATUS_OK; -} -spi_status_t -spi_arch_deselect(const spi_device_t *dev) -{ - ti_lib_gpio_set_dio(dev->pin_spi_cs); - - return SPI_DEV_STATUS_OK; -} diff --git a/arch/cpu/cc26xx-cc13xx/doxygen-group.txt b/arch/cpu/cc26x0-cc13x0/doxygen-group.txt similarity index 100% rename from arch/cpu/cc26xx-cc13xx/doxygen-group.txt rename to arch/cpu/cc26x0-cc13x0/doxygen-group.txt diff --git a/arch/cpu/cc26xx-cc13xx/fault-handlers.c b/arch/cpu/cc26x0-cc13x0/fault-handlers.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/fault-handlers.c rename to arch/cpu/cc26x0-cc13x0/fault-handlers.c diff --git a/arch/cpu/cc26xx-cc13xx/ieee-addr.c b/arch/cpu/cc26x0-cc13x0/ieee-addr.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/ieee-addr.c rename to arch/cpu/cc26x0-cc13x0/ieee-addr.c diff --git a/arch/cpu/cc26xx-cc13xx/ieee-addr.h b/arch/cpu/cc26x0-cc13x0/ieee-addr.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/ieee-addr.h rename to arch/cpu/cc26x0-cc13x0/ieee-addr.h diff --git a/arch/cpu/cc26xx-cc13xx/int-master.c b/arch/cpu/cc26x0-cc13x0/int-master.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/int-master.c rename to arch/cpu/cc26x0-cc13x0/int-master.c diff --git a/arch/cpu/cc26xx-cc13xx/lib/cc13xxware b/arch/cpu/cc26x0-cc13x0/lib/cc13xxware similarity index 100% rename from arch/cpu/cc26xx-cc13xx/lib/cc13xxware rename to arch/cpu/cc26x0-cc13x0/lib/cc13xxware diff --git a/arch/cpu/cc26xx-cc13xx/lib/cc2640r2-sdk b/arch/cpu/cc26x0-cc13x0/lib/cc2640r2-sdk similarity index 100% rename from arch/cpu/cc26xx-cc13xx/lib/cc2640r2-sdk rename to arch/cpu/cc26x0-cc13x0/lib/cc2640r2-sdk diff --git a/arch/cpu/cc26xx-cc13xx/lib/cc26xxware b/arch/cpu/cc26x0-cc13x0/lib/cc26xxware similarity index 100% rename from arch/cpu/cc26xx-cc13xx/lib/cc26xxware rename to arch/cpu/cc26x0-cc13x0/lib/cc26xxware diff --git a/arch/cpu/cc26xx-cc13xx/lpm.c b/arch/cpu/cc26x0-cc13x0/lpm.c similarity index 98% rename from arch/cpu/cc26xx-cc13xx/lpm.c rename to arch/cpu/cc26x0-cc13x0/lpm.c index 4c8e0256b..2337fb24f 100644 --- a/arch/cpu/cc26xx-cc13xx/lpm.c +++ b/arch/cpu/cc26x0-cc13x0/lpm.c @@ -162,10 +162,10 @@ lpm_shutdown(uint32_t wakeup_pin, uint32_t io_pull, uint32_t wake_on) ti_lib_aon_wuc_mcu_power_off_config(MCU_VIRT_PWOFF_DISABLE); /* Latch the IOs in the padring and enable I/O pad sleep mode */ -#if !defined(ThisLibraryIsFor_CC26x0R2_HaltIfViolated) - ti_lib_pwr_ctrl_io_freeze_enable(); + ti_lib_aon_ioc_freeze_enable(); + HWREG(AON_SYSCTL_BASE + AON_SYSCTL_O_SLEEPCTL) = 0; + ti_lib_sys_ctrl_aon_sync(); -#endif /* Turn off VIMS cache, CRAM and TRAM - possibly not required */ ti_lib_prcm_cache_retention_disable(); ti_lib_vims_mode_set(VIMS_BASE, VIMS_MODE_OFF); @@ -193,12 +193,7 @@ wake_up(void) ti_lib_sys_ctrl_aon_sync(); /* Adjust recharge settings */ -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated - // May need to change to XOSC_IN_LOW_POWER_MODE - ti_lib_sys_ctrl_adjust_recharge_after_power_down(XOSC_IN_HIGH_POWER_MODE); -#else ti_lib_sys_ctrl_adjust_recharge_after_power_down(); -#endif /* * Release the request to the uLDO diff --git a/arch/cpu/cc26xx-cc13xx/lpm.h b/arch/cpu/cc26x0-cc13x0/lpm.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/lpm.h rename to arch/cpu/cc26x0-cc13x0/lpm.h diff --git a/arch/cpu/cc26xx-cc13xx/random.c b/arch/cpu/cc26x0-cc13x0/random.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/random.c rename to arch/cpu/cc26x0-cc13x0/random.c diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/api/ieee_cmd.h b/arch/cpu/cc26x0-cc13x0/rf-core/api/ieee_cmd.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/api/ieee_cmd.h rename to arch/cpu/cc26x0-cc13x0/rf-core/api/ieee_cmd.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/api/ieee_mailbox.h b/arch/cpu/cc26x0-cc13x0/rf-core/api/ieee_mailbox.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/api/ieee_mailbox.h rename to arch/cpu/cc26x0-cc13x0/rf-core/api/ieee_mailbox.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/ble-cc2650.c b/arch/cpu/cc26x0-cc13x0/rf-core/ble-cc2650.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/ble-cc2650.c rename to arch/cpu/cc26x0-cc13x0/rf-core/ble-cc2650.c diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/ble-hal-cc26xx.c b/arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/ble-hal-cc26xx.c similarity index 99% rename from arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/ble-hal-cc26xx.c rename to arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/ble-hal-cc26xx.c index a9b949d93..3245b9c4f 100644 --- a/arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/ble-hal-cc26xx.c +++ b/arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/ble-hal-cc26xx.c @@ -386,7 +386,7 @@ on(void) rf_core_power_down(); return RF_CORE_CMD_ERROR; } - rf_core_setup_interrupts(0); + rf_core_setup_interrupts(); oscillators_switch_to_hf_xosc(); if(rf_ble_cmd_setup_ble_mode() != RF_BLE_CMD_OK) { diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/ble-hal-cc26xx.h b/arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/ble-hal-cc26xx.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/ble-hal-cc26xx.h rename to arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/ble-hal-cc26xx.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/rf-ble-cmd.c b/arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/rf-ble-cmd.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/rf-ble-cmd.c rename to arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/rf-ble-cmd.c diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/rf-ble-cmd.h b/arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/rf-ble-cmd.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/rf-ble-cmd.h rename to arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/rf-ble-cmd.h diff --git a/arch/cpu/cc26x0-cc13x0/rf-core/cc13xx-50kbps-tsch.c b/arch/cpu/cc26x0-cc13x0/rf-core/cc13xx-50kbps-tsch.c new file mode 100644 index 000000000..af76d63e3 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/rf-core/cc13xx-50kbps-tsch.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * IEEE 802.15.4 TSCH timeslot timings for CC13xx chips at 50kbps datarate + * \author + * Atis Elsts + * + */ + +#include "contiki.h" +#include "net/mac/tsch/tsch.h" + +#define CC13XX_TSCH_DEFAULT_TS_CCA_OFFSET 1800 +#define CC13XX_TSCH_DEFAULT_TS_CCA 128 +#define CC13XX_TSCH_DEFAULT_TS_TX_OFFSET 2500 +#define CC13XX_TSCH_DEFAULT_TS_RX_OFFSET (CC13XX_TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) +#define CC13XX_TSCH_DEFAULT_TS_RX_ACK_DELAY 2000 +#define CC13XX_TSCH_DEFAULT_TS_TX_ACK_DELAY 3000 +#define CC13XX_TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT +#define CC13XX_TSCH_DEFAULT_TS_ACK_WAIT 3000 +#define CC13XX_TSCH_DEFAULT_TS_RX_TX 192 +#define CC13XX_TSCH_DEFAULT_TS_MAX_ACK 10000 +#define CC13XX_TSCH_DEFAULT_TS_MAX_TX 21600 + +/* Timeslot length: 40000 usec */ +#define CC13XX_TSCH_DEFAULT_TS_TIMESLOT_LENGTH 40000 + +/* TSCH timeslot timing (microseconds) */ +const uint16_t tsch_timing_cc13xx_50kbps[tsch_ts_elements_count] = { + CC13XX_TSCH_DEFAULT_TS_CCA_OFFSET, + CC13XX_TSCH_DEFAULT_TS_CCA, + CC13XX_TSCH_DEFAULT_TS_TX_OFFSET, + CC13XX_TSCH_DEFAULT_TS_RX_OFFSET, + CC13XX_TSCH_DEFAULT_TS_RX_ACK_DELAY, + CC13XX_TSCH_DEFAULT_TS_TX_ACK_DELAY, + CC13XX_TSCH_DEFAULT_TS_RX_WAIT, + CC13XX_TSCH_DEFAULT_TS_ACK_WAIT, + CC13XX_TSCH_DEFAULT_TS_RX_TX, + CC13XX_TSCH_DEFAULT_TS_MAX_ACK, + CC13XX_TSCH_DEFAULT_TS_MAX_TX, + CC13XX_TSCH_DEFAULT_TS_TIMESLOT_LENGTH, +}; diff --git a/arch/cpu/cc26x0-cc13x0/rf-core/cc13xx-50kbps-tsch.h b/arch/cpu/cc26x0-cc13x0/rf-core/cc13xx-50kbps-tsch.h new file mode 100644 index 000000000..5d4fdb376 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/rf-core/cc13xx-50kbps-tsch.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#ifndef CC13XX_50KBPS_TSCH_H_ +#define CC13XX_50KBPS_TSCH_H_ + +#include "contiki.h" + +/* TSCH timeslot timing (microseconds) */ +extern const uint16_t tsch_timing_cc13xx_50kbps[]; + +#endif /* CC13XX_50KBPS_TSCH_H_ */ diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/dot-15-4g.h b/arch/cpu/cc26x0-cc13x0/rf-core/dot-15-4g.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/dot-15-4g.h rename to arch/cpu/cc26x0-cc13x0/rf-core/dot-15-4g.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c b/arch/cpu/cc26x0-cc13x0/rf-core/ieee-mode.c similarity index 82% rename from arch/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c rename to arch/cpu/cc26x0-cc13x0/rf-core/ieee-mode.c index 3516147b5..9f4021d21 100644 --- a/arch/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c +++ b/arch/cpu/cc26x0-cc13x0/rf-core/ieee-mode.c @@ -120,6 +120,8 @@ static uint8_t rf_stats[16] = { 0 }; /* The size of the RF commands buffer */ #define RF_CMD_BUFFER_SIZE 128 /*---------------------------------------------------------------------------*/ +#define RAT_TIMESTAMP_OFFSET_2_4_GHZ 0 +/*---------------------------------------------------------------------------*/ /** * \brief Returns the current status of a running Radio Op command * \param a A pointer with the buffer used to initiate the command @@ -130,55 +132,9 @@ static uint8_t rf_stats[16] = { 0 }; */ #define RF_RADIO_OP_GET_STATUS(a) (((rfc_radioOp_t *)a)->status) /*---------------------------------------------------------------------------*/ -/* Special value returned by CMD_IEEE_CCA_REQ when an RSSI is not available */ -#define RF_CMD_CCA_REQ_RSSI_UNKNOWN -128 - -/* Used for the return value of channel_clear */ -#define RF_CCA_CLEAR 1 -#define RF_CCA_BUSY 0 - -/* Used as an error return value for get_cca_info */ -#define RF_GET_CCA_INFO_ERROR 0xFF - -/* - * Values of the individual bits of the ccaInfo field in CMD_IEEE_CCA_REQ's - * status struct - */ -#define RF_CMD_CCA_REQ_CCA_STATE_IDLE 0 /* 00 */ -#define RF_CMD_CCA_REQ_CCA_STATE_BUSY 1 /* 01 */ -#define RF_CMD_CCA_REQ_CCA_STATE_INVALID 2 /* 10 */ - -#define RF_CMD_CCA_REQ_CCA_CORR_IDLE (0 << 4) -#define RF_CMD_CCA_REQ_CCA_CORR_BUSY (1 << 4) -#define RF_CMD_CCA_REQ_CCA_CORR_INVALID (3 << 4) -#define RF_CMD_CCA_REQ_CCA_CORR_MASK (3 << 4) - -#define RF_CMD_CCA_REQ_CCA_SYNC_BUSY (1 << 6) -/*---------------------------------------------------------------------------*/ #define IEEE_MODE_CHANNEL_MIN 11 #define IEEE_MODE_CHANNEL_MAX 26 /*---------------------------------------------------------------------------*/ -/* How long to wait for an ongoing ACK TX to finish before starting frame TX */ -#define TX_WAIT_TIMEOUT (RTIMER_SECOND >> 11) - -/* How long to wait for the RF to enter RX in rf_cmd_ieee_rx */ -#define ENTER_RX_WAIT_TIMEOUT (RTIMER_SECOND >> 10) - -/* How long to wait for the RF to react on CMD_ABORT: around 1 msec */ -#define RF_TURN_OFF_WAIT_TIMEOUT (RTIMER_SECOND >> 10) - -/* How long to wait for the RF to finish TX of a packet or an ACK */ -#define TX_FINISH_WAIT_TIMEOUT (RTIMER_SECOND >> 7) - -#define LIMITED_BUSYWAIT(cond, timeout) do { \ - rtimer_clock_t end_time = RTIMER_NOW() + timeout; \ - while(cond) { \ - if(!RTIMER_CLOCK_LT(RTIMER_NOW(), end_time)) { \ - break; \ - } \ - } \ - } while(0) -/*---------------------------------------------------------------------------*/ /* TX Power dBm lookup table - values from SmartRF Studio */ typedef struct output_config { radio_value_t dbm; @@ -211,33 +167,6 @@ static const output_config_t output_power[] = { /* Default TX Power - position in output_power[] */ static const output_config_t *tx_power_current = &output_power[0]; /*---------------------------------------------------------------------------*/ -static volatile int8_t last_rssi = 0; -static volatile uint8_t last_corr_lqi = 0; - -extern int32_t rat_offset; - -/*---------------------------------------------------------------------------*/ -/* SFD timestamp in RTIMER ticks */ -static volatile uint32_t last_packet_timestamp = 0; -/* SFD timestamp in RAT ticks (but 64 bits) */ -static uint64_t last_rat_timestamp64 = 0; - -/* For RAT overflow handling */ -static struct ctimer rat_overflow_timer; -static volatile uint32_t rat_overflow_counter = 0; -static rtimer_clock_t last_rat_overflow = 0; - -/* RAT has 32-bit register, overflows once 18 minutes */ -#define RAT_RANGE 4294967296ull -/* approximate value */ -#define RAT_OVERFLOW_PERIOD_SECONDS (60 * 18) - -/* XXX: don't know what exactly is this, looks like the time to Tx 3 octets */ -#define TIMESTAMP_OFFSET -(USEC_TO_RADIO(32 * 3) - 1) /* -95.75 usec */ -/*---------------------------------------------------------------------------*/ -/* Are we currently in poll mode? */ -static uint8_t poll_mode = 0; - static rfc_CMD_IEEE_MOD_FILT_t filter_cmd; /*---------------------------------------------------------------------------*/ /* @@ -256,27 +185,28 @@ static uint8_t cmd_ieee_rx_buf[RF_CMD_BUFFER_SIZE] CC_ALIGN(4); #define DATA_ENTRY_LENSZ_BYTE 1 #define DATA_ENTRY_LENSZ_WORD 2 /* 2 bytes */ -#define RX_BUF_SIZE 144 -/* Four receive buffers entries with room for 1 IEEE802.15.4 frame in each */ -static uint8_t rx_buf_0[RX_BUF_SIZE] CC_ALIGN(4); -static uint8_t rx_buf_1[RX_BUF_SIZE] CC_ALIGN(4); -static uint8_t rx_buf_2[RX_BUF_SIZE] CC_ALIGN(4); -static uint8_t rx_buf_3[RX_BUF_SIZE] CC_ALIGN(4); - -#define RX_BUF_INCLUDE_CRC 1 -#define RX_BUF_INCLUDE_RSSI 1 -#define RX_BUF_INCLUDE_CORR 1 -#define RX_BUF_INCLUDE_TIMESTAMP 1 - /* The size of the metadata (excluding the packet length field) */ #define RX_BUF_METADATA_SIZE \ - (2 * RX_BUF_INCLUDE_CRC + RX_BUF_INCLUDE_RSSI + RX_BUF_INCLUDE_CORR + 4 * RX_BUF_INCLUDE_TIMESTAMP) + (2 * RF_CORE_RX_BUF_INCLUDE_CRC \ + + RF_CORE_RX_BUF_INCLUDE_RSSI \ + + RF_CORE_RX_BUF_INCLUDE_CORR \ + + 4 * RF_CORE_RX_BUF_INCLUDE_TIMESTAMP) /* The offset of the packet length in a rx buffer */ #define RX_BUF_LENGTH_OFFSET sizeof(rfc_dataEntry_t) /* The offset of the packet data in a rx buffer */ #define RX_BUF_DATA_OFFSET (RX_BUF_LENGTH_OFFSET + 1) +#define RX_BUF_SIZE (RX_BUF_DATA_OFFSET \ + + NETSTACK_RADIO_MAX_PAYLOAD_LEN \ + + RX_BUF_METADATA_SIZE) + +/* Four receive buffers entries with room for 1 IEEE802.15.4 frame in each */ +static uint8_t rx_buf_0[RX_BUF_SIZE] CC_ALIGN(4); +static uint8_t rx_buf_1[RX_BUF_SIZE] CC_ALIGN(4); +static uint8_t rx_buf_2[RX_BUF_SIZE] CC_ALIGN(4); +static uint8_t rx_buf_3[RX_BUF_SIZE] CC_ALIGN(4); + /* The RX Data Queue */ static dataQueue_t rx_data_queue = { 0 }; @@ -358,8 +288,8 @@ transmitting(void) return 0; } - if((cmd.currentRssi == RF_CMD_CCA_REQ_RSSI_UNKNOWN) && - (cmd.ccaInfo.ccaEnergy == RF_CMD_CCA_REQ_CCA_STATE_BUSY)) { + if((cmd.currentRssi == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN) && + (cmd.ccaInfo.ccaEnergy == RF_CORE_CMD_CCA_REQ_CCA_STATE_BUSY)) { return 1; } @@ -368,12 +298,12 @@ transmitting(void) /*---------------------------------------------------------------------------*/ /** * \brief Returns CCA information - * \return RF_GET_CCA_INFO_ERROR if the RF was not on + * \return RF_CORE_GET_CCA_INFO_ERROR if the RF was not on * \return On success, the return value is formatted as per the ccaInfo field * of CMD_IEEE_CCA_REQ * * It is the caller's responsibility to make sure the RF is on. This function - * will return RF_GET_CCA_INFO_ERROR if the RF is off + * will return RF_CORE_GET_CCA_INFO_ERROR if the RF is off * * This function will in fact wait for a valid CCA state */ @@ -385,20 +315,20 @@ get_cca_info(void) if(!rf_is_on()) { PRINTF("get_cca_info: Not on\n"); - return RF_GET_CCA_INFO_ERROR; + return RF_CORE_GET_CCA_INFO_ERROR; } memset(&cmd, 0x00, sizeof(cmd)); - cmd.ccaInfo.ccaState = RF_CMD_CCA_REQ_CCA_STATE_INVALID; + cmd.ccaInfo.ccaState = RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID; - while(cmd.ccaInfo.ccaState == RF_CMD_CCA_REQ_CCA_STATE_INVALID) { + while(cmd.ccaInfo.ccaState == RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID) { memset(&cmd, 0x00, sizeof(cmd)); cmd.commandNo = CMD_IEEE_CCA_REQ; if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) { PRINTF("get_cca_info: CMDSTA=0x%08lx\n", cmd_status); - return RF_GET_CCA_INFO_ERROR; + return RF_CORE_GET_CCA_INFO_ERROR; } } @@ -425,14 +355,14 @@ get_rssi(void) was_off = 1; if(on() != RF_CORE_CMD_OK) { PRINTF("get_rssi: on() failed\n"); - return RF_CMD_CCA_REQ_RSSI_UNKNOWN; + return RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN; } } memset(&cmd, 0x00, sizeof(cmd)); - cmd.ccaInfo.ccaEnergy = RF_CMD_CCA_REQ_CCA_STATE_INVALID; + cmd.ccaInfo.ccaEnergy = RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID; - while(cmd.ccaInfo.ccaEnergy == RF_CMD_CCA_REQ_CCA_STATE_INVALID) { + while(cmd.ccaInfo.ccaEnergy == RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID) { memset(&cmd, 0x00, sizeof(cmd)); cmd.commandNo = CMD_IEEE_CCA_REQ; @@ -440,7 +370,7 @@ get_rssi(void) PRINTF("get_rssi: CMDSTA=0x%08lx\n", cmd_status); /* Make sure to return RSSI unknown */ - cmd.currentRssi = RF_CMD_CCA_REQ_RSSI_UNKNOWN; + cmd.currentRssi = RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN; break; } } @@ -558,8 +488,8 @@ rf_cmd_ieee_rx() return RF_CORE_CMD_ERROR; } - LIMITED_BUSYWAIT(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) != RF_CORE_RADIO_OP_STATUS_ACTIVE, - ENTER_RX_WAIT_TIMEOUT); + RTIMER_BUSYWAIT_UNTIL(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == RF_CORE_RADIO_OP_STATUS_ACTIVE, + RF_CORE_ENTER_RX_TIMEOUT); /* Wait to enter RX */ if(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) != RF_CORE_RADIO_OP_STATUS_ACTIVE) { @@ -615,11 +545,11 @@ init_rf_params(void) cmd->rxConfig.bAutoFlushCrc = 1; cmd->rxConfig.bAutoFlushIgn = 0; cmd->rxConfig.bIncludePhyHdr = 0; - cmd->rxConfig.bIncludeCrc = RX_BUF_INCLUDE_CRC; - cmd->rxConfig.bAppendRssi = RX_BUF_INCLUDE_RSSI; - cmd->rxConfig.bAppendCorrCrc = RX_BUF_INCLUDE_CORR; + cmd->rxConfig.bIncludeCrc = RF_CORE_RX_BUF_INCLUDE_CRC; + cmd->rxConfig.bAppendRssi = RF_CORE_RX_BUF_INCLUDE_RSSI; + cmd->rxConfig.bAppendCorrCrc = RF_CORE_RX_BUF_INCLUDE_CORR; cmd->rxConfig.bAppendSrcInd = 0; - cmd->rxConfig.bAppendTimestamp = RX_BUF_INCLUDE_TIMESTAMP; + cmd->rxConfig.bAppendTimestamp = RF_CORE_RX_BUF_INCLUDE_TIMESTAMP; cmd->pRxQ = &rx_data_queue; cmd->pOutput = (rfc_ieeeRxOutput_t *)rf_stats; @@ -714,7 +644,7 @@ rx_off(void) } /* Wait for ongoing ACK TX to finish */ - LIMITED_BUSYWAIT(transmitting(), TX_FINISH_WAIT_TIMEOUT); + RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT); /* Send a CMD_ABORT command to RF Core */ if(rf_core_send_cmd(CMDR_DIR_CMD(CMD_ABORT), &cmd_status) != RF_CORE_CMD_OK) { @@ -722,7 +652,7 @@ rx_off(void) /* Continue nonetheless */ } - LIMITED_BUSYWAIT(rf_is_on(), RF_TURN_OFF_WAIT_TIMEOUT); + RTIMER_BUSYWAIT_UNTIL(!rf_is_on(), RF_CORE_TURN_OFF_TIMEOUT); if(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == IEEE_DONE_STOPPED || RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == IEEE_DONE_ABORT) { @@ -773,8 +703,8 @@ soft_off(void) return; } - LIMITED_BUSYWAIT((cmd->status & RF_CORE_RADIO_OP_MASKED_STATUS) == - RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING, RF_TURN_OFF_WAIT_TIMEOUT); + RTIMER_BUSYWAIT_UNTIL((cmd->status & RF_CORE_RADIO_OP_MASKED_STATUS) != + RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING, RF_CORE_TURN_OFF_TIMEOUT); } /*---------------------------------------------------------------------------*/ static uint8_t @@ -791,71 +721,10 @@ soft_on(void) static const rf_core_primary_mode_t mode_ieee = { soft_off, soft_on, + rf_is_on, + RAT_TIMESTAMP_OFFSET_2_4_GHZ }; /*---------------------------------------------------------------------------*/ -static uint8_t -check_rat_overflow(bool first_time) -{ - static uint32_t last_value; - uint32_t current_value; - uint8_t interrupts_disabled; - - /* Bail out if the RF is not on */ - if(!rf_is_on()) { - return 0; - } - - interrupts_disabled = ti_lib_int_master_disable(); - if(first_time) { - last_value = HWREG(RFC_RAT_BASE + RATCNT); - } else { - current_value = HWREG(RFC_RAT_BASE + RATCNT); - if(current_value + RAT_RANGE / 4 < last_value) { - /* Overflow detected */ - last_rat_overflow = RTIMER_NOW(); - rat_overflow_counter++; - } - last_value = current_value; - } - if(!interrupts_disabled) { - ti_lib_int_master_enable(); - } - return 1; -} -/*---------------------------------------------------------------------------*/ -static void -handle_rat_overflow(void *unused) -{ - uint8_t success; - uint8_t was_off = 0; - - if(!rf_is_on()) { - was_off = 1; - if(on() != RF_CORE_CMD_OK) { - PRINTF("overflow: on() failed\n"); - ctimer_set(&rat_overflow_timer, CLOCK_SECOND, - handle_rat_overflow, NULL); - return; - } - } - - success = check_rat_overflow(false); - - if(was_off) { - off(); - } - - if(success) { - /* Retry after half of the interval */ - ctimer_set(&rat_overflow_timer, RAT_OVERFLOW_PERIOD_SECONDS * CLOCK_SECOND / 2, - handle_rat_overflow, NULL); - } else { - /* Retry sooner */ - ctimer_set(&rat_overflow_timer, CLOCK_SECOND, - handle_rat_overflow, NULL); - } -} -/*---------------------------------------------------------------------------*/ static int init(void) { @@ -889,9 +758,7 @@ init(void) rf_core_primary_mode_register(&mode_ieee); - check_rat_overflow(true); - ctimer_set(&rat_overflow_timer, RAT_OVERFLOW_PERIOD_SECONDS * CLOCK_SECOND / 2, - handle_rat_overflow, NULL); + rf_core_rat_init(); process_start(&rf_core_process, NULL); return 1; @@ -935,7 +802,7 @@ transmit(unsigned short transmit_len) do { tx_active = transmitting(); } while(tx_active == 1 && - (RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + TX_WAIT_TIMEOUT))); + (RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + RF_CORE_TX_TIMEOUT))); if(tx_active) { PRINTF("transmit: Already TXing and wait timed out\n"); @@ -957,7 +824,7 @@ transmit(unsigned short transmit_len) cmd.startTrigger.triggerType = TRIG_NOW; /* Enable the LAST_FG_COMMAND_DONE interrupt, which will wake us up */ - rf_core_cmd_done_en(true, poll_mode); + rf_core_cmd_done_en(true); ret = rf_core_send_cmd((uint32_t)&cmd, &cmd_status); @@ -973,7 +840,7 @@ transmit(unsigned short transmit_len) * 1) make the `lpm_sleep()` call here unconditional; * 2) change the radio ISR priority to allow radio ISR to interrupt rtimer ISR. */ - if(!poll_mode) { + if(!rf_core_poll_mode) { lpm_sleep(); } } @@ -1007,7 +874,7 @@ transmit(unsigned short transmit_len) * Disable LAST_FG_COMMAND_DONE interrupt. We don't really care about it * except when we are transmitting */ - rf_core_cmd_done_dis(poll_mode); + rf_core_cmd_done_dis(); if(was_off) { off(); @@ -1036,46 +903,6 @@ release_data_entry(void) rx_read_entry = entry->pNextEntry; } /*---------------------------------------------------------------------------*/ -static uint32_t -calc_last_packet_timestamp(uint32_t rat_timestamp) -{ - uint64_t rat_timestamp64; - uint32_t adjusted_overflow_counter; - uint8_t was_off = 0; - - if(!rf_is_on()) { - was_off = 1; - on(); - } - - if(rf_is_on()) { - check_rat_overflow(false); - if(was_off) { - off(); - } - } - - adjusted_overflow_counter = rat_overflow_counter; - - /* if the timestamp is large and the last oveflow was recently, - assume that the timestamp refers to the time before the overflow */ - if(rat_timestamp > (uint32_t)(RAT_RANGE * 3 / 4)) { - if(RTIMER_CLOCK_LT(RTIMER_NOW(), - last_rat_overflow + RAT_OVERFLOW_PERIOD_SECONDS * RTIMER_SECOND / 4)) { - adjusted_overflow_counter--; - } - } - - /* add the overflowed time to the timestamp */ - rat_timestamp64 = rat_timestamp + RAT_RANGE * adjusted_overflow_counter; - /* correct timestamp so that it refers to the end of the SFD */ - rat_timestamp64 += TIMESTAMP_OFFSET; - - last_rat_timestamp64 = rat_timestamp64 - rat_offset; - - return RADIO_TO_RTIMER(rat_timestamp64 - rat_offset); -} -/*---------------------------------------------------------------------------*/ static int read_frame(void *buf, unsigned short buf_len) { @@ -1111,20 +938,20 @@ read_frame(void *buf, unsigned short buf_len) memcpy(buf, (uint8_t *)rx_read_entry + RX_BUF_DATA_OFFSET, len); - last_rssi = (int8_t)rx_read_entry[RX_BUF_DATA_OFFSET + len + 2]; - last_corr_lqi = (uint8_t)rx_read_entry[RX_BUF_DATA_OFFSET + len + 3] & STATUS_CORRELATION; + rf_core_last_rssi = (int8_t)rx_read_entry[RX_BUF_DATA_OFFSET + len]; + rf_core_last_corr_lqi = (uint8_t)rx_read_entry[RX_BUF_DATA_OFFSET + len + 1] & STATUS_CORRELATION; /* get the timestamp */ - memcpy(&rat_timestamp, (uint8_t *)rx_read_entry + RX_BUF_DATA_OFFSET + len + 4, 4); + memcpy(&rat_timestamp, (uint8_t *)rx_read_entry + RX_BUF_DATA_OFFSET + len + 2, 4); - last_packet_timestamp = calc_last_packet_timestamp(rat_timestamp); + rf_core_last_packet_timestamp = rf_core_convert_rat_to_rtimer(rat_timestamp); - if(!poll_mode) { + if(!rf_core_poll_mode) { /* Not in poll mode: packetbuf should not be accessed in interrupt context. * In poll mode, the last packet RSSI and link quality can be obtained through * RADIO_PARAM_LAST_RSSI and RADIO_PARAM_LAST_LINK_QUALITY */ - packetbuf_set_attr(PACKETBUF_ATTR_RSSI, last_rssi); - packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, last_corr_lqi); + packetbuf_set_attr(PACKETBUF_ATTR_RSSI, rf_core_last_rssi); + packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, rf_core_last_corr_lqi); } release_data_entry(); @@ -1137,7 +964,7 @@ channel_clear(void) { uint8_t was_off = 0; uint8_t cca_info; - int ret = RF_CCA_CLEAR; + int ret = RF_CORE_CCA_CLEAR; /* * If we are in the middle of a BLE operation, we got called by ContikiMAC @@ -1145,7 +972,7 @@ channel_clear(void) */ if(rf_ble_is_active() == RF_BLE_ACTIVE) { PRINTF("channel_clear: Interrupt context but BLE in progress\n"); - return RF_CCA_CLEAR; + return RF_CORE_CCA_CLEAR; } if(rf_is_on()) { @@ -1157,7 +984,7 @@ channel_clear(void) * * We could probably even simply return that the channel is clear */ - LIMITED_BUSYWAIT(transmitting(), TX_FINISH_WAIT_TIMEOUT); + RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT); } else { was_off = 1; if(on() != RF_CORE_CMD_OK) { @@ -1165,21 +992,21 @@ channel_clear(void) if(was_off) { off(); } - return RF_CCA_CLEAR; + return RF_CORE_CCA_CLEAR; } } cca_info = get_cca_info(); - if(cca_info == RF_GET_CCA_INFO_ERROR) { + if(cca_info == RF_CORE_GET_CCA_INFO_ERROR) { PRINTF("channel_clear: CCA error\n"); - ret = RF_CCA_CLEAR; + ret = RF_CORE_CCA_CLEAR; } else { /* * cca_info bits 1:0 - ccaStatus * Return 1 (clear) if idle or invalid. */ - ret = (cca_info & 0x03) != RF_CMD_CCA_REQ_CCA_STATE_BUSY; + ret = (cca_info & 0x03) != RF_CORE_CMD_CCA_REQ_CCA_STATE_BUSY; } if(was_off) { @@ -1218,12 +1045,12 @@ receiving_packet(void) cca_info = get_cca_info(); /* If we can't read CCA info, return "not receiving" */ - if(cca_info == RF_GET_CCA_INFO_ERROR) { + if(cca_info == RF_CORE_GET_CCA_INFO_ERROR) { return 0; } /* If sync has been seen, return 1 (receiving) */ - if(cca_info & RF_CMD_CCA_REQ_CCA_SYNC_BUSY) { + if(cca_info & RF_CORE_CMD_CCA_REQ_CCA_SYNC_BUSY) { return 1; } @@ -1241,7 +1068,7 @@ pending_packet(void) if(entry->status == DATA_ENTRY_STATUS_FINISHED || entry->status == DATA_ENTRY_STATUS_BUSY) { rv = 1; - if(!poll_mode) { + if(!rf_core_poll_mode) { process_poll(&rf_core_process); } } @@ -1292,7 +1119,7 @@ on(void) return RF_CORE_CMD_ERROR; } - rf_core_setup_interrupts(poll_mode); + rf_core_setup_interrupts(); if(rf_radio_setup() != RF_CORE_CMD_OK) { PRINTF("on: radio_setup() failed\n"); @@ -1314,7 +1141,7 @@ off(void) return RF_CORE_CMD_OK; } - LIMITED_BUSYWAIT(transmitting(), TX_FINISH_WAIT_TIMEOUT); + RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT); /* stopping the rx explicitly results in lower sleep-mode power usage */ rx_off(); @@ -1394,7 +1221,7 @@ get_value(radio_param_t param, radio_value_t *value) if(cmd->frameFiltOpt.autoAckEn) { *value |= RADIO_RX_MODE_AUTOACK; } - if(poll_mode) { + if(rf_core_poll_mode) { *value |= RADIO_RX_MODE_POLL_MODE; } @@ -1411,7 +1238,7 @@ get_value(radio_param_t param, radio_value_t *value) case RADIO_PARAM_RSSI: *value = get_rssi(); - if(*value == RF_CMD_CCA_REQ_RSSI_UNKNOWN) { + if(*value == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN) { return RADIO_RESULT_ERROR; } else { return RADIO_RESULT_OK; @@ -1429,10 +1256,25 @@ get_value(radio_param_t param, radio_value_t *value) *value = OUTPUT_POWER_MAX; return RADIO_RESULT_OK; case RADIO_PARAM_LAST_RSSI: - *value = last_rssi; + *value = rf_core_last_rssi; return RADIO_RESULT_OK; case RADIO_PARAM_LAST_LINK_QUALITY: - *value = last_corr_lqi; + *value = rf_core_last_corr_lqi; + return RADIO_RESULT_OK; + case RADIO_CONST_PHY_OVERHEAD: + *value = (radio_value_t)RADIO_PHY_OVERHEAD; + return RADIO_RESULT_OK; + case RADIO_CONST_BYTE_AIR_TIME: + *value = (radio_value_t)RADIO_BYTE_AIR_TIME; + return RADIO_RESULT_OK; + case RADIO_CONST_DELAY_BEFORE_TX: + *value = (radio_value_t)RADIO_DELAY_BEFORE_TX; + return RADIO_RESULT_OK; + case RADIO_CONST_DELAY_BEFORE_RX: + *value = (radio_value_t)RADIO_DELAY_BEFORE_RX; + return RADIO_RESULT_OK; + case RADIO_CONST_DELAY_BEFORE_DETECT: + *value = (radio_value_t)RADIO_DELAY_BEFORE_DETECT; return RADIO_RESULT_OK; default: return RADIO_RESULT_NOT_SUPPORTED; @@ -1498,9 +1340,9 @@ set_value(radio_param_t param, radio_value_t value) cmd->frameFiltOpt.bPanCoord = 0; cmd->frameFiltOpt.bStrictLenFilter = 0; - old_poll_mode = poll_mode; - poll_mode = (value & RADIO_RX_MODE_POLL_MODE) != 0; - if(poll_mode == old_poll_mode) { + old_poll_mode = rf_core_poll_mode; + rf_core_poll_mode = (value & RADIO_RX_MODE_POLL_MODE) != 0; + if(rf_core_poll_mode == old_poll_mode) { uint32_t cmd_status; /* do not turn the radio on and off, just send an update command */ @@ -1552,7 +1394,7 @@ set_value(radio_param_t param, radio_value_t value) /* Restart the radio timer (RAT). This causes resynchronization between RAT and RTC: useful for TSCH. */ if(rf_core_restart_rat() == RF_CORE_CMD_OK) { - check_rat_overflow(false); + rf_core_check_rat_overflow(); } if(rx_on() != RF_CORE_CMD_OK) { @@ -1590,7 +1432,7 @@ get_object(radio_param_t param, void *dest, size_t size) if(size != sizeof(rtimer_clock_t) || !dest) { return RADIO_RESULT_INVALID_VALUE; } - *(rtimer_clock_t *)dest = last_packet_timestamp; + *(rtimer_clock_t *)dest = rf_core_last_packet_timestamp; return RADIO_RESULT_OK; } diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/prop-mode-tx-power.c b/arch/cpu/cc26x0-cc13x0/rf-core/prop-mode-tx-power.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/prop-mode-tx-power.c rename to arch/cpu/cc26x0-cc13x0/rf-core/prop-mode-tx-power.c diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/prop-mode.c b/arch/cpu/cc26x0-cc13x0/rf-core/prop-mode.c similarity index 78% rename from arch/cpu/cc26xx-cc13xx/rf-core/prop-mode.c rename to arch/cpu/cc26x0-cc13x0/rf-core/prop-mode.c index 66b4a4370..0ce6de5dd 100644 --- a/arch/cpu/cc26xx-cc13xx/rf-core/prop-mode.c +++ b/arch/cpu/cc26x0-cc13x0/rf-core/prop-mode.c @@ -115,24 +115,6 @@ */ #define RF_RADIO_OP_GET_STATUS(a) GET_FIELD_V(a, radioOp, status) /*---------------------------------------------------------------------------*/ -/* Special value returned by CMD_IEEE_CCA_REQ when an RSSI is not available */ -#define RF_CMD_CCA_REQ_RSSI_UNKNOWN -128 - -/* Used for the return value of channel_clear */ -#define RF_CCA_CLEAR 1 -#define RF_CCA_BUSY 0 - -/* Used as an error return value for get_cca_info */ -#define RF_GET_CCA_INFO_ERROR 0xFF - -/* - * Values of the individual bits of the ccaInfo field in CMD_IEEE_CCA_REQ's - * status struct - */ -#define RF_CMD_CCA_REQ_CCA_STATE_IDLE 0 /* 00 */ -#define RF_CMD_CCA_REQ_CCA_STATE_BUSY 1 /* 01 */ -#define RF_CMD_CCA_REQ_CCA_STATE_INVALID 2 /* 10 */ - #ifdef PROP_MODE_CONF_RSSI_THRESHOLD #define PROP_MODE_RSSI_THRESHOLD PROP_MODE_CONF_RSSI_THRESHOLD #else @@ -141,6 +123,8 @@ static int8_t rssi_threshold = PROP_MODE_RSSI_THRESHOLD; /*---------------------------------------------------------------------------*/ +static volatile uint8_t is_receiving_packet; +/*---------------------------------------------------------------------------*/ static int on(void); static int off(void); @@ -170,12 +154,6 @@ static rfc_propRxOutput_t rx_stats; #define DOT_4G_PHR_DW_BIT 0 #endif /*---------------------------------------------------------------------------*/ -/* How long to wait for an ongoing ACK TX to finish before starting frame TX */ -#define TX_WAIT_TIMEOUT (RTIMER_SECOND >> 11) - -/* How long to wait for the RF to enter RX in rf_cmd_ieee_rx */ -#define ENTER_RX_WAIT_TIMEOUT (RTIMER_SECOND >> 10) -/*---------------------------------------------------------------------------*/ /* TX power table for the 431-527MHz band */ #ifdef PROP_MODE_CONF_TX_POWER_431_527 #define PROP_MODE_TX_POWER_431_527 PROP_MODE_CONF_TX_POWER_431_527 @@ -222,12 +200,29 @@ static const prop_mode_tx_power_config_t *tx_power_current = &TX_POWER_DRIVER[1] #define DATA_ENTRY_LENSZ_BYTE 1 #define DATA_ENTRY_LENSZ_WORD 2 /* 2 bytes */ +/* The size of the metadata (excluding the packet length field) */ +#define RX_BUF_METADATA_SIZE \ + (CRC_LEN * RF_CORE_RX_BUF_INCLUDE_CRC \ + + RF_CORE_RX_BUF_INCLUDE_RSSI \ + + RF_CORE_RX_BUF_INCLUDE_CORR \ + + 4 * RF_CORE_RX_BUF_INCLUDE_TIMESTAMP) + +/* The offset of the packet length in a rx buffer */ +#define RX_BUF_LENGTH_OFFSET sizeof(rfc_dataEntry_t) +/* The offset of the packet data in a rx buffer */ +#define RX_BUF_DATA_OFFSET (RX_BUF_LENGTH_OFFSET + DOT_4G_PHR_LEN) + +#define ALIGN_TO_4(size) (((size) + 3) & ~3) + +#define RX_BUF_SIZE ALIGN_TO_4(RX_BUF_DATA_OFFSET \ + + NETSTACK_RADIO_MAX_PAYLOAD_LEN \ + + RX_BUF_METADATA_SIZE) + /* * RX buffers. * PROP_MODE_RX_BUF_CNT buffers of RX_BUF_SIZE bytes each. The start of each * buffer must be 4-byte aligned, therefore RX_BUF_SIZE must divide by 4 */ -#define RX_BUF_SIZE 140 static uint8_t rx_buf[PROP_MODE_RX_BUF_CNT][RX_BUF_SIZE] CC_ALIGN(4); /* The RX Data Queue */ @@ -236,6 +231,12 @@ static dataQueue_t rx_data_queue = { 0 }; /* Receive entry pointer to keep track of read items */ volatile static uint8_t *rx_read_entry; /*---------------------------------------------------------------------------*/ +/* + * Increasing this number causes unicast Tx immediately after broadcast Rx to have + * negative synchronization errors ("dr" in TSCH logs); decreasing it: the opposite. + */ +#define RAT_TIMESTAMP_OFFSET_SUB_GHZ USEC_TO_RADIO(160 * 6 - 240) +/*---------------------------------------------------------------------------*/ /* The outgoing frame buffer */ #define TX_BUF_PAYLOAD_LEN 180 #define TX_BUF_HDR_LEN 2 @@ -272,13 +273,13 @@ get_rssi(void) was_off = 1; if(on() != RF_CORE_CMD_OK) { PRINTF("get_rssi: on() failed\n"); - return RF_CMD_CCA_REQ_RSSI_UNKNOWN; + return RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN; } } - rssi = RF_CMD_CCA_REQ_RSSI_UNKNOWN; + rssi = RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN; - while((rssi == RF_CMD_CCA_REQ_RSSI_UNKNOWN || rssi == 0) && ++attempts < 10) { + while((rssi == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN || rssi == 0) && ++attempts < 10) { memset(&cmd, 0x00, sizeof(cmd)); cmd.commandNo = CMD_GET_RSSI; @@ -420,13 +421,17 @@ static uint8_t rf_cmd_prop_rx() { uint32_t cmd_status; - rtimer_clock_t t0; volatile rfc_CMD_PROP_RX_ADV_t *cmd_rx_adv; int ret; cmd_rx_adv = (rfc_CMD_PROP_RX_ADV_t *)&smartrf_settings_cmd_prop_rx_adv; cmd_rx_adv->status = RF_CORE_RADIO_OP_STATUS_IDLE; + cmd_rx_adv->rxConf.bIncludeCrc = RF_CORE_RX_BUF_INCLUDE_CRC; + cmd_rx_adv->rxConf.bAppendRssi = RF_CORE_RX_BUF_INCLUDE_RSSI; + cmd_rx_adv->rxConf.bAppendTimestamp = RF_CORE_RX_BUF_INCLUDE_TIMESTAMP; + cmd_rx_adv->rxConf.bAppendStatus = RF_CORE_RX_BUF_INCLUDE_CORR; + /* * Set the max Packet length. This is for the payload only, therefore * 2047 - length offset @@ -441,10 +446,8 @@ rf_cmd_prop_rx() return RF_CORE_CMD_ERROR; } - t0 = RTIMER_NOW(); - - while(cmd_rx_adv->status != RF_CORE_RADIO_OP_STATUS_ACTIVE && - (RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + ENTER_RX_WAIT_TIMEOUT))); + RTIMER_BUSYWAIT_UNTIL(cmd_rx_adv->status == RF_CORE_RADIO_OP_STATUS_ACTIVE, + RF_CORE_ENTER_RX_TIMEOUT); /* Wait to enter RX */ if(cmd_rx_adv->status != RF_CORE_RADIO_OP_STATUS_ACTIVE) { @@ -506,13 +509,16 @@ rx_off_prop(void) return RF_CORE_CMD_OK; } + /* Wait for ongoing ACK TX to finish */ + RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT); + /* Send a CMD_ABORT command to RF Core */ if(rf_core_send_cmd(CMDR_DIR_CMD(CMD_ABORT), &cmd_status) != RF_CORE_CMD_OK) { PRINTF("rx_off_prop: CMD_ABORT status=0x%08lx\n", cmd_status); /* Continue nonetheless */ } - while(rf_is_on()); + RTIMER_BUSYWAIT_UNTIL(!rf_is_on(), RF_CORE_TURN_OFF_TIMEOUT); if(smartrf_settings_cmd_prop_rx_adv.status == PROP_DONE_STOPPED || smartrf_settings_cmd_prop_rx_adv.status == PROP_DONE_ABORT) { @@ -583,8 +589,8 @@ soft_off_prop(void) return; } - while((cmd->status & RF_CORE_RADIO_OP_MASKED_STATUS) == - RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING); + RTIMER_BUSYWAIT_UNTIL((cmd->status & RF_CORE_RADIO_OP_MASKED_STATUS) != + RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING, RF_CORE_TURN_OFF_TIMEOUT); } /*---------------------------------------------------------------------------*/ static uint8_t @@ -606,6 +612,8 @@ soft_on_prop(void) static const rf_core_primary_mode_t mode_prop = { soft_off_prop, soft_on_prop, + rf_is_on, + RAT_TIMESTAMP_OFFSET_SUB_GHZ }; /*---------------------------------------------------------------------------*/ static int @@ -637,10 +645,15 @@ init(void) return RF_CORE_CMD_ERROR; } + /* Enable the "sync word seen" interrupt */ + ti_lib_rfc_hw_int_enable(RFC_DBELL_RFHWIEN_MDMSOFT); + ENERGEST_ON(ENERGEST_TYPE_LISTEN); rf_core_primary_mode_register(&mode_prop); + rf_core_rat_init(); + process_start(&rf_core_process, NULL); return 1; @@ -701,7 +714,7 @@ transmit(unsigned short transmit_len) rx_off_prop(); /* Enable the LAST_COMMAND_DONE interrupt to wake us up */ - rf_core_cmd_done_en(false, false); + rf_core_cmd_done_en(false); ret = rf_core_send_cmd((uint32_t)cmd_tx_adv, &cmd_status); @@ -714,7 +727,14 @@ transmit(unsigned short transmit_len) /* Idle away while the command is running */ while((cmd_tx_adv->status & RF_CORE_RADIO_OP_MASKED_STATUS) == RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING) { - lpm_sleep(); + /* Note: for now sleeping while Tx'ing in polling mode is disabled. + * To enable it: + * 1) make the `lpm_sleep()` call here unconditional; + * 2) change the radio ISR priority to allow radio ISR to interrupt rtimer ISR. + */ + if(!rf_core_poll_mode) { + lpm_sleep(); + } } if(cmd_tx_adv->status == RF_CORE_RADIO_OP_STATUS_PROP_DONE_OK) { @@ -743,7 +763,7 @@ transmit(unsigned short transmit_len) * Disable LAST_FG_COMMAND_DONE interrupt. We don't really care about it * except when we are transmitting */ - rf_core_cmd_done_dis(false); + rf_core_cmd_done_dis(); /* Workaround. Set status to IDLE */ cmd_tx_adv->status = RF_CORE_RADIO_OP_STATUS_IDLE; @@ -764,47 +784,98 @@ send(const void *payload, unsigned short payload_len) return transmit(payload_len); } /*---------------------------------------------------------------------------*/ -static int -read_frame(void *buf, unsigned short buf_len) +static void +release_data_entry(void) { - int_master_status_t status; rfc_dataEntryGeneral_t *entry = (rfc_dataEntryGeneral_t *)rx_read_entry; uint8_t *data_ptr = &entry->data; - int len = 0; + int_master_status_t interrupt_status; - if(entry->status == DATA_ENTRY_STATUS_FINISHED) { + /* Clear the length field (2 bytes) */ + data_ptr[0] = 0; + data_ptr[1] = 0; - /* - * First 2 bytes in the data entry are the length. - * Our data entry consists of: Payload + RSSI (1 byte) + Status (1 byte) - * This length includes all of those. - */ - len = (*(uint16_t *)data_ptr); - data_ptr += 2; - len -= 2; + /* Set status to 0 "Pending" in element */ + entry->status = DATA_ENTRY_STATUS_PENDING; + rx_read_entry = entry->pNextEntry; - if(len > 0) { - if(len <= buf_len) { - memcpy(buf, data_ptr, len); - } - - packetbuf_set_attr(PACKETBUF_ATTR_RSSI, (int8_t)data_ptr[len]); - packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, 0x7F); - } - - /* Move read entry pointer to next entry */ - rx_read_entry = entry->pNextEntry; - entry->status = DATA_ENTRY_STATUS_PENDING; - } - - status = critical_enter(); - if(rx_is_full) { - rx_is_full = false; + interrupt_status = critical_enter(); + if(rf_core_rx_is_full) { + rf_core_rx_is_full = false; PRINTF("RXQ was full, re-enabling radio!\n"); rx_on_prop(); } - critical_exit(status); - + critical_exit(interrupt_status); + +} +/*---------------------------------------------------------------------------*/ +static int +read_frame(void *buf, unsigned short buf_len) +{ + rfc_dataEntryGeneral_t *entry = (rfc_dataEntryGeneral_t *)rx_read_entry; + uint8_t *data_ptr = &entry->data; + int len = 0; + uint32_t rat_timestamp; + + /* wait for entry to become finished */ + rtimer_clock_t t0 = RTIMER_NOW(); + while(entry->status == DATA_ENTRY_STATUS_BUSY + && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (RTIMER_SECOND / 50))); + + /* Make sure the flag is reset */ + is_receiving_packet = 0; + + if(entry->status != DATA_ENTRY_STATUS_FINISHED) { + /* No available data */ + return 0; + } + + /* + * First 2 bytes in the data entry are the length. + * Our data entry consists of: + * Payload + RSSI (1 byte) + Timestamp (4 bytes) + Status (1 byte) + * This length includes all of those. + */ + len = (*(uint16_t *)data_ptr); + + if(len <= RX_BUF_METADATA_SIZE) { + PRINTF("RF: too short!"); + + release_data_entry(); + return 0; + } + + data_ptr += 2; + len -= RX_BUF_METADATA_SIZE; + + if(len > buf_len) { + PRINTF("RF: too long\n"); + + release_data_entry(); + return 0; + } + + memcpy(buf, data_ptr, len); + + /* get the RSSI and status */ + rf_core_last_rssi = (int8_t)data_ptr[len]; + rf_core_last_corr_lqi = data_ptr[len + 5]; + + /* get the timestamp */ + memcpy(&rat_timestamp, data_ptr + len + 1, 4); + + rf_core_last_packet_timestamp = rf_core_convert_rat_to_rtimer(rat_timestamp); + + if(!rf_core_poll_mode) { + /* Not in poll mode: packetbuf should not be accessed in interrupt context. + * In poll mode, the last packet RSSI and link quality can be obtained through + * RADIO_PARAM_LAST_RSSI and RADIO_PARAM_LAST_LINK_QUALITY */ + packetbuf_set_attr(PACKETBUF_ATTR_RSSI, rf_core_last_rssi); + packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, rf_core_last_corr_lqi); + } + + release_data_entry(); + return len; } /*---------------------------------------------------------------------------*/ @@ -813,14 +884,14 @@ channel_clear(void) { uint8_t was_off = 0; uint32_t cmd_status; - int8_t rssi = RF_CMD_CCA_REQ_RSSI_UNKNOWN; + int8_t rssi = RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN; /* * If we are in the middle of a BLE operation, we got called by ContikiMAC * from within an interrupt context. Indicate a clear channel */ if(rf_ble_is_active() == RF_BLE_ACTIVE) { - return RF_CCA_CLEAR; + return RF_CORE_CCA_CLEAR; } if(!rf_core_is_accessible()) { @@ -830,16 +901,16 @@ channel_clear(void) if(was_off) { off(); } - return RF_CCA_CLEAR; + return RF_CORE_CCA_CLEAR; } } else { if(transmitting()) { PRINTF("channel_clear: called while in TX\n"); - return RF_CCA_CLEAR; + return RF_CORE_CCA_CLEAR; } } - while(rssi == RF_CMD_CCA_REQ_RSSI_UNKNOWN || rssi == 0) { + while(rssi == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN || rssi == 0) { if(rf_core_send_cmd(CMDR_DIR_CMD(CMD_GET_RSSI), &cmd_status) != RF_CORE_CMD_OK) { break; @@ -853,10 +924,10 @@ channel_clear(void) } if(rssi >= rssi_threshold) { - return RF_CCA_BUSY; + return RF_CORE_CCA_BUSY; } - return RF_CCA_CLEAR; + return RF_CORE_CCA_CLEAR; } /*---------------------------------------------------------------------------*/ static int @@ -866,11 +937,23 @@ receiving_packet(void) return 0; } - if(channel_clear() == RF_CCA_CLEAR) { - return 0; + if(!is_receiving_packet) { + /* Look for the modem synchronization word detection interrupt flag. + * This flag is raised when the synchronization word is received. + */ + if(HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFHWIFG) & RFC_DBELL_RFHWIFG_MDMSOFT) { + is_receiving_packet = 1; + } + } else { + /* After the start of the packet: reset the Rx flag once the channel gets clear */ + is_receiving_packet = (channel_clear() == RF_CORE_CCA_BUSY); + if(!is_receiving_packet) { + /* Clear the modem sync flag */ + ti_lib_rfc_hw_int_clear(RFC_DBELL_RFHWIFG_MDMSOFT); + } } - return 1; + return is_receiving_packet; } /*---------------------------------------------------------------------------*/ static int @@ -881,9 +964,12 @@ pending_packet(void) /* Go through all RX buffers and check their status */ do { - if(entry->status == DATA_ENTRY_STATUS_FINISHED) { - rv += 1; - process_poll(&rf_core_process); + if(entry->status == DATA_ENTRY_STATUS_FINISHED + || entry->status == DATA_ENTRY_STATUS_BUSY) { + rv = 1; + if(!rf_core_poll_mode) { + process_poll(&rf_core_process); + } } entry = (rfc_dataEntry_t *)entry->pNextEntry; @@ -904,18 +990,18 @@ on(void) return RF_CORE_CMD_OK; } - /* - * Request the HF XOSC as the source for the HF clock. Needed before we can - * use the FS. This will only request, it will _not_ perform the switch. - */ - oscillators_request_hf_xosc(); - if(rf_is_on()) { PRINTF("on: We were on. PD=%u, RX=0x%04x \n", rf_core_is_accessible(), smartrf_settings_cmd_prop_rx_adv.status); return RF_CORE_CMD_OK; } + /* + * Request the HF XOSC as the source for the HF clock. Needed before we can + * use the FS. This will only request, it will _not_ perform the switch. + */ + oscillators_request_hf_xosc(); + if(!rf_core_is_accessible()) { if(rf_core_power_up() != RF_CORE_CMD_OK) { PRINTF("on: rf_core_power_up() failed\n"); @@ -958,7 +1044,7 @@ on(void) } } - rf_core_setup_interrupts(false); + rf_core_setup_interrupts(); init_rx_buffers(); @@ -985,6 +1071,9 @@ on(void) static int off(void) { + int i; + rfc_dataEntry_t *entry; + /* * If we are in the middle of a BLE operation, we got called by ContikiMAC * from within an interrupt context. Abort, but pretend everything is OK. @@ -998,15 +1087,39 @@ off(void) ENERGEST_OFF(ENERGEST_TYPE_LISTEN); +#if !CC2650_FAST_RADIO_STARTUP /* Switch HF clock source to the RCOSC to preserve power */ oscillators_switch_to_hf_rc(); +#endif /* We pulled the plug, so we need to restore the status manually */ smartrf_settings_cmd_prop_rx_adv.status = RF_CORE_RADIO_OP_STATUS_IDLE; + /* + * Just in case there was an ongoing RX (which started after we begun the + * shutdown sequence), we don't want to leave the buffer in state == ongoing + */ + for(i = 0; i < PROP_MODE_RX_BUF_CNT; i++) { + entry = (rfc_dataEntry_t *)rx_buf[i]; + if(entry->status == DATA_ENTRY_STATUS_BUSY) { + entry->status = DATA_ENTRY_STATUS_PENDING; + } + } + return RF_CORE_CMD_OK; } /*---------------------------------------------------------------------------*/ +/* Enable or disable CCA before sending */ +static radio_result_t +set_send_on_cca(uint8_t enable) +{ + if(enable) { + /* this driver does not have support for CCA on Tx */ + return RADIO_RESULT_NOT_SUPPORTED; + } + return RADIO_RESULT_OK; +} +/*---------------------------------------------------------------------------*/ static radio_result_t get_value(radio_param_t param, radio_value_t *value) { @@ -1022,6 +1135,15 @@ get_value(radio_param_t param, radio_value_t *value) case RADIO_PARAM_CHANNEL: *value = (radio_value_t)get_channel(); return RADIO_RESULT_OK; + case RADIO_PARAM_RX_MODE: + *value = 0; + if(rf_core_poll_mode) { + *value |= RADIO_RX_MODE_POLL_MODE; + } + return RADIO_RESULT_OK; + case RADIO_PARAM_TX_MODE: + *value = 0; + return RADIO_RESULT_OK; case RADIO_PARAM_TXPOWER: *value = get_tx_power(); return RADIO_RESULT_OK; @@ -1031,7 +1153,7 @@ get_value(radio_param_t param, radio_value_t *value) case RADIO_PARAM_RSSI: *value = get_rssi(); - if(*value == RF_CMD_CCA_REQ_RSSI_UNKNOWN) { + if(*value == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN) { return RADIO_RESULT_ERROR; } else { return RADIO_RESULT_OK; @@ -1048,6 +1170,28 @@ get_value(radio_param_t param, radio_value_t *value) case RADIO_CONST_TXPOWER_MAX: *value = OUTPUT_POWER_MAX; return RADIO_RESULT_OK; + case RADIO_PARAM_LAST_RSSI: + *value = rf_core_last_rssi; + return RADIO_RESULT_OK; + case RADIO_PARAM_LAST_LINK_QUALITY: + *value = rf_core_last_corr_lqi; + return RADIO_RESULT_OK; + case RADIO_CONST_PHY_OVERHEAD: + /* 2 header bytes, 2 or 4 bytes CRC */ + *value = (radio_value_t)(DOT_4G_PHR_LEN + CRC_LEN); + return RADIO_RESULT_OK; + case RADIO_CONST_BYTE_AIR_TIME: + *value = (radio_value_t)RADIO_BYTE_AIR_TIME; + return RADIO_RESULT_OK; + case RADIO_CONST_DELAY_BEFORE_TX: + *value = (radio_value_t)RADIO_DELAY_BEFORE_TX; + return RADIO_RESULT_OK; + case RADIO_CONST_DELAY_BEFORE_RX: + *value = (radio_value_t)RADIO_DELAY_BEFORE_RX; + return RADIO_RESULT_OK; + case RADIO_CONST_DELAY_BEFORE_DETECT: + *value = (radio_value_t)RADIO_DELAY_BEFORE_DETECT; + return RADIO_RESULT_OK; default: return RADIO_RESULT_NOT_SUPPORTED; } @@ -1056,8 +1200,8 @@ get_value(radio_param_t param, radio_value_t *value) static radio_result_t set_value(radio_param_t param, radio_value_t value) { - uint8_t was_off = 0; radio_result_t rv = RADIO_RESULT_OK; + uint8_t old_poll_mode; switch(param) { case RADIO_PARAM_POWER_MODE: @@ -1087,6 +1231,25 @@ set_value(radio_param_t param, radio_value_t value) set_channel((uint8_t)value); break; + + case RADIO_PARAM_RX_MODE: + if(value & ~(RADIO_RX_MODE_POLL_MODE)) { + return RADIO_RESULT_INVALID_VALUE; + } + + old_poll_mode = rf_core_poll_mode; + rf_core_poll_mode = (value & RADIO_RX_MODE_POLL_MODE) != 0; + if(rf_core_poll_mode == old_poll_mode) { + return RADIO_RESULT_OK; + } + break; + + case RADIO_PARAM_TX_MODE: + if(value & ~(RADIO_TX_MODE_SEND_ON_CCA)) { + return RADIO_RESULT_INVALID_VALUE; + } + return set_send_on_cca((value & RADIO_TX_MODE_SEND_ON_CCA) != 0); + case RADIO_PARAM_TXPOWER: if(value < TX_POWER_DRIVER[get_tx_power_array_last_element()].dbm || value > OUTPUT_POWER_MAX) { @@ -1103,8 +1266,7 @@ set_value(radio_param_t param, radio_value_t value) } return RADIO_RESULT_OK; - case RADIO_PARAM_RX_MODE: - return RADIO_RESULT_OK; + case RADIO_PARAM_CCA_THRESHOLD: rssi_threshold = (int8_t)value; break; @@ -1112,28 +1274,29 @@ set_value(radio_param_t param, radio_value_t value) return RADIO_RESULT_NOT_SUPPORTED; } - /* If we reach here we had no errors. Apply new settings */ + /* If off, the new configuration will be applied the next time radio is started */ if(!rf_is_on()) { - was_off = 1; - if(on() != RF_CORE_CMD_OK) { - PRINTF("set_value: on() failed (2)\n"); - return RADIO_RESULT_ERROR; - } + return RADIO_RESULT_OK; } + /* If we reach here we had no errors. Apply new settings */ if(rx_off_prop() != RF_CORE_CMD_OK) { PRINTF("set_value: rx_off_prop() failed\n"); rv = RADIO_RESULT_ERROR; } - if(soft_on_prop() != RF_CORE_CMD_OK) { - PRINTF("set_value: rx_on_prop() failed\n"); - rv = RADIO_RESULT_ERROR; + /* Restart the radio timer (RAT). + This causes resynchronization between RAT and RTC: useful for TSCH. */ + if(rf_core_restart_rat() != RF_CORE_CMD_OK) { + PRINTF("set_value: rf_core_restart_rat() failed\n"); + /* do not set the error */ + } else { + rf_core_check_rat_overflow(); } - /* If we were off, turn back off */ - if(was_off) { - off(); + if(soft_on_prop() != RF_CORE_CMD_OK) { + PRINTF("set_value: soft_on_prop() failed\n"); + rv = RADIO_RESULT_ERROR; } return rv; @@ -1142,6 +1305,15 @@ set_value(radio_param_t param, radio_value_t value) static radio_result_t get_object(radio_param_t param, void *dest, size_t size) { + if(param == RADIO_PARAM_LAST_PACKET_TIMESTAMP) { + if(size != sizeof(rtimer_clock_t) || !dest) { + return RADIO_RESULT_INVALID_VALUE; + } + *(rtimer_clock_t *)dest = rf_core_last_packet_timestamp; + + return RADIO_RESULT_OK; + } + return RADIO_RESULT_NOT_SUPPORTED; } /*---------------------------------------------------------------------------*/ diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/prop-mode.h b/arch/cpu/cc26x0-cc13x0/rf-core/prop-mode.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/prop-mode.h rename to arch/cpu/cc26x0-cc13x0/rf-core/prop-mode.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/rf-ble.c b/arch/cpu/cc26x0-cc13x0/rf-core/rf-ble.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/rf-ble.c rename to arch/cpu/cc26x0-cc13x0/rf-core/rf-ble.c diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/rf-ble.h b/arch/cpu/cc26x0-cc13x0/rf-core/rf-ble.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/rf-ble.h rename to arch/cpu/cc26x0-cc13x0/rf-core/rf-ble.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/rf-core.c b/arch/cpu/cc26x0-cc13x0/rf-core/rf-core.c similarity index 80% rename from arch/cpu/cc26xx-cc13xx/rf-core/rf-core.c rename to arch/cpu/cc26x0-cc13x0/rf-core/rf-core.c index 185842b42..7e638509d 100644 --- a/arch/cpu/cc26xx-cc13xx/rf-core/rf-core.c +++ b/arch/cpu/cc26x0-cc13x0/rf-core/rf-core.c @@ -99,12 +99,37 @@ static rfc_radioOp_t *last_radio_op = NULL; /* A struct holding pointers to the primary mode's abort() and restore() */ static const rf_core_primary_mode_t *primary_mode = NULL; /*---------------------------------------------------------------------------*/ +/* RAT has 32-bit register, overflows once 18 minutes */ +#define RAT_RANGE 4294967296ull +/* approximate value */ +#define RAT_OVERFLOW_PERIOD_SECONDS (60 * 18) + +/* how often to check for the overflow, as a minimum */ +#define RAT_OVERFLOW_TIMER_INTERVAL (CLOCK_SECOND * RAT_OVERFLOW_PERIOD_SECONDS / 3) + /* Radio timer (RAT) offset as compared to the rtimer counter (RTC) */ -int32_t rat_offset = 0; -static bool rat_offset_known = false; +static int32_t rat_offset; +static bool rat_offset_known; + +/* Value during the last read of the RAT register */ +static uint32_t rat_last_value; + +/* For RAT overflow handling */ +static struct ctimer rat_overflow_timer; +static volatile uint32_t rat_overflow_counter; +static rtimer_clock_t rat_last_overflow; + +static void rat_overflow_check_timer_cb(void *); +/*---------------------------------------------------------------------------*/ +volatile int8_t rf_core_last_rssi = RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN; +volatile uint8_t rf_core_last_corr_lqi = 0; +volatile uint32_t rf_core_last_packet_timestamp = 0; +/*---------------------------------------------------------------------------*/ +/* Are we currently in poll mode? */ +uint8_t rf_core_poll_mode = 0; /*---------------------------------------------------------------------------*/ /* Buffer full flag */ -volatile bool rx_is_full = false; +volatile bool rf_core_rx_is_full = false; /*---------------------------------------------------------------------------*/ PROCESS(rf_core_process, "CC13xx / CC26xx RF driver"); /*---------------------------------------------------------------------------*/ @@ -399,8 +424,8 @@ rf_core_set_modesel() } else if(chip_type == CHIP_TYPE_CC1350) { HWREG(PRCM_BASE + PRCM_O_RFCMODESEL) = PRCM_RFCMODESEL_CURR_MODE5; rv = RF_CORE_CMD_OK; -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated - } else if (chip_type == CHIP_TYPE_CC2640R2) { +#if CPU_FAMILY_CC26X0R2 + } else if(chip_type == CHIP_TYPE_CC2640R2) { HWREG(PRCM_BASE + PRCM_O_RFCMODESEL) = PRCM_RFCMODESEL_CURR_MODE1; rv = RF_CORE_CMD_OK; #endif @@ -451,10 +476,10 @@ rf_core_restart_rat(void) } /*---------------------------------------------------------------------------*/ void -rf_core_setup_interrupts(bool poll_mode) +rf_core_setup_interrupts(void) { bool interrupts_disabled; - const uint32_t enabled_irqs = poll_mode ? ENABLED_IRQS_POLL_MODE : ENABLED_IRQS; + const uint32_t enabled_irqs = rf_core_poll_mode ? ENABLED_IRQS_POLL_MODE : ENABLED_IRQS; /* We are already turned on by the caller, so this should not happen */ if(!rf_core_is_accessible()) { @@ -485,19 +510,23 @@ rf_core_setup_interrupts(bool poll_mode) } /*---------------------------------------------------------------------------*/ void -rf_core_cmd_done_en(bool fg, bool poll_mode) +rf_core_cmd_done_en(bool fg) { - uint32_t irq = fg ? IRQ_LAST_FG_COMMAND_DONE : IRQ_LAST_COMMAND_DONE; - const uint32_t enabled_irqs = poll_mode ? ENABLED_IRQS_POLL_MODE : ENABLED_IRQS; + uint32_t irq = 0; + const uint32_t enabled_irqs = rf_core_poll_mode ? ENABLED_IRQS_POLL_MODE : ENABLED_IRQS; + + if(!rf_core_poll_mode) { + irq = fg ? IRQ_LAST_FG_COMMAND_DONE : IRQ_LAST_COMMAND_DONE; + } HWREG(RFC_DBELL_NONBUF_BASE + RFC_DBELL_O_RFCPEIFG) = enabled_irqs; HWREG(RFC_DBELL_NONBUF_BASE + RFC_DBELL_O_RFCPEIEN) = enabled_irqs | irq; } /*---------------------------------------------------------------------------*/ void -rf_core_cmd_done_dis(bool poll_mode) +rf_core_cmd_done_dis(void) { - const uint32_t enabled_irqs = poll_mode ? ENABLED_IRQS_POLL_MODE : ENABLED_IRQS; + const uint32_t enabled_irqs = rf_core_poll_mode ? ENABLED_IRQS_POLL_MODE : ENABLED_IRQS; HWREG(RFC_DBELL_NONBUF_BASE + RFC_DBELL_O_RFCPEIEN) = enabled_irqs; } /*---------------------------------------------------------------------------*/ @@ -544,6 +573,123 @@ rf_core_primary_mode_restore() return RF_CORE_CMD_ERROR; } /*---------------------------------------------------------------------------*/ +uint8_t +rf_core_rat_init(void) +{ + rat_last_value = HWREG(RFC_RAT_BASE + RATCNT); + + ctimer_set(&rat_overflow_timer, RAT_OVERFLOW_TIMER_INTERVAL, + rat_overflow_check_timer_cb, NULL); + + return 1; +} +/*---------------------------------------------------------------------------*/ +uint8_t +rf_core_check_rat_overflow(void) +{ + uint32_t rat_current_value; + uint8_t interrupts_disabled; + + /* Bail out if the RF is not on */ + if(primary_mode == NULL || !primary_mode->is_on()) { + return 0; + } + + interrupts_disabled = ti_lib_int_master_disable(); + + rat_current_value = HWREG(RFC_RAT_BASE + RATCNT); + if(rat_current_value + RAT_RANGE / 4 < rat_last_value) { + /* Overflow detected */ + rat_last_overflow = RTIMER_NOW(); + rat_overflow_counter++; + } + rat_last_value = rat_current_value; + + if(!interrupts_disabled) { + ti_lib_int_master_enable(); + } + + return 1; +} +/*---------------------------------------------------------------------------*/ +static void +rat_overflow_check_timer_cb(void *unused) +{ + uint8_t success = 0; + uint8_t was_off = 0; + + if(primary_mode != NULL) { + + if(!primary_mode->is_on()) { + was_off = 1; + if(NETSTACK_RADIO.on() != RF_CORE_CMD_OK) { + PRINTF("overflow: on() failed\n"); + ctimer_set(&rat_overflow_timer, CLOCK_SECOND, + rat_overflow_check_timer_cb, NULL); + return; + } + } + + success = rf_core_check_rat_overflow(); + + if(was_off) { + NETSTACK_RADIO.off(); + } + } + + if(success) { + /* Retry after half of the interval */ + ctimer_set(&rat_overflow_timer, RAT_OVERFLOW_TIMER_INTERVAL, + rat_overflow_check_timer_cb, NULL); + } else { + /* Retry sooner */ + ctimer_set(&rat_overflow_timer, CLOCK_SECOND, + rat_overflow_check_timer_cb, NULL); + } +} +/*---------------------------------------------------------------------------*/ +uint32_t +rf_core_convert_rat_to_rtimer(uint32_t rat_timestamp) +{ + uint64_t rat_timestamp64; + uint32_t adjusted_overflow_counter; + uint8_t was_off = 0; + + if(primary_mode == NULL) { + PRINTF("rf_core_convert_rat_to_rtimer: not initialized\n"); + return 0; + } + + if(!primary_mode->is_on()) { + was_off = 1; + NETSTACK_RADIO.on(); + } + + rf_core_check_rat_overflow(); + + if(was_off) { + NETSTACK_RADIO.off(); + } + + adjusted_overflow_counter = rat_overflow_counter; + + /* if the timestamp is large and the last oveflow was recently, + assume that the timestamp refers to the time before the overflow */ + if(rat_timestamp > (uint32_t)(RAT_RANGE * 3 / 4)) { + if(RTIMER_CLOCK_LT(RTIMER_NOW(), + rat_last_overflow + RAT_OVERFLOW_PERIOD_SECONDS * RTIMER_SECOND / 4)) { + adjusted_overflow_counter--; + } + } + + /* add the overflowed time to the timestamp */ + rat_timestamp64 = rat_timestamp + RAT_RANGE * adjusted_overflow_counter; + /* correct timestamp so that it refers to the end of the SFD */ + rat_timestamp64 += primary_mode->sfd_timestamp_offset; + + return RADIO_TO_RTIMER(rat_timestamp64 - rat_offset); +} +/*---------------------------------------------------------------------------*/ PROCESS_THREAD(rf_core_process, ev, data) { int len; @@ -582,11 +728,11 @@ cc26xx_rf_cpe1_isr(void) return; } } - + if(HWREG(RFC_DBELL_NONBUF_BASE + RFC_DBELL_O_RFCPEIFG) & IRQ_RX_BUF_FULL) { PRINTF("\nRF: BUF_FULL\n\n"); /* set a flag that the buffer is full*/ - rx_is_full = true; + rf_core_rx_is_full = true; /* make sure read_frame() will be called to make space in RX buffer */ process_poll(&rf_core_process); /* Clear the IRQ_RX_BUF_FULL interrupt flag by writing zero to bit */ diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/rf-core.h b/arch/cpu/cc26x0-cc13x0/rf-core/rf-core.h similarity index 86% rename from arch/cpu/cc26xx-cc13xx/rf-core/rf-core.h rename to arch/cpu/cc26x0-cc13x0/rf-core/rf-core.h index c97ab0f8a..6cb65672d 100644 --- a/arch/cpu/cc26xx-cc13xx/rf-core/rf-core.h +++ b/arch/cpu/cc26x0-cc13x0/rf-core/rf-core.h @@ -133,6 +133,17 @@ typedef struct rf_core_primary_mode_s { * \return RF_CORE_CMD_OK or RF_CORE_CMD_ERROR */ uint8_t (*restore)(void); + + /** + * \brief A pointer to a function that checks if the radio is on + * \return 1 or 0 + */ + uint8_t (*is_on)(void); + + /** + * \brief Offset of the end of SFD when compared to the radio HW-generated timestamp + */ + int16_t sfd_timestamp_offset; } rf_core_primary_mode_t; /*---------------------------------------------------------------------------*/ /* RF Command status constants - Correspond to values in the CMDSTA register */ @@ -263,12 +274,65 @@ typedef struct rf_core_primary_mode_s { /* Radio timer register */ #define RATCNT 0x00000004 /*---------------------------------------------------------------------------*/ -/* Buffer full flag */ -extern volatile bool rx_is_full; +/* Special value returned by CMD_IEEE_CCA_REQ when an RSSI is not available */ +#define RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN -128 + +/* Used for the return value of channel_clear */ +#define RF_CORE_CCA_CLEAR 1 +#define RF_CORE_CCA_BUSY 0 + +/* Used as an error return value for get_cca_info */ +#define RF_CORE_GET_CCA_INFO_ERROR 0xFF + +/* + * Values of the individual bits of the ccaInfo field in CMD_IEEE_CCA_REQ's + * status struct + */ +#define RF_CORE_CMD_CCA_REQ_CCA_STATE_IDLE 0 /* 00 */ +#define RF_CORE_CMD_CCA_REQ_CCA_STATE_BUSY 1 /* 01 */ +#define RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID 2 /* 10 */ + +#define RF_CORE_CMD_CCA_REQ_CCA_CORR_IDLE (0 << 4) +#define RF_CORE_CMD_CCA_REQ_CCA_CORR_BUSY (1 << 4) +#define RF_CORE_CMD_CCA_REQ_CCA_CORR_INVALID (3 << 4) +#define RF_CORE_CMD_CCA_REQ_CCA_CORR_MASK (3 << 4) + +#define RF_CORE_CMD_CCA_REQ_CCA_SYNC_BUSY (1 << 6) +/*---------------------------------------------------------------------------*/ +#define RF_CORE_RX_BUF_INCLUDE_CRC 0 +#define RF_CORE_RX_BUF_INCLUDE_RSSI 1 +#define RF_CORE_RX_BUF_INCLUDE_CORR 1 +#define RF_CORE_RX_BUF_INCLUDE_TIMESTAMP 1 +/*---------------------------------------------------------------------------*/ +/* How long to wait for an ongoing ACK TX to finish before starting frame TX */ +#define RF_CORE_TX_TIMEOUT (RTIMER_SECOND >> 11) + +/* How long to wait for the RF to enter RX in rf_cmd_ieee_rx */ +#define RF_CORE_ENTER_RX_TIMEOUT (RTIMER_SECOND >> 10) + +/* How long to wait for the RF to react on CMD_ABORT: around 1 msec */ +#define RF_CORE_TURN_OFF_TIMEOUT (RTIMER_SECOND >> 10) + +/* How long to wait for the RF to finish TX of a packet or an ACK */ +#define RF_CORE_TX_FINISH_TIMEOUT (RTIMER_SECOND >> 7) + /*---------------------------------------------------------------------------*/ /* Make the main driver process visible to mode drivers */ PROCESS_NAME(rf_core_process); /*---------------------------------------------------------------------------*/ +/* Buffer full flag */ +extern volatile bool rf_core_rx_is_full; +/*---------------------------------------------------------------------------*/ +/* RSSI of the last read frame */ +extern volatile int8_t rf_core_last_rssi; +/* Correlation/LQI of the last read frame */ +extern volatile uint8_t rf_core_last_corr_lqi; +/* SFD timestamp of the last read frame, in rtimer ticks */ +extern volatile uint32_t rf_core_last_packet_timestamp; +/*---------------------------------------------------------------------------*/ +/* Are we currently in poll mode? */ +extern uint8_t rf_core_poll_mode; +/*---------------------------------------------------------------------------*/ /** * \brief Check whether the RF core is accessible * \retval RF_CORE_ACCESSIBLE The core is powered and ready for access @@ -383,20 +447,19 @@ uint8_t rf_core_boot(void); /** * \brief Setup RF core interrupts */ -void rf_core_setup_interrupts(bool poll_mode); +void rf_core_setup_interrupts(void); /** * \brief Enable interrupt on command done. * \param fg set true to enable irq on foreground command done and false for * background commands or if not in ieee mode. - * \param poll_mode true if the driver is in poll mode * * This is used within TX routines in order to be able to sleep the CM3 and * wake up after TX has finished * * \sa rf_core_cmd_done_dis() */ -void rf_core_cmd_done_en(bool fg, bool poll_mode); +void rf_core_cmd_done_en(bool fg); /** * \brief Disable the LAST_CMD_DONE and LAST_FG_CMD_DONE interrupts. @@ -405,7 +468,7 @@ void rf_core_cmd_done_en(bool fg, bool poll_mode); * * \sa rf_core_cmd_done_en() */ -void rf_core_cmd_done_dis(bool poll_mode); +void rf_core_cmd_done_dis(void); /** * \brief Returns a pointer to the most recent proto-dependent Radio Op @@ -467,6 +530,22 @@ void rf_core_primary_mode_abort(void); * \brief Abort the currently running primary radio op */ uint8_t rf_core_primary_mode_restore(void); + +/** + * \brief Initialize the RAT to RTC conversion machinery + */ +uint8_t rf_core_rat_init(void); + +/** + * \brief Check if RAT overflow has occured and increment the overflow counter if so + */ +uint8_t rf_core_check_rat_overflow(void); + +/** + * \brief Convert from RAT timestamp to rtimer ticks + */ +uint32_t rf_core_convert_rat_to_rtimer(uint32_t rat_timestamp); + /*---------------------------------------------------------------------------*/ #endif /* RF_CORE_H_ */ /*---------------------------------------------------------------------------*/ diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/rf-switch.h b/arch/cpu/cc26x0-cc13x0/rf-core/rf-switch.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/rf-switch.h rename to arch/cpu/cc26x0-cc13x0/rf-core/rf-switch.h diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/smartrf-settings.c b/arch/cpu/cc26x0-cc13x0/rf-core/smartrf-settings.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/smartrf-settings.c rename to arch/cpu/cc26x0-cc13x0/rf-core/smartrf-settings.c diff --git a/arch/cpu/cc26xx-cc13xx/rf-core/smartrf-settings.h b/arch/cpu/cc26x0-cc13x0/rf-core/smartrf-settings.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rf-core/smartrf-settings.h rename to arch/cpu/cc26x0-cc13x0/rf-core/smartrf-settings.h diff --git a/arch/cpu/cc26xx-cc13xx/rtimer-arch.c b/arch/cpu/cc26x0-cc13x0/rtimer-arch.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rtimer-arch.c rename to arch/cpu/cc26x0-cc13x0/rtimer-arch.c diff --git a/arch/cpu/cc26xx-cc13xx/rtimer-arch.h b/arch/cpu/cc26x0-cc13x0/rtimer-arch.h similarity index 100% rename from arch/cpu/cc26xx-cc13xx/rtimer-arch.h rename to arch/cpu/cc26x0-cc13x0/rtimer-arch.h diff --git a/arch/cpu/cc26xx-cc13xx/slip-arch.c b/arch/cpu/cc26x0-cc13x0/slip-arch.c similarity index 100% rename from arch/cpu/cc26xx-cc13xx/slip-arch.c rename to arch/cpu/cc26x0-cc13x0/slip-arch.c diff --git a/arch/cpu/cc26xx-cc13xx/ti-lib.h b/arch/cpu/cc26x0-cc13x0/ti-lib.h similarity index 97% rename from arch/cpu/cc26xx-cc13xx/ti-lib.h rename to arch/cpu/cc26x0-cc13x0/ti-lib.h index 6e6d78afb..0d8400813 100644 --- a/arch/cpu/cc26xx-cc13xx/ti-lib.h +++ b/arch/cpu/cc26x0-cc13x0/ti-lib.h @@ -52,9 +52,6 @@ #ifndef TI_LIB_H_ #define TI_LIB_H_ /*---------------------------------------------------------------------------*/ -/* Include ROM API */ -#include "ti-lib-rom.h" -/*---------------------------------------------------------------------------*/ /* aon_batmon.h */ #include "driverlib/aon_batmon.h" @@ -200,14 +197,16 @@ #define ti_lib_chipinfo_get_device_id_hw_rev_code(...) ChipInfo_GetDeviceIdHwRevCode(__VA_ARGS__) #define ti_lib_chipinfo_get_chip_type(...) ChipInfo_GetChipType(__VA_ARGS__) #define ti_lib_chipinfo_get_chip_family(...) ChipInfo_GetChipFamily(__VA_ARGS__) -#ifdef ThisLibraryIsFor_CC26x0R2_HaltIfViolated + +#if CPU_FAMILY_CC26X0R2 #define ti_lib_chipinfo_chip_family_is_cc26xx(...) ChipInfo_ChipFamilyIs_CC26x0(__VA_ARGS__) #define ti_lib_chipinfo_chip_family_is_cc13xx(...) ChipInfo_ChipFamilyIs_CC13x0(__VA_ARGS__) -#define ti_lib_chipinfo_chip_family_is_cc26x0r2(...) ChipInfo_ChipFamilyIs_CC26x0R2(__VA_ARGS__) +#define ti_lib_chipinfo_chip_family_is_cc26x0r2(...) ChipInfo_ChipFamilyIs_CC26x0R2(__VA_ARGS__) #else #define ti_lib_chipinfo_chip_family_is_cc26xx(...) ChipInfo_ChipFamilyIsCC26xx(__VA_ARGS__) #define ti_lib_chipinfo_chip_family_is_cc13xx(...) ChipInfo_ChipFamilyIsCC13xx(__VA_ARGS__) -#endif +#endif /* CPU_FAMILY_CC26X0R2 */ + #define ti_lib_chipinfo_get_hw_revision(...) ChipInfo_GetHwRevision(__VA_ARGS__) #define ti_lib_chipinfo_hw_revision_is_1_0(...) ChipInfo_HwRevisionIs_1_0(__VA_ARGS__) #define ti_lib_chipinfo_hw_revision_is_gteq_2_0(...) ChipInfo_HwRevisionIs_GTEQ_2_0(__VA_ARGS__) @@ -394,16 +393,15 @@ #define ti_lib_pwr_ctrl_source_get(...) PowerCtrlSourceGet(__VA_ARGS__) #define ti_lib_pwr_ctrl_reset_source_get(...) PowerCtrlResetSourceGet(__VA_ARGS__) #define ti_lib_pwr_ctrl_reset_source_clear(...) PowerCtrlResetSourceClear(__VA_ARGS__) -#if !defined(ThisLibraryIsFor_CC26x0R2_HaltIfViolated) -#define ti_lib_pwr_ctrl_io_freeze_enable(...) PowerCtrlIOFreezeEnable(__VA_ARGS__) -#define ti_lib_pwr_ctrl_io_freeze_disable(...) PowerCtrlIOFreezeDisable(__VA_ARGS__) -#endif /*---------------------------------------------------------------------------*/ /* rfc.h */ #include "driverlib/rfc.h" #define ti_lib_rfc_rtrim(...) RFCRTrim(__VA_ARGS__) #define ti_lib_rfc_adi3vco_ldo_voltage_mode(...) RFCAdi3VcoLdoVoltageMode(__VA_ARGS__) +#define ti_lib_rfc_hw_int_enable(...) RFCHwIntEnable(__VA_ARGS__) +#define ti_lib_rfc_hw_int_disable(...) RFCHwIntDisable(__VA_ARGS__) +#define ti_lib_rfc_hw_int_clear(...) RFCHwIntClear(__VA_ARGS__) /*---------------------------------------------------------------------------*/ /* sys_ctrl.h */ #include "driverlib/sys_ctrl.h" @@ -416,7 +414,14 @@ #define ti_lib_sys_ctrl_aon_sync(...) SysCtrlAonSync(__VA_ARGS__) #define ti_lib_sys_ctrl_aon_update(...) SysCtrlAonUpdate(__VA_ARGS__) #define ti_lib_sys_ctrl_set_recharge_before_power_down(...) SysCtrlSetRechargeBeforePowerDown(__VA_ARGS__) -#define ti_lib_sys_ctrl_adjust_recharge_after_power_down(...) SysCtrlAdjustRechargeAfterPowerDown(__VA_ARGS__) + +#if CPU_FAMILY_CC26X0R2 +/* May need to change to XOSC_IN_LOW_POWER_MODE */ +#define ti_lib_sys_ctrl_adjust_recharge_after_power_down() SysCtrlAdjustRechargeAfterPowerDown(XOSC_IN_HIGH_POWER_MODE) +#else +#define ti_lib_sys_ctrl_adjust_recharge_after_power_down() SysCtrlAdjustRechargeAfterPowerDown() +#endif /* CPU_FAMILY_CC26X0R2 */ + #define ti_lib_sys_ctrl_dcdc_voltage_conditional_control(...) SysCtrl_DCDC_VoltageConditionalControl(__VA_ARGS__) #define ti_lib_sys_ctrl_reset_source_get(...) SysCtrlResetSourceGet(__VA_ARGS__) #define ti_lib_sys_ctrl_system_reset(...) SysCtrlSystemReset(__VA_ARGS__) @@ -567,6 +572,14 @@ #define ti_lib_watchdog_stall_enable(...) WatchdogStallEnable(__VA_ARGS__) #define ti_lib_watchdog_stall_disable(...) WatchdogStallDisable(__VA_ARGS__) /*---------------------------------------------------------------------------*/ +/* crypto.h */ +#include "driverlib/crypto.h" + +#define ti_lib_crypto_aes_load_key(...) CRYPTOAesLoadKey(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb(...) CRYPTOAesEcb(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb_status(...) CRYPTOAesEcbStatus(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb_finish(...) CRYPTOAesEcbFinish(__VA_ARGS__) +/*---------------------------------------------------------------------------*/ #endif /* TI_LIB_H_ */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/cpu/cc26xx-cc13xx/Makefile.cc13xx b/arch/cpu/cc26xx-cc13xx/Makefile.cc13xx deleted file mode 100644 index 093aa1f02..000000000 --- a/arch/cpu/cc26xx-cc13xx/Makefile.cc13xx +++ /dev/null @@ -1,7 +0,0 @@ -TI_XXWARE_PATH = lib/cc13xxware - -CONTIKI_CPU_SOURCEFILES += smartrf-settings.c prop-mode.c prop-mode-tx-power.c - -CFLAGS += -DCPU_FAMILY_CC13XX=1 - -include $(CONTIKI_CPU)/Makefile.cc26xx-cc13xx diff --git a/arch/cpu/cc26xx-cc13xx/Makefile.cc26x0r2f b/arch/cpu/cc26xx-cc13xx/Makefile.cc26x0r2f deleted file mode 100644 index decece481..000000000 --- a/arch/cpu/cc26xx-cc13xx/Makefile.cc26x0r2f +++ /dev/null @@ -1,3 +0,0 @@ -TI_XXWARE_PATH = lib/cc2640r2-sdk - -include $(CONTIKI_CPU)/Makefile.cc26xx-cc13xx diff --git a/arch/cpu/cc26xx-cc13xx/Makefile.cc26xx b/arch/cpu/cc26xx-cc13xx/Makefile.cc26xx deleted file mode 100644 index 4bc2cdcad..000000000 --- a/arch/cpu/cc26xx-cc13xx/Makefile.cc26xx +++ /dev/null @@ -1,3 +0,0 @@ -TI_XXWARE_PATH = lib/cc26xxware - -include $(CONTIKI_CPU)/Makefile.cc26xx-cc13xx diff --git a/arch/cpu/cc26xx-cc13xx/ti-lib-rom.h b/arch/cpu/cc26xx-cc13xx/ti-lib-rom.h deleted file mode 100644 index e9fe50111..000000000 --- a/arch/cpu/cc26xx-cc13xx/ti-lib-rom.h +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup cc26xx-ti-lib - * @{ - * - * \file - * Header file with CC13xxware/CC26xxware ROM API. - */ -/*---------------------------------------------------------------------------*/ -#ifndef TI_LIB_ROM_H_ -#define TI_LIB_ROM_H_ -/*---------------------------------------------------------------------------*/ -/* rom.h */ -#include "driverlib/rom.h" - -/* AON API */ -#define ti_lib_rom_aon_event_mcu_wake_up_set ROM_AONEventMcuWakeUpSet -#define ti_lib_rom_aon_event_mcu_wake_up_get ROM_AONEventMcuWakeUpGet -#define ti_lib_rom_aon_event_aux_wake_up_set ROM_AONEventAuxWakeUpSet -#define ti_lib_rom_aon_event_aux_wake_up_get ROM_AONEventAuxWakeUpGet -#define ti_lib_rom_aon_event_mcu_set ROM_AONEventMcuSet -#define ti_lib_rom_aon_event_mcu_get ROM_AONEventMcuGet - -/* AON_WUC API */ -#define ti_lib_rom_aon_wuc_aux_reset ROM_AONWUCAuxReset -#define ti_lib_rom_aon_wuc_recharge_ctrl_config_set ROM_AONWUCRechargeCtrlConfigSet -#define ti_lib_rom_aon_wuc_osc_config ROM_AONWUCOscConfig - -/* AUX_TDC API */ -#define ti_lib_rom_aux_tdc_config_set ROM_AUXTDCConfigSet -#define ti_lib_rom_aux_tdc_measurement_done ROM_AUXTDCMeasurementDone - -/* AUX_WUC API */ -#define ti_lib_rom_aux_wuc_clock_enable ROM_AUXWUCClockEnable -#define ti_lib_rom_aux_wuc_clock_disable ROM_AUXWUCClockDisable -#define ti_lib_rom_aux_wuc_clock_status ROM_AUXWUCClockStatus -#define ti_lib_rom_aux_wuc_power_ctrl ROM_AUXWUCPowerCtrl - -/* FLASH API */ -#define ti_lib_rom_flash_power_mode_get ROM_FlashPowerModeGet -#define ti_lib_rom_flash_protection_set ROM_FlashProtectionSet -#define ti_lib_rom_flash_protection_get ROM_FlashProtectionGet -#define ti_lib_rom_flash_protection_save ROM_FlashProtectionSave -#define ti_lib_rom_flash_efuse_read_row ROM_FlashEfuseReadRow -#define ti_lib_rom_flash_disable_sectors_for_write ROM_FlashDisableSectorsForWrite - -/* I2C API */ -#define ti_lib_rom_i2c_master_init_exp_clk ROM_I2CMasterInitExpClk -#define ti_lib_rom_i2c_master_err ROM_I2CMasterErr - -/* INTERRUPT API */ -#define ti_lib_rom_int_priority_grouping_set ROM_IntPriorityGroupingSet -#define ti_lib_rom_int_priority_grouping_get ROM_IntPriorityGroupingGet -#define ti_lib_rom_int_priority_set ROM_IntPrioritySet -#define ti_lib_rom_int_priority_get ROM_IntPriorityGet -#define ti_lib_rom_int_enable ROM_IntEnable -#define ti_lib_rom_int_disable ROM_IntDisable -#define ti_lib_rom_int_pend_set ROM_IntPendSet -#define ti_lib_rom_int_pend_get ROM_IntPendGet -#define ti_lib_rom_int_pend_clear ROM_IntPendClear - -/* IOC API */ -#define ti_lib_rom_ioc_port_configure_set ROM_IOCPortConfigureSet -#if !defined(ThisLibraryIsFor_CC26x0R2_HaltIfViolated) -#define ti_lib_rom_ioc_port_configure_get ROM_IOCPortConfigureGet -#define ti_lib_rom_ioc_io_shutdown_set ROM_IOCIOShutdownSet -#define ti_lib_rom_ioc_io_mode_set ROM_IOCIOModeSet -#define ti_lib_rom_ioc_io_int_set ROM_IOCIOIntSet -#define ti_lib_rom_ioc_io_port_pull_set ROM_IOCIOPortPullSet -#define ti_lib_rom_ioc_io_hyst_set ROM_IOCIOHystSet -#define ti_lib_rom_ioc_io_input_set ROM_IOCIOInputSet -#define ti_lib_rom_ioc_io_slew_ctrl_set ROM_IOCIOSlewCtrlSet -#define ti_lib_rom_ioc_io_drv_strength_set ROM_IOCIODrvStrengthSet -#define ti_lib_rom_ioc_io_port_id_set ROM_IOCIOPortIdSet -#define ti_lib_rom_ioc_int_enable ROM_IOCIntEnable -#define ti_lib_rom_ioc_int_disable ROM_IOCIntDisable -#define ti_lib_rom_ioc_pin_type_gpio_input ROM_IOCPinTypeGpioInput -#define ti_lib_rom_ioc_pin_type_gpio_output ROM_IOCPinTypeGpioOutput -#define ti_lib_rom_ioc_pin_type_uart ROM_IOCPinTypeUart -#define ti_lib_rom_ioc_pin_type_ssi_master ROM_IOCPinTypeSsiMaster -#define ti_lib_rom_ioc_pin_type_ssi_slave ROM_IOCPinTypeSsiSlave -#define ti_lib_rom_ioc_pin_type_i2c ROM_IOCPinTypeI2c -#define ti_lib_rom_ioc_pin_type_aux ROM_IOCPinTypeAux -#endif - -/* PRCM API */ -#define ti_lib_rom_prcm_inf_clock_configure_set ROM_PRCMInfClockConfigureSet -#define ti_lib_rom_prcm_inf_clock_configure_get ROM_PRCMInfClockConfigureGet -#define ti_lib_rom_prcm_audio_clock_config_set ROM_PRCMAudioClockConfigSet -#define ti_lib_rom_prcm_power_domain_on ROM_PRCMPowerDomainOn -#define ti_lib_rom_prcm_power_domain_off ROM_PRCMPowerDomainOff -#define ti_lib_rom_prcm_peripheral_run_enable ROM_PRCMPeripheralRunEnable -#define ti_lib_rom_prcm_peripheral_run_disable ROM_PRCMPeripheralRunDisable -#define ti_lib_rom_prcm_peripheral_sleep_enable ROM_PRCMPeripheralSleepEnable -#define ti_lib_rom_prcm_peripheral_sleep_disable ROM_PRCMPeripheralSleepDisable -#define ti_lib_rom_prcm_peripheral_deep_sleep_enable ROM_PRCMPeripheralDeepSleepEnable -#define ti_lib_rom_prcm_peripheral_deep_sleep_disable ROM_PRCMPeripheralDeepSleepDisable -#define ti_lib_rom_prcm_power_domain_status ROM_PRCMPowerDomainStatus -#define ti_lib_rom_prcm_deep_sleep ROM_PRCMDeepSleep - -/* SMPH API */ -#define ti_lib_rom_smph_acquire ROM_SMPHAcquire - -/* SSI API */ -#define ti_lib_rom_ssi_config_set_exp_clk ROM_SSIConfigSetExpClk -#define ti_lib_rom_ssi_data_put ROM_SSIDataPut -#define ti_lib_rom_ssi_data_put_non_blocking ROM_SSIDataPutNonBlocking -#define ti_lib_rom_ssi_data_get ROM_SSIDataGet -#define ti_lib_rom_ssi_data_get_non_blocking ROM_SSIDataGetNonBlocking - -/* TIMER API */ -#define ti_lib_rom_timer_configure ROM_TimerConfigure -#define ti_lib_rom_timer_level_control ROM_TimerLevelControl -#define ti_lib_rom_timer_stall_control ROM_TimerStallControl -#define ti_lib_rom_timer_wait_on_trigger_control ROM_TimerWaitOnTriggerControl - -/* TRNG API */ -#define ti_lib_rom_trng_number_get ROM_TRNGNumberGet - -/* UART API */ -#define ti_lib_rom_uart_fifo_level_get ROM_UARTFIFOLevelGet -#define ti_lib_rom_uart_config_set_exp_clk ROM_UARTConfigSetExpClk -#define ti_lib_rom_uart_config_get_exp_clk ROM_UARTConfigGetExpClk -#define ti_lib_rom_uart_disable ROM_UARTDisable -#define ti_lib_rom_uart_char_get_non_blocking ROM_UARTCharGetNonBlocking -#define ti_lib_rom_uart_char_get ROM_UARTCharGet -#define ti_lib_rom_uart_char_put_non_blocking ROM_UARTCharPutNonBlocking -#define ti_lib_rom_uart_char_put ROM_UARTCharPut - -/* UDMA API */ -#define ti_lib_rom_udma_channel_attribute_enable ROM_uDMAChannelAttributeEnable -#define ti_lib_rom_udma_channel_attribute_disable ROM_uDMAChannelAttributeDisable -#define ti_lib_rom_udma_channel_attribute_get ROM_uDMAChannelAttributeGet -#define ti_lib_rom_udma_channel_control_set ROM_uDMAChannelControlSet -#define ti_lib_rom_udma_channel_transfer_set ROM_uDMAChannelTransferSet -#define ti_lib_rom_udma_channel_scatter_gather_set ROM_uDMAChannelScatterGatherSet -#define ti_lib_rom_udma_channel_size_get ROM_uDMAChannelSizeGet -#define ti_lib_rom_udma_channel_mode_get ROM_uDMAChannelModeGet - -/* VIMS API */ -#define ti_lib_rom_vims_configure ROM_VIMSConfigure -#define ti_lib_rom_vims_mode_set ROM_VIMSModeSet - -/* HAPI */ -#define ti_lib_hapi_crc32(a, b, c) HapiCrc32(a, b, c) -#define ti_lib_hapi_get_flash_size() HapiGetFlashSize() -#define ti_lib_hapi_get_chip_id() HapiGetChipId() -#define ti_lib_hapi_sector_erase(a) HapiSectorErase(a) -#define ti_lib_hapi_program_flash(a, b, c) HapiProgramFlash(a, b, c) -#define ti_lib_hapi_reset_device() HapiResetDevice() -#define ti_lib_hapi_fletcher32(a, b, c) HapiFletcher32(a, b, c) -#define ti_lib_hapi_min_value(a, b) HapiMinValue(a,b) -#define ti_lib_hapi_max_value(a, b) HapiMaxValue(a,b) -#define ti_lib_hapi_mean_value(a, b) HapiMeanValue(a,b) -#define ti_lib_hapi_stand_deviation_value(a, b) HapiStandDeviationValue(a,b) -#define ti_lib_hapi_hf_source_safe_switch() HapiHFSourceSafeSwitch() -#define ti_lib_hapi_select_comp_a_input(a) HapiSelectCompAInput(a) -#define ti_lib_hapi_select_comp_a_ref(a) HapiSelectCompARef(a) -#define ti_lib_hapi_select_adc_comp_b_input(a) HapiSelectADCCompBInput(a) -#define ti_lib_hapi_select_comp_b_ref(a) HapiSelectCompBRef(a) -/*---------------------------------------------------------------------------*/ -#endif /* TI_LIB_ROM_H_ */ -/*---------------------------------------------------------------------------*/ -/** - * @} - */ diff --git a/arch/cpu/msp430/Makefile.msp430 b/arch/cpu/msp430/Makefile.msp430 index 841e11285..7927002b6 100644 --- a/arch/cpu/msp430/Makefile.msp430 +++ b/arch/cpu/msp430/Makefile.msp430 @@ -11,7 +11,7 @@ CFLAGS += -gstabs+ .SUFFIXES: ### Define the CPU directory -CONTIKI_CPU=$(CONTIKI)/arch/cpu/msp430 +CONTIKI_CPU=$(ARCH_PATH)/cpu/msp430 ### Define the source files we have in the MSP430 port diff --git a/arch/cpu/msp430/uip-ipchksum.c b/arch/cpu/msp430/uip-ipchksum.c index e4cb308d0..442c154dc 100644 --- a/arch/cpu/msp430/uip-ipchksum.c +++ b/arch/cpu/msp430/uip-ipchksum.c @@ -51,7 +51,7 @@ uint16_t uip_ipchksum(void) { /* Assumes proper alignement of uip_buf. */ - uint16_t *p = (uint16_t *)&uip_buf[UIP_LLH_LEN]; + uint16_t *p = (uint16_t *)UIP_IP_BUF; register uint16_t sum; sum = p[0]; diff --git a/arch/cpu/native/Makefile.native b/arch/cpu/native/Makefile.native index d7832d358..30883622d 100644 --- a/arch/cpu/native/Makefile.native +++ b/arch/cpu/native/Makefile.native @@ -20,6 +20,30 @@ endif CFLAGSNO = -Wall -g -I/usr/local/include $(CFLAGSWERROR) CFLAGS += $(CFLAGSNO) +### Are we building with code size optimisations? +SMALL ?= 0 + +# The optimizations on native platform cannot be enabled in GCC (not Clang) versions less than 7.2 +GCC_IS_CLANG := $(shell gcc --version 2> /dev/null | grep clang) +ifneq ($(GCC_IS_CLANG),) + NATIVE_CAN_OPTIIMIZE = 1 +else + GCC_VERSION := $(shell gcc -dumpfullversion -dumpversion | cut -b1-3) + ifeq ($(shell expr $(GCC_VERSION) \>= 7.2), 1) + NATIVE_CAN_OPTIIMIZE = 1 + else + NATIVE_CAN_OPTIIMIZE = 0 + endif +endif + +ifeq ($(NATIVE_CAN_OPTIIMIZE),1) + ifeq ($(SMALL),1) + CFLAGS += -Os + else + CFLAGS += -O2 + endif +endif + ifeq ($(HOST_OS),Darwin) AROPTS = -rc LDFLAGS_WERROR := -Wl,-fatal_warnings diff --git a/arch/cpu/native/net/tun6-net.c b/arch/cpu/native/net/tun6-net.c index d46d91d42..f1e8c2e77 100644 --- a/arch/cpu/native/net/tun6-net.c +++ b/arch/cpu/native/net/tun6-net.c @@ -263,7 +263,7 @@ output(const linkaddr_t *localdest) { LOG_DBG("SUT: %u\n", uip_len); if(uip_len > 0) { - return tun_output(&uip_buf[UIP_LLH_LEN], uip_len); + return tun_output(uip_buf, uip_len); } return 0; } @@ -297,7 +297,7 @@ handle_fd(fd_set *rset, fd_set *wset) LOG_INFO("Tun6-handle FD\n"); if(FD_ISSET(tunfd, rset)) { - size = tun_input(&uip_buf[UIP_LLH_LEN], sizeof(uip_buf)); + size = tun_input(uip_buf, sizeof(uip_buf)); LOG_DBG("TUN data incoming read:%d\n", size); uip_len = size; tcpip_input(); diff --git a/arch/cpu/nrf52832/Makefile.nrf52832 b/arch/cpu/nrf52832/Makefile.nrf52832 index c97c728e5..102e74486 100644 --- a/arch/cpu/nrf52832/Makefile.nrf52832 +++ b/arch/cpu/nrf52832/Makefile.nrf52832 @@ -25,9 +25,9 @@ ifneq ($(NRF52_WITHOUT_SOFTDEVICE),1) NRF52_SOFTDEVICE := $(shell find $(NRF52_SDK_ROOT) -name *iot*_softdevice.hex | head -n 1) endif $(info SoftDevice: $(NRF52_SOFTDEVICE)) - LDSCRIPT := $(CONTIKI_CPU)/ld/nrf52-$(NRF52_DK_REVISION)-sd.ld + LDSCRIPT ?= $(CONTIKI_CPU)/ld/nrf52-$(NRF52_DK_REVISION)-sd.ld else - LDSCRIPT := $(CONTIKI_CPU)/ld/nrf52.ld + LDSCRIPT ?= $(CONTIKI_CPU)/ld/nrf52.ld endif OUTPUT_FILENAME := $(CONTIKI_PROJECT) @@ -172,11 +172,11 @@ vpath %.s $(ASM_PATHS) OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS) -CLEAN += nrf52832.a +NRFLIB = $(BUILD_DIR_BOARD)/nrf52832.a -TARGET_LIBS = nrf52832.a $(NRF52_SDK_ROOT)/components/iot/ble_6lowpan/lib/ble_6lowpan.a +TARGET_LIBS = $(NRFLIB) $(NRF52_SDK_ROOT)/components/iot/ble_6lowpan/lib/ble_6lowpan.a -nrf52832.a: $(OBJECTS) +$(NRFLIB): $(OBJECTS) $(TRACE_AR) $(Q)$(AR) $(AROPTS) $@ $^ @@ -185,10 +185,12 @@ $(OBJECT_DIRECTORY)/%.o: %.s $(TRACE_CC) $(Q)$(CC) $(ASMFLAGS) $(addprefix -I$(NRF52_SDK_ROOT)/, $(INC_PATHS)) -c -o $@ $< -%.jlink: - sed -e 's/#OUTPUT_FILENAME#/$*.hex/' $(CONTIKI_CPU)/flash.jlink > $@ +include $(ARCH_PATH)/cpu/arm/cortex-m/cm4/Makefile.cm4 -%.flash: %.hex %.jlink +%.jlink: $(OUT_HEX) + sed -e 's,#OUTPUT_FILENAME#,$<,' $(CONTIKI_CPU)/flash.jlink > $@ + +%.flash: %.jlink @echo Flashing: $^ $(JLINK) $(JLINK_OPTS) -CommanderScript $*.jlink @@ -203,5 +205,3 @@ erase: $(JLINK) $(JLINK_OPTS) -CommanderScript $(CONTIKI_CPU)/erase.jlink .PHONY: softdevice.jlink - -include $(CONTIKI)/arch/cpu/arm/cortex-m/cm4/Makefile.cm4 diff --git a/arch/cpu/nrf52832/ble/ble-mac.c b/arch/cpu/nrf52832/ble/ble-mac.c index 4fe1ef934..d604a911d 100644 --- a/arch/cpu/nrf52832/ble/ble-mac.c +++ b/arch/cpu/nrf52832/ble/ble-mac.c @@ -347,6 +347,12 @@ off(void) return 1; } /*---------------------------------------------------------------------------*/ +static int +max_payload(void) +{ + return PACKETBUF_SIZE; +} +/*---------------------------------------------------------------------------*/ static void init(void) { @@ -371,7 +377,8 @@ const struct mac_driver ble_ipsp_mac_driver = { send_packet, NULL, on, - off + off, + max_payload }; /*---------------------------------------------------------------------------*/ /** diff --git a/arch/cpu/nrf52832/ld/nrf52-pca10036-sd.ld b/arch/cpu/nrf52832/ld/nrf52-pca10036-sd.ld index f3bc5bc43..2afa9e6e2 100644 --- a/arch/cpu/nrf52832/ld/nrf52-pca10036-sd.ld +++ b/arch/cpu/nrf52832/ld/nrf52-pca10036-sd.ld @@ -14,3 +14,5 @@ INCLUDE "nrf5x_common.ld" /* These symbols are used by the stack check library. */ _stack = end; _stack_origin = ORIGIN(RAM) + LENGTH(RAM); +_heap = _stack; +_eheap = _stack_origin; diff --git a/arch/cpu/nrf52832/ld/nrf52-pca10040-sd.ld b/arch/cpu/nrf52832/ld/nrf52-pca10040-sd.ld index 0bc7349e3..b2c747f0a 100644 --- a/arch/cpu/nrf52832/ld/nrf52-pca10040-sd.ld +++ b/arch/cpu/nrf52832/ld/nrf52-pca10040-sd.ld @@ -14,3 +14,5 @@ INCLUDE "nrf5x_common.ld" /* These symbols are used by the stack check library. */ _stack = end; _stack_origin = ORIGIN(RAM) + LENGTH(RAM); +_heap = _stack; +_eheap = _stack_origin; diff --git a/arch/cpu/nrf52832/ld/nrf52.ld b/arch/cpu/nrf52832/ld/nrf52.ld index 87fc9152d..3fead1250 100644 --- a/arch/cpu/nrf52832/ld/nrf52.ld +++ b/arch/cpu/nrf52832/ld/nrf52.ld @@ -14,3 +14,5 @@ INCLUDE "nrf5x_common.ld" /* These symbols are used by the stack check library. */ _stack = end; _stack_origin = ORIGIN(RAM) + LENGTH(RAM); +_heap = _stack; +_eheap = _stack_origin; diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/Makefile.cc13xx-cc26xx b/arch/cpu/simplelink-cc13xx-cc26xx/Makefile.cc13xx-cc26xx index dfd14637e..bb7bf82f5 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/Makefile.cc13xx-cc26xx +++ b/arch/cpu/simplelink-cc13xx-cc26xx/Makefile.cc13xx-cc26xx @@ -3,7 +3,7 @@ CC13x2_CC26x2_PRE_RTM ?= 1 -# Core SDK is placed as a submodule under arch/cpu/cc13xx-cc26xx/lib. +# Core SDK is placed as a submodule under arch/cpu/simplelink-cc13xx-cc26xx/lib. # Do a sanity check that Core SDK submodule has been initialized. ifndef CORE_SDK CORE_SDK := $(CONTIKI_CPU)/lib/coresdk_cc13xx_cc26xx @@ -120,7 +120,7 @@ LDFLAGS += -nostartfiles LDFLAGS += -static # Linker script -LDSCRIPT := $(CONTIKI_CPU)/$(SUBFAMILY)/$(SUBFAMILY).lds +LDSCRIPT ?= $(CONTIKI_CPU)/$(SUBFAMILY)/$(SUBFAMILY).lds # Globally linked libraries TARGET_LIBFILES += -lc -lgcc -lnosys -lm diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/Makefile.cc13x0-cc26x0 b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/Makefile.cc13x0-cc26x0 index 9aa7c5be2..9be4ad6ab 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/Makefile.cc13x0-cc26x0 +++ b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/Makefile.cc13x0-cc26x0 @@ -8,4 +8,4 @@ TARGET_LIBFILES += $(SDK_DRIVERS)/lib/drivers_$(SDK_LIB_NAME).am3g TARGET_LIBFILES += $(SDK_DEVICES)/driverlib/bin/gcc/driverlib.lib # CC13x0/CC26x0 is a Cortex-M3 architecture -include $(CONTIKI)/arch/cpu/arm/cortex-m/cm3/Makefile.cm3 +include $(ARCH_PATH)/cpu/arm/cortex-m/cm3/Makefile.cm3 diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/cc13x0-cc26x0.lds b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/cc13x0-cc26x0.lds index 26930dea8..2bc8ec8cc 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/cc13x0-cc26x0.lds +++ b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/cc13x0-cc26x0.lds @@ -95,42 +95,42 @@ SECTIONS { * --------- DO NOT MODIFY ----------- */ UDMACC26XX_dmaSpi0RxControlTableEntry_is_placed = 0; - .dmaSpi0RxControlTableEntry DMA_SPI0_RX_CTEA : AT (DMA_SPI0_RX_CTEA) { + .dmaSpi0RxControlTableEntry DMA_SPI0_RX_CTEA (NOLOAD) : AT (DMA_SPI0_RX_CTEA) { *(.dmaSpi0RxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaSpi0TxControlTableEntry_is_placed = 0; - .dmaSpi0TxControlTableEntry DMA_SPI0_TX_CTEA : AT (DMA_SPI0_TX_CTEA) { + .dmaSpi0TxControlTableEntry DMA_SPI0_TX_CTEA (NOLOAD) : AT (DMA_SPI0_TX_CTEA) { *(.dmaSpi0TxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaADCPriControlTableEntry_is_placed = 0; - .dmaADCPriControlTableEntry DMA_ADC_PRI_CTEA : AT (DMA_ADC_PRI_CTEA) { + .dmaADCPriControlTableEntry DMA_ADC_PRI_CTEA (NOLOAD) : AT (DMA_ADC_PRI_CTEA) { *(.dmaADCPriControlTableEntry) } > REGION_DATA UDMACC26XX_dmaGPT0APriControlTableEntry_is_placed = 0; - .dmaGPT0APriControlTableEntry DMA_GPT0A_PRI_CTEA : AT (DMA_GPT0A_PRI_CTEA) { + .dmaGPT0APriControlTableEntry DMA_GPT0A_PRI_CTEA (NOLOAD) : AT (DMA_GPT0A_PRI_CTEA) { *(.dmaGPT0APriControlTableEntry) } > REGION_DATA UDMACC26XX_dmaSpi1RxControlTableEntry_is_placed = 0; - .dmaSpi1RxControlTableEntry DMA_SPI1_RX_CTEA : AT (DMA_SPI1_RX_CTEA) { + .dmaSpi1RxControlTableEntry DMA_SPI1_RX_CTEA (NOLOAD) : AT (DMA_SPI1_RX_CTEA) { *(.dmaSpi1RxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaSpi1TxControlTableEntry_is_placed = 0; - .dmaSpi1TxControlTableEntry DMA_SPI1_TX_CTEA : AT (DMA_SPI1_TX_CTEA) { + .dmaSpi1TxControlTableEntry DMA_SPI1_TX_CTEA (NOLOAD) : AT (DMA_SPI1_TX_CTEA) { *(.dmaSpi1TxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaADCAltControlTableEntry_is_placed = 0; - .dmaADCAltControlTableEntry DMA_ADC_ALT_CTEA : AT (DMA_ADC_ALT_CTEA) { + .dmaADCAltControlTableEntry DMA_ADC_ALT_CTEA (NOLOAD) : AT (DMA_ADC_ALT_CTEA) { *(.dmaADCAltControlTableEntry) } > REGION_DATA UDMACC26XX_dmaGPT0AAltControlTableEntry_is_placed = 0; - .dmaGPT0AAltControlTableEntry DMA_GPT0A_ALT_CTEA : AT (DMA_GPT0A_ALT_CTEA) { + .dmaGPT0AAltControlTableEntry DMA_GPT0A_ALT_CTEA (NOLOAD) : AT (DMA_GPT0A_ALT_CTEA) { *(.dmaGPT0AAltControlTableEntry) } > REGION_DATA diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/Makefile.cc13x2-cc26x2 b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/Makefile.cc13x2-cc26x2 index 52333f47d..afe0ae429 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/Makefile.cc13x2-cc26x2 +++ b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/Makefile.cc13x2-cc26x2 @@ -8,4 +8,4 @@ TARGET_LIBFILES += $(SDK_DRIVERS)/lib/drivers_$(SDK_LIB_NAME).am4fg TARGET_LIBFILES += $(SDK_DEVICES)/driverlib/bin/gcc/driverlib.lib # CC13x2/CC26x2 is a Cortex-M4 architecture -include $(CONTIKI)/arch/cpu/arm/cortex-m/cm4/Makefile.cm4 +include $(ARCH_PATH)/cpu/arm/cortex-m/cm4/Makefile.cm4 diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/cc13x2-cc26x2.lds b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/cc13x2-cc26x2.lds index 9c2211cb8..dc1a608be 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/cc13x2-cc26x2.lds +++ b/arch/cpu/simplelink-cc13xx-cc26xx/cc13x2-cc26x2/cc13x2-cc26x2.lds @@ -95,42 +95,42 @@ SECTIONS { * --------- DO NOT MODIFY ----------- */ UDMACC26XX_dmaSpi0RxControlTableEntry_is_placed = 0; - .dmaSpi0RxControlTableEntry DMA_SPI0_RX_CTEA : AT (DMA_SPI0_RX_CTEA) { + .dmaSpi0RxControlTableEntry DMA_SPI0_RX_CTEA (NOLOAD) : AT (DMA_SPI0_RX_CTEA) { *(.dmaSpi0RxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaSpi0TxControlTableEntry_is_placed = 0; - .dmaSpi0TxControlTableEntry DMA_SPI0_TX_CTEA : AT (DMA_SPI0_TX_CTEA) { + .dmaSpi0TxControlTableEntry DMA_SPI0_TX_CTEA (NOLOAD) : AT (DMA_SPI0_TX_CTEA) { *(.dmaSpi0TxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaADCPriControlTableEntry_is_placed = 0; - .dmaADCPriControlTableEntry DMA_ADC_PRI_CTEA : AT (DMA_ADC_PRI_CTEA) { + .dmaADCPriControlTableEntry DMA_ADC_PRI_CTEA (NOLOAD) : AT (DMA_ADC_PRI_CTEA) { *(.dmaADCPriControlTableEntry) } > REGION_DATA UDMACC26XX_dmaGPT0APriControlTableEntry_is_placed = 0; - .dmaGPT0APriControlTableEntry DMA_GPT0A_PRI_CTEA : AT (DMA_GPT0A_PRI_CTEA) { + .dmaGPT0APriControlTableEntry DMA_GPT0A_PRI_CTEA (NOLOAD) : AT (DMA_GPT0A_PRI_CTEA) { *(.dmaGPT0APriControlTableEntry) } > REGION_DATA UDMACC26XX_dmaSpi1RxControlTableEntry_is_placed = 0; - .dmaSpi1RxControlTableEntry DMA_SPI1_RX_CTEA : AT (DMA_SPI1_RX_CTEA) { + .dmaSpi1RxControlTableEntry DMA_SPI1_RX_CTEA (NOLOAD) : AT (DMA_SPI1_RX_CTEA) { *(.dmaSpi1RxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaSpi1TxControlTableEntry_is_placed = 0; - .dmaSpi1TxControlTableEntry DMA_SPI1_TX_CTEA : AT (DMA_SPI1_TX_CTEA) { + .dmaSpi1TxControlTableEntry DMA_SPI1_TX_CTEA (NOLOAD) : AT (DMA_SPI1_TX_CTEA) { *(.dmaSpi1TxControlTableEntry) } > REGION_DATA UDMACC26XX_dmaADCAltControlTableEntry_is_placed = 0; - .dmaADCAltControlTableEntry DMA_ADC_ALT_CTEA : AT (DMA_ADC_ALT_CTEA) { + .dmaADCAltControlTableEntry DMA_ADC_ALT_CTEA (NOLOAD) : AT (DMA_ADC_ALT_CTEA) { *(.dmaADCAltControlTableEntry) } > REGION_DATA UDMACC26XX_dmaGPT0AAltControlTableEntry_is_placed = 0; - .dmaGPT0AAltControlTableEntry DMA_GPT0A_ALT_CTEA : AT (DMA_GPT0A_ALT_CTEA) { + .dmaGPT0AAltControlTableEntry DMA_GPT0A_ALT_CTEA (NOLOAD) : AT (DMA_GPT0A_ALT_CTEA) { *(.dmaGPT0AAltControlTableEntry) } > REGION_DATA diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/cc13xx-cc26xx-conf.h b/arch/cpu/simplelink-cc13xx-cc26xx/cc13xx-cc26xx-conf.h index 87fa922b9..49013070e 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/cc13xx-cc26xx-conf.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/cc13xx-cc26xx-conf.h @@ -133,6 +133,32 @@ #define RF_MODE RF_CONF_MODE #endif /* RF_CONF_MODE */ +/* Sub-1 GHz path front-end mode configuration */ +#ifdef RF_SUB_1_GHZ_CONF_FRONT_END_MODE +#define RF_SUB_1_GHZ_FRONT_END_MODE RF_SUB_1_GHZ_CONF_FRONT_END_MODE +#else +#define RF_SUB_1_GHZ_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#endif + +#ifdef RF_SUB_1_GHZ_CONF_BIAS_MODE +#define RF_SUB_1_GHZ_BIAS_MODE RF_SUB_1_GHZ_CONF_BIAS_MODE +#else +#define RF_SUB_1_GHZ_BIAS_MODE RF_BIAS_MODE_INTERNAL +#endif + +/* 2.4 GHz path front-end mode configuration */ +#ifdef RF_2_4_GHZ_CONF_FRONT_END_MODE +#define RF_2_4_GHZ_FRONT_END_MODE RF_2_4_GHZ_CONF_FRONT_END_MODE +#else +#define RF_2_4_GHZ_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#endif + +#ifdef RF_2_4_GHZ_CONF_BIAS_MODE +#define RF_2_4_GHZ_BIAS_MODE RF_2_4_GHZ_CONF_BIAS_MODE +#else +#define RF_2_4_GHZ_BIAS_MODE RF_BIAS_MODE_INTERNAL +#endif + /* Number of RX buffers. */ #ifndef RF_CONF_RX_BUF_CNT #define RF_CONF_RX_BUF_CNT 4 diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/dev/gpio-hal-arch.c b/arch/cpu/simplelink-cc13xx-cc26xx/dev/gpio-hal-arch.c index e219faa93..f07a1f693 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/dev/gpio-hal-arch.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/dev/gpio-hal-arch.c @@ -96,7 +96,7 @@ to_hal_cfg(PIN_Config pin_cfg, gpio_hal_pin_cfg_t *cfg) /* Input config */ if(pin_cfg & PIN_BM_INPUT_MODE) { /* Hysteresis config */ - if((pin_cfg & PIN_BM_HYSTERESIS) == PIN_HYSTERESIS) { + if((pin_cfg & PIN_BM_HYSTERESIS) == PIN_BM_HYSTERESIS) { *cfg |= GPIO_HAL_PIN_CFG_HYSTERESIS; } diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/dev/spi-arch.c b/arch/cpu/simplelink-cc13xx-cc26xx/dev/spi-arch.c index a019a33fb..f4c6114e8 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/dev/spi-arch.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/dev/spi-arch.c @@ -235,30 +235,6 @@ spi_arch_transfer(const spi_device_t *dev, return SPI_DEV_STATUS_OK; } /*---------------------------------------------------------------------------*/ -spi_status_t -spi_arch_select(const spi_device_t *dev) -{ - if(!spi_arch_has_lock(dev)) { - return SPI_DEV_STATUS_BUS_NOT_OWNED; - } - - PINCC26XX_setOutputValue(dev->pin_spi_cs, 0); - - return SPI_DEV_STATUS_OK; -} -/*---------------------------------------------------------------------------*/ -spi_status_t -spi_arch_deselect(const spi_device_t *dev) -{ - if(!spi_arch_has_lock(dev)) { - return SPI_DEV_STATUS_BUS_NOT_OWNED; - } - - PINCC26XX_setOutputValue(dev->pin_spi_cs, 1); - - return SPI_DEV_STATUS_OK; -} -/*---------------------------------------------------------------------------*/ /** * @} * @} diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.c index 7fc79ae2d..f9902f826 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.c @@ -56,12 +56,18 @@ /* TI-RTOS RF Mode Object */ RF_Mode rf_ble_mode = { - .rfMode = RF_MODE_BLE, + .rfMode = RF_MODE_MULTIPLE, .cpePatchFxn = &rf_patch_cpe_ble, .mcePatchFxn = 0, .rfePatchFxn = &rf_patch_rfe_ble, }; /*---------------------------------------------------------------------------*/ +/* + * CMD_RADIO_SETUP must be configured with default TX power value + * in the .txPower field. + */ +#define DEFAULT_TX_POWER 0x5F3C /* 5 dBm */ +/*---------------------------------------------------------------------------*/ /* Overrides for CMD_RADIO_SETUP */ uint32_t rf_ble_overrides[] CC_ALIGN(4) = { @@ -109,46 +115,14 @@ rfc_CMD_RADIO_SETUP_t rf_ble_cmd_radio_setup = .condition.nSkip = 0x0, .mode = 0x00, .loDivider = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, - .txPower = 0x3D3F, + .txPower = DEFAULT_TX_POWER, .pRegOverride = rf_ble_overrides, }; /*---------------------------------------------------------------------------*/ -/* Structure for CMD_BLE_ADV_NC.pParams */ -rfc_bleAdvPar_t rf_ble_adv_par = -{ - .pRxQ = 0, - .rxConfig.bAutoFlushIgnored = 0x0, - .rxConfig.bAutoFlushCrcErr = 0x0, - .rxConfig.bAutoFlushEmpty = 0x0, - .rxConfig.bIncludeLenByte = 0x0, - .rxConfig.bIncludeCrc = 0x0, - .rxConfig.bAppendRssi = 0x0, - .rxConfig.bAppendStatus = 0x0, - .rxConfig.bAppendTimestamp = 0x0, - .advConfig.advFilterPolicy = 0x0, - .advConfig.deviceAddrType = 0x0, - .advConfig.peerAddrType = 0x0, - .advConfig.bStrictLenFilter = 0x0, - .advConfig.rpaMode = 0x0, - .advLen = 0x18, - .scanRspLen = 0x00, - .pAdvData = 0, - .pScanRspData = 0, - .pDeviceAddress = 0, - .pWhiteList = 0, - .__dummy0 = 0x0000, - .__dummy1 = 0x00, - .endTrigger.triggerType = TRIG_NEVER, - .endTrigger.bEnaCmd = 0x0, - .endTrigger.triggerNo = 0x0, - .endTrigger.pastTrig = 0x0, - .endTime = 0x00000000, -}; -/*---------------------------------------------------------------------------*/ /* CMD_BLE_ADV_NC: BLE Non-Connectable Advertiser Command */ rfc_CMD_BLE_ADV_NC_t rf_ble_cmd_ble_adv_nc = { @@ -159,13 +133,13 @@ rfc_CMD_BLE_ADV_NC_t rf_ble_cmd_ble_adv_nc = .startTrigger.triggerType = TRIG_NOW, .startTrigger.bEnaCmd = 0x0, .startTrigger.triggerNo = 0x0, - .startTrigger.pastTrig = 0x0, - .condition.rule = COND_NEVER, + .startTrigger.pastTrig = 0x1, + .condition.rule = 0x0, /* set by driver */ .condition.nSkip = 0x0, - .channel = 0x8C, - .whitening.init = 0x51, + .channel = 0x00, /* set by driver */ + .whitening.init = 0x00, /* set by driver */ .whitening.bOverride = 0x1, - .pParams = &rf_ble_adv_par, - .pOutput = 0, + .pParams = 0x00000000, /* set by driver */ + .pOutput = 0x00000000, /* set by driver */ }; /*---------------------------------------------------------------------------*/ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.h index 81fbf67ba..4d94edca8 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ble-settings.h @@ -43,7 +43,6 @@ extern RF_Mode rf_ble_mode; /*---------------------------------------------------------------------------*/ /* RF Core API commands */ extern rfc_CMD_RADIO_SETUP_t rf_ble_cmd_radio_setup; -extern rfc_bleAdvPar_t rf_ble_adv_par; extern rfc_CMD_BLE_ADV_NC_t rf_ble_cmd_ble_adv_nc; /*---------------------------------------------------------------------------*/ /* RF Core API Overrides */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ieee-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ieee-settings.c index 2a383e6c6..1537f693c 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ieee-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/ieee-settings.c @@ -57,7 +57,7 @@ /* TI-RTOS RF Mode Object */ RF_Mode rf_ieee_mode = { - .rfMode = RF_MODE_IEEE_15_4, + .rfMode = RF_MODE_MULTIPLE, .cpePatchFxn = 0, .mcePatchFxn = 0, .rfePatchFxn = 0, @@ -110,8 +110,8 @@ rfc_CMD_RADIO_SETUP_t rf_cmd_ieee_radio_setup = .condition.rule = COND_NEVER, .condition.nSkip = 0x0, .mode = 0x01, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, .txPower = DEFAULT_TX_POWER, /* 5 dBm default */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/prop-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/prop-settings.c index 64dbe591f..3d57fc3a3 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/prop-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x0/prop-settings.c @@ -68,7 +68,7 @@ /* TI-RTOS RF Mode Object */ RF_Mode rf_prop_mode = { - .rfMode = RF_MODE_PROPRIETARY_SUB_1, + .rfMode = RF_MODE_MULTIPLE, .cpePatchFxn = &rf_patch_cpe_genfsk, .mcePatchFxn = 0, .rfePatchFxn = &rf_patch_rfe_genfsk, diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.c index b9e32a04d..b43ce21a6 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.c @@ -47,7 +47,7 @@ #include DeviceFamily_constructPath(driverlib/rf_mailbox.h) #include DeviceFamily_constructPath(driverlib/rf_common_cmd.h) #include DeviceFamily_constructPath(driverlib/rf_ble_cmd.h) -#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_bt5.h) +#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_multi_protocol.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_rfe_bt5.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_mce_bt5.h) @@ -59,11 +59,17 @@ RF_Mode rf_ble_mode = { .rfMode = RF_MODE_AUTO, - .cpePatchFxn = &rf_patch_cpe_bt5, + .cpePatchFxn = &rf_patch_cpe_multi_protocol, .mcePatchFxn = &rf_patch_mce_bt5, .rfePatchFxn = &rf_patch_rfe_bt5, }; /*---------------------------------------------------------------------------*/ +/* + * CMD_RADIO_SETUP must be configured with default TX power value + * in the .txPower field. + */ +#define DEFAULT_TX_POWER 0x941E /* 5 dBm */ +/*---------------------------------------------------------------------------*/ /* Overrides for CMD_BLE5_RADIO_SETUP */ uint32_t rf_ble_overrides_common[] CC_ALIGN(4) = { @@ -137,71 +143,39 @@ rfc_CMD_BLE5_RADIO_SETUP_t rf_ble_cmd_radio_setup = .defaultPhy.mainMode = 0x0, .defaultPhy.coding = 0x0, .loDivider = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, - .txPower = 0x941E, + .txPower = DEFAULT_TX_POWER, .pRegOverrideCommon = rf_ble_overrides_common, .pRegOverride1Mbps = rf_ble_overrides_1mbps, .pRegOverride2Mbps = rf_ble_overrides_2mbps, .pRegOverrideCoded = rf_ble_overrides_coded, }; /*---------------------------------------------------------------------------*/ -/* Structure for CMD_BLE5_ADV_NC.pParams */ -rfc_bleAdvPar_t rf_ble_adv_par = -{ - .pRxQ = 0, - .rxConfig.bAutoFlushIgnored = 0x0, - .rxConfig.bAutoFlushCrcErr = 0x0, - .rxConfig.bAutoFlushEmpty = 0x0, - .rxConfig.bIncludeLenByte = 0x0, - .rxConfig.bIncludeCrc = 0x0, - .rxConfig.bAppendRssi = 0x0, - .rxConfig.bAppendStatus = 0x0, - .rxConfig.bAppendTimestamp = 0x0, - .advConfig.advFilterPolicy = 0x0, - .advConfig.deviceAddrType = 0x0, - .advConfig.peerAddrType = 0x0, - .advConfig.bStrictLenFilter = 0x0, - .advConfig.rpaMode = 0x0, - .advLen = 0x18, - .scanRspLen = 0x00, - .pAdvData = 0, - .pScanRspData = 0, - .pDeviceAddress = 0, - .pWhiteList = 0, - .__dummy0 = 0x0000, - .__dummy1 = 0x00, - .endTrigger.triggerType = TRIG_NEVER, - .endTrigger.bEnaCmd = 0x0, - .endTrigger.triggerNo = 0x0, - .endTrigger.pastTrig = 0x0, - .endTime = 0x00000000, -}; -/*---------------------------------------------------------------------------*/ /* CMD_BLE5_ADV_NC: Bluetooth 5 Non-Connectable Advertiser Command */ rfc_CMD_BLE5_ADV_NC_t rf_ble_cmd_ble_adv_nc = { - .commandNo = 0x182D, - .status = 0x0000, - .pNextOp = 0, // INSERT APPLICABLE POINTER: (uint8_t*)&xxx + .commandNo = CMD_BLE5_ADV_NC, + .status = IDLE, + .pNextOp = 0, .startTime = 0x00000000, - .startTrigger.triggerType = 0x0, + .startTrigger.triggerType = TRIG_NOW, .startTrigger.bEnaCmd = 0x0, .startTrigger.triggerNo = 0x0, - .startTrigger.pastTrig = 0x0, - .condition.rule = 0x1, + .startTrigger.pastTrig = 0x1, + .condition.rule = 0x0, /* set by driver */ .condition.nSkip = 0x0, - .channel = 0x8C, - .whitening.init = 0x51, + .channel = 0x00, /* set by driver */ + .whitening.init = 0x00, /* set by driver */ .whitening.bOverride = 0x1, .phyMode.mainMode = 0x0, .phyMode.coding = 0x0, .rangeDelay = 0x00, .txPower = 0x0000, - .pParams = &rf_ble_adv_par, - .pOutput = 0, // INSERT APPLICABLE POINTER: (uint8_t*)&xxx + .pParams = 0x00000000, /* set by driver */ + .pOutput = 0x00000000, /* set by driver */ .tx20Power = 0x00000000, }; /*---------------------------------------------------------------------------*/ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.h index 2e6defa71..c5295c868 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ble-settings.h @@ -43,7 +43,6 @@ extern RF_Mode rf_ble_mode; /*---------------------------------------------------------------------------*/ /* RF Core API commands */ extern rfc_CMD_BLE5_RADIO_SETUP_t rf_ble_cmd_radio_setup; -extern rfc_bleAdvPar_t rf_ble_adv_par; extern rfc_CMD_BLE5_ADV_NC_t rf_ble_cmd_ble_adv_nc; /*---------------------------------------------------------------------------*/ /* RF Core API Overrides */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ieee-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ieee-settings.c index 1f5e3d8d7..a81a83385 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ieee-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/ieee-settings.c @@ -51,7 +51,7 @@ #include DeviceFamily_constructPath(driverlib/rf_mailbox.h) #include DeviceFamily_constructPath(driverlib/rf_common_cmd.h) #include DeviceFamily_constructPath(driverlib/rf_ieee_cmd.h) -#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_ieee_802_15_4.h) +#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_multi_protocol.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_mce_ieee_802_15_4.h) #include @@ -62,7 +62,7 @@ RF_Mode rf_ieee_mode = { .rfMode = RF_MODE_AUTO, - .cpePatchFxn = &rf_patch_cpe_ieee_802_15_4, + .cpePatchFxn = &rf_patch_cpe_multi_protocol, .mcePatchFxn = &rf_patch_mce_ieee_802_15_4, .rfePatchFxn = 0, }; @@ -180,8 +180,8 @@ rfc_CMD_RADIO_SETUP_t rf_cmd_ieee_radio_setup = .condition.nSkip = 0x0, .mode = 0x01, .loDivider = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, .txPower = DEFAULT_TX_POWER, /* 5 dBm default */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/prop-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/prop-settings.c index c63e6bef7..0f098c5e1 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/prop-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc13x2/prop-settings.c @@ -63,7 +63,7 @@ #include DeviceFamily_constructPath(driverlib/rf_mailbox.h) #include DeviceFamily_constructPath(driverlib/rf_common_cmd.h) #include DeviceFamily_constructPath(driverlib/rf_prop_cmd.h) -#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_prop.h) +#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_multi_protocol.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_rfe_genfsk.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_mce_genfsk.h) @@ -75,7 +75,7 @@ RF_Mode rf_prop_mode = { .rfMode = RF_MODE_AUTO, - .cpePatchFxn = &rf_patch_cpe_prop, + .cpePatchFxn = &rf_patch_cpe_multi_protocol, .mcePatchFxn = &rf_patch_mce_genfsk, .rfePatchFxn = &rf_patch_rfe_genfsk, }; diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.c index 7edc3e487..9c19021fd 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.c @@ -55,12 +55,18 @@ /* TI-RTOS RF Mode Object */ RF_Mode rf_ble_mode = { - .rfMode = RF_MODE_BLE, + .rfMode = RF_MODE_MULTIPLE, .cpePatchFxn = &rf_patch_cpe_ble, .mcePatchFxn = 0, .rfePatchFxn = &rf_patch_rfe_ble, }; /*---------------------------------------------------------------------------*/ +/* + * CMD_RADIO_SETUP must be configured with default TX power value + * in the .txPower field. + */ +#define DEFAULT_TX_POWER 0x9330 /* 5 dBm */ +/*---------------------------------------------------------------------------*/ /* Overrides for CMD_RADIO_SETUP */ uint32_t rf_ble_overrides[] CC_ALIGN(4) = { @@ -105,46 +111,14 @@ rfc_CMD_RADIO_SETUP_t rf_ble_cmd_radio_setup = .condition.nSkip = 0x0, .mode = 0x00, .__dummy0 = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, - .txPower = 0x9330, + .txPower = DEFAULT_TX_POWER, .pRegOverride = rf_ble_overrides, }; /*---------------------------------------------------------------------------*/ -/* Structure for CMD_BLE_ADV_NC.pParams */ -rfc_bleAdvPar_t rf_ble_adv_par = -{ - .pRxQ = 0, - .rxConfig.bAutoFlushIgnored = 0x0, - .rxConfig.bAutoFlushCrcErr = 0x0, - .rxConfig.bAutoFlushEmpty = 0x0, - .rxConfig.bIncludeLenByte = 0x0, - .rxConfig.bIncludeCrc = 0x0, - .rxConfig.bAppendRssi = 0x0, - .rxConfig.bAppendStatus = 0x0, - .rxConfig.bAppendTimestamp = 0x0, - .advConfig.advFilterPolicy = 0x0, - .advConfig.deviceAddrType = 0x0, - .advConfig.peerAddrType = 0x0, - .advConfig.bStrictLenFilter = 0x0, - .advConfig.rpaMode = 0x0, - .advLen = 0x18, - .scanRspLen = 0x00, - .pAdvData = 0, - .pScanRspData = 0, - .pDeviceAddress = 0, - .pWhiteList = 0, - .__dummy0 = 0x0000, - .__dummy1 = 0x00, - .endTrigger.triggerType = TRIG_NEVER, - .endTrigger.bEnaCmd = 0x0, - .endTrigger.triggerNo = 0x0, - .endTrigger.pastTrig = 0x0, - .endTime = 0x00000000, -}; -/*---------------------------------------------------------------------------*/ /* CMD_BLE_ADV_NC: BLE Non-Connectable Advertiser Command */ rfc_CMD_BLE_ADV_NC_t rf_ble_cmd_ble_adv_nc = { @@ -155,13 +129,13 @@ rfc_CMD_BLE_ADV_NC_t rf_ble_cmd_ble_adv_nc = .startTrigger.triggerType = TRIG_NOW, .startTrigger.bEnaCmd = 0x0, .startTrigger.triggerNo = 0x0, - .startTrigger.pastTrig = 0x0, - .condition.rule = COND_NEVER, + .startTrigger.pastTrig = 0x1, + .condition.rule = 0x0, /* set by driver */ .condition.nSkip = 0x0, - .channel = 0x8C, - .whitening.init = 0x51, + .channel = 0x00, /* set by driver */ + .whitening.init = 0x00, /* set by driver */ .whitening.bOverride = 0x1, - .pParams = &rf_ble_adv_par, - .pOutput = 0, + .pParams = 0x00000000, /* set by driver */ + .pOutput = 0x00000000, /* set by driver */ }; /*---------------------------------------------------------------------------*/ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.h index c89304105..0679265d6 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ble-settings.h @@ -43,7 +43,6 @@ extern RF_Mode rf_ble_mode; /*---------------------------------------------------------------------------*/ /* RF Core API commands */ extern rfc_CMD_RADIO_SETUP_t rf_ble_cmd_radio_setup; -extern rfc_bleAdvPar_t rf_ble_adv_par; extern rfc_CMD_BLE_ADV_NC_t rf_ble_cmd_ble_adv_nc; /*---------------------------------------------------------------------------*/ /* RF Core API Overrides */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ieee-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ieee-settings.c index e93561962..4d860109d 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ieee-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x0/ieee-settings.c @@ -53,7 +53,7 @@ /* TI-RTOS RF Mode Object */ RF_Mode rf_ieee_mode = { - .rfMode = RF_MODE_IEEE_15_4, + .rfMode = RF_MODE_MULTIPLE, .cpePatchFxn = &rf_patch_cpe_ieee, .mcePatchFxn = 0, .rfePatchFxn = 0, @@ -107,8 +107,8 @@ rfc_CMD_RADIO_SETUP_t rf_cmd_ieee_radio_setup = .condition.nSkip = 0x0, .mode = 0x01, .__dummy0 = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, .txPower = DEFAULT_TX_POWER, /* 5 dBm default */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.c index 8ee2b2684..21ed73201 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.c @@ -46,7 +46,7 @@ #include DeviceFamily_constructPath(driverlib/rf_mailbox.h) #include DeviceFamily_constructPath(driverlib/rf_common_cmd.h) #include DeviceFamily_constructPath(driverlib/rf_ble_cmd.h) -#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_bt5.h) +#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_multi_protocol.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_rfe_bt5.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_mce_bt5.h) @@ -58,11 +58,17 @@ RF_Mode rf_ble_mode = { .rfMode = RF_MODE_AUTO, - .cpePatchFxn = &rf_patch_cpe_bt5, + .cpePatchFxn = &rf_patch_cpe_multi_protocol, .mcePatchFxn = &rf_patch_mce_bt5, .rfePatchFxn = &rf_patch_rfe_bt5, }; /*---------------------------------------------------------------------------*/ +/* + * CMD_RADIO_SETUP must be configured with default TX power value + * in the .txPower field. + */ +#define DEFAULT_TX_POWER 0x941E /* 5 dBm */ +/*---------------------------------------------------------------------------*/ /* Overrides for CMD_BLE5_RADIO_SETUP */ uint32_t rf_ble_overrides_common[] CC_ALIGN(4) = { @@ -137,71 +143,39 @@ rfc_CMD_BLE5_RADIO_SETUP_t rf_ble_cmd_radio_setup = .defaultPhy.mainMode = 0x0, .defaultPhy.coding = 0x0, .loDivider = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, - .txPower = 0x941E, + .txPower = DEFAULT_TX_POWER, .pRegOverrideCommon = rf_ble_overrides_common, .pRegOverride1Mbps = rf_ble_overrides_1mbps, .pRegOverride2Mbps = rf_ble_overrides_2mbps, .pRegOverrideCoded = rf_ble_overrides_coded, }; /*---------------------------------------------------------------------------*/ -/* Structure for CMD_BLE5_ADV_NC.pParams */ -rfc_bleAdvPar_t rf_ble_adv_par = -{ - .pRxQ = 0, - .rxConfig.bAutoFlushIgnored = 0x0, - .rxConfig.bAutoFlushCrcErr = 0x0, - .rxConfig.bAutoFlushEmpty = 0x0, - .rxConfig.bIncludeLenByte = 0x0, - .rxConfig.bIncludeCrc = 0x0, - .rxConfig.bAppendRssi = 0x0, - .rxConfig.bAppendStatus = 0x0, - .rxConfig.bAppendTimestamp = 0x0, - .advConfig.advFilterPolicy = 0x0, - .advConfig.deviceAddrType = 0x0, - .advConfig.peerAddrType = 0x0, - .advConfig.bStrictLenFilter = 0x0, - .advConfig.rpaMode = 0x0, - .advLen = 0x18, - .scanRspLen = 0x00, - .pAdvData = 0, - .pScanRspData = 0, - .pDeviceAddress = 0, - .pWhiteList = 0, - .__dummy0 = 0x0000, - .__dummy1 = 0x00, - .endTrigger.triggerType = TRIG_NEVER, - .endTrigger.bEnaCmd = 0x0, - .endTrigger.triggerNo = 0x0, - .endTrigger.pastTrig = 0x0, - .endTime = 0x00000000, -}; -/*---------------------------------------------------------------------------*/ /* CMD_BLE5_ADV_NC: Bluetooth 5 Non-Connectable Advertiser Command */ rfc_CMD_BLE5_ADV_NC_t rf_ble_cmd_ble_adv_nc = { - .commandNo = 0x182D, - .status = 0x0000, - .pNextOp = 0, // INSERT APPLICABLE POINTER: (uint8_t*)&xxx + .commandNo = CMD_BLE5_ADV_NC, + .status = IDLE, + .pNextOp = 0, .startTime = 0x00000000, - .startTrigger.triggerType = 0x0, + .startTrigger.triggerType = TRIG_NOW, .startTrigger.bEnaCmd = 0x0, .startTrigger.triggerNo = 0x0, - .startTrigger.pastTrig = 0x0, - .condition.rule = 0x1, + .startTrigger.pastTrig = 0x1, + .condition.rule = 0x0, /* set by driver */ .condition.nSkip = 0x0, - .channel = 0x8C, - .whitening.init = 0x51, + .channel = 0x00, /* set by driver */ + .whitening.init = 0x00, /* set by driver */ .whitening.bOverride = 0x1, .phyMode.mainMode = 0x0, .phyMode.coding = 0x0, .rangeDelay = 0x00, .txPower = 0x0000, - .pParams = &rf_ble_adv_par, - .pOutput = 0, // INSERT APPLICABLE POINTER: (uint8_t*)&xxx + .pParams = 0x00000000, /* set by driver */ + .pOutput = 0x00000000, /* set by driver */ .tx20Power = 0x00000000, }; /*---------------------------------------------------------------------------*/ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.h index 2e6defa71..c5295c868 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ble-settings.h @@ -43,7 +43,6 @@ extern RF_Mode rf_ble_mode; /*---------------------------------------------------------------------------*/ /* RF Core API commands */ extern rfc_CMD_BLE5_RADIO_SETUP_t rf_ble_cmd_radio_setup; -extern rfc_bleAdvPar_t rf_ble_adv_par; extern rfc_CMD_BLE5_ADV_NC_t rf_ble_cmd_ble_adv_nc; /*---------------------------------------------------------------------------*/ /* RF Core API Overrides */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ieee-settings.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ieee-settings.c index cc012da89..d95ff87ba 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ieee-settings.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf-settings/cc26x2/ieee-settings.c @@ -44,7 +44,7 @@ #include DeviceFamily_constructPath(driverlib/rf_mailbox.h) #include DeviceFamily_constructPath(driverlib/rf_common_cmd.h) #include DeviceFamily_constructPath(driverlib/rf_ieee_cmd.h) -#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_ieee_802_15_4.h) +#include DeviceFamily_constructPath(rf_patches/rf_patch_cpe_multi_protocol.h) #include DeviceFamily_constructPath(rf_patches/rf_patch_mce_ieee_802_15_4.h) #include @@ -55,7 +55,7 @@ RF_Mode rf_ieee_mode = { .rfMode = RF_MODE_AUTO, - .cpePatchFxn = &rf_patch_cpe_ieee_802_15_4, + .cpePatchFxn = &rf_patch_cpe_multi_protocol, .mcePatchFxn = &rf_patch_mce_ieee_802_15_4, .rfePatchFxn = 0, }; @@ -104,8 +104,8 @@ rfc_CMD_RADIO_SETUP_t rf_cmd_ieee_radio_setup = .condition.nSkip = 0x0, .mode = 0x01, .loDivider = 0x00, - .config.frontEndMode = 0x0, - .config.biasMode = 0x0, + .config.frontEndMode = 0x0, /* set by driver */ + .config.biasMode = 0x0, /* set by driver */ .config.analogCfgMode = 0x0, .config.bNoFsPowerUp = 0x0, .txPower = DEFAULT_TX_POWER, diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.c index 357e7e7c5..7a445a229 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.c @@ -81,7 +81,7 @@ ble_addr_ptr(void) } /*---------------------------------------------------------------------------*/ int -ble_addr_cpy(uint8_t *dst) +ble_addr_be_cpy(uint8_t *dst) { if(!dst) { return -1; @@ -102,6 +102,23 @@ ble_addr_cpy(uint8_t *dst) } /*---------------------------------------------------------------------------*/ int +ble_addr_le_cpy(uint8_t *dst) +{ + if(!dst) { + return -1; + } + + volatile const uint8_t *const ble_addr = ble_addr_ptr(); + + size_t i; + for(i = 0; i < BLE_ADDR_SIZE; i++) { + dst[i] = ble_addr[i]; + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +int ble_addr_to_eui64(uint8_t *dst, uint8_t *src) { if(!dst || !src) { @@ -126,7 +143,7 @@ ble_addr_to_eui64_cpy(uint8_t *dst) int res; uint8_t ble_addr[BLE_ADDR_SIZE]; - res = ble_addr_cpy(ble_addr); + res = ble_addr_le_cpy(ble_addr); if(res) { return -1; } diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.h index fd63c17d0..effc95d22 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-addr.h @@ -59,6 +59,7 @@ uint8_t *ble_addr_ptr(void); /*---------------------------------------------------------------------------*/ /** * \brief Copy the node's factory BLE address to a destination memory area + * in big-endian (be) order. * \param dst A pointer to the destination area where the BLE address is to be * written * \return 0 : Returned successfully @@ -68,7 +69,21 @@ uint8_t *ble_addr_ptr(void); * the process. The factory address on devices is normally little-endian, * therefore you should expect dst to store the address in a big-endian order. */ -int ble_addr_cpy(uint8_t *dst); +int ble_addr_be_cpy(uint8_t *dst); +/*---------------------------------------------------------------------------*/ +/** + * \brief Copy the node's factory BLE address to a destination memory area + * in little-endian (le) order. + * \param dst A pointer to the destination area where the BLE address is to be + * written + * \return 0 : Returned successfully + * -1 : Returned with error + * + * This function will copy 6 bytes, but will **not** invert the byte order. + * This is usefull for the RF core which assumes the BLE MAC address in + * little-endian order. + */ +int ble_addr_le_cpy(uint8_t *dst); /*---------------------------------------------------------------------------*/ /** * \brief Copy the node's BLE address to a destination memory area and converts diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-beacond.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-beacond.c index 383d168be..2e8d9d4bc 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-beacond.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ble-beacond.c @@ -52,6 +52,7 @@ #include /*---------------------------------------------------------------------------*/ +#include "rf/rf.h" #include "rf/sched.h" #include "rf/ble-addr.h" #include "rf/ble-beacond.h" @@ -70,32 +71,58 @@ /*---------------------------------------------------------------------------*/ #if RF_CONF_BLE_BEACON_ENABLE /*---------------------------------------------------------------------------*/ -/* BLE Advertisement channels. Not to be changed by the user. */ -typedef enum { - BLE_ADV_CHANNEL_37 = (1 << 0), - BLE_ADV_CHANNEL_38 = (1 << 1), - BLE_ADV_CHANNEL_39 = (1 << 2), - - BLE_ADV_CHANNEL_ALL = (BLE_ADV_CHANNEL_37 | - BLE_ADV_CHANNEL_38 | - BLE_ADV_CHANNEL_39), -} ble_adv_channel_t; - -#define BLE_ADV_CHANNEL_MIN 37 -#define BLE_ADV_CHANNEL_MAX 39 -/*---------------------------------------------------------------------------*/ /* Maximum BLE advertisement size. Not to be changed by the user. */ #define BLE_ADV_MAX_SIZE 31 /*---------------------------------------------------------------------------*/ -/* BLE Intervals: Send a burst of advertisements every BLE_ADV_INTERVAL secs */ -#define BLE_ADV_INTERVAL (CLOCK_SECOND * 5) -#define BLE_ADV_DUTY_CYCLE (CLOCK_SECOND / 10) -#define BLE_ADV_MESSAGES 10 +/* + * BLE Intervals: Send a burst of advertisements every BLE_ADV_INTERVAL + * specified in milliseconds. + */ +#define BLE_ADV_INTERVAL ((100 * CLOCK_SECOND) / 1000) + +/* GAP Advertisement data types */ +#define BLE_ADV_TYPE_FLAGS 0x01 +#define BLE_ADV_TYPE_16BIT_MORE 0x02 +#define BLE_ADV_TYPE_16BIT_COMPLETE 0x03 +#define BLE_ADV_TYPE_32BIT_MORE 0x04 +#define BLE_ADV_TYPE_32BIT_COMPLETE 0x05 +#define BLE_ADV_TYPE_128BIT_MORE 0x06 +#define BLE_ADV_TYPE_128BIT_COMPLETE 0x07 +#define BLE_ADV_TYPE_LOCAL_NAME_SHORT 0x08 +#define BLE_ADV_TYPE_LOCAL_NAME_COMPLETE 0x09 +#define BLE_ADV_TYPE_POWER_LEVEL 0x0A +#define BLE_ADV_TYPE_OOB_CLASS_OF_DEVICE 0x0D +#define BLE_ADV_TYPE_OOB_SIMPLE_PAIRING_HASHC 0x0E +#define BLE_ADV_TYPE_OOB_SIMPLE_PAIRING_RANDR 0x0F +#define BLE_ADV_TYPE_SM_TK 0x10 +#define BLE_ADV_TYPE_SM_OOB_FLAG 0x11 +#define BLE_ADV_TYPE_SLAVE_CONN_INTERVAL_RANGE 0x12 +#define BLE_ADV_TYPE_SIGNED_DATA 0x13 +#define BLE_ADV_TYPE_SERVICE_LIST_16BIT 0x14 +#define BLE_ADV_TYPE_SERVICE_LIST_128BIT 0x15 +#define BLE_ADV_TYPE_SERVICE_DATA 0x16 +#define BLE_ADV_TYPE_PUBLIC_TARGET_ADDR 0x17 +#define BLE_ADV_TYPE_RANDOM_TARGET_ADDR 0x18 +#define BLE_ADV_TYPE_APPEARANCE 0x19 +#define BLE_ADV_TYPE_ADV_INTERVAL 0x1A +#define BLE_ADV_TYPE_LE_BD_ADDR 0x1B +#define BLE_ADV_TYPE_LE_ROLE 0x1C +#define BLE_ADV_TYPE_SIMPLE_PAIRING_HASHC_256 0x1D +#define BLE_ADV_TYPE_SIMPLE_PAIRING_RANDR_256 0x1E +#define BLE_ADV_TYPE_SERVICE_DATA_32BIT 0x20 +#define BLE_ADV_TYPE_SERVICE_DATA_128BIT 0x21 +#define BLE_ADV_TYPE_3D_INFO_DATA 0x3D +#define BLE_ADV_TYPE_MANUFACTURER_SPECIFIC 0xFF + +/* GAP Advertisement data type flags */ + +/* Discovery Mode: LE Limited Discoverable Mode */ +#define BLE_ADV_TYPE_FLAGS_LIMITED 0x01 +/* Discovery Mode: LE General Discoverable Mode */ +#define BLE_ADV_TYPE_FLAGS_GENERAL 0x02 +/* Discovery Mode: BR/EDR Not Supported */ +#define BLE_ADV_TYPE_FLAGS_BREDR_NOT_SUPPORTED 0x04 -/* BLE Advertisement-related macros */ -#define BLE_ADV_TYPE_DEVINFO 0x01 -#define BLE_ADV_TYPE_NAME 0x09 -#define BLE_ADV_TYPE_MANUFACTURER 0xFF #define BLE_ADV_NAME_BUF_LEN BLE_ADV_MAX_SIZE #define BLE_ADV_PAYLOAD_BUF_LEN 64 #define BLE_UUID_SIZE 16 @@ -117,6 +144,11 @@ typedef struct { /* RF driver */ RF_Handle rf_handle; + + /* BLE command specific structures. Common accross BLE and BLE5. */ + uint8_t ble_mac_addr[6]; + rfc_bleAdvPar_t ble_adv_par; + rfc_bleAdvOutput_t ble_adv_output; } ble_beacond_t; static ble_beacond_t ble_beacond; @@ -126,13 +158,13 @@ PROCESS(ble_beacond_process, "RF BLE Beacon Daemon Process"); rf_ble_beacond_result_t rf_ble_beacond_init(void) { - ble_adv_par.pDeviceAddress = (uint16_t *)ble_addr_ptr(); + ble_cmd_radio_setup.config.frontEndMode = RF_2_4_GHZ_FRONT_END_MODE; + ble_cmd_radio_setup.config.biasMode = RF_2_4_GHZ_BIAS_MODE; RF_Params rf_params; RF_Params_init(&rf_params); - /* Should immediately turn off radio if possible */ - rf_params.nInactivityTimeout = 0; + rf_params.nInactivityTimeout = RF_CONF_INACTIVITY_TIMEOUT; ble_beacond.rf_handle = ble_open(&rf_params); @@ -140,6 +172,18 @@ rf_ble_beacond_init(void) return RF_BLE_BEACOND_ERROR; } + /* + * It is important that the contents of the BLE MAC address is copied into + * RAM, as the System CPU, and subsequently flash, goes idle when pending + * on an RF command. This causes pend to hang forever. + */ + ble_addr_le_cpy(ble_beacond.ble_mac_addr); + ble_beacond.ble_adv_par.pDeviceAddress = (uint16_t *)ble_beacond.ble_mac_addr; + ble_beacond.ble_adv_par.endTrigger.triggerType = TRIG_NEVER; + + rf_ble_cmd_ble_adv_nc.pParams = &ble_beacond.ble_adv_par; + rf_ble_cmd_ble_adv_nc.pOutput = &ble_beacond.ble_adv_output; + return RF_BLE_BEACOND_OK; } /*---------------------------------------------------------------------------*/ @@ -154,12 +198,14 @@ rf_ble_beacond_config(clock_time_t interval, const char *name) ble_beacond.ble_adv_interval = interval; res = RF_BLE_BEACOND_OK; + } else { + ble_beacond.ble_adv_interval = BLE_ADV_INTERVAL; } if(name != NULL) { const size_t name_len = strlen(name); - if((name_len == 0) || (name_len >= BLE_ADV_NAME_BUF_LEN)) { + if((0 < name_len) && (name_len < BLE_ADV_NAME_BUF_LEN)) { ble_beacond.adv_name_len = name_len; memcpy(ble_beacond.adv_name, name, name_len); @@ -235,37 +281,9 @@ rf_ble_get_tx_power(void) return dbm; } /*---------------------------------------------------------------------------*/ -static rf_ble_beacond_result_t -ble_beacon_burst(uint8_t channels_bm, uint8_t *data, uint8_t len) -{ - rf_result_t res; - - uint8_t channel; - for(channel = BLE_ADV_CHANNEL_MIN; channel <= BLE_ADV_CHANNEL_MAX; ++channel) { - const uint8_t channel_bv = (1 << (channel - BLE_ADV_CHANNEL_MIN)); - if((channel_bv & channels_bm) == 0) { - continue; - } - - ble_adv_par.advLen = len; - ble_adv_par.pAdvData = data; - - ble_cmd_beacon.channel = channel; - - res = ble_sched_beacon(NULL, 0); - - if(res != RF_RESULT_OK) { - return RF_BLE_BEACOND_ERROR; - } - } - - return RF_BLE_BEACOND_OK; -} -/*---------------------------------------------------------------------------*/ PROCESS_THREAD(ble_beacond_process, ev, data) { - static size_t i; - static size_t len; + size_t len; PROCESS_BEGIN(); @@ -278,30 +296,33 @@ PROCESS_THREAD(ble_beacond_process, ev, data) PROCESS_EXIT(); } + /* Device info */ /* Set the adv payload each pass: The device name may have changed */ len = 0; - /* Device info */ - ble_beacond.tx_buf[len++] = (uint8_t)0x02; /* 2 bytes */ - ble_beacond.tx_buf[len++] = (uint8_t)BLE_ADV_TYPE_DEVINFO; - ble_beacond.tx_buf[len++] = (uint8_t)0x1A; /* LE general discoverable + BR/EDR */ - ble_beacond.tx_buf[len++] = (uint8_t)ble_beacond.adv_name_len; - ble_beacond.tx_buf[len++] = (uint8_t)BLE_ADV_TYPE_NAME; + #define append_byte(x) ble_beacond.tx_buf[len++] = (uint8_t)((x)) + + /* 2 bytes */ + append_byte(2); + append_byte(BLE_ADV_TYPE_FLAGS); + /* LE general discoverable + BR/EDR not supported */ + append_byte(BLE_ADV_TYPE_FLAGS_GENERAL | + BLE_ADV_TYPE_FLAGS_BREDR_NOT_SUPPORTED); + + /* 1 + len(name) bytes (excluding zero termination) */ + append_byte(1 + ble_beacond.adv_name_len); + append_byte(BLE_ADV_TYPE_LOCAL_NAME_COMPLETE); memcpy(ble_beacond.tx_buf + len, ble_beacond.adv_name, ble_beacond.adv_name_len); len += ble_beacond.adv_name_len; - /* - * Send BLE_ADV_MESSAGES beacon bursts. Each burst on all three - * channels, with a BLE_ADV_DUTY_CYCLE interval between bursts - */ - ble_beacon_burst(BLE_ADV_CHANNEL_ALL, ble_beacond.tx_buf, len); - for(i = 1; i < BLE_ADV_MESSAGES; ++i) { - etimer_set(&ble_beacond.ble_adv_et, BLE_ADV_DUTY_CYCLE); - PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&ble_beacond.ble_adv_et)); + #undef append_byte - ble_beacon_burst(BLE_ADV_CHANNEL_ALL, ble_beacond.tx_buf, len); - } + /* Send advertisements on all three channels */ + ble_beacond.ble_adv_par.advLen = len; + ble_beacond.ble_adv_par.pAdvData = ble_beacond.tx_buf; + + ble_sched_beacons(BLE_ADV_CHANNEL_ALL); } PROCESS_END(); } diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ieee-mode.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ieee-mode.c index d9f3a5acf..6f21c72ba 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/ieee-mode.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/ieee-mode.c @@ -217,6 +217,9 @@ rat_overflow_cb(void *arg) static void init_rf_params(void) { + cmd_radio_setup.config.frontEndMode = RF_2_4_GHZ_FRONT_END_MODE; + cmd_radio_setup.config.biasMode = RF_2_4_GHZ_BIAS_MODE; + data_queue_t *rx_q = data_queue_init(sizeof(lensz_t)); cmd_rx.pRxQ = rx_q; @@ -550,32 +553,42 @@ read(void *buf, unsigned short buf_len) static rf_result_t cca_request(cmd_cca_req_t *cmd_cca_req) { + RF_Stat stat = RF_StatRadioInactiveError; rf_result_t res; + bool stop_rx = false; - const bool rx_is_idle = !rx_is_active(); + /* RX is required to be running in order to do a CCA request */ + if(!rx_is_active()) { + /* If RX is not pending, i.e. soon to be running, schedule the RX command */ + if(cmd_rx.status != PENDING) { + res = netstack_sched_rx(false); + if(res != RF_RESULT_OK) { + LOG_ERR("CCA request failed to schedule RX\n"); + return res; + } - if(rx_is_idle) { - res = netstack_sched_rx(false); - if(res != RF_RESULT_OK) { + /* We only stop RX if we had to schedule it */ + stop_rx = true; + } + + /* Make sure RX is running before we continue, unless we timeout and fail */ + RTIMER_BUSYWAIT_UNTIL(!rx_is_active(), TIMEOUT_ENTER_RX_WAIT); + + if(!rx_is_active()) { + LOG_ERR("CCA request failed to turn on RX, RX status=0x%04X\n", cmd_rx.status); return RF_RESULT_ERROR; } } - const rtimer_clock_t t0 = RTIMER_NOW(); - while((cmd_rx.status != ACTIVE) && - RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + TIMEOUT_ENTER_RX_WAIT)) ; + /* Perform the CCA request */ + stat = RF_runImmediateCmd(ieee_radio.rf_handle, (uint32_t *)&cmd_cca_req); - RF_Stat stat = RF_StatRadioInactiveError; - if(rx_is_active()) { - stat = RF_runImmediateCmd(ieee_radio.rf_handle, (uint32_t *)&cmd_cca_req); - } - - if(rx_is_idle) { + if(stop_rx) { netstack_stop_rx(); } if(stat != RF_StatCmdDoneSuccess) { - LOG_ERR("CCA request failed, stat=0x%02X\n", stat); + LOG_ERR("CCA request command failed, stat=0x%02X\n", stat); return RF_RESULT_ERROR; } diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/prop-mode.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf/prop-mode.c index a100f262a..241ae986d 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/prop-mode.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/prop-mode.c @@ -61,6 +61,7 @@ #include /*---------------------------------------------------------------------------*/ /* Platform RF dev */ +#include "rf/rf.h" #include "rf/dot-15-4g.h" #include "rf/sched.h" #include "rf/data-queue.h" @@ -193,6 +194,8 @@ static int off(void); static void init_rf_params(void) { + cmd_radio_setup.config.frontEndMode = RF_SUB_1_GHZ_FRONT_END_MODE; + cmd_radio_setup.config.biasMode = RF_SUB_1_GHZ_BIAS_MODE; cmd_radio_setup.centerFreq = PROP_MODE_CENTER_FREQ; cmd_radio_setup.loDivider = PROP_MODE_LO_DIVIDER; @@ -207,26 +210,36 @@ static int8_t get_rssi(void) { rf_result_t res; + bool stop_rx = false; + int8_t rssi = RF_GET_RSSI_ERROR_VAL; - const bool rx_is_idle = !rx_is_active(); + /* RX is required to be running in order to do a RSSI measurement */ + if(!rx_is_active()) { + /* If RX is not pending, i.e. soon to be running, schedule the RX command */ + if(cmd_rx.status != PENDING) { + res = netstack_sched_rx(false); + if(res != RF_RESULT_OK) { + LOG_ERR("RSSI measurement failed to schedule RX\n"); + return res; + } - if(rx_is_idle) { - res = netstack_sched_rx(false); - if(res != RF_RESULT_OK) { - return RF_GET_RSSI_ERROR_VAL; + /* We only stop RX if we had to schedule it */ + stop_rx = true; + } + + /* Make sure RX is running before we continue, unless we timeout and fail */ + RTIMER_BUSYWAIT_UNTIL(!rx_is_active(), TIMEOUT_ENTER_RX_WAIT); + + if(!rx_is_active()) { + LOG_ERR("RSSI measurement failed to turn on RX, RX status=0x%04X\n", cmd_rx.status); + return RF_RESULT_ERROR; } } - const rtimer_clock_t t0 = RTIMER_NOW(); - while((cmd_rx.status != ACTIVE) && - RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + TIMEOUT_ENTER_RX_WAIT)) ; + /* Perform the RSSI measurement */ + rssi = RF_getRssi(prop_radio.rf_handle); - int8_t rssi = RF_GET_RSSI_ERROR_VAL; - if(rx_is_active()) { - rssi = RF_getRssi(prop_radio.rf_handle); - } - - if(rx_is_idle) { + if(stop_rx) { netstack_stop_rx(); } diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/rf.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf/rf.h index cfc6b2277..65b2b1543 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/rf.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/rf.h @@ -60,6 +60,37 @@ RF_MODE_2_4_GHZ) /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \name The different front-end modes the CC13xx/CC26xx devices support. The + * front-end mode can be configured independently of the bias mode. The + * two types of modes are as follows: + * - Differential: Both RF_P and RF_N are used as a differential RF + * interface. + * - Single ended: Either the RF_P pin or the RF_N pin is used as the + * RF path. + * + * @{ + */ +/* Available front-end mode configurations */ +#define RF_FRONT_END_MODE_DIFFERENTIAL 0 +#define RF_FRONT_END_MODE_SINGLE_ENDED_RFP 1 +#define RF_FRONT_END_MODE_SINGLE_ENDED_RFN 2 +/** @} */ +/*---------------------------------------------------------------------------*/ +/** + * \name The different bias modes the CC13xx/CC26xx devices support. The + * bias mode can be configured independently of the front-end mode. The + * two different modes are as follows: + * - Internal bias: the LNA is biased by an internal bias. + * - External bias: the LNA is biased by an external bias. + * + * @{ + */ +/* Available bias mode configurations */ +#define RF_BIAS_MODE_INTERNAL 0 +#define RF_BIAS_MODE_EXTERNAL 1 +/** @} */ +/*---------------------------------------------------------------------------*/ #endif /* RF_CORE_H_ */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.c b/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.c index 44387c138..f15d33d2f 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.c +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.c @@ -51,6 +51,7 @@ #include #include DeviceFamily_constructPath(driverlib/rf_common_cmd.h) #include DeviceFamily_constructPath(driverlib/rf_mailbox.h) +#include DeviceFamily_constructPath(driverlib/rf_ble_mailbox.h) #include /*---------------------------------------------------------------------------*/ @@ -68,8 +69,6 @@ #define LOG_MODULE "Radio" #define LOG_LEVEL LOG_LEVEL_NONE /*---------------------------------------------------------------------------*/ -/* Configuration parameters */ -/*---------------------------------------------------------------------------*/ #define CMD_FS_RETRIES 3 #define RF_EVENTS_CMD_DONE (RF_EventCmdDone | RF_EventLastCmdDone | \ @@ -82,6 +81,13 @@ #define EVENTS_CMD_DONE(events) (((events) & RF_EVENTS_CMD_DONE) != 0) /*---------------------------------------------------------------------------*/ +/* BLE advertisement channel range (inclusive) */ +#define BLE_ADV_CHANNEL_MIN 37 +#define BLE_ADV_CHANNEL_MAX 39 + +/* Number of BLE advertisement channels */ +#define NUM_BLE_ADV_CHANNELS (BLE_ADV_CHANNEL_MAX - BLE_ADV_CHANNEL_MIN + 1) +/*---------------------------------------------------------------------------*/ /* Synth re-calibration every 3 minutes */ #define SYNTH_RECAL_INTERVAL (CLOCK_SECOND * 60 * 3) /* Set re-calibration interval with a jitter of 10 seconds */ @@ -89,6 +95,12 @@ static struct etimer synth_recal_timer; /*---------------------------------------------------------------------------*/ +#if (DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X0_CC26X0) +typedef rfc_CMD_BLE_ADV_NC_t ble_cmd_adv_nc_t; +#elif (DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X2_CC26X2) +typedef rfc_CMD_BLE5_ADV_NC_t ble_cmd_adv_nc_t; +#endif +/*---------------------------------------------------------------------------*/ static RF_Object rf_netstack; #if RF_CONF_BLE_BEACON_ENABLE @@ -479,45 +491,171 @@ ble_open(RF_Params *params) #endif } /*---------------------------------------------------------------------------*/ +#if RF_CONF_BLE_BEACON_ENABLE +static RF_Op * +init_ble_adv_array(ble_cmd_adv_nc_t *ble_adv_array, uint8_t bm_channel) +{ + RF_Op *first_ble_adv = NULL; + ble_cmd_adv_nc_t *cmd_adv_37 = &ble_adv_array[0]; + ble_cmd_adv_nc_t *cmd_adv_38 = &ble_adv_array[1]; + ble_cmd_adv_nc_t *cmd_adv_39 = &ble_adv_array[2]; + + /* Setup channel 37 advertisement if enabled */ + if(bm_channel & BLE_ADV_CHANNEL_37) { + /* Default configurations from ble_cmd_adv_nc */ + memcpy(cmd_adv_37, &ble_cmd_adv_nc, sizeof(ble_cmd_adv_nc)); + + cmd_adv_37->channel = 37; + /* Magic number: initialization for whitener, specific for channel 37 */ + cmd_adv_37->whitening.init = 0x65; + + /* + * The next advertisement is chained depending on whether they are + * enbled or not. If both 38 and 39 are disabled, then there is no + * chaining. + */ + if(bm_channel & BLE_ADV_CHANNEL_38) { + cmd_adv_37->pNextOp = (RF_Op *)cmd_adv_38; + cmd_adv_37->condition.rule = COND_ALWAYS; + } else if(bm_channel & BLE_ADV_CHANNEL_39) { + cmd_adv_37->pNextOp = (RF_Op *)cmd_adv_39; + cmd_adv_37->condition.rule = COND_ALWAYS; + } else { + cmd_adv_37->pNextOp = NULL; + cmd_adv_37->condition.rule = COND_NEVER; + } + + /* Channel 37 will always be first if enabled */ + first_ble_adv = (RF_Op *)cmd_adv_37; + } + + /* Setup channel 38 advertisement if enabled */ + if(bm_channel & BLE_ADV_CHANNEL_38) { + memcpy(cmd_adv_38, &ble_cmd_adv_nc, sizeof(ble_cmd_adv_nc)); + + cmd_adv_38->channel = 38; + /* Magic number: initialization for whitener, specific for channel 38 */ + cmd_adv_38->whitening.init = 0x66; + + /* + * The next advertisement is chained depending on whether they are + * enbled or not. If 39 is disabled, then there is no chaining. + */ + if(bm_channel & BLE_ADV_CHANNEL_39) { + cmd_adv_38->pNextOp = (RF_Op *)cmd_adv_39; + cmd_adv_38->condition.rule = COND_ALWAYS; + } else { + cmd_adv_38->pNextOp = NULL; + cmd_adv_38->condition.rule = COND_NEVER; + } + + /* + * Channel 38 is only first if the first_ble_adv pointer is not + * set by channel 37. + */ + if(first_ble_adv == NULL) { + first_ble_adv = (RF_Op *)cmd_adv_38; + } + } + + /* Setup channel 39 advertisement if enabled */ + if(bm_channel & BLE_ADV_CHANNEL_39) { + memcpy(cmd_adv_39, &ble_cmd_adv_nc, sizeof(ble_cmd_adv_nc)); + + cmd_adv_39->channel = 39; + /* Magic number: initialization for whitener, specific for channel 39 */ + cmd_adv_39->whitening.init = 0x67; + + /* Channel 39 is always the last advertisement in the chain */ + cmd_adv_39->pNextOp = NULL; + cmd_adv_39->condition.rule = COND_NEVER; + + /* + * Channel 39 is only first if the first_ble_adv pointer is not + * set by channel 37 or channel 38. + */ + if(first_ble_adv == NULL) { + first_ble_adv = (RF_Op *)cmd_adv_39; + } + } + + return first_ble_adv; +} +#endif /* RF_CONF_BLE_BEACON_ENABLE */ +/*---------------------------------------------------------------------------*/ rf_result_t -ble_sched_beacon(RF_Callback cb, RF_EventMask bm_event) +ble_sched_beacons(uint8_t bm_channel) { #if RF_CONF_BLE_BEACON_ENABLE - RF_ScheduleCmdParams sched_params; - RF_ScheduleCmdParams_init(&sched_params); + /* + * Allocate the advertisement commands on the stack rather than statically + * to RAM in order to save space. We don't need them after the + * advertisements have been transmitted. + */ + ble_cmd_adv_nc_t ble_cmd_adv_nc_array[NUM_BLE_ADV_CHANNELS]; + RF_Op *initial_adv = NULL; + RF_ScheduleCmdParams sched_params; + RF_CmdHandle beacon_handle; + RF_EventMask beacon_events; + rf_result_t rf_result; + + /* If no channels are mapped, then early return OK */ + if((bm_channel & BLE_ADV_CHANNEL_ALL) == 0) { + return RF_RESULT_OK; + } + + initial_adv = init_ble_adv_array(ble_cmd_adv_nc_array, bm_channel); + + if(initial_adv == NULL) { + LOG_ERR("Initializing BLE Advertisement chain failed\n"); + return RF_RESULT_ERROR; + } + + RF_ScheduleCmdParams_init(&sched_params); sched_params.priority = RF_PriorityNormal; sched_params.endTime = 0; sched_params.allowDelay = RF_AllowDelayAny; - CMD_STATUS(ble_cmd_beacon) = PENDING; - - RF_CmdHandle beacon_handle = RF_scheduleCmd( - &rf_ble, - (RF_Op *)&ble_cmd_beacon, - &sched_params, - cb, - bm_event); + /* + * The most efficient way to schedule the command is as follows: + * 1. Schedule the BLE advertisement chain + * 2. Reschedule the RX command IF it was running. + * 3. Pend on the BLE avertisement chain + */ + beacon_handle = RF_scheduleCmd( + &rf_ble, + initial_adv, + &sched_params, + NULL, + 0); if(!CMD_HANDLE_OK(beacon_handle)) { LOG_ERR("Unable to schedule BLE Beacon command, handle=%d status=0x%04x\n", - beacon_handle, CMD_STATUS(ble_cmd_beacon)); + beacon_handle, CMD_STATUS(ble_cmd_adv_nc)); + return RF_RESULT_ERROR; } - const uint_fast8_t rx_key = cmd_rx_disable(); + /* Note that this only reschedules RX if it is running */ + rf_result = cmd_rx_restore(cmd_rx_disable()); /* Wait until Beacon operation finishes */ - RF_EventMask beacon_events = RF_pendCmd(&rf_ble, beacon_handle, 0); - if(!EVENTS_CMD_DONE(beacon_events)) { - LOG_ERR("Pending on scheduled BLE Beacon command generated error, events=0x%08llx status=0x%04x\n", - beacon_events, CMD_STATUS(ble_cmd_beacon)); + beacon_events = RF_pendCmd(&rf_ble, beacon_handle, 0); + + if(rf_result != RF_RESULT_OK) { + LOG_ERR("Rescheduling CMD_RX failed when BLE advertising\n"); + + return RF_RESULT_ERROR; + } + + if(!EVENTS_CMD_DONE(beacon_events)) { + LOG_ERR("Pending on scheduled BLE Beacon command generated error, events=0x%08llx status=0x%04x\n", + beacon_events, CMD_STATUS(ble_cmd_adv_nc)); - cmd_rx_restore(rx_key); return RF_RESULT_ERROR; } - cmd_rx_restore(rx_key); return RF_RESULT_OK; #else diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.h index de32dbbd4..fe4963036 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/sched.h @@ -58,6 +58,16 @@ typedef enum { RF_RESULT_ERROR, } rf_result_t; /*---------------------------------------------------------------------------*/ +typedef enum { + BLE_ADV_CHANNEL_37 = (1 << 0), + BLE_ADV_CHANNEL_38 = (1 << 1), + BLE_ADV_CHANNEL_39 = (1 << 2), + + BLE_ADV_CHANNEL_ALL = (BLE_ADV_CHANNEL_37 | + BLE_ADV_CHANNEL_38 | + BLE_ADV_CHANNEL_39), +} ble_adv_channel_t; +/*---------------------------------------------------------------------------*/ /** * \name Common RF scheduler functionality. * @@ -91,7 +101,7 @@ rf_result_t netstack_stop_rx(void); * @{ */ RF_Handle ble_open(RF_Params *params); -rf_result_t ble_sched_beacon(RF_Callback cb, RF_EventMask bm_event); +rf_result_t ble_sched_beacons(uint8_t bm_adv_channel); /** @} */ /*---------------------------------------------------------------------------*/ #endif /* RF_SCHED_H_ */ diff --git a/arch/cpu/simplelink-cc13xx-cc26xx/rf/settings.h b/arch/cpu/simplelink-cc13xx-cc26xx/rf/settings.h index 174114bfb..1d20fce91 100644 --- a/arch/cpu/simplelink-cc13xx-cc26xx/rf/settings.h +++ b/arch/cpu/simplelink-cc13xx-cc26xx/rf/settings.h @@ -89,7 +89,7 @@ #define ble_mode rf_ble_mode #define ble_cmd_radio_setup rf_ble_cmd_radio_setup #define ble_adv_par rf_ble_adv_par -#define ble_cmd_beacon rf_ble_cmd_ble_adv_nc +#define ble_cmd_adv_nc rf_ble_cmd_ble_adv_nc /* CC13x2/CC26x2 devices */ #elif (DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X2_CC26X2) @@ -97,7 +97,7 @@ #define ble_mode rf_ble_mode #define ble_cmd_radio_setup rf_ble_cmd_radio_setup #define ble_adv_par rf_ble_adv_par -#define ble_cmd_beacon rf_ble_cmd_ble_adv_nc +#define ble_cmd_adv_nc rf_ble_cmd_ble_adv_nc #endif /* DeviceFamily_PARENT */ diff --git a/arch/dev/cc1200/cc1200.c b/arch/dev/cc1200/cc1200.c index 5f76e4817..dd90fdda7 100644 --- a/arch/dev/cc1200/cc1200.c +++ b/arch/dev/cc1200/cc1200.c @@ -310,15 +310,7 @@ extern const cc1200_rf_cfg_t CC1200_RF_CFG; #define LOCK_SPI() do { spi_locked++; } while(0) #define SPI_IS_LOCKED() (spi_locked != 0) #define RELEASE_SPI() do { spi_locked--; } while(0) -/*---------------------------------------------------------------------------*/ -#define BUSYWAIT_UNTIL(cond, max_time) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) { \ - watchdog_periodic(); \ - } \ - } while(0) + /*---------------------------------------------------------------------------*/ #if CC1200_USE_GPIO2 /* Configure GPIO interrupts. GPIO0: falling, GPIO2: rising edge */ @@ -378,29 +370,9 @@ extern const cc1200_rf_cfg_t CC1200_RF_CFG; #define INFO(...) #endif -#if DEBUG_LEVEL > 0 -/* - * As BUSYWAIT_UNTIL was mainly used to test for a state transition, - * we define a separate macro for this adding the possibility to - * throw an error message when the timeout exceeds - */ -#define BUSYWAIT_UNTIL_STATE(s, t) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while((state() != s) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (t))) {} \ - if(!(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (t)))) { \ - printf("RF: Timeout exceeded in line %d!\n", __LINE__); \ - } \ - } while(0) -#else -#define BUSYWAIT_UNTIL_STATE(s, t) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while((state() != s) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (t))) {} \ - } while(0) -#endif +/* Busy-wait (time-bounded) until the radio reaches a given state */ +#define RTIMER_BUSYWAIT_UNTIL_STATE(s, t) RTIMER_BUSYWAIT_UNTIL(state() == (s), t) + /*---------------------------------------------------------------------------*/ /* Variables */ /*---------------------------------------------------------------------------*/ @@ -604,7 +576,7 @@ PROCESS_THREAD(cc1200_process, ev, data) /* * We are on and not in TX. As every function of this driver * assures that we are in RX mode - * (using BUSYWAIT_UNTIL_STATE(STATE_RX, ...) construct) in + * (using RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, ...) construct) in * either rx_rx(), idle_calibrate_rx() or transmit(), * something probably went wrong in the rx interrupt handler * if we are not in RX at this point. @@ -893,7 +865,7 @@ transmit(unsigned short transmit_len) * again as they were turned off in idle() */ - BUSYWAIT_UNTIL_STATE(STATE_RX, + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, CC1200_RF_CFG.tx_rx_turnaround); ENABLE_GPIO_INTERRUPTS(); @@ -1033,7 +1005,7 @@ channel_clear(void) } /* Wait for CARRIER_SENSE_VALID signal */ - BUSYWAIT_UNTIL(((rssi0 = single_read(CC1200_RSSI0)) + RTIMER_BUSYWAIT_UNTIL(((rssi0 = single_read(CC1200_RSSI0)) & CC1200_CARRIER_SENSE_VALID), RTIMER_SECOND / 100); RF_ASSERT(rssi0 & CC1200_CARRIER_SENSE_VALID); @@ -1135,7 +1107,7 @@ on(void) /* Wake-up procedure. Wait for GPIO0 to de-assert (CHIP_RDYn) */ cc1200_arch_spi_select(); - BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0), + RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0), RTIMER_SECOND / 100); RF_ASSERT((cc1200_arch_gpio0_read_pin() == 0)); cc1200_arch_spi_deselect(); @@ -1239,7 +1211,7 @@ get_rssi(void) } /* Wait for CARRIER_SENSE_VALID signal */ - BUSYWAIT_UNTIL(((rssi0 = single_read(CC1200_RSSI0)) + RTIMER_BUSYWAIT_UNTIL(((rssi0 = single_read(CC1200_RSSI0)) & CC1200_CARRIER_SENSE_VALID), RTIMER_SECOND / 100); RF_ASSERT(rssi0 & CC1200_CARRIER_SENSE_VALID); @@ -1706,20 +1678,20 @@ configure(void) while(1) { #if (CC1200_RF_TESTMODE == 1) watchdog_periodic(); - BUSYWAIT_UNTIL(0, RTIMER_SECOND / 10); + RTIMER_BUSYWAIT(RTIMER_SECOND / 10); leds_off(LEDS_YELLOW); leds_on(LEDS_RED); watchdog_periodic(); - BUSYWAIT_UNTIL(0, RTIMER_SECOND / 10); + RTIMER_BUSYWAIT(RTIMER_SECOND / 10); leds_off(LEDS_RED); leds_on(LEDS_YELLOW); #else watchdog_periodic(); - BUSYWAIT_UNTIL(0, RTIMER_SECOND / 10); + RTIMER_BUSYWAIT(RTIMER_SECOND / 10); leds_off(LEDS_GREEN); leds_on(LEDS_RED); watchdog_periodic(); - BUSYWAIT_UNTIL(0, RTIMER_SECOND / 10); + RTIMER_BUSYWAIT(RTIMER_SECOND / 10); leds_off(LEDS_RED); leds_on(LEDS_GREEN); #endif @@ -1741,11 +1713,11 @@ configure(void) while(1) { watchdog_periodic(); - BUSYWAIT_UNTIL(0, RTIMER_SECOND / 10); + RTIMER_BUSYWAIT(RTIMER_SECOND / 10); leds_off(LEDS_GREEN); leds_on(LEDS_YELLOW); watchdog_periodic(); - BUSYWAIT_UNTIL(0, RTIMER_SECOND / 10); + RTIMER_BUSYWAIT(RTIMER_SECOND / 10); leds_off(LEDS_YELLOW); leds_on(LEDS_GREEN); clock_delay_usec(1000); @@ -1866,8 +1838,8 @@ calibrate(void) INFO("RF: Calibrate\n"); strobe(CC1200_SCAL); - BUSYWAIT_UNTIL_STATE(STATE_CALIBRATE, RTIMER_SECOND / 100); - BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_CALIBRATE, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100); #if CC1200_CAL_TIMEOUT_SECONDS cal_timer = clock_seconds(); @@ -1904,7 +1876,7 @@ idle(void) } strobe(CC1200_SIDLE); - BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100); } /* idle(), 21.05.2015 */ /*---------------------------------------------------------------------------*/ @@ -1922,7 +1894,7 @@ idle_calibrate_rx(void) rf_flags &= ~RF_RX_PROCESSING_PKT; strobe(CC1200_SFRX); strobe(CC1200_SRX); - BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100); ENABLE_GPIO_INTERRUPTS(); @@ -1947,7 +1919,7 @@ rx_rx(void) strobe(CC1200_SFTX); } else { strobe(CC1200_SIDLE); - BUSYWAIT_UNTIL_STATE(STATE_IDLE, + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100); } @@ -1959,7 +1931,7 @@ rx_rx(void) strobe(CC1200_SFRX); strobe(CC1200_SRX); - BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100); } /*---------------------------------------------------------------------------*/ @@ -2002,14 +1974,14 @@ idle_tx_rx(const uint8_t *payload, uint16_t payload_len) #if USE_SFSTXON /* Wait for synthesizer to be ready */ - BUSYWAIT_UNTIL_STATE(STATE_FSTXON, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_STATE(STATE_FSTXON, RTIMER_SECOND / 100); #endif /* Start TX */ strobe(CC1200_STX); /* Wait for TX to start. */ - BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 1), RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 1), RTIMER_SECOND / 100); /* Turned off at the latest in idle() */ TX_LEDS_ON(); @@ -2072,7 +2044,7 @@ idle_tx_rx(const uint8_t *payload, uint16_t payload_len) */ INFO("RF: TX failure!\n"); - BUSYWAIT_UNTIL((state() != STATE_TX), RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL((state() != STATE_TX), RTIMER_SECOND / 100); /* Re-configure GPIO2 */ single_write(CC1200_IOCFG2, GPIO2_IOCFG); idle(); @@ -2086,12 +2058,12 @@ idle_tx_rx(const uint8_t *payload, uint16_t payload_len) } else { /* Wait for TX to complete */ - BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0), + RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0), CC1200_RF_CFG.tx_pkt_lifetime); } #else /* Wait for TX to complete */ - BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0), + RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0), CC1200_RF_CFG.tx_pkt_lifetime); #endif diff --git a/arch/dev/cc2420/cc2420.c b/arch/dev/cc2420/cc2420.c index a1572b0ef..545f917ac 100644 --- a/arch/dev/cc2420/cc2420.c +++ b/arch/dev/cc2420/cc2420.c @@ -114,6 +114,7 @@ PROCESS(cc2420_process, "CC2420 driver"); #define CORR_THR(n) (((n) & 0x1f) << 6) #define FIFOP_THR(n) ((n) & 0x7f) #define RXBPF_LOCUR (1 << 13); +#define TX_MODE (3 << 2) int cc2420_on(void); int cc2420_off(void); @@ -135,6 +136,8 @@ static void set_poll_mode(uint8_t enable); static void set_send_on_cca(uint8_t enable); static void set_auto_ack(uint8_t enable); +static void set_test_mode(uint8_t enable, uint8_t modulated); + signed char cc2420_last_rssi; uint8_t cc2420_last_correlation; @@ -156,7 +159,11 @@ get_value(radio_param_t param, radio_value_t *value) } switch(param) { case RADIO_PARAM_POWER_MODE: - *value = receive_on ? RADIO_POWER_MODE_ON : RADIO_POWER_MODE_OFF; + if((getreg(CC2420_MDMCTRL1) & TX_MODE) & 0x08) { + *value = RADIO_POWER_MODE_CARRIER_ON; + } else { + *value = receive_on ? RADIO_POWER_MODE_ON : RADIO_POWER_MODE_OFF; + } return RADIO_RESULT_OK; case RADIO_PARAM_CHANNEL: *value = cc2420_get_channel(); @@ -237,6 +244,11 @@ set_value(radio_param_t param, radio_value_t value) cc2420_off(); return RADIO_RESULT_OK; } + if(value == RADIO_POWER_MODE_CARRIER_ON || + value == RADIO_POWER_MODE_CARRIER_OFF) { + set_test_mode((value == RADIO_POWER_MODE_CARRIER_ON), 0); + return RADIO_RESULT_OK; + } return RADIO_RESULT_INVALID_VALUE; case RADIO_PARAM_CHANNEL: if(value < 11 || value > 26) { @@ -1120,3 +1132,45 @@ set_send_on_cca(uint8_t enable) send_on_cca = enable; } /*---------------------------------------------------------------------------*/ +/* Enable or disable radio test mode emmiting modulated or unmodulated + * (carrier) signal. See datasheet page 55. + */ +static uint16_t prev_MDMCTRL1, prev_DACTST; +static uint8_t was_on; + +static void +set_test_mode(uint8_t enable, uint8_t modulated) +{ + radio_value_t mode; + get_value(RADIO_PARAM_POWER_MODE, &mode); + + if(enable) { + if(mode == RADIO_POWER_MODE_CARRIER_ON) { + return; + } + was_on = (mode == RADIO_POWER_MODE_ON); + off(); + prev_MDMCTRL1 = getreg(CC2420_MDMCTRL1); + setreg(CC2420_MDMCTRL1, 0x050C); + if(!modulated) { + prev_DACTST = getreg(CC2420_DACTST); + setreg(CC2420_DACTST, 0x1800); + } + /* actually starts the test mode */ + strobe(CC2420_STXON); + } else { + if(mode != RADIO_POWER_MODE_CARRIER_ON) { + return; + } + strobe(CC2420_SRFOFF); + if(!modulated) { + setreg(CC2420_DACTST, prev_DACTST); + } + setreg(CC2420_MDMCTRL1, prev_MDMCTRL1); + /* actually stops the carrier */ + if(was_on) { + on(); + } + } +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/cc2538dk/Makefile.cc2538dk b/arch/platform/cc2538dk/Makefile.cc2538dk index d80fd8d5f..bdbc7598f 100644 --- a/arch/platform/cc2538dk/Makefile.cc2538dk +++ b/arch/platform/cc2538dk/Makefile.cc2538dk @@ -14,7 +14,7 @@ CONTIKI_TARGET_SOURCEFILES += board-buttons.c als-sensor.c CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) ### Define the CPU directory -CONTIKI_CPU=$(CONTIKI)/arch/cpu/cc2538 +CONTIKI_CPU=$(ARCH_PATH)/cpu/cc2538 include $(CONTIKI_CPU)/Makefile.cc2538 MODULES += os/storage/cfs @@ -28,11 +28,12 @@ endif BSL = $(CONTIKI)/tools/cc2538-bsl/cc2538-bsl.py -%.upload: %.bin %.elf +%.upload: $(OUT_BIN) $(OUT_ELF) ifeq ($(wildcard $(BSL)), ) @echo "ERROR: Could not find the cc2538-bsl script. Did you run 'git submodule update --init' ?" else - $(eval BSL_ADDRESS_ARG := -a $(shell $(OBJDUMP) -h $*.elf | grep -B1 LOAD | \ + $(eval BSL_ADDRESS_ARG := -a $(shell $(OBJDUMP) -h \ + $(BUILD_DIR_BOARD)/$*.elf | grep -B1 LOAD | \ grep -Ev 'LOAD|\-\-' | awk '{print "0x" $$5}' | \ sort -g | head -1)) $(PYTHON) $(BSL) $(BSL_FLAGS) $(BSL_ADDRESS_ARG) $< diff --git a/arch/platform/cc2538dk/dev/board.h b/arch/platform/cc2538dk/dev/board.h index 80f036fd7..2540b7ce7 100644 --- a/arch/platform/cc2538dk/dev/board.h +++ b/arch/platform/cc2538dk/dev/board.h @@ -220,6 +220,18 @@ #endif /* #if SPI1_IN_USE */ /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \name CC2538 TSCH configuration + * + * @{ + */ +#define RADIO_PHY_OVERHEAD CC2538_PHY_OVERHEAD +#define RADIO_BYTE_AIR_TIME CC2538_BYTE_AIR_TIME +#define RADIO_DELAY_BEFORE_TX CC2538_DELAY_BEFORE_TX +#define RADIO_DELAY_BEFORE_RX CC2538_DELAY_BEFORE_RX +#define RADIO_DELAY_BEFORE_DETECT CC2538_DELAY_BEFORE_DETECT +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx b/arch/platform/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 similarity index 78% rename from arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx rename to arch/platform/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 index 03eefcf8b..622445dd4 100644 --- a/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx +++ b/arch/platform/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 @@ -1,17 +1,17 @@ -# srf06-cc26xx platform makefile +# cc26x0-cc13x0 platform makefile ifndef CONTIKI $(error CONTIKI not defined! You must specify where CONTIKI resides!) endif ### Board and BSP selection -BOARD ?= srf06/cc26xx -BOARDS = srf06/cc26xx srf06/cc13xx launchpad/cc2640r2 launchpad/cc2650 launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +BOARD ?= srf06/cc26x0 +BOARDS = srf06/cc26x0 srf06/cc13x0 launchpad/cc2640r2 launchpad/cc2650 launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 CONTIKI_TARGET_DIRS += . ### Include the board-specific makefile -PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET) +PLATFORM_ROOT_DIR = $(ARCH_PATH)/platform/$(TARGET) -include $(PLATFORM_ROOT_DIR)/$(BOARD)/Makefile.$(notdir $(BOARD)) CONTIKI_TARGET_SOURCEFILES += platform.c leds-arch.c @@ -25,5 +25,5 @@ SMALL ?= 0 ### Define the CPU directory and pull in the correct CPU makefile. This will ### be defined by one of the makefiles included above and it can be either ### Makefile.cc26xx or Makefile.cc13xx -CONTIKI_CPU=$(CONTIKI)/arch/cpu/cc26xx-cc13xx +CONTIKI_CPU=$(ARCH_PATH)/cpu/cc26x0-cc13x0 include $(CONTIKI_CPU)/Makefile.$(CPU_FAMILY) diff --git a/arch/platform/srf06-cc26xx/cfs-coffee-arch.h b/arch/platform/cc26x0-cc13x0/cfs-coffee-arch.h similarity index 100% rename from arch/platform/srf06-cc26xx/cfs-coffee-arch.h rename to arch/platform/cc26x0-cc13x0/cfs-coffee-arch.h diff --git a/arch/platform/srf06-cc26xx/common/xmem.c b/arch/platform/cc26x0-cc13x0/common/xmem.c similarity index 100% rename from arch/platform/srf06-cc26xx/common/xmem.c rename to arch/platform/cc26x0-cc13x0/common/xmem.c diff --git a/arch/platform/srf06-cc26xx/contiki-conf.h b/arch/platform/cc26x0-cc13x0/contiki-conf.h similarity index 98% rename from arch/platform/srf06-cc26xx/contiki-conf.h rename to arch/platform/cc26x0-cc13x0/contiki-conf.h index 23c086db3..de61c102c 100644 --- a/arch/platform/srf06-cc26xx/contiki-conf.h +++ b/arch/platform/cc26x0-cc13x0/contiki-conf.h @@ -32,7 +32,7 @@ * @{ * * \file - * Configuration for the srf06-cc26xx platform + * Configuration for the cc26x0-cc13x0 platform */ #ifndef CONTIKI_CONF_H #define CONTIKI_CONF_H diff --git a/arch/platform/srf06-cc26xx/launchpad/Makefile.launchpad b/arch/platform/cc26x0-cc13x0/launchpad/Makefile.launchpad similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/Makefile.launchpad rename to arch/platform/cc26x0-cc13x0/launchpad/Makefile.launchpad diff --git a/arch/platform/srf06-cc26xx/launchpad/board-buttons.c b/arch/platform/cc26x0-cc13x0/launchpad/board-buttons.c similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/board-buttons.c rename to arch/platform/cc26x0-cc13x0/launchpad/board-buttons.c diff --git a/arch/platform/srf06-cc26xx/launchpad/board-peripherals.h b/arch/platform/cc26x0-cc13x0/launchpad/board-peripherals.h similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/board-peripherals.h rename to arch/platform/cc26x0-cc13x0/launchpad/board-peripherals.h diff --git a/arch/platform/srf06-cc26xx/launchpad/board.c b/arch/platform/cc26x0-cc13x0/launchpad/board.c similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/board.c rename to arch/platform/cc26x0-cc13x0/launchpad/board.c diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1310/Makefile.cc1310 b/arch/platform/cc26x0-cc13x0/launchpad/cc1310/Makefile.cc1310 similarity index 91% rename from arch/platform/srf06-cc26xx/launchpad/cc1310/Makefile.cc1310 rename to arch/platform/cc26x0-cc13x0/launchpad/cc1310/Makefile.cc1310 index 8d3d2abd4..e9f7bab91 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1310/Makefile.cc1310 +++ b/arch/platform/cc26x0-cc13x0/launchpad/cc1310/Makefile.cc1310 @@ -1,5 +1,5 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc13xx +CPU_FAMILY = cc13x0 ### Add to the source dirs CONTIKI_TARGET_DIRS += launchpad/cc1310 diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h b/arch/platform/cc26x0-cc13x0/launchpad/cc1310/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/cc1310/board.h rename to arch/platform/cc26x0-cc13x0/launchpad/cc1310/board.h diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1350/Makefile.cc1350 b/arch/platform/cc26x0-cc13x0/launchpad/cc1350/Makefile.cc1350 similarity index 93% rename from arch/platform/srf06-cc26xx/launchpad/cc1350/Makefile.cc1350 rename to arch/platform/cc26x0-cc13x0/launchpad/cc1350/Makefile.cc1350 index 0d1f65359..811a3f61b 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1350/Makefile.cc1350 +++ b/arch/platform/cc26x0-cc13x0/launchpad/cc1350/Makefile.cc1350 @@ -1,5 +1,5 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc13xx +CPU_FAMILY = cc13x0 ### Add to the source dirs CONTIKI_TARGET_DIRS += launchpad/cc1350 diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h b/arch/platform/cc26x0-cc13x0/launchpad/cc1350/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/cc1350/board.h rename to arch/platform/cc26x0-cc13x0/launchpad/cc1350/board.h diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1350/rf-switch.c b/arch/platform/cc26x0-cc13x0/launchpad/cc1350/rf-switch.c similarity index 97% rename from arch/platform/srf06-cc26xx/launchpad/cc1350/rf-switch.c rename to arch/platform/cc26x0-cc13x0/launchpad/cc1350/rf-switch.c index 42e648a73..639b246cc 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1350/rf-switch.c +++ b/arch/platform/cc26x0-cc13x0/launchpad/cc1350/rf-switch.c @@ -63,9 +63,9 @@ LPM_MODULE(rf_switch_module, NULL, shutdown_handler, NULL, LPM_DOMAIN_NONE); void rf_switch_init() { - ti_lib_rom_ioc_pin_type_gpio_output(POWER_PIN); + ti_lib_ioc_pin_type_gpio_output(POWER_PIN); ti_lib_gpio_clear_dio(POWER_PIN); - ti_lib_rom_ioc_pin_type_gpio_output(SELECT_PIN); + ti_lib_ioc_pin_type_gpio_output(SELECT_PIN); ti_lib_gpio_clear_dio(SELECT_PIN); lpm_register_module(&rf_switch_module); diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1350/tx-power-driver.c b/arch/platform/cc26x0-cc13x0/launchpad/cc1350/tx-power-driver.c similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/cc1350/tx-power-driver.c rename to arch/platform/cc26x0-cc13x0/launchpad/cc1350/tx-power-driver.c diff --git a/arch/platform/srf06-cc26xx/launchpad/cc2640r2/Makefile.cc2640r2 b/arch/platform/cc26x0-cc13x0/launchpad/cc2640r2/Makefile.cc2640r2 similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/cc2640r2/Makefile.cc2640r2 rename to arch/platform/cc26x0-cc13x0/launchpad/cc2640r2/Makefile.cc2640r2 diff --git a/arch/platform/srf06-cc26xx/launchpad/cc2640r2/board.h b/arch/platform/cc26x0-cc13x0/launchpad/cc2640r2/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/cc2640r2/board.h rename to arch/platform/cc26x0-cc13x0/launchpad/cc2640r2/board.h diff --git a/arch/platform/srf06-cc26xx/launchpad/cc2650/Makefile.cc2650 b/arch/platform/cc26x0-cc13x0/launchpad/cc2650/Makefile.cc2650 similarity index 91% rename from arch/platform/srf06-cc26xx/launchpad/cc2650/Makefile.cc2650 rename to arch/platform/cc26x0-cc13x0/launchpad/cc2650/Makefile.cc2650 index 72e50de3a..399cd79e5 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc2650/Makefile.cc2650 +++ b/arch/platform/cc26x0-cc13x0/launchpad/cc2650/Makefile.cc2650 @@ -1,5 +1,5 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc26xx +CPU_FAMILY = cc26x0 ### Add to the source dirs CONTIKI_TARGET_DIRS += launchpad/cc2650 diff --git a/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h b/arch/platform/cc26x0-cc13x0/launchpad/cc2650/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/cc2650/board.h rename to arch/platform/cc26x0-cc13x0/launchpad/cc2650/board.h diff --git a/arch/platform/srf06-cc26xx/launchpad/leds-arch.c b/arch/platform/cc26x0-cc13x0/launchpad/leds-arch.c similarity index 100% rename from arch/platform/srf06-cc26xx/launchpad/leds-arch.c rename to arch/platform/cc26x0-cc13x0/launchpad/leds-arch.c diff --git a/arch/platform/srf06-cc26xx/platform.c b/arch/platform/cc26x0-cc13x0/platform.c similarity index 97% rename from arch/platform/srf06-cc26xx/platform.c rename to arch/platform/cc26x0-cc13x0/platform.c index 427504ed8..d160c13e1 100644 --- a/arch/platform/srf06-cc26xx/platform.c +++ b/arch/platform/cc26x0-cc13x0/platform.c @@ -158,11 +158,11 @@ platform_init_stage_one() * latches in the first place. Before doing these things though, we should * allow software to first regain control of pins */ -#if !defined(ThisLibraryIsFor_CC26x0R2_HaltIfViolated) - ti_lib_pwr_ctrl_io_freeze_disable(); + ti_lib_aon_ioc_freeze_disable(); + HWREG(AON_SYSCTL_BASE + AON_SYSCTL_O_SLEEPCTL) = 1; + ti_lib_sys_ctrl_aon_sync(); -#endif - ti_lib_rom_int_enable(INT_AON_GPIO_EDGE); + ti_lib_int_enable(INT_AON_GPIO_EDGE); ti_lib_int_master_enable(); soc_rtc_init(); diff --git a/arch/platform/srf06-cc26xx/sensortag/Makefile.sensortag b/arch/platform/cc26x0-cc13x0/sensortag/Makefile.sensortag similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/Makefile.sensortag rename to arch/platform/cc26x0-cc13x0/sensortag/Makefile.sensortag diff --git a/arch/platform/srf06-cc26xx/sensortag/bmp-280-sensor.c b/arch/platform/cc26x0-cc13x0/sensortag/bmp-280-sensor.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/bmp-280-sensor.c rename to arch/platform/cc26x0-cc13x0/sensortag/bmp-280-sensor.c diff --git a/arch/platform/srf06-cc26xx/sensortag/bmp-280-sensor.h b/arch/platform/cc26x0-cc13x0/sensortag/bmp-280-sensor.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/bmp-280-sensor.h rename to arch/platform/cc26x0-cc13x0/sensortag/bmp-280-sensor.h diff --git a/arch/platform/srf06-cc26xx/sensortag/board-buttons.c b/arch/platform/cc26x0-cc13x0/sensortag/board-buttons.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/board-buttons.c rename to arch/platform/cc26x0-cc13x0/sensortag/board-buttons.c diff --git a/arch/platform/srf06-cc26xx/sensortag/board-i2c.c b/arch/platform/cc26x0-cc13x0/sensortag/board-i2c.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/board-i2c.c rename to arch/platform/cc26x0-cc13x0/sensortag/board-i2c.c diff --git a/arch/platform/srf06-cc26xx/sensortag/board-i2c.h b/arch/platform/cc26x0-cc13x0/sensortag/board-i2c.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/board-i2c.h rename to arch/platform/cc26x0-cc13x0/sensortag/board-i2c.h diff --git a/arch/platform/srf06-cc26xx/sensortag/board-peripherals.h b/arch/platform/cc26x0-cc13x0/sensortag/board-peripherals.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/board-peripherals.h rename to arch/platform/cc26x0-cc13x0/sensortag/board-peripherals.h diff --git a/arch/platform/srf06-cc26xx/sensortag/board.c b/arch/platform/cc26x0-cc13x0/sensortag/board.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/board.c rename to arch/platform/cc26x0-cc13x0/sensortag/board.c diff --git a/arch/platform/srf06-cc26xx/sensortag/buzzer.c b/arch/platform/cc26x0-cc13x0/sensortag/buzzer.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/buzzer.c rename to arch/platform/cc26x0-cc13x0/sensortag/buzzer.c diff --git a/arch/platform/srf06-cc26xx/sensortag/buzzer.h b/arch/platform/cc26x0-cc13x0/sensortag/buzzer.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/buzzer.h rename to arch/platform/cc26x0-cc13x0/sensortag/buzzer.h diff --git a/arch/platform/srf06-cc26xx/sensortag/cc1350/Makefile.cc1350 b/arch/platform/cc26x0-cc13x0/sensortag/cc1350/Makefile.cc1350 similarity index 91% rename from arch/platform/srf06-cc26xx/sensortag/cc1350/Makefile.cc1350 rename to arch/platform/cc26x0-cc13x0/sensortag/cc1350/Makefile.cc1350 index cdae4c2b1..f0f924e97 100644 --- a/arch/platform/srf06-cc26xx/sensortag/cc1350/Makefile.cc1350 +++ b/arch/platform/cc26x0-cc13x0/sensortag/cc1350/Makefile.cc1350 @@ -1,5 +1,5 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc13xx +CPU_FAMILY = cc13x0 ### Add to the source dirs CONTIKI_TARGET_DIRS += sensortag/cc1350 diff --git a/arch/platform/srf06-cc26xx/sensortag/cc1350/board.h b/arch/platform/cc26x0-cc13x0/sensortag/cc1350/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/cc1350/board.h rename to arch/platform/cc26x0-cc13x0/sensortag/cc1350/board.h diff --git a/arch/platform/srf06-cc26xx/sensortag/cc1350/leds-arch.c b/arch/platform/cc26x0-cc13x0/sensortag/cc1350/leds-arch.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/cc1350/leds-arch.c rename to arch/platform/cc26x0-cc13x0/sensortag/cc1350/leds-arch.c diff --git a/arch/platform/srf06-cc26xx/sensortag/cc2650/Makefile.cc2650 b/arch/platform/cc26x0-cc13x0/sensortag/cc2650/Makefile.cc2650 similarity index 91% rename from arch/platform/srf06-cc26xx/sensortag/cc2650/Makefile.cc2650 rename to arch/platform/cc26x0-cc13x0/sensortag/cc2650/Makefile.cc2650 index e91cabda4..4aa83e352 100644 --- a/arch/platform/srf06-cc26xx/sensortag/cc2650/Makefile.cc2650 +++ b/arch/platform/cc26x0-cc13x0/sensortag/cc2650/Makefile.cc2650 @@ -1,5 +1,5 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc26xx +CPU_FAMILY = cc26x0 ### Add to the source dirs CONTIKI_TARGET_DIRS += sensortag/cc2650 diff --git a/arch/platform/srf06-cc26xx/sensortag/cc2650/board.h b/arch/platform/cc26x0-cc13x0/sensortag/cc2650/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/cc2650/board.h rename to arch/platform/cc26x0-cc13x0/sensortag/cc2650/board.h diff --git a/arch/platform/srf06-cc26xx/sensortag/cc2650/leds-arch.c b/arch/platform/cc26x0-cc13x0/sensortag/cc2650/leds-arch.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/cc2650/leds-arch.c rename to arch/platform/cc26x0-cc13x0/sensortag/cc2650/leds-arch.c diff --git a/arch/platform/srf06-cc26xx/sensortag/hdc-1000-sensor.c b/arch/platform/cc26x0-cc13x0/sensortag/hdc-1000-sensor.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/hdc-1000-sensor.c rename to arch/platform/cc26x0-cc13x0/sensortag/hdc-1000-sensor.c diff --git a/arch/platform/srf06-cc26xx/sensortag/hdc-1000-sensor.h b/arch/platform/cc26x0-cc13x0/sensortag/hdc-1000-sensor.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/hdc-1000-sensor.h rename to arch/platform/cc26x0-cc13x0/sensortag/hdc-1000-sensor.h diff --git a/arch/platform/srf06-cc26xx/sensortag/mpu-9250-sensor.c b/arch/platform/cc26x0-cc13x0/sensortag/mpu-9250-sensor.c similarity index 99% rename from arch/platform/srf06-cc26xx/sensortag/mpu-9250-sensor.c rename to arch/platform/cc26x0-cc13x0/sensortag/mpu-9250-sensor.c index a2b3651e5..9e9685cd3 100644 --- a/arch/platform/srf06-cc26xx/sensortag/mpu-9250-sensor.c +++ b/arch/platform/cc26x0-cc13x0/sensortag/mpu-9250-sensor.c @@ -601,11 +601,11 @@ configure(int type, int enable) { switch(type) { case SENSORS_HW_INIT: - ti_lib_rom_ioc_pin_type_gpio_input(BOARD_IOID_MPU_INT); + ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_MPU_INT); ti_lib_ioc_io_port_pull_set(BOARD_IOID_MPU_INT, IOC_IOPULL_DOWN); ti_lib_ioc_io_hyst_set(BOARD_IOID_MPU_INT, IOC_HYST_ENABLE); - ti_lib_rom_ioc_pin_type_gpio_output(BOARD_IOID_MPU_POWER); + ti_lib_ioc_pin_type_gpio_output(BOARD_IOID_MPU_POWER); ti_lib_ioc_io_drv_strength_set(BOARD_IOID_MPU_POWER, IOC_CURRENT_4MA, IOC_STRENGTH_MAX); ti_lib_gpio_clear_dio(BOARD_IOID_MPU_POWER); diff --git a/arch/platform/srf06-cc26xx/sensortag/mpu-9250-sensor.h b/arch/platform/cc26x0-cc13x0/sensortag/mpu-9250-sensor.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/mpu-9250-sensor.h rename to arch/platform/cc26x0-cc13x0/sensortag/mpu-9250-sensor.h diff --git a/arch/platform/srf06-cc26xx/sensortag/opt-3001-sensor.c b/arch/platform/cc26x0-cc13x0/sensortag/opt-3001-sensor.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/opt-3001-sensor.c rename to arch/platform/cc26x0-cc13x0/sensortag/opt-3001-sensor.c diff --git a/arch/platform/srf06-cc26xx/sensortag/opt-3001-sensor.h b/arch/platform/cc26x0-cc13x0/sensortag/opt-3001-sensor.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/opt-3001-sensor.h rename to arch/platform/cc26x0-cc13x0/sensortag/opt-3001-sensor.h diff --git a/arch/platform/srf06-cc26xx/sensortag/sensor-common.c b/arch/platform/cc26x0-cc13x0/sensortag/sensor-common.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/sensor-common.c rename to arch/platform/cc26x0-cc13x0/sensortag/sensor-common.c diff --git a/arch/platform/srf06-cc26xx/sensortag/sensor-common.h b/arch/platform/cc26x0-cc13x0/sensortag/sensor-common.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/sensor-common.h rename to arch/platform/cc26x0-cc13x0/sensortag/sensor-common.h diff --git a/arch/platform/srf06-cc26xx/sensortag/sensortag-sensors.c b/arch/platform/cc26x0-cc13x0/sensortag/sensortag-sensors.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/sensortag-sensors.c rename to arch/platform/cc26x0-cc13x0/sensortag/sensortag-sensors.c diff --git a/arch/platform/srf06-cc26xx/sensortag/tmp-007-sensor.c b/arch/platform/cc26x0-cc13x0/sensortag/tmp-007-sensor.c similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/tmp-007-sensor.c rename to arch/platform/cc26x0-cc13x0/sensortag/tmp-007-sensor.c diff --git a/arch/platform/srf06-cc26xx/sensortag/tmp-007-sensor.h b/arch/platform/cc26x0-cc13x0/sensortag/tmp-007-sensor.h similarity index 100% rename from arch/platform/srf06-cc26xx/sensortag/tmp-007-sensor.h rename to arch/platform/cc26x0-cc13x0/sensortag/tmp-007-sensor.h diff --git a/arch/platform/srf06-cc26xx/srf06/Makefile.srf06 b/arch/platform/cc26x0-cc13x0/srf06/Makefile.srf06 similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/Makefile.srf06 rename to arch/platform/cc26x0-cc13x0/srf06/Makefile.srf06 diff --git a/arch/platform/srf06-cc26xx/srf06/als-sensor.c b/arch/platform/cc26x0-cc13x0/srf06/als-sensor.c similarity index 93% rename from arch/platform/srf06-cc26xx/srf06/als-sensor.c rename to arch/platform/cc26x0-cc13x0/srf06/als-sensor.c index 3b6fdecb9..42e714065 100644 --- a/arch/platform/srf06-cc26xx/srf06/als-sensor.c +++ b/arch/platform/cc26x0-cc13x0/srf06/als-sensor.c @@ -59,10 +59,10 @@ config(int type, int enable) ti_lib_ioc_pin_type_gpio_output(BOARD_IOID_ALS_PWR); break; case SENSORS_ACTIVE: - ti_lib_rom_ioc_pin_type_gpio_output(BOARD_IOID_ALS_PWR); - ti_lib_rom_ioc_port_configure_set(BOARD_IOID_ALS_OUT, IOC_PORT_GPIO, - IOC_STD_OUTPUT); - ti_lib_rom_ioc_pin_type_gpio_input(BOARD_IOID_ALS_OUT); + ti_lib_ioc_pin_type_gpio_output(BOARD_IOID_ALS_PWR); + ti_lib_ioc_port_configure_set(BOARD_IOID_ALS_OUT, IOC_PORT_GPIO, + IOC_STD_OUTPUT); + ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_ALS_OUT); if(enable) { ti_lib_gpio_set_dio(BOARD_IOID_ALS_PWR); diff --git a/arch/platform/srf06-cc26xx/srf06/als-sensor.h b/arch/platform/cc26x0-cc13x0/srf06/als-sensor.h similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/als-sensor.h rename to arch/platform/cc26x0-cc13x0/srf06/als-sensor.h diff --git a/arch/platform/srf06-cc26xx/srf06/board-buttons.c b/arch/platform/cc26x0-cc13x0/srf06/board-buttons.c similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/board-buttons.c rename to arch/platform/cc26x0-cc13x0/srf06/board-buttons.c diff --git a/arch/platform/srf06-cc26xx/srf06/board-peripherals.h b/arch/platform/cc26x0-cc13x0/srf06/board-peripherals.h similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/board-peripherals.h rename to arch/platform/cc26x0-cc13x0/srf06/board-peripherals.h diff --git a/arch/platform/srf06-cc26xx/srf06/board.c b/arch/platform/cc26x0-cc13x0/srf06/board.c similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/board.c rename to arch/platform/cc26x0-cc13x0/srf06/board.c diff --git a/arch/platform/srf06-cc26xx/srf06/cc13xx/Makefile.cc13xx b/arch/platform/cc26x0-cc13x0/srf06/cc13x0/Makefile.cc13x0 similarity index 72% rename from arch/platform/srf06-cc26xx/srf06/cc13xx/Makefile.cc13xx rename to arch/platform/cc26x0-cc13x0/srf06/cc13x0/Makefile.cc13x0 index b83084651..ec20fcb63 100644 --- a/arch/platform/srf06-cc26xx/srf06/cc13xx/Makefile.cc13xx +++ b/arch/platform/cc26x0-cc13x0/srf06/cc13x0/Makefile.cc13x0 @@ -1,7 +1,7 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc13xx +CPU_FAMILY = cc13x0 ### Include the common sensortag makefile include $(PLATFORM_ROOT_DIR)/srf06/Makefile.srf06 -CONTIKI_TARGET_DIRS += srf06/cc13xx +CONTIKI_TARGET_DIRS += srf06/cc13x0 diff --git a/arch/platform/srf06-cc26xx/srf06/cc13xx/board.h b/arch/platform/cc26x0-cc13x0/srf06/cc13x0/board.h similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/cc13xx/board.h rename to arch/platform/cc26x0-cc13x0/srf06/cc13x0/board.h diff --git a/arch/platform/srf06-cc26xx/srf06/cc26xx/Makefile.cc26xx b/arch/platform/cc26x0-cc13x0/srf06/cc26x0/Makefile.cc26x0 similarity index 71% rename from arch/platform/srf06-cc26xx/srf06/cc26xx/Makefile.cc26xx rename to arch/platform/cc26x0-cc13x0/srf06/cc26x0/Makefile.cc26x0 index 841442a5c..84ef217a8 100644 --- a/arch/platform/srf06-cc26xx/srf06/cc26xx/Makefile.cc26xx +++ b/arch/platform/cc26x0-cc13x0/srf06/cc26x0/Makefile.cc26x0 @@ -1,7 +1,7 @@ ### Will allow the inclusion of the correct CPU makefile -CPU_FAMILY = cc26xx +CPU_FAMILY = cc26x0 ### Include the common makefile include $(PLATFORM_ROOT_DIR)/srf06/Makefile.srf06 -CONTIKI_TARGET_DIRS += srf06/cc26xx +CONTIKI_TARGET_DIRS += srf06/cc26x0 diff --git a/arch/platform/srf06-cc26xx/srf06/cc26xx/board.h b/arch/platform/cc26x0-cc13x0/srf06/cc26x0/board.h similarity index 99% rename from arch/platform/srf06-cc26xx/srf06/cc26xx/board.h rename to arch/platform/cc26x0-cc13x0/srf06/cc26x0/board.h index f31a94fb3..cd6d2b1e4 100644 --- a/arch/platform/srf06-cc26xx/srf06/cc26xx/board.h +++ b/arch/platform/cc26x0-cc13x0/srf06/cc26x0/board.h @@ -31,7 +31,7 @@ /** \addtogroup cc26xx-srf-tag * @{ * - * \defgroup srf06-cc26xx-peripherals Peripherals for the SmartRF06EB + CC26xxEM + * \defgroup cc26x0-cc13x0-peripherals Peripherals for the SmartRF06EB + CC26xxEM * * Defines related to the SmartRF06 Evaluation Board with a CC26xxEM * diff --git a/arch/platform/srf06-cc26xx/srf06/leds-arch.c b/arch/platform/cc26x0-cc13x0/srf06/leds-arch.c similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/leds-arch.c rename to arch/platform/cc26x0-cc13x0/srf06/leds-arch.c diff --git a/arch/platform/srf06-cc26xx/srf06/srf06-sensors.c b/arch/platform/cc26x0-cc13x0/srf06/srf06-sensors.c similarity index 100% rename from arch/platform/srf06-cc26xx/srf06/srf06-sensors.c rename to arch/platform/cc26x0-cc13x0/srf06/srf06-sensors.c diff --git a/arch/platform/cooja/Makefile.cooja b/arch/platform/cooja/Makefile.cooja index d06aa08d5..023fe3d9a 100644 --- a/arch/platform/cooja/Makefile.cooja +++ b/arch/platform/cooja/Makefile.cooja @@ -34,20 +34,20 @@ endif endif ## QUICKSTART -#MAIN_SRC = $(OBJECTDIR)/$(LIBNAME).c -MAIN_OBJ = $(OBJECTDIR)/$(LIBNAME).o -ARCHIVE = $(OBJECTDIR)/$(LIBNAME).a -JNILIB = $(OBJECTDIR)/$(LIBNAME).$(TARGET) +#MAIN_SRC = $(BUILD_DIR_BOARD)/$(LIBNAME).c +MAIN_OBJ = $(BUILD_DIR_BOARD)/$(LIBNAME).o +ARCHIVE = $(BUILD_DIR_BOARD)/$(LIBNAME).a +JNILIB = $(BUILD_DIR_BOARD)/$(LIBNAME).$(TARGET) CONTIKI_APP_OBJ = $(CONTIKI_APP).o ### COOJA platform sources -COOJA = $(CONTIKI)/arch/platform/$(TARGET) +COOJA = $(ARCH_PATH)/platform/$(TARGET) CONTIKI_TARGET_DIRS = . dev lib sys cfs net # (COOJA_SOURCEDIRS contains additional sources dirs set from simulator) vpath %.c $(COOJA_SOURCEDIRS) -COOJA_BASE = simEnvChange.c cooja_mt.c cooja_mtarch.c rtimer-arch.c slip.c watchdog.c +COOJA_BASE = simEnvChange.c cooja_mt.c cooja_mtarch.c rtimer-arch.c slip.c watchdog.c int-master.c COOJA_INTFS = beep.c button-sensor.c ip.c leds-arch.c moteid.c \ pir-sensor.c rs232.c vib-sensor.c \ @@ -67,7 +67,7 @@ CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) CLEAN += COOJA.log ### Define the CPU directory -CONTIKI_CPU=$(CONTIKI)/arch/cpu/x86 +CONTIKI_CPU=$(ARCH_PATH)/cpu/x86 ### Compiler arguments #CC = gcc diff --git a/arch/platform/cooja/Makefile.customrules-cooja b/arch/platform/cooja/Makefile.customrules-cooja index 42d6e125e..60d6240f1 100644 --- a/arch/platform/cooja/Makefile.customrules-cooja +++ b/arch/platform/cooja/Makefile.customrules-cooja @@ -10,11 +10,11 @@ CUSTOM_RULE_LINK=1 REDEF_PRINTF=1 # Redefine functions to enable printf()s inside Cooja -# NB: Assumes ARCHIVE was not overridden and is in $(OBJECTDIR) +# NB: Assumes ARCHIVE was not overridden and is in $(BUILD_DIR_BOARD) $(ARCHIVE): $(CONTIKI_OBJECTFILES) | $(OBJECTDIR) $(AR_COMMAND_1) $^ $(AR_COMMAND_2) -# NB: Assumes JNILIB was not overridden and is in $(OBJECTDIR) +# NB: Assumes JNILIB was not overridden and is in $(BUILD_DIR_BOARD) $(JNILIB): $(CONTIKI_APP_OBJ) $(MAIN_OBJ) $(PROJECT_OBJECTFILES) $(ARCHIVE) | $(OBJECTDIR) ifdef REDEF_PRINTF diff --git a/arch/platform/cooja/dev/watchdog.c b/arch/platform/cooja/dev/watchdog.c index a30fc0fc8..347d9a996 100644 --- a/arch/platform/cooja/dev/watchdog.c +++ b/arch/platform/cooja/dev/watchdog.c @@ -30,8 +30,10 @@ * */ - /* Dummy watchdog routines for the Raven 1284p */ + /* Dummy watchdog routines for Cooja motes */ #include "dev/watchdog.h" +#include "lib/simEnvChange.h" +#include "sys/cooja_mt.h" /*---------------------------------------------------------------------------*/ void @@ -47,6 +49,9 @@ watchdog_start(void) void watchdog_periodic(void) { + /* Yield and give control back to the simulator scheduler */ + simProcessRunValue = 1; + cooja_mt_yield(); } /*---------------------------------------------------------------------------*/ void diff --git a/arch/platform/cooja/int-master.c b/arch/platform/cooja/int-master.c new file mode 100644 index 000000000..177dbd442 --- /dev/null +++ b/arch/platform/cooja/int-master.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "sys/int-master.h" + +#include +/*---------------------------------------------------------------------------*/ +#define DISABLED 0 +#define ENABLED 1 +/*---------------------------------------------------------------------------*/ +static int_master_status_t stat = DISABLED; +/*---------------------------------------------------------------------------*/ +void +int_master_enable(void) +{ + stat = ENABLED; +} +/*---------------------------------------------------------------------------*/ +int_master_status_t +int_master_read_and_disable(void) +{ + int_master_status_t rv = stat; + stat = DISABLED; + return rv; +} +/*---------------------------------------------------------------------------*/ +void +int_master_status_set(int_master_status_t status) +{ + stat = status; +} +/*---------------------------------------------------------------------------*/ +bool +int_master_is_enabled(void) +{ + return stat == DISABLED ? false : true; +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/cooja/net/uip-driver.c b/arch/platform/cooja/net/uip-driver.c index 0e7209a6c..fd86cc9c6 100644 --- a/arch/platform/cooja/net/uip-driver.c +++ b/arch/platform/cooja/net/uip-driver.c @@ -48,7 +48,7 @@ static uint8_t uip_driver_send(const linkaddr_t *addr) { - packetbuf_copyfrom(&uip_buf[UIP_LLH_LEN], uip_len); + packetbuf_copyfrom(uip_buf, uip_len); /* XXX we should provide a callback function that is called when the packet is sent. For now, we just supply a NULL pointer. */ @@ -64,9 +64,8 @@ init(void) static void input(void) { - if(packetbuf_datalen() > 0 && - packetbuf_datalen() <= UIP_BUFSIZE - UIP_LLH_LEN) { - memcpy(&uip_buf[UIP_LLH_LEN], packetbuf_dataptr(), packetbuf_datalen()); + if(packetbuf_datalen() > 0 && packetbuf_datalen() <= UIP_BUFSIZE) { + memcpy(uip_buf, packetbuf_dataptr(), packetbuf_datalen()); uip_len = packetbuf_datalen(); tcpip_input(); } diff --git a/arch/platform/jn516x/Makefile.jn516x b/arch/platform/jn516x/Makefile.jn516x index 9ad9488d5..051c336fb 100644 --- a/arch/platform/jn516x/Makefile.jn516x +++ b/arch/platform/jn516x/Makefile.jn516x @@ -229,30 +229,34 @@ ALLLIBS = $(addprefix -l,$(LDLIBS)) $(addprefix -l,$(LDSTACKLIBS)) $(addprefix - ABS_APPLIBS = $(addsuffix _$(JENNIC_CHIP_FAMILY).a,$(addprefix $(COMPONENTS_BASE_DIR)/Library/lib,$(APPLIBS))) ifneq ($(wildcard $(SDK_BASE_DIR)/Components/Library/*),) -# The SDK is fully installed, proceed to linking and objcopy to ready-to-upload .jn516x.bin file -%.$(TARGET): %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) $(ABS_APPLIBS) - echo ${filter %.a,$^} +# The SDK is fully installed, proceed to linking +$(BUILD_DIR_BOARD)/%.$(TARGET): %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) $(ABS_APPLIBS) + @echo ${filter %.a,$^} $(Q)$(CC) -Wl,--gc-sections $(LDFLAGS) -T$(LINKCMD) -o $@ -Wl,--start-group \ $(patsubst /cygdrive/c/%,c:/%,${filter-out %.a,$^}) \ $(patsubst /cygdrive/c/%,c:/%,${filter %.a,$^}) \ $(ALLLIBS) -Wl,--end-group -Wl,-Map,$(CONTIKI_NG_PROJECT_MAP) - $(OBJCOPY) -S -O binary $@ $@.bin else # The SDK does not include libraries, only build objects and libraries, skip linking -%.$(TARGET): %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) - echo Creating empty $@ +$(BUILD_DIR_BOARD)/%.$(TARGET): %.o $(PROJECT_OBJECTFILES) $(PROJECT_LIBRARIES) $(CONTIKI_NG_TARGET_LIB) + @echo Creating empty $@ touch $@ endif -%.$(TARGET).bin: %.$(TARGET) +$(BUILD_DIR_BOARD)/%.$(TARGET).bin: $(BUILD_DIR_BOARD)/%.$(TARGET) + $(TRACE_OBJCOPY) $(Q)$(OBJCOPY) -S -O binary $< $@ +%.$(TARGET).bin: $(BUILD_DIR_BOARD)/%.$(TARGET).bin + $(TRACE_CP) + $(Q)$(CP) $< $@ + ### Upload target to one jn516x mote specified by MOTE=portNumber ifeq ($(HOST_OS),Windows) -%.upload: %.$(TARGET).bin +%.upload: $(BUILD_DIR_BOARD)/%.$(TARGET).bin ${FLASH_PROGRAMMER} -a -c $(DEV_PORT) -B 1000000 -s -w -f $< else -%.upload: %.$(TARGET).bin +%.upload: $(BUILD_DIR_BOARD)/%.$(TARGET).bin ${FLASH_PROGRAMMER} -V 10 -v -s $(DEV_PORT) -I 38400 -P 1000000 -f $< endif @@ -276,7 +280,7 @@ motelistinfo: $(Q)$(MOTELIST) ${FLASH_PROGRAMMER} \? ### Upload target to all connected jn516x motes -%.uploadall: %.$(TARGET).bin +%.uploadall: $(BUILD_DIR_BOARD)/%.$(TARGET).bin $(Q)$(MOTELIST) ${FLASH_PROGRAMMER} $< ### Flash the given file to all connected jn516x motes diff --git a/arch/platform/jn516x/dev/micromac-radio.c b/arch/platform/jn516x/dev/micromac-radio.c index 6e2ddb908..bad81f4f0 100644 --- a/arch/platform/jn516x/dev/micromac-radio.c +++ b/arch/platform/jn516x/dev/micromac-radio.c @@ -130,13 +130,6 @@ #define MICROMAC_CONF_ALWAYS_ON 1 #endif /* MICROMAC_CONF_ALWAYS_ON */ -#define BUSYWAIT_UNTIL(cond, max_time) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) ; \ - } while(0) - /* Local variables */ static volatile signed char radio_last_rssi; static volatile uint8_t radio_last_correlation; /* LQI */ @@ -377,14 +370,14 @@ transmit(unsigned short payload_len) (send_on_cca ? E_MMAC_TX_USE_CCA : E_MMAC_TX_NO_CCA)); #endif if(poll_mode) { - BUSYWAIT_UNTIL(u32MMAC_PollInterruptSource(E_MMAC_INT_TX_COMPLETE), MAX_PACKET_DURATION); + RTIMER_BUSYWAIT_UNTIL(u32MMAC_PollInterruptSource(E_MMAC_INT_TX_COMPLETE), MAX_PACKET_DURATION); } else { if(in_ack_transmission) { /* as nested interupts are not possible, the tx flag will never be cleared */ - BUSYWAIT_UNTIL(FALSE, MAX_ACK_DURATION); + RTIMER_BUSYWAIT_UNTIL(FALSE, MAX_ACK_DURATION); } else { /* wait until the tx flag is cleared */ - BUSYWAIT_UNTIL(!tx_in_progress, MAX_PACKET_DURATION); + RTIMER_BUSYWAIT_UNTIL(!tx_in_progress, MAX_PACKET_DURATION); } } diff --git a/arch/platform/jn516x/dev/uart-driver.c b/arch/platform/jn516x/dev/uart-driver.c index 31a4b286c..aeeffeeb4 100644 --- a/arch/platform/jn516x/dev/uart-driver.c +++ b/arch/platform/jn516x/dev/uart-driver.c @@ -66,14 +66,6 @@ extern volatile unsigned char xonxoff_state; #endif /* UART_XONXOFF_FLOW_CTRL */ -/*** Macro Definitions ***/ -#define BUSYWAIT_UNTIL(cond, max_time) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) ; \ - } while(0) - #define DEBUG_UART_BUFFERED FALSE #define CHAR_DEADLINE (uart_char_delay * 100) @@ -276,7 +268,7 @@ uart_driver_write_with_deadline(uint8_t uart_dev, uint8_t ch) volatile int16_t write = 0; watchdog_periodic(); /* wait until there is space in tx fifo */ - BUSYWAIT_UNTIL(write = (uart_driver_get_tx_fifo_available_space(uart_dev) > 0), + RTIMER_BUSYWAIT_UNTIL(write = (uart_driver_get_tx_fifo_available_space(uart_dev) > 0), CHAR_DEADLINE); /* write only if there is space so we do not get stuck */ if(write) { diff --git a/arch/platform/jn516x/slip.c b/arch/platform/jn516x/slip.c index 173a14d60..41652e181 100644 --- a/arch/platform/jn516x/slip.c +++ b/arch/platform/jn516x/slip.c @@ -46,7 +46,6 @@ #include #include "net/ipv6/uip.h" -#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) #include "dev/slip.h" @@ -117,7 +116,7 @@ unsigned long slip_received, slip_frames; #ifdef SLIP_CONF_RX_BUFSIZE #define RX_BUFSIZE SLIP_CONF_RX_BUFSIZE -#if RX_BUFSIZE < (UIP_BUFSIZE - UIP_LLH_LEN + 16) +#if RX_BUFSIZE < (UIP_BUFSIZE + 16) #error "SLIP_CONF_RX_BUFSIZE too small for UIP_BUFSIZE" #endif @@ -195,7 +194,7 @@ slip_send(void) slip_arch_writeb(SLIP_END); - ptr = &uip_buf[UIP_LLH_LEN]; + ptr = uip_buf; for(i = 0; i < uip_len; ++i) { c = *ptr++; slip_write_char(c); @@ -317,8 +316,7 @@ PROCESS_THREAD(slip_process, ev, data) PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); /* Move packet from rxbuf to buffer provided by uIP. */ - uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], - UIP_BUFSIZE - UIP_LLH_LEN); + uip_len = slip_poll_handler(uip_buf, UIP_BUFSIZE); PRINTF("SLIP: recv bytes %u frames RECV: %u. is_full %u, is_dropping %u.\n", end_counter, uip_len, is_full, is_dropping); diff --git a/arch/platform/native/Makefile.native b/arch/platform/native/Makefile.native index afeddd22f..51681284e 100644 --- a/arch/platform/native/Makefile.native +++ b/arch/platform/native/Makefile.native @@ -28,5 +28,5 @@ CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) MAKE_MAC ?= MAKE_MAC_NULLMAC ### Define the CPU directory -CONTIKI_CPU=$(CONTIKI)/arch/cpu/native -include $(CONTIKI)/arch/cpu/native/Makefile.native +CONTIKI_CPU=$(ARCH_PATH)/cpu/native +include $(ARCH_PATH)/cpu/native/Makefile.native diff --git a/arch/platform/native/platform.c b/arch/platform/native/platform.c index d00efaac1..b2c25e223 100644 --- a/arch/platform/native/platform.c +++ b/arch/platform/native/platform.c @@ -196,16 +196,13 @@ set_lladdr(void) static void set_global_address(void) { - static uip_ipaddr_t ipaddr; - static uip_ipaddr_t *prefix = NULL; + uip_ipaddr_t ipaddr; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ - if(prefix == NULL) { - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); - } else { - memcpy(&ipaddr, prefix, 8); - } + uip_ip6addr_copy(&ipaddr, default_prefix); + /* Assumes that the uip_lladdr is set */ uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); @@ -215,7 +212,8 @@ set_global_address(void) LOG_INFO_("\n"); /* set the PREFIX::1 address to the IF */ - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 1); + uip_ip6addr_copy(&ipaddr, default_prefix); + ipaddr.u8[15] = 1; uip_ds6_defrt_add(&ipaddr, 0); } #endif diff --git a/arch/platform/nrf52dk/Makefile.nrf52dk b/arch/platform/nrf52dk/Makefile.nrf52dk index e1d56c090..ff554b35c 100644 --- a/arch/platform/nrf52dk/Makefile.nrf52dk +++ b/arch/platform/nrf52dk/Makefile.nrf52dk @@ -3,7 +3,7 @@ ifndef CONTIKI endif ### Include the board-specific makefile -PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET) +PLATFORM_ROOT_DIR = $(ARCH_PATH)/platform/$(TARGET) CONTIKI_TARGET_DIRS += . dev config CONTIKI_SOURCEFILES += platform.c leds-arch.c nrf52dk-sensors.c button-sensor.c temperature-sensor.c @@ -21,5 +21,5 @@ endif SMALL ?= 0 ### Define the CPU directory and pull in the correct CPU makefile. -CONTIKI_CPU=$(CONTIKI)/arch/cpu/nrf52832 +CONTIKI_CPU=$(ARCH_PATH)/cpu/nrf52832 include $(CONTIKI_CPU)/Makefile.nrf52832 diff --git a/arch/platform/nrf52dk/contiki-conf.h b/arch/platform/nrf52dk/contiki-conf.h index 7b3b66314..12d1b2fc9 100644 --- a/arch/platform/nrf52dk/contiki-conf.h +++ b/arch/platform/nrf52dk/contiki-conf.h @@ -74,6 +74,10 @@ /* Packet buffer */ #define PACKETBUF_CONF_SIZE 1280 /**< Required IPv6 MTU size */ + +/* Queuebuf */ +#define QUEUEBUF_CONF_ENABLED 0 + /** @} */ /** diff --git a/arch/platform/nrf52dk/dev/temperature-sensor.c b/arch/platform/nrf52dk/dev/temperature-sensor.c index 5aac8b3ef..f42fd3fe3 100644 --- a/arch/platform/nrf52dk/dev/temperature-sensor.c +++ b/arch/platform/nrf52dk/dev/temperature-sensor.c @@ -64,7 +64,16 @@ static int value(int type) { #ifndef SOFTDEVICE_PRESENT - return nrf_temp_read(); + int32_t volatile temp; + + NRF_TEMP->TASKS_START = 1; + /* nRF52832 datasheet: one temperature measurement takes typically 36 us */ + RTIMER_BUSYWAIT_UNTIL(NRF_TEMP->EVENTS_DATARDY, RTIMER_SECOND * 72 / 1000000); + NRF_TEMP->EVENTS_DATARDY = 0; + temp = nrf_temp_read(); + NRF_TEMP->TASKS_STOP = 1; + + return temp; #else int32_t temp; sd_temp_get(&temp); diff --git a/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 b/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 index d3863fdbc..f720a9ae8 100644 --- a/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 +++ b/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 @@ -11,7 +11,7 @@ endif ### Configure the build for the board and pull in board-specific sources CONTIKI_TARGET_DIRS += . dev -PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET) +PLATFORM_ROOT_DIR = $(ARCH_PATH)/platform/$(TARGET) ### Include CONTIKI_TARGET_SOURCEFILES += platform.c board.c @@ -21,7 +21,7 @@ CONTIKI_TARGET_SOURCEFILES += antenna.c adxl346.c max44009.c sht21.c tps62730.c CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) ### Define the CPU directory -CONTIKI_CPU=$(CONTIKI)/arch/cpu/cc2538 +CONTIKI_CPU=$(ARCH_PATH)/cpu/cc2538 include $(CONTIKI_CPU)/Makefile.cc2538 MODULES += os/storage/cfs @@ -35,11 +35,12 @@ endif BSL = $(CONTIKI)/tools/cc2538-bsl/cc2538-bsl.py -%.upload: %.bin %.elf +%.upload: $(OUT_BIN) $(OUT_ELF) ifeq ($(wildcard $(BSL)), ) @echo "ERROR: Could not find the cc2538-bsl script. Did you run 'git submodule update --init' ?" else - $(eval BSL_ADDRESS_ARG := -a $(shell $(OBJDUMP) -h $*.elf | grep -B1 LOAD | \ + $(eval BSL_ADDRESS_ARG := -a $(shell $(OBJDUMP) -h \ + $(BUILD_DIR_BOARD)/$*.elf | grep -B1 LOAD | \ grep -Ev 'LOAD|\-\-' | awk '{print "0x" $$5}' | \ sort -g | head -1)) $(PYTHON) $(BSL) $(BSL_FLAGS) $(BSL_ADDRESS_ARG) $< diff --git a/arch/platform/openmote-cc2538/board.h b/arch/platform/openmote-cc2538/board.h index abb89b8e2..6e2a15e46 100644 --- a/arch/platform/openmote-cc2538/board.h +++ b/arch/platform/openmote-cc2538/board.h @@ -174,6 +174,18 @@ #define I2C_SDA_PIN 4 /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \name CC2538 TSCH configuration + * + * @{ + */ +#define RADIO_PHY_OVERHEAD CC2538_PHY_OVERHEAD +#define RADIO_BYTE_AIR_TIME CC2538_BYTE_AIR_TIME +#define RADIO_DELAY_BEFORE_TX CC2538_DELAY_BEFORE_TX +#define RADIO_DELAY_BEFORE_RX CC2538_DELAY_BEFORE_RX +#define RADIO_DELAY_BEFORE_DETECT CC2538_DELAY_BEFORE_DETECT +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/simplelink/Makefile.simplelink b/arch/platform/simplelink/Makefile.simplelink index d14643c3f..acb613d6e 100644 --- a/arch/platform/simplelink/Makefile.simplelink +++ b/arch/platform/simplelink/Makefile.simplelink @@ -8,9 +8,7 @@ ifndef CONTIKI $(error 'CONTIKI' not defined! You must specify where CONTIKI resides!) endif -ifndef BOARD - $(error 'BOARD' not defined! You must specify which board you are using!) -endif +BOARD ?= srf06/cc26x0 ################################################################################ ### Resolve the SimpleLink Family diff --git a/arch/platform/simplelink/cc13xx-cc26xx/Makefile.cc13xx-cc26xx b/arch/platform/simplelink/cc13xx-cc26xx/Makefile.cc13xx-cc26xx index 9f2dbe45b..39a3fc504 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/Makefile.cc13xx-cc26xx +++ b/arch/platform/simplelink/cc13xx-cc26xx/Makefile.cc13xx-cc26xx @@ -51,7 +51,7 @@ CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) CPU_FAMILY = cc13xx-cc26xx # Define the CPU directory and pull in the correct CPU Makefile -CONTIKI_CPU := $(realpath $(CONTIKI)/arch/cpu/simplelink-$(CPU_FAMILY)) +CONTIKI_CPU := $(realpath $(ARCH_PATH)/cpu/simplelink-$(CPU_FAMILY)) include $(CONTIKI_CPU)/Makefile.$(CPU_FAMILY) MODULES += os/net os/net/mac os/net/mac/framer diff --git a/arch/platform/simplelink/cc13xx-cc26xx/contiki-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/contiki-conf.h index 212f4fd48..542f69869 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/contiki-conf.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/contiki-conf.h @@ -31,6 +31,10 @@ * \addtogroup cc13xx-cc26xx-platform * @{ * + * The order of which these header files are included is important in order + * for the configurations to be correctly set. This has to do with some + * slight unfortunate configuration dependencies of the board file. + * * \file * Configuration for the SimpleLink CC13xx/CC26xx platform. * \author @@ -40,18 +44,16 @@ #ifndef CONTIKI_CONF_H_ #define CONTIKI_CONF_H_ /*---------------------------------------------------------------------------*/ -#include "board-conf.h" -/*---------------------------------------------------------------------------*/ -/* Include Project Specific conf */ +/* Include project-specific configurations */ #ifdef PROJECT_CONF_PATH #include PROJECT_CONF_PATH #endif /*---------------------------------------------------------------------------*/ -/* Include CPU-related configuration */ -#include "cc13xx-cc26xx-conf.h" +/* Include board-specific configurations */ +#include "board-conf.h" /*---------------------------------------------------------------------------*/ -/* Must be included after cc13xx-cc26xx-conf.h */ -#include +/* Include CPU-related configurations */ +#include "cc13xx-cc26xx-conf.h" /*---------------------------------------------------------------------------*/ #endif /* CONTIKI_CONF_H_ */ /*---------------------------------------------------------------------------*/ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/board-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/board-conf.h index 71d59501d..5e5c75528 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/board-conf.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/board-conf.h @@ -50,7 +50,7 @@ #ifndef BOARD_CONF_H_ #define BOARD_CONF_H_ /*---------------------------------------------------------------------------*/ -#include "contiki-conf.h" +#include "rf-conf.h" /*---------------------------------------------------------------------------*/ /** * \name LED configurations for the dev/leds.h API. diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/Board.h index 6b3c762d7..88999fe6f 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/Board.h @@ -36,6 +36,9 @@ #define Board_CC1310_LAUNCHXL #define BOARD_STRING "TI CC1310 LaunchPad" +#define RF_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + #ifdef __cplusplus extern "C" { #endif diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/CC1310_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/CC1310_LAUNCHXL.c index b33dab9ad..b5db4e61d 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/CC1310_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/CC1310_LAUNCHXL.c @@ -498,7 +498,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1310_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1310_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1310_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1310_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1310_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1310_LAUNCHXL_UART_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1310_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC1310_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/rf-conf.h new file mode 100644 index 000000000..d26d3a864 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1310/rf-conf.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for the Sub-1 GHz path + * on the radio. + * + * These are the following front-end mode configurations for the + * CC1310-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/Board.h index c3763b019..a230c764e 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1312R1_LAUNCHXL -#define BOARD_STRING "TI CC1312R1 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1312R1_LAUNCHXL.h" +#define Board_CC1312R1_LAUNCHXL +#define BOARD_STRING "TI CC1312R1 LaunchPad" + #define Board_initGeneral() CC1312R1_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC1312R1_LAUNCHXL_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1312R1_LAUNCHXL_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/CC1312R1_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/CC1312R1_LAUNCHXL.c index 44fd41035..a51bba632 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/CC1312R1_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/CC1312R1_LAUNCHXL.c @@ -619,7 +619,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1312R1_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1312R1_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1312R1_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1312R1_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1312R1_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1312R1_LAUNCHXL_UART_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1312R1_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC1312R1_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/rf-conf.h new file mode 100644 index 000000000..f649dd170 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1312r1/rf-conf.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for the Sub-1 GHz path + * on the radio. + * + * These are the following front-end mode configurations for the + * CC1312R1-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/Board.h index c8094432a..4b8fbe5c0 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1350_LAUNCHXL_433 -#define BOARD_STRING "TI CC1350-433 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1350_LAUNCHXL_433.h" +#define Board_CC1350_LAUNCHXL_433 +#define BOARD_STRING "TI CC1350-433 LaunchPad" + #define Board_initGeneral() CC1350_LAUNCHXL_433_initGeneral() #define Board_shutDownExtFlash() CC1350_LAUNCHXL_433_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1350_LAUNCHXL_433_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433.c index b4d34a54b..aa690dff4 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433.c @@ -493,7 +493,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1350_LAUNCHXL_433_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1350_LAUNCHXL_433_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1350_LAUNCHXL_433_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1350_LAUNCHXL_433_UART_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1350_LAUNCHXL_433_UART_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1350_LAUNCHXL_433_UART_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1350_LAUNCHXL_433_DIO1_RF_SUB1GHZ | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX, /* RF SW Switch defaults to 2.4 GHz path*/ CC1350_LAUNCHXL_433_DIO30_RF_POWER | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX, /* External RF Switch is powered off by default */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433_fxns.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433_fxns.c index a8a6b16c0..92e349480 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433_fxns.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/CC1350_LAUNCHXL_433_fxns.c @@ -165,23 +165,60 @@ void Board_initHook() #if defined(Board_RF_SUB1GHZ) /* - * ======== CC1350_LAUNCHXL_433_rfDriverCallback ======== - * This is an implementation for the CC1350 launchpad which uses a + * Mask to be used to determine the effective value of the setup command's + * loDivider field. + */ +#define LODIVIDER_MASK 0x7F + +/* + * ======== rfDriverCallback ======== + * This is an implementation for the CC1350 LaunchPad which uses a * single signal for antenna switching. */ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) { + /* Decode input arguments. */ (void)client; RF_RadioSetup* setupCommand = (RF_RadioSetup*)arg; + /* Local variable. */ + bool sub1GHz = false; + uint8_t loDivider = 0; + if (events & RF_GlobalEventRadioSetup) { /* Power up the antenna switch */ PINCC26XX_setOutputValue(Board_RF_POWER, 1); - if (setupCommand->common.commandNo == CMD_PROP_RADIO_DIV_SETUP) { - /* Sub-1 GHz, requires antenna switch high */ + /* Decision about the frequency band shall be made based on the + loDivider field. */ + switch (setupCommand->common.commandNo) { + case (CMD_RADIO_SETUP): + case (CMD_BLE5_RADIO_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->common.loDivider; + + /* Sub-1 GHz, requires antenna switch high. */ + if ((loDivider != 0) && (loDivider != 2)) { + sub1GHz = true; + } + break; + case (CMD_PROP_RADIO_DIV_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->prop_div.loDivider; + + /* Sub-1 GHz, requires antenna switch high. */ + if ((loDivider != 0) && (loDivider != 2)) { + sub1GHz = true; + } + break; + default:break; + } + + /* Select the correct antenna. */ + if (sub1GHz) { PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 1); } + else { + PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 0); + } } else if (events & RF_GlobalEventRadioPowerDown) { /* Disable antenna switch to save current */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/rf-conf.h new file mode 100644 index 000000000..371af4d89 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350-4/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1350-4-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/Board.h index 2099b1845..97ff04a59 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1350_LAUNCHXL -#define BOARD_STRING "TI CC1350 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1350_LAUNCHXL.h" +#define Board_CC1350_LAUNCHXL +#define BOARD_STRING "TI CC1350 LaunchPad" + #define Board_initGeneral() CC1350_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC1350_LAUNCHXL_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1350_LAUNCHXL_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL.c index 1277fc66a..ff7f47351 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL.c @@ -498,7 +498,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1350_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1350_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1350_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1350_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1350_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1350_LAUNCHXL_UART_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1350_LAUNCHXL_DIO1_RF_SUB1GHZ | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX, /* RF SW Switch defaults to 2.4 GHz path*/ CC1350_LAUNCHXL_DIO30_RF_POWER | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX, /* External RF Switch is powered off by default */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL_fxns.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL_fxns.c index 90f69a0c0..56de7c562 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL_fxns.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/CC1350_LAUNCHXL_fxns.c @@ -166,6 +166,12 @@ void Board_initHook() */ #if defined(Board_RF_SUB1GHZ) +/* + * Mask to be used to determine the effective value of the setup command's + * loDivider field. + */ +#define LODIVIDER_MASK 0x7F + /* * ======== rfDriverCallback ======== * This is an implementation for the CC1350 launchpad which uses a @@ -173,17 +179,48 @@ void Board_initHook() */ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) { + /* Decode input arguments. */ (void)client; RF_RadioSetup* setupCommand = (RF_RadioSetup*)arg; + /* Local variable. */ + bool sub1GHz = false; + uint8_t loDivider = 0; + if (events & RF_GlobalEventRadioSetup) { /* Power up the antenna switch */ PINCC26XX_setOutputValue(Board_RF_POWER, 1); - if (setupCommand->common.commandNo == CMD_PROP_RADIO_DIV_SETUP) { - /* Sub-1 GHz, requires antenna switch high */ + /* Decision about the frequency band shall be made based on the + loDivider field. */ + switch (setupCommand->common.commandNo) { + case (CMD_RADIO_SETUP): + case (CMD_BLE5_RADIO_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->common.loDivider; + + /* Sub-1 GHz, requires antenna switch high. */ + if ((loDivider != 0) && (loDivider != 2)) { + sub1GHz = true; + } + break; + case (CMD_PROP_RADIO_DIV_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->prop_div.loDivider; + + /* Sub-1 GHz, requires antenna switch high. */ + if ((loDivider != 0) && (loDivider != 2)) { + sub1GHz = true; + } + break; + default:break; + } + + /* Select the correct antenna. */ + if (sub1GHz) { PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 1); } + else { + PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 0); + } } else if (events & RF_GlobalEventRadioPowerDown) { /* Disable antenna switch to save current */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/rf-conf.h new file mode 100644 index 000000000..e6cd0b744 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1350/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1350-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/Board.h index d22d73c17..40f76e18b 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1352P_2_LAUNCHXL -#define BOARD_STRING "TI CC1352P-2 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1352P_2_LAUNCHXL.h" +#define Board_CC1352P_2_LAUNCHXL +#define BOARD_STRING "TI CC1352P-2 LaunchPad" + #define Board_initGeneral() CC1352P_2_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC1352P_2_LAUNCHXL_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1352P_2_LAUNCHXL_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL.c index a1b74bce4..c2717cc70 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL.c @@ -585,7 +585,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1352P_2_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352P_2_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352P_2_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1352P_2_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1352P_2_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1352P_2_LAUNCHXL_UART0_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1352P_2_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC1352P_2_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL_fxns.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL_fxns.c index f8b4930c8..57f545360 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL_fxns.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/CC1352P_2_LAUNCHXL_fxns.c @@ -152,6 +152,12 @@ void CC1352P_2_LAUNCHXL_shutDownExtFlash(void) */ #if defined(Board_RF_SUB1GHZ) +/* + * Mask to be used to determine the effective value of the setup command's + * loDivider field. + */ +#define LODIVIDER_MASK 0x7F + /* * ======== Antenna switching ======== */ @@ -170,7 +176,7 @@ void initAntennaSwitch() } /* - * ======== rfDriverCallback ======== + * ======== CC1352P1_LAUNCHXL_rfDriverCallback ======== * Sets up the antenna switch depending on the current PHY configuration. * Truth table: * @@ -183,6 +189,10 @@ void initAntennaSwitch() */ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) { + /* Local variable. */ + bool sub1GHz = false; + uint8_t loDivider = 0; + /* Switch off all paths first. Needs to be done anyway in every sub-case below. */ PINCC26XX_setOutputValue(Board_RF_24GHZ, 0); PINCC26XX_setOutputValue(Board_RF_HIGH_PA, 0); @@ -195,15 +205,36 @@ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) /* Decode the generic argument as a setup command. */ RF_RadioSetup* setupCommand = (RF_RadioSetup*)arg; - if (setupCommand->common.commandNo == CMD_PROP_RADIO_DIV_SETUP) { + switch (setupCommand->common.commandNo) { + case (CMD_RADIO_SETUP): + case (CMD_BLE5_RADIO_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->common.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + case (CMD_PROP_RADIO_DIV_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->prop_div.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + default:break; + } + + if (sub1GHz) { /* Sub-1 GHz */ if (paType == RF_TxPowerTable_HighPA) { /* PA enable --> HIGH PA * LNA enable --> Sub-1 GHz */ PINCC26XX_setMux(antennaPins, Board_RF_24GHZ, PINCC26XX_MUX_GPIO); - // Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not - // de-asserted on CC1352 Rev A. + /* Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not + de-asserted on CC1352 Rev A. */ PINCC26XX_setMux(antennaPins, Board_RF_HIGH_PA, PINCC26XX_MUX_RFC_GPO3); PINCC26XX_setMux(antennaPins, Board_RF_SUB1GHZ, PINCC26XX_MUX_RFC_GPO0); } else { @@ -221,8 +252,8 @@ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) * LNA enable --> 2.4 GHz */ PINCC26XX_setMux(antennaPins, Board_RF_24GHZ, PINCC26XX_MUX_RFC_GPO0); - // Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not - // de-asserted on CC1352 Rev A. + /* Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not + de-asserted on CC1352 Rev A. */ PINCC26XX_setMux(antennaPins, Board_RF_HIGH_PA, PINCC26XX_MUX_RFC_GPO3); PINCC26XX_setMux(antennaPins, Board_RF_SUB1GHZ, PINCC26XX_MUX_GPIO); } else { diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/rf-conf.h new file mode 100644 index 000000000..1eb0926b7 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-2/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1352P-2-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/Board.h index 381651708..de9bc8e9d 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1352P_4_LAUNCHXL -#define BOARD_STRING "TI CC1352P-4 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1352P_4_LAUNCHXL.h" +#define Board_CC1352P_4_LAUNCHXL +#define BOARD_STRING "TI CC1352P-4 LaunchPad" + #define Board_initGeneral() CC1352P_4_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC1352P_4_LAUNCHXL_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1352P_4_LAUNCHXL_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL.c index 63a41d64b..bd4f43032 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL.c @@ -585,7 +585,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1352P_4_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352P_4_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352P_4_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1352P_4_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1352P_4_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1352P_4_LAUNCHXL_UART0_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1352P_4_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC1352P_4_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL_fxns.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL_fxns.c index ef9c0531c..eea9c3b33 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL_fxns.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/CC1352P_4_LAUNCHXL_fxns.c @@ -152,6 +152,12 @@ void CC1352P_4_LAUNCHXL_shutDownExtFlash(void) */ #if defined(Board_RF_SUB1GHZ) +/* + * Mask to be used to determine the effective value of the setup command's + * loDivider field. + */ +#define LODIVIDER_MASK 0x7F + /* * ======== Antenna switching ======== */ @@ -170,7 +176,7 @@ void initAntennaSwitch() } /* - * ======== rfDriverCallback ======== + * ======== CC1352P1_LAUNCHXL_rfDriverCallback ======== * Sets up the antenna switch depending on the current PHY configuration. * Truth table: * @@ -183,6 +189,10 @@ void initAntennaSwitch() */ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) { + /* Local variable. */ + bool sub1GHz = false; + uint8_t loDivider = 0; + /* Switch off all paths first. Needs to be done anyway in every sub-case below. */ PINCC26XX_setOutputValue(Board_RF_24GHZ, 0); PINCC26XX_setOutputValue(Board_RF_HIGH_PA, 0); @@ -195,15 +205,36 @@ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) /* Decode the generic argument as a setup command. */ RF_RadioSetup* setupCommand = (RF_RadioSetup*)arg; - if (setupCommand->common.commandNo == CMD_PROP_RADIO_DIV_SETUP) { + switch (setupCommand->common.commandNo) { + case (CMD_RADIO_SETUP): + case (CMD_BLE5_RADIO_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->common.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + case (CMD_PROP_RADIO_DIV_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->prop_div.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + default:break; + } + + if (sub1GHz) { /* Sub-1 GHz */ if (paType == RF_TxPowerTable_HighPA) { /* PA enable --> HIGH PA * LNA enable --> Sub-1 GHz */ PINCC26XX_setMux(antennaPins, Board_RF_24GHZ, PINCC26XX_MUX_GPIO); - // Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not - // de-asserted on CC1352 Rev A. + /* Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not + de-asserted on CC1352 Rev A. */ PINCC26XX_setMux(antennaPins, Board_RF_HIGH_PA, PINCC26XX_MUX_RFC_GPO3); PINCC26XX_setMux(antennaPins, Board_RF_SUB1GHZ, PINCC26XX_MUX_RFC_GPO0); } else { @@ -221,8 +252,8 @@ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) * LNA enable --> 2.4 GHz */ PINCC26XX_setMux(antennaPins, Board_RF_24GHZ, PINCC26XX_MUX_RFC_GPO0); - // Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not - // de-asserted on CC1352 Rev A. + /* Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not + de-asserted on CC1352 Rev A. */ PINCC26XX_setMux(antennaPins, Board_RF_HIGH_PA, PINCC26XX_MUX_RFC_GPO3); PINCC26XX_setMux(antennaPins, Board_RF_SUB1GHZ, PINCC26XX_MUX_GPIO); } else { diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/rf-conf.h new file mode 100644 index 000000000..af183614b --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p-4/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1352P-4-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/Board.h index 1fef09180..db07b3a0d 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1352P1_LAUNCHXL -#define BOARD_STRING "TI CC1352P1 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1352P1_LAUNCHXL.h" +#define Board_CC1352P1_LAUNCHXL +#define BOARD_STRING "TI CC1352P1 LaunchPad" + #define Board_initGeneral() CC1352P1_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC1352P1_LAUNCHXL_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1352P1_LAUNCHXL_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL.c index d4f953fba..cff74bc03 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL.c @@ -585,7 +585,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1352P1_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352P1_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352P1_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1352P1_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1352P1_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1352P1_LAUNCHXL_UART0_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1352P1_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC1352P1_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL_fxns.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL_fxns.c index 9f3000929..795f60106 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL_fxns.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/CC1352P1_LAUNCHXL_fxns.c @@ -152,6 +152,12 @@ void CC1352P1_LAUNCHXL_shutDownExtFlash(void) */ #if defined(Board_RF_SUB1GHZ) +/* + * Mask to be used to determine the effective value of the setup command's + * loDivider field. + */ +#define LODIVIDER_MASK 0x7F + /* * ======== Antenna switching ======== */ @@ -170,7 +176,7 @@ void initAntennaSwitch() } /* - * ======== rfDriverCallback ======== + * ======== CC1352P1_LAUNCHXL_rfDriverCallback ======== * Sets up the antenna switch depending on the current PHY configuration. * Truth table: * @@ -183,6 +189,10 @@ void initAntennaSwitch() */ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) { + /* Local variable. */ + bool sub1GHz = false; + uint8_t loDivider = 0; + /* Switch off all paths first. Needs to be done anyway in every sub-case below. */ PINCC26XX_setOutputValue(Board_RF_24GHZ, 0); PINCC26XX_setOutputValue(Board_RF_HIGH_PA, 0); @@ -195,15 +205,36 @@ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) /* Decode the generic argument as a setup command. */ RF_RadioSetup* setupCommand = (RF_RadioSetup*)arg; - if (setupCommand->common.commandNo == CMD_PROP_RADIO_DIV_SETUP) { + switch (setupCommand->common.commandNo) { + case (CMD_RADIO_SETUP): + case (CMD_BLE5_RADIO_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->common.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + case (CMD_PROP_RADIO_DIV_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->prop_div.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + default:break; + } + + if (sub1GHz) { /* Sub-1 GHz */ if (paType == RF_TxPowerTable_HighPA) { /* PA enable --> HIGH PA * LNA enable --> Sub-1 GHz */ PINCC26XX_setMux(antennaPins, Board_RF_24GHZ, PINCC26XX_MUX_GPIO); - // Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not - // de-asserted on CC1352 Rev A. + /* Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not + de-asserted on CC1352 Rev A. */ PINCC26XX_setMux(antennaPins, Board_RF_HIGH_PA, PINCC26XX_MUX_RFC_GPO3); PINCC26XX_setMux(antennaPins, Board_RF_SUB1GHZ, PINCC26XX_MUX_RFC_GPO0); } else { @@ -221,8 +252,8 @@ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) * LNA enable --> 2.4 GHz */ PINCC26XX_setMux(antennaPins, Board_RF_24GHZ, PINCC26XX_MUX_RFC_GPO0); - // Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not - // de-asserted on CC1352 Rev A. + /* Note: RFC_GPO3 is a work-around because the RFC_GPO1 (PA enable signal) is sometimes not + de-asserted on CC1352 Rev A. */ PINCC26XX_setMux(antennaPins, Board_RF_HIGH_PA, PINCC26XX_MUX_RFC_GPO3); PINCC26XX_setMux(antennaPins, Board_RF_SUB1GHZ, PINCC26XX_MUX_GPIO); } else { diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/rf-conf.h new file mode 100644 index 000000000..c18573f7a --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352p1/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1352P1-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/Board.h index 8bf0ee002..83b27069a 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1352R1_LAUNCHXL -#define BOARD_STRING "TI CC1352R1 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC1352R1_LAUNCHXL.h" +#define Board_CC1352R1_LAUNCHXL +#define BOARD_STRING "TI CC1352R1 LaunchPad" + #define Board_initGeneral() CC1352R1_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC1352R1_LAUNCHXL_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1352R1_LAUNCHXL_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL.c index a9197a4f5..f3d002206 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL.c @@ -36,8 +36,6 @@ * CC1352R1_LAUNCHXL board. */ -#include "contiki.h" - #include #include #include @@ -609,7 +607,7 @@ const PIN_Config BoardGpioInitTable[] = { CC1352R1_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352R1_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC1352R1_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC1352R1_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC1352R1_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC1352R1_LAUNCHXL_UART0_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC1352R1_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC1352R1_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL_fxns.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL_fxns.c index 520b91297..79d97d7b6 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL_fxns.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/CC1352R1_LAUNCHXL_fxns.c @@ -164,23 +164,61 @@ void Board_initHook() */ #if defined(Board_RF_SUB1GHZ) +/* + * Mask to be used to determine the effective value of the setup command's + * loDivider field. + */ +#define LODIVIDER_MASK 0x7F + /* * ======== rfDriverCallback ======== - * This is an implementation for the CC1352R1 launchpad which uses a + * This is an implementation for the CC1352R launchpad which uses a * single signal for antenna switching. */ void rfDriverCallback(RF_Handle client, RF_GlobalEvent events, void *arg) { + /* Decode input arguments. */ (void)client; RF_RadioSetup* setupCommand = (RF_RadioSetup*)arg; - if ((events & RF_GlobalEventRadioSetup) && - (setupCommand->common.commandNo == CMD_PROP_RADIO_DIV_SETUP)) { - /* Sub-1 GHz, requires antenna switch high */ - PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 1); + /* Local variable. */ + bool sub1GHz = false; + uint8_t loDivider = 0; + + if (events & RF_GlobalEventRadioSetup) { + /* Decision about the frequency band shall be made based on the + loDivider field. */ + switch (setupCommand->common.commandNo) { + case (CMD_RADIO_SETUP): + case (CMD_BLE5_RADIO_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->common.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + case (CMD_PROP_RADIO_DIV_SETUP): + loDivider = LODIVIDER_MASK & setupCommand->prop_div.loDivider; + + /* Sub-1GHz front-end. */ + if (loDivider != 0) { + sub1GHz = true; + } + break; + default:break; + } + + /* Select the correct antenna. */ + if (sub1GHz) { + PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 1); + } + else { + PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 0); + } } else if (events & RF_GlobalEventRadioPowerDown) { - /* Disable antenna switch to save current */ + /* Set the antenna to 2.4 GHz as default. */ PINCC26XX_setOutputValue(Board_RF_SUB1GHZ, 0); } } diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/rf-conf.h new file mode 100644 index 000000000..ba652cd53 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc1352r1/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1352R1-LAUNCHXL board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/Board.h index 8f3e437f0..577ce0422 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/Board.h @@ -33,18 +33,18 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC2650_LAUNCHXL -#define BOARD_STRING "TI CC2650 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC2650_LAUNCHXL.h" -#define Board_initGeneral() CC2650_LAUNCHXL_initGeneral() +#define Board_CC2650_LAUNCHXL +#define BOARD_STRING "TI CC2650 LaunchPad" + +#define Board_initGeneral() CC2650_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC2650_LAUNCHXL_shutDownExtFlash() -#define Board_wakeUpExtFlash() CC2650_LAUNCHXL_wakeUpExtFlash() +#define Board_wakeUpExtFlash() CC2650_LAUNCHXL_wakeUpExtFlash() /* These #defines allow us to reuse TI-RTOS across other device families */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/CC2650_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/CC2650_LAUNCHXL.c index ed0a4796e..02fd96471 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/CC2650_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/CC2650_LAUNCHXL.c @@ -497,7 +497,7 @@ const PIN_Config BoardGpioInitTable[] = { CC2650_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC2650_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC2650_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC2650_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC2650_LAUNCHXL_UART_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC2650_LAUNCHXL_UART_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC2650_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC2650_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/rf-conf.h new file mode 100644 index 000000000..9d6abaeac --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc2650/rf-conf.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for the 2.4 GHz path + * on the radio. + * + * These are the following front-end mode configurations for the + * CC2650-LAUNCHXL board: + * - 2.4 GHz: differential and internal bias + * + * @{ + */ +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_INTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/Board.h index a19145b2c..ddf5e4946 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/Board.h @@ -33,20 +33,18 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC26X2R1_LAUNCHXL -#define BOARD_STRING "TI CC26x2R1 LaunchPad" - #ifdef __cplusplus extern "C" { #endif #include "CC26X2R1_LAUNCHXL.h" -#define Board_initGeneral() CC26X2R1_LAUNCHXL_initGeneral() +#define Board_CC26X2R1_LAUNCHXL +#define BOARD_STRING "TI CC26x2R1 LaunchPad" +#define Board_initGeneral() CC26X2R1_LAUNCHXL_initGeneral() #define Board_shutDownExtFlash() CC26X2R1_LAUNCHXL_shutDownExtFlash() - -#define Board_wakeUpExtFlash() CC26X2R1_LAUNCHXL_wakeUpExtFlash() +#define Board_wakeUpExtFlash() CC26X2R1_LAUNCHXL_wakeUpExtFlash() /* These #defines allow us to reuse TI-RTOS across other device families */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/CC26X2R1_LAUNCHXL.c b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/CC26X2R1_LAUNCHXL.c index db47744de..854d1d2ba 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/CC26X2R1_LAUNCHXL.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/CC26X2R1_LAUNCHXL.c @@ -35,7 +35,6 @@ * This file is responsible for setting up the board specific items for the * CC26X2R1_LAUNCHXL board. */ -#include "contiki.h" #include #include @@ -619,7 +618,7 @@ const PIN_Config BoardGpioInitTable[] = { CC26X2R1_LAUNCHXL_PIN_BTN1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC26X2R1_LAUNCHXL_PIN_BTN2 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES | PIN_HYSTERESIS, /* Button is active low */ CC26X2R1_LAUNCHXL_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, /* External flash chip select */ - CC26X2R1_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLDOWN, /* UART RX via debugger back channel */ + CC26X2R1_LAUNCHXL_UART0_RX | PIN_INPUT_EN | PIN_PULLUP, /* UART RX via debugger back channel */ CC26X2R1_LAUNCHXL_UART0_TX | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL, /* UART TX via debugger back channel */ CC26X2R1_LAUNCHXL_SPI0_MOSI | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master out - slave in */ CC26X2R1_LAUNCHXL_SPI0_MISO | PIN_INPUT_EN | PIN_PULLDOWN, /* SPI master in - slave out */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/rf-conf.h new file mode 100644 index 000000000..ed58c8732 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/launchpad/cc26x2r1/rf-conf.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for the 2.4 GHz path + * on the radio. + * + * These are the following front-end mode configurations for the + * CC26X2R1-LAUNCHXL board: + * - 2.4 GHz: differential and internal bias + * + * @{ + */ +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_INTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/platform.c b/arch/platform/simplelink/cc13xx-cc26xx/platform.c index c78fe1946..56030445a 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/platform.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/platform.c @@ -250,9 +250,9 @@ platform_init_stage_three(void) LOG_INFO("RF: Channel %d", chan); if(NETSTACK_RADIO.get_value(RADIO_PARAM_PAN_ID, &pan) == RADIO_RESULT_OK) { - LOG_INFO(", PANID 0x%04X", pan); + LOG_INFO_(", PANID 0x%04X", pan); } - LOG_INFO("\n"); + LOG_INFO_("\n"); LOG_INFO("Node ID: %d\n", node_id); diff --git a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/board-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/board-conf.h index bb8689c98..5ff2f78d8 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/board-conf.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/board-conf.h @@ -50,8 +50,7 @@ #ifndef BOARD_CONF_H_ #define BOARD_CONF_H_ /*---------------------------------------------------------------------------*/ -#include "contiki-conf.h" -/*---------------------------------------------------------------------------*/ +#include "rf-conf.h" #include "leds-arch.h" /*---------------------------------------------------------------------------*/ /** diff --git a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/Board.h index ecdb6d2af..e32e74fc1 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1350STK -#define BOARD_STRING "TI CC1350 SensorTag" - #ifdef __cplusplus extern "C" { #endif #include "CC1350STK.h" +#define Board_CC1350STK +#define BOARD_STRING "TI CC1350 SensorTag" + #define Board_initGeneral() CC1350STK_initGeneral() #define Board_shutDownExtFlash() CC1350STK_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1350STK_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/rf-conf.h new file mode 100644 index 000000000..1f3dfbe52 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc1350/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1350STK board: + * - Sub-1 GHz: single-ended RFN and external bias + * - 2.4 GHz: single-ended RFP and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_SINGLE_ENDED_RFN +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_SINGLE_ENDED_RFP +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/Board.h index e334645bb..8425de454 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC2650STK -#define BOARD_STRING "TI CC2650 SensorTag" - #ifdef __cplusplus extern "C" { #endif #include "CC2650STK.h" +#define Board_CC2650STK +#define BOARD_STRING "TI CC2650 SensorTag" + #define Board_initGeneral() CC2650STK_initGeneral() #define Board_shutDownExtFlash() CC2650STK_shutDownExtFlash() #define Board_wakeUpExtFlash() CC2650STK_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/rf-conf.h new file mode 100644 index 000000000..48db37a96 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/cc2650/rf-conf.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for the 2.4 GHz path + * on the radio. + * + * These are the following front-end mode configurations for the + * CC2650STK board: + * - 2.4 GHz: differential and internal bias + * + * @{ + */ +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_INTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/opt-3001-sensor.c b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/opt-3001-sensor.c index 885fefe88..42e3976d8 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/sensortag/opt-3001-sensor.c +++ b/arch/platform/simplelink/cc13xx-cc26xx/sensortag/opt-3001-sensor.c @@ -303,9 +303,13 @@ value(int type) result_value = SWAP16(result_value); - uint32_t e = (result_value & 0x0FFF) >> 0; - uint32_t m = (result_value & 0xF000) >> 12; - uint32_t converted = m * 100 * (1 << e); + /* formula for computing lux: lux = 0.01 * 2^e * m + * scale up by 100 to avoid floating point, then require + * users to scale down by same. + */ + uint32_t m = (result_value & 0x0FFF) >> 0; + uint32_t e = (result_value & 0xF000) >> 12; + uint32_t converted = m * (1 << e); PRINTF("OPT: %04X r=%d (centilux)\n", result_value, (int)(converted)); diff --git a/arch/platform/simplelink/cc13xx-cc26xx/srf06/board-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/srf06/board-conf.h index 037e7eb3f..7d6f8328f 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/srf06/board-conf.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/srf06/board-conf.h @@ -42,6 +42,8 @@ #ifndef BOARD_CONF_H_ #define BOARD_CONF_H_ /*---------------------------------------------------------------------------*/ +#include "rf-conf.h" +/*---------------------------------------------------------------------------*/ /** * \name LED configurations for the dev/leds.h API. * diff --git a/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/Board.h index 0941b222f..48d232e54 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC1350DK_7XD -#define BOARD_STRING "TI SmartRF06EB + CC13x0 EM" - #ifdef __cplusplus extern "C" { #endif #include "CC1350DK_7XD.h" +#define Board_CC1350DK_7XD +#define BOARD_STRING "TI SmartRF06EB + CC13x0 EM" + #define Board_initGeneral() CC1350DK_7XD_initGeneral() #define Board_shutDownExtFlash() CC1350DK_7XD_shutDownExtFlash() #define Board_wakeUpExtFlash() CC1350DK_7XD_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/rf-conf.h new file mode 100644 index 000000000..327e8fb5d --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc13x0/rf-conf.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for both the Sub-1 GHz + * path and the 2.4 GHz path on the radio. + * + * These are the following front-end mode configurations for the + * CC1350DK-7XD board: + * - Sub-1 GHz: differential and external bias + * - 2.4 GHz: differential and external bias + * + * @{ + */ +#define RF_SUB_1_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_SUB_1_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL + +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_EXTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/Board.h b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/Board.h index c42d3a169..d90291418 100644 --- a/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/Board.h +++ b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/Board.h @@ -33,15 +33,15 @@ #ifndef __BOARD_H #define __BOARD_H -#define Board_CC2650DK_7ID -#define BOARD_STRING "TI SmartRF06EB + CC26x0 EM" - #ifdef __cplusplus extern "C" { #endif #include "CC2650DK_7ID.h" +#define Board_CC2650DK_7ID +#define BOARD_STRING "TI SmartRF06EB + CC26x0 EM" + #define Board_initGeneral() CC2650DK_7ID_initGeneral() #define Board_shutDownExtFlash() CC2650DK_7ID_shutDownExtFlash() #define Board_wakeUpExtFlash() CC2650DK_7ID_wakeUpExtFlash() diff --git a/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/rf-conf.h b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/rf-conf.h new file mode 100644 index 000000000..bdbade005 --- /dev/null +++ b/arch/platform/simplelink/cc13xx-cc26xx/srf06/cc26x0/rf-conf.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup launchpad-peripherals + * @{ + * + * \file + * Header file with board-specific RF configurations. + * \author + * Texas Instruments + * \note + * This file should not be included directly + */ +/*---------------------------------------------------------------------------*/ +#ifndef RF_CONF_H_ +#define RF_CONF_H_ +/*---------------------------------------------------------------------------*/ +#include "rf/rf.h" +/*---------------------------------------------------------------------------*/ +/** + * \name Board-specific front-end mode configurations for the 2.4 GHz path + * on the radio. + * + * These are the following front-end mode configurations for the + * CC2650DK-7ID board: + * - 2.4 GHz: differential and internal bias + * + * @{ + */ +#define RF_2_4_GHZ_CONF_FRONT_END_MODE RF_FRONT_END_MODE_DIFFERENTIAL +#define RF_2_4_GHZ_CONF_BIAS_MODE RF_BIAS_MODE_INTERNAL +/** @} */ +/*---------------------------------------------------------------------------*/ +#endif /* RF_CONF_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + */ diff --git a/arch/platform/sky/Makefile.common b/arch/platform/sky/Makefile.common index 5018da3da..b72f92ed8 100644 --- a/arch/platform/sky/Makefile.common +++ b/arch/platform/sky/Makefile.common @@ -18,7 +18,7 @@ endif CONTIKI_TARGET_SOURCEFILES += $(ARCH) $(UIPDRIVERS) MCU=msp430f1611 -include $(CONTIKI)/arch/cpu/msp430/Makefile.msp430 +include $(ARCH_PATH)/cpu/msp430/Makefile.msp430 ifdef IAR diff --git a/arch/platform/sky/Makefile.sky b/arch/platform/sky/Makefile.sky index 0398486b0..ec2324085 100644 --- a/arch/platform/sky/Makefile.sky +++ b/arch/platform/sky/Makefile.sky @@ -4,6 +4,6 @@ CONTIKI_TARGET_SOURCEFILES += contiki-sky-platform.c \ sht11.c sht11-sensor.c light-sensor.c battery-sensor.c \ button-sensor.c -include $(CONTIKI)/arch/platform/sky/Makefile.common +include $(ARCH_PATH)/platform/sky/Makefile.common MODULES += arch/dev/cc2420 arch/dev/sht11 arch/dev/ds2411 os/storage/cfs diff --git a/arch/platform/z1/Makefile.common b/arch/platform/z1/Makefile.common new file mode 100644 index 000000000..42172022b --- /dev/null +++ b/arch/platform/z1/Makefile.common @@ -0,0 +1,145 @@ + +# +# Currently enabling SMALL breaks the build on this platform. +# No large effect is expected anyway: unused sections are discarded even if SMALL is not set. +# +SMALL=0 + +ARCH = leds.c xmem.c i2cmaster.c \ + spi-legacy.c cc2420.c cc2420-arch.c cc2420-arch-sfd.c\ + node-id-z1.c sensors.c button-sensor.c cfs-coffee.c \ + uart0.c uart0-putchar.c uip-ipchksum.c \ + slip.c slip_uart0.c z1-sensors.c adxl345.c temperature-sensor.c \ + z1-phidgets.c light-sensor.c battery-sensor.c sky-sensors.c tmp102.c \ + platform.c + +CONTIKI_TARGET_DIRS = . dev apps +ifndef CONTIKI_TARGET_MAIN +CONTIKI_TARGET_MAIN = contiki-main.c +endif + +CONTIKI_TARGET_SOURCEFILES += $(ARCH) + +MCU=msp430f2617 +CPU_HAS_MSP430X=1 + +LDFLAGS += -Wl,--defsym -Wl,__P1SEL2=0x0041 -Wl,--defsym -Wl,__P5SEL2=0x0045 + +ifdef nodemac +CFLAGS += -DMACID=$(nodemac) +endif + +CFLAGS += -g + +include $(ARCH_PATH)/cpu/msp430/Makefile.msp430 + + +NUMPAR=20 +IHEXFILE=tmpimage.ihex + +ifeq ($(HOST_OS),Darwin) + ifndef MOTELIST + USBDEVPREFIX= + SERIALDUMP = $(CONTIKI)/tools/sky/serialdump-linux + MOTELIST = $(CONTIKI)/tools/zolertia/motelist-zolertia-macos + BSL = $(CONTIKI)/tools/zolertia/z1-bsl-nopic --z1 + BSL_FILETYPE = -I + MOTES = $(shell $(MOTELIST) -b z1 -c 2>&- | \ + cut -f 2 -d ,) + REFNUM = $(shell $(MOTELIST) -c 2>&- | \ + cut -f 1 -d , | tail -c5 | sed 's/^0*//') + ifneq (,$(REFNUM)) + # No device fo-und + ifeq (,$(findstring und, $(REFNUM))) + CFLAGS += -DSERIALNUM=$(REFNUM:0%=%) + endif + endif + endif +else + # If we are not running under Mac, we assume Linux + ifndef MOTELIST + USBDEVPREFIX= + SERIALDUMP = $(CONTIKI)/tools/sky/serialdump-linux + MOTELIST = $(CONTIKI)/tools/zolertia/motelist-zolertia + BSL = $(CONTIKI)/tools/zolertia/z1-bsl-nopic --z1 + BSL_FILETYPE = -I + MOTES = $(shell $(MOTELIST) -b z1 -c 2>&- | \ + cut -f 2 -d , | \ + perl -ne 'print $$1 . " " if(m-(/dev/\w+)-);') + CMOTES=$(MOTES) + REFNUM = $(shell $(MOTELIST) -c 2>&- | \ + cut -f 1 -d , | tail -c5 | sed 's/^0*//') + ifneq (,$(REFNUM)) + # No device fo-und + ifeq (,$(findstring und, $(REFNUM))) + CFLAGS += -DSERIALNUM=$(REFNUM) + endif + endif + endif +endif + +motelist: + $(MOTELIST) +z1-motelist: + $(MOTELIST) -b z1 +z1-motes: + @echo $(MOTES) + +ifdef MOTE +%.upload: %.ihex + cp $< $(IHEXFILE) + $(MAKE) z1-u.$(subst /,-,$(word $(MOTE), $(MOTES))) +else # MOTE +%.upload: %.ihex + cp $< $(IHEXFILE) + @echo $(MOTES) + $(MAKE) z1-reset z1-upload +endif # MOTE + +z1-upload: z1-reset + $(MAKE) -j $(NUMPAR) z1-upload-sequence + +z1-upload-sequence: $(foreach PORT, $(MOTES), z1-u.$(subst /,-,$(PORT))) + @echo Done + +z1-reset: + $(MAKE) -k -j $(NUMPAR) z1-reset-sequence + +z1-reset-sequence: $(foreach PORT, $(MOTES), z1-r.$(subst /,-,$(PORT))) + @echo Done + +z1-u.%: + @echo +++++ Erasing $(subst -,/,$*); \ + $(BSL) -c $(subst -,/,$*) -e && sleep 2 ; \ + echo +++++ Programming $(subst -,/,$*) ; \ + $(BSL) -c $(subst -,/,$*) $(BSL_FILETYPE) -p $(IHEXFILE) && sleep 2 ; \ + echo +++++ Resetting $(subst -,/,$*) ; \ + $(BSL) -c $(subst -,/,$*) -r + +z1-r.%: + $(BSL) -c $(subst -,/,$*) -r + +sizeplot: + msp430-size $(OBJECTDIR)/*.o | $(CONTIKI)/tools/sky/check-size > size-data + gnuplot $(CONTIKI)/tools/sky/plot-size + gv size.pdf + +winslip: +ifdef INTERFACE + $(CONTIKI)/tools/wpcapslip/wpcapslip -s $(USBDEVPREFIX)$(firstword $(CMOTES)) $(INTERFACE) 172.16.0.0 255.255.0.0 +else + @echo "Usage: \"$(MAKE) $@ INTERFACE=\"" + @echo "Use the \"ipconfig\" command to find out the IP addresses of the local interfaces" +endif + +linslip: $(CONTIKI)/tools/tunslip + $(CONTIKI)/tools/tunslip -s $(USBDEVPREFIX)$(firstword $(CMOTES)) 172.16.0.0 255.255.0.0 + +$(CONTIKI)/tools/tunslip: + (cd $(CONTIKI)/tools; $(MAKE) tunslip) + +ifdef MOTE + PORT = $(USBDEVPREFIX)$(word $(MOTE), $(CMOTES)) +else + PORT = $(USBDEVPREFIX)$(firstword $(CMOTES)) +endif diff --git a/arch/platform/z1/Makefile.z1 b/arch/platform/z1/Makefile.z1 new file mode 100644 index 000000000..aeff61bf2 --- /dev/null +++ b/arch/platform/z1/Makefile.z1 @@ -0,0 +1,11 @@ +# Common Makefile between Z1 and Z1SP + +CONTIKI_TARGET_SOURCEFILES += contiki-z1-platform.c + +include $(ARCH_PATH)/platform/z1/Makefile.common + +ifeq ($(ZOLERTIA_Z1SP),1) +include $(ARCH_PATH)/platform/z1/Makefile.z1sp +endif + +MODULES += arch/dev/cc2420 os/storage/cfs diff --git a/arch/platform/z1/Makefile.z1sp b/arch/platform/z1/Makefile.z1sp new file mode 100644 index 000000000..29b8fcbd1 --- /dev/null +++ b/arch/platform/z1/Makefile.z1sp @@ -0,0 +1,6 @@ +# Makefile for Z1 Starter Platform + +# This is the actual flag we need to include specific Z1SP components +CFLAGS += -DZ1_IS_Z1SP + +CONTIKI_TARGET_SOURCEFILES += potentiometer-sensor.c diff --git a/arch/platform/z1/README.z1sp b/arch/platform/z1/README.z1sp new file mode 100644 index 000000000..8db1e48da --- /dev/null +++ b/arch/platform/z1/README.z1sp @@ -0,0 +1,9 @@ +Using the Z1 starter platform (Z1SP) +============================================ + +To enable the Z1SP components, you should include in your application Makefile +the ZOLERTIA_Z1SP flag set to 1, see "examples/z1/Makefile". + +For Z1SP specific information please go to: + +http://zolertia.sourceforge.net/wiki/index.php/Mainpage:z1sp diff --git a/arch/platform/z1/apps/burn-nodeid.c b/arch/platform/z1/apps/burn-nodeid.c new file mode 100644 index 000000000..0f469cac4 --- /dev/null +++ b/arch/platform/z1/apps/burn-nodeid.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * A program for burning a node ID into the flash ROM of a Tmote Sky node. + * \author + * Adam Dunkels + */ + +#include "dev/leds.h" +#include "dev/watchdog.h" +#include "sys/node-id.h" +#include "contiki.h" +#include "sys/etimer.h" + +#include "node-id-z1.h" + +#include + +static struct etimer etimer; + +PROCESS(burn_process, "Burn node id"); +AUTOSTART_PROCESSES(&burn_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(burn_process, ev, data) +{ + PROCESS_BEGIN(); + + etimer_set(&etimer, 5*CLOCK_SECOND); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + + watchdog_stop(); + leds_on(LEDS_RED); +#if NODEID + #warning "***** BURNING NODE ID" + printf("Burning node id %d\n", NODEID); + node_id_burn(NODEID); + leds_on(LEDS_BLUE); + node_id_restore(); + printf("Restored node id %d\n", node_id); +#else +#error "burn-nodeid must be compiled with nodeid=" + node_id_restore(); + printf("Restored node id %d\n", node_id); +#endif + leds_off(LEDS_RED + LEDS_BLUE); + watchdog_start(); + while(1) { + PROCESS_WAIT_EVENT(); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/cfs-coffee-arch.h b/arch/platform/z1/cfs-coffee-arch.h new file mode 100644 index 000000000..3225e2f32 --- /dev/null +++ b/arch/platform/z1/cfs-coffee-arch.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Coffee architecture-dependent header for the Zolertia Z1 platform. + * \author + * Nicolas Tsiftes + * Enric M. Calvo + */ + +#ifndef CFS_COFFEE_ARCH_H +#define CFS_COFFEE_ARCH_H + +#include "contiki-conf.h" +#include "dev/xmem.h" + +/*** M25P16 Memory Organization +The memory is organized as: +16Mbit = 2 097 152 bytes (8 bits each) +32 sectors (512 Kbits, 65536 bytes each) +8192 pages (256 bytes each). +Each page can be individually programmed (bits are programmed from 1 to 0). The device is +sector or bulk erasable (bits are erased from 0 to 1) but not page erasable +*/ + +/* Total size of the External Flash Memory in the Z1 */ +#define COFFEE_XMEM_TOTAL_SIZE_KB 2048UL + +/* Coffee configuration parameters. */ +#define COFFEE_SECTOR_SIZE 65536UL +#define COFFEE_PAGE_SIZE 256UL +#define COFFEE_START COFFEE_SECTOR_SIZE +#define COFFEE_SIZE (COFFEE_XMEM_TOTAL_SIZE_KB * 1024UL - COFFEE_START) +#define COFFEE_NAME_LENGTH 16 +#define COFFEE_MAX_OPEN_FILES 6 +#define COFFEE_FD_SET_SIZE 8 +#define COFFEE_LOG_TABLE_LIMIT 256 +#define COFFEE_DYN_SIZE 4*1024 +#define COFFEE_LOG_SIZE 1024 + +#define COFFEE_MICRO_LOGS 1 + +/* Flash operations. */ +#define COFFEE_WRITE(buf, size, offset) \ + xmem_pwrite((char *)(buf), (size), COFFEE_START + (offset)) + +#define COFFEE_READ(buf, size, offset) \ + xmem_pread((char *)(buf), (size), COFFEE_START + (offset)) + +#define COFFEE_ERASE(sector) \ + xmem_erase(COFFEE_SECTOR_SIZE, COFFEE_START + (sector) * COFFEE_SECTOR_SIZE) + +/* Coffee types. */ +typedef int16_t coffee_page_t; + +#endif /* !CFS_COFFEE_ARCH_H */ diff --git a/arch/platform/z1/contiki-conf.h b/arch/platform/z1/contiki-conf.h new file mode 100644 index 000000000..e31a9fbcd --- /dev/null +++ b/arch/platform/z1/contiki-conf.h @@ -0,0 +1,72 @@ +#ifndef CONTIKI_CONF_H +#define CONTIKI_CONF_H + +/* include the project config */ +#ifdef PROJECT_CONF_PATH +#include PROJECT_CONF_PATH +#endif /* PROJECT_CONF_PATH */ +/*---------------------------------------------------------------------------*/ +#include "z1-def.h" +#include "msp430-def.h" +/*---------------------------------------------------------------------------*/ + +/* Configure radio driver */ +#ifndef NETSTACK_CONF_RADIO +#define NETSTACK_CONF_RADIO cc2420_driver +#endif /* NETSTACK_CONF_RADIO */ + +/* Symbol for the TSCH 15ms timeslot timing template */ +#define TSCH_CONF_ARCH_HDR_PATH "dev/cc2420/cc2420-tsch-15ms.h" + +/* The TSCH default slot length of 10ms is a bit too short for this platform, + * use 15ms instead. */ +#ifndef TSCH_CONF_DEFAULT_TIMESLOT_TIMING +#define TSCH_CONF_DEFAULT_TIMESLOT_TIMING tsch_timeslot_timing_us_15000 +#endif /* TSCH_CONF_DEFAULT_TIMESLOT_TIMING */ + +/* Save RAM through a smaller uIP buffer */ +#ifndef UIP_CONF_BUFFER_SIZE +#define UIP_CONF_BUFFER_SIZE 140 +#endif + +#define PROCESS_CONF_NUMEVENTS 8 +#define PROCESS_CONF_STATS 1 +/*#define PROCESS_CONF_FASTPOLL 4*/ + +/* So far, printfs without interrupt. */ +#define UART0_CONF_TX_WITH_INTERRUPT 0 +/* This does not work in Cooja. */ +#define UART0_CONF_RX_WITH_DMA 0 + +/* Handle 10 neighbors */ +#ifndef NBR_TABLE_CONF_MAX_NEIGHBORS +#define NBR_TABLE_CONF_MAX_NEIGHBORS 10 +#endif + +/* Handle 10 routes */ +#ifndef NETSTACK_MAX_ROUTE_ENTRIES +#define NETSTACK_MAX_ROUTE_ENTRIES 10 +#endif + +/* Handle 10 links */ +#ifndef TSCH_SCHEDULE_CONF_MAX_LINKS +#define TSCH_SCHEDULE_CONF_MAX_LINKS 10 +#endif + +#ifndef TSCH_CONF_MAX_INCOMING_PACKETS +#define TSCH_CONF_MAX_INCOMING_PACKETS 2 +#endif + +#ifndef TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR +#define TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR 4 +#endif + +/* Platform-specific (H/W) AES implementation */ +#ifndef AES_128_CONF +#define AES_128_CONF cc2420_aes_128_driver +#endif /* AES_128_CONF */ + +/*---------------------------------------------------------------------------*/ +#include "msp430-conf.h" +/*---------------------------------------------------------------------------*/ +#endif /* CONTIKI_CONF_H */ diff --git a/arch/platform/z1/contiki-z1-platform.c b/arch/platform/z1/contiki-z1-platform.c new file mode 100644 index 000000000..60272bb00 --- /dev/null +++ b/arch/platform/z1/contiki-z1-platform.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2011, Zolertia(TM) is a trademark of Advancare,SL + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * Author: Enric M. Calvo based on previous work by + * Niclas Finne , Joakim Eriksson + * + */ + +#include "dev/button-sensor.h" + +void +init_platform(void) +{ + process_start(&sensors_process, NULL); +} diff --git a/arch/platform/z1/dev/adxl345.c b/arch/platform/z1/dev/adxl345.c new file mode 100644 index 000000000..08e170432 --- /dev/null +++ b/arch/platform/z1/dev/adxl345.c @@ -0,0 +1,411 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * Copyright (c) 2016, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \file + * Device drivers for adxl345 accelerometer in Zolertia Z1. + * \author + * Marcus Lundén, SICS + * Enric M. Calvo, Zolertia + * Antonio Lignan, Zolertia + */ +/*---------------------------------------------------------------------------*/ +#include +#include "contiki.h" +#include "adxl345.h" +#include "cc2420.h" +#include "i2cmaster.h" +#include "isr_compat.h" +#include "lib/sensors.h" +/*---------------------------------------------------------------------------*/ +#define DEBUG 0 +#if DEBUG +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif +/*---------------------------------------------------------------------------*/ +static uint8_t enabled; +/*---------------------------------------------------------------------------*/ +/* Callback pointers when interrupt occurs */ +void (*accm_int1_cb)(uint8_t reg); +void (*accm_int2_cb)(uint8_t reg); +/*---------------------------------------------------------------------------*/ +/* Bitmasks for the interrupts */ +static uint16_t int1_mask = 0, int2_mask = 0; + +/* Default values for adxl345 at startup. + * This will be sent to the adxl345 in a + * stream at init to set it up in a default state + */ + +static uint8_t adxl345_default_settings[] = { + /* Note, as the two first two bulks are to be written in a stream, they contain + * the register address as first byte in that section. + * 0--14 are in one stream, start at ADXL345_THRESH_TAP + */ + /* XXX NB Register address, not register value!! */ + ADXL345_THRESH_TAP, + ADXL345_THRESH_TAP_DEFAULT, + ADXL345_OFSX_DEFAULT, + ADXL345_OFSY_DEFAULT, + ADXL345_OFSZ_DEFAULT, + ADXL345_DUR_DEFAULT, + ADXL345_LATENT_DEFAULT, + ADXL345_WINDOW_DEFAULT, + ADXL345_THRESH_ACT_DEFAULT, + ADXL345_THRESH_INACT_DEFAULT, + ADXL345_TIME_INACT_DEFAULT, + ADXL345_ACT_INACT_CTL_DEFAULT, + ADXL345_THRESH_FF_DEFAULT, + ADXL345_TIME_FF_DEFAULT, + ADXL345_TAP_AXES_DEFAULT, + + /* 15--19 start at ADXL345_BW_RATE */ + /* XXX NB Register address, not register value!! */ + ADXL345_BW_RATE, + ADXL345_BW_RATE_DEFAULT, + ADXL345_POWER_CTL_DEFAULT, + ADXL345_INT_ENABLE_DEFAULT, + ADXL345_INT_MAP_DEFAULT, + + /* These two: 20, 21 write separately */ + ADXL345_DATA_FORMAT_DEFAULT, + ADXL345_FIFO_CTL_DEFAULT +}; +/*---------------------------------------------------------------------------*/ +PROCESS(accmeter_process, "Accelerometer process"); +/*---------------------------------------------------------------------------*/ +static void +accm_write_reg(uint8_t reg, uint8_t val) +{ + uint8_t tx_buf[] = {reg, val}; + + i2c_transmitinit(ADXL345_ADDR); + while (i2c_busy()); + PRINTF("ADXL345: I2C Ready to TX\n"); + + i2c_transmit_n(2, tx_buf); + while (i2c_busy()); + PRINTF("ADXL345: WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg); +} +/*---------------------------------------------------------------------------*/ +/* First byte in stream must be the register address to begin writing to. + * The data is then written from second byte and increasing. + */ +static void +accm_write_stream(uint8_t len, uint8_t *data) +{ + i2c_transmitinit(ADXL345_ADDR); + while (i2c_busy()); + PRINTF("ADXL345: I2C Ready to TX(stream)\n"); + + i2c_transmit_n(len, data); // start tx and send conf reg + while (i2c_busy()); + PRINTF("ADXL345: WRITE_STR %u B to 0x%02X\n", len, data[0]); +} + +/*---------------------------------------------------------------------------*/ +static uint8_t +accm_read_reg(uint8_t reg) +{ + uint8_t retVal = 0; + uint8_t rtx = reg; + PRINTF("ADXL345: READ_REG 0x%02X\n", reg); + + /* transmit the register to read */ + i2c_transmitinit(ADXL345_ADDR); + while (i2c_busy()); + i2c_transmit_n(1, &rtx); + while (i2c_busy()); + + /* receive the data */ + i2c_receiveinit(ADXL345_ADDR); + while (i2c_busy()); + i2c_receive_n(1, &retVal); + while (i2c_busy()); + + return retVal; +} +/*---------------------------------------------------------------------------*/ +static void +accm_read_stream(uint8_t reg, uint8_t len, uint8_t *whereto) +{ + uint8_t rtx = reg; + PRINTF("ADXL345: READ_STR %u B from 0x%02X\n", len, reg); + + /* transmit the register to start reading from */ + i2c_transmitinit(ADXL345_ADDR); + while (i2c_busy()); + i2c_transmit_n(1, &rtx); + while (i2c_busy()); + + /* receive the data */ + i2c_receiveinit(ADXL345_ADDR); + while (i2c_busy()); + i2c_receive_n(len, whereto); + while (i2c_busy()); +} + +/*---------------------------------------------------------------------------*/ +/* Read an axis of the accelerometer (x, y or z). Return value is a signed + * 10 bit int. + * The resolution of the acceleration measurement can be increased up to 13 bit, + * but will change the data format of this read out. Refer to the data sheet if + * so is wanted/needed. + */ +int16_t +accm_read_axis(enum ADXL345_AXIS axis) +{ + int16_t rd = 0; + uint8_t tmp[2]; + if(axis > Z_AXIS){ + return 0; + } + accm_read_stream(ADXL345_DATAX0 + axis, 2, &tmp[0]); + rd = (int16_t)(tmp[0] | (tmp[1]<<8)); + return rd; +} +/*---------------------------------------------------------------------------*/ +int +accm_set_grange(uint8_t grange) +{ + uint8_t tempreg = 0; + + if(grange > ADXL345_RANGE_16G) { + PRINTF("ADXL345: grange invalid: %u\n", grange); + return ADXL345_ERROR; + } + + if(!enabled) { + return ADXL345_ERROR; + } + + /* Keep the previous contents of the register, zero out the last two bits */ + tempreg = (accm_read_reg(ADXL345_DATA_FORMAT) & 0xFC); + tempreg |= grange; + accm_write_reg(ADXL345_DATA_FORMAT, tempreg); + return ADXL345_SUCCESS; +} + +/*---------------------------------------------------------------------------*/ +void +accm_init(void) +{ + PRINTF("ADXL345: init\n"); + accm_int1_cb = NULL; + accm_int2_cb = NULL; + + /* Set up ports and pins for interrups. */ + ADXL345_DIR &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN); + ADXL345_SEL &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN); + ADXL345_SEL2 &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN); + + /* Set up ports and pins for I2C communication */ + i2c_enable(); + + /* set default register values. */ + accm_write_stream(15, &adxl345_default_settings[0]); + accm_write_stream(5, &adxl345_default_settings[15]); + accm_write_reg(ADXL345_DATA_FORMAT, adxl345_default_settings[20]); + accm_write_reg(ADXL345_FIFO_CTL, adxl345_default_settings[21]); + + process_start(&accmeter_process, NULL); + + /* Enable msp430 interrupts on the two interrupt pins. */ + dint(); + /* low to high transition interrupts */ + ADXL345_IES &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN); + /* enable interrupts */ + ADXL345_IE |= (ADXL345_INT1_PIN | ADXL345_INT2_PIN); + eint(); + + enabled = 1; +} +/*---------------------------------------------------------------------------*/ +void +accm_stop(void) +{ + dint(); + ADXL345_IE &= ~(ADXL345_INT1_PIN | ADXL345_INT2_PIN); + accm_write_reg(ADXL345_INT_ENABLE, ~(int1_mask | int2_mask)); + accm_write_reg(ADXL345_INT_MAP, ~int2_mask); + eint(); + enabled = 0; +} +/*---------------------------------------------------------------------------*/ +int +accm_set_irq(uint8_t int1, uint8_t int2) +{ + if(!enabled) { + return ADXL345_ERROR; + } + + /* Set the corresponding interrupt mapping to INT1 or INT2 */ + PRINTF("ADXL345: IRQs set to INT1: 0x%02X IRQ2: 0x%02X\n", int1, int2); + + int1_mask = int1; + int2_mask = int2; + + accm_write_reg(ADXL345_INT_ENABLE, (int1 | int2)); + /* int1 bits are zeroes in the map register so this is for both ints */ + accm_write_reg(ADXL345_INT_MAP, int2); + return ADXL345_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +/* Invoked after an interrupt happened. Reads the interrupt source reg at the + * accelerometer, which resets the interrupts, and invokes the corresponding + * callback. It passes the source register value so the callback can determine + * what interrupt happened, if several interrupts are mapped to the same pin. + */ +static void +poll_handler(void) +{ + uint8_t ireg = 0; + ireg = accm_read_reg(ADXL345_INT_SOURCE); + + /* Invoke callbacks for the corresponding interrupts */ + if(ireg & int1_mask){ + if(accm_int1_cb != NULL){ + PRINTF("ADXL345: INT1 cb invoked\n"); + accm_int1_cb(ireg); + } + } else if(ireg & int2_mask){ + if(accm_int2_cb != NULL){ + PRINTF("ADXL345: INT2 cb invoked\n"); + accm_int2_cb(ireg); + } + } +} +/*---------------------------------------------------------------------------*/ +/* This process is sleeping until an interrupt from the accelerometer occurs, + * which polls this process from the interrupt service routine. */ +PROCESS_THREAD(accmeter_process, ev, data) +{ + PROCESS_POLLHANDLER(poll_handler()); + PROCESS_EXITHANDLER(); + PROCESS_BEGIN(); + while(1){ + PROCESS_WAIT_EVENT_UNTIL(0); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +/* This interrupt vector is shared with the interrupts from CC2420, so that + * was moved here + */ +static struct timer suppressTimer1, suppressTimer2; + +ISR(PORT1, port1_isr) +{ + /* ADXL345_IFG.x goes high when interrupt occurs, use to check what + * interrupted + */ + if((ADXL345_IFG & ADXL345_INT1_PIN) && !(ADXL345_IFG & BV(CC2420_FIFOP_PIN))){ + /* Check if this should be suppressed or not */ + if(timer_expired(&suppressTimer1)) { + timer_set(&suppressTimer1, SUPPRESS_TIME_INT1); + ADXL345_IFG &= ~ADXL345_INT1_PIN; // clear interrupt flag + process_poll(&accmeter_process); + LPM4_EXIT; + } + } else if((ADXL345_IFG & ADXL345_INT2_PIN) && + !(ADXL345_IFG & BV(CC2420_FIFOP_PIN))){ + /* Check if this should be suppressed or not */ + if(timer_expired(&suppressTimer2)) { + timer_set(&suppressTimer2, SUPPRESS_TIME_INT2); + /* clear interrupt flag */ + ADXL345_IFG &= ~ADXL345_INT2_PIN; + process_poll(&accmeter_process); + LPM4_EXIT; + } + } else { + /* CC2420 interrupt */ + if(cc2420_interrupt()) { + LPM4_EXIT; + } + } +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) +{ + if(type != SENSORS_ACTIVE) { + return ADXL345_ERROR; + } + + if(value) { + accm_init(); + } else { + accm_stop(); + } + enabled = value; + return ADXL345_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return enabled; + } + return ADXL345_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + if(!enabled) { + return ADXL345_ERROR; + } + + if((type != X_AXIS) && (type != Y_AXIS) && (type != Z_AXIS)) { + return ADXL345_ERROR; + } + + switch(type) { + case X_AXIS: + return accm_read_axis(X_AXIS); + case Y_AXIS: + return accm_read_axis(Y_AXIS); + case Z_AXIS: + return accm_read_axis(Z_AXIS); + default: + return ADXL345_ERROR; + } +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(adxl345, ADXL345_SENSOR, value, configure, status); +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/dev/adxl345.h b/arch/platform/z1/dev/adxl345.h new file mode 100644 index 000000000..a8d4789d8 --- /dev/null +++ b/arch/platform/z1/dev/adxl345.h @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * Copyright (c) 2016, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \file + * Device drivers header file for adxl345 accelerometer in Zolertia Z1. + * \author + * Marcus Lundén, SICS + * Enric Calvo, Zolertia + * Antonio Lignan, Zolertia + */ +/*---------------------------------------------------------------------------*/ +#ifndef ADXL345_H_ +#define ADXL345_H_ +#include +#include "dev/i2cmaster.h" +#include "lib/sensors.h" +/*---------------------------------------------------------------------------*/ +/* Used in accm_read_axis(), eg accm_read_axis(X_AXIS) */ +enum ADXL345_AXIS { + X_AXIS = 0, + Y_AXIS = 2, + Z_AXIS = 4, +}; +/* -------------------------------------------------------------------------- */ +/* Init the accelerometer: ports, pins, registers, interrupts (none enabled), + * I2C, default threshold values etc. + */ +void accm_init(void); + +/* Read an axis of the accelerometer (x, y or z). Return value is a signed 10 + * bit int. + * The resolution of the acceleration measurement can be increased up to 13 bit, + * but will change the data format of this read out. Refer to the data sheet if + * so is wanted/needed. + */ +int16_t accm_read_axis(enum ADXL345_AXIS axis); + +/* Sets the g-range, ie the range the accelerometer measures (ie 2g means -2 to + * +2 g on every axis). Possible values: + * - ADXL345_RANGE_2G + * - ADXL345_RANGE_4G + * - ADXL345_RANGE_8G + * - ADXL345_RANGE_16G + */ +int accm_set_grange(uint8_t grange); + +/* Map interrupt (FF, tap, dbltap etc) to interrupt pin (IRQ_INT1, IRQ_INT2). + * This must come after accm_init() as the registers will otherwise be + * overwritten. + */ +int accm_set_irq(uint8_t int1, uint8_t int2); + +/* Macros for setting the pointers to callback functions from the interrupts. + * The function will be called with an uint8_t as parameter, containing the + * interrupt flag register from the ADXL345. That way, several interrupts can be + * mapped to the same pin and be read + */ +#define ACCM_REGISTER_INT1_CB(ptr) accm_int1_cb = ptr; +#define ACCM_REGISTER_INT2_CB(ptr) accm_int2_cb = ptr; +/* -------------------------------------------------------------------------- */ +/* Application definitions, change if required by application. */ + +/* Time after an interrupt that subsequent interrupts are suppressed. Should + * later be turned into one specific time per type of interrupt (tap, freefall. + * etc) + */ +#define SUPPRESS_TIME_INT1 CLOCK_SECOND/4 +#define SUPPRESS_TIME_INT2 CLOCK_SECOND/4 + +/* Suggested defaults according to the data sheet etc */ +#define ADXL345_THRESH_TAP_DEFAULT 0x48 /* 4.5g (0x30 == 3.0g) */ +#define ADXL345_OFSX_DEFAULT 0x00 /* for calibration only */ +#define ADXL345_OFSY_DEFAULT 0x00 +#define ADXL345_OFSZ_DEFAULT 0x00 +#define ADXL345_DUR_DEFAULT 0x20 /* 20 ms (datasheet: 10ms++) */ +#define ADXL345_LATENT_DEFAULT 0x50 /* 100 ms (datasheet: 20ms++) */ +#define ADXL345_WINDOW_DEFAULT 0xFF /* 320 ms (datasheet: 80ms++) */ +#define ADXL345_THRESH_ACT_DEFAULT 0x15 /* 1.3g (62.5 mg/LSB) */ +#define ADXL345_THRESH_INACT_DEFAULT 0x08 /* 0.5g (62.5 mg/LSB) */ +#define ADXL345_TIME_INACT_DEFAULT 0x02 /* 2 s (1 s/LSB) */ +#define ADXL345_ACT_INACT_CTL_DEFAULT 0xFF /* all axis, ac-coupled */ +#define ADXL345_THRESH_FF_DEFAULT 0x09 /* 563 mg */ +#define ADXL345_TIME_FF_DEFAULT 0x20 /* 60 ms */ +#define ADXL345_TAP_AXES_DEFAULT 0x07 /* all axis, no suppression */ + +#define ADXL345_BW_RATE_DEFAULT (0x00 | ADXL345_SRATE_100) /* 100 Hz */ +/* link bit set, no autosleep, start normal measuring */ +#define ADXL345_POWER_CTL_DEFAULT 0x28 +#define ADXL345_INT_ENABLE_DEFAULT 0x00 /* no interrupts enabled */ +#define ADXL345_INT_MAP_DEFAULT 0x00 /* all mapped to int_1 */ + +/* XXX NB: In the data format register, data format of axis readings is chosen + * between left or right justify. This affects the position of the MSB/LSB and is + * different depending on g-range and resolution. If changed, make sure this is + * reflected in the _read_axis() function. Also, the resolution can be increased + * from 10 bit to at most 13 bit, but this also changes position of MSB etc on data + * format so check this in read_axis() too. + */ +/* right-justify, 2g, 10-bit mode, int is active high */ +#define ADXL345_DATA_FORMAT_DEFAULT (0x00 | ADXL345_RANGE_2G) +#define ADXL345_FIFO_CTL_DEFAULT 0x00 /* FIFO bypass mode */ + +/* -------------------------------------------------------------------------- */ +/* Reference definitions, should not be changed */ +/* adxl345 slave address */ +#define ADXL345_ADDR 0x53 + +/* ADXL345 registers */ +#define ADXL345_DEVID 0x00 +/* registers 0x01 to 0x1C are reserved, do not access */ +#define ADXL345_THRESH_TAP 0x1D +#define ADXL345_OFSX 0x1E +#define ADXL345_OFSY 0x1F +#define ADXL345_OFSZ 0x20 +#define ADXL345_DUR 0x21 +#define ADXL345_LATENT 0x22 +#define ADXL345_WINDOW 0x23 +#define ADXL345_THRESH_ACT 0x24 +#define ADXL345_THRESH_INACT 0x25 +#define ADXL345_TIME_INACT 0x26 +#define ADXL345_ACT_INACT_CTL 0x27 +#define ADXL345_THRESH_FF 0x28 +#define ADXL345_TIME_FF 0x29 +#define ADXL345_TAP_AXES 0x2A +#define ADXL345_ACT_TAP_STATUS 0x2B +#define ADXL345_BW_RATE 0x2C +#define ADXL345_POWER_CTL 0x2D +#define ADXL345_INT_ENABLE 0x2E +#define ADXL345_INT_MAP 0x2F +#define ADXL345_INT_SOURCE 0x30 +#define ADXL345_DATA_FORMAT 0x31 +#define ADXL345_DATAX0 0x32 /* read only, LSByte X, two's complement */ +#define ADXL345_DATAX1 0x33 /* read only, MSByte X */ +#define ADXL345_DATAY0 0x34 /* read only, LSByte Y */ +#define ADXL345_DATAY1 0x35 /* read only, MSByte X */ +#define ADXL345_DATAZ0 0x36 /* read only, LSByte Z */ +#define ADXL345_DATAZ1 0x37 /* read only, MSByte X */ +#define ADXL345_FIFO_CTL 0x38 +#define ADXL345_FIFO_STATUS 0x39 /* read only */ + +/* ADXL345 interrupts */ +#define ADXL345_INT_DISABLE 0X00 /* used for disabling interrupts */ +#define ADXL345_INT_OVERRUN 0X01 +#define ADXL345_INT_WATERMARK 0X02 +#define ADXL345_INT_FREEFALL 0X04 +#define ADXL345_INT_INACTIVITY 0X08 +#define ADXL345_INT_ACTIVITY 0X10 +#define ADXL345_INT_DOUBLETAP 0X20 +#define ADXL345_INT_TAP 0X40 +#define ADXL345_INT_DATAREADY 0X80 + +/* Accelerometer hardware ports, pins and registers on the msp430 µC */ +#define ADXL345_DIR P1DIR +#define ADXL345_PIN P1PIN +#define ADXL345_REN P1REN +#define ADXL345_SEL P1SEL +#define ADXL345_SEL2 P1SEL2 +#define ADXL345_INT1_PIN (1<<6) /* P1.6 */ +#define ADXL345_INT2_PIN (1<<7) /* P1.7 */ +#define ADXL345_IES P1IES +#define ADXL345_IE P1IE +#define ADXL345_IFG P1IFG +#define ADXL345_VECTOR PORT1_VECTOR + +/* g-range for DATA_FORMAT register */ +#define ADXL345_RANGE_2G 0x00 +#define ADXL345_RANGE_4G 0x01 +#define ADXL345_RANGE_8G 0x02 +#define ADXL345_RANGE_16G 0x03 + + +/* The adxl345 has programmable sample rates, but unexpected results may occur + * if the wrong rate and I2C bus speed is used (see datasheet p 17). Sample + * rates in Hz. This setting does not change the internal sampling rate, just + * how often it is piped to the output registers (ie the interrupt features use + * the full sample rate internally). + * Example use: + * adxl345_set_reg(ADXL345_BW_RATE, ((_ADXL345_STATUS & LOW_POWER) + * | ADXL345_SRATE_50)); + */ + +/* XXX NB don't use at all as I2C data rate<= 400kHz */ +#define ADXL345_SRATE_3200 0x0F +/* XXX NB don't use at all as I2C data rate<= 400kHz */ +#define ADXL345_SRATE_1600 0x0E +#define ADXL345_SRATE_800 0x0D /* when I2C data rate == 400 kHz */ +#define ADXL345_SRATE_400 0x0C /* when I2C data rate == 400 kHz */ +#define ADXL345_SRATE_200 0x0B /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_100 0x0A /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_50 0x09 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_25 0x08 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_12_5 0x07 /* 12.5 Hz, when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_6_25 0x06 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_3_13 0x05 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_1_56 0x04 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_0_78 0x03 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_0_39 0x02 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_0_20 0x01 /* when I2C data rate >= 100 kHz */ +#define ADXL345_SRATE_0_10 0x00 /* 0.10 Hz, when I2C data rate >= 100 kHz */ +/* -------------------------------------------------------------------------- */ +/* Callback pointers for the interrupts */ +extern void (*accm_int1_cb)(uint8_t reg); +extern void (*accm_int2_cb)(uint8_t reg); +/* -------------------------------------------------------------------------- */ +#define ACCM_INT1 0x01 +#define ACCM_INT2 0x02 +#define ADXL345_SUCCESS 0x00 +#define ADXL345_ERROR (-1) +/* -------------------------------------------------------------------------- */ +#define ADXL345_SENSOR "ADXL345 sensor" +/* -------------------------------------------------------------------------- */ +extern const struct sensors_sensor adxl345; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +#endif /* ifndef ADXL345_H_ */ diff --git a/arch/platform/z1/dev/battery-sensor.c b/arch/platform/z1/dev/battery-sensor.c new file mode 100644 index 000000000..dd787ccae --- /dev/null +++ b/arch/platform/z1/dev/battery-sensor.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * ----------------------------------------------------------------- + * + * Author : Adam Dunkels, Joakim Eriksson, Niclas Finne + * Created : 2005-11-01 + * Updated : $Date: 2010/08/25 19:30:52 $ + * $Revision: 1.11 $ + */ +#include "contiki.h" +#include "dev/battery-sensor.h" +#include "dev/sky-sensors.h" + +/* Configure ADC12_2 to sample channel 11 (voltage) and use */ +/* the Vref+ as reference (SREF_1) since it is a stable reference */ +#define INPUT_CHANNEL (1 << INCH_11) +#define INPUT_REFERENCE SREF_1 +#define BATTERY_MEM ADC12MEM11 + +const struct sensors_sensor battery_sensor; +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return BATTERY_MEM; +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + return sky_sensors_configure(INPUT_CHANNEL, INPUT_REFERENCE, type, c); +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + return sky_sensors_status(INPUT_CHANNEL, type); +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR, value, configure, status); diff --git a/tools/sky/uip6-bridge/dev/slip.h b/arch/platform/z1/dev/button-sensor.c similarity index 51% rename from tools/sky/uip6-bridge/dev/slip.h rename to arch/platform/z1/dev/button-sensor.c index d164ee97c..58479bf3b 100644 --- a/tools/sky/uip6-bridge/dev/slip.h +++ b/arch/platform/z1/dev/button-sensor.c @@ -1,4 +1,3 @@ -/* -*- C -*- */ /* * Copyright (c) 2005, Swedish Institute of Computer Science * All rights reserved. @@ -31,62 +30,72 @@ * */ -#ifndef SLIP_H_ -#define SLIP_H_ - #include "contiki.h" +#include "lib/sensors.h" +#include "dev/hwconf.h" +#include "dev/button-sensor.h" +#include "isr_compat.h" -PROCESS_NAME(slip_process); +const struct sensors_sensor button_sensor; -/** - * Send an IP packet from the uIP buffer with SLIP. - */ -uint8_t slip_send(void); +static struct timer debouncetimer; +static int status(int type); -/** - * Input a SLIP byte. - * - * This function is called by the RS232/SIO device driver to pass - * incoming bytes to the SLIP driver. The function can be called from - * an interrupt context. - * - * For systems using low-power CPU modes, the return value of the - * function can be used to determine if the CPU should be woken up or - * not. If the function returns non-zero, the CPU should be powered - * up. If the function returns zero, the CPU can continue to be - * powered down. - * - * \param c The data that is to be passed to the SLIP driver - * - * \return Non-zero if the CPU should be powered up, zero otherwise. - */ -int slip_input_byte(unsigned char c); +HWCONF_PIN(BUTTON, 2, 5); +HWCONF_IRQ(BUTTON, 2, 5); -uint8_t slip_write(const void *ptr, int len); +/*---------------------------------------------------------------------------*/ +ISR(PORT2, irq_p2) +{ + if(BUTTON_CHECK_IRQ()) { + if(timer_expired(&debouncetimer)) { + timer_set(&debouncetimer, CLOCK_SECOND / 4); + sensors_changed(&button_sensor); + LPM4_EXIT; + } + } + P2IFG = 0x00; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return BUTTON_READ() || !timer_expired(&debouncetimer); +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + switch (type) { + case SENSORS_ACTIVE: + if (c) { + if(!status(SENSORS_ACTIVE)) { + timer_set(&debouncetimer, 0); + BUTTON_IRQ_EDGE_SELECTD(); -/* Did we receive any bytes lately? */ -extern uint8_t slip_active; + BUTTON_SELECT(); + BUTTON_MAKE_INPUT(); -/* Statistics. */ -extern uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop; - -/** - * Set a function to be called when there is activity on the SLIP - * interface; used for detecting if a node is a gateway node. - */ -void slip_set_input_callback(void (*callback)(void)); - -/** - * Set a function to be called when a packet has been received. - * Default is tcpip_input(). - */ -void slip_set_tcpip_input_callback(void (*callback)(void)); - -/* - * These machine dependent functions and an interrupt service routine - * must be provided externally (slip_arch.c). - */ -void slip_arch_init(void); -void slip_arch_writeb(unsigned char c); - -#endif /* SLIP_H_ */ + BUTTON_ENABLE_IRQ(); + } + } else { + BUTTON_DISABLE_IRQ(); + } + return 1; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch (type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return BUTTON_IRQ_ENABLED(); + } + return 0; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, + value, configure, status); diff --git a/arch/platform/z1/dev/cc2420-arch.c b/arch/platform/z1/dev/cc2420-arch.c new file mode 100644 index 000000000..394c5c569 --- /dev/null +++ b/arch/platform/z1/dev/cc2420-arch.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include "contiki.h" +#include "contiki-net.h" +#include "dev/spi-legacy.h" +#include "cc2420.h" +#include "isr_compat.h" + +#ifdef CC2420_CONF_SFD_TIMESTAMPS +#define CONF_SFD_TIMESTAMPS CC2420_CONF_SFD_TIMESTAMPS +#endif /* CC2420_CONF_SFD_TIMESTAMPS */ + +#ifndef CONF_SFD_TIMESTAMPS +#define CONF_SFD_TIMESTAMPS 0 +#endif /* CONF_SFD_TIMESTAMPS */ + +#ifdef CONF_SFD_TIMESTAMPS +#include "cc2420-arch-sfd.h" +#endif /* CONF_SFD_TIMESTAMPS */ + +/*---------------------------------------------------------------------------*/ +#if 0 +/* this is now handled in the ADXL345 accelerometer code as it uses irq on port1 too. */ +ISR(CC2420_IRQ, cc24240_port1_interrupt) +{ + if(cc2420_interrupt()) { + LPM4_EXIT; + } +} +#endif +/*---------------------------------------------------------------------------*/ +void +cc2420_arch_init(void) +{ + spi_init(); + + /* all input by default, set these as output */ + CC2420_CSN_PORT(DIR) |= BV(CC2420_CSN_PIN); + CC2420_VREG_PORT(DIR) |= BV(CC2420_VREG_PIN); + CC2420_RESET_PORT(DIR) |= BV(CC2420_RESET_PIN); + +#if CONF_SFD_TIMESTAMPS + cc2420_arch_sfd_init(); +#endif /* CONF_SFD_TIMESTAMPS */ + + CC2420_SPI_DISABLE(); /* Unselect radio. */ +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/dev/i2cmaster.c b/arch/platform/z1/dev/i2cmaster.c new file mode 100644 index 000000000..59437b904 --- /dev/null +++ b/arch/platform/z1/dev/i2cmaster.c @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * I2C communication device drivers for Zolertia Z1 sensor node. + * \author + * Enric M. Calvo, Zolertia + * Marcus Lundén, SICS + */ + +#include "i2cmaster.h" +#include "isr_compat.h" + +signed char tx_byte_ctr, rx_byte_ctr; +unsigned char rx_buf[2]; +unsigned char *tx_buf_ptr; +unsigned char *rx_buf_ptr; +unsigned char receive_data; +unsigned char transmit_data1; +unsigned char transmit_data2; +unsigned char prescale_lsb = I2C_PRESC_Z1_LSB; +unsigned char prescale_msb = I2C_PRESC_Z1_MSB; +volatile unsigned int i; /* volatile to prevent optimization */ + +/* ------------------------------------------------------------------------------ + * Change the data rate prior initializing transmission or reception + * ----------------------------------------------------------------------------- */ +void +i2c_setrate(uint8_t p_lsb, uint8_t p_msb) +{ + prescale_lsb = p_lsb; + prescale_lsb = p_msb; +} +/* ------------------------------------------------------------------------------ + * This function initializes the USCI module for master-receive operation. + * ----------------------------------------------------------------------------- */ +void +i2c_receiveinit(uint8_t slave_address) +{ + UCB1CTL1 = UCSWRST; /* Enable SW reset */ + UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; /* I2C Master, synchronous mode */ + UCB1CTL1 = UCSSEL_2 | UCSWRST; /* Use SMCLK, keep SW reset */ + UCB1BR0 = prescale_lsb; /* prescaler (default 400 kHz) */ + UCB1BR1 = prescale_msb; + UCB1I2CSA = slave_address; /* set slave address */ + UCB1CTL1 &= ~UCTR; /* I2C Receiver */ + UCB1CTL1 &= ~UCSWRST; /* Clear SW reset, resume operation */ + UCB1I2CIE = UCNACKIE; +#if I2C_RX_WITH_INTERRUPT + UC1IE = UCB1RXIE; /* Enable RX interrupt if desired */ +#endif +} +/* ------------------------------------------------------------------------------ + * Initializes USCI for master-transmit operation. + * ------------------------------------------------------------------------------ */ +void +i2c_transmitinit(uint8_t slave_address) +{ + UCB1CTL1 |= UCSWRST; /* Enable SW reset */ + UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC); /* I2C Master, synchronous mode */ + UCB1CTL1 = UCSSEL_2 + UCSWRST; /* Use SMCLK, keep SW reset */ + UCB1BR0 = prescale_lsb; /* prescaler (default 400 kHz) */ + UCB1BR1 = prescale_msb; + UCB1I2CSA = slave_address; /* Set slave address */ + UCB1CTL1 &= ~UCSWRST; /* Clear SW reset, resume operation */ + UCB1I2CIE = UCNACKIE; + UC1IE = UCB1TXIE; /* Enable TX ready interrupt */ +} +/* ------------------------------------------------------------------------------ + * This function is used to start an I2C communication in master-receiver mode WITHOUT INTERRUPTS + * for more than 1 byte + * ------------------------------------------------------------------------------ */ +static volatile uint8_t rx_byte_tot = 0; +uint8_t +i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf) +{ + + rx_byte_tot = byte_ctr; + rx_byte_ctr = byte_ctr; + rx_buf_ptr = rx_buf; + + while((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG)) /* Slave acks address or not? */ + PRINTFDEBUG("____ UCTXSTT not clear OR NACK received\n"); + +#if I2C_RX_WITH_INTERRUPT + PRINTFDEBUG(" RX Interrupts: YES \n"); + + /* SPECIAL-CASE: Stop condition must be sent while receiving the 1st byte for 1-byte only read operations */ + if(rx_byte_tot == 1) { /* See page 537 of slau144e.pdf */ + dint(); + UCB1CTL1 |= UCTXSTT; /* I2C start condition */ + while(UCB1CTL1 & UCTXSTT) /* Waiting for Start bit to clear */ + PRINTFDEBUG("____ STT clear wait\n"); + UCB1CTL1 |= UCTXSTP; /* I2C stop condition */ + eint(); + } else { /* all other cases */ + UCB1CTL1 |= UCTXSTT; /* I2C start condition */ + } + return 0; + +#else + uint8_t n_received = 0; + + PRINTFDEBUG(" RX Interrupts: NO \n"); + + UCB1CTL1 |= UCTXSTT; /* I2C start condition */ + + while(rx_byte_ctr > 0) { + if(UC1IFG & UCB1RXIFG) { /* Waiting for Data */ + rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF; + rx_byte_ctr--; + UC1IFG &= ~UCB1RXIFG; /* Clear USCI_B1 RX int flag */ + n_received++; + } + } + UCB1CTL1 |= UCTXSTP; /* I2C stop condition */ + return n_received; +#endif +} +/* ------------------------------------------------------------------------------ + * This function is used to check if there is communication in progress. + * ------------------------------------------------------------------------------ */ +uint8_t +i2c_busy(void) +{ + return UCB1STAT & UCBBUSY; +} +/*---------------------------------------------------------------------------- + * Setup ports and pins for I2C use. + * ------------------------------------------------------------------------------ */ + +void +i2c_enable(void) +{ + I2C_PxSEL |= (I2C_SDA | I2C_SCL); /* Secondary function (USCI) selected */ + I2C_PxSEL2 |= (I2C_SDA | I2C_SCL); /* Secondary function (USCI) selected */ + I2C_PxDIR |= I2C_SCL; /* SCL is output (not needed?) */ + I2C_PxDIR &= ~I2C_SDA; /* SDA is input (not needed?) */ + I2C_PxREN |= (I2C_SDA | I2C_SCL); /* Activate internal pull-up/-down resistors */ + I2C_PxOUT |= (I2C_SDA | I2C_SCL); /* Select pull-up resistors */ +} +void +i2c_disable(void) +{ + I2C_PxSEL &= ~(I2C_SDA | I2C_SCL); /* GPIO function selected */ + I2C_PxSEL2 &= ~(I2C_SDA | I2C_SCL); /* GPIO function selected */ + I2C_PxREN &= ~(I2C_SDA | I2C_SCL); /* Deactivate internal pull-up/-down resistors */ + I2C_PxOUT &= ~(I2C_SDA | I2C_SCL); /* Select pull-up resistors */ +} +/* ------------------------------------------------------------------------------ + * This function is used to start an I2C communication in master-transmit mode. + * ------------------------------------------------------------------------------ */ +static volatile uint8_t tx_byte_tot = 0; +void +i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf) +{ + tx_byte_tot = byte_ctr; + tx_byte_ctr = byte_ctr; + tx_buf_ptr = tx_buf; + UCB1CTL1 |= UCTR + UCTXSTT; /* I2C TX, start condition */ +} +/*----------------------------------------------------------------------------*/ +ISR(USCIAB1TX, i2c_tx_interrupt) +{ + /* TX Part */ + if(UC1IFG & UCB1TXIFG) { /* TX int. condition */ + if(tx_byte_ctr == 0) { + UCB1CTL1 |= UCTXSTP; /* I2C stop condition */ + UC1IFG &= ~UCB1TXIFG; /* Clear USCI_B1 TX int flag */ + } else { + UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr]; + tx_byte_ctr--; + } + } + /* RX Part */ +#if I2C_RX_WITH_INTERRUPT + else if(UC1IFG & UCB1RXIFG) { /* RX int. condition */ + rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF; + rx_byte_ctr--; + if(rx_byte_ctr == 1) { /* stop condition should be set before receiving last byte */ + /* Only for 1-byte transmissions, STOP is handled in receive_n_int */ + if(rx_byte_tot != 1) { + UCB1CTL1 |= UCTXSTP; /* I2C stop condition */ + } + UC1IFG &= ~UCB1RXIFG; /* Clear USCI_B1 RX int flag. XXX Just in case, check if necessary */ + } + } +#endif +} + +ISR(USCIAB1RX, i2c_rx_interrupt) +{ + if(UCB1STAT & UCNACKIFG) { + PRINTFDEBUG("!!! NACK received in RX\n"); + UCB1CTL1 |= UCTXSTP; + UCB1STAT &= ~UCNACKIFG; + } +} diff --git a/arch/platform/z1/dev/i2cmaster.h b/arch/platform/z1/dev/i2cmaster.h new file mode 100644 index 000000000..cf9d96e6d --- /dev/null +++ b/arch/platform/z1/dev/i2cmaster.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * I2C communication device driver header file for Zolertia Z1 sensor node. + * \author + * Enric Calvo, Zolertia + * Marcus Lundén, SICS + */ + +#ifndef I2CMASTER_H_ +#define I2CMASTER_H_ + +#include "contiki.h" + +void i2c_enable(void); +void i2c_disable(void); + +void i2c_receiveinit(uint8_t slave_address); +uint8_t i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf); + +void i2c_transmitinit(uint8_t slave_address); +void i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf); + +uint8_t i2c_busy(void); +void i2c_setrate(uint8_t p_lsb, uint8_t p_msb); + +#if 0 +#include +#define PRINTFDEBUG(...) printf(__VA_ARGS__) +#else +#define PRINTFDEBUG(...) +#endif + +#endif /* #ifdef I2CMASTER_H_ */ diff --git a/arch/platform/z1/dev/light-sensor.c b/arch/platform/z1/dev/light-sensor.c new file mode 100644 index 000000000..ebc52d4db --- /dev/null +++ b/arch/platform/z1/dev/light-sensor.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2005-2010, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#include "contiki.h" +#include "lib/sensors.h" +#include "dev/sky-sensors.h" +#include "dev/light-sensor.h" +#include "dev/potentiometer-sensor.h" + +/* Photodiode 1 (P64) on INCH_4 */ +/* Photodiode 2 (P65) on INCH_5 */ +/* Enric#define INPUT_CHANNEL ((1 << INCH_4) | (1 << INCH_5)) */ +#ifndef INPUT_CHANNEL +#define INPUT_CHANNEL (1 << INCH_4) +#endif + +#ifndef INPUT_REFERENCE +#define INPUT_REFERENCE SREF_0 +#endif + +#ifdef POTENTIOMETER_MEM +#define PHOTOSYNTHETIC_MEM POTENTIOMETER_MEM +#define TOTAL_SOLAR_MEM POTENTIOMETER_MEM +#else +#define PHOTOSYNTHETIC_MEM ADC12MEM4 +#define TOTAL_SOLAR_MEM ADC12MEM4 +#endif + + +const struct sensors_sensor light_sensor; + +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + switch(type) { + /* Photosynthetically Active Radiation. */ + case LIGHT_SENSOR_PHOTOSYNTHETIC: + return PHOTOSYNTHETIC_MEM; + + /* Total Solar Radiation. */ + case LIGHT_SENSOR_TOTAL_SOLAR: + return TOTAL_SOLAR_MEM; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + return sky_sensors_status(INPUT_CHANNEL, type); +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + return sky_sensors_configure(INPUT_CHANNEL, INPUT_REFERENCE, type, c); +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(light_sensor, "Light", value, configure, status); diff --git a/arch/platform/z1/dev/light-sensor.h b/arch/platform/z1/dev/light-sensor.h new file mode 100644 index 000000000..411c6fa33 --- /dev/null +++ b/arch/platform/z1/dev/light-sensor.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * + * ----------------------------------------------------------------- + * + * Author : Adam Dunkels, Joakim Eriksson, Niclas Finne + * Created : 2010-01-08 + * Updated : $Date: 2010/01/14 20:23:02 $ + * $Revision: 1.2 $ + */ + +#ifndef LIGHT_SENSOR_H_ +#define LIGHT_SENSOR_H_ + +#include "lib/sensors.h" + +extern const struct sensors_sensor light_sensor; + +#define LIGHT_SENSOR_PHOTOSYNTHETIC 0 +#define LIGHT_SENSOR_TOTAL_SOLAR 1 + + +#endif /* LIGHT-SENSOR_H_ */ diff --git a/arch/platform/z1/dev/light-ziglet.c b/arch/platform/z1/dev/light-ziglet.c new file mode 100644 index 000000000..3f322e30a --- /dev/null +++ b/arch/platform/z1/dev/light-ziglet.c @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Device drivers for light ziglet sensor in Zolertia Z1. + * It is recommended to use with a 100KHz data rate + * \author + * Antonio Lignan, Zolertia + * Marcus Lundén, SICS + */ + +#include +#include "contiki.h" +#include "i2cmaster.h" +#include "light-ziglet.h" + +#if 0 +#define PRINTFDEBUG(...) printf(__VA_ARGS__) +#else +#define PRINTFDEBUG(...) +#endif + +/* Bitmasks and bit flag variable for keeping track of tmp102 status. */ +enum TSL2563_STATUSTYPES { + /* must be a bit and not more, not using 0x00. */ + INITED = 0x01, + RUNNING = 0x02, + STOPPED = 0x04, +}; + +static enum TSL2563_STATUSTYPES _TSL2563_STATUS = 0x00; + +uint16_t +calculateLux(uint16_t *buffer) +{ + uint32_t ch0, ch1 = 0; + uint32_t aux = (1 << 14); + uint32_t ratio, lratio, tmp = 0; + + ch0 = (buffer[0] * aux) >> 10; + ch1 = (buffer[1] * aux) >> 10; + + PRINTFDEBUG("B0 %u, B1 %u\n", buffer[0], buffer[1]); + PRINTFDEBUG("ch0 %lu, ch1 %lu\n", ch0, ch1); + + ratio = (ch1 << 10); + ratio = ratio / ch0; + lratio = (ratio + 1) >> 1; + + PRINTFDEBUG("ratio %lu, lratio %lu\n", ratio, lratio); + + if((lratio >= 0) && (lratio <= K1T)) { + tmp = (ch0 * B1T) - (ch1 * M1T); + } else if(lratio <= K2T) { + tmp = (ch0 * B2T) - (ch1 * M2T); + } else if(lratio <= K3T) { + tmp = (ch0 * B3T) - (ch1 * M3T); + } else if(lratio <= K4T) { + tmp = (ch0 * B4T) - (ch1 * M4T); + } else if(lratio <= K5T) { + tmp = (ch0 * B5T) - (ch1 * M5T); + } else if(lratio <= K6T) { + tmp = (ch0 * B6T) - (ch1 * M6T); + } else if(lratio <= K7T) { + tmp = (ch0 * B7T) - (ch1 * M7T); + } else if(lratio > K8T) { + tmp = (ch0 * B8T) - (ch1 * M8T); + } + + if(tmp < 0) { + tmp = 0; + } + + tmp += (1 << 13); + + PRINTFDEBUG("tmp %lu\n", tmp); + + return tmp >> 14; +} +/*---------------------------------------------------------------------------*/ +/* Init the light ziglet sensor: ports, pins, registers, interrupts (none enabled), I2C, + default threshold values etc. */ + +void +light_ziglet_init(void) +{ + if(!(_TSL2563_STATUS & INITED)) { + PRINTFDEBUG("light ziglet init\n"); + _TSL2563_STATUS |= INITED; + + /* Set up ports and pins for I2C communication */ + i2c_enable(); + return; + } +} +/*---------------------------------------------------------------------------*/ +/* Write to a 16-bit register. + args: + reg register to write to + val value to write + */ + +void +tsl2563_write_reg(uint8_t reg, uint16_t val) +{ + uint8_t tx_buf[] = { reg, 0x00, 0x00 }; + + tx_buf[1] = (uint8_t)(val >> 8); + tx_buf[2] = (uint8_t)(val & 0x00FF); + + i2c_transmitinit(TSL2563_ADDR); + while(i2c_busy()); + PRINTFDEBUG("I2C Ready to TX\n"); + + i2c_transmit_n(3, tx_buf); + while(i2c_busy()); + PRINTFDEBUG("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg); +} +/*---------------------------------------------------------------------------*/ +/* Read register. + args: + reg what register to read + returns the value of the read register type uint16_t + */ + +uint16_t +tsl2563_read_reg(uint8_t reg) +{ + uint16_t readBuf[] = { 0x00, 0x00 }; + uint8_t buf[] = { 0x00, 0x00, 0x00, 0x00 }; + uint16_t retVal = 0; + uint8_t rtx = reg; + + /* Transmit the register to read */ + i2c_transmitinit(TSL2563_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, &rtx); + while(i2c_busy()); + + /* Receive the data */ + i2c_receiveinit(TSL2563_ADDR); + while(i2c_busy()); + i2c_receive_n(4, buf); + while(i2c_busy()); + + PRINTFDEBUG("\nb0 %u, b1 %u, b2 %u, b3 %u\n", buf[0], buf[1], buf[2], buf[3]); + + readBuf[0] = (buf[1] << 8 | (buf[0])); + readBuf[1] = (buf[3] << 8 | (buf[2])); + + retVal = calculateLux(readBuf); + return retVal; +} +uint16_t +light_ziglet_on(void) +{ + uint16_t data; + uint8_t regon = TSL2563_PWRN; + /* Turn on the sensor */ + i2c_transmitinit(TSL2563_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, ®on); + while(i2c_busy()); + data = (uint16_t)tsl2563_read_reg(TSL2563_READ); + return data; +} +void +light_ziglet_off(void) +{ + uint8_t regoff = 0x00; + /* Turn off the sensor */ + i2c_transmitinit(TSL2563_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, ®off); + while(i2c_busy()); + return; +} +/*---------------------------------------------------------------------------*/ +/* Read light ziglet sensor + */ + +uint16_t +light_ziglet_read(void) +{ + uint16_t lux = 0; + lux = light_ziglet_on(); + light_ziglet_off(); + return lux; +} diff --git a/arch/platform/z1/dev/light-ziglet.h b/arch/platform/z1/dev/light-ziglet.h new file mode 100644 index 000000000..4db18e24e --- /dev/null +++ b/arch/platform/z1/dev/light-ziglet.h @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Device drivers header file for light ziglet sensor in Zolertia Z1 WSN Platform. + * \author + * Antonio Lignan, Zolertia + * Marcus Lundén, SICS + */ + +#ifndef LIGHT_ZIGLET_H_ +#define LIGHT_ZIGLET_H_ +#include +#include "i2cmaster.h" + +/* Init the light ziglet sensor: ports, pins, I2C, interrupts */ +void light_ziglet_init(void); + +/* Write to a register. + args: + reg register to write to + val value to write + */ +void tsl2563_write_reg(uint8_t reg, uint16_t val); + +/* Read one register. + args: + reg what register to read + returns the value of the read register + */ +uint16_t tsl2563_read_reg(uint8_t reg); + +/* Takes a single light reading + args: none + returns a lux value + */ +uint16_t light_ziglet_read(); + +/* Calculates the lux values from the calibration table + args: raw values from sensor + returns a lux value + */ +uint16_t calculateLux(uint16_t *readRaw); + +/* Turns the light ziglet ON and polls the sensor for a light reading */ +uint16_t light_ziglet_on(void); + +/* -------------------------------------------------------------------------- */ +/* Reference definitions */ + +/* TSL2563 slave address */ +#define TSL2563_ADDR 0x39 + +/* Registers */ +#define TSL2563_READ 0xAC +#define TSL2563_PWRN 0x03 + +/* Calibration settings */ +#define K1T 0X0040 +#define B1T 0x01f2 +#define M1T 0x01b2 + +#define K2T 0x0080 +#define B2T 0x0214 +#define M2T 0x02d1 + +#define K3T 0x00c0 +#define B3T 0x023f +#define M3T 0x037b + +#define K4T 0x0100 +#define B4T 0x0270 +#define M4T 0x03fe + +#define K5T 0x0138 +#define B5T 0x016f +#define M5T 0x01fc + +#define K6T 0x019a +#define B6T 0x00d2 +#define M6T 0x00fb + +#define K7T 0x029a +#define B7T 0x0018 +#define M7T 0x0012 + +#define K8T 0x029a +#define B8T 0x0000 +#define M8T 0x0000 + +/* -------------------------------------------------------------------------- */ +#endif /* ifndef LIGHT_ZIGLET_H_ */ + diff --git a/arch/platform/z1/dev/potentiometer-sensor.c b/arch/platform/z1/dev/potentiometer-sensor.c new file mode 100644 index 000000000..12597bd0e --- /dev/null +++ b/arch/platform/z1/dev/potentiometer-sensor.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2011 Zolertia(TM) is a trademark by Advancare,SL + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * ----------------------------------------------------------------- + * + * Author : Enric M. Calvo (based on work by A. Dunkels, J. Eriksson, N. Finne) + * Created : 2011-02-22 + * $Revision: 1.0 $ + */ + +#include "dev/potentiometer-sensor.h" +#include "dev/sky-sensors.h" +#include "contiki.h" + +/* Configure ADC12_2 to sample channel 11 (voltage) and use */ +/* the Vref+ as reference (SREF_1) since it is a stable reference */ +#define INPUT_CHANNEL (1 << INCH_4) +#define INPUT_REFERENCE SREF_0 +#define POTENTIOMETER_MEM ADC12MEM4 + +const struct sensors_sensor potentiometer_sensor; +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return POTENTIOMETER_MEM; +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + return sky_sensors_configure(INPUT_CHANNEL, INPUT_REFERENCE, type, c); +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + return sky_sensors_status(INPUT_CHANNEL, type); +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(potentiometer_sensor, POTENTIOMETER_SENSOR, value, configure, status); diff --git a/arch/platform/z1/dev/potentiometer-sensor.h b/arch/platform/z1/dev/potentiometer-sensor.h new file mode 100644 index 000000000..d9d8b983d --- /dev/null +++ b/arch/platform/z1/dev/potentiometer-sensor.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2011 Zolertia(TM) is a trademark by Advancare,SL + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * ----------------------------------------------------------------- + * + * Author : Enric M. Calvo (based on work by A. Dunkels, J. Eriksson, N. Finne) + * Created : 2011-02-22 + * $Revision: 1.0 $ + */ + +#ifndef POTENTIOMETER_SENSOR_H_ +#define POTENTIOMETER_SENSOR_H_ + +#include "lib/sensors.h" + +extern const struct sensors_sensor potentiometer_sensor; + +#define POTENTIOMETER_SENSOR "Potentiometer" + +#endif /* POTENTIOMETER_SENSOR_H_ */ diff --git a/arch/platform/z1/dev/reed-sensor.c b/arch/platform/z1/dev/reed-sensor.c new file mode 100644 index 000000000..7e7a0df6d --- /dev/null +++ b/arch/platform/z1/dev/reed-sensor.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2015, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/** + * \file + * Reed sensor driver file + * \author + * Antonio Lignan + */ + +#include "contiki.h" +#include "lib/sensors.h" +#include "dev/reed-sensor.h" +#include "sys/process.h" +#include "sys/ctimer.h" +/*---------------------------------------------------------------------------*/ +#ifndef REED_CHECK_PERIOD +#define REED_CHECK_PERIOD CLOCK_SECOND +#endif +/*---------------------------------------------------------------------------*/ +static int current_status = -1; +static struct ctimer change_timer; +process_event_t reed_sensor_event_changed; +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return ~(REED_PORT_DIR & REED_READ_PIN); + } + return REED_SENSOR_ERROR; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + if((!status(SENSORS_ACTIVE)) || (type != REED_SENSOR_VAL)) { + return REED_SENSOR_ERROR; + } + return (REED_PORT_READ & REED_READ_PIN) ? REED_CLOSED : REED_OPEN; +} +/*---------------------------------------------------------------------------*/ +static void +check_callback(void *data) +{ + static int new_status; + if(current_status == -1) { + ctimer_stop(&change_timer); + return; + } + + new_status = value(REED_SENSOR_VAL); + if(new_status != current_status) { + current_status = new_status; + process_post(PROCESS_BROADCAST, reed_sensor_event_changed, ¤t_status); + } + ctimer_reset(&change_timer); +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + switch(type) { + case SENSORS_ACTIVE: + if(c) { + if(!status(SENSORS_ACTIVE)) { + REED_PORT_SEL |= REED_READ_PIN; + REED_PORT_DIR &= ~REED_READ_PIN; + REED_PORT_REN |= REED_READ_PIN; + REED_PORT_PRES |= REED_READ_PIN; + } + } else { + REED_PORT_DIR |= REED_READ_PIN; + REED_PORT_REN &= ~REED_READ_PIN; + } + return REED_SENSOR_SUCCESS; + case REED_SENSOR_MODE: + if(c == REED_SENSOR_EVENT_MODE) { + current_status = value(REED_SENSOR_VAL); + ctimer_set(&change_timer, REED_CHECK_PERIOD, check_callback, NULL); + } else if(c == REED_SENSOR_EVENT_POLL) { + current_status = -1; + ctimer_stop(&change_timer); + } else { + return REED_SENSOR_ERROR; + } + return REED_SENSOR_SUCCESS; + } + return REED_SENSOR_ERROR; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(reed_sensor, REED_SENSOR, value, configure, status); diff --git a/arch/platform/z1/dev/reed-sensor.h b/arch/platform/z1/dev/reed-sensor.h new file mode 100644 index 000000000..7e86716f5 --- /dev/null +++ b/arch/platform/z1/dev/reed-sensor.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2015, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/** + * Header file for the reed sensor + * + * The Reed sensor allows to be used either by polling the sensor status or by + * setting up a timer on the background ticking every REED_CHECK_PERIOD, posting + * a reed_sensor_event_changed event, informing the application about a change + * in the sensor status (basically open or closed). To enable each mode + * (default is polling) call the configure() function with REED_SENSOR_MODE + * using REED_SENSOR_EVENT_MODE or REED_SENSOR_POLL_MODE, after having + * initialized the device using SENSORS_ACTIVATE(reed_sensor). + * + * \file + * Reed sensor header file + * \author + * Antonio Lignan + */ +#include "lib/sensors.h" + +#ifndef REED_SENSOR_H_ +#define REED_SENSOR_H_ +/* -------------------------------------------------------------------------- */ +#define REED_SENSOR_ERROR -1 +#define REED_SENSOR_SUCCESS 0x00 +#define REED_SENSOR_VAL 0x01 +/* -------------------------------------------------------------------------- */ +#define REED_OPEN 0x00 +#define REED_CLOSED 0x01 +/* -------------------------------------------------------------------------- */ +#define REED_SENSOR_MODE 0x01 +#define REED_SENSOR_EVENT_MODE 0x0A +#define REED_SENSOR_EVENT_POLL 0x0B +/* -------------------------------------------------------------------------- */ +#define REED_PORT_DIR P4DIR +#define REED_PORT_SEL P4SEL +#define REED_PORT_REN P4REN +#define REED_PORT_READ P4IN +#define REED_PORT_PRES P4OUT +#define REED_READ_PIN (1 << 2) +/* -------------------------------------------------------------------------- */ +#define REED_SENSOR "Reed Sensor" +/* -------------------------------------------------------------------------- */ +extern const struct sensors_sensor reed_sensor; +extern process_event_t reed_sensor_event_changed; +/* -------------------------------------------------------------------------- */ +#endif /* ifndef REED_SENSOR_H_ */ diff --git a/arch/platform/z1/dev/relay-phidget.c b/arch/platform/z1/dev/relay-phidget.c new file mode 100644 index 000000000..d78734b21 --- /dev/null +++ b/arch/platform/z1/dev/relay-phidget.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * + * ----------------------------------------------------------------- + * + * \file + * Device simple driver for generic relay in phidget port of Zolertia Z1 + * \author + * Antonio Lignan, Zolertia + * + */ + +#include "contiki.h" +#include "relay-phidget.h" + +static uint8_t controlPin; + +enum PHIDGET_RELAY_STATUSTYPES { + /* must be a bit and not more, not using 0x00. */ + INITED = 0x01, + RUNNING = 0x02, + STOPPED = 0x04, +}; + +static enum PHIDGET_RELAY_STATUSTYPES _RELAY_STATUS = 0x00; + +/*---------------------------------------------------------------------------*/ + +void +relay_enable(uint8_t pin) +{ + + if(!(_RELAY_STATUS & INITED)) { + + _RELAY_STATUS |= INITED; + + /* Selects the pin to be configure as the control pin of the relay module */ + controlPin = (1 << pin); + + /* Configures the control pin */ + P6SEL &= ~controlPin; + P6DIR |= controlPin; + } +} +/*---------------------------------------------------------------------------*/ + +void +relay_on() +{ + if((_RELAY_STATUS & INITED)) { + P6OUT |= controlPin; + } +} +/*---------------------------------------------------------------------------*/ +void +relay_off() +{ + if((_RELAY_STATUS & INITED)) { + P6OUT &= ~controlPin; + } +} +/*---------------------------------------------------------------------------*/ + +int8_t +relay_toggle() +{ + if((_RELAY_STATUS & INITED)) { + P6OUT ^= controlPin; + if((P6OUT & controlPin)) { + return 1; + } + return 0; + } + return -1; +} +/*---------------------------------------------------------------------------*/ + diff --git a/arch/platform/z1/dev/relay-phidget.h b/arch/platform/z1/dev/relay-phidget.h new file mode 100644 index 000000000..580151f58 --- /dev/null +++ b/arch/platform/z1/dev/relay-phidget.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * ----------------------------------------------------------------- + * + * \file + * Device simple driver for generic relay in phidget port of Zolertia Z1 + * \author + * Antonio Lignan, Zolertia + * + */ + +#ifndef RELAY_PHIDGET_H_ +#define RELAY_PHIDGETS_H_ + +void relay_enable(uint8_t pin); + +void relay_on(); +void relay_off(); +int8_t relay_toggle(); + +#endif /* RELAY_PHIDGET_H_ */ diff --git a/arch/platform/z1/dev/sht11-arch.h b/arch/platform/z1/dev/sht11-arch.h new file mode 100644 index 000000000..1cd1bc8e6 --- /dev/null +++ b/arch/platform/z1/dev/sht11-arch.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2011, Zolertia(TM) is a trademark of Advancare,SL + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Architecture-specific definitions for the SHT11 sensor on Zolertia Z1 + * when connected to the Ziglet port. + * \author + * Enric M. Calvo , adapted from work by + * Nicolas Tsiftes + */ + +#ifndef SHT11_ARCH_H +#define SHT11_ARCH_H + +/* Architecture-specific definitions for the SHT11 sensor on Zolertia Z1 + * when connected to the Ziglet port. + * CAUTION: I2C needs to be disabled to use the bitbang protocol of SHT11 + */ + +#define SHT11_ARCH_SDA 1 /* P5.1 */ +#define SHT11_ARCH_SCL 2 /* P5.2 */ +/* SHT11_ARCH_PWR is not needed, but until and *-arch abstraction exists, this + * should fix it + */ +#define SHT11_ARCH_PWR 3 /* P5.3 -- unused pin */ + +#define SHT11_PxDIR P5DIR +#define SHT11_PxIN P5IN +#define SHT11_PxOUT P5OUT +#define SHT11_PxSEL P5SEL +#define SHT11_PxREN P5REN + +#endif diff --git a/tools/sky/uip6-bridge/uip6-bridge-tap.c b/arch/platform/z1/dev/sht25.c similarity index 50% rename from tools/sky/uip6-bridge/uip6-bridge-tap.c rename to arch/platform/z1/dev/sht25.c index 694499b6d..b8356087b 100644 --- a/tools/sky/uip6-bridge/uip6-bridge-tap.c +++ b/arch/platform/z1/dev/sht25.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, Swedish Institute of Computer Science. + * Copyright (c) 2015, Zolertia * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,127 +26,109 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * This file is part of the Contiki operating system. * */ - /** * \file - * A brief description of what this file is + * SHT25 temperature and humidity sensor driver * \author - * Niclas Finne - * Joakim Eriksson + * Antonio Lignan */ - -#include "contiki.h" -#include "net/ipv6/uip.h" -#include "dev/slip.h" -#include "dev/leds.h" -#include "sicslow_ethernet.h" - -#include "net/packetbuf.h" - #include -#include - -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - -PROCESS(uip6_bridge, "IPv6/6lowpan TAP bridge"); -PROCESS(tcpip_process, "tcpip dummy"); -AUTOSTART_PROCESSES(&uip6_bridge); - +#include "contiki.h" +#include "i2cmaster.h" +#include "dev/sht25.h" +#include "lib/sensors.h" /*---------------------------------------------------------------------------*/ -static uint8_t (* outputfunc)(const uip_lladdr_t *a); -uint8_t -tcpip_output(const uip_lladdr_t *a) +static uint8_t enabled; +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) { - if(outputfunc != NULL) { - outputfunc(a); - leds_toggle(LEDS_GREEN); + if(type != SENSORS_ACTIVE) { + return SHT25_ERROR; + } + if(value) { + i2c_enable(); + } else { + i2c_disable(); + } + enabled = value; + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return enabled; } return 0; } -void -tcpip_ipv6_output(void) +/*---------------------------------------------------------------------------*/ +static uint16_t +sht25_read_reg(uint8_t reg) { -} -void -tcpip_set_outputfunc(uint8_t (*f)(const uip_lladdr_t *)) -{ - outputfunc = f; -} -PROCESS_THREAD(tcpip_process, ev, data) -{ - PROCESS_BEGIN(); - PROCESS_END(); + uint8_t buf[] = { 0x00, 0x00 }; + uint16_t retval; + uint8_t rtx = reg; + + /* transmit the register to read */ + i2c_transmitinit(SHT25_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, &rtx); + while(i2c_busy()); + /* receive the data */ + i2c_receiveinit(SHT25_ADDR); + while(i2c_busy()); + i2c_receive_n(2, &buf[0]); + while(i2c_busy()); + + retval = (uint16_t)(buf[0] << 8 | (buf[1])); + return retval; } /*---------------------------------------------------------------------------*/ -/* Packet from SICSLoWPAN */ -void -tcpip_input(void) +static int16_t +sht25_convert(uint8_t variable, uint16_t value) { - if(uip_len > 0) { - mac_LowpanToEthernet(); - if(uip_len > 0) { - slip_write(uip_buf, uip_len); - leds_toggle(LEDS_RED); - uip_clear_buf(); - } + int16_t rd; + uint32_t buff; + buff = (uint32_t)value; + if(variable == SHT25_VAL_TEMP) { + buff *= 17572; + buff = buff >> 16; + rd = (int16_t)buff - 4685; + } else { + buff *= 12500; + buff = buff >> 16; + rd = (int16_t)buff - 600; + rd = (rd > 10000) ? 10000 : rd; } + return rd; } /*---------------------------------------------------------------------------*/ -/* Packet from SLIP */ -static void -slip_tcpip_input(void) +static int16_t +sht25_read(uint8_t variable) { - /* TODO Should fix this in slip configuration */ - memmove(uip_buf, &uip_buf[UIP_LLH_LEN], uip_len); - mac_ethernetToLowpan(uip_buf); -} -/*---------------------------------------------------------------------------*/ -static void -slip_activity(void) -{ - leds_toggle(LEDS_BLUE); -} -/*---------------------------------------------------------------------------*/ -PROCESS_THREAD(uip6_bridge, ev, data) -{ - PROCESS_BEGIN(); + int16_t rd; + uint16_t raw; - printf("Setting up SLIP\n"); - - mac_ethernetSetup(); - - slip_arch_init(); - slip_set_input_callback(slip_activity); - slip_set_tcpip_input_callback(slip_tcpip_input); - process_start(&slip_process, NULL); - - PROCESS_END(); -} -/*---------------------------------------------------------------------------*/ -int -putchar(int c) -{ -#define SLIP_END 0300 - static char debug_frame = 0; - - if(!debug_frame) { /* Start of debug output */ - slip_arch_writeb(SLIP_END); - slip_arch_writeb('\r'); /* Type debug line == '\r' */ - debug_frame = 1; + if((variable != SHT25_VAL_TEMP) && (variable != SHT25_VAL_HUM)) { + return SHT25_ERROR; } - - slip_arch_writeb((char)c); - - /* - * Line buffered output, a newline marks the end of debug output and - * implicitly flushes debug output. - */ - if(c == '\n') { - slip_arch_writeb(SLIP_END); - debug_frame = 0; - } - return c; + raw = sht25_read_reg(variable); + rd = sht25_convert(variable, raw); + return rd; } /*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return sht25_read(type); +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(sht25, SHT25_SENSOR, value, configure, status); +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/dev/sht25.h b/arch/platform/z1/dev/sht25.h new file mode 100644 index 000000000..65d9399d3 --- /dev/null +++ b/arch/platform/z1/dev/sht25.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2015, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/** + * \file + * SHT25 temperature and humidity sensor driver + * \author + * Antonio Lignan + */ +#include "lib/sensors.h" + +#ifndef SHT25_H_ +#define SHT25_H_ + +/* -------------------------------------------------------------------------- */ +#define SHT25_ADDR 0x40 +#define SHT25_TEMP_HOLD 0xE3 +#define SHT25_HUM_HOLD 0xE5 +#define SHT25_TEMP_NO_HOLD 0xF3 +#define SHT25_HUM_NO_HOLD 0xF5 +#define SHT2X_UREG_WRITE 0xE6 +#define SHT2X_UREG_READ 0xE7 +#define SHT2X_SOFT_RESET 0XFE +#define SHT2X_NULL 0x00 +/* -------------------------------------------------------------------------- */ +#define SHT2X_RES_14T_12RH 0x00 +#define SHT2X_RES_12T_08RH 0x01 +#define SHT2X_RES_13T_10RH 0x80 +#define SHT2X_RES_11T_11RH 0x81 +#define SHT2X_HEATER_ON 0x04 +#define SHT2X_HEATER_OFF 0x00 +#define SHT2X_OTP_RELOAD_EN 0x00 +#define SHT2X_OTP_RELOAD_DIS 0x02 +/* -------------------------------------------------------------------------- */ +#define SHT25_VAL_TEMP SHT25_TEMP_HOLD +#define SHT25_VAL_HUM SHT25_HUM_HOLD +#define SHT25_ERROR -1 +/* -------------------------------------------------------------------------- */ +#define SHT25_SENSOR "SHT25 Sensor" +/* -------------------------------------------------------------------------- */ +extern const struct sensors_sensor sht25; +/* -------------------------------------------------------------------------- */ +#endif /* ifndef SHT25_H_ */ diff --git a/arch/platform/z1/dev/sky-sensors.c b/arch/platform/z1/dev/sky-sensors.c new file mode 100644 index 000000000..48ac22428 --- /dev/null +++ b/arch/platform/z1/dev/sky-sensors.c @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * + * ----------------------------------------------------------------- + * + * Author : Joakim Eriksson + * Created : 2010-02-02 + * Updated : $Date: 2010/08/25 19:30:53 $ + * $Revision: 1.3 $ + */ +#include "contiki.h" +#include "lib/sensors.h" + +#define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno] + +static uint16_t adc_on; +static uint16_t ready; +/*---------------------------------------------------------------------------*/ +static CC_INLINE void +start(void) +{ + uint16_t c, last; + + /* Set up the ADC. */ + P6DIR = 0xff; + P6OUT = 0x00; + + /* Setup ADC12, ref., sampling time */ + /* XXX Note according to the specification a minimum of 17 ms should + be allowed after turn on of the internal reference generator. */ + ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC + REFON; + /* Use sampling timer, repeat-sequence-of-channels */ + ADC12CTL1 = SHP + CONSEQ_3; + + last = 15; + for(c = 0; c < 16; c++) { + /* Clear all end-of-sequences */ + ADC12MCTL_NO(c) &= ~EOS; + if(adc_on & (1 << c)) { + if(last == 15) { + /* Set new start of sequence to lowest active memory holder */ + ADC12CTL1 |= (c * CSTARTADD_1); + } + last = c; + } + } + + /* Set highest end-of-sequence. */ + ADC12MCTL_NO(last) |= EOS; + + ADC12CTL0 |= ADC12ON; + ADC12CTL0 |= ENC; /* enable conversion */ + ADC12CTL0 |= ADC12SC; /* sample & convert */ +} +/*---------------------------------------------------------------------------*/ +static CC_INLINE void +stop(void) +{ + /* stop converting immediately, turn off reference voltage, etc. */ + + ADC12CTL0 &= ~ENC; + /* need to remove CONSEQ_3 if not EOS is configured */ + ADC12CTL1 &= ~CONSEQ_3; + + /* wait for conversion to stop */ + while(ADC12CTL1 & ADC12BUSY); + + /* clear any pending interrupts */ + ADC12IFG = 0; +} +/*---------------------------------------------------------------------------*/ +int +sky_sensors_status(uint16_t input, int type) +{ + if(type == SENSORS_ACTIVE) { + return (adc_on & input) == input; + } + if(type == SENSORS_READY) { + ready |= ADC12IFG & adc_on & input; + return (ready & adc_on & input) == input; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +int +sky_sensors_configure(uint16_t input, uint8_t ref, int type, int value) +{ + uint16_t c; + + if(type == SENSORS_ACTIVE) { + stop(); + + if(value) { + adc_on |= input; + P6SEL |= input & 0xff; + + /* Set ADC config */ + for(c = 0; c < 16; c++) { + if(input & (1 << c)) { + ADC12MCTL_NO(c) = (c * INCH_1) | ref; + } + } + + } else { + adc_on &= ~input; + ready &= ~input; + P6SEL &= ~(input & 0xff); + } + + if(adc_on == 0) { + P6DIR = 0x00; + P6SEL = 0x00; + + /* Turn off ADC and internal reference generator */ + ADC12CTL0 = 0; + ADC12CTL1 = 0; + } else { + start(); + } + return 1; + } + return 0; +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/dev/sky-sensors.h b/arch/platform/z1/dev/sky-sensors.h new file mode 100644 index 000000000..df2aeea48 --- /dev/null +++ b/arch/platform/z1/dev/sky-sensors.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * ----------------------------------------------------------------- + * + * Author : Joakim Eriksson + * Created : 2010-02-02 + * Updated : $Date: 2010/08/25 19:30:53 $ + * $Revision: 1.2 $ + */ + +#ifndef SKY_SENSORS_H_ +#define SKY_SENSORS_H_ + +int sky_sensors_status(uint16_t input, int type); +int sky_sensors_configure(uint16_t input, uint8_t reference, + int type, int value); + +#endif /* SKY_SENSORS_H_ */ diff --git a/arch/platform/z1/dev/temperature-sensor.c b/arch/platform/z1/dev/temperature-sensor.c new file mode 100644 index 000000000..1d4328d24 --- /dev/null +++ b/arch/platform/z1/dev/temperature-sensor.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \file + * Sensor driver for reading the built-in temperature sensor in the CPU. + * \author + * Adam Dunkels + * Joakim Eriksson + * Niclas Finne + */ + +#include "dev/temperature-sensor.h" +#include "dev/sky-sensors.h" +#include "contiki.h" + +#define INPUT_CHANNEL (1 << INCH_10) +#define INPUT_REFERENCE SREF_1 +#define TEMPERATURE_MEM ADC12MEM10 + +const struct sensors_sensor temperature_sensor; + +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return TEMPERATURE_MEM; +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + return sky_sensors_configure(INPUT_CHANNEL, INPUT_REFERENCE, type, c); +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + return sky_sensors_status(INPUT_CHANNEL, type); +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR, + value, configure, status); diff --git a/tools/sky/uip6-bridge/project-conf.h b/arch/platform/z1/dev/temperature-sensor.h similarity index 81% rename from tools/sky/uip6-bridge/project-conf.h rename to arch/platform/z1/dev/temperature-sensor.h index 651d45cda..05eafe852 100644 --- a/tools/sky/uip6-bridge/project-conf.h +++ b/arch/platform/z1/dev/temperature-sensor.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, Swedish Institute of Computer Science. + * Copyright (c) 2010, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,20 +30,20 @@ /** * \file - * A brief description of what this file is + * Temperature sensor header file. * \author - * Niclas Finne + * Adam Dunkels * Joakim Eriksson + * Niclas Finne */ -#ifndef BRIDGE_CONF_H_ -#define BRIDGE_CONF_H_ +#ifndef TEMPERATURE_SENSOR_H_ +#define TEMPERATURE_SENSOR_H_ -#undef UIP_CONF_ROUTER +#include "lib/sensors.h" -#undef UIP_CONF_LLH_LEN -#define UIP_CONF_LLH_LEN 14 +extern const struct sensors_sensor temperature_sensor; -#undef UIP_CONF_BUFFER_SIZE -#define UIP_CONF_BUFFER_SIZE 256 -#endif /* BRIDGE_CONF_H_ */ +#define TEMPERATURE_SENSOR "Temperature" + +#endif /* TEMPERATURE_SENSOR_H_ */ diff --git a/arch/platform/z1/dev/tlc59116.c b/arch/platform/z1/dev/tlc59116.c new file mode 100644 index 000000000..2db0bf093 --- /dev/null +++ b/arch/platform/z1/dev/tlc59116.c @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2013, Jelmer Tiete. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Device drivers for tlc59116 i2c led driver on Zolertia Z1. + * See http://www.ti.com/product/tlc59116 for datasheet. + * \author + * Jelmer Tiete, VUB + */ + +#include +#include "contiki.h" +#include "tlc59116.h" +#include "i2cmaster.h" + +/*---------------------------------------------------------------------------*/ +/* Write to a register. + * args: + * reg register to write to + * val value to write + */ + +void +tlc59116_write_reg(uint8_t reg, uint8_t val) +{ + uint8_t tx_buf[] = { reg, val }; + + i2c_transmitinit(TLC59116_ADDR); + while(i2c_busy()); + PRINTFDEBUG("I2C Ready to TX\n"); + + i2c_transmit_n(2, tx_buf); + while(i2c_busy()); + PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg); +} +/*---------------------------------------------------------------------------*/ +/* Write several registers from a stream. + * args: + * len number of bytes to write + * data pointer to where the data is written from + * + * First byte in stream must be the register address to begin writing to. + * The data is then written from second byte and increasing. + */ + +void +tlc59116_write_stream(uint8_t len, uint8_t *data) +{ + i2c_transmitinit(TLC59116_ADDR); + while(i2c_busy()); + PRINTFDEBUG("I2C Ready to TX(stream)\n"); + + i2c_transmit_n(len, data); /* start tx and send conf reg */ + while(i2c_busy()); + PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]); +} +/*---------------------------------------------------------------------------*/ +/* Read one register. + * args: + * reg what register to read + * returns the value of the read register + */ + +uint8_t +tlc59116_read_reg(uint8_t reg) +{ + uint8_t retVal = 0; + uint8_t rtx = reg; + + PRINTFDEBUG("READ_REG 0x%02X\n", reg); + + /* transmit the register to read */ + i2c_transmitinit(TLC59116_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, &rtx); + while(i2c_busy()); + + /* receive the data */ + i2c_receiveinit(TLC59116_ADDR); + while(i2c_busy()); + i2c_receive_n(1, &retVal); + while(i2c_busy()); + + return retVal; +} +/*---------------------------------------------------------------------------*/ +/* Read several registers in a stream. + * args: + * reg what register to start reading from + * len number of bytes to read + * whereto pointer to where the data is saved + */ + +void +tlc59116_read_stream(uint8_t reg, uint8_t len, uint8_t *whereto) +{ + uint8_t rtx = reg; + + PRINTFDEBUG("READ_STR %u B from 0x%02X\n", len, reg); + + /* transmit the register to start reading from */ + i2c_transmitinit(TLC59116_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, &rtx); + while(i2c_busy()); + + /* receive the data */ + i2c_receiveinit(TLC59116_ADDR); + while(i2c_busy()); + i2c_receive_n(len, whereto); + while(i2c_busy()); +} +/*---------------------------------------------------------------------------*/ +/* Set pwm value for individual led. Make sure PWM mode is enabled. + * args: + * led led output -> 0 till 15 + * pwm led pwm value + */ + +void +tlc59116_led(uint8_t led, uint8_t pwm) +{ + if((led < 0) || (led > 15)) { + PRINTFDEBUG("TLC59116: wrong led value."); + } else { + tlc59116_write_reg(led + TLC59116_PWM0, pwm); + } +} +/*---------------------------------------------------------------------------*/ +/* Init the led driver: ports, pins, registers, interrupts (none enabled), I2C, + * default threshold values etc. + */ + +void +tlc59116_init(void) +{ + /* Set up ports and pins for I2C communication */ + i2c_enable(); + + /* set default register values. */ + tlc59116_write_reg(TLC59116_MODE1, TLC59116_MODE1_DEFAULT); + tlc59116_write_reg(TLC59116_MODE2, TLC59116_MODE2_DEFAULT); + + /*Set all PWM values to 0x00 (off) */ + /*This would maybe be better with a SWRST */ + uint8_t tx_buf[] = + { TLC59116_PWM0_AUTOINCR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + tlc59116_write_stream(17, tx_buf); + + /* set all leds to PWM control */ + tlc59116_write_reg(TLC59116_LEDOUT0, TLC59116_LEDOUT_PWM); + tlc59116_write_reg(TLC59116_LEDOUT1, TLC59116_LEDOUT_PWM); + tlc59116_write_reg(TLC59116_LEDOUT2, TLC59116_LEDOUT_PWM); + tlc59116_write_reg(TLC59116_LEDOUT3, TLC59116_LEDOUT_PWM); +} diff --git a/arch/platform/z1/dev/tlc59116.h b/arch/platform/z1/dev/tlc59116.h new file mode 100644 index 000000000..c5b74f76a --- /dev/null +++ b/arch/platform/z1/dev/tlc59116.h @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2013, Jelmer Tiete. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Device drivers header file for TLC59116 i2c led driver on Zolertia Z1. + * See http://www.ti.com/product/tlc59116 for datasheet. + * \author + * Jelmer Tiete, VUB + */ + +#ifndef TLC59116_H_ +#define TLC59116_H_ +#include +#include "dev/i2cmaster.h" + +#if 0 +#include +#define PRINTFDEBUG(...) printf(__VA_ARGS__) +#else +#define PRINTFDEBUG(...) +#endif + + +/* -------------------------------------------------------------------------- */ +/* Init the led driver: ports, pins, registers, I2C*/ +void tlc59116_init(void); + +/* Write to a register. + * args: + * reg register to write to + * val value to write + */ +void tlc59116_write_reg(uint8_t reg, uint8_t val); + +/* Write several registers from a stream. + * args: + * len number of bytes to read + * data pointer to where the data is read from + * First byte in stream must be the register address to begin writing to. + * The data is then written from the second byte and increasing. The address byte + * is not included in length len. + */ +void tlc59116_write_stream(uint8_t len, uint8_t * data); + +/* Read one register. + * args: + * reg what register to read + * returns the value of the read register + */ +uint8_t tlc59116_read_reg(uint8_t reg); + +/* Read several registers in a stream. + * args: + * reg what register to start reading from + * len number of bytes to read + * whereto pointer to where the data is saved + */ +void tlc59116_read_stream(uint8_t reg, uint8_t len, uint8_t * whereto); + +/* Set pwm value for individual led + * args: + * led led output -> 0 till 15 + * pwm led pwm value + */ +void tlc59116_led(uint8_t led, uint8_t pwm); + + +/* -------------------------------------------------------------------------- */ +/* Application definitions, change if required by application. */ + +/* Suggested defaults according to the data sheet etc */ +#define TLC59116_MODE1_DEFAULT 0x00 /* Default (no sub or all call) + OSC on */ +#define TLC59116_MODE2_DEFAULT 0x00 /* Default (output change on stop) */ + +#define TLC59116_LEDOUT_PWM 0xAA /* LDRx = 01 -> PWM; 4 leds per reg: 01010101b -> 0xAA */ + +/* -------------------------------------------------------------------------- */ +/* Reference definitions, should not be changed */ +/* TLC59116 slave address */ +#define TLC59116_ADDR 0x60 /* 7bit adress, 8bit write adress: 0xC0 */ + /* address with all address pins pulled to ground */ +/* TLC59116 registers */ +#define TLC59116_MODE1 0x00 +#define TLC59116_MODE2 0x01 +#define TLC59116_PWM0_AUTOINCR 0xA2 /* auto increment address for first pwm register */ +#define TLC59116_PWM0 0x02 +#define TLC59116_PWM1 0x03 +#define TLC59116_PWM2 0x04 +#define TLC59116_PWM3 0x05 +#define TLC59116_PWM4 0x06 +#define TLC59116_PWM5 0x07 +#define TLC59116_PWM6 0x08 +#define TLC59116_PWM7 0x09 +#define TLC59116_PWM8 0x0A +#define TLC59116_PWM9 0x0B +#define TLC59116_PWM10 0x0C +#define TLC59116_PWM11 0x0D +#define TLC59116_PWM12 0x0E +#define TLC59116_PWM13 0x0F +#define TLC59116_PWM14 0x10 +#define TLC59116_PWM15 0x11 +#define TLC59116_GRPPWM 0x12 +#define TLC59116_GRPFREQ 0x13 +#define TLC59116_LEDOUT0 0x14 +#define TLC59116_LEDOUT1 0x15 +#define TLC59116_LEDOUT2 0x16 +#define TLC59116_LEDOUT3 0x17 + +/* More registers follow, but not used in this implementation */ + +/* -------------------------------------------------------------------------- */ +#endif /* ifndef TLC59116_H_ */ diff --git a/arch/platform/z1/dev/tmp102.c b/arch/platform/z1/dev/tmp102.c new file mode 100644 index 000000000..4bf267246 --- /dev/null +++ b/arch/platform/z1/dev/tmp102.c @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * Copyright (c) 2016, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \file + * Device drivers for tmp102 temperature sensor in Zolertia Z1. + * \author + * Enric M. Calvo, Zolertia + * Marcus Lundén, SICS + * Antonio Lignan, Zolertia + */ +/*---------------------------------------------------------------------------*/ +#include +#include "contiki.h" +#include "i2cmaster.h" +#include "tmp102.h" +#include "lib/sensors.h" +/*---------------------------------------------------------------------------*/ +#define DEBUG 0 +#if DEBUG +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif +/*---------------------------------------------------------------------------*/ +static uint8_t enabled; +/*---------------------------------------------------------------------------*/ +void +tmp102_init(void) +{ + /* Power Up TMP102 via pin */ + TMP102_PWR_DIR |= TMP102_PWR_PIN; + TMP102_PWR_SEL &= ~TMP102_PWR_SEL; + TMP102_PWR_SEL2 &= ~TMP102_PWR_SEL; + TMP102_PWR_REN &= ~TMP102_PWR_SEL; + TMP102_PWR_OUT |= TMP102_PWR_PIN; + + /* Set up ports and pins for I2C communication */ + i2c_enable(); + + enabled = 1; +} +/*---------------------------------------------------------------------------*/ +void +tmp102_stop(void) +{ + /* Power off */ + TMP102_PWR_OUT &= ~TMP102_PWR_PIN; + enabled = 0; +} +/*---------------------------------------------------------------------------*/ +void +tmp102_write_reg(uint8_t reg, uint16_t val) +{ + uint8_t tx_buf[] = { reg, 0x00, 0x00 }; + + tx_buf[1] = (uint8_t)(val >> 8); + tx_buf[2] = (uint8_t)(val & 0x00FF); + + i2c_transmitinit(TMP102_ADDR); + while(i2c_busy()); + PRINTF("I2C Ready to TX\n"); + + i2c_transmit_n(3, tx_buf); + while(i2c_busy()); + PRINTF("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg); +} +/*---------------------------------------------------------------------------*/ +uint16_t +tmp102_read_reg(uint8_t reg) +{ + uint8_t buf[] = { 0x00, 0x00 }; + uint16_t retVal = 0; + uint8_t rtx = reg; + PRINTF("READ_REG 0x%02X\n", reg); + + /* transmit the register to read */ + i2c_transmitinit(TMP102_ADDR); + while(i2c_busy()); + i2c_transmit_n(1, &rtx); + while(i2c_busy()); + + /* receive the data */ + i2c_receiveinit(TMP102_ADDR); + while(i2c_busy()); + i2c_receive_n(2, &buf[0]); + while(i2c_busy()); + + retVal = (uint16_t)(buf[0] << 8 | (buf[1])); + + return retVal; +} +/*---------------------------------------------------------------------------*/ +uint16_t +tmp102_read_temp_raw(void) +{ + uint16_t rd = 0; + rd = tmp102_read_reg(TMP102_TEMP); + return rd; +} +/*---------------------------------------------------------------------------*/ +int16_t +tmp102_read_temp_x100(void) +{ + int16_t raw = 0; + int16_t sign = 1; + int16_t abstemp, temp_int; + + raw = (int16_t)tmp102_read_reg(TMP102_TEMP); + if(raw < 0) { + abstemp = (raw ^ 0xFFFF) + 1; + sign = -1; + } else { + abstemp = raw; + } + + /* Integer part of the temperature value and percents*/ + temp_int = (abstemp >> 8) * sign * 100; + temp_int += ((abstemp & 0xff) * 100) / 0x100; + return temp_int; +} +/*---------------------------------------------------------------------------*/ +int8_t +tmp102_read_temp_simple(void) +{ + /* Casted to int8_t: We don't expect temperatures outside -128 to 127 C */ + return tmp102_read_temp_x100() / 100; +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) +{ + if(type != SENSORS_ACTIVE) { + return TMP102_ERROR; + } + if(value) { + tmp102_init(); + } else { + tmp102_stop(); + } + enabled = value; + return TMP102_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return enabled; + } + return TMP102_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return (int)tmp102_read_temp_x100(); +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(tmp102, TMP102_SENSOR, value, configure, status); +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/dev/tmp102.h b/arch/platform/z1/dev/tmp102.h new file mode 100644 index 000000000..cf7a71eb8 --- /dev/null +++ b/arch/platform/z1/dev/tmp102.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * Copyright (c) 2016, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Device drivers header file for tmp102 temperature sensor in Zolertia + * Z1 WSN Platform. + * \author + * Enric M. Calvo, Zolertia + * Marcus Lundén, SICS + * Antonio Lignan, Zolertia + */ +/* -------------------------------------------------------------------------- */ +#ifndef TMP102_H_ +#define TMP102_H_ +#include +#include "lib/sensors.h" +#include "i2cmaster.h" +/* -------------------------------------------------------------------------- */ +void tmp102_init(void); +void tmp102_write_reg(uint8_t reg, uint16_t val); +uint16_t tmp102_read_reg(uint8_t reg); +uint16_t tmp102_read_temp_raw(); +int8_t tmp102_read_temp_simple(); +int16_t tmp102_read_temp_x100(); +/* -------------------------------------------------------------------------- */ +#define TMP102_ADDR 0x48 +#define TMP102_TEMP 0x00 +#define TMP102_CONF 0x01 +#define TMP102_TLOW 0x02 +#define TMP102_THIGH 0x03 + +/* TMP102 pin-out */ +#define TMP102_PWR_DIR P5DIR +#define TMP102_PWR_SEL P5SEL +#define TMP102_PWR_SEL2 P5SEL2 +#define TMP102_PWR_REN P5REN +#define TMP102_PWR_OUT P5OUT +#define TMP102_PWR_PIN (1<<0) /* P5.0 */ +/* -------------------------------------------------------------------------- */ +#define TMP102_SUCCESS 0 +#define TMP102_ERROR (-1) +#define TMP102_READ 0x01 +/* -------------------------------------------------------------------------- */ +#define TMP102_SENSOR "TMP102 sensor" +/* -------------------------------------------------------------------------- */ +extern const struct sensors_sensor tmp102; +/* -------------------------------------------------------------------------- */ +#endif /* ifndef TMP102_H_ */ diff --git a/arch/platform/z1/dev/xmem.c b/arch/platform/z1/dev/xmem.c new file mode 100644 index 000000000..89ca9c17b --- /dev/null +++ b/arch/platform/z1/dev/xmem.c @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \file + * Device driver for the ST M25P16 40MHz 1Mbyte external memory. + * \author + * Björn Grönvall + * Enric M. Calvo + * + * Data is written bit inverted (~-operator) to flash so that + * unwritten data will read as zeros (UNIX style). + */ + +#include +#include + +#include "contiki.h" +#include "dev/spi-legacy.h" +#include "dev/xmem.h" +#include "dev/watchdog.h" + +#if 1 +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) do {} while (0) +#endif + +#define SPI_FLASH_INS_WREN 0x06 +#define SPI_FLASH_INS_WRDI 0x04 +#define SPI_FLASH_INS_RDSR 0x05 +#define SPI_FLASH_INS_WRSR 0x01 +#define SPI_FLASH_INS_READ 0x03 +#define SPI_FLASH_INS_FAST_READ 0x0b +#define SPI_FLASH_INS_PP 0x02 +#define SPI_FLASH_INS_SE 0xd8 +#define SPI_FLASH_INS_BE 0xc7 +#define SPI_FLASH_INS_DP 0xb9 +#define SPI_FLASH_INS_RES 0xab +/*---------------------------------------------------------------------------*/ +static void +write_enable(void) +{ + int s; + + s = splhigh(); + SPI_FLASH_ENABLE(); + + SPI_WRITE(SPI_FLASH_INS_WREN); + + SPI_FLASH_DISABLE(); + splx(s); +} +/*---------------------------------------------------------------------------*/ +static unsigned +read_status_register(void) +{ + unsigned char u; + + int s; + + s = splhigh(); + SPI_FLASH_ENABLE(); + + + SPI_WRITE(SPI_FLASH_INS_RDSR); + + SPI_FLUSH(); + SPI_READ(u); + + SPI_FLASH_DISABLE(); + splx(s); + + return u; +} +/*---------------------------------------------------------------------------*/ +/* + * Wait for a write/erase operation to finish. + */ +static unsigned +wait_ready(void) +{ + unsigned u; + do { + u = read_status_register(); + watchdog_periodic(); + } while(u & 0x01); /* WIP=1, write in progress */ + return u; +} +/*---------------------------------------------------------------------------*/ +/* + * Erase 64k bytes of data. It takes about 1s before WIP goes low! + */ +static void +erase_sector(unsigned long offset) +{ + int s; + wait_ready(); + + write_enable(); + + s = splhigh(); + SPI_FLASH_ENABLE(); + + SPI_WRITE_FAST(SPI_FLASH_INS_SE); + SPI_WRITE_FAST(offset >> 16); /* MSB */ + SPI_WRITE_FAST(offset >> 8); + SPI_WRITE_FAST(offset >> 0); /* LSB */ + SPI_WAITFORTx_ENDED(); + + SPI_FLASH_DISABLE(); + splx(s); +} +/*---------------------------------------------------------------------------*/ +/* + * Initialize external flash *and* SPI bus! + */ +void +xmem_init(void) +{ + spi_init(); + + P4DIR |= BV(FLASH_CS); /* Unnecessary for Zolertia Z1 | BV(FLASH_PWR); */ + P5DIR |= BV(FLASH_HOLD); /* In P5 for Z1 */ + + SPI_FLASH_DISABLE(); /* Unselect flash. */ + SPI_FLASH_UNHOLD(); +} +/*---------------------------------------------------------------------------*/ +int +xmem_pread(void *_p, int size, unsigned long offset) +{ + unsigned char *p = _p; + const unsigned char *end = p + size; + int s; + wait_ready(); + + s = splhigh(); + SPI_FLASH_ENABLE(); + + SPI_WRITE_FAST(SPI_FLASH_INS_READ); + SPI_WRITE_FAST(offset >> 16); /* MSB */ + SPI_WRITE_FAST(offset >> 8); + SPI_WRITE_FAST(offset >> 0); /* LSB */ + SPI_WAITFORTx_ENDED(); + + SPI_FLUSH(); + for(; p < end; p++) { + unsigned char u; + SPI_READ(u); + *p = ~u; + } + + SPI_FLASH_DISABLE(); + splx(s); + + return size; +} +/*---------------------------------------------------------------------------*/ +static const unsigned char * +program_page(unsigned long offset, const unsigned char *p, int nbytes) +{ + const unsigned char *end = p + nbytes; + int s; + + wait_ready(); + + write_enable(); + + s = splhigh(); + SPI_FLASH_ENABLE(); + + SPI_WRITE_FAST(SPI_FLASH_INS_PP); + SPI_WRITE_FAST(offset >> 16); /* MSB */ + SPI_WRITE_FAST(offset >> 8); + SPI_WRITE_FAST(offset >> 0); /* LSB */ + + for(; p < end; p++) { + SPI_WRITE_FAST(~*p); + } + SPI_WAITFORTx_ENDED(); + + SPI_FLASH_DISABLE(); + splx(s); + + return p; +} +/*---------------------------------------------------------------------------*/ +int +xmem_pwrite(const void *_buf, int size, unsigned long addr) +{ + const unsigned char *p = _buf; + const unsigned long end = addr + size; + unsigned long i, next_page; + + for(i = addr; i < end;) { + next_page = (i | 0xff) + 1; + if(next_page > end) { + next_page = end; + } + p = program_page(i, p, next_page - i); + i = next_page; + } + + return size; +} +/*---------------------------------------------------------------------------*/ +int +xmem_erase(long size, unsigned long addr) +{ + unsigned long end = addr + size; + + if(size % XMEM_ERASE_UNIT_SIZE != 0) { + PRINTF("xmem_erase: bad size\n"); + return -1; + } + + if(addr % XMEM_ERASE_UNIT_SIZE != 0) { + PRINTF("xmem_erase: bad offset\n"); + return -1; + } + + for (; addr < end; addr += XMEM_ERASE_UNIT_SIZE) { + erase_sector(addr); + } + + return size; +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/dev/z1-phidgets.c b/arch/platform/z1/dev/z1-phidgets.c new file mode 100644 index 000000000..e86c3e2f0 --- /dev/null +++ b/arch/platform/z1/dev/z1-phidgets.c @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * + * ----------------------------------------------------------------- + * + * Author : Joakim Eriksson + * Created : 2010-02-02 + * Updated : $Date: 2010/11/05 10:31:57 $ + * $Revision: 1.3 $ + */ + +#include "contiki.h" +#include "lib/sensors.h" +#include "dev/z1-phidgets.h" + +static uint8_t adc_on; +static uint8_t active; +/*---------------------------------------------------------------------------*/ +static void +sensors_activate(uint8_t type) +{ + uint8_t pre = adc_on; + + adc_on |= type; + + if(pre == 0 && adc_on > 0) { + P6DIR = 0xff; + P6OUT = 0x00; + P6SEL |= 0x8b; /* bit 7 + 3 + 1 + 0 */ + + /* if nothing was started before, start up the ADC system */ + /* Set up the ADC. */ + ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC; /* Setup ADC12, ref., sampling time */ + ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0; /* Use sampling timer, repeat-sequenc-of-channels */ + /* convert up to MEM4 */ + ADC12MCTL4 |= EOS; + + ADC12CTL0 |= ADC12ON + REFON; + ADC12CTL0 |= ENC; /* enable conversion */ + ADC12CTL0 |= ADC12SC; /* sample & convert */ + } +} +/*---------------------------------------------------------------------------*/ +static void +sensors_deactivate(uint8_t type) +{ + adc_on &= ~type; + + if(adc_on == 0) { + /* stop converting immediately, turn off reference voltage, etc. */ + /* wait for conversion to stop */ + + ADC12CTL0 &= ~ENC; + /* need to remove CONSEQ_3 if not EOS is configured */ + ADC12CTL1 &= ~CONSEQ_3; + + while(ADC12CTL1 & ADC12BUSY); + + ADC12CTL0 = 0; + ADC12CTL1 = 0; + + P6DIR = 0x00; + P6OUT = 0x00; + P6SEL = 0x00; + } +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + /* ADC0 corresponds to the port under the logo, ADC1 to the port over the logo, + ADC2 and ADC3 corresponds to port on the JCreate bottom expansion port) */ + switch(type) { + case PHIDGET5V_1: + return ADC12MEM0; + case PHIDGET5V_2: + return ADC12MEM1; + case PHIDGET3V_1: + return ADC12MEM2; + case PHIDGET3V_2: + return ADC12MEM3; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + case SENSORS_ACTIVE: + case SENSORS_READY: + return active; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + switch(type) { + case SENSORS_ACTIVE: + if(c) { + if(!status(SENSORS_ACTIVE)) { + /* SREF_1 is Vref+ */ + /* MemReg6 == P6.0/A0 == 5V 1 */ + ADC12MCTL0 = (INCH_0 + SREF_0); + /* MemReg7 == P6.3/A3 == 5V 2 */ + ADC12MCTL1 = (INCH_3 + SREF_0); + /* MemReg8 == P6.1/A1 == 3V 1 */ + ADC12MCTL2 = (INCH_1 + SREF_0); + /* MemReg9 == P6.7/A7 == 3V_2 */ + ADC12MCTL3 = (INCH_7 + SREF_0); + + sensors_activate(0x0F); + active = 1; + } + } else { + sensors_deactivate(0x0F); + active = 0; + } + } + return 0; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(phidgets, "Phidgets", value, configure, status); diff --git a/arch/platform/z1/dev/z1-phidgets.h b/arch/platform/z1/dev/z1-phidgets.h new file mode 100644 index 000000000..f56356508 --- /dev/null +++ b/arch/platform/z1/dev/z1-phidgets.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * + * ----------------------------------------------------------------- + * + * Author : Joakim Eriksson + * Created : 2010-08-26 + * Updated : $Date: 2010/08/27 12:58:57 $ + * $Revision: 1.2 $ + */ +#ifndef Z1_PHIDGETS_H_ +#define Z1_PHIDGETS_H_ + +#define PHIDGET5V_1 0 +#define PHIDGET5V_2 1 +#define PHIDGET3V_1 2 +#define PHIDGET3V_2 3 + +extern const struct sensors_sensor phidgets; + +#endif /* Z1_PHIDGETS_H_ */ diff --git a/arch/platform/z1/dev/z1-sensors.c b/arch/platform/z1/dev/z1-sensors.c new file mode 100644 index 000000000..db5b418b9 --- /dev/null +++ b/arch/platform/z1/dev/z1-sensors.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2016, Zolertia - http://www.zolertia.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/*---------------------------------------------------------------------------*/ +/** + * \file + * Zolertia Z1 on-board sensors + * \author + * Antonio Lignan, Zolertia + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "lib/sensors.h" +#include "dev/button-sensor.h" +#include "dev/tmp102.h" +#include "dev/adxl345.h" +/*---------------------------------------------------------------------------*/ +/** \brief Exports global symbols for the sensor API */ +SENSORS(&button_sensor, &adxl345, &tmp102); +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/node-id-z1.c b/arch/platform/z1/node-id-z1.c new file mode 100644 index 000000000..7f65e2c58 --- /dev/null +++ b/arch/platform/z1/node-id-z1.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Utility to store a node id in the external flash + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "sys/node-id.h" +#include "dev/xmem.h" +#include + +unsigned char node_mac[8]; + +/*---------------------------------------------------------------------------*/ +void +node_id_z1_restore(void) +{ + unsigned char buf[12]; + xmem_pread(buf, 12, NODE_ID_XMEM_OFFSET); + if(buf[0] == 0xad && + buf[1] == 0xde) { + node_id = (buf[2] << 8) | buf[3]; + memcpy(node_mac, &buf[4], 8); + } else { + node_id = 0; + } +} +/*---------------------------------------------------------------------------*/ +void +node_id_z1_burn(unsigned short id) +{ + unsigned char buf[12]; + memset(buf, 0, sizeof(buf)); + buf[0] = 0xad; + buf[1] = 0xde; + buf[2] = id >> 8; + buf[3] = id & 0xff; + xmem_erase(XMEM_ERASE_UNIT_SIZE, NODE_ID_XMEM_OFFSET); + xmem_pwrite(buf, 12, NODE_ID_XMEM_OFFSET); +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/node-id-z1.h b/arch/platform/z1/node-id-z1.h new file mode 100644 index 000000000..d70c018d6 --- /dev/null +++ b/arch/platform/z1/node-id-z1.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Adam Dunkels + * + */ + +#ifndef NODE_ID_Z1_H_ +#define NODE_ID_Z1_H_ + +#include + +void node_id_z1_restore(void); +void node_id_z1_burn(uint16_t node_id); + +#endif /* NODE_ID_Z1_H_ */ diff --git a/arch/platform/z1/platform.c b/arch/platform/z1/platform.c new file mode 100644 index 000000000..0d8d248c5 --- /dev/null +++ b/arch/platform/z1/platform.c @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +#include +#include + +#include "contiki.h" +#include "cc2420.h" +#include "dev/leds.h" +#include "dev/serial-line.h" +#include "dev/slip.h" +#include "dev/uart0.h" +#include "dev/watchdog.h" +#include "dev/xmem.h" +#include "lib/random.h" +#include "net/netstack.h" +#include "net/mac/framer/frame802154.h" +#include "dev/adxl345.h" +#include "sys/clock.h" +#include "sys/energest.h" + +#if NETSTACK_CONF_WITH_IPV6 +#include "net/ipv6/uip-ds6.h" +#endif /* NETSTACK_CONF_WITH_IPV6 */ + +#include "node-id-z1.h" +#include "cfs-coffee-arch.h" +#include "cfs/cfs-coffee.h" + +extern unsigned char node_mac[8]; + +#if DCOSYNCH_CONF_ENABLED +static struct timer mgt_timer; +#endif +extern int msp430_dco_required; + +#define UIP_OVER_MESH_CHANNEL 8 +#if NETSTACK_CONF_WITH_IPV4 +static uint8_t is_gateway; +#endif /* NETSTACK_CONF_WITH_IPV4 */ + +#ifdef EXPERIMENT_SETUP +#include "experiment-setup.h" +#endif + +void init_platform(void); +/*---------------------------------------------------------------------------*/ +/* Log configuration */ +#include "sys/log.h" +#define LOG_MODULE "Z1" +#define LOG_LEVEL LOG_LEVEL_MAIN +/*---------------------------------------------------------------------------*/ +#ifdef UART0_CONF_BAUD_RATE +#define UART0_BAUD_RATE UART0_CONF_BAUD_RATE +#else +#define UART0_BAUD_RATE 115200 +#endif +/*---------------------------------------------------------------------------*/ +#if 0 +int +force_float_inclusion() +{ + extern int __fixsfsi; + extern int __floatsisf; + extern int __mulsf3; + extern int __subsf3; + + return __fixsfsi + __floatsisf + __mulsf3 + __subsf3; +} +#endif +/*---------------------------------------------------------------------------*/ +void +uip_log(char *msg) +{ + puts(msg); +} +/*---------------------------------------------------------------------------*/ +#if 0 +void +force_inclusion(int d1, int d2) +{ + snprintf(NULL, 0, "%d", d1 % d2); +} +#endif +/*---------------------------------------------------------------------------*/ +static void +set_lladdr(void) +{ + linkaddr_t addr; + + memset(&addr, 0, sizeof(linkaddr_t)); +#if NETSTACK_CONF_WITH_IPV6 + memcpy(addr.u8, node_mac, sizeof(addr.u8)); +#else + if(node_id == 0) { + int i; + for(i = 0; i < sizeof(linkaddr_t); ++i) { + addr.u8[i] = node_mac[7 - i]; + } + } else { + addr.u8[0] = node_id & 0xff; + addr.u8[1] = node_id >> 8; + } +#endif + linkaddr_set_node_addr(&addr); +} +/*---------------------------------------------------------------------------*/ +void +platform_init_stage_one(void) +{ + /* + * Initalize hardware. + */ + msp430_cpu_init(); + + leds_init(); + leds_on(LEDS_RED); +} +/*---------------------------------------------------------------------------*/ +void +platform_init_stage_two(void) +{ + clock_wait(100); + + uart0_init(BAUD2UBR(UART0_BAUD_RATE)); /* Must come before first printf */ + + xmem_init(); + + leds_off(LEDS_RED); + /* + * Hardware initialization done! + */ + + /* Restore node id if such has been stored in external mem */ + node_id_z1_restore(); + + /* If no MAC address was burned, we use the node id or the Z1 product ID */ + if(!(node_mac[0] | node_mac[1] | node_mac[2] | node_mac[3] | + node_mac[4] | node_mac[5] | node_mac[6] | node_mac[7])) { + +#ifdef SERIALNUM + if(!node_id) { + LOG_INFO("Node id is not set, using Z1 product ID\n"); + node_id = SERIALNUM; + } +#endif + node_mac[0] = 0xc1; /* Hardcoded for Z1 */ + node_mac[1] = 0x0c; /* Hardcoded for Revision C */ + node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that + the 802.15.4 MAC address is compatible with + an Ethernet MAC address - byte 0 (byte 2 in + the DS ID) */ + node_mac[3] = 0x00; /* Hardcoded */ + node_mac[4] = 0x00; /* Hardcoded */ + node_mac[5] = 0x00; /* Hardcoded */ + node_mac[6] = node_id >> 8; + node_mac[7] = node_id & 0xff; + } + + /* Overwrite node MAC if desired at compile time */ +#ifdef MACID +#warning "***** CHANGING DEFAULT MAC *****" + node_mac[0] = 0xc1; /* Hardcoded for Z1 */ + node_mac[1] = 0x0c; /* Hardcoded for Revision C */ + node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that + the 802.15.4 MAC address is compatible with + an Ethernet MAC address - byte 0 (byte 2 in + the DS ID) */ + node_mac[3] = 0x00; /* Hardcoded */ + node_mac[4] = 0x00; /* Hardcoded */ + node_mac[5] = 0x00; /* Hardcoded */ + node_mac[6] = MACID >> 8; + node_mac[7] = MACID & 0xff; +#endif + +#ifdef IEEE_802154_MAC_ADDRESS + /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */ + { + uint8_t ieee[] = IEEE_802154_MAC_ADDRESS; + memcpy(node_mac, ieee, sizeof(uip_lladdr.addr)); + node_mac[7] = node_id & 0xff; + } +#endif /* IEEE_802154_MAC_ADDRESS */ + + random_init(node_mac[6] + node_mac[7]); + + set_lladdr(); + + /* + * main() will turn the radio on inside netstack_init(). The CC2420 + * must already be initialised by that time, so we do this here early. + * Later on in stage three we set correct values for PANID and radio + * short/long address. + */ + cc2420_init(); + + SENSORS_ACTIVATE(adxl345); + + leds_off(LEDS_ALL); +} +/*---------------------------------------------------------------------------*/ +void +platform_init_stage_three(void) +{ + uint8_t longaddr[8]; + uint16_t shortaddr; + + init_platform(); + + shortaddr = (linkaddr_node_addr.u8[0] << 8) + linkaddr_node_addr.u8[1]; + memset(longaddr, 0, sizeof(longaddr)); + linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr); + + cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); + + LOG_INFO("CC2420 CCA threshold %i\n", CC2420_CONF_CCA_THRESH); + +#if DCOSYNCH_CONF_ENABLED + timer_set(&mgt_timer, DCOSYNCH_PERIOD * CLOCK_SECOND); +#endif +} +/*---------------------------------------------------------------------------*/ +void +platform_idle(void) +{ + /* + * Idle processing. + */ + int s = splhigh(); /* Disable interrupts. */ + /* uart0_active is for avoiding LPM3 when still sending or receiving */ + if(process_nevents() != 0 || uart0_active()) { + splx(s); /* Re-enable interrupts. */ + } else { +#if DCOSYNCH_CONF_ENABLED + /* before going down to sleep possibly do some management */ + if(timer_expired(&mgt_timer)) { + watchdog_periodic(); + timer_reset(&mgt_timer); + msp430_sync_dco(); +#if CC2420_CONF_SFD_TIMESTAMPS + cc2420_arch_sfd_init(); +#endif /* CC2420_CONF_SFD_TIMESTAMPS */ + } +#endif + + /* Re-enable interrupts and go to sleep atomically. */ + ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM); + watchdog_stop(); + /* check if the DCO needs to be on - if so - only LPM 1 */ + if (msp430_dco_required) { + _BIS_SR(GIE | CPUOFF); /* LPM1 sleep for DMA to work!. */ + } else { + _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This + statement will block + until the CPU is + woken up by an + interrupt that sets + the wake up flag. */ + } + watchdog_start(); + ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU); + } +} +/*---------------------------------------------------------------------------*/ diff --git a/arch/platform/z1/z1-def.h b/arch/platform/z1/z1-def.h new file mode 100644 index 000000000..e59165ee3 --- /dev/null +++ b/arch/platform/z1/z1-def.h @@ -0,0 +1,287 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/** + * \file + * Platform configuration for the Z1 platform + * \author + * Joakim Eriksson + */ + +#ifndef Z1_DEF_H_ +#define Z1_DEF_H_ + +#include + +/* + * Definitions below are dictated by the hardware and not really + * changeable! + */ + +#define ZOLERTIA_Z1 1 /* Enric */ + +/* 1 len byte, 2 bytes CRC */ + #define RADIO_PHY_OVERHEAD 3 + /* 250kbps data rate. One byte = 32us */ + #define RADIO_BYTE_AIR_TIME 32 +/* Delay between GO signal and SFD: radio fixed delay + 4Bytes preample + 1B SFD -- 1Byte time is 32us + * ~327us + 129preample = 456 us */ +#define RADIO_DELAY_BEFORE_TX ((unsigned)US_TO_RTIMERTICKS(456)) +/* Delay between GO signal and start listening + * ~50us delay + 129preample + ?? = 183 us */ +#define RADIO_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(183)) +/* Delay between the SFD finishes arriving and it is detected in software */ +#define RADIO_DELAY_BEFORE_DETECT 0 + +#define PLATFORM_HAS_LEDS 1 +#define PLATFORM_HAS_BUTTON 1 +#define PLATFORM_HAS_RADIO 1 +#define PLATFORM_HAS_BATTERY 1 + +/* CPU target speed in Hz */ +#define F_CPU 8000000uL /* 8MHz by default */ + +/* the low-level radio driver */ +#define NETSTACK_CONF_RADIO cc2420_driver + +/* XXX Temporary place for defines that are lacking in mspgcc4's gpio.h */ +#ifdef __IAR_SYSTEMS_ICC__ +#ifndef P1SEL2_ +#define P1SEL2_ (0x0041u) /* Port 1 Selection 2*/ +DEFC(P1SEL2, P1SEL2_) +#endif +#ifndef P5SEL2_ +#define P5SEL2_ (0x0045u) /* Port 5 Selection 2*/ +DEFC(P5SEL2, P5SEL2_) +#endif +#else /* __IAR_SYSTEMS_ICC__ */ +#ifdef __GNUC__ +#ifndef P1SEL2_ +#define P1SEL2_ 0x0041 /* Port 1 Selection 2*/ +sfrb(P1SEL2, P1SEL2_); +#endif +#ifndef P5SEL2_ +#define P5SEL2_ 0x0045 /* Port 5 Selection 2*/ +sfrb(P5SEL2, P5SEL2_); +#endif +#endif /* __GNUC__ */ +#endif /* __IAR_SYSTEMS_ICC__ */ + +/* + * Definitions below are dictated by the hardware and not really + * changeable! + */ + +/* LED ports */ +#ifdef Z1_IS_Z1SP +#define LEDS_PxDIR P4DIR +#define LEDS_PxOUT P4OUT +#define LEDS_CONF_RED 0x04 +#define LEDS_CONF_GREEN 0x01 +#define LEDS_CONF_YELLOW 0x80 +#else +#define LEDS_PxDIR P5DIR +#define LEDS_PxOUT P5OUT +#define LEDS_CONF_RED 0x10 +#define LEDS_CONF_GREEN 0x40 +#define LEDS_CONF_YELLOW 0x20 +#endif /* Z1_IS_Z1SP */ + +#define LEDS_CONF_LEGACY_API 1 + +/* DCO speed resynchronization for more robust UART, etc. */ +#ifndef DCOSYNCH_CONF_ENABLED +#define DCOSYNCH_CONF_ENABLED (!(MAC_CONF_WITH_TSCH)) /* TSCH needs timerB +for SFD timestamping */ +#endif /* DCOSYNCH_CONF_ENABLED */ + +#ifndef CC2420_CONF_SFD_TIMESTAMPS +#define CC2420_CONF_SFD_TIMESTAMPS (MAC_CONF_WITH_TSCH) /* TSCH needs SFD timestamping */ +#endif /* CC2420_CONF_SFD_TIMESTAMPS */ + +#ifndef DCOSYNCH_CONF_PERIOD +#define DCOSYNCH_CONF_PERIOD 30 +#endif /* DCOSYNCH_CONF_PERIOD */ + +#define ROM_ERASE_UNIT_SIZE 512 +#define XMEM_ERASE_UNIT_SIZE (64 * 1024L) + +#define CFS_CONF_OFFSET_TYPE long + +/* Use the first 64k of external flash for node configuration */ +#define NODE_ID_XMEM_OFFSET (0 * XMEM_ERASE_UNIT_SIZE) + +/* Use the second 64k of external flash for codeprop. */ +#define EEPROMFS_ADDR_CODEPROP (1 * XMEM_ERASE_UNIT_SIZE) + +#define CFS_XMEM_CONF_OFFSET (2 * XMEM_ERASE_UNIT_SIZE) +#define CFS_XMEM_CONF_SIZE (1 * XMEM_ERASE_UNIT_SIZE) + +#define CFS_RAM_CONF_SIZE 4096 + +/* + * SPI bus configuration for the Z1 mote. + */ + +/* SPI input/output registers. */ +#define SPI_TXBUF UCB0TXBUF +#define SPI_RXBUF UCB0RXBUF + +/* USART0 Tx ready? */ +#define SPI_WAITFOREOTx() while((UCB0STAT & UCBUSY) != 0) +/* USART0 Rx ready? */ +#define SPI_WAITFOREORx() while((IFG2 & UCB0RXIFG) == 0) +/* USART0 Tx buffer ready? */ +#define SPI_WAITFORTxREADY() while((IFG2 & UCB0TXIFG) == 0) + +#define MOSI 1 /* P3.1 - Output: SPI Master out - slave in (MOSI) */ +#define MISO 2 /* P3.2 - Input: SPI Master in - slave out (MISO) */ +#define SCK 3 /* P3.3 - Output: SPI Serial Clock (SCLK) */ + +/* + * SPI bus - M25P80 external flash configuration. + */ +/* FLASH_PWR P4.3 Output ALWAYS POWERED ON Z1 */ +#define FLASH_CS 4 /* P4.4 Output */ +#define FLASH_HOLD 7 /* P5.7 Output */ + +/* Enable/disable flash access to the SPI bus (active low). */ + +#define SPI_FLASH_ENABLE() (P4OUT &= ~BV(FLASH_CS)) +#define SPI_FLASH_DISABLE() (P4OUT |= BV(FLASH_CS)) + +#define SPI_FLASH_HOLD() (P5OUT &= ~BV(FLASH_HOLD)) +#define SPI_FLASH_UNHOLD() (P5OUT |= BV(FLASH_HOLD)) + +/* + * SPI bus - CC2420 pin configuration. + */ + +#define CC2420_CONF_SYMBOL_LOOP_COUNT 1302 /* 326us msp430X @ 8MHz */ + +/* P1.2 - Input: FIFOP from CC2420 */ +#define CC2420_FIFOP_PORT(type) P1##type +#define CC2420_FIFOP_PIN 2 +/* P1.3 - Input: FIFO from CC2420 */ +#define CC2420_FIFO_PORT(type) P1##type +#define CC2420_FIFO_PIN 3 +/* P1.4 - Input: CCA from CC2420 */ +#define CC2420_CCA_PORT(type) P1##type +#define CC2420_CCA_PIN 4 +/* P4.1 - Input: SFD from CC2420 */ +#define CC2420_SFD_PORT(type) P4##type +#define CC2420_SFD_PIN 1 +/* P3.0 - Output: SPI Chip Select (CS_N) */ +#define CC2420_CSN_PORT(type) P3##type +#define CC2420_CSN_PIN 0 +/* P4.5 - Output: VREG_EN to CC2420 */ +#define CC2420_VREG_PORT(type) P4##type +#define CC2420_VREG_PIN 5 +/* P4.6 - Output: RESET_N to CC2420 */ +#define CC2420_RESET_PORT(type) P4##type +#define CC2420_RESET_PIN 6 + +#define CC2420_IRQ_VECTOR PORT1_VECTOR + +/* Pin status. */ +#define CC2420_FIFOP_IS_1 (!!(CC2420_FIFOP_PORT(IN) & BV(CC2420_FIFOP_PIN))) +#define CC2420_FIFO_IS_1 (!!(CC2420_FIFO_PORT(IN) & BV(CC2420_FIFO_PIN))) +#define CC2420_CCA_IS_1 (!!(CC2420_CCA_PORT(IN) & BV(CC2420_CCA_PIN))) +#define CC2420_SFD_IS_1 (!!(CC2420_SFD_PORT(IN) & BV(CC2420_SFD_PIN))) + +/* The CC2420 reset pin. */ +#define SET_RESET_INACTIVE() (CC2420_RESET_PORT(OUT) |= BV(CC2420_RESET_PIN)) +#define SET_RESET_ACTIVE() (CC2420_RESET_PORT(OUT) &= ~BV(CC2420_RESET_PIN)) + +/* CC2420 voltage regulator enable pin. */ +#define SET_VREG_ACTIVE() (CC2420_VREG_PORT(OUT) |= BV(CC2420_VREG_PIN)) +#define SET_VREG_INACTIVE() (CC2420_VREG_PORT(OUT) &= ~BV(CC2420_VREG_PIN)) + +/* CC2420 rising edge trigger for external interrupt 0 (FIFOP). */ +#define CC2420_FIFOP_INT_INIT() do { \ + CC2420_FIFOP_PORT(IES) &= ~BV(CC2420_FIFOP_PIN); \ + CC2420_CLEAR_FIFOP_INT(); \ +} while(0) + +/* FIFOP on external interrupt 0. */ +#define CC2420_ENABLE_FIFOP_INT() do { CC2420_FIFOP_PORT(IE) |= BV(CC2420_FIFOP_PIN); } while(0) +#define CC2420_DISABLE_FIFOP_INT() do { CC2420_FIFOP_PORT(IE) &= ~BV(CC2420_FIFOP_PIN); } while(0) +#define CC2420_CLEAR_FIFOP_INT() do { CC2420_FIFOP_PORT(IFG) &= ~BV(CC2420_FIFOP_PIN); } while(0) + +/* + * Enables/disables CC2420 access to the SPI bus (not the bus). + * (Chip Select) + */ + +/* ENABLE CSn (active low) */ +#define CC2420_SPI_ENABLE() (CC2420_CSN_PORT(OUT) &= ~BV(CC2420_CSN_PIN)) +/* DISABLE CSn (active low) */ +#define CC2420_SPI_DISABLE() (CC2420_CSN_PORT(OUT) |= BV(CC2420_CSN_PIN)) +#define CC2420_SPI_IS_ENABLED() ((CC2420_CSN_PORT(OUT) & BV(CC2420_CSN_PIN)) != BV(CC2420_CSN_PIN)) + +/* + * I2C configuration + */ + +#define I2C_PxDIR P5DIR +#define I2C_PxIN P5IN +#define I2C_PxOUT P5OUT +#define I2C_PxSEL P5SEL +#define I2C_PxSEL2 P5SEL2 +#define I2C_PxREN P5REN + +#define I2C_SDA (1 << 1) /* SDA == P5.1 */ +#define I2C_SCL (1 << 2) /* SCL == P5.2 */ +#define I2C_PRESC_1KHZ_LSB 0x00 +#define I2C_PRESC_1KHZ_MSB 0x20 +#define I2C_PRESC_100KHZ_LSB 0x50 +#define I2C_PRESC_100KHZ_MSB 0x00 +#define I2C_PRESC_400KHZ_LSB 0x14 +#define I2C_PRESC_400KHZ_MSB 0x00 + +/* Set rate as high as possible by default */ +#ifndef I2C_PRESC_Z1_LSB +#define I2C_PRESC_Z1_LSB I2C_PRESC_400KHZ_LSB +#endif + +#ifndef I2C_PRESC_Z1_MSB +#define I2C_PRESC_Z1_MSB I2C_PRESC_400KHZ_MSB +#endif + +/* I2C configuration with RX interrupts */ +#ifdef I2C_CONF_RX_WITH_INTERRUPT +#define I2C_RX_WITH_INTERRUPT I2C_CONF_RX_WITH_INTERRUPT +#else /* I2C_CONF_RX_WITH_INTERRUPT */ +#define I2C_RX_WITH_INTERRUPT 1 +#endif /* I2C_CONF_RX_WITH_INTERRUPT */ + +/* Platform-specific define for the end of the stack region */ +#define STACK_CONF_ORIGIN ((void *)0x3100) + +#endif /* PLATFORM_CONF_H_ */ diff --git a/arch/platform/zoul/Makefile.zoul b/arch/platform/zoul/Makefile.zoul index 38b86fd13..e65290b42 100644 --- a/arch/platform/zoul/Makefile.zoul +++ b/arch/platform/zoul/Makefile.zoul @@ -25,7 +25,7 @@ CFLAGS += -DDATE="\"`date +"%02u %02d %02m %02y %02H %02M %02S"`\"" ### Configure the build for the board and pull in board-specific sources CONTIKI_TARGET_DIRS += . dev CONTIKI_TARGET_DIRS += . $(BOARD) -PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET) +PLATFORM_ROOT_DIR = $(ARCH_PATH)/platform/$(TARGET) ### Include the board dir if one exists -include $(PLATFORM_ROOT_DIR)/$(BOARD)/Makefile.$(BOARD) @@ -39,30 +39,15 @@ CONTIKI_TARGET_SOURCEFILES += $(BOARD_SOURCEFILES) CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) ### Define the CPU directory -CONTIKI_CPU=$(CONTIKI)/arch/cpu/cc2538 +CONTIKI_CPU=$(ARCH_PATH)/cpu/cc2538 include $(CONTIKI_CPU)/Makefile.cc2538 MODULES += arch/dev/cc1200 arch/dev/rgb-led os/storage/cfs BSL = $(CONTIKI)/tools/cc2538-bsl/cc2538-bsl.py -### Use the specific Zoul subplatform to query for connected devices -ifdef MOTELIST_ZOLERTIA - MOTELIST_FLAGS += -b $(MOTELIST_ZOLERTIA) -endif - -### Detect if a mote is connected over serial port -ifeq ($(HOST_OS),Darwin) - USBDEVPREFIX= - MOTELIST := $(CONTIKI)/tools/zolertia/motelist-zolertia-macos - MOTES := $(shell $(MOTELIST) -c 2>&- | cut -f 2 -d ,) -else -### If we are not running under Mac, we assume Linux - USBDEVPREFIX= - MOTELIST := $(CONTIKI)/tools/zolertia/motelist-zolertia - MOTES := $(shell $(MOTELIST) -b $(MOTELIST_ZOLERTIA) -c 2>&- | cut -f 2 -d , | \ - perl -ne 'print $$1 . " " if(m-(/dev/\w+)-);') -endif +MOTES := $(shell python $(TOOLS_DIR)/motelist/motelist.py --omit-header \ + | grep $(MOTELIST_ZOLERTIA) | cut -f1 -d " ") ### If PORT is defined, override to keep backward compatibility ifdef PORT @@ -81,11 +66,12 @@ endif ### Variable that expands into a pattern rule to upload to a given MOTE. ### Requires $(MOTE) to be defined -### $$$$ Double escapes $s that need to be passed to the shell - once for when make parses UPLOAD_RULE, and once for when the expanded rule is parsed by make. +### $$$$ Double escapes $s that need to be passed to the shell - once for when +### make parses UPLOAD_RULE, and once for when the expanded rule is parsed by make. define UPLOAD_RULE -%.$(MOTE): %.bin %.elf +%.$(MOTE): $(OUT_BIN) $(OUT_ELF) @echo "Flashing $(MOTE)" - @BSL_ADDRESS=`$(OBJDUMP) -h $$*.elf | grep -B1 LOAD | \ + @BSL_ADDRESS=`$(OBJDUMP) -h $(BUILD_DIR_BOARD)/$$*.elf | grep -B1 LOAD | \ grep -Ev 'LOAD|\-\-' | awk '{print "0x" $$$$5}' | \ sort -g | head -1`; \ $(PYTHON) $(BSL) $(BSL_FLAGS) -b $(BSL_SPEED) -a $$$${BSL_ADDRESS} -p $(MOTE) $$< @@ -94,10 +80,8 @@ endef ### Create an upload rule for every MOTE connected $(foreach MOTE,$(MOTES),$(eval $(UPLOAD_RULE))) -motelist: - $(MOTELIST) -zoul-motelist: - $(MOTELIST) $(MOTELIST_FLAGS) +.PHONY: zoul-motes + zoul-motes: @echo $(MOTES) diff --git a/arch/platform/zoul/dev/cc1200-zoul-arch.c b/arch/platform/zoul/dev/cc1200-zoul-arch.c index 8c9d8cdd7..b0fd8dd96 100644 --- a/arch/platform/zoul/dev/cc1200-zoul-arch.c +++ b/arch/platform/zoul/dev/cc1200-zoul-arch.c @@ -76,18 +76,8 @@ /*---------------------------------------------------------------------------*/ #if DEBUG_CC1200_ARCH > 0 #define PRINTF(...) printf(__VA_ARGS__) -#define BUSYWAIT_UNTIL(cond, max_time) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) {} \ - if(!(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time)))) { \ - printf("ARCH: Timeout exceeded in line %d!\n", __LINE__); \ - } \ - } while(0) #else #define PRINTF(...) -#define BUSYWAIT_UNTIL(cond, max_time) while(!cond) #endif /*---------------------------------------------------------------------------*/ extern int cc1200_rx_interrupt(void); @@ -105,7 +95,7 @@ cc1200_arch_spi_select(void) /* Set CSn to low (0) */ GPIO_CLR_PIN(CC1200_SPI_CSN_PORT_BASE, CC1200_SPI_CSN_PIN_MASK); /* The MISO pin should go low before chip is fully enabled. */ - BUSYWAIT_UNTIL( + RTIMER_BUSYWAIT_UNTIL( GPIO_READ_PIN(CC1200_SPI_MISO_PORT_BASE, CC1200_SPI_MISO_PIN_MASK) == 0, RTIMER_SECOND / 100); } @@ -288,7 +278,7 @@ cc1200_arch_init(void) cc1200_arch_spi_deselect(); /* Ensure MISO is high */ - BUSYWAIT_UNTIL( + RTIMER_BUSYWAIT_UNTIL( GPIO_READ_PIN(CC1200_SPI_MISO_PORT_BASE, CC1200_SPI_MISO_PIN_MASK), RTIMER_SECOND / 10); } @@ -297,4 +287,3 @@ cc1200_arch_init(void) * @} * @} */ - diff --git a/arch/platform/zoul/dev/dht22.c b/arch/platform/zoul/dev/dht22.c index e5d14d53b..23174b2e6 100644 --- a/arch/platform/zoul/dev/dht22.c +++ b/arch/platform/zoul/dev/dht22.c @@ -51,15 +51,6 @@ #define PRINTF(...) #endif /*---------------------------------------------------------------------------*/ -#define BUSYWAIT_UNTIL(max_time) \ - do { \ - rtimer_clock_t t0; \ - t0 = RTIMER_NOW(); \ - while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) { \ - watchdog_periodic(); \ - } \ - } while(0) -/*---------------------------------------------------------------------------*/ #define DHT22_PORT_BASE GPIO_PORT_TO_BASE(DHT22_PORT) #define DHT22_PIN_MASK GPIO_PIN_MASK(DHT22_PIN) /*---------------------------------------------------------------------------*/ @@ -80,12 +71,12 @@ dht22_read(void) /* Exit low power mode and initialize variables */ GPIO_SET_OUTPUT(DHT22_PORT_BASE, DHT22_PIN_MASK); GPIO_SET_PIN(DHT22_PORT_BASE, DHT22_PIN_MASK); - BUSYWAIT_UNTIL(DHT22_AWAKE_TIME); + RTIMER_BUSYWAIT(DHT22_AWAKE_TIME); memset(dht22_data, 0, DHT22_BUFFER); /* Initialization sequence */ GPIO_CLR_PIN(DHT22_PORT_BASE, DHT22_PIN_MASK); - BUSYWAIT_UNTIL(DHT22_START_TIME); + RTIMER_BUSYWAIT(DHT22_START_TIME); GPIO_SET_PIN(DHT22_PORT_BASE, DHT22_PIN_MASK); clock_delay_usec(DHT22_READY_TIME); diff --git a/arch/platform/zoul/firefly-reva/Makefile.firefly-reva b/arch/platform/zoul/firefly-reva/Makefile.firefly-reva index afecf9fa5..bfbcb03fb 100644 --- a/arch/platform/zoul/firefly-reva/Makefile.firefly-reva +++ b/arch/platform/zoul/firefly-reva/Makefile.firefly-reva @@ -1,2 +1,2 @@ -MOTELIST_ZOLERTIA = firefly +MOTELIST_ZOLERTIA := Firefly BOARD_SOURCEFILES += board.c diff --git a/arch/platform/zoul/firefly/Makefile.firefly b/arch/platform/zoul/firefly/Makefile.firefly index afecf9fa5..bfbcb03fb 100644 --- a/arch/platform/zoul/firefly/Makefile.firefly +++ b/arch/platform/zoul/firefly/Makefile.firefly @@ -1,2 +1,2 @@ -MOTELIST_ZOLERTIA = firefly +MOTELIST_ZOLERTIA := Firefly BOARD_SOURCEFILES += board.c diff --git a/arch/platform/zoul/orion/Makefile.orion b/arch/platform/zoul/orion/Makefile.orion index ebc1bdad4..b7313afc0 100644 --- a/arch/platform/zoul/orion/Makefile.orion +++ b/arch/platform/zoul/orion/Makefile.orion @@ -1,4 +1,4 @@ -MOTELIST_ZOLERTIA = orion +MOTELIST_ZOLERTIA := Orion MODULES += arch/dev/enc28j60 CC2538_ENC28J60_ARCH ?= gpio ifeq ($(WITH_IP64),1) diff --git a/arch/platform/zoul/remote-reva/Makefile.remote-reva b/arch/platform/zoul/remote-reva/Makefile.remote-reva index fcccdf0f4..ec4095265 100644 --- a/arch/platform/zoul/remote-reva/Makefile.remote-reva +++ b/arch/platform/zoul/remote-reva/Makefile.remote-reva @@ -1,4 +1,4 @@ -MOTELIST_ZOLERTIA = remote +MOTELIST_ZOLERTIA := RE-Mote BOARD_SOURCEFILES += board.c antenna-sw.c mmc-arch.c rtcc.c power-mgmt.c MODULES += os/lib/fs/fat os/lib/fs/fat/option arch/platform/zoul/fs/fat arch/dev/disk/mmc diff --git a/arch/platform/zoul/remote-revb/Makefile.remote-revb b/arch/platform/zoul/remote-revb/Makefile.remote-revb index fcccdf0f4..ec4095265 100644 --- a/arch/platform/zoul/remote-revb/Makefile.remote-revb +++ b/arch/platform/zoul/remote-revb/Makefile.remote-revb @@ -1,4 +1,4 @@ -MOTELIST_ZOLERTIA = remote +MOTELIST_ZOLERTIA := RE-Mote BOARD_SOURCEFILES += board.c antenna-sw.c mmc-arch.c rtcc.c power-mgmt.c MODULES += os/lib/fs/fat os/lib/fs/fat/option arch/platform/zoul/fs/fat arch/dev/disk/mmc diff --git a/examples/6tisch/6p-packet/Makefile b/examples/6tisch/6p-packet/Makefile index ac92805ae..707a02485 100644 --- a/examples/6tisch/6p-packet/Makefile +++ b/examples/6tisch/6p-packet/Makefile @@ -1,8 +1,7 @@ CONTIKI_PROJECT = sixp-node PROJECT_SOURCEFILES += test-sf.c -PLATFORMS_EXCLUDE = sky nrf52dk native simplelink -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +PLATFORMS_EXCLUDE = sky z1 nrf52dk native simplelink CONTIKI = ../../../ diff --git a/examples/6tisch/channel-selection-demo/Makefile b/examples/6tisch/channel-selection-demo/Makefile index a44c76f10..e3c4288f6 100644 --- a/examples/6tisch/channel-selection-demo/Makefile +++ b/examples/6tisch/channel-selection-demo/Makefile @@ -3,8 +3,8 @@ all: $(CONTIKI_PROJECT) CONTIKI=../../.. -PLATFORMS_EXCLUDE = sky nrf52dk native -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +PLATFORMS_EXCLUDE = sky z1 nrf52dk native +BOARDS_EXCLUDE = srf06/cc13x0 launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 # The channel selection library MODULES += os/services/tsch-cs diff --git a/examples/6tisch/etsi-plugtest-2017/Makefile b/examples/6tisch/etsi-plugtest-2017/Makefile index b5aeb66f5..6b1a23a4c 100644 --- a/examples/6tisch/etsi-plugtest-2017/Makefile +++ b/examples/6tisch/etsi-plugtest-2017/Makefile @@ -1,8 +1,8 @@ CONTIKI_PROJECT = node all: $(CONTIKI_PROJECT) -PLATFORMS_EXCLUDE = sky nrf52dk native simplelink -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +PLATFORMS_EXCLUDE = sky z1 nrf52dk native simplelink +BOARDS_EXCLUDE = sensortag/cc2650 sensortag/cc1350 MAKE_WITH_SECURITY ?= 0 # force Security from command line ifeq ($(MAKE_WITH_SECURITY),1) diff --git a/examples/6tisch/etsi-plugtest-2017/README.md b/examples/6tisch/etsi-plugtest-2017/README.md index ab296bff8..7369918fa 100644 --- a/examples/6tisch/etsi-plugtest-2017/README.md +++ b/examples/6tisch/etsi-plugtest-2017/README.md @@ -18,7 +18,7 @@ The following hardwares were used in the event: * Zolertia Remote (TARGET=`zoul`, BOARD=`remote`) * JN156x (TARGET=`jn516`) -* CC2650 LaunchPad (TARGET=`srf06-cc26xx`, BOARD=`launchpad/cc2650`) +* CC2650 LaunchPad (TARGET=`cc26x0-cc13x0`, BOARD=`launchpad/cc2650`) ## Usage diff --git a/examples/6tisch/simple-node/Makefile b/examples/6tisch/simple-node/Makefile index c2e976b4e..06a83ebdd 100644 --- a/examples/6tisch/simple-node/Makefile +++ b/examples/6tisch/simple-node/Makefile @@ -2,7 +2,6 @@ CONTIKI_PROJECT = node all: $(CONTIKI_PROJECT) PLATFORMS_EXCLUDE = sky nrf52dk native simplelink -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 CONTIKI=../../.. diff --git a/examples/6tisch/simple-node/node.c b/examples/6tisch/simple-node/node.c index 381d81f8e..b8e29e7f6 100644 --- a/examples/6tisch/simple-node/node.c +++ b/examples/6tisch/simple-node/node.c @@ -60,7 +60,7 @@ PROCESS_THREAD(node_process, ev, data) is_coordinator = 0; -#if CONTIKI_TARGET_COOJA +#if CONTIKI_TARGET_COOJA || CONTIKI_TARGET_Z1 is_coordinator = (node_id == 1); #endif diff --git a/examples/6tisch/sixtop/Makefile b/examples/6tisch/sixtop/Makefile index 2469cd445..1e6f0ef5f 100644 --- a/examples/6tisch/sixtop/Makefile +++ b/examples/6tisch/sixtop/Makefile @@ -1,8 +1,7 @@ CONTIKI_PROJECT = node-sixtop all: $(CONTIKI_PROJECT) -PLATFORMS_EXCLUDE = sky nrf52dk native simplelink -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +PLATFORMS_EXCLUDE = sky z1 nrf52dk native simplelink PROJECT_SOURCEFILES += sf-simple.c CONTIKI=../../.. diff --git a/examples/6tisch/timesync-demo/Makefile b/examples/6tisch/timesync-demo/Makefile new file mode 100644 index 000000000..85c29659f --- /dev/null +++ b/examples/6tisch/timesync-demo/Makefile @@ -0,0 +1,24 @@ +CONTIKI_PROJECT = node +all: $(CONTIKI_PROJECT) + +CONTIKI=../../.. + +PLATFORMS_EXCLUDE = sky z1 nrf52dk native + +# force Orchestra from command line +MAKE_WITH_ORCHESTRA ?= 0 +# force Security from command line +MAKE_WITH_SECURITY ?= 0 + +MAKE_MAC = MAKE_MAC_TSCH +MODULES += os/services/shell + +ifeq ($(MAKE_WITH_ORCHESTRA),1) +MODULES += os/services/orchestra +endif + +ifeq ($(MAKE_WITH_SECURITY),1) +CFLAGS += -DWITH_SECURITY=1 +endif + +include $(CONTIKI)/Makefile.include diff --git a/examples/6tisch/timesync-demo/README.md b/examples/6tisch/timesync-demo/README.md new file mode 100644 index 000000000..cf130ee73 --- /dev/null +++ b/examples/6tisch/timesync-demo/README.md @@ -0,0 +1,18 @@ +This is a demonstration of application-level time synchronization using TSCH. + +In a TSCH network, all nodes are synchronized to a global time counter maintained +by the coordinator. This fact can be exploited to measure properties such as latency. + +The modes periodically send their detected network uptime to the coordinator. +The coordinator receives these packets, prints its local network uptime +and the time difference. This time difference is equal to the end-to-end latency, +which includes both the time to prepare the packet, the time until an appropriate +slot in the TSCH schedule, the over-the-air time (negligible). If the packet +does not arrive with the first attempt, it also includes the retransmission time. + +The nodes in this example do not have any notion of the wall-clock time. +That would need additional synchronization between the TSCH network and an external clock source. +For example, one can periodically distribute UNIX timestamps over the UART interface +on the border router to implement this feature. Alternatively, the data collected from +the TSCH network can be timestamped with just TSCH timestamps, and them on the external gateway +these timestamps could be converted to wall-clock time. \ No newline at end of file diff --git a/examples/6tisch/timesync-demo/node.c b/examples/6tisch/timesync-demo/node.c new file mode 100644 index 000000000..408421cec --- /dev/null +++ b/examples/6tisch/timesync-demo/node.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2015, SICS Swedish ICT. + * Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +/** + * \file + * A RPL+TSCH node demonstrating application-level time syncrhonization. + * + * \author Atis Elsts + * Simon Duquennoy + */ + +#include "contiki.h" +#include "net/routing/routing.h" +#include "net/netstack.h" +#include "net/ipv6/simple-udp.h" +#include "net/mac/tsch/tsch.h" +#include "lib/random.h" +#include "sys/node-id.h" + +#include "sys/log.h" +#define LOG_MODULE "App" +#define LOG_LEVEL LOG_LEVEL_INFO + +#define UDP_CLIENT_PORT 8765 +#define UDP_SERVER_PORT 5678 + +#define SEND_INTERVAL (60 * CLOCK_SECOND) +/*---------------------------------------------------------------------------*/ +static struct simple_udp_connection client_conn, server_conn; + +PROCESS(node_process, "RPL Node"); +AUTOSTART_PROCESSES(&node_process); +/*---------------------------------------------------------------------------*/ +static void +udp_rx_callback(struct simple_udp_connection *c, + const uip_ipaddr_t *sender_addr, + uint16_t sender_port, + const uip_ipaddr_t *receiver_addr, + uint16_t receiver_port, + const uint8_t *data, + uint16_t datalen) +{ + uint64_t local_time_clock_ticks = tsch_get_network_uptime_ticks(); + uint64_t remote_time_clock_ticks; + + if(datalen >= sizeof(remote_time_clock_ticks)) { + memcpy(&remote_time_clock_ticks, data, sizeof(remote_time_clock_ticks)); + + LOG_INFO("Received from "); + LOG_INFO_6ADDR(sender_addr); + LOG_INFO_(", created at %lu, now %lu, latency %lu clock ticks\n", + (unsigned long)remote_time_clock_ticks, + (unsigned long)local_time_clock_ticks, + (unsigned long)(local_time_clock_ticks - remote_time_clock_ticks)); + } +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(node_process, ev, data) +{ + static struct etimer periodic_timer; + int is_coordinator; + uip_ipaddr_t dest_ipaddr; + + PROCESS_BEGIN(); + + is_coordinator = 0; + +#if CONTIKI_TARGET_COOJA + is_coordinator = (node_id == 1); +#endif + + if(is_coordinator) { + NETSTACK_ROUTING.root_start(); + } + + /* Initialize UDP connections */ + simple_udp_register(&server_conn, UDP_SERVER_PORT, NULL, + UDP_CLIENT_PORT, udp_rx_callback); + simple_udp_register(&client_conn, UDP_CLIENT_PORT, NULL, + UDP_SERVER_PORT, NULL); + + NETSTACK_MAC.on(); + + etimer_set(&periodic_timer, random_rand() % SEND_INTERVAL); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); + + if(tsch_is_coordinator) { + break; + } + + if(NETSTACK_ROUTING.node_is_reachable() && NETSTACK_ROUTING.get_root_ipaddr(&dest_ipaddr)) { + /* Send network uptime timestamp to the DAG root */ + uint64_t network_uptime; + network_uptime = tsch_get_network_uptime_ticks(); + simple_udp_sendto(&client_conn, &network_uptime, sizeof(network_uptime), &dest_ipaddr); + LOG_INFO("Sent network uptime timestamp %lu to ", (unsigned long)network_uptime); + LOG_INFO_6ADDR(&dest_ipaddr); + LOG_INFO_("\n"); + } else { + LOG_INFO("Not reachable yet\n"); + } + + /* Add some jitter */ + etimer_set(&periodic_timer, SEND_INTERVAL + - CLOCK_SECOND + (random_rand() % (2 * CLOCK_SECOND))); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/examples/6tisch/timesync-demo/project-conf.h b/examples/6tisch/timesync-demo/project-conf.h new file mode 100644 index 000000000..f787b8dfe --- /dev/null +++ b/examples/6tisch/timesync-demo/project-conf.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2015, SICS Swedish ICT. + * Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \author Simon Duquennoy + * Atis Elsts + */ + +#ifndef __PROJECT_CONF_H__ +#define __PROJECT_CONF_H__ + +/* Set to enable TSCH security */ +#ifndef WITH_SECURITY +#define WITH_SECURITY 0 +#endif /* WITH_SECURITY */ + +/* USB serial takes space, free more space elsewhere */ +#define SICSLOWPAN_CONF_FRAG 0 +#define UIP_CONF_BUFFER_SIZE 160 + +/*******************************************************/ +/******************* Configure TSCH ********************/ +/*******************************************************/ + +/* IEEE802.15.4 PANID */ +#define IEEE802154_CONF_PANID 0x81a5 + +/* Do not start TSCH at init, wait for NETSTACK_MAC.on() */ +#define TSCH_CONF_AUTOSTART 0 + +/* 6TiSCH minimal schedule length. + * Larger values result in less frequent active slots: reduces capacity and saves energy. */ +#define TSCH_SCHEDULE_CONF_DEFAULT_LENGTH 3 + +#if WITH_SECURITY + +/* Enable security */ +#define LLSEC802154_CONF_ENABLED 1 + +#endif /* WITH_SECURITY */ + +/*******************************************************/ +/************* Other system configuration **************/ +/*******************************************************/ + +/* Logging */ +#define LOG_CONF_LEVEL_RPL LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_TCPIP LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_IPV6 LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_6LOWPAN LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_MAC LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_FRAMER LOG_LEVEL_WARN +#define TSCH_LOG_CONF_PER_SLOT 0 + +#endif /* __PROJECT_CONF_H__ */ diff --git a/examples/6tisch/tsch-stats/Makefile b/examples/6tisch/tsch-stats/Makefile index 0c8863301..156c0d526 100644 --- a/examples/6tisch/tsch-stats/Makefile +++ b/examples/6tisch/tsch-stats/Makefile @@ -3,8 +3,8 @@ all: $(CONTIKI_PROJECT) CONTIKI=../../.. -PLATFORMS_EXCLUDE = sky nrf52dk native -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +PLATFORMS_EXCLUDE = sky z1 nrf52dk native +BOARDS_EXCLUDE = srf06/cc13x0 launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 # force Orchestra from command line MAKE_WITH_ORCHESTRA ?= 0 diff --git a/examples/benchmarks/rpl-req-resp/Makefile b/examples/benchmarks/rpl-req-resp/Makefile index 11413ecd3..dadd7a2d7 100644 --- a/examples/benchmarks/rpl-req-resp/Makefile +++ b/examples/benchmarks/rpl-req-resp/Makefile @@ -1,8 +1,8 @@ CONTIKI_PROJECT = node all: $(CONTIKI_PROJECT) -PLATFORMS_EXCLUDE = sky nrf52dk native simplelink -BOARDS_EXCLUDE = srf06/cc13xx launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 +PLATFORMS_EXCLUDE = sky z1 nrf52dk native simplelink +BOARDS_EXCLUDE = srf06/cc13x0 launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350 MODULES_REL += ../testbeds MODULES += os/services/deployment diff --git a/examples/benchmarks/rpl-req-resp/node.c b/examples/benchmarks/rpl-req-resp/node.c index 4b92415f9..bc7552797 100644 --- a/examples/benchmarks/rpl-req-resp/node.c +++ b/examples/benchmarks/rpl-req-resp/node.c @@ -42,6 +42,8 @@ #include "contiki-net.h" #include "services/deployment/deployment.h" +#include + /* Log configuration */ #include "sys/log.h" #define LOG_MODULE "App" diff --git a/examples/benchmarks/rpl-req-resp/parse.py b/examples/benchmarks/rpl-req-resp/parse.py index a15b9c49b..dd36ef4c9 100644 --- a/examples/benchmarks/rpl-req-resp/parse.py +++ b/examples/benchmarks/rpl-req-resp/parse.py @@ -70,6 +70,9 @@ def parseRPL(log): if res: # This was the last line, commit full topology return {'event': 'topology' } + res = re.compile('initialized DAG').match(log) + if res: + return {'event': 'DAGinit' } return None def parseEnergest(log): @@ -123,6 +126,7 @@ def doParse(file): "ranks": [], "trickle": [], "switches": [], + "DAGinits": [], "topology": [], } @@ -178,6 +182,8 @@ def doParse(file): arrays["trickle"].append(entry) elif(ret['event'] == 'switch'): arrays["switches"].append(entry) + elif(ret['event'] == 'DAGinit'): + arrays["DAGinits"].append(entry) elif(ret['event'] == 'sending'): if not ret['message'] in arrays: arrays[ret['message']] = [] @@ -255,6 +261,7 @@ def main(): outputStats(dfs, "ranks", "rank", "mean", "RPL rank (ETX-128)") outputStats(dfs, "switches", "pswitch", "count", "RPL parent switches (#)") + outputStats(dfs, "DAGinits", "event", "count", "RPL joining DAG (#)") outputStats(dfs, "trickle", "trickle", "mean", "RPL Trickle period (min)") outputStats(dfs, "DIS", "message", "count", "RPL DIS sent (#)", "rpl-dis") diff --git a/examples/coap/coap-example-client/Makefile b/examples/coap/coap-example-client/Makefile index 1d3440990..21c9649e7 100644 --- a/examples/coap/coap-example-client/Makefile +++ b/examples/coap/coap-example-client/Makefile @@ -3,7 +3,7 @@ CONTIKI_PROJECT = coap-example-client all: $(CONTIKI_PROJECT) # Do not try to build on Sky because of code size limitation -PLATFORMS_EXCLUDE = sky +PLATFORMS_EXCLUDE = sky z1 # Include the CoAP implementation MODULES += os/net/app-layer/coap diff --git a/examples/coap/coap-example-server/Makefile b/examples/coap/coap-example-server/Makefile index 6d9e2db85..1647568cb 100644 --- a/examples/coap/coap-example-server/Makefile +++ b/examples/coap/coap-example-server/Makefile @@ -2,7 +2,7 @@ CONTIKI_PROJECT = coap-example-server all: $(CONTIKI_PROJECT) # Do not try to build on Sky because of code size limitation -PLATFORMS_EXCLUDE = sky +PLATFORMS_EXCLUDE = sky z1 # Include the CoAP implementation MODULES += os/net/app-layer/coap diff --git a/examples/coap/coap-example-server/resources/res-push.c b/examples/coap/coap-example-server/resources/res-push.c index ffc8c9c07..3056f3a0d 100644 --- a/examples/coap/coap-example-server/resources/res-push.c +++ b/examples/coap/coap-example-server/resources/res-push.c @@ -50,7 +50,7 @@ PERIODIC_RESOURCE(res_push, NULL, NULL, NULL, - 5 * CLOCK_SECOND, + 5000, res_periodic_handler); /* diff --git a/examples/coap/coap-example-server/resources/res-temperature.c b/examples/coap/coap-example-server/resources/res-temperature.c index ac34dbe33..5616924a6 100644 --- a/examples/coap/coap-example-server/resources/res-temperature.c +++ b/examples/coap/coap-example-server/resources/res-temperature.c @@ -66,7 +66,7 @@ PERIODIC_RESOURCE(res_temperature, NULL, NULL, NULL, - CLOCK_SECOND, + 1000, res_periodic_handler); static void diff --git a/examples/coap/coap-plugtest-server/resources/res-plugtest-obs.c b/examples/coap/coap-plugtest-server/resources/res-plugtest-obs.c index 7d191e751..3200d1d88 100644 --- a/examples/coap/coap-plugtest-server/resources/res-plugtest-obs.c +++ b/examples/coap/coap-plugtest-server/resources/res-plugtest-obs.c @@ -58,7 +58,7 @@ PERIODIC_RESOURCE(res_plugtest_obs, NULL, res_put_handler, res_delete_handler, - 5 * CLOCK_SECOND, + 5000, res_periodic_handler); static int32_t obs_counter = 0; diff --git a/examples/coap/coap-plugtest-server/resources/res-plugtest-separate.c b/examples/coap/coap-plugtest-server/resources/res-plugtest-separate.c index c0ae81d11..bbcfc9d77 100644 --- a/examples/coap/coap-plugtest-server/resources/res-plugtest-separate.c +++ b/examples/coap/coap-plugtest-server/resources/res-plugtest-separate.c @@ -57,7 +57,7 @@ PERIODIC_RESOURCE(res_plugtest_separate, NULL, NULL, NULL, - 3 * CLOCK_SECOND, + 3000, res_resume_handler); /* A structure to store the required information */ diff --git a/examples/dev/button-hal/Makefile b/examples/dev/button-hal/Makefile index b2a73fe3c..263e9ec65 100644 --- a/examples/dev/button-hal/Makefile +++ b/examples/dev/button-hal/Makefile @@ -3,6 +3,6 @@ CONTIKI = ../../.. all: $(CONTIKI_PROJECT) -PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul native simplelink +PLATFORMS_ONLY = cc26x0-cc13x0 cc2538dk openmote-cc2538 zoul native simplelink include $(CONTIKI)/Makefile.include diff --git a/examples/dev/gpio-hal/Makefile b/examples/dev/gpio-hal/Makefile index 70f38a11a..234388c09 100644 --- a/examples/dev/gpio-hal/Makefile +++ b/examples/dev/gpio-hal/Makefile @@ -1,7 +1,7 @@ CONTIKI_PROJECT = gpio-hal-example CONTIKI = ../../.. -PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul native simplelink +PLATFORMS_ONLY = cc26x0-cc13x0 cc2538dk openmote-cc2538 zoul native simplelink include $(CONTIKI)/Makefile.identify-target diff --git a/examples/dev/gpio-hal/srf06-cc26xx/pins.c b/examples/dev/gpio-hal/cc26x0-cc13x0/pins.c similarity index 100% rename from examples/dev/gpio-hal/srf06-cc26xx/pins.c rename to examples/dev/gpio-hal/cc26x0-cc13x0/pins.c diff --git a/examples/dev/gpio-hal/gpio-hal-example.c b/examples/dev/gpio-hal/gpio-hal-example.c index 380c06388..75137e26a 100644 --- a/examples/dev/gpio-hal/gpio-hal-example.c +++ b/examples/dev/gpio-hal/gpio-hal-example.c @@ -46,9 +46,9 @@ static uint8_t counter; /*---------------------------------------------------------------------------*/ /* Print gpio_hal_pin_mask_t using the correct format */ #if GPIO_HAL_PIN_COUNT > 32 -#define PIN_MASK_FMT PRIx64 +#define PIN_MASK_FMT "0x%016" PRIx64 #else -#define PIN_MASK_FMT PRIx32 +#define PIN_MASK_FMT "0x%08" PRIx32 #endif /*---------------------------------------------------------------------------*/ PROCESS(gpio_hal_example, "GPIO HAL Example"); @@ -127,7 +127,7 @@ PROCESS_THREAD(gpio_hal_example, ev, data) } /* Test read */ - printf("%u: Pins are 1-%u, 2=%u, 3=%u, mask=0x%08" PIN_MASK_FMT "\n", + printf("%u: Pins are 1-%u, 2=%u, 3=%u, mask=" PIN_MASK_FMT "\n", counter & 7, gpio_hal_arch_read_pin(out_pin1), gpio_hal_arch_read_pin(out_pin2), diff --git a/examples/libs/ipv6-hooks/ipv6-hooks.c b/examples/libs/ipv6-hooks/ipv6-hooks.c index d3ca8861c..8d8561049 100644 --- a/examples/libs/ipv6-hooks/ipv6-hooks.c +++ b/examples/libs/ipv6-hooks/ipv6-hooks.c @@ -10,8 +10,6 @@ #define LOG_MODULE "App" #define LOG_LEVEL LOG_LEVEL_INFO -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - /*---------------------------------------------------------------------------*/ PROCESS(ipv6_hooks_process, "IPv6 Hooks"); AUTOSTART_PROCESSES(&ipv6_hooks_process); diff --git a/examples/libs/ipv6-uipbuf/udp-client.c b/examples/libs/ipv6-uipbuf/udp-client.c index bee1cda83..5616874b5 100644 --- a/examples/libs/ipv6-uipbuf/udp-client.c +++ b/examples/libs/ipv6-uipbuf/udp-client.c @@ -12,9 +12,6 @@ #define UDP_CLIENT_PORT 8765 #define UDP_SERVER_PORT 5678 -static struct simple_udp_connection udp_conn; - -#define START_INTERVAL (15 * CLOCK_SECOND) #define SEND_INTERVAL (60 * CLOCK_SECOND) static struct simple_udp_connection udp_conn; diff --git a/examples/libs/timers/all-timers.c b/examples/libs/timers/all-timers.c index 2a1f27b28..bb6dfed90 100644 --- a/examples/libs/timers/all-timers.c +++ b/examples/libs/timers/all-timers.c @@ -79,7 +79,7 @@ PROCESS_THREAD(timer_process, ev, data) PROCESS_BEGIN(); ctimer_set(&timer_ctimer, CLOCK_SECOND, ctimer_callback, NULL); - rtimer_set(&timer_rtimer, RTIMER_NOW() + CLOCK_SECOND / 2, 0, + rtimer_set(&timer_rtimer, RTIMER_NOW() + RTIMER_SECOND / 2, 0, rtimer_callback, NULL); while(1) { diff --git a/examples/lwm2m-ipso-objects/Makefile b/examples/lwm2m-ipso-objects/Makefile index c2cef2cfd..a8d461859 100644 --- a/examples/lwm2m-ipso-objects/Makefile +++ b/examples/lwm2m-ipso-objects/Makefile @@ -2,7 +2,7 @@ CONTIKI_PROJECT = example-ipso-objects CONTIKI_SOURCEFILES += serial-protocol.c example-ipso-temperature.c -PLATFORMS_EXCLUDE = sky +PLATFORMS_EXCLUDE = sky z1 all: $(CONTIKI_PROJECT) diff --git a/examples/lwm2m-ipso-objects/example-ipso-objects.c b/examples/lwm2m-ipso-objects/example-ipso-objects.c index 51bd84040..60ca4bdb9 100644 --- a/examples/lwm2m-ipso-objects/example-ipso-objects.c +++ b/examples/lwm2m-ipso-objects/example-ipso-objects.c @@ -34,6 +34,7 @@ * \author * Joakim Eriksson, joakime@sics.se * Niclas Finne, nfi@sics.se + * Carlos Gonzalo Peces, carlosgp143@gmail.com */ #include "contiki.h" @@ -51,18 +52,28 @@ #define DEBUG DEBUG_NONE #include "net/ipv6/uip-debug.h" +/* Define this macro to non-zero to register via a bootstrap server */ #ifndef REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER #define REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER 0 #endif -#ifndef REGISTER_WITH_LWM2M_SERVER -#define REGISTER_WITH_LWM2M_SERVER 1 +#if REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER +#define SERVER_TYPE LWM2M_RD_CLIENT_BOOTSTRAP_SERVER +#else +#define SERVER_TYPE LWM2M_RD_CLIENT_LWM2M_SERVER #endif #ifndef LWM2M_SERVER_ADDRESS #define LWM2M_SERVER_ADDRESS "coap://[fd00::1]" #endif +static lwm2m_session_info_t session_info; + +/* Define this macro to register with a second LWM2M server */ +#ifdef LWM2M_SERVER_ADDRESS_SECOND +static lwm2m_session_info_t session_info_second; +#endif + #if BOARD_SENSORTAG #include "board-peripherals.h" @@ -148,13 +159,17 @@ setup_lwm2m_servers(void) coap_endpoint_t server_ep; if(coap_endpoint_parse(LWM2M_SERVER_ADDRESS, strlen(LWM2M_SERVER_ADDRESS), &server_ep) != 0) { - lwm2m_rd_client_register_with_bootstrap_server(&server_ep); - lwm2m_rd_client_register_with_server(&server_ep); + lwm2m_rd_client_register_with_server(&session_info, &server_ep, SERVER_TYPE); } #endif /* LWM2M_SERVER_ADDRESS */ - lwm2m_rd_client_use_bootstrap_server(REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER); - lwm2m_rd_client_use_registration_server(REGISTER_WITH_LWM2M_SERVER); +#ifdef LWM2M_SERVER_ADDRESS_SECOND + coap_endpoint_t server_ep_second; + if(coap_endpoint_parse(LWM2M_SERVER_ADDRESS_SECOND, strlen(LWM2M_SERVER_ADDRESS_SECOND), + &server_ep_second) != 0) { + lwm2m_rd_client_register_with_server(&session_info_second, &server_ep_second, SERVER_TYPE); + } +#endif /* LWM2M_SERVER_ADDRESS_SECOND */ } /*---------------------------------------------------------------------------*/ PROCESS_THREAD(example_ipso_objects, ev, data) diff --git a/examples/mqtt-client/Makefile b/examples/mqtt-client/Makefile index a9ab11613..29b3267e1 100644 --- a/examples/mqtt-client/Makefile +++ b/examples/mqtt-client/Makefile @@ -8,6 +8,6 @@ CONTIKI = ../.. MODULES_REL += arch/platform/$(TARGET) -PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul native simplelink +PLATFORMS_ONLY = cc26x0-cc13x0 cc2538dk openmote-cc2538 zoul native simplelink include $(CONTIKI)/Makefile.include diff --git a/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c b/examples/mqtt-client/arch/cpu/cc26x0-cc13x0/builtin-sensors.c similarity index 100% rename from examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c rename to examples/mqtt-client/arch/cpu/cc26x0-cc13x0/builtin-sensors.c diff --git a/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h b/examples/mqtt-client/arch/cpu/cc26x0-cc13x0/builtin-sensors.h similarity index 100% rename from examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h rename to examples/mqtt-client/arch/cpu/cc26x0-cc13x0/builtin-sensors.h diff --git a/examples/mqtt-client/arch/platform/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 b/examples/mqtt-client/arch/platform/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 new file mode 100644 index 000000000..7c74123c6 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc26x0-cc13x0 diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/module-macros.h b/examples/mqtt-client/arch/platform/cc26x0-cc13x0/module-macros.h similarity index 100% rename from examples/mqtt-client/arch/platform/srf06-cc26xx/module-macros.h rename to examples/mqtt-client/arch/platform/cc26x0-cc13x0/module-macros.h diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/cc26x0-cc13x0/mqtt-client-extensions.c similarity index 100% rename from examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c rename to examples/mqtt-client/arch/platform/cc26x0-cc13x0/mqtt-client-extensions.c diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx b/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx deleted file mode 100644 index dcec87bad..000000000 --- a/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx +++ /dev/null @@ -1 +0,0 @@ -MODULES_REL += arch/cpu/cc26xx-cc13xx diff --git a/examples/mqtt-client/mqtt-client.c b/examples/mqtt-client/mqtt-client.c index be3c97cdb..b71a193ea 100644 --- a/examples/mqtt-client/mqtt-client.c +++ b/examples/mqtt-client/mqtt-client.c @@ -47,7 +47,11 @@ #include /*---------------------------------------------------------------------------*/ #define LOG_MODULE "mqtt-client" +#ifdef MQTT_CLIENT_CONF_LOG_LEVEL +#define LOG_LEVEL MQTT_CLIENT_CONF_LOG_LEVEL +#else #define LOG_LEVEL LOG_LEVEL_NONE +#endif /*---------------------------------------------------------------------------*/ /* Controls whether the example will work in IBM Watson IoT platform mode */ #ifdef MQTT_CLIENT_CONF_WITH_IBM_WATSON @@ -305,6 +309,7 @@ pub_handler(const char *topic, uint16_t topic_len, const uint8_t *chunk, } if(strncmp(&topic[10], "leds", 4) == 0) { + LOG_DBG("Received MQTT SUB\n"); if(chunk[0] == '1') { leds_on(LEDS_RED); } else if(chunk[0] == '0') { diff --git a/examples/mqtt-client/project-conf.h b/examples/mqtt-client/project-conf.h index 66ba909ce..6b011f7b0 100644 --- a/examples/mqtt-client/project-conf.h +++ b/examples/mqtt-client/project-conf.h @@ -52,7 +52,9 @@ * devices, set your Org ID here and then make sure you set the correct token * through MQTT_CLIENT_CONF_AUTH_TOKEN. */ +#ifndef MQTT_CLIENT_CONF_ORG_ID #define MQTT_CLIENT_CONF_ORG_ID "quickstart" +#endif /* * The MQTT username. diff --git a/examples/multicast/Makefile b/examples/multicast/Makefile index 86c16e481..3e8a88d4a 100644 --- a/examples/multicast/Makefile +++ b/examples/multicast/Makefile @@ -3,8 +3,8 @@ all: $(CONTIKI_PROJECT) # nrf52dk only supports slave mode, i.e., with no routing PLATFORMS_EXCLUDE = nrf52dk -# does not fit sky motes -PLATFORMS_EXCLUDE += sky +# does not fit on sky and z1 motes +PLATFORMS_EXCLUDE += sky z1 CONTIKI = ../.. diff --git a/examples/multicast/srf06-cc26xx/module-macros.h b/examples/multicast/cc26x0-cc13x0/module-macros.h similarity index 100% rename from examples/multicast/srf06-cc26xx/module-macros.h rename to examples/multicast/cc26x0-cc13x0/module-macros.h diff --git a/examples/multicast/sink.c b/examples/multicast/sink.c index 34ad6009d..2b6f48846 100644 --- a/examples/multicast/sink.c +++ b/examples/multicast/sink.c @@ -55,8 +55,6 @@ static struct uip_udp_conn *sink_conn; static uint16_t count; -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #if !NETSTACK_CONF_WITH_IPV6 || !UIP_CONF_ROUTER || !UIP_IPV6_MULTICAST || !UIP_CONF_IPV6_RPL #error "This example can not work with the current contiki configuration" #error "Check the values of: NETSTACK_CONF_WITH_IPV6, UIP_CONF_ROUTER, UIP_CONF_IPV6_RPL" @@ -82,9 +80,10 @@ join_mcast_group(void) { uip_ipaddr_t addr; uip_ds6_maddr_t *rv; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); /* First, set our v6 global */ - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&addr, default_prefix); uip_ds6_set_addr_iid(&addr, &uip_lladdr); uip_ds6_addr_add(&addr, 0, ADDR_AUTOCONF); diff --git a/examples/platform-specific/cc26xx/Makefile b/examples/platform-specific/cc26x0-cc13x0/Makefile similarity index 57% rename from examples/platform-specific/cc26xx/Makefile rename to examples/platform-specific/cc26x0-cc13x0/Makefile index 40e484bcc..bd9600c78 100644 --- a/examples/platform-specific/cc26xx/Makefile +++ b/examples/platform-specific/cc26x0-cc13x0/Makefile @@ -1,6 +1,6 @@ -CONTIKI_PROJECT = cc26xx-demo +CONTIKI_PROJECT = cc26x0-demo -PLATFORMS_ONLY = srf06-cc26xx +PLATFORMS_ONLY = cc26x0-cc13x0 all: $(CONTIKI_PROJECT) diff --git a/examples/platform-specific/cc26x0-cc13x0/Makefile.target b/examples/platform-specific/cc26x0-cc13x0/Makefile.target new file mode 100644 index 000000000..bd54a4fc7 --- /dev/null +++ b/examples/platform-specific/cc26x0-cc13x0/Makefile.target @@ -0,0 +1 @@ +TARGET = cc26x0-cc13x0 diff --git a/examples/platform-specific/cc26xx/README.md b/examples/platform-specific/cc26x0-cc13x0/README.md similarity index 100% rename from examples/platform-specific/cc26xx/README.md rename to examples/platform-specific/cc26x0-cc13x0/README.md diff --git a/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/Makefile b/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/Makefile new file mode 100644 index 000000000..052927896 --- /dev/null +++ b/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/Makefile @@ -0,0 +1,11 @@ +CONTIKI_PROJECT=client + +all: $(CONTIKI_PROJECT) + +PLATFORMS_ONLY = cc26x0-cc13x0 +BOARDS_ONLY = launchpad/cc2650 sensortag/cc2650 srf06/cc26x0 + +MAKE_MAC = MAKE_MAC_BLE +MAKE_NET = MAKE_NET_IPV6 +CONTIKI = ../../../.. +include $(CONTIKI)/Makefile.include diff --git a/examples/platform-specific/cc26xx/ble-ipv6/README.md b/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/README.md similarity index 95% rename from examples/platform-specific/cc26xx/ble-ipv6/README.md rename to examples/platform-specific/cc26x0-cc13x0/ble-ipv6/README.md index d33c7329f..078667264 100644 --- a/examples/platform-specific/cc26xx/ble-ipv6/README.md +++ b/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/README.md @@ -28,12 +28,12 @@ It has been tested on the TI CC2650 SensorTag and the TI CC2650 LaunchPad hardwa The IPv6-over-BLE stack comes with the following modules: ### BLE radio -The implementation of the BLE radio for the TI CC26xx platform is implemented in `arch/cpu/cc26xx-cc13xx/rf-core/ble-cc2650.c` -and `arch/cpu/cc26xx-cc13xx/rf-core/ble-hal/*.[ch]`. +The implementation of the BLE radio for the TI CC26xx platform is implemented in `arch/cpu/cc26x0-cc13x0/rf-core/ble-cc2650.c` +and `arch/cpu/cc26x0-cc13x0/rf-core/ble-hal/*.[ch]`. These files contain all the hardware specific code for supporting BLE as a link layer. ### BLE L2CAP layer -The L2CAP LE credit-based flow control support is implemented in `arch/cpu/cc26xx-cc13xx/rf-core/ble-l2cap.c`. +The L2CAP LE credit-based flow control support is implemented in `arch/cpu/cc26x0-cc13x0/rf-core/ble-l2cap.c`. Besides implementing rudimentary L2CAP support, this module handles fragmentation of large IPv6 packets. ## Using BLEach diff --git a/examples/platform-specific/cc26xx/ble-ipv6/client.c b/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/client.c similarity index 100% rename from examples/platform-specific/cc26xx/ble-ipv6/client.c rename to examples/platform-specific/cc26x0-cc13x0/ble-ipv6/client.c diff --git a/examples/platform-specific/cc26xx/ble-ipv6/project-conf.h b/examples/platform-specific/cc26x0-cc13x0/ble-ipv6/project-conf.h similarity index 100% rename from examples/platform-specific/cc26xx/ble-ipv6/project-conf.h rename to examples/platform-specific/cc26x0-cc13x0/ble-ipv6/project-conf.h diff --git a/examples/platform-specific/cc26xx/cc26xx-demo.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-demo.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-demo.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-demo.c index 81e99f3fa..1b71feeb7 100644 --- a/examples/platform-specific/cc26xx/cc26xx-demo.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-demo.c @@ -42,7 +42,7 @@ * Example project demonstrating the CC13xx/CC26xx platforms * * This example will work for the following boards: - * - srf06-cc26xx: SmartRF06EB + CC13xx/CC26xx EM + * - cc26x0-cc13x0: SmartRF06EB + CC13xx/CC26xx EM * - CC2650 and CC1350 SensorTag * - CC1310, CC1350, CC2650 LaunchPads * diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/Makefile b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/Makefile similarity index 87% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/Makefile rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/Makefile index 05dbface1..6d70a3a24 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/Makefile +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/Makefile @@ -1,7 +1,7 @@ -CONTIKI_PROJECT = cc26xx-web-demo +CONTIKI_PROJECT = cc26x0-web-demo all: $(CONTIKI_PROJECT) -PLATFORMS_ONLY = srf06-cc26xx +PLATFORMS_ONLY = cc26x0-cc13x0 MODULES_REL += ./resources diff --git a/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/Makefile.target b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/Makefile.target new file mode 100644 index 000000000..bd54a4fc7 --- /dev/null +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/Makefile.target @@ -0,0 +1 @@ +TARGET = cc26x0-cc13x0 diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/README.md b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/README.md similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/README.md rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/README.md index 912fcb72a..6a85ee0b5 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/README.md +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/README.md @@ -180,6 +180,6 @@ parser into the firmware. IBM Watson IoT Platform ---------------------------- -To use IBM Watson IoT Platform, you have to go to SECURITY tab of Device page to select "TLS Optional". This step is critical. If you don't do this, you need to use TLS for connection and default cc26xx-web-demo won't work. +To use IBM Watson IoT Platform, you have to go to SECURITY tab of Device page to select "TLS Optional". This step is critical. If you don't do this, you need to use TLS for connection and default cc26x0-web-demo won't work. ![IBM Watson IoT Platform TLS Optional Configuration](img/ibm-watson-iot-platform-tls-optional.png) diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-web-demo.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-web-demo.c index 3944a58b2..6333d5ad5 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-web-demo.c @@ -28,7 +28,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -48,7 +48,7 @@ #include "dev/button-hal.h" #include "batmon-sensor.h" #include "httpd-simple.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include "mqtt-client.h" #include "coap-server.h" diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-web-demo.h similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-web-demo.h index 0761725c7..e7252496c 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-web-demo.h @@ -31,7 +31,7 @@ * \addtogroup cc26xx-examples * @{ * - * \defgroup cc26xx-web-demo CC26xx Web Demo + * \defgroup cc26x0-web-demo CC26xx Web Demo * @{ * * An example demonstrating: @@ -119,7 +119,7 @@ /*---------------------------------------------------------------------------*/ /* Default configuration values */ #define CC26XX_WEB_DEMO_DEFAULT_ORG_ID "quickstart" -#if CPU_FAMILY_CC13XX +#if CPU_FAMILY_CC13X0 #define CC26XX_WEB_DEMO_DEFAULT_TYPE_ID "cc13xx" #else #define CC26XX_WEB_DEMO_DEFAULT_TYPE_ID "cc26xx" diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/cetic-6lbr-client.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cetic-6lbr-client.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/cetic-6lbr-client.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cetic-6lbr-client.c index 569a36a89..fc8044380 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/cetic-6lbr-client.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cetic-6lbr-client.c @@ -24,7 +24,7 @@ * SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/coap-server.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/coap-server.c index 37b9a295d..bda7eb8ff 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/coap-server.c @@ -28,7 +28,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -40,7 +40,7 @@ #include "coap-engine.h" #include "board-peripherals.h" #include "rf-core/rf-ble.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include #include diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.h b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/coap-server.h similarity index 98% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.h rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/coap-server.h index 7399597c8..0d8c1d40b 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/coap-server.h +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/coap-server.h @@ -28,7 +28,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/httpd-simple.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/httpd-simple.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/httpd-simple.c index e23e78efe..101b6441b 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/httpd-simple.c @@ -29,7 +29,7 @@ * */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -38,11 +38,12 @@ /*---------------------------------------------------------------------------*/ #include "contiki.h" #include "httpd-simple.h" +#include "net/ipv6/uip-ds6-nbr.h" #include "net/ipv6/uip-ds6-route.h" #include "batmon-sensor.h" #include "lib/sensors.h" #include "lib/list.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include "mqtt-client.h" #include "net-uart.h" @@ -436,8 +437,8 @@ PT_THREAD(generate_index(struct httpd_state *s)) PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, SECTION_OPEN "Neighbors" CONTENT_OPEN)); - for(s->nbr = nbr_table_head(ds6_neighbors); s->nbr != NULL; - s->nbr = nbr_table_next(ds6_neighbors, s->nbr)) { + for(s->nbr = uip_ds6_nbr_head(); s->nbr != NULL; + s->nbr = uip_ds6_nbr_next(s->nbr)) { PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "\n")); diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/httpd-simple.h b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/httpd-simple.h similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/httpd-simple.h rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/httpd-simple.h index 25b8db3e5..2b04b264c 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/httpd-simple.h +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/httpd-simple.h @@ -44,7 +44,7 @@ /*---------------------------------------------------------------------------*/ #include "contiki-net.h" #include "sys/process.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" /*---------------------------------------------------------------------------*/ /* Ideally a multiple of TCP_MSS */ #ifdef HTTPD_SIMPLE_CONF_MAIN_BUF_SIZE diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/img/6lbr-web.png b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/6lbr-web.png similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/img/6lbr-web.png rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/6lbr-web.png diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/img/ibm-watson-iot-platform-tls-optional.png b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/ibm-watson-iot-platform-tls-optional.png similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/img/ibm-watson-iot-platform-tls-optional.png rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/ibm-watson-iot-platform-tls-optional.png diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/img/quickstart-sensortag.png b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/quickstart-sensortag.png similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/img/quickstart-sensortag.png rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/quickstart-sensortag.png diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/img/sensor-readings-config.png b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/sensor-readings-config.png similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/img/sensor-readings-config.png rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/sensor-readings-config.png diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/img/well-known-core.png b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/well-known-core.png similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/img/well-known-core.png rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/img/well-known-core.png diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/mqtt-client.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/mqtt-client.c index 7562fb89a..0250de322 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/mqtt-client.c @@ -28,7 +28,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -45,7 +45,7 @@ #include "lib/sensors.h" #include "dev/button-hal.h" #include "board-peripherals.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include "dev/leds.h" #include "mqtt-client.h" #include "httpd-simple.h" diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.h b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/mqtt-client.h similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.h rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/mqtt-client.h index ab7c08227..68b26af6d 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.h +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/mqtt-client.h @@ -28,7 +28,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/net-uart.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/net-uart.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/net-uart.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/net-uart.c index ca3bf8c82..a2ea899da 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/net-uart.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/net-uart.c @@ -28,7 +28,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/net-uart.h b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/net-uart.h similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/net-uart.h rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/net-uart.h diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/project-conf.h b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/project-conf.h similarity index 100% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/project-conf.h rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/project-conf.h diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-ble-advd.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-ble-advd.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-ble-advd.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-ble-advd.c index a94a0b7a0..b32cb82ec 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-ble-advd.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-ble-advd.c @@ -28,7 +28,7 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-device.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-device.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-device.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-device.c index 46344d1ef..bc61b8510 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-device.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-device.c @@ -29,7 +29,7 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -41,7 +41,7 @@ #include "coap.h" #include "sys/clock.h" #include "coap-server.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include "ti-lib.h" diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-leds.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-leds.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-leds.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-leds.c index 6c396145c..538ecc0bd 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-leds.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-leds.c @@ -29,7 +29,7 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-net.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-net.c similarity index 98% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-net.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-net.c index 81575f345..43a4da354 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-net.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-net.c @@ -29,7 +29,7 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -41,7 +41,7 @@ #include "coap.h" #include "net/ipv6/uip-ds6.h" #include "coap-server.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include "ti-lib.h" diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-sensors.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-sensors.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-sensors.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-sensors.c index 9ccc79e97..1852f7cb6 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-sensors.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-sensors.c @@ -29,7 +29,7 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file @@ -39,7 +39,7 @@ #include "contiki.h" #include "coap-engine.h" #include "coap.h" -#include "cc26xx-web-demo.h" +#include "cc26x0-web-demo.h" #include "coap-server.h" #include diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-toggle-leds.c b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-toggle-leds.c similarity index 99% rename from examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-toggle-leds.c rename to examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-toggle-leds.c index 1c708b539..00e632cef 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/resources/res-toggle-leds.c +++ b/examples/platform-specific/cc26x0-cc13x0/cc26x0-web-demo/resources/res-toggle-leds.c @@ -31,7 +31,7 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup cc26xx-web-demo + * \addtogroup cc26x0-web-demo * @{ * * \file diff --git a/examples/platform-specific/cc26xx/project-conf.h b/examples/platform-specific/cc26x0-cc13x0/project-conf.h similarity index 100% rename from examples/platform-specific/cc26xx/project-conf.h rename to examples/platform-specific/cc26x0-cc13x0/project-conf.h diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/Makefile b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/Makefile similarity index 83% rename from examples/platform-specific/cc26xx/very-sleepy-demo/Makefile rename to examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/Makefile index 46da822d3..d62dff3e3 100644 --- a/examples/platform-specific/cc26xx/very-sleepy-demo/Makefile +++ b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/Makefile @@ -1,6 +1,6 @@ CONTIKI_PROJECT = very-sleepy-demo -PLATFORMS_ONLY = srf06-cc26xx +PLATFORMS_ONLY = cc26x0-cc13x0 all: $(CONTIKI_PROJECT) diff --git a/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/Makefile.target b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/Makefile.target new file mode 100644 index 000000000..bd54a4fc7 --- /dev/null +++ b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/Makefile.target @@ -0,0 +1 @@ +TARGET = cc26x0-cc13x0 diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/README.md b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/README.md similarity index 100% rename from examples/platform-specific/cc26xx/very-sleepy-demo/README.md rename to examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/README.md diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/project-conf.h b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/project-conf.h similarity index 100% rename from examples/platform-specific/cc26xx/very-sleepy-demo/project-conf.h rename to examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/project-conf.h diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c b/examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/very-sleepy-demo.c similarity index 100% rename from examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c rename to examples/platform-specific/cc26x0-cc13x0/very-sleepy-demo/very-sleepy-demo.c diff --git a/examples/platform-specific/cc26xx/Makefile.target b/examples/platform-specific/cc26xx/Makefile.target deleted file mode 100644 index 15890aa6a..000000000 --- a/examples/platform-specific/cc26xx/Makefile.target +++ /dev/null @@ -1 +0,0 @@ -TARGET = srf06-cc26xx diff --git a/examples/platform-specific/cc26xx/ble-ipv6/Makefile b/examples/platform-specific/cc26xx/ble-ipv6/Makefile deleted file mode 100644 index 4ba928ad3..000000000 --- a/examples/platform-specific/cc26xx/ble-ipv6/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -CONTIKI_PROJECT=client - -all: $(CONTIKI_PROJECT) - -PLATFORMS_ONLY = srf06-cc26xx -BOARDS_ONLY = launchpad/cc2650 sensortag/cc2650 srf06/cc26xx - -MAKE_MAC = MAKE_MAC_BLE -MAKE_NET = MAKE_NET_IPV6 -CONTIKI = ../../../.. -include $(CONTIKI)/Makefile.include \ No newline at end of file diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/Makefile.target b/examples/platform-specific/cc26xx/cc26xx-web-demo/Makefile.target deleted file mode 100644 index 15890aa6a..000000000 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/Makefile.target +++ /dev/null @@ -1 +0,0 @@ -TARGET = srf06-cc26xx diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/Makefile.target b/examples/platform-specific/cc26xx/very-sleepy-demo/Makefile.target deleted file mode 100644 index 15890aa6a..000000000 --- a/examples/platform-specific/cc26xx/very-sleepy-demo/Makefile.target +++ /dev/null @@ -1 +0,0 @@ -TARGET = srf06-cc26xx diff --git a/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h b/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h index 568fd4b6d..32f7534e7 100644 --- a/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h +++ b/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h @@ -44,7 +44,7 @@ #define REST_MAX_CHUNK_SIZE 256 /* Network config */ -//#define UIP_CONF_BUFFER_SIZE (REST_MAX_CHUNK_SIZE + UIP_LLH_LEN + UIP_IPUDPH_LEN + COAP_MAX_HEADER_SIZE) +//#define UIP_CONF_BUFFER_SIZE (REST_MAX_CHUNK_SIZE + UIP_IPUDPH_LEN + COAP_MAX_HEADER_SIZE) //#define UIP_CONF_BUFFER_SIZE (REST_MAX_CHUNK_SIZE + 0 + 48 + 70) #define UIP_CONF_BUFFER_SIZE 1280 /* ipv6 required minimum */ diff --git a/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c b/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c index 251b0d135..b3ef311fd 100644 --- a/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c +++ b/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c @@ -50,8 +50,6 @@ #define DEBUG DEBUG_PRINT #include "net/ipv6/uip-debug.h" -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #define INTERVAL (10) #define BLINK_TIME (CLOCK_SECOND/4) diff --git a/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c b/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c index 584f54318..9e38d5d3a 100644 --- a/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c +++ b/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c @@ -97,7 +97,6 @@ PROCESS_THREAD(er_example_server, ev, data) PRINTF("Starting Erbium Example Server\n"); PRINTF("uIP buffer: %u\n", UIP_BUFSIZE); - PRINTF("LL header: %u\n", UIP_LLH_LEN); PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN); PRINTF("REST max chunk: %u\n", COAP_MAX_CHUNK_SIZE); diff --git a/examples/platform-specific/zoul/orion/ip64-router/ip64-router.c b/examples/platform-specific/zoul/orion/ip64-router/ip64-router.c index 5f2a98e24..4b3279e44 100644 --- a/examples/platform-specific/zoul/orion/ip64-router/ip64-router.c +++ b/examples/platform-specific/zoul/orion/ip64-router/ip64-router.c @@ -45,6 +45,7 @@ #include "contiki-net.h" #include "net/ipv6/uip.h" #include "net/ipv6/uip-ds6.h" +#include "net/ipv6/uip-ds6-nbr.h" #include "net/routing/routing.h" #include "dev/leds.h" #include "ip64/ip64.h" @@ -142,9 +143,9 @@ PT_THREAD(generate_routes(struct httpd_state *s)) #endif ADD("Neighbors
");
 
-  for(nbr = nbr_table_head(ds6_neighbors);
+  for(nbr = uip_ds6_nbr_head();
       nbr != NULL;
-      nbr = nbr_table_next(ds6_neighbors, nbr)) {
+      nbr = uip_ds6_nbr_next(nbr)) {
 
 #if WEBSERVER_CONF_NEIGHBOR_STATUS
 #if BUF_USES_STACK
diff --git a/examples/rpl-border-router/Makefile b/examples/rpl-border-router/Makefile
index 4d2db2a85..8d210dd3c 100644
--- a/examples/rpl-border-router/Makefile
+++ b/examples/rpl-border-router/Makefile
@@ -3,7 +3,7 @@ all: $(CONTIKI_PROJECT)
 CONTIKI = ../..
 
 # The BR is either native or embedded, and in the latter case must support SLIP
-PLATFORMS_EXCLUDE = nrf52dk
+PLATFORMS_EXCLUDE = nrf52dk z1
 
 # Include RPL BR module
 MODULES += os/services/rpl-border-router
diff --git a/examples/rpl-border-router/webserver/webserver.c b/examples/rpl-border-router/webserver/webserver.c
index 3658381bf..c4b472837 100644
--- a/examples/rpl-border-router/webserver/webserver.c
+++ b/examples/rpl-border-router/webserver/webserver.c
@@ -32,6 +32,7 @@
 
 #include "contiki.h"
 #include "net/routing/routing.h"
+#include "net/ipv6/uip-ds6-nbr.h"
 #include "net/ipv6/uip-ds6-route.h"
 #include "net/ipv6/uip-sr.h"
 
@@ -90,9 +91,9 @@ PT_THREAD(generate_routes(struct httpd_state *s))
 
   ADD("  Neighbors\n  
    \n"); SEND(&s->sout); - for(nbr = nbr_table_head(ds6_neighbors); + for(nbr = uip_ds6_nbr_head(); nbr != NULL; - nbr = nbr_table_next(ds6_neighbors, nbr)) { + nbr = uip_ds6_nbr_next(nbr)) { ADD("
  • "); ipaddr_add(&nbr->ipaddr); ADD("
  • \n"); diff --git a/examples/rpl-udp/udp-client.c b/examples/rpl-udp/udp-client.c index 1fb030034..5dc7a0763 100644 --- a/examples/rpl-udp/udp-client.c +++ b/examples/rpl-udp/udp-client.c @@ -12,9 +12,6 @@ #define UDP_CLIENT_PORT 8765 #define UDP_SERVER_PORT 5678 -static struct simple_udp_connection udp_conn; - -#define START_INTERVAL (15 * CLOCK_SECOND) #define SEND_INTERVAL (60 * CLOCK_SECOND) static struct simple_udp_connection udp_conn; @@ -32,16 +29,21 @@ udp_rx_callback(struct simple_udp_connection *c, const uint8_t *data, uint16_t datalen) { - unsigned count = *(unsigned *)data; - LOG_INFO("Received response %u from ", count); + + LOG_INFO("Received response '%.*s' from ", datalen, (char *) data); LOG_INFO_6ADDR(sender_addr); +#if LLSEC802154_CONF_ENABLED + LOG_INFO_(" LLSEC LV:%d", uipbuf_get_attr(UIPBUF_ATTR_LLSEC_LEVEL)); +#endif LOG_INFO_("\n"); + } /*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer periodic_timer; static unsigned count; + static char str[32]; uip_ipaddr_t dest_ipaddr; PROCESS_BEGIN(); @@ -59,7 +61,8 @@ PROCESS_THREAD(udp_client_process, ev, data) LOG_INFO("Sending request %u to ", count); LOG_INFO_6ADDR(&dest_ipaddr); LOG_INFO_("\n"); - simple_udp_sendto(&udp_conn, &count, sizeof(count), &dest_ipaddr); + snprintf(str, sizeof(str), "hello %d", count); + simple_udp_sendto(&udp_conn, str, strlen(str), &dest_ipaddr); count++; } else { LOG_INFO("Not reachable yet\n"); diff --git a/examples/rpl-udp/udp-server.c b/examples/rpl-udp/udp-server.c index 399cabfc4..072eb8ae8 100644 --- a/examples/rpl-udp/udp-server.c +++ b/examples/rpl-udp/udp-server.c @@ -54,15 +54,13 @@ udp_rx_callback(struct simple_udp_connection *c, const uint8_t *data, uint16_t datalen) { - unsigned count = *(unsigned *)data; - LOG_INFO("Received request %u from ", count); + LOG_INFO("Received request '%.*s' from ", datalen, (char *) data); LOG_INFO_6ADDR(sender_addr); LOG_INFO_("\n"); #if WITH_SERVER_REPLY - LOG_INFO("Sending response %u to ", count); - LOG_INFO_6ADDR(sender_addr); - LOG_INFO_("\n"); - simple_udp_sendto(&udp_conn, &count, sizeof(count), sender_addr); + /* send back the same string to the client as an echo reply */ + LOG_INFO("Sending response.\n"); + simple_udp_sendto(&udp_conn, data, datalen, sender_addr); #endif /* WITH_SERVER_REPLY */ } /*---------------------------------------------------------------------------*/ diff --git a/examples/sensniff/Makefile b/examples/sensniff/Makefile index 090bc694d..78cb41a3e 100644 --- a/examples/sensniff/Makefile +++ b/examples/sensniff/Makefile @@ -1,7 +1,7 @@ CONTIKI_PROJECT = sensniff CONTIKI = ../.. -PLATFORMS_ONLY = cc2538dk openmote-cc2538 zoul srf06-cc26xx jn516x simplelink +PLATFORMS_ONLY = cc2538dk openmote-cc2538 z1 zoul cc26x0-cc13x0 jn516x simplelink PROJECT_SOURCEFILES += sensniff-mac.c netstack.c MODULES_REL += pool $(TARGET) diff --git a/examples/sensniff/srf06-cc26xx/target-conf.h b/examples/sensniff/cc26x0-cc13x0/target-conf.h similarity index 100% rename from examples/sensniff/srf06-cc26xx/target-conf.h rename to examples/sensniff/cc26x0-cc13x0/target-conf.h diff --git a/examples/sensniff/pool/cc2530-cc2531-io.h b/examples/sensniff/z1/target-conf.h similarity index 87% rename from examples/sensniff/pool/cc2530-cc2531-io.h rename to examples/sensniff/z1/target-conf.h index cfed98f1d..6e3db2b48 100644 --- a/examples/sensniff/pool/cc2530-cc2531-io.h +++ b/examples/sensniff/z1/target-conf.h @@ -29,15 +29,13 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /*---------------------------------------------------------------------------*/ -#ifndef CC2530_CC2531_IO_H_ -#define CC2530_CC2531_IO_H_ +#ifndef TARGET_CONF_H_ +#define TARGET_CONF_H_ /*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "dev/io-arch.h" +#define MSP430_IO_CONF_USE_UART1 0 +#define UART0_CONF_BAUD_RATE 460800 /*---------------------------------------------------------------------------*/ -#define sensniff_io_byte_out(b) io_arch_writeb(b) -#define sensniff_io_flush() io_arch_flush() -#define sensniff_io_set_input(f) io_arch_set_input(f) +#define SENSNIFF_IO_DRIVER_H "pool/msp430-io.h" /*---------------------------------------------------------------------------*/ -#endif /* CC2530_CC2531_IO_H_ */ +#endif /* TARGET_CONF_H_ */ /*---------------------------------------------------------------------------*/ diff --git a/examples/slip-radio/slip-radio.c b/examples/slip-radio/slip-radio.c index a20bd8676..8ef22ff55 100644 --- a/examples/slip-radio/slip-radio.c +++ b/examples/slip-radio/slip-radio.c @@ -226,12 +226,11 @@ slip_radio_cmd_handler(const uint8_t *data, int len) static void slip_input_callback(void) { - LOG_DBG("SR-SIN: %u '%c%c'\n", uip_len, - uip_buf[UIP_LLH_LEN], uip_buf[UIP_LLH_LEN + 1]); - if(!cmd_input(&uip_buf[UIP_LLH_LEN], uip_len)) { + LOG_DBG("SR-SIN: %u '%c%c'\n", uip_len, uip_buf[0], uip_buf[1]); + if(!cmd_input(uip_buf, uip_len)) { cmd_send((uint8_t *)"EUnknown command", 16); } - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void diff --git a/examples/storage/cfs-coffee/README.md b/examples/storage/cfs-coffee/README.md index 028f518f8..640124d56 100644 --- a/examples/storage/cfs-coffee/README.md +++ b/examples/storage/cfs-coffee/README.md @@ -23,7 +23,7 @@ Supported Hardware (tested or known to work) * cc2538dk * openmote-cc2538 * zoul -* TI srf06-cc26xx +* TI cc26x0-cc13x0 - sensortag - launchpad diff --git a/examples/websocket/Makefile b/examples/websocket/Makefile index 87d0789c1..d0b38d95d 100644 --- a/examples/websocket/Makefile +++ b/examples/websocket/Makefile @@ -3,6 +3,6 @@ all: $(CONTIKI_PROJECT) CONTIKI=../.. MODULES += os/net/app-layer/http-socket -PLATFORMS_EXCLUDE = sky +PLATFORMS_EXCLUDE = sky z1 include $(CONTIKI)/Makefile.include diff --git a/os/contiki-main.c b/os/contiki-main.c index db8893717..43e301746 100644 --- a/os/contiki-main.c +++ b/os/contiki-main.c @@ -48,6 +48,7 @@ #include "sys/stack-check.h" #include "dev/watchdog.h" +#include "net/queuebuf.h" #include "net/app-layer/coap/coap-engine.h" #include "services/rpl-border-router/rpl-border-router.h" #include "services/orchestra/orchestra.h" @@ -89,6 +90,9 @@ main(void) platform_init_stage_two(); +#if QUEUEBUF_ENABLED + queuebuf_init(); +#endif /* QUEUEBUF_ENABLED */ netstack_init(); node_id_init(); diff --git a/os/contiki-net.h b/os/contiki-net.h index 1eb50dd8d..6384c51ef 100644 --- a/os/contiki-net.h +++ b/os/contiki-net.h @@ -44,10 +44,8 @@ #include "net/ipv6/uip-nameserver.h" #include "net/routing/routing.h" -#if NETSTACK_CONF_WITH_IPV6 #include "net/ipv6/uip-icmp6.h" #include "net/ipv6/uip-ds6.h" -#endif /* NETSTACK_CONF_WITH_IPV6 */ #include "net/ipv6/resolv.h" diff --git a/os/dev/gpio-hal.h b/os/dev/gpio-hal.h index 9fe541772..5ad9f472b 100644 --- a/os/dev/gpio-hal.h +++ b/os/dev/gpio-hal.h @@ -189,7 +189,7 @@ void gpio_hal_event_handler(gpio_hal_pin_mask_t pins); * \param pin The pin * \return The corresponding mask */ -#define gpio_hal_pin_to_mask(pin) (1 << (pin)) +#define gpio_hal_pin_to_mask(pin) ((gpio_hal_pin_mask_t)1 << (pin)) /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/os/dev/radio.h b/os/dev/radio.h index e6ca64f3d..e279e9efe 100644 --- a/os/dev/radio.h +++ b/os/dev/radio.h @@ -196,7 +196,9 @@ enum { /* Radio power modes */ enum { RADIO_POWER_MODE_OFF, - RADIO_POWER_MODE_ON + RADIO_POWER_MODE_ON, + RADIO_POWER_MODE_CARRIER_ON, + RADIO_POWER_MODE_CARRIER_OFF }; /** diff --git a/os/dev/slip.c b/os/dev/slip.c index 3cfe9ea07..7426151c4 100644 --- a/os/dev/slip.c +++ b/os/dev/slip.c @@ -52,7 +52,7 @@ static uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop; #endif /*---------------------------------------------------------------------------*/ /* Must be at least one byte larger than UIP_BUFSIZE! */ -#define RX_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN + 16) +#define RX_BUFSIZE (UIP_BUFSIZE + 16) /*---------------------------------------------------------------------------*/ enum { STATE_TWOPACKETS = 0, /* We have 2 packets and drop incoming data. */ @@ -87,7 +87,7 @@ slip_set_input_callback(void (*c)(void)) void slip_send(void) { - slip_write(&uip_buf[UIP_LLH_LEN], uip_len); + slip_write(uip_buf, uip_len); } /*---------------------------------------------------------------------------*/ void @@ -252,8 +252,7 @@ PROCESS_THREAD(slip_process, ev, data) slip_active = 1; /* Move packet from rxbuf to buffer provided by uIP. */ - uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], - UIP_BUFSIZE - UIP_LLH_LEN); + uip_len = slip_poll_handler(uip_buf, UIP_BUFSIZE); if(uip_len > 0) { if(input_callback) { diff --git a/os/dev/spi.c b/os/dev/spi.c index 145a4f72e..091cf8e51 100644 --- a/os/dev/spi.c +++ b/os/dev/spi.c @@ -67,13 +67,21 @@ spi_release(const spi_device_t *dev) spi_status_t spi_select(const spi_device_t *dev) { - return spi_arch_select(dev); + if(!spi_arch_has_lock(dev)) { + return SPI_DEV_STATUS_BUS_NOT_OWNED; + } + + gpio_hal_arch_clear_pin(dev->pin_spi_cs); + + return SPI_DEV_STATUS_OK; } /*---------------------------------------------------------------------------*/ spi_status_t spi_deselect(const spi_device_t *dev) { - return spi_arch_deselect(dev); + gpio_hal_arch_set_pin(dev->pin_spi_cs); + + return SPI_DEV_STATUS_OK; } /*---------------------------------------------------------------------------*/ bool diff --git a/os/dev/spi.h b/os/dev/spi.h index b7114717f..9dda05b8e 100644 --- a/os/dev/spi.h +++ b/os/dev/spi.h @@ -323,25 +323,6 @@ spi_status_t spi_arch_transfer(const spi_device_t *dev, uint8_t *buf, int rlen, int ignore_len); -/** - * \brief Selects an SPI device - * \param dev An SPI device configuration that specifies the CS pin. - * \return SPI return code - * - * Clears the CS pin. It should work only if the device has already - * locked the SPI controller. - */ -spi_status_t spi_arch_select(const spi_device_t *dev); - -/** - * \brief Deselects an SPI device - * \param dev An SPI device configuration that specifies the CS pin. - * \return SPI return code - * - * Set the CS pin. Locking the SPI controller is not needed. - */ -spi_status_t spi_arch_deselect(const spi_device_t *dev); - #endif /* SPI_H_ */ /*---------------------------------------------------------------------------*/ /** diff --git a/os/lib/json/jsonparse.c b/os/lib/json/jsonparse.c index 6277b56c4..941420521 100644 --- a/os/lib/json/jsonparse.c +++ b/os/lib/json/jsonparse.c @@ -32,34 +32,42 @@ #include "jsonparse.h" #include #include +#include /*--------------------------------------------------------------------*/ -static int +static bool push(struct jsonparse_state *state, char c) { - state->stack[state->depth] = c; - state->depth++; - state->vtype = 0; - return state->depth < JSONPARSE_MAX_DEPTH; + if(state->depth < JSONPARSE_MAX_DEPTH) { + state->stack[state->depth] = c; + state->depth++; + state->vtype = 0; + return true; + } else { + return false; + } } /*--------------------------------------------------------------------*/ -static void +static bool modify(struct jsonparse_state *state, char c) { if(state->depth > 0) { state->stack[state->depth - 1] = c; + return true; + } else { + return false; } } /*--------------------------------------------------------------------*/ -static char +static bool pop(struct jsonparse_state *state) { if(state->depth == 0) { - return JSON_TYPE_ERROR; + return false; } state->depth--; state->vtype = state->stack[state->depth]; - return state->stack[state->depth]; + return true; } /*--------------------------------------------------------------------*/ /* will pass by the value and store the start and length of the value for @@ -134,15 +142,11 @@ skip_ws(struct jsonparse_state *state) } } /*--------------------------------------------------------------------*/ -static int +static bool is_atomic(struct jsonparse_state *state) { char v = state->vtype; - if(v == 'N' || v == '"' || v == '0' || v == 'n' || v == 't' || v == 'f') { - return 1; - } else { - return 0; - } + return v == 'N' || v == '"' || v == '0' || v == 'n' || v == 't' || v == 'f'; } /*--------------------------------------------------------------------*/ void @@ -163,6 +167,7 @@ jsonparse_next(struct jsonparse_state *state) char c; char s; char v; + bool ret; skip_ws(state); c = state->json[state->pos]; @@ -173,48 +178,51 @@ jsonparse_next(struct jsonparse_state *state) switch(c) { case '{': if((s == 0 && v == 0) || s == '[' || s == ':') { - push(state, c); - } else { - state->error = JSON_ERROR_UNEXPECTED_OBJECT; - return JSON_TYPE_ERROR; + if(push(state, c)) { + return c; + } } - return c; + state->error = JSON_ERROR_UNEXPECTED_OBJECT; + return JSON_TYPE_ERROR; case '}': if((s == ':' && v != ',' && v != 0 ) || (s == '{' && v == 0)) { - pop(state); - } else { - state->error = JSON_ERROR_UNEXPECTED_END_OF_OBJECT; - return JSON_TYPE_ERROR; + if(pop(state)) { + return c; + } } - return c; + state->error = JSON_ERROR_UNEXPECTED_END_OF_OBJECT; + return JSON_TYPE_ERROR; case ']': if(s == '[' && v != ',') { - pop(state); - } else { - state->error = JSON_ERROR_UNEXPECTED_END_OF_ARRAY; - return JSON_TYPE_ERROR; + if(pop(state)) { + return c; + } } - return c; + state->error = JSON_ERROR_UNEXPECTED_END_OF_ARRAY; + return JSON_TYPE_ERROR; case ':': if(s == '{' && v == 'N') { - modify(state, ':'); + ret = modify(state, ':'); state->vtype = 0; - } else { - state->error = JSON_ERROR_SYNTAX; - return JSON_TYPE_ERROR; + if(ret) { + return jsonparse_next(state); + } } - return jsonparse_next(state); + state->error = JSON_ERROR_SYNTAX; + return JSON_TYPE_ERROR; case ',': if(s == ':' && v != 0) { - modify(state, '{'); + ret = modify(state, '{'); state->vtype = c; + if(ret) { + return c; + } } else if(s == '[') { state->vtype = c; - } else { - state->error = JSON_ERROR_SYNTAX; - return JSON_TYPE_ERROR; + return c; } - return c; + state->error = JSON_ERROR_SYNTAX; + return JSON_TYPE_ERROR; case '"': if((s == 0 && v == 0) || s == '{' || s == '[' || s == ':') { return atomic(state, c = (s == '{' ? JSON_TYPE_PAIR_NAME : c)); @@ -225,12 +233,12 @@ jsonparse_next(struct jsonparse_state *state) return c; case '[': if((s == 0 && v == 0) || s == '[' || s == ':') { - push(state, c); - } else { - state->error = JSON_ERROR_UNEXPECTED_ARRAY; - return JSON_TYPE_ERROR; + if(push(state, c)) { + return c; + } } - return c; + state->error = JSON_ERROR_UNEXPECTED_ARRAY; + return JSON_TYPE_ERROR; case 0: if(v == 0 || state->depth > 0) { state->error = JSON_ERROR_SYNTAX; diff --git a/os/lib/json/jsontree.c b/os/lib/json/jsontree.c index 13d7d8604..259525376 100644 --- a/os/lib/json/jsontree.c +++ b/os/lib/json/jsontree.c @@ -198,7 +198,10 @@ jsontree_print_next(struct jsontree_context *js_ctx) } else { ov = o->values[index]; } - /* TODO check max depth */ + if(js_ctx->depth >= JSONTREE_MAX_DEPTH - 1) { + /* Too deep: return 0 */ + return 0; + } js_ctx->depth++; /* step down to value... */ js_ctx->index[js_ctx->depth] = 0; /* and init index */ js_ctx->values[js_ctx->depth] = ov; @@ -299,7 +302,10 @@ find_next(struct jsontree_context *js_ctx) } else { ov = o->values[index]; } - /* TODO check max depth */ + if(js_ctx->depth >= JSONTREE_MAX_DEPTH - 1) { + /* Too deep: return NULL */ + return NULL; + } js_ctx->depth++; /* step down to value... */ js_ctx->index[js_ctx->depth] = 0; /* and init index */ js_ctx->values[js_ctx->depth] = ov; diff --git a/os/net/app-layer/coap/coap-conf.h b/os/net/app-layer/coap/coap-conf.h index 911017ed8..5b610dca3 100644 --- a/os/net/app-layer/coap/coap-conf.h +++ b/os/net/app-layer/coap/coap-conf.h @@ -99,7 +99,9 @@ #endif /* COAP_MAX_OBSERVERS */ /* Interval in notifies in which NON notifies are changed to CON notifies to check client. */ -#ifndef COAP_OBSERVE_REFRESH_INTERVAL +#ifdef COAP_CONF_OBSERVE_REFRESH_INTERVAL +#define COAP_OBSERVE_REFRESH_INTERVAL COAP_CONF_OBSERVE_REFRESH_INTERVAL +#else #define COAP_OBSERVE_REFRESH_INTERVAL 20 #endif /* COAP_OBSERVE_REFRESH_INTERVAL */ diff --git a/os/net/app-layer/coap/coap-observe.c b/os/net/app-layer/coap/coap-observe.c index d6ef3887e..3574acac4 100644 --- a/os/net/app-layer/coap/coap-observe.c +++ b/os/net/app-layer/coap/coap-observe.c @@ -244,7 +244,9 @@ coap_notify_observers_sub(coap_resource_t *resource, const char *subpath) /*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */ if((transaction = coap_new_transaction(coap_get_mid(), &obs->endpoint))) { - if(obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) { + /* if COAP_OBSERVE_REFRESH_INTERVAL is zero, never send observations as confirmable messages */ + if(COAP_OBSERVE_REFRESH_INTERVAL != 0 + && (obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0)) { LOG_DBG(" Force Confirmable for\n"); notification->type = COAP_TYPE_CON; } diff --git a/os/net/app-layer/coap/coap-uip.c b/os/net/app-layer/coap/coap-uip.c index e2be92326..8109db20e 100644 --- a/os/net/app-layer/coap/coap-uip.c +++ b/os/net/app-layer/coap/coap-uip.c @@ -70,21 +70,13 @@ #endif /* WITH_DTLS */ /* sanity check for configured values */ -#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPH_LEN - UIP_UDPH_LEN) +#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN) #error "UIP_CONF_BUFFER_SIZE too small for COAP_MAX_CHUNK_SIZE" #endif #define SERVER_LISTEN_PORT UIP_HTONS(COAP_DEFAULT_PORT) #define SERVER_LISTEN_SECURE_PORT UIP_HTONS(COAP_DEFAULT_SECURE_PORT) -/* direct access into the buffer */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#if NETSTACK_CONF_WITH_IPV6 -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#else -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#endif - #ifdef WITH_DTLS static dtls_handler_t cb; static dtls_context_t *dtls_context = NULL; diff --git a/os/net/app-layer/mqtt/mqtt.c b/os/net/app-layer/mqtt/mqtt.c index 46a542c3f..9571244d8 100644 --- a/os/net/app-layer/mqtt/mqtt.c +++ b/os/net/app-layer/mqtt/mqtt.c @@ -872,6 +872,7 @@ parse_publish_vhdr(struct mqtt_connection *conn, /* Read out topic length */ if(conn->in_packet.topic_len_received == 0) { + conn->in_packet.topic_pos = 0; conn->in_packet.topic_len = (input_data_ptr[(*pos)++] << 8); conn->in_packet.byte_counter++; if(*pos >= input_data_len) { @@ -880,7 +881,11 @@ parse_publish_vhdr(struct mqtt_connection *conn, conn->in_packet.topic_len |= input_data_ptr[(*pos)++]; conn->in_packet.byte_counter++; conn->in_packet.topic_len_received = 1; - + /* Abort if topic is longer than our topic buffer */ + if(conn->in_packet.topic_len > MQTT_MAX_TOPIC_LENGTH) { + DBG("MQTT - topic too long %u/%u\n", conn->in_packet.topic_len, MQTT_MAX_TOPIC_LENGTH); + return; + } DBG("MQTT - Read PUBLISH topic len %i\n", conn->in_packet.topic_len); /* WARNING: Check here if TOPIC fits in payload area, otherwise error */ } diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index 5b6f1b863..169064b25 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -103,26 +103,18 @@ static struct uip_udp_conn *c; static uip_ipaddr_t src_ip; static uip_ipaddr_t des_ip; /*---------------------------------------------------------------------------*/ -/* uIPv6 Pointers */ -/*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -/*---------------------------------------------------------------------------*/ /* Local function prototypes */ /*---------------------------------------------------------------------------*/ static void icmp_input(void); static void icmp_output(void); static void mcast_fwd(void *p); -int remove_ext_hdr(void); /*---------------------------------------------------------------------------*/ /* Internal Data Structures */ /*---------------------------------------------------------------------------*/ struct multicast_on_behalf{ /* ICMP message of multicast_on_behalf */ uint16_t mcast_port; uip_ipaddr_t mcast_ip; - uint8_t mcast_payload[UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN]; + uint8_t mcast_payload[UIP_BUFSIZE - UIP_IPUDPH_LEN]; }; #define UIP_ICMP_MOB 18 /* Size of multicast_on_behalf ICMP header */ /*---------------------------------------------------------------------------*/ @@ -143,7 +135,7 @@ icmp_output() struct multicast_on_behalf *mob; mob = (struct multicast_on_behalf *)UIP_ICMP_PAYLOAD; - memcpy(&mob->mcast_payload, &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], uip_slen); + memcpy(&mob->mcast_payload, &uip_buf[UIP_IPUDPH_LEN], uip_slen); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; @@ -164,8 +156,7 @@ icmp_output() PRINT6ADDR(&UIP_IP_BUF->destipaddr); PRINTF("\n"); - UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8; - UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + payload_len); UIP_ICMP_BUF->type = ICMP6_ESMRF; UIP_ICMP_BUF->icode = ESMRF_ICMP_CODE; @@ -199,7 +190,7 @@ icmp_input() } #endif - remove_ext_hdr(); + uip_remove_ext_hdr(); PRINTF("ESMRF: ICMPv6 In from "); PRINT6ADDR(&UIP_IP_BUF->srcipaddr); @@ -210,11 +201,11 @@ icmp_input() VERBOSE_PRINTF("ESMRF: ICMPv6 In, parse from %p to %p\n", UIP_ICMP_PAYLOAD, (uint8_t *)UIP_ICMP_PAYLOAD + uip_len - - uip_l2_l3_icmp_hdr_len); + uip_l3_icmp_hdr_len); locmobptr = (struct multicast_on_behalf *) UIP_ICMP_PAYLOAD; - loclen = uip_len - (uip_l2_l3_icmp_hdr_len + UIP_ICMP_MOB); + loclen = uip_len - (uip_l3_icmp_hdr_len + UIP_ICMP_MOB); uip_ipaddr_copy(&src_ip, &UIP_IP_BUF->srcipaddr); uip_ipaddr_copy(&des_ip, &UIP_IP_BUF->destipaddr); @@ -224,13 +215,13 @@ icmp_input() c->rport = locmobptr->mcast_port; uip_slen = loclen; uip_udp_conn=c; - memcpy(&uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], locmobptr->mcast_payload, - loclen > UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN? - UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN: loclen); + memcpy(&uip_buf[UIP_IPUDPH_LEN], locmobptr->mcast_payload, + loclen > UIP_BUFSIZE - UIP_IPUDPH_LEN? + UIP_BUFSIZE - UIP_IPUDPH_LEN: loclen); uip_process(UIP_UDP_SEND_CONN); - memcpy(&mcast_buf, &uip_buf[UIP_LLH_LEN], uip_len); + memcpy(&mcast_buf, uip_buf, uip_len); mcast_len = uip_len; /* pass the packet to our uip_process to check if it is allowed to * accept this packet or not */ @@ -240,7 +231,7 @@ icmp_input() uip_process(UIP_DATA); - memcpy(&uip_buf[UIP_LLH_LEN], &mcast_buf, mcast_len); + memcpy(uip_buf, &mcast_buf, mcast_len); uip_len = mcast_len; /* Return the IP of the original Multicast sender */ uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &src_ip); @@ -252,17 +243,17 @@ icmp_input() /* If we enter here, we will definitely forward */ tcpip_ipv6_output(); } - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void mcast_fwd(void *p) { - memcpy(&uip_buf[UIP_LLH_LEN], &mcast_buf, mcast_len); + memcpy(uip_buf, &mcast_buf, mcast_len); uip_len = mcast_len; UIP_IP_BUF->ttl--; tcpip_output(NULL); - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static uint8_t @@ -352,7 +343,7 @@ in() fwd_delay = fwd_delay * (1 + ((random_rand() >> 11) % fwd_spread)); } - memcpy(&mcast_buf, &uip_buf[UIP_LLH_LEN], uip_len); + memcpy(&mcast_buf, uip_buf, uip_len); mcast_len = uip_len; ctimer_set(&mcast_periodic, fwd_delay, mcast_fwd, NULL); } diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index 1d19512d6..909ebbfb6 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -276,7 +276,7 @@ struct mcast_packet { uint16_t seq_val; /* host-byte order */ struct sliding_window *sw; /* Pointer to the SW this packet belongs to */ uint8_t flags; /* Is-Used, Must Send, Is Listed */ - uint8_t buff[UIP_BUFSIZE - UIP_LLH_LEN]; + uint8_t buff[UIP_BUFSIZE]; }; /* Flag bits */ @@ -289,7 +289,7 @@ struct mcast_packet { #define MCAST_PACKET_GET_SEED(p) ((seed_id_t *)&((p)->seed_id)) #else #define MCAST_PACKET_GET_SEED(p) \ - ((seed_id_t *)&((struct uip_ip_hdr *)&(p)->buff[UIP_LLH_LEN])->srcipaddr) + ((seed_id_t *)&((struct uip_ip_hdr *)&(p)->buff[0])->srcipaddr) #endif /** @@ -456,14 +456,9 @@ static uint16_t last_seq; /*---------------------------------------------------------------------------*/ /* uIPv6 Pointers */ /*---------------------------------------------------------------------------*/ -#define UIP_DATA_BUF ((uint8_t *)&uip_buf[uip_l2_l3_hdr_len + UIP_UDPH_LEN]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#define UIP_EXT_BUF_NEXT ((uint8_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + HBHO_TOTAL_LEN]) -#define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + 2]) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) +#define UIP_EXT_BUF ((struct uip_ext_hdr *)UIP_IP_PAYLOAD(0)) +#define UIP_EXT_BUF_NEXT ((uint8_t *)(UIP_IP_PAYLOAD(HBHO_TOTAL_LEN))) +#define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)(UIP_IP_PAYLOAD(0) + 2)) extern uint16_t uip_slen; /*---------------------------------------------------------------------------*/ /* Local function prototypes */ @@ -864,8 +859,7 @@ icmp_output() roll_tm_create_dest(&UIP_IP_BUF->destipaddr); uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); - UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8; - UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + payload_len); UIP_ICMP_BUF->type = ICMP6_ROLL_TM; UIP_ICMP_BUF->icode = ROLL_TM_ICMP_CODE; @@ -1153,10 +1147,10 @@ icmp_input() VERBOSE_PRINTF("ROLL TM: ICMPv6 In, parse from %p to %p\n", UIP_ICMP_PAYLOAD, (uint8_t *)UIP_ICMP_PAYLOAD + uip_len - - uip_l2_l3_icmp_hdr_len); + uip_l3_icmp_hdr_len); while(locslhptr < (struct sequence_list_header *)((uint8_t *)UIP_ICMP_PAYLOAD + - uip_len - uip_l2_l3_icmp_hdr_len)) { + uip_len - uip_l3_icmp_hdr_len)) { VERBOSE_PRINTF("ROLL TM: ICMPv6 In, seq hdr @ %p\n", locslhptr); if((locslhptr->flags & SEQUENCE_LIST_RES) != 0) { @@ -1321,7 +1315,7 @@ static void out() { - if(uip_len + HBHO_TOTAL_LEN > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + HBHO_TOTAL_LEN > UIP_BUFSIZE) { PRINTF("ROLL TM: Multicast Out can not add HBHO. Packet too long\n"); goto drop; } @@ -1355,13 +1349,11 @@ out() HBH_SET_M(lochbhmptr); #endif - uip_ext_len += HBHO_TOTAL_LEN; - uip_len += HBHO_TOTAL_LEN; + uipbuf_add_ext_hdr(HBHO_TOTAL_LEN); /* Update the proto and length field in the v6 header */ UIP_IP_BUF->proto = UIP_PROTO_HBHO; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); PRINTF("ROLL TM: Multicast Out, HBHO: T=%u, L=%u, M=%u, S=0x%02x%02x\n", lochbhmptr->type, lochbhmptr->len, HBH_GET_M(lochbhmptr), @@ -1383,7 +1375,7 @@ out() drop: uip_slen = 0; - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static uint8_t diff --git a/os/net/ipv6/multicast/smrf.c b/os/net/ipv6/multicast/smrf.c index 86a053648..dd24968ba 100644 --- a/os/net/ipv6/multicast/smrf.c +++ b/os/net/ipv6/multicast/smrf.c @@ -77,18 +77,14 @@ static uip_buf_t mcast_buf; static uint8_t fwd_delay; static uint8_t fwd_spread; /*---------------------------------------------------------------------------*/ -/* uIPv6 Pointers */ -/*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -/*---------------------------------------------------------------------------*/ static void mcast_fwd(void *p) { - memcpy(&uip_buf[UIP_LLH_LEN], &mcast_buf, mcast_len); + memcpy(uip_buf, &mcast_buf, mcast_len); uip_len = mcast_len; UIP_IP_BUF->ttl--; tcpip_output(NULL); - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static uint8_t @@ -178,7 +174,7 @@ in() fwd_delay = fwd_delay * (1 + ((random_rand() >> 11) % fwd_spread)); } - memcpy(&mcast_buf, &uip_buf[UIP_LLH_LEN], uip_len); + memcpy(&mcast_buf, uip_buf, uip_len); mcast_len = uip_len; ctimer_set(&mcast_periodic, fwd_delay, mcast_fwd, NULL); } diff --git a/os/net/ipv6/resolv.c b/os/net/ipv6/resolv.c index 2bcd3983b..29af2b4cf 100644 --- a/os/net/ipv6/resolv.c +++ b/os/net/ipv6/resolv.c @@ -116,8 +116,6 @@ int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t n); #endif /* __SDCC */ -#define UIP_UDP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - /* If RESOLV_CONF_SUPPORTS_MDNS is set, then queries * for domain names in the local TLD will use mDNS as * described by draft-cheshire-dnsext-multicastdns. @@ -837,7 +835,7 @@ newdata(void) } else { uip_udp_packet_sendto(resolv_conn, uip_appdata, mdns_prep_host_announce_packet(), - &UIP_UDP_BUF->srcipaddr, + &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport); } return; diff --git a/os/net/ipv6/sicslowpan.c b/os/net/ipv6/sicslowpan.c index 9e4e3a8b4..d61d44baf 100644 --- a/os/net/ipv6/sicslowpan.c +++ b/os/net/ipv6/sicslowpan.c @@ -121,33 +121,11 @@ #define SICSLOWPAN_UDP_BUF(buf) ((struct uip_udp_hdr *)&buf[UIP_IPH_LEN]) #define SICSLOWPAN_IPPAYLOAD_BUF(buf) (&buf[UIP_IPH_LEN]) - -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_UDP_BUF(p) ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN + p]) -#define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLIPH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLIPH_LEN]) -#define UIP_IPPAYLOAD_BUF(pos) (&uip_buf[UIP_LLIPH_LEN + pos]) +#define UIP_IPPAYLOAD_BUF_POS(pos) (&uip_buf[UIP_IPH_LEN + (pos)]) +#define UIP_UDP_BUF_POS(pos) ((struct uip_udp_hdr *)UIP_IPPAYLOAD_BUF_POS(pos)) /** @} */ - -/** \brief Maximum available size for frame headers, - link layer security-related overhead, as well as - 6LoWPAN payload. */ -#ifdef SICSLOWPAN_CONF_MAC_MAX_PAYLOAD -#define MAC_MAX_PAYLOAD SICSLOWPAN_CONF_MAC_MAX_PAYLOAD -#else /* SICSLOWPAN_CONF_MAC_MAX_PAYLOAD */ -#define MAC_MAX_PAYLOAD (127 - 2) -#endif /* SICSLOWPAN_CONF_MAC_MAX_PAYLOAD */ - -/** \brief Maximum size of a frame header. This value is - * used in case framer returns an error */ -#ifdef SICSLOWPAN_CONF_MAC_MAX_HEADER -#define MAC_MAX_HEADER SICSLOWPAN_CONF_MAC_MAX_HEADER -#else /* SICSLOWPAN_CONF_MAC_MAX_HEADER */ -#define MAC_MAX_HEADER 21 -#endif /* SICSLOWPAN_CONF_MAC_MAX_HEADER */ - /* set this to zero if not compressing EXT_HDR - for backwards compatibility */ #ifdef SICSLOWPAN_CONF_COMPRESS_EXT_HDR #define COMPRESS_EXT_HDR SICSLOWPAN_CONF_COMPRESS_EXT_HDR @@ -250,7 +228,7 @@ static uint16_t my_tag; #define SICSLOWPAN_FRAGMENT_SIZE SICSLOWPAN_CONF_FRAGMENT_SIZE #else /* The default fragment size (110 bytes for 127-2 bytes frames) */ -#define SICSLOWPAN_FRAGMENT_SIZE (MAC_MAX_PAYLOAD - 15) +#define SICSLOWPAN_FRAGMENT_SIZE (127 - 2 - 15) #endif /* Assuming that the worst growth for uncompression is 38 bytes */ @@ -466,9 +444,9 @@ set_packet_attrs(void) /* assign values to the channel attribute (port or type + code) */ if(UIP_IP_BUF->proto == UIP_PROTO_UDP) { - c = UIP_UDP_BUF(0)->srcport; - if(UIP_UDP_BUF(0)->destport < c) { - c = UIP_UDP_BUF(0)->destport; + c = UIP_UDP_BUF_POS(0)->srcport; + if(UIP_UDP_BUF_POS(0)->destport < c) { + c = UIP_UDP_BUF_POS(0)->destport; } } else if(UIP_IP_BUF->proto == UIP_PROTO_TCP) { c = UIP_TCP_BUF->srcport; @@ -919,7 +897,7 @@ compress_hdr_iphc(linkaddr_t *link_destaddr) /* Handle the header here! */ { struct uip_ext_hdr *ext_hdr = - (struct uip_ext_hdr *) UIP_IPPAYLOAD_BUF(ext_hdr_len); + (struct uip_ext_hdr *) UIP_IPPAYLOAD_BUF_POS(ext_hdr_len); int len; proto = proto == -1 ? SICSLOWPAN_NHC_ETX_HDR_DESTO : proto; /* Len is defined to be in octets from the length byte */ @@ -958,7 +936,7 @@ compress_hdr_iphc(linkaddr_t *link_destaddr) case UIP_PROTO_UDP: /* allocate a byte for the next header posision as UDP has no next */ hc06_ptr++; - udp_buf = UIP_UDP_BUF(ext_hdr_len); + udp_buf = UIP_UDP_BUF_POS(ext_hdr_len); LOG_DBG("compression: inlined UDP ports on send side: %x, %x\n", UIP_HTONS(udp_buf->srcport), UIP_HTONS(udp_buf->destport)); /* Mask out the last 4 bits can be used as a mask */ @@ -1548,8 +1526,6 @@ fragment_copy_payload_and_send(uint16_t uip_offset, linkaddr_t *dest) { static uint8_t output(const linkaddr_t *localdest) { - int framer_hdrlen; - int max_payload; int frag_needed; /* The MAC address of the destination of the packet */ @@ -1588,13 +1564,23 @@ output(const linkaddr_t *localdest) /* Calculate NETSTACK_FRAMER's header length, that will be added in the NETSTACK_MAC */ packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &dest); - framer_hdrlen = NETSTACK_FRAMER.length(); - if(framer_hdrlen < 0) { - /* Framing failed, we assume the maximum header length */ - framer_hdrlen = MAC_MAX_HEADER; - } +#if LLSEC802154_USES_AUX_HEADER + /* copy LLSEC level */ + packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, + uipbuf_get_attr(UIPBUF_ATTR_LLSEC_LEVEL)); +#if LLSEC802154_USES_EXPLICIT_KEYS + packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, + uipbuf_get_attr(UIPBUF_ATTR_LLSEC_KEY_ID)); +#endif /* LLSEC802154_USES_EXPLICIT_KEYS */ +#endif /* LLSEC802154_USES_AUX_HEADER */ - mac_max_payload = MAC_MAX_PAYLOAD - framer_hdrlen; + mac_max_payload = NETSTACK_MAC.max_payload(); + + if(mac_max_payload <= 0) { + /* Framing failed, drop packet */ + LOG_WARN("output: failed to calculate payload size - dropping packet\n"); + return 0; + } /* Try to compress the headers */ #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPV6 @@ -1615,22 +1601,17 @@ output(const linkaddr_t *localdest) } #endif /* SICSLOWPAN_COMPRESSION >= SICSLOWPAN_COMPRESSION_IPHC */ - /* Calculate NETSTACK_FRAMER's header length, that will be added in the NETSTACK_MAC. - * We calculate it here only to make a better decision of whether the outgoing packet - * needs to be fragmented or not. */ - packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &dest); - framer_hdrlen = NETSTACK_FRAMER.length(); - if(framer_hdrlen < 0) { - /* Framing failed, we assume the maximum header length */ - framer_hdrlen = MAC_MAX_HEADER; - } + /* Use the mac_max_payload to understand what is the max payload in a MAC + * packet. We calculate it here only to make a better decision of whether + * the outgoing packet needs to be fragmented or not. */ - max_payload = MAC_MAX_PAYLOAD - framer_hdrlen; - frag_needed = (int)uip_len - (int)uncomp_hdr_len + (int)packetbuf_hdr_len > max_payload; + packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &dest); + + frag_needed = (int)uip_len - (int)uncomp_hdr_len + (int)packetbuf_hdr_len > mac_max_payload; LOG_INFO("output: header len %d -> %d, total len %d -> %d, MAC max payload %d, frag_needed %d\n", uncomp_hdr_len, packetbuf_hdr_len, uip_len, uip_len - uncomp_hdr_len + packetbuf_hdr_len, - max_payload, frag_needed); + mac_max_payload, frag_needed); if(frag_needed) { #if SICSLOWPAN_CONF_FRAG @@ -1650,11 +1631,11 @@ output(const linkaddr_t *localdest) /* Total IPv6 payload */ int total_payload = (uip_len - uncomp_hdr_len); /* IPv6 payload that goes to first fragment */ - int frag1_payload = (max_payload - packetbuf_hdr_len - SICSLOWPAN_FRAG1_HDR_LEN) & 0xfffffff8; + int frag1_payload = (mac_max_payload - packetbuf_hdr_len - SICSLOWPAN_FRAG1_HDR_LEN) & 0xfffffff8; /* max IPv6 payload in each FRAGN. Must be multiple of 8 bytes */ - int fragn_max_payload = (max_payload - SICSLOWPAN_FRAGN_HDR_LEN) & 0xfffffff8; + int fragn_max_payload = (mac_max_payload - SICSLOWPAN_FRAGN_HDR_LEN) & 0xfffffff8; /* max IPv6 payload in the last fragment. Needs not be multiple of 8 bytes */ - int last_fragn_max_payload = max_payload - SICSLOWPAN_FRAGN_HDR_LEN; + int last_fragn_max_payload = mac_max_payload - SICSLOWPAN_FRAGN_HDR_LEN; /* sum of all IPv6 payload that goes to non-first and non-last fragments */ int middle_fragn_total_payload = MAX(total_payload - frag1_payload - last_fragn_max_payload, 0); /* Ceiling of: 2 + middle_fragn_total_payload / fragn_max_payload */ @@ -1806,6 +1787,9 @@ input(void) return; } + /* Clear uipbuf and set default attributes */ + uipbuf_clear(); + /* This is default uip_buf since we assume that this is not fragmented */ buffer = (uint8_t *)UIP_IP_BUF; @@ -1840,7 +1824,6 @@ input(void) } buffer = frag_info[frag_context].first_frag; - break; case SICSLOWPAN_DISPATCH_FRAGN: /* @@ -1936,12 +1919,12 @@ input(void) /* Sanity-check size of incoming packet to avoid buffer overflow */ { - int req_size = UIP_LLH_LEN + uncomp_hdr_len + (uint16_t)(frag_offset << 3) + int req_size = uncomp_hdr_len + (uint16_t)(frag_offset << 3) + packetbuf_payload_len; if(req_size > sizeof(uip_buf)) { LOG_ERR( - "input: packet dropped, minimum required IP_BUF size: %d+%d+%d+%d=%d (current size: %u)\n", - UIP_LLH_LEN, uncomp_hdr_len, (uint16_t)(frag_offset << 3), + "input: packet dropped, minimum required IP_BUF size: %d+%d+%d=%d (current size: %u)\n", + uncomp_hdr_len, (uint16_t)(frag_offset << 3), packetbuf_payload_len, req_size, (unsigned)sizeof(uip_buf)); return; } @@ -2004,6 +1987,19 @@ input(void) callback->input_callback(); } +#if LLSEC802154_USES_AUX_HEADER + /* + * Assuming that the last packet in packetbuf is containing + * the LLSEC state so that it can be copied to uipbuf. + */ + uipbuf_set_attr(UIPBUF_ATTR_LLSEC_LEVEL, + packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)); +#if LLSEC802154_USES_EXPLICIT_KEYS + uipbuf_set_attr(UIPBUF_ATTR_LLSEC_KEY_ID, + packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX)); +#endif /* LLSEC802154_USES_EXPLICIT_KEYS */ +#endif /* LLSEC802154_USES_AUX_HEADER */ + tcpip_input(); #if SICSLOWPAN_CONF_FRAG } @@ -2061,11 +2057,6 @@ sicslowpan_init(void) #endif /* SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 1 */ #endif /* SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPHC */ - - /* We use the queuebuf module if fragmentation is enabled */ -#if SICSLOWPAN_CONF_FRAG - queuebuf_init(); -#endif } /*--------------------------------------------------------------------*/ int diff --git a/os/net/ipv6/simple-udp.c b/os/net/ipv6/simple-udp.c index c98107c99..08986d827 100644 --- a/os/net/ipv6/simple-udp.c +++ b/os/net/ipv6/simple-udp.c @@ -52,8 +52,6 @@ PROCESS(simple_udp_process, "Simple UDP process"); static uint8_t started = 0; static uint8_t databuffer[UIP_BUFSIZE]; -#define UIP_IP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - /*---------------------------------------------------------------------------*/ static void init_simple_udp(void) @@ -134,7 +132,7 @@ PROCESS_THREAD(simple_udp_process, ev, data) { struct simple_udp_connection *c; PROCESS_BEGIN(); - + while(1) { PROCESS_WAIT_EVENT(); if(ev == tcpip_event) { @@ -165,9 +163,9 @@ PROCESS_THREAD(simple_udp_process, ev, data) PROCESS_CONTEXT_BEGIN(c->client_process); c->receive_callback(c, &(UIP_IP_BUF->srcipaddr), - UIP_HTONS(UIP_IP_BUF->srcport), + UIP_HTONS(UIP_UDP_BUF->srcport), &(UIP_IP_BUF->destipaddr), - UIP_HTONS(UIP_IP_BUF->destport), + UIP_HTONS(UIP_UDP_BUF->destport), databuffer, uip_datalen()); PROCESS_CONTEXT_END(); } diff --git a/os/net/ipv6/tcpip.c b/os/net/ipv6/tcpip.c index 8d546c1d7..16176e6e2 100644 --- a/os/net/ipv6/tcpip.c +++ b/os/net/ipv6/tcpip.c @@ -55,10 +55,6 @@ #define LOG_MODULE "TCP/IP" #define LOG_LEVEL LOG_LEVEL_TCPIP -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLIPH_LEN + uip_ext_len]) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_TCP_BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) - #ifdef UIP_FALLBACK_INTERFACE extern struct uip_fallback_interface UIP_FALLBACK_INTERFACE; #endif @@ -129,7 +125,7 @@ tcpip_output(const uip_lladdr_t *a) return ret; } else { /* Ok, ignore and drop... */ - uip_clear_buf(); + uipbuf_clear(); return 0; } } @@ -452,18 +448,16 @@ tcpip_input(void) NETSTACK_IP_PROCESS) { process_post_synch(&tcpip_process, PACKET_INPUT, NULL); } /* else - do nothing and drop */ - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ -extern void remove_ext_hdr(void); -/*---------------------------------------------------------------------------*/ static void output_fallback(void) { #ifdef UIP_FALLBACK_INTERFACE LOG_INFO("fallback: removing ext hdrs & setting proto %d %d\n", uip_ext_len, *((uint8_t *)UIP_IP_BUF + 40)); - remove_ext_hdr(); + uip_remove_ext_hdr(); /* Inform the other end that the destination is not reachable. If it's * not informed routes might get lost unexpectedly until there's a need * to send a new packet to the peer */ @@ -658,7 +652,7 @@ tcpip_ipv6_output(void) if(!NETSTACK_ROUTING.ext_header_update()) { /* Packet can not be forwarded */ LOG_ERR("output: routing protocol extension header update error\n"); - uip_clear_buf(); + uipbuf_clear(); return; } @@ -746,7 +740,7 @@ send_packet: } exit: - uip_clear_buf(); + uipbuf_clear(); return; } /*---------------------------------------------------------------------------*/ diff --git a/os/net/ipv6/udp-socket.c b/os/net/ipv6/udp-socket.c index 700306ca1..f104a6b6c 100644 --- a/os/net/ipv6/udp-socket.c +++ b/os/net/ipv6/udp-socket.c @@ -38,9 +38,6 @@ PROCESS(udp_socket_process, "UDP socket process"); static uint8_t buf[UIP_BUFSIZE]; -#define UIP_IP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - - /*---------------------------------------------------------------------------*/ static void init(void) @@ -182,9 +179,9 @@ PROCESS_THREAD(udp_socket_process, ev, data) PROCESS_CONTEXT_BEGIN(c->p); c->input_callback(c, c->ptr, &(UIP_IP_BUF->srcipaddr), - UIP_HTONS(UIP_IP_BUF->srcport), + UIP_HTONS(UIP_UDP_BUF->srcport), &(UIP_IP_BUF->destipaddr), - UIP_HTONS(UIP_IP_BUF->destport), + UIP_HTONS(UIP_UDP_BUF->destport), buf, uip_datalen()); PROCESS_CONTEXT_END(); } diff --git a/os/net/ipv6/uip-ds6-nbr.c b/os/net/ipv6/uip-ds6-nbr.c index f937986b5..b8f8b63d7 100644 --- a/os/net/ipv6/uip-ds6-nbr.c +++ b/os/net/ipv6/uip-ds6-nbr.c @@ -55,19 +55,67 @@ #include "net/ipv6/uip-nd6.h" #include "net/routing/routing.h" +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS +#include "lib/memb.h" +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ + /* Log configuration */ #include "sys/log.h" #define LOG_MODULE "IPv6 Nbr" #define LOG_LEVEL LOG_LEVEL_IPV6 -NBR_TABLE_GLOBAL(uip_ds6_nbr_t, ds6_neighbors); +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS +/** + * Add nbr to the list in nbr_entry. In other words, this function associates an + * IPv6 address in nbr with a link-layer address in nbr_entry. + * \param nbr the neighbor cache entry for an IPv6 address + * \param nbr_entry the nbr_table entry for an link-layer address + */ +static void add_uip_ds6_nbr_to_nbr_entry(uip_ds6_nbr_t *nbr, + uip_ds6_nbr_entry_t *nbr_entry); + +/** + * Remove nbr from the list of the corresponding nbr_entry + * \param nbr a neighbor cache entry (nbr) to be removed + */ +static void remove_uip_ds6_nbr_from_nbr_entry(uip_ds6_nbr_t *nbr); + +/** + * Remove nbr_etnry from nbr_table + * \param nbr_entry a nbr_table entry (nbr_entry) to be removed + */ +static void remove_nbr_entry(uip_ds6_nbr_entry_t *nbr_entry); + +/** + * Free memory for a specified neighbor cache entry + * \param nbr a neighbor cache entry to be freed + */ +static void free_uip_ds6_nbr(uip_ds6_nbr_t *nbr); + +/** + * Callback function called when a nbr_table entry is removed + * \param nbr_entry a nbr_entry to be removed + */ +static void callback_nbr_entry_removal(uip_ds6_nbr_entry_t *nbr_entry); + +NBR_TABLE(uip_ds6_nbr_entry_t, uip_ds6_nbr_entries); +MEMB(uip_ds6_nbr_memb, uip_ds6_nbr_t, UIP_DS6_NBR_MAX_NEIGHBOR_CACHES); +#else +NBR_TABLE(uip_ds6_nbr_t, ds6_neighbors); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ /*---------------------------------------------------------------------------*/ void uip_ds6_neighbors_init(void) { link_stats_init(); +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + memb_init(&uip_ds6_nbr_memb); + nbr_table_register(uip_ds6_nbr_entries, + (nbr_table_callback *)callback_nbr_entry_removal); +#else nbr_table_register(ds6_neighbors, (nbr_table_callback *)uip_ds6_nbr_rm); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * @@ -75,8 +123,63 @@ uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state, nbr_table_reason_t reason, void *data) { - uip_ds6_nbr_t *nbr = nbr_table_add_lladdr(ds6_neighbors, (linkaddr_t*)lladdr - , reason, data); + uip_ds6_nbr_t *nbr; + +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + + assert(uip_ds6_nbr_lookup(ipaddr) == NULL); + if(uip_ds6_nbr_lookup(ipaddr)) { + LOG_ERR("%s: uip_ds6_nbr for ", __func__); + LOG_ERR_6ADDR(ipaddr); + LOG_ERR_("has already existed\n"); + return NULL; + } + + /* firstly, allocate memory for a new nbr cache entry */ + if((nbr = (uip_ds6_nbr_t *)memb_alloc(&uip_ds6_nbr_memb)) == NULL) { + LOG_ERR("%s: cannot allocate a new uip_ds6_nbr\n", __func__); + return NULL; + } + + /* secondly, get or allocate nbr_entry for the link-layer address */ + nbr_entry = nbr_table_get_from_lladdr(uip_ds6_nbr_entries, + (const linkaddr_t *)lladdr); + if(nbr_entry == NULL) { + if((nbr_entry = + nbr_table_add_lladdr(uip_ds6_nbr_entries, + (linkaddr_t*)lladdr, reason, data)) == NULL) { + LOG_ERR("%s: cannot allocate a new uip_ds6_nbr_entry\n", __func__); + /* return from this function later */ + } else { + LIST_STRUCT_INIT(nbr_entry, uip_ds6_nbrs); + } + } + + /* free nbr and return if nbr_entry is not available */ + if((nbr_entry == NULL) || + (list_length(nbr_entry->uip_ds6_nbrs) == UIP_DS6_NBR_MAX_6ADDRS_PER_NBR)) { + if(list_length(nbr_entry->uip_ds6_nbrs) == UIP_DS6_NBR_MAX_6ADDRS_PER_NBR) { + /* + * it's already had the maximum number of IPv6 addresses; cannot + * add another. + */ + LOG_ERR("%s: no room in nbr_entry for ", __func__); + LOG_ERR_LLADDR((const linkaddr_t *)lladdr); + LOG_ERR_("\n"); + } + /* free the newly allocated memory in this function call */ + memb_free(&uip_ds6_nbr_memb, nbr); + return NULL; + } else { + /* everything is fine; nbr is ready to be used */ + /* it has room to add another IPv6 address */ + add_uip_ds6_nbr_to_nbr_entry(nbr, nbr_entry); + } +#else + nbr = nbr_table_add_lladdr(ds6_neighbors, (linkaddr_t*)lladdr, reason, data); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ + if(nbr) { uip_ipaddr_copy(&nbr->ipaddr, ipaddr); #if UIP_ND6_SEND_RA || !UIP_CONF_ROUTER @@ -113,10 +216,91 @@ uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, } } +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS +/*---------------------------------------------------------------------------*/ +static void +add_uip_ds6_nbr_to_nbr_entry(uip_ds6_nbr_t *nbr, + uip_ds6_nbr_entry_t *nbr_entry) +{ + LOG_DBG("%s: add nbr(%p) to nbr_entry (%p)\n", + __func__, nbr, nbr_entry); + nbr->nbr_entry = nbr_entry; + list_add(nbr_entry->uip_ds6_nbrs, nbr); +} +/*---------------------------------------------------------------------------*/ +static void +remove_uip_ds6_nbr_from_nbr_entry(uip_ds6_nbr_t *nbr) +{ + if(nbr == NULL) { + return; + } + LOG_DBG("%s: remove nbr(%p) from nbr_entry (%p)\n", + __func__, nbr, nbr->nbr_entry); + list_remove(nbr->nbr_entry->uip_ds6_nbrs, nbr); +} +/*---------------------------------------------------------------------------*/ +static void +remove_nbr_entry(uip_ds6_nbr_entry_t *nbr_entry) +{ + if(nbr_entry == NULL) { + return; + } + LOG_DBG("%s: remove nbr_entry (%p) from nbr_table\n", + __func__, nbr_entry); + (void)nbr_table_remove(uip_ds6_nbr_entries, nbr_entry); +} +/*---------------------------------------------------------------------------*/ +static void +free_uip_ds6_nbr(uip_ds6_nbr_t *nbr) +{ + if(nbr == NULL) { + return; + } +#if UIP_CONF_IPV6_QUEUE_PKT + uip_packetqueue_free(&nbr->packethandle); +#endif /* UIP_CONF_IPV6_QUEUE_PKT */ + NETSTACK_ROUTING.neighbor_state_changed(nbr); + assert(nbr->nbr_entry != NULL); + if(nbr->nbr_entry == NULL) { + LOG_ERR("%s: unexpected error nbr->nbr_entry is NULL\n", __func__); + } else { + remove_uip_ds6_nbr_from_nbr_entry(nbr); + if(list_length(nbr->nbr_entry->uip_ds6_nbrs) == 0) { + remove_nbr_entry(nbr->nbr_entry); + } + } + LOG_DBG("%s: free memory for nbr(%p)\n", __func__, nbr); + memb_free(&uip_ds6_nbr_memb, nbr); +} +/*---------------------------------------------------------------------------*/ +static void +callback_nbr_entry_removal(uip_ds6_nbr_entry_t *nbr_entry) +{ + uip_ds6_nbr_t *nbr; + uip_ds6_nbr_t *next_nbr; + if(nbr_entry == NULL) { + return; + } + for(nbr = (uip_ds6_nbr_t *)list_head(nbr_entry->uip_ds6_nbrs); + nbr != NULL; + nbr = next_nbr) { + next_nbr = (uip_ds6_nbr_t *)list_item_next(nbr); + free_uip_ds6_nbr(nbr); + } +} +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ /*---------------------------------------------------------------------------*/ int uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr) { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + if(nbr == NULL) { + return 0; + } else { + free_uip_ds6_nbr(nbr); + return 1; + } +#else /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ if(nbr != NULL) { #if UIP_CONF_IPV6_QUEUE_PKT uip_packetqueue_free(&nbr->packethandle); @@ -125,19 +309,52 @@ uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr) return nbr_table_remove(ds6_neighbors, nbr); } return 0; +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } /*---------------------------------------------------------------------------*/ int uip_ds6_nbr_update_ll(uip_ds6_nbr_t **nbr_pp, const uip_lladdr_t *new_ll_addr) { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + uip_ds6_nbr_t *nbr; +#else uip_ds6_nbr_t nbr_backup; +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ if(nbr_pp == NULL || new_ll_addr == NULL) { LOG_ERR("%s: invalid argument\n", __func__); return -1; } +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + + if((nbr_entry = + nbr_table_get_from_lladdr(uip_ds6_nbr_entries, + (const linkaddr_t *)new_ll_addr)) == NULL) { + if((nbr_entry = + nbr_table_add_lladdr(uip_ds6_nbr_entries, + (const linkaddr_t*)new_ll_addr, + NBR_TABLE_REASON_IPV6_ND, NULL)) == NULL) { + LOG_ERR("%s: cannot allocate a nbr_entry for", __func__); + LOG_ERR_LLADDR((const linkaddr_t *)new_ll_addr); + return -1; + } else { + LIST_STRUCT_INIT(nbr_entry, uip_ds6_nbrs); + } + } + + nbr = *nbr_pp; + + remove_uip_ds6_nbr_from_nbr_entry(nbr); + if(list_length(nbr->nbr_entry->uip_ds6_nbrs) == 0) { + remove_nbr_entry(nbr->nbr_entry); + } + add_uip_ds6_nbr_to_nbr_entry(nbr, nbr_entry); + +#else /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ + /* make sure new_ll_addr is not used in some other nbr */ if(uip_ds6_nbr_ll_lookup(new_ll_addr) != NULL) { LOG_ERR("%s: new_ll_addr, ", __func__); @@ -159,6 +376,7 @@ uip_ds6_nbr_update_ll(uip_ds6_nbr_t **nbr_pp, const uip_lladdr_t *new_ll_addr) return -1; } memcpy(*nbr_pp, &nbr_backup, sizeof(uip_ds6_nbr_t)); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ return 0; } @@ -173,46 +391,88 @@ uip_ds6_nbr_get_ipaddr(const uip_ds6_nbr_t *nbr) const uip_lladdr_t * uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr) { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + if(nbr == NULL) { + return NULL; + } + return (const uip_lladdr_t *)nbr_table_get_lladdr(uip_ds6_nbr_entries, + nbr->nbr_entry); +#else return (const uip_lladdr_t *)nbr_table_get_lladdr(ds6_neighbors, nbr); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } /*---------------------------------------------------------------------------*/ int uip_ds6_nbr_num(void) { - uip_ds6_nbr_t *nbr; - int num; + int num = 0; - num = 0; +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + for(nbr_entry = nbr_table_head(uip_ds6_nbr_entries); + nbr_entry != NULL; + nbr_entry = nbr_table_next(uip_ds6_nbr_entries, nbr_entry)) { + num += list_length(nbr_entry->uip_ds6_nbrs); + } +#else + uip_ds6_nbr_t *nbr; for(nbr = nbr_table_head(ds6_neighbors); nbr != NULL; nbr = nbr_table_next(ds6_neighbors, nbr)) { num++; } +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ return num; } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * uip_ds6_nbr_head(void) { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + if((nbr_entry = nbr_table_head(uip_ds6_nbr_entries)) == NULL) { + return NULL; + } + assert(list_head(nbr_entry->uip_ds6_nbrs) != NULL); + return (uip_ds6_nbr_t *)list_head(nbr_entry->uip_ds6_nbrs); +#else return nbr_table_head(ds6_neighbors); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * uip_ds6_nbr_next(uip_ds6_nbr_t *nbr) { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + if(nbr == NULL) { + return NULL; + } + if(list_item_next(nbr) != NULL) { + return list_item_next(nbr); + } + nbr_entry = nbr_table_next(uip_ds6_nbr_entries, nbr->nbr_entry); + if(nbr_entry == NULL) { + return NULL; + } else { + assert(list_head(nbr_entry->uip_ds6_nbrs) != NULL); + return (uip_ds6_nbr_t *)list_head(nbr_entry->uip_ds6_nbrs); + } +#else return nbr_table_next(ds6_neighbors, nbr); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr) { - uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors); - if(ipaddr != NULL) { - while(nbr != NULL) { - if(uip_ipaddr_cmp(&nbr->ipaddr, ipaddr)) { - return nbr; - } - nbr = nbr_table_next(ds6_neighbors, nbr); + uip_ds6_nbr_t *nbr; + if(ipaddr == NULL) { + return NULL; + } + for(nbr = uip_ds6_nbr_head(); nbr != NULL; nbr = uip_ds6_nbr_next(nbr)) { + if(uip_ipaddr_cmp(&nbr->ipaddr, ipaddr)) { + return nbr; } } return NULL; @@ -221,7 +481,23 @@ uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr) uip_ds6_nbr_t * uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr) { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + /* + * we cannot determine which entry should return by lladdr alone; + * return the first entry associated with lladdr. + */ + nbr_entry = + (uip_ds6_nbr_entry_t *)nbr_table_get_from_lladdr(uip_ds6_nbr_entries, + (linkaddr_t*)lladdr); + if(nbr_entry == NULL) { + return NULL; + } + assert(list_head(nbr_entry->uip_ds6_nbrs) != NULL); + return (uip_ds6_nbr_t *)list_head(nbr_entry->uip_ds6_nbrs); +#else return nbr_table_get_from_lladdr(ds6_neighbors, (linkaddr_t*)lladdr); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } /*---------------------------------------------------------------------------*/ @@ -239,6 +515,20 @@ uip_ds6_nbr_lladdr_from_ipaddr(const uip_ipaddr_t *ipaddr) uip_ds6_nbr_t *nbr = uip_ds6_nbr_lookup(ipaddr); return nbr ? uip_ds6_nbr_get_ll(nbr) : NULL; } +#if UIP_DS6_LL_NUD +/*---------------------------------------------------------------------------*/ +static void +update_nbr_reachable_state_by_ack(uip_ds6_nbr_t *nbr, const linkaddr_t *lladdr) +{ + if(nbr != NULL && nbr->state != NBR_INCOMPLETE) { + nbr->state = NBR_REACHABLE; + stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000); + LOG_INFO("received a link layer ACK : "); + LOG_INFO_LLADDR(lladdr); + LOG_INFO_(" is reachable.\n"); + } +} +#endif /* UIP_DS6_LL_NUD */ /*---------------------------------------------------------------------------*/ void uip_ds6_link_callback(int status, int numtx) @@ -266,14 +556,22 @@ uip_ds6_link_callback(int status, int numtx) * acknowledges link packets. */ if(status == MAC_TX_OK) { uip_ds6_nbr_t *nbr; - nbr = uip_ds6_nbr_ll_lookup((uip_lladdr_t *)dest); - if(nbr != NULL && nbr->state != NBR_INCOMPLETE) { - nbr->state = NBR_REACHABLE; - stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000); - LOG_INFO("received a link layer ACK : "); - LOG_INFO_LLADDR((uip_lladdr_t *)dest); - LOG_INFO_(" is reachable.\n"); +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + uip_ds6_nbr_entry_t *nbr_entry; + if((nbr_entry = + (uip_ds6_nbr_entry_t *)nbr_table_get_from_lladdr(uip_ds6_nbr_entries, + dest)) == NULL) { + return; } + for(nbr = (uip_ds6_nbr_t *)list_head(nbr_entry->uip_ds6_nbrs); + nbr != NULL; + nbr = (uip_ds6_nbr_t *)list_item_next(nbr)) { + update_nbr_reachable_state_by_ack(nbr, dest); + } +#else /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ + nbr = uip_ds6_nbr_ll_lookup((uip_lladdr_t *)dest); + update_nbr_reachable_state_by_ack(nbr, dest); +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ } #endif /* UIP_DS6_LL_NUD */ } @@ -283,7 +581,7 @@ uip_ds6_link_callback(int status, int numtx) void uip_ds6_neighbor_periodic(void) { - uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors); + uip_ds6_nbr_t *nbr = uip_ds6_nbr_head(); while(nbr != NULL) { switch(nbr->state) { case NBR_REACHABLE: @@ -354,7 +652,7 @@ uip_ds6_neighbor_periodic(void) default: break; } - nbr = nbr_table_next(ds6_neighbors, nbr); + nbr = uip_ds6_nbr_next(nbr); } } /*---------------------------------------------------------------------------*/ @@ -373,7 +671,7 @@ uip_ds6_nbr_refresh_reachable_state(const uip_ipaddr_t *ipaddr) uip_ds6_nbr_t * uip_ds6_get_least_lifetime_neighbor(void) { - uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors); + uip_ds6_nbr_t *nbr = uip_ds6_nbr_head(); uip_ds6_nbr_t *nbr_expiring = NULL; while(nbr != NULL) { if(nbr_expiring != NULL) { @@ -384,7 +682,7 @@ uip_ds6_get_least_lifetime_neighbor(void) } else { nbr_expiring = nbr; } - nbr = nbr_table_next(ds6_neighbors, nbr); + nbr = uip_ds6_nbr_next(nbr); } return nbr_expiring; } diff --git a/os/net/ipv6/uip-ds6-nbr.h b/os/net/ipv6/uip-ds6-nbr.h index d645ac627..6479c972e 100644 --- a/os/net/ipv6/uip-ds6-nbr.h +++ b/os/net/ipv6/uip-ds6-nbr.h @@ -54,6 +54,10 @@ #if UIP_CONF_IPV6_QUEUE_PKT #include "net/ipv6/uip-packetqueue.h" #endif /*UIP_CONF_QUEUE_PKT */ +#if UIP_DS6_NBR_CONF_MULTI_IPV6_ADDRS +#include "lib/assert.h" +#include "lib/list.h" +#endif /*--------------------------------------------------*/ /** \brief Possible states for the nbr cache entries */ @@ -63,10 +67,46 @@ #define NBR_DELAY 3 #define NBR_PROBE 4 -NBR_TABLE_DECLARE(ds6_neighbors); +/** \brief Set non-zero (1) to enable multiple IPv6 addresses to be + * associated with a link-layer address */ +#ifdef UIP_DS6_NBR_CONF_MULTI_IPV6_ADDRS +#define UIP_DS6_NBR_MULTI_IPV6_ADDRS UIP_DS6_NBR_CONF_MULTI_IPV6_ADDRS +#else +#define UIP_DS6_NBR_MULTI_IPV6_ADDRS 0 +#endif /* UIP_DS6_NBR_CONF_MULTI_IPV6_ADDRS */ -/** \brief An entry in the nbr cache */ +/** \brief Set the maximum number of IPv6 addresses per link-layer + * address */ +#ifdef UIP_DS6_NBR_CONF_MAX_6ADDRS_PER_NBR +#define UIP_DS6_NBR_MAX_6ADDRS_PER_NBR UIP_DS6_NBR_CONF_MAX_6ADDRS_PER_NBR +#else +#define UIP_DS6_NBR_MAX_6ADDRS_PER_NBR 2 +#endif /* UIP_DS6_NBR_CONF_MAX_6ADDRS_PER_NBR */ + +/** \brief Set the maximum number of neighbor cache entries */ +#ifdef UIP_DS6_NBR_CONF_MAX_NEIGHBOR_CACHES +#define UIP_DS6_NBR_MAX_NEIGHBOR_CACHES UIP_DS6_NBR_CONF_MAX_NEIGHBOR_CACHES +#else +#define UIP_DS6_NBR_MAX_NEIGHBOR_CACHES \ + (NBR_TABLE_MAX_NEIGHBORS * UIP_DS6_NBR_MAX_6ADDRS_PER_NBR) +#endif /* UIP_DS6_NBR_CONF_MAX_NEIGHBOR_CACHES */ + +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS +/** \brief nbr_table entry when UIP_DS6_NBR_MULTI_IPV6_ADDRS is + * enabled. uip_ds6_nbrs is a list of uip_ds6_nbr_t objects */ +typedef struct { + LIST_STRUCT(uip_ds6_nbrs); +} uip_ds6_nbr_entry_t; +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ + +/** \brief The default nbr_table entry (when + * UIP_DS6_NBR_MULTI_IPV6_ADDRS is disabled), that implements nbr + * cache */ typedef struct uip_ds6_nbr { +#if UIP_DS6_NBR_MULTI_IPV6_ADDRS + struct uip_ds6_nbr *next; + uip_ds6_nbr_entry_t *nbr_entry; +#endif /* UIP_DS6_NBR_MULTI_IPV6_ADDRS */ uip_ipaddr_t ipaddr; uint8_t isrouter; uint8_t state; @@ -83,25 +123,119 @@ typedef struct uip_ds6_nbr { void uip_ds6_neighbors_init(void); -/** \brief Neighbor Cache basic routines */ +/** + * Add a neighbor cache for a specified IPv6 address, which is + * associated with a specified link-layer address + * \param ipaddr IPv6 address of a neighbor to add + * \param lladdr Link-layer address to associate with ipaddr + * \param isrouter Set 1 if the neighbor is a router + * \param state Set the initial neighbor cache state (e.g., + * NBR_INCOMPLETE) + * \param reason Set a reason of the addition (e.g., + * NBR_TABLE_REASON_RPL_DIO) + * \param data Set data associated with the nbr cache + * \return the address of a newly added nbr cache on success, NULL on + * failure +*/ uip_ds6_nbr_t *uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state, nbr_table_reason_t reason, void *data); + +/** + * Remove a neighbor cache + * \param nbr the address of a neighbor cache to remove + * \return 1 on success, 0 on failure (nothing was removed) + */ int uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr); + +/** + * Get the link-layer address associated with a specified nbr cache + * \param nbr the address of a neighbor cache + * \return pointer to the link-layer address on success, NULL on failure + */ const uip_lladdr_t *uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr); -int uip_ds6_nbr_update_ll(uip_ds6_nbr_t **nbr, const uip_lladdr_t *new_ll_addr); -const uip_ipaddr_t *uip_ds6_nbr_get_ipaddr(const uip_ds6_nbr_t *nbr); -uip_ds6_nbr_t *uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr); -uip_ds6_nbr_t *uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr); -uip_ipaddr_t *uip_ds6_nbr_ipaddr_from_lladdr(const uip_lladdr_t *lladdr); + +/** + * Get the link-layer address associated with a specified IPv6 address + * \param ipaddr an IPv6 address used as a search key + * \return the pointer to the link-layer address on success, NULL on failure + */ const uip_lladdr_t *uip_ds6_nbr_lladdr_from_ipaddr(const uip_ipaddr_t *ipaddr); -void uip_ds6_link_callback(int status, int numtx); -void uip_ds6_neighbor_periodic(void); + +/** + * Update the link-layer address associated with an IPv6 address + * \param nbr the double pointer to a neighbor cache which has the + * target IPv6 address + * \param new_ll_addr the new link-layer address of the IPv6 address + * return 0 on success, -1 on failure + */ +int uip_ds6_nbr_update_ll(uip_ds6_nbr_t **nbr, const uip_lladdr_t *new_ll_addr); + +/** + * Get an IPv6 address of a neighbor cache + * \param nbr the pointer to a neighbor cache + * \return the pointer to an IPv6 address associated with the neighbor cache + * \note This returns the first IPv6 address found in the neighbor + * cache when UIP_DS6_NBR_MULTI_IPV6_ADDRS is enabled + */ +const uip_ipaddr_t *uip_ds6_nbr_get_ipaddr(const uip_ds6_nbr_t *nbr); + +/** + * Get an IPv6 address associated with a specified link-layer address + * \param lladdr a link-layer address used as a search key + * \return the pointer to an IPv6 address associated with the neighbor cache + * \note This returns the first IPv6 address found in the neighbor + * cache when UIP_DS6_NBR_MULTI_IPV6_ADDRS is enabled + */ +uip_ipaddr_t *uip_ds6_nbr_ipaddr_from_lladdr(const uip_lladdr_t *lladdr); + +/** + * Get the neighbor cache associated with a specified IPv6 address + * \param ipaddr an IPv6 address used as a search key + * \return the pointer to a neighbor cache on success, NULL on failure + */ +uip_ds6_nbr_t *uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr); + +/** + * Get the neighbor cache associated with a specified link-layer address + * \param lladdr a link-layer address used as a search key + * \return the pointer to a neighbor cache on success, NULL on failure + */ +uip_ds6_nbr_t *uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr); + +/** + * Return the number of neighbor caches + * \return the number of neighbor caches in use + */ int uip_ds6_nbr_num(void); + +/** + * Get the first neighbor cache in nbr_table + * \return the pointer to the first neighbor cache entry + */ uip_ds6_nbr_t *uip_ds6_nbr_head(void); + +/** + * Get the next neighbor cache of a specified one + * \param nbr the pointer to a neighbor cache + * \return the pointer to the next one on success, NULL on failure + */ uip_ds6_nbr_t *uip_ds6_nbr_next(uip_ds6_nbr_t *nbr); +/** + * The callback function to update link-layer stats in a neighbor + * cache + * \param status MAC return value defined in mac.h + * \param numtx the number of transmissions happened for a packet + */ +void uip_ds6_link_callback(int status, int numtx); + +/** + * The housekeeping function called periodically + */ +void uip_ds6_neighbor_periodic(void); + #if UIP_ND6_SEND_NS /** * \brief Refresh the reachable state of a neighbor. This function diff --git a/os/net/ipv6/uip-ds6.c b/os/net/ipv6/uip-ds6.c index 4e82b3745..bcfb9b0c8 100644 --- a/os/net/ipv6/uip-ds6.c +++ b/os/net/ipv6/uip-ds6.c @@ -95,10 +95,29 @@ static uip_ds6_prefix_t *locprefix; static const uint8_t iid_prefix[] = { 0x00, 0x00 , 0x00 , 0xff , 0xfe , 0x00 }; #endif /* (UIP_LLADDR_LEN == 2) */ +/* The default prefix */ +static uip_ip6addr_t default_prefix = { + .u16 = { 0, 0, 0, 0, 0, 0, 0, 0 } +}; +/*---------------------------------------------------------------------------*/ +const uip_ip6addr_t * +uip_ds6_default_prefix() +{ + return &default_prefix; +} +/*---------------------------------------------------------------------------*/ +void +uip_ds6_set_default_prefix(const uip_ip6addr_t *prefix) +{ + uip_ip6addr_copy(&default_prefix, prefix); +} /*---------------------------------------------------------------------------*/ void uip_ds6_init(void) { + if(uip_is_addr_unspecified(&default_prefix)) { + uip_ip6addr(&default_prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + } uip_ds6_neighbors_init(); uip_ds6_route_init(); diff --git a/os/net/ipv6/uip-ds6.h b/os/net/ipv6/uip-ds6.h index 45394fec3..0edc1fd6d 100644 --- a/os/net/ipv6/uip-ds6.h +++ b/os/net/ipv6/uip-ds6.h @@ -296,6 +296,22 @@ uip_ds6_prefix_t *uip_ds6_prefix_lookup(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen); uint8_t uip_ds6_is_addr_onlink(uip_ipaddr_t *ipaddr); +/** + * \brief Retrieve the Default IPv6 prefix + * \retval A pointer to the default prefix + */ +const uip_ip6addr_t *uip_ds6_default_prefix(void); + +/** + * \brief Set the Default IPv6 prefix + * \param prefix A pointer to the new default prefix + * + * uip_ds6_init() will set the default prefix to UIP_DS6_DEFAULT_PREFIX + * unless this function here has been called beforehand to set a new default + * prefix. + */ +void uip_ds6_set_default_prefix(const uip_ip6addr_t *prefix); + /** @} */ /** \name Unicast address list basic routines */ diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index 707a13309..18dcef89c 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -53,11 +53,7 @@ #define LOG_MODULE "ICMPv6" #define LOG_LEVEL LOG_LEVEL_IPV6 -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_FIRST_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLIPH_LEN]) +#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)UIP_ICMP_PAYLOAD) /** \brief temporary IP address */ static uip_ipaddr_t tmp_ipaddr; @@ -134,22 +130,7 @@ echo_request_input(void) uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &tmp_ipaddr); } - if(uip_ext_len > 0) { - /* Remove extension headers if any */ - UIP_IP_BUF->proto = UIP_PROTO_ICMP6; - uip_len -= uip_ext_len; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); - /* move the echo request payload (starting after the icmp header) - * to the new location in the reply. - * The shift is equal to the length of the extension headers present - * Note: UIP_ICMP_BUF still points to the echo request at this stage - */ - memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len, - (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN, - (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN)); - uip_ext_len = 0; - } + uip_remove_ext_hdr(); /* Below is important for the correctness of UIP_ICMP_BUF and the * checksum @@ -173,47 +154,38 @@ echo_request_input(void) void uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { /* check if originating packet is not an ICMP error */ - if(uip_ext_len) { - if(UIP_EXT_BUF->next == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { - uip_clear_buf(); - return; - } - } else { - if(UIP_IP_BUF->proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { - uip_clear_buf(); - return; - } + uint16_t shift; + if(uip_last_proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { + uipbuf_clear(); + return; } - /* Remove all extension headers related to the routing protocol in place */ + /* Remove all extension headers related to the routing protocol in place. + * Keep all other extension headers, so as to match original packet. */ NETSTACK_ROUTING.ext_header_remove(); /* remember data of original packet before shifting */ uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->destipaddr); - uip_len += UIP_IPICMPH_LEN + UIP_ICMP6_ERROR_LEN; - - if(uip_len > UIP_LINK_MTU) { - uip_len = UIP_LINK_MTU; - } - - memmove((uint8_t *)UIP_ICMP6_ERROR_BUF + uip_ext_len + UIP_ICMP6_ERROR_LEN, - (void *)UIP_IP_BUF, uip_len - UIP_IPICMPH_LEN - uip_ext_len - UIP_ICMP6_ERROR_LEN); + /* The ICMPv6 error message contains as much of possible of the invoking packet + * (see RFC 4443 section 3). Make space for the additional IPv6 and + * ICMPv6 headers here and move payload to the "right". What we move includes + * extension headers */ + shift = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ICMP6_ERROR_LEN; + uip_len += shift; + uip_len = MIN(uip_len, UIP_LINK_MTU); + memmove(uip_buf + shift, (void *)UIP_IP_BUF, uip_len - shift); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; - if (uip_ext_len) { - UIP_FIRST_EXT_BUF->next = UIP_PROTO_ICMP6; - } else { - UIP_IP_BUF->proto = UIP_PROTO_ICMP6; - } + UIP_IP_BUF->proto = UIP_PROTO_ICMP6; UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit; /* the source should not be unspecified nor multicast, the check for multicast is done in uip_process */ if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)){ - uip_clear_buf(); + uipbuf_clear(); return; } @@ -223,7 +195,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { if(type == ICMP6_PARAM_PROB && code == ICMP6_PARAMPROB_OPTION){ uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr); } else { - uip_clear_buf(); + uipbuf_clear(); return; } } else { @@ -234,8 +206,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { UIP_ICMP_BUF->type = type; UIP_ICMP_BUF->icode = code; UIP_ICMP6_ERROR_BUF->param = uip_htonl(param); - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); UIP_ICMP_BUF->icmpchksum = 0; UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum(); @@ -258,8 +229,7 @@ uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len) UIP_IP_BUF->flow = 0; UIP_IP_BUF->proto = UIP_PROTO_ICMP6; UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit; - UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8; - UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + payload_len); memcpy(&UIP_IP_BUF->destipaddr, dest, sizeof(*dest)); uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); @@ -297,22 +267,7 @@ echo_reply_input(void) uip_ipaddr_copy(&sender, &UIP_IP_BUF->srcipaddr); ttl = UIP_IP_BUF->ttl; - if(uip_ext_len > 0) { - /* Remove extension headers if any */ - UIP_IP_BUF->proto = UIP_PROTO_ICMP6; - uip_len -= uip_ext_len; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); - /* move the echo reply payload (starting after the icmp header) - * to the new location in the reply. The shift is equal to the - * length of the extension headers present Note: UIP_ICMP_BUF - * still points to the echo request at this stage - */ - memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len, - (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN, - (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN)); - uip_ext_len = 0; - } + uip_remove_ext_hdr(); /* Call all registered applications to let them know an echo reply has been received. */ @@ -329,7 +284,7 @@ echo_reply_input(void) } } - uip_clear_buf(); + uipbuf_clear(); return; } /*---------------------------------------------------------------------------*/ diff --git a/os/net/ipv6/uip-icmp6.h b/os/net/ipv6/uip-icmp6.h index 2808ea7de..5f8caf080 100644 --- a/os/net/ipv6/uip-icmp6.h +++ b/os/net/ipv6/uip-icmp6.h @@ -38,7 +38,7 @@ /** * \file * Header file for ICMPv6 message and error handing (RFC 4443) - * \author Julien Abeille + * \author Julien Abeille * \author Mathilde Durvy */ @@ -52,8 +52,8 @@ /** @{ */ #define ICMP6_DST_UNREACH 1 /**< dest unreachable */ #define ICMP6_PACKET_TOO_BIG 2 /**< packet too big */ -#define ICMP6_TIME_EXCEEDED 3 /**< time exceeded */ -#define ICMP6_PARAM_PROB 4 /**< ip6 header bad */ +#define ICMP6_TIME_EXCEEDED 3 /**< time exceeded */ +#define ICMP6_PARAM_PROB 4 /**< ip6 header bad */ #define ICMP6_ECHO_REQUEST 128 /**< Echo request */ #define ICMP6_ECHO_REPLY 129 /**< Echo reply */ @@ -76,10 +76,10 @@ /** \name ICMPv6 Destination Unreachable message codes*/ /** @{ */ #define ICMP6_DST_UNREACH_NOROUTE 0 /**< no route to destination */ -#define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */ +#define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */ #define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /**< not a neighbor(obsolete) */ #define ICMP6_DST_UNREACH_BEYONDSCOPE 2 /**< beyond scope of source address */ -#define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */ +#define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */ #define ICMP6_DST_UNREACH_NOPORT 4 /**< port unreachable */ /** @} */ @@ -116,7 +116,7 @@ typedef struct uip_icmp6_error{ * \param param 32 bit parameter of the error message, semantic depends on error */ void -uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param); +uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param); /** * \brief Send an icmpv6 message diff --git a/os/net/ipv6/uip-nameserver.c b/os/net/ipv6/uip-nameserver.c index 83a1943b0..755b33992 100644 --- a/os/net/ipv6/uip-nameserver.c +++ b/os/net/ipv6/uip-nameserver.c @@ -108,7 +108,7 @@ uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime) * the the eldest ones */ } } - + if(e == NULL) { if((e = memb_alloc(&dnsmemb)) != NULL) { list_add(dns, e); @@ -221,11 +221,7 @@ uip_nameserver_count(void) } return list_length(dns); #else /* UIP_NAMESERVER_POOL_SIZE > 1 */ -#if NETSTACK_CONF_WITH_IPV6 if(uip_is_addr_unspecified(&serveraddr)) { -#else /* NETSTACK_CONF_WITH_IPV6 */ - if(uip_ipaddr_cmp(&serveraddr, &uip_all_zeroes_addr)) { -#endif /* NETSTACK_CONF_WITH_IPV6 */ return 0; } else { return 1; diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 63ae46b47..f6efc83de 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -84,29 +84,20 @@ /*------------------------------------------------------------------*/ /** @{ */ /** \name Pointers to the header structures. - * All pointers except UIP_IP_BUF depend on uip_ext_len, which at - * packet reception, is the total length of the extension headers. - * - * The pointer to ND6 options header also depends on nd6_opt_offset, - * which we set in each function. - * - * Care should be taken when manipulating these buffers about the - * value of these length variables */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) /**< Pointer to IP header */ -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) /**< Pointer to ICMP header*/ /**@{ Pointers to messages just after icmp header */ -#define UIP_ND6_RS_BUF ((uip_nd6_rs *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_ND6_RA_BUF ((uip_nd6_ra *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_ND6_NS_BUF ((uip_nd6_ns *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_ND6_NA_BUF ((uip_nd6_na *)&uip_buf[uip_l2_l3_icmp_hdr_len]) +#define UIP_ND6_RS_BUF ((uip_nd6_rs *)UIP_ICMP_PAYLOAD) +#define UIP_ND6_RA_BUF ((uip_nd6_ra *)UIP_ICMP_PAYLOAD) +#define UIP_ND6_NS_BUF ((uip_nd6_ns *)UIP_ICMP_PAYLOAD) +#define UIP_ND6_NA_BUF ((uip_nd6_na *)UIP_ICMP_PAYLOAD) /** @} */ /** Pointer to ND option */ -#define UIP_ND6_OPT_HDR_BUF ((uip_nd6_opt_hdr *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) -#define UIP_ND6_OPT_PREFIX_BUF ((uip_nd6_opt_prefix_info *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) -#define UIP_ND6_OPT_MTU_BUF ((uip_nd6_opt_mtu *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) -#define UIP_ND6_OPT_RDNSS_BUF ((uip_nd6_opt_dns *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) +#define ND6_OPT(opt) ((unsigned char *)(UIP_ICMP_PAYLOAD + (opt))) +#define ND6_OPT_HDR_BUF(opt) ((uip_nd6_opt_hdr *)ND6_OPT(opt)) +#define ND6_OPT_PREFIX_BUF(opt) ((uip_nd6_opt_prefix_info *)ND6_OPT(opt)) +#define ND6_OPT_MTU_BUF(opt) ((uip_nd6_opt_mtu *)ND6_OPT(opt)) +#define ND6_OPT_RDNSS_BUF(opt) ((uip_nd6_opt_dns *)ND6_OPT(opt)) /** @} */ #if UIP_ND6_SEND_NS || UIP_ND6_SEND_NA || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER @@ -203,14 +194,14 @@ ns_input(void) nd6_opt_offset = UIP_ND6_NS_LEN; while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { #if UIP_CONF_IPV6_CHECKS - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("NS received is bad\n"); goto discard; } #endif /* UIP_CONF_IPV6_CHECKS */ - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: - nd6_opt_llao = &uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]; + nd6_opt_llao = &uip_buf[uip_l3_icmp_hdr_len + nd6_opt_offset]; #if UIP_CONF_IPV6_CHECKS /* There must be NO option in a DAD NS */ if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) { @@ -252,7 +243,7 @@ ns_input(void) LOG_WARN("ND option not supported in NS"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } addr = uip_ds6_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr); @@ -323,12 +314,11 @@ create_na: #if UIP_CONF_ROUTER flags = flags | UIP_ND6_NA_FLAG_ROUTER; #endif - uip_ext_len = 0; + uipbuf_clear(); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; - UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */ - UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN); UIP_IP_BUF->proto = UIP_PROTO_ICMP6; UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT; @@ -338,14 +328,13 @@ create_na: UIP_ND6_NA_BUF->flagsreserved = flags; memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &addr->ipaddr, sizeof(uip_ipaddr_t)); - create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN], + create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_NA_LEN], UIP_ND6_OPT_TLLAO); UIP_ICMP_BUF->icmpchksum = 0; UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum(); - uip_len = - UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len(UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN); UIP_STAT(++uip_stat.nd6.sent); LOG_INFO("Sending NA to "); @@ -358,7 +347,7 @@ create_na: return; discard: - uip_clear_buf(); + uipbuf_clear(); return; } #endif /* UIP_ND6_SEND_NA */ @@ -369,7 +358,7 @@ discard: void uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) { - uip_ext_len = 0; + uipbuf_clear(); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; @@ -385,7 +374,6 @@ uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) UIP_ICMP_BUF->icode = 0; UIP_ND6_NS_BUF->reserved = 0; uip_ipaddr_copy((uip_ipaddr_t *) &UIP_ND6_NS_BUF->tgtipaddr, tgt); - UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */ /* * check if we add a SLLAO option: for DAD, MUST NOT, for NUD, MAY * (here yes), for Address resolution , MUST @@ -398,13 +386,12 @@ uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) } if (uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) { LOG_ERR("Dropping NS due to no suitable source address\n"); - uip_clear_buf(); + uipbuf_clear(); return; } - UIP_IP_BUF->len[1] = - UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN); - create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN], + create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_NS_LEN], UIP_ND6_OPT_SLLAO); uip_len = @@ -493,20 +480,20 @@ na_input(void) nd6_opt_llao = NULL; while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { #if UIP_CONF_IPV6_CHECKS - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("NA received is bad\n"); goto discard; } #endif /*UIP_CONF_IPV6_CHECKS */ - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_TLLAO: - nd6_opt_llao = (uint8_t *)UIP_ND6_OPT_HDR_BUF; + nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset); break; default: LOG_WARN("ND option not supported in NA\n"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } addr = uip_ds6_addr_lookup(&UIP_ND6_NA_BUF->tgtipaddr); /* Message processing, including TLLAO if any */ @@ -605,7 +592,7 @@ na_input(void) #endif /*UIP_CONF_IPV6_QUEUE_PKT */ discard: - uip_clear_buf(); + uipbuf_clear(); return; } #endif /* UIP_ND6_SEND_NS */ @@ -644,20 +631,20 @@ rs_input(void) while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { #if UIP_CONF_IPV6_CHECKS - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("RS received is bad\n"); goto discard; } #endif /*UIP_CONF_IPV6_CHECKS */ - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: - nd6_opt_llao = (uint8_t *)UIP_ND6_OPT_HDR_BUF; + nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset); break; default: LOG_WARN("ND option not supported in RS\n"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } /* Options processing: only SLLAO */ if(nd6_opt_llao != NULL) { @@ -701,7 +688,7 @@ rs_input(void) uip_ds6_send_ra_sollicited(); discard: - uip_clear_buf(); + uipbuf_clear(); return; } @@ -746,31 +733,31 @@ uip_nd6_ra_output(uip_ipaddr_t * dest) for(prefix = uip_ds6_prefix_list; prefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; prefix++) { if((prefix->isused) && (prefix->advertise)) { - UIP_ND6_OPT_PREFIX_BUF->type = UIP_ND6_OPT_PREFIX_INFO; - UIP_ND6_OPT_PREFIX_BUF->len = UIP_ND6_OPT_PREFIX_INFO_LEN / 8; - UIP_ND6_OPT_PREFIX_BUF->preflen = prefix->length; - UIP_ND6_OPT_PREFIX_BUF->flagsreserved1 = prefix->l_a_reserved; - UIP_ND6_OPT_PREFIX_BUF->validlt = uip_htonl(prefix->vlifetime); - UIP_ND6_OPT_PREFIX_BUF->preferredlt = uip_htonl(prefix->plifetime); - UIP_ND6_OPT_PREFIX_BUF->reserved2 = 0; - uip_ipaddr_copy(&(UIP_ND6_OPT_PREFIX_BUF->prefix), &(prefix->ipaddr)); + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_PREFIX_INFO; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_PREFIX_INFO_LEN / 8; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->preflen = prefix->length; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->flagsreserved1 = prefix->l_a_reserved; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->validlt = uip_htonl(prefix->vlifetime); + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->preferredlt = uip_htonl(prefix->plifetime); + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->reserved2 = 0; + uip_ipaddr_copy(&(ND6_OPT_PREFIX_BUF(nd6_opt_offset)->prefix), &(prefix->ipaddr)); nd6_opt_offset += UIP_ND6_OPT_PREFIX_INFO_LEN; uip_len += UIP_ND6_OPT_PREFIX_INFO_LEN; } } /* Source link-layer option */ - create_llao((uint8_t *)UIP_ND6_OPT_HDR_BUF, UIP_ND6_OPT_SLLAO); + create_llao((uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset), UIP_ND6_OPT_SLLAO); uip_len += UIP_ND6_OPT_LLAO_LEN; nd6_opt_offset += UIP_ND6_OPT_LLAO_LEN; /* MTU */ - UIP_ND6_OPT_MTU_BUF->type = UIP_ND6_OPT_MTU; - UIP_ND6_OPT_MTU_BUF->len = UIP_ND6_OPT_MTU_LEN >> 3; - UIP_ND6_OPT_MTU_BUF->reserved = 0; - //UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(uip_ds6_if.link_mtu); - UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(1500); + ND6_OPT_MTU_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_MTU; + ND6_OPT_MTU_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_MTU_LEN >> 3; + ND6_OPT_MTU_BUF(nd6_opt_offset)->reserved = 0; + //ND6_OPT_MTU_BUF(nd6_opt_offset)->mtu = uip_htonl(uip_ds6_if.link_mtu); + ND6_OPT_MTU_BUF(nd6_opt_offset)->mtu = uip_htonl(1500); uip_len += UIP_ND6_OPT_MTU_LEN; nd6_opt_offset += UIP_ND6_OPT_MTU_LEN; @@ -778,27 +765,26 @@ uip_nd6_ra_output(uip_ipaddr_t * dest) #if UIP_ND6_RA_RDNSS if(uip_nameserver_count() > 0) { uint8_t i = 0; - uip_ipaddr_t *ip = &UIP_ND6_OPT_RDNSS_BUF->ip; + uip_ipaddr_t *ip = &ND6_OPT_RDNSS_BUF(nd6_opt_offset)->ip; uip_ipaddr_t *dns = NULL; - UIP_ND6_OPT_RDNSS_BUF->type = UIP_ND6_OPT_RDNSS; - UIP_ND6_OPT_RDNSS_BUF->reserved = 0; - UIP_ND6_OPT_RDNSS_BUF->lifetime = uip_nameserver_next_expiration(); - if(UIP_ND6_OPT_RDNSS_BUF->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) { - UIP_ND6_OPT_RDNSS_BUF->lifetime -= clock_seconds(); + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_RDNSS; + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->reserved = 0; + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime = uip_nameserver_next_expiration(); + if(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) { + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime -= clock_seconds(); } while((dns = uip_nameserver_get(i)) != NULL) { uip_ipaddr_copy(ip++, dns); i++; } - UIP_ND6_OPT_RDNSS_BUF->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1); + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1); LOG_INFO("%d nameservers reported\n", i); - uip_len += UIP_ND6_OPT_RDNSS_BUF->len << 3; - nd6_opt_offset += UIP_ND6_OPT_RDNSS_BUF->len << 3; + uip_len += ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len << 3; + nd6_opt_offset += ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len << 3; } #endif /* UIP_ND6_RA_RDNSS */ - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); /*ICMP checksum */ UIP_ICMP_BUF->icmpchksum = 0; @@ -829,17 +815,15 @@ uip_nd6_rs_output(void) uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); UIP_ICMP_BUF->type = ICMP6_RS; UIP_ICMP_BUF->icode = 0; - UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */ if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) { UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_RS_LEN; uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN; } else { uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN; - UIP_IP_BUF->len[1] = - UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN); - create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_RS_LEN], + create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN], UIP_ND6_OPT_SLLAO); } @@ -904,14 +888,14 @@ ra_input(void) /* Options processing */ nd6_opt_offset = UIP_ND6_RA_LEN; while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("RA received is bad"); goto discard; } - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: LOG_DBG("Processing SLLAO option in RA\n"); - nd6_opt_llao = (uint8_t *) UIP_ND6_OPT_HDR_BUF; + nd6_opt_llao = (uint8_t *) ND6_OPT_HDR_BUF(nd6_opt_offset); nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr); if(!extract_lladdr_from_llao_aligned(&lladdr_aligned)) { /* failed to extract llao - discard packet */ @@ -944,11 +928,11 @@ ra_input(void) case UIP_ND6_OPT_MTU: LOG_DBG("Processing MTU option in RA\n"); uip_ds6_if.link_mtu = - uip_ntohl(((uip_nd6_opt_mtu *) UIP_ND6_OPT_HDR_BUF)->mtu); + uip_ntohl(((uip_nd6_opt_mtu *) ND6_OPT_HDR_BUF(nd6_opt_offset))->mtu); break; case UIP_ND6_OPT_PREFIX_INFO: LOG_DBG("Processing PREFIX option in RA\n"); - nd6_opt_prefix_info = (uip_nd6_opt_prefix_info *) UIP_ND6_OPT_HDR_BUF; + nd6_opt_prefix_info = (uip_nd6_opt_prefix_info *) ND6_OPT_HDR_BUF(nd6_opt_offset); if((uip_ntohl(nd6_opt_prefix_info->validlt) >= uip_ntohl(nd6_opt_prefix_info->preferredlt)) && (!uip_is_addr_linklocal(&nd6_opt_prefix_info->prefix))) { @@ -1035,14 +1019,14 @@ ra_input(void) #if UIP_ND6_RA_RDNSS case UIP_ND6_OPT_RDNSS: LOG_DBG("Processing RDNSS option\n"); - uint8_t naddr = (UIP_ND6_OPT_RDNSS_BUF->len - 1) / 2; - uip_ipaddr_t *ip = (uip_ipaddr_t *)(&UIP_ND6_OPT_RDNSS_BUF->ip); + uint8_t naddr = (ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len - 1) / 2; + uip_ipaddr_t *ip = (uip_ipaddr_t *)(&ND6_OPT_RDNSS_BUF(nd6_opt_offset)->ip); LOG_DBG("got %d nameservers\n", naddr); while(naddr-- > 0) { LOG_DBG("nameserver: "); LOG_DBG_6ADDR(ip); - LOG_DBG_(" lifetime: %"PRIx32"\n", uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime)); - uip_nameserver_update(ip, uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime)); + LOG_DBG_(" lifetime: %"PRIx32"\n", uip_ntohl(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime)); + uip_nameserver_update(ip, uip_ntohl(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime)); ip++; } break; @@ -1051,7 +1035,7 @@ ra_input(void) LOG_ERR("ND option not supported in RA\n"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } defrt = uip_ds6_defrt_lookup(&UIP_IP_BUF->srcipaddr); @@ -1092,7 +1076,7 @@ ra_input(void) #endif /*UIP_CONF_IPV6_QUEUE_PKT */ discard: - uip_clear_buf(); + uipbuf_clear(); return; } #endif /* !UIP_CONF_ROUTER */ diff --git a/os/net/ipv6/uip-packetqueue.h b/os/net/ipv6/uip-packetqueue.h index b265b1ecf..1deacb9ba 100644 --- a/os/net/ipv6/uip-packetqueue.h +++ b/os/net/ipv6/uip-packetqueue.h @@ -7,7 +7,7 @@ struct uip_packetqueue_handle; struct uip_packetqueue_packet { struct uip_ds6_queued_packet *next; - uint8_t queue_buf[UIP_BUFSIZE - UIP_LLH_LEN]; + uint8_t queue_buf[UIP_BUFSIZE]; uint16_t queue_buf_len; struct ctimer lifetimer; struct uip_packetqueue_handle *handle; diff --git a/os/net/ipv6/uip-udp-packet.c b/os/net/ipv6/uip-udp-packet.c index 91ddf598c..c5526b162 100644 --- a/os/net/ipv6/uip-udp-packet.c +++ b/os/net/ipv6/uip-udp-packet.c @@ -51,10 +51,10 @@ void uip_udp_packet_send(struct uip_udp_conn *c, const void *data, int len) { #if UIP_UDP - if(data != NULL && len <= (UIP_BUFSIZE - (UIP_LLH_LEN + UIP_IPUDPH_LEN))) { + if(data != NULL && len <= (UIP_BUFSIZE - UIP_IPUDPH_LEN)) { uip_udp_conn = c; uip_slen = len; - memmove(&uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], data, len); + memmove(&uip_buf[UIP_IPUDPH_LEN], data, len); uip_process(UIP_UDP_SEND_CONN); #if UIP_IPV6_MULTICAST diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 9f5eb159d..32ffc6ed4 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -62,22 +62,24 @@ #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN) /* Size of IP + UDP header */ #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP + TCP header */ -#define UIP_TCPIP_HLEN UIP_IPTCPH_LEN -#define UIP_IPICMPH_LEN (UIP_IPH_LEN + UIP_ICMPH_LEN) /* Size of ICMP + IP header */ -#define UIP_LLIPH_LEN (UIP_LLH_LEN + UIP_IPH_LEN) /* Size of L2 + IP header */ -#if NETSTACK_CONF_WITH_IPV6 -/** - * The sums below are quite used in ND. When used for uip_buf, we - * include link layer length when used for uip_len, we do not, hence - * we need values with and without LLH_LEN we do not use capital - * letters as these values are variable - */ -#define uip_l2_l3_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len) -#define uip_l2_l3_icmp_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) -#define uip_l3_hdr_len (UIP_IPH_LEN + uip_ext_len) -#define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) -#endif /*NETSTACK_CONF_WITH_IPV6*/ +#define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) + +/** + * Direct access to IPv6 header + */ +#define UIP_IP_BUF ((struct uip_ip_hdr *)uip_buf) +#define UIP_IP_PAYLOAD(ext) ((unsigned char *)uip_buf + UIP_IPH_LEN + (ext)) + +/** + * Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_ext_len) + */ +#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) +#define UIP_ICMP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_ICMPH_LEN) +#define UIP_UDP_BUF ((struct uip_udp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) +#define UIP_UDP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_UDPH_LEN) +#define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) +#define UIP_TCP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_TCPH_LEN) #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" @@ -100,12 +102,7 @@ typedef union uip_ip6addr_t { uint16_t u16[8]; } uip_ip6addr_t; -#if NETSTACK_CONF_WITH_IPV6 typedef uip_ip6addr_t uip_ipaddr_t; -#else /* NETSTACK_CONF_WITH_IPV6 */ -typedef uip_ip4addr_t uip_ipaddr_t; -#endif /* NETSTACK_CONF_WITH_IPV6 */ - /*---------------------------------------------------------------------------*/ #define UIP_802154_SHORTADDR_LEN 2 @@ -1048,11 +1045,7 @@ struct uip_udp_conn *uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport); (addr1)->u16[1] == (addr2)->u16[1]) #define uip_ip6addr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0) -#if NETSTACK_CONF_WITH_IPV6 #define uip_ipaddr_cmp(addr1, addr2) uip_ip6addr_cmp(addr1, addr2) -#else /* NETSTACK_CONF_WITH_IPV6 */ -#define uip_ipaddr_cmp(addr1, addr2) uip_ip4addr_cmp(addr1, addr2) -#endif /* NETSTACK_CONF_WITH_IPV6 */ /** * Compare two IP addresses with netmasks @@ -1308,31 +1301,16 @@ extern uint16_t uip_len; * The length of the extension headers */ extern uint8_t uip_ext_len; + +/** The final protocol after IPv6 extension headers: + * UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6 */ +extern uint8_t uip_last_proto; /** @} */ #if UIP_URGDATA > 0 extern uint16_t uip_urglen, uip_surglen; #endif /* UIP_URGDATA > 0 */ -/* - * Clear uIP buffer - * - * This function clears the uIP buffer by reseting the uip_len and - * uip_ext_len pointers. - */ -#if NETSTACK_CONF_WITH_IPV6 -#define uip_clear_buf() { \ - uip_len = 0; \ - uip_ext_len = 0; \ - uipbuf_clear_attr();\ -} -#else /*NETSTACK_CONF_WITH_IPV6*/ -#define uip_clear_buf() { \ - uip_len = 0; \ - uipbuf_clear_attr();\ -} -#endif /*NETSTACK_CONF_WITH_IPV6*/ - /** * Representation of a uIP TCP connection. * @@ -1494,13 +1472,11 @@ struct uip_stats { checksum. */ } udp; /**< UDP statistics. */ #endif /* UIP_UDP */ -#if NETSTACK_CONF_WITH_IPV6 struct { uip_stats_t drop; /**< Number of dropped ND6 packets. */ uip_stats_t recv; /**< Number of recived ND6 packets */ uip_stats_t sent; /**< Number of sent ND6 packets */ } nd6; -#endif /*NETSTACK_CONF_WITH_IPV6*/ }; @@ -1619,113 +1595,14 @@ void uip_process(uint8_t flag); #define UIP_STOPPED 16 -/* The TCP and IP headers. */ -struct uip_tcpip_hdr { -#if NETSTACK_CONF_WITH_IPV6 - /* IPv6 header. */ - uint8_t vtc, - tcflow; - uint16_t flow; - uint8_t len[2]; - uint8_t proto, ttl; - uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IPv4 header. */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ - - /* TCP header. */ - uint16_t srcport, - destport; - uint8_t seqno[4], - ackno[4], - tcpoffset, - flags, - wnd[2]; - uint16_t tcpchksum; - uint8_t urgp[2]; - uint8_t optdata[4]; -}; - -/* The ICMP and IP headers. */ -struct uip_icmpip_hdr { -#if NETSTACK_CONF_WITH_IPV6 - /* IPv6 header. */ - uint8_t vtc, - tcf; - uint16_t flow; - uint8_t len[2]; - uint8_t proto, ttl; - uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IPv4 header. */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ - - /* ICMP header. */ - uint8_t type, icode; - uint16_t icmpchksum; -#if !NETSTACK_CONF_WITH_IPV6 - uint16_t id, seqno; - uint8_t payload[1]; -#endif /* !NETSTACK_CONF_WITH_IPV6 */ -}; - - -/* The UDP and IP headers. */ -struct uip_udpip_hdr { -#if NETSTACK_CONF_WITH_IPV6 - /* IPv6 header. */ - uint8_t vtc, - tcf; - uint16_t flow; - uint8_t len[2]; - uint8_t proto, ttl; - uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IP header. */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ - - /* UDP header. */ - uint16_t srcport, - destport; - uint16_t udplen; - uint16_t udpchksum; -}; - /* * In IPv6 the length of the L3 headers before the transport header is * not fixed, due to the possibility to include extension option headers * after the IP header. hence we split here L3 and L4 headers */ /* The IP header */ + struct uip_ip_hdr { -#if NETSTACK_CONF_WITH_IPV6 /* IPV6 header */ uint8_t vtc; uint8_t tcflow; @@ -1733,18 +1610,6 @@ struct uip_ip_hdr { uint8_t len[2]; uint8_t proto, ttl; uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IPV4 header */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ }; @@ -1860,9 +1725,6 @@ struct uip_tcp_hdr { struct uip_icmp_hdr { uint8_t type, icode; uint16_t icmpchksum; -#if !NETSTACK_CONF_WITH_IPV6 - uint16_t id, seqno; -#endif /* !NETSTACK_CONF_WITH_IPV6 */ }; @@ -1889,8 +1751,7 @@ struct uip_udp_hdr { * * \hideinitializer */ -#define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) -#define UIP_APPDATA_PTR (void *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN] +#define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_IPTCPH_LEN) #define UIP_PROTO_ICMP 1 #define UIP_PROTO_TCP 6 @@ -1898,7 +1759,6 @@ struct uip_udp_hdr { #define UIP_PROTO_ICMP6 58 -#if NETSTACK_CONF_WITH_IPV6 /** @{ */ /** \brief extension headers types */ #define UIP_PROTO_HBHO 0 @@ -1908,7 +1768,7 @@ struct uip_udp_hdr { #define UIP_PROTO_NONE 59 /** @} */ -#define uip_is_proto_ext_hdr(proto) (proto == UIP_PROTO_HBHO || proto == UIP_PROTO_DESTO || proto == UIP_PROTO_ROUTING || proto == UIP_PROTO_FRAG || proto == UIP_PROTO_NONE) +#define uip_is_proto_ext_hdr(proto) ((proto) != UIP_PROTO_TCP && (proto) != UIP_PROTO_UDP && (proto) != UIP_PROTO_ICMP6) /** @{ */ /** \brief Destination and Hop By Hop extension headers option types */ @@ -1937,9 +1797,6 @@ struct uip_udp_hdr { /** @} */ -#endif /* NETSTACK_CONF_WITH_IPV6 */ - - #if UIP_FIXEDADDR extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr; #else /* UIP_FIXEDADDR */ @@ -1954,10 +1811,6 @@ extern const uip_lladdr_t uip_lladdr; extern uip_lladdr_t uip_lladdr; #endif - - - -#if NETSTACK_CONF_WITH_IPV6 /** Length of the link local prefix */ #define UIP_LLPREF_LEN 10 @@ -2174,8 +2027,6 @@ extern uip_lladdr_t uip_lladdr; (((a)->u8[14]) == ((b)->u8[14])) && \ (((a)->u8[15]) == ((b)->u8[15]))) -#endif /*NETSTACK_CONF_WITH_IPV6*/ - /** * A non-error message that indicates that a packet should be * processed locally. @@ -2288,6 +2139,11 @@ uint16_t uip_udpchksum(void); */ uint16_t uip_icmp6chksum(void); +/** + * Removes all IPv6 extension headers from uip_buf, updates length fields + * (uip_len and uip_ext_len) + */ +void uip_remove_ext_hdr(void); #endif /* UIP_H_ */ diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index dfbec52b7..24274a306 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -114,46 +114,26 @@ uip_lladdr_t uip_lladdr = {{0x00,0x06,0x98,0x00,0x02,0x32}}; * @{ */ /*---------------------------------------------------------------------------*/ -/** - * \brief Type of the next header in IPv6 header or extension headers - * - * Can be the next header field in the IPv6 header or in an extension header. - * When doing fragment reassembly, we must change the value of the next header - * field in the header before the fragmentation header, hence we need a pointer - * to this field. - */ -uint8_t *uip_next_hdr; /** \brief bitmap we use to record which IPv6 headers we have already seen */ uint8_t uip_ext_bitmap = 0; /** - * \brief length of the extension headers read. updated each time we process - * a header + * \brief Total length of all IPv6 extension headers */ uint8_t uip_ext_len = 0; -/** \brief length of the header options read */ -uint8_t uip_ext_opt_offset = 0; +/** \brief The final protocol after IPv6 extension headers: + * UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6 */ +uint8_t uip_last_proto = 0; /** @} */ /*---------------------------------------------------------------------------*/ /* Buffers */ /*---------------------------------------------------------------------------*/ /** - * \name Buffer defines + * \name Reassembly buffer definition * @{ */ -#define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0]) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ROUTING_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_FRAG_BUF ((struct uip_frag_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_DESTO_BUF ((struct uip_desto_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len]) +#define FBUF ((struct uip_ip_hdr *)&uip_reassbuf[0]) + /** @} */ /** * \name Buffer variables @@ -341,7 +321,7 @@ uip_ipchksum(void) { uint16_t sum; - sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN); + sum = chksum(0, uip_buf, UIP_IPH_LEN); LOG_DBG("uip_ipchksum: sum 0x%04x\n", sum); return (sum == 0) ? 0xffff : uip_htons(sum); } @@ -362,10 +342,10 @@ upper_layer_chksum(uint8_t proto) volatile uint16_t upper_layer_len; uint16_t sum; - upper_layer_len = (((uint16_t)(UIP_IP_BUF->len[0]) << 8) + UIP_IP_BUF->len[1] - uip_ext_len); + upper_layer_len = uipbuf_get_len_field(UIP_IP_BUF) - uip_ext_len; LOG_DBG("Upper layer checksum len: %d from: %d\n", upper_layer_len, - UIP_IPH_LEN + UIP_LLH_LEN + uip_ext_len); + (int)(UIP_IP_PAYLOAD(uip_ext_len) - uip_buf)); /* First sum pseudoheader. */ /* IP protocol and length fields. This addition cannot carry. */ @@ -373,9 +353,8 @@ upper_layer_chksum(uint8_t proto) /* Sum IP source and destination addresses. */ sum = chksum(sum, (uint8_t *)&UIP_IP_BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t)); - /* Sum TCP header and data. */ - sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN + uip_ext_len], - upper_layer_len); + /* Sum upper-layer header and data. */ + sum = chksum(sum, UIP_IP_PAYLOAD(uip_ext_len), upper_layer_len); return (sum == 0) ? 0xffff : uip_htons(sum); } @@ -409,6 +388,7 @@ uip_init(void) { int c; + uipbuf_init(); uip_ds6_init(); uip_icmp6_init(); uip_nd6_init(); @@ -510,29 +490,27 @@ uip_connect(const uip_ipaddr_t *ripaddr, uint16_t rport) #endif /* UIP_TCP && UIP_ACTIVE_OPEN */ /*---------------------------------------------------------------------------*/ void -remove_ext_hdr(void) +uip_remove_ext_hdr(void) { - int last_uip_ext_len; /* Remove ext header before TCP/UDP processing. */ if(uip_ext_len > 0) { - LOG_DBG("Cutting ext-header before processing (extlen: %d, uiplen: %d)\n", + LOG_DBG("Removing IPv6 extension headers (extlen: %d, uiplen: %d)\n", uip_ext_len, uip_len); if(uip_len < UIP_IPH_LEN + uip_ext_len) { - LOG_ERR("ERROR: uip_len too short compared to ext len\n"); - uip_clear_buf(); + LOG_ERR("uip_len too short compared to ext len\n"); + uipbuf_clear(); return; } - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - UIP_IP_BUF->proto = UIP_EXT_BUF->next; - memmove(((uint8_t *)UIP_TCP_BUF), (uint8_t *)UIP_TCP_BUF + last_uip_ext_len, - uip_len - UIP_IPH_LEN - last_uip_ext_len); - uip_len -= last_uip_ext_len; + /* Set proto */ + UIP_IP_BUF->proto = uip_last_proto; + /* Move IP payload to the "left"*/ + memmove(UIP_IP_PAYLOAD(0), UIP_IP_PAYLOAD(uip_ext_len), + uip_len - UIP_IPH_LEN - uip_ext_len); /* Update the IP length. */ - UIP_IP_BUF->len[0] = (uip_len - UIP_IPH_LEN) >> 8; - UIP_IP_BUF->len[1] = (uip_len - UIP_IPH_LEN) & 0xff; + uipbuf_add_ext_hdr(-uip_ext_len); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); } } /*---------------------------------------------------------------------------*/ @@ -610,7 +588,7 @@ uip_listen(uint16_t port) /*---------------------------------------------------------------------------*/ #if UIP_CONF_IPV6_REASSEMBLY -#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN) +#define UIP_REASS_BUFSIZE (UIP_BUFSIZE) static uint8_t uip_reassbuf[UIP_REASS_BUFSIZE]; @@ -646,11 +624,12 @@ static uint32_t uip_id; /* For every packet that is to be fragmented, the source #define IP_MF 0x0001 static uint16_t -uip_reass(void) +uip_reass(uint8_t *prev_proto_ptr) { uint16_t offset=0; uint16_t len; uint16_t i; + struct uip_frag_hdr *frag_buf = (struct uip_frag_hdr *)UIP_IP_PAYLOAD(uip_ext_len); /* If ip_reasstmr is zero, no packet is present in the buffer */ /* We first write the unfragmentable part of IP header into the reassembly @@ -662,7 +641,7 @@ uip_reass(void) etimer_set(&uip_reass_timer, UIP_REASS_MAXAGE*CLOCK_SECOND); uip_reass_on = 1; uip_reassflags = 0; - uip_id = UIP_FRAG_BUF->id; + uip_id = frag_buf->id; /* Clear the bitmap. */ memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap)); } @@ -673,9 +652,9 @@ uip_reass(void) */ if(uip_ipaddr_cmp(&FBUF->srcipaddr, &UIP_IP_BUF->srcipaddr) && uip_ipaddr_cmp(&FBUF->destipaddr, &UIP_IP_BUF->destipaddr) && - UIP_FRAG_BUF->id == uip_id) { + frag_buf->id == uip_id) { len = uip_len - uip_ext_len - UIP_IPH_LEN - UIP_FRAGH_LEN; - offset = (uip_ntohs(UIP_FRAG_BUF->offsetresmore) & 0xfff8); + offset = (uip_ntohs(frag_buf->offsetresmore) & 0xfff8); /* in byte, originaly in multiple of 8 bytes*/ LOG_INFO("len %d\n", len); LOG_INFO("offset %d\n", offset); @@ -686,7 +665,7 @@ uip_reass(void) * Part is obtained from the Next Header field of the first * fragment's Fragment header. */ - *uip_next_hdr = UIP_FRAG_BUF->next; + *prev_proto_ptr = frag_buf->next; memcpy(FBUF, UIP_IP_BUF, uip_ext_len + UIP_IPH_LEN); LOG_INFO("src "); LOG_INFO_6ADDR(&FBUF->srcipaddr); @@ -707,7 +686,7 @@ uip_reass(void) /* If this fragment has the More Fragments flag set to zero, it is the last fragment*/ - if((uip_ntohs(UIP_FRAG_BUF->offsetresmore) & IP_MF) == 0) { + if((uip_ntohs(frag_buf->offsetresmore) & IP_MF) == 0) { uip_reassflags |= UIP_REASS_FLAG_LASTFRAG; /*calculate the size of the entire packet*/ uip_reasslen = offset + len; @@ -731,7 +710,7 @@ uip_reass(void) /* Copy the fragment into the reassembly buffer, at the right offset. */ memcpy((uint8_t *)FBUF + UIP_IPH_LEN + uip_ext_len + offset, - (uint8_t *)UIP_FRAG_BUF + UIP_FRAGH_LEN, len); + (uint8_t *)frag_buf + UIP_FRAGH_LEN, len); /* Update the bitmap. */ if(offset >> 6 == (offset + len) >> 6) { @@ -777,10 +756,8 @@ uip_reass(void) uip_reasslen += UIP_IPH_LEN + uip_ext_len; memcpy(UIP_IP_BUF, FBUF, uip_reasslen); - UIP_IP_BUF->len[0] = ((uip_reasslen - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_reasslen - UIP_IPH_LEN) & 0xff); - LOG_INFO("reassembled packet %d (%d)\n", uip_reasslen, - (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]); + uipbuf_set_len_field(UIP_IP_BUF, uip_reasslen - UIP_IPH_LEN); + LOG_INFO("reassembled packet %d (%d)\n", uip_reasslen, uipbuf_get_len_field(UIP_IP_BUF)); return uip_reasslen; @@ -810,7 +787,7 @@ uip_reass_over(void) * any RFC, we decided not to include it as it reduces the size of * the packet. */ - uip_clear_buf(); + uipbuf_clear(); memcpy(UIP_IP_BUF, FBUF, UIP_IPH_LEN); /* copy the header for src and dest address*/ uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_REASSEMBLY, 0); @@ -840,16 +817,29 @@ uip_add_rcv_nxt(uint16_t n) * \brief Process the options in Destination and Hop By Hop extension headers */ static uint8_t -ext_hdr_options_process(void) +ext_hdr_options_process(uint8_t *ext_buf) { /* * Length field in the extension header: length of the header in units of * 8 bytes, excluding the first 8 bytes * length field in an option : the length of data in the option */ - uip_ext_opt_offset = 2; - while(uip_ext_opt_offset < ((UIP_EXT_BUF->len << 3) + 8)) { - switch(UIP_EXT_HDR_OPT_BUF->type) { + uint16_t opt_offset = 2; /* 2 first bytes in ext header */ + struct uip_hbho_hdr *ext_hdr = (struct uip_hbho_hdr *)ext_buf; + uint16_t ext_hdr_len = (ext_hdr->len << 3) + 8; + + while(opt_offset + 2 <= ext_hdr_len) { /* + 2 for opt header */ + struct uip_ext_hdr_opt *opt_hdr = (struct uip_ext_hdr_opt *)(ext_buf + opt_offset); + uint16_t opt_len = opt_hdr->len + 2; + + if(opt_offset + opt_len > ext_hdr_len) { + LOG_ERR("RPL Option too long: Dropping Packet\n"); + uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, + (ext_buf + opt_offset) - uip_buf); + return 2; + } + + switch(opt_hdr->type) { /* * for now we do not support any options except padding ones * PAD1 does not make sense as the header must be 8bytes aligned, @@ -857,11 +847,11 @@ ext_hdr_options_process(void) */ case UIP_EXT_HDR_OPT_PAD1: LOG_DBG("Processing PAD1 option\n"); - uip_ext_opt_offset += 1; + opt_offset += 1; break; case UIP_EXT_HDR_OPT_PADN: LOG_DBG("Processing PADN option\n"); - uip_ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF->opt_len + 2; + opt_offset += opt_len; break; case UIP_EXT_HDR_OPT_RPL: /* Fixes situation when a node that is not using RPL @@ -872,12 +862,13 @@ ext_hdr_options_process(void) * Using this fix, the header is ignored, and the next header (if * present) is processed. */ - if(!NETSTACK_ROUTING.ext_header_hbh_update(uip_ext_opt_offset)) { + LOG_DBG("Processing RPL option\n"); + if(!NETSTACK_ROUTING.ext_header_hbh_update(ext_buf, opt_offset)) { LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } - uip_ext_opt_offset += (UIP_EXT_HDR_OPT_BUF->len) + 2; - return 0; + opt_offset += opt_len; + break; default: /* * check the two highest order bits of the option @@ -892,8 +883,8 @@ ext_hdr_options_process(void) * Problem, Code 2, message to the packet's Source Address, * pointing to the unrecognized Option Type. */ - LOG_DBG("MSB %x\n", UIP_EXT_HDR_OPT_BUF->type); - switch(UIP_EXT_HDR_OPT_BUF->type & 0xC0) { + LOG_DBG("Unrecognized option, MSB 0x%x\n", opt_hdr->type); + switch(opt_hdr->type & 0xC0) { case 0: break; case 0x40: @@ -904,22 +895,49 @@ ext_hdr_options_process(void) } case 0x80: uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, - (uint32_t)UIP_IPH_LEN + uip_ext_len + uip_ext_opt_offset); + (ext_buf + opt_offset) - uip_buf); return 2; } /* in the cases were we did not discard, update ext_opt* */ - uip_ext_opt_offset += UIP_EXT_HDR_OPT_BUF->len + 2; + opt_offset += opt_len; break; } } return 0; } - - +/*---------------------------------------------------------------------------*/ +static bool +uip_check_mtu(void) +{ + if(uip_len > UIP_LINK_MTU) { + uip_icmp6_error_output(ICMP6_PACKET_TOO_BIG, 0, UIP_LINK_MTU); + UIP_STAT(++uip_stat.ip.drop); + return false; + } else { + return true; + } +} +/*---------------------------------------------------------------------------*/ +static bool +uip_update_ttl(void) +{ + if(UIP_IP_BUF->ttl <= 1) { + uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT, 0); + UIP_STAT(++uip_stat.ip.drop); + return false; + } else { + UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1; + return true; + } +} /*---------------------------------------------------------------------------*/ void uip_process(uint8_t flag) { + uint8_t *last_header; + uint8_t protocol; + uint8_t *next_header; + struct uip_ext_hdr *ext_ptr; #if UIP_TCP int c; uint16_t tmp16; @@ -931,7 +949,7 @@ uip_process(uint8_t flag) goto udp_send; } #endif /* UIP_UDP */ - uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN]; + uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN]; /* Check if we were invoked because of a poll request for a particular connection. */ @@ -955,7 +973,7 @@ uip_process(uint8_t flag) } else if(flag == UIP_TIMER) { /* Reset the length variables. */ #if UIP_TCP - uip_clear_buf(); + uipbuf_clear(); uip_slen = 0; /* Increase the initial sequence number. */ @@ -1068,7 +1086,7 @@ uip_process(uint8_t flag) if(flag == UIP_UDP_TIMER) { if(uip_udp_conn->lport != 0) { uip_conn = NULL; - uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN + UIP_LLH_LEN]; + uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN]; uip_len = uip_slen = 0; uip_flags = UIP_POLL; UIP_UDP_APPCALL(); @@ -1101,8 +1119,8 @@ uip_process(uint8_t flag) * value.. */ - if((UIP_IP_BUF->len[0] << 8) + UIP_IP_BUF->len[1] <= uip_len) { - uip_len = (UIP_IP_BUF->len[0] << 8) + UIP_IP_BUF->len[1] + UIP_IPH_LEN; + if(uipbuf_get_len_field(UIP_IP_BUF) <= uip_len) { + uip_len = uipbuf_get_len_field(UIP_IP_BUF) + UIP_IPH_LEN; /* * The length reported in the IPv6 header is the * length of the payload that follows the @@ -1115,10 +1133,21 @@ uip_process(uint8_t flag) * header (40 bytes). */ } else { - LOG_ERR("packet shorter than reported in IP header."); + LOG_ERR("packet shorter than reported in IP header\n"); goto drop; } + /* Check sanity of extension headers, and compute the total extension header + * length (uip_ext_len) as well as the final protocol (uip_last_proto) */ + uip_last_proto = 0; + last_header = uipbuf_get_last_header(uip_buf, uip_len, &uip_last_proto); + if(last_header == NULL) { + LOG_ERR("invalid extension header chain\n"); + goto drop; + } + /* Set uip_ext_len */ + uip_ext_len = last_header - UIP_IP_PAYLOAD(0); + LOG_INFO("packet received from "); LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); LOG_INFO_(" to "); @@ -1140,32 +1169,20 @@ uip_process(uint8_t flag) #if UIP_CONF_ROUTER /* - * Next header field processing. In IPv6, we can have extension headers, - * if present, the Hop-by-Hop Option must be processed before forwarding + * If present, the Hop-by-Hop Option must be processed before forwarding * the packet. */ - uip_next_hdr = &UIP_IP_BUF->proto; - uip_ext_len = 0; - uip_ext_bitmap = 0; - if(*uip_next_hdr == UIP_PROTO_HBHO) { -#if UIP_CONF_IPV6_CHECKS - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO; -#endif /* UIP_CONF_IPV6_CHECKS */ - switch(ext_hdr_options_process()) { + + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); + if(next_header != NULL && protocol == UIP_PROTO_HBHO) { + switch(ext_hdr_options_process(next_header)) { case 0: - /* continue */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; + break; /* done */ case 1: - LOG_ERR("Dropping packet after extension header processing\n"); - /* silently discard */ - goto drop; + goto drop; /* silently discard */ case 2: - LOG_ERR("Sending error message after extension header processing\n"); - /* send icmp error message (created in ext_hdr_options_process) - * and discard*/ - goto send; + goto send; /* send icmp error message (created in + ext_hdr_options_process) and discard */ } } @@ -1201,23 +1218,12 @@ uip_process(uint8_t flag) !uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr) && !uip_is_addr_loopback(&UIP_IP_BUF->destipaddr)) { - - /* Check MTU */ - if(uip_len > UIP_LINK_MTU) { - uip_icmp6_error_output(ICMP6_PACKET_TOO_BIG, 0, UIP_LINK_MTU); - UIP_STAT(++uip_stat.ip.drop); - goto send; - } - /* Check Hop Limit */ - if(UIP_IP_BUF->ttl <= 1) { - uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, - ICMP6_TIME_EXCEED_TRANSIT, 0); - UIP_STAT(++uip_stat.ip.drop); + if(!uip_check_mtu() || !uip_update_ttl()) { + /* Send ICMPv6 error, prepared by the function that just returned false */ goto send; } - UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1; - LOG_INFO("Forwarding packet towards "); + LOG_INFO("Forwarding packet to next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); UIP_STAT(++uip_stat.ip.forwarded); @@ -1246,35 +1252,20 @@ uip_process(uint8_t flag) UIP_STAT(++uip_stat.ip.drop); goto drop; } - - /* - * Next header field processing. In IPv6, we can have extension headers, - * they are processed here - */ - uip_next_hdr = &UIP_IP_BUF->proto; - uip_ext_len = 0; - uip_ext_bitmap = 0; #endif /* UIP_CONF_ROUTER */ #if UIP_IPV6_MULTICAST && UIP_CONF_ROUTER process: #endif /* UIP_IPV6_MULTICAST && UIP_CONF_ROUTER */ - while(1) { - switch(*uip_next_hdr){ -#if UIP_TCP - case UIP_PROTO_TCP: - /* TCP, for both IPv4 and IPv6 */ - goto tcp_input; -#endif /* UIP_TCP */ -#if UIP_UDP - case UIP_PROTO_UDP: - /* UDP, for both IPv4 and IPv6 */ - goto udp_input; -#endif /* UIP_UDP */ - case UIP_PROTO_ICMP6: - /* ICMPv6 */ - goto icmp6_input; + /* IPv6 extension header processing: loop until reaching upper-layer protocol */ + uip_ext_bitmap = 0; + for(next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); + next_header != NULL && uip_is_proto_ext_hdr(protocol); + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false)) { + + ext_ptr = (struct uip_ext_hdr *)next_header; + switch(protocol) { case UIP_PROTO_HBHO: LOG_DBG("Processing hbh header\n"); /* Hop by hop option header */ @@ -1286,145 +1277,140 @@ uip_process(uint8_t flag) uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO; } #endif /*UIP_CONF_IPV6_CHECKS*/ - switch(ext_hdr_options_process()) { + switch(ext_hdr_options_process(next_header)) { case 0: - /*continue*/ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; + break; /* done */ case 1: - /*silently discard*/ - goto drop; + goto drop; /* silently discard */ case 2: - /* send icmp error message (created in ext_hdr_options_process) - * and discard*/ - goto send; + goto send; /* send icmp error message (created in + ext_hdr_options_process) and discard */ } break; - case UIP_PROTO_DESTO: + case UIP_PROTO_DESTO: #if UIP_CONF_IPV6_CHECKS - /* Destination option header. if we saw two already, drop */ - LOG_DBG("Processing desto header\n"); - if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO1) { - if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO2) { - goto bad_hdr; - } else{ - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO2; - } - } else { - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO1; - } -#endif /*UIP_CONF_IPV6_CHECKS*/ - switch(ext_hdr_options_process()) { - case 0: - /*continue*/ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - case 1: - /*silently discard*/ - goto drop; - case 2: - /* send icmp error message (created in ext_hdr_options_process) - * and discard*/ - goto send; - } - break; - case UIP_PROTO_ROUTING: -#if UIP_CONF_IPV6_CHECKS - /* Routing header. If we saw one already, drop */ - if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_ROUTING) { - goto bad_hdr; - } else { - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_ROUTING; - } -#endif /*UIP_CONF_IPV6_CHECKS*/ - /* - * Routing Header length field is in units of 8 bytes, excluding - * As per RFC2460 section 4.4, if routing type is unrecognized: - * if segments left = 0, ignore the header - * if segments left > 0, discard packet and send icmp error pointing - * to the routing type - */ - - LOG_DBG("Processing Routing header\n"); - if(UIP_ROUTING_BUF->seg_left > 0) { - if(NETSTACK_ROUTING.ext_header_srh_update()) { - - /* With routing header, the detination address is us and will - * be swapped later to the next hop. Because of this, the MTU - * and TTL were not checked and updated yet. Do this now. */ - - /* Check MTU */ - if(uip_len > UIP_LINK_MTU) { - uip_icmp6_error_output(ICMP6_PACKET_TOO_BIG, 0, UIP_LINK_MTU); - UIP_STAT(++uip_stat.ip.drop); - goto send; - } - /* Check Hop Limit */ - if(UIP_IP_BUF->ttl <= 1) { - uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, - ICMP6_TIME_EXCEED_TRANSIT, 0); - UIP_STAT(++uip_stat.ip.drop); - goto send; - } - UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1; - - LOG_INFO("Forwarding packet to next hop "); - LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); - LOG_INFO_("\n"); - UIP_STAT(++uip_stat.ip.forwarded); - - goto send; /* Proceed to forwarding */ - } - uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, UIP_IPH_LEN + uip_ext_len + 2); - UIP_STAT(++uip_stat.ip.drop); - LOG_ERR("unrecognized routing type"); - goto send; - } - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - case UIP_PROTO_FRAG: - /* Fragmentation header:call the reassembly function, then leave */ -#if UIP_CONF_IPV6_REASSEMBLY - LOG_INFO("Processing fragmentation header\n"); - uip_len = uip_reass(); - if(uip_len == 0) { - goto drop; - } - if(uip_reassflags & UIP_REASS_FLAG_ERROR_MSG){ - /* we are not done with reassembly, this is an error message */ - goto send; - } - /*packet is reassembled, reset the next hdr to the beginning - of the IP header and restart the parsing of the reassembled pkt*/ - LOG_INFO("Processing reassembled packet\n"); - uip_ext_len = 0; - uip_ext_bitmap = 0; - uip_next_hdr = &UIP_IP_BUF->proto; - break; -#else /* UIP_CONF_IPV6_REASSEMBLY */ - UIP_STAT(++uip_stat.ip.drop); - UIP_STAT(++uip_stat.ip.fragerr); - LOG_ERR("fragment dropped."); - goto drop; -#endif /* UIP_CONF_IPV6_REASSEMBLY */ - case UIP_PROTO_NONE: - goto drop; - default: + /* Destination option header. if we saw two already, drop */ + LOG_DBG("Processing desto header\n"); + if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO1) { + if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO2) { goto bad_hdr; + } else{ + uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO2; + } + } else { + uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO1; + } +#endif /*UIP_CONF_IPV6_CHECKS*/ + switch(ext_hdr_options_process(next_header)) { + case 0: + break; /* done */ + case 1: + goto drop; /* silently discard */ + case 2: + goto send; /* send icmp error message (created in + ext_hdr_options_process) and discard */ + } + break; + case UIP_PROTO_ROUTING: +#if UIP_CONF_IPV6_CHECKS + /* Routing header. If we saw one already, drop */ + if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_ROUTING) { + goto bad_hdr; + } else { + uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_ROUTING; + } +#endif /*UIP_CONF_IPV6_CHECKS*/ + /* + * Routing Header length field is in units of 8 bytes, excluding + * As per RFC2460 section 4.4, if routing type is unrecognized: + * if segments left = 0, ignore the header + * if segments left > 0, discard packet and send icmp error pointing + * to the routing type + */ + + LOG_DBG("Processing Routing header\n"); + if(((struct uip_routing_hdr *)ext_ptr)->seg_left > 0) { + /* Process source routing header */ + if(NETSTACK_ROUTING.ext_header_srh_update()) { + + /* The MTU and TTL were not checked and updated yet, because with + * a routing header, the IPv6 destination address was set to us + * even though we act only as forwarder. Check MTU and TTL now */ + if(!uip_check_mtu() || !uip_update_ttl()) { + /* Send ICMPv6 error, prepared by the function that just returned false */ + goto send; + } + + LOG_INFO("Forwarding packet to next hop "); + LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); + LOG_INFO_("\n"); + UIP_STAT(++uip_stat.ip.forwarded); + + goto send; /* Proceed to forwarding */ + } else { + LOG_ERR("Unrecognized routing type\n"); + goto bad_hdr; + } + } + break; + case UIP_PROTO_FRAG: + /* Fragmentation header:call the reassembly function, then leave */ +#if UIP_CONF_IPV6_REASSEMBLY + LOG_INFO("Processing fragmentation header\n"); + uip_len = uip_reass(&ext_ptr->next); + if(uip_len == 0) { + goto drop; + } + if(uip_reassflags & UIP_REASS_FLAG_ERROR_MSG) { + /* we are not done with reassembly, this is an error message */ + goto send; + } + /* packet is reassembled. Restart the parsing of the reassembled pkt */ + LOG_INFO("Processing reassembled packet\n"); + uip_ext_bitmap = 0; + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); + break; +#else /* UIP_CONF_IPV6_REASSEMBLY */ + UIP_STAT(++uip_stat.ip.drop); + UIP_STAT(++uip_stat.ip.fragerr); + LOG_ERR("fragment dropped."); + goto drop; +#endif /* UIP_CONF_IPV6_REASSEMBLY */ + case UIP_PROTO_NONE: + goto drop; + default: + goto bad_hdr; } } + + /* Process upper-layer input */ + if(next_header != NULL) { + switch(protocol) { +#if UIP_TCP + case UIP_PROTO_TCP: + /* TCP, for both IPv4 and IPv6 */ + goto tcp_input; +#endif +#if UIP_UDP + case UIP_PROTO_UDP: + /* UDP, for both IPv4 and IPv6 */ + goto udp_input; +#endif + case UIP_PROTO_ICMP6: + /* ICMPv6 */ + goto icmp6_input; + } + } + bad_hdr: /* * RFC 2460 send error message parameterr problem, code unrecognized * next header, pointing to the next header field */ - uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, (uint32_t)(uip_next_hdr - (uint8_t *)UIP_IP_BUF)); + uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, (uint32_t)(next_header - uip_buf)); UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.protoerr); - LOG_ERR("unrecognized header"); + LOG_ERR("unrecognized header\n"); goto send; /* End of headers processing */ @@ -1465,7 +1451,7 @@ uip_process(uint8_t flag) LOG_ERR("Unknown ICMPv6 message type/code %d\n", UIP_ICMP_BUF->type); UIP_STAT(++uip_stat.icmp.drop); UIP_STAT(++uip_stat.icmp.typeerr); - uip_clear_buf(); + uipbuf_clear(); } if(uip_len > 0) { @@ -1480,7 +1466,7 @@ uip_process(uint8_t flag) /* UDP input processing. */ udp_input: - remove_ext_hdr(); + uip_remove_ext_hdr(); LOG_INFO("Receiving UDP packet\n"); @@ -1540,10 +1526,10 @@ uip_process(uint8_t flag) UIP_STAT(++uip_stat.udp.recv); uip_len = uip_len - UIP_IPUDPH_LEN; - uip_appdata = &uip_buf[UIP_IPUDPH_LEN + UIP_LLH_LEN]; + uip_appdata = &uip_buf[UIP_IPUDPH_LEN]; uip_conn = NULL; uip_flags = UIP_NEWDATA; - uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN + UIP_LLH_LEN]; + uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN]; uip_slen = 0; UIP_UDP_APPCALL(); @@ -1557,8 +1543,7 @@ uip_process(uint8_t flag) /* For IPv6, the IP length field does not include the IPv6 IP header length. */ - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0x00; @@ -1574,7 +1559,7 @@ uip_process(uint8_t flag) uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &uip_udp_conn->ripaddr); uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); - uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN]; + uip_appdata = &uip_buf[UIP_IPTCPH_LEN]; #if UIP_UDP_CHECKSUMS /* Calculate UDP checksum. */ @@ -1592,7 +1577,7 @@ uip_process(uint8_t flag) /* TCP input processing. */ tcp_input: - remove_ext_hdr(); + uip_remove_ext_hdr(); UIP_STAT(++uip_stat.tcp.recv); LOG_INFO("Receiving TCP packet\n"); @@ -1609,7 +1594,7 @@ uip_process(uint8_t flag) /* Make sure that the TCP port number is not zero. */ if(UIP_TCP_BUF->destport == 0 || UIP_TCP_BUF->srcport == 0) { - LOG_ERR("tcp: zero port."); + LOG_ERR("tcp: zero port\n"); goto drop; } @@ -1726,7 +1711,7 @@ uip_process(uint8_t flag) the remote end will retransmit the packet at a time when we have more spare connections. */ UIP_STAT(++uip_stat.tcp.syndrop); - LOG_ERR("tcp: found no unused connections."); + LOG_ERR("tcp: found no unused connections\n"); goto drop; } uip_conn = uip_connr; @@ -1757,7 +1742,7 @@ uip_process(uint8_t flag) /* Parse the TCP MSS option, if present. */ if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) { for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) { - opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c]; + opt = uip_buf[UIP_IPTCPH_LEN + c]; if(opt == TCP_OPT_END) { /* End of options. */ break; @@ -1765,10 +1750,10 @@ uip_process(uint8_t flag) ++c; /* NOP option. */ } else if(opt == TCP_OPT_MSS && - uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { + uip_buf[UIP_IPTCPH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { /* An MSS option with the right option length. */ - tmp16 = ((uint16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | - (uint16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c]; + tmp16 = ((uint16_t)uip_buf[UIP_IPTCPH_LEN + 2 + c] << 8) | + (uint16_t)uip_buf[UIP_IPTCPH_LEN + 3 + c]; uip_connr->initialmss = uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; @@ -1777,12 +1762,12 @@ uip_process(uint8_t flag) } else { /* All other options have a length field, so that we easily can skip past them. */ - if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) { + if(uip_buf[UIP_IPTCPH_LEN + 1 + c] == 0) { /* If the length field is zero, the options are malformed and we don't process them further. */ break; } - c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c]; + c += uip_buf[UIP_IPTCPH_LEN + 1 + c]; } } } @@ -1944,7 +1929,7 @@ uip_process(uint8_t flag) /* Parse the TCP MSS option, if present. */ if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) { for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) { - opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c]; + opt = uip_buf[UIP_IPTCPH_LEN + c]; if(opt == TCP_OPT_END) { /* End of options. */ break; @@ -1952,10 +1937,10 @@ uip_process(uint8_t flag) ++c; /* NOP option. */ } else if(opt == TCP_OPT_MSS && - uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { + uip_buf[UIP_IPTCPH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { /* An MSS option with the right option length. */ - tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | - uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c]; + tmp16 = (uip_buf[UIP_IPTCPH_LEN + 2 + c] << 8) | + uip_buf[UIP_IPTCPH_LEN + 3 + c]; uip_connr->initialmss = uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; @@ -1964,12 +1949,12 @@ uip_process(uint8_t flag) } else { /* All other options have a length field, so that we easily can skip past them. */ - if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) { + if(uip_buf[UIP_IPTCPH_LEN + 1 + c] == 0) { /* If the length field is zero, the options are malformed and we don't process them further. */ break; } - c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c]; + c += uip_buf[UIP_IPTCPH_LEN + 1 + c]; } } } @@ -1981,7 +1966,7 @@ uip_process(uint8_t flag) uip_add_rcv_nxt(1); uip_flags = UIP_CONNECTED | UIP_NEWDATA; uip_connr->len = 0; - uip_clear_buf(); + uipbuf_clear(); uip_slen = 0; UIP_APPCALL(); goto appsend; @@ -2083,7 +2068,7 @@ uip_process(uint8_t flag) When the application is called, the global variable uip_len contains the length of the incoming data. The application can access the incoming data through the global pointer - uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN + uip_appdata, which usually points UIP_IPTCPH_LEN bytes into the uip_buf array. If the application wishes to send any data, this data should be @@ -2152,7 +2137,7 @@ uip_process(uint8_t flag) packet had new data in it, we must send out a packet. */ if(uip_slen > 0 && uip_connr->len > 0) { /* Add the length of the IP and TCP headers. */ - uip_len = uip_connr->len + UIP_TCPIP_HLEN; + uip_len = uip_connr->len + UIP_IPTCPH_LEN; /* We always set the ACK flag in response packets. */ UIP_TCP_BUF->flags = TCP_ACK | TCP_PSH; /* Send the packet. */ @@ -2161,7 +2146,7 @@ uip_process(uint8_t flag) /* If there is no data to send, just send out a pure ACK if there is newdata. */ if(uip_flags & UIP_NEWDATA) { - uip_len = UIP_TCPIP_HLEN; + uip_len = UIP_IPTCPH_LEN; UIP_TCP_BUF->flags = TCP_ACK; goto tcp_send_noopts; } @@ -2289,8 +2274,7 @@ uip_process(uint8_t flag) UIP_IP_BUF->proto = UIP_PROTO_TCP; UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); UIP_TCP_BUF->urgp[0] = UIP_TCP_BUF->urgp[1] = 0; @@ -2305,8 +2289,7 @@ uip_process(uint8_t flag) #endif UIP_IP_BUF->flow = 0x00; send: - LOG_INFO("Sending packet with length %d (%d)\n", uip_len, - (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]); + LOG_INFO("Sending packet with length %d (%d)\n", uip_len, uipbuf_get_len_field(UIP_IP_BUF)); UIP_STAT(++uip_stat.ip.sent); /* Return and let the caller do the actual transmission. */ @@ -2314,7 +2297,7 @@ uip_process(uint8_t flag) return; drop: - uip_clear_buf(); + uipbuf_clear(); uip_ext_bitmap = 0; uip_flags = 0; return; @@ -2338,18 +2321,16 @@ uip_send(const void *data, int len) int copylen; if(uip_sappdata != NULL) { - copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN - - (int)((char *)uip_sappdata - - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN])); + copylen = MIN(len, UIP_BUFSIZE - UIP_IPTCPH_LEN - + (int)((char *)uip_sappdata - (char *)UIP_TCP_PAYLOAD)); } else { - copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN); + copylen = MIN(len, UIP_BUFSIZE - UIP_IPTCPH_LEN); } if(copylen > 0) { uip_slen = copylen; if(data != uip_sappdata) { if(uip_sappdata == NULL) { - memcpy((char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN], - (data), uip_slen); + memcpy(UIP_TCP_PAYLOAD, (data), uip_slen); } else { memcpy(uip_sappdata, (data), uip_slen); } diff --git a/os/net/ipv6/uipbuf.c b/os/net/ipv6/uipbuf.c index 6ccbb99aa..f7e8f4b06 100644 --- a/os/net/ipv6/uipbuf.c +++ b/os/net/ipv6/uipbuf.c @@ -30,53 +30,150 @@ * */ #include "contiki.h" -#include "uip.h" +#include "net/ipv6/uip.h" #include "net/ipv6/uipbuf.h" #include /*---------------------------------------------------------------------------*/ static uint16_t uipbuf_attrs[UIPBUF_ATTR_MAX]; +static uint16_t uipbuf_default_attrs[UIPBUF_ATTR_MAX]; +/*---------------------------------------------------------------------------*/ +void +uipbuf_clear(void) +{ + uip_len = 0; + uip_ext_len = 0; + uip_last_proto = 0; + uipbuf_clear_attr(); +} +/*---------------------------------------------------------------------------*/ +bool +uipbuf_add_ext_hdr(int16_t len) +{ + if(len + uip_len <= UIP_LINK_MTU && len + uip_len >= 0) { + uip_ext_len += len; + uip_len += len; + return true; + } else { + return false; + } +} +/*---------------------------------------------------------------------------*/ +bool +uipbuf_set_len(uint16_t len) +{ + if(len <= UIP_LINK_MTU) { + uip_len = len; + return true; + } else { + return false; + } +} +/*---------------------------------------------------------------------------*/ +void +uipbuf_set_len_field(struct uip_ip_hdr *hdr, uint16_t len) +{ + hdr->len[0] = (len >> 8); + hdr->len[1] = (len & 0xff); +} +/*---------------------------------------------------------------------------*/ +uint16_t +uipbuf_get_len_field(struct uip_ip_hdr *hdr) +{ + return ((uint16_t)(hdr->len[0]) << 8) + hdr->len[1]; +} /*---------------------------------------------------------------------------*/ /* Get the next header given the buffer - start indicates that this is start of the IPv6 header - needs to be set to 0 when in an ext hdr */ -uint8_t* -uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, uint8_t start) +uint8_t * +uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, bool start) { - int ext_len = 0; - struct uip_ip_hdr *ipbuf = (struct uip_ip_hdr *) buffer; - struct uip_ext_hdr *ext = NULL; + int curr_hdr_len = 0; + int next_hdr_len = 0; + uint8_t *next_header = NULL; + struct uip_ip_hdr *ipbuf = NULL; + struct uip_ext_hdr *curr_ext = NULL; + struct uip_ext_hdr *next_ext = NULL; if(start) { /* protocol in the IP buffer */ + ipbuf = (struct uip_ip_hdr *)buffer; *protocol = ipbuf->proto; - return buffer + UIP_IPH_LEN; + curr_hdr_len = UIP_IPH_LEN; } else { /* protocol in the Ext hdr */ - ext = (struct uip_ext_hdr *) buffer; - *protocol = ext->next; + curr_ext = (struct uip_ext_hdr *)buffer; + *protocol = curr_ext->next; /* This is just an ext header */ - ext_len = (ext->len << 3) + 8; - return buffer + ext_len; + curr_hdr_len = (curr_ext->len << 3) + 8; } + + /* Check first if enough space for current header */ + if(curr_hdr_len > size) { + return NULL; + } + next_header = buffer + curr_hdr_len; + + /* Check if the buffer is large enough for the next header */ + if(uip_is_proto_ext_hdr(*protocol)) { + next_ext = (struct uip_ext_hdr *)next_header; + next_hdr_len = (next_ext->len << 3) + 8; + } else { + if(*protocol == UIP_PROTO_TCP) { + next_hdr_len = UIP_TCPH_LEN; + } else if(*protocol == UIP_PROTO_UDP) { + next_hdr_len = UIP_UDPH_LEN; + } else if(*protocol == UIP_PROTO_ICMP6) { + next_hdr_len = UIP_ICMPH_LEN; + } + } + + /* Size must be enough to hold both the current and next header */ + if(next_hdr_len == 0 || curr_hdr_len + next_hdr_len > size) { + return NULL; + } + + return next_header; } /*---------------------------------------------------------------------------*/ /* Get the final header given the buffer - that is assumed to be at start of an IPv6 header */ -uint8_t* +uint8_t * uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol) { uint8_t *nbuf; - nbuf = uipbuf_get_next_header(buffer, size, protocol, 1); - while(uip_is_proto_ext_hdr(*protocol)) { - /* send in and move beyond the ext hdr */ - nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), protocol, 0); + nbuf = uipbuf_get_next_header(buffer, size, protocol, true); + while(nbuf != NULL && uip_is_proto_ext_hdr(*protocol)) { + /* move to the ext hdr */ + nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), protocol, false); } + + /* In case the buffer wasn't large enough for all headers, return NULL */ return nbuf; } /*---------------------------------------------------------------------------*/ +uint8_t * +uipbuf_search_header(uint8_t *buffer, uint16_t size, uint8_t protocol) +{ + uint8_t *nbuf; + uint8_t next_proto; + + nbuf = uipbuf_get_next_header(buffer, size, &next_proto, true); + while(nbuf != NULL && next_proto != protocol && uip_is_proto_ext_hdr(next_proto)) { + /* move to the ext hdr */ + nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), &next_proto, false); + } + + if(next_proto == protocol) { + return nbuf; + } else { + return NULL; + } +} +/*---------------------------------------------------------------------------*/ /** * Common functions for uipbuf (attributes, etc). * @@ -101,15 +198,21 @@ uipbuf_set_attr(uint8_t type, uint16_t value) return 0; } /*---------------------------------------------------------------------------*/ +int +uipbuf_set_default_attr(uint8_t type, uint16_t value) +{ + if(type < UIPBUF_ATTR_MAX) { + uipbuf_default_attrs[type] = value; + return 1; + } + return 0; +} +/*---------------------------------------------------------------------------*/ void uipbuf_clear_attr(void) { - /* set everything to "zero" */ - memset(uipbuf_attrs, 0, sizeof(uipbuf_attrs)); - - /* And initialize anything that should be initialized */ - uipbuf_set_attr(UIPBUF_ATTR_MAX_MAC_TRANSMISSIONS, - UIP_MAX_MAC_TRANSMISSIONS_UNDEFINED); + /* set everything to "defaults" */ + memcpy(uipbuf_attrs, uipbuf_default_attrs, sizeof(uipbuf_attrs)); } /*---------------------------------------------------------------------------*/ void @@ -131,3 +234,17 @@ uipbuf_is_attr_flag(uint16_t flag) return (uipbuf_attrs[UIPBUF_ATTR_FLAGS] & flag) == flag; } /*---------------------------------------------------------------------------*/ +void +uipbuf_init(void) +{ + memset(uipbuf_default_attrs, 0, sizeof(uipbuf_default_attrs)); + /* And initialize anything that should be initialized */ + uipbuf_set_default_attr(UIPBUF_ATTR_MAX_MAC_TRANSMISSIONS, + UIP_MAX_MAC_TRANSMISSIONS_UNDEFINED); + /* set the not-set default value - this will cause the MAC layer to + configure its default */ + uipbuf_set_default_attr(UIPBUF_ATTR_LLSEC_LEVEL, + UIPBUF_ATTR_LLSEC_LEVEL_MAC_DEFAULT); +} + +/*---------------------------------------------------------------------------*/ diff --git a/os/net/ipv6/uipbuf.h b/os/net/ipv6/uipbuf.h index 337ffe17f..fdea60b28 100644 --- a/os/net/ipv6/uipbuf.h +++ b/os/net/ipv6/uipbuf.h @@ -34,6 +34,40 @@ #define UIPBUF_H_ #include "contiki.h" +struct uip_ip_hdr; + +/** + * \brief Resets uIP buffer + */ +void uipbuf_clear(void); + +/** + * \brief Update uip buffer length for addition of an extension header + * \param len The length of the new extension header + * \retval true if the length fields were successfully set, false otherwise + */ +bool uipbuf_add_ext_hdr(int16_t len); + +/** + * \brief Set the length of the uIP buffer + * \param len The new length + * \retval true if the len was successfully set, false otherwise + */ +bool uipbuf_set_len(uint16_t len); + +/** + * \brief Updates the length field in the uIP buffer + * \param buffer The IPv6 header + * \param len The new length value + */ +void uipbuf_set_len_field(struct uip_ip_hdr *hdr, uint16_t len); + +/** + * \brief Returns the value of the length field in the uIP buffer + * \param buffer The IPv6 header + * \retvel The length value + */ +uint16_t uipbuf_get_len_field(struct uip_ip_hdr *hdr); /** * \brief Get the next IPv6 header. @@ -41,11 +75,11 @@ * \param size The size of the data in the buffer * \param protocol A pointer to a variable where the protocol of the header will be stored * \param start A flag that indicates if this is expected to be the IPv6 packet header or a later header (Extension header) - * \retval returns address of the starting position of the next header + * \retval returns address of the next header, or NULL in case of insufficient buffer space * * This function moves to the next header in a IPv6 packet. */ -uint8_t* uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, uint8_t start); +uint8_t *uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, bool start); /** @@ -53,12 +87,22 @@ uint8_t* uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protoco * \param buffer A pointer to the buffer holding the IPv6 packet * \param size The size of the data in the buffer * \param protocol A pointer to a variable where the protocol of the header will be stored - * \retval returns address of the starting position of the next header + * \retval returns address of the last header, or NULL in case of insufficient buffer space * * This function moves to the last header of the IPv6 packet. */ -uint8_t* uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol); +uint8_t *uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol); +/** + * \brief Get an IPv6 header with a given protocol field. + * \param buffer A pointer to the buffer holding the IPv6 packet + * \param size The size of the data in the buffer + * \param protocol The protocol we are looking for + * \retval returns address of the header if found, else NULL + * + * This function moves to the last header of the IPv6 packet. + */ +uint8_t *uipbuf_search_header(uint8_t *buffer, uint16_t size, uint8_t protocol); /** * \brief Get the value of the attribute @@ -81,6 +125,17 @@ uint16_t uipbuf_get_attr(uint8_t type); */ int uipbuf_set_attr(uint8_t type, uint16_t value); +/** + * \brief Set the default value of the attribute + * \param type The attribute to set the default value of + * \param value The value to set + * \retval 0 - indicates failure of setting the value + * \retval 1 - indicates success of setting the value + * + * This function sets the default value of a uipbuf attribute. + */ +int uipbuf_set_default_attr(uint8_t type, uint16_t value); + /** * \brief Set bits in the uipbuf attribute flags. * \param flag_bits The bits to set in the flag. @@ -115,6 +170,14 @@ uint16_t uipbuf_is_attr_flag(uint16_t flag_bits); */ void uipbuf_clear_attr(void); +/** + * \brief Initialize uipbuf attributes. + * + * This function initialize all attributes in the uipbuf + * attributes including all flags. + */ +void uipbuf_init(void); + /** * \brief The bits defined for uipbuf attributes flag. * @@ -124,6 +187,9 @@ void uipbuf_clear_attr(void); /* Avoid using prefix compression on the packet (6LoWPAN) */ #define UIPBUF_ATTR_FLAGS_6LOWPAN_NO_PREFIX_COMPRESSION 0x02 +/* MAC will set the default for this packet */ +#define UIPBUF_ATTR_LLSEC_LEVEL_MAC_DEFAULT 0xffff + /** * \brief The attributes defined for uipbuf attributes function. * diff --git a/os/net/ipv6/uiplib.c b/os/net/ipv6/uiplib.c index a6d8b4f0c..e4a7a49b7 100644 --- a/os/net/ipv6/uiplib.c +++ b/os/net/ipv6/uiplib.c @@ -56,7 +56,6 @@ #define LOG_LEVEL LOG_LEVEL_NONE /*---------------------------------------------------------------------------*/ -#if NETSTACK_CONF_WITH_IPV6 int uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr) { @@ -118,7 +117,6 @@ uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr) return 1; } -#endif /* NETSTACK_CONF_WITH_IPV6 */ /*---------------------------------------------------------------------------*/ /* Parse a IPv4-address from a string. Returns the number of characters read * for the address. */ diff --git a/os/net/ipv6/uiplib.h b/os/net/ipv6/uiplib.h index b30a1bcb5..aaf91282f 100644 --- a/os/net/ipv6/uiplib.h +++ b/os/net/ipv6/uiplib.h @@ -68,11 +68,7 @@ * \retval 0 If the IP address could not be parsed. * \retval Non-zero If the IP address was parsed. */ -#if NETSTACK_CONF_WITH_IPV6 #define uiplib_ipaddrconv uiplib_ip6addrconv -#else /* NETSTACK_CONF_WITH_IPV6 */ -#define uiplib_ipaddrconv uiplib_ip4addrconv -#endif /* NETSTACK_CONF_WITH_IPV6 */ int uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *addr); int uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *addr); diff --git a/os/net/ipv6/uipopt.h b/os/net/ipv6/uipopt.h index 15c79e20a..a75c59ea0 100644 --- a/os/net/ipv6/uipopt.h +++ b/os/net/ipv6/uipopt.h @@ -121,26 +121,6 @@ * @{ */ -/** - * The link level header length. - * - * This is the offset into the uip_buf where the IP header can be - * found. For Ethernet, this should be set to 14. For SLIP, this - * should be set to 0. - * - * \note we probably won't use this constant for other link layers than - * ethernet as they have variable header length (this is due to variable - * number and type of address fields and to optional security features) - * E.g.: 802.15.4 -> 2 + (1/2*4/8) + 0/5/6/10/14 - * 802.11 -> 4 + (6*3/4) + 2 - * \hideinitializer - */ -#ifdef UIP_CONF_LLH_LEN -#define UIP_LLH_LEN (UIP_CONF_LLH_LEN) -#else /* UIP_LLH_LEN */ -#define UIP_LLH_LEN 0 -#endif /* UIP_CONF_LLH_LEN */ - /** * The size of the uIP packet buffer. * @@ -151,12 +131,11 @@ * \hideinitializer */ #ifndef UIP_CONF_BUFFER_SIZE -#define UIP_BUFSIZE (UIP_LINK_MTU + UIP_LLH_LEN) +#define UIP_BUFSIZE (UIP_LINK_MTU) #else /* UIP_CONF_BUFFER_SIZE */ #define UIP_BUFSIZE (UIP_CONF_BUFFER_SIZE) #endif /* UIP_CONF_BUFFER_SIZE */ - /** * Determines if statistics support should be compiled in. * @@ -248,11 +227,6 @@ void uip_log(char *msg); /** The maximum transmission unit at the IP Layer*/ #define UIP_LINK_MTU 1280 -#ifndef NETSTACK_CONF_WITH_IPV6 -/** Do we use IPv6 or not (default: no) */ -#define NETSTACK_CONF_WITH_IPV6 0 -#endif - #ifndef UIP_CONF_IPV6_QUEUE_PKT /** Do we do per %neighbor queuing during address resolution (default: no) */ #define UIP_CONF_IPV6_QUEUE_PKT 0 @@ -440,15 +414,15 @@ void uip_log(char *msg); * The TCP maximum segment size. * * This is should not be to set to more than - * UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN. + * UIP_BUFSIZE - UIP_IPTCPH_LEN. */ #ifdef UIP_CONF_TCP_MSS -#if UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) +#if UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_IPTCPH_LEN) #error UIP_CONF_TCP_MSS is too large for the current UIP_BUFSIZE -#endif /* UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) */ +#endif /* UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_IPTCPH_LEN) */ #define UIP_TCP_MSS (UIP_CONF_TCP_MSS) #else /* UIP_CONF_TCP_MSS */ -#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) +#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_IPTCPH_LEN) #endif /* UIP_CONF_TCP_MSS */ /** diff --git a/os/net/mac/ble/ble-l2cap.c b/os/net/mac/ble/ble-l2cap.c index 076d919e8..fb755cc85 100644 --- a/os/net/mac/ble/ble-l2cap.c +++ b/os/net/mac/ble/ble-l2cap.c @@ -505,6 +505,12 @@ off(void) return 0; } /*---------------------------------------------------------------------------*/ +static int +max_payload(void) +{ + return BLE_L2CAP_NODE_MTU; +} +/*---------------------------------------------------------------------------*/ const struct mac_driver ble_l2cap_driver = { "ble-l2cap", init, @@ -512,6 +518,7 @@ const struct mac_driver ble_l2cap_driver = { input, on, off, + max_payload, }; /*---------------------------------------------------------------------------*/ PROCESS_THREAD(ble_l2cap_tx_process, ev, data) diff --git a/os/net/mac/csma/anti-replay.c b/os/net/mac/csma/anti-replay.c new file mode 100644 index 000000000..347674184 --- /dev/null +++ b/os/net/mac/csma/anti-replay.c @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2014, Hasso-Plattner-Institut. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Protects against replay attacks by comparing with the last + * unicast or broadcast frame counter of the sender. + * \author + * Konrad Krentz + */ + +/** + * \addtogroup csma + * @{ + */ + +#include "net/mac/csma/anti-replay.h" +#include "net/packetbuf.h" +#include "net/mac/llsec802154.h" + +#if LLSEC802154_USES_FRAME_COUNTER + +/* This node's current frame counter value */ +static uint32_t counter; + +/*---------------------------------------------------------------------------*/ +void +anti_replay_set_counter(void) +{ + frame802154_frame_counter_t reordered_counter; + + ++counter; + reordered_counter.u32 = LLSEC802154_HTONL(counter); + + packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, reordered_counter.u16[0]); + packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, reordered_counter.u16[1]); +} +/*---------------------------------------------------------------------------*/ +uint32_t +anti_replay_get_counter(void) +{ + frame802154_frame_counter_t disordered_counter; + + disordered_counter.u16[0] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1); + disordered_counter.u16[1] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3); + + return LLSEC802154_HTONL(disordered_counter.u32); +} +/*---------------------------------------------------------------------------*/ +void +anti_replay_init_info(struct anti_replay_info *info) +{ + info->last_broadcast_counter + = info->last_unicast_counter + = anti_replay_get_counter(); +} +/*---------------------------------------------------------------------------*/ +int +anti_replay_was_replayed(struct anti_replay_info *info) +{ + uint32_t received_counter; + + received_counter = anti_replay_get_counter(); + + if(packetbuf_holds_broadcast()) { + /* broadcast */ + if(received_counter <= info->last_broadcast_counter) { + return 1; + } else { + info->last_broadcast_counter = received_counter; + return 0; + } + } else { + /* unicast */ + if(received_counter <= info->last_unicast_counter) { + return 1; + } else { + info->last_unicast_counter = received_counter; + return 0; + } + } +} +/*---------------------------------------------------------------------------*/ +#endif /* LLSEC802154_USES_FRAME_COUNTER */ + +/** @} */ diff --git a/os/net/mac/csma/anti-replay.h b/os/net/mac/csma/anti-replay.h new file mode 100644 index 000000000..9211a6e7a --- /dev/null +++ b/os/net/mac/csma/anti-replay.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2014, Hasso-Plattner-Institut. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * Interface to anti-replay mechanisms. + * \author + * Konrad Krentz + */ + +/** + * \addtogroup llsec802154 + * @{ + */ + +#ifndef ANTI_REPLAY_H +#define ANTI_REPLAY_H + +#include "contiki.h" + +struct anti_replay_info { + uint32_t last_broadcast_counter; + uint32_t last_unicast_counter; +}; + +/** + * \brief Sets the frame counter packetbuf attributes. + */ +void anti_replay_set_counter(void); + +/** + * \brief Gets the frame counter from packetbuf. + */ +uint32_t anti_replay_get_counter(void); + +/** + * \brief Initializes the anti-replay information about the sender + * \param info Anti-replay information about the sender + */ +void anti_replay_init_info(struct anti_replay_info *info); + +/** + * \brief Checks if received frame was replayed + * \param info Anti-replay information about the sender + * \retval 0 <-> received frame was not replayed + */ +int anti_replay_was_replayed(struct anti_replay_info *info); + +#endif /* ANTI_REPLAY_H */ + +/** @} */ diff --git a/os/net/mac/csma/ccm-star-packetbuf.c b/os/net/mac/csma/ccm-star-packetbuf.c new file mode 100644 index 000000000..17b9befdc --- /dev/null +++ b/os/net/mac/csma/ccm-star-packetbuf.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2013, Hasso-Plattner-Institut. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * CCM* convenience functions for LLSEC use + * \author + * Justin King-Lacroix + * Konrad Krentz + */ + +#include "net/linkaddr.h" +#include "net/packetbuf.h" +#include "net/mac/llsec802154.h" +#include + +#if LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER + +/*---------------------------------------------------------------------------*/ +static const uint8_t * +get_extended_address(const linkaddr_t *addr) +#if LINKADDR_SIZE == 2 +{ + /* workaround for short addresses: derive EUI64 as in RFC 6282 */ + static linkaddr_extended_t template = { { 0x00 , 0x00 , 0x00 , + 0xFF , 0xFE , 0x00 , 0x00 , 0x00 } }; + template.u16[3] = LLSEC802154_HTONS(addr->u16); + + return template.u8; +} +#else /* LINKADDR_SIZE == 2 */ +{ + return addr->u8; +} +#endif /* LINKADDR_SIZE == 2 */ +/*---------------------------------------------------------------------------*/ +void +ccm_star_packetbuf_set_nonce(uint8_t *nonce, int forward) +{ + const linkaddr_t *source_addr; + + source_addr = forward ? &linkaddr_node_addr : packetbuf_addr(PACKETBUF_ADDR_SENDER); + memcpy(nonce, get_extended_address(source_addr), 8); + nonce[8] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3) >> 8; + nonce[9] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3) & 0xff; + nonce[10] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1) >> 8; + nonce[11] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1) & 0xff; + nonce[12] = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL); +} +/*---------------------------------------------------------------------------*/ +#endif /* LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER */ diff --git a/os/net/mac/csma/ccm-star-packetbuf.h b/os/net/mac/csma/ccm-star-packetbuf.h new file mode 100644 index 000000000..578bdef96 --- /dev/null +++ b/os/net/mac/csma/ccm-star-packetbuf.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2013, Hasso-Plattner-Institut. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#ifndef CCM_STAR_PACKETBUF_H_ +#define CCM_STAR_PACKETBUF_H_ + +/*---------------------------------------------------------------------------*/ +void ccm_star_packetbuf_set_nonce(uint8_t *nonce, int forward); + +#endif /* CCM_STAR_PACKETBUF_H_ */ diff --git a/os/net/mac/csma/csma-output.c b/os/net/mac/csma/csma-output.c index 9c88d569d..653743967 100644 --- a/os/net/mac/csma/csma-output.c +++ b/os/net/mac/csma/csma-output.c @@ -40,6 +40,7 @@ */ #include "net/mac/csma/csma.h" +#include "net/mac/csma/csma-security.h" #include "net/packetbuf.h" #include "net/queuebuf.h" #include "dev/watchdog.h" @@ -49,11 +50,7 @@ #include "net/netstack.h" #include "lib/list.h" #include "lib/memb.h" - -#if CONTIKI_TARGET_COOJA -#include "lib/simEnvChange.h" -#include "sys/cooja_mt.h" -#endif /* CONTIKI_TARGET_COOJA */ +#include "lib/assert.h" /* Log configuration */ #include "sys/log.h" @@ -135,7 +132,10 @@ MEMB(packet_memb, struct packet_queue, MAX_QUEUED_PACKETS); MEMB(metadata_memb, struct qbuf_metadata, MAX_QUEUED_PACKETS); LIST(neighbor_list); -static void packet_sent(void *ptr, int status, int num_transmissions); +static void packet_sent(struct neighbor_queue *n, + struct packet_queue *q, + int status, + int num_transmissions); static void transmit_from_queue(void *ptr); /*---------------------------------------------------------------------------*/ static struct neighbor_queue * @@ -166,7 +166,7 @@ backoff_period(void) } /*---------------------------------------------------------------------------*/ static int -send_one_packet(void *ptr) +send_one_packet(struct neighbor_queue *n, struct packet_queue *q) { int ret; int last_sent_ok = 0; @@ -174,9 +174,16 @@ send_one_packet(void *ptr) packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr); packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1); - if(NETSTACK_FRAMER.create() < 0) { +#if LLSEC802154_ENABLED +#if LLSEC802154_USES_EXPLICIT_KEYS + /* This should possibly be taken from upper layers in the future */ + packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, CSMA_LLSEC_KEY_ID_MODE); +#endif /* LLSEC802154_USES_EXPLICIT_KEYS */ +#endif /* LLSEC802154_ENABLED */ + + if(csma_security_create_frame() < 0) { /* Failed to allocate space for headers */ - LOG_ERR("failed to create packet\n"); + LOG_ERR("failed to create packet, seqno: %d\n", packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)); ret = MAC_TX_ERR_FATAL; } else { int is_broadcast; @@ -201,17 +208,10 @@ send_one_packet(void *ptr) if(is_broadcast) { ret = MAC_TX_OK; } else { - rtimer_clock_t wt; - /* Check for ack */ - wt = RTIMER_NOW(); - watchdog_periodic(); - while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + CSMA_ACK_WAIT_TIME)) { -#if CONTIKI_TARGET_COOJA - simProcessRunValue = 1; - cooja_mt_yield(); -#endif /* CONTIKI_TARGET_COOJA */ - } + + /* Wait for max CSMA_ACK_WAIT_TIME */ + RTIMER_BUSYWAIT_UNTIL(NETSTACK_RADIO.pending_packet(), CSMA_ACK_WAIT_TIME); ret = MAC_TX_NOACK; if(NETSTACK_RADIO.receiving_packet() || @@ -220,17 +220,8 @@ send_one_packet(void *ptr) int len; uint8_t ackbuf[CSMA_ACK_LEN]; - if(CSMA_AFTER_ACK_DETECTED_WAIT_TIME > 0) { - wt = RTIMER_NOW(); - watchdog_periodic(); - while(RTIMER_CLOCK_LT(RTIMER_NOW(), - wt + CSMA_AFTER_ACK_DETECTED_WAIT_TIME)) { -#if CONTIKI_TARGET_COOJA - simProcessRunValue = 1; - cooja_mt_yield(); -#endif /* CONTIKI_TARGET_COOJA */ - } - } + /* Wait an additional CSMA_AFTER_ACK_DETECTED_WAIT_TIME to complete reception */ + RTIMER_BUSYWAIT_UNTIL(NETSTACK_RADIO.pending_packet(), CSMA_AFTER_ACK_DETECTED_WAIT_TIME); if(NETSTACK_RADIO.pending_packet()) { len = NETSTACK_RADIO.read(ackbuf, CSMA_ACK_LEN); @@ -258,7 +249,7 @@ send_one_packet(void *ptr) last_sent_ok = 1; } - packet_sent(ptr, ret, 1); + packet_sent(n, q, ret, 1); return last_sent_ok; } /*---------------------------------------------------------------------------*/ @@ -276,7 +267,7 @@ transmit_from_queue(void *ptr) n->transmissions, list_length(n->packet_queue)); /* Send first packet in the neighbor queue */ queuebuf_to_packetbuf(q->buf); - send_one_packet(n); + send_one_packet(n, q); } } } @@ -409,30 +400,15 @@ tx_ok(struct packet_queue *q, struct neighbor_queue *n, int num_transmissions) } /*---------------------------------------------------------------------------*/ static void -packet_sent(void *ptr, int status, int num_transmissions) +packet_sent(struct neighbor_queue *n, + struct packet_queue *q, + int status, + int num_transmissions) { - struct neighbor_queue *n; - struct packet_queue *q; + assert(n != NULL); + assert(q != NULL); - n = ptr; - if(n == NULL) { - return; - } - - /* Find out what packet this callback refers to */ - for(q = list_head(n->packet_queue); - q != NULL; q = list_item_next(q)) { - if(queuebuf_attr(q->buf, PACKETBUF_ATTR_MAC_SEQNO) == - packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) { - break; - } - } - - if(q == NULL) { - LOG_WARN("packet sent: seqno %u not found\n", - packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)); - return; - } else if(q->ptr == NULL) { + if(q->ptr == NULL) { LOG_WARN("packet sent: no metadata\n"); return; } @@ -560,5 +536,4 @@ csma_output_init(void) memb_init(&packet_memb); memb_init(&metadata_memb); memb_init(&neighbor_memb); - queuebuf_init(); } diff --git a/os/net/mac/csma/csma-security.c b/os/net/mac/csma/csma-security.c new file mode 100644 index 000000000..0b44fa348 --- /dev/null +++ b/os/net/mac/csma/csma-security.c @@ -0,0 +1,308 @@ +/* + * Copyright (c) 2017, RISE SICS + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * CSMA security + * \author + * Joakim Eriksson + */ + +/** + * \addtogroup csma + * @{ +*/ + +#include "contiki.h" +#include "net/mac/csma/csma.h" +#include "net/mac/csma/anti-replay.h" +#include "net/mac/csma/csma-security.h" +#include "net/mac/framer/frame802154.h" +#include "net/mac/framer/framer-802154.h" +#include "net/mac/llsec802154.h" +#include "net/netstack.h" +#include "net/packetbuf.h" +#include "lib/ccm-star.h" +#include "lib/aes-128.h" +#include +#include +#include "ccm-star-packetbuf.h" +/* Log configuration */ +#include "sys/log.h" +#define LOG_MODULE "CSMA" +#define LOG_LEVEL LOG_LEVEL_MAC + +#if LOG_LEVEL == LOG_LEVEL_DBG +static const char * HEX = "0123456789ABCDEF"; +#endif + +#if LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER + +#define MIC_LEN(level) LLSEC802154_MIC_LEN(level) + +#if LLSEC802154_USES_EXPLICIT_KEYS +#define LLSEC_KEY_INDEX (FRAME802154_IMPLICIT_KEY == packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE) \ + ? 0 \ + : packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX)) +#define LLSEC_KEY_MODE (packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE)) +#else +#define LLSEC_KEY_INDEX (0) +#define LLSEC_KEY_MODE (FRAME802154_IMPLICIT_KEY) +#endif /* LLSEC802154_USES_EXPLICIT_KEYS */ + +/** + * The keys for LLSEC for CSMA + */ +typedef struct { + uint8_t u8[16]; +} aes_key_t; +static aes_key_t keys[CSMA_LLSEC_MAXKEYS]; + +/* assumed to be 16 bytes */ +int +csma_security_set_key(uint8_t index, const uint8_t *key) +{ + if(key != NULL && index < CSMA_LLSEC_MAXKEYS) { + memcpy(keys[index].u8, key, 16); + return 1; + } + return 0; +} + +#define N_KEYS (sizeof(keys) / sizeof(aes_key)) +/*---------------------------------------------------------------------------*/ +static int +aead(uint8_t hdrlen, int forward) +{ + uint8_t totlen; + uint8_t nonce[CCM_STAR_NONCE_LENGTH]; + uint8_t *m; + uint8_t m_len; + uint8_t *a; + uint8_t a_len; + uint8_t *result; + /* Allocate for MAX level */ + uint8_t generated_mic[MIC_LEN(7)]; + uint8_t *mic; + uint8_t key_index; + aes_key_t *key; + uint8_t with_encryption; + + key_index = LLSEC_KEY_INDEX; + if(key_index >= CSMA_LLSEC_MAXKEYS) { + LOG_ERR("Key not available: %u\n", key_index); + return 0; + } + + key = &keys[key_index]; + + ccm_star_packetbuf_set_nonce(nonce, forward); + totlen = packetbuf_totlen(); + a = packetbuf_hdrptr(); + + with_encryption = + (packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x4) ? 1 : 0; + + if(with_encryption) { + a_len = hdrlen; + m = a + a_len; + m_len = totlen - hdrlen; + } else { + a_len = totlen; + m = NULL; + m_len = 0; + } + + mic = a + totlen; + result = forward ? mic : generated_mic; + + CCM_STAR.set_key(key->u8); + CCM_STAR.aead(nonce, + m, m_len, + a, a_len, + result, MIC_LEN(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x07), + forward); + + if(forward) { + packetbuf_set_datalen(packetbuf_datalen() + MIC_LEN(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x07)); + return 1; + } else { + return (memcmp(generated_mic, mic, MIC_LEN(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x07)) == 0); + } +} + +/*---------------------------------------------------------------------------*/ +int +csma_security_create_frame(void) +{ + int hdr_len; + + packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME); + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0 && + LLSEC_KEY_INDEX != 0xffff) { + anti_replay_set_counter(); + } + + hdr_len = NETSTACK_FRAMER.create(); + if(hdr_len < 0) { + return hdr_len; + } + + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0) { +#if LOG_LEVEL == LOG_LEVEL_DBG + int i = 0; + uint8_t *p; + LOG_DBG(" Payload before (%d):", packetbuf_totlen()); + p = packetbuf_hdrptr(); + for(i = 0; i < packetbuf_totlen(); i++) { + LOG_DBG_("%c%c", HEX[(p[i] >> 4) & 0x0f], HEX[p[i] & 0x0f]); + } + LOG_DBG("\n"); +#endif + + if(!aead(hdr_len, 1)) { + LOG_ERR("failed to encrypt packet to "); + LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); + LOG_ERR_("\n"); + return FRAMER_FAILED; + } + LOG_INFO("LLSEC-OUT:"); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER)); + LOG_INFO_(" "); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); + LOG_INFO_(" %u (%u) LV:%d, KEY:0x%02x\n", packetbuf_datalen(), packetbuf_totlen(), + packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL), LLSEC_KEY_INDEX); + +#if LOG_LEVEL == LOG_LEVEL_DBG + LOG_DBG(" Payload after: (%d)", packetbuf_totlen()); + p = packetbuf_hdrptr(); + for(i = 0; i < packetbuf_totlen(); i++) { + LOG_DBG_("%c%c", HEX[(p[i] >> 4) & 0x0f], HEX[p[i] & 0x0f]); + } + LOG_DBG_("\n"); +#endif + + } + return hdr_len; +} + +/*---------------------------------------------------------------------------*/ +int +csma_security_frame_len(void) +{ + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0 && + LLSEC_KEY_INDEX != 0xffff) { + return NETSTACK_FRAMER.length() + + MIC_LEN(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x07); + } + return NETSTACK_FRAMER.length(); +} +/*---------------------------------------------------------------------------*/ +int +csma_security_parse_frame(void) +{ + int hdr_len; + + hdr_len = NETSTACK_FRAMER.parse(); + if(hdr_len < 0) { + return hdr_len; + } + + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) == 0) { + /* No security - no more processing required */ + return hdr_len; + } + + LOG_INFO("LLSEC-IN: "); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER)); + LOG_INFO_(" "); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); + LOG_INFO_(" %d %u (%u) LV:%d KM:%d KEY:0x%02x\n", hdr_len, packetbuf_datalen(), + packetbuf_totlen(), packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL), + LLSEC_KEY_MODE, + LLSEC_KEY_INDEX); + + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) != CSMA_LLSEC_SECURITY_LEVEL) { + LOG_INFO("received frame with wrong security level (%u) from ", + packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER)); + LOG_INFO_("\n"); + return FRAMER_FAILED; + } + + if(LLSEC_KEY_MODE != CSMA_LLSEC_KEY_ID_MODE) { + LOG_INFO("received frame with wrong key id mode (%u) from ", LLSEC_KEY_MODE); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER)); + LOG_INFO("\n"); + return FRAMER_FAILED; + } + + if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER), &linkaddr_node_addr)) { + LOG_INFO("frame from ourselves\n"); + return FRAMER_FAILED; + } + + if(packetbuf_datalen() <= MIC_LEN(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x07)) { + LOG_ERR("MIC error - too little data in frame!\n"); + return FRAMER_FAILED; + } + + packetbuf_set_datalen(packetbuf_datalen() - MIC_LEN(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x07)); + if(!aead(hdr_len, 0)) { + LOG_INFO("received unauthentic frame %u from ", + (unsigned int) anti_replay_get_counter()); + LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER)); + LOG_INFO_("\n"); + return FRAMER_FAILED; + } + + /* TODO anti-reply protection */ + return hdr_len; +} +/*---------------------------------------------------------------------------*/ +#else +/* The "unsecure" version of the create frame / parse frame */ +int +csma_security_create_frame(void) +{ + packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME); + return NETSTACK_FRAMER.create(); +} +int +csma_security_parse_frame(void) +{ + return NETSTACK_FRAMER.parse(); +} + +#endif /* LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER */ + +/** @} */ diff --git a/os/net/mac/csma/csma-security.h b/os/net/mac/csma/csma-security.h new file mode 100644 index 000000000..97092b84d --- /dev/null +++ b/os/net/mac/csma/csma-security.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018, Tiny Mesh AS + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * LLSEC802154 Security related configuration + * \author + * Olav Frengstad + */ + +#ifndef CSMA_SECURITY_H_ +#define CSMA_SECURITY_H_ + + +#ifdef CSMA_CONF_LLSEC_DEFAULT_KEY0 +#define CSMA_LLSEC_DEFAULT_KEY0 CSMA_CONF_LLSEC_DEFAULT_KEY0 +#else +#define CSMA_LLSEC_DEFAULT_KEY0 {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f} +#endif + +#ifdef CSMA_CONF_LLSEC_SECURITY_LEVEL +#define CSMA_LLSEC_SECURITY_LEVEL CSMA_CONF_LLSEC_SECURITY_LEVEL +#else +#define CSMA_LLSEC_SECURITY_LEVEL 5 +#endif /* CSMA_CONF_LLSEC_SECURITY_LEVEL */ + +#ifdef CSMA_CONF_LLSEC_KEY_ID_MODE +#define CSMA_LLSEC_KEY_ID_MODE CSMA_CONF_LLSEC_KEY_ID_MODE +#else +#define CSMA_LLSEC_KEY_ID_MODE FRAME802154_IMPLICIT_KEY +#endif /* CSMA_CONF_LLSEC_KEY_ID_MODE */ + +#ifdef CSMA_CONF_LLSEC_KEY_INDEX +#define CSMA_LLSEC_KEY_INDEX CSMA_CONF_LLSEC_KEY_INDEX +#else +#define CSMA_LLSEC_KEY_INDEX 0 +#endif /* CSMA_CONF_LLSEC_KEY_INDEX */ + +#ifdef CSMA_CONF_LLSEC_MAXKEYS +#define CSMA_LLSEC_MAXKEYS CSMA_CONF_LLSEC_MAXKEYS +#else +#define CSMA_LLSEC_MAXKEYS 1 +#endif + +#endif /* CSMA_SECURITY_H_ */ diff --git a/os/net/mac/csma/csma.c b/os/net/mac/csma/csma.c index 7259e35c8..87aa6a1af 100644 --- a/os/net/mac/csma/csma.c +++ b/os/net/mac/csma/csma.c @@ -49,10 +49,25 @@ #define LOG_MODULE "CSMA" #define LOG_LEVEL LOG_LEVEL_MAC + +static void +init_sec(void) +{ +#if LLSEC802154_USES_AUX_HEADER + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) == + PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT) { + packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, + CSMA_LLSEC_SECURITY_LEVEL); + } +#endif +} /*---------------------------------------------------------------------------*/ static void send_packet(mac_callback_t sent, void *ptr) { + + init_sec(); + csma_output_packet(sent, ptr); } /*---------------------------------------------------------------------------*/ @@ -60,22 +75,20 @@ static void input_packet(void) { #if CSMA_SEND_SOFT_ACK - int original_datalen; - uint8_t *original_dataptr; - - original_datalen = packetbuf_datalen(); - original_dataptr = packetbuf_dataptr(); + uint8_t ackdata[CSMA_ACK_LEN]; #endif if(packetbuf_datalen() == CSMA_ACK_LEN) { /* Ignore ack packets */ LOG_DBG("ignored ack\n"); - } else if(NETSTACK_FRAMER.parse() < 0) { + } else if(csma_security_parse_frame() < 0) { LOG_ERR("failed to parse %u\n", packetbuf_datalen()); } else if(!linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_node_addr) && !packetbuf_holds_broadcast()) { LOG_WARN("not for us\n"); + } else if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER), &linkaddr_node_addr)) { + LOG_WARN("frame from ourselves\n"); } else { int duplicate = 0; @@ -91,20 +104,11 @@ input_packet(void) } #if CSMA_SEND_SOFT_ACK - { - frame802154_t info154; - frame802154_parse(original_dataptr, original_datalen, &info154); - if(info154.fcf.frame_type == FRAME802154_DATAFRAME && - info154.fcf.ack_required != 0 && - linkaddr_cmp((linkaddr_t *)&info154.dest_addr, - &linkaddr_node_addr)) { - uint8_t ackdata[CSMA_ACK_LEN] = {0, 0, 0}; - - ackdata[0] = FRAME802154_ACKFRAME; - ackdata[1] = 0; - ackdata[2] = info154.seq; - NETSTACK_RADIO.send(ackdata, CSMA_ACK_LEN); - } + if(packetbuf_attr(PACKETBUF_ATTR_MAC_ACK)) { + ackdata[0] = FRAME802154_ACKFRAME; + ackdata[1] = 0; + ackdata[2] = ((uint8_t *)packetbuf_hdrptr())[2]; + NETSTACK_RADIO.send(ackdata, CSMA_ACK_LEN); } #endif /* CSMA_SEND_SOFT_ACK */ if(!duplicate) { @@ -131,16 +135,41 @@ off(void) static void init(void) { + +#if LLSEC802154_USES_AUX_HEADER +#ifdef CSMA_LLSEC_DEFAULT_KEY0 + uint8_t key[16] = CSMA_LLSEC_DEFAULT_KEY0; + csma_security_set_key(0, key); +#endif +#endif /* LLSEC802154_USES_AUX_HEADER */ csma_output_init(); on(); } /*---------------------------------------------------------------------------*/ +static int +max_payload(void) +{ + int framer_hdrlen; + + init_sec(); + + framer_hdrlen = NETSTACK_FRAMER.length(); + + if(framer_hdrlen < 0) { + /* Framing failed, we assume the maximum header length */ + framer_hdrlen = CSMA_MAC_MAX_HEADER; + } + + return CSMA_MAC_LEN - framer_hdrlen; +} +/*---------------------------------------------------------------------------*/ const struct mac_driver csma_driver = { "CSMA", init, send_packet, input_packet, on, - off + off, + max_payload, }; /*---------------------------------------------------------------------------*/ diff --git a/os/net/mac/csma/csma.h b/os/net/mac/csma/csma.h index 9e0773f28..88396db0b 100644 --- a/os/net/mac/csma/csma.h +++ b/os/net/mac/csma/csma.h @@ -65,6 +65,25 @@ #define CSMA_ACK_LEN 3 +/* Default MAC len for 802.15.4 classic */ +#ifdef CSMA_MAC_CONF_LEN +#define CSMA_MAC_LEN CSMA_MAC_CONF_LEN +#else +#define CSMA_MAC_LEN 127 - 2 +#endif + +/* just a default - with LLSEC, etc */ +#define CSMA_MAC_MAX_HEADER 21 + + extern const struct mac_driver csma_driver; +/* CSMA security framer functions */ +int csma_security_create_frame(void); +int csma_security_parse_frame(void); + +/* key management for CSMA */ +int csma_security_set_key(uint8_t index, const uint8_t *key); + + #endif /* CSMA_H_ */ diff --git a/os/net/mac/framer/framer-802154.c b/os/net/mac/framer/framer-802154.c index b4c24da19..8e92b9b2d 100644 --- a/os/net/mac/framer/framer-802154.c +++ b/os/net/mac/framer/framer-802154.c @@ -240,6 +240,7 @@ parse(void) if(hdr_len && packetbuf_hdrreduce(hdr_len)) { packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, frame.fcf.frame_type); + packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, frame.fcf.ack_required); if(frame.fcf.dest_addr_mode) { if(frame.dest_pid != frame802154_get_pan_id() && diff --git a/os/net/mac/framer/nullframer.c b/os/net/mac/framer/nullframer.c index 019ed2209..ae7bc925d 100644 --- a/os/net/mac/framer/nullframer.c +++ b/os/net/mac/framer/nullframer.c @@ -87,6 +87,7 @@ parse(void) } packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr); packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, frame.seq); + packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, frame.fcf.ack_required); return 0; } #endif diff --git a/os/net/mac/llsec802154.h b/os/net/mac/llsec802154.h index fe1a9d231..e808fef7a 100644 --- a/os/net/mac/llsec802154.h +++ b/os/net/mac/llsec802154.h @@ -74,6 +74,12 @@ #define LLSEC802154_USES_AUX_HEADER LLSEC802154_ENABLED #endif /* LLSEC802154_CONF_USES_AUX_HEADER */ +#ifdef LLSEC802154_CONF_USES_FRAME_COUNTER +#define LLSEC802154_USES_FRAME_COUNTER LLSEC802154_CONF_USES_FRAME_COUNTER +#else +#define LLSEC802154_USES_FRAME_COUNTER LLSEC802154_ENABLED +#endif /* LLSEC802154_CONF_USES_FRAME_COUNTER */ + #if UIP_BYTE_ORDER == UIP_LITTLE_ENDIAN #define LLSEC802154_HTONS(n) (n) #define LLSEC802154_HTONL(n) (n) diff --git a/os/net/mac/mac.h b/os/net/mac/mac.h index 4f2a75b15..f64838b4c 100644 --- a/os/net/mac/mac.h +++ b/os/net/mac/mac.h @@ -76,6 +76,9 @@ struct mac_driver { /** Turn the MAC layer off. */ int (* off)(void); + + /** Read out estimated max payload size based on payload in packetbuf */ + int (* max_payload)(void); }; /* Generic MAC return values. */ diff --git a/os/net/mac/nullmac/nullmac.c b/os/net/mac/nullmac/nullmac.c index a6199a3f9..de92ee5d8 100644 --- a/os/net/mac/nullmac/nullmac.c +++ b/os/net/mac/nullmac/nullmac.c @@ -67,6 +67,12 @@ off(void) return 0; } /*---------------------------------------------------------------------------*/ +static int +max_payload(void) +{ + return 0; +} +/*---------------------------------------------------------------------------*/ static void init(void) { @@ -78,6 +84,7 @@ const struct mac_driver nullmac_driver = { send_packet, packet_input, on, - off + off, + max_payload, }; /*---------------------------------------------------------------------------*/ diff --git a/os/net/mac/tsch/tsch-adaptive-timesync.c b/os/net/mac/tsch/tsch-adaptive-timesync.c index cf10e9cf1..2745abca2 100644 --- a/os/net/mac/tsch/tsch-adaptive-timesync.c +++ b/os/net/mac/tsch/tsch-adaptive-timesync.c @@ -45,6 +45,7 @@ #include "net/mac/tsch/tsch.h" #include +#include #if TSCH_ADAPTIVE_TIMESYNC diff --git a/os/net/mac/tsch/tsch-const.h b/os/net/mac/tsch/tsch-const.h index edc989378..c4712ce2d 100644 --- a/os/net/mac/tsch/tsch-const.h +++ b/os/net/mac/tsch/tsch-const.h @@ -66,8 +66,8 @@ /* 1 channel, sequence length 1 */ #define TSCH_HOPPING_SEQUENCE_1_1 (uint8_t[]){ 20 } -/* Max TSCH packet lenght */ -#define TSCH_PACKET_MAX_LEN MIN(127, PACKETBUF_SIZE) +/* Max TSCH packet lenght - last bytes are CRC in default 802.15.4 packets */ +#define TSCH_PACKET_MAX_LEN MIN(127 - 2, PACKETBUF_SIZE) /* The jitter to remove in ticks. * This should be the sum of measurement errors on Tx and Rx nodes. @@ -77,9 +77,7 @@ /* The approximate number of slots per second */ #define TSCH_SLOTS_PER_SECOND (1000000 / tsch_timing_us[tsch_ts_timeslot_length]) -/* Calculate packet tx/rx duration in rtimer ticks based on sent - * packet len in bytes with 802.15.4 250kbps data rate. - * One byte = 32us. Add two bytes for CRC and one for len field */ +/* Calculate packet tx/rx duration in rtimer ticks based on packet length in bytes. */ #define TSCH_PACKET_DURATION(len) US_TO_RTIMERTICKS(RADIO_BYTE_AIR_TIME * ((len) + RADIO_PHY_OVERHEAD)) /* Convert rtimer ticks to clock and vice versa */ diff --git a/os/net/mac/tsch/tsch-rpl.c b/os/net/mac/tsch/tsch-rpl.c index 951648d91..e8db94b57 100644 --- a/os/net/mac/tsch/tsch-rpl.c +++ b/os/net/mac/tsch/tsch-rpl.c @@ -136,5 +136,13 @@ tsch_rpl_callback_parent_switch(rpl_parent_t *old, rpl_parent_t *new) rpl_parent_get_ipaddr(new))); } } +/*---------------------------------------------------------------------------*/ +/* Check RPL has joined DODAG. + * To use, set #define TSCH_RPL_CHECK_DODAG_JOINED tsch_rpl_check_dodag_joined */ +int +tsch_rpl_check_dodag_joined(void) +{ + return NETSTACK_ROUTING.node_has_joined(); +} #endif /* UIP_CONF_IPV6_RPL */ /** @} */ diff --git a/os/net/mac/tsch/tsch-rpl.h b/os/net/mac/tsch/tsch-rpl.h index 23a148db8..95412e3d2 100644 --- a/os/net/mac/tsch/tsch-rpl.h +++ b/os/net/mac/tsch/tsch-rpl.h @@ -79,6 +79,12 @@ void tsch_rpl_callback_new_dio_interval(clock_time_t dio_interval); * \param new The new RPL parent */ void tsch_rpl_callback_parent_switch(rpl_parent_t *old, rpl_parent_t *new); +/** + * \brief Check RPL has joined DODAG. + * To use, set TSCH_RPL_CHECK_DODAG_JOINED tsch_rpl_check_dodag_joined + * \return 1 if joined, 0 otherwise + */ +int tsch_rpl_check_dodag_joined(void); #endif /* __TSCH_RPL_H__ */ /** @} */ diff --git a/os/net/mac/tsch/tsch-slot-operation.c b/os/net/mac/tsch/tsch-slot-operation.c index b1a3f6ba8..4397a4805 100644 --- a/os/net/mac/tsch/tsch-slot-operation.c +++ b/os/net/mac/tsch/tsch-slot-operation.c @@ -52,10 +52,7 @@ #include "net/queuebuf.h" #include "net/mac/framer/framer-802154.h" #include "net/mac/tsch/tsch.h" -#if CONTIKI_TARGET_COOJA -#include "lib/simEnvChange.h" -#include "sys/cooja_mt.h" -#endif /* CONTIKI_TARGET_COOJA */ +#include "sys/critical.h" #include "sys/log.h" /* TSCH debug macros, i.e. to set LEDs or GPIOs on various TSCH @@ -130,9 +127,10 @@ struct tsch_packet *dequeued_array[TSCH_DEQUEUED_ARRAY_SIZE]; struct ringbufindex input_ringbuf; struct input_packet input_array[TSCH_MAX_INCOMING_PACKETS]; +/* Updates and reads of the next two variables must be atomic (i.e. both together) */ /* Last time we received Sync-IE (ACK or data packet from a time source) */ static struct tsch_asn_t last_sync_asn; -clock_time_t last_sync_time; /* Same info, in clock_time_t units */ +clock_time_t tsch_last_sync_time; /* Same info, in clock_time_t units */ /* A global lock for manipulating data structures safely from outside of interrupt */ static volatile int tsch_locked = 0; @@ -206,10 +204,7 @@ tsch_get_lock(void) busy_wait = 1; busy_wait_time = RTIMER_NOW(); while(tsch_in_slot_operation) { -#if CONTIKI_TARGET_COOJA - simProcessRunValue = 1; - cooja_mt_yield(); -#endif /* CONTIKI_TARGET_COOJA */ + watchdog_periodic(); } busy_wait_time = RTIMER_NOW() - busy_wait_time; } @@ -303,7 +298,7 @@ tsch_schedule_slot_operation(struct rtimer *tm, rtimer_clock_t ref_time, rtimer_ } /* block until the time to schedule comes */ - BUSYWAIT_UNTIL_ABS(0, ref_time, offset); + RTIMER_BUSYWAIT_UNTIL_ABS(0, ref_time, offset); return 0; } /*---------------------------------------------------------------------------*/ @@ -314,7 +309,7 @@ tsch_schedule_slot_operation(struct rtimer *tm, rtimer_clock_t ref_time, rtimer_ do { \ if(tsch_schedule_slot_operation(tm, ref_time, offset - RTIMER_GUARD, str)) { \ PT_YIELD(pt); \ - BUSYWAIT_UNTIL_ABS(0, ref_time, offset); \ + RTIMER_BUSYWAIT_UNTIL_ABS(0, ref_time, offset); \ } \ } while(0); /*---------------------------------------------------------------------------*/ @@ -354,6 +349,33 @@ get_packet_and_neighbor_for_link(struct tsch_link *link, struct tsch_neighbor ** return p; } /*---------------------------------------------------------------------------*/ +uint64_t +tsch_get_network_uptime_ticks(void) +{ + uint64_t uptime_asn; + uint64_t uptime_ticks; + int_master_status_t status; + + if(!tsch_is_associated) { + /* not associated, network uptime is not known */ + return (uint64_t)-1; + } + + status = critical_enter(); + + uptime_asn = last_sync_asn.ls4b + ((uint64_t)last_sync_asn.ms1b << 32); + /* first calculate the at the uptime at the last sync in rtimer ticks */ + uptime_ticks = uptime_asn * tsch_timing[tsch_ts_timeslot_length]; + /* then convert to clock ticks (assume that CLOCK_SECOND divides RTIMER_ARCH_SECOND) */ + uptime_ticks /= (RTIMER_ARCH_SECOND / CLOCK_SECOND); + /* then add the ticks passed since the last timesync */ + uptime_ticks += (clock_time() - tsch_last_sync_time); + + critical_exit(status); + + return uptime_ticks; +} +/*---------------------------------------------------------------------------*/ /** * This function turns on the radio. Its semantics is dependent on * the value of TSCH_RADIO_ON_DURING_TIMESLOT constant: @@ -514,7 +536,7 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t)) TSCH_DEBUG_TX_EVENT(); tsch_radio_on(TSCH_RADIO_CMD_ON_WITHIN_TIMESLOT); /* CCA */ - BUSYWAIT_UNTIL_ABS(!(cca_status &= NETSTACK_RADIO.channel_clear()), + RTIMER_BUSYWAIT_UNTIL_ABS(!(cca_status &= NETSTACK_RADIO.channel_clear()), current_slot_start, tsch_timing[tsch_ts_cca_offset] + tsch_timing[tsch_ts_cca]); TSCH_DEBUG_TX_EVENT(); /* there is not enough time to turn radio off */ @@ -561,14 +583,14 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t)) TSCH_DEBUG_TX_EVENT(); tsch_radio_on(TSCH_RADIO_CMD_ON_WITHIN_TIMESLOT); /* Wait for ACK to come */ - BUSYWAIT_UNTIL_ABS(NETSTACK_RADIO.receiving_packet(), + RTIMER_BUSYWAIT_UNTIL_ABS(NETSTACK_RADIO.receiving_packet(), tx_start_time, tx_duration + tsch_timing[tsch_ts_rx_ack_delay] + tsch_timing[tsch_ts_ack_wait] + RADIO_DELAY_BEFORE_DETECT); TSCH_DEBUG_TX_EVENT(); ack_start_time = RTIMER_NOW() - RADIO_DELAY_BEFORE_DETECT; /* Wait for ACK to finish */ - BUSYWAIT_UNTIL_ABS(!NETSTACK_RADIO.receiving_packet(), + RTIMER_BUSYWAIT_UNTIL_ABS(!NETSTACK_RADIO.receiving_packet(), ack_start_time, tsch_timing[tsch_ts_max_ack]); TSCH_DEBUG_TX_EVENT(); tsch_radio_off(TSCH_RADIO_CMD_OFF_WITHIN_TIMESLOT); @@ -630,7 +652,7 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t)) tsch_timesync_update(current_neighbor, since_last_timesync, drift_correction); /* Keep track of sync time */ last_sync_asn = tsch_current_asn; - last_sync_time = clock_time(); + tsch_last_sync_time = clock_time(); tsch_schedule_keepalive(); } mac_tx_status = MAC_TX_OK; @@ -650,6 +672,8 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t)) mac_tx_status = MAC_TX_ERR; } } + } else { + mac_tx_status = MAC_TX_ERR; } } @@ -748,7 +772,7 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t)) packet_seen = NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet(); if(!packet_seen) { /* Check if receiving within guard time */ - BUSYWAIT_UNTIL_ABS((packet_seen = NETSTACK_RADIO.receiving_packet()), + RTIMER_BUSYWAIT_UNTIL_ABS((packet_seen = NETSTACK_RADIO.receiving_packet()), current_slot_start, tsch_timing[tsch_ts_rx_offset] + tsch_timing[tsch_ts_rx_wait] + RADIO_DELAY_BEFORE_DETECT); } if(!packet_seen) { @@ -760,7 +784,7 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t)) rx_start_time = RTIMER_NOW() - RADIO_DELAY_BEFORE_DETECT; /* Wait until packet is received, turn radio off */ - BUSYWAIT_UNTIL_ABS(!NETSTACK_RADIO.receiving_packet(), + RTIMER_BUSYWAIT_UNTIL_ABS(!NETSTACK_RADIO.receiving_packet(), current_slot_start, tsch_timing[tsch_ts_rx_offset] + tsch_timing[tsch_ts_rx_wait] + tsch_timing[tsch_ts_max_tx]); TSCH_DEBUG_RX_EVENT(); tsch_radio_off(TSCH_RADIO_CMD_OFF_WITHIN_TIMESLOT); @@ -887,7 +911,7 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t)) int32_t since_last_timesync = TSCH_ASN_DIFF(tsch_current_asn, last_sync_asn); /* Keep track of last sync time */ last_sync_asn = tsch_current_asn; - last_sync_time = clock_time(); + tsch_last_sync_time = clock_time(); /* Save estimated drift */ drift_correction = -estimated_drift; is_drift_correction_used = 1; @@ -1019,6 +1043,13 @@ PT_THREAD(tsch_slot_operation(struct rtimer *t, void *ptr)) /* End of slot operation, schedule next slot or resynchronize */ + if(tsch_is_coordinator) { + /* Update the `last_sync_*` variables to avoid large errors + * in the application-level time synchronization */ + last_sync_asn = tsch_current_asn; + tsch_last_sync_time = clock_time(); + } + /* Do we need to resynchronize? i.e., wait for EB again */ if(!tsch_is_coordinator && (TSCH_ASN_DIFF(tsch_current_asn, last_sync_asn) > (100 * TSCH_CLOCK_TO_SLOTS(TSCH_DESYNC_THRESHOLD / 100, tsch_timing[tsch_ts_timeslot_length])))) { @@ -1120,10 +1151,14 @@ void tsch_slot_operation_sync(rtimer_clock_t next_slot_start, struct tsch_asn_t *next_slot_asn) { + int_master_status_t status; + current_slot_start = next_slot_start; tsch_current_asn = *next_slot_asn; + status = critical_enter(); last_sync_asn = tsch_current_asn; - last_sync_time = clock_time(); + tsch_last_sync_time = clock_time(); + critical_exit(status); current_link = NULL; } /*---------------------------------------------------------------------------*/ diff --git a/os/net/mac/tsch/tsch-slot-operation.h b/os/net/mac/tsch/tsch-slot-operation.h index b1c05697b..22547a2c9 100644 --- a/os/net/mac/tsch/tsch-slot-operation.h +++ b/os/net/mac/tsch/tsch-slot-operation.h @@ -56,7 +56,7 @@ extern struct tsch_packet *dequeued_array[TSCH_DEQUEUED_ARRAY_SIZE]; extern struct ringbufindex input_ringbuf; extern struct input_packet input_array[TSCH_MAX_INCOMING_PACKETS]; /* Last clock_time_t where synchronization happened */ -extern clock_time_t last_sync_time; +extern clock_time_t tsch_last_sync_time; /* Counts the length of the current burst */ extern int tsch_current_burst_count; diff --git a/os/net/mac/tsch/tsch.c b/os/net/mac/tsch/tsch.c index e78b888d0..6278ce0dc 100644 --- a/os/net/mac/tsch/tsch.c +++ b/os/net/mac/tsch/tsch.c @@ -243,11 +243,49 @@ tsch_reset(void) } /* TSCH keep-alive functions */ +/*---------------------------------------------------------------------------*/ +/* Resynchronize to last_eb_nbr. + * Return non-zero if this function schedules the next keepalive. + * Return zero otherwise. + */ +static int +resynchronize(const linkaddr_t *original_time_source_addr) +{ + const struct tsch_neighbor *current_time_source = tsch_queue_get_time_source(); + if(current_time_source && !linkaddr_cmp(¤t_time_source->addr, original_time_source_addr)) { + /* Time source has already been changed (e.g. by RPL). Let's see if it works. */ + LOG_INFO("time source has been changed to "); + LOG_INFO_LLADDR(¤t_time_source->addr); + LOG_INFO_("\n"); + return 0; + } + /* Switch time source to the last neighbor we received an EB from */ + if(linkaddr_cmp(&last_eb_nbr_addr, &linkaddr_null)) { + LOG_WARN("not able to re-synchronize, received no EB from other neighbors\n"); + if(sync_count == 0) { + /* We got no synchronization at all in this session, leave the network */ + tsch_disassociate(); + } + return 0; + } else { + LOG_WARN("re-synchronizing on "); + LOG_WARN_LLADDR(&last_eb_nbr_addr); + LOG_WARN_("\n"); + /* We simply pick the last neighbor we receiver sync information from */ + tsch_queue_update_time_source(&last_eb_nbr_addr); + tsch_join_priority = last_eb_nbr_jp + 1; + /* Try to get in sync ASAP */ + tsch_schedule_keepalive_immediately(); + return 1; + } +} + /*---------------------------------------------------------------------------*/ /* Tx callback for keepalive messages */ static void keepalive_packet_sent(void *ptr, int status, int transmissions) { + int schedule_next_keepalive = 1; /* Update neighbor link statistics */ link_stats_packet_sent(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), status, transmissions); /* Call RPL callback if RPL is enabled */ @@ -258,28 +296,14 @@ keepalive_packet_sent(void *ptr, int status, int transmissions) LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); LOG_INFO_(", st %d-%d\n", status, transmissions); - /* We got no ack, try to recover by switching to the last neighbor we received an EB from */ + /* We got no ack, try to resynchronize */ if(status == MAC_TX_NOACK) { - if(linkaddr_cmp(&last_eb_nbr_addr, &linkaddr_null)) { - LOG_WARN("not able to re-synchronize, received no EB from other neighbors\n"); - if(sync_count == 0) { - /* We got no synchronization at all in this session, leave the network */ - tsch_disassociate(); - } - } else { - LOG_WARN("re-synchronizing on "); - LOG_WARN_LLADDR(&last_eb_nbr_addr); - LOG_WARN_("\n"); - /* We simply pick the last neighbor we receiver sync information from */ - tsch_queue_update_time_source(&last_eb_nbr_addr); - tsch_join_priority = last_eb_nbr_jp + 1; - /* Try to get in sync ASAP */ - tsch_schedule_keepalive_immediately(); - return; - } + schedule_next_keepalive = !resynchronize(packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); } - tsch_schedule_keepalive(); + if(schedule_next_keepalive) { + tsch_schedule_keepalive(); + } } /*---------------------------------------------------------------------------*/ /* Prepare and send a keepalive message */ @@ -737,7 +761,7 @@ PT_THREAD(tsch_scan(struct pt *pt)) if(!is_packet_pending && NETSTACK_RADIO.receiving_packet()) { /* If we are currently receiving a packet, wait until end of reception */ t0 = RTIMER_NOW(); - BUSYWAIT_UNTIL_ABS((is_packet_pending = NETSTACK_RADIO.pending_packet()), t0, RTIMER_SECOND / 100); + RTIMER_BUSYWAIT_UNTIL_ABS((is_packet_pending = NETSTACK_RADIO.pending_packet()), t0, RTIMER_SECOND / 100); } if(is_packet_pending) { @@ -837,7 +861,12 @@ PROCESS_THREAD(tsch_send_eb_process, ev, data) while(1) { unsigned long delay; - if(tsch_is_associated && tsch_current_eb_period > 0) { + if(tsch_is_associated && tsch_current_eb_period > 0 +#ifdef TSCH_RPL_CHECK_DODAG_JOINED + /* Implementation section 6.3 of RFC 8180 */ + && TSCH_RPL_CHECK_DODAG_JOINED() +#endif /* TSCH_RPL_CHECK_DODAG_JOINED */ + ) { /* Enqueue EB only if there isn't already one in queue */ if(tsch_queue_packet_count(&tsch_eb_address) == 0) { uint8_t hdr_len = 0; @@ -948,8 +977,7 @@ tsch_init(void) return; } - /* Init the queuebuf and TSCH sub-modules */ - queuebuf_init(); + /* Init TSCH sub-modules */ tsch_reset(); tsch_queue_init(); tsch_schedule_init(); @@ -1128,13 +1156,21 @@ turn_off(void) return 1; } /*---------------------------------------------------------------------------*/ +static int +max_payload(void) +{ + /* Setup security... before. */ + return TSCH_PACKET_MAX_LEN - NETSTACK_FRAMER.length(); +} +/*---------------------------------------------------------------------------*/ const struct mac_driver tschmac_driver = { "TSCH", tsch_init, send_packet, packet_input, turn_on, - turn_off + turn_off, + max_payload, }; /*---------------------------------------------------------------------------*/ /** @} */ diff --git a/os/net/mac/tsch/tsch.h b/os/net/mac/tsch/tsch.h index 65a46521f..ba2926341 100644 --- a/os/net/mac/tsch/tsch.h +++ b/os/net/mac/tsch/tsch.h @@ -65,30 +65,12 @@ frequency hopping for enhanced reliability. #include "net/mac/tsch/tsch-rpl.h" #endif /* UIP_CONF_IPV6_RPL */ -#if CONTIKI_TARGET_COOJA -#include "lib/simEnvChange.h" -#include "sys/cooja_mt.h" -#endif /* CONTIKI_TARGET_COOJA */ /* Include Arch-Specific conf */ #ifdef TSCH_CONF_ARCH_HDR_PATH #include TSCH_CONF_ARCH_HDR_PATH #endif /* TSCH_CONF_ARCH_HDR_PATH */ -/*********** Macros *********/ - -/* Wait for a condition with timeout t0+offset. */ -#if CONTIKI_TARGET_COOJA -#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) { \ - simProcessRunValue = 1; \ - cooja_mt_yield(); \ - }; -#else -#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) ; -#endif /* CONTIKI_TARGET_COOJA */ - /*********** Callbacks *********/ /* Link callbacks to RPL in case RPL is enabled */ @@ -106,6 +88,10 @@ frequency hopping for enhanced reliability. #define TSCH_CALLBACK_KA_SENT tsch_rpl_callback_ka_sent #endif /* TSCH_CALLBACK_KA_SENT */ +#ifndef TSCH_RPL_CHECK_DODAG_JOINED +#define TSCH_RPL_CHECK_DODAG_JOINED tsch_rpl_check_dodag_joined +#endif /* TSCH_RPL_CHECK_DODAG_JOINED */ + #endif /* UIP_CONF_IPV6_RPL */ #if BUILD_WITH_ORCHESTRA @@ -135,6 +121,11 @@ void TSCH_CALLBACK_LEAVING_NETWORK(); void TSCH_CALLBACK_KA_SENT(); #endif +/* Called by TSCH before sending a EB */ +#ifdef TSCH_RPL_CHECK_DODAG_JOINED +int TSCH_RPL_CHECK_DODAG_JOINED(); +#endif + /* Called by TSCH form interrupt after receiving a frame, enabled upper-layer to decide * whether to ACK or NACK */ #ifdef TSCH_CALLBACK_DO_NACK @@ -176,7 +167,7 @@ extern uint8_t tsch_current_channel; extern uint8_t tsch_hopping_sequence[TSCH_HOPPING_SEQUENCE_MAX_LEN]; extern struct tsch_asn_divisor_t tsch_hopping_sequence_length; /* TSCH timeslot timing (in micro-second) */ -uint16_t tsch_timing_us[tsch_ts_elements_count]; +extern uint16_t tsch_timing_us[tsch_ts_elements_count]; /* TSCH timeslot timing (in rtimer ticks) */ extern rtimer_clock_t tsch_timing[tsch_ts_elements_count]; /* Statistics on the current session */ @@ -244,6 +235,12 @@ void tsch_schedule_keepalive(void); * Schedule a keep-alive immediately */ void tsch_schedule_keepalive_immediately(void); +/** + * Get the time, in clock ticks, since the TSCH network was started. + * + * \return The network uptime, or -1 if the node is not part of a TSCH network. + */ +uint64_t tsch_get_network_uptime_ticks(void); /** * Leave the TSCH network we are currently in */ diff --git a/os/net/packetbuf.h b/os/net/packetbuf.h index 20e071474..c077ae940 100644 --- a/os/net/packetbuf.h +++ b/os/net/packetbuf.h @@ -55,6 +55,7 @@ #include "contiki.h" #include "net/linkaddr.h" #include "net/mac/llsec802154.h" +#include "net/mac/csma/csma-security.h" #include "net/mac/tsch/tsch-conf.h" /** @@ -238,6 +239,11 @@ enum { PACKETBUF_ATTR_KEY_INDEX, #endif /* LLSEC802154_USES_EXPLICIT_KEYS */ +#if LLSEC802154_USES_FRAME_COUNTER + PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, + PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, +#endif /* LLSEC802154_USES_FRAME_COUNTER */ + /* Scope 2 attributes: used between end-to-end nodes. */ /* These must be last */ PACKETBUF_ADDR_SENDER, @@ -277,6 +283,8 @@ void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs, #define PACKETBUF_ATTR_BYTE 8 #define PACKETBUF_ADDRSIZE (LINKADDR_SIZE * PACKETBUF_ATTR_BYTE) +#define PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT 0xffff + struct packetbuf_attrlist { uint8_t type; uint8_t len; diff --git a/os/net/queuebuf.h b/os/net/queuebuf.h index 60216465d..ef66da780 100644 --- a/os/net/queuebuf.h +++ b/os/net/queuebuf.h @@ -55,6 +55,12 @@ #include "net/packetbuf.h" +#ifdef QUEUEBUF_CONF_ENABLED +#define QUEUEBUF_ENABLED QUEUEBUF_CONF_ENABLED +#else /* QUEUEBUF_CONF_ENABLED */ +#define QUEUEBUF_ENABLED 1 +#endif /* QUEUEBUF_CONF_ENABLED */ + /* QUEUEBUF_NUM is the total number of queuebuf */ #ifdef QUEUEBUF_CONF_NUM #define QUEUEBUF_NUM QUEUEBUF_CONF_NUM diff --git a/os/net/routing/nullrouting/nullrouting.c b/os/net/routing/nullrouting/nullrouting.c index bb1399fbb..318cd1baa 100644 --- a/os/net/routing/nullrouting/nullrouting.c +++ b/os/net/routing/nullrouting/nullrouting.c @@ -107,7 +107,7 @@ static void ext_header_remove(void) { #if NETSTACK_CONF_WITH_IPV6 - uip_ext_len = 0; + uip_remove_ext_hdr(); #endif /* NETSTACK_CONF_WITH_IPV6 */ } /*---------------------------------------------------------------------------*/ @@ -118,7 +118,7 @@ ext_header_update(void) } /*---------------------------------------------------------------------------*/ static int -ext_header_hbh_update(int uip_ext_opt_offset) +ext_header_hbh_update(uint8_t *ext_buf, int opt_offset) { return 1; } diff --git a/os/net/routing/routing.h b/os/net/routing/routing.h index 7fe6e7ca8..3cee3d9b2 100644 --- a/os/net/routing/routing.h +++ b/os/net/routing/routing.h @@ -57,7 +57,7 @@ struct routing_driver { /** * Set the prefix, for nodes that will operate as root * - * \param prefix The prefix. If NULL, UIP_DS6_DEFAULT_PREFIX is used instead + * \param prefix The prefix. If NULL, uip_ds6_default_prefix() is used instead * \param iid The IID. If NULL, it will be built from uip_ds6_set_addr_iid. */ void (* root_set_prefix)(uip_ipaddr_t *prefix, uip_ipaddr_t *iid); @@ -131,12 +131,13 @@ struct routing_driver { * Process and update the routing protocol hob-by-hop * extention headers of the current uIP packet. * - * \param uip_ext_opt_offset The offset within the uIP packet where - * extension headers start + * \param ext_buf A pointer to the ext header buffer + * \param opt_offset The offset within the extension header where + * the option starts * \return 1 in case the packet is valid and to be processed further, * 0 in case the packet must be dropped. */ - int (* ext_header_hbh_update)(int uip_ext_opt_offset); + int (* ext_header_hbh_update)(uint8_t *ext_buf, int opt_offset); /** * Process and update SRH in-place, * i.e. internal address swapping as per RFC6554 diff --git a/os/net/routing/rpl-classic/rpl-dag-root.c b/os/net/routing/rpl-classic/rpl-dag-root.c index 3a0162627..f45c2d317 100644 --- a/os/net/routing/rpl-classic/rpl-dag-root.c +++ b/os/net/routing/rpl-classic/rpl-dag-root.c @@ -48,12 +48,15 @@ static void set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid) { static uip_ipaddr_t root_ipaddr; + const uip_ipaddr_t *default_prefix; int i; + default_prefix = uip_ds6_default_prefix(); + /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ if(prefix == NULL) { - uip_ip6addr(&root_ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&root_ipaddr, default_prefix); } else { memcpy(&root_ipaddr, prefix, 8); } @@ -115,9 +118,11 @@ rpl_dag_root_start(void) if(root_if != NULL) { rpl_dag_t *dag; uip_ipaddr_t prefix; + const uip_ipaddr_t *default_prefix; rpl_set_root(RPL_DEFAULT_INSTANCE, ipaddr); dag = rpl_get_any_dag(); + default_prefix = uip_ds6_default_prefix(); /* If there are routes in this dag, we remove them all as we are from now on the new dag root and the old routes are wrong */ @@ -129,7 +134,7 @@ rpl_dag_root_start(void) dag->instance->def_route = NULL; } - uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&prefix, default_prefix); rpl_set_prefix(dag, &prefix, 64); LOG_INFO("root_set_prefix: created a new RPL dag\n"); return 0; diff --git a/os/net/routing/rpl-classic/rpl-dag.c b/os/net/routing/rpl-classic/rpl-dag.c index cbaee6959..83a85792b 100644 --- a/os/net/routing/rpl-classic/rpl-dag.c +++ b/os/net/routing/rpl-classic/rpl-dag.c @@ -126,7 +126,7 @@ rpl_get_nbr(rpl_parent_t *parent) { const linkaddr_t *lladdr = rpl_get_parent_lladdr(parent); if(lladdr != NULL) { - return nbr_table_get_from_lladdr(ds6_neighbors, lladdr); + return uip_ds6_nbr_ll_lookup((const uip_lladdr_t *)lladdr); } else { return NULL; } @@ -145,9 +145,9 @@ rpl_dag_init(void) } /*---------------------------------------------------------------------------*/ rpl_parent_t * -rpl_get_parent(uip_lladdr_t *addr) +rpl_get_parent(const uip_lladdr_t *addr) { - rpl_parent_t *p = nbr_table_get_from_lladdr(rpl_parents, (linkaddr_t *)addr); + rpl_parent_t *p = nbr_table_get_from_lladdr(rpl_parents, (const linkaddr_t *)addr); return p; } /*---------------------------------------------------------------------------*/ @@ -368,15 +368,16 @@ rpl_set_root(uint8_t instance_id, uip_ipaddr_t *dag_id) if(uip_ipaddr_cmp(&dag->dag_id, dag_id)) { version = dag->version; RPL_LOLLIPOP_INCREMENT(version); - } - if(dag == dag->instance->current_dag) { - LOG_INFO("Dropping a joined DAG when setting this node as root\n"); - rpl_set_default_route(instance, NULL); - dag->instance->current_dag = NULL; } else { - LOG_INFO("Dropping a DAG when setting this node as root\n"); + if(dag == dag->instance->current_dag) { + LOG_INFO("Dropping a joined DAG when setting this node as root\n"); + rpl_set_default_route(instance, NULL); + dag->instance->current_dag = NULL; + } else { + LOG_INFO("Dropping a DAG when setting this node as root\n"); + } + rpl_free_dag(dag); } - rpl_free_dag(dag); } } } diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 8488d5ac6..34d9b4907 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -60,46 +60,35 @@ #include #include -/*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) -#define UIP_RH_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_RPL_SRH_BUF ((struct uip_rpl_srh_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_RH_LEN]) -#define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_RPL_BUF ((struct uip_ext_hdr_opt_rpl *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) /*---------------------------------------------------------------------------*/ int -rpl_ext_header_hbh_update(int uip_ext_opt_offset) +rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset) { rpl_instance_t *instance; int down; uint16_t sender_rank; uint8_t sender_closer; uip_ds6_route_t *route; - rpl_parent_t *sender = NULL; + rpl_parent_t *sender; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)ext_buf; + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset); - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF->opt_type != UIP_EXT_HDR_OPT_RPL - || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF->len, - UIP_EXT_HDR_OPT_RPL_BUF->opt_type, - UIP_EXT_HDR_OPT_RPL_BUF->opt_len); + hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF->instance); + instance = rpl_get_instance(rpl_opt->instance); if(instance == NULL) { - LOG_ERR("Unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF->instance); + LOG_ERR("Unknown instance: %u\n", rpl_opt->instance); return 0; } - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_FWD_ERR) { + if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("Forward error!\n"); /* We should try to repair it by removing the neighbor that caused the packet to be forwareded in the first place. We drop any @@ -123,14 +112,14 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) return 0; } down = 0; - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_DOWN) { + if(rpl_opt->flags & RPL_HDR_OPT_DOWN) { down = 1; } - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF->senderrank); + sender_rank = UIP_HTONS(rpl_opt->senderrank); sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - if(sender != NULL && (UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_RANK_ERR)) { + if(sender != NULL && (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR)) { /* A rank error was signalled, attempt to repair it by updating * the sender's rank from ext header */ sender->rank = sender_rank; @@ -160,7 +149,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) instance->unicast_dio_target = sender; rpl_schedule_unicast_dio_immediately(instance); } - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_RANK_ERR) { + if(rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) { RPL_STAT(rpl_stats.loop_errors++); LOG_ERR(" Rank error signalled in RPL option!\n"); /* Packet must be dropped and dio trickle timer reset, see RFC6550 - 11.2.2.2 */ @@ -169,7 +158,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) } LOG_WARN("Single error tolerated\n"); RPL_STAT(rpl_stats.loop_warnings++); - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_RANK_ERR; + rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR; return 1; } @@ -181,41 +170,19 @@ int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) { #if RPL_WITH_NON_STORING - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; rpl_dag_t *dag; uip_sr_node_t *dest_node; uip_sr_node_t *root_node; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing - * header. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); dag = rpl_get_dag(&UIP_IP_BUF->destipaddr); root_node = uip_sr_get_node(dag, &dag->dag_id); dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr); - if((uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF->routing_type == RPL_RH_TYPE_SRH) || + if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) || (dest_node != NULL && root_node != NULL && dest_node->parent == root_node)) { /* Routing header found or the packet destined for a direct child of the root. @@ -224,11 +191,9 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) * forwarding to next hop */ uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr); uip_create_linklocal_prefix(ipaddr); - uip_ext_len = last_uip_ext_len; return 1; } - uip_ext_len = last_uip_ext_len; return 0; #else /* RPL_WITH_NON_STORING */ return 0; /* SRH not found */ @@ -239,34 +204,13 @@ int rpl_ext_header_srh_update(void) { #if RPL_WITH_NON_STORING - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; + struct uip_rpl_srh_hdr *srh_header; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing - * header. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } - - if(uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF->routing_type == RPL_RH_TYPE_SRH) { + if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) { /* SRH found, now look for next hop */ uint8_t cmpri, cmpre; uint8_t ext_len; @@ -275,11 +219,12 @@ rpl_ext_header_srh_update(void) uint8_t segments_left; uip_ipaddr_t current_dest_addr; - segments_left = UIP_RH_BUF->seg_left; - ext_len = (UIP_RH_BUF->len * 8) + 8; - cmpri = UIP_RPL_SRH_BUF->cmpr >> 4; - cmpre = UIP_RPL_SRH_BUF->cmpr & 0x0f; - padding = UIP_RPL_SRH_BUF->pad >> 4; + srh_header = (struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN); + segments_left = rh_header->seg_left; + ext_len = rh_header->len * 8 + 8; + cmpri = srh_header->cmpr >> 4; + cmpre = srh_header->cmpr & 0x0f; + padding = srh_header->pad >> 4; path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1; (void)path_len; @@ -290,7 +235,7 @@ rpl_ext_header_srh_update(void) /* We are the final destination, do nothing */ } else { uint8_t i = path_len - segments_left; /* The index of the next address to be visited */ - uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); + uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); uint8_t cmpr = segments_left == 1 ? cmpre : cmpri; /* As per RFC6554: swap the IPv6 destination address and address[i] */ @@ -303,17 +248,15 @@ rpl_ext_header_srh_update(void) memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr); /* Update segments left field */ - UIP_RH_BUF->seg_left--; + rh_header->seg_left--; LOG_INFO("SRH next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); } - uip_ext_len = last_uip_ext_len; return 1; } - uip_ext_len = last_uip_ext_len; return 0; #else /* RPL_WITH_NON_STORING */ return 0; /* SRH not found */ @@ -336,7 +279,6 @@ static int insert_srh_header(void) { /* Implementation of RFC6554 */ - uint8_t temp_len; uint8_t path_len; uint8_t ext_len; uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */ @@ -348,6 +290,10 @@ insert_srh_header(void) rpl_dag_t *dag; uip_ipaddr_t node_addr; + /* Always insest SRH as first extension header */ + struct uip_routing_hdr *rh_hdr = (struct uip_routing_hdr *)UIP_IP_PAYLOAD(0); + struct uip_rpl_srh_hdr *srh_hdr = (struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN); + LOG_INFO("SRH creating source routing header with destination "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); @@ -418,33 +364,33 @@ insert_srh_header(void) path_len, cmpri, cmpre, ext_len, padding); /* Check if there is enough space to store the extension header */ - if(uip_len + ext_len > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + ext_len > UIP_LINK_MTU) { LOG_ERR("Packet too long: impossible to add source routing header (%u bytes)\n", ext_len); return 0; } - /* Move existing ext headers and payload uip_ext_len further */ - memmove(uip_buf + uip_l2_l3_hdr_len + ext_len, - uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); - memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); + /* Move existing ext headers and payload ext_len further */ + memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len, + uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN); + memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len); - /* Insert source routing header */ - UIP_RH_BUF->next = UIP_IP_BUF->proto; + /* Insert source routing header (as first ext header) */ + rh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF->len = (ext_len - 8) / 8; - UIP_RH_BUF->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF->seg_left = path_len; + rh_hdr->len = (ext_len - 8) / 8; + rh_hdr->routing_type = RPL_RH_TYPE_SRH; + rh_hdr->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF->pad = padding << 4; + srh_hdr->cmpr = (cmpri << 4) + cmpre; + srh_hdr->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); @@ -459,15 +405,9 @@ insert_srh_header(void) NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr); - /* In-place update of IPv6 length field */ - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += ext_len; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } - - uip_ext_len += ext_len; - uip_len += ext_len; + /* Update the IPv6 length field */ + uipbuf_add_ext_hdr(ext_len); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); return 1; } @@ -476,44 +416,37 @@ static int update_hbh_header(void) { rpl_instance_t *instance; - int uip_ext_opt_offset; - int last_uip_ext_len; rpl_parent_t *parent; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(0) + 2); - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { - - LOG_ERR("Hop-by-hop extension header has wrong size (%u %u)\n", - UIP_EXT_HDR_OPT_RPL_BUF->opt_len, - uip_ext_len); + LOG_ERR("Hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF->instance); + instance = rpl_get_instance(rpl_opt->instance); if(instance == NULL || !instance->used || !instance->current_dag->joined) { LOG_ERR("Unable to add/update hop-by-hop extension header: incorrect instance\n"); - uip_ext_len = last_uip_ext_len; return 0; /* Drop */ } LOG_INFO("Updating RPL option\n"); /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = instance->instance_id; + rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank); + rpl_opt->instance = instance->instance_id; if(RPL_IS_STORING(instance)) { /* In non-storing mode, downwards traffic does not have the HBH option */ /* Check the direction of the down flag, as per Section 11.2.2.3, which states that if a packet is going down it should in general not go back up again. If this happens, a RPL_HDR_OPT_FWD_ERR should be flagged. */ - if((UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_DOWN)) { + if((rpl_opt->flags & RPL_HDR_OPT_DOWN)) { if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_FWD_ERR; + rpl_opt->flags |= RPL_HDR_OPT_FWD_ERR; LOG_WARN("RPL forwarding error\n"); /* We should send back the packet to the originating parent, but it is not feasible yet, so we send a No-Path DAO instead */ @@ -532,63 +465,51 @@ update_hbh_header(void) if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { /* No route was found, so this packet will go towards the RPL root. If so, we should not set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF->flags &= ~RPL_HDR_OPT_DOWN; + rpl_opt->flags &= ~RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going up\n"); } else { /* A DAO route was found so we set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_DOWN; + rpl_opt->flags |= RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going down\n"); } } } } - uip_ext_len = last_uip_ext_len; return 1; } /*---------------------------------------------------------------------------*/ static int insert_hbh_header(const rpl_instance_t *instance) { - int uip_ext_opt_offset; - int last_uip_ext_len; - uint8_t temp_len; - - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2)); /* Insert hop-by-hop header */ LOG_DBG("Creating hop-by-hop option\n"); - if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) { LOG_ERR("Packet too long: impossible to add hop-by-hop option\n"); - uip_ext_len = last_uip_ext_len; return 0; } - /* Move existing ext headers and payload UIP_EXT_BUF further */ - memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF, uip_len - UIP_IPH_LEN); - memset(UIP_HBHO_BUF, 0, RPL_HOP_BY_HOP_LEN); + /* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further */ + memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN); + memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); - /* Update IP and HBH protocol and fields */ - UIP_HBHO_BUF->next = UIP_IP_BUF->proto; + /* Insert HBH header (as first ext header) */ + hbh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = instance->instance_id; - uip_len += RPL_HOP_BY_HOP_LEN; - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } + hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL; + rpl_opt->opt_len = RPL_HDR_OPT_LEN; + rpl_opt->flags = 0; + rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank); + rpl_opt->instance = instance->instance_id; - uip_ext_len = last_uip_ext_len + RPL_HOP_BY_HOP_LEN; + uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); /* Update header before returning */ return update_hbh_header(); @@ -597,52 +518,33 @@ insert_hbh_header(const rpl_instance_t *instance) void rpl_ext_header_remove(void) { - uint8_t temp_len; - uint8_t rpl_ext_hdr_len; - int uip_ext_opt_offset; - uint8_t *uip_next_hdr; + uint8_t *prev_proto_ptr; + uint8_t protocol; + uint8_t ext_len; + uint8_t *next_header; + struct uip_ext_hdr *ext_ptr; + struct uip_ext_hdr_opt *opt_ptr; - uip_ext_len = 0; - uip_ext_opt_offset = 2; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for hop-by-hop and routing headers */ - while(uip_next_hdr != NULL) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_ROUTING: - if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL)) { - /* Remove hop-by-hop and routing headers */ - *uip_next_hdr = UIP_EXT_BUF->next; - rpl_ext_hdr_len = (UIP_EXT_BUF->len * 8) + 8; - temp_len = UIP_IP_BUF->len[1]; - uip_len -= rpl_ext_hdr_len; - UIP_IP_BUF->len[1] -= rpl_ext_hdr_len; - if(UIP_IP_BUF->len[1] > temp_len) { - UIP_IP_BUF->len[0]--; - } - LOG_DBG("Removing RPL extension header (type %u, len %u)\n", *uip_next_hdr, rpl_ext_hdr_len); - memmove(UIP_EXT_BUF, ((uint8_t *)UIP_EXT_BUF) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); - } else { - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - } - break; - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, any header other than the Destination - * Options header does not appear between the Hop-by-Hop - * Options header and the Routing header. - * - * We're moving to the next header only if uip_next_hdr has - * UIP_PROTO_DESTO. Otherwise, we'll return. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - default: - return; + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &UIP_IP_BUF->proto; + while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) { + opt_ptr = (struct uip_ext_hdr_opt *)(next_header + 2); + if(protocol == UIP_PROTO_ROUTING || (protocol == UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) { + /* Remove ext header */ + *prev_proto_ptr = ext_ptr->next; + ext_len = ext_ptr->len * 8 + 8; + uipbuf_add_ext_hdr(-ext_len); + /* Update length field and rest of packer to the "left" */ + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); + memmove(next_header, next_header + ext_len, uip_len - (next_header - uip_buf)); + /* Update loop variables */ + protocol = *prev_proto_ptr; + } else { + /* move to the ext hdr */ + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &ext_ptr->next; } } } diff --git a/os/net/routing/rpl-classic/rpl-icmp6.c b/os/net/routing/rpl-classic/rpl-icmp6.c index a246e576e..baa6b08a7 100644 --- a/os/net/routing/rpl-classic/rpl-icmp6.c +++ b/os/net/routing/rpl-classic/rpl-icmp6.c @@ -70,9 +70,6 @@ #define RPL_DIO_MOP_MASK 0x38 #define RPL_DIO_PREFERENCE_MASK 0x07 -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) /*---------------------------------------------------------------------------*/ static void dis_input(void); static void dio_input(void); @@ -251,7 +248,7 @@ dis_input(void) } } } - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -471,7 +468,7 @@ dio_input(void) rpl_process_dio(&from, &dio); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -810,7 +807,7 @@ dao_input_storing(void) /* independent if we remove or not - ACK the request */ if(flags & RPL_DAO_K_FLAG) { /* indicate that we accepted the no-path DAO */ - uip_clear_buf(); + uipbuf_clear(); dao_ack_output(instance, &dao_sender_addr, sequence, RPL_DAO_ACK_UNCONDITIONAL_ACCEPT); } @@ -900,7 +897,7 @@ fwd_dao: } if(should_ack) { LOG_DBG("Sending DAO ACK\n"); - uip_clear_buf(); + uipbuf_clear(); dao_ack_output(instance, &dao_sender_addr, sequence, RPL_DAO_ACK_UNCONDITIONAL_ACCEPT); } @@ -1014,7 +1011,7 @@ dao_input_nonstoring(void) if(flags & RPL_DAO_K_FLAG) { LOG_DBG("Sending DAO ACK\n"); - uip_clear_buf(); + uipbuf_clear(); dao_ack_output(instance, &dao_sender_addr, sequence, RPL_DAO_ACK_UNCONDITIONAL_ACCEPT); } @@ -1047,7 +1044,7 @@ dao_input(void) } discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ #if RPL_WITH_DAO_ACK @@ -1286,7 +1283,7 @@ dao_ack_input(void) instance = rpl_get_instance(instance_id); if(instance == NULL) { - uip_clear_buf(); + uipbuf_clear(); return; } @@ -1294,7 +1291,7 @@ dao_ack_input(void) parent = rpl_find_parent(instance->current_dag, &UIP_IP_BUF->srcipaddr); if(parent == NULL) { /* not a known instance - drop the packet and ignore */ - uip_clear_buf(); + uipbuf_clear(); return; } } else { @@ -1303,7 +1300,7 @@ dao_ack_input(void) if(instance->current_dag->rank == ROOT_RANK(instance)) { LOG_DBG("DODAG root received a DAO ACK, ignoring it\n"); - uip_clear_buf(); + uipbuf_clear(); return; } @@ -1363,7 +1360,7 @@ dao_ack_input(void) } } #endif /* RPL_WITH_DAO_ACK */ - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void diff --git a/os/net/routing/rpl-classic/rpl-nbr-policy.c b/os/net/routing/rpl-classic/rpl-nbr-policy.c index 344840d0e..d23111e46 100644 --- a/os/net/routing/rpl-classic/rpl-nbr-policy.c +++ b/os/net/routing/rpl-classic/rpl-nbr-policy.c @@ -65,12 +65,11 @@ * neighbors and are not only MAC neighbors. */ #define MAX_CHILDREN (NBR_TABLE_MAX_NEIGHBORS - 2) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) static int num_parents; /* any node that are possible parents */ static int num_children; /* all children that we have as nexthop */ static int num_free; -static linkaddr_t *worst_rank_nbr; /* the parent that has the worst rank */ +static const linkaddr_t *worst_rank_nbr; /* the parent that has the worst rank */ static rpl_rank_t worst_rank; /*---------------------------------------------------------------------------*/ #if LOG_DBG_ENABLED @@ -112,9 +111,9 @@ update_nbr(void) num_parents = 0; num_children = 0; - nbr = nbr_table_head(ds6_neighbors); + nbr = uip_ds6_nbr_head(); while(nbr != NULL) { - linkaddr_t *lladdr = nbr_table_get_lladdr(ds6_neighbors, nbr); + const linkaddr_t *lladdr = (const linkaddr_t *)uip_ds6_nbr_get_ll(nbr); is_used = 0; /* @@ -127,7 +126,7 @@ update_nbr(void) num_children++; } - parent = rpl_get_parent((uip_lladdr_t *)lladdr); + parent = rpl_get_parent((const uip_lladdr_t *)lladdr); if(parent != NULL) { num_parents++; @@ -159,7 +158,7 @@ update_nbr(void) LOG_DBG_("\n"); } - nbr = nbr_table_next(ds6_neighbors, nbr); + nbr = uip_ds6_nbr_next(nbr); num_used++; } /* how many more IP neighbors can be have? */ diff --git a/os/net/routing/rpl-classic/rpl.h b/os/net/routing/rpl-classic/rpl.h index db87cc824..e690abeae 100644 --- a/os/net/routing/rpl-classic/rpl.h +++ b/os/net/routing/rpl-classic/rpl.h @@ -275,7 +275,7 @@ rpl_dag_t *rpl_get_dag(const uip_ipaddr_t *addr); rpl_dag_t *rpl_get_any_dag(void); rpl_instance_t *rpl_get_instance(uint8_t instance_id); int rpl_ext_header_update(void); -int rpl_ext_header_hbh_update(int); +int rpl_ext_header_hbh_update(uint8_t *, int); void rpl_insert_header(void); void rpl_ext_header_remove(void); const struct link_stats *rpl_get_parent_link_stats(rpl_parent_t *p); @@ -285,7 +285,7 @@ uint16_t rpl_get_parent_link_metric(rpl_parent_t *p); rpl_rank_t rpl_rank_via_parent(rpl_parent_t *p); const linkaddr_t *rpl_get_parent_lladdr(rpl_parent_t *p); uip_ipaddr_t *rpl_parent_get_ipaddr(rpl_parent_t *nbr); -rpl_parent_t *rpl_get_parent(uip_lladdr_t *addr); +rpl_parent_t *rpl_get_parent(const uip_lladdr_t *addr); rpl_rank_t rpl_get_parent_rank(uip_lladdr_t *addr); void rpl_dag_init(void); uip_ds6_nbr_t *rpl_get_nbr(rpl_parent_t *parent); diff --git a/os/net/routing/rpl-lite/rpl-dag-root.c b/os/net/routing/rpl-lite/rpl-dag-root.c index c3e356828..38f5697e7 100644 --- a/os/net/routing/rpl-lite/rpl-dag-root.c +++ b/os/net/routing/rpl-lite/rpl-dag-root.c @@ -76,13 +76,16 @@ static void set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid) { static uip_ipaddr_t root_ipaddr; + const uip_ipaddr_t *default_prefix; int i; uint8_t state; + default_prefix = uip_ds6_default_prefix(); + /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ if(prefix == NULL) { - uip_ip6addr(&root_ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&root_ipaddr, default_prefix); } else { memcpy(&root_ipaddr, prefix, 8); } diff --git a/os/net/routing/rpl-lite/rpl-dag-root.h b/os/net/routing/rpl-lite/rpl-dag-root.h index 71b999765..f035e8595 100644 --- a/os/net/routing/rpl-lite/rpl-dag-root.h +++ b/os/net/routing/rpl-lite/rpl-dag-root.h @@ -45,7 +45,7 @@ /** * Set a prefix in case the node is later set as dag root. * - * \param prefix The prefix. If NULL, UIP_DS6_DEFAULT_PREFIX is used instead + * \param prefix The prefix. If NULL, uip_ds6_default_prefix() is used instead * \param iid The IID. If NULL, it will be built from uip_ds6_set_addr_iid. */ void rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid); diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index af67cdaff..cb8dcc190 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -53,46 +53,16 @@ #define LOG_MODULE "RPL" #define LOG_LEVEL LOG_LEVEL_RPL -/*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) -#define UIP_RH_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_RPL_SRH_BUF ((struct uip_rpl_srh_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_RH_LEN]) -#define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_RPL_BUF ((struct uip_ext_hdr_opt_rpl *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) - /*---------------------------------------------------------------------------*/ int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) { - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; uip_sr_node_t *dest_node; uip_sr_node_t *root_node; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing header. - */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); if(!rpl_is_addr_in_our_dag(&UIP_IP_BUF->destipaddr)) { return 0; @@ -101,8 +71,7 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) root_node = uip_sr_get_node(NULL, &curr_instance.dag.dag_id); dest_node = uip_sr_get_node(NULL, &UIP_IP_BUF->destipaddr); - if((uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF->routing_type == RPL_RH_TYPE_SRH) || + if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) || (dest_node != NULL && root_node != NULL && dest_node->parent == root_node)) { /* Routing header found or the packet destined for a direct child of the root. @@ -111,20 +80,18 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) * forwarding to next hop */ uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr); uip_create_linklocal_prefix(ipaddr); - uip_ext_len = last_uip_ext_len; return 1; } LOG_DBG("no SRH found\n"); - uip_ext_len = last_uip_ext_len; return 0; } /*---------------------------------------------------------------------------*/ int rpl_ext_header_srh_update(void) { - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; + struct uip_rpl_srh_hdr *srh_header; uint8_t cmpri, cmpre; uint8_t ext_len; uint8_t padding; @@ -132,40 +99,21 @@ rpl_ext_header_srh_update(void) uint8_t segments_left; uip_ipaddr_t current_dest_addr; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing header. - */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } - - if(uip_next_hdr == NULL || *uip_next_hdr != UIP_PROTO_ROUTING - || UIP_RH_BUF->routing_type != RPL_RH_TYPE_SRH) { + if(rh_header == NULL || rh_header->routing_type != RPL_RH_TYPE_SRH) { LOG_INFO("SRH not found\n"); - uip_ext_len = last_uip_ext_len; return 0; } /* Parse SRH */ - segments_left = UIP_RH_BUF->seg_left; - ext_len = (UIP_RH_BUF->len * 8) + 8; - cmpri = UIP_RPL_SRH_BUF->cmpr >> 4; - cmpre = UIP_RPL_SRH_BUF->cmpr & 0x0f; - padding = UIP_RPL_SRH_BUF->pad >> 4; + srh_header = (struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN); + segments_left = rh_header->seg_left; + ext_len = rh_header->len * 8 + 8; + cmpri = srh_header->cmpr >> 4; + cmpre = srh_header->cmpr & 0x0f; + padding = srh_header->pad >> 4; path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1; (void)path_len; @@ -177,7 +125,7 @@ rpl_ext_header_srh_update(void) /* We are the final destination, do nothing */ } else { uint8_t i = path_len - segments_left; /* The index of the next address to be visited */ - uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); + uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); uint8_t cmpr = segments_left == 1 ? cmpre : cmpri; /* As per RFC6554: swap the IPv6 destination address with address[i] */ @@ -190,14 +138,13 @@ rpl_ext_header_srh_update(void) memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr); /* Update segments left field */ - UIP_RH_BUF->seg_left--; + rh_header->seg_left--; LOG_INFO("SRH next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); } - uip_ext_len = last_uip_ext_len; return 1; } /*---------------------------------------------------------------------------*/ @@ -223,7 +170,6 @@ static int insert_srh_header(void) { /* Implementation of RFC6554 */ - uint8_t temp_len; uint8_t path_len; uint8_t ext_len; uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */ @@ -234,6 +180,10 @@ insert_srh_header(void) uip_sr_node_t *node; uip_ipaddr_t node_addr; + /* Always insest SRH as first extension header */ + struct uip_routing_hdr *rh_hdr = (struct uip_routing_hdr *)UIP_IP_PAYLOAD(0); + struct uip_rpl_srh_hdr *srh_hdr = (struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN); + LOG_INFO("SRH creating source routing header with destination "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_(" \n"); @@ -304,33 +254,33 @@ insert_srh_header(void) path_len, cmpri, cmpre, ext_len, padding); /* Check if there is enough space to store the extension header */ - if(uip_len + ext_len > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + ext_len > UIP_LINK_MTU) { LOG_ERR("packet too long: impossible to add source routing header (%u bytes)\n", ext_len); return 0; } - /* Move existing ext headers and payload uip_ext_len further */ - memmove(uip_buf + uip_l2_l3_hdr_len + ext_len, - uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); - memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); + /* Move existing ext headers and payload ext_len further */ + memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len, + uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN); + memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len); - /* Insert source routing header */ - UIP_RH_BUF->next = UIP_IP_BUF->proto; + /* Insert source routing header (as first ext header) */ + rh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF->len = (ext_len - 8) / 8; - UIP_RH_BUF->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF->seg_left = path_len; + rh_hdr->len = (ext_len - 8) / 8; + rh_hdr->routing_type = RPL_RH_TYPE_SRH; + rh_hdr->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF->pad = padding << 4; + srh_hdr->cmpr = (cmpri << 4) + cmpre; + srh_hdr->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); @@ -345,21 +295,15 @@ insert_srh_header(void) NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr); - /* In-place update of IPv6 length field */ - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += ext_len; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } - - uip_ext_len += ext_len; - uip_len += ext_len; + /* Update the IPv6 length field */ + uipbuf_add_ext_hdr(ext_len); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); return 1; } /*---------------------------------------------------------------------------*/ int -rpl_ext_header_hbh_update(int uip_ext_opt_offset) +rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset) { int down; int rank_error_signaled; @@ -367,32 +311,31 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) uint16_t sender_rank; uint8_t sender_closer; rpl_nbr_t *sender; - uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF->opt_type; - uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF->opt_len; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)ext_buf; + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset); - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || opt_type != UIP_EXT_HDR_OPT_RPL - || opt_len != RPL_HDR_OPT_LEN) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF->len, opt_type, opt_len); + hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF->instance) { - LOG_ERR("unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF->instance); + if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) { + LOG_ERR("unknown instance: %u\n", rpl_opt->instance); return 0; /* Drop */ } - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_FWD_ERR) { + if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("forward error!\n"); return 0; /* Drop */ } - down = (UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF->senderrank); + down = (rpl_opt->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; + sender_rank = UIP_HTONS(rpl_opt->senderrank); sender = nbr_table_get_from_lladdr(rpl_neighbors, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - rank_error_signaled = (UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; + rank_error_signaled = (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; sender_closer = sender_rank < curr_instance.dag.rank; loop_detected = (down && !sender_closer) || (!down && sender_closer); @@ -406,7 +349,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) if(loop_detected) { /* Set forward error flag */ - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_RANK_ERR; + rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR; } return rpl_process_hbh(sender, sender_rank, loop_detected, rank_error_signaled); @@ -418,34 +361,27 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) static int update_hbh_header(void) { - int uip_ext_opt_offset; - int last_uip_ext_len; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2)); - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { - - LOG_ERR("hop-by-hop extension header has wrong size (%u %u)\n", - UIP_EXT_HDR_OPT_RPL_BUF->opt_len, uip_ext_len); + LOG_ERR("hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF->instance) { + if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) { LOG_ERR("unable to add/update hop-by-hop extension header: incorrect instance\n"); - uip_ext_len = last_uip_ext_len; return 0; /* Drop */ } /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = curr_instance.instance_id; + rpl_opt->senderrank = UIP_HTONS(curr_instance.dag.rank); + rpl_opt->instance = curr_instance.instance_id; } - uip_ext_len = last_uip_ext_len; return 1; } /*---------------------------------------------------------------------------*/ @@ -456,45 +392,34 @@ update_hbh_header(void) static int insert_hbh_header(void) { - int uip_ext_opt_offset; - int last_uip_ext_len; - uint8_t temp_len; - - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2)); /* Insert hop-by-hop header */ LOG_INFO("creating hop-by-hop option\n"); - if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) { LOG_ERR("packet too long: impossible to add hop-by-hop option\n"); - uip_ext_len = last_uip_ext_len; return 0; } - /* Move existing ext headers and payload UIP_EXT_BUF further */ - memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF, uip_len - UIP_IPH_LEN); - memset(UIP_HBHO_BUF, 0, RPL_HOP_BY_HOP_LEN); + /* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further */ + memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN); + memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); - /* Update IP and HBH protocol and fields */ - UIP_HBHO_BUF->next = UIP_IP_BUF->proto; + /* Insert HBH header (as first ext header) */ + hbh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = curr_instance.instance_id; - uip_len += RPL_HOP_BY_HOP_LEN; - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } + hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL; + rpl_opt->opt_len = RPL_HDR_OPT_LEN; + rpl_opt->flags = 0; + rpl_opt->senderrank = UIP_HTONS(curr_instance.dag.rank); + rpl_opt->instance = curr_instance.instance_id; - uip_ext_len = last_uip_ext_len + RPL_HOP_BY_HOP_LEN; + uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); /* Update header before returning */ return update_hbh_header(); @@ -531,54 +456,34 @@ rpl_ext_header_update(void) void rpl_ext_header_remove(void) { - uint8_t temp_len; - uint8_t rpl_ext_hdr_len; - int uip_ext_opt_offset; - uint8_t *uip_next_hdr; + uint8_t *prev_proto_ptr; + uint8_t protocol; + uint8_t ext_len; + uint8_t *next_header; + struct uip_ext_hdr *ext_ptr; + struct uip_ext_hdr_opt *opt_ptr; - uip_ext_len = 0; - uip_ext_opt_offset = 2; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for hop-by-hop and routing headers */ - while(uip_next_hdr != NULL) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_ROUTING: - if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL)) { - /* Remove hop-by-hop and routing headers */ - *uip_next_hdr = UIP_EXT_BUF->next; - rpl_ext_hdr_len = (UIP_EXT_BUF->len * 8) + 8; - temp_len = UIP_IP_BUF->len[1]; - uip_len -= rpl_ext_hdr_len; - UIP_IP_BUF->len[1] -= rpl_ext_hdr_len; - if(UIP_IP_BUF->len[1] > temp_len) { - UIP_IP_BUF->len[0]--; - } - LOG_INFO("removing RPL extension header (type %u, len %u)\n", *uip_next_hdr, rpl_ext_hdr_len); - memmove(UIP_EXT_BUF, ((uint8_t *)UIP_EXT_BUF) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); - } else { - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - } - break; - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, any header other than the Destination - * Options header does not appear between the Hop-by-Hop - * Options header and the Routing header. - * - * We're moving to the next header only if uip_next_hdr has - * UIP_PROTO_DESTO. Otherwise, we'll return. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; - break; - default: - return; + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &UIP_IP_BUF->proto; + while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) { + opt_ptr = (struct uip_ext_hdr_opt *)(next_header + 2); + if(protocol == UIP_PROTO_ROUTING || (protocol == UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) { + /* Remove ext header */ + *prev_proto_ptr = ext_ptr->next; + ext_len = ext_ptr->len * 8 + 8; + uipbuf_add_ext_hdr(-ext_len); + /* Update length field and move rest of packet to the "left" */ + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); + memmove(next_header, next_header + ext_len, uip_len - (next_header - uip_buf)); + /* Update loop variables */ + protocol = *prev_proto_ptr; + } else { + /* move to the ext hdr */ + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &ext_ptr->next; } } } - /** @}*/ diff --git a/os/net/routing/rpl-lite/rpl-ext-header.h b/os/net/routing/rpl-lite/rpl-ext-header.h index df61bc325..c2d0db5bc 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.h +++ b/os/net/routing/rpl-lite/rpl-ext-header.h @@ -63,12 +63,13 @@ int rpl_ext_header_srh_update(void); * Process and update the RPL hop-by-hop extension headers of * the current uIP packet. * -* \param uip_ext_opt_offset The offset within the uIP packet where -* extension headers start +* \param ext_buf A pointer to the ext header buffer +* \param opt_offset The offset within the extension header where +* the option starts * \return 1 in case the packet is valid and to be processed further, * 0 in case the packet must be dropped. */ -int rpl_ext_header_hbh_update(int uip_ext_opt_offset); +int rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset); /** * Adds/updates all RPL extension headers to current uIP packet. diff --git a/os/net/routing/rpl-lite/rpl-icmp6.c b/os/net/routing/rpl-lite/rpl-icmp6.c index 8cf4c853c..335c640d9 100644 --- a/os/net/routing/rpl-lite/rpl-icmp6.c +++ b/os/net/routing/rpl-lite/rpl-icmp6.c @@ -62,10 +62,6 @@ #define RPL_DIO_MOP_MASK 0x38 #define RPL_DIO_PREFERENCE_MASK 0x07 -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) - /*---------------------------------------------------------------------------*/ static void dis_input(void); static void dio_input(void); @@ -147,7 +143,7 @@ dis_input(void) rpl_process_dis(&UIP_IP_BUF->srcipaddr, uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -329,7 +325,7 @@ dio_input(void) rpl_process_dio(&from, &dio); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -540,7 +536,7 @@ dao_input(void) rpl_process_dao(&from, &dao); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -651,7 +647,7 @@ dao_ack_input(void) rpl_process_dao_ack(sequence, status); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void diff --git a/os/net/routing/rpl-lite/rpl-nbr-policy.c b/os/net/routing/rpl-lite/rpl-nbr-policy.c index 9752b1446..188ceb648 100644 --- a/os/net/routing/rpl-lite/rpl-nbr-policy.c +++ b/os/net/routing/rpl-lite/rpl-nbr-policy.c @@ -59,11 +59,10 @@ * NOTE: this policy assumes that all neighbors end up being IPv6 * neighbors and are not only MAC neighbors. */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) static int num_parents; /* all nodes that are possible parents */ static int num_free; -static linkaddr_t *worst_rank_nbr_lladdr; /* lladdr of the the neighbor with the worst rank */ +static const linkaddr_t *worst_rank_nbr_lladdr; /* lladdr of the the neighbor with the worst rank */ static rpl_rank_t worst_rank; /*---------------------------------------------------------------------------*/ @@ -79,10 +78,10 @@ update_state(void) worst_rank_nbr_lladdr = NULL; num_parents = 0; - ds6_nbr = nbr_table_head(ds6_neighbors); + ds6_nbr = uip_ds6_nbr_head(); while(ds6_nbr != NULL) { - linkaddr_t *nbr_lladdr = nbr_table_get_lladdr(ds6_neighbors, ds6_nbr); + const linkaddr_t *nbr_lladdr = (const linkaddr_t *)uip_ds6_nbr_get_ll(ds6_nbr); rpl_nbr = rpl_neighbor_get_from_lladdr((uip_lladdr_t *)nbr_lladdr); if(rpl_nbr != NULL && rpl_neighbor_is_parent(rpl_nbr)) { @@ -98,7 +97,7 @@ update_state(void) worst_rank_nbr_lladdr = nbr_lladdr; } - ds6_nbr = nbr_table_next(ds6_neighbors, ds6_nbr); + ds6_nbr = uip_ds6_nbr_next(ds6_nbr); num_used++; } /* how many more IP neighbors can be have? */ diff --git a/os/net/routing/rpl-lite/rpl-neighbor.c b/os/net/routing/rpl-lite/rpl-neighbor.c index a0f5d95b0..94a26fe75 100644 --- a/os/net/routing/rpl-lite/rpl-neighbor.c +++ b/os/net/routing/rpl-lite/rpl-neighbor.c @@ -181,9 +181,9 @@ rpl_neighbor_count(void) static uip_ds6_nbr_t * rpl_get_ds6_nbr(rpl_nbr_t *nbr) { - const linkaddr_t *lladdr = rpl_neighbor_get_lladdr(nbr); + const uip_lladdr_t *lladdr = (const uip_lladdr_t *)rpl_neighbor_get_lladdr(nbr); if(lladdr != NULL) { - return nbr_table_get_from_lladdr(ds6_neighbors, lladdr); + return uip_ds6_nbr_ll_lookup(lladdr); } else { return NULL; } diff --git a/os/services/ip64/ip64-arp.c b/os/services/ip64/ip64-arp.c index 75197558d..6fd996920 100644 --- a/os/services/ip64/ip64-arp.c +++ b/os/services/ip64/ip64-arp.c @@ -139,7 +139,7 @@ ip64_arp_timer(void) { struct arp_entry *tabptr; int i; - + ++arptime; for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; @@ -157,7 +157,7 @@ arp_update(uip_ip4addr_t *ipaddr, struct uip_eth_addr *ethaddr) { register struct arp_entry *tabptr = arp_table; int i, c; - + /* Walk through the ARP mapping table and try to find an entry to update. If none is found, the IP -> MAC address mapping is inserted in the ARP table. */ @@ -170,7 +170,7 @@ arp_update(uip_ip4addr_t *ipaddr, struct uip_eth_addr *ethaddr) /* Check if the source IP address of the incoming packet matches the IP address in this ARP table entry. */ if(uip_ip4addr_cmp(ipaddr, &tabptr->ipaddr)) { - + /* An old entry found, update this and return. */ memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); tabptr->time = arptime; @@ -239,7 +239,7 @@ ip64_arp_arp_input(const uint8_t *packet, uint16_t packet_len) table, since it is likely that we will do more communication with this host in the future. */ arp_update(&arphdr->sipaddr, &arphdr->shwaddr); - + arphdr->opcode = UIP_HTONS(ARP_REPLY); memcpy(arphdr->dhwaddr.addr, arphdr->shwaddr.addr, 6); @@ -275,7 +275,7 @@ ip64_arp_check_cache(const uint8_t *nlhdr) printf("check cache %d.%d.%d.%d\n", uip_ipaddr_to_quad(&ipv4_hdr->destipaddr)); - + /* First check if destination is a local broadcast. */ uip_ipaddr(&broadcast_addr, 255,255,255,255); if(uip_ip4addr_cmp(&ipv4_hdr->destipaddr, &broadcast_addr)) { @@ -321,7 +321,7 @@ ip64_arp_create_ethhdr(uint8_t *llhdr, const uint8_t *nlhdr) struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)nlhdr; struct ip64_eth_hdr *ethhdr = (struct ip64_eth_hdr *)llhdr; uip_ip4addr_t broadcast_addr; - + /* Find the destination IP address in the ARP table and construct the Ethernet header. If the destination IP addres isn't on the local network, we use the default router's IP address instead. @@ -371,7 +371,7 @@ ip64_arp_create_ethhdr(uint8_t *llhdr, const uint8_t *nlhdr) } memcpy(ethhdr->src.addr, ip64_eth_addr.addr, 6); - + ethhdr->type = UIP_HTONS(IP64_ETH_TYPE_IP); return sizeof(struct ip64_eth_hdr); } @@ -382,7 +382,7 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)nlhdr; struct arp_hdr *arp_hdr = (struct arp_hdr *)llhdr; uip_ip4addr_t ipaddr; - + if(!uip_ipaddr_maskcmp(&ipv4_hdr->destipaddr, ip64_get_hostaddr(), ip64_get_netmask())) { @@ -394,7 +394,7 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) /* Else, we use the destination IP address. */ uip_ip4addr_copy(&ipaddr, &ipv4_hdr->destipaddr); } - + memset(arp_hdr->ethhdr.dest.addr, 0xff, 6); memset(arp_hdr->dhwaddr.addr, 0x00, 6); memcpy(arp_hdr->ethhdr.src.addr, ip64_eth_addr.addr, 6); @@ -408,8 +408,8 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) arp_hdr->hwlen = 6; arp_hdr->protolen = 4; arp_hdr->ethhdr.type = UIP_HTONS(IP64_ETH_TYPE_ARP); - - uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN]; + + uip_appdata = &uip_buf[UIP_IPTCPH_LEN]; return sizeof(struct arp_hdr); } diff --git a/os/services/ip64/ip64-dhcpc.c b/os/services/ip64/ip64-dhcpc.c index 8e8e286f8..10a5184e3 100644 --- a/os/services/ip64/ip64-dhcpc.c +++ b/os/services/ip64/ip64-dhcpc.c @@ -61,7 +61,7 @@ struct dhcp_msg { uint8_t options[312]; }; -#if (UIP_BUFSIZE - UIP_LLH_LEN - UIP_UDPIP_HLEN) < 548 +#if (UIP_BUFSIZE - UIP_UDPIP_HLEN) < 548 #error UIP_CONF_BUFFER_SIZE may be too small to accomodate DHCPv4 packets #error Increase UIP_CONF_BUFFER_SIZE in your project-conf.h, or contiki-conf.h #error A good size is 600 bytes @@ -191,12 +191,12 @@ send_request(void) struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; create_msg(m); - + end = add_msg_type(&m->options[4], DHCPREQUEST); end = add_server_id(end); end = add_req_ipaddr(end); end = add_end(end); - + uip_send(uip_appdata, (int)(end - (uint8_t *)uip_appdata)); } /*---------------------------------------------------------------------------*/ @@ -239,7 +239,7 @@ static uint8_t parse_msg(void) { struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; - + if(m->op == DHCP_REPLY && memcmp(m->xid, &xid, sizeof(xid)) == 0 && memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { @@ -258,7 +258,7 @@ msg_for_me(void) struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; uint8_t *optptr = &m->options[4]; uint8_t *end = (uint8_t*)uip_appdata + uip_datalen(); - + if(m->op == DHCP_REPLY && memcmp(m->xid, &xid, sizeof(xid)) == 0 && memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { @@ -280,7 +280,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) clock_time_t ticks; PT_BEGIN(&s.pt); - + init: xid++; s.state = STATE_SENDING; @@ -305,7 +305,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) s.ticks *= 2; } } - + selecting: s.ticks = CLOCK_SECOND; do { @@ -330,7 +330,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) goto init; } } while(s.state != STATE_CONFIG_RECEIVED); - + bound: #if 0 printf("Got IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.ipaddr)); @@ -343,7 +343,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) #endif ip64_dhcpc_configured(&s); - + #define MAX_TICKS (~((clock_time_t)0) / 2) #define MAX_TICKS32 (~((uint32_t)0)) #define IMIN(a, b) ((a) < (b) ? (a) : (b)) @@ -407,12 +407,12 @@ ip64_dhcpc_init(const void *mac_addr, int mac_len) uip_ip6addr_t v6addr; uip_ip4addr_t v4addr; struct uip_udp_conn *conn2; - + s.mac_addr = mac_addr; s.mac_len = mac_len; s.state = STATE_INITIAL; - uip_ipaddr(&v4addr, 255,255,255,255); + uip_ipaddr(&v4addr, 255,255,255,255); ip64_addr_4to6(&v4addr, &v6addr); s.conn = udp_new(&v6addr, UIP_HTONS(IP64_DHCPC_SERVER_PORT), NULL); conn2 = udp_new(NULL, UIP_HTONS(IP64_DHCPC_SERVER_PORT), NULL); @@ -437,7 +437,7 @@ void ip64_dhcpc_request(void) { uip_ipaddr_t ipaddr; - + if(s.state == STATE_INITIAL) { uip_ipaddr(&ipaddr, 0,0,0,0); uip_sethostaddr(&ipaddr); diff --git a/os/services/ip64/ip64-eth-interface.c b/os/services/ip64/ip64-eth-interface.c index 1dcaeb5ad..dc89610e5 100644 --- a/os/services/ip64/ip64-eth-interface.c +++ b/os/services/ip64/ip64-eth-interface.c @@ -39,8 +39,6 @@ #include -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #define DEBUG DEBUG_NONE #include "net/ipv6/uip-debug.h" #define printf(...) @@ -62,7 +60,7 @@ ip64_eth_interface_input(uint8_t *packet, uint16_t len) printf("-------------->\n"); uip_len = ip64_4to6(&packet[sizeof(struct ip64_eth_hdr)], len - sizeof(struct ip64_eth_hdr), - &uip_buf[UIP_LLH_LEN]); + uip_buf); if(uip_len > 0) { printf("ip64_interface_process: converted %d bytes\n", uip_len); @@ -96,7 +94,7 @@ output(void) PRINTF("\n"); printf("<--------------\n"); - len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len, + len = ip64_6to4(uip_buf, uip_len, &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]); printf("ip64-interface: output len %d\n", len); diff --git a/os/services/ip64/ip64-slip-interface.c b/os/services/ip64/ip64-slip-interface.c index 10422f2c7..fb4f69627 100644 --- a/os/services/ip64/ip64-slip-interface.c +++ b/os/services/ip64/ip64-slip-interface.c @@ -38,8 +38,6 @@ #include #include -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #define DEBUG DEBUG_NONE #include "net/ipv6/uip-debug.h" @@ -57,24 +55,24 @@ static void input_callback(void) { /*PRINTF("SIN: %u\n", uip_len);*/ - if(uip_buf[UIP_LLH_LEN] == '!') { - PRINTF("Got configuration message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); - uip_clear_buf(); + if(uip_buf[0] == '!') { + PRINTF("Got configuration message of type %c\n", uip_buf[1]); + uipbuf_clear(); #if 0 - if(uip_buf[UIP_LLH_LEN + 1] == 'P') { + if(uip_buf[1] == 'P') { uip_ipaddr_t prefix; /* Here we set a prefix !!! */ memset(&prefix, 0, 16); - memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8); + memcpy(&prefix, &uip_buf[2], 8); PRINTF("Setting prefix "); PRINT6ADDR(&prefix); PRINTF("\n"); set_prefix_64(&prefix); } #endif - } else if(uip_buf[UIP_LLH_LEN] == '?') { - PRINTF("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); - if(uip_buf[UIP_LLH_LEN + 1] == 'M') { + } else if(uip_buf[0] == '?') { + PRINTF("Got request message of type %c\n", uip_buf[1]); + if(uip_buf[1] == 'M') { const char *hexchar = "0123456789abcdef"; int j; /* this is just a test so far... just to see if it works */ @@ -86,21 +84,21 @@ input_callback(void) uip_len = 18; slip_write(uip_buf, uip_len); } - uip_clear_buf(); + uipbuf_clear(); } else { - + /* Save the last sender received over SLIP to avoid bouncing the packet back if no route is found */ uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr); - - uint16_t len = ip64_4to6(&uip_buf[UIP_LLH_LEN], uip_len, + + uint16_t len = ip64_4to6(uip_buf, uip_len, ip64_packet_buffer); if(len > 0) { - memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len); + memcpy(uip_buf, ip64_packet_buffer, len); uip_len = len; /* PRINTF("send len %d\n", len); */ } else { - uip_clear_buf(); + uipbuf_clear(); } } } @@ -129,11 +127,11 @@ output(void) if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) { PRINTF("ip64-interface: output, not sending bounced message\n"); } else { - len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len, + len = ip64_6to4(uip_buf, uip_len, ip64_packet_buffer); PRINTF("ip64-interface: output len %d\n", len); if(len > 0) { - memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len); + memcpy(uip_buf, ip64_packet_buffer, len); uip_len = len; slip_send(); return len; diff --git a/os/services/lwm2m/lwm2m-rd-client.c b/os/services/lwm2m/lwm2m-rd-client.c index 4b4b11528..6bb58f91a 100644 --- a/os/services/lwm2m/lwm2m-rd-client.c +++ b/os/services/lwm2m/lwm2m-rd-client.c @@ -54,6 +54,7 @@ #include "coap-endpoint.h" #include "coap-callback-api.h" #include "lwm2m-security.h" +#include "lib/list.h" #include #include #include @@ -76,18 +77,11 @@ #define LWM2M_DEFAULT_CLIENT_LIFETIME 30 /* sec */ #endif -#define MAX_RD_UPDATE_WAIT 5000 - #define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT) #define BS_REMOTE_PORT UIP_HTONS(5685) #define STATE_MACHINE_UPDATE_INTERVAL 500 -static struct lwm2m_session_info session_info; -static coap_callback_request_state_t rd_request_state; - -static coap_message_t request[1]; /* This way the message can be treated as pointer as usual. */ - /* The states for the RD client state machine */ /* When node is unregistered it ends up in UNREGISTERED and this is going to be there until use X or Y kicks it @@ -114,38 +108,32 @@ static coap_message_t request[1]; /* This way the message can be treated as #define FLAG_RD_DATA_UPDATE_TRIGGERED 0x02 #define FLAG_RD_DATA_UPDATE_ON_DIRTY 0x10 -static uint8_t rd_state = 0; -static uint8_t rd_flags = FLAG_RD_DATA_UPDATE_ON_DIRTY; -static uint64_t wait_until_network_check = 0; -static uint64_t last_update; +LIST(session_info_list); +/* Shared by all sessions, used by only one at a time in the FSM */ static char query_data[64]; /* allocate some data for queries and updates */ static uint8_t rd_data[128]; /* allocate some data for the RD */ -static uint32_t rd_block1; -static uint8_t rd_more; -static coap_timer_t rd_timer; -static void (*rd_callback)(coap_callback_request_state_t *callback_state); - -static coap_timer_t block1_timer; +static coap_timer_t rd_timer; /* Timer to tick the FSM periodically */ +static char default_ep[20]; #if LWM2M_QUEUE_MODE_ENABLED -static coap_timer_t queue_mode_client_awake_timer; /* Timer to control the client's - * awake time - */ -static uint8_t queue_mode_client_awake; /* 1 - client is awake, - * 0 - client is sleeping - */ +static coap_timer_t queue_mode_client_awake_timer; /* Timer to control the client's + * awake time + */ +static uint8_t queue_mode_client_awake; /* 1 - client is awake, + * 0 - client is sleeping + */ static uint16_t queue_mode_client_awake_time; /* The time to be awake */ /* Callback for the client awake timer */ -static void queue_mode_awake_timer_callback(coap_timer_t *timer); +static void queue_mode_awake_timer_callback(coap_timer_t *timer); #endif static void check_periodic_observations(); static void update_callback(coap_callback_request_state_t *callback_state); - +/*---------------------------------------------------------------------------*/ static int -set_rd_data(coap_message_t *request) +set_rd_data(lwm2m_session_info_t *session_info) { lwm2m_buffer_t outbuf; @@ -155,31 +143,31 @@ set_rd_data(coap_message_t *request) outbuf.len = 0; /* this will also set the request payload */ - rd_more = lwm2m_engine_set_rd_data(&outbuf, 0); - coap_set_payload(request, rd_data, outbuf.len); + session_info->rd_more = lwm2m_engine_set_rd_data(&outbuf, 0); + coap_set_payload(session_info->request, rd_data, outbuf.len); - if(rd_more) { + if(session_info->rd_more) { /* set the first block here */ LOG_DBG("Setting block1 in request\n"); - coap_set_header_block1(request, 0, 1, sizeof(rd_data)); + coap_set_header_block1(session_info->request, 0, 1, sizeof(rd_data)); } return outbuf.len; } /*---------------------------------------------------------------------------*/ static void -prepare_update(coap_message_t *request, int triggered) +prepare_update(lwm2m_session_info_t *session_info, int triggered) { - coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0); - coap_set_header_uri_path(request, session_info.assigned_ep); + coap_init_message(session_info->request, COAP_TYPE_CON, COAP_POST, 0); + coap_set_header_uri_path(session_info->request, session_info->assigned_ep); - snprintf(query_data, sizeof(query_data) - 1, "?lt=%d&b=%s", session_info.lifetime, session_info.binding); - LOG_DBG("UPDATE:%s %s\n", session_info.assigned_ep, query_data); - coap_set_header_uri_query(request, query_data); + snprintf(query_data, sizeof(query_data) - 1, "?lt=%d&b=%s", session_info->lifetime, session_info->binding); + LOG_DBG("UPDATE:%s %s\n", session_info->assigned_ep, query_data); + coap_set_header_uri_query(session_info->request, query_data); - if((triggered || rd_flags & FLAG_RD_DATA_UPDATE_ON_DIRTY) && (rd_flags & FLAG_RD_DATA_DIRTY)) { - rd_flags &= ~FLAG_RD_DATA_DIRTY; - set_rd_data(request); - rd_callback = update_callback; + if((triggered || session_info->rd_flags & FLAG_RD_DATA_UPDATE_ON_DIRTY) && (session_info->rd_flags & FLAG_RD_DATA_DIRTY)) { + session_info->rd_flags &= ~FLAG_RD_DATA_DIRTY; + set_rd_data(session_info); + session_info->rd_callback = update_callback; } } /*---------------------------------------------------------------------------*/ @@ -198,164 +186,152 @@ has_network_access(void) } /*---------------------------------------------------------------------------*/ int -lwm2m_rd_client_is_registered(void) +lwm2m_rd_client_is_registered(lwm2m_session_info_t *session_info) { - return rd_state == REGISTRATION_DONE || rd_state == UPDATE_SENT; -} -/*---------------------------------------------------------------------------*/ -void -lwm2m_rd_client_use_bootstrap_server(int use) -{ - session_info.use_bootstrap = use != 0; - if(session_info.use_bootstrap) { - rd_state = INIT; - } + return session_info->rd_state == REGISTRATION_DONE || session_info->rd_state == UPDATE_SENT; } /*---------------------------------------------------------------------------*/ /* will take another argument when we support multiple sessions */ void -lwm2m_rd_client_set_session_callback(session_callback_t cb) +lwm2m_rd_client_set_session_callback(lwm2m_session_info_t *session_info, session_callback_t cb) { - session_info.callback = cb; + session_info->callback = cb; } /*---------------------------------------------------------------------------*/ static void -perform_session_callback(int state) +perform_session_callback(lwm2m_session_info_t *session_info, int state) { - if(session_info.callback != NULL) { + if(session_info->callback != NULL) { LOG_DBG("Performing session callback: %d cb:%p\n", - state, session_info.callback); - session_info.callback(&session_info, state); - } -} -/*---------------------------------------------------------------------------*/ -void -lwm2m_rd_client_use_registration_server(int use) -{ - session_info.use_registration = use != 0; - if(session_info.use_registration) { - rd_state = INIT; + state, session_info->callback); + session_info->callback(session_info, state); } } /*---------------------------------------------------------------------------*/ uint16_t -lwm2m_rd_client_get_lifetime(void) +lwm2m_rd_client_get_lifetime(lwm2m_session_info_t *session_info) { - return session_info.lifetime; + return session_info->lifetime; } /*---------------------------------------------------------------------------*/ void -lwm2m_rd_client_set_lifetime(uint16_t lifetime) +lwm2m_rd_client_set_lifetime(lwm2m_session_info_t *session_info, uint16_t lifetime) { if(lifetime > 0) { - session_info.lifetime = lifetime; + session_info->lifetime = lifetime; } else { - session_info.lifetime = LWM2M_DEFAULT_CLIENT_LIFETIME; + session_info->lifetime = LWM2M_DEFAULT_CLIENT_LIFETIME; } } /*---------------------------------------------------------------------------*/ void +lwm2m_rd_client_set_endpoint_name(lwm2m_session_info_t *session_info, const char *endpoint) +{ + if(endpoint != NULL) { + session_info->ep = endpoint; + } +} +/*---------------------------------------------------------------------------*/ +void +lwm2m_rd_client_set_default_endpoint_name(const char *endpoint) +{ + strncpy(default_ep, endpoint, sizeof(default_ep) - 1); + default_ep[sizeof(default_ep) - 1] = '\0'; +} +/*---------------------------------------------------------------------------*/ +void lwm2m_rd_client_set_update_rd(void) { - rd_flags |= FLAG_RD_DATA_DIRTY; + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list); + while(session_info != NULL) { + session_info->rd_flags |= FLAG_RD_DATA_DIRTY; + session_info = session_info->next; + } } /*---------------------------------------------------------------------------*/ void -lwm2m_rd_client_set_automatic_update(int update) +lwm2m_rd_client_set_automatic_update(lwm2m_session_info_t *session_info, int update) { - rd_flags = (rd_flags & ~FLAG_RD_DATA_UPDATE_ON_DIRTY) | + session_info->rd_flags = (session_info->rd_flags & ~FLAG_RD_DATA_UPDATE_ON_DIRTY) | (update != 0 ? FLAG_RD_DATA_UPDATE_ON_DIRTY : 0); } /*---------------------------------------------------------------------------*/ void -lwm2m_rd_client_register_with_server(const coap_endpoint_t *server) +lwm2m_rd_client_register_with_server(lwm2m_session_info_t *session_info, + const coap_endpoint_t *server, + lwm2m_rd_client_server_type_t server_type) { - coap_endpoint_copy(&session_info.server_ep, server); - session_info.has_registration_server_info = 1; - session_info.registered = 0; - if(session_info.use_registration) { - rd_state = INIT; + if(session_info->ep == NULL) { + session_info->ep = default_ep; } -} -/*---------------------------------------------------------------------------*/ -static int -update_registration_server(void) -{ - if(session_info.has_registration_server_info) { - return 1; + /* default binding U = UDP, UQ = UDP Q-mode*/ +#if LWM2M_QUEUE_MODE_CONF_ENABLED + session_info->binding = "UQ"; + /* Enough margin to ensure that the client is not unregistered (we + * do not know the time it can stay awake) + */ + session_info->lifetime = (LWM2M_QUEUE_MODE_DEFAULT_CLIENT_SLEEP_TIME / 1000) * 2; +#else + session_info->binding = "U"; + if(session_info->lifetime == 0) { + session_info->lifetime = LWM2M_DEFAULT_CLIENT_LIFETIME; + } +#endif /* LWM2M_QUEUE_MODE_CONF_ENABLED */ + + session_info->rd_flags = FLAG_RD_DATA_UPDATE_ON_DIRTY; + session_info->has_bs_server_info = 0; + session_info->has_registration_server_info = 0; + session_info->wait_until_network_check = 0; + session_info->last_update = 0; + session_info->last_rd_progress = 0; + session_info->bootstrapped = 0; + session_info->rd_state = INIT; + + if(server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER) { + coap_endpoint_copy(&session_info->bs_server_ep, server); + session_info->has_bs_server_info = 1; + session_info->use_server_type = LWM2M_RD_CLIENT_BOOTSTRAP_SERVER; + } else { + coap_endpoint_copy(&session_info->server_ep, server); + session_info->use_server_type = LWM2M_RD_CLIENT_LWM2M_SERVER; + session_info->has_registration_server_info = 1; } -#if UIP_CONF_IPV6_RPL - { - rpl_dag_t *dag; - - /* Use the DAG id as server address if no other has been specified */ - dag = rpl_get_any_dag(); - if(dag != NULL) { - /* create coap-endpoint? */ - /* uip_ipaddr_copy(&server_ipaddr, &dag->dag_id); */ - /* server_port = REMOTE_PORT; */ - return 1; - } - } -#endif /* UIP_CONF_IPV6_RPL */ - - return 0; -} -/*---------------------------------------------------------------------------*/ -void -lwm2m_rd_client_register_with_bootstrap_server(const coap_endpoint_t *server) -{ - coap_endpoint_copy(&session_info.bs_server_ep, server); - session_info.has_bs_server_info = 1; - session_info.bootstrapped = 0; - session_info.registered = 0; - if(session_info.use_bootstrap) { - rd_state = INIT; - } + list_add(session_info_list, session_info); /* Add to the list of sessions */ } /*---------------------------------------------------------------------------*/ int -lwm2m_rd_client_deregister(void) +lwm2m_rd_client_deregister(lwm2m_session_info_t *session_info) { - if(lwm2m_rd_client_is_registered()) { - rd_state = DEREGISTER; + if(lwm2m_rd_client_is_registered(session_info)) { + session_info->rd_state = DEREGISTER; return 1; } /* Not registered */ return 0; } /*---------------------------------------------------------------------------*/ -void -lwm2m_rd_client_update_triggered(void) +static lwm2m_session_info_t * +get_session_info_from_server_ep(const coap_endpoint_t *server_ep) { - rd_flags |= FLAG_RD_DATA_UPDATE_TRIGGERED; - /* Here we need to do an CoAP timer poll - to get a quick request transmission! */ + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list); + while(session_info != NULL) { + if(coap_endpoint_cmp(&session_info->server_ep, server_ep)) { + return session_info; + } + session_info = session_info->next; + } + return NULL; } /*---------------------------------------------------------------------------*/ -static int -update_bootstrap_server(void) +void +lwm2m_rd_client_update_triggered(const coap_endpoint_t *server_ep) { - if(session_info.has_bs_server_info) { - return 1; + lwm2m_session_info_t *session_info = get_session_info_from_server_ep(server_ep); + if(session_info) { + session_info->rd_flags |= FLAG_RD_DATA_UPDATE_TRIGGERED; } - -#if UIP_CONF_IPV6_RPL - { - rpl_dag_t *dag; - - /* Use the DAG id as server address if no other has been specified */ - dag = rpl_get_any_dag(); - if(dag != NULL) { - /* create coap endpoint */ - /* uip_ipaddr_copy(&bs_server_ipaddr, &dag->dag_id); */ - /* bs_server_port = REMOTE_PORT; */ - return 1; - } - } -#endif /* UIP_CONF_IPV6_RPL */ - - return 0; + /* Here we need to do an CoAP timer poll - to get a quick request transmission! */ } /*---------------------------------------------------------------------------*/ /* @@ -373,29 +349,32 @@ bootstrap_callback(coap_callback_request_state_t *callback_state) { coap_request_state_t *state = &callback_state->state; LOG_DBG("Bootstrap callback Response: %d, ", state->response != NULL); + + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data; + if(state->status == COAP_REQUEST_STATUS_RESPONSE) { if(CHANGED_2_04 == state->response->code) { LOG_DBG_("Considered done!\n"); - rd_state = BOOTSTRAP_DONE; + session_info->rd_state = BOOTSTRAP_DONE; return; } /* Possible error response codes are 4.00 Bad request & 4.15 Unsupported content format */ LOG_DBG_("Failed with code %d. Retrying\n", state->response->code); /* TODO Application callback? */ - rd_state = INIT; - } else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) { + session_info->rd_state = INIT; + } else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) { LOG_DBG_("Server not responding! Retry?"); - rd_state = DO_BOOTSTRAP; + session_info->rd_state = DO_BOOTSTRAP; } else if(state->status == COAP_REQUEST_STATUS_FINISHED) { LOG_DBG_("Request finished. Ignore\n"); } else { LOG_DBG_("Unexpected error! Retry?"); - rd_state = DO_BOOTSTRAP; + session_info->rd_state = DO_BOOTSTRAP; } } /*---------------------------------------------------------------------------*/ static void -produce_more_rd(void) +produce_more_rd(lwm2m_session_info_t *session_info) { lwm2m_buffer_t outbuf; @@ -406,23 +385,23 @@ produce_more_rd(void) outbuf.size = sizeof(rd_data); outbuf.len = 0; - rd_block1++; + session_info->rd_block1++; /* this will also set the request payload */ - rd_more = lwm2m_engine_set_rd_data(&outbuf, rd_block1); - coap_set_payload(request, rd_data, outbuf.len); + session_info->rd_more = lwm2m_engine_set_rd_data(&outbuf, session_info->rd_block1); + coap_set_payload(session_info->request, rd_data, outbuf.len); LOG_DBG("Setting block1 in request - block: %d more: %d\n", - (int)rd_block1, (int)rd_more); - coap_set_header_block1(request, rd_block1, rd_more, sizeof(rd_data)); + (int)session_info->rd_block1, (int)session_info->rd_more); + coap_set_header_block1(session_info->request, session_info->rd_block1, session_info->rd_more, sizeof(rd_data)); - coap_send_request(&rd_request_state, &session_info.server_ep, request, rd_callback); + coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request, session_info->rd_callback); } /*---------------------------------------------------------------------------*/ static void block1_rd_callback(coap_timer_t *timer) { - produce_more_rd(); + produce_more_rd((lwm2m_session_info_t *)timer->user_data); } /*---------------------------------------------------------------------------*/ /* @@ -433,20 +412,24 @@ registration_callback(coap_callback_request_state_t *callback_state) { coap_request_state_t *state = &callback_state->state; LOG_DBG("Registration callback. Status: %d. Response: %d, ", state->status, state->response != NULL); + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data; + if(state->status == COAP_REQUEST_STATUS_RESPONSE) { /* check state and possibly set registration to done */ /* If we get a continue - we need to call the rd generator one more time */ + if(CONTINUE_2_31 == state->response->code) { /* We assume that size never change?! */ - coap_get_header_block1(state->response, &rd_block1, NULL, NULL, NULL); - coap_timer_set_callback(&block1_timer, block1_rd_callback); - coap_timer_set(&block1_timer, 1); /* delay 1 ms */ + coap_get_header_block1(state->response, &session_info->rd_block1, NULL, NULL, NULL); + coap_timer_set_user_data(&session_info->block1_timer, (void *)session_info); + coap_timer_set_callback(&session_info->block1_timer, block1_rd_callback); + coap_timer_set(&session_info->block1_timer, 1); /* delay 1 ms */ LOG_DBG_("Continue\n"); } else if(CREATED_2_01 == state->response->code) { if(state->response->location_path_len < LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN) { - memcpy(session_info.assigned_ep, state->response->location_path, + memcpy(session_info->assigned_ep, state->response->location_path, state->response->location_path_len); - session_info.assigned_ep[state->response->location_path_len] = 0; + session_info->assigned_ep[state->response->location_path_len] = 0; /* if we decide to not pass the lt-argument on registration, we should force an initial "update" to register lifetime with server */ #if LWM2M_QUEUE_MODE_ENABLED #if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION @@ -454,14 +437,14 @@ registration_callback(coap_callback_request_state_t *callback_state) lwm2m_queue_mode_set_first_request(); } #endif - lwm2m_rd_client_fsm_execute_queue_mode_awake(); /* Avoid 500 ms delay and move directly to the state*/ + lwm2m_rd_client_fsm_execute_queue_mode_awake(session_info); /* Avoid 500 ms delay and move directly to the state*/ #else - rd_state = REGISTRATION_DONE; + session_info->rd_state = REGISTRATION_DONE; #endif /* remember the last reg time */ - last_update = coap_timer_uptime(); - LOG_DBG_("Done (assigned EP='%s')!\n", session_info.assigned_ep); - perform_session_callback(LWM2M_RD_CLIENT_REGISTERED); + session_info->last_update = coap_timer_uptime(); + LOG_DBG_("Done (assigned EP='%s')!\n", session_info->assigned_ep); + perform_session_callback(session_info, LWM2M_RD_CLIENT_REGISTERED); return; } @@ -474,15 +457,15 @@ registration_callback(coap_callback_request_state_t *callback_state) LOG_DBG_("failed with code %d. Re-init network\n", state->response->code); } /* TODO Application callback? */ - rd_state = INIT; + session_info->rd_state = INIT; } else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) { LOG_DBG_("Server not responding, trying to reconnect\n"); - rd_state = INIT; - } else if(state->status == COAP_REQUEST_STATUS_FINISHED){ + session_info->rd_state = INIT; + } else if(state->status == COAP_REQUEST_STATUS_FINISHED) { LOG_DBG_("Request finished. Ignore\n"); } else { LOG_DBG_("Unexpected error, trying to reconnect\n"); - rd_state = INIT; + session_info->rd_state = INIT; } } /*---------------------------------------------------------------------------*/ @@ -495,18 +478,20 @@ update_callback(coap_callback_request_state_t *callback_state) coap_request_state_t *state = &callback_state->state; LOG_DBG("Update callback. Status: %d. Response: %d, ", state->status, state->response != NULL); + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data; + if(state->status == COAP_REQUEST_STATUS_RESPONSE) { /* If we get a continue - we need to call the rd generator one more time */ if(CONTINUE_2_31 == state->response->code) { /* We assume that size never change?! */ LOG_DBG_("Continue\n"); - coap_get_header_block1(state->response, &rd_block1, NULL, NULL, NULL); - coap_timer_set_callback(&block1_timer, block1_rd_callback); - coap_timer_set(&block1_timer, 1); /* delay 1 ms */ + coap_get_header_block1(state->response, &session_info->rd_block1, NULL, NULL, NULL); + coap_timer_set_callback(&session_info->block1_timer, block1_rd_callback); + coap_timer_set(&session_info->block1_timer, 1); /* delay 1 ms */ } else if(CHANGED_2_04 == state->response->code) { LOG_DBG_("Done!\n"); /* remember the last reg time */ - last_update = coap_timer_uptime(); + session_info->last_update = coap_timer_uptime(); #if LWM2M_QUEUE_MODE_ENABLED /* If it has been waked up by a notification, send the stored notifications in queue */ if(lwm2m_queue_mode_is_waked_up_by_notification()) { @@ -519,25 +504,26 @@ update_callback(coap_callback_request_state_t *callback_state) lwm2m_queue_mode_set_first_request(); } #endif /* LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION */ - lwm2m_rd_client_fsm_execute_queue_mode_awake(); /* Avoid 500 ms delay and move directly to the state*/ + lwm2m_rd_client_fsm_execute_queue_mode_awake(session_info); /* Avoid 500 ms delay and move directly to the state*/ #else - rd_state = REGISTRATION_DONE; - rd_flags &= ~FLAG_RD_DATA_UPDATE_TRIGGERED; -#endif /* LWM2M_QUEUE_MODE_ENABLED */ + + session_info->rd_state = REGISTRATION_DONE; + session_info->rd_flags &= ~FLAG_RD_DATA_UPDATE_TRIGGERED; +#endif /* LWM2M_QUEUE_MODE_DEFAULT_CLIENT_SLEEP_TIME */ } else { /* Possible error response codes are 4.00 Bad request & 4.04 Not Found */ LOG_DBG_("Failed with code %d. Retrying registration\n", state->response->code); - rd_state = DO_REGISTRATION; + session_info->rd_state = DO_REGISTRATION; } } else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) { LOG_DBG_("Server not responding, trying to reconnect\n"); - rd_state = INIT; - } else if(state->status == COAP_REQUEST_STATUS_FINISHED){ + session_info->rd_state = INIT; + } else if(state->status == COAP_REQUEST_STATUS_FINISHED) { LOG_DBG_("Request finished. Ignore\n"); } else { LOG_DBG_("Unexpected error, trying to reconnect\n"); - rd_state = INIT; + session_info->rd_state = INIT; } } /*---------------------------------------------------------------------------*/ @@ -549,19 +535,49 @@ deregister_callback(coap_callback_request_state_t *callback_state) state->status, state->response != NULL ? state->response->code : 0); + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data; + if(state->status == COAP_REQUEST_STATUS_RESPONSE && (DELETED_2_02 == state->response->code)) { LOG_DBG("Deregistration success\n"); - rd_state = DEREGISTERED; - perform_session_callback(LWM2M_RD_CLIENT_DEREGISTERED); + session_info->rd_state = DEREGISTERED; + perform_session_callback(session_info, LWM2M_RD_CLIENT_DEREGISTERED); } else { LOG_DBG("Deregistration failed\n"); - if(rd_state == DEREGISTER_SENT) { - rd_state = DEREGISTER_FAILED; - perform_session_callback(LWM2M_RD_CLIENT_DEREGISTER_FAILED); + if(session_info->rd_state == DEREGISTER_SENT) { + session_info->rd_state = DEREGISTER_FAILED; + perform_session_callback(session_info, LWM2M_RD_CLIENT_DEREGISTER_FAILED); } } } /*---------------------------------------------------------------------------*/ +#if LWM2M_QUEUE_MODE_ENABLED +static int +all_sessions_in_queue_mode_state(void) +{ + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list); + while(session_info != NULL) { + if(((session_info->rd_state & 0xF) != 0xE)) { + return 0; + } + session_info = session_info->next; + } + return 1; +} +/*---------------------------------------------------------------------------*/ +static int +all_sessions_in_queue_mode_awake(void) +{ + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list); + while(session_info != NULL) { + if(session_info->rd_state != QUEUE_MODE_AWAKE) { + return 0; + } + session_info = session_info->next; + } + return 1; +} +#endif /* LWM2M_QUEUE_MODE_ENABLED */ +/*---------------------------------------------------------------------------*/ /* CoAP timer callback */ static void periodic_process(coap_timer_t *timer) @@ -571,7 +587,7 @@ periodic_process(coap_timer_t *timer) /* reschedule the CoAP timer */ #if LWM2M_QUEUE_MODE_ENABLED /* In Queue Mode, the machine is not executed periodically, but with the awake/sleeping times */ - if(!((rd_state & 0xF) == 0xE)) { + if(!all_sessions_in_queue_mode_state()) { coap_timer_reset(&rd_timer, STATE_MACHINE_UPDATE_INTERVAL); } #else @@ -580,233 +596,243 @@ periodic_process(coap_timer_t *timer) now = coap_timer_uptime(); - LOG_DBG("RD Client - state: %d, ms: %lu\n", rd_state, - (unsigned long)coap_timer_uptime()); + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list); + while(session_info != NULL) { - switch(rd_state) { - case INIT: - LOG_DBG("RD Client started with endpoint '%s' and client lifetime %d\n", session_info.ep, session_info.lifetime); - rd_state = WAIT_NETWORK; - break; - case WAIT_NETWORK: - if(now > wait_until_network_check) { - /* check each 10 seconds before next check */ - LOG_DBG("Checking for network... %lu\n", - (unsigned long)wait_until_network_check); - wait_until_network_check = now + 10000; - if(has_network_access()) { - /* Either do bootstrap then registration */ - if(session_info.use_bootstrap) { - rd_state = DO_BOOTSTRAP; - } else { - rd_state = DO_REGISTRATION; - } - } - /* Otherwise wait until for a network to join */ - } - break; - case DO_BOOTSTRAP: - if(session_info.use_bootstrap && session_info.bootstrapped == 0) { - if(update_bootstrap_server()) { - /* prepare request, TID is set by COAP_BLOCKING_REQUEST() */ - coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0); - coap_set_header_uri_path(request, "/bs"); + LOG_DBG("RD Client with assigned ep: %s - state: %d, ms: %lu\n", session_info->assigned_ep, session_info->rd_state, + (unsigned long)coap_timer_uptime()); - snprintf(query_data, sizeof(query_data) - 1, "?ep=%s", session_info.ep); - coap_set_header_uri_query(request, query_data); - LOG_INFO("Registering ID with bootstrap server ["); - LOG_INFO_COAP_EP(&session_info.bs_server_ep); - LOG_INFO_("] as '%s'\n", query_data); - - if(coap_send_request(&rd_request_state, &session_info.bs_server_ep, - request, bootstrap_callback)) { - rd_state = BOOTSTRAP_SENT; - } - } - } - break; - case BOOTSTRAP_SENT: - /* Just wait for bootstrap to be done... */ - break; - case BOOTSTRAP_DONE: - /* check that we should still use bootstrap */ - if(session_info.use_bootstrap) { - lwm2m_security_server_t *security; - LOG_DBG("*** Bootstrap - checking for server info...\n"); - /* get the security object - ignore bootstrap servers */ - for(security = lwm2m_security_get_first(); - security != NULL; - security = lwm2m_security_get_next(security)) { - if(security->bootstrap == 0) { - break; - } - } - - if(security != NULL) { - /* get the server URI */ - if(security->server_uri_len > 0) { - uint8_t secure = 0; - - LOG_DBG("**** Found security instance using: "); - LOG_DBG_COAP_STRING((const char *)security->server_uri, - security->server_uri_len); - LOG_DBG_(" (len %d) \n", security->server_uri_len); - /* TODO Should verify it is a URI */ - /* Check if secure */ - secure = strncmp((const char *)security->server_uri, - "coaps:", 6) == 0; - - if(!coap_endpoint_parse((const char *)security->server_uri, - security->server_uri_len, - &session_info.server_ep)) { - LOG_DBG("Failed to parse server URI!\n"); + switch(session_info->rd_state) { + case INIT: + LOG_DBG("RD Client started with endpoint '%s' and client lifetime %d\n", session_info->ep, session_info->lifetime); + session_info->rd_state = WAIT_NETWORK; + break; + case WAIT_NETWORK: + if(now > session_info->wait_until_network_check) { + /* check each 10 seconds before next check */ + LOG_DBG("Checking for network... %lu\n", + (unsigned long)session_info->wait_until_network_check); + session_info->wait_until_network_check = now + 10000; + if(has_network_access()) { + /* Either do bootstrap then registration */ + if(session_info->use_server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER) { + session_info->rd_state = DO_BOOTSTRAP; } else { - LOG_DBG("Server address:"); - LOG_DBG_COAP_EP(&session_info.server_ep); - LOG_DBG_("\n"); - if(secure) { - LOG_DBG("Secure CoAP requested but not supported - can not bootstrap\n"); - } else { - lwm2m_rd_client_register_with_server(&session_info.server_ep); - session_info.bootstrapped++; - } + session_info->rd_state = DO_REGISTRATION; } - } else { - LOG_DBG("** failed to parse URI "); - LOG_DBG_COAP_STRING((const char *)security->server_uri, - security->server_uri_len); - LOG_DBG_("\n"); + } + /* Otherwise wait until for a network to join */ + } + break; + case DO_BOOTSTRAP: + if(session_info->use_server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER && + session_info->bootstrapped == 0 && + session_info->has_bs_server_info) { + + /* prepare request, TID is set by COAP_BLOCKING_REQUEST() */ + coap_init_message(session_info->request, COAP_TYPE_CON, COAP_POST, 0); + coap_set_header_uri_path(session_info->request, "/bs"); + + snprintf(query_data, sizeof(query_data) - 1, "?ep=%s", session_info->ep); + coap_set_header_uri_query(session_info->request, query_data); + LOG_INFO("Registering ID with bootstrap server ["); + LOG_INFO_COAP_EP(&session_info->bs_server_ep); + LOG_INFO_("] as '%s'\n", query_data); + /* Add session info as user data to use it in the callbacks */ + session_info->rd_request_state.state.user_data = (void *)session_info; + if(coap_send_request(&session_info->rd_request_state, &session_info->bs_server_ep, + session_info->request, bootstrap_callback)) { + session_info->rd_state = BOOTSTRAP_SENT; } } + break; + case BOOTSTRAP_SENT: + /* Just wait for bootstrap to be done... */ + break; + case BOOTSTRAP_DONE: + /* check that we should still use bootstrap */ + if(session_info->use_server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER) { + lwm2m_security_server_t *security; + LOG_DBG("*** Bootstrap - checking for server info...\n"); + /* get the security object - ignore bootstrap servers */ + for(security = lwm2m_security_get_first(); + security != NULL; + security = lwm2m_security_get_next(security)) { + if(security->bootstrap == 0) { + break; + } + } - /* if we did not register above - then fail this and restart... */ - if(session_info.bootstrapped == 0) { - /* Not ready. Lets retry with the bootstrap server again */ - rd_state = DO_BOOTSTRAP; - } else { - rd_state = DO_REGISTRATION; + if(security != NULL) { + /* get the server URI */ + if(security->server_uri_len > 0) { + uint8_t secure = 0; + + LOG_DBG("**** Found security instance using: "); + LOG_DBG_COAP_STRING((const char *)security->server_uri, + security->server_uri_len); + LOG_DBG_(" (len %d) \n", security->server_uri_len); + /* TODO Should verify it is a URI */ + /* Check if secure */ + secure = strncmp((const char *)security->server_uri, + "coaps:", 6) == 0; + + if(!coap_endpoint_parse((const char *)security->server_uri, + security->server_uri_len, + &session_info->server_ep)) { + LOG_DBG("Failed to parse server URI!\n"); + } else { + LOG_DBG("Server address:"); + LOG_DBG_COAP_EP(&session_info->server_ep); + LOG_DBG_("\n"); + if(secure) { + LOG_DBG("Secure CoAP requested but not supported - can not bootstrap\n"); + } else { + lwm2m_rd_client_register_with_server(session_info, &session_info->server_ep, LWM2M_RD_CLIENT_LWM2M_SERVER); + session_info->bootstrapped++; + } + } + } else { + LOG_DBG("** failed to parse URI "); + LOG_DBG_COAP_STRING((const char *)security->server_uri, + security->server_uri_len); + LOG_DBG_("\n"); + } + } + + /* if we did not register above - then fail this and restart... */ + if(session_info->bootstrapped == 0) { + /* Not ready. Lets retry with the bootstrap server again */ + session_info->rd_state = DO_BOOTSTRAP; + } else { + session_info->rd_state = DO_REGISTRATION; + } } - } - break; - case DO_REGISTRATION: - if(!coap_endpoint_is_connected(&session_info.server_ep)) { - /* Not connected... wait a bit... and retry connection */ - coap_endpoint_connect(&session_info.server_ep); - LOG_DBG("Wait until connected... \n"); - return; - } - if(session_info.use_registration && !session_info.registered && - update_registration_server()) { - int len; - - /* prepare request, TID was set by COAP_BLOCKING_REQUEST() */ - coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0); - coap_set_header_uri_path(request, "/rd"); - - snprintf(query_data, sizeof(query_data) - 1, "?ep=%s<=%d&b=%s", session_info.ep, session_info.lifetime, session_info.binding); - coap_set_header_uri_query(request, query_data); - - len = set_rd_data(request); - rd_callback = registration_callback; - - LOG_INFO("Registering with ["); - LOG_INFO_COAP_EP(&session_info.server_ep); - LOG_INFO_("] lwm2m endpoint '%s': '", query_data); - if(len) { - LOG_INFO_COAP_STRING((const char *)rd_data, len); + break; + case DO_REGISTRATION: + if(!coap_endpoint_is_connected(&session_info->server_ep)) { + /* Not connected... wait a bit... and retry connection */ + coap_endpoint_connect(&session_info->server_ep); + LOG_DBG("Wait until connected... \n"); + return; } - LOG_INFO_("' More:%d\n", rd_more); - if(coap_send_request(&rd_request_state, &session_info.server_ep, - request, registration_callback)){ - rd_state = REGISTRATION_SENT; + if(session_info->use_server_type == LWM2M_RD_CLIENT_LWM2M_SERVER && + !lwm2m_rd_client_is_registered(session_info) && + session_info->has_registration_server_info) { + int len; + + /* prepare request, TID was set by COAP_BLOCKING_REQUEST() */ + coap_init_message(session_info->request, COAP_TYPE_CON, COAP_POST, 0); + coap_set_header_uri_path(session_info->request, "/rd"); + + snprintf(query_data, sizeof(query_data) - 1, "?ep=%s<=%d&b=%s", session_info->ep, session_info->lifetime, session_info->binding); + coap_set_header_uri_query(session_info->request, query_data); + + len = set_rd_data(session_info); + session_info->rd_callback = registration_callback; + + LOG_INFO("Registering with ["); + LOG_INFO_COAP_EP(&session_info->server_ep); + LOG_INFO_("] lwm2m endpoint '%s': '", query_data); + if(len) { + LOG_INFO_COAP_STRING((const char *)rd_data, len); + } + LOG_INFO_("' More:%d\n", session_info->rd_more); + + /* Add session info as user data to use it in the callbacks */ + session_info->rd_request_state.state.user_data = (void *)session_info; + if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, + session_info->request, registration_callback)) { + + session_info->rd_state = REGISTRATION_SENT; + } + session_info->last_rd_progress = coap_timer_uptime(); } - } - break; - case REGISTRATION_SENT: - /* just wait until the callback kicks us to the next state... */ - break; - case REGISTRATION_DONE: - /* All is done! */ + break; + case REGISTRATION_SENT: + /* just wait until the callback kicks us to the next state... */ + break; + case REGISTRATION_DONE: + /* All is done! */ - check_periodic_observations(); /* TODO: manage periodic observations */ + check_periodic_observations(); /* TODO: manage periodic observations */ - /* check if it is time for the next update */ - if((rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED) || - ((uint32_t)session_info.lifetime * 500) <= now - last_update) { - /* triggered or time to send an update to the server, at half-time! sec vs ms */ - prepare_update(request, rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED); - if(coap_send_request(&rd_request_state, &session_info.server_ep, request, - update_callback)) { - rd_state = UPDATE_SENT; + /* check if it is time for the next update */ + if((session_info->rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED) || + ((uint32_t)session_info->lifetime * 500) <= now - session_info->last_update) { + /* triggered or time to send an update to the server, at half-time! sec vs ms */ + prepare_update(session_info, session_info->rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED); + + /* Add session info as user data to use it in the callbacks */ + session_info->rd_request_state.state.user_data = (void *)session_info; + if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request, + update_callback)) { + session_info->rd_state = UPDATE_SENT; + } + session_info->last_rd_progress = coap_timer_uptime(); } - } - break; + break; + #if LWM2M_QUEUE_MODE_ENABLED - case QUEUE_MODE_AWAKE: - LOG_DBG("Queue Mode: Client is AWAKE at %lu\n", (unsigned long)coap_timer_uptime()); - queue_mode_client_awake = 1; - queue_mode_client_awake_time = lwm2m_queue_mode_get_awake_time(); - coap_timer_set(&queue_mode_client_awake_timer, queue_mode_client_awake_time); - break; - case QUEUE_MODE_SEND_UPDATE: -/* Define this macro to make the necessary actions for waking up, - * depending on the platform - */ + case QUEUE_MODE_AWAKE: + LOG_DBG("Queue Mode: Client is AWAKE at %lu\n", (unsigned long)coap_timer_uptime()); + if((queue_mode_client_awake = all_sessions_in_queue_mode_awake())) { + queue_mode_client_awake_time = lwm2m_queue_mode_get_awake_time(); + coap_timer_set(&queue_mode_client_awake_timer, queue_mode_client_awake_time); + } + break; + case QUEUE_MODE_SEND_UPDATE: + /* Define this macro to make the necessary actions for waking up, + * depending on the platform + */ #ifdef LWM2M_QUEUE_MODE_WAKE_UP - LWM2M_QUEUE_MODE_WAKE_UP(); + LWM2M_QUEUE_MODE_WAKE_UP(); #endif /* LWM2M_QUEUE_MODE_WAKE_UP */ - prepare_update(request, rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED); - if(coap_send_request(&rd_request_state, &session_info.server_ep, request, - update_callback)) { - rd_state = UPDATE_SENT; - } - break; + prepare_update(session_info, session_info->rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED); + /* Add session info as user data to use it in the callbacks */ + session_info->rd_request_state.state.user_data = (void *)session_info; + if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request, + update_callback)) { + session_info->rd_state = UPDATE_SENT; + } + session_info->last_rd_progress = coap_timer_uptime(); + break; #endif /* LWM2M_QUEUE_MODE_ENABLED */ - case UPDATE_SENT: - /* just wait until the callback kicks us to the next state... */ - break; - case DEREGISTER: - LOG_INFO("DEREGISTER %s\n", session_info.assigned_ep); - coap_init_message(request, COAP_TYPE_CON, COAP_DELETE, 0); - coap_set_header_uri_path(request, session_info.assigned_ep); - if(coap_send_request(&rd_request_state, &session_info.server_ep, request, - deregister_callback)) { - rd_state = DEREGISTER_SENT; - } - break; - case DEREGISTER_SENT: - break; - case DEREGISTER_FAILED: - break; - case DEREGISTERED: - break; + case UPDATE_SENT: + /* just wait until the callback kicks us to the next state... */ + break; + case DEREGISTER: + LOG_INFO("DEREGISTER %s\n", session_info->assigned_ep); + coap_init_message(session_info->request, COAP_TYPE_CON, COAP_DELETE, 0); + coap_set_header_uri_path(session_info->request, session_info->assigned_ep); - default: - LOG_WARN("Unhandled state: %d\n", rd_state); + /* Add session info as user data to use it in the callbacks */ + session_info->rd_request_state.state.user_data = (void *)session_info; + if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request, + deregister_callback)) { + session_info->rd_state = DEREGISTER_SENT; + } + break; + case DEREGISTER_SENT: + break; + case DEREGISTER_FAILED: + break; + case DEREGISTERED: + break; + + default: + LOG_WARN("Unhandled state: %d\n", session_info->rd_state); + } + session_info = session_info->next; } } /*---------------------------------------------------------------------------*/ void lwm2m_rd_client_init(const char *ep) { - session_info.ep = ep; - /* default binding U = UDP, UQ = UDP Q-mode*/ -#if LWM2M_QUEUE_MODE_ENABLED - session_info.binding = "UQ"; - /* Enough margin to ensure that the client is not unregistered (we - * do not know the time it can stay awake) - */ - session_info.lifetime = (LWM2M_QUEUE_MODE_DEFAULT_CLIENT_SLEEP_TIME / 1000) * 2; -#else - session_info.binding = "U"; - if(session_info.lifetime == 0) { - session_info.lifetime = LWM2M_DEFAULT_CLIENT_LIFETIME; - } -#endif - - rd_state = INIT; + lwm2m_rd_client_set_default_endpoint_name(ep); /* call the RD client periodically */ coap_timer_set_callback(&rd_timer, periodic_process); @@ -823,7 +849,7 @@ check_periodic_observations(void) } /*---------------------------------------------------------------------------*/ /* - *Queue Mode Support + * Queue Mode Support */ #if LWM2M_QUEUE_MODE_ENABLED /*---------------------------------------------------------------------------*/ @@ -846,27 +872,31 @@ queue_mode_awake_timer_callback(coap_timer_t *timer) LOG_DBG("Queue Mode: Client is SLEEPING at %lu\n", (unsigned long)coap_timer_uptime()); queue_mode_client_awake = 0; -/* Define this macro to enter sleep mode depending on the platform */ + lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list); + while(session_info != NULL) { + session_info->rd_state = QUEUE_MODE_SEND_UPDATE; + session_info = session_info->next; + } + coap_timer_set(&rd_timer, lwm2m_queue_mode_get_sleep_time()); + /* Define this macro to enter sleep mode depending on the platform */ #ifdef LWM2M_QUEUE_MODE_SLEEP_MS LWM2M_QUEUE_MODE_SLEEP_MS(lwm2m_queue_mode_get_sleep_time()); #endif /* LWM2M_QUEUE_MODE_SLEEP_MS */ - rd_state = QUEUE_MODE_SEND_UPDATE; - coap_timer_set(&rd_timer, lwm2m_queue_mode_get_sleep_time()); } /*---------------------------------------------------------------------------*/ void -lwm2m_rd_client_fsm_execute_queue_mode_awake() +lwm2m_rd_client_fsm_execute_queue_mode_awake(lwm2m_session_info_t *session_info) { coap_timer_stop(&rd_timer); - rd_state = QUEUE_MODE_AWAKE; + session_info->rd_state = QUEUE_MODE_AWAKE; periodic_process(&rd_timer); } /*---------------------------------------------------------------------------*/ void -lwm2m_rd_client_fsm_execute_queue_mode_update() +lwm2m_rd_client_fsm_execute_queue_mode_update(lwm2m_session_info_t *session_info) { coap_timer_stop(&rd_timer); - rd_state = QUEUE_MODE_SEND_UPDATE; + session_info->rd_state = QUEUE_MODE_SEND_UPDATE; periodic_process(&rd_timer); } /*---------------------------------------------------------------------------*/ diff --git a/os/services/lwm2m/lwm2m-rd-client.h b/os/services/lwm2m/lwm2m-rd-client.h index cfc5c549d..58fa0a8c2 100644 --- a/os/services/lwm2m/lwm2m-rd-client.h +++ b/os/services/lwm2m/lwm2m-rd-client.h @@ -46,6 +46,13 @@ #ifndef LWM2M_RD_CLIENT_H_ #define LWM2M_RD_CLIENT_H_ +/* The type of server to use for registration: bootstrap or LWM2M */ +typedef enum { + LWM2M_RD_CLIENT_BOOTSTRAP_SERVER, + LWM2M_RD_CLIENT_LWM2M_SERVER +} lwm2m_rd_client_server_type_t; + +/* Session callback states */ #define LWM2M_RD_CLIENT_BOOTSTRAPPED 1 #define LWM2M_RD_CLIENT_REGISTERED 2 #define LWM2M_RD_CLIENT_DEREGISTERED 3 @@ -54,28 +61,70 @@ #include "lwm2m-object.h" #include "lwm2m-queue-mode-conf.h" +#include "coap-endpoint.h" +#include "coap-callback-api.h" struct lwm2m_session_info; typedef void (*session_callback_t)(struct lwm2m_session_info *session, int status); -int lwm2m_rd_client_is_registered(void); -void lwm2m_rd_client_use_bootstrap_server(int use); -void lwm2m_rd_client_use_registration_server(int use); -void lwm2m_rd_client_register_with_server(const coap_endpoint_t *server); -void lwm2m_rd_client_register_with_bootstrap_server(const coap_endpoint_t *server); -uint16_t lwm2m_rd_client_get_lifetime(void); -void lwm2m_rd_client_set_lifetime(uint16_t lifetime); +#ifndef LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN +#define LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN 15 +#endif /* LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN */ +/*---------------------------------------------------------------------------*/ +/*- Server session----------------------------*/ +/*---------------------------------------------------------------------------*/ +typedef struct lwm2m_session_info { + + struct lwm2m_session_info *next; + /* Information */ + const char *ep; + const char *binding; + char assigned_ep[LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN]; + uint16_t lifetime; + coap_endpoint_t bs_server_ep; + coap_endpoint_t server_ep; + lwm2m_rd_client_server_type_t use_server_type; + uint8_t has_bs_server_info; + uint8_t has_registration_server_info; + uint8_t bootstrapped; /* bootstrap done */ + session_callback_t callback; + + /* CoAP Request */ + coap_callback_request_state_t rd_request_state; + coap_message_t request[1]; /* This way the message can be treated as pointer as usual. */ + + /* RD parameters */ + uint8_t rd_state; + uint8_t rd_flags; + uint64_t wait_until_network_check; + uint64_t last_update; + uint64_t last_rd_progress; + + /* Blosk Transfer */ + uint32_t rd_block1; + uint8_t rd_more; + void (*rd_callback)(coap_callback_request_state_t *state); + coap_timer_t block1_timer; +} lwm2m_session_info_t; + +int lwm2m_rd_client_is_registered(lwm2m_session_info_t *session_info); +void lwm2m_rd_client_register_with_server(lwm2m_session_info_t *session_info, const coap_endpoint_t *server, lwm2m_rd_client_server_type_t server_type); +uint16_t lwm2m_rd_client_get_lifetime(lwm2m_session_info_t *session_info); +void lwm2m_rd_client_set_lifetime(lwm2m_session_info_t *session_info, uint16_t lifetime); +void lwm2m_rd_client_set_endpoint_name(lwm2m_session_info_t *session_info, const char *endpoint); +void lwm2m_rd_client_set_default_endpoint_name(const char *endpoint); + /* Indicate that something in the object list have changed */ void lwm2m_rd_client_set_update_rd(void); /* Control if the object list should be automatically updated at updates of lifetime */ -void lwm2m_rd_client_set_automatic_update(int update); +void lwm2m_rd_client_set_automatic_update(lwm2m_session_info_t *session_info, int update); /* trigger an immediate update */ -void lwm2m_rd_client_update_triggered(void); +void lwm2m_rd_client_update_triggered(const coap_endpoint_t *server_ep); -int lwm2m_rd_client_deregister(void); +int lwm2m_rd_client_deregister(lwm2m_session_info_t *session_info); void lwm2m_rd_client_init(const char *ep); -void lwm2m_rd_client_set_session_callback(session_callback_t cb); +void lwm2m_rd_client_set_session_callback(lwm2m_session_info_t *session_info, session_callback_t cb); #if LWM2M_QUEUE_MODE_ENABLED uint8_t lwm2m_rd_client_is_client_awake(void); @@ -84,28 +133,5 @@ void lwm2m_rd_client_fsm_execute_queue_mode_awake(); void lwm2m_rd_client_fsm_execute_queue_mode_update(); #endif -#ifndef LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN -#define LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN 15 -#endif /* LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN */ - -/*---------------------------------------------------------------------------*/ -/*- Server session-*Currently single session only*---------------------------*/ -/*---------------------------------------------------------------------------*/ -struct lwm2m_session_info { - const char *ep; - const char *binding; - char assigned_ep[LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN]; - uint16_t lifetime; - coap_endpoint_t bs_server_ep; - coap_endpoint_t server_ep; - uint8_t use_bootstrap; - uint8_t has_bs_server_info; - uint8_t use_registration; - uint8_t has_registration_server_info; - uint8_t registered; - uint8_t bootstrapped; /* bootstrap done */ - session_callback_t callback; -}; - #endif /* LWM2M_RD_CLIENT_H_ */ /** @} */ diff --git a/os/services/lwm2m/lwm2m-server.c b/os/services/lwm2m/lwm2m-server.c index bf0398b4f..b777d72d6 100644 --- a/os/services/lwm2m/lwm2m-server.c +++ b/os/services/lwm2m/lwm2m-server.c @@ -221,7 +221,7 @@ lwm2m_callback(lwm2m_object_instance_t *object, } else if(ctx->operation == LWM2M_OP_EXECUTE) { switch(ctx->resource_id) { case LWM2M_SERVER_REG_UPDATE_TRIGGER_ID: - lwm2m_rd_client_update_triggered(); + lwm2m_rd_client_update_triggered(ctx->request->src_ep); break; } } else { diff --git a/os/services/rpl-border-router/embedded/slip-bridge.c b/os/services/rpl-border-router/embedded/slip-bridge.c index d1f97339d..b1349ac0a 100644 --- a/os/services/rpl-border-router/embedded/slip-bridge.c +++ b/os/services/rpl-border-router/embedded/slip-bridge.c @@ -43,8 +43,6 @@ #include "dev/slip.h" #include /*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -/*---------------------------------------------------------------------------*/ /* Log configuration */ #include "sys/log.h" #define LOG_MODULE "SLIP" @@ -63,34 +61,34 @@ request_prefix(void) uip_buf[1] = 'P'; uip_len = 2; slip_write(uip_buf, uip_len); - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void slip_input_callback(void) { LOG_DBG("SIN: %u\n", uip_len); - if(uip_buf[UIP_LLH_LEN] == '!') { + if(uip_buf[0] == '!') { LOG_INFO("Got configuration message of type %c\n", - uip_buf[UIP_LLH_LEN + 1]); - if(uip_buf[UIP_LLH_LEN + 1] == 'P') { + uip_buf[1]); + if(uip_buf[1] == 'P') { uip_ipaddr_t prefix; /* Here we set a prefix !!! */ memset(&prefix, 0, 16); - memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8); + memcpy(&prefix, &uip_buf[2], 8); - uip_clear_buf(); + uipbuf_clear(); LOG_INFO("Setting prefix "); LOG_INFO_6ADDR(&prefix); LOG_INFO_("\n"); set_prefix_64(&prefix); } - uip_clear_buf(); + uipbuf_clear(); - } else if(uip_buf[UIP_LLH_LEN] == '?') { - LOG_INFO("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); - if(uip_buf[UIP_LLH_LEN + 1] == 'M') { + } else if(uip_buf[0] == '?') { + LOG_INFO("Got request message of type %c\n", uip_buf[1]); + if(uip_buf[1] == 'M') { char *hexchar = "0123456789abcdef"; int j; /* this is just a test so far... just to see if it works */ @@ -102,7 +100,7 @@ slip_input_callback(void) uip_len = 18; slip_write(uip_buf, uip_len); } - uip_clear_buf(); + uipbuf_clear(); } else { /* Save the last sender received over SLIP to avoid bouncing the packet back if no route is found */ diff --git a/os/services/rpl-border-router/native/border-router-mac.c b/os/services/rpl-border-router/native/border-router-mac.c index fbf8b6496..7fe4fc6ed 100644 --- a/os/services/rpl-border-router/native/border-router-mac.c +++ b/os/services/rpl-border-router/native/border-router-mac.c @@ -64,6 +64,20 @@ struct tx_callback { /*---------------------------------------------------------------------------*/ static struct tx_callback callbacks[MAX_CALLBACKS]; /*---------------------------------------------------------------------------*/ +void +init_sec(void) +{ + /* use the CSMA LLSEC config parameter */ +#if LLSEC802154_USES_AUX_HEADER + if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) == + PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT) { + packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, + CSMA_LLSEC_SECURITY_LEVEL); + } +#endif +} +/*---------------------------------------------------------------------------*/ + void packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx) { @@ -164,6 +178,13 @@ off() return 1; } /*---------------------------------------------------------------------------*/ +static int +max_payload() +{ + init_sec(); + return 127 - NETSTACK_FRAMER.length(); +} +/*---------------------------------------------------------------------------*/ static void init(void) { @@ -176,6 +197,7 @@ const struct mac_driver border_router_mac_driver = { send_packet, packet_input, on, - off + off, + max_payload, }; /*---------------------------------------------------------------------------*/ diff --git a/os/services/rpl-border-router/native/slip-config.c b/os/services/rpl-border-router/native/slip-config.c index a8e17f22c..7797a129f 100644 --- a/os/services/rpl-border-router/native/slip-config.c +++ b/os/services/rpl-border-router/native/slip-config.c @@ -142,14 +142,20 @@ slip_config_handle_arguments(int argc, char **argv) fprintf(stderr, " -a host Connect via TCP to server at \n"); fprintf(stderr, " -p port Connect via TCP to server at :\n"); fprintf(stderr, " -t tundev Name of interface (default tun0)\n"); +#ifdef __APPLE__ + fprintf(stderr, " -v level Verbosity level\n"); +#else fprintf(stderr, " -v[level] Verbosity level\n"); +#endif fprintf(stderr, " -v0 No messages\n"); fprintf(stderr, " -v1 Encapsulated SLIP debug messages (default)\n"); fprintf(stderr, " -v2 Printable strings after they are received\n"); fprintf(stderr, " -v3 Printable strings and SLIP packet notifications\n"); fprintf(stderr, " -v4 All printable characters as they are received\n"); fprintf(stderr, " -v5 All SLIP packets in hex\n"); +#ifndef __APPLE__ fprintf(stderr, " -v Equivalent to -v3\n"); +#endif fprintf(stderr, " -d[basedelay] Minimum delay between outgoing SLIP packets.\n"); fprintf(stderr, " Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n"); fprintf(stderr, " -d is equivalent to -d10.\n"); diff --git a/os/services/rpl-border-router/native/slip-dev.c b/os/services/rpl-border-router/native/slip-dev.c index 7ea764836..3ccd1db4b 100644 --- a/os/services/rpl-border-router/native/slip-dev.c +++ b/os/services/rpl-border-router/native/slip-dev.c @@ -562,7 +562,7 @@ slip_init(void) slip_send(slipfd, SLIP_END); inslip = fdopen(slipfd, "r"); if(inslip == NULL) { - err(1, "main: fdopen"); + err(1, "slip_init: fdopen"); } } /*---------------------------------------------------------------------------*/ diff --git a/os/services/rpl-border-router/native/tun-bridge.c b/os/services/rpl-border-router/native/tun-bridge.c index f321577b3..26bc80bd7 100644 --- a/os/services/rpl-border-router/native/tun-bridge.c +++ b/os/services/rpl-border-router/native/tun-bridge.c @@ -210,7 +210,7 @@ tun_init() tunfd = tun_alloc(slip_config_tundev); if(tunfd == -1) { - err(1, "main: open"); + err(1, "tun_init: open"); } select_set_callback(tunfd, &tun_select_callback); @@ -256,7 +256,7 @@ output(void) { LOG_DBG("SUT: %u\n", uip_len); if(uip_len > 0) { - return tun_output(&uip_buf[UIP_LLH_LEN], uip_len); + return tun_output(uip_buf, uip_len); } return 0; } @@ -298,7 +298,7 @@ handle_fd(fd_set *rset, fd_set *wset) int size; if(FD_ISSET(tunfd, rset)) { - size = tun_input(&uip_buf[UIP_LLH_LEN], sizeof(uip_buf)); + size = tun_input(uip_buf, sizeof(uip_buf)); /* printf("TUN data incoming read:%d\n", size); */ uip_len = size; tcpip_input(); diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index e8bb4b6b2..0a48f7ce9 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -55,6 +55,9 @@ #if MAC_CONF_WITH_TSCH #include "net/mac/tsch/tsch.h" #endif /* MAC_CONF_WITH_TSCH */ +#if MAC_CONF_WITH_CSMA +#include "net/mac/csma/csma.h" +#endif #include "net/routing/routing.h" #include "net/mac/llsec802154.h" @@ -69,16 +72,19 @@ #define PING_TIMEOUT (5 * CLOCK_SECOND) +#if NETSTACK_CONF_WITH_IPV6 static struct uip_icmp6_echo_reply_notification echo_reply_notification; static shell_output_func *curr_ping_output_func = NULL; static struct process *curr_ping_process; static uint8_t curr_ping_ttl; static uint16_t curr_ping_datalen; +#endif /* NETSTACK_CONF_WITH_IPV6 */ #if TSCH_WITH_SIXTOP static shell_command_6top_sub_cmd_t sixtop_sub_cmd = NULL; #endif /* TSCH_WITH_SIXTOP */ static struct shell_command_set_t builtin_shell_command_set; LIST(shell_command_sets); +#if NETSTACK_CONF_WITH_IPV6 /*---------------------------------------------------------------------------*/ static const char * ds6_nbr_state_to_str(uint8_t state) @@ -98,6 +104,64 @@ ds6_nbr_state_to_str(uint8_t state) return "Unknown"; } } +/*---------------------------------------------------------------------------*/ +static void +echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, uint16_t datalen) +{ + if(curr_ping_output_func != NULL) { + curr_ping_output_func = NULL; + curr_ping_ttl = ttl; + curr_ping_datalen = datalen; + process_poll(curr_ping_process); + } +} +/*---------------------------------------------------------------------------*/ +static +PT_THREAD(cmd_ping(struct pt *pt, shell_output_func output, char *args)) +{ + static uip_ipaddr_t remote_addr; + static struct etimer timeout_timer; + char *next_args; + + PT_BEGIN(pt); + + SHELL_ARGS_INIT(args, next_args); + + /* Get argument (remote IPv6) */ + SHELL_ARGS_NEXT(args, next_args); + if(args == NULL) { + SHELL_OUTPUT(output, "Destination IPv6 address is not specified\n"); + PT_EXIT(pt); + } else if(uiplib_ipaddrconv(args, &remote_addr) == 0) { + SHELL_OUTPUT(output, "Invalid IPv6 address: %s\n", args); + PT_EXIT(pt); + } + + SHELL_OUTPUT(output, "Pinging "); + shell_output_6addr(output, &remote_addr); + SHELL_OUTPUT(output, "\n"); + + /* Send ping request */ + curr_ping_process = PROCESS_CURRENT(); + curr_ping_output_func = output; + etimer_set(&timeout_timer, PING_TIMEOUT); + uip_icmp6_send(&remote_addr, ICMP6_ECHO_REQUEST, 0, 4); + PT_WAIT_UNTIL(pt, curr_ping_output_func == NULL || etimer_expired(&timeout_timer)); + + if(curr_ping_output_func != NULL) { + SHELL_OUTPUT(output, "Timeout\n"); + curr_ping_output_func = NULL; + } else { + SHELL_OUTPUT(output, "Received ping reply from "); + shell_output_6addr(output, &remote_addr); + SHELL_OUTPUT(output, ", len %u, ttl %u, delay %lu ms\n", + curr_ping_datalen, curr_ping_ttl, (1000*(clock_time() - timeout_timer.timer.start))/CLOCK_SECOND); + } + + PT_END(pt); +} +#endif /* NETSTACK_CONF_WITH_IPV6 */ + #if ROUTING_CONF_RPL_LITE /*---------------------------------------------------------------------------*/ static const char * @@ -215,63 +279,18 @@ PT_THREAD(cmd_rpl_status(struct pt *pt, shell_output_func output, char *args)) PT_END(pt); } -#endif /* ROUTING_CONF_RPL_LITE */ -/*---------------------------------------------------------------------------*/ -static void -echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, uint16_t datalen) -{ - if(curr_ping_output_func != NULL) { - curr_ping_output_func = NULL; - curr_ping_ttl = ttl; - curr_ping_datalen = datalen; - process_poll(curr_ping_process); - } -} /*---------------------------------------------------------------------------*/ static -PT_THREAD(cmd_ping(struct pt *pt, shell_output_func output, char *args)) +PT_THREAD(cmd_rpl_refresh_routes(struct pt *pt, shell_output_func output, char *args)) { - static uip_ipaddr_t remote_addr; - static struct etimer timeout_timer; - char *next_args; - PT_BEGIN(pt); - SHELL_ARGS_INIT(args, next_args); - - /* Get argument (remote IPv6) */ - SHELL_ARGS_NEXT(args, next_args); - if(args == NULL) { - SHELL_OUTPUT(output, "Destination IPv6 address is not specified\n"); - PT_EXIT(pt); - } else if(uiplib_ipaddrconv(args, &remote_addr) == 0) { - SHELL_OUTPUT(output, "Invalid IPv6 address: %s\n", args); - PT_EXIT(pt); - } - - SHELL_OUTPUT(output, "Pinging "); - shell_output_6addr(output, &remote_addr); - SHELL_OUTPUT(output, "\n"); - - /* Send ping request */ - curr_ping_process = PROCESS_CURRENT(); - curr_ping_output_func = output; - etimer_set(&timeout_timer, PING_TIMEOUT); - uip_icmp6_send(&remote_addr, ICMP6_ECHO_REQUEST, 0, 4); - PT_WAIT_UNTIL(pt, curr_ping_output_func == NULL || etimer_expired(&timeout_timer)); - - if(curr_ping_output_func != NULL) { - SHELL_OUTPUT(output, "Timeout\n"); - curr_ping_output_func = NULL; - } else { - SHELL_OUTPUT(output, "Received ping reply from "); - shell_output_6addr(output, &remote_addr); - SHELL_OUTPUT(output, ", len %u, ttl %u, delay %lu ms\n", - curr_ping_datalen, curr_ping_ttl, (1000*(clock_time() - timeout_timer.timer.start))/CLOCK_SECOND); - } + SHELL_OUTPUT(output, "Triggering routes refresh\n"); + rpl_refresh_routes("Shell"); PT_END(pt); } +#endif /* ROUTING_CONF_RPL_LITE */ /*---------------------------------------------------------------------------*/ static void shell_output_log_levels(shell_output_func output) @@ -394,7 +413,8 @@ PT_THREAD(cmd_rpl_set_root(struct pt *pt, shell_output_func output, char *args)) PT_EXIT(pt); } } else { - uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&prefix, default_prefix); } if(is_on) { @@ -424,7 +444,7 @@ PT_THREAD(cmd_rpl_global_repair(struct pt *pt, shell_output_func output, char *a { PT_BEGIN(pt); - SHELL_OUTPUT(output, "Triggering routing global repair\n") + SHELL_OUTPUT(output, "Triggering routing global repair\n"); NETSTACK_ROUTING.global_repair("Shell"); PT_END(pt); @@ -440,20 +460,20 @@ PT_THREAD(cmd_rpl_local_repair(struct pt *pt, shell_output_func output, char *ar PT_END(pt); } +#endif /* UIP_CONF_IPV6_RPL */ /*---------------------------------------------------------------------------*/ -#if ROUTING_CONF_RPL_LITE static -PT_THREAD(cmd_rpl_refresh_routes(struct pt *pt, shell_output_func output, char *args)) +PT_THREAD(cmd_macaddr(struct pt *pt, shell_output_func output, char *args)) { PT_BEGIN(pt); - SHELL_OUTPUT(output, "Triggering routes refresh\n") - rpl_refresh_routes("Shell"); + SHELL_OUTPUT(output, "Node MAC address: "); + shell_output_lladdr(output, &linkaddr_node_addr); + SHELL_OUTPUT(output, "\n"); PT_END(pt); } -#endif /* ROUTING_CONF_RPL_LITE */ -#endif /* UIP_CONF_IPV6_RPL */ +#if NETSTACK_CONF_WITH_IPV6 /*---------------------------------------------------------------------------*/ static PT_THREAD(cmd_ipaddr(struct pt *pt, shell_output_func output, char *args)) @@ -475,7 +495,6 @@ PT_THREAD(cmd_ipaddr(struct pt *pt, shell_output_func output, char *args)) } PT_END(pt); - } /*---------------------------------------------------------------------------*/ static @@ -506,6 +525,7 @@ PT_THREAD(cmd_ip_neighbors(struct pt *pt, shell_output_func output, char *args)) PT_END(pt); } +#endif /* NETSTACK_CONF_WITH_IPV6 */ #if MAC_CONF_WITH_TSCH /*---------------------------------------------------------------------------*/ static @@ -581,13 +601,18 @@ PT_THREAD(cmd_tsch_status(struct pt *pt, shell_output_func output, char *args)) } else { SHELL_OUTPUT(output, "none\n"); } - SHELL_OUTPUT(output, "-- Last synchronized: %lu seconds ago\n", (clock_time() - last_sync_time) / CLOCK_SECOND); - SHELL_OUTPUT(output, "-- Drift w.r.t. coordinator: %ld ppm\n", tsch_adaptive_timesync_get_drift_ppm()); + SHELL_OUTPUT(output, "-- Last synchronized: %lu seconds ago\n", + (clock_time() - tsch_last_sync_time) / CLOCK_SECOND); + SHELL_OUTPUT(output, "-- Drift w.r.t. coordinator: %ld ppm\n", + tsch_adaptive_timesync_get_drift_ppm()); + SHELL_OUTPUT(output, "-- Network uptime: %lu seconds\n", + (unsigned long)(tsch_get_network_uptime_ticks() / CLOCK_SECOND)); } PT_END(pt); } #endif /* MAC_CONF_WITH_TSCH */ +#if NETSTACK_CONF_WITH_IPV6 /*---------------------------------------------------------------------------*/ static PT_THREAD(cmd_routes(struct pt *pt, shell_output_func output, char *args)) @@ -653,6 +678,7 @@ PT_THREAD(cmd_routes(struct pt *pt, shell_output_func output, char *args)) PT_END(pt); } +#endif /* NETSTACK_CONF_WITH_IPV6 */ /*---------------------------------------------------------------------------*/ static PT_THREAD(cmd_reboot(struct pt *pt, shell_output_func output, char *args)) @@ -729,14 +755,83 @@ PT_THREAD(cmd_6top(struct pt *pt, shell_output_func output, char *args)) } #endif /* TSCH_WITH_SIXTOP */ /*---------------------------------------------------------------------------*/ +#if LLSEC802154_ENABLED +static +PT_THREAD(cmd_llsec_setlv(struct pt *pt, shell_output_func output, char *args)) +{ + + PT_BEGIN(pt); + + if(args == NULL) { + SHELL_OUTPUT(output, "Default LLSEC level is %d\n", + uipbuf_get_attr(UIPBUF_ATTR_LLSEC_LEVEL)); + PT_EXIT(pt); + } else { + int lv = atoi(args); + if(lv < 0 || lv > 7) { + SHELL_OUTPUT(output, "Illegal LLSEC Level %d\n", lv); + PT_EXIT(pt); + } else { + uipbuf_set_default_attr(UIPBUF_ATTR_LLSEC_LEVEL, lv); + uipbuf_clear_attr(); + SHELL_OUTPUT(output, "LLSEC default level set %d\n", lv); + } + } + + PT_END(pt); +} +/*---------------------------------------------------------------------------*/ +static +PT_THREAD(cmd_llsec_setkey(struct pt *pt, shell_output_func output, char *args)) +{ + char *next_args; + + PT_BEGIN(pt); + + SHELL_ARGS_INIT(args, next_args); + + if(args == NULL) { + SHELL_OUTPUT(output, "Provide an index and a 16-char string for the key\n"); + PT_EXIT(pt); + } else { + int key; + SHELL_ARGS_NEXT(args, next_args); + key = atoi(args); + if(key < 0) { + SHELL_OUTPUT(output, "Illegal LLSEC Key index %d\n", key); + PT_EXIT(pt); + } else { +#if MAC_CONF_WITH_CSMA + /* Get next arg (key-string) */ + SHELL_ARGS_NEXT(args, next_args); + if(args == NULL) { + SHELL_OUTPUT(output, "Provide both an index and a key\n"); + } else if(strlen(args) == 16) { + csma_security_set_key(key, (const uint8_t *) args); + SHELL_OUTPUT(output, "Set key for index %d\n", key); + } else { + SHELL_OUTPUT(output, "Wrong length of key: '%s' (%d)\n", args, strlen(args)); + } +#else + SHELL_OUTPUT(output, "Set key not supported.\n"); + PT_EXIT(pt); +#endif + } + } + PT_END(pt); +} +#endif /* LLSEC802154_ENABLED */ +/*---------------------------------------------------------------------------*/ void shell_commands_init(void) { list_init(shell_command_sets); list_add(shell_command_sets, &builtin_shell_command_set); +#if NETSTACK_CONF_WITH_IPV6 /* Set up Ping Reply callback */ uip_icmp6_echo_reply_callback_add(&echo_reply_notification, echo_reply_handler); +#endif /* NETSTACK_CONF_WITH_IPV6 */ } /*---------------------------------------------------------------------------*/ void @@ -776,23 +871,24 @@ shell_command_lookup(const char *name) const struct shell_command_t builtin_shell_commands[] = { { "help", cmd_help, "'> help': Shows this help" }, { "reboot", cmd_reboot, "'> reboot': Reboot the board by watchdog_reboot()" }, + { "log", cmd_log, "'> log module level': Sets log level (0--4) for a given module (or \"all\"). For module \"mac\", level 4 also enables per-slot logging." }, + { "mac-addr", cmd_macaddr, "'> mac-addr': Shows the node's MAC address" }, +#if NETSTACK_CONF_WITH_IPV6 { "ip-addr", cmd_ipaddr, "'> ip-addr': Shows all IPv6 addresses" }, { "ip-nbr", cmd_ip_neighbors, "'> ip-nbr': Shows all IPv6 neighbors" }, - { "log", cmd_log, "'> log module level': Sets log level (0--4) for a given module (or \"all\"). For module \"mac\", level 4 also enables per-slot logging." }, { "ping", cmd_ping, "'> ping addr': Pings the IPv6 address 'addr'" }, + { "routes", cmd_routes, "'> routes': Shows the route entries" }, +#endif /* NETSTACK_CONF_WITH_IPV6 */ #if UIP_CONF_IPV6_RPL { "rpl-set-root", cmd_rpl_set_root, "'> rpl-set-root 0/1 [prefix]': Sets node as root (1) or not (0). A /64 prefix can be optionally specified." }, { "rpl-local-repair", cmd_rpl_local_repair, "'> rpl-local-repair': Triggers a RPL local repair" }, #if ROUTING_CONF_RPL_LITE { "rpl-refresh-routes", cmd_rpl_refresh_routes, "'> rpl-refresh-routes': Refreshes all routes through a DTSN increment" }, -#endif /* ROUTING_CONF_RPL_LITE */ - { "rpl-global-repair", cmd_rpl_global_repair, "'> rpl-global-repair': Triggers a RPL global repair" }, -#endif /* UIP_CONF_IPV6_RPL */ -#if ROUTING_CONF_RPL_LITE { "rpl-status", cmd_rpl_status, "'> rpl-status': Shows a summary of the current RPL state" }, { "rpl-nbr", cmd_rpl_nbr, "'> rpl-nbr': Shows the RPL neighbor table" }, #endif /* ROUTING_CONF_RPL_LITE */ - { "routes", cmd_routes, "'> routes': Shows the route entries" }, + { "rpl-global-repair", cmd_rpl_global_repair, "'> rpl-global-repair': Triggers a RPL global repair" }, +#endif /* UIP_CONF_IPV6_RPL */ #if MAC_CONF_WITH_TSCH { "tsch-set-coordinator", cmd_tsch_set_coordinator, "'> tsch-set-coordinator 0/1 [0/1]': Sets node as coordinator (1) or not (0). Second, optional parameter: enable (1) or disable (0) security." }, { "tsch-schedule", cmd_tsch_schedule, "'> tsch-schedule': Shows the current TSCH schedule" }, @@ -801,6 +897,10 @@ const struct shell_command_t builtin_shell_commands[] = { #if TSCH_WITH_SIXTOP { "6top", cmd_6top, "'> 6top help': Shows 6top command usage" }, #endif /* TSCH_WITH_SIXTOP */ +#if LLSEC802154_ENABLED + { "llsec-set-level", cmd_llsec_setlv, "'> llsec-set-level ': Set the level of link layer security (show if no lv argument)"}, + { "llsec-set-key", cmd_llsec_setkey, "'> llsec-set-key ': Set the key of link layer security"}, +#endif /* LLSEC802154_ENABLED */ { NULL, NULL, NULL }, }; diff --git a/os/services/shell/shell.c b/os/services/shell/shell.c index c219b1903..f5383ee5f 100644 --- a/os/services/shell/shell.c +++ b/os/services/shell/shell.c @@ -52,6 +52,7 @@ #include "net/ipv6/ip64-addr.h" #include "net/ipv6/uiplib.h" +#if NETSTACK_CONF_WITH_IPV6 /*---------------------------------------------------------------------------*/ void shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr) @@ -60,6 +61,7 @@ shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr) uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr); SHELL_OUTPUT(output, "%s", buf); } +#endif /* NETSTACK_CONF_WITH_IPV6 */ /*---------------------------------------------------------------------------*/ void shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr) @@ -98,19 +100,22 @@ PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd)) cmd++; } - /* Look for arguments */ - args = strchr(cmd, ' '); - if(args != NULL) { - *args = '\0'; - args++; - } + /* Skip empty lines */ + if(*cmd != '\0') { + /* Look for arguments */ + args = strchr(cmd, ' '); + if(args != NULL) { + *args = '\0'; + args++; + } - cmd_descr = shell_command_lookup(cmd); - if(cmd_descr != NULL) { - static struct pt cmd_pt; - PT_SPAWN(pt, &cmd_pt, cmd_descr->func(&cmd_pt, output, args)); - } else { - SHELL_OUTPUT(output, "Command not found. Type 'help' for a list of commands\n"); + cmd_descr = shell_command_lookup(cmd); + if(cmd_descr != NULL) { + static struct pt cmd_pt; + PT_SPAWN(pt, &cmd_pt, cmd_descr->func(&cmd_pt, output, args)); + } else { + SHELL_OUTPUT(output, "Command not found. Type 'help' for a list of commands\n"); + } } output_prompt(output); diff --git a/os/sys/ctimer.h b/os/sys/ctimer.h index 2d391a5e5..e7e463ac4 100644 --- a/os/sys/ctimer.h +++ b/os/sys/ctimer.h @@ -76,7 +76,8 @@ struct ctimer { * is the exact time that the callback timer last * expired. Therefore, this function will cause the timer * to be stable over time, unlike the ctimer_restart() - * function. + * function. If this is executed before the timer expired, + * this function has no effect. * * \sa ctimer_restart() */ diff --git a/os/sys/etimer.h b/os/sys/etimer.h index 02ad5df11..2922b9aa2 100644 --- a/os/sys/etimer.h +++ b/os/sys/etimer.h @@ -107,7 +107,8 @@ void etimer_set(struct etimer *et, clock_time_t interval); * is the exact time that the event timer last * expired. Therefore, this function will cause the timer * to be stable over time, unlike the etimer_restart() - * function. + * function. If this is executed before the timer expired, + * this function has no effect. * * \sa etimer_restart() */ diff --git a/os/sys/rtimer.h b/os/sys/rtimer.h index 44ad92a2f..260f08950 100644 --- a/os/sys/rtimer.h +++ b/os/sys/rtimer.h @@ -54,6 +54,8 @@ #define RTIMER_H_ #include "contiki.h" +#include "dev/watchdog.h" +#include /** \brief The rtimer size (in bytes) */ #ifdef RTIMER_CONF_CLOCK_SIZE @@ -186,6 +188,26 @@ void rtimer_arch_schedule(rtimer_clock_t t); #define RTIMER_GUARD_TIME (RTIMER_ARCH_SECOND >> 14) #endif /* RTIMER_CONF_GUARD_TIME */ +/** \brief Busy-wait until a condition. Start time is t0, max wait time is max_time */ +#define RTIMER_BUSYWAIT_UNTIL_ABS(cond, t0, max_time) \ + ({ \ + bool c; \ + while(!(c = cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (max_time))) { \ + watchdog_periodic(); \ + } \ + c; \ + }) + +/** \brief Busy-wait until a condition for at most max_time */ +#define RTIMER_BUSYWAIT_UNTIL(cond, max_time) \ + ({ \ + rtimer_clock_t t0 = RTIMER_NOW(); \ + RTIMER_BUSYWAIT_UNTIL_ABS(cond, t0, max_time); \ + }) + +/** \brief Busy-wait for a fixed duration */ +#define RTIMER_BUSYWAIT(duration) RTIMER_BUSYWAIT_UNTIL(0, duration) + #endif /* RTIMER_H_ */ /** @} */ diff --git a/os/sys/stimer.c b/os/sys/stimer.c index 1301dc41e..007587e83 100644 --- a/os/sys/stimer.c +++ b/os/sys/stimer.c @@ -77,7 +77,8 @@ stimer_set(struct stimer *t, unsigned long interval) * given to the stimer_set() function. The start point of the interval * is the exact time that the timer last expired. Therefore, this * function will cause the timer to be stable over time, unlike the - * stimer_restart() function. + * stimer_restart() function. If this is executed before the + * timer expired, this function has no effect. * * \param t A pointer to the timer. * @@ -86,7 +87,9 @@ stimer_set(struct stimer *t, unsigned long interval) void stimer_reset(struct stimer *t) { - t->start += t->interval; + if(stimer_expired(t)) { + t->start += t->interval; + } } /*---------------------------------------------------------------------------*/ /** diff --git a/os/sys/timer.c b/os/sys/timer.c index 1eeae64de..b99aa4127 100644 --- a/os/sys/timer.c +++ b/os/sys/timer.c @@ -74,9 +74,8 @@ timer_set(struct timer *t, clock_time_t interval) * given to the timer_set() function. The start point of the interval * is the exact time that the timer last expired. Therefore, this * function will cause the timer to be stable over time, unlike the - * timer_restart() function. - * - * \note Must not be executed before timer expired + * timer_restart() function. If this is executed before the + * timer expired, this function has no effect. * * \param t A pointer to the timer. * \sa timer_restart() @@ -84,7 +83,9 @@ timer_set(struct timer *t, clock_time_t interval) void timer_reset(struct timer *t) { - t->start += t->interval; + if(timer_expired(t)) { + t->start += t->interval; + } } /*---------------------------------------------------------------------------*/ /** diff --git a/tests/01-compile-base/Makefile b/tests/01-compile-base/Makefile index d9ef0ba58..51e68f9a2 100644 --- a/tests/01-compile-base/Makefile +++ b/tests/01-compile-base/Makefile @@ -6,6 +6,7 @@ hello-world/native \ hello-world/native:MAKE_NET=MAKE_NET_NULLNET \ hello-world/native:MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC \ hello-world/sky \ +hello-world/z1 \ storage/eeprom-test/native \ libs/logging/native \ libs/energest/native \ @@ -16,7 +17,7 @@ libs/stack-check/sky \ lwm2m-ipso-objects/native \ lwm2m-ipso-objects/native:MAKE_WITH_DTLS=1 \ lwm2m-ipso-objects/native:DEFINES=LWM2M_Q_MODE_CONF_ENABLED=1 \ -lwm2m-ipso-objects/native:DEFINES=LWM2M_Q_MODE_CONF_ENABLED=1,LWM2M_Q_MODE_CONF_INCLUDE_DYNAMIC_ADAPTATION=1\ +lwm2m-ipso-objects/native:DEFINES=LWM2M_Q_MODE_CONF_ENABLED=1,LWM2M_Q_MODE_CONF_INCLUDE_DYNAMIC_ADAPTATION=1 \ rpl-udp/sky \ rpl-border-router/native \ rpl-border-router/native:MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC \ @@ -24,10 +25,13 @@ rpl-border-router/sky \ slip-radio/sky \ libs/ipv6-hooks/sky \ nullnet/native \ +nullnet/sky \ +nullnet/sky:MAKE_MAC=MAKE_MAC_TSCH \ mqtt-client/native \ coap/coap-example-client/native \ coap/coap-example-server/native \ coap/coap-plugtest-server/native \ +sensniff/z1 \ TOOLS= diff --git a/tests/02-compile-arm-ports-01/Makefile b/tests/02-compile-arm-ports-01/Makefile index b3a3abdda..43e008c8e 100644 --- a/tests/02-compile-arm-ports-01/Makefile +++ b/tests/02-compile-arm-ports-01/Makefile @@ -2,41 +2,41 @@ EXAMPLESDIR=../../examples TOOLSDIR=../../tools EXAMPLES = \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC \ -platform-specific/cc26xx/very-sleepy-demo/srf06-cc26xx:BOARD=sensortag/cc2650 \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:BOARD=sensortag/cc2650 \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:BOARD=sensortag/cc1350 \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:BOARD=srf06/cc13xx \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:BOARD=launchpad/cc2650 \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:BOARD=launchpad/cc1310 \ -platform-specific/cc26xx/cc26xx-web-demo/srf06-cc26xx:BOARD=launchpad/cc1350 \ -platform-specific/cc26xx/very-sleepy-demo/srf06-cc26xx \ -platform-specific/cc26xx/ble-ipv6/srf06-cc26xx:BOARD=sensortag/cc2650 \ -nullnet/srf06-cc26xx:BOARD=sensortag/cc2650 \ -rpl-border-router/srf06-cc26xx:BOARD=launchpad/cc2650 \ -sensniff/srf06-cc26xx \ -sensniff/srf06-cc26xx:BOARD=launchpad/cc1310 \ -dev/gpio-hal/srf06-cc26xx:BOARD=srf06/cc13xx \ -dev/gpio-hal/srf06-cc26xx:BOARD=srf06/cc26xx \ -dev/gpio-hal/srf06-cc26xx:BOARD=sensortag/cc1350 \ -dev/gpio-hal/srf06-cc26xx:BOARD=sensortag/cc2650 \ -dev/gpio-hal/srf06-cc26xx:BOARD=launchpad/cc1310 \ -dev/gpio-hal/srf06-cc26xx:BOARD=launchpad/cc1350 \ -dev/gpio-hal/srf06-cc26xx:BOARD=launchpad/cc2650 \ -dev/gpio-hal/srf06-cc26xx:BOARD=launchpad/cc2640r2 \ -dev/leds/srf06-cc26xx:BOARD=srf06/cc13xx \ -dev/leds/srf06-cc26xx:BOARD=srf06/cc26xx \ -dev/leds/srf06-cc26xx:BOARD=sensortag/cc1350 \ -dev/leds/srf06-cc26xx:BOARD=sensortag/cc2650 \ -dev/leds/srf06-cc26xx:BOARD=launchpad/cc1310 \ -dev/leds/srf06-cc26xx:BOARD=launchpad/cc1350 \ -dev/leds/srf06-cc26xx:BOARD=launchpad/cc2650 \ -dev/leds/srf06-cc26xx:BOARD=launchpad/cc2640r2 \ -6tisch/etsi-plugtest-2017/srf06-cc26xx:BOARD=launchpad/cc2650 \ -mqtt-client/srf06-cc26xx:BOARD=srf06/cc26xx \ -mqtt-client/srf06-cc26xx:BOARD=launchpad/cc2650 \ -mqtt-client/srf06-cc26xx:BOARD=sensortag/cc2650 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC \ +platform-specific/cc26x0-cc13x0/very-sleepy-demo/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:BOARD=sensortag/cc1350 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:BOARD=srf06/cc13x0 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:BOARD=launchpad/cc2650 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:BOARD=launchpad/cc1310 \ +platform-specific/cc26x0-cc13x0/cc26x0-web-demo/cc26x0-cc13x0:BOARD=launchpad/cc1350 \ +platform-specific/cc26x0-cc13x0/very-sleepy-demo/cc26x0-cc13x0 \ +platform-specific/cc26x0-cc13x0/ble-ipv6/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ +nullnet/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ +rpl-border-router/cc26x0-cc13x0:BOARD=launchpad/cc2650 \ +sensniff/cc26x0-cc13x0 \ +sensniff/cc26x0-cc13x0:BOARD=launchpad/cc1310 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=srf06/cc13x0 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=srf06/cc26x0 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=sensortag/cc1350 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=launchpad/cc1310 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=launchpad/cc1350 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=launchpad/cc2650 \ +dev/gpio-hal/cc26x0-cc13x0:BOARD=launchpad/cc2640r2 \ +dev/leds/cc26x0-cc13x0:BOARD=srf06/cc13x0 \ +dev/leds/cc26x0-cc13x0:BOARD=srf06/cc26x0 \ +dev/leds/cc26x0-cc13x0:BOARD=sensortag/cc1350 \ +dev/leds/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ +dev/leds/cc26x0-cc13x0:BOARD=launchpad/cc1310 \ +dev/leds/cc26x0-cc13x0:BOARD=launchpad/cc1350 \ +dev/leds/cc26x0-cc13x0:BOARD=launchpad/cc2650 \ +dev/leds/cc26x0-cc13x0:BOARD=launchpad/cc2640r2 \ +6tisch/etsi-plugtest-2017/cc26x0-cc13x0:BOARD=launchpad/cc2650 \ +mqtt-client/cc26x0-cc13x0:BOARD=srf06/cc26x0 \ +mqtt-client/cc26x0-cc13x0:BOARD=launchpad/cc2650 \ +mqtt-client/cc26x0-cc13x0:BOARD=sensortag/cc2650 \ hello-world/simplelink:BOARD=launchpad/cc26x2r1 \ hello-world/simplelink:BOARD=sensortag/cc2650 \ nullnet/simplelink:BOARD=sensortag/cc2650 \ @@ -80,7 +80,6 @@ coap/coap-example-server/cc2538dk \ slip-radio/cc2538dk \ lwm2m-ipso-objects/cc2538dk \ lwm2m-ipso-objects/cc2538dk:DEFINES=LWM2M_Q_MODE_CONF_ENABLED=1 \ -lwm2m-ipso-objects/native:DEFINES=LWM2M_Q_MODE_CONF_ENABLED=1,LWM2M_Q_MODE_CONF_INCLUDE_DYNAMIC_ADAPTATION=1\ multicast/cc2538dk \ dev/gpio-hal/cc2538dk \ dev/leds/cc2538dk \ @@ -90,6 +89,8 @@ platform-specific/cc2538-common/pka/cc2538dk \ hello-world/cc2538dk \ rpl-border-router/cc2538dk \ rpl-border-router/cc2538dk:MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC \ +6tisch/simple-node/cc2538dk \ +6tisch/simple-node/cc2538dk:MAKE_WITH_SECURITY=1,MAKE_WITH_ORCHESTRA=1 \ hello-world/nrf52dk \ platform-specific/nrf52dk/coap-demo/coap-server/nrf52dk \ platform-specific/nrf52dk/coap-demo/coap-client/nrf52dk:SERVER_IPV6_EP=ffff \ diff --git a/tests/03-compile-arm-ports-02/Makefile b/tests/03-compile-arm-ports-02/Makefile index 190d36e4d..655967e1b 100644 --- a/tests/03-compile-arm-ports-02/Makefile +++ b/tests/03-compile-arm-ports-02/Makefile @@ -28,6 +28,7 @@ storage/antelope-shell/zoul \ 6tisch/simple-node/zoul:MAKE_WITH_ORCHESTRA=1 \ 6tisch/simple-node/zoul:MAKE_WITH_SECURITY=1 \ libs/logging/zoul \ +libs/logging/zoul:MAKE_MAC=MAKE_MAC_TSCH \ 6tisch/etsi-plugtest-2017/zoul:BOARD=remote \ 6tisch/6p-packet/zoul \ 6tisch/sixtop/zoul \ @@ -68,6 +69,8 @@ libs/ipv6-hooks/openmote-cc2538 \ libs/shell/openmote-cc2538 \ libs/simple-energest/openmote-cc2538 \ libs/deployment/openmote-cc2538 \ +6tisch/simple-node/openmote-cc2538 \ +6tisch/simple-node/openmote-cc2538:MAKE_WITH_SECURITY=1,MAKE_WITH_ORCHESTRA=1 \ TOOLS= diff --git a/tests/07-simulation-base/07-hello-world-z1.csc b/tests/07-simulation-base/07-hello-world-z1.csc new file mode 100644 index 000000000..33963c36c --- /dev/null +++ b/tests/07-simulation-base/07-hello-world-z1.csc @@ -0,0 +1,83 @@ + + + + Hello World (Z1) + generated + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.mspmote.Z1MoteType + z11 + Z1 Mote Type #z11 + [CONTIKI_DIR]/examples/hello-world/hello-world.c + make hello-world.z1 TARGET=z1 + [CONTIKI_DIR]/examples/hello-world/hello-world.z1 + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.interfaces.IPAddress + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + org.contikios.cooja.mspmote.interfaces.MspClock + org.contikios.cooja.mspmote.interfaces.MspMoteID + org.contikios.cooja.mspmote.interfaces.Msp802154Radio + org.contikios.cooja.mspmote.interfaces.MspDefaultSerial + org.contikios.cooja.mspmote.interfaces.MspLED + org.contikios.cooja.mspmote.interfaces.MspDebugOutput + + + + + org.contikios.cooja.interfaces.Position + 94.96401380574989 + 21.247662337471553 + 0.0 + + + org.contikios.cooja.mspmote.interfaces.MspMoteID + 1 + + z11 + + + + org.contikios.cooja.plugins.SimControl + 280 + 2 + 160 + 38 + 13 + + + org.contikios.cooja.plugins.LogListener + + + + 680 + 1 + 240 + 109 + 377 + + + org.contikios.cooja.plugins.ScriptRunner + + [CONFIG_DIR]/hello-world.js + true + + 600 + 0 + 700 + 330 + 24 + + + diff --git a/tests/07-simulation-base/23-rpl-tsch-z1.csc b/tests/07-simulation-base/23-rpl-tsch-z1.csc new file mode 100644 index 000000000..170300f55 --- /dev/null +++ b/tests/07-simulation-base/23-rpl-tsch-z1.csc @@ -0,0 +1,191 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + RPL+TSCH (Z1) + 1 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.mspmote.Z1MoteType + z11 + Z1 Mote Type #z11 + [CONTIKI_DIR]/examples/6tisch/simple-node/node.c + make TARGET=z1 clean + make -j node.z1 TARGET=z1 MAKE_WITH_ORCHESTRA=0 MAKE_WITH_SECURITY=0 MAKE_WITH_PERIODIC_ROUTES_PRINT=1 + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.interfaces.IPAddress + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + org.contikios.cooja.mspmote.interfaces.MspClock + org.contikios.cooja.mspmote.interfaces.MspMoteID + org.contikios.cooja.mspmote.interfaces.Msp802154Radio + org.contikios.cooja.mspmote.interfaces.MspDefaultSerial + org.contikios.cooja.mspmote.interfaces.MspLED + org.contikios.cooja.mspmote.interfaces.MspDebugOutput + + + + + org.contikios.cooja.interfaces.Position + -1.285769821276336 + 38.58045647334346 + 0.0 + + + org.contikios.cooja.mspmote.interfaces.MspMoteID + 1 + + z11 + + + + + org.contikios.cooja.interfaces.Position + -19.324109516886306 + 76.23135780254927 + 0.0 + + + org.contikios.cooja.mspmote.interfaces.MspMoteID + 2 + + z11 + + + + + org.contikios.cooja.interfaces.Position + 5.815501305791592 + 76.77463755494317 + 0.0 + + + org.contikios.cooja.mspmote.interfaces.MspMoteID + 3 + + z11 + + + + + org.contikios.cooja.interfaces.Position + 31.920697784030082 + 50.5212265977149 + 0.0 + + + org.contikios.cooja.mspmote.interfaces.MspMoteID + 4 + + z11 + + + + + org.contikios.cooja.interfaces.Position + 47.21747673247198 + 30.217765340599726 + 0.0 + + + org.contikios.cooja.mspmote.interfaces.MspMoteID + 5 + + z11 + + + + org.contikios.cooja.plugins.SimControl + 242 + 4 + 160 + 11 + 241 + + + org.contikios.cooja.plugins.Visualizer + + true + org.contikios.cooja.plugins.skins.IDVisualizerSkin + org.contikios.cooja.plugins.skins.GridVisualizerSkin + org.contikios.cooja.plugins.skins.TrafficVisualizerSkin + org.contikios.cooja.plugins.skins.UDGMVisualizerSkin + 1.7405603810040515 0.0 0.0 1.7405603810040515 47.95980153208088 -42.576134155447555 + + 236 + 3 + 230 + 1 + 1 + + + org.contikios.cooja.plugins.LogListener + + ID:1 + + + + 1031 + 0 + 394 + 273 + 6 + + + org.contikios.cooja.plugins.TimeLine + + 0 + 1 + 2 + 3 + 4 + + + + 16529.88882215865 + + 1304 + 2 + 311 + 0 + 412 + + + org.contikios.cooja.plugins.ScriptRunner + + + true + + 764 + 1 + 995 + 963 + 111 + + diff --git a/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c b/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c index e84c57637..0a3b16b15 100644 --- a/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c +++ b/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c @@ -36,10 +36,13 @@ PROCESS_THREAD(udp_process, ev, data) static struct etimer send_timer; uip_ipaddr_t addr; static int alive; + const uip_ipaddr_t *default_prefix; PROCESS_BEGIN(); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 2); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + addr.u16[7] = UIP_HTONS(2); uip_ds6_addr_add(&addr, 0, ADDR_AUTOCONF); simple_udp_register(&broadcast_connection, UDP_PORT, diff --git a/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c b/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c index 4e2e664b3..e56daa342 100644 --- a/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c +++ b/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c @@ -51,12 +51,15 @@ PROCESS_THREAD(udp_process, ev, data) etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { + const uip_ipaddr_t *default_prefix; uint8_t buf[SIZE]; PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); printf("Sending unicast\n"); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 2); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + addr.u16[7] = UIP_HTONS(2); simple_udp_sendto(&broadcast_connection, buf, sizeof(buf), &addr); } diff --git a/tests/07-simulation-base/hello-world.js b/tests/07-simulation-base/hello-world.js new file mode 100644 index 000000000..5afaabe18 --- /dev/null +++ b/tests/07-simulation-base/hello-world.js @@ -0,0 +1,9 @@ +TIMEOUT(5000); + +while(true) { + log.log("> " + msg + "\n"); + if (msg.equals('Hello, world')) { + log.testOK(); + } + YIELD(); +} diff --git a/tests/08-native-runs/02-mqtt-client.sh b/tests/08-native-runs/02-mqtt-client.sh new file mode 100755 index 000000000..cb1ee937a --- /dev/null +++ b/tests/08-native-runs/02-mqtt-client.sh @@ -0,0 +1,79 @@ +#!/bin/bash +source ../utils.sh + +# Contiki directory +CONTIKI=$1 + +# Example code directory +CODE_DIR=$CONTIKI/examples/mqtt-client/ +CODE=mqtt-client + +CLIENT_LOG=$CODE.log +CLIENT_TESTLOG=$CODE.testlog +CLIENT_ERR=$CODE.err +MOSQ_SUB_LOG=mosquitto_sub.log +MOSQ_SUB_ERR=mosquitto_sub.err + +# Start mosquitto server +echo "Starting mosquitto daemon" +mosquitto &> /dev/null & +MOSQID=$! +sleep 2 + +# Start mosquitto_sub client. Subscribe +echo "Starting mosquitto subscriber" +mosquitto_sub -t iot-2/evt/status/fmt/json > $MOSQ_SUB_LOG 2> $MOSQ_SUB_ERR & +MSUBID=$! +sleep 2 + +# Starting Contiki-NG native node +echo "Starting native node" +make -C $CODE_DIR TARGET=native \ + DEFINES=MQTT_CLIENT_CONF_ORG_ID=\\\"travis-test\\\",MQTT_CLIENT_CONF_LOG_LEVEL=LOG_LEVEL_DBG \ + > make.log 2> make.err +sudo $CODE_DIR/$CODE.native > $CLIENT_LOG 2> $CLIENT_ERR & +CPID=$! + +# The mqtt-client will publish every 30 secs. Wait for 45 +sleep 45 + +# Send a publish to the mqtt client +mosquitto_pub -m "1" -t iot-2/cmd/leds/fmt/json + +echo "Closing native node" +sleep 2 +kill_bg $CPID + +echo "Stopping mosquitto daemon" +kill_bg $MOSQID + +echo "Stopping mosquitto subscriber" +kill_bg $MSUBID + +# Success criteria: +# * mosquitto_sub output not empty +# * mqtt-client.native output contains "MQTT SUB" +SUB_RCV=`grep "MQTT SUB" $CLIENT_LOG` +if [ -s "$MOSQ_SUB_LOG" -a -n "$SUB_RCV" ] +then + cp $CLIENT_LOG $CODE.testlog + printf "%-32s TEST OK\n" "$CODE" | tee $CODE.testlog; +else + echo "==== make.log ====" ; cat make.log; + echo "==== make.err ====" ; cat make.err; + echo "==== $CLIENT_LOG ====" ; cat $CLIENT_LOG; + echo "==== $CLIENT_ERR ====" ; cat $CLIENT_ERR; + echo "==== $MOSQ_SUB_LOG ====" ; cat $MOSQ_SUB_LOG; + echo "==== $MOSQ_SUB_ERR ====" ; cat $MOSQ_SUB_ERR; + + printf "%-32s TEST FAIL\n" "$CODE" | tee $CODE.testlog; +fi + +rm make.log +rm make.err +rm $CLIENT_LOG $CLIENT_ERR +rm $MOSQ_SUB_LOG $MOSQ_SUB_ERR + +# We do not want Make to stop -> Return 0 +# The Makefile will check if a log contains FAIL at the end +exit 0 diff --git a/tests/09-ipv6/09-ping-lla-ula-csma-w-rpl.csc b/tests/09-ipv6/09-ping-lla-ula-csma-w-rpl.csc new file mode 100644 index 000000000..66e2a8c9a --- /dev/null +++ b/tests/09-ipv6/09-ping-lla-ula-csma-w-rpl.csc @@ -0,0 +1,108 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + My simulation + 1.0 + 123456 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.contikimote.ContikiMoteType + mtype787 + Cooja Mote Type #1 + [CONTIKI_DIR]/tests/09-ipv6/code/node.c + make clean + make WITH_ULA=1 WITH_CSMA=1 node.cooja + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.Battery + org.contikios.cooja.contikimote.interfaces.ContikiVib + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + org.contikios.cooja.contikimote.interfaces.ContikiBeeper + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.contikimote.interfaces.ContikiIPAddress + org.contikios.cooja.contikimote.interfaces.ContikiRadio + org.contikios.cooja.contikimote.interfaces.ContikiButton + org.contikios.cooja.contikimote.interfaces.ContikiPIR + org.contikios.cooja.contikimote.interfaces.ContikiClock + org.contikios.cooja.contikimote.interfaces.ContikiLED + org.contikios.cooja.contikimote.interfaces.ContikiCFS + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + false + + + + org.contikios.cooja.interfaces.Position + 14.03051207883138 + 82.02801380504546 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 1 + + + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + ip-nbr~;ping fe80::202:2:2:2~;ip-nbr~;help~;je;p~;ping da~;ping~;help~; + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.interfaces.Position + 28.22612889898729 + 43.60027658221718 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 2 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.plugins.ScriptRunner + + [CONTIKI_DIR]/tests/09-ipv6/js/ping-test-lla-ula.js + true + + 495 + 0 + 525 + 190 + 18 + + diff --git a/tests/09-ipv6/10-ping-lla-ula-tsch-w-rpl.csc b/tests/09-ipv6/10-ping-lla-ula-tsch-w-rpl.csc new file mode 100644 index 000000000..e6957af28 --- /dev/null +++ b/tests/09-ipv6/10-ping-lla-ula-tsch-w-rpl.csc @@ -0,0 +1,108 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + My simulation + 1.0 + 123456 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.contikimote.ContikiMoteType + mtype787 + Cooja Mote Type #1 + [CONTIKI_DIR]/tests/09-ipv6/code/node.c + make clean + make WITH_ULA=1 WITH_TSCH=1 node.cooja + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.Battery + org.contikios.cooja.contikimote.interfaces.ContikiVib + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + org.contikios.cooja.contikimote.interfaces.ContikiBeeper + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.contikimote.interfaces.ContikiIPAddress + org.contikios.cooja.contikimote.interfaces.ContikiRadio + org.contikios.cooja.contikimote.interfaces.ContikiButton + org.contikios.cooja.contikimote.interfaces.ContikiPIR + org.contikios.cooja.contikimote.interfaces.ContikiClock + org.contikios.cooja.contikimote.interfaces.ContikiLED + org.contikios.cooja.contikimote.interfaces.ContikiCFS + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + false + + + + org.contikios.cooja.interfaces.Position + 14.03051207883138 + 82.02801380504546 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 1 + + + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + ip-nbr~;ping fe80::202:2:2:2~;ip-nbr~;help~;je;p~;ping da~;ping~;help~; + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.interfaces.Position + 28.22612889898729 + 43.60027658221718 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 2 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.plugins.ScriptRunner + + [CONTIKI_DIR]/tests/09-ipv6/js/ping-test-lla-ula.js + true + + 495 + 0 + 525 + 190 + 18 + + diff --git a/tests/09-ipv6/11-ping-lla-ula-csma-wo-rpl.csc b/tests/09-ipv6/11-ping-lla-ula-csma-wo-rpl.csc new file mode 100644 index 000000000..b95a1ff4b --- /dev/null +++ b/tests/09-ipv6/11-ping-lla-ula-csma-wo-rpl.csc @@ -0,0 +1,108 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + My simulation + 1.0 + 123456 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.contikimote.ContikiMoteType + mtype787 + Cooja Mote Type #1 + [CONTIKI_DIR]/tests/09-ipv6/code/node.c + make clean + make WITH_NBR_MULTI_IPV6_ADDRS=1 WITH_ULA=1 WITH_CSMA=1 WITHOUT_RPL=1 node.cooja + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.Battery + org.contikios.cooja.contikimote.interfaces.ContikiVib + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + org.contikios.cooja.contikimote.interfaces.ContikiBeeper + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.contikimote.interfaces.ContikiIPAddress + org.contikios.cooja.contikimote.interfaces.ContikiRadio + org.contikios.cooja.contikimote.interfaces.ContikiButton + org.contikios.cooja.contikimote.interfaces.ContikiPIR + org.contikios.cooja.contikimote.interfaces.ContikiClock + org.contikios.cooja.contikimote.interfaces.ContikiLED + org.contikios.cooja.contikimote.interfaces.ContikiCFS + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + false + + + + org.contikios.cooja.interfaces.Position + 14.03051207883138 + 82.02801380504546 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 1 + + + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + ip-nbr~;ping fe80::202:2:2:2~;ip-nbr~;help~;je;p~;ping da~;ping~;help~; + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.interfaces.Position + 28.22612889898729 + 43.60027658221718 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 2 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.plugins.ScriptRunner + + [CONTIKI_DIR]/tests/09-ipv6/js/ping-test-lla-ula.js + true + + 495 + 0 + 525 + 190 + 18 + + diff --git a/tests/09-ipv6/12-ping-lla-ula-tsch-wo-rpl.csc b/tests/09-ipv6/12-ping-lla-ula-tsch-wo-rpl.csc new file mode 100644 index 000000000..20f8a12ae --- /dev/null +++ b/tests/09-ipv6/12-ping-lla-ula-tsch-wo-rpl.csc @@ -0,0 +1,108 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + My simulation + 1.0 + 123456 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.contikimote.ContikiMoteType + mtype787 + Cooja Mote Type #1 + [CONTIKI_DIR]/tests/09-ipv6/code/node.c + make clean + make WITH_NBR_MULTI_IPV6_ADDRS=1 WITH_ULA=1 WITH_TSCH=1 WITHOUT_RPL=1 node.cooja + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.Battery + org.contikios.cooja.contikimote.interfaces.ContikiVib + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + org.contikios.cooja.contikimote.interfaces.ContikiBeeper + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.contikimote.interfaces.ContikiIPAddress + org.contikios.cooja.contikimote.interfaces.ContikiRadio + org.contikios.cooja.contikimote.interfaces.ContikiButton + org.contikios.cooja.contikimote.interfaces.ContikiPIR + org.contikios.cooja.contikimote.interfaces.ContikiClock + org.contikios.cooja.contikimote.interfaces.ContikiLED + org.contikios.cooja.contikimote.interfaces.ContikiCFS + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + false + + + + org.contikios.cooja.interfaces.Position + 14.03051207883138 + 82.02801380504546 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 1 + + + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + ip-nbr~;ping fe80::202:2:2:2~;ip-nbr~;help~;je;p~;ping da~;ping~;help~; + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.interfaces.Position + 28.22612889898729 + 43.60027658221718 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 2 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype787 + + + + org.contikios.cooja.plugins.ScriptRunner + + [CONTIKI_DIR]/tests/09-ipv6/js/ping-test-lla-ula.js + true + + 495 + 0 + 525 + 190 + 18 + + diff --git a/tests/09-ipv6/code/Makefile b/tests/09-ipv6/code/Makefile index 2c910f1cd..d8f4cab55 100644 --- a/tests/09-ipv6/code/Makefile +++ b/tests/09-ipv6/code/Makefile @@ -28,6 +28,10 @@ ifeq ($(WITH_TSCH),1) CFLAGS += -DTSCH_CONF_DEFAULT_HOPPING_SEQUENCE=TSCH_HOPPING_SEQUENCE_1_1 endif +ifeq ($(WITH_NBR_MULTI_IPV6_ADDRS),1) + CFLAGS += -DUIP_DS6_NBR_CONF_MULTI_IPV6_ADDRS=1 +endif + PLATFORM_ONLY = cooja TARGET = cooja diff --git a/tests/09-ipv6/code/node.c b/tests/09-ipv6/code/node.c index f465e2642..a99261865 100644 --- a/tests/09-ipv6/code/node.c +++ b/tests/09-ipv6/code/node.c @@ -44,6 +44,7 @@ PROCESS_THREAD(node_process, ev, data) static struct etimer et; #if WITH_ULA static uip_ipaddr_t ipaddr; + const uip_ipaddr_t *default_prefix; #endif /* WITH_ULA */ #if WITH_TSCH @@ -60,7 +61,8 @@ PROCESS_THREAD(node_process, ev, data) #endif /* WITH_TSCH */ #if WITH_ULA - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); diff --git a/tests/09-ipv6/js/ping-test-lla-ula.js b/tests/09-ipv6/js/ping-test-lla-ula.js new file mode 100644 index 000000000..c15dcbf15 --- /dev/null +++ b/tests/09-ipv6/js/ping-test-lla-ula.js @@ -0,0 +1,72 @@ +TIMEOUT(20000, log.testFailed()); + +dst_lla = "fe80::202:2:2:2"; +dst_ula = "fd00::202:2:2:2"; +dst_mac = "0002.0002.0002.0002"; +step = 0; +rpl_is_enabled = false; + +while(1) { + YIELD(); + log.log(time + " " + id + " "+ msg + "\n"); + + if(msg.contains("Node ID: ")) { + if(id == 1) { + write(sim.getMoteWithID(1), "rpl-set-root 1"); + } + step += 1; + } + + if(msg.contains("Setting as DAG root")) { + rpl_is_enabled = true; + } + + if(step == 2 && time > 15000000) { + write(sim.getMoteWithID(1), "ping " + dst_lla); + step += 1; + } + + if(step == 4 && time > 15000000) { + write(sim.getMoteWithID(1), "ping " + dst_ula); + step += 1; + } + + if(msg.contains("Received ping reply")) { + if(step == 3) { + step += 1; + } else { + step += 1; + write(sim.getMoteWithID(1), "ip-nbr"); + } + } + + if(step == 6 && rpl_is_enabled) { + /* when RPL is enabled, we skip examining ip-nbr results */ + log.testOK(); + } + + if(msg.contains("<->")) { + re = /-- | <-> |, router|, state /; + nc = msg.split(re); + ip_addr = nc[1]; + ll_addr = nc[2]; + is_router = nc[3]; + state = nc[4].trim(); + if(ll_addr == dst_mac && + state == "Reachable") { + if(step == 6 && ip_addr == dst_lla) { + step += 1; + } else if(step == 7 && ip_addr == dst_ula) { + log.testOK(); + } else { + /* unexpected case */ + log.testFailed(); + } + } else { + log.log(ip_addr + "\n"); + log.log(ll_addr + "\n"); + log.log(state + "\n"); + log.testFailed(); + } + } +} diff --git a/tests/10-ipv6-nbr/01-test-nbr-multi-addrs.sh b/tests/10-ipv6-nbr/01-test-nbr-multi-addrs.sh new file mode 100755 index 000000000..8f73ee237 --- /dev/null +++ b/tests/10-ipv6-nbr/01-test-nbr-multi-addrs.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +TEST_NAME=01-test-nbr-multi-addrs + +if [ $# -eq 1 ]; then + # a (relative) path to CONTIKI_DIR is supposed to be given as $1 + TEST_DIR=$1/tests/10-ipv6-nbr +else + TEST_DIR=.//tests/10-ipv6-nbr +fi +SRC_DIR=${TEST_DIR}/nbr-multi-addrs +EXEC_FILE_NAME=test.native + +make -C ${SRC_DIR} clean + +echo "build the test program"... +make -C ${SRC_DIR} > ${TEST_NAME}.log + +echo "run the test..." +${TEST_DIR}/${SRC_DIR}/${EXEC_FILE_NAME} | tee ${TEST_NAME}.log | \ + grep -vE '^\[' >> ${TEST_NAME}.testlog diff --git a/tests/10-ipv6-nbr/Makefile b/tests/10-ipv6-nbr/Makefile new file mode 100644 index 000000000..c46e5271d --- /dev/null +++ b/tests/10-ipv6-nbr/Makefile @@ -0,0 +1 @@ +include ../Makefile.script-test diff --git a/tests/10-ipv6-nbr/nbr-multi-addrs/Makefile b/tests/10-ipv6-nbr/nbr-multi-addrs/Makefile new file mode 100644 index 000000000..711bf0987 --- /dev/null +++ b/tests/10-ipv6-nbr/nbr-multi-addrs/Makefile @@ -0,0 +1,14 @@ +CONTIKI_PROJECT = test +all: $(CONTIKI_PROJECT) + +CFLAGS += -DUNIT_TEST_PRINT_FUNCTION=my_test_print +CFLAGS += -DLOG_CONF_LEVEL_IPV6=LOG_LEVEL_DBG +CFLAGS += -DNBR_TABLE_FIND_REMOVABLE=my_always_return_null +CFLAGS += -DUIP_DS6_NBR_CONF_MULTI_IPV6_ADDRS=1 + +PLATFORM_ONLY = native +TARGET = native +MODULES += os/sys/log os/services/unit-test + +CONTIKI = ../../../ +include $(CONTIKI)/Makefile.include diff --git a/tests/10-ipv6-nbr/nbr-multi-addrs/test.c b/tests/10-ipv6-nbr/nbr-multi-addrs/test.c new file mode 100644 index 000000000..2cefd7cca --- /dev/null +++ b/tests/10-ipv6-nbr/nbr-multi-addrs/test.c @@ -0,0 +1,260 @@ +/* + * Copyright (c) 2018, Yasuyuki Tanaka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include + +#define LOG_MODULE "test" +#define LOG_LEVEL LOG_LEVEL_DBG + +/* report function defined in unit-test.c */ +void unit_test_print_report(const unit_test_t *utp); + +static const uint8_t is_router = 1; +static const uint8_t state = NBR_INCOMPLETE; +static const nbr_table_reason_t reason = NBR_TABLE_REASON_UNDEFINED; + +static void remove_all_entries_in_neighbor_cache(void); + +PROCESS(node_process, "Node"); +AUTOSTART_PROCESSES(&node_process); + +void +my_test_print(const unit_test_t *utp) +{ + unit_test_print_report(utp); + if(utp->result == unit_test_failure) { + printf("\nTEST FAILED\n"); + exit(1); /* exit by failure */ + } +} + +/* my_always_return_null() is set to NBR_TABLE_FIND_REMOVABLE */ +const linkaddr_t * +my_always_return_null(nbr_table_reason_t reason, void *data) +{ + return NULL; +} + +void +remove_all_entries_in_neighbor_cache(void) +{ + uip_ds6_nbr_t *nbr, *next_nbr; + for(nbr = uip_ds6_nbr_head(); nbr != NULL; nbr = next_nbr) { + next_nbr = uip_ds6_nbr_next(nbr); + uip_ds6_nbr_rm(nbr); + } + /* + * uip_ds6_nbr_rm() cannot free the memory for an entry in nbr-table. There is + * no API to free or deallocate unused nbr-table entry. Because of that, + * nbr-table has some link-layer addresses even though this function removes + * all the neighbor cache entries. + */ +} + +UNIT_TEST_REGISTER(add_v6addrs_to_neighbor, + "add IPv6 addresses to a single neighbor"); +UNIT_TEST(add_v6addrs_to_neighbor) +{ + uip_ipaddr_t ipaddr; + uip_lladdr_t lladdr; + uip_ds6_nbr_t *nbr; + const uip_lladdr_t *ret_lladdr; + + memset(&ipaddr, 0, sizeof(ipaddr)); + memset(&lladdr, 0, sizeof(lladdr)); + + UNIT_TEST_BEGIN(); + + /* make sure the neighbor cache table is empty */ + remove_all_entries_in_neighbor_cache(); + UNIT_TEST_ASSERT(uip_ds6_nbr_head() == NULL); + UNIT_TEST_ASSERT(uip_ds6_nbr_num() == 0); + + /* prepare a link-layer address */ + LOG_DBG("link-layer addr: "); + LOG_DBG_LLADDR((const linkaddr_t *)&lladdr); + LOG_DBG_("\n"); + + for(int i = 0; i <= UIP_DS6_NBR_MAX_6ADDRS_PER_NBR; i++) { + ipaddr.u8[0] = i; + LOG_DBG("adding ipv6 addr ("); + LOG_DBG_6ADDR(&ipaddr); + LOG_DBG_("[i=%u])\n", i); + + /* add a binding of the IPv6 address and the MAC address */ + nbr = uip_ds6_nbr_add(&ipaddr, &lladdr, is_router, state, reason, NULL); + if(i < UIP_DS6_NBR_MAX_6ADDRS_PER_NBR) { + UNIT_TEST_ASSERT(nbr != NULL); + UNIT_TEST_ASSERT(memcmp(&nbr->ipaddr, &ipaddr, sizeof(ipaddr)) == 0); + UNIT_TEST_ASSERT(nbr->state == state); + UNIT_TEST_ASSERT(uip_ds6_nbr_num() == (i + 1)); + /* + * for some reason, nbr->isrouter is not set if both UIP_ND6_SEND_RA and + * !UIP_CONF_ROUTER is 0 (see uip-ds6-nbr.c) + */ + // UNIT_TEST_ASSERT(nbr->isrouter == is_router); + ret_lladdr = uip_ds6_nbr_lladdr_from_ipaddr((const uip_ipaddr_t *)&ipaddr); + UNIT_TEST_ASSERT(ret_lladdr != NULL); + UNIT_TEST_ASSERT(memcmp(ret_lladdr, &lladdr, sizeof(lladdr)) == 0); + + } else { + /* i == UIP_DS6_NBR_MAX_6ADDRS_PER_NBR; the address shouldn't be added */ + UNIT_TEST_ASSERT(nbr == NULL); + } + } + + UNIT_TEST_END(); +} + +UNIT_TEST_REGISTER(remove_v6addrs_of_neighbor, + "remove IPv6 addresses associated with a single neighbor"); +UNIT_TEST(remove_v6addrs_of_neighbor) +{ + uip_ipaddr_t ipaddr; + uip_lladdr_t lladdr; + uip_ds6_nbr_t *nbr, *next_nbr; + + memset(&ipaddr, 0, sizeof(ipaddr)); + memset(&lladdr, 0, sizeof(lladdr)); + + UNIT_TEST_BEGIN(); + + /* make sure the neighbor cache table is empty */ + remove_all_entries_in_neighbor_cache(); + UNIT_TEST_ASSERT(uip_ds6_nbr_head() == NULL); + UNIT_TEST_ASSERT(uip_ds6_nbr_num() == 0); + + /* prepare a link-layer address */ + LOG_DBG("link-layer addr: "); + LOG_DBG_LLADDR((const linkaddr_t *)&lladdr); + LOG_DBG_("\n"); + + /* fill the neighbor entry associated with the link-layer address */ + for(int i = 0; i < UIP_DS6_NBR_MAX_6ADDRS_PER_NBR; i++) { + ipaddr.u8[0] = i; + nbr = uip_ds6_nbr_add(&ipaddr, &lladdr, is_router, state, reason, NULL); + UNIT_TEST_ASSERT(nbr != NULL); + UNIT_TEST_ASSERT(uip_ds6_nbr_num() == (i + 1)); + } + + /* remove IPv6 addresses for the link-layer address one by one */ + for(nbr = uip_ds6_nbr_head(); nbr != NULL; nbr = next_nbr) { + LOG_DBG("removing nbr:%p\n", nbr); + next_nbr = uip_ds6_nbr_next(nbr); + UNIT_TEST_ASSERT(uip_ds6_nbr_rm(nbr) == 1); + } + UNIT_TEST_ASSERT(uip_ds6_nbr_num() == 0); + UNIT_TEST_ASSERT(uip_ds6_nbr_head() == NULL); + + UNIT_TEST_END(); + +} + +UNIT_TEST_REGISTER(fill_neighbor_cache_table, + "fill the neighbor cache table"); +UNIT_TEST(fill_neighbor_cache_table) +{ + /* + * We should be able to add the same number of link-layer addresses as + * NBR_TABLE_MAX_NEIGHBORS. In addition, we should be add the same number of + * IPv6 addresses per link-layer address as + * UIP_DS6_NBR_CONF_MAX_6ADDRS_PER_NBR. + */ + uip_ipaddr_t ipaddr; + uip_lladdr_t lladdr; + uip_ds6_nbr_t *nbr; + + memset(&ipaddr, 0, sizeof(ipaddr)); + memset(&lladdr, 0, sizeof(lladdr)); + + UNIT_TEST_BEGIN(); + + /* make sure the neighbor cache table is empty */ + remove_all_entries_in_neighbor_cache(); + UNIT_TEST_ASSERT(uip_ds6_nbr_head() == NULL); + UNIT_TEST_ASSERT(uip_ds6_nbr_num() == 0); + + for(int i = 0; i <= NBR_TABLE_MAX_NEIGHBORS; i++) { + lladdr.addr[0] = i & 0xFF; + lladdr.addr[1] = i >> 8; + for(int j = 0; j <= UIP_DS6_NBR_MAX_6ADDRS_PER_NBR; j++) { + ipaddr.u8[0] = i & 0xFF; + ipaddr.u8[1] = i >> 8; + ipaddr.u8[2] = j; + LOG_DBG("adding ipv6 addr ("); + LOG_DBG_6ADDR(&ipaddr); + LOG_DBG_(") to link-layer addr ("); + LOG_DBG_LLADDR((const linkaddr_t *)&lladdr); + LOG_DBG_(") [i=%u,j=%u]\n", i, j); + + nbr = uip_ds6_nbr_add(&ipaddr, &lladdr, is_router, state, reason, NULL); + if((i < NBR_TABLE_MAX_NEIGHBORS) && + (j < UIP_DS6_NBR_MAX_6ADDRS_PER_NBR)) { + UNIT_TEST_ASSERT(nbr != NULL); + } else if(i == NBR_TABLE_MAX_NEIGHBORS) { + /* we should not be able to add a link-layer address any more */ + UNIT_TEST_ASSERT(j == 0); + UNIT_TEST_ASSERT(nbr == NULL); + break; + } else if(j == UIP_DS6_NBR_MAX_6ADDRS_PER_NBR) { + /* we should not be able to add an IPv6 address any more */ + UNIT_TEST_ASSERT(i < NBR_TABLE_MAX_NEIGHBORS); + UNIT_TEST_ASSERT(nbr == NULL); + break; + } else { + /* shouldn't come here */ + UNIT_TEST_ASSERT(false); + } + } + } + + UNIT_TEST_END(); +} + +PROCESS_THREAD(node_process, ev, data) +{ + PROCESS_BEGIN(); + + UNIT_TEST_RUN(add_v6addrs_to_neighbor); + UNIT_TEST_RUN(remove_v6addrs_of_neighbor); + UNIT_TEST_RUN(fill_neighbor_cache_table); + + printf("\nTEST SUCCEEDED\n"); + exit(0); /* success: all the test passed */ + + PROCESS_END(); +} diff --git a/tests/14-rpl-lite/02-rpl-root-reboot-2.csc b/tests/14-rpl-lite/02-rpl-root-reboot-2.csc new file mode 100644 index 000000000..50b3153dd --- /dev/null +++ b/tests/14-rpl-lite/02-rpl-root-reboot-2.csc @@ -0,0 +1,294 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + My simulation + 10.0 + 123456 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.contikimote.ContikiMoteType + mtype34 + mote + [CONTIKI_DIR]/examples/libs/shell/example.c + make example.cooja TARGET=cooja MAKE_ROUTING=MAKE_ROUTING_RPL_LITE -j + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.Battery + org.contikios.cooja.contikimote.interfaces.ContikiVib + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + org.contikios.cooja.contikimote.interfaces.ContikiBeeper + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.contikimote.interfaces.ContikiIPAddress + org.contikios.cooja.contikimote.interfaces.ContikiRadio + org.contikios.cooja.contikimote.interfaces.ContikiButton + org.contikios.cooja.contikimote.interfaces.ContikiPIR + org.contikios.cooja.contikimote.interfaces.ContikiClock + org.contikios.cooja.contikimote.interfaces.ContikiLED + org.contikios.cooja.contikimote.interfaces.ContikiCFS + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + false + + + + org.contikios.cooja.interfaces.Position + 50.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 3 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype34 + + + + org.contikios.cooja.interfaces.Position + 100.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 2 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype34 + + + + org.contikios.cooja.interfaces.Position + 150.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 1 + + + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + ip-addr~;routes~;routing~; + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype34 + + + + org.contikios.cooja.interfaces.Position + 0.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 4 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype34 + + + + org.contikios.cooja.plugins.SimControl + 280 + 0 + 160 + 400 + 0 + + + org.contikios.cooja.plugins.Visualizer + + true + org.contikios.cooja.plugins.skins.IDVisualizerSkin + org.contikios.cooja.plugins.skins.GridVisualizerSkin + org.contikios.cooja.plugins.skins.TrafficVisualizerSkin + org.contikios.cooja.plugins.skins.UDGMVisualizerSkin + org.contikios.cooja.plugins.skins.MoteTypeVisualizerSkin + 0.9090909090909091 0.0 0.0 0.9090909090909091 125.81818181818181 173.0 + + 400 + 4 + 400 + 1 + 1 + + + org.contikios.cooja.plugins.LogListener + + + + + + 604 + 3 + 784 + 400 + 160 + + + org.contikios.cooja.plugins.Notes + + Enter notes here + true + + 944 + 1 + 160 + 680 + 0 + + + org.contikios.cooja.plugins.ScriptRunner + + + true + + 617 + 2 + 785 + 1007 + 160 + + + diff --git a/tests/14-rpl-lite/code/receiver-node.c b/tests/14-rpl-lite/code/receiver-node.c index b5265d3f7..b5cd26a15 100644 --- a/tests/14-rpl-lite/code/receiver-node.c +++ b/tests/14-rpl-lite/code/receiver-node.c @@ -73,8 +73,9 @@ set_global_address(void) static uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); diff --git a/tests/14-rpl-lite/code/sender-node.c b/tests/14-rpl-lite/code/sender-node.c index 350758110..00bd6d5c5 100644 --- a/tests/14-rpl-lite/code/sender-node.c +++ b/tests/14-rpl-lite/code/sender-node.c @@ -71,8 +71,9 @@ set_global_address(void) uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); @@ -92,6 +93,7 @@ PROCESS_THREAD(sender_node_process, ev, data) static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; + const uip_ipaddr_t *default_prefix; PROCESS_BEGIN(); @@ -109,7 +111,13 @@ PROCESS_THREAD(sender_node_process, ev, data) PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0x0201, 0x001, 0x001, 0x001); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + + addr.u16[4] = UIP_HTONS(0x0201); + addr.u16[5] = UIP_HTONS(0x0001); + addr.u16[6] = UIP_HTONS(0x0001); + addr.u16[7] = UIP_HTONS(0x0001); { static unsigned int message_number; diff --git a/tests/15-rpl-classic/02-rpl-root-reboot-2.csc b/tests/15-rpl-classic/02-rpl-root-reboot-2.csc new file mode 100644 index 000000000..6de8e9583 --- /dev/null +++ b/tests/15-rpl-classic/02-rpl-root-reboot-2.csc @@ -0,0 +1,294 @@ + + + [APPS_DIR]/mrm + [APPS_DIR]/mspsim + [APPS_DIR]/avrora + [APPS_DIR]/serial_socket + [APPS_DIR]/powertracker + + My simulation + 10.0 + 123456 + 1000000 + + org.contikios.cooja.radiomediums.UDGM + 50.0 + 100.0 + 1.0 + 1.0 + + + 40000 + + + org.contikios.cooja.contikimote.ContikiMoteType + mtype512 + mote + [CONTIKI_DIR]/examples/libs/shell/example.c + make example.cooja TARGET=cooja MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC -j + org.contikios.cooja.interfaces.Position + org.contikios.cooja.interfaces.Battery + org.contikios.cooja.contikimote.interfaces.ContikiVib + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + org.contikios.cooja.contikimote.interfaces.ContikiBeeper + org.contikios.cooja.interfaces.RimeAddress + org.contikios.cooja.contikimote.interfaces.ContikiIPAddress + org.contikios.cooja.contikimote.interfaces.ContikiRadio + org.contikios.cooja.contikimote.interfaces.ContikiButton + org.contikios.cooja.contikimote.interfaces.ContikiPIR + org.contikios.cooja.contikimote.interfaces.ContikiClock + org.contikios.cooja.contikimote.interfaces.ContikiLED + org.contikios.cooja.contikimote.interfaces.ContikiCFS + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + org.contikios.cooja.interfaces.Mote2MoteRelations + org.contikios.cooja.interfaces.MoteAttributes + false + + + + org.contikios.cooja.interfaces.Position + 50.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 3 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype512 + + + + org.contikios.cooja.interfaces.Position + 100.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 2 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype512 + + + + org.contikios.cooja.interfaces.Position + 150.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 1 + + + org.contikios.cooja.contikimote.interfaces.ContikiRS232 + ip-addr~;routes~;routing~; + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype512 + + + + org.contikios.cooja.interfaces.Position + 0.0 + 0.0 + 0.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiMoteID + 4 + + + org.contikios.cooja.contikimote.interfaces.ContikiRadio + 250.0 + + + org.contikios.cooja.contikimote.interfaces.ContikiEEPROM + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== + + mtype512 + + + + org.contikios.cooja.plugins.SimControl + 280 + 0 + 160 + 400 + 0 + + + org.contikios.cooja.plugins.Visualizer + + true + org.contikios.cooja.plugins.skins.IDVisualizerSkin + org.contikios.cooja.plugins.skins.GridVisualizerSkin + org.contikios.cooja.plugins.skins.TrafficVisualizerSkin + org.contikios.cooja.plugins.skins.UDGMVisualizerSkin + org.contikios.cooja.plugins.skins.MoteTypeVisualizerSkin + 0.9090909090909091 0.0 0.0 0.9090909090909091 125.81818181818181 173.0 + + 400 + 4 + 400 + 1 + 1 + + + org.contikios.cooja.plugins.LogListener + + + + + + 604 + 3 + 784 + 400 + 160 + + + org.contikios.cooja.plugins.Notes + + Enter notes here + true + + 944 + 1 + 160 + 680 + 0 + + + org.contikios.cooja.plugins.ScriptRunner + + + true + + 617 + 2 + 785 + 1007 + 160 + + + diff --git a/tests/15-rpl-classic/code/receiver-node.c b/tests/15-rpl-classic/code/receiver-node.c index 798aea84d..3854784d0 100644 --- a/tests/15-rpl-classic/code/receiver-node.c +++ b/tests/15-rpl-classic/code/receiver-node.c @@ -73,8 +73,9 @@ set_global_address(void) static uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); diff --git a/tests/15-rpl-classic/code/sender-node.c b/tests/15-rpl-classic/code/sender-node.c index 350758110..00bd6d5c5 100644 --- a/tests/15-rpl-classic/code/sender-node.c +++ b/tests/15-rpl-classic/code/sender-node.c @@ -71,8 +71,9 @@ set_global_address(void) uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); @@ -92,6 +93,7 @@ PROCESS_THREAD(sender_node_process, ev, data) static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; + const uip_ipaddr_t *default_prefix; PROCESS_BEGIN(); @@ -109,7 +111,13 @@ PROCESS_THREAD(sender_node_process, ev, data) PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0x0201, 0x001, 0x001, 0x001); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + + addr.u16[4] = UIP_HTONS(0x0201); + addr.u16[5] = UIP_HTONS(0x0001); + addr.u16[6] = UIP_HTONS(0x0001); + addr.u16[7] = UIP_HTONS(0x0001); { static unsigned int message_number; diff --git a/tests/18-coap-lwm2m/example-lwm2m-standalone b/tests/18-coap-lwm2m/example-lwm2m-standalone index 67b858437..3b23ca8d6 160000 --- a/tests/18-coap-lwm2m/example-lwm2m-standalone +++ b/tests/18-coap-lwm2m/example-lwm2m-standalone @@ -1 +1 @@ -Subproject commit 67b858437f7cf1e4e027d821c4c2ac15fdf2ab44 +Subproject commit 3b23ca8d6a19d281b6330d3f24afc2e1bb14ddb7 diff --git a/tests/19-out-of-tree-build/Makefile b/tests/19-out-of-tree-build/Makefile new file mode 100644 index 000000000..30d7e2302 --- /dev/null +++ b/tests/19-out-of-tree-build/Makefile @@ -0,0 +1,20 @@ +EXAMPLESDIR=$(HOME)/out-of-tree-tests +TOOLSDIR=$(HOME)/contiki-ng/tools + +EXAMPLES = \ +hello-world/native \ +hello-world/native:MAKE_NET=MAKE_NET_NULLNET \ +hello-world/native:MAKE_ROUTING=MAKE_ROUTING_RPL_CLASSIC \ +hello-world/sky \ +hello-world/nrf52dk \ +hello-world/cc2538dk \ +hello-world/zoul \ +hello-world/openmote-cc2538 \ +hello-world/cc26x0-cc13x0 \ +hello-world/jn516x \ +hello-world/simplelink:BOARD=launchpad/cc26x2r1 \ +hello-world/simplelink:BOARD=sensortag/cc2650 \ + +TOOLS= + +include ../Makefile.compile-test diff --git a/tests/Makefile.script-test b/tests/Makefile.script-test index e4dd0da6a..e58acec80 100644 --- a/tests/Makefile.script-test +++ b/tests/Makefile.script-test @@ -5,18 +5,14 @@ CONTIKI=../.. all: clean summary -summary: cooja $(TESTLOGS) +summary: $(TESTLOGS) @cat *.testlog > summary @echo "========== Summary ==========" @cat summary -%.testlog: %.sh cooja +%.testlog: %.sh @echo "========== Running script test $(basename $@).sh ==========" @bash "$(basename $@).sh" "$(CONTIKI)" clean: @rm -f *.*log report summary - -cooja: $(CONTIKI)/tools/cooja/dist/cooja.jar -$(CONTIKI)/tools/cooja/dist/cooja.jar: - (cd $(CONTIKI)/tools/cooja; ant jar) diff --git a/tests/compile-all/build.sh b/tests/compile-all/build.sh index fcbbd7bd7..81441816e 100755 --- a/tests/compile-all/build.sh +++ b/tests/compile-all/build.sh @@ -40,17 +40,21 @@ # To invoke the building for a specific platform, run: # $ PLATFORMS=zoul ./build.sh # +CONTIKI_NG_TOP_DIR="../.." +EXAMPLES_DIR=$CONTIKI_NG_TOP_DIR/examples if [[ "$PLATFORMS" == "" ]] then - PLATFORMS=`ls ../../arch/platform` + PLATFORMS=`ls $CONTIKI_NG_TOP_DIR/arch/platform` fi if [[ "$MAKEFILES" == "" ]] then - MAKEFILES=`find ../../examples/ -name Makefile` + MAKEFILES=`find $EXAMPLES_DIR -name Makefile` fi +HELLO_WORLD=$EXAMPLES_DIR/hello-world + # Set the make goal the first argument of the script or to "all" if called w/o arguments if [[ $# -gt 0 ]] then @@ -97,22 +101,14 @@ do continue fi - if [[ "$platform" == "srf06-cc26xx" ]] + # Detect all boards for the current platform by calling + # make TARGET=$platform boards + # in the hello-world dir. + BOARDS=`make -s -C $HELLO_WORLD TARGET=$platform boards \ + | grep -v "no boards" | rev | cut -f3- -d" " | rev` + + if [[ -z $BOARDS ]] then - # srf06-cc26xx has multiple boards - BOARDS="srf06/cc26xx srf06/cc13xx launchpad/cc2650 launchpad/cc1310 launchpad/cc1350 sensortag/cc2650 sensortag/cc1350" - elif [[ "$platform" == "simplelink" ]] - then - # SimpleLink has multiple boards - BOARDS="launchpad/cc1310 launchpad/cc1350 launchpad/cc1350-4 launchpad/cc2650 \ - sensortag/cc1350 sensortag/cc2650 srf06/cc13x0 srf06/cc26x0 \ - launchpad/cc1312r1 launchpad/cc1352r1 launchpad/cc1352p1 launchpad/cc1352p-2 launchpad/cc1352p-4 launchpad/cc26x2r1" - elif [[ "$platform" == "zoul" ]] - then - # Zoul has multiple boards - BOARDS="remote-reva remote-revb firefly-reva firefly orion" - else - # Other platforms have just a single board BOARDS="default" fi diff --git a/tools/cooja b/tools/cooja index a5904b983..63538bbb8 160000 --- a/tools/cooja +++ b/tools/cooja @@ -1 +1 @@ -Subproject commit a5904b98366478bb9e7f7fe9f0bb78fc064914c5 +Subproject commit 63538bbb882ba06a7b8cf97c11ce2fe4d22e4f88 diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile index 9d15704ac..6eb9dbb17 100644 --- a/tools/docker/Dockerfile +++ b/tools/docker/Dockerfile @@ -1,29 +1,58 @@ FROM 32bit/ubuntu:16.04 +ENV DEBIAN_FRONTEND noninteractive + # Tools -RUN apt-get update && \ - apt-get install -y --no-install-recommends \ - build-essential doxygen git wget unzip python-serial python-pip \ - default-jdk ant srecord iputils-tracepath rlwrap \ - mosquitto mosquitto-clients gdb \ - && apt-get clean +RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF && \ + echo "deb http://download.mono-project.com/repo/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list && \ + apt-get -qq update && \ + apt-get -qq -y --no-install-recommends install \ + ant \ + build-essential \ + default-jdk \ + doxygen \ + gdb \ + git \ + gksu \ + gtk-sharp2 \ + iputils-tracepath \ + libcanberra-gtk-module:i386 \ + libgtk2.0-0 \ + mono-complete \ + mosquitto \ + mosquitto-clients \ + npm \ + python-pip \ + python-serial \ + rlwrap \ + screen \ + srecord \ + uml-utilities \ + unzip \ + wget \ + > /dev/null \ + && apt-get -qq clean + +# Install coap-cli +RUN npm -q install coap-cli -g \ + && sudo ln -s /usr/bin/nodejs /usr/bin/node # Install ARM toolchain -RUN wget https://launchpad.net/gcc-arm-embedded/5.0/5-2015-q4-major/+download/gcc-arm-none-eabi-5_2-2015q4-20151219-linux.tar.bz2 && \ +RUN wget -nv https://launchpad.net/gcc-arm-embedded/5.0/5-2015-q4-major/+download/gcc-arm-none-eabi-5_2-2015q4-20151219-linux.tar.bz2 && \ tar xjf gcc-arm-none-eabi-5_2-2015q4-20151219-linux.tar.bz2 -C /tmp/ && \ cp -f -r /tmp/gcc-arm-none-eabi-5_2-2015q4/* /usr/local/ && \ rm -rf /tmp/gcc-arm-none-eabi-* gcc-arm-none-eabi-*-linux.tar.bz2 # Install msp430 toolchain -RUN wget http://simonduq.github.io/resources/mspgcc-4.7.2-compiled.tar.bz2 && \ +RUN wget -nv http://simonduq.github.io/resources/mspgcc-4.7.2-compiled.tar.bz2 && \ tar xjf mspgcc*.tar.bz2 -C /tmp/ && \ cp -f -r /tmp/msp430/* /usr/local/ && \ rm -rf /tmp/msp430 mspgcc*.tar.bz2 # Install NXP toolchain (partial, with binaries excluded. Download from nxp.com) -RUN wget http://simonduq.github.io/resources/ba-elf-gcc-4.7.4-part1.tar.bz2 && \ - wget http://simonduq.github.io/resources/ba-elf-gcc-4.7.4-part2.tar.bz2 && \ - wget http://simonduq.github.io/resources/jn516x-sdk-4163-1416.tar.bz2 && \ +RUN wget -nv http://simonduq.github.io/resources/ba-elf-gcc-4.7.4-part1.tar.bz2 && \ + wget -nv http://simonduq.github.io/resources/ba-elf-gcc-4.7.4-part2.tar.bz2 && \ + wget -nv http://simonduq.github.io/resources/jn516x-sdk-4163-1416.tar.bz2 && \ mkdir /tmp/jn516x-sdk /tmp/ba-elf-gcc && \ tar xjf jn516x-sdk-*.tar.bz2 -C /tmp/jn516x-sdk && \ tar xjf ba-elf-gcc-*part1.tar.bz2 -C /tmp/ba-elf-gcc && \ @@ -35,18 +64,17 @@ RUN wget http://simonduq.github.io/resources/ba-elf-gcc-4.7.4-part1.tar.bz2 && \ ENV PATH="/usr/ba-elf-gcc/bin:${PATH}" ## Install nRF52 SDK -RUN wget https://developer.nordicsemi.com/nRF5_IoT_SDK/nRF5_IoT_SDK_v0.9.x/nrf5_iot_sdk_3288530.zip && \ +RUN wget -nv https://developer.nordicsemi.com/nRF5_IoT_SDK/nRF5_IoT_SDK_v0.9.x/nrf5_iot_sdk_3288530.zip && \ mkdir /usr/nrf52-sdk && \ - unzip nrf5_iot_sdk_3288530.zip -d /usr/nrf52-sdk && \ + unzip -q nrf5_iot_sdk_3288530.zip -d /usr/nrf52-sdk && \ rm nrf5_iot_sdk_3288530.zip ENV NRF52_SDK_ROOT /usr/nrf52-sdk # Install sphinx and sphinx_rtd_theme, required for building and testing the # readthedocs API documentation -RUN pip install --upgrade pip -RUN pip install setuptools -RUN pip install sphinx_rtd_theme sphinx +RUN pip -q install --upgrade pip +RUN pip -q install setuptools && pip -q install sphinx_rtd_theme sphinx # Create user, enable X forwarding, add to group dialout # -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix @@ -74,21 +102,8 @@ WORKDIR ${HOME} RUN echo "#!/bin/bash\nant -Dbasedir=${COOJA} -f ${COOJA}/build.xml run" > ${HOME}/cooja && \ chmod +x ${HOME}/cooja -# Install coap-cli -RUN sudo apt-get install -y npm \ - && sudo apt-get clean \ - && sudo npm install coap-cli -g \ - && sudo ln -s /usr/bin/nodejs /usr/bin/node - -# Install Mono and libcanberra-gtk:i386 (for Renode) -RUN sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \ - && echo "deb http://download.mono-project.com/repo/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list \ - && sudo apt-get update \ - && sudo apt-get install -y mono-complete gksu libgtk2.0-0 screen uml-utilities gtk-sharp2 libcanberra-gtk-module:i386 \ - && sudo apt-get clean - # Download, build and install Renode -RUN git clone https://github.com/renode/renode.git \ +RUN git clone --quiet https://github.com/renode/renode.git \ && cd ${HOME}/renode \ && git checkout v1.3 \ && ./build.sh diff --git a/tools/doxygen/Doxyfile b/tools/doxygen/Doxyfile index dab2c18e3..2cd7dba72 100644 --- a/tools/doxygen/Doxyfile +++ b/tools/doxygen/Doxyfile @@ -807,8 +807,8 @@ EXCLUDE_SYMLINKS = NO # Note that the wildcards are matched against the file with absolute path, so to # exclude all test directories for example use the pattern */test/* -EXCLUDE_PATTERNS = */cpu/cc26xx-cc13xx/lib/* \ - */cpu/cc26xx-cc13xx/rf-core/api/* \ +EXCLUDE_PATTERNS = */cpu/cc26x0-cc13x0/lib/* \ + */cpu/cc26x0-cc13x0/rf-core/api/* \ */cpu/simplelink-cc13xx-cc26xx/lib/* \ */cpu/simplelink-cc13xx-cc26xx/rf-settings/* \ */cpu/simplelink-cc13xx-cc26xx/cc13x0-cc26x0/driverlib/* \ diff --git a/tools/serial-io/tunslip6.c b/tools/serial-io/tunslip6.c index 48b0ecd0f..61013c30c 100644 --- a/tools/serial-io/tunslip6.c +++ b/tools/serial-io/tunslip6.c @@ -856,14 +856,20 @@ fprintf(stderr," -s siodev Serial device (default /dev/ttyUSB0)\n"); fprintf(stderr," -M Interface MTU (default and min: 1280)\n"); fprintf(stderr," -T Make tap interface (default is tun interface)\n"); fprintf(stderr," -t tundev Name of interface (default tap0 or tun0)\n"); +#ifdef __APPLE__ +fprintf(stderr," -v level Verbosity level\n"); +#else fprintf(stderr," -v[level] Verbosity level\n"); +#endif fprintf(stderr," -v0 No messages\n"); fprintf(stderr," -v1 Encapsulated SLIP debug messages (default)\n"); fprintf(stderr," -v2 Printable strings after they are received\n"); fprintf(stderr," -v3 Printable strings and SLIP packet notifications\n"); fprintf(stderr," -v4 All printable characters as they are received\n"); fprintf(stderr," -v5 All SLIP packets in hex\n"); +#ifndef __APPLE__ fprintf(stderr," -v Equivalent to -v3\n"); +#endif fprintf(stderr," -d[basedelay] Minimum delay between outgoing SLIP packets.\n"); fprintf(stderr," Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n"); fprintf(stderr," -d is equivalent to -d10.\n"); diff --git a/tools/sky/uip6-bridge/Makefile b/tools/sky/uip6-bridge/Makefile deleted file mode 100644 index 55da6798e..000000000 --- a/tools/sky/uip6-bridge/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -ifdef CONTIKI_HOME -CONTIKI = $(CONTIKI_HOME) -else -ifndef CONTIKI -CONTIKI=../../.. -endif -endif - -DEFINES=WITH_SLIP=1 - -ifndef TARGET -TARGET=sky -endif - -MODULES_REL += dev -PROJECT_SOURCEFILES = fakeuip.c sicslow_ethernet.c - -all: uip6-bridge-tap.sky - -upload: uip6-bridge-tap.ihex - cp $< $(IHEXFILE) - $(MAKE) sky-u.$(subst /,-,$(word $(MOTE), $(MOTES))) - -include $(CONTIKI)/Makefile.include - -../../tapslip6: ../../tapslip6.c - (cd ../../; $(MAKE) tapslip6) - -ifndef MOTE -MOTE=1 -endif - -connect: ../../tapslip6 - sudo ../../tapslip6 -t tap0 -s $(USBDEVPREFIX)$(word $(MOTE), $(CMOTES)) 127.0.0.1 255.0.0.0 - -bridge: - @sudo service radvd restart || echo radvd could not be restarted - sudo route add -6 fd00::/64 tap0 - sudo ip -6 address add fd00::1/64 dev tap0 diff --git a/tools/sky/uip6-bridge/dev/slip.c b/tools/sky/uip6-bridge/dev/slip.c deleted file mode 100644 index e8062e515..000000000 --- a/tools/sky/uip6-bridge/dev/slip.c +++ /dev/null @@ -1,323 +0,0 @@ -/* -*- C -*- */ -/* - * Copyright (c) 2005, Swedish Institute of Computer Science - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - * - */ - - -#include -#include -#include "dev/ds2411/ds2411.h" -#include "contiki.h" - -#include "net/ipv6/uip.h" -#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) - -#include "dev/slip.h" - -#define SLIP_END 0300 -#define SLIP_ESC 0333 -#define SLIP_ESC_END 0334 -#define SLIP_ESC_ESC 0335 - -PROCESS(slip_process, "SLIP driver"); - -uint8_t slip_active; - -#if 1 -#define SLIP_STATISTICS(statement) -#else -uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop; -#define SLIP_STATISTICS(statement) statement -#endif - -/* Must be at least one byte larger than UIP_BUFSIZE! */ -#define RX_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN + 16) - -enum { - STATE_TWOPACKETS = 0, /* We have 2 packets and drop incoming data. */ - STATE_OK = 1, - STATE_ESC = 2, - STATE_RUBBISH = 3, -}; - -/* - * Variables begin and end manage the buffer space in a cyclic - * fashion. The first used byte is at begin and end is one byte past - * the last. I.e. [begin, end) is the actively used space. - * - * If begin != pkt_end we have a packet at [begin, pkt_end), - * furthermore, if state == STATE_TWOPACKETS we have one more packet at - * [pkt_end, end). If more bytes arrive in state STATE_TWOPACKETS - * they are discarded. - */ - -static uint8_t state = STATE_TWOPACKETS; -static uint16_t begin, end; -static uint8_t rxbuf[RX_BUFSIZE]; -static uint16_t pkt_end; /* SLIP_END tracker. */ - -static void (* input_callback)(void) = NULL; -static void (* tcpip_input_callback)(void) = NULL; -/*---------------------------------------------------------------------------*/ -void -slip_set_input_callback(void (*c)(void)) -{ - input_callback = c; -} -/*---------------------------------------------------------------------------*/ -void -slip_set_tcpip_input_callback(void (*c)(void)) -{ - tcpip_input_callback = c; -} -/*---------------------------------------------------------------------------*/ -uint8_t -slip_write(const void *_ptr, int len) -{ - const uint8_t *ptr = _ptr; - uint16_t i; - uint8_t c; - - slip_arch_writeb(SLIP_END); - - for(i = 0; i < len; ++i) { - c = *ptr++; - if(c == SLIP_END) { - slip_arch_writeb(SLIP_ESC); - c = SLIP_ESC_END; - } else if(c == SLIP_ESC) { - slip_arch_writeb(SLIP_ESC); - c = SLIP_ESC_ESC; - } - slip_arch_writeb(c); - } - slip_arch_writeb(SLIP_END); - - return len; -} -/*---------------------------------------------------------------------------*/ -static void -rxbuf_init(void) -{ - begin = end = pkt_end = 0; - state = STATE_OK; -} -/*---------------------------------------------------------------------------*/ -/* Upper half does the polling. */ -static uint16_t -slip_poll_handler(uint8_t *outbuf, uint16_t blen) -{ - /* This is a hack and won't work across buffer edge! */ - if(rxbuf[begin] == 'C') { - int i; - if(begin < end && (end - begin) >= 6 - && memcmp(&rxbuf[begin], "CLIENT", 6) == 0) { - state = STATE_TWOPACKETS; /* Interrupts do nothing. */ - memset(&rxbuf[begin], 0x0, 6); - - rxbuf_init(); - - for(i = 0; i < 13; i++) { - slip_arch_writeb("CLIENTSERVER\300"[i]); - } - return 0; - } - } else if(rxbuf[begin] == '?') { - int i, j; - char* hexchar = "0123456789abcdef"; - if(begin < end && (end - begin) >= 2 - && rxbuf[begin + 1] == 'M') { - state = STATE_TWOPACKETS; /* Interrupts do nothing. */ - rxbuf[begin] = 0; - rxbuf[begin + 1] = 0; - - rxbuf_init(); - - /* this is just a test so far... just to see if it works */ - slip_arch_writeb('!'); - slip_arch_writeb('M'); - for(j = 0; j < 8; j++) { - slip_arch_writeb(hexchar[ds2411_id[j] >> 4]); - slip_arch_writeb(hexchar[ds2411_id[j] & 15]); - } - slip_arch_writeb(SLIP_END); - return 0; - } - } - - /* - * Interrupt can not change begin but may change pkt_end. - * If pkt_end != begin it will not change again. - */ - if(begin != pkt_end) { - uint16_t len; - - if(begin < pkt_end) { - len = pkt_end - begin; - if(len > blen) { - len = 0; - } else { - memcpy(outbuf, &rxbuf[begin], len); - } - } else { - len = (RX_BUFSIZE - begin) + (pkt_end - 0); - if(len > blen) { - len = 0; - } else { - unsigned i; - for(i = begin; i < RX_BUFSIZE; i++) { - *outbuf++ = rxbuf[i]; - } - for(i = 0; i < pkt_end; i++) { - *outbuf++ = rxbuf[i]; - } - } - } - - /* Remove data from buffer together with the copied packet. */ - begin = pkt_end; - if(state == STATE_TWOPACKETS) { - pkt_end = end; - state = STATE_OK; /* Assume no bytes where lost! */ - - /* One more packet is buffered, need to be polled again! */ - process_poll(&slip_process); - } - return len; - } - - return 0; -} -/*---------------------------------------------------------------------------*/ -PROCESS_THREAD(slip_process, ev, data) -{ - PROCESS_BEGIN(); - - rxbuf_init(); - - while(1) { - PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); - - slip_active = 1; - - /* Move packet from rxbuf to buffer provided by uIP. */ - uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], - UIP_BUFSIZE - UIP_LLH_LEN); - if(uip_len > 0) { - if(tcpip_input_callback) { - tcpip_input_callback(); - } else { - tcpip_input(); - } - } - } - - PROCESS_END(); -} -/*---------------------------------------------------------------------------*/ -int -slip_input_byte(unsigned char c) -{ - switch(state) { - case STATE_RUBBISH: - if(c == SLIP_END) { - state = STATE_OK; - } - return 0; - - case STATE_TWOPACKETS: /* Two packets are already buffered! */ - return 0; - - case STATE_ESC: - if(c == SLIP_ESC_END) { - c = SLIP_END; - } else if(c == SLIP_ESC_ESC) { - c = SLIP_ESC; - } else { - state = STATE_RUBBISH; - SLIP_STATISTICS(slip_rubbish++); - end = pkt_end; /* remove rubbish */ - return 0; - } - state = STATE_OK; - break; - - case STATE_OK: - if(c == SLIP_ESC) { - state = STATE_ESC; - return 0; - } else if(c == SLIP_END) { - /* - * We have a new packet, possibly of zero length. - * - * There may already be one packet buffered. - */ - if(end != pkt_end) { /* Non zero length. */ - if(begin == pkt_end) { /* None buffered. */ - pkt_end = end; - } else { - state = STATE_TWOPACKETS; - SLIP_STATISTICS(slip_twopackets++); - } - process_poll(&slip_process); - return 1; - } - return 0; - } - break; - } - - /* add_char: */ - { - unsigned next; - next = end + 1; - if(next == RX_BUFSIZE) { - next = 0; - } - if(next == begin) { /* rxbuf is full */ - state = STATE_RUBBISH; - SLIP_STATISTICS(slip_overflow++); - end = pkt_end; /* remove rubbish */ - return 0; - } - rxbuf[end] = c; - end = next; - } - - /* There could be a separate poll routine for this. */ - if(c == 'T' && rxbuf[begin] == 'C') { - process_poll(&slip_process); - return 1; - } - - return 0; -} -/*---------------------------------------------------------------------------*/ diff --git a/tools/sky/uip6-bridge/fakeuip.c b/tools/sky/uip6-bridge/fakeuip.c deleted file mode 100644 index 84f6440ce..000000000 --- a/tools/sky/uip6-bridge/fakeuip.c +++ /dev/null @@ -1,136 +0,0 @@ - -/* Various stub functions and uIP variables other code might need to - * compile. Allows you to save needing to compile all of uIP in just - * to get a few things */ - - -#include "net/ipv6/uip.h" -#include "net/ipv6/uip-ds6.h" -#include - -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - -uip_buf_t uip_aligned_buf; - -uint16_t uip_len; - -struct uip_stats uip_stat; - -uip_lladdr_t uip_lladdr; - -uint16_t uip_htons(uint16_t val) { return UIP_HTONS(val);} - -uip_ds6_netif_t uip_ds6_if; - -/********** UIP_DS6.c **********/ - -void -uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr) -{ - /* We consider only links with IEEE EUI-64 identifier or - IEEE 48-bit MAC addresses */ -#if (UIP_LLADDR_LEN == 8) - memcpy(ipaddr->u8 + 8, lladdr, UIP_LLADDR_LEN); - ipaddr->u8[8] ^= 0x02; -#elif (UIP_LLADDR_LEN == 6) - memcpy(ipaddr->u8 + 8, lladdr, 3); - ipaddr->u8[11] = 0xff; - ipaddr->u8[12] = 0xfe; - memcpy(ipaddr->u8 + 13, lladdr + 3, 3); - ipaddr->u8[8] ^= 0x02; -#else -#error fakeuip.c cannot build interface address when UIP_LLADDR_LEN is not 6 or 8 -#endif -} - -/*---------------------------------------------------------------------------*/ -/* - * get a link local address - - * state = -1 => any address is ok. Otherwise state = desired state of addr. - * (TENTATIVE, PREFERRED, DEPRECATED) - */ -uip_ds6_addr_t * -uip_ds6_get_link_local(int8_t state) { - uip_ds6_addr_t *locaddr; - for(locaddr = uip_ds6_if.addr_list; - locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) { - if((locaddr->isused) && (state == - 1 || locaddr->state == state) - && (uip_is_addr_linklocal(&locaddr->ipaddr))) { - return locaddr; - } - } - return NULL; -} - -uip_ds6_addr_t * -uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type) -{ - return NULL; -} -/********** UIP.c ****************/ - -static uint16_t -chksum(uint16_t sum, const uint8_t *data, uint16_t len) -{ - uint16_t t; - const uint8_t *dataptr; - const uint8_t *last_byte; - - dataptr = data; - last_byte = data + len - 1; - - while(dataptr < last_byte) { /* At least two more bytes */ - t = (dataptr[0] << 8) + dataptr[1]; - sum += t; - if(sum < t) { - sum++; /* carry */ - } - dataptr += 2; - } - - if(dataptr == last_byte) { - t = (dataptr[0] << 8) + 0; - sum += t; - if(sum < t) { - sum++; /* carry */ - } - } - - /* Return sum in host byte order. */ - return sum; -} - -static uint16_t -upper_layer_chksum(uint8_t proto) -{ - uint16_t upper_layer_len; - uint16_t sum; - - upper_layer_len = (((uint16_t)(UIP_IP_BUF->len[0]) << 8) + UIP_IP_BUF->len[1]) ; - - /* First sum pseudoheader. */ - /* IP protocol and length fields. This addition cannot carry. */ - sum = upper_layer_len + proto; - /* Sum IP source and destination addresses. */ - sum = chksum(sum, (uint8_t *)&UIP_IP_BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t)); - - /* Sum TCP header and data. */ - sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], - upper_layer_len); - - return (sum == 0) ? 0xffff : uip_htons(sum); -} - -/*---------------------------------------------------------------------------*/ -uint16_t -uip_icmp6chksum(void) -{ - return upper_layer_chksum(UIP_PROTO_ICMP6); -} - -/*---------------------------------------------------------------------------*/ -void -uip_ds6_link_callback(int status, int numtx) -{ - -} diff --git a/tools/sky/uip6-bridge/radvd.conf b/tools/sky/uip6-bridge/radvd.conf deleted file mode 100644 index d6dd41e2b..000000000 --- a/tools/sky/uip6-bridge/radvd.conf +++ /dev/null @@ -1,16 +0,0 @@ -interface tap0 { - AdvSendAdvert on; - AdvLinkMTU 1280; - AdvCurHopLimit 128; - AdvReachableTime 360000; - MinRtrAdvInterval 100; - MaxRtrAdvInterval 150; - AdvDefaultLifetime 200; - prefix AAAA::/64 - { - AdvOnLink on; - AdvAutonomous on; - AdvPreferredLifetime 4294967295; - AdvValidLifetime 4294967295; - }; -}; diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c deleted file mode 100644 index 7217e3229..000000000 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ /dev/null @@ -1,882 +0,0 @@ -/** - * \file sicslow_ethernet.c - * Routines to interface between Ethernet and 6LowPan - * - * \author - * Colin O'Flynn - * - * \addtogroup usbstick - */ - -/* Copyright (c) 2008 by: - * Colin O'Flynn coflynn@newae.com - * Eric Gnoske egnoske@gmail.com - * Blake Leverett bleverett@gmail.com - * Mike Vidales mavida404@gmail.com - * Kevin Brown kbrown3@uccs.edu - * Nate Bohlmann nate@elfwerks.com - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of the copyright holders nor the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - \ingroup usbstick - \defgroup sicslowinterop 6LowPan Ethernet Interop - @{ -*/ - - -/** - \par Ethernet to 6LowPan Address Translation - - It should be obvious that since 802.15.4 addresses are 8 - bytes, and 802.3 addresses are 6 bytes, some form of - address translation is needed. These routines provide this - - \par 802.3 Address Formats - - 802.3 MAC addresses used here have this form: - - \verbatim - +----+----+----+----+----+----+----+----+ - + + + + + + TR + GL + MU + - +----+----+----+----+----+----+----+----+ - \endverbatim - - - It can be seen this is like a normal ethernet MAC address, - with GL being the Global/Local bit, and MU being the - Multicast/Unicast bit. - - The addition is the 'TR' bit, which if set indicates that - the address must be translated when going between 802.15.4 - and 802.3. - - \par Address Translation - - If the TRANSLATE (TR) bit is CLEAR, this means the 5th and - 4th LSBytes of the 802.15.4 address are fffe, aka the address - has the hexidecial form: - - xxxxxxfffexxxxxx - - \note - You should always aim to set the 802.15.4 addresses - of the devices on your network to ones that will - satisfy this requirement. Some examples are: - \note - 0x02 23 42 ff fe 73 92 28 - \note - 0x82 00 82 ff fe cd ee 22 - - \note - So the most significant octets MUST - have bit 0 CLEAR, bit 1 SET, and bit 2 CLEAR. The remaining - bits in this octet can be anything. - - If the TRANSLATE bit is SET, this means the address on the - 802.3 side does not directly convert to an 802.15.4 address. - To translate it, the remainder of the octet is used as an - index in a look-up table. This look-up table simply stores - the 4th, 5th, and 8th octet of the 802.15.4 address, and attaches - them to the remaining 5 bytes of the 802.3 address. - - In this way there can be 32 different 802.15.4 'prefixes', - requiring only 96 bytes of RAM in a storage table on the - 802.3 to 802.15.4 bridge. - - Mulitcast addresses on 802.3 are mapped to broadcast addresses on - 802.15.4 and vis-versa. Since IPv6 does not use 802.3 broadcast, - this code will drop all 802.3 broadcast packets. They are most - likely something unwanted, such as IPv4 packets that snuck in. - - \par Notes on how addresses are stored - - An 802.15.4 address will be reported for example as: - - 0x8877665544332211 - - Stored in the array as passed to these functions, it will be: - \verbatim - array[0] = 0x88; - array[1] = 0x77; - array[2] = 0x66; - etc. - \endverbatim - - An 802.3 address will be reported for example as: - 02:43:53:35:45:45 - - Stored in the array as passed to these functions, it will be: - \verbatim - array[0] = 0x02; - array[1] = 0x43; - array[2] = 0x53; - array[3] = 0x35 - etc. - \endverbatim -*/ - -#include "uip.h" -#include "uip_arp.h" /* For ethernet header structure */ -#include "net/ipv6/sicslowpan.h" -#include "sicslow_ethernet.h" - -#include -#include -#include - -//#define PRINTF printf -#define PRINTF(...) - -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#define ETHBUF(x) ((struct uip_eth_hdr *)x) - -//For little endian, such as our friend mr. AVR -#ifndef LSB -#define LSB(u16) (((uint8_t *)&(u16))[0]) //!< Least significant byte of \a u16. -#define MSB(u16) (((uint8_t *)&(u16))[1]) //!< Most significant byte of \a u16. -#endif - -usbstick_mode_t usbstick_mode; - -uint8_t mac_createSicslowpanLongAddr(uint8_t * ethernet, uip_lladdr_t * lowpan); -uint8_t mac_createEthernetAddr(uint8_t * ethernet, uip_lladdr_t * lowpan); -/* uint8_t memcmp_reverse(uint8_t * a, uint8_t * b, uint8_t num); */ -/* void mac_ethhijack_nondata(const struct mac_driver *r); */ -/* void mac_ethhijack(const struct mac_driver *r); */ - -/* extern void (*sicslowmac_snifferhook)(const struct mac_driver *r); */ - - -//! Location of TRANSLATE (TR) bit in Ethernet address -#define TRANSLATE_BIT_MASK (1<<2) -//! Location of LOCAL (GL) bit in Ethernet address -#define LOCAL_BIT_MASK (1<<1) -//! Location of MULTICAST (MU) bit in Ethernet address -#define MULTICAST_BIT_MASK (1<<0) - -#define PREFIX_BUFFER_SIZE 32 - -uint8_t prefixCounter; -uint8_t prefixBuffer[PREFIX_BUFFER_SIZE][3]; - -/* 6lowpan max size + ethernet header size + 1 */ -uint8_t raw_buf[127+ UIP_LLH_LEN +1]; - - -/* void tcpip_input( void ) */ -/* { */ -/* mac_LowpanToEthernet(); */ -/* } */ - -/** - * \brief Perform any setup needed - */ -/* struct mac_driver * pmac; */ -void mac_ethernetSetup(void) -{ - usbstick_mode.sicslowpan = 1; - usbstick_mode.sendToRf = 1; - usbstick_mode.translate = 1; - usbstick_mode.raw = 1; - -/* sicslowinput = pinput; */ - - -/* pmac = sicslowmac_get_driver(); */ -/* pmac->set_receive_function(mac_ethhijack); */ -/* sicslowmac_snifferhook = mac_ethhijack_nondata; */ -} - - -/** - * \brief Take a packet received over the ethernet link, and send it - * out over 802.15.4 - */ -void mac_ethernetToLowpan(uint8_t * ethHeader) -{ - //Dest address - uip_lladdr_t destAddr; - uip_lladdr_t *destAddrPtr = NULL; - - PRINTF("Packet type: %x\n", ((struct uip_eth_hdr *) ethHeader)->type); - - //RUM doesn't support sending data - #if UIP_CONF_USE_RUM - return; - #endif - - //If not IPv6 we don't do anything - if (((struct uip_eth_hdr *) ethHeader)->type != UIP_HTONS(UIP_ETHTYPE_IPV6)) { - PRINTF("eth2low: Packet is not IPv6, dropping\n"); -/* rndis_stat.txbad++; */ - uip_clear_buf(); - return; - } - - // In sniffer mode we don't ever send anything - if (usbstick_mode.sendToRf == 0) { - uip_clear_buf(); - return; - } - - /* IPv6 uses 33-33-xx-xx-xx-xx prefix for multicast ND stuff */ - if ( (((struct uip_eth_hdr *) ethHeader)->dest.addr[0] == 0x33) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[1] == 0x33) ) - { - PRINTF("eth2low: Ethernet multicast packet received\n"); - ;//Do Nothing - } else if ( (((struct uip_eth_hdr *) ethHeader)->dest.addr[0] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[1] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[2] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[3] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[4] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[5] == 0xFF) ) { - /* IPv6 does not use broadcast addresses, hence this should not happen */ - PRINTF("eth2low: Ethernet broadcast address received, should not happen?\n"); -/* rndis_stat.txbad++; */ - uip_clear_buf(); - return; - } else { - PRINTF("eth2low: Addressed packet received... "); - //Check this returns OK - if (mac_createSicslowpanLongAddr( &(((struct uip_eth_hdr *) ethHeader)->dest.addr[0]), &destAddr) == 0) { - PRINTF(" translation failed\n"); -/* rndis_stat.txbad++; */ - uip_clear_buf(); - return; - } - PRINTF(" translated OK\n"); - destAddrPtr = &destAddr; - } - - //Remove header from length before passing onward - if(uip_len > UIP_LLH_LEN) { - uip_len -= UIP_LLH_LEN; - - //Some IP packets have link layer in them, need to change them around! - if (usbstick_mode.translate) { - /* uint8_t transReturn = */ - mac_translateIPLinkLayer(ll_802154_type); - PRINTF("IPTranslation: returns %d\n", transReturn); - } - - if (usbstick_mode.sendToRf){ - tcpip_output(destAddrPtr); - /* rndis_stat.txok++; */ - } - } - - uip_clear_buf(); - -} - - -/** - * \brief Take a packet received over the 802.15.4 link, and send it - * out over ethernet, performing any translations needed. - */ -void mac_LowpanToEthernet(void) -{ -/* parsed_frame = sicslowmac_get_frame(); */ - - //Setup generic ethernet stuff - ETHBUF(uip_buf)->type = uip_htons(UIP_ETHTYPE_IPV6); - - //Check for broadcast message - if(packetbuf_holds_broadcast()) { -/* if( ( parsed_frame->fcf->destAddrMode == SHORTADDRMODE) && */ -/* ( parsed_frame->dest_addr->addr16 == 0xffff) ) { */ - ETHBUF(uip_buf)->dest.addr[0] = 0x33; - ETHBUF(uip_buf)->dest.addr[1] = 0x33; - ETHBUF(uip_buf)->dest.addr[2] = UIP_IP_BUF->destipaddr.u8[12]; - ETHBUF(uip_buf)->dest.addr[3] = UIP_IP_BUF->destipaddr.u8[13]; - ETHBUF(uip_buf)->dest.addr[4] = UIP_IP_BUF->destipaddr.u8[14]; - ETHBUF(uip_buf)->dest.addr[5] = UIP_IP_BUF->destipaddr.u8[15]; - } else { - //Otherwise we have a real address - mac_createEthernetAddr((uint8_t *) &(ETHBUF(uip_buf)->dest.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); - } - - - mac_createEthernetAddr((uint8_t *) &(ETHBUF(uip_buf)->src.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER)); - - //We only do address translation in network mode! - if (usbstick_mode.translate) { - //Some IP packets have link layer in them, need to change them around! - mac_translateIPLinkLayer(ll_8023_type); - } - - PRINTF("Low2Eth: Sending packet to ethernet\n"); - - uip_len += UIP_LLH_LEN; - -/* rndis_send(uip_buf, uip_len, 1); */ -/* rndis_stat.rxok++; */ -/* uip_clear_buf(); */ -} - -/** - * \brief Translate IP packet's possible link-layer addresses, passing - * the message to the appropriate higher level function for this - * packet (aka: ICMP) - * \param target The target we want to end up with - either ll_8023_type - * for ethernet, or ll_802154_type for 802.15.4 - * \return Returns how successful the translation was - * \retval 0 Addresses, if present, were translated. - * \retval <0 Negative return values indicate various errors, as defined - * by the higher level function. - */ -int8_t mac_translateIPLinkLayer(lltype_t target) -{ - -#if UIP_LLADDR_LEN == 8 - if (UIP_IP_BUF->proto == UIP_PROTO_ICMP6) { - PRINTF("eth2low: ICMP Message detected\n"); - return mac_translateIcmpLinkLayer(target); - } - return 0; -#else - return 1; -#endif - -} - -#include "net/ipv6/uip-icmp6.h" -#include "net/ipv6/uip-nd6.h" - -typedef struct { - uint8_t type; - uint8_t length; - uint8_t data[16]; -} icmp_opts_t; - -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#define UIP_ICMP_OPTS(x) ((icmp_opts_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + x]) - -void slide(uint8_t * data, uint8_t length, int16_t slide); - -/** - * \brief Translate the link-layer (L2) addresses in an ICMP packet. - * This will just be NA/NS/RA/RS packets currently. - * \param target The target we want to end up with - either ll_8023_type - * for ethernet, or ll_802154_type for 802.15.4 - * \return Returns how successful the translation was - * \retval 0 Addresses, if present, were translated. - * \retval -1 ICMP message was unknown type, nothing done. - * \retval -2 ICMP Length does not make sense? - * \retval -3 Unknown 'target' type - */ -int8_t mac_translateIcmpLinkLayer(lltype_t target) -{ - uint16_t icmp_opt_offset = 0; - int16_t len = UIP_IP_BUF->len[1] | (UIP_IP_BUF->len[0] << 8); - - uint16_t iplen; - - uint8_t i; - - int16_t sizechange; - - uint8_t llbuf[16]; - - //Figure out offset to start of options - switch(UIP_ICMP_BUF->type) { - case ICMP6_NS: - case ICMP6_NA: - icmp_opt_offset = 24; - break; - - case ICMP6_RS: - icmp_opt_offset = 8; - break; - - case ICMP6_RA: - icmp_opt_offset = 16; - break; - - case ICMP6_REDIRECT: - icmp_opt_offset = 40; - break; - - /** Things without link-layer */ - case ICMP6_DST_UNREACH: - case ICMP6_PACKET_TOO_BIG: - case ICMP6_TIME_EXCEEDED: - case ICMP6_PARAM_PROB: - case ICMP6_ECHO_REQUEST: - case ICMP6_ECHO_REPLY: - return 0; - break; - - default: - return -1; - } - - //Figure out length of options - len -= icmp_opt_offset; - - //Sanity check - if (len < 8) return -2; - - //While we have options to do... - while (len >= 8){ - - //If we have one of these, we have something useful! - if (((UIP_ICMP_OPTS(icmp_opt_offset)->type) == UIP_ND6_OPT_SLLAO) || - ((UIP_ICMP_OPTS(icmp_opt_offset)->type) == UIP_ND6_OPT_TLLAO) ) { - - /* Shrinking the buffer may thrash things, so we store the old - link-layer address */ - for(i = 0; i < (UIP_ICMP_OPTS(icmp_opt_offset)->length*8 - 2); i++) { - llbuf[i] = UIP_ICMP_OPTS(icmp_opt_offset)->data[i]; - } - - //Shrink/grow buffer as needed - if (target == ll_802154_type) { - //Current is 802.3, Hence current link-layer option is 6 extra bytes - sizechange = 8; - slide(UIP_ICMP_OPTS(icmp_opt_offset)->data + 6, len - 6, sizechange); - } else if (target == ll_8023_type) { - /* Current is 802.15.4, Hence current link-layer option is 14 extra - * bytes. - * (Actual LL is 8 bytes, but total option length is in multiples of - * 8 Bytes, hence 8 + 2 = 10. Closest is 16 bytes, then 16 bytes for - * total optional length - 2 bytes for type + length leaves 14 ) - */ - sizechange = -8; - slide(UIP_ICMP_OPTS(icmp_opt_offset)->data + 14, len - 14, sizechange); - } else { - return -3; //Uh-oh! - } - - //Translate addresses - if (target == ll_802154_type) { - mac_createSicslowpanLongAddr(llbuf, (uip_lladdr_t *)UIP_ICMP_OPTS(icmp_opt_offset)->data); - } else { - mac_createEthernetAddr(UIP_ICMP_OPTS(icmp_opt_offset)->data, (uip_lladdr_t *)llbuf); - } - - //Adjust the length - if (target == ll_802154_type) { - UIP_ICMP_OPTS(icmp_opt_offset)->length = 2; - } else { - UIP_ICMP_OPTS(icmp_opt_offset)->length = 1; - } - - //Adjust the IP header length, as well as uIP length - iplen = UIP_IP_BUF->len[1] | (UIP_IP_BUF->len[0]<<8); - iplen += sizechange; - len += sizechange; - - UIP_IP_BUF->len[1] = (uint8_t)iplen; - UIP_IP_BUF->len[0] = (uint8_t)(iplen >> 8); - - uip_len += sizechange; - - //We broke ICMP checksum, be sure to fix that - UIP_ICMP_BUF->icmpchksum = 0; - UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum(); - - //Finally set up next run in while loop - len -= 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - icmp_opt_offset += 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - } else { - - //Not an option we care about, ignore it - len -= 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - - //This shouldn't happen! - if (UIP_ICMP_OPTS(icmp_opt_offset)->length == 0) { - PRINTF("Option in ND packet has length zero, error?\n"); - len = 0; - } - - icmp_opt_offset += 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - - } //If ICMP_OPT is one we care about - - } //while(len >= 8) - - return 0; - -} - - -/** - * \brief Create a 802.15.4 long address from a 802.3 address - * \param ethernet Pointer to ethernet address - * \param lowpan Pointer to 802.15.4 address - */ -uint8_t mac_createSicslowpanLongAddr(uint8_t * ethernet, uip_lladdr_t * lowpan) -{ - uint8_t index; - -#if UIP_LLADDR_LEN == 8 - //Special case - if the address is our address, we just copy over what we know to be - //our 802.15.4 address - -/* if (memcmp_reverse((uint8_t *)&rndis_ethernet_addr, ethernet, 6) == 0) { */ - if (memcmp((uint8_t *)&uip_lladdr.addr[2], ethernet, 6) == 0) { - memcpy((uint8_t *)lowpan, uip_lladdr.addr, 8); -/* byte_reverse((uint8_t *)lowpan, 8); */ - - return 1; - } - - //Check if translate bit is set, hence we have to look up the prefix - if (ethernet[0] & TRANSLATE_BIT_MASK) { - - //Get top bits - index = ethernet[0] >> 3; - - //Check this is plausible... - if (index >= prefixCounter) { - return 0; - } - - //Copy over prefix - lowpan->addr[0] = prefixBuffer[index][0]; - lowpan->addr[3] = prefixBuffer[index][1]; - lowpan->addr[4] = prefixBuffer[index][2]; - - //Bit is clear - //so we copy all six - } else { - lowpan->addr[0] = ethernet[0]; - lowpan->addr[3] = 0xff; - lowpan->addr[4] = 0xfe; - } - - //Copy over reamining five bytes - lowpan->addr[1] = ethernet[1]; - lowpan->addr[2] = ethernet[2]; - lowpan->addr[5] = ethernet[3]; - lowpan->addr[6] = ethernet[4]; - lowpan->addr[7] = ethernet[5]; - -#else - uint8_t i; - - for(i = 0; i < UIP_LLADDR_LEN; i++) { - lowpan->addr[i] = ethernet[i]; - } -#endif - - return 1; -} - - -/** - * \brief Create a 802.3 address from a 802.15.4 long address - * \param ethernet Pointer to ethernet address - * \param lowpan Pointer to 802.15.4 address - */ -uint8_t mac_createEthernetAddr(uint8_t * ethernet, uip_lladdr_t * lowpan) -{ - uint8_t index = 0; - uint8_t i; -/* uint8_t j, match; */ - -#if UIP_LLADDR_LEN == 8 - - //Special case - if the address is our address, we just copy over what we know to be - //our 802.3 address -/* if (memcmp_reverse(uip_lladdr.addr, (uint8_t *)lowpan, 8) == 0) { */ - if (memcmp(uip_lladdr.addr, (uint8_t *)lowpan, 8) == 0) { - memcpy(ethernet, &uip_lladdr.addr[2], 6); -/* byte_reverse(ethernet, 6); */ - - return 1; - } - - - //Check if we need to do anything: - if ((lowpan->addr[3] == 0xff) && (lowpan->addr[4] == 0xfe) && - ((lowpan->addr[0] & TRANSLATE_BIT_MASK) == 0) && - ((lowpan->addr[0] & MULTICAST_BIT_MASK) == 0) && - (lowpan->addr[0] & LOCAL_BIT_MASK)) { - - /** Nope: just copy over 6 bytes **/ - ethernet[0] = lowpan->addr[0]; - ethernet[1] = lowpan->addr[1]; - ethernet[2] = lowpan->addr[2]; - ethernet[3] = lowpan->addr[5]; - ethernet[4] = lowpan->addr[6]; - ethernet[5] = lowpan->addr[7]; - - - } else { - - /** Yes: need to store prefix **/ - for (i = 0; i < prefixCounter; i++) { - //Check the current prefix - if it fails, check next one - - - if ((lowpan->addr[0] == prefixBuffer[i][0]) && - (lowpan->addr[3] == prefixBuffer[i][1]) && - (lowpan->addr[4] == prefixBuffer[i][2])) { - break; - } - - } - - index = i; - - //Deal with overflow, iron-fist style - if (index >= PREFIX_BUFFER_SIZE) { - index = 0; - prefixCounter = PREFIX_BUFFER_SIZE; - } else { - //Are we making a new one? - if (index == prefixCounter) { - prefixCounter++; - } - } - - //Copy the prefix over, no matter if we have a new or old one - prefixBuffer[index][0] = lowpan->addr[0]; - prefixBuffer[index][1] = lowpan->addr[3]; - prefixBuffer[index][2] = lowpan->addr[4]; - - //Create ethernet MAC address now - ethernet[1] = lowpan->addr[1]; - ethernet[2] = lowpan->addr[2]; - ethernet[3] = lowpan->addr[5]; - ethernet[4] = lowpan->addr[6]; - ethernet[5] = lowpan->addr[7]; - - - ethernet[0] = TRANSLATE_BIT_MASK | LOCAL_BIT_MASK | (index << 3); - } - -#else - - //Create ethernet MAC address now - for(i = 0; i < UIP_LLADDR_LEN; i++) { - ethernet[i] = lowpan->addr[i]; - } -#endif - - return 1; -} - - -/** - * \brief Slide the pointed to memory up a certain amount, - * growing/shrinking a buffer - * \param data Pointer to start of data buffer - * \param length Length of the data buffer - * \param slide How many bytes to slide the buffer up in memory (if +) or - * down in memory (if -) - */ -void slide(uint8_t * data, uint8_t length, int16_t slide) -{ - //Sanity checks - if (!length) return; - if (!slide) return; - - uint8_t i = 0; - - while(length) { - length--; - - //If we are sliding up, we do from the top of the buffer down - if (slide > 0) { - *(data + length + slide) = *(data + length); - - //If we are sliding down, we do from the bottom of the buffer up - } else { - *(data + slide + i) = *(data + i); - } - - i++; - } -} - -/*--------------------------------------------------------------------*/ -/** \brief Process a received 6lowpan packet. Hijack function. - * \param r The MAC layer - * - * The 6lowpan packet is put in packetbuf by the MAC. This routine calls - * any other needed layers (either 6lowpan, or just raw ethernet dump) - */ -#if 0 -void mac_ethhijack(const struct mac_driver *r) -{ - if (usbstick_mode.raw) { - mac_802154raw(r); - } - - if (usbstick_mode.sicslowpan) { - -#if UIP_CONF_USE_RUM - if (parsed_frame->payload[4]) { /* RUM 6lowpan frame type */ -#endif - sicslowinput(r); -#if UIP_CONF_USE_RUM - } -#endif - - - } - -} -#endif - -#if 0 -void mac_ethhijack_nondata(const struct mac_driver *r) -{ - if (usbstick_mode.raw) - mac_802154raw(r); -} -#endif - -/*--------------------------------------------------------------------*/ -/*--------------------------------------------------------------------*/ -/** \brief Logs a sent 6lowpan frame - * - * This routine passes a frame - * directly to the ethernet layer without decompressing. - */ -#if 0 -void mac_logTXtoEthernet(frame_create_params_t *p,frame_result_t *frame_result) -{ - uint8_t sendlen; - - /* Make sure we are supposed to do this */ - if (usbstick_mode.raw == 0) return; - -/* Get the raw frame */ - memcpy(&raw_buf[UIP_LLH_LEN], frame_result->frame, frame_result->length); - sendlen = frame_result->length; - - //Setup generic ethernet stuff - ETHBUF(raw_buf)->type = uip_htons(UIP_ETHTYPE_802154); - - uint64_t tempaddr; - - - //Check for broadcast message - //if(linkaddr_cmp((const linkaddr_t *)destAddr, &linkaddr_null)) { - if( ( p->fcf.destAddrMode == SHORTADDRMODE) && - ( p->dest_addr.addr16 == 0xffff) ) { - ETHBUF(raw_buf)->dest.addr[0] = 0x33; - ETHBUF(raw_buf)->dest.addr[1] = 0x33; - ETHBUF(raw_buf)->dest.addr[2] = UIP_IP_BUF->destipaddr.u8[12]; - ETHBUF(raw_buf)->dest.addr[3] = UIP_IP_BUF->destipaddr.u8[13]; - ETHBUF(raw_buf)->dest.addr[4] = UIP_IP_BUF->destipaddr.u8[14]; - ETHBUF(raw_buf)->dest.addr[5] = UIP_IP_BUF->destipaddr.u8[15]; - } else { - - tempaddr = p->dest_addr.addr64; - - byte_reverse((uint8_t *)&tempaddr, 8); - - //Otherwise we have a real address - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->dest.addr[0]), - (uip_lladdr_t *)&tempaddr); - - } - - tempaddr = p->src_addr.addr64; - - byte_reverse((uint8_t *)&tempaddr, 8); - - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->src.addr[0]), - (uip_lladdr_t *)&tempaddr); - - PRINTF("Low2Eth: Sending 802.15.4 packet to ethernet\n"); - - sendlen += UIP_LLH_LEN; - - rndis_send(raw_buf, sendlen, 0); - rndis_stat.rxok++; - return; -} -#endif - -/*--------------------------------------------------------------------*/ -/** \brief Process a received 6lowpan packet. - * \param r The MAC layer - * - * The 6lowpan packet is put in packetbuf by the MAC. This routine passes - * it directly to the ethernet layer without decompressing. - */ -#if 0 -void mac_802154raw(const struct mac_driver *r) -{ - uint8_t sendlen; - - parsed_frame = sicslowmac_get_frame(); - - /* Get the raw frame */ - memcpy(&raw_buf[UIP_LLH_LEN], radio_frame_data(), radio_frame_length()); - sendlen = radio_frame_length(); - - //Setup generic ethernet stuff - ETHBUF(raw_buf)->type = uip_htons(UIP_ETHTYPE_802154); - - - //Check for broadcast message - //if(linkaddr_cmp((const linkaddr_t *)destAddr, &linkaddr_null)) { - if( ( parsed_frame->fcf->destAddrMode == SHORTADDRMODE) && - ( parsed_frame->dest_addr->addr16 == 0xffff) ) { - ETHBUF(raw_buf)->dest.addr[0] = 0x33; - ETHBUF(raw_buf)->dest.addr[1] = 0x33; - ETHBUF(raw_buf)->dest.addr[2] = UIP_IP_BUF->destipaddr.u8[12]; - ETHBUF(raw_buf)->dest.addr[3] = UIP_IP_BUF->destipaddr.u8[13]; - ETHBUF(raw_buf)->dest.addr[4] = UIP_IP_BUF->destipaddr.u8[14]; - ETHBUF(raw_buf)->dest.addr[5] = UIP_IP_BUF->destipaddr.u8[15]; - } else { - - //Otherwise we have a real address - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->dest.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); - - } - - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->src.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER)); - - PRINTF("Low2Eth: Sending 802.15.4 packet to ethernet\n"); - - sendlen += UIP_LLH_LEN; - - rndis_send(raw_buf, sendlen, 1); - rndis_stat.rxok++; - - return; -} -#endif -/** @} */ -/** @} */ diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.h b/tools/sky/uip6-bridge/sicslow_ethernet.h deleted file mode 100644 index 2e48c188e..000000000 --- a/tools/sky/uip6-bridge/sicslow_ethernet.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * \file sicslow_ethernet.c - * Routines to interface between Ethernet and 6LowPan - * - * \author - * Colin O'Flynn - * - * \addtogroup usbstick - */ - -/* Copyright (c) 2008 by: - - Colin O'Flynn coflynn@newae.com - Eric Gnoske egnoske@gmail.com - Blake Leverett bleverett@gmail.com - Mike Vidales mavida404@gmail.com - Kevin Brown kbrown3@uccs.edu - Nate Bohlmann nate@elfwerks.com - - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - * Neither the name of the copyright holders nor the names of - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef SICSLOW_ETHERNET_H -#define SICSLOW_ETHERNET_H - -typedef enum { - ll_802154_type, - ll_8023_type -} lltype_t; - - -typedef struct { - uint8_t sicslowpan :1; - uint8_t sendToRf :1; - uint8_t translate :1; - uint8_t raw :1; -} usbstick_mode_t; - - -#define UIP_ETHTYPE_802154 0x809A - -extern usbstick_mode_t usbstick_mode; - - -int8_t mac_translateIcmpLinkLayer(lltype_t target); -int8_t mac_translateIPLinkLayer(lltype_t target); -void mac_LowpanToEthernet(void); -void mac_ethernetToLowpan(uint8_t * ethHeader); -void mac_ethernetSetup(void); -/* void mac_802154raw(const struct mac_driver *r); */ -/* void mac_logTXtoEthernet(frame_create_params_t *p,frame_result_t *frame_result); */ - -#endif diff --git a/tools/wpcapslip/wpcap.c b/tools/wpcapslip/wpcap.c index 54fc4e873..0d667d617 100644 --- a/tools/wpcapslip/wpcap.c +++ b/tools/wpcapslip/wpcap.c @@ -451,7 +451,7 @@ arp_out(struct ethip_hdr *iphdr, int len) arphdr->protolen = 4; arphdr->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP); - /* uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];*/ + /* uip_appdata = &uip_buf[UIP_IPTCPH_LEN];*/ return sizeof(struct arp_hdr); } diff --git a/tools/zolertia/z1-bsl b/tools/zolertia/z1-bsl new file mode 100755 index 000000000..31d58d1df --- /dev/null +++ b/tools/zolertia/z1-bsl @@ -0,0 +1,1929 @@ +#!/usr/bin/env python +# Serial Bootstrap Loader software for the MSP430 embedded proccessor. +# +# (C) 2001-2003 Chris Liechti +# this is distributed under a free software license, see license-bsl.txt +# +# fixes from Colin Domoney +# +# based on the application note slas96b.pdf from Texas Instruments, Inc., +# Volker Rzehak +# additional infos from slaa089a.pdf +# +# Forked by Travis Goodspeed for use with the GoodFET +# JTAG programmer. + +import sys, time, string, cStringIO, struct +sys.path.append("/usr/lib/tinyos") +import serial, os, glob + +#forked from TinyOS Telos version. +VERSION = string.split("Revision: 1.39-goodfet-8 z1 fork")[1] + +DEBUG = 0 #disable debug messages by default + +#copy of the patch file provided by TI +#this part is (C) by Texas Instruments +PATCH = """@0220 +31 40 1A 02 09 43 B0 12 2A 0E B0 12 BA 0D 55 42 +0B 02 75 90 12 00 1F 24 B0 12 BA 02 55 42 0B 02 +75 90 16 00 16 24 75 90 14 00 11 24 B0 12 84 0E +06 3C B0 12 94 0E 03 3C 21 53 B0 12 8C 0E B2 40 +10 A5 2C 01 B2 40 00 A5 28 01 30 40 42 0C 30 40 +76 0D 30 40 AC 0C 16 42 0E 02 17 42 10 02 E2 B2 +08 02 14 24 B0 12 10 0F 36 90 00 10 06 28 B2 40 +00 A5 2C 01 B2 40 40 A5 28 01 D6 42 06 02 00 00 +16 53 17 83 EF 23 B0 12 BA 02 D3 3F B0 12 10 0F +17 83 FC 23 B0 12 BA 02 D0 3F 18 42 12 02 B0 12 +10 0F D2 42 06 02 12 02 B0 12 10 0F D2 42 06 02 +13 02 38 E3 18 92 12 02 BF 23 E2 B3 08 02 BC 23 +30 41 +q +""" + +#These BSL's are (C) by TI. They come with the application note slaa089a +F1X_BSL = """@0220 +24 02 2E 02 31 40 20 02 2B D2 C0 43 EA FF 32 C2 +F2 C0 32 00 00 00 B2 40 80 5A 20 01 F2 40 85 00 +57 00 F2 40 80 00 56 00 E2 D3 21 00 E2 D3 22 00 +E2 C3 26 00 E2 C2 2A 00 E2 C2 2E 00 B2 40 10 A5 +2C 01 B2 40 00 A5 28 01 3B C0 3A 00 B0 12 D6 04 +82 43 12 02 09 43 36 40 0A 02 37 42 B0 12 AC 05 +C6 4C 00 00 16 53 17 83 F9 23 D2 92 0C 02 0D 02 +28 20 55 42 0B 02 75 90 12 00 80 24 75 90 10 00 +6D 24 B0 12 9C 04 55 42 0B 02 75 90 18 00 31 24 +75 90 1E 00 B8 24 75 90 20 00 17 24 2B B2 11 24 +75 90 16 00 22 24 75 90 14 00 B3 24 75 90 1A 00 +18 24 75 90 1C 00 45 24 04 3C B0 12 36 05 BE 3F +21 53 B0 12 3C 05 BA 3F 03 43 B0 12 36 05 D2 42 +0E 02 56 00 D2 42 0F 02 57 00 D2 42 10 02 16 02 +AD 3F B0 12 36 05 10 42 0E 02 16 42 0E 02 15 43 +07 3C 36 40 FE FF B2 40 06 A5 10 02 35 40 0C 00 +B2 40 00 A5 2C 01 92 42 10 02 28 01 B6 43 00 00 +92 B3 2C 01 FD 23 15 83 F3 23 36 90 FE FF CD 27 +37 40 80 00 36 F0 80 FF 36 90 00 11 0E 28 07 57 +36 F0 00 FF 36 90 00 12 08 28 07 57 36 F0 00 FE +04 3C 16 42 0E 02 17 42 10 02 35 43 75 96 03 20 +17 83 FC 23 B2 3F 82 46 00 02 B3 3F 36 40 E0 FF +37 40 20 00 B0 12 AC 05 7C 96 01 24 2B D3 17 83 +F9 23 2B C2 B0 12 9C 04 2B D2 9F 3F 16 42 0E 02 +17 42 10 02 2B B2 38 24 3B D0 10 00 B0 12 AC 05 +36 90 00 10 06 2C 36 90 00 01 09 2C C6 4C 00 00 +25 3C B2 40 00 A5 2C 01 B2 40 40 A5 28 01 16 B3 +03 20 C2 4C 14 02 1A 3C C2 4C 15 02 86 9A FD FF +08 24 2B D3 3B B0 20 00 04 20 3B D0 20 00 82 46 +00 02 36 90 01 02 04 28 3B D2 3B B0 10 00 02 24 +3B C0 32 00 1A 42 14 02 86 4A FF FF 16 53 17 83 +CD 23 B0 12 9C 04 61 3F B0 12 AC 05 17 83 FC 23 +B0 12 9C 04 5E 3F B2 40 F0 0F 0E 02 B2 40 10 00 +10 02 B2 40 80 00 0A 02 D2 42 10 02 0C 02 D2 42 +10 02 0D 02 82 43 12 02 09 43 36 40 0A 02 27 42 +7C 46 B0 12 40 05 17 83 FB 23 16 42 0E 02 17 42 +10 02 36 90 00 01 0A 28 B2 46 14 02 5C 42 14 02 +B0 12 40 05 17 83 5C 42 15 02 01 3C 7C 46 B0 12 +40 05 17 83 EE 23 B2 E3 12 02 5C 42 12 02 B0 12 +40 05 5C 42 13 02 B0 12 40 05 E0 3E 18 42 12 02 +B0 12 AC 05 C2 4C 12 02 B0 12 AC 05 C2 4C 13 02 +38 E3 3B B2 0A 24 86 9A FE FF 07 24 3B B0 20 00 +04 20 16 53 82 46 00 02 2B D3 18 92 12 02 08 23 +2B B3 06 23 30 41 E2 B2 28 00 FD 27 E2 B2 28 00 +FD 23 B2 40 24 02 60 01 E2 B2 28 00 FD 27 15 42 +70 01 05 11 05 11 05 11 82 45 02 02 05 11 82 45 +04 02 B2 80 1E 00 04 02 57 42 16 02 37 80 03 00 +05 11 05 11 17 53 FD 23 35 50 40 A5 82 45 2A 01 +35 42 B2 40 24 02 60 01 92 92 70 01 02 02 FC 2F +15 83 F7 23 09 43 7C 40 90 00 02 3C 7C 40 A0 00 +C2 43 07 02 C9 EC 12 02 19 E3 1B C3 55 42 07 02 +55 45 56 05 00 55 0C 2E 2E 2E 2E 2E 2E 2E 2E 1A +34 34 92 42 70 01 72 01 B2 50 0C 00 72 01 07 3C +1B B3 0B 20 82 43 62 01 92 B3 62 01 FD 27 E2 C3 +21 00 0A 3C 4C 11 F6 2B 1B E3 82 43 62 01 92 B3 +62 01 FD 27 E2 D3 21 00 92 52 02 02 72 01 D2 53 +07 02 F0 90 0C 00 61 FC D1 23 30 41 C2 43 09 02 +1B C3 55 42 09 02 55 45 BC 05 00 55 0C 56 56 56 +56 56 56 56 56 36 76 00 E2 B2 28 00 FD 23 92 42 +70 01 72 01 92 52 04 02 72 01 82 43 62 01 92 B3 +62 01 FD 27 E2 B2 28 00 1E 28 2B D3 1C 3C 4C 10 +1A 3C 82 43 62 01 92 B3 62 01 FD 27 E2 B2 28 00 +01 28 1B E3 1B B3 01 24 2B D3 C9 EC 12 02 19 E3 +0A 3C 82 43 62 01 92 B3 62 01 FD 27 E2 B2 28 00 +E6 2B 4C 10 1B E3 92 52 02 02 72 01 D2 53 09 02 +C0 3F 82 43 62 01 92 B3 62 01 FD 27 E2 B2 28 00 +01 2C 2B D3 30 41 +q +""" + +F4X_BSL = """@0220 +24 02 2E 02 31 40 20 02 2B D2 C0 43 EA FF 32 C2 +F2 C0 32 00 00 00 B2 40 80 5A 20 01 32 D0 40 00 +C2 43 50 00 F2 40 98 00 51 00 F2 C0 80 00 52 00 +D2 D3 21 00 D2 D3 22 00 D2 C3 26 00 E2 C3 22 00 +E2 C3 26 00 B2 40 10 A5 2C 01 B2 40 00 A5 28 01 +3B C0 3A 00 B0 12 DE 04 82 43 12 02 09 43 36 40 +0A 02 37 42 B0 12 B4 05 C6 4C 00 00 16 53 17 83 +F9 23 D2 92 0C 02 0D 02 28 20 55 42 0B 02 75 90 +12 00 80 24 75 90 10 00 6D 24 B0 12 A4 04 55 42 +0B 02 75 90 18 00 31 24 75 90 1E 00 B8 24 75 90 +20 00 17 24 2B B2 11 24 75 90 16 00 22 24 75 90 +14 00 B3 24 75 90 1A 00 18 24 75 90 1C 00 45 24 +04 3C B0 12 3E 05 BE 3F 21 53 B0 12 44 05 BA 3F +03 43 B0 12 3E 05 D2 42 0E 02 50 00 D2 42 0F 02 +51 00 D2 42 10 02 16 02 AD 3F B0 12 3E 05 10 42 +0E 02 16 42 0E 02 15 43 07 3C 36 40 FE FF B2 40 +06 A5 10 02 35 40 0C 00 B2 40 00 A5 2C 01 92 42 +10 02 28 01 B6 43 00 00 92 B3 2C 01 FD 23 15 83 +F3 23 36 90 FE FF CD 27 37 40 80 00 36 F0 80 FF +36 90 00 11 0E 28 07 57 36 F0 00 FF 36 90 00 12 +08 28 07 57 36 F0 00 FE 04 3C 16 42 0E 02 17 42 +10 02 35 43 75 96 03 20 17 83 FC 23 B2 3F 82 46 +00 02 B3 3F 36 40 E0 FF 37 40 20 00 B0 12 B4 05 +7C 96 01 24 2B D3 17 83 F9 23 2B C2 B0 12 A4 04 +2B D2 9F 3F 16 42 0E 02 17 42 10 02 2B B2 38 24 +3B D0 10 00 B0 12 B4 05 36 90 00 10 06 2C 36 90 +00 01 09 2C C6 4C 00 00 25 3C B2 40 00 A5 2C 01 +B2 40 40 A5 28 01 16 B3 03 20 C2 4C 14 02 1A 3C +C2 4C 15 02 86 9A FD FF 08 24 2B D3 3B B0 20 00 +04 20 3B D0 20 00 82 46 00 02 36 90 01 02 04 28 +3B D2 3B B0 10 00 02 24 3B C0 32 00 1A 42 14 02 +86 4A FF FF 16 53 17 83 CD 23 B0 12 A4 04 61 3F +B0 12 B4 05 17 83 FC 23 B0 12 A4 04 5E 3F B2 40 +F0 0F 0E 02 B2 40 10 00 10 02 B2 40 80 00 0A 02 +D2 42 10 02 0C 02 D2 42 10 02 0D 02 82 43 12 02 +09 43 36 40 0A 02 27 42 7C 46 B0 12 48 05 17 83 +FB 23 16 42 0E 02 17 42 10 02 36 90 00 01 0A 28 +B2 46 14 02 5C 42 14 02 B0 12 48 05 17 83 5C 42 +15 02 01 3C 7C 46 B0 12 48 05 17 83 EE 23 B2 E3 +12 02 5C 42 12 02 B0 12 48 05 5C 42 13 02 B0 12 +48 05 E0 3E 18 42 12 02 B0 12 B4 05 C2 4C 12 02 +B0 12 B4 05 C2 4C 13 02 38 E3 3B B2 0A 24 86 9A +FE FF 07 24 3B B0 20 00 04 20 16 53 82 46 00 02 +2B D3 18 92 12 02 08 23 2B B3 06 23 30 41 E2 B3 +20 00 FD 27 E2 B3 20 00 FD 23 B2 40 24 02 60 01 +E2 B3 20 00 FD 27 15 42 70 01 05 11 05 11 05 11 +82 45 02 02 05 11 82 45 04 02 B2 80 1E 00 04 02 +57 42 16 02 37 80 03 00 05 11 05 11 17 53 FD 23 +35 50 40 A5 82 45 2A 01 35 42 B2 40 24 02 60 01 +92 92 70 01 02 02 FC 2F 15 83 F7 23 09 43 7C 40 +90 00 02 3C 7C 40 A0 00 C2 43 07 02 C9 EC 12 02 +19 E3 1B C3 55 42 07 02 55 45 5E 05 00 55 0C 2E +2E 2E 2E 2E 2E 2E 2E 1A 34 34 92 42 70 01 72 01 +B2 50 0C 00 72 01 07 3C 1B B3 0B 20 82 43 62 01 +92 B3 62 01 FD 27 D2 C3 21 00 0A 3C 4C 11 F6 2B +1B E3 82 43 62 01 92 B3 62 01 FD 27 D2 D3 21 00 +92 52 02 02 72 01 D2 53 07 02 F0 90 0C 00 59 FC +D1 23 30 41 C2 43 09 02 1B C3 55 42 09 02 55 45 +C4 05 00 55 0C 56 56 56 56 56 56 56 56 36 76 00 +E2 B3 20 00 FD 23 92 42 70 01 72 01 92 52 04 02 +72 01 82 43 62 01 92 B3 62 01 FD 27 E2 B3 20 00 +1E 28 2B D3 1C 3C 4C 10 1A 3C 82 43 62 01 92 B3 +62 01 FD 27 E2 B3 20 00 01 28 1B E3 1B B3 01 24 +2B D3 C9 EC 12 02 19 E3 0A 3C 82 43 62 01 92 B3 +62 01 FD 27 E2 B3 20 00 E6 2B 4C 10 1B E3 92 52 +02 02 72 01 D2 53 09 02 C0 3F 82 43 62 01 92 B3 +62 01 FD 27 E2 B3 20 00 01 2C 2B D3 30 41 +q +""" + +CALIB_TABLES = """ +84 C0 16 FE FF FF FF FF FF FF FF FF FF FF FF FF +FF FF FF FF FF FF FF FF FF FF 10 08 80 00 00 01 +7E 27 0B 79 0D AA 7E 4D 06 DF 08 33 08 FE FF FF +FF FF FF FF FF FF 08 01 8F 95 8E 9A 8D 88 86 D1 +""" + +#cpu types for "change baudrate" +#use strings as ID so that they can be used in outputs too +F1x = "F1x family" +F2x = "F2x family" +F4x = "F4x family" + +#known device list +deviceids = { + 0xf149: F1x, + 0xf16c: F1x, #for GoodFET10 to '20 + 0xf112: F1x, + 0xf413: F4x, + 0xf123: F1x, + 0xf449: F4x, + 0x1232: F1x, + 0xf26f: F2x, #for GoodFET20 + 0xf227: F2x, #for GoodFET30 +} + +#GoodFET firmware +firmware = { + 0xf16c: "http://goodfet.sourceforge.net/dist/msp430x1612.hex", + 0xf26f: "http://goodfet.sourceforge.net/dist/msp430x2618.hex", + 0xf227: "http://goodfet.sourceforge.net/dist/msp430x2274.hex" +} + +class BSLException(Exception): + pass + +class LowLevel: + "lowlevel communication" + #Constants + MODE_SSP = 0 + MODE_BSL = 1 + + BSL_SYNC = 0x80 + BSL_TXPWORD = 0x10 + BSL_TXBLK = 0x12 #Transmit block to boot loader + BSL_UNLOCK_INFO = 0x12 #unlocks / locks info segment A Not working on msp430f2617 + BSL_RXBLK = 0x14 #Receive block from boot loader + BSL_ERASE = 0x16 #Erase one segment + BSL_MERAS = 0x18 #Erase complete FLASH memory + BSL_CHANGEBAUD = 0x20 #Change baudrate + BSL_LOADPC = 0x1A #Load PC and start execution + BSL_TXVERSION = 0x1E #Get BSL version + + #Upper limit of address range that might be modified by + #"BSL checksum bug". + BSL_CRITICAL_ADDR = 0x0A00 + + #Header Definitions + CMD_FAILED = 0x70 + DATA_FRAME = 0x80 + DATA_ACK = 0x90 + DATA_NAK = 0xA0 + + QUERY_POLL = 0xB0 + QUERY_RESPONSE = 0x50 + + OPEN_CONNECTION = 0xC0 + ACK_CONNECTION = 0x40 + + DEFAULT_TIMEOUT = 1 + DEFAULT_PROLONG = 10 + MAX_FRAME_SIZE = 256 + MAX_DATA_BYTES = 250 + MAX_DATA_WORDS = 125 + + MAX_FRAME_COUNT = 16 + + #Error messages + ERR_COM = "Unspecific error" + ERR_RX_NAK = "NAK received (wrong password?)" + #ERR_CMD_NOT_COMPLETED = "Command did not send ACK: indicates that it didn't complete correctly" + ERR_CMD_FAILED = "Command failed, is not defined or is not allowed" + ERR_BSL_SYNC = "Bootstrap loader synchronization error" + ERR_FRAME_NUMBER = "Frame sequence number error." + + def calcChecksum(self, data, length): + """Calculates a checksum of "data".""" + checksum = 0 + + for i in range(length/2): + checksum = checksum ^ (ord(data[i*2]) | (ord(data[i*2+1]) << 8)) #xor-ing + return 0xffff & (checksum ^ 0xffff) #inverting + + def __init__(self, aTimeout = None, aProlongFactor = None): + """init bsl object, don't connect yet""" + if aTimeout is None: + self.timeout = self.DEFAULT_TIMEOUT + else: + self.timeout = aTimeout + if aProlongFactor is None: + self.prolongFactor = self.DEFAULT_PROLONG + else: + self.prolongFactor = aProlongFactor + + #flags for inverted use of control pins + #used for some hardware + self.invertRST = 0 + self.invertTEST = 0 + self.swapRSTTEST = 0 + self.telosLatch = 0 + self.telosI2C = 0 + + self.z1 = 0 + + self.protocolMode = self.MODE_BSL + self.BSLMemAccessWarning = 0 #Default: no warning. + self.slowmode = 0 + + def comInit(self, port): + """Tries to open the serial port given and + initialises the port and variables. + The timeout and the number of allowed errors is multiplied by + 'aProlongFactor' after transmission of a command to give + plenty of time to the micro controller to finish the command. + Returns zero if the function is successful.""" + if DEBUG > 1: sys.stderr.write("* comInit()\n") + self.seqNo = 0 + self.reqNo = 0 + self.rxPtr = 0 + self.txPtr = 0 + # Startup-Baudrate: 9600,8,E,1, 1s timeout + self.serialport = serial.Serial( + port, + 9600, + parity = serial.PARITY_EVEN, + timeout = self.timeout + ) + if DEBUG: sys.stderr.write("using serial port %r\n" % self.serialport.portstr) + #self.SetRSTpin() #enable power + #self.SetTESTpin() #enable power + self.serialport.flushInput() + self.serialport.flushOutput() + + def comDone(self): + """Closes the used serial port. + This function must be called at the end of a program, + otherwise the serial port might not be released and can not be + used in other programs. + Returns zero if the function is successful.""" + if DEBUG > 1: sys.stderr.write("* comDone()") + self.SetRSTpin(1) #disable power + self.SetTESTpin(0) #disable power + self.serialport.close() + + def comRxHeader(self): + """receive header and split data""" + if DEBUG > 1: sys.stderr.write("* comRxHeader()\n") + + hdr = self.serialport.read(1) + if not hdr: raise BSLException("Timeout") + rxHeader = ord(hdr) & 0xf0; + rxNum = ord(hdr) & 0x0f; + + if self.protocolMode == self.MODE_BSL: + self.reqNo = 0 + self.seqNo = 0 + rxNum = 0 + if DEBUG > 1: sys.stderr.write("* comRxHeader() OK\n") + return rxHeader, rxNum + + def comRxFrame(self, rxNum): + if DEBUG > 1: sys.stderr.write("* comRxFrame()\n") + rxFrame = chr(self.DATA_FRAME | rxNum) + + if DEBUG > 2: sys.stderr.write(" comRxFrame() header...\n") + rxFramedata = self.serialport.read(3) + if len(rxFramedata) != 3: raise BSLException("Timeout") + rxFrame = rxFrame + rxFramedata + + if DEBUG > 3: sys.stderr.write(" comRxFrame() check header...\n") + if rxFrame[1] == chr(0) and rxFrame[2] == rxFrame[3]: #Add. header info. correct? + rxLengthCRC = ord(rxFrame[2]) + 2 #Add CRC-Bytes to length + if DEBUG > 2: sys.stderr.write(" comRxFrame() receiving data, size: %s\n" % rxLengthCRC) + + rxFramedata = self.serialport.read(rxLengthCRC) + if len(rxFramedata) != rxLengthCRC: raise BSLException("Timeout") + rxFrame = rxFrame + rxFramedata + #Check received frame: + if DEBUG > 3: sys.stderr.write(" comRxFrame() crc check\n") + #rxLength+4: Length with header but w/o CRC: + checksum = self.calcChecksum(rxFrame, ord(rxFrame[2]) + 4) + if rxFrame[ord(rxFrame[2])+4] == chr(0xff & checksum) and \ + rxFrame[ord(rxFrame[2])+5] == chr(0xff & (checksum >> 8)): #Checksum correct? + #Frame received correctly (=> send next frame) + if DEBUG > 2: sys.stderr.write("* comRxFrame() OK\n") + return rxFrame + else: + if DEBUG: sys.stderr.write(" comRxFrame() Checksum wrong\n") + else: + if DEBUG: sys.stderr.write(" comRxFrame() Header corrupt %r" % rxFrame) + raise BSLException(self.ERR_COM) #Frame has errors! + + def comTxHeader(self, txHeader): + """send header""" + if DEBUG > 1: sys.stderr.write("* txHeader()\n") + self.serialport.write(txHeader) + + def comTxRx(self, cmd, dataOut, length): + """Sends the command cmd with the data given in dataOut to the + microcontroller and expects either an acknowledge or a frame + with result from the microcontroller. The results are stored + in dataIn (if not a NULL pointer is passed). + In this routine all the necessary protocol stuff is handled. + Returns zero if the function was successful.""" + if DEBUG > 1: sys.stderr.write("* comTxRx()\n") + txFrame = [] + rxHeader = 0 + rxNum = 0 + + dataOut = list(dataOut) #convert to a list for simpler data fill in + #Transmitting part ---------------------------------------- + #Prepare data for transmit + if (length % 2) != 0: + #/* Fill with one byte to have even number of bytes to send */ + if self.protocolMode == self.MODE_BSL: + dataOut.append(0xFF) #fill with 0xFF + else: + dataOut.append(0) #fill with zero + + txFrame = "%c%c%c%c" % (self.DATA_FRAME | self.seqNo, cmd, len(dataOut), len(dataOut)) + + self.reqNo = (self.seqNo + 1) % self.MAX_FRAME_COUNT + + txFrame = txFrame + string.join(dataOut,'') + checksum = self.calcChecksum(txFrame, length + 4) + txFrame = txFrame + chr(checksum & 0xff) + txFrame = txFrame + chr((checksum >> 8) & 0xff) + + accessAddr = (0x0212 + (checksum^0xffff)) & 0xfffe #0x0212: Address of wCHKSUM + if self.BSLMemAccessWarning and accessAddr < self.BSL_CRITICAL_ADDR: + sys.stderr.write("WARNING: This command might change data at address %04x or %04x!\n" % (accessAddr, accessAddr + 1)) + + self.serialport.flushInput() #clear receiving queue + #TODO: Check after each transmitted character, + #TODO: if microcontroller did send a character (probably a NAK!). + for c in txFrame: + self.serialport.write(c) + if DEBUG > 3: sys.stderr.write("\ttx %02x" % ord(c)) + #if self.serialport.inWaiting(): break #abort when BSL replies, probably NAK + else: + if DEBUG > 1: sys.stderr.write( " comTxRx() transmit OK\n") + + #Receiving part ------------------------------------------- + rxHeader, rxNum = self.comRxHeader() #receive header + if DEBUG > 1: sys.stderr.write(" comTxRx() rxHeader=0x%02x, rxNum=%d, seqNo=%d, reqNo=%s\n" % (rxHeader, rxNum, self.seqNo, self.reqNo)) + if rxHeader == self.DATA_ACK: #acknowledge/OK + if DEBUG > 2: sys.stderr.write(" comTxRx() DATA_ACK\n") + if rxNum == self.reqNo: + self.seqNo = self.reqNo + if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_ACK OK\n") + return #Acknowledge received correctly => next frame + raise BSLException(self.ERR_FRAME_NUMBER) + elif rxHeader == self.DATA_NAK: #not acknowledge/error + if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_NAK\n") + raise BSLException(self.ERR_RX_NAK) + elif rxHeader == self.DATA_FRAME: #receive data + if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_FRAME\n") + if rxNum == self.reqNo: + rxFrame = self.comRxFrame(rxNum) + return rxFrame + raise BSLException(self.ERR_FRAME_NUMBER) + elif rxHeader == self.CMD_FAILED: #Frame ok, but command failed. + if DEBUG > 2: sys.stderr.write("* comTxRx() CMD_FAILED\n") + raise BSLException(self.ERR_CMD_FAILED) + + raise BSLException("Unknown header 0x%02x\nAre you downloading to RAM into an old device that requires the patch? Try option -U" % rxHeader) + + def SetDTR(self, level, invert): + """Controls DTR pin (0: GND; 1: VCC; unless inverted flag is set)""" + #print 'fucking the dtr' + if invert: + self.serialport.setDTR(not level) + else: + self.serialport.setDTR(level) + if self.slowmode: + time.sleep(0.040) + + def SetRTS(self, level, invert): + """Controls RTS pin (0: GND; 1: VCC; unless inverted flag is set)""" + if invert: + self.serialport.setRTS(not level) + else: + self.serialport.setRTS(level) + if self.slowmode: + time.sleep(0.040) + + def SetRSTpin(self, level=1): + """Controls RST/NMI pin (0: GND; 1: VCC; unless inverted flag is set)""" + if self.swapRSTTEST: + self.SetRTS(level, self.invertRST) + else: + self.SetDTR(level, self.invertRST) + + def SetTESTpin(self, level=1): + """Controls TEST pin (inverted on board: 0: VCC; 1: GND; unless inverted flag is set)""" + if self.swapRSTTEST: + self.SetDTR(level, self.invertTEST) + else: + self.SetRTS(level, self.invertTEST) + + + def writepicROM(self, address, data): + ''' Writes data to @address''' + for i in range(7,-1,-1): + self.picROMclock((address >> i) & 0x01) + self.picROMclock(0) + recbuf = 0 + for i in range(7,-1,-1): + s = ((data >> i) & 0x01) + #print s + if i < 1: + r = not self.picROMclock(s, True) + else: + r = not self.picROMclock(s) + recbuf = (recbuf << 1) + r + + self.picROMclock(0, True) + #k = 1 + #while not self.serial.getCTS(): + # pass + #time.sleep(0.1) + return recbuf + + def readpicROM(self, address): + ''' reads a byte from @address''' + for i in range(7,-1,-1): + self.picROMclock((address >> i) & 0x01) + self.picROMclock(1) + recbuf = 0 + r = 0 + for i in range(7,-1,-1): + r = self.picROMclock(0) + recbuf = (recbuf << 1) + r + self.picROMclock(r) + #time.sleep(0.1) + return recbuf + + def picROMclock(self, masterout, slow = False): + #print "setting masterout to "+str(masterout) + self.serialport.setRTS(masterout) + self.serialport.setDTR(1) + #time.sleep(0.02) + self.serialport.setDTR(0) + if slow: + time.sleep(0.02) + return self.serialport.getCTS() + + def picROMfastclock(self, masterout): + #print "setting masterout to "+str(masterout) + self.serialport.setRTS(masterout) + self.serialport.setDTR(1) + self.serialport.setDTR(0) + time.sleep(0.02) + return self.serialport.getCTS() + + def bslResetZ1(self, invokeBSL=0): + ''' + Applies BSL entry sequence on RST/NMI and TEST/VPP pins + Parameters: + invokeBSL = 1: complete sequence + invokeBSL = 0: only RST/NMI pin accessed + + By now only BSL mode is accessed + ''' + + if DEBUG > 1: sys.stderr.write("* bslReset(invokeBSL=%s)\n" % invokeBSL) + if invokeBSL: + #sys.stderr.write("in Z1 bsl reset...\n") + time.sleep(0.1) + self.writepicROM(0xFF, 0xFF) + time.sleep(0.1) + #sys.stderr.write("z1 bsl reset done...\n") + else: + #sys.stderr.write("in Z1 reset...\n") + time.sleep(0.1) + self.writepicROM(0xFF, 0xFE) + time.sleep(0.1) + #sys.stderr.write("z1 reset done...\n") + + def telosSetSCL(self, level): + self.serialport.setRTS(not level) + + def telosSetSDA(self, level): + self.serialport.setDTR(not level) + + def telosI2CStart(self): + self.telosSetSDA(1) + self.telosSetSCL(1) + self.telosSetSDA(0) + + def telosI2CStop(self): + self.telosSetSDA(0) + self.telosSetSCL(1) + self.telosSetSDA(1) + + def telosI2CWriteBit(self, bit): + self.telosSetSCL(0) + self.telosSetSDA(bit) + time.sleep(2e-6) + self.telosSetSCL(1) + time.sleep(1e-6) + self.telosSetSCL(0) + + def telosI2CWriteByte(self, byte): + self.telosI2CWriteBit( byte & 0x80 ); + self.telosI2CWriteBit( byte & 0x40 ); + self.telosI2CWriteBit( byte & 0x20 ); + self.telosI2CWriteBit( byte & 0x10 ); + self.telosI2CWriteBit( byte & 0x08 ); + self.telosI2CWriteBit( byte & 0x04 ); + self.telosI2CWriteBit( byte & 0x02 ); + self.telosI2CWriteBit( byte & 0x01 ); + self.telosI2CWriteBit( 0 ); # "acknowledge" + + def telosI2CWriteCmd(self, addr, cmdbyte): + self.telosI2CStart() + self.telosI2CWriteByte( 0x90 | (addr << 1) ) + self.telosI2CWriteByte( cmdbyte ) + self.telosI2CStop() + + def telosBReset(self,invokeBSL=0): + # "BSL entry sequence at dedicated JTAG pins" + # rst !s0: 0 0 0 0 1 1 + # tck !s1: 1 0 1 0 0 1 + # s0|s1: 1 3 1 3 2 0 + + # "BSL entry sequence at shared JTAG pins" + # rst !s0: 0 0 0 0 1 1 + # tck !s1: 0 1 0 1 1 0 + # s0|s1: 3 1 3 1 0 2 + if invokeBSL: + self.telosI2CWriteCmd(0,1) + self.telosI2CWriteCmd(0,3) + self.telosI2CWriteCmd(0,1) + self.telosI2CWriteCmd(0,3) + self.telosI2CWriteCmd(0,2) + self.telosI2CWriteCmd(0,0) + else: + self.telosI2CWriteCmd(0,3) + self.telosI2CWriteCmd(0,2) + self.telosI2CWriteCmd(0,0) + time.sleep(0.250) #give MSP430's oscillator time to stabilize + self.serialport.flushInput() #clear buffers + + def bslReset(self, invokeBSL=0): + """Applies BSL entry sequence on RST/NMI and TEST/VPP pins + Parameters: + invokeBSL = 1: complete sequence + invokeBSL = 0: only RST/NMI pin accessed + + RST is inverted twice on boot loader hardware + TEST is inverted (only once) + Need positive voltage on DTR, RTS for power-supply of hardware""" + + #print 'goint to reset!' + + if self.telosI2C: + self.telosBReset(invokeBSL) + return + + if self.z1: + if DEBUG > 1: sys.stderr.write("* entering bsl with z1\n") + self.bslResetZ1(invokeBSL) + return + + if DEBUG > 1: sys.stderr.write("* bslReset(invokeBSL=%s)\n" % invokeBSL) + self.SetRSTpin(1) #power suply + self.SetTESTpin(1) #power suply + time.sleep(0.250) #charge capacitor on boot loader hardware + + if self.telosLatch: + self.SetTESTpin(0) + self.SetRSTpin(0) + self.SetTESTpin(1) + + self.SetRSTpin(0) #RST pin: GND + if invokeBSL: + self.SetTESTpin(1) #TEST pin: GND + self.SetTESTpin(0) #TEST pin: Vcc + self.SetTESTpin(1) #TEST pin: GND + self.SetTESTpin(0) #TEST pin: Vcc + self.SetRSTpin (1) #RST pin: Vcc + self.SetTESTpin(1) #TEST pin: GND + else: + self.SetRSTpin(1) #RST pin: Vcc + time.sleep(0.250) #give MSP430's oscillator time to stabilize + + self.serialport.flushInput() #clear buffers + + def bslSync(self,wait=0): + """Transmits Synchronization character and expects to receive Acknowledge character + if wait is 0 it must work the first time. otherwise if wait is 1 + it is retried (forever). + """ + loopcnt = 5 #Max. tries to get synchronization + + if DEBUG > 1: sys.stderr.write("* bslSync(wait=%d)\n" % wait) + while wait or loopcnt: + loopcnt = loopcnt - 1 #count down tries + self.serialport.flushInput() #clear input, in case a prog is running + + self.serialport.write(chr(self.BSL_SYNC)) #Send synchronization byte + c = self.serialport.read(1) #read answer + if c == chr(self.DATA_ACK): #ACk + if DEBUG > 1: sys.stderr.write(" bslSync() OK\n") + return #Sync. successful + elif not c: #timeout + if loopcnt > 4: + if DEBUG > 1: + sys.stderr.write(" bslSync() timeout, retry ...\n") + elif loopcnt == 4: + #nmi may have caused the first reset to be ignored, try again + self.bslReset(0) + self.bslReset(1) + elif loopcnt > 0: + if DEBUG > 1: + sys.stderr.write(" bslSync() timeout, retry ...\n") + else : + if DEBUG > 1: + sys.stderr.write(" bslSync() timeout\n") + else: #garbage + if DEBUG > 1: sys.stderr.write(" bslSync() failed (0x%02x), retry ...\n" % ord(c)) + + raise BSLException(self.ERR_BSL_SYNC) #Sync. failed + + def bslTxRx(self, cmd, addr, length = 0, blkout = None, wait=0): + """Transmits a command (cmd) with its parameters: + start-address (addr), length (len) and additional + data (blkout) to boot loader. + wait specified if the bsl sync should be tried once or + repeated, forever + Parameters return by boot loader are passed via blkin. + """ + if DEBUG > 1: sys.stderr.write("* bslTxRx()\n") + + if cmd == self.BSL_TXBLK: + #Align to even start address + if (addr % 2) != 0: + addr = addr - 1 #Decrement address and + blkout = chr(0xFF) + blkOut #fill first byte of blkout with 0xFF + length = length + 1 + #Make sure that len is even + if (length % 2) != 0: + blkout = blkout + chr(0xFF) #Inc. len and fill last byte of blkout with 0xFF + length = length + 1 + + elif cmd == self.BSL_RXBLK: + #Align to even start address + if (addr % 2) != 0: + addr = addr - 1 #Decrement address but + length = length + 1 #request an additional byte + #Make sure that len is even + if (length % 2) != 0: + length = length + 1 + + #if cmd == self.BSL_TXBLK or cmd == self.BSL_TXPWORD: + # length = len + 4 + + #Add necessary information data to frame + dataOut = struct.pack("= 1: + #Read one line + l = file.readline() + if not l: break #EOF + l = l.strip() + if l[0] == 'q': break + elif l[0] == '@': #if @ => new address => send frame and set new addr. + #create a new segment + if segmentdata: + self.segments.append( Segment(startAddr, string.join(segmentdata,'')) ) + startAddr = int(l[1:],16) + segmentdata = [] + else: + for i in string.split(l): + segmentdata.append(chr(int(i,16))) + if segmentdata: + self.segments.append( Segment(startAddr, string.join(segmentdata,'')) ) + + def loadELF(self, file): + """load data from a (opened) file in ELF object format. + File must be seekable""" + import elf + obj = elf.ELFObject() + obj.fromFile(file) + if obj.e_type != elf.ELFObject.ET_EXEC: + raise Exception("No executable") + for section in obj.getSections(): + if DEBUG: + sys.stderr.write("ELF section %s at 0x%04x %d bytes\n" % (section.name, section.lma, len(section.data))) + if len(section.data): + self.segments.append( Segment(section.lma, section.data) ) + + def loadFile(self, filename): + """fill memory with the contents of a file. file type is determined from extension""" + #TODO: do a contents based detection + if filename[-4:].lower() == '.txt': + self.loadTIText(open(filename, "rb")) + elif filename[-4:].lower() in ('.a43', '.hex'): + self.loadIHex(open(filename, "rb")) + else: + self.loadELF(open(filename, "rb")) + + def getMemrange(self, fromadr, toadr): + """get a range of bytes from the memory. unavailable values are filled with 0xff.""" + res = '' + toadr = toadr + 1 #python indxes are excluding end, so include it + while fromadr < toadr: + #print "fromto: %04x %04x" % (fromadr, toadr) + for seg in self.segments: + #print seg + segend = seg.startaddress + len(seg.data) + if seg.startaddress <= fromadr and fromadr < segend: + #print "startok 0x%04x %d" % (seg.startaddress, len(seg.data)) + #print ("0x%04x "*3) % (segend, fromadr, toadr) + if toadr > segend: #not all data in segment + #print "out of segment" + catchlength = segend-fromadr + else: + catchlength = toadr-fromadr + #print toadr-fromadr + #print catchlength + res = res + seg.data[fromadr-seg.startaddress : fromadr-seg.startaddress+catchlength] + fromadr = fromadr + catchlength #adjust start + if len(res) >= toadr-fromadr: + break#return res + else: + res = res + chr(255) + fromadr = fromadr + 1 #adjust start + #print "fill FF" + #print "res: %r" % res + return res + + +class BootStrapLoader(LowLevel): + """higher level Bootstrap Loader functions.""" + + ERR_VERIFY_FAILED = "Error: verification failed" + ERR_ERASE_CHECK_FAILED = "Error: erase check failed" + + ACTION_PROGRAM = 0x01 #Mask: program data + ACTION_VERIFY = 0x02 #Mask: verify data + ACTION_ERASE_CHECK = 0x04 #Mask: erase check + + #Max. bytes sent within one frame if parsing a TI TXT file. + #( >= 16 and == n*16 and <= MAX_DATA_BYTES!) + MAXDATA = 240-16 + + + def __init__(self, *args, **kargs): + LowLevel.__init__(self, *args, **kargs) + self.byteCtr = 0 + self.meraseCycles = 1 + self.patchRequired = 0 + self.patchLoaded = 0 + self.bslVer = 0 + self.passwd = None + self.data = None + self.maxData = self.MAXDATA + self.cpu = None + self.pwdsent = False + + + def preparePatch(self): + """prepare to download patch""" + if DEBUG > 1: sys.stderr.write("* preparePatch()\n") + + if self.patchLoaded: + #Load PC with 0x0220. + #This will invoke the patched bootstrap loader subroutines. + self.bslTxRx(self.BSL_LOADPC, #Command: Load PC + 0x0220) #Address to load into PC + self.BSLMemAccessWarning = 0 #Error is removed within workaround code + return + + def postPatch(self): + """setup after the patch is loaded""" + if DEBUG > 1: sys.stderr.write("* postPatch()\n") + if self.patchLoaded: + self.BSLMemAccessWarning = 1 #Turn warning back on. + + + def verifyBlk(self, addr, blkout, action): + """verify memory against data or 0xff""" + if DEBUG > 1: sys.stderr.write("* verifyBlk()\n") + + if action & self.ACTION_VERIFY or action & self.ACTION_ERASE_CHECK: + if DEBUG: sys.stderr.write(" Check starting at 0x%04x, %d bytes ... \n" % (addr, len(blkout))) + + self.preparePatch() + blkin = self.bslTxRx(self.BSL_RXBLK, addr, len(blkout)) + self.postPatch() + + for i in range(len(blkout)): + if action & self.ACTION_VERIFY: + #Compare data in blkout and blkin + if blkin[i] != blkout[i]: + sys.stderr.write("Verification failed at 0x%04x (0x%02x, 0x%02x)\n" % (addr+i, ord(blkin[i]), ord(blkout[i]))) + sys.stderr.flush() + raise BSLException(self.ERR_VERIFY_FAILED) #Verify failed! + continue + elif action & self.ACTION_ERASE_CHECK: + #Compare data in blkin with erase pattern + if blkin[i] != chr(0xff): + sys.stderr.write("Erase Check failed at 0x%04x (0x%02x)\n" % (addr+i, ord(blkin[i]))) + sys.stderr.flush() + raise BSLException(self.ERR_ERASE_CHECK_FAILED) #Erase Check failed! + continue + def readBlk(self,adr,len): + """Read a block of memory.""" + blkin = self.bslTxRx(self.BSL_RXBLK, addr, len(blkout)) + + def programBlk(self, addr, blkout, action): + """program a memory block""" + if DEBUG > 1: sys.stderr.write("* programBlk()\n") + + #Check, if specified range is erased + self.verifyBlk(addr, blkout, action & self.ACTION_ERASE_CHECK) + + if action & self.ACTION_PROGRAM: + if DEBUG: sys.stderr.write(" Program starting at 0x%04x, %i bytes ...\n" % (addr, len(blkout))) + self.preparePatch() + #Program block + self.bslTxRx(self.BSL_TXBLK, addr, len(blkout), blkout) + self.postPatch() + + #Verify block + self.verifyBlk(addr, blkout, action & self.ACTION_VERIFY) + + #segments: + #list of tuples or lists: + #segements = [ (addr1, [d0,d1,d2,...]), (addr2, [e0,e1,e2,...])] + def programData(self, segments, action): + """programm or verify data""" + if DEBUG > 1: sys.stderr.write("* programData()\n") + for seg in segments: + currentAddr = seg.startaddress + pstart = 0 + while pstart len(seg.data): + length = len(seg.data) - pstart + self.programBlk(currentAddr, seg.data[pstart:pstart+length], action) + pstart = pstart + length + currentAddr = currentAddr + length + self.byteCtr = self.byteCtr + length #total sum + + def uploadData(self, startaddress, size, wait=0): + """upload a datablock""" + if DEBUG > 1: sys.stderr.write("* uploadData()\n") + data = '' + pstart = 0 + while pstart size: + length = size - pstart + data = data + self.bslTxRx(self.BSL_RXBLK, + pstart+startaddress, + length, + wait=wait)[:-2] #cut away checksum + pstart = pstart + length + return data + + def txPasswd(self, passwd=None, wait=0): + """transmit password, default if None is given.""" + if DEBUG > 1: sys.stderr.write("* txPassword(%r)\n" % passwd) + if passwd is None or self.pwdsent: + #Send "standard" password to get access to protected functions. + sys.stderr.write("Transmit default password ...\n") + sys.stderr.flush() + #Flash is completely erased, the contents of all Flash cells is 0xff + passwd = chr(0xff)*32 + else: + #sanity check of password + if len(passwd) != 32: + raise ValueError, "password has wrong length (%d)\n" % len(passwd) + sys.stderr.write('Transmit password ...\n') + sys.stderr.flush() + #send the password + self.bslTxRx(self.BSL_TXPWORD, #Command: Transmit Password + 0xffe0, #Address of interupt vectors + 0x0020, #Number of bytes + passwd, #password + wait=wait) #if wait is 1, try to sync forever + self.pwdsent = True + + + def getNewPassword(self): + print 'getting new password' + newpassword = self.bslTxRx(self.BSL_RXBLK, 0xffe0, 0x0020) + return newpassword + + + def writeCalibTables(self): + print 'writing new tables' + print CALIB_TABLES + calibtables = ["84C016FEFFFFFFFFFFFFFFFFFFFFFFFF", + "FFFFFFFFFFFFFFFFFFFF100880000001", + "7E270B790DAA7E4D06DF083308FEFFFF", + "FFFFFFFFFFFF08018F958E9A8D8886D1"] + self.programBlk(0x10C0, "84C016FEFFFFFFFFFFFFFFFFFFFFFFFF", self.ACTION_PROGRAM) + self.programBlk(0x10D0, "FFFFFFFFFFFFFFFFFFFF100880000001", self.ACTION_PROGRAM) + self.programBlk(0x10E0, "7E270B790DAA7E4D06DF083308FEFFFF", self.ACTION_PROGRAM) + self.programBlk(0x10F0, "FFFFFFFFFFFF08018F958E9A8D8886D1", self.ACTION_PROGRAM) + + def writeCalibTables2(self): + print 'writing new tables 2ond part' + #self.programBlk(0x10C0, "84C016FEFFFFFFFFFFFFFFFFFFFFFFFF", self.ACTION_PROGRAM) + #self.programBlk(0x10D0, "FFFFFFFFFFFFFFFFFFFF100880000001", self.ACTION_PROGRAM) + #self.programBlk(0x10E0, "7E270B790DAA7E4D06DF083308FEFFFF", self.ACTION_PROGRAM) + #self.programBlk(0x10F0, "FFFFFFFFFFFF08018F958E9A8D8886D1", self.ACTION_PROGRAM) + + def actionSecureInfoSegment(self): + print 'securing info segment' + self.programBlk(0xFFBE, "0000", self.ACTION_PROGRAM) + print 'info segment secured' + + #----------------------------------------------------------------- + + def actionMassErase(self): + """Erase the flash memory completely (with mass erase command)""" + sys.stderr.write("Mass Erase...\n") + sys.stderr.flush() + self.bslReset(1) #Invoke the boot loader. + for i in range(self.meraseCycles): + if i == 1: sys.stderr.write("Additional Mass Erase Cycles...\n") + self.bslTxRx(self.BSL_MERAS, #Command: Mass Erase + 0xff00, #Any address within flash memory. + 0xa506) #Required setting for mass erase! + self.passwd = None #No password file required! + #print "Mass Erase complete" + #Transmit password to get access to protected BSL functions. + self.txPasswd() + + def actionMainErase(self): + """Erase the main flash memory only""" + sys.stderr.write("Main Erase...\n") + sys.stderr.flush() + self.bslTxRx(self.BSL_ERASE, #Command: Segment Erase + 0xfffe, #Any address within flash memory. + 0xa504) #Required setting for main erase! + #self.passwd = None #Password gets erased WHY?? + + + + def actionStartBSL(self, usepatch=1, adjsp=1, replacementBSL=None, forceBSL=0, mayuseBSL=0, speed=None, bslreset=1): + """start BSL, download patch if desired and needed, adjust SP if desired""" + sys.stderr.write("Invoking BSL...\n") + sys.stderr.flush() + + #for '30, invertTEST=0, else =1 + if bslreset: + #detext if is z1? + self.bslReset(1) #Invoke the boot loader. + + + if(self.z1): + ''' recover password from pic''' + + self.txPasswd(self.passwd) #transmit password + + #Read actual bootstrap loader version. + #sys.stderr.write("Reading BSL version ...\n") + blkin = self.bslTxRx(self.BSL_RXBLK, #Command: Read/Receive Block + 0x0ff0, #Start address + 16) #No. of bytes to read + dev_id, bslVerHi, bslVerLo = struct.unpack(">H8xBB4x", blkin[:-2]) #cut away checksum and extract data + self.dev_id=dev_id; + + + if self.cpu is None: #cpy type forced? + if deviceids.has_key(dev_id): + self.cpu = deviceids[dev_id] #try to autodectect CPU type + + if DEBUG: + sys.stderr.write("Autodetect successful: %04x -> %s\n" % (dev_id, self.cpu)) + else: + sys.stderr.write("Autodetect failed! Unkown ID: %04x. Trying to continue anyway.\n" % dev_id) + self.cpu = F1x #assume something and try anyway.. + + sys.stderr.write("Current bootstrap loader version: %x.%x (Device ID: %04x)\n" % (bslVerHi, bslVerLo, dev_id)) + sys.stderr.flush() + self.bslVer = (bslVerHi << 8) | bslVerLo + + if self.bslVer <= 0x0110: #check if patch is needed + self.BSLMemAccessWarning = 1 + else: + self.BSLMemAccessWarning = 0 #Fixed in newer versions of BSL. + + if self.bslVer <= 0x0130 and adjsp: + #only do this on BSL where it's needed to prevent + #malfunction with F4xx devices/ newer ROM-BSLs + + #Execute function within bootstrap loader + #to prepare stack pointer for the following patch. + #This function will lock the protected functions again. + sys.stderr.write("Adjust SP. Load PC with 0x0C22 ...\n") + self.bslTxRx(self.BSL_LOADPC, #Command: Load PC + 0x0C22) #Address to load into PC + #Re-send password to re-gain access to protected functions. + self.txPasswd(self.passwd) + + #get internal BSL replacement if needed or forced by the user + #required if speed is set but an old BSL is in the device + #if a BSL is given by the user, that one is used and not the internal one + if ((mayuseBSL and speed and self.bslVer < 0x0150) or forceBSL) and replacementBSL is None: + replacementBSL = Memory() #File to program + if self.cpu == F4x: + if DEBUG: + sys.stderr.write("Using built in BSL replacement for F4x devices\n") + sys.stderr.flush() + replacementBSL.loadTIText(cStringIO.StringIO(F4X_BSL)) #parse embedded BSL + else: + if DEBUG: + sys.stderr.write("Using built in BSL replacement for F1x devices\n") + sys.stderr.flush() + replacementBSL.loadTIText(cStringIO.StringIO(F1X_BSL)) #parse embedded BSL + + #now download the new BSL, if allowed and needed (version lower than the + #the replacement) or forced + if replacementBSL is not None: + self.actionDownloadBSL(replacementBSL) + + #debug message with the real BSL version in use (may have changed after replacement BSL) + if DEBUG: + sys.stderr.write("Current bootstrap loader version: 0x%04x\n" % (self.bslVer,)) + sys.stderr.flush() + + #now apply workarounds or patches if BSL in use requires that + if self.bslVer <= 0x0110: #check if patch is needed + if usepatch: #test if patch is desired + sys.stderr.write("Patch for flash programming required!\n") + self.patchRequired = 1 + + sys.stderr.write("Load and verify patch ...\n") + sys.stderr.flush() + #Programming and verification is done in one pass. + #The patch file is only read and parsed once. + segments = Memory() #data to program + segments.loadTIText(cStringIO.StringIO(PATCH)) #parse embedded patch + #program patch + self.programData(segments, self.ACTION_PROGRAM | self.ACTION_VERIFY) + self.patchLoaded = 1 + else: + if DEBUG: + sys.stderr.write("Device needs patch, but not applied (usepatch is false).\n") #message if not patched + + #should the baudrate be changed? + if speed is not None: + self.actionChangeBaudrate(speed) #change baudrate + + def actionDownloadBSL(self, bslsegments): + sys.stderr.write("Load new BSL into RAM...\n") + sys.stderr.flush() + self.programData(bslsegments, self.ACTION_PROGRAM) + sys.stderr.write("Verify new BSL...\n") + sys.stderr.flush() + self.programData(bslsegments, self.ACTION_VERIFY) #File to verify + + #Read startvector of bootstrap loader + #blkin = self.bslTxRx(self.BSL_RXBLK, 0x0300, 2) + #blkin = self.bslTxRx(self.BSL_RXBLK, 0x0220, 2) + blkin = self.bslTxRx(self.BSL_RXBLK, bslsegments[0].startaddress, 2) + startaddr = struct.unpack("%s" % (firmware[self.dev_id],fn)) + + fw=Memory(fn); + #fw.loadIhex(open(fn,"rb")); + + sys.stderr.write("Program ...\n") + sys.stderr.flush() + self.programData(fw, self.ACTION_PROGRAM | self.ACTION_VERIFY) + sys.stderr.write("%i bytes programmed.\n" % self.byteCtr) + sys.stderr.flush() + + def actionVerify(self): + """Verify programmed data""" + if self.data is not None: + sys.stderr.write("Verify ...\n") + sys.stderr.flush() + self.programData(self.data, self.ACTION_VERIFY) + else: + raise BSLException, "verify without data not possible" + + def actionReset(self): + """perform a reset, start user programm""" + sys.stderr.write("Reset device ...\n") + sys.stderr.flush() + self.bslReset(0) #only reset + + def actionRun(self, address=0x220): + """start program at specified address""" + sys.stderr.write("Load PC with 0x%04x ...\n" % address) + sys.stderr.flush() + self.bslTxRx(self.BSL_LOADPC, #Command: Load PC + address) #Address to load into PC + + #table with values from slaa089a.pdf + bauratetable = { + F1x: { + 9600:[0x8580, 0x0000], + 19200:[0x86e0, 0x0001], + 38400:[0x87e0, 0x0002], + }, + F2x: { + 9600:[0x8580, 0x0000], + 19200:[0x8B00, 0x0001], + 38400:[0x8C80, 0x0002], + }, + F4x: { + 9600:[0x9800, 0x0000], + 19200:[0xb000, 0x0001], + 38400:[0xc800, 0x0002], + }, + } + def actionChangeBaudrate(self, baudrate=9600): + """change baudrate. first the command is sent, then the comm + port is reprogrammed. only possible with newer MSP430-BSL versions. + (ROM >=1.6, downloadable >=1.5)""" + try: + baudconfigs = self.bauratetable[self.cpu] + except KeyError: + raise ValueError, "unknown CPU type %s, can't switch baudrate" % self.cpu + try: + a,l = baudconfigs[baudrate] + except KeyError: + raise ValueError, "baudrate not valid. valid values are %r" % baudconfigs.keys() + + sys.stderr.write("Changing baudrate to %d ...\n" % baudrate) + sys.stderr.flush() + self.bslTxRx(self.BSL_CHANGEBAUD, #Command: change baudrate + a, l) #args are coded in adr and len + self.serialport.flush() + time.sleep(0.010) #recomended delay + self.serialport.baudrate = baudrate + + def actionReadBSLVersion(self): + """informational output of BSL version number. + (newer MSP430-BSLs only)""" + ans = self.bslTxRx(self.BSL_TXVERSION, 0) #Command: receive version info + #the following values are in big endian style!!! + family_type, bsl_version = struct.unpack(">H8xH4x", ans[:-2]) #cut away checksum and extract data + print "Device Type: 0x%04x\nBSL version: 0x%04x\n" % (family_type, bsl_version) + + def actionWriteNewPassword(self): + passwd = self.getNewPassword() + #print passwd + self.picWritePassword(passwd) + + def actionRecoverPassword(self): + p = self.picRecoverPassword() + #print p + self.passwd = p + + def picRecoverPassword(self): + ''' recovers password from picrom''' + print 'recovering pic password', + p = '' + + for i in range(0,32): + c = self.readpicROM(i) + print c, + p = p + chr(c) + + print 'done' + return p + + def picWritePassword(self, password): + ''' writes a new password to picrom''' + print 'writing new password', + for i in range(0,32): + c = ord(password[i]) + print c, + self.writepicROM(i, c) + print 'done' + + +def usage(): + """print some help message""" + sys.stderr.write(""" +USAGE: %s [options] [file] +Version: %s + +If "-" is specified as file the data is read from the stdinput. +A file ending with ".txt" is considered to be in TIText format, +'.a43' and '.hex' as IntelHex and all other filenames are +considered as ELF files. + +General options: + -h, --help Show this help screen. + -c, --comport=port Specify the communication port to be used. + (Default is 0) + 0->COM1 / ttyS0 + 1->COM2 / ttyS1 + etc. + -P, --password=file Specify a file with the interrupt vectors that + are used as password. This can be any file that + has previously been used to program the device. + (e.g. -P INT_VECT.TXT). + -f, --framesize=num Max. number of data bytes within one transmitted + frame (16 to 240 in steps of 16) (e.g. -f 240). + -m, --erasecycles=num Number of mass erase cycles (default is 1). Some + old F149 devices need additional erase cycles. + On newer devices it is no longer needed. (e.g. for + an old F149: -m20) + -U, --unpatched Do not download the BSL patch, even when it is + needed. This is used when a program is downloaded + into RAM and executed from there (and where flash + programming is not needed.) + -D, --debug Increase level of debug messages. This won't be + very useful for the average user... + -I, --intelhex Force fileformat to IntelHex + -T, --titext Force fileformat to be TIText + -N, --notimeout Don't use timeout on serial port (use with care) + -B, --bsl=bsl.txt Load and use new BSL from the TI Text file + -S, --speed=baud Reconfigure speed, only possible with newer + MSP403-BSL versions (>1.5, read slaa089a.pdf for + details). If the --bsl option is not used, an + internal BSL replacement will be loaded. + Needs a target with at least 2kB RAM! + Possible values are 9600, 19200, 38400 + (default 9600) + -1, --f1x Specify CPU family, in case autodetect fails + -2, --f2x Specify CPU family, in case autodetect fails + -4, --f4x Specify CPU family, in case autodetect fails + --F1x and --f2x are only needed when the "change + baudrate" feature is used and the autodetect feature + fails. If the device ID that is uploaded is known, it + has precedence to the command line option. + --invert-reset Invert signal on RST pin (used for some BSL hardware) + --invert-test Invert signal on TEST/TCK pin (used for some BSL + hardware) + --swap-reset-test Swap the RST and TEST pins (used for some BSL hardware) + --telos-latch Special twiddle in BSL reset for Telos hardware + --telos-i2c DTR/RTS map via an I2C switch to TCK/RST in Telos Rev.B + --telos Implies options --invert-reset, --invert-test, + --swap-reset-test, and --telos-latch + --telosb Implies options --swap-reset-test, --telos-i2c, + --no-BSL-download, and --speed=38400 + --goodfet10 + --goodfet20 + --goodfet30 + --tmote Identical operation to --telosb + --z1 Bootstrap a Z1 + --no-BSL-download Do not download replacement BSL (disable automatic) + --force-BSL-download Download replacement BSL even if not needed (the one + in the device would have the required features) + --slow Add delays when operating the conrol pins. Useful if + the pins/circuit has high capacitance. + +Program Flow Specifiers: + -e, --masserase Mass Erase (clear all flash memory) + -E, --erasecheck Erase Check by file + -p, --program Program file + -v, --verify Verify by file + +The order of the above options matters! The table is ordered by normal +execution order. For the options "Epv" a file must be specified. +Program flow specifiers default to "pvr" if a file is given. +Don't forget to specify "e" or "eE" when programming flash! + +Data retreiving: + -u, --upload=addr Upload a datablock (see also: -s). + -s, --size=num Size of the data block do upload. (Default is 2) + -x, --hex Show a hexadecimal display of the uploaded data. + (Default) + -b, --bin Get binary uploaded data. This can be used + to redirect the output into a file. + +Do before exit: + -g, --go=address Start programm execution at specified address. + This implies option --wait. + -r, --reset Reset connected MSP430. Starts application. + This is a normal device reset and will start + the programm that is specified in the reset + vector. (see also -g) + -w, --wait Wait for before closing serial port. + +If it says "NAK received" it's probably because you specified no or a +wrong password. +""" % (sys.argv[0], VERSION)) + +#add some arguments to a function, but don't call it yet, instead return +#a wrapper object for later invocation +class curry: + """create a callable with some arguments specified in advance""" + def __init__(self, fun, *args, **kwargs): + self.fun = fun + self.pending = args[:] + self.kwargs = kwargs.copy() + + def __call__(self, *args, **kwargs): + if kwargs and self.kwargs: + kw = self.kwargs.copy() + kw.update(kwargs) + else: + kw = kwargs or self.kwargs + return apply(self.fun, self.pending + args, kw) + + def __repr__(self): + #first try if it a function + try: + return "curry(%s, %r, %r)" % (self.fun.func_name, self.pending, self.kwargs) + except AttributeError: + #fallback for callable classes + return "curry(%s, %r, %r)" % (self.fun, self.pending, self.kwargs) + +def hexify(line, bytes, width=16): + return '%04x %s%s %s' % ( + line, + ('%02x '*len(bytes)) % tuple(bytes), + ' '* (width-len(bytes)), + ('%c'*len(bytes)) % tuple(map(lambda x: (x>=32 and x<127) and x or ord('.'), bytes)) + ) + +#Main: +def main(itest=1): + global DEBUG + import getopt + filetype = None + filename = None + comPort = None #Default setting. + speed = None + unpatched = 0 + reset = 0 + wait = 0 #wait at the end + goaddr = None + bsl = BootStrapLoader() + toinit = [] + todo = [] + startaddr = None + size = 2 + hexoutput = 1 + notimeout = 0 + bslrepl = None + mayuseBSL = 1 + forceBSL = 0 + dumpivt = 0 + dumpinfo = 0 + + bsl.invertRST = 1 + bsl.invertTEST = itest + + if comPort is None and os.environ.get("GOODFET")!=None: + glob_list = glob.glob(os.environ.get("GOODFET")); + if len(glob_list) > 0: + comPort = glob_list[0]; + if comPort is None: + glob_list = glob.glob("/dev/tty.usbserial*"); + if len(glob_list) > 0: + comPort = glob_list[0]; + if comPort is None: + glob_list = glob.glob("/dev/ttyUSB*"); + if len(glob_list) > 0: + comPort = glob_list[0]; + + + sys.stderr.write("MSP430 Bootstrap Loader Version: %s\n" % VERSION) + + try: + opts, args = getopt.getopt(sys.argv[1:], + "hc:P:wf:m:neEpvrg:UDudsxbITNB:S:V14", + ["help", "comport=", "password=", "wait", "framesize=", + "erasecycles=", "masserase", "erasecheck", "program", + "verify", "reset", "go=", "unpatched", "debug", + "upload=", "download=", "size=", "hex", "bin", + "intelhex", "titext", "notimeout", "bsl=", "speed=", + "bslversion", "f1x", "f2x", "f4x", "invert-reset", "invert-test", + "swap-reset-test", "telos-latch", "telos-i2c", "telos", "telosb", + "tmote","no-BSL-download", "force-BSL-download", "slow", + "dumpivt", "dumpinfo", "fromweb", + "goodfet30", "goodfet20", "goodfet10", "z1", "mainerase" + ] + ) + except getopt.GetoptError: + # print help information and exit: + usage() + sys.exit(2) + + for o, a in opts: + if o in ("-h", "--help"): + usage() + sys.exit() + elif o in ("-c", "--comport"): + try: + comPort = int(a) #try to convert decimal + except ValueError: + comPort = a #take the string and let serial driver decide + elif o in ("-P", "--password"): + #extract password from file + bsl.passwd = Memory(a).getMemrange(0xffe0, 0xffff) + elif o in ("-w", "--wait"): + wait = 1 + elif o in ("--dumpivt"): + dumpivt=1; + elif o in ("--dumpinfo"): + dumpinfo=1; + elif o in ("-f", "--framesize"): + try: + maxData = int(a) #try to convert decimal + except ValueError: + sys.stderr.write("framesize must be a valid number\n") + sys.exit(2) + #Make sure that conditions for maxData are met: + #( >= 16 and == n*16 and <= MAX_DATA_BYTES!) + if maxData > BootStrapLoader.MAX_DATA_BYTES: + maxData = BootStrapLoader.MAX_DATA_BYTES + elif maxData < 16: + maxData = 16 + bsl.maxData = maxData - (maxData % 16) + sys.stderr.write( "Max. number of data bytes within one frame set to %i.\n" % maxData) + elif o in ("-m", "--erasecycles"): + try: + meraseCycles = int(a) #try to convert decimal + except ValueError: + sys.stderr.write("erasecycles must be a valid number\n") + sys.exit(2) + #sanity check of value + if meraseCycles < 1: + sys.stderr.write("erasecycles must be a positive number\n") + sys.exit(2) + if meraseCycles > 20: + sys.stderr.write("warning: erasecycles set to a large number (>20): %d\n" % meraseCycles) + sys.stderr.write( "Number of mass erase cycles set to %i.\n" % meraseCycles) + bsl.meraseCycles = meraseCycles + elif o in ("-e", "--masserase"): + #toinit.append(bsl.actionMassErase) #Erase Flash + todo.append(bsl.actionMainErase) #Erase Flash + #todo.append(bsl.actionSecureInfoSegment) + elif o in ("-n", "--mainerase"): + #todo.append(bsl.actionMainErase) #Erase Flash + toinit.append(bsl.actionMassErase) #Erase Flash + elif o in ("-E", "--erasecheck"): + toinit.append(bsl.actionEraseCheck) #Erase Check (by file) + elif o in ("-p", "--program"): + #pass + todo.append(bsl.actionProgram) #Program file + if bsl.z1: + #todo.append(bsl.actionUnlockInfo) + #todo.append(bsl.writeCalibTables) + #todo.append(bsl.writeCalibTables2) + + todo.append(bsl.actionWriteNewPassword) + + pass + elif o in ("--fromweb"): + toinit.append(bsl.actionMassErase) #Erase Flash + todo.append(bsl.actionFromweb) #Program GoodFET code + elif o in ("-v", "--verify"): + todo.append(bsl.actionVerify) #Verify file + elif o in ("-r", "--reset"): + reset = 1 + elif o in ("-g", "--go"): + try: + goaddr = int(a) #try to convert decimal + except ValueError: + try: + goaddr = int(a[2:],16) #try to convert hex + except ValueError: + sys.stderr.write("go address must be a valid number\n") + sys.exit(2) + wait = 1 + elif o in ("-U", "--unpatched"): + unpatched = 1 + elif o in ("-D", "--debug"): + DEBUG = DEBUG + 1 + elif o in ("-u", "--upload"): + try: + startaddr = int(a) #try to convert decimal + except ValueError: + try: + startaddr = int(a,16) #try to convert hex + except ValueError: + sys.stderr.write("upload address must be a valid number\n") + sys.exit(2) + elif o in ("-s", "--size"): + try: + size = int(a) + except ValueError: + try: + size = int(a,16) + except ValueError: + sys.stderr.write("size must be a valid number\n") + sys.exit(2) + elif o in ("-x", "--hex"): + hexoutput = 1 + elif o in ("-b", "--bin"): + hexoutput = 0 + elif o in ("-I", "--intelhex"): + filetype = 0 + elif o in ("-T", "--titext"): + filetype = 1 + elif o in ("-N", "--notimeout"): + notimeout = 1 + elif o in ("-B", "--bsl"): + bslrepl = Memory() #File to program + bslrepl.loadFile(a) + elif o in ("-V", "--bslversion"): + todo.append(bsl.actionReadBSLVersion) #load replacement BSL as first item + elif o in ("-S", "--speed"): + try: + speed = int(a) #try to convert decimal + except ValueError: + sys.stderr.write("speed must be decimal number\n") + sys.exit(2) + elif o in ("-1", "--f1x"): + bsl.cpu = F1x + elif o in ("-2", "--f2x"): + bsl.cpu = F2x + elif o in ("-4", "--f4x"): + bsl.cpu = F4x + elif o in ("--invert-reset", ): + bsl.invertRST = 1 + elif o in ("--invert-test", ): + bsl.invertTEST = 1 + elif o in ("--swap-reset-test", ): + bsl.swapRSTTEST = 1 + elif o in ("--telos-latch", ): + bsl.telosLatch = 1 + elif o in ("--telos-i2c", ): + bsl.telosI2C = 1 + elif o in ("--telos", ): + bsl.invertRST = 1 + bsl.invertTEST = 1 + bsl.swapRSTTEST = 1 + bsl.telosLatch = 1 + elif o in ("--goodfet10", ): + bsl.invertRST = 1 + bsl.invertTEST = 1 + elif o in ("--goodfet20", ): + bsl.invertRST = 1 + bsl.invertTEST = 1 + elif o in ("--goodfet30", ): + bsl.invertRST = 1 + bsl.invertTEST = 0 + elif o in ("--telosb", ): + bsl.swapRSTTEST = 1 + bsl.telosI2C = 1 + mayuseBSL = 0 + speed = 38400 + elif o in ("--tmote", ): + bsl.swapRSTTEST = 1 + bsl.telosI2C = 1 + mayuseBSL = 0 + speed = 38400 + elif o in ("--z1", ): + bsl.z1 = 1 + speed = 38400 + toinit.append(bsl.actionRecoverPassword) + elif o in ("--no-BSL-download", ): + mayuseBSL = 0 + elif o in ("--force-BSL-download", ): + forceBSL = 1 + elif o in ("--slow", ): + bsl.slowmode = 1 + + if len(args) == 0: + sys.stderr.write("Use -h for help\n"); + sys.stderr.write("Use --fromweb to upgrade a GoodFET.\n") + elif len(args) == 1: #a filename is given + if not todo: #if there are no actions yet + todo.extend([ #add some useful actions... + bsl.actionProgram, + bsl.actionVerify, + ]) + filename = args[0] + else: #number of args is wrong + usage() + sys.exit(2) + + if DEBUG: #debug infos + sys.stderr.write("Debug level set to %d\n" % DEBUG) + sys.stderr.write("Python version: %s\n" % sys.version) + + #sanity check of options + if notimeout and goaddr is not None and startaddr is not None: + sys.stderr.write("Option --notimeout can not be used together with both --upload and --go\n") + sys.exit(1) + + if notimeout: + sys.stderr.write("Warning: option --notimeout can cause improper function in some cases!\n") + bsl.timeout = 0 + + if goaddr and reset: + sys.stderr.write("Warning: option --reset ignored as --go is specified!\n") + reset = 0 + + if startaddr and reset: + sys.stderr.write("Warning: option --reset ignored as --upload is specified!\n") + reset = 0 + + sys.stderr.flush() + + #prepare data to download + bsl.data = Memory() #prepare downloaded data + if filetype is not None: #if the filetype is given... + if filename is None: + raise ValueError("no filename but filetype specified") + if filename == '-': #get data from stdin + file = sys.stdin + else: + file = open(filename, "rb") #or from a file + if filetype == 0: #select load function + bsl.data.loadIHex(file) #intel hex + elif filetype == 1: + bsl.data.loadTIText(file) #TI's format + else: + raise ValueError("illegal filetype specified") + else: #no filetype given... + if filename == '-': #for stdin: + bsl.data.loadIHex(sys.stdin) #assume intel hex + elif filename: + bsl.data.loadFile(filename) #autodetect otherwise + + if DEBUG > 3: sys.stderr.write("File: %r" % filename) + + bsl.comInit(comPort) #init port + + #initialization list + if toinit: #erase and erase check + if DEBUG: sys.stderr.write("Preparing device ...\n") + #bsl.actionStartBSL(usepatch=0, adjsp=0) #no workarounds needed + #if speed: bsl.actionChangeBaudrate(speed) #change baud rate as fast as possible + for f in toinit: f() + + if todo or goaddr or startaddr: + if DEBUG: sys.stderr.write("Actions ...\n") + #connect to the BSL + bsl.actionStartBSL( + usepatch=not unpatched, + replacementBSL=bslrepl, + forceBSL=forceBSL, + mayuseBSL=mayuseBSL, + speed=speed, + ) + + #work list + if todo: + if DEBUG > 0: #debug + #show a nice list of sheduled actions + sys.stderr.write("TODO list:\n") + for f in todo: + try: + sys.stderr.write(" %s\n" % f.func_name) + except AttributeError: + sys.stderr.write(" %r\n" % f) + for f in todo: f() #work through todo list + + if reset: #reset device first if desired + bsl.actionReset() + if dumpivt: + bsl.txPasswd(); #default pass + data=bsl.uploadData(0xc00,1024); + hex=""; + for c in data: + hex+=("%02x "%ord(c)); + print hex; + if dumpinfo: + bsl.txPasswd(); #default pass + data=bsl.uploadData(0x1000,256); + hex="@1000\n"; + for c in data: + hex+=("%02x "%ord(c)); + hex+="\nq\n"; + print hex; + + if goaddr is not None: #start user programm at specified address + bsl.actionRun(goaddr) #load PC and execute + + #upload datablock and output + if startaddr is not None: + if goaddr: #if a program was started... + #don't restart BSL but wait for the device to enter it itself + sys.stderr.write("Waiting for device to reconnect for upload: ") + sys.stderr.flush() + bsl.txPasswd(bsl.passwd, wait=1) #synchronize, try forever... + data = bsl.uploadData(startaddr, size) #upload data + else: + data = bsl.uploadData(startaddr, size) #upload data + if hexoutput: #depending on output format + m = 0 + while m < len(data): #print a hex display + print hexify(startaddr+m, map(ord,data[m:m+16])) + m = m + 16 + else: + sys.stdout.write(data) #binary output w/o newline! + wait = 0 #wait makes no sense as after the upload the device is still in BSL + + if wait: #wait at the end if desired + sys.stderr.write("Press ...\n") #display a prompt + sys.stderr.flush() + raw_input() #wait for newline + + bsl.comDone() #Release serial communication port + +if __name__ == '__main__': + try: + main(1) + except SystemExit: + raise #let pass exit() calls + except KeyboardInterrupt: + if DEBUG: raise #show full trace in debug mode + sys.stderr.write("user abort.\n") #short messy in user mode + sys.exit(1) #set errorlevel for script usage + except Exception, msg: #every Exception is caught and displayed + if DEBUG: raise #show full trace in debug mode + #sys.stderr.write("\nAn error occoured:\n%s\n" % msg) #short messy in user mode + #sys.exit(1) #set errorlevel for script usage + main(0); diff --git a/tools/zolertia/z1-bsl-nopic b/tools/zolertia/z1-bsl-nopic new file mode 100755 index 000000000..8b5edd8f5 --- /dev/null +++ b/tools/zolertia/z1-bsl-nopic @@ -0,0 +1,1941 @@ +#!/usr/bin/env python2 +# Serial Bootstrap Loader software for the MSP430 embedded proccessor. +# +# (C) 2001-2003 Chris Liechti +# this is distributed under a free software license, see license-bsl.txt +# +# fixes from Colin Domoney +# +# based on the application note slas96b.pdf from Texas Instruments, Inc., +# Volker Rzehak +# additional infos from slaa089a.pdf +# +# Forked by Travis Goodspeed for use with the GoodFET +# JTAG programmer. + +import sys, time, string, cStringIO, struct +sys.path.append("/usr/lib/tinyos") +import serial, os, glob + +#forked from TinyOS Telos version. +VERSION = string.split("Revision: 1.39-goodfet-8 z1 fork")[1] + +DEBUG = 0 #disable debug messages by default + +#copy of the patch file provided by TI +#this part is (C) by Texas Instruments +PATCH = """@0220 +31 40 1A 02 09 43 B0 12 2A 0E B0 12 BA 0D 55 42 +0B 02 75 90 12 00 1F 24 B0 12 BA 02 55 42 0B 02 +75 90 16 00 16 24 75 90 14 00 11 24 B0 12 84 0E +06 3C B0 12 94 0E 03 3C 21 53 B0 12 8C 0E B2 40 +10 A5 2C 01 B2 40 00 A5 28 01 30 40 42 0C 30 40 +76 0D 30 40 AC 0C 16 42 0E 02 17 42 10 02 E2 B2 +08 02 14 24 B0 12 10 0F 36 90 00 10 06 28 B2 40 +00 A5 2C 01 B2 40 40 A5 28 01 D6 42 06 02 00 00 +16 53 17 83 EF 23 B0 12 BA 02 D3 3F B0 12 10 0F +17 83 FC 23 B0 12 BA 02 D0 3F 18 42 12 02 B0 12 +10 0F D2 42 06 02 12 02 B0 12 10 0F D2 42 06 02 +13 02 38 E3 18 92 12 02 BF 23 E2 B3 08 02 BC 23 +30 41 +q +""" + +#These BSL's are (C) by TI. They come with the application note slaa089a +F1X_BSL = """@0220 +24 02 2E 02 31 40 20 02 2B D2 C0 43 EA FF 32 C2 +F2 C0 32 00 00 00 B2 40 80 5A 20 01 F2 40 85 00 +57 00 F2 40 80 00 56 00 E2 D3 21 00 E2 D3 22 00 +E2 C3 26 00 E2 C2 2A 00 E2 C2 2E 00 B2 40 10 A5 +2C 01 B2 40 00 A5 28 01 3B C0 3A 00 B0 12 D6 04 +82 43 12 02 09 43 36 40 0A 02 37 42 B0 12 AC 05 +C6 4C 00 00 16 53 17 83 F9 23 D2 92 0C 02 0D 02 +28 20 55 42 0B 02 75 90 12 00 80 24 75 90 10 00 +6D 24 B0 12 9C 04 55 42 0B 02 75 90 18 00 31 24 +75 90 1E 00 B8 24 75 90 20 00 17 24 2B B2 11 24 +75 90 16 00 22 24 75 90 14 00 B3 24 75 90 1A 00 +18 24 75 90 1C 00 45 24 04 3C B0 12 36 05 BE 3F +21 53 B0 12 3C 05 BA 3F 03 43 B0 12 36 05 D2 42 +0E 02 56 00 D2 42 0F 02 57 00 D2 42 10 02 16 02 +AD 3F B0 12 36 05 10 42 0E 02 16 42 0E 02 15 43 +07 3C 36 40 FE FF B2 40 06 A5 10 02 35 40 0C 00 +B2 40 00 A5 2C 01 92 42 10 02 28 01 B6 43 00 00 +92 B3 2C 01 FD 23 15 83 F3 23 36 90 FE FF CD 27 +37 40 80 00 36 F0 80 FF 36 90 00 11 0E 28 07 57 +36 F0 00 FF 36 90 00 12 08 28 07 57 36 F0 00 FE +04 3C 16 42 0E 02 17 42 10 02 35 43 75 96 03 20 +17 83 FC 23 B2 3F 82 46 00 02 B3 3F 36 40 E0 FF +37 40 20 00 B0 12 AC 05 7C 96 01 24 2B D3 17 83 +F9 23 2B C2 B0 12 9C 04 2B D2 9F 3F 16 42 0E 02 +17 42 10 02 2B B2 38 24 3B D0 10 00 B0 12 AC 05 +36 90 00 10 06 2C 36 90 00 01 09 2C C6 4C 00 00 +25 3C B2 40 00 A5 2C 01 B2 40 40 A5 28 01 16 B3 +03 20 C2 4C 14 02 1A 3C C2 4C 15 02 86 9A FD FF +08 24 2B D3 3B B0 20 00 04 20 3B D0 20 00 82 46 +00 02 36 90 01 02 04 28 3B D2 3B B0 10 00 02 24 +3B C0 32 00 1A 42 14 02 86 4A FF FF 16 53 17 83 +CD 23 B0 12 9C 04 61 3F B0 12 AC 05 17 83 FC 23 +B0 12 9C 04 5E 3F B2 40 F0 0F 0E 02 B2 40 10 00 +10 02 B2 40 80 00 0A 02 D2 42 10 02 0C 02 D2 42 +10 02 0D 02 82 43 12 02 09 43 36 40 0A 02 27 42 +7C 46 B0 12 40 05 17 83 FB 23 16 42 0E 02 17 42 +10 02 36 90 00 01 0A 28 B2 46 14 02 5C 42 14 02 +B0 12 40 05 17 83 5C 42 15 02 01 3C 7C 46 B0 12 +40 05 17 83 EE 23 B2 E3 12 02 5C 42 12 02 B0 12 +40 05 5C 42 13 02 B0 12 40 05 E0 3E 18 42 12 02 +B0 12 AC 05 C2 4C 12 02 B0 12 AC 05 C2 4C 13 02 +38 E3 3B B2 0A 24 86 9A FE FF 07 24 3B B0 20 00 +04 20 16 53 82 46 00 02 2B D3 18 92 12 02 08 23 +2B B3 06 23 30 41 E2 B2 28 00 FD 27 E2 B2 28 00 +FD 23 B2 40 24 02 60 01 E2 B2 28 00 FD 27 15 42 +70 01 05 11 05 11 05 11 82 45 02 02 05 11 82 45 +04 02 B2 80 1E 00 04 02 57 42 16 02 37 80 03 00 +05 11 05 11 17 53 FD 23 35 50 40 A5 82 45 2A 01 +35 42 B2 40 24 02 60 01 92 92 70 01 02 02 FC 2F +15 83 F7 23 09 43 7C 40 90 00 02 3C 7C 40 A0 00 +C2 43 07 02 C9 EC 12 02 19 E3 1B C3 55 42 07 02 +55 45 56 05 00 55 0C 2E 2E 2E 2E 2E 2E 2E 2E 1A +34 34 92 42 70 01 72 01 B2 50 0C 00 72 01 07 3C +1B B3 0B 20 82 43 62 01 92 B3 62 01 FD 27 E2 C3 +21 00 0A 3C 4C 11 F6 2B 1B E3 82 43 62 01 92 B3 +62 01 FD 27 E2 D3 21 00 92 52 02 02 72 01 D2 53 +07 02 F0 90 0C 00 61 FC D1 23 30 41 C2 43 09 02 +1B C3 55 42 09 02 55 45 BC 05 00 55 0C 56 56 56 +56 56 56 56 56 36 76 00 E2 B2 28 00 FD 23 92 42 +70 01 72 01 92 52 04 02 72 01 82 43 62 01 92 B3 +62 01 FD 27 E2 B2 28 00 1E 28 2B D3 1C 3C 4C 10 +1A 3C 82 43 62 01 92 B3 62 01 FD 27 E2 B2 28 00 +01 28 1B E3 1B B3 01 24 2B D3 C9 EC 12 02 19 E3 +0A 3C 82 43 62 01 92 B3 62 01 FD 27 E2 B2 28 00 +E6 2B 4C 10 1B E3 92 52 02 02 72 01 D2 53 09 02 +C0 3F 82 43 62 01 92 B3 62 01 FD 27 E2 B2 28 00 +01 2C 2B D3 30 41 +q +""" + +F4X_BSL = """@0220 +24 02 2E 02 31 40 20 02 2B D2 C0 43 EA FF 32 C2 +F2 C0 32 00 00 00 B2 40 80 5A 20 01 32 D0 40 00 +C2 43 50 00 F2 40 98 00 51 00 F2 C0 80 00 52 00 +D2 D3 21 00 D2 D3 22 00 D2 C3 26 00 E2 C3 22 00 +E2 C3 26 00 B2 40 10 A5 2C 01 B2 40 00 A5 28 01 +3B C0 3A 00 B0 12 DE 04 82 43 12 02 09 43 36 40 +0A 02 37 42 B0 12 B4 05 C6 4C 00 00 16 53 17 83 +F9 23 D2 92 0C 02 0D 02 28 20 55 42 0B 02 75 90 +12 00 80 24 75 90 10 00 6D 24 B0 12 A4 04 55 42 +0B 02 75 90 18 00 31 24 75 90 1E 00 B8 24 75 90 +20 00 17 24 2B B2 11 24 75 90 16 00 22 24 75 90 +14 00 B3 24 75 90 1A 00 18 24 75 90 1C 00 45 24 +04 3C B0 12 3E 05 BE 3F 21 53 B0 12 44 05 BA 3F +03 43 B0 12 3E 05 D2 42 0E 02 50 00 D2 42 0F 02 +51 00 D2 42 10 02 16 02 AD 3F B0 12 3E 05 10 42 +0E 02 16 42 0E 02 15 43 07 3C 36 40 FE FF B2 40 +06 A5 10 02 35 40 0C 00 B2 40 00 A5 2C 01 92 42 +10 02 28 01 B6 43 00 00 92 B3 2C 01 FD 23 15 83 +F3 23 36 90 FE FF CD 27 37 40 80 00 36 F0 80 FF +36 90 00 11 0E 28 07 57 36 F0 00 FF 36 90 00 12 +08 28 07 57 36 F0 00 FE 04 3C 16 42 0E 02 17 42 +10 02 35 43 75 96 03 20 17 83 FC 23 B2 3F 82 46 +00 02 B3 3F 36 40 E0 FF 37 40 20 00 B0 12 B4 05 +7C 96 01 24 2B D3 17 83 F9 23 2B C2 B0 12 A4 04 +2B D2 9F 3F 16 42 0E 02 17 42 10 02 2B B2 38 24 +3B D0 10 00 B0 12 B4 05 36 90 00 10 06 2C 36 90 +00 01 09 2C C6 4C 00 00 25 3C B2 40 00 A5 2C 01 +B2 40 40 A5 28 01 16 B3 03 20 C2 4C 14 02 1A 3C +C2 4C 15 02 86 9A FD FF 08 24 2B D3 3B B0 20 00 +04 20 3B D0 20 00 82 46 00 02 36 90 01 02 04 28 +3B D2 3B B0 10 00 02 24 3B C0 32 00 1A 42 14 02 +86 4A FF FF 16 53 17 83 CD 23 B0 12 A4 04 61 3F +B0 12 B4 05 17 83 FC 23 B0 12 A4 04 5E 3F B2 40 +F0 0F 0E 02 B2 40 10 00 10 02 B2 40 80 00 0A 02 +D2 42 10 02 0C 02 D2 42 10 02 0D 02 82 43 12 02 +09 43 36 40 0A 02 27 42 7C 46 B0 12 48 05 17 83 +FB 23 16 42 0E 02 17 42 10 02 36 90 00 01 0A 28 +B2 46 14 02 5C 42 14 02 B0 12 48 05 17 83 5C 42 +15 02 01 3C 7C 46 B0 12 48 05 17 83 EE 23 B2 E3 +12 02 5C 42 12 02 B0 12 48 05 5C 42 13 02 B0 12 +48 05 E0 3E 18 42 12 02 B0 12 B4 05 C2 4C 12 02 +B0 12 B4 05 C2 4C 13 02 38 E3 3B B2 0A 24 86 9A +FE FF 07 24 3B B0 20 00 04 20 16 53 82 46 00 02 +2B D3 18 92 12 02 08 23 2B B3 06 23 30 41 E2 B3 +20 00 FD 27 E2 B3 20 00 FD 23 B2 40 24 02 60 01 +E2 B3 20 00 FD 27 15 42 70 01 05 11 05 11 05 11 +82 45 02 02 05 11 82 45 04 02 B2 80 1E 00 04 02 +57 42 16 02 37 80 03 00 05 11 05 11 17 53 FD 23 +35 50 40 A5 82 45 2A 01 35 42 B2 40 24 02 60 01 +92 92 70 01 02 02 FC 2F 15 83 F7 23 09 43 7C 40 +90 00 02 3C 7C 40 A0 00 C2 43 07 02 C9 EC 12 02 +19 E3 1B C3 55 42 07 02 55 45 5E 05 00 55 0C 2E +2E 2E 2E 2E 2E 2E 2E 1A 34 34 92 42 70 01 72 01 +B2 50 0C 00 72 01 07 3C 1B B3 0B 20 82 43 62 01 +92 B3 62 01 FD 27 D2 C3 21 00 0A 3C 4C 11 F6 2B +1B E3 82 43 62 01 92 B3 62 01 FD 27 D2 D3 21 00 +92 52 02 02 72 01 D2 53 07 02 F0 90 0C 00 59 FC +D1 23 30 41 C2 43 09 02 1B C3 55 42 09 02 55 45 +C4 05 00 55 0C 56 56 56 56 56 56 56 56 36 76 00 +E2 B3 20 00 FD 23 92 42 70 01 72 01 92 52 04 02 +72 01 82 43 62 01 92 B3 62 01 FD 27 E2 B3 20 00 +1E 28 2B D3 1C 3C 4C 10 1A 3C 82 43 62 01 92 B3 +62 01 FD 27 E2 B3 20 00 01 28 1B E3 1B B3 01 24 +2B D3 C9 EC 12 02 19 E3 0A 3C 82 43 62 01 92 B3 +62 01 FD 27 E2 B3 20 00 E6 2B 4C 10 1B E3 92 52 +02 02 72 01 D2 53 09 02 C0 3F 82 43 62 01 92 B3 +62 01 FD 27 E2 B3 20 00 01 2C 2B D3 30 41 +q +""" + +CALIB_TABLES = """ +84 C0 16 FE FF FF FF FF FF FF FF FF FF FF FF FF +FF FF FF FF FF FF FF FF FF FF 10 08 80 00 00 01 +7E 27 0B 79 0D AA 7E 4D 06 DF 08 33 08 FE FF FF +FF FF FF FF FF FF 08 01 8F 95 8E 9A 8D 88 86 D1 +""" + +#cpu types for "change baudrate" +#use strings as ID so that they can be used in outputs too +F1x = "F1x family" +F2x = "F2x family" +F4x = "F4x family" + +#known device list +deviceids = { + 0xf149: F1x, + 0xf16c: F1x, #for GoodFET10 to '20 + 0xf112: F1x, + 0xf413: F4x, + 0xf123: F1x, + 0xf449: F4x, + 0x1232: F1x, + 0xf26f: F2x, #for GoodFET20 + 0xf227: F2x, #for GoodFET30 +} + +#GoodFET firmware +firmware = { + 0xf16c: "http://goodfet.sourceforge.net/dist/msp430x1612.hex", + 0xf26f: "http://goodfet.sourceforge.net/dist/msp430x2618.hex", + 0xf227: "http://goodfet.sourceforge.net/dist/msp430x2274.hex" +} + +class BSLException(Exception): + pass + +class LowLevel: + "lowlevel communication" + #Constants + MODE_SSP = 0 + MODE_BSL = 1 + + BSL_SYNC = 0x80 + BSL_TXPWORD = 0x10 + BSL_TXBLK = 0x12 #Transmit block to boot loader + BSL_UNLOCK_INFO = 0x12 #unlocks / locks info segment A Not working on msp430f2617 + BSL_RXBLK = 0x14 #Receive block from boot loader + BSL_ERASE = 0x16 #Erase one segment + BSL_MERAS = 0x18 #Erase complete FLASH memory + BSL_CHANGEBAUD = 0x20 #Change baudrate + BSL_SETMEMOFFSET = 0x21 #Set memory offset + BSL_LOADPC = 0x1A #Load PC and start execution + BSL_TXVERSION = 0x1E #Get BSL version + + #Upper limit of address range that might be modified by + #"BSL checksum bug". + BSL_CRITICAL_ADDR = 0x0A00 + + #Header Definitions + CMD_FAILED = 0x70 + DATA_FRAME = 0x80 + DATA_ACK = 0x90 + DATA_NAK = 0xA0 + + QUERY_POLL = 0xB0 + QUERY_RESPONSE = 0x50 + + OPEN_CONNECTION = 0xC0 + ACK_CONNECTION = 0x40 + + DEFAULT_TIMEOUT = 1 + DEFAULT_PROLONG = 10 + MAX_FRAME_SIZE = 256 + MAX_DATA_BYTES = 250 + MAX_DATA_WORDS = 125 + + MAX_FRAME_COUNT = 16 + + #Error messages + ERR_COM = "Unspecific error" + ERR_RX_NAK = "NAK received (wrong password?)" + #ERR_CMD_NOT_COMPLETED = "Command did not send ACK: indicates that it didn't complete correctly" + ERR_CMD_FAILED = "Command failed, is not defined or is not allowed" + ERR_BSL_SYNC = "Bootstrap loader synchronization error" + ERR_FRAME_NUMBER = "Frame sequence number error." + + def calcChecksum(self, data, length): + """Calculates a checksum of "data".""" + checksum = 0 + + for i in range(length/2): + checksum = checksum ^ (ord(data[i*2]) | (ord(data[i*2+1]) << 8)) #xor-ing + return 0xffff & (checksum ^ 0xffff) #inverting + + def __init__(self, aTimeout = None, aProlongFactor = None): + """init bsl object, don't connect yet""" + if aTimeout is None: + self.timeout = self.DEFAULT_TIMEOUT + else: + self.timeout = aTimeout + if aProlongFactor is None: + self.prolongFactor = self.DEFAULT_PROLONG + else: + self.prolongFactor = aProlongFactor + + #flags for inverted use of control pins + #used for some hardware + self.invertRST = 0 + self.invertTEST = 0 + self.swapRSTTEST = 0 + self.telosLatch = 0 + self.telosI2C = 0 + + self.z1 = 0 + + self.protocolMode = self.MODE_BSL + self.BSLMemAccessWarning = 0 #Default: no warning. + self.slowmode = 0 + + def comInit(self, port): + """Tries to open the serial port given and + initialises the port and variables. + The timeout and the number of allowed errors is multiplied by + 'aProlongFactor' after transmission of a command to give + plenty of time to the micro controller to finish the command. + Returns zero if the function is successful.""" + if DEBUG > 1: sys.stderr.write("* comInit()\n") + self.seqNo = 0 + self.reqNo = 0 + self.rxPtr = 0 + self.txPtr = 0 + # Startup-Baudrate: 9600,8,E,1, 1s timeout + self.serialport = serial.Serial( + port, + 9600, + parity = serial.PARITY_EVEN, + timeout = self.timeout + ) + if DEBUG: sys.stderr.write("using serial port %r\n" % self.serialport.portstr) + #self.SetRSTpin() #enable power + #self.SetTESTpin() #enable power + self.serialport.flushInput() + self.serialport.flushOutput() + + def comDone(self): + """Closes the used serial port. + This function must be called at the end of a program, + otherwise the serial port might not be released and can not be + used in other programs. + Returns zero if the function is successful.""" + if DEBUG > 1: sys.stderr.write("* comDone()") + self.SetRSTpin(1) #disable power + self.SetTESTpin(0) #disable power + self.serialport.close() + + def comRxHeader(self): + """receive header and split data""" + if DEBUG > 1: sys.stderr.write("* comRxHeader()\n") + + hdr = self.serialport.read(1) + if not hdr: raise BSLException("Timeout") + rxHeader = ord(hdr) & 0xf0; + rxNum = ord(hdr) & 0x0f; + + if self.protocolMode == self.MODE_BSL: + self.reqNo = 0 + self.seqNo = 0 + rxNum = 0 + if DEBUG > 1: sys.stderr.write("* comRxHeader() OK\n") + return rxHeader, rxNum + + def comRxFrame(self, rxNum): + if DEBUG > 1: sys.stderr.write("* comRxFrame()\n") + rxFrame = chr(self.DATA_FRAME | rxNum) + + if DEBUG > 2: sys.stderr.write(" comRxFrame() header...\n") + rxFramedata = self.serialport.read(3) + if len(rxFramedata) != 3: raise BSLException("Timeout") + rxFrame = rxFrame + rxFramedata + + if DEBUG > 3: sys.stderr.write(" comRxFrame() check header...\n") + if rxFrame[1] == chr(0) and rxFrame[2] == rxFrame[3]: #Add. header info. correct? + rxLengthCRC = ord(rxFrame[2]) + 2 #Add CRC-Bytes to length + if DEBUG > 2: sys.stderr.write(" comRxFrame() receiving data, size: %s\n" % rxLengthCRC) + + rxFramedata = self.serialport.read(rxLengthCRC) + if len(rxFramedata) != rxLengthCRC: raise BSLException("Timeout") + rxFrame = rxFrame + rxFramedata + #Check received frame: + if DEBUG > 3: sys.stderr.write(" comRxFrame() crc check\n") + #rxLength+4: Length with header but w/o CRC: + checksum = self.calcChecksum(rxFrame, ord(rxFrame[2]) + 4) + if rxFrame[ord(rxFrame[2])+4] == chr(0xff & checksum) and \ + rxFrame[ord(rxFrame[2])+5] == chr(0xff & (checksum >> 8)): #Checksum correct? + #Frame received correctly (=> send next frame) + if DEBUG > 2: sys.stderr.write("* comRxFrame() OK\n") + return rxFrame + else: + if DEBUG: sys.stderr.write(" comRxFrame() Checksum wrong\n") + else: + if DEBUG: sys.stderr.write(" comRxFrame() Header corrupt %r" % rxFrame) + raise BSLException(self.ERR_COM) #Frame has errors! + + def comTxHeader(self, txHeader): + """send header""" + if DEBUG > 1: sys.stderr.write("* txHeader()\n") + self.serialport.write(txHeader) + + def comTxRx(self, cmd, dataOut, length): + """Sends the command cmd with the data given in dataOut to the + microcontroller and expects either an acknowledge or a frame + with result from the microcontroller. The results are stored + in dataIn (if not a NULL pointer is passed). + In this routine all the necessary protocol stuff is handled. + Returns zero if the function was successful.""" + if DEBUG > 1: sys.stderr.write("* comTxRx()\n") + txFrame = [] + rxHeader = 0 + rxNum = 0 + + dataOut = list(dataOut) #convert to a list for simpler data fill in + #Transmitting part ---------------------------------------- + #Prepare data for transmit + if (length % 2) != 0: + #/* Fill with one byte to have even number of bytes to send */ + if self.protocolMode == self.MODE_BSL: + dataOut.append(0xFF) #fill with 0xFF + else: + dataOut.append(0) #fill with zero + + txFrame = "%c%c%c%c" % (self.DATA_FRAME | self.seqNo, cmd, len(dataOut), len(dataOut)) + + self.reqNo = (self.seqNo + 1) % self.MAX_FRAME_COUNT + + txFrame = txFrame + string.join(dataOut,'') + checksum = self.calcChecksum(txFrame, length + 4) + txFrame = txFrame + chr(checksum & 0xff) + txFrame = txFrame + chr((checksum >> 8) & 0xff) + + accessAddr = (0x0212 + (checksum^0xffff)) & 0xfffe #0x0212: Address of wCHKSUM + if self.BSLMemAccessWarning and accessAddr < self.BSL_CRITICAL_ADDR: + sys.stderr.write("WARNING: This command might change data at address %04x or %04x!\n" % (accessAddr, accessAddr + 1)) + + self.serialport.flushInput() #clear receiving queue + #TODO: Check after each transmitted character, + #TODO: if microcontroller did send a character (probably a NAK!). + for c in txFrame: + self.serialport.write(c) + if DEBUG > 3: sys.stderr.write("\ttx %02x" % ord(c)) + #if self.serialport.inWaiting(): break #abort when BSL replies, probably NAK + else: + if DEBUG > 1: sys.stderr.write( " comTxRx() transmit OK\n") + + #Receiving part ------------------------------------------- + rxHeader, rxNum = self.comRxHeader() #receive header + if DEBUG > 1: sys.stderr.write(" comTxRx() rxHeader=0x%02x, rxNum=%d, seqNo=%d, reqNo=%s\n" % (rxHeader, rxNum, self.seqNo, self.reqNo)) + if rxHeader == self.DATA_ACK: #acknowledge/OK + if DEBUG > 2: sys.stderr.write(" comTxRx() DATA_ACK\n") + if rxNum == self.reqNo: + self.seqNo = self.reqNo + if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_ACK OK\n") + return #Acknowledge received correctly => next frame + raise BSLException(self.ERR_FRAME_NUMBER) + elif rxHeader == self.DATA_NAK: #not acknowledge/error + if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_NAK\n") + raise BSLException(self.ERR_RX_NAK) + elif rxHeader == self.DATA_FRAME: #receive data + if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_FRAME\n") + if rxNum == self.reqNo: + rxFrame = self.comRxFrame(rxNum) + return rxFrame + raise BSLException(self.ERR_FRAME_NUMBER) + elif rxHeader == self.CMD_FAILED: #Frame ok, but command failed. + if DEBUG > 2: sys.stderr.write("* comTxRx() CMD_FAILED\n") + raise BSLException(self.ERR_CMD_FAILED) + + raise BSLException("Unknown header 0x%02x\nAre you downloading to RAM into an old device that requires the patch? Try option -U" % rxHeader) + + def SetDTR(self, level, invert): + """Controls DTR pin (0: GND; 1: VCC; unless inverted flag is set)""" + #print 'fucking the dtr' + if invert: + self.serialport.setDTR(not level) + else: + self.serialport.setDTR(level) + if self.slowmode: + time.sleep(0.040) + + def SetRTS(self, level, invert): + """Controls RTS pin (0: GND; 1: VCC; unless inverted flag is set)""" + if invert: + self.serialport.setRTS(not level) + else: + self.serialport.setRTS(level) + if self.slowmode: + time.sleep(0.040) + + def SetRSTpin(self, level=1): + """Controls RST/NMI pin (0: GND; 1: VCC; unless inverted flag is set)""" + if self.swapRSTTEST: + self.SetRTS(level, self.invertRST) + else: + self.SetDTR(level, self.invertRST) + + def SetTESTpin(self, level=1): + """Controls TEST pin (inverted on board: 0: VCC; 1: GND; unless inverted flag is set)""" + if self.swapRSTTEST: + self.SetDTR(level, self.invertTEST) + else: + self.SetRTS(level, self.invertTEST) + + + def writepicROM(self, address, data): + ''' Writes data to @address''' + for i in range(7,-1,-1): + self.picROMclock((address >> i) & 0x01) + self.picROMclock(0) + recbuf = 0 + for i in range(7,-1,-1): + s = ((data >> i) & 0x01) + #print s + if i < 1: + r = not self.picROMclock(s, True) + else: + r = not self.picROMclock(s) + recbuf = (recbuf << 1) + r + + self.picROMclock(0, True) + #k = 1 + #while not self.serial.getCTS(): + # pass + #time.sleep(0.1) + return recbuf + + def readpicROM(self, address): + ''' reads a byte from @address''' + for i in range(7,-1,-1): + self.picROMclock((address >> i) & 0x01) + self.picROMclock(1) + recbuf = 0 + r = 0 + for i in range(7,-1,-1): + r = self.picROMclock(0) + recbuf = (recbuf << 1) + r + self.picROMclock(r) + #time.sleep(0.1) + return recbuf + + def picROMclock(self, masterout, slow = False): + #print "setting masterout to "+str(masterout) + self.serialport.setRTS(masterout) + self.serialport.setDTR(1) + #time.sleep(0.02) + self.serialport.setDTR(0) + if slow: + time.sleep(0.02) + return self.serialport.getCTS() + + def picROMfastclock(self, masterout): + #print "setting masterout to "+str(masterout) + self.serialport.setRTS(masterout) + self.serialport.setDTR(1) + self.serialport.setDTR(0) + time.sleep(0.02) + return self.serialport.getCTS() + + def bslResetZ1(self, invokeBSL=0): + ''' + Applies BSL entry sequence on RST/NMI and TEST/VPP pins + Parameters: + invokeBSL = 1: complete sequence + invokeBSL = 0: only RST/NMI pin accessed + + By now only BSL mode is accessed + ''' + + if DEBUG > 1: sys.stderr.write("* bslReset(invokeBSL=%s)\n" % invokeBSL) + if invokeBSL: + #sys.stderr.write("in Z1 bsl reset...\n") + time.sleep(0.1) + self.writepicROM(0xFF, 0xFF) + time.sleep(0.1) + #sys.stderr.write("z1 bsl reset done...\n") + else: + #sys.stderr.write("in Z1 reset...\n") + time.sleep(0.1) + self.writepicROM(0xFF, 0xFE) + time.sleep(0.1) + #sys.stderr.write("z1 reset done...\n") + + def telosSetSCL(self, level): + self.serialport.setRTS(not level) + + def telosSetSDA(self, level): + self.serialport.setDTR(not level) + + def telosI2CStart(self): + self.telosSetSDA(1) + self.telosSetSCL(1) + self.telosSetSDA(0) + + def telosI2CStop(self): + self.telosSetSDA(0) + self.telosSetSCL(1) + self.telosSetSDA(1) + + def telosI2CWriteBit(self, bit): + self.telosSetSCL(0) + self.telosSetSDA(bit) + time.sleep(2e-6) + self.telosSetSCL(1) + time.sleep(1e-6) + self.telosSetSCL(0) + + def telosI2CWriteByte(self, byte): + self.telosI2CWriteBit( byte & 0x80 ); + self.telosI2CWriteBit( byte & 0x40 ); + self.telosI2CWriteBit( byte & 0x20 ); + self.telosI2CWriteBit( byte & 0x10 ); + self.telosI2CWriteBit( byte & 0x08 ); + self.telosI2CWriteBit( byte & 0x04 ); + self.telosI2CWriteBit( byte & 0x02 ); + self.telosI2CWriteBit( byte & 0x01 ); + self.telosI2CWriteBit( 0 ); # "acknowledge" + + def telosI2CWriteCmd(self, addr, cmdbyte): + self.telosI2CStart() + self.telosI2CWriteByte( 0x90 | (addr << 1) ) + self.telosI2CWriteByte( cmdbyte ) + self.telosI2CStop() + + def telosBReset(self,invokeBSL=0): + # "BSL entry sequence at dedicated JTAG pins" + # rst !s0: 0 0 0 0 1 1 + # tck !s1: 1 0 1 0 0 1 + # s0|s1: 1 3 1 3 2 0 + + # "BSL entry sequence at shared JTAG pins" + # rst !s0: 0 0 0 0 1 1 + # tck !s1: 0 1 0 1 1 0 + # s0|s1: 3 1 3 1 0 2 + if invokeBSL: + self.telosI2CWriteCmd(0,1) + self.telosI2CWriteCmd(0,3) + self.telosI2CWriteCmd(0,1) + self.telosI2CWriteCmd(0,3) + self.telosI2CWriteCmd(0,2) + self.telosI2CWriteCmd(0,0) + else: + self.telosI2CWriteCmd(0,3) + self.telosI2CWriteCmd(0,2) + self.telosI2CWriteCmd(0,0) + time.sleep(0.250) #give MSP430's oscillator time to stabilize + self.serialport.flushInput() #clear buffers + + def bslReset(self, invokeBSL=0): + """Applies BSL entry sequence on RST/NMI and TEST/VPP pins + Parameters: + invokeBSL = 1: complete sequence + invokeBSL = 0: only RST/NMI pin accessed + + RST is inverted twice on boot loader hardware + TEST is inverted (only once) + Need positive voltage on DTR, RTS for power-supply of hardware""" + + #print 'goint to reset!' + + if self.telosI2C: + self.telosBReset(invokeBSL) + return + + if self.z1: + if DEBUG > 1: sys.stderr.write("* entering bsl with z1\n") + self.bslResetZ1(invokeBSL) + return + + if DEBUG > 1: sys.stderr.write("* bslReset(invokeBSL=%s)\n" % invokeBSL) + self.SetRSTpin(1) #power suply + self.SetTESTpin(1) #power suply + time.sleep(0.250) #charge capacitor on boot loader hardware + + if self.telosLatch: + self.SetTESTpin(0) + self.SetRSTpin(0) + self.SetTESTpin(1) + + self.SetRSTpin(0) #RST pin: GND + if invokeBSL: + self.SetTESTpin(1) #TEST pin: GND + self.SetTESTpin(0) #TEST pin: Vcc + self.SetTESTpin(1) #TEST pin: GND + self.SetTESTpin(0) #TEST pin: Vcc + self.SetRSTpin (1) #RST pin: Vcc + self.SetTESTpin(1) #TEST pin: GND + else: + self.SetRSTpin(1) #RST pin: Vcc + time.sleep(0.250) #give MSP430's oscillator time to stabilize + + self.serialport.flushInput() #clear buffers + + def bslSync(self,wait=0): + """Transmits Synchronization character and expects to receive Acknowledge character + if wait is 0 it must work the first time. otherwise if wait is 1 + it is retried (forever). + """ + loopcnt = 5 #Max. tries to get synchronization + + if DEBUG > 1: sys.stderr.write("* bslSync(wait=%d)\n" % wait) + while wait or loopcnt: + loopcnt = loopcnt - 1 #count down tries + self.serialport.flushInput() #clear input, in case a prog is running + + self.serialport.write(chr(self.BSL_SYNC)) #Send synchronization byte + c = self.serialport.read(1) #read answer + if c == chr(self.DATA_ACK): #ACk + if DEBUG > 1: sys.stderr.write(" bslSync() OK\n") + return #Sync. successful + elif not c: #timeout + if loopcnt > 4: + if DEBUG > 1: + sys.stderr.write(" bslSync() timeout, retry ...\n") + elif loopcnt == 4: + #nmi may have caused the first reset to be ignored, try again + self.bslReset(0) + self.bslReset(1) + elif loopcnt > 0: + if DEBUG > 1: + sys.stderr.write(" bslSync() timeout, retry ...\n") + else : + if DEBUG > 1: + sys.stderr.write(" bslSync() timeout\n") + else: #garbage + if DEBUG > 1: sys.stderr.write(" bslSync() failed (0x%02x), retry ...\n" % ord(c)) + + raise BSLException(self.ERR_BSL_SYNC) #Sync. failed + + def bslTxRx(self, cmd, addr, length = 0, blkout = None, wait=0): + """Transmits a command (cmd) with its parameters: + start-address (addr), length (len) and additional + data (blkout) to boot loader. + wait specified if the bsl sync should be tried once or + repeated, forever + Parameters return by boot loader are passed via blkin. + """ + if DEBUG > 1: sys.stderr.write("* bslTxRx()\n") + + if cmd == self.BSL_TXBLK: + #Align to even start address + if (addr % 2) != 0: + addr = addr - 1 #Decrement address and + blkout = chr(0xFF) + blkOut #fill first byte of blkout with 0xFF + length = length + 1 + #Make sure that len is even + if (length % 2) != 0: + blkout = blkout + chr(0xFF) #Inc. len and fill last byte of blkout with 0xFF + length = length + 1 + + elif cmd == self.BSL_RXBLK: + #Align to even start address + if (addr % 2) != 0: + addr = addr - 1 #Decrement address but + length = length + 1 #request an additional byte + #Make sure that len is even + if (length % 2) != 0: + length = length + 1 + + #if cmd == self.BSL_TXBLK or cmd == self.BSL_TXPWORD: + # length = len + 4 + + #Add necessary information data to frame + dataOut = struct.pack("= 1: + #Read one line + l = file.readline() + if not l: break #EOF + l = l.strip() + if l[0] == 'q': break + elif l[0] == '@': #if @ => new address => send frame and set new addr. + #create a new segment + if segmentdata: + self.segments.append( Segment(startAddr, string.join(segmentdata,'')) ) + startAddr = int(l[1:],16) + segmentdata = [] + else: + for i in string.split(l): + segmentdata.append(chr(int(i,16))) + if segmentdata: + self.segments.append( Segment(startAddr, string.join(segmentdata,'')) ) + + def loadELF(self, file): + """load data from a (opened) file in ELF object format. + File must be seekable""" + import elf + obj = elf.ELFObject() + obj.fromFile(file) + if obj.e_type != elf.ELFObject.ET_EXEC: + raise Exception("No executable") + for section in obj.getSections(): + if DEBUG: + sys.stderr.write("ELF section %s at 0x%04x %d bytes\n" % (section.name, section.lma, len(section.data))) + if len(section.data): + self.segments.append( Segment(section.lma, section.data) ) + + def loadFile(self, filename): + """fill memory with the contents of a file. file type is determined from extension""" + #TODO: do a contents based detection + if filename[-4:].lower() == '.txt': + self.loadTIText(open(filename, "rb")) + elif filename[-4:].lower() in ('.a43', '.hex'): + self.loadIHex(open(filename, "rb")) + else: + self.loadELF(open(filename, "rb")) + + def getMemrange(self, fromadr, toadr): + """get a range of bytes from the memory. unavailable values are filled with 0xff.""" + res = '' + toadr = toadr + 1 #python indxes are excluding end, so include it + while fromadr < toadr: + #print "fromto: %04x %04x" % (fromadr, toadr) + for seg in self.segments: + #print seg + segend = seg.startaddress + len(seg.data) + if seg.startaddress <= fromadr and fromadr < segend: + #print "startok 0x%04x %d" % (seg.startaddress, len(seg.data)) + #print ("0x%04x "*3) % (segend, fromadr, toadr) + if toadr > segend: #not all data in segment + #print "out of segment" + catchlength = segend-fromadr + else: + catchlength = toadr-fromadr + #print toadr-fromadr + #print catchlength + res = res + seg.data[fromadr-seg.startaddress : fromadr-seg.startaddress+catchlength] + fromadr = fromadr + catchlength #adjust start + if len(res) >= toadr-fromadr: + break#return res + else: + res = res + chr(255) + fromadr = fromadr + 1 #adjust start + #print "fill FF" + #print "res: %r" % res + return res + + +class BootStrapLoader(LowLevel): + """higher level Bootstrap Loader functions.""" + + ERR_VERIFY_FAILED = "Error: verification failed" + ERR_ERASE_CHECK_FAILED = "Error: erase check failed" + + ACTION_PROGRAM = 0x01 #Mask: program data + ACTION_VERIFY = 0x02 #Mask: verify data + ACTION_ERASE_CHECK = 0x04 #Mask: erase check + + #Max. bytes sent within one frame if parsing a TI TXT file. + #( >= 16 and == n*16 and <= MAX_DATA_BYTES!) + MAXDATA = 240-16 + + + def __init__(self, *args, **kargs): + LowLevel.__init__(self, *args, **kargs) + self.byteCtr = 0 + self.meraseCycles = 1 + self.patchRequired = 0 + self.patchLoaded = 0 + self.bslVer = 0 + self.passwd = None + self.data = None + self.maxData = self.MAXDATA + self.cpu = None + self.pwdsent = False + + + def preparePatch(self): + """prepare to download patch""" + if DEBUG > 1: sys.stderr.write("* preparePatch()\n") + + if self.patchLoaded: + #Load PC with 0x0220. + #This will invoke the patched bootstrap loader subroutines. + self.bslTxRx(self.BSL_LOADPC, #Command: Load PC + 0x0220) #Address to load into PC + self.BSLMemAccessWarning = 0 #Error is removed within workaround code + return + + def postPatch(self): + """setup after the patch is loaded""" + if DEBUG > 1: sys.stderr.write("* postPatch()\n") + if self.patchLoaded: + self.BSLMemAccessWarning = 1 #Turn warning back on. + + + def verifyBlk(self, addr, blkout, action): + """verify memory against data or 0xff""" + if DEBUG > 1: sys.stderr.write("* verifyBlk()\n") + + if action & self.ACTION_VERIFY or action & self.ACTION_ERASE_CHECK: + if DEBUG: sys.stderr.write(" Check starting at 0x%04x, %d bytes ... \n" % (addr, len(blkout))) + + self.preparePatch() + blkin = self.bslTxRx(self.BSL_RXBLK, addr, len(blkout)) + self.postPatch() + + for i in range(len(blkout)): + if action & self.ACTION_VERIFY: + #Compare data in blkout and blkin + if blkin[i] != blkout[i]: + sys.stderr.write("Verification failed at 0x%04x (0x%02x, 0x%02x)\n" % (addr+i, ord(blkin[i]), ord(blkout[i]))) + sys.stderr.flush() + raise BSLException(self.ERR_VERIFY_FAILED) #Verify failed! + continue + elif action & self.ACTION_ERASE_CHECK: + #Compare data in blkin with erase pattern + if blkin[i] != chr(0xff): + sys.stderr.write("Erase Check failed at 0x%04x (0x%02x)\n" % (addr+i, ord(blkin[i]))) + sys.stderr.flush() + raise BSLException(self.ERR_ERASE_CHECK_FAILED) #Erase Check failed! + continue + def readBlk(self,adr,len): + """Read a block of memory.""" + blkin = self.bslTxRx(self.BSL_RXBLK, addr, len(blkout)) + + def programBlk(self, addr, blkout, action): + """program a memory block""" + if DEBUG > 1: sys.stderr.write("* programBlk()\n") + + #Check, if specified range is erased + self.verifyBlk(addr, blkout, action & self.ACTION_ERASE_CHECK) + + if action & self.ACTION_PROGRAM: + if DEBUG: sys.stderr.write(" Program starting at 0x%04x, %i bytes ...\n" % (addr, len(blkout))) + self.preparePatch() + #Program block + self.bslTxRx(self.BSL_TXBLK, addr, len(blkout), blkout) + self.postPatch() + + #Verify block + self.verifyBlk(addr, blkout, action & self.ACTION_VERIFY) + + #segments: + #list of tuples or lists: + #segements = [ (addr1, [d0,d1,d2,...]), (addr2, [e0,e1,e2,...])] + def programData(self, segments, action): + """programm or verify data""" + if DEBUG > 1: sys.stderr.write("* programData()\n") + for seg in segments: + currentAddr = seg.startaddress + offsetAddr = 0 + if (seg.startaddress > 0xFFFF): + offsetAddr = seg.startaddress >> 16 + self.bslTxRx(self.BSL_SETMEMOFFSET, 0, offsetAddr) + currentAddr = currentAddr - (offsetAddr << 16) + pstart = 0 + while pstart len(seg.data): + length = len(seg.data) - pstart + self.programBlk(currentAddr, seg.data[pstart:pstart+length], action) + pstart = pstart + length + currentAddr = currentAddr + length + self.byteCtr = self.byteCtr + length #total sum + + def uploadData(self, startaddress, size, wait=0): + """upload a datablock""" + if DEBUG > 1: sys.stderr.write("* uploadData()\n") + data = '' + pstart = 0 + while pstart size: + length = size - pstart + data = data + self.bslTxRx(self.BSL_RXBLK, + pstart+startaddress, + length, + wait=wait)[:-2] #cut away checksum + pstart = pstart + length + return data + + def txPasswd(self, passwd=None, wait=0): + """transmit password, default if None is given.""" + if DEBUG > 1: sys.stderr.write("* txPassword(%r)\n" % passwd) + if passwd is None or self.pwdsent: + #Send "standard" password to get access to protected functions. + sys.stderr.write("Transmit default password ...\n") + sys.stderr.flush() + #Flash is completely erased, the contents of all Flash cells is 0xff + passwd = chr(0xff)*32 + else: + #sanity check of password + if len(passwd) != 32: + raise ValueError, "password has wrong length (%d)\n" % len(passwd) + sys.stderr.write('Transmit password ...\n') + sys.stderr.flush() + #send the password + self.bslTxRx(self.BSL_TXPWORD, #Command: Transmit Password + 0xffe0, #Address of interupt vectors + 0x0020, #Number of bytes + passwd, #password + wait=wait) #if wait is 1, try to sync forever + self.pwdsent = True + + + def getNewPassword(self): + print 'getting new password' + newpassword = self.bslTxRx(self.BSL_RXBLK, 0xffe0, 0x0020) + return newpassword + + + def writeCalibTables(self): + print 'writing new tables' + print CALIB_TABLES + calibtables = ["84C016FEFFFFFFFFFFFFFFFFFFFFFFFF", + "FFFFFFFFFFFFFFFFFFFF100880000001", + "7E270B790DAA7E4D06DF083308FEFFFF", + "FFFFFFFFFFFF08018F958E9A8D8886D1"] + self.programBlk(0x10C0, "84C016FEFFFFFFFFFFFFFFFFFFFFFFFF", self.ACTION_PROGRAM) + self.programBlk(0x10D0, "FFFFFFFFFFFFFFFFFFFF100880000001", self.ACTION_PROGRAM) + self.programBlk(0x10E0, "7E270B790DAA7E4D06DF083308FEFFFF", self.ACTION_PROGRAM) + self.programBlk(0x10F0, "FFFFFFFFFFFF08018F958E9A8D8886D1", self.ACTION_PROGRAM) + + def writeCalibTables2(self): + print 'writing new tables 2ond part' + #self.programBlk(0x10C0, "84C016FEFFFFFFFFFFFFFFFFFFFFFFFF", self.ACTION_PROGRAM) + #self.programBlk(0x10D0, "FFFFFFFFFFFFFFFFFFFF100880000001", self.ACTION_PROGRAM) + #self.programBlk(0x10E0, "7E270B790DAA7E4D06DF083308FEFFFF", self.ACTION_PROGRAM) + #self.programBlk(0x10F0, "FFFFFFFFFFFF08018F958E9A8D8886D1", self.ACTION_PROGRAM) + + + #----------------------------------------------------------------- + + def actionMassErase(self): + """Erase the flash memory completely (with mass erase command)""" + sys.stderr.write("Mass Erase...\n") + sys.stderr.flush() + self.bslReset(1) #Invoke the boot loader. + for i in range(self.meraseCycles): + if i == 1: sys.stderr.write("Additional Mass Erase Cycles...\n") + self.bslTxRx(self.BSL_MERAS, #Command: Mass Erase + 0xff00, #Any address within flash memory. + 0xa506) #Required setting for mass erase! + self.passwd = None #No password file required! + #print "Mass Erase complete" + #Transmit password to get access to protected BSL functions. + self.txPasswd() + + def actionMainErase(self): + """Erase the main flash memory only""" + sys.stderr.write("Main Erase...\n") + sys.stderr.flush() + self.bslTxRx(self.BSL_ERASE, #Command: Segment Erase + 0xfffe, #Any address within flash memory. + 0xa504) #Required setting for main erase! + #self.passwd = None #Password gets erased WHY?? + + + + def actionStartBSL(self, usepatch=1, adjsp=1, replacementBSL=None, forceBSL=0, mayuseBSL=0, speed=None, bslreset=1): + """start BSL, download patch if desired and needed, adjust SP if desired""" + sys.stderr.write("Invoking BSL...\n") + sys.stderr.flush() + + #for '30, invertTEST=0, else =1 + if bslreset: + #detext if is z1? + self.bslReset(1) #Invoke the boot loader. + + + if(self.z1): + ''' recover password from pic''' + + self.txPasswd(self.passwd) #transmit password + + #Read actual bootstrap loader version. + #sys.stderr.write("Reading BSL version ...\n") + blkin = self.bslTxRx(self.BSL_RXBLK, #Command: Read/Receive Block + 0x0ff0, #Start address + 16) #No. of bytes to read + dev_id, bslVerHi, bslVerLo = struct.unpack(">H8xBB4x", blkin[:-2]) #cut away checksum and extract data + self.dev_id=dev_id; + + + if self.cpu is None: #cpy type forced? + if deviceids.has_key(dev_id): + self.cpu = deviceids[dev_id] #try to autodectect CPU type + + if DEBUG: + sys.stderr.write("Autodetect successful: %04x -> %s\n" % (dev_id, self.cpu)) + else: + sys.stderr.write("Autodetect failed! Unkown ID: %04x. Trying to continue anyway.\n" % dev_id) + self.cpu = F1x #assume something and try anyway.. + + sys.stderr.write("Current bootstrap loader version: %x.%x (Device ID: %04x)\n" % (bslVerHi, bslVerLo, dev_id)) + sys.stderr.flush() + self.bslVer = (bslVerHi << 8) | bslVerLo + + if self.bslVer <= 0x0110: #check if patch is needed + self.BSLMemAccessWarning = 1 + else: + self.BSLMemAccessWarning = 0 #Fixed in newer versions of BSL. + + if self.bslVer <= 0x0130 and adjsp: + #only do this on BSL where it's needed to prevent + #malfunction with F4xx devices/ newer ROM-BSLs + + #Execute function within bootstrap loader + #to prepare stack pointer for the following patch. + #This function will lock the protected functions again. + sys.stderr.write("Adjust SP. Load PC with 0x0C22 ...\n") + self.bslTxRx(self.BSL_LOADPC, #Command: Load PC + 0x0C22) #Address to load into PC + #Re-send password to re-gain access to protected functions. + self.txPasswd(self.passwd) + + #get internal BSL replacement if needed or forced by the user + #required if speed is set but an old BSL is in the device + #if a BSL is given by the user, that one is used and not the internal one + if ((mayuseBSL and speed and self.bslVer < 0x0150) or forceBSL) and replacementBSL is None: + replacementBSL = Memory() #File to program + if self.cpu == F4x: + if DEBUG: + sys.stderr.write("Using built in BSL replacement for F4x devices\n") + sys.stderr.flush() + replacementBSL.loadTIText(cStringIO.StringIO(F4X_BSL)) #parse embedded BSL + else: + if DEBUG: + sys.stderr.write("Using built in BSL replacement for F1x devices\n") + sys.stderr.flush() + replacementBSL.loadTIText(cStringIO.StringIO(F1X_BSL)) #parse embedded BSL + + #now download the new BSL, if allowed and needed (version lower than the + #the replacement) or forced + if replacementBSL is not None: + self.actionDownloadBSL(replacementBSL) + + #debug message with the real BSL version in use (may have changed after replacement BSL) + if DEBUG: + sys.stderr.write("Current bootstrap loader version: 0x%04x\n" % (self.bslVer,)) + sys.stderr.flush() + + #now apply workarounds or patches if BSL in use requires that + if self.bslVer <= 0x0110: #check if patch is needed + if usepatch: #test if patch is desired + sys.stderr.write("Patch for flash programming required!\n") + self.patchRequired = 1 + + sys.stderr.write("Load and verify patch ...\n") + sys.stderr.flush() + #Programming and verification is done in one pass. + #The patch file is only read and parsed once. + segments = Memory() #data to program + segments.loadTIText(cStringIO.StringIO(PATCH)) #parse embedded patch + #program patch + self.programData(segments, self.ACTION_PROGRAM | self.ACTION_VERIFY) + self.patchLoaded = 1 + else: + if DEBUG: + sys.stderr.write("Device needs patch, but not applied (usepatch is false).\n") #message if not patched + + #should the baudrate be changed? + if speed is not None: + self.actionChangeBaudrate(speed) #change baudrate + + def actionDownloadBSL(self, bslsegments): + sys.stderr.write("Load new BSL into RAM...\n") + sys.stderr.flush() + self.programData(bslsegments, self.ACTION_PROGRAM) + sys.stderr.write("Verify new BSL...\n") + sys.stderr.flush() + self.programData(bslsegments, self.ACTION_VERIFY) #File to verify + + #Read startvector of bootstrap loader + #blkin = self.bslTxRx(self.BSL_RXBLK, 0x0300, 2) + #blkin = self.bslTxRx(self.BSL_RXBLK, 0x0220, 2) + blkin = self.bslTxRx(self.BSL_RXBLK, bslsegments[0].startaddress, 2) + startaddr = struct.unpack("%s" % (firmware[self.dev_id],fn)) + + fw=Memory(fn); + #fw.loadIhex(open(fn,"rb")); + + sys.stderr.write("Program ...\n") + sys.stderr.flush() + self.programData(fw, self.ACTION_PROGRAM | self.ACTION_VERIFY) + sys.stderr.write("%i bytes programmed.\n" % self.byteCtr) + sys.stderr.flush() + + def actionVerify(self): + """Verify programmed data""" + if self.data is not None: + sys.stderr.write("Verify ...\n") + sys.stderr.flush() + self.programData(self.data, self.ACTION_VERIFY) + else: + raise BSLException, "verify without data not possible" + + def actionReset(self): + """perform a reset, start user programm""" + sys.stderr.write("Reset device ...\n") + sys.stderr.flush() + self.bslReset(0) #only reset + + def actionRun(self, address=0x220): + """start program at specified address""" + sys.stderr.write("Load PC with 0x%04x ...\n" % address) + sys.stderr.flush() + self.bslTxRx(self.BSL_LOADPC, #Command: Load PC + address) #Address to load into PC + + #table with values from slaa089a.pdf + bauratetable = { + F1x: { + 9600:[0x8580, 0x0000], + 19200:[0x86e0, 0x0001], + 38400:[0x87e0, 0x0002], + }, + F2x: { + 9600:[0x8580, 0x0000], + 19200:[0x8B00, 0x0001], + 38400:[0x8C80, 0x0002], + }, + F4x: { + 9600:[0x9800, 0x0000], + 19200:[0xb000, 0x0001], + 38400:[0xc800, 0x0002], + }, + } + def actionChangeBaudrate(self, baudrate=9600): + """change baudrate. first the command is sent, then the comm + port is reprogrammed. only possible with newer MSP430-BSL versions. + (ROM >=1.6, downloadable >=1.5)""" + try: + baudconfigs = self.bauratetable[self.cpu] + except KeyError: + raise ValueError, "unknown CPU type %s, can't switch baudrate" % self.cpu + try: + a,l = baudconfigs[baudrate] + except KeyError: + raise ValueError, "baudrate not valid. valid values are %r" % baudconfigs.keys() + + sys.stderr.write("Changing baudrate to %d ...\n" % baudrate) + sys.stderr.flush() + self.bslTxRx(self.BSL_CHANGEBAUD, #Command: change baudrate + a, l) #args are coded in adr and len + self.serialport.flush() + time.sleep(0.010) #recomended delay + self.serialport.baudrate = baudrate + + def actionReadBSLVersion(self): + """informational output of BSL version number. + (newer MSP430-BSLs only)""" + ans = self.bslTxRx(self.BSL_TXVERSION, 0) #Command: receive version info + #the following values are in big endian style!!! + family_type, bsl_version = struct.unpack(">H8xH4x", ans[:-2]) #cut away checksum and extract data + print "Device Type: 0x%04x\nBSL version: 0x%04x\n" % (family_type, bsl_version) + + def actionWriteNewPassword(self): + passwd = self.getNewPassword() + #print passwd + self.picWritePassword(passwd) + + def actionRecoverPassword(self): + p = self.picRecoverPassword() + #print p + self.passwd = p + + def picRecoverPassword(self): + ''' recovers password from picrom''' + print 'recovering pic password', + p = '' + + for i in range(0,32): + c = self.readpicROM(i) + print c, + p = p + chr(c) + + print 'done' + return p + + def picWritePassword(self, password): + ''' writes a new password to picrom''' + print 'writing new password', + for i in range(0,32): + c = ord(password[i]) + print c, + self.writepicROM(i, c) + print 'done' + + +def usage(): + """print some help message""" + sys.stderr.write(""" +USAGE: %s [options] [file] +Version: %s + +If "-" is specified as file the data is read from the stdinput. +A file ending with ".txt" is considered to be in TIText format, +'.a43' and '.hex' as IntelHex and all other filenames are +considered as ELF files. + +General options: + -h, --help Show this help screen. + -c, --comport=port Specify the communication port to be used. + (Default is 0) + 0->COM1 / ttyS0 + 1->COM2 / ttyS1 + etc. + -P, --password=file Specify a file with the interrupt vectors that + are used as password. This can be any file that + has previously been used to program the device. + (e.g. -P INT_VECT.TXT). + -f, --framesize=num Max. number of data bytes within one transmitted + frame (16 to 240 in steps of 16) (e.g. -f 240). + -m, --erasecycles=num Number of mass erase cycles (default is 1). Some + old F149 devices need additional erase cycles. + On newer devices it is no longer needed. (e.g. for + an old F149: -m20) + -U, --unpatched Do not download the BSL patch, even when it is + needed. This is used when a program is downloaded + into RAM and executed from there (and where flash + programming is not needed.) + -D, --debug Increase level of debug messages. This won't be + very useful for the average user... + -I, --intelhex Force fileformat to IntelHex + -T, --titext Force fileformat to be TIText + -N, --notimeout Don't use timeout on serial port (use with care) + -B, --bsl=bsl.txt Load and use new BSL from the TI Text file + -S, --speed=baud Reconfigure speed, only possible with newer + MSP403-BSL versions (>1.5, read slaa089a.pdf for + details). If the --bsl option is not used, an + internal BSL replacement will be loaded. + Needs a target with at least 2kB RAM! + Possible values are 9600, 19200, 38400 + (default 9600) + -1, --f1x Specify CPU family, in case autodetect fails + -2, --f2x Specify CPU family, in case autodetect fails + -4, --f4x Specify CPU family, in case autodetect fails + --F1x and --f2x are only needed when the "change + baudrate" feature is used and the autodetect feature + fails. If the device ID that is uploaded is known, it + has precedence to the command line option. + --invert-reset Invert signal on RST pin (used for some BSL hardware) + --invert-test Invert signal on TEST/TCK pin (used for some BSL + hardware) + --swap-reset-test Swap the RST and TEST pins (used for some BSL hardware) + --telos-latch Special twiddle in BSL reset for Telos hardware + --telos-i2c DTR/RTS map via an I2C switch to TCK/RST in Telos Rev.B + --telos Implies options --invert-reset, --invert-test, + --swap-reset-test, and --telos-latch + --telosb Implies options --swap-reset-test, --telos-i2c, + --no-BSL-download, and --speed=38400 + --goodfet10 + --goodfet20 + --goodfet30 + --tmote Identical operation to --telosb + --z1 Bootstrap a Z1 + --no-BSL-download Do not download replacement BSL (disable automatic) + --force-BSL-download Download replacement BSL even if not needed (the one + in the device would have the required features) + --slow Add delays when operating the conrol pins. Useful if + the pins/circuit has high capacitance. + +Program Flow Specifiers: + -e, --masserase Mass Erase (clear all flash memory) + -E, --erasecheck Erase Check by file + -p, --program Program file + -v, --verify Verify by file + +The order of the above options matters! The table is ordered by normal +execution order. For the options "Epv" a file must be specified. +Program flow specifiers default to "pvr" if a file is given. +Don't forget to specify "e" or "eE" when programming flash! + +Data retreiving: + -u, --upload=addr Upload a datablock (see also: -s). + -s, --size=num Size of the data block do upload. (Default is 2) + -x, --hex Show a hexadecimal display of the uploaded data. + (Default) + -b, --bin Get binary uploaded data. This can be used + to redirect the output into a file. + +Do before exit: + -g, --go=address Start programm execution at specified address. + This implies option --wait. + -r, --reset Reset connected MSP430. Starts application. + This is a normal device reset and will start + the programm that is specified in the reset + vector. (see also -g) + -w, --wait Wait for before closing serial port. + +If it says "NAK received" it's probably because you specified no or a +wrong password. +""" % (sys.argv[0], VERSION)) + +#add some arguments to a function, but don't call it yet, instead return +#a wrapper object for later invocation +class curry: + """create a callable with some arguments specified in advance""" + def __init__(self, fun, *args, **kwargs): + self.fun = fun + self.pending = args[:] + self.kwargs = kwargs.copy() + + def __call__(self, *args, **kwargs): + if kwargs and self.kwargs: + kw = self.kwargs.copy() + kw.update(kwargs) + else: + kw = kwargs or self.kwargs + return apply(self.fun, self.pending + args, kw) + + def __repr__(self): + #first try if it a function + try: + return "curry(%s, %r, %r)" % (self.fun.func_name, self.pending, self.kwargs) + except AttributeError: + #fallback for callable classes + return "curry(%s, %r, %r)" % (self.fun, self.pending, self.kwargs) + +def hexify(line, bytes, width=16): + return '%04x %s%s %s' % ( + line, + ('%02x '*len(bytes)) % tuple(bytes), + ' '* (width-len(bytes)), + ('%c'*len(bytes)) % tuple(map(lambda x: (x>=32 and x<127) and x or ord('.'), bytes)) + ) + +#Main: +def main(itest=1): + global DEBUG + import getopt + filetype = None + filename = None + comPort = None #Default setting. + speed = None + unpatched = 0 + reset = 0 + wait = 0 #wait at the end + goaddr = None + bsl = BootStrapLoader() + toinit = [] + todo = [] + startaddr = None + size = 2 + hexoutput = 1 + notimeout = 0 + bslrepl = None + mayuseBSL = 1 + forceBSL = 0 + dumpivt = 0 + dumpinfo = 0 + + bsl.invertRST = 1 + bsl.invertTEST = itest + + if comPort is None and os.environ.get("GOODFET")!=None: + glob_list = glob.glob(os.environ.get("GOODFET")); + if len(glob_list) > 0: + comPort = glob_list[0]; + if comPort is None: + glob_list = glob.glob("/dev/tty.usbserial*"); + if len(glob_list) > 0: + comPort = glob_list[0]; + if comPort is None: + glob_list = glob.glob("/dev/ttyUSB*"); + if len(glob_list) > 0: + comPort = glob_list[0]; + + + sys.stderr.write("MSP430 Bootstrap Loader Version: %s\n" % VERSION) + + try: + opts, args = getopt.getopt(sys.argv[1:], + "hc:P:wf:m:neEpvrg:UDudsxbITNB:S:V14", + ["help", "comport=", "password=", "wait", "framesize=", + "erasecycles=", "masserase", "erasecheck", "program", + "verify", "reset", "go=", "unpatched", "debug", + "upload=", "download=", "size=", "hex", "bin", + "intelhex", "titext", "notimeout", "bsl=", "speed=", + "bslversion", "f1x", "f2x", "f4x", "invert-reset", "invert-test", + "swap-reset-test", "telos-latch", "telos-i2c", "telos", "telosb", + "tmote","no-BSL-download", "force-BSL-download", "slow", + "dumpivt", "dumpinfo", "fromweb", + "goodfet30", "goodfet20", "goodfet10", "z1", "mainerase" + ] + ) + except getopt.GetoptError: + # print help information and exit: + usage() + sys.exit(2) + + for o, a in opts: + if o in ("-h", "--help"): + usage() + sys.exit() + elif o in ("-c", "--comport"): + try: + comPort = int(a) #try to convert decimal + except ValueError: + comPort = a #take the string and let serial driver decide + elif o in ("-P", "--password"): + #extract password from file + bsl.passwd = Memory(a).getMemrange(0xffe0, 0xffff) + elif o in ("-w", "--wait"): + wait = 1 + elif o in ("--dumpivt"): + dumpivt=1; + elif o in ("--dumpinfo"): + dumpinfo=1; + elif o in ("-f", "--framesize"): + try: + maxData = int(a) #try to convert decimal + except ValueError: + sys.stderr.write("framesize must be a valid number\n") + sys.exit(2) + #Make sure that conditions for maxData are met: + #( >= 16 and == n*16 and <= MAX_DATA_BYTES!) + if maxData > BootStrapLoader.MAX_DATA_BYTES: + maxData = BootStrapLoader.MAX_DATA_BYTES + elif maxData < 16: + maxData = 16 + bsl.maxData = maxData - (maxData % 16) + sys.stderr.write( "Max. number of data bytes within one frame set to %i.\n" % maxData) + elif o in ("-m", "--erasecycles"): + try: + meraseCycles = int(a) #try to convert decimal + except ValueError: + sys.stderr.write("erasecycles must be a valid number\n") + sys.exit(2) + #sanity check of value + if meraseCycles < 1: + sys.stderr.write("erasecycles must be a positive number\n") + sys.exit(2) + if meraseCycles > 20: + sys.stderr.write("warning: erasecycles set to a large number (>20): %d\n" % meraseCycles) + sys.stderr.write( "Number of mass erase cycles set to %i.\n" % meraseCycles) + bsl.meraseCycles = meraseCycles + elif o in ("-e", "--masserase"): + toinit.append(bsl.actionMassErase) #Erase Flash + #todo.append(bsl.actionMainErase) #Erase Flash + elif o in ("-n", "--mainerase"): + todo.append(bsl.actionMainErase) #Erase Flash + #toinit.append(bsl.actionMassErase) #Erase Flash + elif o in ("-E", "--erasecheck"): + toinit.append(bsl.actionEraseCheck) #Erase Check (by file) + elif o in ("-p", "--program"): + #pass + todo.append(bsl.actionProgram) #Program file + if bsl.z1: + #todo.append(bsl.actionUnlockInfo) + #todo.append(bsl.writeCalibTables) + #todo.append(bsl.writeCalibTables2) + + #todo.append(bsl.actionWriteNewPassword) + pass + elif o in ("--fromweb"): + toinit.append(bsl.actionMassErase) #Erase Flash + todo.append(bsl.actionFromweb) #Program GoodFET code + elif o in ("-v", "--verify"): + todo.append(bsl.actionVerify) #Verify file + elif o in ("-r", "--reset"): + reset = 1 + elif o in ("-g", "--go"): + try: + goaddr = int(a) #try to convert decimal + except ValueError: + try: + goaddr = int(a[2:],16) #try to convert hex + except ValueError: + sys.stderr.write("go address must be a valid number\n") + sys.exit(2) + wait = 1 + elif o in ("-U", "--unpatched"): + unpatched = 1 + elif o in ("-D", "--debug"): + DEBUG = DEBUG + 1 + elif o in ("-u", "--upload"): + try: + startaddr = int(a) #try to convert decimal + except ValueError: + try: + startaddr = int(a,16) #try to convert hex + except ValueError: + sys.stderr.write("upload address must be a valid number\n") + sys.exit(2) + elif o in ("-s", "--size"): + try: + size = int(a) + except ValueError: + try: + size = int(a,16) + except ValueError: + sys.stderr.write("size must be a valid number\n") + sys.exit(2) + elif o in ("-x", "--hex"): + hexoutput = 1 + elif o in ("-b", "--bin"): + hexoutput = 0 + elif o in ("-I", "--intelhex"): + filetype = 0 + elif o in ("-T", "--titext"): + filetype = 1 + elif o in ("-N", "--notimeout"): + notimeout = 1 + elif o in ("-B", "--bsl"): + bslrepl = Memory() #File to program + bslrepl.loadFile(a) + elif o in ("-V", "--bslversion"): + todo.append(bsl.actionReadBSLVersion) #load replacement BSL as first item + elif o in ("-S", "--speed"): + try: + speed = int(a) #try to convert decimal + except ValueError: + sys.stderr.write("speed must be decimal number\n") + sys.exit(2) + elif o in ("-1", "--f1x"): + bsl.cpu = F1x + elif o in ("-2", "--f2x"): + bsl.cpu = F2x + elif o in ("-4", "--f4x"): + bsl.cpu = F4x + elif o in ("--invert-reset", ): + bsl.invertRST = 1 + elif o in ("--invert-test", ): + bsl.invertTEST = 1 + elif o in ("--swap-reset-test", ): + bsl.swapRSTTEST = 1 + elif o in ("--telos-latch", ): + bsl.telosLatch = 1 + elif o in ("--telos-i2c", ): + bsl.telosI2C = 1 + elif o in ("--telos", ): + bsl.invertRST = 1 + bsl.invertTEST = 1 + bsl.swapRSTTEST = 1 + bsl.telosLatch = 1 + elif o in ("--goodfet10", ): + bsl.invertRST = 1 + bsl.invertTEST = 1 + elif o in ("--goodfet20", ): + bsl.invertRST = 1 + bsl.invertTEST = 1 + elif o in ("--goodfet30", ): + bsl.invertRST = 1 + bsl.invertTEST = 0 + elif o in ("--telosb", ): + bsl.swapRSTTEST = 1 + bsl.telosI2C = 1 + mayuseBSL = 0 + speed = 38400 + elif o in ("--tmote", ): + bsl.swapRSTTEST = 1 + bsl.telosI2C = 1 + mayuseBSL = 0 + speed = 38400 + elif o in ("--z1", ): + bsl.z1 = 1 + speed = 38400 + #toinit.append(bsl.actionRecoverPassword) + elif o in ("--no-BSL-download", ): + mayuseBSL = 0 + elif o in ("--force-BSL-download", ): + forceBSL = 1 + elif o in ("--slow", ): + bsl.slowmode = 1 + + if len(args) == 0: + sys.stderr.write("Use -h for help\n"); + sys.stderr.write("Use --fromweb to upgrade a GoodFET.\n") + elif len(args) == 1: #a filename is given + if not todo: #if there are no actions yet + todo.extend([ #add some useful actions... + bsl.actionProgram, + bsl.actionVerify, + ]) + filename = args[0] + else: #number of args is wrong + usage() + sys.exit(2) + + if DEBUG: #debug infos + sys.stderr.write("Debug level set to %d\n" % DEBUG) + sys.stderr.write("Python version: %s\n" % sys.version) + + #sanity check of options + if notimeout and goaddr is not None and startaddr is not None: + sys.stderr.write("Option --notimeout can not be used together with both --upload and --go\n") + sys.exit(1) + + if notimeout: + sys.stderr.write("Warning: option --notimeout can cause improper function in some cases!\n") + bsl.timeout = 0 + + if goaddr and reset: + sys.stderr.write("Warning: option --reset ignored as --go is specified!\n") + reset = 0 + + if startaddr and reset: + sys.stderr.write("Warning: option --reset ignored as --upload is specified!\n") + reset = 0 + + sys.stderr.flush() + + #prepare data to download + bsl.data = Memory() #prepare downloaded data + if filetype is not None: #if the filetype is given... + if filename is None: + raise ValueError("no filename but filetype specified") + if filename == '-': #get data from stdin + file = sys.stdin + else: + file = open(filename, "rb") #or from a file + if filetype == 0: #select load function + bsl.data.loadIHex(file) #intel hex + elif filetype == 1: + bsl.data.loadTIText(file) #TI's format + else: + raise ValueError("illegal filetype specified") + else: #no filetype given... + if filename == '-': #for stdin: + bsl.data.loadIHex(sys.stdin) #assume intel hex + elif filename: + bsl.data.loadFile(filename) #autodetect otherwise + + if DEBUG > 3: sys.stderr.write("File: %r" % filename) + + bsl.comInit(comPort) #init port + + #initialization list + if toinit: #erase and erase check + if DEBUG: sys.stderr.write("Preparing device ...\n") + #bsl.actionStartBSL(usepatch=0, adjsp=0) #no workarounds needed + #if speed: bsl.actionChangeBaudrate(speed) #change baud rate as fast as possible + for f in toinit: f() + + if todo or goaddr or startaddr: + if DEBUG: sys.stderr.write("Actions ...\n") + #connect to the BSL + bsl.actionStartBSL( + usepatch=not unpatched, + replacementBSL=bslrepl, + forceBSL=forceBSL, + mayuseBSL=mayuseBSL, + speed=speed, + ) + + #work list + if todo: + if DEBUG > 0: #debug + #show a nice list of sheduled actions + sys.stderr.write("TODO list:\n") + for f in todo: + try: + sys.stderr.write(" %s\n" % f.func_name) + except AttributeError: + sys.stderr.write(" %r\n" % f) + for f in todo: f() #work through todo list + + if reset: #reset device first if desired + bsl.actionReset() + if dumpivt: + bsl.txPasswd(); #default pass + data=bsl.uploadData(0xc00,1024); + hex=""; + for c in data: + hex+=("%02x "%ord(c)); + print hex; + if dumpinfo: + bsl.txPasswd(); #default pass + data=bsl.uploadData(0x1000,256); + hex="@1000\n"; + for c in data: + hex+=("%02x "%ord(c)); + hex+="\nq\n"; + print hex; + + if goaddr is not None: #start user programm at specified address + bsl.actionRun(goaddr) #load PC and execute + + #upload datablock and output + if startaddr is not None: + if goaddr: #if a program was started... + #don't restart BSL but wait for the device to enter it itself + sys.stderr.write("Waiting for device to reconnect for upload: ") + sys.stderr.flush() + bsl.txPasswd(bsl.passwd, wait=1) #synchronize, try forever... + data = bsl.uploadData(startaddr, size) #upload data + else: + data = bsl.uploadData(startaddr, size) #upload data + if hexoutput: #depending on output format + m = 0 + while m < len(data): #print a hex display + print hexify(startaddr+m, map(ord,data[m:m+16])) + m = m + 16 + else: + sys.stdout.write(data) #binary output w/o newline! + wait = 0 #wait makes no sense as after the upload the device is still in BSL + + if wait: #wait at the end if desired + sys.stderr.write("Press ...\n") #display a prompt + sys.stderr.flush() + raw_input() #wait for newline + + bsl.comDone() #Release serial communication port + +if __name__ == '__main__': + try: + main(1) + except SystemExit: + raise #let pass exit() calls + except KeyboardInterrupt: + if DEBUG: raise #show full trace in debug mode + sys.stderr.write("user abort.\n") #short messy in user mode + sys.exit(1) #set errorlevel for script usage + except Exception, msg: #every Exception is caught and displayed + if DEBUG: raise #show full trace in debug mode + #sys.stderr.write("\nAn error occoured:\n%s\n" % msg) #short messy in user mode + #sys.exit(1) #set errorlevel for script usage + main(0);