├── .clang-format ├── .cproject ├── .gitignore ├── .project ├── Build └── Makefile ├── README.md └── Source ├── PDM_IDs.h ├── app.zpscfg ├── app_device_temperature.c ├── app_device_temperature.h ├── app_main.c ├── app_main.h ├── app_reporting.c ├── app_reporting.h ├── app_router_node.c ├── app_router_node.h ├── app_serial_commands.c ├── app_serial_commands.h ├── app_start.c ├── app_zcl_task.c ├── app_zcl_task.h ├── bdb_options.h ├── irq_JN516x.S ├── uart.c ├── uart.h └── zcl_options.h /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | # We'll use defaults from the LLVM style, but with 4 columns indentation. 3 | BasedOnStyle: LLVM 4 | AlignConsecutiveBitFields: true 5 | AlignConsecutiveMacros: true 6 | AllowAllArgumentsOnNextLine: false 7 | AllowShortFunctionsOnASingleLine: None 8 | BinPackArguments: false 9 | BinPackParameters: false 10 | BreakBeforeBraces: Stroustrup 11 | ColumnLimit: 120 12 | IndentWidth: 4 -------------------------------------------------------------------------------- /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Build/*.d 2 | Build/*.o 3 | Build/*.bin 4 | Build/*.elf 5 | Build/*.map 6 | Source/pdum_apdu.S 7 | Source/pdum_gen.* 8 | Source/zps_gen.* 9 | 10 | # MacOS indexing file 11 | .DS_Store -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lumi-Router-JN5169 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | ?name? 14 | 15 | 16 | 17 | org.eclipse.cdt.make.core.append_environment 18 | true 19 | 20 | 21 | org.eclipse.cdt.make.core.autoBuildTarget 22 | all 23 | 24 | 25 | org.eclipse.cdt.make.core.buildArguments 26 | 27 | 28 | 29 | org.eclipse.cdt.make.core.buildCommand 30 | make 31 | 32 | 33 | org.eclipse.cdt.make.core.buildLocation 34 | ${ProjDirPath}/Build 35 | 36 | 37 | org.eclipse.cdt.make.core.cleanBuildTarget 38 | clean 39 | 40 | 41 | org.eclipse.cdt.make.core.contents 42 | org.eclipse.cdt.make.core.activeConfigSettings 43 | 44 | 45 | org.eclipse.cdt.make.core.enableAutoBuild 46 | false 47 | 48 | 49 | org.eclipse.cdt.make.core.enableCleanBuild 50 | true 51 | 52 | 53 | org.eclipse.cdt.make.core.enableFullBuild 54 | true 55 | 56 | 57 | org.eclipse.cdt.make.core.fullBuildTarget 58 | all 59 | 60 | 61 | org.eclipse.cdt.make.core.stopOnError 62 | true 63 | 64 | 65 | org.eclipse.cdt.make.core.useDefaultBuildCmd 66 | false 67 | 68 | 69 | 70 | 71 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 72 | 73 | 74 | 75 | 76 | 77 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 78 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 79 | org.eclipse.cdt.core.cnature 80 | 81 | 82 | 83 | Build 84 | 2 85 | PROJECT_LOC/Build 86 | 87 | 88 | Source 89 | 2 90 | PROJECT_LOC/Source 91 | 92 | 93 | JN-SW-4170 94 | 2 95 | ECLIPSE_HOME/sdk/JN-SW-4170 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /Build/Makefile: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # 3 | # MODULE: Makefile 4 | # 5 | # DESCRIPTION: Makefile for the Lumi Router 6 | # 7 | ############################################################################### 8 | # 9 | # This software is owned by NXP B.V. and/or its supplier and is protected 10 | # under applicable copyright laws. All rights are reserved. We grant You, 11 | # and any third parties, a license to use this software solely and 12 | # exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 13 | # You, and any third parties must reproduce the copyright and warranty notice 14 | # and any other legend of ownership on each copy or partial copy of the 15 | # software. 16 | # 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | # POSSIBILITY OF SUCH DAMAGE. 28 | # 29 | # Copyright NXP B.V. 2017. All rights reserved 30 | # 31 | ############################################################################### 32 | # Application target name 33 | 34 | TARGET = LumiRouter 35 | 36 | ############################################################################### 37 | # Application build date 38 | 39 | BUILD_DATE = 20210320 40 | CFLAGS += -DBUILD_DATE_STRING=\"$(BUILD_DATE)\" 41 | 42 | ############################################################################### 43 | # Network settings 44 | 45 | # Channel (0 for default channels) 46 | SINGLE_CHANNEL ?= 0 47 | CFLAGS += -DSINGLE_CHANNEL=$(SINGLE_CHANNEL) 48 | 49 | # Enabling High Power Mode on the Modules 50 | # to support the zigbee module installed in the Aqara ZHWG11LM device 51 | ENABLING_HIGH_POWER_MODE ?= 1 52 | ifeq ($(ENABLING_HIGH_POWER_MODE), 1) 53 | CFLAGS += -DENABLING_HIGH_POWER_MODE 54 | endif 55 | 56 | ############################################################################### 57 | # Target chip is the JN5169 58 | 59 | JENNIC_CHIP = JN5169 60 | JENNIC_CHIP_FAMILY = JN516x 61 | 62 | ############################################################################### 63 | # Select the network stack (e.g. MAC, ZBPro, ZCL) 64 | 65 | JENNIC_STACK = ZCL 66 | 67 | ############################################################################### 68 | # Default SDK is the IEEE802.15.4 SDK 69 | 70 | JENNIC_SDK = JN-SW-4170 71 | 72 | ############################################################################### 73 | # Default MAC is the IEEE802.15.4 Mini MAC 74 | 75 | JENNIC_MAC = MiniMacShim 76 | 77 | ############################################################################### 78 | # ZBPro Stack specific options 79 | 80 | ZBPRO_DEVICE_TYPE = ZCR 81 | PDM_BUILD_TYPE =_EEPROM 82 | 83 | STACK_SIZE = 5000 84 | MINIMUM_HEAP_SIZE = 2000 85 | 86 | ZNCLKCMD = AppBuildZBPro.ld 87 | ENDIAN = BIG_ENDIAN 88 | 89 | ############################################################################### 90 | # Debug options 91 | 92 | DEBUG ?= NONE 93 | 94 | ifeq ($(DEBUG), UART1) 95 | $(info Building with debug UART1 ...) 96 | TRACE = 1 97 | CFLAGS += -DUART_DEBUGGING 98 | CFLAGS += -DDBG_ENABLE 99 | CLFAGS += -DDEBUG_BDB 100 | CFLAGS += -DDEBUG_APP 101 | CFLAGS += -DDEBUG_REPORT 102 | CFLAGS += -DDEBUG_ZCL 103 | CFLAGS += -DDEBUG_UART 104 | CFLAGS += -DDEBUG_SERIAL 105 | CFLAGS += -DDEBUG_DEVICE_TEMPERATURE 106 | endif 107 | 108 | ############################################################################### 109 | # BDB features – Enable as required 110 | 111 | BDB_SUPPORT_NWK_STEERING ?= 1 112 | BDB_SUPPORT_FIND_AND_BIND_TARGET ?= 1 113 | 114 | ############################################################################### 115 | # Generate build file name 116 | 117 | ifneq ($(SINGLE_CHANNEL), 0) 118 | TARGET_FEATURES := $(TARGET_FEATURES)_CH$(SINGLE_CHANNEL) 119 | endif 120 | 121 | ifeq ($(DEBUG), UART1) 122 | TARGET_FEATURES := $(TARGET_FEATURES)_DEBUG 123 | endif 124 | 125 | GENERATED_FILE_NAME = $(TARGET)$(TARGET_FEATURES)_$(BUILD_DATE) 126 | 127 | ############################################################################### 128 | # Path definitions 129 | 130 | # Use if application directory contains multiple targets 131 | SDK_BASE_DIR = $(abspath ../../../sdk/$(JENNIC_SDK)) 132 | APP_BASE = $(abspath ..) 133 | APP_BLD_DIR = $(APP_BASE)/Build 134 | APP_SRC_DIR = $(APP_BASE)/Source 135 | UTIL_SRC_DIR = $(COMPONENTS_BASE_DIR)/ZigbeeCommon/Source 136 | HW_SRC_DIR = $(COMPONENTS_BASE_DIR)/HardwareAPI/Source 137 | 138 | ############################################################################### 139 | # Application Source files 140 | 141 | # Note: Path to source file is found using vpath below, so only .c filename is required 142 | APPSRC = irq_JN516x.S 143 | APPSRC += portasm_JN516x.S 144 | APPSRC += port_JN516x.c 145 | APPSRC += pdum_gen.c 146 | APPSRC += pdum_apdu.S 147 | APPSRC += zps_gen.c 148 | APPSRC += app_start.c 149 | APPSRC += app_main.c 150 | APPSRC += app_router_node.c 151 | APPSRC += app_zcl_task.c 152 | APPSRC += app_reporting.c 153 | APPSRC += app_serial_commands.c 154 | APPSRC += app_device_temperature.c 155 | APPSRC += uart.c 156 | 157 | APP_ZPSCFG = app.zpscfg 158 | 159 | ############################################################################### 160 | # Standard Application header search paths 161 | 162 | INCFLAGS += -I$(APP_SRC_DIR) 163 | INCFLAGS += -I$(APP_SRC_DIR)/.. 164 | 165 | # Application specific include files 166 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/ZCL/Include 167 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/ZCIF/Include 168 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/Xcv/Include/ 169 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/Recal/Include/ 170 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/MicroSpecific/Include 171 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/ZigbeeCommon/Include 172 | INCFLAGS += -I$(COMPONENTS_BASE_DIR)/HardwareAPI/Include 173 | 174 | ############################################################################### 175 | # Optional stack features to pull relevant libraries into the build. 176 | 177 | OPTIONAL_STACK_FEATURES = $(shell $(ZPSCONFIG) -n $(TARGET) -f $(APP_SRC_DIR)/$(APP_ZPSCFG) -y ) 178 | 179 | ############################################################################### 180 | # Configure for the selected chip or chip family 181 | 182 | include $(SDK_BASE_DIR)/Chip/Common/Build/config.mk 183 | include $(SDK_BASE_DIR)/Stack/Common/Build/config.mk 184 | include $(SDK_BASE_DIR)/Components/BDB/Build/config.mk 185 | 186 | ############################################################################### 187 | 188 | TEMP = $(APPSRC:.c=.o) 189 | APPOBJS_TMP = $(TEMP:.S=.o) 190 | APPOBJS := $(addprefix $(APP_BLD_DIR)/,$(APPOBJS_TMP)) 191 | 192 | ############################################################################### 193 | # Application dynamic dependencies 194 | 195 | APPDEPS_TMP = $(APPOBJS_TMP:.o=.d) 196 | APPDEPS := $(addprefix $(APP_BLD_DIR)/,$(APPDEPS_TMP)) 197 | 198 | ############################################################################### 199 | # Linker 200 | 201 | # Add application libraries before chip specific libraries to linker so 202 | # symbols are resolved correctly (i.e. ordering is significant for GCC) 203 | 204 | APPLDLIBS := $(foreach lib,$(APPLIBS),$(if $(wildcard $(addprefix $(COMPONENTS_BASE_DIR)/Library/lib,$(addsuffix _$(JENNIC_CHIP).a,$(lib)))),$(addsuffix _$(JENNIC_CHIP),$(lib)),$(addsuffix _$(JENNIC_CHIP_FAMILY),$(lib)))) 205 | LDLIBS := $(APPLDLIBS) $(LDLIBS) 206 | LDLIBS += JPT_$(JENNIC_CHIP) 207 | 208 | ############################################################################### 209 | # Dependency rules 210 | 211 | .PHONY: all clean 212 | # Path to directories containing application source 213 | vpath % $(APP_SRC_DIR):$(ZCL_SRC_DIRS):$(ZCL_SRC):$(BDB_SRC_DIR):$(UTIL_SRC_DIR):$(HW_SRC_DIR) 214 | 215 | all: $(APP_BLD_DIR)/$(GENERATED_FILE_NAME).bin 216 | 217 | -include $(APPDEPS) 218 | $(APP_BLD_DIR)/%.d: 219 | rm -f $*.o 220 | 221 | $(APP_SRC_DIR)/pdum_gen.c $(APP_SRC_DIR)/pdum_gen.h: $(APP_SRC_DIR)/$(APP_ZPSCFG) $(PDUMCONFIG) 222 | $(info Configuring the PDUM ...) 223 | $(PDUMCONFIG) -z $(TARGET) -f $< -o $(APP_SRC_DIR) 224 | 225 | $(APP_SRC_DIR)/zps_gen.c $(APP_SRC_DIR)/zps_gen.h: $(APP_SRC_DIR)/$(APP_ZPSCFG) $(ZPSCONFIG) 226 | $(info Configuring the Zigbee Protocol Stack ...) 227 | $(ZPSCONFIG) -n $(TARGET) -t $(JENNIC_CHIP) -l $(ZPS_NWK_LIB) -a $(ZPS_APL_LIB) -c $(TOOL_COMMON_BASE_DIR)/$(TOOLCHAIN_PATH) -f $< -o $(APP_SRC_DIR) 228 | 229 | $(APP_BLD_DIR)/%.o: %.S 230 | $(info Assembling $< ...) 231 | $(CC) -c -o $(subst Source,Build,$@) $(CFLAGS) $(INCFLAGS) $< -MD -MF $(APP_BLD_DIR)/$*.d -MP 232 | @echo 233 | 234 | $(APP_BLD_DIR)/%.o: %.c 235 | $(info Compiling $< ...) 236 | $(CC) -c -o $(subst Source,Build,$@) $(CFLAGS) $(INCFLAGS) $< -MD -MF $(APP_BLD_DIR)/$*.d -MP 237 | @echo 238 | 239 | $(APP_BLD_DIR)/$(GENERATED_FILE_NAME).elf: $(APPOBJS) $(addsuffix.a,$(addprefix $(COMPONENTS_BASE_DIR)/Library/lib,$(APPLDLIBS))) 240 | $(info Linking $@ ...) 241 | $(CC) -Wl,--gc-sections -Wl,-u_AppColdStart -Wl,-u_AppWarmStart $(LDFLAGS) -L $(SDK_BASE_DIR)/Stack/ZCL/Build/ -T$(ZNCLKCMD) -o $@ -Wl,--start-group $(APPOBJS) $(addprefix -l,$(LDLIBS)) -lm -Wl,--end-group -Wl,-Map,$(GENERATED_FILE_NAME).map 242 | $(SIZE) $@ 243 | 244 | $(APP_BLD_DIR)/$(GENERATED_FILE_NAME).bin: $(APP_BLD_DIR)/$(GENERATED_FILE_NAME).elf 245 | $(info Generating binary ...) 246 | $(OBJCOPY) -j .version -j .bir -j .flashheader -j .vsr_table -j .vsr_handlers -j .rodata -j .text -j .data -j .bss -j .heap -j .stack -S -O binary $< $@ 247 | 248 | ############################################################################### 249 | 250 | clean: 251 | rm -f $(APPOBJS) $(APPDEPS) $(TARGET)*_$(BUILD_DATE).bin $(TARGET)*_$(BUILD_DATE).elf $(TARGET)*_$(BUILD_DATE).map 252 | rm -f $(APP_SRC_DIR)/pdum_gen.* $(APP_SRC_DIR)/zps_gen.* $(APP_SRC_DIR)/pdum_apdu.S 253 | 254 | ############################################################################### 255 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lumi Router 2 | 3 | This firmware is a replacement for the original firmware for the __Zigbee__ chip JN5169 on __Xiaomi DGNWG05LM__ and __Aqara ZHWG11LM__ gateways which allows to use the gateway as a router (repeater-like) in any Zigbee network instead of the stock coordinator firmware for the propriate Xiaomi MiHome Network. 4 | 5 | --- 6 | 7 | This instruction assumes that an alternative __OpenWRT__ firmware is already installed on the gateway. If you have not done this, use the following instruction [https://openlumi.github.io](https://openlumi.github.io) 8 | 9 | ## Firmware 10 | 11 | 1. Connect to device via SSH. 12 | 2. Issue the following commands in the command line. 13 | 14 | ```shell 15 | wget https://github.com/igo-r/Lumi-Router-JN5169/releases/latest/download/LumiRouter_20210320.bin -O /tmp/LumiRouter.bin 16 | jnflash /tmp/LumiRouter.bin 17 | ``` 18 | 19 | ## Pairing 20 | 21 | Issue the following command in the command line. 22 | 23 | ```shell 24 | jntool erase_pdm 25 | ``` 26 | 27 | After this the device will automatically join. 28 | 29 | ## Restart 30 | 31 | Issue the following command in the command line. 32 | 33 | ```shell 34 | jntool soft_reset 35 | ``` 36 | -------------------------------------------------------------------------------- /Source/PDM_IDs.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: PDM_IDs.h 6 | * 7 | * DESCRIPTION: Persistent Data Manager ID definitions 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2017. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef PDMIDS_H 36 | #define PDMIDS_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | /****************************************************************************/ 43 | /*** Macro Definitions ***/ 44 | /****************************************************************************/ 45 | 46 | #define PDM_ID_APP_ROUTER 0x1 47 | #define PDM_ID_APP_REPORTS 0xa 48 | 49 | /****************************************************************************/ 50 | /*** Type Definitions ***/ 51 | /****************************************************************************/ 52 | 53 | /****************************************************************************/ 54 | /*** Exported Variables ***/ 55 | /****************************************************************************/ 56 | 57 | /****************************************************************************/ 58 | /*** Exported Functions ***/ 59 | /****************************************************************************/ 60 | 61 | /****************************************************************************/ 62 | /*** END OF FILE ***/ 63 | /****************************************************************************/ 64 | 65 | #endif /* PDMIDS_H */ 66 | -------------------------------------------------------------------------------- /Source/app.zpscfg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | -------------------------------------------------------------------------------- /Source/app_device_temperature.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_device_temperature.c 6 | * 7 | * DESCRIPTION: Set of functions/task for read device temperature 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | 41 | /* Application */ 42 | #include "app_device_temperature.h" 43 | #include "app_main.h" 44 | #include "app_zcl_task.h" 45 | 46 | /* SDK JN-SW-4170 */ 47 | #include "AppHardwareApi.h" 48 | #include "ZTimer.h" 49 | #include "dbg.h" 50 | 51 | /****************************************************************************/ 52 | /*** Macro Definitions ***/ 53 | /****************************************************************************/ 54 | 55 | #ifdef DEBUG_DEVICE_TEMPERATURE 56 | #define TRACE_DEVICE_TEMPERATURE TRUE 57 | #else 58 | #define TRACE_DEVICE_TEMPERATURE FALSE 59 | #endif 60 | 61 | #define DEVICE_TEMPERATURE_UPDATE_TIME ZTIMER_TIME_SEC(10) 62 | 63 | /****************************************************************************/ 64 | /*** Type Definitions ***/ 65 | /****************************************************************************/ 66 | 67 | /****************************************************************************/ 68 | /*** Local Function Prototypes ***/ 69 | /****************************************************************************/ 70 | 71 | PRIVATE void APP_vDeviceTemperatureUpdate(void); 72 | PRIVATE int16 APP_i16GetDeviceTemperature(void); 73 | PRIVATE int16 APP_i16ConvertChipTemp(uint16 u16AdcValue); 74 | 75 | /****************************************************************************/ 76 | /*** Exported Variables ***/ 77 | /****************************************************************************/ 78 | 79 | /****************************************************************************/ 80 | /*** Local Variables ***/ 81 | /****************************************************************************/ 82 | 83 | /****************************************************************************/ 84 | /*** Exported Functions ***/ 85 | /****************************************************************************/ 86 | 87 | /**************************************************************************** 88 | * 89 | * NAME: APP_vDeviceTemperatureInit 90 | * 91 | * DESCRIPTION: 92 | * Init Device Temperature 93 | * 94 | ****************************************************************************/ 95 | PUBLIC void APP_vDeviceTemperatureInit(void) 96 | { 97 | vAHI_ApConfigure(E_AHI_AP_REGULATOR_ENABLE, 98 | E_AHI_AP_INT_DISABLE, 99 | E_AHI_AP_SAMPLE_8, 100 | E_AHI_AP_CLOCKDIV_500KHZ, 101 | E_AHI_AP_INTREF); 102 | 103 | while (!bAHI_APRegulatorEnabled()) 104 | ; 105 | 106 | APP_vDeviceTemperatureUpdate(); 107 | 108 | DBG_vPrintf(TRACE_DEVICE_TEMPERATURE, "APP: Init Device Temperature\n"); 109 | 110 | /* Start the Device Temperature timer */ 111 | ZTIMER_eStart(u8TimerDeviceTemperature, DEVICE_TEMPERATURE_UPDATE_TIME); 112 | } 113 | 114 | /**************************************************************************** 115 | * 116 | * NAME: APP_cbTimerDeviceTemperatureUpdate 117 | * 118 | * DESCRIPTION: 119 | * CallBack For Device Temperature Update timer 120 | * 121 | ****************************************************************************/ 122 | PUBLIC void APP_cbTimerDeviceTemperatureUpdate(void *pvParam) 123 | { 124 | APP_vDeviceTemperatureUpdate(); 125 | ZTIMER_eStart(u8TimerDeviceTemperature, DEVICE_TEMPERATURE_UPDATE_TIME); 126 | } 127 | 128 | /****************************************************************************/ 129 | /*** Local Functions ***/ 130 | /****************************************************************************/ 131 | 132 | /**************************************************************************** 133 | * 134 | * NAME: APP_vDeviceTemperatureUpdate 135 | * 136 | * DESCRIPTION: 137 | * Device Temperature update 138 | * 139 | ****************************************************************************/ 140 | PRIVATE void APP_vDeviceTemperatureUpdate(void) 141 | { 142 | int16 i16DeviceTemperature = APP_i16GetDeviceTemperature(); 143 | 144 | DBG_vPrintf(TRACE_DEVICE_TEMPERATURE, "APP: Temp = %d C\n", i16DeviceTemperature); 145 | 146 | sLumiRouter.sDeviceTemperatureConfigurationServerCluster.i16CurrentTemperature = i16DeviceTemperature; 147 | } 148 | 149 | /**************************************************************************** 150 | * 151 | * NAME: APP_i16GetDeviceTemperature 152 | * 153 | * DESCRIPTION: 154 | * Read Device Temperature 155 | * 156 | * RETURNS: 157 | * Device Temperature 158 | * 159 | ****************************************************************************/ 160 | PRIVATE int16 APP_i16GetDeviceTemperature(void) 161 | { 162 | uint16 u16AdcTempSensor; 163 | 164 | vAHI_AdcEnable(E_AHI_ADC_SINGLE_SHOT, E_AHI_AP_INPUT_RANGE_2, E_AHI_ADC_SRC_TEMP); 165 | vAHI_AdcStartSample(); 166 | 167 | while (bAHI_AdcPoll()) 168 | ; 169 | 170 | u16AdcTempSensor = u16AHI_AdcRead(); 171 | 172 | return APP_i16ConvertChipTemp(u16AdcTempSensor); 173 | } 174 | 175 | /**************************************************************************** 176 | * 177 | * NAME: APP_i16ConvertChipTemp 178 | * 179 | * DESCRIPTION: 180 | * Helper Function to convert 10bit ADC reading to degrees C 181 | * Formula: DegC = Typical DegC - ((Reading12 - Typ12) * ScaleFactor) 182 | * Where C = 25 and temps sensor output 730mv at 25C (from datasheet) 183 | * As we use 2Vref and 10bit adc this gives (730/2400)*4096 [=Typ12 =1210] 184 | * Scale factor is half the 0.706 data-sheet resolution DegC/LSB (2Vref) 185 | * 186 | * PARAMETERS: Name Usage 187 | * uint16 u16AdcValue Adc Temperature Value 188 | * 189 | * RETURNS: 190 | * Chip Temperature in DegC 191 | * 192 | ****************************************************************************/ 193 | PRIVATE int16 APP_i16ConvertChipTemp(uint16 u16AdcValue) 194 | { 195 | return (int16)((int32)25 - ((((int32)(u16AdcValue * 4) - (int32)1210) * (int32)353) / (int32)1000)); 196 | } 197 | 198 | /****************************************************************************/ 199 | /*** END OF FILE ***/ 200 | /****************************************************************************/ 201 | -------------------------------------------------------------------------------- /Source/app_device_temperature.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_device_temperature.h 6 | * 7 | * DESCRIPTION: Set of functions/task for read device temperature 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef APP_DEVICE_TEMPERATURE_H 36 | #define APP_DEVICE_TEMPERATURE_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /****************************************************************************/ 45 | /*** Macro Definitions ***/ 46 | /****************************************************************************/ 47 | 48 | /****************************************************************************/ 49 | /*** Type Definitions ***/ 50 | /****************************************************************************/ 51 | 52 | /****************************************************************************/ 53 | /*** External Variables ***/ 54 | /****************************************************************************/ 55 | 56 | /****************************************************************************/ 57 | /*** Exported Functions ***/ 58 | /****************************************************************************/ 59 | 60 | PUBLIC void APP_vDeviceTemperatureInit(void); 61 | PUBLIC void APP_cbTimerDeviceTemperatureUpdate(void *pvParam); 62 | 63 | /****************************************************************************/ 64 | /*** END OF FILE ***/ 65 | /****************************************************************************/ 66 | 67 | #endif /* APP_DEVICE_TEMPERATURE_H */ 68 | -------------------------------------------------------------------------------- /Source/app_main.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_main.c 6 | * 7 | * DESCRIPTION: Application main file 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2017. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | 41 | /* Application */ 42 | #include "app_device_temperature.h" 43 | #include "app_main.h" 44 | #include "app_router_node.h" 45 | #include "app_serial_commands.h" 46 | #include "app_zcl_task.h" 47 | 48 | /* SDK JN-SW-4170 */ 49 | #include "AppHardwareApi.h" 50 | #include "ZQueue.h" 51 | #include "ZTimer.h" 52 | #include "bdb_api.h" 53 | #include "mac_vs_sap.h" 54 | #include "portmacro.h" 55 | #include "zps_apl_af.h" 56 | 57 | /****************************************************************************/ 58 | /*** Macro Definitions ***/ 59 | /****************************************************************************/ 60 | 61 | #ifdef DEBUG_APP 62 | #define TRACE_APP TRUE 63 | #else 64 | #define TRACE_APP FALSE 65 | #endif 66 | 67 | #define APP_ZTIMER_STORAGE 3 68 | 69 | #define BDB_QUEUE_SIZE 2 70 | #define MLME_QUEQUE_SIZE 8 71 | #define MCPS_QUEUE_SIZE 20 72 | #define TIMER_QUEUE_SIZE 8 73 | #define MCPS_DCFM_QUEUE_SIZE 5 74 | #define TX_QUEUE_SIZE 150 75 | #define RX_QUEUE_SIZE 150 76 | 77 | /****************************************************************************/ 78 | /*** Type Definitions ***/ 79 | /****************************************************************************/ 80 | 81 | /****************************************************************************/ 82 | /*** Local Function Prototypes ***/ 83 | /****************************************************************************/ 84 | 85 | /****************************************************************************/ 86 | /*** Exported Variables ***/ 87 | /****************************************************************************/ 88 | 89 | PUBLIC uint8 u8TimerTick; 90 | PUBLIC uint8 u8TimerRestart; 91 | PUBLIC uint8 u8TimerDeviceTemperature; 92 | 93 | PUBLIC tszQueue APP_msgBdbEvents; 94 | PUBLIC tszQueue APP_msgAppEvents; 95 | PUBLIC tszQueue APP_msgSerialTx; 96 | PUBLIC tszQueue APP_msgSerialRx; 97 | 98 | /****************************************************************************/ 99 | /*** Local Variables ***/ 100 | /****************************************************************************/ 101 | 102 | PRIVATE ZTIMER_tsTimer asTimers[APP_ZTIMER_STORAGE + BDB_ZTIMER_STORAGE]; 103 | 104 | PRIVATE BDB_tsZpsAfEvent asBdbEvent[BDB_QUEUE_SIZE]; 105 | PRIVATE MAC_tsMlmeVsDcfmInd asMacMlmeVsDcfmInd[MLME_QUEQUE_SIZE]; 106 | PRIVATE MAC_tsMcpsVsDcfmInd asMacMcpsDcfmInd[MCPS_QUEUE_SIZE]; 107 | PRIVATE zps_tsTimeEvent asTimeEvent[TIMER_QUEUE_SIZE]; 108 | PRIVATE MAC_tsMcpsVsCfmData asMacMcpsDcfm[MCPS_DCFM_QUEUE_SIZE]; 109 | PRIVATE uint8 au8TxBuffer[TX_QUEUE_SIZE]; 110 | PRIVATE uint8 au8RxBuffer[RX_QUEUE_SIZE]; 111 | 112 | /****************************************************************************/ 113 | /*** Exported Functions ***/ 114 | /****************************************************************************/ 115 | 116 | extern void zps_taskZPS(void); 117 | extern void PWRM_vManagePower(void); 118 | 119 | /**************************************************************************** 120 | * 121 | * NAME: APP_vMainLoop 122 | * 123 | * DESCRIPTION: 124 | * Main application loop 125 | * 126 | ****************************************************************************/ 127 | PUBLIC void APP_vMainLoop(void) 128 | { 129 | while (TRUE) { 130 | zps_taskZPS(); 131 | 132 | bdb_taskBDB(); 133 | 134 | ZTIMER_vTask(); 135 | 136 | APP_taskAtSerial(); 137 | 138 | /* Re-load the watch-dog timer. Execution must return through the idle 139 | * task before the CPU is suspended by the power manager. This ensures 140 | * that at least one task / ISR has executed within the watchdog period 141 | * otherwise the system will be reset. */ 142 | vAHI_WatchdogRestart(); 143 | 144 | /* suspends CPU operation when the system is idle or puts the device to 145 | * sleep if there are no activities in progress */ 146 | PWRM_vManagePower(); 147 | } 148 | } 149 | 150 | /**************************************************************************** 151 | * 152 | * NAME: APP_vSetUpHardware 153 | * 154 | * DESCRIPTION: 155 | * Set up interrupts 156 | * 157 | ****************************************************************************/ 158 | PUBLIC void APP_vSetUpHardware(void) 159 | { 160 | TARGET_INITIALISE(); 161 | /* clear interrupt priority level */ 162 | SET_IPL(0); 163 | portENABLE_INTERRUPTS(); 164 | } 165 | 166 | /**************************************************************************** 167 | * 168 | * NAME: APP_vInitResources 169 | * 170 | * DESCRIPTION: 171 | * Initialise resources (timers, queue's etc) 172 | * 173 | ****************************************************************************/ 174 | PUBLIC void APP_vInitResources(void) 175 | { 176 | /* Initialise the Z timer module */ 177 | ZTIMER_eInit(asTimers, sizeof(asTimers) / sizeof(ZTIMER_tsTimer)); 178 | 179 | /* Create Z timers */ 180 | ZTIMER_eOpen(&u8TimerTick, APP_cbTimerZclTick, NULL, ZTIMER_FLAG_PREVENT_SLEEP); 181 | ZTIMER_eOpen(&u8TimerRestart, APP_cbTimerRestart, NULL, ZTIMER_FLAG_PREVENT_SLEEP); 182 | ZTIMER_eOpen(&u8TimerDeviceTemperature, APP_cbTimerDeviceTemperatureUpdate, NULL, ZTIMER_FLAG_PREVENT_SLEEP); 183 | 184 | /* Create all the queues */ 185 | ZQ_vQueueCreate(&APP_msgBdbEvents, BDB_QUEUE_SIZE, sizeof(BDB_tsZpsAfEvent), (uint8 *)asBdbEvent); 186 | ZQ_vQueueCreate(&zps_msgMlmeDcfmInd, MLME_QUEQUE_SIZE, sizeof(MAC_tsMlmeVsDcfmInd), (uint8 *)asMacMlmeVsDcfmInd); 187 | ZQ_vQueueCreate(&zps_msgMcpsDcfmInd, MCPS_QUEUE_SIZE, sizeof(MAC_tsMcpsVsDcfmInd), (uint8 *)asMacMcpsDcfmInd); 188 | ZQ_vQueueCreate(&zps_TimeEvents, TIMER_QUEUE_SIZE, sizeof(zps_tsTimeEvent), (uint8 *)asTimeEvent); 189 | ZQ_vQueueCreate(&zps_msgMcpsDcfm, MCPS_DCFM_QUEUE_SIZE, sizeof(MAC_tsMcpsVsCfmData), (uint8 *)asMacMcpsDcfm); 190 | ZQ_vQueueCreate(&APP_msgSerialTx, TX_QUEUE_SIZE, sizeof(uint8), (uint8 *)au8TxBuffer); 191 | ZQ_vQueueCreate(&APP_msgSerialRx, RX_QUEUE_SIZE, sizeof(uint8), (uint8 *)au8RxBuffer); 192 | } 193 | 194 | /****************************************************************************/ 195 | /*** Local Functions ***/ 196 | /****************************************************************************/ 197 | 198 | /****************************************************************************/ 199 | /*** END OF FILE ***/ 200 | /****************************************************************************/ 201 | -------------------------------------------------------------------------------- /Source/app_main.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_main.h 6 | * 7 | * DESCRIPTION: Application main file 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2017. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef APP_MAIN_H 36 | #define APP_MAIN_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /* SDK JN-SW-4170 */ 45 | #include "ZQueue.h" 46 | 47 | /****************************************************************************/ 48 | /*** Macro Definitions ***/ 49 | /****************************************************************************/ 50 | 51 | /****************************************************************************/ 52 | /*** Type Definitions ***/ 53 | /****************************************************************************/ 54 | 55 | /****************************************************************************/ 56 | /*** Exported Variables ***/ 57 | /****************************************************************************/ 58 | 59 | extern PUBLIC uint8 u8TimerTick; 60 | extern PUBLIC uint8 u8TimerRestart; 61 | extern PUBLIC uint8 u8TimerDeviceTemperature; 62 | 63 | extern PUBLIC tszQueue APP_msgBdbEvents; 64 | extern PUBLIC tszQueue APP_msgAppEvents; 65 | extern PUBLIC tszQueue APP_msgSerialTx; 66 | extern PUBLIC tszQueue APP_msgSerialRx; 67 | 68 | extern PUBLIC tszQueue zps_msgMlmeDcfmInd; 69 | extern PUBLIC tszQueue zps_msgMcpsDcfmInd; 70 | extern PUBLIC tszQueue zps_msgMcpsDcfm; 71 | extern PUBLIC tszQueue zps_TimeEvents; 72 | 73 | /****************************************************************************/ 74 | /*** Exported Functions ***/ 75 | /****************************************************************************/ 76 | 77 | PUBLIC void APP_vMainLoop(void); 78 | PUBLIC void APP_vSetUpHardware(void); 79 | PUBLIC void APP_vInitResources(void); 80 | 81 | /****************************************************************************/ 82 | /*** END OF FILE ***/ 83 | /****************************************************************************/ 84 | 85 | #endif /* APP_MAIN_H */ 86 | -------------------------------------------------------------------------------- /Source/app_reporting.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_reporting.c 6 | * 7 | * DESCRIPTION: Reporting functionality 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | #include 41 | 42 | /* Generated */ 43 | #include "zps_gen.h" 44 | 45 | /* Application */ 46 | #include "PDM_IDs.h" 47 | #include "app_reporting.h" 48 | #include "zcl_options.h" 49 | 50 | /* SDK JN-SW-4170 */ 51 | #include "DeviceTemperatureConfiguration.h" 52 | #include "PDM.h" 53 | #include "dbg.h" 54 | #include "zcl.h" 55 | #include "zcl_common.h" 56 | 57 | /****************************************************************************/ 58 | /*** Macro Definitions ***/ 59 | /****************************************************************************/ 60 | 61 | #ifdef DEBUG_REPORT 62 | #define TRACE_REPORT TRUE 63 | #else 64 | #define TRACE_REPORT FALSE 65 | #endif 66 | 67 | #define DEVICE_TEMPERATURE_MINIMUM_REPORTABLE_CHANGE 0x01 68 | 69 | /****************************************************************************/ 70 | /*** Type Definitions ***/ 71 | /****************************************************************************/ 72 | 73 | typedef struct { 74 | uint16 u16ClusterID; 75 | tsZCL_AttributeReportingConfigurationRecord sAttributeReportingConfigurationRecord; 76 | } APP_tsReports; 77 | 78 | /****************************************************************************/ 79 | /*** Local Function Prototypes ***/ 80 | /****************************************************************************/ 81 | 82 | PRIVATE uint8 APP_u8GetRecordIndex(uint16 u16ClusterID, uint16 u16AttributeEnum); 83 | 84 | /****************************************************************************/ 85 | /*** Exported Variables ***/ 86 | /****************************************************************************/ 87 | 88 | /****************************************************************************/ 89 | /*** Local Variables ***/ 90 | /****************************************************************************/ 91 | 92 | /* Just Two reports for time being */ 93 | PRIVATE APP_tsReports asSavedReports[ZCL_NUMBER_OF_REPORTS]; 94 | 95 | /* define the default reports */ 96 | PRIVATE APP_tsReports asDefaultReports[ZCL_NUMBER_OF_REPORTS] = { 97 | { 98 | GENERAL_CLUSTER_ID_DEVICE_TEMPERATURE_CONFIGURATION, 99 | { 100 | 0, 101 | E_ZCL_INT16, 102 | E_CLD_DEVTEMPCFG_ATTR_ID_CURRENT_TEMPERATURE, 103 | MIN_REPORT_INTERVAL, 104 | MAX_REPORT_INTERVAL, 105 | 0, 106 | {DEVICE_TEMPERATURE_MINIMUM_REPORTABLE_CHANGE}, 107 | }, 108 | }, 109 | }; 110 | 111 | /****************************************************************************/ 112 | /*** Exported Functions ***/ 113 | /****************************************************************************/ 114 | 115 | /**************************************************************************** 116 | * 117 | * NAME: APP_eRestoreReports 118 | * 119 | * DESCRIPTION: 120 | * Loads the reporting information from the EEPROM/PDM 121 | * 122 | ****************************************************************************/ 123 | PUBLIC PDM_teStatus APP_eRestoreReports(void) 124 | { 125 | /* Restore any report data that is previously saved to flash */ 126 | uint16 u16ByteRead; 127 | PDM_teStatus eStatusReportReload = 128 | PDM_eReadDataFromRecord(PDM_ID_APP_REPORTS, asSavedReports, sizeof(asSavedReports), &u16ByteRead); 129 | 130 | DBG_vPrintf(TRACE_REPORT, "eStatusReportReload = %d\n", eStatusReportReload); 131 | /* Restore any application data previously saved to flash */ 132 | 133 | return (eStatusReportReload); 134 | } 135 | 136 | /**************************************************************************** 137 | * 138 | * NAME: APP_vMakeSupportedAttributesReportable 139 | * 140 | * DESCRIPTION: 141 | * Makes the attributes reportable 142 | * 143 | ****************************************************************************/ 144 | PUBLIC void APP_vMakeSupportedAttributesReportable(void) 145 | { 146 | int i; 147 | uint16 u16AttributeEnum; 148 | uint16 u16ClusterId; 149 | tsZCL_AttributeReportingConfigurationRecord *psAttributeReportingConfigurationRecord; 150 | 151 | DBG_vPrintf(TRACE_REPORT, "MAKE Reportable ep %d\n", LUMIROUTER_APPLICATION_ENDPOINT); 152 | 153 | for (i = 0; i < ZCL_NUMBER_OF_REPORTS; i++) { 154 | u16AttributeEnum = asSavedReports[i].sAttributeReportingConfigurationRecord.u16AttributeEnum; 155 | u16ClusterId = asSavedReports[i].u16ClusterID; 156 | psAttributeReportingConfigurationRecord = &(asSavedReports[i].sAttributeReportingConfigurationRecord); 157 | DBG_vPrintf( 158 | TRACE_REPORT, 159 | "Cluster %04x Attribute %04x Min %d Max %d IntV %d Direct %d Change %d\n", 160 | u16ClusterId, 161 | u16AttributeEnum, 162 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16MinimumReportingInterval, 163 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16MaximumReportingInterval, 164 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16TimeoutPeriodField, 165 | asSavedReports[i].sAttributeReportingConfigurationRecord.u8DirectionIsReceived, 166 | asSavedReports[i].sAttributeReportingConfigurationRecord.uAttributeReportableChange.zint8ReportableChange); 167 | eZCL_SetReportableFlag(LUMIROUTER_APPLICATION_ENDPOINT, u16ClusterId, TRUE, FALSE, u16AttributeEnum); 168 | eZCL_CreateLocalReport(LUMIROUTER_APPLICATION_ENDPOINT, 169 | u16ClusterId, 170 | 0, 171 | TRUE, 172 | psAttributeReportingConfigurationRecord); 173 | } 174 | } 175 | 176 | /**************************************************************************** 177 | * 178 | * NAME: APP_vLoadDefaultConfigForReportable 179 | * 180 | * DESCRIPTION: 181 | * Loads a default configuration 182 | * 183 | ****************************************************************************/ 184 | PUBLIC void APP_vLoadDefaultConfigForReportable(void) 185 | { 186 | int i; 187 | 188 | DBG_vPrintf(TRACE_REPORT, "Loading default configuration for reports\n"); 189 | 190 | memset(asSavedReports, 0, sizeof(asSavedReports)); 191 | 192 | for (i = 0; i < ZCL_NUMBER_OF_REPORTS; i++) { 193 | asSavedReports[i] = asDefaultReports[i]; 194 | DBG_vPrintf( 195 | TRACE_REPORT, 196 | "Cluster %04x Type %d Attr %04x Min %d Max %d IntV %d Direct %d Change %d\n", 197 | asSavedReports[i].u16ClusterID, 198 | asSavedReports[i].sAttributeReportingConfigurationRecord.eAttributeDataType, 199 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16AttributeEnum, 200 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16MinimumReportingInterval, 201 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16MaximumReportingInterval, 202 | asSavedReports[i].sAttributeReportingConfigurationRecord.u16TimeoutPeriodField, 203 | asSavedReports[i].sAttributeReportingConfigurationRecord.u8DirectionIsReceived, 204 | asSavedReports[i].sAttributeReportingConfigurationRecord.uAttributeReportableChange.zuint8ReportableChange); 205 | } 206 | 207 | /* Save this Records */ 208 | PDM_eSaveRecordData(PDM_ID_APP_REPORTS, asSavedReports, sizeof(asSavedReports)); 209 | } 210 | 211 | /**************************************************************************** 212 | * 213 | * NAME: APP_vSaveReportableRecord 214 | * 215 | * DESCRIPTION: 216 | * Save reportable record 217 | * 218 | ****************************************************************************/ 219 | PUBLIC void 220 | APP_vSaveReportableRecord(uint16 u16ClusterID, 221 | tsZCL_AttributeReportingConfigurationRecord *psAttributeReportingConfigurationRecord) 222 | { 223 | uint8 u8Index = APP_u8GetRecordIndex(u16ClusterID, psAttributeReportingConfigurationRecord->u16AttributeEnum); 224 | 225 | if (u8Index == 0xFF) { 226 | return; 227 | } 228 | 229 | DBG_vPrintf(TRACE_REPORT, "Save to report %d\n", u8Index); 230 | 231 | /* For CurrentLevel attribute in LevelControl Cluster */ 232 | asSavedReports[u8Index].u16ClusterID = u16ClusterID; 233 | memcpy(&(asSavedReports[u8Index].sAttributeReportingConfigurationRecord), 234 | psAttributeReportingConfigurationRecord, 235 | sizeof(tsZCL_AttributeReportingConfigurationRecord)); 236 | 237 | DBG_vPrintf(TRACE_REPORT, 238 | "Cluster %04x Type %d Attrib %04x Min %d Max %d IntV %d Direction %d Change %d\n", 239 | asSavedReports[u8Index].u16ClusterID, 240 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.eAttributeDataType, 241 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16AttributeEnum, 242 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16MinimumReportingInterval, 243 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16MaximumReportingInterval, 244 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16TimeoutPeriodField, 245 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u8DirectionIsReceived, 246 | asSavedReports[u8Index] 247 | .sAttributeReportingConfigurationRecord.uAttributeReportableChange.zuint8ReportableChange); 248 | 249 | /* Save this Records */ 250 | PDM_eSaveRecordData(PDM_ID_APP_REPORTS, asSavedReports, sizeof(asSavedReports)); 251 | } 252 | 253 | /**************************************************************************** 254 | * 255 | * NAME: APP_vRestoreDefaultRecord 256 | * 257 | * DESCRIPTION: 258 | * Restore Default Record 259 | * 260 | ****************************************************************************/ 261 | PUBLIC void 262 | APP_vRestoreDefaultRecord(uint8 u8EndPointID, 263 | uint16 u16ClusterID, 264 | tsZCL_AttributeReportingConfigurationRecord *psAttributeReportingConfigurationRecord) 265 | { 266 | uint8 u8Index = APP_u8GetRecordIndex(u16ClusterID, psAttributeReportingConfigurationRecord->u16AttributeEnum); 267 | 268 | if (u8Index == 0xFF) { 269 | return; 270 | } 271 | 272 | eZCL_CreateLocalReport(u8EndPointID, 273 | u16ClusterID, 274 | 0, 275 | TRUE, 276 | &(asDefaultReports[u8Index].sAttributeReportingConfigurationRecord)); 277 | 278 | DBG_vPrintf(TRACE_REPORT, "Save to report %d\n", u8Index); 279 | 280 | memcpy(&(asSavedReports[u8Index].sAttributeReportingConfigurationRecord), 281 | &(asDefaultReports[u8Index].sAttributeReportingConfigurationRecord), 282 | sizeof(tsZCL_AttributeReportingConfigurationRecord)); 283 | 284 | DBG_vPrintf(TRACE_REPORT, 285 | "Cluster %04x Type %d Attrib %04x Min %d Max %d IntV %d Direction %d Change %d\n", 286 | asSavedReports[u8Index].u16ClusterID, 287 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.eAttributeDataType, 288 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16AttributeEnum, 289 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16MinimumReportingInterval, 290 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16MaximumReportingInterval, 291 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u16TimeoutPeriodField, 292 | asSavedReports[u8Index].sAttributeReportingConfigurationRecord.u8DirectionIsReceived, 293 | asSavedReports[u8Index] 294 | .sAttributeReportingConfigurationRecord.uAttributeReportableChange.zuint8ReportableChange); 295 | 296 | /* Save this Records */ 297 | PDM_eSaveRecordData(PDM_ID_APP_REPORTS, asSavedReports, sizeof(asSavedReports)); 298 | } 299 | 300 | /****************************************************************************/ 301 | /*** Local Functions ***/ 302 | /****************************************************************************/ 303 | 304 | /**************************************************************************** 305 | * 306 | * NAME: APP_u8GetRecordIndex 307 | * 308 | * DESCRIPTION: 309 | * Get record index 310 | * 311 | * RETURNS: 312 | * Record index 313 | * 314 | ****************************************************************************/ 315 | PRIVATE uint8 APP_u8GetRecordIndex(uint16 u16ClusterID, uint16 u16AttributeEnum) 316 | { 317 | uint8 u8Index = 0xFF; 318 | 319 | if (u16ClusterID == GENERAL_CLUSTER_ID_DEVICE_TEMPERATURE_CONFIGURATION) { 320 | u8Index = REPORT_DEVICE_TEMPERATURE_CONFIGURATION_SLOT; 321 | } 322 | 323 | return u8Index; 324 | } 325 | 326 | /****************************************************************************/ 327 | /*** END OF FILE ***/ 328 | /****************************************************************************/ 329 | -------------------------------------------------------------------------------- /Source/app_reporting.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_reporting.h 6 | * 7 | * DESCRIPTION: Reporting functionality 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef APP_REPORTING_H 36 | #define APP_REPORTING_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /* SDK JN-SW-4170 */ 45 | #include "PDM.h" 46 | #include "zcl.h" 47 | 48 | /****************************************************************************/ 49 | /*** Macro Definitions ***/ 50 | /****************************************************************************/ 51 | 52 | /****************************************************************************/ 53 | /*** Type Definitions ***/ 54 | /****************************************************************************/ 55 | 56 | /****************************************************************************/ 57 | /*** Exported Variables ***/ 58 | /****************************************************************************/ 59 | 60 | /****************************************************************************/ 61 | /*** Exported Functions ***/ 62 | /****************************************************************************/ 63 | 64 | PUBLIC PDM_teStatus APP_eRestoreReports(void); 65 | PUBLIC void APP_vMakeSupportedAttributesReportable(void); 66 | PUBLIC void APP_vLoadDefaultConfigForReportable(void); 67 | PUBLIC void 68 | APP_vSaveReportableRecord(uint16 u16ClusterID, 69 | tsZCL_AttributeReportingConfigurationRecord *psAttributeReportingConfigurationRecord); 70 | PUBLIC void 71 | APP_vRestoreDefaultRecord(uint8 u8EndPointID, 72 | uint16 u16ClusterID, 73 | tsZCL_AttributeReportingConfigurationRecord *psAttributeReportingConfigurationRecord); 74 | 75 | /****************************************************************************/ 76 | /*** END OF FILE ***/ 77 | /****************************************************************************/ 78 | 79 | #endif /* APP_REPORTING_H */ 80 | -------------------------------------------------------------------------------- /Source/app_router_node.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_router_node.c 6 | * 7 | * DESCRIPTION: Router application 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2017. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | 41 | /* Generated */ 42 | #include "pdum_gen.h" 43 | #include "zps_gen.h" 44 | 45 | /* Application */ 46 | #include "PDM_IDs.h" 47 | #include "app_device_temperature.h" 48 | #include "app_main.h" 49 | #include "app_reporting.h" 50 | #include "app_router_node.h" 51 | #include "app_serial_commands.h" 52 | #include "app_zcl_task.h" 53 | 54 | /* SDK JN-SW-4170 */ 55 | #include "AppHardwareApi.h" 56 | #include "PDM.h" 57 | #include "bdb_api.h" 58 | #include "dbg.h" 59 | #include "mac_vs_sap.h" 60 | #include "pdum_apl.h" 61 | #include "pdum_nwk.h" 62 | #include "pwrm.h" 63 | #include "zps_apl_af.h" 64 | #include "zps_apl_aib.h" 65 | #include "zps_apl_aps.h" 66 | #include "zps_apl_zdo.h" 67 | #include "zps_nwk_nib.h" 68 | 69 | /****************************************************************************/ 70 | /*** Macro Definitions ***/ 71 | /****************************************************************************/ 72 | 73 | #ifdef DEBUG_APP 74 | #define TRACE_APP TRUE 75 | #else 76 | #define TRACE_APP FALSE 77 | #endif 78 | 79 | /****************************************************************************/ 80 | /*** Type Definitions ***/ 81 | /****************************************************************************/ 82 | 83 | typedef enum { E_STARTUP, E_RUNNING } APP_teNodeState; 84 | 85 | /****************************************************************************/ 86 | /*** Local Function Prototypes ***/ 87 | /****************************************************************************/ 88 | 89 | PRIVATE void APP_vBdbInit(void); 90 | PRIVATE void APP_vHandleAfEvents(BDB_tsZpsAfEvent *psZpsAfEvent); 91 | PRIVATE void APP_vHandleZdoEvents(BDB_tsZpsAfEvent *psZpsAfEvent); 92 | PRIVATE void APP_vFactoryResetRecords(void); 93 | PRIVATE void APP_vPrintAPSTable(void); 94 | 95 | /****************************************************************************/ 96 | /*** Exported Variables ***/ 97 | /****************************************************************************/ 98 | 99 | /****************************************************************************/ 100 | /*** Local Variables ***/ 101 | /****************************************************************************/ 102 | 103 | PRIVATE APP_teNodeState eNodeState; 104 | 105 | /****************************************************************************/ 106 | /*** Exported Functions ***/ 107 | /****************************************************************************/ 108 | 109 | #ifdef PDM_EEPROM 110 | extern uint8 u8PDM_CalculateFileSystemCapacity(); 111 | extern uint8 u8PDM_GetFileSystemOccupancy(); 112 | #endif 113 | 114 | /**************************************************************************** 115 | * 116 | * NAME: APP_vInitialiseRouter 117 | * 118 | * DESCRIPTION: 119 | * Initialises the application related functions 120 | * 121 | ****************************************************************************/ 122 | PUBLIC void APP_vInitialiseRouter(void) 123 | { 124 | uint16 u16ByteRead; 125 | PDM_teStatus eStatusReportReload; 126 | 127 | /* Stay awake */ 128 | PWRM_eStartActivity(); 129 | 130 | eNodeState = E_STARTUP; 131 | PDM_eReadDataFromRecord(PDM_ID_APP_ROUTER, &eNodeState, sizeof(APP_teNodeState), &u16ByteRead); 132 | 133 | /* Restore any report data that is previously saved to flash */ 134 | eStatusReportReload = APP_eRestoreReports(); 135 | 136 | ZPS_u32MacSetTxBuffers(4); 137 | 138 | /* Initialise ZBPro stack */ 139 | ZPS_eAplAfInit(); 140 | 141 | /* Initialise ZCL */ 142 | APP_ZCL_vInitialise(); 143 | 144 | /* Initialise other software modules 145 | * HERE */ 146 | APP_vBdbInit(); 147 | 148 | /* Always initialise any peripherals used by the application 149 | * HERE */ 150 | APP_vDeviceTemperatureInit(); 151 | 152 | #ifdef PDM_EEPROM 153 | /* The functions u8PDM_CalculateFileSystemCapacity and u8PDM_GetFileSystemOccupancy 154 | * may be called at any time to monitor space available in the eeprom */ 155 | DBG_vPrintf(TRACE_APP, "PDM: Capacity %d\n", u8PDM_CalculateFileSystemCapacity()); 156 | DBG_vPrintf(TRACE_APP, "PDM: Occupancy %d\n", u8PDM_GetFileSystemOccupancy()); 157 | #endif 158 | 159 | DBG_vPrintf(TRACE_APP, "Start Up StaTe %d On Network %d\n", eNodeState, sBDB.sAttrib.bbdbNodeIsOnANetwork); 160 | 161 | /* Load the reports from the PDM or the default ones depending on the PDM load record status */ 162 | if (eStatusReportReload != PDM_E_STATUS_OK) { 163 | /* Load Defaults if the data was not correct */ 164 | APP_vLoadDefaultConfigForReportable(); 165 | } 166 | /* Make the reportable attributes */ 167 | APP_vMakeSupportedAttributesReportable(); 168 | 169 | APP_WriteMessageToSerial("Router started.."); 170 | } 171 | 172 | /**************************************************************************** 173 | * 174 | * NAME: APP_cbTimerRestart 175 | * 176 | * DESCRIPTION: 177 | * CallBack For Restart 178 | * 179 | ****************************************************************************/ 180 | PUBLIC void APP_cbTimerRestart(void *pvParam) 181 | { 182 | vAHI_SwReset(); 183 | } 184 | 185 | /**************************************************************************** 186 | * 187 | * NAME: APP_vBdbCallback 188 | * 189 | * DESCRIPTION: 190 | * Callback from the BDB 191 | * --- 192 | * Called in SDK JN-SW-4170 193 | * 194 | ****************************************************************************/ 195 | PUBLIC void APP_vBdbCallback(BDB_tsBdbEvent *psBdbEvent) 196 | { 197 | BDB_teStatus eStatus; 198 | 199 | switch (psBdbEvent->eEventType) { 200 | case BDB_EVENT_NONE: 201 | break; 202 | 203 | case BDB_EVENT_ZPSAF: // Use with BDB_tsZpsAfEvent 204 | APP_vHandleAfEvents(&psBdbEvent->uEventData.sZpsAfEvent); 205 | break; 206 | 207 | case BDB_EVENT_INIT_SUCCESS: 208 | DBG_vPrintf(TRACE_APP, "APP: BDB_EVENT_INIT_SUCCESS\n"); 209 | if (eNodeState == E_STARTUP) { 210 | eStatus = BDB_eNsStartNwkSteering(); 211 | DBG_vPrintf(TRACE_APP, "BDB Try Steering status %d\n", eStatus); 212 | } 213 | else { 214 | DBG_vPrintf(TRACE_APP, "BDB Init go Running\n"); 215 | eNodeState = E_RUNNING; 216 | PDM_eSaveRecordData(PDM_ID_APP_ROUTER, &eNodeState, sizeof(APP_teNodeState)); 217 | } 218 | break; 219 | 220 | case BDB_EVENT_NWK_FORMATION_SUCCESS: 221 | DBG_vPrintf(TRACE_APP, "APP: NwkFormation Success\n"); 222 | break; 223 | 224 | case BDB_EVENT_NWK_STEERING_SUCCESS: 225 | DBG_vPrintf(TRACE_APP, "APP: NwkSteering Success\n"); 226 | eNodeState = E_RUNNING; 227 | PDM_eSaveRecordData(PDM_ID_APP_ROUTER, &eNodeState, sizeof(APP_teNodeState)); 228 | break; 229 | 230 | default: 231 | break; 232 | } 233 | } 234 | 235 | /****************************************************************************/ 236 | /*** Local Functions ***/ 237 | /****************************************************************************/ 238 | 239 | /**************************************************************************** 240 | * 241 | * NAME: APP_vBdbInit 242 | * 243 | * DESCRIPTION: 244 | * Function to initialize BDB attributes and message queue 245 | * 246 | ****************************************************************************/ 247 | PRIVATE void APP_vBdbInit(void) 248 | { 249 | BDB_tsInitArgs sInitArgs; 250 | 251 | sBDB.sAttrib.bbdbNodeIsOnANetwork = (eNodeState == E_RUNNING) ? TRUE : FALSE; 252 | sInitArgs.hBdbEventsMsgQ = &APP_msgBdbEvents; 253 | BDB_vInit(&sInitArgs); 254 | } 255 | 256 | /**************************************************************************** 257 | * 258 | * NAME: APP_vHandleAfEvents 259 | * 260 | * DESCRIPTION: 261 | * Application handler for stack events 262 | * 263 | ****************************************************************************/ 264 | PRIVATE void APP_vHandleAfEvents(BDB_tsZpsAfEvent *psZpsAfEvent) 265 | { 266 | if (psZpsAfEvent->u8EndPoint == LUMIROUTER_APPLICATION_ENDPOINT) { 267 | if ((psZpsAfEvent->sStackEvent.eType == ZPS_EVENT_APS_DATA_INDICATION) || 268 | (psZpsAfEvent->sStackEvent.eType == ZPS_EVENT_APS_INTERPAN_DATA_INDICATION)) { 269 | APP_ZCL_vEventHandler(&psZpsAfEvent->sStackEvent); 270 | } 271 | } 272 | else if (psZpsAfEvent->u8EndPoint == LUMIROUTER_ZDO_ENDPOINT) { 273 | APP_vHandleZdoEvents(psZpsAfEvent); 274 | } 275 | 276 | /* Ensure Freeing of Apdus */ 277 | if (psZpsAfEvent->sStackEvent.eType == ZPS_EVENT_APS_DATA_INDICATION) { 278 | PDUM_eAPduFreeAPduInstance(psZpsAfEvent->sStackEvent.uEvent.sApsDataIndEvent.hAPduInst); 279 | } 280 | else if (psZpsAfEvent->sStackEvent.eType == ZPS_EVENT_APS_INTERPAN_DATA_INDICATION) { 281 | PDUM_eAPduFreeAPduInstance(psZpsAfEvent->sStackEvent.uEvent.sApsInterPanDataIndEvent.hAPduInst); 282 | } 283 | } 284 | 285 | /**************************************************************************** 286 | * 287 | * NAME: APP_vHandleZdoEvents 288 | * 289 | * DESCRIPTION: 290 | * Application handler for stack events for end point 0 (ZDO) 291 | * 292 | ****************************************************************************/ 293 | PRIVATE void APP_vHandleZdoEvents(BDB_tsZpsAfEvent *psZpsAfEvent) 294 | { 295 | ZPS_tsAfEvent *psAfEvent = &(psZpsAfEvent->sStackEvent); 296 | 297 | switch (psAfEvent->eType) { 298 | case ZPS_EVENT_APS_DATA_INDICATION: 299 | DBG_vPrintf(TRACE_APP, 300 | "APP-ZDO: Data Indication Status %02x from %04x Src Ep Dst %d Ep %d Profile %04x Cluster %04x\n", 301 | psAfEvent->uEvent.sApsDataIndEvent.eStatus, 302 | psAfEvent->uEvent.sApsDataIndEvent.uSrcAddress.u16Addr, 303 | psAfEvent->uEvent.sApsDataIndEvent.u8SrcEndpoint, 304 | psAfEvent->uEvent.sApsDataIndEvent.u8DstEndpoint, 305 | psAfEvent->uEvent.sApsDataIndEvent.u16ProfileId, 306 | psAfEvent->uEvent.sApsDataIndEvent.u16ClusterId); 307 | break; 308 | 309 | case ZPS_EVENT_APS_DATA_CONFIRM: 310 | break; 311 | 312 | case ZPS_EVENT_APS_DATA_ACK: 313 | break; 314 | 315 | case ZPS_EVENT_NWK_STARTED: 316 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Network started\n"); 317 | break; 318 | 319 | case ZPS_EVENT_NWK_JOINED_AS_ROUTER: 320 | DBG_vPrintf(TRACE_APP, 321 | "APP-ZDO: Joined Network Addr %04x Rejoin %d\n", 322 | psAfEvent->uEvent.sNwkJoinedEvent.u16Addr, 323 | psAfEvent->uEvent.sNwkJoinedEvent.bRejoin); 324 | break; 325 | case ZPS_EVENT_NWK_FAILED_TO_START: 326 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Network Failed To start\n"); 327 | break; 328 | 329 | case ZPS_EVENT_NWK_FAILED_TO_JOIN: 330 | DBG_vPrintf(TRACE_APP, 331 | "APP-ZDO: Failed To Join %02x Rejoin %d\n", 332 | psAfEvent->uEvent.sNwkJoinFailedEvent.u8Status, 333 | psAfEvent->uEvent.sNwkJoinFailedEvent.bRejoin); 334 | break; 335 | 336 | case ZPS_EVENT_NWK_NEW_NODE_HAS_JOINED: 337 | DBG_vPrintf(TRACE_APP, 338 | "APP-ZDO: New Node %04x Has Joined\n", 339 | psAfEvent->uEvent.sNwkJoinIndicationEvent.u16NwkAddr); 340 | break; 341 | 342 | case ZPS_EVENT_NWK_DISCOVERY_COMPLETE: 343 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Discovery Complete %02x\n", psAfEvent->uEvent.sNwkDiscoveryEvent.eStatus); 344 | #if TRACE_APP 345 | APP_vPrintAPSTable(); 346 | #endif 347 | break; 348 | 349 | case ZPS_EVENT_NWK_LEAVE_INDICATION: 350 | DBG_vPrintf(TRACE_APP, 351 | "APP-ZDO: Leave Indication %016llx Rejoin %d\n", 352 | psAfEvent->uEvent.sNwkLeaveIndicationEvent.u64ExtAddr, 353 | psAfEvent->uEvent.sNwkLeaveIndicationEvent.u8Rejoin); 354 | if ((psAfEvent->uEvent.sNwkLeaveIndicationEvent.u64ExtAddr == 0UL) && 355 | (psAfEvent->uEvent.sNwkLeaveIndicationEvent.u8Rejoin == 0)) { 356 | /* We sare asked to Leave without rejoin */ 357 | DBG_vPrintf(TRACE_APP, "LEAVE IND -> For Us No Rejoin\n"); 358 | APP_vFactoryResetRecords(); 359 | vAHI_SwReset(); 360 | } 361 | break; 362 | 363 | case ZPS_EVENT_NWK_LEAVE_CONFIRM: 364 | DBG_vPrintf(TRACE_APP, 365 | "APP-ZDO: Leave Confirm status %02x Addr %016llx\n", 366 | psAfEvent->uEvent.sNwkLeaveConfirmEvent.eStatus, 367 | psAfEvent->uEvent.sNwkLeaveConfirmEvent.u64ExtAddr); 368 | if ((psAfEvent->uEvent.sNwkLeaveConfirmEvent.eStatus == ZPS_E_SUCCESS) && 369 | (psAfEvent->uEvent.sNwkLeaveConfirmEvent.u64ExtAddr == 0UL)) { 370 | DBG_vPrintf(TRACE_APP, "Leave -> Reset Data Structures\n"); 371 | APP_vFactoryResetRecords(); 372 | vAHI_SwReset(); 373 | } 374 | break; 375 | 376 | case ZPS_EVENT_NWK_STATUS_INDICATION: 377 | DBG_vPrintf(TRACE_APP, 378 | "APP-ZDO: Network status Indication %02x addr %04x\n", 379 | psAfEvent->uEvent.sNwkStatusIndicationEvent.u8Status, 380 | psAfEvent->uEvent.sNwkStatusIndicationEvent.u16NwkAddr); 381 | break; 382 | 383 | case ZPS_EVENT_NWK_ROUTE_DISCOVERY_CONFIRM: 384 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Discovery Confirm\n"); 385 | break; 386 | 387 | case ZPS_EVENT_NWK_ED_SCAN: 388 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Energy Detect Scan %02x\n", psAfEvent->uEvent.sNwkEdScanConfirmEvent.u8Status); 389 | break; 390 | 391 | case ZPS_EVENT_ZDO_BIND: 392 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Zdo Bind event\n"); 393 | break; 394 | 395 | case ZPS_EVENT_ZDO_UNBIND: 396 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Zdo Unbiind Event\n"); 397 | break; 398 | 399 | case ZPS_EVENT_ZDO_LINK_KEY: 400 | DBG_vPrintf(TRACE_APP, 401 | "APP-ZDO: Zdo Link Key Event Type %d Addr %016llx\n", 402 | psAfEvent->uEvent.sZdoLinkKeyEvent.u8KeyType, 403 | psAfEvent->uEvent.sZdoLinkKeyEvent.u64IeeeLinkAddr); 404 | break; 405 | 406 | case ZPS_EVENT_BIND_REQUEST_SERVER: 407 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Bind Request Server Event\n"); 408 | break; 409 | 410 | case ZPS_EVENT_ERROR: 411 | DBG_vPrintf(TRACE_APP, "APP-ZDO: AF Error Event %d\n", psAfEvent->uEvent.sAfErrorEvent.eError); 412 | break; 413 | 414 | case ZPS_EVENT_TC_STATUS: 415 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Trust Center Status %02x\n", psAfEvent->uEvent.sApsTcEvent.u8Status); 416 | break; 417 | 418 | default: 419 | DBG_vPrintf(TRACE_APP, "APP-ZDO: Unhandled Event %d\n", psAfEvent->eType); 420 | break; 421 | } 422 | } 423 | 424 | /**************************************************************************** 425 | * 426 | * NAME: APP_vFactoryResetRecords 427 | * 428 | * DESCRIPTION: 429 | * Resets persisted data structures to factory new state 430 | * 431 | ****************************************************************************/ 432 | PRIVATE void APP_vFactoryResetRecords(void) 433 | { 434 | /* clear out the stack */ 435 | ZPS_vDefaultStack(); 436 | ZPS_vSetKeys(); 437 | ZPS_eAplAibSetApsUseExtendedPanId(0); 438 | 439 | /* save everything */ 440 | eNodeState = E_STARTUP; 441 | PDM_eSaveRecordData(PDM_ID_APP_ROUTER, &eNodeState, sizeof(APP_teNodeState)); 442 | ZPS_vSaveAllZpsRecords(); 443 | } 444 | 445 | #if TRACE_APP 446 | /**************************************************************************** 447 | * 448 | * NAME: APP_vPrintAPSTable 449 | * 450 | * DESCRIPTION: 451 | * Prints the content of APS table 452 | * 453 | ****************************************************************************/ 454 | PRIVATE void APP_vPrintAPSTable(void) 455 | { 456 | uint8 i; 457 | uint8 j; 458 | 459 | ZPS_tsAplAib *tsAplAib; 460 | 461 | tsAplAib = ZPS_psAplAibGetAib(); 462 | 463 | for (i = 0; i < (tsAplAib->psAplDeviceKeyPairTable->u16SizeOfKeyDescriptorTable + 1); i++) { 464 | DBG_vPrintf(TRACE_APP, 465 | "MAC: %016llx Key: ", 466 | ZPS_u64NwkNibGetMappedIeeeAddr( 467 | ZPS_pvAplZdoGetNwkHandle(), 468 | tsAplAib->psAplDeviceKeyPairTable->psAplApsKeyDescriptorEntry[i].u16ExtAddrLkup)); 469 | for (j = 0; j < 16; j++) { 470 | DBG_vPrintf(TRACE_APP, 471 | "%02x ", 472 | tsAplAib->psAplDeviceKeyPairTable->psAplApsKeyDescriptorEntry[i].au8LinkKey[j]); 473 | } 474 | DBG_vPrintf(TRACE_APP, "\n"); 475 | DBG_vPrintf(TRACE_APP, "Incoming FC: %d\n", tsAplAib->pu32IncomingFrameCounter[i]); 476 | DBG_vPrintf(TRACE_APP, 477 | "Outgoing FC: %d\n", 478 | tsAplAib->psAplDeviceKeyPairTable->psAplApsKeyDescriptorEntry[i].u32OutgoingFrameCounter); 479 | } 480 | } 481 | #endif 482 | 483 | /****************************************************************************/ 484 | /*** END OF FILE ***/ 485 | /****************************************************************************/ 486 | -------------------------------------------------------------------------------- /Source/app_router_node.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_router_node.h 6 | * 7 | * DESCRIPTION: Router application 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef APP_ROUTER_NODE_H 36 | #define APP_ROUTER_NODE_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /****************************************************************************/ 45 | /*** Macro Definitions ***/ 46 | /****************************************************************************/ 47 | 48 | /****************************************************************************/ 49 | /*** Type Definitions ***/ 50 | /****************************************************************************/ 51 | 52 | /****************************************************************************/ 53 | /*** Exported Variables ***/ 54 | /****************************************************************************/ 55 | 56 | /****************************************************************************/ 57 | /*** Exported Functions ***/ 58 | /****************************************************************************/ 59 | 60 | PUBLIC void APP_vInitialiseRouter(void); 61 | PUBLIC void APP_cbTimerRestart(void *pvParam); 62 | 63 | /****************************************************************************/ 64 | /*** END OF FILE ***/ 65 | /****************************************************************************/ 66 | 67 | #endif /* APP_ROUTER_NODE_H */ 68 | -------------------------------------------------------------------------------- /Source/app_serial_commands.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_serial_commands.c 6 | * 7 | * DESCRIPTION: Serial Commands 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | 41 | /* Application */ 42 | #include "app_main.h" 43 | #include "app_serial_commands.h" 44 | #include "uart.h" 45 | 46 | /* SDK JN-SW-4170 */ 47 | #include "PDM.h" 48 | #include "ZQueue.h" 49 | #include "ZTimer.h" 50 | #include "dbg.h" 51 | #include "portmacro.h" 52 | 53 | /****************************************************************************/ 54 | /*** Macro Definitions ***/ 55 | /****************************************************************************/ 56 | 57 | #ifdef DEBUG_SERIAL 58 | #define TRACE_SERIAL TRUE 59 | #else 60 | #define TRACE_SERIAL FALSE 61 | #endif 62 | 63 | #define SL_START_CHAR 0x01 64 | #define SL_ESC_CHAR 0x02 65 | #define SL_END_CHAR 0x03 66 | 67 | #define MAX_PACKET_SIZE 32 68 | 69 | /****************************************************************************/ 70 | /*** Type Definitions ***/ 71 | /****************************************************************************/ 72 | 73 | /* Enumerated list of states for receive state machine */ 74 | typedef enum { 75 | E_STATE_RX_WAIT_START, 76 | E_STATE_RX_WAIT_TYPEMSB, 77 | E_STATE_RX_WAIT_TYPELSB, 78 | E_STATE_RX_WAIT_LENMSB, 79 | E_STATE_RX_WAIT_LENLSB, 80 | E_STATE_RX_WAIT_CRC, 81 | E_STATE_RX_WAIT_DATA 82 | } APP_teRxState; 83 | 84 | /* Serial link message types */ 85 | typedef enum { E_SC_MSG_RESET = 0x0011, E_SC_MSG_ERASE_PERSISTENT_DATA = 0x0012 }; 86 | 87 | /****************************************************************************/ 88 | /*** Local Function Prototypes ***/ 89 | /****************************************************************************/ 90 | 91 | PRIVATE void APP_vProcessRxChar(uint8 u8Char); 92 | PRIVATE void APP_vProcessCommand(void); 93 | PRIVATE void APP_vWriteTxChar(uint8 u8Char); 94 | PRIVATE uint8 APP_u8CalculateCRC(uint16 u16Type, uint16 u16Length, uint8 *pu8Data); 95 | 96 | /****************************************************************************/ 97 | /*** Exported Variables ***/ 98 | /****************************************************************************/ 99 | 100 | /****************************************************************************/ 101 | /*** Local Variables ***/ 102 | /****************************************************************************/ 103 | 104 | PRIVATE uint8 au8LinkRxBuffer[32]; 105 | PRIVATE uint16 u16PacketType; 106 | PRIVATE uint16 u16PacketLength; 107 | PRIVATE uint32 sStorage; 108 | 109 | /****************************************************************************/ 110 | /*** Exported Functions ***/ 111 | /****************************************************************************/ 112 | 113 | /**************************************************************************** 114 | * 115 | * NAME: APP_taskAtSerial 116 | * 117 | * DESCRIPTION: 118 | * Task that obtains a message from the serial Rx message queue. 119 | * 120 | ****************************************************************************/ 121 | PUBLIC void APP_taskAtSerial(void) 122 | { 123 | uint8 u8RxByte; 124 | if (ZQ_bQueueReceive(&APP_msgSerialRx, &u8RxByte)) { 125 | APP_vProcessRxChar(u8RxByte); 126 | } 127 | } 128 | 129 | /**************************************************************************** 130 | * 131 | * NAME: APP_WriteMessageToSerial 132 | * 133 | * DESCRIPTION: 134 | * Write message to the serial link 135 | * 136 | ****************************************************************************/ 137 | PUBLIC void APP_WriteMessageToSerial(const char *message) 138 | { 139 | DBG_vPrintf(TRACE_SERIAL, "APP_WriteMessageToSerial(%s)\n", message); 140 | 141 | for (; *message != '\0'; message++) { 142 | APP_vWriteTxChar(*message); 143 | } 144 | } 145 | 146 | /****************************************************************************/ 147 | /*** Local Functions ***/ 148 | /****************************************************************************/ 149 | 150 | /**************************************************************************** 151 | * 152 | * NAME: APP_vProcessRxChar 153 | * 154 | * DESCRIPTION: 155 | * Processes the received character 156 | * 157 | ****************************************************************************/ 158 | PRIVATE void APP_vProcessRxChar(uint8 u8Char) 159 | { 160 | static APP_teRxState eRxState = E_STATE_RX_WAIT_START; 161 | static uint8 u8CRC; 162 | static uint16 u16Bytes; 163 | static bool bInEsc = FALSE; 164 | 165 | switch (u8Char) { 166 | case SL_START_CHAR: 167 | /* Reset state machine */ 168 | u16Bytes = 0; 169 | bInEsc = FALSE; 170 | DBG_vPrintf(TRACE_SERIAL, "RX Start\n"); 171 | eRxState = E_STATE_RX_WAIT_TYPEMSB; 172 | break; 173 | 174 | case SL_ESC_CHAR: 175 | /* Escape next character */ 176 | bInEsc = TRUE; 177 | break; 178 | 179 | case SL_END_CHAR: 180 | /* End message */ 181 | DBG_vPrintf(TRACE_SERIAL, "Got END\n"); 182 | eRxState = E_STATE_RX_WAIT_START; 183 | if (u16PacketLength < MAX_PACKET_SIZE) { 184 | if (u8CRC == APP_u8CalculateCRC(u16PacketType, u16PacketLength, au8LinkRxBuffer)) { 185 | /* CRC matches - valid packet */ 186 | DBG_vPrintf(TRACE_SERIAL, "APP_vProcessRxChar(%d, %d, %02x)\n", u16PacketType, u16PacketLength, u8CRC); 187 | APP_vProcessCommand(); 188 | } 189 | } 190 | DBG_vPrintf(TRACE_SERIAL, "CRC BAD\n"); 191 | break; 192 | 193 | default: 194 | if (bInEsc) { 195 | /* Unescape the character */ 196 | u8Char ^= 0x10; 197 | bInEsc = FALSE; 198 | } 199 | DBG_vPrintf(TRACE_SERIAL, "Data 0x%x\n", u8Char & 0xFF); 200 | 201 | switch (eRxState) { 202 | case E_STATE_RX_WAIT_START: 203 | break; 204 | 205 | case E_STATE_RX_WAIT_TYPEMSB: 206 | u16PacketType = (uint16)u8Char << 8; 207 | eRxState++; 208 | break; 209 | 210 | case E_STATE_RX_WAIT_TYPELSB: 211 | u16PacketType += (uint16)u8Char; 212 | DBG_vPrintf(TRACE_SERIAL, "Type 0x%x\n", u16PacketType & 0xFFFF); 213 | eRxState++; 214 | break; 215 | 216 | case E_STATE_RX_WAIT_LENMSB: 217 | u16PacketLength = (uint16)u8Char << 8; 218 | eRxState++; 219 | break; 220 | 221 | case E_STATE_RX_WAIT_LENLSB: 222 | u16PacketLength += (uint16)u8Char; 223 | DBG_vPrintf(TRACE_SERIAL, "Length %d\n", u16PacketLength); 224 | if (u16PacketLength > MAX_PACKET_SIZE) { 225 | DBG_vPrintf(TRACE_SERIAL, "Length > MaxLength\n"); 226 | eRxState = E_STATE_RX_WAIT_START; 227 | } 228 | else { 229 | eRxState++; 230 | } 231 | break; 232 | 233 | case E_STATE_RX_WAIT_CRC: 234 | DBG_vPrintf(TRACE_SERIAL, "CRC %02x\n", u8Char); 235 | u8CRC = u8Char; 236 | eRxState++; 237 | break; 238 | 239 | case E_STATE_RX_WAIT_DATA: 240 | if (u16Bytes < u16PacketLength) { 241 | DBG_vPrintf(TRACE_SERIAL, "%02x ", u8Char); 242 | au8LinkRxBuffer[u16Bytes++] = u8Char; 243 | } 244 | break; 245 | } 246 | break; 247 | } 248 | } 249 | 250 | /**************************************************************************** 251 | * 252 | * NAME: APP_vProcessCommand 253 | * 254 | * DESCRIPTION: 255 | * Processed the received command 256 | * 257 | ****************************************************************************/ 258 | PRIVATE void APP_vProcessCommand(void) 259 | { 260 | switch (u16PacketType) { 261 | case E_SC_MSG_RESET: 262 | APP_WriteMessageToSerial("Reset..........."); 263 | ZTIMER_eStart(u8TimerRestart, ZTIMER_TIME_MSEC(100)); 264 | break; 265 | 266 | case E_SC_MSG_ERASE_PERSISTENT_DATA: 267 | APP_WriteMessageToSerial("Erase PDM......."); 268 | PDM_vDeleteAllDataRecords(); 269 | APP_WriteMessageToSerial("Reset..........."); 270 | ZTIMER_eStart(u8TimerRestart, ZTIMER_TIME_MSEC(100)); 271 | break; 272 | 273 | default: 274 | break; 275 | } 276 | } 277 | 278 | /**************************************************************************** 279 | * 280 | * NAME: APP_vWriteTxChar 281 | * 282 | * DESCRIPTION: 283 | * Write byte to the serial link 284 | * 285 | ****************************************************************************/ 286 | PRIVATE void APP_vWriteTxChar(uint8 u8Char) 287 | { 288 | ZPS_eEnterCriticalSection(NULL, &sStorage); 289 | 290 | if (UART_bTxReady() && ZQ_bQueueIsEmpty(&APP_msgSerialTx)) { 291 | /* send byte now and enable irq */ 292 | UART_vSetTxInterrupt(TRUE); 293 | UART_vTxChar(u8Char); 294 | } 295 | else { 296 | ZQ_bQueueSend(&APP_msgSerialTx, &u8Char); 297 | } 298 | 299 | ZPS_eExitCriticalSection(NULL, &sStorage); 300 | } 301 | 302 | /**************************************************************************** 303 | * 304 | * NAME: APP_u8CalculateCRC 305 | * 306 | * DESCRIPTION: 307 | * Calculate CRC of packet 308 | * 309 | * PARAMETERS: Name RW Usage 310 | * u8Type R Message type 311 | * u16Length R Message length 312 | * pu8Data R Message payload 313 | * 314 | * RETURNS: 315 | * CRC of packet 316 | * 317 | ****************************************************************************/ 318 | PRIVATE uint8 APP_u8CalculateCRC(uint16 u16Type, uint16 u16Length, uint8 *pu8Data) 319 | { 320 | int n; 321 | uint8 u8CRC; 322 | 323 | u8CRC = (u16Type >> 0) & 0xff; 324 | u8CRC ^= (u16Type >> 8) & 0xff; 325 | u8CRC ^= (u16Length >> 0) & 0xff; 326 | u8CRC ^= (u16Length >> 8) & 0xff; 327 | 328 | for (n = 0; n < u16Length; n++) { 329 | u8CRC ^= pu8Data[n]; 330 | } 331 | 332 | return (u8CRC); 333 | } 334 | 335 | /****************************************************************************/ 336 | /*** END OF FILE ***/ 337 | /****************************************************************************/ 338 | -------------------------------------------------------------------------------- /Source/app_serial_commands.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_serial_commands.h 6 | * 7 | * DESCRIPTION: Serial Commands 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef APP_SERIAL_COMMANDS_H 36 | #define APP_SERIAL_COMMANDS_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /****************************************************************************/ 45 | /*** Macro Definitions ***/ 46 | /****************************************************************************/ 47 | 48 | /****************************************************************************/ 49 | /*** Type Definitions ***/ 50 | /****************************************************************************/ 51 | 52 | /****************************************************************************/ 53 | /*** Exported Variables ***/ 54 | /****************************************************************************/ 55 | 56 | /****************************************************************************/ 57 | /*** Exported Functions ***/ 58 | /****************************************************************************/ 59 | 60 | PUBLIC void APP_taskAtSerial(void); 61 | PUBLIC void APP_WriteMessageToSerial(const char *message); 62 | 63 | /****************************************************************************/ 64 | /*** END OF FILE ***/ 65 | /****************************************************************************/ 66 | 67 | #endif /* APP_SERIAL_COMMANDS_H */ 68 | -------------------------------------------------------------------------------- /Source/app_start.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_start.c 6 | * 7 | * DESCRIPTION: Router Initialisation 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2017. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | 41 | /* Generated */ 42 | #include "pdum_gen.h" 43 | 44 | /* Application */ 45 | #include "app_main.h" 46 | #include "app_router_node.h" 47 | #include "uart.h" 48 | 49 | /* SDK JN-SW-4170 */ 50 | #include "AppApi.h" 51 | #include "AppHardwareApi.h" 52 | #include "PDM.h" 53 | #include "bdb_api.h" 54 | #include "dbg.h" 55 | #include "dbg_uart.h" 56 | #include "pwrm.h" 57 | #include "zps_nwk_pub.h" 58 | 59 | /****************************************************************************/ 60 | /*** Macro Definitions ***/ 61 | /****************************************************************************/ 62 | 63 | #ifdef DEBUG_APP 64 | #define TRACE_APP TRUE 65 | #else 66 | #define TRACE_APP FALSE 67 | #endif 68 | 69 | /****************************************************************************/ 70 | /*** Type Definitions ***/ 71 | /****************************************************************************/ 72 | 73 | /****************************************************************************/ 74 | /*** Local Function Prototypes ***/ 75 | /****************************************************************************/ 76 | 77 | PRIVATE void APP_vInitialise(void); 78 | PRIVATE void vfExtendedStatusCallBack(ZPS_teExtendedStatus eExtendedStatus); 79 | 80 | /****************************************************************************/ 81 | /*** Exported Variables ***/ 82 | /****************************************************************************/ 83 | 84 | extern void *_stack_low_water_mark; 85 | 86 | /****************************************************************************/ 87 | /*** Local Variables ***/ 88 | /****************************************************************************/ 89 | 90 | /****************************************************************************/ 91 | /*** Exported Functions ***/ 92 | /****************************************************************************/ 93 | 94 | /**************************************************************************** 95 | * 96 | * NAME: vAppMain 97 | * 98 | * DESCRIPTION: 99 | * Entry point for application from a cold start. 100 | * --- 101 | * Called in SDK JN-SW-4170 102 | * 103 | ****************************************************************************/ 104 | PUBLIC void vAppMain(void) 105 | { 106 | /* Wait until FALSE i.e. on XTAL - otherwise UART data will be at wrong speed */ 107 | while (bAHI_GetClkSource() == TRUE) 108 | ; 109 | 110 | /* Move CPU to 32 MHz; vAHI_OptimiseWaitStates automatically called */ 111 | bAHI_SetClockRate(3); 112 | 113 | #ifdef UART_DEBUGGING 114 | /* Initialise the debug diagnostics module to use UART1 at 115K Baud */ 115 | DBG_vUartInit(DBG_E_UART_1, DBG_E_UART_BAUD_RATE_115200); 116 | #endif 117 | 118 | /* Initialise the stack overflow exception to trigger if the end of the 119 | * stack is reached. See the linker command file to adjust the allocated 120 | * stack size. */ 121 | vAHI_SetStackOverflow(TRUE, (uint32)&_stack_low_water_mark); 122 | 123 | /* Catch resets due to watchdog timer expiry. Comment out to harden code. */ 124 | if (bAHI_WatchdogResetEvent()) { 125 | DBG_vPrintf(TRACE_APP, "APP: Watchdog timer has reset device!\n"); 126 | DBG_vDumpStack(); 127 | } 128 | 129 | #ifdef ENABLING_HIGH_POWER_MODE 130 | /* After testing on Xiaomi DGNWG05LM and Aqara ZHWG11LM devices, it was 131 | * decided to use the deprecated vAppApiSetHighPowerMode method for use on 132 | * JN5168 instead of the new vAHI_ModuleConfigure method for use on JN5169. 133 | * I checked the following options: 134 | * - vAHI_ModuleConfigure(E_MODULE_DEFAULT) does not work on Aqara 135 | * - vAHI_ModuleConfigure(E_MODULE_JN5169_001_M03_ETSI) does not work on Aqara 136 | * - vAHI_ModuleConfigure(E_MODULE_JN5169_001_M06_FCC) low signal on Xiaomi 137 | * - vAppApiSetHighPowerMode (APP_API_MODULE_HPM05, TRUE) works well both on Xiaomi and Aqara */ 138 | vAppApiSetHighPowerMode(APP_API_MODULE_HPM05, TRUE); 139 | #endif 140 | 141 | /* idle task commences here */ 142 | DBG_vPrintf(TRACE_APP, "*** ROUTER RESET ***\n"); 143 | 144 | DBG_vPrintf(TRACE_APP, "APP: Entering APP_vSetUpHardware()\n"); 145 | APP_vSetUpHardware(); 146 | 147 | DBG_vPrintf(TRACE_APP, "APP: Entering APP_vInitResources()\n"); 148 | APP_vInitResources(); 149 | 150 | DBG_vPrintf(TRACE_APP, "APP: Entering APP_vInitialise()\n"); 151 | APP_vInitialise(); 152 | 153 | DBG_vPrintf(TRACE_APP, "APP: Entering BDB_vStart()\n"); 154 | BDB_vStart(); 155 | 156 | DBG_vPrintf(TRACE_APP, "APP: Entering APP_vMainLoop()\n"); 157 | APP_vMainLoop(); 158 | } 159 | 160 | /**************************************************************************** 161 | * 162 | * NAME: vAppRegisterPWRMCallbacks 163 | * 164 | * DESCRIPTION: 165 | * Power manager callback. 166 | * Called to allow the application to register sleep and wake callbacks. 167 | * --- 168 | * Called in SDK JN-SW-4170 169 | * 170 | ****************************************************************************/ 171 | PUBLIC void vAppRegisterPWRMCallbacks(void) 172 | { 173 | /* nothing to register as device does not sleep */ 174 | } 175 | 176 | /****************************************************************************/ 177 | /*** Local Functions ***/ 178 | /****************************************************************************/ 179 | 180 | /**************************************************************************** 181 | * 182 | * NAME: APP_vInitialise 183 | * 184 | * DESCRIPTION: 185 | * Initialises Zigbee stack, hardware and application. 186 | * 187 | ****************************************************************************/ 188 | PRIVATE void APP_vInitialise(void) 189 | { 190 | /* Initialise Power Manager even on non-sleeping nodes as it allows the 191 | * device to doze when in the idle task */ 192 | PWRM_vInit(E_AHI_SLEEP_OSCON_RAMON); 193 | 194 | /* Initialise the Persistent Data Manager */ 195 | PDM_eInitialise(63); 196 | 197 | /* Initialise Protocol Data Unit Manager */ 198 | PDUM_vInit(); 199 | 200 | UART_vInit(); 201 | UART_vRtsStartFlow(); 202 | 203 | ZPS_vExtendedStatusSetCallback(vfExtendedStatusCallBack); 204 | 205 | /* Initialise application */ 206 | APP_vInitialiseRouter(); 207 | } 208 | 209 | /**************************************************************************** 210 | * 211 | * NAME: vfExtendedStatusCallBack 212 | * 213 | * DESCRIPTION: 214 | * Callback from stack on extended error situations. 215 | * 216 | ****************************************************************************/ 217 | PRIVATE void vfExtendedStatusCallBack(ZPS_teExtendedStatus eExtendedStatus) 218 | { 219 | DBG_vPrintf(TRACE_APP, "ERROR: Extended status 0x%02x\n", eExtendedStatus); 220 | } 221 | 222 | /****************************************************************************/ 223 | /*** END OF FILE ***/ 224 | /****************************************************************************/ 225 | -------------------------------------------------------------------------------- /Source/app_zcl_task.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_zcl_task.c 6 | * 7 | * DESCRIPTION: ZCL Interface 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | #include 41 | 42 | /* Generated */ 43 | #include "pdum_gen.h" 44 | #include "zps_gen.h" 45 | 46 | /* Application */ 47 | #include "app_main.h" 48 | #include "app_reporting.h" 49 | #include "app_zcl_task.h" 50 | #include "zcl_options.h" 51 | 52 | /* SDK JN-SW-4170 */ 53 | #include "Basic.h" 54 | #include "DeviceTemperatureConfiguration.h" 55 | #include "ZTimer.h" 56 | #include "dbg.h" 57 | #include "zcl.h" 58 | 59 | /****************************************************************************/ 60 | /*** Macro Definitions ***/ 61 | /****************************************************************************/ 62 | 63 | #ifdef DEBUG_ZCL 64 | #define TRACE_ZCL TRUE 65 | #else 66 | #define TRACE_ZCL FALSE 67 | #endif 68 | 69 | #define ZCL_TICK_TIME ZTIMER_TIME_SEC(1) 70 | 71 | /****************************************************************************/ 72 | /*** Type Definitions ***/ 73 | /****************************************************************************/ 74 | 75 | /****************************************************************************/ 76 | /*** Local Function Prototypes ***/ 77 | /****************************************************************************/ 78 | 79 | PRIVATE void APP_ZCL_vTick(void); 80 | PRIVATE void APP_ZCL_cbGeneralCallback(tsZCL_CallBackEvent *psEvent); 81 | PRIVATE void APP_ZCL_cbEndpointCallback(tsZCL_CallBackEvent *psEvent); 82 | PRIVATE void APP_ZCL_vHandleClusterCustomCommands(tsZCL_CallBackEvent *psEvent); 83 | PRIVATE teZCL_Status APP_ZCL_eRegisterEndPoint(tfpZCL_ZCLCallBackFunction cbCallBack, APP_tsLumiRouter *psDeviceInfo); 84 | PRIVATE void APP_ZCL_vDeviceSpecific_Init(void); 85 | 86 | /****************************************************************************/ 87 | /*** Exported Variables ***/ 88 | /****************************************************************************/ 89 | 90 | PUBLIC APP_tsLumiRouter sLumiRouter; 91 | 92 | /****************************************************************************/ 93 | /*** Local Variables ***/ 94 | /****************************************************************************/ 95 | 96 | /****************************************************************************/ 97 | /*** Exported Functions ***/ 98 | /****************************************************************************/ 99 | 100 | /**************************************************************************** 101 | * 102 | * NAME: APP_ZCL_vInitialise 103 | * 104 | * DESCRIPTION: 105 | * Initialises ZCL related functions 106 | * 107 | ****************************************************************************/ 108 | PUBLIC void APP_ZCL_vInitialise(void) 109 | { 110 | teZCL_Status eZCL_Status; 111 | 112 | /* Initialise ZLL */ 113 | eZCL_Status = eZCL_Initialise(&APP_ZCL_cbGeneralCallback, apduZCL); 114 | if (eZCL_Status != E_ZCL_SUCCESS) { 115 | DBG_vPrintf(TRACE_ZCL, "Err: eZLO_Initialise:%d\n", eZCL_Status); 116 | } 117 | 118 | /* Start the tick timer */ 119 | ZTIMER_eStart(u8TimerTick, ZCL_TICK_TIME); 120 | 121 | /* Register Light EndPoint */ 122 | eZCL_Status = APP_ZCL_eRegisterEndPoint(&APP_ZCL_cbEndpointCallback, &sLumiRouter); 123 | if (eZCL_Status != E_ZCL_SUCCESS) { 124 | DBG_vPrintf(TRACE_ZCL, "Error: APP_ZCL_eRegisterEndPoint: %02x\n", eZCL_Status); 125 | } 126 | 127 | APP_ZCL_vDeviceSpecific_Init(); 128 | } 129 | 130 | /**************************************************************************** 131 | * 132 | * NAME: APP_ZCL_vEventHandler 133 | * 134 | * DESCRIPTION: 135 | * Main ZCL processing task 136 | * 137 | ****************************************************************************/ 138 | PUBLIC void APP_ZCL_vEventHandler(ZPS_tsAfEvent *psStackEvent) 139 | { 140 | tsZCL_CallBackEvent sCallBackEvent; 141 | sCallBackEvent.pZPSevent = psStackEvent; 142 | 143 | DBG_vPrintf(TRACE_ZCL, "ZCL_Task endpoint event:%d \n", psStackEvent->eType); 144 | sCallBackEvent.eEventType = E_ZCL_CBET_ZIGBEE_EVENT; 145 | vZCL_EventHandler(&sCallBackEvent); 146 | } 147 | 148 | /**************************************************************************** 149 | * 150 | * NAME: APP_cbTimerZclTick 151 | * 152 | * DESCRIPTION: 153 | * CallBack For ZCL Tick timer 154 | * 155 | ****************************************************************************/ 156 | PUBLIC void APP_cbTimerZclTick(void *pvParam) 157 | { 158 | /* 159 | * If the 1 second tick timer has expired, restart it and pass 160 | * the event on to ZCL 161 | */ 162 | APP_ZCL_vTick(); 163 | ZTIMER_eStart(u8TimerTick, ZCL_TICK_TIME); 164 | } 165 | 166 | /****************************************************************************/ 167 | /*** Local Functions ***/ 168 | /****************************************************************************/ 169 | 170 | /**************************************************************************** 171 | * 172 | * NAME: APP_ZCL_vTick 173 | * 174 | * DESCRIPTION: 175 | * ZCL Tick 176 | * 177 | ****************************************************************************/ 178 | PRIVATE void APP_ZCL_vTick(void) 179 | { 180 | tsZCL_CallBackEvent sCallBackEvent; 181 | 182 | sCallBackEvent.pZPSevent = NULL; 183 | sCallBackEvent.eEventType = E_ZCL_CBET_TIMER; 184 | vZCL_EventHandler(&sCallBackEvent); 185 | } 186 | 187 | /**************************************************************************** 188 | * 189 | * NAME: APP_ZCL_cbGeneralCallback 190 | * 191 | * DESCRIPTION: 192 | * General callback for ZCL events 193 | * 194 | ****************************************************************************/ 195 | PRIVATE void APP_ZCL_cbGeneralCallback(tsZCL_CallBackEvent *psEvent) 196 | { 197 | switch (psEvent->eEventType) { 198 | case E_ZCL_CBET_LOCK_MUTEX: 199 | DBG_vPrintf(TRACE_ZCL, "EVT: Lock Mutex\n"); 200 | break; 201 | 202 | case E_ZCL_CBET_UNLOCK_MUTEX: 203 | DBG_vPrintf(TRACE_ZCL, "EVT: Unlock Mutex\n"); 204 | break; 205 | 206 | case E_ZCL_CBET_UNHANDLED_EVENT: 207 | DBG_vPrintf(TRACE_ZCL, "EVT: Unhandled Event\n"); 208 | break; 209 | 210 | case E_ZCL_CBET_READ_ATTRIBUTES_RESPONSE: 211 | DBG_vPrintf(TRACE_ZCL, "EVT: Read attributes response\n"); 212 | break; 213 | 214 | case E_ZCL_CBET_READ_REQUEST: 215 | DBG_vPrintf(TRACE_ZCL, "EVT: Read request\n"); 216 | break; 217 | 218 | case E_ZCL_CBET_DEFAULT_RESPONSE: 219 | DBG_vPrintf(TRACE_ZCL, "EVT: Default response\n"); 220 | break; 221 | 222 | case E_ZCL_CBET_ERROR: 223 | DBG_vPrintf(TRACE_ZCL, "EVT: Error\n"); 224 | break; 225 | 226 | case E_ZCL_CBET_TIMER: 227 | DBG_vPrintf(TRACE_ZCL, "EVT: Timer\n"); 228 | break; 229 | 230 | case E_ZCL_CBET_ZIGBEE_EVENT: 231 | DBG_vPrintf(TRACE_ZCL, "EVT: ZigBee\n"); 232 | break; 233 | 234 | case E_ZCL_CBET_CLUSTER_CUSTOM: 235 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Custom\n"); 236 | break; 237 | 238 | default: 239 | DBG_vPrintf(TRACE_ZCL, "Invalid event type\n"); 240 | break; 241 | } 242 | } 243 | 244 | /**************************************************************************** 245 | * 246 | * NAME: APP_ZCL_cbEndpointCallback 247 | * 248 | * DESCRIPTION: 249 | * Endpoint specific callback for ZCL events 250 | * 251 | ****************************************************************************/ 252 | PRIVATE void APP_ZCL_cbEndpointCallback(tsZCL_CallBackEvent *psEvent) 253 | { 254 | 255 | switch (psEvent->eEventType) { 256 | case E_ZCL_CBET_LOCK_MUTEX: 257 | break; 258 | 259 | case E_ZCL_CBET_UNLOCK_MUTEX: 260 | break; 261 | 262 | case E_ZCL_CBET_UNHANDLED_EVENT: 263 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Unhandled event\n"); 264 | break; 265 | 266 | case E_ZCL_CBET_READ_INDIVIDUAL_ATTRIBUTE_RESPONSE: 267 | DBG_vPrintf(TRACE_ZCL, 268 | "EP EVT: Rd Attr Rsp %04x AS %d\n", 269 | psEvent->uMessage.sIndividualAttributeResponse.u16AttributeEnum, 270 | psEvent->uMessage.sIndividualAttributeResponse.eAttributeStatus); 271 | break; 272 | 273 | case E_ZCL_CBET_READ_ATTRIBUTES_RESPONSE: 274 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Read attributes response\n"); 275 | break; 276 | 277 | case E_ZCL_CBET_READ_REQUEST: 278 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Read request\n"); 279 | break; 280 | 281 | case E_ZCL_CBET_DEFAULT_RESPONSE: 282 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Default response\n"); 283 | break; 284 | 285 | case E_ZCL_CBET_ERROR: 286 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Error\n"); 287 | break; 288 | 289 | case E_ZCL_CBET_TIMER: 290 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Timer\n"); 291 | break; 292 | 293 | case E_ZCL_CBET_ZIGBEE_EVENT: 294 | DBG_vPrintf(TRACE_ZCL, "EP EVT: ZigBee\n"); 295 | break; 296 | 297 | case E_ZCL_CBET_CLUSTER_CUSTOM: 298 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Custom Cl %04x\n", psEvent->uMessage.sClusterCustomMessage.u16ClusterId); 299 | APP_ZCL_vHandleClusterCustomCommands(psEvent); 300 | break; 301 | 302 | case E_ZCL_CBET_WRITE_INDIVIDUAL_ATTRIBUTE: 303 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Write Individual Attribute Status %02x\n", psEvent->eZCL_Status); 304 | break; 305 | 306 | case E_ZCL_CBET_REPORT_INDIVIDUAL_ATTRIBUTE: { 307 | tsZCL_IndividualAttributesResponse *psIndividualAttributeResponse = 308 | &psEvent->uMessage.sIndividualAttributeResponse; 309 | DBG_vPrintf(TRACE_ZCL, 310 | "Individual Report attribute for Cluster = %d\n", 311 | psEvent->psClusterInstance->psClusterDefinition->u16ClusterEnum); 312 | DBG_vPrintf(TRACE_ZCL, "eAttributeDataType = %d\n", psIndividualAttributeResponse->eAttributeDataType); 313 | DBG_vPrintf(TRACE_ZCL, "u16AttributeEnum = %d\n", psIndividualAttributeResponse->u16AttributeEnum); 314 | DBG_vPrintf(TRACE_ZCL, "eAttributeStatus = %d\n", psIndividualAttributeResponse->eAttributeStatus); 315 | } break; 316 | 317 | case E_ZCL_CBET_REPORT_INDIVIDUAL_ATTRIBUTES_CONFIGURE: { 318 | tsZCL_AttributeReportingConfigurationRecord *psAttributeReportingRecord = 319 | &psEvent->uMessage.sAttributeReportingConfigurationRecord; 320 | DBG_vPrintf( 321 | TRACE_ZCL, 322 | "Individual Configure Report Cluster %d Attrib %d Type %d Min %d Max %d IntV %d Direcct %d Change %d\n", 323 | psEvent->psClusterInstance->psClusterDefinition->u16ClusterEnum, 324 | psAttributeReportingRecord->u16AttributeEnum, 325 | psAttributeReportingRecord->eAttributeDataType, 326 | psAttributeReportingRecord->u16MinimumReportingInterval, 327 | psAttributeReportingRecord->u16MaximumReportingInterval, 328 | psAttributeReportingRecord->u16TimeoutPeriodField, 329 | psAttributeReportingRecord->u8DirectionIsReceived, 330 | psAttributeReportingRecord->uAttributeReportableChange); 331 | 332 | if (E_ZCL_SUCCESS == psEvent->eZCL_Status) { 333 | APP_vSaveReportableRecord(psEvent->psClusterInstance->psClusterDefinition->u16ClusterEnum, 334 | psAttributeReportingRecord); 335 | } 336 | else if (E_ZCL_RESTORE_DEFAULT_REPORT_CONFIGURATION == psEvent->eZCL_Status) { 337 | APP_vRestoreDefaultRecord(LUMIROUTER_APPLICATION_ENDPOINT, 338 | psEvent->psClusterInstance->psClusterDefinition->u16ClusterEnum, 339 | psAttributeReportingRecord); 340 | } 341 | } break; 342 | 343 | case E_ZCL_CBET_CLUSTER_UPDATE: 344 | DBG_vPrintf(TRACE_ZCL, "Update Id %04x\n", psEvent->psClusterInstance->psClusterDefinition->u16ClusterEnum); 345 | break; 346 | 347 | case E_ZCL_CBET_REPORT_REQUEST: 348 | break; 349 | 350 | default: 351 | DBG_vPrintf(TRACE_ZCL, "EP EVT: Invalid evt type 0x%x\n", (uint8)psEvent->eEventType); 352 | break; 353 | } 354 | } 355 | 356 | /**************************************************************************** 357 | * 358 | * NAME: APP_ZCL_vHandleClusterCustomCommands 359 | * 360 | * DESCRIPTION: 361 | * callback for ZCL cluster custom command events 362 | * 363 | ****************************************************************************/ 364 | PRIVATE void APP_ZCL_vHandleClusterCustomCommands(tsZCL_CallBackEvent *psEvent) 365 | { 366 | if (psEvent->uMessage.sClusterCustomMessage.u16ClusterId == GENERAL_CLUSTER_ID_BASIC) { 367 | tsCLD_BasicCallBackMessage *psCallBackMessage = 368 | (tsCLD_BasicCallBackMessage *)psEvent->uMessage.sClusterCustomMessage.pvCustomData; 369 | if (psCallBackMessage->u8CommandId == E_CLD_BASIC_CMD_RESET_TO_FACTORY_DEFAULTS) { 370 | DBG_vPrintf(TRACE_ZCL, "Basic Factory Reset Received\n"); 371 | memset(&sLumiRouter, 0, sizeof(APP_tsLumiRouter)); 372 | APP_ZCL_eRegisterEndPoint(&APP_ZCL_cbEndpointCallback, &sLumiRouter); 373 | APP_ZCL_vDeviceSpecific_Init(); 374 | } 375 | } 376 | } 377 | 378 | /**************************************************************************** 379 | * 380 | * NAME: APP_ZCL_eRegisterEndPoint 381 | * 382 | * DESCRIPTION: 383 | * Register ZLO endpoints 384 | * 385 | * PARAMETERS: Name Usage 386 | * cbCallBack Pointer to endpoint callback 387 | * psDeviceInfo Pointer to struct containing 388 | * data for endpoint 389 | * 390 | * RETURNS: 391 | * teZCL_Status 392 | * 393 | ****************************************************************************/ 394 | PRIVATE teZCL_Status APP_ZCL_eRegisterEndPoint(tfpZCL_ZCLCallBackFunction cbCallBack, APP_tsLumiRouter *psDeviceInfo) 395 | { 396 | /* Fill in end point details */ 397 | psDeviceInfo->sEndPoint.u8EndPointNumber = LUMIROUTER_APPLICATION_ENDPOINT; 398 | psDeviceInfo->sEndPoint.u16ManufacturerCode = ZCL_MANUFACTURER_CODE; 399 | psDeviceInfo->sEndPoint.u16ProfileEnum = HA_PROFILE_ID; 400 | psDeviceInfo->sEndPoint.bIsManufacturerSpecificProfile = FALSE; 401 | psDeviceInfo->sEndPoint.u16NumberOfClusters = 402 | sizeof(APP_tsLumiRouterClusterInstances) / sizeof(tsZCL_ClusterInstance); 403 | psDeviceInfo->sEndPoint.psClusterInstance = (tsZCL_ClusterInstance *)&psDeviceInfo->sClusterInstance; 404 | psDeviceInfo->sEndPoint.bDisableDefaultResponse = ZCL_DISABLE_DEFAULT_RESPONSES; 405 | psDeviceInfo->sEndPoint.pCallBackFunctions = cbCallBack; 406 | 407 | if (eCLD_BasicCreateBasic(&psDeviceInfo->sClusterInstance.sBasicServer, 408 | TRUE, 409 | &sCLD_Basic, 410 | &psDeviceInfo->sBasicServerCluster, 411 | &au8BasicClusterAttributeControlBits[0]) != E_ZCL_SUCCESS) { 412 | return E_ZCL_FAIL; 413 | } 414 | 415 | if (eCLD_DeviceTemperatureConfigurationCreateDeviceTemperatureConfiguration( 416 | &psDeviceInfo->sClusterInstance.sDeviceTemperatureConfigurationServer, 417 | TRUE, 418 | &sCLD_DeviceTemperatureConfiguration, 419 | &psDeviceInfo->sDeviceTemperatureConfigurationServerCluster, 420 | &au8DeviceTempConfigClusterAttributeControlBits[0]) != E_ZCL_SUCCESS) { 421 | return E_ZCL_FAIL; 422 | } 423 | 424 | return eZCL_Register(&psDeviceInfo->sEndPoint); 425 | } 426 | 427 | /**************************************************************************** 428 | * 429 | * NAME: APP_ZCL_vDeviceSpecific_Init 430 | * 431 | * DESCRIPTION: 432 | * ZCL specific initialization 433 | * 434 | ****************************************************************************/ 435 | PRIVATE void APP_ZCL_vDeviceSpecific_Init(void) 436 | { 437 | memcpy(sLumiRouter.sBasicServerCluster.au8ManufacturerName, BAS_MANUF_NAME_STRING, CLD_BAS_MANUF_NAME_SIZE); 438 | memcpy(sLumiRouter.sBasicServerCluster.au8ModelIdentifier, BAS_MODEL_ID_STRING, CLD_BAS_MODEL_ID_SIZE); 439 | memcpy(sLumiRouter.sBasicServerCluster.au8DateCode, BAS_DATE_STRING, CLD_BAS_DATE_SIZE); 440 | memcpy(sLumiRouter.sBasicServerCluster.au8SWBuildID, BAS_SW_BUILD_STRING, CLD_BAS_SW_BUILD_SIZE); 441 | 442 | sLumiRouter.sDeviceTemperatureConfigurationServerCluster.i16CurrentTemperature = 0; 443 | } 444 | 445 | /****************************************************************************/ 446 | /*** END OF FILE ***/ 447 | /****************************************************************************/ 448 | -------------------------------------------------------------------------------- /Source/app_zcl_task.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: app_zcl_task.h 6 | * 7 | * DESCRIPTION: ZCL Interface 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /* Description. */ 37 | /* If you do not need this file to be parsed by doxygen then delete @file */ 38 | /****************************************************************************/ 39 | 40 | /** @file 41 | * Add brief description here. 42 | * Add more detailed description here 43 | */ 44 | 45 | /****************************************************************************/ 46 | /* Description End */ 47 | /****************************************************************************/ 48 | 49 | #ifndef APP_ZCL_TASK_H 50 | #define APP_ZCL_TASK_H 51 | 52 | /****************************************************************************/ 53 | /*** Include Files ***/ 54 | /****************************************************************************/ 55 | 56 | #include 57 | 58 | /* SDK JN-SW-4170 */ 59 | #include "Basic.h" 60 | #include "DeviceTemperatureConfiguration.h" 61 | #include "zcl.h" 62 | 63 | /****************************************************************************/ 64 | /*** Macro Definitions ***/ 65 | /****************************************************************************/ 66 | 67 | /****************************************************************************/ 68 | /*** Type Definitions ***/ 69 | /****************************************************************************/ 70 | 71 | typedef struct { 72 | tsZCL_ClusterInstance sBasicServer; 73 | tsZCL_ClusterInstance sDeviceTemperatureConfigurationServer; 74 | 75 | } APP_tsLumiRouterClusterInstances __attribute__((aligned(4))); 76 | 77 | typedef struct { 78 | tsZCL_EndPointDefinition sEndPoint; 79 | 80 | /* Cluster instances */ 81 | APP_tsLumiRouterClusterInstances sClusterInstance; 82 | 83 | /* Basic Cluster - Server */ 84 | tsCLD_Basic sBasicServerCluster; 85 | 86 | /* Device Temperature Configuration Cluster - Server */ 87 | tsCLD_DeviceTemperatureConfiguration sDeviceTemperatureConfigurationServerCluster; 88 | 89 | } APP_tsLumiRouter; 90 | 91 | /****************************************************************************/ 92 | /*** Exported Variables ***/ 93 | /****************************************************************************/ 94 | 95 | extern PUBLIC APP_tsLumiRouter sLumiRouter; 96 | 97 | /****************************************************************************/ 98 | /*** Exported Functions ***/ 99 | /****************************************************************************/ 100 | 101 | PUBLIC void APP_ZCL_vInitialise(void); 102 | PUBLIC void APP_ZCL_vEventHandler(ZPS_tsAfEvent *psStackEvent); 103 | PUBLIC void APP_cbTimerZclTick(void *pvParam); 104 | 105 | /****************************************************************************/ 106 | /*** END OF FILE ***/ 107 | /****************************************************************************/ 108 | 109 | #endif /* APP_ZCL_TASK_H */ 110 | -------------------------------------------------------------------------------- /Source/bdb_options.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: bdb_options.h 6 | * 7 | * DESCRIPTION: Configuration options 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef BDB_OPTIONS_H 36 | #define BDB_OPTIONS_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /****************************************************************************/ 45 | /*** Macro Definitions ***/ 46 | /****************************************************************************/ 47 | 48 | #if (SINGLE_CHANNEL < 11 || SINGLE_CHANNEL > 26) 49 | #define BDB_PRIMARY_CHANNEL_SET (0x02108800) 50 | #define BDB_SECONDARY_CHANNEL_SET (0x07FFF800 ^ BDB_PRIMARY_CHANNEL_SET) 51 | #define BDBC_TL_PRIMARY_CHANNEL_SET (0x02108800) 52 | #define BDBC_TL_SECONDARY_CHANNEL_SET (0x07fff800 ^ BDBC_TL_PRIMARY_CHANNEL_SET) 53 | #else 54 | #warning Single channel only! 55 | #define BDB_PRIMARY_CHANNEL_SET (1 << SINGLE_CHANNEL) 56 | #define BDB_SECONDARY_CHANNEL_SET (0) 57 | #define BDBC_TL_PRIMARY_CHANNEL_SET (0x02108800) 58 | #define BDBC_TL_SECONDARY_CHANNEL_SET (0x07fff800 ^ BDBC_TL_PRIMARY_CHANNEL_SET) 59 | #endif 60 | 61 | /* BDB Constants used by all nodes 62 | Note - Must not change for final app */ 63 | #define BDBC_MAX_SAME_NETWORK_RETRY_ATTEMPTS (10) 64 | #define BDBC_MIN_COMMISSIONING_TIME (180) 65 | #define BDBC_REC_SAME_NETWORK_RETRY_ATTEMPTS (3) 66 | #define BDBC_TC_LINK_KEY_EXCHANGE_TIMEOUT (5) 67 | 68 | /* BDB Constants used by nodes supporting touchlink 69 | Note - Must not change for final app */ 70 | #define BDBC_TL_INTERPAN_TRANS_ID_LIFETIME (8) 71 | #define BDBC_TL_MIN_STARTUP_DELAY_TIME (2) 72 | #define BDBC_TL_RX_WINDOW_DURATION (5) 73 | #define BDBC_TL_SCAN_TIME_BASE_DURATION_MS (250) 74 | 75 | /* BDB Attribute values */ 76 | #define BDB_COMMISSIONING_GROUP_ID (0xFFFF) 77 | #define BDB_COMMISSIONING_MODE (0x0F) 78 | #define BDB_COMMISSIONING_STATUS (0x00) 79 | #define BDB_JOINING_NODE_EUI64 (0x0000000000000000) 80 | #define BDB_JOIN_USES_INSTALL_CODE_KEY (FALSE) 81 | 82 | #define BDB_NODE_JOIN_LINK_KEY_TYPE (0x00) 83 | 84 | #define BDB_SCAN_DURATION (0x04) 85 | #define BDB_TC_LINK_KEY_EXCHANGE_ATTEMPTS (0x00) 86 | #define BDB_TC_LINK_KEY_EXCHANGE_ATTEMPTS_MAX (0x03) 87 | #define BDB_TC_LINK_KEY_EXCHANGE_METHOD (0x00) 88 | #define BDB_TRUST_CENTER_NODE_JOIN_TIMEOUT (0x0F) 89 | #define BDB_TRUST_CENTER_REQUIRE_KEYEXCHANGE (TRUE) 90 | #define BDB_SET_DEFAULT_TC_POLICY (FALSE) 91 | 92 | /****************************************************************************/ 93 | /*** Type Definitions ***/ 94 | /****************************************************************************/ 95 | 96 | /****************************************************************************/ 97 | /*** Exported Variables ***/ 98 | /****************************************************************************/ 99 | 100 | /****************************************************************************/ 101 | /*** Exported Functions ***/ 102 | /****************************************************************************/ 103 | 104 | /****************************************************************************/ 105 | /*** END OF FILE ***/ 106 | /****************************************************************************/ 107 | 108 | #endif /* BDB_OPTIONS_H */ 109 | -------------------------------------------------------------------------------- /Source/irq_JN516x.S: -------------------------------------------------------------------------------- 1 | ########################################################################### 2 | # Module Name: Low level interrupt handler for JN5169 FreeRTOS port 3 | # 4 | # Created By: Ian Morris 5 | # 6 | # Original Release: January 21, 2010 7 | # 8 | # Module Description: Handles interrupts required by FreeRTOS to switch 9 | # tasks, automatically using the tick timer or 10 | # manually using a system call. 11 | # 12 | ########################################################################### 13 | 14 | .globl PIC_ChannelPriorities 15 | .section .text,"ax" 16 | .align 4 17 | .type PIC_ChannelPriorities, @object 18 | .size PIC_ChannelPriorities, 16 19 | PIC_ChannelPriorities: 20 | .byte 0 # pwm1 priority 21 | .byte 0 # pwm2 priority 22 | .byte 0 # system controller priority 23 | .byte 7 # MAC priority 24 | .byte 0 # AES priority 25 | .byte 0 # PHY priority 26 | .byte 5 # uart0 priority 27 | .byte 0 # uart1 priority 28 | .byte 0 # timer0 priority 29 | .byte 0 # spi slave priority 30 | .byte 0 # i2c maste/slave priority 31 | .byte 0 # spi master priority 32 | .byte 0 # pwm4 priority 33 | .byte 0 # analog peripherals priority 34 | .byte 0 # pwm3 priority 35 | .byte 12 # tick timer priority 36 | 37 | 38 | .globl PIC_SwVectTable 39 | .section .text,"ax" 40 | .extern zps_isrMAC 41 | .extern ISR_vTickTimer 42 | .extern APP_isrUart 43 | .align 4 44 | .type PIC_SwVectTable, @object 45 | .size PIC_SwVectTable, 64 46 | PIC_SwVectTable: 47 | .word vUnclaimedInterrupt # 0 48 | .word vUnclaimedInterrupt # 1 49 | .word vUnclaimedInterrupt # 2 50 | .word vUnclaimedInterrupt # 3 51 | .word vUnclaimedInterrupt # 4 52 | .word APP_isrUart # 5 53 | .word vUnclaimedInterrupt # 6 54 | .word zps_isrMAC # 7 55 | .word vUnclaimedInterrupt # 8 56 | .word vUnclaimedInterrupt # 9 57 | .word vUnclaimedInterrupt # 10 58 | .word vUnclaimedInterrupt # 11 59 | .word ISR_vTickTimer # 12 60 | .word vUnclaimedInterrupt # 13 61 | .word vUnclaimedInterrupt # 14 62 | .word vUnclaimedInterrupt # 15 63 | -------------------------------------------------------------------------------- /Source/uart.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: uart.c 6 | * 7 | * DESCRIPTION: UART interface 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | /****************************************************************************/ 36 | /*** Include Files ***/ 37 | /****************************************************************************/ 38 | 39 | #include 40 | #include 41 | 42 | /* Application */ 43 | #include "app_main.h" 44 | #include "uart.h" 45 | 46 | /* SDK JN-SW-4170 */ 47 | #include "AppHardwareApi.h" 48 | #include "ZQueue.h" 49 | #include "dbg.h" 50 | 51 | /****************************************************************************/ 52 | /*** Macro Definitions ***/ 53 | /****************************************************************************/ 54 | 55 | #ifdef DEBUG_UART 56 | #define TRACE_UART TRUE 57 | #else 58 | #define TRACE_UART FALSE 59 | #endif 60 | 61 | #define UART E_AHI_UART_0 62 | #define UART_BAUD_RATE 115200 63 | #define UART_START_ADR 0x02003000UL 64 | 65 | /****************************************************************************/ 66 | /*** Type Definitions ***/ 67 | /****************************************************************************/ 68 | 69 | /****************************************************************************/ 70 | /*** Local Function Prototypes ***/ 71 | /****************************************************************************/ 72 | 73 | PRIVATE void UART_vSetBaudRate(uint32 u32BaudRate); 74 | 75 | /****************************************************************************/ 76 | /*** Exported Variables ***/ 77 | /****************************************************************************/ 78 | 79 | /****************************************************************************/ 80 | /*** Local Variables ***/ 81 | /****************************************************************************/ 82 | 83 | PRIVATE uint8 txbuf[16]; 84 | PRIVATE uint8 rxbuf[127]; 85 | 86 | /****************************************************************************/ 87 | /*** Exported Functions ***/ 88 | /****************************************************************************/ 89 | 90 | /**************************************************************************** 91 | * 92 | * NAME: UART_vInit 93 | * 94 | * DESCRIPTION: 95 | * Initialising UART 96 | * 97 | ****************************************************************************/ 98 | PUBLIC void UART_vInit(void) 99 | { 100 | DBG_vPrintf(TRACE_UART, "Initialising UART... "); 101 | 102 | vAHI_UartSetRTSCTS(UART, FALSE); 103 | 104 | bAHI_UartEnable(UART, txbuf, (uint8)16, rxbuf, (uint8)127); 105 | 106 | vAHI_UartReset(UART, TRUE, TRUE); 107 | vAHI_UartReset(UART, FALSE, FALSE); 108 | 109 | /* Set the clock divisor register to give required buad, this has to be done 110 | directly as the normal routines (in ROM) do not support all baud rates */ 111 | UART_vSetBaudRate(UART_BAUD_RATE); 112 | 113 | vAHI_UartSetControl(UART, FALSE, FALSE, E_AHI_UART_WORD_LEN_8, TRUE, FALSE); 114 | vAHI_UartSetInterrupt(UART, FALSE, FALSE, FALSE, TRUE, E_AHI_UART_FIFO_LEVEL_1); 115 | 116 | DBG_vPrintf(TRACE_UART, "Done\n"); 117 | } 118 | 119 | /**************************************************************************** 120 | * 121 | * NAME: APP_isrUart 122 | * 123 | * DESCRIPTION: 124 | * Handle interrupts from uart 125 | * 126 | ****************************************************************************/ 127 | PUBLIC void APP_isrUart(void) 128 | { 129 | uint32 u32ItemBitmap = ((*((volatile uint32 *)(UART_START_ADR + 0x08))) >> 1) & 0x0007; 130 | uint8 u8Byte; 131 | 132 | if (u32ItemBitmap & E_AHI_UART_INT_RXDATA) { 133 | u8Byte = u8AHI_UartReadData(UART); 134 | ZQ_bQueueSend(&APP_msgSerialRx, &u8Byte); 135 | } 136 | else if (u32ItemBitmap & E_AHI_UART_INT_TX) { 137 | if (ZQ_bQueueReceive(&APP_msgSerialTx, &u8Byte)) { 138 | UART_vSetTxInterrupt(TRUE); 139 | vAHI_UartWriteData(UART, u8Byte); 140 | } 141 | else { 142 | /* disable tx interrupt as nothing to send */ 143 | UART_vSetTxInterrupt(FALSE); 144 | } 145 | } 146 | } 147 | 148 | /**************************************************************************** 149 | * 150 | * NAME: UART_vTxChar 151 | * 152 | * DESCRIPTION: 153 | * Set UART RS-232 RTS line low to allow further data 154 | * 155 | ****************************************************************************/ 156 | PUBLIC void UART_vTxChar(uint8 u8Char) 157 | { 158 | vAHI_UartWriteData(UART, u8Char); 159 | } 160 | 161 | /**************************************************************************** 162 | * 163 | * NAME: UART_bTxReady 164 | * 165 | * DESCRIPTION: 166 | * Set UART RS-232 RTS line low to allow further data 167 | * 168 | ****************************************************************************/ 169 | PUBLIC bool_t UART_bTxReady() 170 | { 171 | return u8AHI_UartReadLineStatus(UART) & E_AHI_UART_LS_THRE; 172 | } 173 | 174 | /**************************************************************************** 175 | * 176 | * NAME: UART_vSetTxInterrupt 177 | * 178 | * DESCRIPTION: 179 | * Enable / disable the tx interrupt 180 | * 181 | ****************************************************************************/ 182 | PUBLIC void UART_vSetTxInterrupt(bool_t bState) 183 | { 184 | vAHI_UartSetInterrupt(UART, FALSE, FALSE, bState, TRUE, E_AHI_UART_FIFO_LEVEL_1); 185 | } 186 | 187 | /**************************************************************************** 188 | * 189 | * NAME: UART_vRtsStartFlow 190 | * 191 | * DESCRIPTION: 192 | * Set UART RS-232 RTS line low to allow further data 193 | * 194 | ****************************************************************************/ 195 | PUBLIC void UART_vRtsStartFlow(void) 196 | { 197 | vAHI_UartSetControl(UART, FALSE, FALSE, E_AHI_UART_WORD_LEN_8, TRUE, E_AHI_UART_RTS_LOW); 198 | } 199 | 200 | /**************************************************************************** 201 | * 202 | * NAME: UART_vRtsStopFlow 203 | * 204 | * DESCRIPTION: 205 | * Set UART RS-232 RTS line high to stop any further data coming in 206 | * 207 | ****************************************************************************/ 208 | PUBLIC void UART_vRtsStopFlow(void) 209 | { 210 | vAHI_UartSetControl(UART, FALSE, FALSE, E_AHI_UART_WORD_LEN_8, TRUE, E_AHI_UART_RTS_HIGH); 211 | } 212 | 213 | /****************************************************************************/ 214 | /*** Local Functions ***/ 215 | /****************************************************************************/ 216 | 217 | /**************************************************************************** 218 | * 219 | * NAME: UART_vSetBaudRate 220 | * 221 | * DESCRIPTION: 222 | * Set baud rates UART 223 | * 224 | ****************************************************************************/ 225 | PRIVATE void UART_vSetBaudRate(uint32 u32BaudRate) 226 | { 227 | uint16 u16Divisor = 0; 228 | uint32 u32Remainder; 229 | uint8 u8ClocksPerBit = 16; 230 | uint32 u32CalcBaudRate = 0; 231 | int32 i32BaudError = 0x7FFFFFFF; 232 | 233 | while (abs(i32BaudError) > (int32)(u32BaudRate >> 4)) { 234 | if (--u8ClocksPerBit < 3) { 235 | return; 236 | } 237 | 238 | /* Calculate Divisor register = 16MHz / (16 x baud rate) */ 239 | u16Divisor = (uint16)(16000000UL / ((u8ClocksPerBit + 1) * u32BaudRate)); 240 | 241 | /* Correct for rounding errors */ 242 | u32Remainder = (uint32)(16000000UL % ((u8ClocksPerBit + 1) * u32BaudRate)); 243 | 244 | if (u32Remainder >= (((u8ClocksPerBit + 1) * u32BaudRate) / 2)) { 245 | u16Divisor += 1; 246 | } 247 | 248 | u32CalcBaudRate = (16000000UL / ((u8ClocksPerBit + 1) * u16Divisor)); 249 | 250 | i32BaudError = (int32)u32CalcBaudRate - (int32)u32BaudRate; 251 | } 252 | 253 | /* Set the calculated clocks per bit */ 254 | vAHI_UartSetClocksPerBit(UART, u8ClocksPerBit); 255 | 256 | /* Set the calculated divisor */ 257 | vAHI_UartSetBaudDivisor(UART, u16Divisor); 258 | } 259 | 260 | /****************************************************************************/ 261 | /*** END OF FILE ***/ 262 | /****************************************************************************/ 263 | -------------------------------------------------------------------------------- /Source/uart.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: uart.h 6 | * 7 | * DESCRIPTION: UART interface 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef UART_H 36 | #define UART_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /****************************************************************************/ 45 | /*** Macro Definitions ***/ 46 | /****************************************************************************/ 47 | 48 | /****************************************************************************/ 49 | /*** Type Definitions ***/ 50 | /****************************************************************************/ 51 | 52 | /****************************************************************************/ 53 | /*** Exported Variables ***/ 54 | /****************************************************************************/ 55 | 56 | /****************************************************************************/ 57 | /*** Exported Functions ***/ 58 | /****************************************************************************/ 59 | 60 | PUBLIC void UART_vInit(void); 61 | PUBLIC void APP_isrUart(void); 62 | PUBLIC void UART_vTxChar(uint8 u8TxChar); 63 | PUBLIC bool_t UART_bTxReady(void); 64 | PUBLIC void UART_vSetTxInterrupt(bool_t bState); 65 | PUBLIC void UART_vRtsStartFlow(void); 66 | PUBLIC void UART_vRtsStopFlow(void); 67 | 68 | /****************************************************************************/ 69 | /*** END OF FILE ***/ 70 | /****************************************************************************/ 71 | 72 | #endif /* UART_H */ 73 | -------------------------------------------------------------------------------- /Source/zcl_options.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * MODULE: Lumi Router 4 | * 5 | * COMPONENT: zcl_options.h 6 | * 7 | * DESCRIPTION: Options Header for ZigBee Cluster Library functions 8 | * 9 | **************************************************************************** 10 | * 11 | * This software is owned by NXP B.V. and/or its supplier and is protected 12 | * under applicable copyright laws. All rights are reserved. We grant You, 13 | * and any third parties, a license to use this software solely and 14 | * exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179]. 15 | * You, and any third parties must reproduce the copyright and warranty notice 16 | * and any other legend of ownership on each copy or partial copy of the 17 | * software. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * Copyright NXP B.V. 2016. All rights reserved 32 | * 33 | ****************************************************************************/ 34 | 35 | #ifndef ZCL_OPTIONS_H 36 | #define ZCL_OPTIONS_H 37 | 38 | /****************************************************************************/ 39 | /*** Include Files ***/ 40 | /****************************************************************************/ 41 | 42 | #include 43 | 44 | /****************************************************************************/ 45 | /*** Macro Definitions ***/ 46 | /****************************************************************************/ 47 | 48 | /****************************************************************************/ 49 | /* ZCL Specific initialization */ 50 | /****************************************************************************/ 51 | /* This is the NXP manufacturer code.If creating new a manufacturer */ 52 | /* specific command apply to the Zigbee alliance for an Id for your company */ 53 | /* Also update the manufacturer code in .zpscfg: Node Descriptor->misc */ 54 | #define ZCL_MANUFACTURER_CODE 0x1037 55 | 56 | /* Number of endpoints supported by this device */ 57 | #define ZCL_NUMBER_OF_ENDPOINTS 1 58 | 59 | /* ZCL has all cooperative task */ 60 | #define COOPERATIVE 61 | 62 | /* Set this Tue to disable non error default responses from clusters */ 63 | #define ZCL_DISABLE_DEFAULT_RESPONSES (TRUE) 64 | #define ZCL_DISABLE_APS_ACK (TRUE) 65 | 66 | /* Which Custom commands needs to be supported */ 67 | #define ZCL_ATTRIBUTE_READ_SERVER_SUPPORTED 68 | #define ZCL_ATTRIBUTE_WRITE_SERVER_SUPPORTED 69 | 70 | /* Configuring Attribute Reporting */ 71 | #define ZCL_ATTRIBUTE_REPORTING_SERVER_SUPPORTED 72 | #define ZCL_CONFIGURE_ATTRIBUTE_REPORTING_SERVER_SUPPORTED 73 | #define ZCL_READ_ATTRIBUTE_REPORTING_CONFIGURATION_SERVER_SUPPORTED 74 | #define ZCL_SYSTEM_MIN_REPORT_INTERVAL 0 75 | #define ZCL_SYSTEM_MAX_REPORT_INTERVAL 60 76 | 77 | /* Reporting related configuration */ 78 | enum { REPORT_DEVICE_TEMPERATURE_CONFIGURATION_SLOT = 0, NUMBER_OF_REPORTS }; 79 | 80 | #define ZCL_NUMBER_OF_REPORTS NUMBER_OF_REPORTS 81 | #define MIN_REPORT_INTERVAL 60 82 | #define MAX_REPORT_INTERVAL 300 83 | 84 | #define CLD_BIND_SERVER 85 | #define MAX_NUM_BIND_QUEUE_BUFFERS ZCL_NUMBER_OF_REPORTS 86 | #define MAX_PDU_BIND_QUEUE_PAYLOAD_SIZE 24 87 | 88 | /* Enable wild card profile */ 89 | #define ZCL_ALLOW_WILD_CARD_PROFILE 90 | 91 | /****************************************************************************/ 92 | /* Enable Cluster */ 93 | /* */ 94 | /* Add the following #define's to your zcl_options.h file to enable */ 95 | /* cluster and their client or server instances */ 96 | /****************************************************************************/ 97 | #define CLD_BASIC 98 | #define BASIC_SERVER 99 | 100 | /* Fixing a build error 101 | * Due to an error in the SDK 102 | * JN-SW-4170/Components/ZCL/Devices/ZHA/Generic/Source/plug_control.c:157 103 | */ 104 | // #define CLD_DEVICE_TEMPERATURE_CONFIGURATION 105 | #define DEVICE_TEMPERATURE_CONFIGURATION_SERVER 106 | 107 | /****************************************************************************/ 108 | /* Basic Cluster - Optional Attributes */ 109 | /* */ 110 | /* Add the following #define's to your zcl_options.h file to add optional */ 111 | /* attributes to the basic cluster. */ 112 | /****************************************************************************/ 113 | #define CLD_BAS_ATTR_APPLICATION_VERSION 114 | #define CLD_BAS_ATTR_STACK_VERSION 115 | #define CLD_BAS_ATTR_HARDWARE_VERSION 116 | #define CLD_BAS_ATTR_MANUFACTURER_NAME 117 | #define CLD_BAS_ATTR_MODEL_IDENTIFIER 118 | #define CLD_BAS_ATTR_DATE_CODE 119 | #define CLD_BAS_ATTR_SW_BUILD_ID 120 | 121 | #define BAS_MANUF_NAME_STRING "NXP" 122 | #define BAS_MODEL_ID_STRING "openlumi.gw_router.jn5169" 123 | #define BAS_DATE_STRING BUILD_DATE_STRING 124 | #define BAS_SW_BUILD_STRING "1000-0001" 125 | 126 | #define CLD_BAS_APP_VERSION (1) 127 | #define CLD_BAS_STACK_VERSION (1) 128 | #define CLD_BAS_HARDWARE_VERSION (1) 129 | #define CLD_BAS_MANUF_NAME_SIZE (3) 130 | #define CLD_BAS_MODEL_ID_SIZE (25) 131 | #define CLD_BAS_DATE_SIZE (8) 132 | #define CLD_BAS_POWER_SOURCE E_CLD_BAS_PS_SINGLE_PHASE_MAINS 133 | #define CLD_BAS_SW_BUILD_SIZE (9) 134 | 135 | #define CLD_BAS_CMD_RESET_TO_FACTORY_DEFAULTS 136 | 137 | /****************************************************************************/ 138 | /* Device Temperature Configuration Cluster - Optional Attributes */ 139 | /* */ 140 | /* Add the following #define's to your zcl_options.h file to add optional */ 141 | /* attributes to the time cluster. */ 142 | /****************************************************************************/ 143 | 144 | /****************************************************************************/ 145 | /*** Type Definitions ***/ 146 | /****************************************************************************/ 147 | 148 | /****************************************************************************/ 149 | /*** Exported Variables ***/ 150 | /****************************************************************************/ 151 | 152 | /****************************************************************************/ 153 | /*** Exported Functions ***/ 154 | /****************************************************************************/ 155 | 156 | /****************************************************************************/ 157 | /*** END OF FILE ***/ 158 | /****************************************************************************/ 159 | 160 | #endif /* ZCL_OPTIONS_H */ 161 | --------------------------------------------------------------------------------