summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr
diff options
context:
space:
mode:
Diffstat (limited to 'digital/zigbit/bitcloud/stack/Components/HAL/avr')
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/Makefile274
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/gpio.h147
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAdc.h68
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAppClock.h101
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAssert.h108
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAtomic.h57
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halClkCtrl.h70
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDbg.h95
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDiagnostic.h51
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halEeprom.h87
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halFCPU.h36
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInit.h31
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInterrupt.h36
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halIrq.h99
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halPwm.h199
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleep.h73
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleepTimerClock.h146
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSpi.h180
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halUsart.h327
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halW1.h93
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halWdt.h53
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/i2c.h185
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/macros.m90152
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/calibration.c141
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/cstartup.s90250
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAdc.c163
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c124
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s79
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s9080
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halClkCtrl.c122
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halEeprom.c66
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halInit.c71
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halIrq.c119
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halPwm.c161
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleep.c297
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleepTimerClock.c304
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSpi.c142
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halUsart.c190
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s210
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s90211
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halWdtInit.c175
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/i2c.c90
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/wdt.c79
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halMacIsr.h77
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfCtrl.h127
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfPio.h43
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfSpi.h80
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halMacIsr.c152
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfCtrl.c214
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfSpi.c90
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halUid.c87
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halMacIsr.h77
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfCtrl.h142
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfPio.h41
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfSpi.h79
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halMacIsr.c152
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfCtrl.c257
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfSpi.c90
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halUid.c87
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halAtmelUid.h68
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halMacIsr.h77
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfCtrl.h142
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfPio.h41
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfSpi.h79
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halAtmelUid.c248
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halMacIsr.c152
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfCtrl.c281
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfSpi.c87
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halUid.c72
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/adc.c133
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/appTimer.c169
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/eeprom.c156
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/halTaskManager.c305
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/i2cPacket.c371
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/irq.c143
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/pwm.c165
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/resetReason.c59
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleep.c79
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleepTimer.c126
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/spi.c602
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/timer.c114
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usart.c1061
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usb.c435
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/w1.c382
84 files changed, 13084 insertions, 0 deletions
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/Makefile b/digital/zigbit/bitcloud/stack/Components/HAL/avr/Makefile
new file mode 100644
index 00000000..450e054d
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/Makefile
@@ -0,0 +1,274 @@
+HAL_PATH = ..
+include $(HAL_PATH)/Makerules
+
+######
+LIBDIR = $(STACK_LIB_PATH)
+LIB = $(LIBDIR)/lib$(HAL_LIB).a
+
+ifeq ($(HAL), ATMEGA1281)
+ BUILDDIR = $(HAL_PATH)/avr/atmega1281
+endif
+
+ifeq ($(HAL), ATMEGA2561)
+ BUILDDIR = $(HAL_PATH)/avr/atmega1281
+endif
+
+ifeq ($(HAL), ATMEGA1284)
+ BUILDDIR = $(HAL_PATH)/avr/atmega1284
+endif
+
+ifeq ($(HAL), AT90USB1287)
+ BUILDDIR = $(HAL_PATH)/avr/at90usb1287
+endif
+
+ifeq ($(HAL), ATMEGA128RFA1)
+ BUILDDIR = $(HAL_PATH)/avr/atmega128rfa1
+endif
+
+#### DEFINES FLAGS #######
+CFLAGS += -DHAL_USE_ADC
+CFLAGS += -DHAL_USE_EE_READY
+# CFLAGS += -DHAL_USE_TWI
+CFLAGS += -DHAL_USE_TIMER4_COMPA
+CFLAGS += -DHAL_USE_EXT_HANDLER
+ifneq ($(HAL), AT90USB1287)
+ CFLAGS += -DHAL_USE_SLEEP
+ CFLAGS += -DHAL_USE_TIMER2_COMPA
+ CFLAGS += -DHAL_USE_WAKEUP
+endif
+ifneq ($(HAL), ATMEGA1284)
+ CFLAGS += -DHAL_USE_PWM
+endif #ATMEGA1284
+ifeq ($(HAL), AT90USB1287)
+ CFLAGS += -DHAL_USE_USB
+endif
+ifeq ($(HAL), ATMEGA128RFA1)
+ CFLAGS += -D_HAL_HW_AES_
+ CFLAGS += -DHAL_USE_SPI
+endif
+CFLAGS += -DSLEEP_PRESCALER_1024
+ifeq ($(HAL), ATMEGA1281)
+ ifeq ($(HAL_TINY_UID), TRUE)
+ CFLAGS += -D_HAL_TINY_UID_
+ endif
+endif
+
+
+##### DECLARES USART CHANNELS TO BE USED ##########
+CFLAGS += -DHAL_USE_USART
+ifneq ($(HAL), AT90USB1287)
+ CFLAGS += -DNUM_USART_CHANNELS=2
+else
+ CFLAGS += -DNUM_USART_CHANNELS=1
+endif # AT90USB1287
+
+ifneq ($(HAL), AT90USB1287)
+ CFLAGS += -DHAL_USE_USART_CHANNEL_0
+endif # AT90USB1287
+
+CFLAGS += -DHAL_USE_USART_CHANNEL_1
+
+# Defines which USART uses hardware control pins - only one port at
+# the same time can be hardware controlled. If the macros is not declared -
+# hw control is not used.
+ifeq (, $(findstring $(HAL), ATMEGA1284 AT90USB1287 ATMEGA128RFA1))
+ #CFLAGS += -DHARDWARE_CONTROL_PINS_PORT_ASSIGNMENT=USART_CHANNEL_0
+ CFLAGS += -DHW_CONTROL_PINS_PORT_ASSIGNMENT=USART_CHANNEL_1
+endif
+
+ifeq ($(SYSTEM_TIME_ON), SLEEP_TIMER)
+ CFLAGS += -D_SYSTEM_TIME_ON_SLEEP_TIMER_
+endif
+
+##### PATHS FLAGS OF INCLUDES #########
+CFLAGS += -I$(HAL_HWD_COMMON_PATH)/include
+CFLAGS += -I$(HAL_MAC_API_PATH)/include
+CFLAGS += -I$(BUILDDIR)/../../include
+CFLAGS += -I$(SE_PATH)/include
+
+#### ASM DEFINES FLAGS #######
+ASMFLAGS = -I$(HAL_HWD_COMMON_PATH)/include
+ifeq ($(COMPILER_TYPE), GCC)
+ ASMFLAGS += -mmcu=$(CPU)
+ ASM_FILE_EXT = s
+endif
+ifeq ($(COMPILER_TYPE), IAR)
+ ASMFLAGS += -s+ # Enables case sensitivity.
+ ASMFLAGS += -w+ # Enables warnings.
+ ASMFLAGS += -D$(HAL_FREQUENCY)
+ ASMFLAGS += -M'<>'# Change asm.macro argument quote chars.
+ ASMFLAGS += -L$(HAL_PATH)/avr/list # Generate a list on: <path> /<source> <.lst>
+ ASMFLAGS += -t8 # Set tab spacing.
+ ASMFLAGS += -u_enhancedCore
+ ASMFLAGS += -D__HAS_ENHANCED_CORE__=1
+ ASMFLAGS += -D__HAS_MUL__=1
+ ASMFLAGS += -D__MEMORY_MODEL__=2
+ ifneq (, $(findstring $(HAL), ATMEGA1281 ATMEGA1284 AT90USB1287 ATMEGA128RFA1))
+ ASMFLAGS += -v3 # Select processor option: Relative jumps do not wrap.
+ ifeq ($(HAL), ATMEGA1281)
+ ASMFLAGS += -D__ATmega1281__=1
+ endif
+ endif
+ ifneq (, $(findstring $(HAL), ATMEGA2561))
+ ASMFLAGS += -v5 # Select processor option: Relative jumps do not wrap.
+ ASMFLAGS += -D__ATmega2561__=1
+ endif
+ ASMFLAGS += -D__HAS_ELPM__=1
+ ASMFLAGS += -S
+ ASM_FILE_EXT = s90
+endif
+
+# Main clock of CPU in Hz.
+ifeq ($(HAL_FREQUENCY), HAL_4MHz)
+ F_CPU=4000000
+endif
+ifeq ($(HAL_FREQUENCY), HAL_8MHz)
+ F_CPU=8000000
+endif
+
+###### LIB ##########
+LIBS = $(LIB) $(LIBDIR)/$(WDT_INIT_OBJ)
+
+ifneq ($(HAL), ATMEGA1284)
+ common_hwd += halPwm
+endif
+ifneq ($(HAL), AT90USB1287)
+ common_hwd += halSleepTimerClock
+endif
+ifeq ($(HAL), AT90USB1287)
+ common_hwd += halUsb
+endif
+ifeq ($(HAL), ATMEGA128RFA1)
+ common_hwd += halSecurityModule
+ common_hwd += halDelay
+endif
+common_hwd += halAppClock
+common_hwd += halClkCtrl
+common_hwd += halUsart
+common_hwd += halIrq
+# common_hwd += i2c
+common_hwd += halEeprom
+common_hwd += wdt
+common_hwd += halSleep
+common_hwd += halAdc
+common_hwd += halSpi
+common_hwd += calibration
+common_hwd += halInit
+
+ifneq ($(HAL), ATMEGA128RFA1)
+ personal_hwd += halRfSpi
+endif
+
+personal_hwd += halRfCtrl
+personal_hwd += halUid
+personal_hwd += halMacIsr
+
+ifeq ($(HAL), ATMEGA1281)
+ ifeq ($(HAL_TINY_UID), TRUE)
+ personal_hwd += halAtmelUid
+ endif
+endif
+
+asm_hwd += halW1
+asm_hwd += halCalibration
+ifeq ($(COMPILER_TYPE), IAR)
+ asm_hwd += cstartup
+endif
+
+ifneq ($(HAL), ATMEGA1284)
+ hwi += pwm
+endif
+ifneq ($(HAL), AT90USB1287)
+ hwi += sleepTimer
+endif
+ifeq ($(HAL), AT90USB1287)
+ hwi += usb
+endif
+hwi += appTimer
+hwi += halTaskManager
+hwi += usart
+hwi += w1
+hwi += irq
+hwi += resetReason
+# hwi += i2cPacket
+hwi += eeprom
+hwi += timer
+hwi += adc
+hwi += spi
+hwi += sleep
+
+objects_hwd = $(addsuffix .o,$(addprefix $(HAL_HWD_COMMON_PATH)/objs/,$(common_hwd)))
+objects_hwd += $(addsuffix .o,$(addprefix $(HAL_MAC_API_PATH)/objs/,$(personal_hwd)))
+sources_hwd = $(addsuffix .c,$(addprefix $(HAL_HWD_COMMON_PATH)/src/,$(common_hwd)))
+sources_hwd += $(addsuffix .c,$(addprefix $(HAL_MAC_API_PATH)/src/,$(personal_hwd)))
+objects_asm_hwd = $(addsuffix .o,$(addprefix $(HAL_HWD_COMMON_PATH)/objs/,$(asm_hwd)))
+sources_asm_hwd = $(addsuffix .$(ASM_FILE_EXT),$(addprefix $(HAL_HWD_COMMON_PATH)/src/,$(asm_hwd)))
+objects_hwi = $(addsuffix .o,$(addprefix $(BUILDDIR)/../common/objs/,$(hwi)))
+sources_hwi = $(addsuffix .c,$(addprefix $(BUILDDIR)/../common/src/,$(hwi)))
+
+###### TARGETS ################
+all: component_label WdtInit $(LIB)
+
+component_label:
+ @echo
+ @echo ----------------------------------------------------
+ @echo HAL library creation.
+ @echo ----------------------------------------------------
+
+################ common part ##############################
+$(HAL_HWD_COMMON_PATH)/objs/%.o: $(HAL_HWD_COMMON_PATH)/src/%.c
+ $(CC_MSG)
+ $(Q)$(CC) $(CFLAGS) $^ -o $@
+################ common part ##############################
+
+################ personal part ##############################
+$(HAL_MAC_API_PATH)/objs/%.o: $(HAL_MAC_API_PATH)/src/%.c
+ $(CC_MSG)
+ $(Q)$(CC) $(CFLAGS) $^ -o $@
+################ personal part ##############################
+
+################ common assembler part ######################
+$(HAL_HWD_COMMON_PATH)/objs/%.o: $(HAL_HWD_COMMON_PATH)/src/%.$(ASM_FILE_EXT)
+ $(AS_MSG)
+ $(Q)$(AS) $(ASMFLAGS) -o $@ $^
+
+ifeq ($(COMPILER_TYPE), IAR)
+gen_fcpu_header:
+ @echo "FCPU EQU $(F_CPU)" > $(HAL_HWD_COMMON_PATH)/include/halIarD.h
+endif
+
+ifeq ($(COMPILER_TYPE), GCC)
+gen_fcpu_header:
+ @echo ".equ FCPU, $(F_CPU)" > $(HAL_HWD_COMMON_PATH)/include/halGccD.h
+endif
+################ common assembler part ######################
+
+################ hwi part ###################################
+$(BUILDDIR)/../common/objs/%.o: $(BUILDDIR)/../common/src/%.c
+ $(CC_MSG)
+ $(Q)$(CC) $(CFLAGS) $^ -o $@
+################ hwi part ###################################
+
+################
+WdtInit: $(HAL_HWD_COMMON_PATH)/src/halWdtInit.c
+ $(CC_MSG)
+ $(Q)$(CC) $(CFLAGS) $(HAL_HWD_COMMON_PATH)/src/halWdtInit.c -o $(LIBDIR)/$(WDT_INIT_OBJ)
+################
+$(LIB): $(objects_hwd) gen_fcpu_header $(objects_asm_hwd) $(objects_hwi)
+ $(AR_MSG)
+ $(Q)$(AR) $(AR_KEYS) $(LIB) $(objects_hwd) $(objects_asm_hwd) $(objects_hwi)
+ $(SIZE_MSG)
+ $(Q)$(SHOW_SIZE) -td $(LIBDIR)/$(WDT_INIT_OBJ)
+ $(SIZE_MSG)
+ $(Q)$(SHOW_SIZE) -td $(LIB)
+
+# $(foreach lib_iter,$(wildcard $(LIBDIR)/lib*$(STACK_LIB)*.a), $(shell $(AR) $(AR_KEYS) $(lib_iter) $(objects_hwd) $(objects_asm_hwd) $(objects_hwi)))
+################
+clean:
+ @echo
+ @echo ----------------------------------------------------
+ @echo HAL component cleaning.
+ $(Q)rm -f $(objects_hwd) $(objects_hwi) $(LIBS) $(objects_asm_hwd)
+ $(Q)rm -f $(HAL_PATH)/lib/*.a $(HAL_PATH)/lib/*.o $(HAL_PATH)/avr/list/*.*
+ @echo HAL cleaning done!
+ @echo ----------------------------------------------------
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/gpio.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/gpio.h
new file mode 100644
index 00000000..421cc4d6
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/gpio.h
@@ -0,0 +1,147 @@
+/***************************************************************************//**
+ \file gpio.h
+
+ \brief Implementation of gpio defines.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 4/12/08 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _GPIO_H
+#define _GPIO_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+// \cond
+#include <types.h>
+// \endcond
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/******************************************************************************
+* void gpioX_set() sets GPIOX pin to logical 1 level.
+* void gpioX_clr() clears GPIOX pin to logical 0 level.
+* void gpioX_make_in makes GPIOX pin as input.
+* void gpioX_make_in makes GPIOX pin as output.
+* uint8_t gpioX_read() returns logical level GPIOX pin.
+* uint8_t gpioX_state() returns configuration of GPIOX port.
+*******************************************************************************/
+#define HAL_ASSIGN_PIN(name, port, bit) \
+INLINE void GPIO_##name##_set() {PORT##port |= (1 << bit);} \
+INLINE void GPIO_##name##_clr() {PORT##port &= ~(1 << bit);} \
+INLINE uint8_t GPIO_##name##_read() {return (PIN##port & (1 << bit)) != 0;} \
+INLINE uint8_t GPIO_##name##_state() {return (DDR##port & (1 << bit)) != 0;} \
+INLINE void GPIO_##name##_make_out() {DDR##port |= (1 << bit);} \
+INLINE void GPIO_##name##_make_in() {DDR##port &= ~(1 << bit); PORT##port &= ~(1 << bit);} \
+INLINE void GPIO_##name##_make_pullup() {PORT##port |= (1 << bit);}\
+INLINE void GPIO_##name##_toggle() {PORT##port ^= (1 << bit);}
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+// the macros for the manipulation by GPIO0
+HAL_ASSIGN_PIN(0, B, 5);
+// the macros for the manipulation by GPIO1
+HAL_ASSIGN_PIN(1, B, 6);
+// the macros for the manipulation by GPIO2
+HAL_ASSIGN_PIN(2, B, 7);
+// the macros for the manipulation by GPIO3
+HAL_ASSIGN_PIN(3, G, 0);
+// the macros for the manipulation by GPIO4
+HAL_ASSIGN_PIN(4, G, 1);
+// the macros for the manipulation by GPIO5
+HAL_ASSIGN_PIN(5, G, 2);
+// the macros for the manipulation by GPIO6
+HAL_ASSIGN_PIN(6, D, 6);
+// the macros for the manipulation by GPIO7
+HAL_ASSIGN_PIN(7, D, 7);
+// the macros for the manipulation by GPIO8
+HAL_ASSIGN_PIN(8, E, 3);
+
+// macroses only for STK500
+// the macros for the manipulation by GPIO9
+HAL_ASSIGN_PIN(9, C, 0);
+// the macros for the manipulation by GPIO10
+HAL_ASSIGN_PIN(10, C, 1);
+// the macros for the manipulation by GPIO11
+HAL_ASSIGN_PIN(11, C, 2);
+// the macros for the manipulation by GPIO12
+HAL_ASSIGN_PIN(12, C, 3);
+// the macros for the manipulation by GPIO13
+HAL_ASSIGN_PIN(13, C, 4);
+// the macros for the manipulation by GPIO14
+HAL_ASSIGN_PIN(14, C, 5);
+// the macros for the manipulation by GPIO15
+HAL_ASSIGN_PIN(15, C, 6);
+// the macros for the manipulation by GPIO16
+HAL_ASSIGN_PIN(16, C, 7);
+// macroses only for STK500
+
+// macroses only for Rcb
+// the macros for the manipulation by GPIOE2
+HAL_ASSIGN_PIN(E2, E, 2);
+// the macros for the manipulation by GPIOE3
+HAL_ASSIGN_PIN(E3, E, 3);
+// the macros for the manipulation by GPIOE4
+HAL_ASSIGN_PIN(E4, E, 4);
+// the macros for the manipulation by GPIOE5
+HAL_ASSIGN_PIN(E5, E, 5);
+// macroses only for Rcb
+
+// the macros for the manipulation by GPIO_I2C_CLK
+HAL_ASSIGN_PIN(I2C_CLK, D, 0);
+// the macros for the manipulation by GPIO_I2C_DATA
+HAL_ASSIGN_PIN(I2C_DATA, D, 1);
+// the macros for the manipulation by GPIO_USART1_TXD
+HAL_ASSIGN_PIN(USART1_TXD, D, 2);
+// the macros for the manipulation by GPIO_USART1_RXD
+HAL_ASSIGN_PIN(USART1_RXD, D, 3);
+// the macros for the manipulation by GPIO_USART1_EXTCLK
+HAL_ASSIGN_PIN(USART1_EXTCLK, D, 5);
+// the macros for the manipulation by GPIO_USART_RTS
+HAL_ASSIGN_PIN(USART_RTS, D, 4);
+// the macros for the manipulation by GPIO_USART_CTS
+HAL_ASSIGN_PIN(USART_CTS, D, 5);
+// the macros for the manipulation by GPIO_ADC_INPUT_3
+HAL_ASSIGN_PIN(ADC_INPUT_3, F, 3);
+// the macros for the manipulation by GPIO_ADC_INPUT_2
+HAL_ASSIGN_PIN(ADC_INPUT_2, F, 2);
+// the macros for the manipulation by GPIO_ADC_INPUT_1
+HAL_ASSIGN_PIN(ADC_INPUT_1, F, 1);
+// the macros for the manipulation by GPIO_BAT
+HAL_ASSIGN_PIN(BAT, F, 0);
+// the macros for the manipulation by GPIO_1WR
+HAL_ASSIGN_PIN(1WR, G, 5);
+// the macros for the manipulation by GPIO_USART_DTR
+HAL_ASSIGN_PIN(USART_DTR, E, 4);
+// the macros for the manipulation by GPIO_USART0_TXD
+HAL_ASSIGN_PIN(USART0_TXD, E, 0);
+// the macros for the manipulation by GPIO_USART0_RXD
+HAL_ASSIGN_PIN(USART0_RXD, E, 1);
+// the macros for the manipulation by GPIO_USART0_EXTCLK
+HAL_ASSIGN_PIN(USART0_EXTCLK, E, 2);
+// the macros for the manipulation by GPIO_IRQ_7
+HAL_ASSIGN_PIN(IRQ_7, E, 7);
+// the macros for the manipulation by GPIO_IRQ_6
+HAL_ASSIGN_PIN(IRQ_6, E, 6);
+
+#ifdef _HAL_USE_AMPLIFIER_
+ // the macros for the manipulation sleep power amplifier
+ HAL_ASSIGN_PIN(POW_AMPLF_SLP, C, 1);
+#endif
+
+#endif /* _GPIO_H */
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAdc.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAdc.h
new file mode 100644
index 00000000..d64f6bd2
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAdc.h
@@ -0,0 +1,68 @@
+/**************************************************************************//**
+ \file halAdc.h
+
+ \brief Declaration of hardware depended ADC interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALADC_H
+#define _HALADC_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <halTaskManager.h>
+#include <halFCPU.h>
+#include <adc.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Initializations the ADC.
+\param[in]
+ param - pointer to parameter structure
+******************************************************************************/
+void halOpenAdc(HAL_AdcParams_t *param);
+
+/**************************************************************************//**
+\brief starts convertion on the ADC channel.
+\param[in]
+ channel - channel number.
+******************************************************************************/
+void halStartAdc(uint8_t channel);
+
+/**************************************************************************//**
+\brief Closes the ADC.
+******************************************************************************/
+void halCloseAdc(void);
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/**************************************************************************//**
+\brief SIG_ADC interrupt handler signal implementation
+******************************************************************************/
+INLINE void halSigAdcInterrupt(void)
+{
+ halPostTask3(HAL_ADC);
+}
+
+#endif /* _HALADC_H */
+
+// eof halSdc.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAppClock.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAppClock.h
new file mode 100644
index 00000000..47a90fac
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAppClock.h
@@ -0,0 +1,101 @@
+/**************************************************************************//**
+ \file halAppClock.h
+
+ \brief Declarations of appTimer hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALAPPCLOCK_H
+#define _HALAPPCLOCK_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <halTaskManager.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/** \brief system timer interval in ms */
+#define HAL_APPTIMERINTERVAL 10ul
+/** \brief frequency prescaler for system timer */
+#define TIMER_FREQUENCY_PRESCALER 8
+/** \brief timer counter top value */
+#define TOP_TIMER_COUNTER_VALUE ((F_CPU/1000ul) / TIMER_FREQUENCY_PRESCALER) * HAL_APPTIMERINTERVAL
+/** \brief cpu clk / 8 */
+#define HAL_CLOCK_SELECTION_MASK (1 << CS11)
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Initialization appTimer clock.
+******************************************************************************/
+void halInitAppClock(void);
+
+/**************************************************************************//**
+\brief Synchronization system time which based on application timer.
+******************************************************************************/
+void halAppSystemTimeSynchronize(void);
+
+/**************************************************************************//**
+\brief Return time of sleep timer.
+
+\return
+ time in ms.
+******************************************************************************/
+uint32_t halGetTimeOfAppTimer(void);
+
+/**************************************************************************//**
+\brief Return system time in us
+
+\param[out]
+ mem - memory for system time
+******************************************************************************/
+void halGetSystemTimeUs(uint64_t *mem);
+
+/**************************************************************************//**
+\brief Takes account of the sleep interval.
+
+\param[in]
+ interval - time of sleep
+******************************************************************************/
+void halAdjustSleepInterval(uint32_t interval);
+
+/******************************************************************************
+ Inline static functions prototypes section.
+******************************************************************************/
+/**************************************************************************//**
+\brief Enables appTimer clock.
+******************************************************************************/
+INLINE void halStartAppClock(void)
+{
+ TCCR4B |= HAL_CLOCK_SELECTION_MASK;
+}
+
+/**************************************************************************//**
+\brief Disables appTimer clock.
+******************************************************************************/
+INLINE void halStopAppClock(void)
+{
+ TCCR4B &= ~HAL_CLOCK_SELECTION_MASK; // stop the timer
+}
+
+#endif /*_HALAPPCLOCK_H*/
+
+// eof halAppClock.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAssert.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAssert.h
new file mode 100644
index 00000000..92e0497f
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAssert.h
@@ -0,0 +1,108 @@
+/**************************************************************************//**
+ \file halAssert.h
+
+ \brief Implementation of avr assert algorithm.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 18/08/08 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALASSERT_H
+#define _HALASSERT_H
+
+#ifdef __IAR_SYSTEMS_ICC__
+#ifndef _SYSTEM_BUILD
+#pragma system_include
+#endif
+#endif
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <wdtCtrl.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define DELAY_VALUE 0x000000ul
+
+#ifdef _SYS_ASSERT_ON_
+
+#if defined(_HAL_ASSERT_INTERFACE_UART0_)
+ #define UBRRNH UBRR0H
+ #define UBRRNL UBRR0L
+ #define UCSRNA UCSR0A
+ #define UCSRNB UCSR0B
+ #define UCSRNC UCSR0C
+ #define UDRN UDR0
+#elif defined(_HAL_ASSERT_INTERFACE_UART1_)
+ #define UBRRNH UBRR1H
+ #define UBRRNL UBRR1L
+ #define UCSRNA UCSR1A
+ #define UCSRNB UCSR1B
+ #define UCSRNC UCSR1C
+ #define UDRN UDR1
+#else
+ #error " Unknown assert interface "
+#endif
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+INLINE void halAssert(uint8_t condition, uint16_t dbgCode)
+{
+ if (!condition)
+ {
+ uint32_t delay;
+
+ HAL_StopWdt();
+ asm("cli");
+ DDRB |= 0xE0;
+ /* Init UART */
+ UBRRNH = 0;
+ if (4000000ul == HAL_ReadFreq())
+ UBRRNL = 12;
+ else
+ UBRRNL = 25;
+ UCSRNA = (1 << U2X1);
+ UCSRNB = (1 << TXEN1);
+ UCSRNC = (3 << UCSZ10);
+ while(1)
+ {
+ PORTB &= ~0xE0;
+ /* Send high byte of message to UART */
+ while (!(UCSRNA & (1 << UDRE1)));
+ UDRN = (dbgCode >> 8);
+ /* Send low byte of message to UART */
+ while (!(UCSRNA & (1 << UDRE1)));
+ UDRN = dbgCode;
+ delay = DELAY_VALUE;
+ while (delay--);
+
+ PORTB |= 0xE0;
+ delay = (DELAY_VALUE / 2);
+ while(delay--);
+ }
+ }
+}
+
+#else /* _SYS_ASSERT_ON_ */
+ #define halAssert(condition, dbgCode)
+#endif /* _SYS_ASSERT_ON_ */
+
+#endif /* _HALASSERT_H */
+
+// eof halAssert.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAtomic.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAtomic.h
new file mode 100644
index 00000000..245abf6f
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halAtomic.h
@@ -0,0 +1,57 @@
+/**************************************************************************//**
+ \file halAtomic.h
+
+ \brief Implementation of atomic sections.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALATOMIC_H
+#define _HALATOMIC_H
+
+#include <types.h>
+
+typedef uint8_t atomic_t;
+
+/******************************************************************************
+Saves global interrupt bit. Disables global interrupt.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+INLINE atomic_t halStartAtomic(void)
+{
+ atomic_t result = SREG;
+ cli();
+ return result;
+}
+
+/******************************************************************************
+Restores global interrupt.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halEndAtomic(atomic_t sreg)
+{
+ SREG = sreg;
+}
+
+#endif /* _HALATOMIC_H */
+// eof atomic.h
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halClkCtrl.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halClkCtrl.h
new file mode 100644
index 00000000..6bc7a11d
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halClkCtrl.h
@@ -0,0 +1,70 @@
+/**************************************************************************//**
+ \file halClkCtrl.h
+
+ \brief Declarations of clock control hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ 16/04/09 A. Khromykh - Refactored
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALCLKCTRL_H
+#define _HALCLKCTRL_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <halFCPU.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+/**************************************************************************//**
+\brief Possible clock source
+******************************************************************************/
+typedef enum
+{
+ INTERNAL_RC,
+ OTHER_SOURCE
+} ClkSource_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Initialization system clock.
+******************************************************************************/
+void halInitFreq(void);
+
+/**************************************************************************//**
+\brief Return clock source
+
+\return
+ clock source.
+******************************************************************************/
+ClkSource_t halGetClockSource(void);
+
+/**************************************************************************//**
+\brief System clock.
+
+\return
+ system clock in Hz.
+******************************************************************************/
+uint32_t HAL_ReadFreq(void);
+
+#endif /* _HALCLKCTRL_H */
+
+// eof halClkCtrl.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDbg.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDbg.h
new file mode 100644
index 00000000..def330ba
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDbg.h
@@ -0,0 +1,95 @@
+/***************************************************************************//**
+ \file halDbg.h
+
+ \brief Declarations of hal , bsb mistake interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 09/11/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALDBG_H
+#define _HALDBG_H
+
+#include <dbg.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+enum
+{
+ APPTIMER_MISTAKE = 0x2000,
+ INCORRECT_EEPROM_ADDRESS = 0x2001,
+ MEMORY_CANNOT_WRITE = 0x2002,
+ USARTC_HALUSARTRXBUFFERFILLER_0 = 0x2003,
+ USARTC_HALSIGUSARTTRANSMISSIONCOMPLETE_0 = 0x2004,
+ USARTC_HALSIGUSARTRECEPTIONCOMPLETE_0 = 0x2005,
+ HALUSARTH_HALCLOSEUSART_0 = 0X2006,
+ HALUSARTH_HALENABLEUSARTDREMINTERRUPT_0 = 0X2007,
+ HALUSARTH_HALDISABLEUSARTDREMINTERRUPT_0 = 0X2008,
+ HALUSARTH_HALENABLEUSARTTXCINTERRUPT_0 = 0X2009,
+ HALUSARTH_HALDISABLEUSARTTXCINTERRUPT_0 = 0X200A,
+ HALUSARTH_HALENABLEUSARTRXCINTERRUPT_0 = 0X200B,
+ HALUSARTH_HALDISABLEUSARTRXCINTERRUPT_0 = 0X200C,
+ HALUSARTH_HALSENDUSARTBYTE_0 = 0X200D,
+ USARTC_HALUSARTSAVEERRORREASON = 0x200E,
+ USARTC_HALSIGUSARTERROROCCURED_0 = 0x200F,
+ USARTC_HALUNKNOWNERRORREASON_0 = 0x2010,
+
+ HAL_USART_TX_EMPTY_LIMIT = 0x2FDB,
+ HAL_USART_TRANS_COMPLETE_LIMIT = 0x2FDC,
+ HAL_USART_HW_CONTROLLER_LIMIT = 0x2FDD,
+ HAL_SLEEP_TIMER_SYNCHRONIZE_LIMIT = 0x2FDE,
+ HAL_GET_SLEEP_TIME_LIMIT = 0x2FDF,
+ HALISR_EEPROM_WRITE_TIME_LIMIT = 0x2FE0,
+ HAL_APP_TIMER_SYNCHRONIZE_LIMIT = 0x2FE1,
+ HALISR_INT5_VECT_TIME_LIMIT = 0x2FE2,
+ HALISR_ADC_TIME_LIMIT = 0x2FE3,
+ HALISR_TIMER4_COMPA_TIME_LIMIT = 0x2FE4,
+ HALATOM_SETLOWFUSES_TIME_LIMIT = 0x2FE5,
+ HALATOM_INITFREQ_TIME_LIMIT = 0x2FE6,
+ HALISR_EEPROM_READY_TIME_LIMIT = 0x2FE7,
+ HALISR_INT6_VECT_TIME_LIMIT = 0x2FE8,
+ HALISR_INT7_VECT_TIME_LIMIT = 0x2FE9,
+ HALISR_TIMER2_COMPA_TIME_LIMIT = 0x2FEA,
+ HALISR_TIMER2_OVF_TIME_LIMIT = 0x2FEB,
+ HALISR_USART0_UDR_TIME_LIMIT = 0x2FEC,
+ HALISR_USART0_TX_TIME_LIMIT = 0x2FED,
+ HALISR_USART0_RX_TIME_LIMIT = 0x2FEE,
+ HALISR_USART1_UDRE_TIME_LIMIT = 0x2FEF,
+ HALISR_USART1_TX_TIME_LIMIT = 0x2FF0,
+ HALISR_USART1_RX_TIME_LIMIT = 0x2FF1,
+ HALISR_INT4_TIME_LIMIT = 0x2FF2,
+ HALISR_TWI_TIME_LIMIT = 0x2FF3,
+ HALATOM_STARTWDT_TIME_LIMIT = 0x2FF4,
+ HALISR_WDT_TIME_LIMIT = 0x2FF5,
+ HALATOM_WRITEBYTE_RFSPI_TIME_LIMIT = 0x2FF6,
+ HALISR_TIMER3_COMPA_TIME_LIMIT = 0x2FF7,
+ HALISR_PHYDISPATCH_RFINT_TIME_LIMIT = 0x2FF8,
+ HALATOM_GETTIME_OF_APPTIMER_1_TIME_LIMIT = 0x2FF9,
+ HALATOM_GETTIME_OF_APPTIMER_2_TIME_LIMIT = 0x2FFA,
+ HALATOM_GETTIME_OF_APPTIMER_3_TIME_LIMIT = 0x2FFB,
+ HALATOM_WRITE_USART_TIME_LIMIT = 0x2FFC,
+ HALATOM_READ_USART_TIME_LIMIT = 0x2FFD,
+ HALATOM_USART_RX_COMPLETE_TIME_LIMIT = 0x2FFE,
+ HALATOM_CLEAR_TIME_CONTROL_TIME_LIMIT = 0x2FFF
+};
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+
+#endif /* _HALDBG_H */
+
+// eof halDbg.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDiagnostic.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDiagnostic.h
new file mode 100644
index 00000000..53fb0302
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halDiagnostic.h
@@ -0,0 +1,51 @@
+/**************************************************************************//**
+ \file halDiagnostic.h
+
+ \brief Implementation of diagnostics defines.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 20/05/09 D. Kasyanov - Created
+ ******************************************************************************/
+
+#ifndef _HALDIAGNOSTIC_H
+#define _HALDIAGNOSTIC_H
+
+#include <halFCPU.h>
+#include <halDbg.h>
+
+#if defined (MEASURE)
+ #define TCNT5_ACCESS_TIME 8
+ #define DEFALUT_TIME_LIMIT 100
+ #define TIMER3_COMPA_TIME_LIMIT 150
+ #define PHYDISPATCH_RFINT_TIME_LIMIT 210
+
+ #define BEGIN_MEASURE { \
+ uint16_t timeLimit = DEFALUT_TIME_LIMIT; \
+ uint16_t start = TCNT5; uint16_t offset;
+
+ #define END_MEASURE(code) offset = (TCNT5 - start - TCNT5_ACCESS_TIME) / (F_CPU/1000000ul); \
+ if (HALISR_TIMER3_COMPA_TIME_LIMIT == code) timeLimit = TIMER3_COMPA_TIME_LIMIT; \
+ if (HALISR_PHYDISPATCH_RFINT_TIME_LIMIT == code) timeLimit = PHYDISPATCH_RFINT_TIME_LIMIT; \
+ if (timeLimit != 0) { \
+ if (offset > timeLimit) { \
+ TCCR5B = 0; TCNT5 = offset; assert(0,code); \
+ } \
+ } \
+ }
+
+#else
+ #define BEGIN_MEASURE
+ #define END_MEASURE(code)
+#endif
+
+
+#endif /* _HALDIAGNOSTIC_H */
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halEeprom.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halEeprom.h
new file mode 100644
index 00000000..0ca06bfb
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halEeprom.h
@@ -0,0 +1,87 @@
+/**************************************************************************//**
+ \file halEeprom.h
+
+ \brief Provides interface for the access to hardware dependent
+ EEPROM module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALEEPROM_H
+#define _HALEEPROM_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halTaskManager.h>
+#include <eeprom.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/** \brief the mask to rise interrupt when operation on EEPROM was completed */
+#define HAL_EEPROM_WRITE_MASK_INT (1 << EEMPE | 1 << EERIE)
+#define HAL_EEPROM_WRITE_MASK (1 << EEMPE)
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Writes a byte to EEPROM.
+\param[in]
+ EECRMask - mask that define capability of interrupt after byte writing.
+\param[in]
+ address - address of byte
+\param[in]
+ data - data.
+******************************************************************************/
+void halEepromWrite(uint8_t EECRMask, uint16_t address, uint8_t data);
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/**************************************************************************//**
+\brief Waits completion of previous operation.
+******************************************************************************/
+INLINE void halWaitEepromReady(void)
+{
+ while (EECR & (1 << EEPE)); // wait for completion of previous write
+}
+
+/**************************************************************************//**
+\brief Reads byte from EEPROM.
+\param[in]
+ address -address of byte.
+\return
+ a read byte.
+******************************************************************************/
+INLINE uint8_t halReadEeprom(uint16_t address)
+{
+ EEAR = address;
+ EECR |= (1 << EERE);
+ return EEDR;
+}
+
+/**************************************************************************//**
+\brief Posts the task to taskhandler that "EEPROM ready"
+ interrupt has occured.
+******************************************************************************/
+INLINE void halSigEepromReadyInterrupt(void)
+{
+ halPostTask3(HAL_EE_READY);
+}
+#endif /*_HALEEPROM_H*/
+//eof halEeprom.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halFCPU.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halFCPU.h
new file mode 100644
index 00000000..fe961e60
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halFCPU.h
@@ -0,0 +1,36 @@
+/**************************************************************************//**
+ \file halFCPU.h
+
+ \brief Declaration F_CPU for C code.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 6/10/08 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/* Main clock of CPU in Hz. */
+#if defined(HAL_3d6864MHz)
+ #define F_CPU 3686400
+#elif defined(HAL_4MHz)
+ #define F_CPU 4000000
+#elif defined(HAL_7d3728MHz)
+ #define F_CPU 7372800
+#elif defined(HAL_8MHz)
+ #define F_CPU 8000000
+#endif
+
+// eof halFCPU.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInit.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInit.h
new file mode 100644
index 00000000..b68aac65
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInit.h
@@ -0,0 +1,31 @@
+/**************************************************************************//**
+ \file halInit.h
+
+ \brief HAL start up module interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/06/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALINIT_H
+#define _HALINIT_H
+/******************************************************************************
+ Performs start up HAL initialization.
+******************************************************************************/
+void HAL_Init(void);
+
+#endif /* _HALINIT_H */
+
+// eof halInit.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInterrupt.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInterrupt.h
new file mode 100644
index 00000000..03300064
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halInterrupt.h
@@ -0,0 +1,36 @@
+/**************************************************************************//**
+ \file halInterrupt.h
+
+ \brief Macroses to manipulate global interrupts.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALINTERRUPT_H
+#define _HALINTERRUPT_H
+
+/******************************************************************************
+Enables global interrupt.
+******************************************************************************/
+#define HAL_EnableInterrupts() sei()
+
+/******************************************************************************
+Disables global interrupt.
+******************************************************************************/
+#define HAL_DisableInterrupts() cli()
+
+#endif /* _HALINTERRUPT_H */
+// eof halInterrupt.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halIrq.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halIrq.h
new file mode 100644
index 00000000..687447a3
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halIrq.h
@@ -0,0 +1,99 @@
+/***************************************************************************//**
+ \file halIrq.h
+
+ \brief Declaration of HWD IRQ interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALIRQ_H
+#define _HALIRQ_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <halTaskManager.h>
+#include <irq.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#if defined(PLATFORM_ZIGBIT)
+ #define MAX_NUM_LINES 2
+ #define MIN_VALID_IRQ_NUMBER IRQ_6
+#else
+ #define MAX_NUM_LINES 3
+ #define MIN_VALID_IRQ_NUMBER IRQ_5
+#endif
+/** \brief number valid interrupt. */
+#define HAL_NUM_IRQ_LINES MAX_NUM_LINES
+/** \brief first valid interrupt. */
+#define HAL_FIRST_VALID_IRQ MIN_VALID_IRQ_NUMBER
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+/** \brief user's callback type. */
+typedef void (* IrqCallback_t)(void);
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Sets configuration of pins and the registers.
+\param[in]
+ irqNumber - number of interrupt.
+\param[in]
+ irqMode - mode of interrupt.
+******************************************************************************/
+void halSetIrqConfig(uint8_t irqNumber, uint8_t irqMode);
+
+/**************************************************************************//**
+\brief Clears configuration of pins and the registers.
+\param[in]
+ irqNumber - number of interrupt.
+******************************************************************************/
+void halClrIrqConfig(uint8_t irqNumber);
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/**************************************************************************//**
+\brief Enables external interrupt
+\param[in]
+ irqNumber - number of external interrupt.
+******************************************************************************/
+INLINE void halEnableIrqInterrupt(uint8_t irqNumber)
+{
+ // Enable external interrupt request
+ EIMSK |= (1 << irqNumber);
+}
+
+/**************************************************************************//**
+\brief Disables external interrupt
+\param[in]
+ irqNumber - number of external interrupt.
+******************************************************************************/
+INLINE void halDisableIrqInterrupt(uint8_t irqNumber)
+{
+ // Disable external interrupt request
+ EIMSK &= ~(1 << irqNumber);
+}
+
+#endif /* _HALIRQ_H */
+//eof halirq.h
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halPwm.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halPwm.h
new file mode 100644
index 00000000..6c026ce0
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halPwm.h
@@ -0,0 +1,199 @@
+/**************************************************************************//**
+ \file halPwm.h
+
+ \brief Declaration of hardware depended PWM interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/11/08 A. Taradov - Created
+ 5/04/11 A.Razinkov - Refactored
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALPWM_H
+#define _HALPWM_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <gpio.h>
+#include <pwm.h>
+
+/******************************************************************************
+ Definitions section
+******************************************************************************/
+/* Waveform Generation Mode bits position. PWM module independent. */
+#define WGMn0 0
+#define WGMn1 1
+#define WGMn2 3
+#define WGMn3 4
+
+/* Force Output Compare bits position. PWM module independent. */
+#define FOCnA 7
+#define FOCnB 6
+#define FOCnC 5
+
+/* Clock Select bits position. PWM module independent. */
+#define CSn0 0
+#define CSn1 1
+#define CSn2 2
+
+/* Compare Output Mode bits position. PWM module independent. */
+#define COMnA0 6
+#define COMnA1 7
+#define COMnB0 4
+#define COMnB1 5
+#define COMnC0 2
+#define COMnC1 3
+
+/* PWN unit base channel pins position. PWM module dependent. */
+#define PWM_UNIT_1_BASE_CHANNEL_PIN PB5
+#define PWM_UNIT_3_BASE_CHANNEL_PIN PE3
+
+/* Force Output Compare base bit. PWM module independent. */
+#define FOCNX_BASE_BIT FOCnA
+/* Compare Output Mode base bit number. PWM module independent. */
+#define COMNX0_BASE_BIT COMnA0
+/* Compare Output Mode bitfield size. PWM module and channel independent. */
+#define COMNX_BITFIELD_SIZE 2
+/* Compare Output Mode low bit number. PWM module independent. */
+#define COMnx0(descriptor) ((descriptor)->service.COMnx0)
+/* Compare Output Mode high bit number. PWM module independent. */
+#define COMnx1(descriptor) ((descriptor)->service.COMnx0 + 1)
+/* Output Compare Register. PWM module and channel dependent. */
+#define OCRnx(descriptor) (*((descriptor)->service.OCRnx))
+/* Data Direction Rregister. PWM module dependent. */
+#define DDRn(descriptor) (*((descriptor)->service.DDRn))
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Initializes the PWM.
+
+\param [in] pwmUnit - PWM unit number.
+ Equal to ID of Timer/Counter witch serves PWM module.
+******************************************************************************/
+void halOpenPwm(HAL_PwmUnit_t pwmUnit);
+
+/**************************************************************************//**
+\brief Starts PWM on specified channel.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halStartPwm(HAL_PwmDescriptor_t *descriptor);
+
+/**************************************************************************//**
+\brief Stops PWM on specified channel.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halStopPwm(HAL_PwmDescriptor_t *descriptor);
+
+/**************************************************************************//**
+\brief Sets base frequency of module. Common for all module channels.
+
+\param [in] pwmUnit - PWM unit number. Equal to corresponding Timer/Counter ID.
+\param [in] top - value for the TOP register.
+\param [in] prescaler - clock prescaler.
+******************************************************************************/
+void halSetPwmFrequency(HAL_PwmUnit_t pwmUnit, uint16_t top, HAL_PwmPrescaler_t prescaler);
+
+/**************************************************************************//**
+\brief Sets compare value for the PWM channel.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halSetPwmCompareValue(HAL_PwmDescriptor_t *descriptor, uint16_t cmpValue);
+
+/**************************************************************************//**
+\brief Closes the PWM.
+
+\param [in] pwmUnit - PWM unit number.
+ Equal to ID of Timer/Counter witch serves PWM module.
+******************************************************************************/
+void halClosePwm(HAL_PwmUnit_t pwmUnit);
+
+/**************************************************************************//**
+\brief Prepare PWM channel access. Determine control registers, ports, pins etc.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halPreparePwmChannelAccess(HAL_PwmDescriptor_t *descriptor);
+
+/**************************************************************************//**
+\brief Configure corresponding pin as PWM out.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+static inline void halMakeOutPwmPin(HAL_PwmDescriptor_t *descriptor)
+{
+ DDRn(descriptor) |=
+ (1 << (descriptor->service.pwmBaseChannelPin + descriptor->channel));
+}
+
+/**************************************************************************//**
+\brief Configure corresponding PWM output pin as in.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+static inline void halMakeInPwmPin(HAL_PwmDescriptor_t *descriptor)
+{
+ DDRn(descriptor) &=
+ ~(1 << (descriptor->service.pwmBaseChannelPin + descriptor->channel));
+}
+
+/**************************************************************************//**
+\brief Perform two-step writing to 16-bit registers with special access rule:
+ TCNTn, OCRnA/B/C, ICRn.
+
+\param [in] reg - register address.
+\param [in] word - word to move.
+******************************************************************************/
+static inline void halMoveWordToRegister(volatile uint16_t *reg, uint16_t word)
+{
+ATOMIC_SECTION_ENTER
+ /* High byte writing */
+ *((volatile uint8_t*)(reg) + 1) = (uint8_t)(word >> 8);
+ /* Low byte writing */
+ *(volatile uint8_t*)(reg) = (uint8_t)(word);
+ATOMIC_SECTION_LEAVE
+}
+
+/**************************************************************************//**
+\brief Perform two-step reading of 16-bit registers with special access rule:
+ TCNTn, OCRnA/B/C, ICRn.
+
+\param [in] reg - register address.
+
+\return register value
+******************************************************************************/
+static inline uint16_t halReadWordFromRegister(volatile uint16_t *reg)
+{
+ uint16_t word;
+ATOMIC_SECTION_ENTER
+ /* Low byte reading */
+ word = *(volatile uint8_t*)(reg);
+ /* High byte reading */
+ word |= ((uint16_t)(*((volatile uint8_t*)(reg) + 1)) << 8);
+ATOMIC_SECTION_LEAVE
+ return word;
+}
+
+#endif /* _HALPWM_H */
+
+// eof halPwm.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleep.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleep.h
new file mode 100644
index 00000000..c82c1a09
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleep.h
@@ -0,0 +1,73 @@
+/**************************************************************************//**
+ \file halSleep.h
+
+ \brief Interface to control sleep mode.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 1/12/09 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALSLEEP_H
+#define _HALSLEEP_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <sleep.h>
+#include <sleepTimer.h>
+
+/******************************************************************************
+ Defines section
+******************************************************************************/
+#define HAL_ACTIVE_MODE 0
+#define HAL_SLEEP_MODE 1
+#define HAL_SLEEP_TIMER_IS_STOPPED 0
+#define HAL_SLEEP_TIMER_IS_STARTED 1
+#define HAL_SLEEP_TIMER_IS_WAKEUP_SOURCE 0
+#define HAL_EXT_IRQ_IS_WAKEUP_SOURCE 1
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef struct
+{
+ HAL_WakeUpCallback_t callback;
+ HAL_SleepTimer_t sleepTimer;
+ uint8_t wakeupStation : 1;
+ uint8_t wakeupSource : 1;
+ uint8_t sleepTimerState : 1;
+} HalSleepControl_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Switch on system power.
+
+\param[in]
+ wakeupSource - wake up source
+******************************************************************************/
+void halPowerOn(const uint8_t wakeupSource);
+
+/*******************************************************************************
+ Shutdown system.
+ NOTES:
+ the application should be sure the poweroff will not be
+ interrupted after the execution of the sleep().
+*******************************************************************************/
+void halPowerOff(void);
+
+#endif /* _HALSLEEP_H */
+// eof halSleep.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleepTimerClock.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleepTimerClock.h
new file mode 100644
index 00000000..36ab9849
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSleepTimerClock.h
@@ -0,0 +1,146 @@
+/**************************************************************************//**
+ \file halSleepTimerClock.h
+
+ \brief Definition for count out requested sleep interval.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/06/07 E. Ivanov - Created
+ 7/04/09 A. Khromykh - Refactored
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALSLEEPTIMERCLOCK_H
+#define _HALSLEEPTIMERCLOCK_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <halTaskManager.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define SLEEPTIMER_CLOCK 32768lu
+
+#if defined(SLEEP_PRESCALER_1)
+ #define SLEEPTIMER_DIVIDER 1ul
+ #define SLEEPTIMER_PRESCALER (1u << CS20) // No prescaling
+#elif defined(SLEEP_PRESCALER_8)
+ #define SLEEPTIMER_DIVIDER 8ul
+ #define SLEEPTIMER_PRESCALER (1u << CS21) // clk/8
+#elif defined(SLEEP_PRESCALER_32)
+ #define SLEEPTIMER_DIVIDER 32ul
+ #define SLEEPTIMER_PRESCALER ((1u << CS20) | (1u << CS21)) // clk/32
+#elif defined(SLEEP_PRESCALER_64)
+ #define SLEEPTIMER_DIVIDER 64ul
+ #define SLEEPTIMER_PRESCALER (1u << CS22) // clk/64
+#elif defined(SLEEP_PRESCALER_128)
+ #define SLEEPTIMER_DIVIDER 128ul
+ #define SLEEPTIMER_PRESCALER ((1u << CS20) | (1u << CS22)) // clk/128
+#elif defined(SLEEP_PRESCALER_256)
+ #define SLEEPTIMER_DIVIDER 256ul
+ #define SLEEPTIMER_PRESCALER ((1u << CS21) | (1u << CS22)) // clk/256
+#elif defined(SLEEP_PRESCALER_1024)
+ #define SLEEPTIMER_DIVIDER 1024ul
+ #define SLEEPTIMER_PRESCALER ((1u << CS20) | (1u << CS21) | (1u << CS22)) // clk/1024
+#endif
+
+#define HAL_ASSR_FLAGS ((1 << TCN2UB) | (1 << OCR2AUB) | (1 << OCR2BUB) | (1 << TCR2AUB) | (1 << TCR2BUB))
+// to write some value for correct work of the asynchronous timer
+#define SOME_VALUE_FOR_SYNCHRONIZATION 0x44
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+Starts the sleep timer clock.
+******************************************************************************/
+void halStartSleepTimerClock(void);
+
+/******************************************************************************
+Stops the sleep timer clock.
+******************************************************************************/
+void halStopSleepTimerClock(void);
+
+/******************************************************************************
+Sets interval.
+Parameters:
+ value - contains number of ticks which the timer must count out.
+Returns:
+ none.
+******************************************************************************/
+void halSetSleepTimerInterval(uint32_t value);
+
+/******************************************************************************
+Returns the sleep timer frequency in Hz.
+Parameters:
+ none.
+Returns:
+ the sleep timer frequency in Hz.
+******************************************************************************/
+uint32_t halSleepTimerFrequency(void);
+
+/**************************************************************************//**
+\brief Clear timer control structure
+******************************************************************************/
+void halClearTimeControl(void);
+
+/**************************************************************************//**
+\brief Wake up procedure for all external interrupts
+******************************************************************************/
+void halWakeupFromIrq(void);
+
+/**************************************************************************//**
+\brief Get time of sleep timer.
+
+\return
+ time in ms.
+******************************************************************************/
+uint32_t halGetTimeOfSleepTimer(void);
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/******************************************************************************
+Disables the sleep timer interrupt.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halDisableSleepTimerInt(void)
+{
+ // Disables 8-bit Timer/Counter2 compare channel A and overflow interrupt
+ TIMSK2 &= (~(1 << OCIE2A) & ~(1 << TOIE2));
+}
+
+/******************************************************************************
+ Interrupt handler signal implementation
+******************************************************************************/
+INLINE void halInterruptSleepClock(void)
+{
+ halPostTask0(HAL_ASYNC_TIMER);
+}
+
+/******************************************************************************
+ Interrupt handler signal implementation
+******************************************************************************/
+INLINE void halSynchronizeSleepTime(void)
+{
+ halPostTask0(HAL_SYNC_SLEEP_TIME);
+}
+
+#endif /* _HALSLEEPTIMERCLOCK_H */
+// eof halSleepTimerClock.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSpi.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSpi.h
new file mode 100644
index 00000000..59ab72c8
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSpi.h
@@ -0,0 +1,180 @@
+/*****************************************************************************//**
+\file halSpi.h
+
+\brief Declarations of USART SPI mode.
+
+\author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+\internal
+ History:
+ 29/06/07 E. Ivanov - Created
+**********************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+#ifndef _HALSPI_H
+#define _HALSPI_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <usart.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define SPI_CHANNEL_0 USART_CHANNEL_0 // USART0 AtMega1281/2561 start addresss
+#define SPI_CHANNEL_1 USART_CHANNEL_1 // USART1 AtMega1281/2561 start addresss
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+// spi channel
+typedef UsartChannel_t SpiChannel_t;
+
+// types of the clock mode
+typedef enum
+{
+ // leading edge sample RX bit (rising), trailing edge setup TX bit (falling).
+ SPI_CLOCK_MODE0,
+ // leading edge setup TX bit (rising), trailing edge sample RX bit (falling).
+ SPI_CLOCK_MODE1,
+ // leading edge sample RX bit (falling), trailing edge setup TX bit (rising).
+ SPI_CLOCK_MODE2,
+ // leading edge setup TX bit (falling), trailing edge sample RX bit (rising).
+ SPI_CLOCK_MODE3
+} SpiClockMode_t;
+
+// clock rate
+typedef enum
+{
+ SPI_CLOCK_RATE_62 = ((F_CPU / (2 * 62500ul)) - 1),
+ SPI_CLOCK_RATE_125 = ((F_CPU / (2 * 125000ul)) - 1),
+ SPI_CLOCK_RATE_250 = ((F_CPU / (2 * 250000ul)) - 1),
+ SPI_CLOCK_RATE_500 = ((F_CPU / (2 * 500000ul)) - 1),
+ SPI_CLOCK_RATE_1000 = ((F_CPU / (2 * 1000000ul)) - 1),
+ SPI_CLOCK_RATE_2000 = ((F_CPU / (2 * 2000000ul)) - 1)
+} SpiBaudRate_t;
+
+// Data order
+typedef enum
+{
+ SPI_DATA_MSB_FIRST, // data with MSB first
+ SPI_DATA_LSB_FIRST // data with LSB first
+} SpiDataOrder_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+Disables USART channel.
+Parameters:
+ tty - spi channel.
+******************************************************************************/
+void halClearUsartSpi(SpiChannel_t tty);
+
+/******************************************************************************
+Write a length bytes to the SPI.
+Parameters:
+ tty - spi channel
+ buffer - pointer to application data buffer;
+ length - number bytes for transfer;
+Returns:
+ number of written bytes
+******************************************************************************/
+uint16_t halSyncUsartSpiWriteData(SpiChannel_t tty, uint8_t *buffer, uint16_t length);
+
+/******************************************************************************
+Write & read a length bytes to & from the SPI.
+Parameters:
+ tty - spi channel
+ buffer - pointer to application data buffer;
+ length - number bytes for transfer;
+Returns:
+ number of written & read bytes
+******************************************************************************/
+uint16_t halSyncUsartSpiReadData(SpiChannel_t tty, uint8_t *buffer, uint16_t length);
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/******************************************************************************
+Enables data register empty interrupt.
+Parameters:
+ tty - spi channel.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halEnableUsartSpiDremInterrupt(SpiChannel_t tty)
+{
+ UCSRnB(tty) |= (1 << UDRIE0);
+}
+
+/******************************************************************************
+Disables data register empty interrupt.
+Parameters:
+ tty - spi channel.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halDisableUsartSpiDremInterrupt(SpiChannel_t tty)
+{
+ UCSRnB(tty) &= ~(1 << UDRIE0);
+}
+
+/******************************************************************************
+Enables transmit complete interrupt.
+Parameters:
+ tty - spi channel.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halEnableUsartSpiTxcInterrupt(SpiChannel_t tty)
+{
+ UCSRnB(tty) |= (1 << TXCIE0);
+}
+
+/******************************************************************************
+Disables transmit complete interrupt.
+Parameters:
+ tty - spi channel.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halDisableUsartSpiTxcInterrupt(SpiChannel_t tty)
+{
+ UCSRnB(tty) &= ~(1 << TXCIE0);
+}
+
+/*****************************************************************************
+Enables receive complete interrupt.
+Parameters:
+ tty - spi channel.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halEnableUsartSpiRxcInterrupt(SpiChannel_t tty)
+{
+ UCSRnB(tty) |= (1 << RXCIE0);
+}
+
+/*****************************************************************************
+Disables receive complete interrupt.
+Parameters:
+ tty - spi channel.
+Returns:
+ none.
+******************************************************************************/
+INLINE void halDisableUsartSpiRxcInterrupt(SpiChannel_t tty)
+{
+ UCSRnB(tty) &= ~(1 << RXCIE0);
+}
+#endif
+//eof halSpi.h
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halUsart.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halUsart.h
new file mode 100644
index 00000000..ccdd5e8f
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halUsart.h
@@ -0,0 +1,327 @@
+/*****************************************************************************//**
+\file halUsart.h
+
+\brief Declarations of usart hardware-dependent module.
+
+\author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+\internal
+ History:
+ 29/05/07 E. Ivanov - Created
+**********************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HAL_USART_H
+#define _HAL_USART_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halDbg.h>
+#include <halClkCtrl.h>
+#include <halTaskManager.h>
+#include <gpio.h>
+#include <mnUtils.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/* if USART_DOUBLE_SPEED is 1 the USART uses U2Xn bit (Double speed the usart transmition).
+ if USART_DOUBLE_SPEED is 0 then U2Xn bit is not been used.
+ */
+#ifndef USART_DOUBLE_SPEED
+ #define USART_DOUBLE_SPEED 1ul
+#endif
+
+#define USART_CHANNEL_0 0xC0 // USART0 AtMega1281/2561 start address
+#define USART_CHANNEL_1 0xC8 // USART1 AtMega1281/2561 start address
+
+#if NUM_USART_CHANNELS == 2
+ #define HAL_GET_INDEX_BY_CHANNEL(channel) ((channel - USART_CHANNEL_0) >> 3)
+#else
+ #define HAL_GET_INDEX_BY_CHANNEL(channel) (channel - channel)
+#endif
+
+#define UCSRnA(tty) MMIO_BYTE(tty + 0)
+#define UCSRnB(tty) MMIO_BYTE(tty + 1)
+#define UCSRnC(tty) MMIO_BYTE(tty + 2)
+#define UBRRnL(tty) MMIO_BYTE(tty + 4)
+#define UBRRnH(tty) MMIO_BYTE(tty + 5)
+#define UBRRn(tty) MMIO_WORD(tty + 4)
+#define UDRn(tty) MMIO_BYTE(tty + 6)
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+// usart channel
+typedef uint8_t UsartChannel_t;
+
+// clock rate of usart
+typedef enum
+{
+ USART_BAUDRATE_1200 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 1200ul) - 1ul), // 1200 baud rate
+ USART_BAUDRATE_2400 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 2400ul) - 1ul), // 2400 baud rate
+ USART_BAUDRATE_4800 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 4800ul) - 1ul), // 4800 baud rate
+ USART_BAUDRATE_9600 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 9600ul) - 1ul), // 9600 baud rate
+ USART_BAUDRATE_19200 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 19200ul) - 1ul), // 19200 baud rate
+ USART_BAUDRATE_38400 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 38400ul) - 1ul), // 38400 baud rate
+ USART_BAUDRATE_115200 = (unsigned)((F_CPU * (USART_DOUBLE_SPEED + 1ul)) / (16ul * 115200ul)), // 115200 baud rate
+ USART_SYNC_BAUDRATE_1200 = (uint16_t)((F_CPU / (2ul * 1200ul)) - 1ul),
+ USART_SYNC_BAUDRATE_2400 = (uint16_t)((F_CPU / (2ul * 2400ul)) - 1ul),
+ USART_SYNC_BAUDRATE_4800 = (uint16_t)((F_CPU / (2ul * 4800ul)) - 1ul),
+ USART_SYNC_BAUDRATE_9600 = (uint16_t)((F_CPU / (2ul * 9600ul)) - 1ul),
+ USART_SYNC_BAUDRATE_38400 = (uint16_t)((F_CPU / (2ul * 38400ul)) - 1ul),
+ USART_SYNC_BAUDRATE_57600 = (uint16_t)((F_CPU / (2ul * 57600ul)) - 1ul),
+ USART_SYNC_BAUDRATE_115200 = (uint16_t)((F_CPU / (2ul * 115200ul)) - 1ul)
+} UsartBaudRate_t;
+
+// usart data length
+typedef enum
+{
+ USART_DATA5 = (0 << UCSZ12) | (0 << UCSZ11) | (0 << UCSZ10), // 5 bits data length
+ USART_DATA6 = (0 << UCSZ12) | (0 << UCSZ11) | (1 << UCSZ10), // 6 bits data length
+ USART_DATA7 = (0 << UCSZ12) | (1 << UCSZ11) | (0 << UCSZ10), // 7 bits data length
+ USART_DATA8 = (0 << UCSZ12) | (1 << UCSZ11) | (1 << UCSZ10), // 8 bits data length
+} UsartData_t;
+
+// parity mode
+typedef enum
+{
+ USART_PARITY_NONE = (0 << UPM11) | (0 << UPM10), // Non parity mode
+ USART_PARITY_EVEN = (1 << UPM11) | (0 << UPM10), // Even parity mode
+ USART_PARITY_ODD = (1 << UPM11) | (1 << UPM10) // Odd parity mode
+} UsartParity_t;
+
+// number of stop bits
+typedef enum
+{
+ USART_STOPBIT_1 = (0 << USBS1), // 1 stop bits mode
+ USART_STOPBIT_2 = (1 << USBS1) // 2 stop bits mode
+} UsartStopBits_t;
+
+// USART task IDs.
+typedef enum
+{
+ #if defined(HAL_USE_USART_CHANNEL_0)
+ HAL_USART_TASK_USART0_DRE,
+ HAL_USART_TASK_USART0_TXC,
+ HAL_USART_TASK_USART0_RXC,
+ #if defined(_USE_USART_ERROR_EVENT_)
+ HAL_USART_TASK_USART0_ERR,
+ #endif
+ #endif
+
+ #if defined(HAL_USE_USART_CHANNEL_1)
+ HAL_USART_TASK_USART1_DRE,
+ HAL_USART_TASK_USART1_TXC,
+ HAL_USART_TASK_USART1_RXC,
+ #if defined(_USE_USART_ERROR_EVENT_)
+ HAL_USART_TASK_USART1_ERR,
+ #endif
+ #endif
+
+ HAL_USART_TASKS_NUMBER
+} HalUsartTaskId_t;
+
+// Defines edge of clock to sample data.
+/*
+------------------------------------------------------------------------------------
+| | Transmitted Data Changed (Output | Received Data Sampled (Input on |
+| | of TxDn Pin) | RxDn Pin) |
+|------------|-----------------------------------|----------------------------------
+|FALLING_EDGE| Rising XCKn Edge | Falling XCKn Edge |
+|RISING_EDGE | Falling XCKn Edge | Rising XCKn Edge |
+------------------------------------------------------------------------------------
+*/
+typedef enum
+{
+ USART_EDGE_MODE_FALLING = 0,
+ USART_EDGE_MODE_RISING = 1
+} UsartEdgeMode_t;
+
+// USART synchronization mode.
+typedef enum
+{
+ USART_MODE_ASYNC = ((0 << UMSEL01) | (0 << UMSEL00)),
+ USART_MODE_SYNC = ((0 << UMSEL01) | (1 << UMSEL00))
+} UsartMode_t;
+
+// clck is output in master mode else input
+typedef enum
+{
+ USART_CLK_MODE_MASTER = 0,
+ USART_CLK_MODE_SLAVE = 1
+} UsartClkMode_t;
+
+#if defined(_USE_USART_ERROR_EVENT_)
+ // usart receiver error reason
+ typedef enum
+ {
+ FRAME_ERROR,
+ DATA_OVERRUN,
+ PARITY_ERROR
+ } UsartErrorReason_t;
+#endif
+
+// usart control
+typedef struct
+{
+ volatile uint16_t txPointOfRead;
+ volatile uint16_t txPointOfWrite;
+ volatile uint16_t rxPointOfRead;
+ volatile uint16_t rxPointOfWrite;
+ volatile uint16_t rxBytesInBuffer;
+ uint8_t usartShiftRegisterEmpty;
+#if defined(_USE_USART_ERROR_EVENT_)
+ uint8_t errorReason;
+#endif
+} HalUsartService_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Puts the byte received to the cyclic buffer.
+
+\param[in]
+ tty - channel number.
+\param[in]
+ data - data to put.
+******************************************************************************/
+void halUsartRxBufferFiller(UsartChannel_t tty, uint8_t data);
+
+/**************************************************************************//**
+\brief Checks the channel number.
+
+\param[in]
+ channel - channel to be verified.
+
+\return
+ true if channel is possible, \n
+ false otherwise.
+******************************************************************************/
+bool halIsUsartChannelCorrect(UsartChannel_t channel);
+
+#if defined(_USE_USART_ERROR_EVENT_)
+/**************************************************************************//**
+\brief Save status register for analyzing of the error reason.
+
+\param[in]
+ tty - channel number.
+\param[in]
+ status - usart status register.
+******************************************************************************/
+void halUsartSaveErrorReason(UsartChannel_t tty, uint8_t status);
+#endif
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/**************************************************************************//**
+ \brief Disables USART channel
+
+ \param tty - number of USART channel.
+ \return none.
+******************************************************************************/
+INLINE void halCloseUsart(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALCLOSEUSART_0);
+ UCSRnB(tty) = 0x00;
+}
+
+/**************************************************************************//**
+ \brief Enables data register empty interrupt
+
+ \param tty - number of USART channel.
+ \return none.
+******************************************************************************/
+INLINE void halEnableUsartDremInterrupt(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALENABLEUSARTDREMINTERRUPT_0);
+ UCSRnB(tty) |= (1 << UDRIE1);
+}
+
+/**************************************************************************//**
+ \brief Disables data register empty interrupt
+
+ \param tty - number of USART channel.
+ \return none.
+******************************************************************************/
+INLINE void halDisableUsartDremInterrupt(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALDISABLEUSARTDREMINTERRUPT_0);
+ UCSRnB(tty) &= ~(1 << UDRIE1);
+}
+
+/**************************************************************************//**
+ \brief Enables transmit complete interrupt
+
+ \param tty - number of USART channel.
+ \return none.
+******************************************************************************/
+INLINE void halEnableUsartTxcInterrupt(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALENABLEUSARTTXCINTERRUPT_0);
+ UCSRnB(tty) |= (1 << TXCIE1);
+}
+
+/**************************************************************************//**
+ \brief Disables transmit complete interrupt
+
+ \param tty - number of USART channel.
+ return none.
+******************************************************************************/
+INLINE void halDisableUsartTxcInterrupt(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALDISABLEUSARTTXCINTERRUPT_0);
+ UCSRnB(tty) &= ~(1 << TXCIE1);
+}
+
+/**************************************************************************//**
+ \brief Enables receive complete interrupt
+
+ \param tty - number of USART channel.
+ \return none.
+******************************************************************************/
+INLINE void halEnableUsartRxcInterrupt(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALENABLEUSARTRXCINTERRUPT_0);
+ UCSRnB(tty) |= (1 << RXCIE0);
+}
+
+/**************************************************************************//**
+ \brief Disables receive complete interrupt
+
+ \param tty - number of USART channel.
+ \return none.
+******************************************************************************/
+INLINE void halDisableUsartRxcInterrupt(UsartChannel_t tty)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALDISABLEUSARTRXCINTERRUPT_0);
+ UCSRnB(tty) &= ~(1 << RXCIE0);
+}
+
+/**************************************************************************//**
+ \brief Puts byte to data register of USART
+
+ \param tty - number of USART channel.
+ data - byte to send.
+ \return none.
+******************************************************************************/
+INLINE void halSendUsartByte(UsartChannel_t tty, uint8_t data)
+{
+ assert((USART_CHANNEL_0 == tty) || (USART_CHANNEL_1 == tty), HALUSARTH_HALSENDUSARTBYTE_0);
+ UCSRnA(tty) |= (1 << TXC1); // clear transmite complete flag
+ UDRn(tty) = data;
+}
+
+#endif /* _HAL_USART_H */
+//eof halUsart.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halW1.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halW1.h
new file mode 100644
index 00000000..46c66fd7
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halW1.h
@@ -0,0 +1,93 @@
+/***************************************************************************//**
+ \file halW1.h
+
+ \brief Declarations of 1-wire hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALW1_H
+#define _HALW1_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <inttypes.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+/** \brief i2c 1-wire status */
+typedef enum
+{
+ /** \brief There is no device on the bus */
+ W1_NO_DEVICE_STATUS,
+ /** \brief At least one device is on the bus */
+ W1_SUCCESS_STATUS,
+ /** \brief Invalid CRC was read during the device search operation */
+ W1_INVALID_CRC
+} W1Status_t;
+
+/***************************************************************************//**
+\brief Reads byte from the bus.
+
+\return
+ byte read from the bus.
+*******************************************************************************/
+uint8_t halReadW1(void);
+
+/***************************************************************************//**
+\brief Reads bit from the bus.
+
+\return
+ Read bit is placed to position of last significant bit.
+*******************************************************************************/
+uint8_t halReadW1Bit(void);
+
+/***************************************************************************//**
+\brief Writes bit to the bus.
+
+\param[in]
+ value - to write. The bit is placed to position of last significant bit.
+*******************************************************************************/
+void halWriteW1bit(uint8_t value);
+
+/***************************************************************************//**
+\brief Writes byte to the bus
+
+\param[in]
+ value - byte to write.
+*******************************************************************************/
+void halWriteW1(uint8_t value);
+
+/***************************************************************************//**
+\brief Resets all devices connected to the bus.
+
+\return
+ 0 - there are some devices at the bus. \n
+ 1 - there are not any devices at the bus.
+*******************************************************************************/
+uint8_t halResetW1(void);
+
+/**************************************************************************//**
+\brief Performs delay in microseconds
+
+\param[in]
+ delay - number of microseconds to be delay
+******************************************************************************/
+void __delay_us(uint8_t delay);
+
+#endif /* _HALW1_H */
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halWdt.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halWdt.h
new file mode 100644
index 00000000..bdf3ee27
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halWdt.h
@@ -0,0 +1,53 @@
+/**************************************************************************//**
+ \file halWdt.h
+
+ \brief Declarations of wdt hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 1/10/08 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALWDT_H
+#define _HALWDT_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halClkCtrl.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#if defined(__ICCAVR__)
+
+/** Enable the watch dog timer with a specific timeout value */
+#define wdt_enable(timeout) do { \
+ uint8_t volatile sreg_temp = SREG; \
+ cli(); \
+ __watchdog_reset(); \
+ WDTCSR |= (1 << WDCE) | (1 << WDE); \
+ WDTCSR = (1 << WDE) | timeout; \
+ SREG = sreg_temp; \
+} while (0)
+
+#define wdt_disable() MCUSR = 0; \
+ WDTCSR |= (1 << WDCE) | (1 << WDE); \
+ WDTCSR = 0x00;
+
+#endif
+
+#endif /* _HALWDT_H */
+
+//eof halWdt.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/i2c.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/i2c.h
new file mode 100644
index 00000000..79e6b2e6
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/i2c.h
@@ -0,0 +1,185 @@
+/***************************************************************************//**
+ \file i2c.h
+
+ \brief Declarations of i2c interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _I2C_H
+#define _I2C_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <i2cPacket.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/** \brief TWI status codes. */
+enum
+{
+ TWS_BUSERROR = 0x00,
+ TWS_START = 0x08,
+ TWS_RSTART = 0x10,
+ TWS_MT_SLA_ACK = 0x18,
+ TWS_MT_SLA_NACK = 0x20,
+ TWS_MT_DATA_ACK = 0x28,
+ TWS_MT_DATA_NACK = 0x30,
+ TWS_M_ARB_LOST = 0x38,
+ TWS_MR_SLA_ACK = 0x40,
+ TWS_MR_SLA_NACK = 0x48,
+ TWS_MR_DATA_ACK = 0x50,
+ TWS_MR_DATA_NACK = 0x58
+};
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Inits TWI module. Setup the speed of TWI.
+\param[in]
+ i2cMode - the speed of TWI.
+******************************************************************************/
+void halInitI2c(HAL_i2cMode_t *i2cMode);
+
+/**************************************************************************//**
+\brief Notification about the start condition was sent.
+******************************************************************************/
+void halSendStartDoneI2c(void);
+
+/**************************************************************************//**
+\brief Notification that byte was written to the TWI.
+\param[in]
+ result - contains result of previous operation.
+******************************************************************************/
+void halWriteDoneI2c(void);
+
+/**************************************************************************//**
+\brief Notification that byte was read from the TWI.
+\param[in]
+ data - contains byte that was read.
+******************************************************************************/
+void halReadDoneI2c(uint8_t data);
+
+/**************************************************************************//**
+\brief Notification that last byte was read from the TWI. Needs send STOP condition
+on bus.
+\param[in]
+ data - contains byte that was read.
+******************************************************************************/
+void halReadLastByteDoneI2c(uint8_t data);
+
+/**************************************************************************//**
+\brief Notification that address byte was written to the TWI and was read ACK.
+Starts reading data.
+******************************************************************************/
+void halMasterReadWriteAddressAckI2c(void);
+
+/**************************************************************************//**
+\brief Resets TWI bus and i2c HAL.
+******************************************************************************/
+void halI2cBusReset(void);
+
+/******************************************************************************
+ Inline static functions section
+******************************************************************************/
+/**************************************************************************//**
+\brief Loop for waiting for end of stop condition on bus.
+******************************************************************************/
+INLINE void halWaitEndOfStopStation(void)
+{
+ loop_until_bit_is_clear(TWCR, TWSTO);
+}
+
+/**************************************************************************//**
+\brief Enables interrupt on TWI.
+******************************************************************************/
+INLINE void halInterruptEnableI2c(void)
+{
+ TWCR |= (1 << TWIE);
+}
+
+/**************************************************************************//**
+\brief Disables interrupt on TWI.
+******************************************************************************/
+INLINE void halInterruptDisableI2c(void)
+{
+ TWCR &= (~(1 << TWIE));
+}
+
+/*************************************************************************//**
+\brief Returns byte that was read from the TWI.
+******************************************************************************/
+INLINE uint8_t halReadByteI2c(void)
+{
+ return TWDR;
+}
+
+/*************************************************************************//**
+\brief Resets the TWI.
+******************************************************************************/
+INLINE void halResetI2c(void)
+{
+ TWCR = ((1 << TWSTO) | (1 << TWINT)); // Reset TWI
+}
+
+/**************************************************************************//**
+\brief Begins writing an byte to TWI.
+\param[in]
+ data - an byte for sending.
+******************************************************************************/
+INLINE void halWriteI2c(uint8_t data)
+{
+ TWDR = data;
+ TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
+}
+
+/**************************************************************************//**
+\brief Begins read an byte from TWI.
+\param[in]
+ ack - defines the need to send ACK after an byte was recieved.
+******************************************************************************/
+INLINE void halReadI2c(bool ack)
+{
+ if (ack)
+ TWCR |= (1 << TWEA);
+ else
+ TWCR &= ~(1 << TWEA);
+
+ TWCR |= ((1 << TWINT) | (1 << TWIE) | (1 << TWEN)); // Trigger the TWI
+}
+
+/**************************************************************************//**
+\brief Directs TWI to send stop condition.
+******************************************************************************/
+INLINE void halSendStopI2c(void)
+{
+ TWCR = ((1 << TWSTO) | (1 << TWINT) | (1 << TWEN));
+}
+
+/**************************************************************************//**
+\brief Directs the TWI to send start condition.
+******************************************************************************/
+INLINE void halSendStartI2c(void)
+{
+ TWCR = ((1 << TWSTA) | (1 <<TWINT) | (1 << TWEN) | (1 << TWIE));
+}
+
+#endif /* _I2C_H*/
+
+// eof i2c.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/macros.m90 b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/macros.m90
new file mode 100644
index 00000000..57ce6f92
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/macros.m90
@@ -0,0 +1,152 @@
+;----------------------------------------------------------------------------
+;
+; MACROS.M90
+;
+; This module contains the A90/AVR C macros
+; used by cstartup.s90 and other assemble source.
+;
+; File version: $Revision: 1.8 $
+;
+;
+;----------------------------------------------------------------------------
+
+#if (((__TID__ >> 8) & 0x7F) != 90)
+#error This file should only be assembled by aa90 or aavr
+#endif
+
+#define A90_PROC_OPTION ((__TID__ >> 4) & 0x0F)
+
+/* Long or relative jumps and calls */
+#if (A90_PROC_OPTION == 0) || (A90_PROC_OPTION == 1)
+#define XCALL RCALL
+#define XJMP RJMP
+#else
+#define XCALL CALL
+#define XJMP JMP
+#endif
+
+/* Length of pointer registers (X/Y/Z) */
+#if (A90_PROC_OPTION == 0) || (A90_PROC_OPTION == 2)
+#define A90_POINTER_REG_SIZE 1
+#define A90_TINY_INDEX
+#else /*!(A90_PROC_OPTION == 0) || (A90_PROC_OPTION == 2)*/
+#if (A90_PROC_OPTION == 1) || (A90_PROC_OPTION == 3) || (A90_PROC_OPTION ==5)
+#define A90_POINTER_REG_SIZE 2
+#else /*!(A90_PROC_OPTION == 1) || (A90_PROC_OPTION == 3) || (A90_PROC_OPTION ==5)*/
+#if (A90_PROC_OPTION == 4) || (A90_PROC_OPTION == 6)
+#define A90_POINTER_REG_SIZE 3
+#define A90_EXTENDED_DATA
+#else /*!(A90_PROC_OPTION == 4) || (A90_PROC_OPTION == 6)*/
+#error Unknown processor option!!
+#endif /*!(A90_PROC_OPTION == 4) || (A90_PROC_OPTION == 6)*/
+#endif /*!(A90_PROC_OPTION == 1) || (A90_PROC_OPTION == 3) || (A90_PROC_OPTION ==5)*/
+#endif /*!(A90_PROC_OPTION == 0) || (A90_PROC_OPTION == 2)*/
+
+#if (A90_PROC_OPTION > 4)
+#define A90_LARGE_CODE
+#endif
+
+#if (A90_PROC_OPTION > 1)
+#define A90_HAS_POSSIBLE_ELPM
+#endif
+
+#ifdef A90_HAS_POSSIBLE_ELPM
+#ifdef __HAS_ELPM__
+#define A90_HAS_ELPM
+#else
+#ifndef SMALL_FLASH
+#define A90_HAS_ELPM
+#endif
+#endif
+#endif
+
+#if A90_PROC_OPTION > 1
+#define A90_24BIT_GENERIC
+#endif
+
+#if A90_PROC_OPTION < 2
+#define A90_16BIT_GENERIC
+#endif
+
+#ifdef __MEMORY_MODEL__
+
+#define TINY_MEMORY_MODEL 0
+#define SMALL_MEMORY_MODEL 1
+#define LARGE_MEMORY_MODEL 2
+
+#if __MEMORY_MODEL__ == 1
+#undef MEMORY_MODEL
+#define MEMORY_MODEL TINY_MEMORY_MODEL
+#endif
+
+#if __MEMORY_MODEL__ == 2
+#undef MEMORY_MODEL
+#define MEMORY_MODEL SMALL_MEMORY_MODEL
+#endif
+
+#if __MEMORY_MODEL__ == 3
+#undef MEMORY_MODEL
+#define MEMORY_MODEL LARGE_MEMORY_MODEL
+#endif
+
+#else
+
+#ifdef MEMORY_MODEL
+#define t 0
+#define s 1
+#define l 2
+
+#define TINY_MEMORY_MODEL 0
+#define SMALL_MEMORY_MODEL 1
+#define LARGE_MEMORY_MODEL 2
+
+#if MEMORY_MODEL == t
+#undef MEMORY_MODEL
+#define MEMORY_MODEL TINY_MEMORY_MODEL
+#endif
+
+#if MEMORY_MODEL == s
+#undef MEMORY_MODEL
+#define MEMORY_MODEL SMALL_MEMORY_MODEL
+#endif
+
+#if MEMORY_MODEL == l
+#undef MEMORY_MODEL
+#define MEMORY_MODEL LARGE_MEMORY_MODEL
+#endif
+
+#undef t
+#undef s
+#undef l
+#endif
+#endif
+
+/* Register nicknames */
+#define T0 R0
+#define T1 R1
+#define T2 R2
+#define T3 R3
+#define P0 R16
+#define P1 R17
+#define P2 R18
+#define P3 R19
+#define Q0 R20
+#define Q1 R21
+#define Q2 R22
+#define Q3 R23
+#define X0 R26
+#define X1 R27
+#define X2 R25
+#define Y0 R28
+#define Y1 R29
+#define Z0 R30
+#define Z1 R31
+#define Z2 R19
+
+/* I/O-Space Register nicknames */
+#define RAMPD 0x38
+#define RAMPX 0x39
+#define RAMPY 0x3A
+#define RAMPZ 0x3B
+#define EIND 0x3C
+#define SREG 0x3F
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/calibration.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/calibration.c
new file mode 100644
index 00000000..7d59a5e0
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/calibration.c
@@ -0,0 +1,141 @@
+/**************************************************************************//**
+ \file calibration.c
+
+ \brief the calibration of the internal RC generator.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/06/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <calibration.h>
+#include <atomic.h>
+#include <halW1.h>
+
+/******************************************************************************
+ Defines section
+******************************************************************************/
+#define INTERNAL_CLOCK F_CPU
+
+// low frequency oscillator clock for 1 cycle measurement
+#define EXTERNAL_TICKS 1
+// mcu clocks number of one procedure measurement
+#define CYCLE_LENGTH 7
+// stability crystal oscillator frequency
+#define REFERENCE_CLOCK (32768/1024)
+// Etalon mcu clock number for 1 ticks of 32 Hz asynchronous timer
+#define REFERENCE_COUNT (INTERNAL_CLOCK * EXTERNAL_TICKS) / (REFERENCE_CLOCK * CYCLE_LENGTH)
+// up direction
+#define UP_DIRECT 2
+// down direction
+#define DOWN_DIRECT 1
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+Calculates number of cycles during EXTERNAL_TICKS period.
+Parameters:
+ none
+Returns:
+ number of the cycles.
+******************************************************************************/
+uint16_t halMeasurement(void);
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Performs calibration of the internal RC generator.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void halCalibrateInternalRc(void)
+{
+ uint16_t count;
+ uint8_t cycles = 0x80;
+ uint16_t counterGate = REFERENCE_COUNT;
+ uint8_t direct = 0;
+
+ do
+ {
+ // perform clock measurement
+ count = halMeasurement();
+ if (count > REFERENCE_COUNT)
+ {
+ if ((counterGate < (count - REFERENCE_COUNT)) && (UP_DIRECT == direct))
+ { // previous measurement was more correct
+ OSCCAL--;
+ NOP;
+ break;
+ }
+ OSCCAL--;
+ NOP;
+ counterGate = count - REFERENCE_COUNT;
+ direct = DOWN_DIRECT;
+ }
+
+ if (count < REFERENCE_COUNT)
+ {
+ if ((counterGate < (REFERENCE_COUNT - count)) && (DOWN_DIRECT == direct))
+ { // previous measurement was more exactly
+ OSCCAL++;
+ NOP;
+ break;
+ }
+ OSCCAL++;
+ NOP;
+ counterGate = REFERENCE_COUNT - count;
+ direct = UP_DIRECT;
+ }
+
+ if (REFERENCE_COUNT == count)
+ break;
+
+ } while (--cycles);
+}
+
+/******************************************************************************
+Performs calibration of the main clock generator.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void HAL_CalibrateMainClock(void)
+{
+ if (INTERNAL_RC == halGetClockSource())
+ halCalibrateInternalRc();
+}
+
+/******************************************************************************
+Starts calibration after program starting or waking up from power down.
+******************************************************************************/
+void halStartingCalibrate(void)
+{
+ uint16_t i;
+
+ for (i = 0; i < 5000; i++)
+ { /* wait for 1 second start up low frequency generator*/
+ __delay_us(200); // 200 us
+ }
+ HAL_CalibrateMainClock();
+}
+// eof calibration.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/cstartup.s90 b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/cstartup.s90
new file mode 100644
index 00000000..ce20e615
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/cstartup.s90
@@ -0,0 +1,250 @@
+;----------------------------------------------------------------------------
+;
+; This module contains the AVR C and EC++ startup
+; routine and must usually be tailored to suit
+; customer's hardware.
+;
+; File version: $Revision: 1.13 $
+;
+;----------------------------------------------------------------------------
+#include <macros.m90>
+
+;----------------------------------------------------------------------------
+; Set up the INTVEC segment with a reset vector
+;----------------------------------------------------------------------------
+ MODULE ?RESET
+
+ COMMON INTVEC:CODE:ROOT(1) ; Align at an even address
+
+ EXTERN ?BC_STARTUP
+ PUBLIC __bitcloud_start
+ PUBLIC ?RESET
+
+ ORG $0
+__bitcloud_start:
+?RESET:
+ XJMP ?BC_STARTUP
+
+ ENDMOD
+
+;----------------------------------------------------------------------------
+; Forward declarations of segments used in initialization
+;----------------------------------------------------------------------------
+ RSEG CSTACK:DATA:NOROOT(0)
+ RSEG RSTACK:DATA:NOROOT(0)
+
+;----------------------------------------------------------------------------
+; Perform C initialization
+;----------------------------------------------------------------------------
+ MODULE ?BC_STARTUP
+
+ EXTERN __low_level_init
+ EXTERN __segment_init
+#ifdef _ECLIB_ECPP
+ EXTERN __call_ctors
+#endif /* _ECLIB_ECPP */
+ EXTERN main
+ EXTERN exit
+ EXTERN _exit
+ ; jump NULL handler
+ EXTERN halWdtInit
+
+;----------------------------------------------------------------------------
+; If the return address stack is located in external SRAM, make sure that
+; you have uncommented the correct code in __low_level_init!!!
+;----------------------------------------------------------------------------
+ RSEG CODE:CODE:NOROOT(1)
+ PUBLIC ?BC_STARTUP
+ PUBLIC __RESTART
+ EXTERN ?RESET
+
+__RESTART:
+?BC_STARTUP:
+ XCALL halWdtInit ; call stop wdt, read reset reason and jump NULL handler
+
+#if A90_POINTER_REG_SIZE > 2
+ PUBLIC ?zero_reg_initialization
+
+?zero_reg_initialization:
+ CLR R15
+ OUT RAMPD,R15
+#endif
+
+ REQUIRE ?SETUP_STACK
+ REQUIRE ?RESET
+
+ RSEG CODE:CODE:NOROOT(1)
+ PUBLIC __RSTACK_in_external_ram
+
+__RSTACK_in_external_ram:
+ LDI R16,0xC0
+ OUT 0x35,R16 ;Enable the external SRAM with a wait state
+
+ RSEG CODE:CODE:NOROOT(1)
+ PUBLIC __RSTACK_in_external_ram_new_way
+ EXTERN __?XMCRA
+
+__RSTACK_in_external_ram_new_way:
+ LDI R16,0x8C ;SRE=1,SRL2=0,SRL1=0,SRL0=0,SRW11=1,SRW10=1,SRW01=0,SRW00=0
+ STS __?XMCRA,R16 ;Enable the external SRAM with maximum wait state.
+
+;----------------------------------------------------------------------------
+; Set up the CSTACK and RSTACK pointers.
+;----------------------------------------------------------------------------
+ RSEG CODE:CODE:NOROOT(1)
+ ;; Fill up stacks with repeated pattern 0xCD (same pattern is used by IAR stack gauge)
+?FILL_RSTACK:
+ LDI R16, 0xCD
+ LDI X0, LOW(SFB(RSTACK))
+ LDI Y0, LOW(SFE(RSTACK))
+#if A90_POINTER_REG_SIZE > 1
+ LDI X1, HIGH(SFB(RSTACK))
+ LDI Y1, HIGH(SFE(RSTACK))
+#else
+ LDI X1, 0
+ LDI Y1, 0
+#endif
+?FILL_RSTACK_LOOP:
+ ST X+, R16
+ CP X0, Y0
+ CPC X1, Y1
+ BRNE ?FILL_RSTACK_LOOP
+
+?FILL_CSTACK:
+ LDI X0, LOW(SFB(CSTACK))
+ LDI Y0, LOW(SFE(CSTACK))
+#if A90_POINTER_REG_SIZE > 1
+ LDI X1, HIGH(SFB(CSTACK))
+ LDI Y1, HIGH(SFE(CSTACK))
+#else
+ LDI X1, 0
+ LDI Y1, 0
+#endif
+?FILL_CSTACK_LOOP:
+ ST X+, R16
+ CP X0, Y0
+ CPC X1, Y1
+ BRNE ?FILL_CSTACK_LOOP
+
+?SETUP_STACK:
+ ;; Return address stack (RSTACK)
+ LDI R16,LOW(SFE(RSTACK)-1)
+ OUT 0x3D,R16
+#if A90_POINTER_REG_SIZE > 1
+ LDI R16,HIGH(SFE(RSTACK)-1)
+ OUT 0x3E,R16
+#endif
+
+ ;; Data stack (CSTACK)
+ LDI Y0,LOW(SFE(CSTACK))
+#if A90_POINTER_REG_SIZE > 1
+#if MEMORY_MODEL == TINY_MEMORY_MODEL
+ LDI Y1,0
+#else
+ LDI Y1,HIGH(SFE(CSTACK))
+#endif
+#if A90_POINTER_REG_SIZE > 2
+ LDI Z0,HWRD(SFB(CSTACK))
+ OUT RAMPY,Z0
+#endif
+#endif
+
+#if A90_POINTER_REG_SIZE > 2
+; Nothing here, the things previously here has been done earlier.
+#else
+ REQUIRE ?call_low_level_init
+
+;----------------------------------------------------------------------------
+; Clear R15 so that it can be used as zero register by the code generator.
+; The compiler will emit a "REQUIRE ?zero_reg_initialization" statement if
+; this optimization has been enabled.
+;----------------------------------------------------------------------------
+ RSEG CODE:CODE:NOROOT(1)
+ PUBLIC ?zero_reg_initialization
+
+?zero_reg_initialization:
+ CLR R15
+
+;----------------------------------------------------------------------------
+; Call __low_level_init to do low level initializatons. Modify the supplied
+; __low_level_init module to add your own initialization code or to
+; remove segment initialization (by returning 0).
+;----------------------------------------------------------------------------
+ RSEG CODE:CODE:NOROOT(1)
+#endif
+ PUBLIC ?call_low_level_init
+
+?call_low_level_init:
+ XCALL __low_level_init
+
+ REQUIRE ?cstartup_call_main
+
+;----------------------------------------------------------------------------
+; Call __segment_init to initialize segments.
+;----------------------------------------------------------------------------
+ RSEG CODE:CODE:NOROOT(1)
+ PUBLIC ?need_segment_init
+
+?need_segment_init:
+ TST P0
+ BREQ ?skip_segment_init
+ XCALL __segment_init
+?skip_segment_init:
+
+;----------------------------------------------------------------------------
+; Call the constructors of all global objects. This code will only
+; be used if any EC++ modules defines global objects that need to
+; have its constructor called before main.
+;----------------------------------------------------------------------------
+#ifdef _ECLIB_ECPP
+ RSEG DIFUNCT:CODE:NOROOT(0)
+ RSEG CODE:CODE:NOROOT(1)
+
+ PUBLIC ?call_ctors
+
+?call_ctors:
+#ifdef __HAS_ELPM__
+ LDI P0,LOW(SFB(DIFUNCT))
+ LDI P1,LOW(SFB(DIFUNCT) >> 8)
+ LDI P2,SFB(DIFUNCT) >> 16
+
+ LDI Q0,LOW(SFE(DIFUNCT))
+ LDI Q1,LOW(SFE(DIFUNCT) >> 8)
+ LDI Q2,SFE(DIFUNCT) >> 16
+#else
+ LDI P0,LOW(SFB(DIFUNCT))
+ LDI P1,SFB(DIFUNCT) >> 8
+
+ LDI P2,LOW(SFE(DIFUNCT))
+ LDI P3,SFE(DIFUNCT) >> 8
+#endif
+
+ XCALL __call_ctors
+#endif /* _ECLIB_ECPP */
+
+;----------------------------------------------------------------------------
+; Call main
+;----------------------------------------------------------------------------
+ RSEG CODE:CODE:NOROOT(1)
+
+ PUBLIC ?cstartup_call_main
+
+?cstartup_call_main:
+ XCALL main
+ XCALL exit
+ XJMP _exit
+
+ END
+
+;----------------------------------------------------------------------------
+; $Log: cstartup.s90 $
+; Revision 1.13 2005/02/09 16:34:50Z IPEO
+; Revision 1.12 2005/02/09 12:12:46Z IPEO
+; Revision 1.11 2005/02/09 11:32:04Z IPEO
+; Revision 1.10 2005/01/26 13:56:34Z IPEO
+; Revision 1.9 2005/01/17 15:24:14Z IPEO
+; Revision 1.8 2003/11/07 16:34:04Z IPEO
+; Revision 1.7 2003/09/04 13:48:25Z IPEO
+; Revision 1.6 2003/08/22 14:09:09Z IPEO
+; Revision 1.5 2003/08/22 08:54:09Z IPEO
+; Revision 1.4 2003/08/20 08:38:55Z IPEO
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAdc.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAdc.c
new file mode 100644
index 00000000..b9e19a0b
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAdc.c
@@ -0,0 +1,163 @@
+/**************************************************************************//**
+ \file halAdc.c
+
+ \brief Implementation of hardware depended ADC interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halAdc.h>
+#include <halW1.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Defines section
+******************************************************************************/
+#define ALL_CHANNEL_MASK 0x1F
+#define CHANNEL_MASK_1 0x01
+#define CHANNEL_MASK_2 0x03
+#define CHANNEL_MASK_3 0x04
+#define CHANNEL_MASK_4 0x0C
+#define DELAY_FOR_STABILIZE 125
+
+/******************************************************************************
+ Constants section
+******************************************************************************/
+#if F_CPU == 4000000
+ PROGMEM_DECLARE(const uint8_t halAdcDivider[5]) = {2, 3, 4, 5, 6};
+#elif F_CPU == 8000000
+ PROGMEM_DECLARE(const uint8_t halAdcDivider[5]) = {3, 4, 5, 6, 7};
+#endif
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static volatile uint8_t halAdcResolution = RESOLUTION_8_BIT;
+static volatile void *halAdcDataPointer = NULL;
+static volatile uint16_t halAdcCurCount = 0;
+static volatile uint16_t halAdcMaxCount = 0;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Initializations the ADC.
+Parameters:
+ param - pointer to parameter structure
+Returns:
+ none.
+******************************************************************************/
+void halOpenAdc(HAL_AdcParams_t *param)
+{
+ halAdcMaxCount = param->selectionsAmount;
+ halAdcResolution = param->resolution;
+ halAdcDataPointer = param->bufferPointer;
+
+ /* sets voltage reference */
+ ADMUX = param->voltageReference;
+ /* Enable left adjust result */
+ if (RESOLUTION_8_BIT == halAdcResolution)
+ ADMUX |= (1 << ADLAR);
+
+ uint8_t tmp;
+ memcpy_P(&tmp, &(halAdcDivider[param->sampleRate]), 1);
+ ADCSRA = tmp | (1 << ADEN);
+}
+
+/******************************************************************************
+Starts convertion on the ADC channel.
+Parameters:
+ channel - channel number.
+Returns:
+ none.
+******************************************************************************/
+void halStartAdc(uint8_t channel)
+{
+ halAdcCurCount = 0;
+ /* disable digital buffers */
+ if (HAL_ADC_CHANNEL3 >= channel)
+ {
+ DIDR0 = (1 << channel);
+ }
+ else
+ {
+ if ((HAL_ADC_DIFF_CHANNEL0 == channel) || (HAL_ADC_DIFF_CHANNEL2 == channel))
+ DIDR0 = CHANNEL_MASK_1;
+ else if ((HAL_ADC_DIFF_CHANNEL1 == channel) || (HAL_ADC_DIFF_CHANNEL3 == channel))
+ DIDR0 = CHANNEL_MASK_2;
+ else if ((HAL_ADC_DIFF_CHANNEL4 == channel) || (HAL_ADC_DIFF_CHANNEL6 == channel))
+ DIDR0 = CHANNEL_MASK_3;
+ else if ((HAL_ADC_DIFF_CHANNEL5 == channel) || (HAL_ADC_DIFF_CHANNEL7 == channel))
+ DIDR0 = CHANNEL_MASK_4;
+ }
+
+ uint8_t tmp = ADMUX & ALL_CHANNEL_MASK;
+
+ /* clear previous channel number */
+ ADMUX &= ~ALL_CHANNEL_MASK;
+ /* set current channel number */
+ ADMUX |= channel;
+
+ /* if new differential channel is settled then must make 125 us delay for gain stabilize. */
+ if ((tmp != channel) && (HAL_ADC_CHANNEL3 < channel))
+ __delay_us(DELAY_FOR_STABILIZE);
+
+ if (halAdcMaxCount > 1)
+ ADCSRA |= ((1 << ADIE) | (1 << ADATE) | (1 << ADSC)); // Starts running mode
+ else
+ ADCSRA |= ((1 << ADIE) | (1 << ADSC)); // Starts one conversion
+}
+
+/******************************************************************************
+Closes the ADC.
+Parameters:
+ none
+Returns:
+ none
+******************************************************************************/
+void halCloseAdc(void)
+{
+ ADMUX = 0;
+ ADCSRA = 0;
+ // Digital input enable
+ DIDR0 = 0;
+}
+
+/******************************************************************************
+ADC conversion complete interrupt handler.
+******************************************************************************/
+ISR(ADC_vect)
+{
+ BEGIN_MEASURE
+ // Read ADC conversion result
+ if (RESOLUTION_8_BIT == halAdcResolution)
+ ((uint8_t *)halAdcDataPointer)[halAdcCurCount++] = ADCH;
+ else
+ ((uint16_t *)halAdcDataPointer)[halAdcCurCount++] = ADC;
+
+ if (halAdcCurCount == halAdcMaxCount)
+ {
+ // Disable ADC Interrupt
+ ADCSRA &= ~(1 << ADIE);
+ halSigAdcInterrupt();
+ }
+ END_MEASURE(HALISR_ADC_TIME_LIMIT)
+}
+// eof halAdc.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c
new file mode 100644
index 00000000..c462b896
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c
@@ -0,0 +1,124 @@
+/**************************************************************************//**
+ \file halAppClock.c
+
+ \brief Implementation of appTimer hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halAppClock.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static uint32_t halAppTime = 0ul; // time of application timer
+uint8_t halAppTimeOvfw = 0;
+static volatile uint8_t halAppIrqCount = 0;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Initialization appTimer clock.
+******************************************************************************/
+void halInitAppClock(void)
+{
+ OCR4A = TOP_TIMER_COUNTER_VALUE; // 1 millisecond timer interrupt interval.
+ TCCR4B = (1 << WGM12); // CTC mode
+ halStartAppClock(); // clock source is cpu divided by 8
+ TIMSK4 |= (1 << OCIE4A); // Enable TC4 interrupt
+}
+
+/******************************************************************************
+Return time of sleep timer.
+
+Returns:
+ time in ms.
+******************************************************************************/
+uint32_t halGetTimeOfAppTimer(void)
+{
+ halAppSystemTimeSynchronize();
+ return halAppTime;
+}
+
+/******************************************************************************
+Return system time in us
+
+Parameters:
+ mem - memory for system time
+Returns:
+ none.
+******************************************************************************/
+void halGetSystemTimeUs(uint64_t *mem)
+{
+#if (F_CPU == 4000000ul)
+ *mem = 1000ul * halAppTime + (TCNT4 << 1);
+#endif
+#if (F_CPU == 8000000ul)
+ *mem = 1000ul * halAppTime + TCNT4;
+#endif
+}
+
+/**************************************************************************//**
+\brief Takes account of the sleep interval.
+
+\param[in]
+ interval - time of sleep
+******************************************************************************/
+void halAdjustSleepInterval(uint32_t interval)
+{
+ halAppTime += interval;
+ halPostTask4(HAL_TIMER4_COMPA);
+}
+
+/**************************************************************************//**
+Synchronization system time which based on application timer.
+******************************************************************************/
+void halAppSystemTimeSynchronize(void)
+{
+ uint8_t tmpCounter;
+ uint32_t tmpValue;
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ tmpCounter = halAppIrqCount;
+ halAppIrqCount = 0;
+ END_MEASURE(HAL_APP_TIMER_SYNCHRONIZE_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ tmpValue = tmpCounter * HAL_APPTIMERINTERVAL;
+ halAppTime += tmpValue;
+ if (halAppTime < tmpValue)
+ halAppTimeOvfw++;
+}
+
+/******************************************************************************
+Interrupt handler
+******************************************************************************/
+ISR(TIMER4_COMPA_vect)
+{
+ BEGIN_MEASURE
+ halAppIrqCount++;
+ halPostTask4(HAL_TIMER4_COMPA);
+ // infinity loop spy
+ SYS_InfinityLoopMonitoring();
+ END_MEASURE(HALISR_TIMER4_COMPA_TIME_LIMIT)
+}
+// eof halAppClock.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s
new file mode 100644
index 00000000..a6332b7d
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s
@@ -0,0 +1,79 @@
+/**************************************************************************//**
+ \file halCalibration.s
+
+ \brief Implementation of measurement of mcu clock.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 1/10/08 A. Khromykh - Created
+*******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+Calculates number of cycles during EXTERNAL_TICKS period.
+Parameters:
+ none
+Returns:
+ number of the cycles (r25:r24).
+******************************************************************************/
+.global halMeasurement
+.type halMeasurement,@function
+halMeasurement:
+ push r21
+
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+
+ ; local copy cnt
+ push r28
+ push r29
+ ldi r28, 0 ; cnt = 0
+ ldi r29, 0 ; cnt = 0
+
+ ; r21 = TCNT2
+ push r23 ; save SREG to stack, to clear r23
+ lds r21, 0x00B2
+ ; while(TCNT2 == r21);
+ __l0:
+ lds r23, 0x00B2
+ cp r23, r21
+ breq __l0
+
+ ; r23++
+ inc r23
+
+ ; measurement
+ __l1:
+ adiw r28, 0x01 ; cnt ++ (2 cycle)
+ lds r21, 0x00B2 ; read TCNT2
+ cp r21, r23 ; if (TCNT2 == TCNT2old)
+ brne __l1 ;
+
+ pop r23 ; load SREG from stack
+
+ ; return cnt
+ movw r24, r28
+
+ pop r29
+ pop r28
+
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ pop r21
+ ret
+
+; eof halCalibration.s
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s90 b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s90
new file mode 100644
index 00000000..cbdb8d13
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halCalibration.s90
@@ -0,0 +1,80 @@
+/**************************************************************************//**
+ \file halCalibration.s90
+
+ \brief Implementation of measurement of mcu clock.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 1/10/08 A. Khromykh - Created
+*******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/*=============================================================
+Calculates number of cycles during EXTERNAL_TICKS period.
+Parameters:
+ none
+Returns:
+ number of the cycles (r17:r16).
+===============================================================*/
+PUBLIC halMeasurement
+RSEG CODE
+halMeasurement:
+ push r21
+
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+
+ ; local copy cnt
+ push r28
+ push r29
+ ldi r28, 0 ; cnt = 0
+ ldi r29, 0 ; cnt = 0
+
+ ; r21 = TCNT2
+ push r23 ; save SREG to stack, to clear r23
+ lds r21, 0x00B2
+ ; while(TCNT2 == r21);
+ __l0:
+ lds r23, 0x00B2
+ cp r23, r21
+ breq __l0
+
+ ; r23++
+ inc r23
+
+ ; measurement
+ __l1:
+ adiw r28, 0x01 ; cnt ++ (2 cycle)
+ lds r21, 0x00B2 ; read TCNT2
+ cp r21, r23 ; if (TCNT2 == TCNT2old)
+ brne __l1 ;
+
+ pop r23 ; load SREG from stack
+
+ ; return cnt
+ movw r16, r28
+
+ pop r29
+ pop r28
+
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ pop r21
+ ret
+
+; eof halCalibration.s90
+END
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halClkCtrl.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halClkCtrl.c
new file mode 100644
index 00000000..e6f968d1
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halClkCtrl.c
@@ -0,0 +1,122 @@
+/**************************************************************************//**
+ \file halClkCtrl.c
+
+ \brief Implementation of clock control module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ 16/04/09 A. Khromykh - Refactored
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <halRfCtrl.h>
+#include <atomic.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+// defines fuse mask for RC oscillator
+#define HAL_RC_OSCILLATOR_CLOCK 0x02
+// mask for CKSEL bits
+#define HAL_CKSEL_MASK 0x0F
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+void halStartingCalibrate(void);
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static volatile ClkSource_t clkClockSource;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief Initialization system clock.
+******************************************************************************/
+void halInitFreq(void)
+{
+ uint8_t lowFuseByte;
+
+ // wait for end of eeprom writing
+ while (EECR & (1 << EEPE));
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ lowFuseByte = SF_GET_LOW_FUSES();
+ END_MEASURE(HALATOM_SETLOWFUSES_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ if (HAL_RC_OSCILLATOR_CLOCK == (lowFuseByte & HAL_CKSEL_MASK))
+ clkClockSource = INTERNAL_RC;
+ else
+ clkClockSource = OTHER_SOURCE;
+
+ if (INTERNAL_RC == clkClockSource)
+ {
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ ASM (
+ "push r21 \n\t"
+
+ "ldi r21, 0x80 \n\t" /* CLKPR = 1 << CLKPCE */
+ "sts 0x0061, r21 \n\t" /* CLKPR = 1 << CLKPCE */
+
+#if (F_CPU == 4000000ul)
+ "ldi r21, 0x01 \n\t" /* CLKPR = 1 << CLKPS0 (1 cycle) */
+ "sts 0x0061, r21 \n\t" /* CLKPR = 1 << CLKPS0 (2 cycle) */
+#endif
+#if (F_CPU == 8000000ul)
+ "ldi r21, 0x00 \n\t" /* CLKPR = 0 (1 cycle) */
+ "sts 0x0061, r21 \n\t" /* CLKPR = 0 (2 cycle) */
+#endif
+
+ "pop r21 \n\t"
+ );
+
+ END_MEASURE(HALATOM_INITFREQ_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+ halStartingCalibrate();
+ }
+}
+
+/**************************************************************************//**
+\brief Return clock source
+
+\return
+ clock source.
+******************************************************************************/
+ClkSource_t halGetClockSource(void)
+{
+ return clkClockSource;
+}
+
+/**************************************************************************//**
+\brief System clock.
+
+\return
+ system clock in Hz.
+******************************************************************************/
+uint32_t HAL_ReadFreq(void)
+{
+ return (uint32_t)F_CPU;
+}
+
+// eof halClkCtrl.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halEeprom.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halEeprom.c
new file mode 100644
index 00000000..0dfda94e
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halEeprom.c
@@ -0,0 +1,66 @@
+/**************************************************************************//**
+ \file halEeprom.c
+
+ \brief Implementation of the hardware dependent the EEPROM module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <atomic.h>
+#include <halEeprom.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Writes a byte to EEPROM.
+Parameters:
+ EECRMask - mask that define capability of interrupt after byte writing.
+ address - address of byte
+ data - data.
+Returns:
+ none.
+******************************************************************************/
+void halEepromWrite(uint8_t EECRMask, uint16_t address, uint8_t data)
+{
+ while (EECR & (1 << EEPE)); // wait for completion of previous eeprom write
+ while (SPMCSR & (1 << SPMEN)); // wait for completion of previous program memory write
+ EEAR = address;
+ EEDR = data;
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ EECR = EECRMask;
+ EECR |= (1 << EEPE);
+ END_MEASURE(HALISR_EEPROM_WRITE_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+}
+
+/******************************************************************************
+Interrupt handler.
+******************************************************************************/
+ISR(EE_READY_vect)
+{
+ BEGIN_MEASURE
+ EECR &= ~(1 << EERIE); //disable interrupt
+ halSigEepromReadyInterrupt();
+ END_MEASURE(HALISR_EEPROM_READY_TIME_LIMIT)
+}
+
+// eof helEeprom.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halInit.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halInit.c
new file mode 100644
index 00000000..593e6257
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halInit.c
@@ -0,0 +1,71 @@
+/**************************************************************************//**
+ \file halInit.c
+
+ \brief HAL start up module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/06/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halAppClock.h>
+#include <halSleepTimerClock.h>
+#include <halRfSpi.h>
+#include <halIrq.h>
+#include <halInterrupt.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Reads uid from external devices.
+******************************************************************************/
+void halReadUid(void);
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Performs start up HAL initialization.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void HAL_Init(void)
+{
+ /* Init first diagnostic timer */
+#ifdef MEASURE
+ TCCR5B = (1 << CS50);
+#endif
+
+ HAL_InitRfSpi();
+ /* start sleep time */
+ halStartSleepTimerClock();
+ /* initialization work frequency &
+ * start calibration */
+ halInitFreq();
+ /* Reads unique ID */
+ halReadUid();
+ /* initialization and start application timer */
+ halInitAppClock();
+ /* initialization dtr interrupt */
+ halSetIrqConfig(IRQ_4, IRQ_LOW_LEVEL);
+ /* global enable interrupt*/
+ HAL_EnableInterrupts();
+}
+// eof halInit.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halIrq.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halIrq.c
new file mode 100644
index 00000000..b119fe4d
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halIrq.c
@@ -0,0 +1,119 @@
+/**************************************************************************//**
+ \file halIrq.c
+
+ \brief Implementation of HWD IRQ interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halIrq.h>
+#include <sleep.h>
+#include <halSleepTimerClock.h>
+#include <halAppClock.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+#if defined(PLATFORM_ZIGBIT)
+ IrqCallback_t IrqCallbackList[HAL_NUM_IRQ_LINES] = {NULL, NULL};
+#else
+ IrqCallback_t IrqCallbackList[HAL_NUM_IRQ_LINES] = {NULL, NULL, NULL};
+#endif
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Sets configuration of pins and the registers.
+Parameters:
+ irqNumber - number of interrupt.
+ irqMode - mode of interrupt
+Returns:
+ none.
+******************************************************************************/
+void halSetIrqConfig(uint8_t irqNumber, uint8_t irqMode)
+{
+ uint8_t ui8ShiftCount = (irqNumber - IRQ_4) << 1;
+ // IRQ pin is input
+ DDRE &= ~(1 << irqNumber);
+ PORTE |= (1 << irqNumber);
+ // Clear previous settings of corresponding interrupt sense control
+ EICRB &= ~(3 << ui8ShiftCount);
+ // Setup corresponding interrupt sence control
+ EICRB |= (irqMode & 0x03) << ui8ShiftCount;
+ // Clear the INTn interrupt flag
+ EIFR |= (1 << irqNumber);
+}
+
+/**************************************************************************//**
+\brief Clears configuration of pins and the registers.
+\param[in]
+ irqNumber - number of interrupt.
+******************************************************************************/
+void halClrIrqConfig(uint8_t irqNumber)
+{
+ uint8_t ui8ShiftCount = (irqNumber - IRQ_4) << 1;
+ DDRE &= ~(1 << irqNumber);// IRQ pin is input
+ PORTE &= ~(1 << irqNumber); // pullup off
+ EICRB &= ~(3 << ui8ShiftCount);
+}
+
+#if !defined(PLATFORM_ZIGBIT)
+/******************************************************************************
+ External interrupt 5 handler
+******************************************************************************/
+ISR(INT5_vect)
+{
+ BEGIN_MEASURE
+ halWakeupFromIrq();
+ /* user's callback */
+ if (NULL != IrqCallbackList[IRQ_5 - HAL_FIRST_VALID_IRQ])
+ IrqCallbackList[IRQ_5 - HAL_FIRST_VALID_IRQ]();
+ END_MEASURE(HALISR_INT5_VECT_TIME_LIMIT)
+}
+#endif
+
+/******************************************************************************
+ External interrupt 6 handler
+******************************************************************************/
+ISR(INT6_vect)
+{
+ BEGIN_MEASURE
+ halWakeupFromIrq();
+ /* user's callback */
+ if (NULL != IrqCallbackList[IRQ_6 - HAL_FIRST_VALID_IRQ])
+ IrqCallbackList[IRQ_6 - HAL_FIRST_VALID_IRQ]();
+ END_MEASURE(HALISR_INT6_VECT_TIME_LIMIT)
+}
+
+/******************************************************************************
+ External interrupt 7 handler
+******************************************************************************/
+ISR(INT7_vect)
+{
+ BEGIN_MEASURE
+ halWakeupFromIrq();
+ /* user's callback */
+ if (NULL != IrqCallbackList[IRQ_7 - HAL_FIRST_VALID_IRQ])
+ IrqCallbackList[IRQ_7 - HAL_FIRST_VALID_IRQ]();
+ END_MEASURE(HALISR_INT7_VECT_TIME_LIMIT)
+}
+// eof irq.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halPwm.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halPwm.c
new file mode 100644
index 00000000..d5cd8a74
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halPwm.c
@@ -0,0 +1,161 @@
+/**************************************************************************//**
+ \file halPwm.c
+
+ \brief Implementation of hardware depended PWM interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/11/08 A. Taradov - Created
+ 5/04/11 A.Razinkov - Refactored
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <halPwm.h>
+#include <atomic.h>
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+
+/**************************************************************************//**
+\brief Prepare PWM channel access. Determine control registers, ports, pins etc.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halPreparePwmChannelAccess(HAL_PwmDescriptor_t *descriptor)
+{
+ /* PWM output port and pin determination */
+ switch (descriptor->unit)
+ {
+ case PWM_UNIT_1:
+ descriptor->service.DDRn = &DDRB;
+ descriptor->service.pwmBaseChannelPin = PWM_UNIT_1_BASE_CHANNEL_PIN;
+ break;
+
+ case PWM_UNIT_3:
+ descriptor->service.DDRn = &DDRE;
+ descriptor->service.pwmBaseChannelPin = PWM_UNIT_3_BASE_CHANNEL_PIN;
+ break;
+
+ /* Invalid PWM unit identifier */
+ default:
+ break;
+ }
+ /* PWM control registers determination */
+ descriptor->service.COMnx0 = COMNX0_BASE_BIT - (descriptor->channel * COMNX_BITFIELD_SIZE);
+ descriptor->service.OCRnx = (&OCRnA(descriptor->unit) + (descriptor->channel));
+}
+
+/**************************************************************************//**
+\brief Initializes the PWM.
+
+\param [in] pwmUnit - PWM unit number.
+ Equal to ID of Timer/Counter witch serves PWM module.
+******************************************************************************/
+void halOpenPwm(HAL_PwmUnit_t pwmUnit)
+{
+ /* Clear Timer/Counter */
+ halMoveWordToRegister(&TCNTn(pwmUnit), 0x0000);
+ /* Clear Timer/Counter Input Capture register */
+ halMoveWordToRegister(&ICRn(pwmUnit), 0x0000);
+ /* Configure port for normal operation */
+ TCCRnA(pwmUnit) = 0x00;
+ /* Configure PWM mode: phase and frequency correct, TOP = ICRn */
+ TCCRnB(pwmUnit) = (1 << WGMn3);
+}
+
+/**************************************************************************//**
+\brief Starts PWM on specified channel.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halStartPwm(HAL_PwmDescriptor_t *descriptor)
+{
+ /* Force compare on channel to clear output */
+ TCCRnC(descriptor->unit) |= (1 << (FOCNX_BASE_BIT - descriptor->channel));
+ /* Configure PWM pin as output */
+ halMakeOutPwmPin(descriptor);
+ /* Configure Compare Output Mode for PWM channel */
+ TCCRnA(descriptor->unit) |=
+ ((1 << COMnx1(descriptor)) | (1 << COMnx0(descriptor)));
+ if (PWM_POLARITY_INVERTED == descriptor->polarity)
+ TCCRnA(descriptor->unit) &= ~(1 << COMnx0(descriptor));
+}
+
+/**************************************************************************//**
+\brief Stops PWM on specified channel.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halStopPwm(HAL_PwmDescriptor_t *descriptor)
+{
+ /* Clean compare register and stop Timer/Counter */
+ halMoveWordToRegister(&OCRnx(descriptor), 0x0000);
+ TCCRnA(descriptor->unit) &= ~((1 << COMnx1(descriptor)) | (1 << COMnx0(descriptor)));
+ /* Configure PWM pin as intput */
+ halMakeInPwmPin(descriptor);
+}
+
+/**************************************************************************//**
+\brief Sets base frequency of module. Common for all module channels.
+
+\param [in] pwmUnit - PWM unit number. Equal to corresponding Timer/Counter ID.
+\param [in] top - value for the TOP register.
+\param [in] prescaler - clock prescaler.
+******************************************************************************/
+void halSetPwmFrequency(HAL_PwmUnit_t pwmUnit, uint16_t top, HAL_PwmPrescaler_t prescaler)
+{
+ /* Stop Timer/Counter */
+ TCCRnB(pwmUnit) &= ~((1 << CSn2) | (1 << CSn1) | (1 << CSn0));
+ /* Set new TOP register (ICRn in our case) */
+ halMoveWordToRegister(&ICRn(pwmUnit), top);
+ /* Initialize Timer/Counter with new TOP value */
+ halMoveWordToRegister(&TCNTn(pwmUnit), top);
+ /* Clear all PWM outputs */
+ halMoveWordToRegister(&OCRnA(pwmUnit), 0x0000);
+ halMoveWordToRegister(&OCRnB(pwmUnit), 0x0000);
+ halMoveWordToRegister(&OCRnC(pwmUnit), 0x0000);
+
+ /* Run Timer/Counter */
+ TCCRnB(pwmUnit) |= prescaler;
+}
+
+/**************************************************************************//**
+\brief Sets compare value for the PWM channel.
+
+\param [in] descriptor - PWM channel descriptor.
+******************************************************************************/
+void halSetPwmCompareValue(HAL_PwmDescriptor_t *descriptor, uint16_t cmpValue)
+{
+ halMoveWordToRegister(&OCRnx(descriptor), cmpValue);
+}
+
+/**************************************************************************//**
+\brief Closes the PWM.
+
+\param [in] pwmUnit - PWM unit number.
+ Equal to ID of Timer/Counter witch serves PWM module.
+******************************************************************************/
+void halClosePwm(HAL_PwmUnit_t pwmUnit)
+{
+ /* Configure port for normal operation */
+ TCCRnA(pwmUnit) = 0x00;
+ /* Disable PWM and stop timer */
+ TCCRnB(pwmUnit) = 0x00;
+}
+
+// eof halPwm.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleep.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleep.c
new file mode 100644
index 00000000..305dfd39
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleep.c
@@ -0,0 +1,297 @@
+/**************************************************************************//**
+ \file halSleep.c
+
+ \brief Implementation of sleep modes.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 1/12/09 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <sleepTimer.h>
+#include <halSleepTimerClock.h>
+#include <halRfPio.h>
+#include <halRfCtrl.h>
+#include <halSleep.h>
+#include <halIrq.h>
+#include <halDbg.h>
+#include <halAppClock.h>
+#include <halEeprom.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Performs calibration of the main clock generator.
+******************************************************************************/
+void halStartingCalibrate(void);
+
+#ifdef _HAL_RF_RX_TX_INDICATOR_
+/**************************************************************************//**
+\brief Turn on pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
+the radio transceiver.
+******************************************************************************/
+void phyRxTxSwitcherOn(void);
+
+/**************************************************************************//**
+\brief Turn off pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
+the radio transceiver.
+******************************************************************************/
+void phyRxTxSwitcherOff(void);
+
+#endif
+
+#ifdef _HAL_ANT_DIVERSITY_
+/**************************************************************************//**
+\brief Turn on pin 9 (DIG1) and pin 10 (DIG2) to enable antenna select.
+******************************************************************************/
+void phyAntennaSwitcherOn(void);
+
+/**************************************************************************//**
+\brief Turn off pin 9 (DIG1) and pin 10 (DIG2) to disable antenna select.
+******************************************************************************/
+void phyAntennaSwitcherOff(void);
+
+#endif // _HAL_ANT_DIVERSITY_
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern volatile bool halEnableDtrWakeUp;
+extern HalSleepControl_t halSleepControl;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static uint32_t halTimeStartOfSleep = 0ul;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief Switch on system power.
+
+\param[in]
+ wakeupSource - wake up source
+******************************************************************************/
+void halPowerOn(const uint8_t wakeupSource)
+{
+ halSleepControl.wakeupStation = HAL_ACTIVE_MODE;
+ halSleepControl.wakeupSource = wakeupSource;
+
+ if (INTERNAL_RC == halGetClockSource())
+ {
+ GPIO_RF_SLP_TR_clr();
+ }
+ else
+ {
+ GPIO_RF_SLP_TR_make_in();
+ TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0)); // no compare
+ while (ASSR & HAL_ASSR_FLAGS);
+ }
+ GPIO_RF_SLP_TR_make_out();
+
+ #ifdef _HAL_USE_AMPLIFIER_
+ // set one on pin. Enable power amplifier.
+ GPIO_POW_AMPLF_SLP_set();
+ #endif
+
+ halPostTask4(HAL_WAKEUP);
+}
+
+/******************************************************************************
+Shutdowns system.
+ NOTES:
+ the application should be sure the poweroff will not be
+ interrupted after the execution of the sleep().
+******************************************************************************/
+void halPowerOff(void)
+{
+ if (HAL_ACTIVE_MODE == halSleepControl.wakeupStation)
+ return; // it is a too late to sleep.
+
+ // stop application timer clock
+ halStopAppClock(); // will be shutdown
+ if (0ul == halTimeStartOfSleep)
+ { // start of sleep procedure
+ // save time of stopping of the application timer
+ halTimeStartOfSleep = halGetTimeOfSleepTimer();
+ }
+
+ #ifdef _HAL_USE_AMPLIFIER_
+ // set zero on pin. Disable power amplifier.
+ GPIO_POW_AMPLF_SLP_clr();
+ #endif
+
+ #ifdef _HAL_RF_RX_TX_INDICATOR_
+
+ // disable front end driver if that is supported
+ phyRxTxSwitcherOff();
+
+ #endif
+
+ #ifdef _HAL_ANT_DIVERSITY_
+
+ // disable antenna diversity switcher
+ phyAntennaSwitcherOff();
+
+ #endif
+
+ if (halEnableDtrWakeUp)
+ { /* enable DTR (irq 4) wake up */
+ halEnableIrqInterrupt(IRQ_4);
+ } /* enable DTR (irq 4) wake up */
+
+ // wait for end of eeprom writing
+ halWaitEepromReady();
+
+ if (INTERNAL_RC == halGetClockSource())
+ {
+ GPIO_RF_SLP_TR_set();
+ GPIO_RF_SLP_TR_make_out();
+
+ if (HAL_SLEEP_TIMER_IS_STARTED == halSleepControl.sleepTimerState)
+ { // sleep timer is started
+ SMCR = (1 << SM1) | (1 << SM0) | (1 << SE); // power-save
+ __SLEEP;
+ SMCR = 0;
+ }
+ else
+ {
+ halStopSleepTimerClock();
+ SMCR = (1 << SM1) | (1 << SE); // power-down
+ __SLEEP;
+ SMCR = 0;
+ halStartSleepTimerClock();
+ halStartingCalibrate();
+ }
+ }
+ else
+ {
+ uint8_t timsk4 = TIMSK4;
+ uint8_t twcr = TWCR;
+ uint8_t adcsra = ADCSRA;
+ TIMSK4 = 0;
+ TWCR = 0;
+ ADCSRA = 0;
+ GPIO_RF_SLP_TR_make_out();
+ SMCR = (1 << SM1) | (1 << SM0) | (1 << SE); // power-save
+ __SLEEP;
+ SMCR = 0;
+ TIMSK4 = timsk4;
+ TWCR = twcr;
+ ADCSRA = adcsra;
+ }
+
+ // wait for time about 1 TOSC1 cycle for correct re-entering from power save mode to power save mode
+ // wait for time about 1 TOSC1 cycle for correct reading TCNT2 after wake up to
+ OCR2B = SOME_VALUE_FOR_SYNCHRONIZATION;
+ while (ASSR & HAL_ASSR_FLAGS);
+}
+
+/******************************************************************************
+ Prepares system for power-save, power-down.
+ Power-down the mode is possible only when internal RC is used
+ Parameters:
+ none.
+ Returns:
+ -1 there is no possibility to power-down system.
+******************************************************************************/
+int HAL_Sleep(void)
+{
+ if (INTERNAL_RC != halGetClockSource())
+ {
+ if (HAL_SLEEP_TIMER_IS_STOPPED == halSleepControl.sleepTimerState)
+ { // sleep timer isn't started
+ return -1;
+ }
+ GPIO_RF_SLP_TR_make_in();
+
+ while (ASSR & HAL_ASSR_FLAGS);
+ if (!(TIMSK2 & (1 << OCIE2A)))
+ { // compare interrupt is disabled
+ OCR2A = 0xFF;
+ while (ASSR & HAL_ASSR_FLAGS);
+ }
+
+ TCCR2A |= ((1 << COM2A1) | (1 << COM2A0)); // set OC2A on compare
+ while (ASSR & HAL_ASSR_FLAGS);
+ TCCR2B |= (1 << FOC2A); // force output to set OC2A
+ while (ASSR & HAL_ASSR_FLAGS);
+ TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0)); // no compare
+ while (ASSR & HAL_ASSR_FLAGS);
+ TCCR2A |= (1 << COM2A1); // clear OC2A on compare
+ while (ASSR & HAL_ASSR_FLAGS);
+ }
+
+ halSleepControl.wakeupStation = HAL_SLEEP_MODE; // the reset of sign of entry to the sleep mode.
+ while (ASSR & HAL_ASSR_FLAGS);
+ halPostTask4(HAL_SLEEP);
+ return 0;
+}
+
+/******************************************************************************
+ Handler for task manager. It is executed when system has waked up.
+******************************************************************************/
+void halWakeupHandler(void)
+{
+ uint32_t timeEndOfSleep;
+
+ // save time of stopping of the application timer
+ timeEndOfSleep = halGetTimeOfSleepTimer();
+
+ timeEndOfSleep -= halTimeStartOfSleep; // time of sleep
+ halTimeStartOfSleep = 0ul;
+ // adjust application timer interval
+ halAdjustSleepInterval(timeEndOfSleep);
+ // start application timer clock
+ halStartAppClock();
+ // Wait for when radio will be waked up.
+ halWaitRadio();
+
+ #ifdef _HAL_ANT_DIVERSITY_
+
+ // enable antenna diversity switcher
+ phyAntennaSwitcherOn();
+
+ #endif
+
+ #ifdef _HAL_RF_RX_TX_INDICATOR_
+
+ // enable front end driver if that is supported
+ phyRxTxSwitcherOn();
+
+ #endif
+
+ if (HAL_SLEEP_TIMER_IS_WAKEUP_SOURCE == halSleepControl.wakeupSource)
+ {
+ if (halSleepControl.callback)
+ halSleepControl.callback();
+ }
+}
+
+/*******************************************************************************
+ Makes MCU enter Idle mode.
+*******************************************************************************/
+void HAL_IdleMode(void)
+{
+ SMCR = 0x1;
+ __SLEEP;
+ SMCR = 0;
+}
+
+// eof sleep.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleepTimerClock.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleepTimerClock.c
new file mode 100644
index 00000000..70038e7f
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleepTimerClock.c
@@ -0,0 +1,304 @@
+/**************************************************************************//**
+ \file halSleepTimer.c
+
+ \brief Module for count out requested sleep interval.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/06/07 E. Ivanov - Created
+ 7/04/09 A. Khromykh - Refactored
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <sleepTimer.h>
+#include <halSleepTimerClock.h>
+#include <halSleep.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define LSB_IN_DWORD(A) ((uint32_t)A & 0x000000FF)
+#define MULTIPLY_ON_31d25(A) (((uint32_t)A << 5) - (uint32_t)A + ((uint32_t)A >> 2))
+#define MAX_TIMER_VALUE 0xFF
+#define SLEEP_TIMER_ITERATOR (1000ul * 256ul * SLEEPTIMER_DIVIDER / SLEEPTIMER_CLOCK)
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef struct
+{
+ volatile uint16_t interval; // Contains number of timer full interval before will load reminder.
+ volatile uint8_t remainder; // Contains number of ticks that passed before timer firing
+} TimerControl_t;
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern HalSleepControl_t halSleepControl;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static volatile TimerControl_t halTimerControl;
+// time of sleep timer in ms.
+static uint32_t halSleepTime = 0ul;
+// upper byte of sleep time
+uint8_t halSleepTimerOvfw = 0;
+// interrupt counter
+static volatile uint8_t halIrqOvfwCount = 0;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief Clear timer control structure
+******************************************************************************/
+void halClearTimeControl(void)
+{
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ // clear timer control structure
+ halTimerControl.remainder = 0;
+ halTimerControl.interval = 0;
+ END_MEASURE(HALATOM_CLEAR_TIME_CONTROL_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+}
+
+/**************************************************************************//**
+\brief Wake up procedure for all external interrupts
+******************************************************************************/
+void halWakeupFromIrq(void)
+{
+ if (HAL_SLEEP_MODE == halSleepControl.wakeupStation)
+ {
+ halPowerOn(HAL_EXT_IRQ_IS_WAKEUP_SOURCE);
+ // disable compare match interrupt
+ TIMSK2 &= ~(1 << OCIE2A);
+ // clear timer control structure
+ halClearTimeControl();
+ // stop high sleep timer logic
+ halSleepControl.sleepTimerState = HAL_SLEEP_TIMER_IS_STOPPED;
+ }
+}
+
+/******************************************************************************
+Starts the sleep timer clock.
+******************************************************************************/
+void halStartSleepTimerClock(void)
+{
+ //1. Disable the Timer/Counter2 interrupts by clearing OCIE2x and TOIE2.
+ halDisableSleepTimerInt();
+ //2. Select clock source by setting AS2 as appropriate.
+ ASSR |= (1 << AS2); // clock source is TOSC1 pin
+ //3. Write new values to TCNT2, OCR2x, and TCCR2x.
+ TCNT2 = 0;
+ TCCR2A = 0x00; // normal operation, OC2A&OC2B disconnected
+ TCCR2B = 0x00;
+ OCR2A = 0x00;
+ //4. To switch to asynchronous operation: Wait for TCN2UB, OCR2xUB, and TCR2xUB.
+ while (ASSR & HAL_ASSR_FLAGS);
+ //5. Clear the Timer/Counter2 Interrupt Flags.
+ TIFR2 = (1 << OCF2A) | (1 << TOV2);
+ //6. Enable interrupts, if needed.
+ TCCR2B = SLEEPTIMER_PRESCALER; // start timer
+ TIMSK2 |= (1 << TOIE2); // enable overflow interrupt
+}
+
+/******************************************************************************
+Stops the sleep timer clock.
+******************************************************************************/
+void halStopSleepTimerClock(void)
+{
+ while (ASSR & HAL_ASSR_FLAGS);
+ //1. Disable the Timer/Counter2 interrupts by clearing OCIE2x and TOIE2.
+ halDisableSleepTimerInt();
+ TCCR2B &= ~SLEEPTIMER_PRESCALER; // Stops the timer
+ GTCCR |= (1 << PSRASY); // Reset prescaler
+ while (ASSR & HAL_ASSR_FLAGS);
+ // switch of oscillator
+ ASSR &= ~(1 << AS2);
+}
+
+/******************************************************************************
+Sets interval.
+Parameters:
+ value - contains number of ticks which the timer must count out.
+Returns:
+ none.
+******************************************************************************/
+void halSetSleepTimerInterval(uint32_t value)
+{
+ uint8_t currCounter = TCNT2;
+ uint32_t tempValue = LSB_IN_DWORD(~currCounter);
+
+ if (value > tempValue)
+ {
+ value -= tempValue;
+ // halTimerControl.interval = value / 255
+ halTimerControl.interval = value >> 8;
+ halTimerControl.remainder = value & 0xFF;
+ }
+ else
+ { // enough timer reminder before overflow
+ currCounter += (uint8_t)value;
+ // wait for end of synchronization
+ while (ASSR & HAL_ASSR_FLAGS);
+ // load compared value
+ OCR2A = currCounter;
+ // clear compare interrupt flag
+ TIFR2 = 1 << OCF2A;
+ // enable compare match interrupt
+ TIMSK2 |= (1 << OCIE2A);
+ }
+}
+
+/******************************************************************************
+Return time of sleep timer.
+
+Returns:
+ time in ms.
+******************************************************************************/
+uint32_t halGetTimeOfSleepTimer(void)
+{
+ uint32_t tempValue;
+ uint8_t tmpCounter;
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ // read interrupt counter
+ tmpCounter = halIrqOvfwCount;
+ // read asynchronous counter
+ tempValue = TCNT2;
+ // wait for setup asynchronous registers
+ OCR2B = SOME_VALUE_FOR_SYNCHRONIZATION;
+ while (ASSR & HAL_ASSR_FLAGS);
+ if (TIFR2 & (1 << TOV2))
+ { // there is issued interrupt
+ tempValue = TCNT2;
+ tempValue += MAX_TIMER_VALUE;
+ }
+ END_MEASURE(HAL_GET_SLEEP_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ tempValue += tmpCounter * MAX_TIMER_VALUE;
+
+ #if defined(SLEEP_PRESCALER_1024)
+ // one tick time 31.25 ms.
+ return (halSleepTime + MULTIPLY_ON_31d25(tempValue));
+ #else
+ #warning 'to do counting sleep timer for that prescaler'
+ return (halSleepTime + tempValue * (1000 * SLEEPTIMER_DIVIDER / SLEEPTIMER_CLOCK));
+ #endif
+}
+
+/******************************************************************************
+Returns the sleep timer frequency in Hz.
+Parameters:
+ none.
+Returns:
+ frequency.
+******************************************************************************/
+uint32_t halSleepTimerFrequency(void)
+{
+ return (SLEEPTIMER_CLOCK / SLEEPTIMER_DIVIDER);
+}
+
+/**************************************************************************//**
+Synchronization system time which based on sleep timer.
+******************************************************************************/
+void halSleepSystemTimeSynchronize(void)
+{
+ uint8_t tmpCounter;
+ uint32_t tmpValue;
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ tmpCounter = halIrqOvfwCount;
+ halIrqOvfwCount = 0;
+ END_MEASURE(HAL_SLEEP_TIMER_SYNCHRONIZE_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ tmpValue = tmpCounter * SLEEP_TIMER_ITERATOR;
+ halSleepTime += tmpValue;
+ if (halSleepTime < tmpValue)
+ halSleepTimerOvfw++;
+}
+
+/******************************************************************************
+Compare interrupt handler.
+******************************************************************************/
+ISR(TIMER2_COMPA_vect)
+{
+ BEGIN_MEASURE
+ // disable compare match interrupt
+ TIMSK2 &= ~(1 << OCIE2A);
+ // nulling for adjusting
+ halTimerControl.remainder = 0;
+ if (HAL_SLEEP_MODE == halSleepControl.wakeupStation)
+ halPowerOn(HAL_SLEEP_TIMER_IS_WAKEUP_SOURCE);
+ // post task for task manager
+ if (HAL_SLEEP_TIMER_IS_STARTED == halSleepControl.sleepTimerState)
+ halInterruptSleepClock();
+ END_MEASURE(HALISR_TIMER2_COMPA_TIME_LIMIT)
+}
+
+/******************************************************************************
+Overflow interrupt handler.
+******************************************************************************/
+ISR(TIMER2_OVF_vect)
+{
+ BEGIN_MEASURE
+ if (0 == halTimerControl.interval)
+ {
+ if (0 == halTimerControl.remainder)
+ {
+ if (HAL_SLEEP_MODE == halSleepControl.wakeupStation)
+ halPowerOn(HAL_SLEEP_TIMER_IS_WAKEUP_SOURCE);
+ // post task for task manager
+ if (HAL_SLEEP_TIMER_IS_STARTED == halSleepControl.sleepTimerState)
+ halInterruptSleepClock();
+ }
+ else
+ {
+ // wait for end of synchronization
+ while (ASSR & HAL_ASSR_FLAGS);
+ // load compared value
+ OCR2A = halTimerControl.remainder;
+ // clear compare interrupt flag
+ TIFR2 = 1 << OCF2A;
+ // enable compare match interrupt
+ TIMSK2 |= (1 << OCIE2A);
+ if (HAL_SLEEP_MODE == halSleepControl.wakeupStation)
+ HAL_Sleep();
+ }
+ }
+ else
+ {
+ halTimerControl.interval--;
+ if (HAL_SLEEP_MODE == halSleepControl.wakeupStation)
+ HAL_Sleep();
+ }
+
+ halIrqOvfwCount++;
+ halSynchronizeSleepTime();
+
+ END_MEASURE(HALISR_TIMER2_OVF_TIME_LIMIT)
+}
+
+//eof halSleepTimerClock.c
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSpi.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSpi.c
new file mode 100644
index 00000000..b0c8bf0a
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSpi.c
@@ -0,0 +1,142 @@
+/**************************************************************************//**
+\file halSpi.c
+
+\brief Implementation of USART SPI mode.
+
+\author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+\internal
+ History:
+ 29/06/07 E. Ivanov - Created
+******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <spi.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define UDORD0 2
+#define UCPHA0 1
+#define UCPOL0 0
+#define SPI_CLOCK_MODE_AMOUNT 4
+#define SPI_DATA_ORDER_AMOUNT 2
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Set the parameters of USART to work at SPI mode.
+Parameters:
+ descriptor - pointer to the spi descriptor.
+Returns:
+ none.
+******************************************************************************/
+void halSetUsartSpiConfig(HAL_SpiDescriptor_t *descriptor)
+{
+ uint8_t clockMode[SPI_CLOCK_MODE_AMOUNT] = {((0 << UCPOL0) | (0 << UCPHA0)),
+ ((0 << UCPOL0) | (1 << UCPHA0)),
+ ((1 << UCPOL0) | (0 << UCPHA0)),
+ ((1 << UCPOL0) | (1 << UCPHA0))};
+ uint8_t dataOrder[SPI_DATA_ORDER_AMOUNT] = {(0 << UDORD0),
+ (1 << UDORD0)};
+
+ // setting of the spi gpio direct
+ if (SPI_CHANNEL_0 == descriptor->tty)
+ GPIO_USART0_EXTCLK_make_out();
+ else
+ GPIO_USART1_EXTCLK_make_out();
+
+ UBRRn(descriptor->tty) = 0;
+ // Set MSPI mode
+ UCSRnC(descriptor->tty) = (1 << UMSEL01) | (1 << UMSEL00);
+ // Set clock mode and data order
+ UCSRnC(descriptor->tty) |= (dataOrder[descriptor->dataOrder] | clockMode[descriptor->clockMode]);
+ // Enable receiver and transmitter
+ UCSRnB(descriptor->tty) = (1 << RXEN0) | (1 << TXEN0);
+ // Set baud rate
+ UBRRn(descriptor->tty) = descriptor->baudRate;
+}
+
+/******************************************************************************
+Disables USART channel.
+Parameters:
+ tty - spi channel.
+******************************************************************************/
+void halClearUsartSpi(SpiChannel_t tty)
+{
+ if (SPI_CHANNEL_0 == tty)
+ GPIO_USART0_EXTCLK_make_in();
+ else
+ GPIO_USART1_EXTCLK_make_in();
+
+ UCSRnB(tty) = 0x00; // disable
+}
+
+/******************************************************************************
+Write a length bytes to the SPI.
+Parameters:
+ tty - spi channel
+ buffer - pointer to application data buffer;
+ length - number bytes for transfer;
+Returns:
+ number of written bytes
+******************************************************************************/
+uint16_t halSyncUsartSpiWriteData(SpiChannel_t tty, uint8_t *buffer, uint16_t length)
+{
+ uint16_t i;
+ uint8_t temp;
+
+ for (i = 0; i < length; i++)
+ {
+ // Wait for empty transmit buffer
+ while (!(UCSRnA(tty) & (1 << UDRE0)));
+ // Send data
+ UDRn(tty) = *(buffer + i);
+ // Wait for data to be received
+ while (!(UCSRnA(tty) & (1 << RXC0)));
+ // receives data to clear received usart buffer
+ temp = UDRn(tty);
+ (void)temp;
+ }
+ return i;
+}
+
+/******************************************************************************
+Write & read a length bytes to & from the SPI.
+Parameters:
+ tty - spi channel
+ buffer - pointer to application data buffer;
+ length - number bytes for transfer;
+Returns:
+ number of written & read bytes
+******************************************************************************/
+uint16_t halSyncUsartSpiReadData(SpiChannel_t tty, uint8_t *buffer, uint16_t length)
+{
+ uint16_t i;
+
+ for (i = 0; i < length; i++)
+ {
+ // Wait for empty transmit buffer
+ while (!(UCSRnA(tty) & (1 << UDRE0)));
+ // Send data
+ UDRn(tty) = *(buffer + i);
+ // Wait for data to be received
+ while (!(UCSRnA(tty) & (1 << RXC0)));
+ // Receive data
+ *(buffer + i) = UDRn(tty);
+ }
+ return i;
+}
+
+//end of halSpi.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halUsart.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halUsart.c
new file mode 100644
index 00000000..90388ed0
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halUsart.c
@@ -0,0 +1,190 @@
+/**************************************************************************//**
+\file halUsart.c
+
+\brief Implementation of usart hardware-dependent module.
+
+\author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+\internal
+ History:
+ 29/05/07 E. Ivanov - Created
+*******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <sleep.h>
+#include <usart.h>
+#include <halSleepTimerClock.h>
+#include <halAppClock.h>
+#include <halIrq.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+void halPostUsartTask(HalUsartTaskId_t taskId);
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern volatile bool halEnableDtrWakeUp;
+extern void (* dtrWakeUpCallback)(void);
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+ \brief Sets USART module parameters.
+ \param
+ usartmode - pointer to HAL_UsartDescriptor_t
+ \return
+ none.
+******************************************************************************/
+void halSetUsartConfig(HAL_UsartDescriptor_t *usartMode)
+{
+ UCSRnB(usartMode->tty) = 0x00; // disable usart
+ UBRRn(usartMode->tty) = usartMode->baudrate; // usart speed
+
+ if (USART_MODE_ASYNC == usartMode->mode)
+ {
+ UCSRnA(usartMode->tty) = (uint8_t)USART_DOUBLE_SPEED << U2X0; // Double the USART Transmition Speed
+ UCSRnC(usartMode->tty) = 0x00;
+ }
+ else
+ {
+ UCSRnA(usartMode->tty) = 0;
+ UCSRnC(usartMode->tty) = usartMode->edge; // edge select
+ }
+
+ UCSRnC(usartMode->tty) |= usartMode->mode;
+ UCSRnC(usartMode->tty) |= usartMode->dataLength; // character size
+ UCSRnC(usartMode->tty) |= usartMode->parity; // parity mode
+ UCSRnC(usartMode->tty) |= usartMode->stopbits; // stop bit select
+ UCSRnA(usartMode->tty) |= (1 << RXC0); // clear receive interrupt
+ UCSRnB(usartMode->tty) |= (1 << RXEN1) | (1 << TXEN1); // usart enable
+ UCSRnB(usartMode->tty) |= (1 << RXCIE0) ; // receive interrupt enable
+}
+
+/**************************************************************************//**
+ \brief The interrupt handler of USART0 - data register is empty.
+******************************************************************************/
+ISR(USART0_UDRE_vect)
+{
+ BEGIN_MEASURE
+ // We must disable the interrupt because we must "break" context.
+ halDisableUsartDremInterrupt(USART_CHANNEL_0);
+ halPostUsartTask(HAL_USART_TASK_USART0_DRE);
+ END_MEASURE(HALISR_USART0_UDR_TIME_LIMIT)
+}
+
+/**************************************************************************//**
+ \brief The interrupt handler of USART0 - transmission is completed.
+******************************************************************************/
+ISR(USART0_TX_vect)
+{
+ BEGIN_MEASURE
+ halDisableUsartTxcInterrupt(USART_CHANNEL_0);
+ halPostUsartTask(HAL_USART_TASK_USART0_TXC);
+ END_MEASURE(HALISR_USART0_TX_TIME_LIMIT)
+}
+
+/**************************************************************************//**
+ \brief The interrupt handler of USART0 - reception is completed.
+******************************************************************************/
+ISR(USART0_RX_vect)
+{
+ BEGIN_MEASURE
+ uint8_t status = UCSR0A;
+ uint8_t data = UDR0;
+
+ if (!(status & ((1 << FE0) | (1 << DOR0) | (1 << UPE0))))
+ {
+ halUsartRxBufferFiller(USART_CHANNEL_0, data);
+ halPostUsartTask(HAL_USART_TASK_USART0_RXC);
+ }
+ #if defined(_USE_USART_ERROR_EVENT_)
+ else // There is an error in the received byte.
+ {
+ halUsartSaveErrorReason(USART_CHANNEL_0, status);
+ halPostUsartTask(HAL_USART_TASK_USART0_ERR);
+ }
+ #endif
+
+ END_MEASURE(HALISR_USART0_RX_TIME_LIMIT)
+}
+
+/**************************************************************************//**
+ \brief The interrupt handler of USART1 - data register is empty.
+******************************************************************************/
+ISR(USART1_UDRE_vect)
+{
+ BEGIN_MEASURE
+ // We must disable the interrupt because we must "break" context.
+ halDisableUsartDremInterrupt(USART_CHANNEL_1);
+ halPostUsartTask(HAL_USART_TASK_USART1_DRE);
+ END_MEASURE(HALISR_USART1_UDRE_TIME_LIMIT)
+}
+
+/**************************************************************************//**
+ \brief The interrupt handler of USART1 - transmission is completed.
+******************************************************************************/
+ISR(USART1_TX_vect)
+{
+ BEGIN_MEASURE
+ halDisableUsartTxcInterrupt(USART_CHANNEL_1);
+ halPostUsartTask(HAL_USART_TASK_USART1_TXC);
+ END_MEASURE(HALISR_USART1_TX_TIME_LIMIT)
+}
+
+/**************************************************************************//**
+ \brief The interrupt handler of USART1 - reception is completed.
+******************************************************************************/
+ISR(USART1_RX_vect)
+{
+ BEGIN_MEASURE
+ uint8_t status = UCSR1A;
+ uint8_t data = UDR1;
+
+ if (!(status & ((1 << FE1) | (1 << DOR1) | (1 << UPE1))))
+ {
+ halUsartRxBufferFiller(USART_CHANNEL_1, data);
+ halPostUsartTask(HAL_USART_TASK_USART1_RXC);
+ }
+ #if defined(_USE_USART_ERROR_EVENT_)
+ else // There is an error in the received byte.
+ {
+ halUsartSaveErrorReason(USART_CHANNEL_1, status);
+ halPostUsartTask(HAL_USART_TASK_USART1_ERR);
+ }
+ #endif
+ END_MEASURE(HALISR_USART1_RX_TIME_LIMIT)
+}
+
+/**************************************************************************//**
+/brief External interrupt 4 (DTR) handler
+******************************************************************************/
+ISR(INT4_vect)
+{
+ BEGIN_MEASURE
+ halWakeupFromIrq();
+
+ if (halEnableDtrWakeUp)
+ { /* enable DTR (irq 4) wake up */
+ halDisableIrqInterrupt(IRQ_4);
+ } /* enable DTR (irq 4) wake up */
+
+ if (NULL != dtrWakeUpCallback)
+ dtrWakeUpCallback();
+ END_MEASURE(HALISR_INT4_TIME_LIMIT)
+}
+// eof halUsart.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s
new file mode 100644
index 00000000..1c76747f
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s
@@ -0,0 +1,210 @@
+/**************************************************************************//**
+ \file halW1.s
+
+ \brief Implementation of 1-wire hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+.include "halGccD.h"
+.set __w1_port, 0x14 ; PORTG
+.set __w1_bit, 5 ; PORTG5
+.set __w1_ddr, 0x13 ; DDRG
+.set __w1_pin, 0x12 ; PING
+
+/*=============================================================
+ Resets all devices connected to the bus. Function asserts on
+ the bus reset pulse and detects presence pulse. The result is
+ contained in r24.
+ Parameters:
+ Returns:
+ W1_SUCCESS_STATUS - If device(s) was(were) detected.
+ W1_NO_DEVICE_STATUS - If device(s) was(were) not detected.
+===============================================================*/
+.global halResetW1
+.type halResetW1,@function
+halResetW1:
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+ ; Pull down
+ sbi __w1_ddr, __w1_bit
+ cbi __w1_port, __w1_bit
+ ; Reset Low Time (500 us)
+ ldi r24, 250
+ call __delay_us
+ ldi r24, 250
+ call __delay_us
+ ; Tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+ ; Presence-Detect Sample Time (70 us)
+ ldi r24, 70
+ call __delay_us
+ ; Precense-Detect
+ ldi r24, 0x01
+ sbic __w1_pin, __w1_bit
+ ldi r24, 0x00
+ push r24
+ ; Tail of Reset High Time
+ ldi r24, 240
+ call __delay_us
+ pop r24
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ ret
+
+/*=============================================================
+ Reads bit from the bus
+ Returns:
+ bit read from the bus in r24
+===============================================================*/
+.global halReadW1Bit
+.type halReadW1Bit,@function
+halReadW1Bit:
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+ ; Pull down
+ sbi __w1_ddr, __w1_bit
+ cbi __w1_port, __w1_bit
+ ; Read Low Time (6 us)
+ ldi r24, 6
+ call __delay_us
+ ; Tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+ ; Tail of Read Sample Time (10 us)
+ ldi r24, 10
+ call __delay_us
+ ; Read Sample
+ clc
+ sbic __w1_pin, __w1_bit
+ sec
+ rol r24
+ ; Tail of Timeslot Duration
+ push r24
+ ldi r24, 100
+ call __delay_us
+ pop r24
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ ret
+
+/*=============================================================
+ Reads byte from the bus
+ Returns:
+ byte read from the bus in r24
+===============================================================*/
+.global halReadW1
+.type halReadW1,@function
+halReadW1:
+ push r25
+ push r23
+ ldi r25, 8
+__read_bit_again:
+ call halReadW1Bit
+ ror r24
+ ror r23
+ dec r25
+ tst r25
+ brne __read_bit_again
+ mov r24, r23
+ pop r23
+ pop r25
+ ret
+
+/*=============================================================
+ Writes bit to the bus
+ Parameters:
+ value - bit that should be written to the bus.
+===============================================================*/
+.global halWriteW1bit
+.type halWriteW1bit,@function
+halWriteW1bit:
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+ ; Pull down
+ cbi __w1_port, __w1_bit
+ sbi __w1_ddr, __w1_bit
+ ; Write-1 Low Time
+ push r24
+ ldi r24, 6
+ call __delay_us
+ pop r24
+ ; Write bit
+ ror r24
+ brcc __w1_write_zero
+ ; Write-One -> tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+__w1_write_zero:
+ ; Tail of Timeslot Duration
+ push r24
+ ldi r24, 100
+ call __delay_us
+ pop r24
+ ; Tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ ret
+
+/*=============================================================
+ Writes byte to the bus
+ Parameters:
+ value - byte that should be written to the bus.
+===============================================================*/
+.global halWriteW1
+.type halWriteW1,@function
+halWriteW1:
+ push r25
+ ldi r25, 8
+__write_bit_again:
+ call halWriteW1bit
+ dec r25
+ tst r25
+ brne __write_bit_again
+ pop r25
+ ret
+
+/*=============================================================
+ Delay in microseconds.
+ Parameters:
+ us - delay time in microseconds
+===============================================================*/
+.global __delay_us
+.type __delay_us,@function
+__delay_us:
+__w0:
+.if FCPU==8000000
+ nop
+ nop
+ nop
+ nop
+.endif
+ dec r24
+ tst r24
+ brne __w0
+ ret
+; eof halW1.s
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s90 b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s90
new file mode 100644
index 00000000..a12f063d
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halW1.s90
@@ -0,0 +1,211 @@
+/**************************************************************************//**
+ \file halW1.s90
+
+ \brief Implementation of 1-wire hardware-dependent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#include <halIarD.h>
+__w1_port VAR 0x14 ; PORTG
+__w1_bit VAR 5 ; PORTG5
+__w1_ddr VAR 0x13 ; DDRG
+__w1_pin VAR 0x12 ; PING
+
+/*=============================================================
+ Resets all devices connected to the bus. Function asserts on
+ the bus reset pulse and detects presence pulse. The result is
+ contained in r16.
+ Parameters:
+ Returns:
+ W1_SUCCESS_STATUS - If device(s) was(were) detected.
+ W1_NO_DEVICE_STATUS - If device(s) was(were) not detected.
+===============================================================*/
+PUBLIC halResetW1
+RSEG CODE
+halResetW1:
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+ ; Pull down
+ sbi __w1_ddr, __w1_bit
+ cbi __w1_port, __w1_bit
+ ; Reset Low Time (500 us)
+ ldi r16, 250
+ call __delay_us
+ ldi r16, 250
+ call __delay_us
+ ; Tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+ ; Presence-Detect Sample Time (70 us)
+ ldi r16, 70
+ call __delay_us
+ ; Precense-Detect
+ ldi r16, 0x01
+ sbic __w1_pin, __w1_bit
+ ldi r16, 0x00
+ push r16
+ ; Tail of Reset High Time
+ ldi r16, 240
+ call __delay_us
+ pop r16
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ ret
+
+/*=============================================================
+ Reads bit from the bus
+ Returns:
+ bit read from the bus in r16
+===============================================================*/
+PUBLIC halReadW1Bit
+RSEG CODE
+halReadW1Bit:
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+ ; Pull down
+ sbi __w1_ddr, __w1_bit
+ cbi __w1_port, __w1_bit
+ ; Read Low Time (6 us)
+ ldi r16, 6
+ call __delay_us
+ ; Tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+ ; Tail of Read Sample Time (10 us)
+ ldi r16, 10
+ call __delay_us
+ ; Read Sample
+ clc
+ sbic __w1_pin, __w1_bit
+ sec
+ rol r16
+ ; Tail of Timeslot Duration
+ push r16
+ ldi r16, 100
+ call __delay_us
+ pop r16
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ ret
+
+/*=============================================================
+ Reads byte from the bus
+ Returns:
+ byte read from the bus in r16
+===============================================================*/
+PUBLIC halReadW1
+RSEG CODE
+halReadW1:
+ push r25
+ push r23
+ ldi r25, 8
+__read_bit_again:
+ call halReadW1Bit
+ ror r16
+ ror r23
+ dec r25
+ tst r25
+ brne __read_bit_again
+ mov r16, r23
+ pop r23
+ pop r25
+ ret
+
+/*=============================================================
+ Writes bit to the bus
+ Parameters:
+ value - bit that should be written to the bus.
+===============================================================*/
+PUBLIC halWriteW1bit
+RSEG CODE
+halWriteW1bit:
+ ; Store SREG
+ push r23
+ in r23, 0x3F
+ ; Disable interrupts
+ cli
+ ; Pull down
+ cbi __w1_port, __w1_bit
+ sbi __w1_ddr, __w1_bit
+ ; Write-1 Low Time
+ push r16
+ ldi r16, 6
+ call __delay_us
+ pop r16
+ ; Write bit
+ ror r16
+ brcc __w1_write_zero
+ ; Write-One -> tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+__w1_write_zero:
+ ; Tail of Timeslot Duration
+ push r16
+ ldi r16, 100
+ call __delay_us
+ pop r16
+ ; Tri-state (external pullup)
+ cbi __w1_ddr, __w1_bit
+ ; Restore SREG
+ out 0x3F, r23
+ pop r23
+ ret
+
+/*=============================================================
+ Writes byte to the bus
+ Parameters:
+ value - byte that should be written to the bus.
+===============================================================*/
+PUBLIC halWriteW1
+RSEG CODE
+halWriteW1:
+ push r25
+ ldi r25, 8
+__write_bit_again:
+ call halWriteW1bit
+ dec r25
+ tst r25
+ brne __write_bit_again
+ pop r25
+ ret
+
+/*=============================================================
+ Delay in microseconds.
+ Parameters:
+ us - delay time in microseconds
+===============================================================*/
+PUBLIC __delay_us
+RSEG CODE
+__delay_us:
+__w0:
+#if FCPU==8000000
+ nop
+ nop
+ nop
+ nop
+#endif
+ dec r16
+ tst r16
+ brne __w0
+ ret
+; eof halW1.s
+END
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halWdtInit.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halWdtInit.c
new file mode 100644
index 00000000..eb2a9dff
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halWdtInit.c
@@ -0,0 +1,175 @@
+/**************************************************************************//**
+ \file halWdtInit.c
+
+ \brief Implementation of WDT start up procedure.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <resetReason.h>
+#include <halAppClock.h>
+
+/******************************************************************************
+ Defines section
+******************************************************************************/
+#define PIN_OUT 62500
+#define MEANING_BITS 0x1F
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+#ifdef _SYS_ASSERT_ON_
+ INLINE void halJumpNullHandler(void);
+#endif
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+#if defined(__GNUC__)
+ uint8_t halResetReason __attribute__ ((section (".noinit")));
+#elif defined(__ICCAVR__)
+ __no_init uint8_t halResetReason;
+#endif
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Resets and stops wdt. Saves the reason of reset.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+#if defined(__GNUC__)
+
+ void halWdtInit(void) __attribute__ ((naked)) \
+ __attribute__ ((section (".init0")));
+
+ void halWdtInit(void)
+ {
+ ASM("clr r1");
+#elif defined(__ICCAVR__)
+
+ __task void halWdtInit(void)
+ {
+ ASM("clr r15");
+#else
+ #error 'Compiler not supported.'
+#endif
+
+ if (TEMP_WARM_RESET != halResetReason)
+ {
+ halResetReason = MCUSR & MEANING_BITS;
+
+ if (halResetReason & POWER_ON_RESET)
+ halResetReason = POWER_ON_RESET;
+ }
+ else
+ {
+ halResetReason = WARM_RESET;
+ }
+ MCUSR = 0;
+ WDTCSR |= (1 << WDCE) | (1 << WDE);
+ WDTCSR = 0x00;
+
+ #ifdef _SYS_ASSERT_ON_
+ halJumpNullHandler();
+ #endif
+ }
+
+#ifdef _SYS_ASSERT_ON_
+/******************************************************************************
+Jump to NULL handler.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void halJumpNullHandler(void)
+{
+ if (0 == halResetReason) // was jump on NULL
+ {
+ register volatile uint16_t tmp;
+ tmp = SP;
+
+ ASM ("cli");
+ DDRB |= 0xE0;
+ /* Init UART*/
+ UBRR1H = 0;
+ #if (F_CPU == 4000000ul)
+ UBRR1L = 12;
+ #elif (F_CPU == 8000000ul)
+ UBRR1L = 25;
+ #endif
+ UCSR1A = (1 << U2X1);
+ UCSR1B = (1 << TXEN1);
+ UCSR1C = (3 << UCSZ10); // 8-bit data
+
+ /* Init timer counter 4.*/
+ OCR4A = 0;
+ /* Disable TC4 interrupt */
+ TIMSK4 &= ~(1 << OCIE4A);
+ /* main clk / 8 */
+ TCCR4B = (1 << WGM12) | (1 << CS11);
+
+ while (1)
+ {
+ do
+ { /* Send byte to UART */
+ while (!(UCSR1A & (1 << UDRE1)));
+ UDR1 = *((uint8_t *)SP);
+ SP++;
+ } while (RAMEND >= SP);
+ SP = tmp;
+
+ PORTB |= 0x80;
+ TCNT4 = 0;
+ while(TCNT4 < PIN_OUT);
+ PORTB &= ~0x80;
+ PORTB |= 0x40;
+ TCNT4 = 0;
+ while(TCNT4 < PIN_OUT);
+ PORTB &= ~0x40;
+ PORTB |= 0x20;
+ TCNT4 = 0;
+ while(TCNT4 < PIN_OUT);
+ PORTB &= ~0x20;
+ }
+ }
+}
+#endif
+
+#if defined(__GNUC__) && defined(_REPORT_STATS_)
+void halFillStack(void) __attribute__ ((naked, section (".init1")));
+/**************************************************************************//**
+\brief Fill cstack with repeated pattern 0xCD
+******************************************************************************/
+void halFillStack(void)
+{
+ extern uint16_t __stack_start;
+ extern uint16_t __stack;
+
+ for (uint8_t *start = (uint8_t *)&__stack_start; start <= (uint8_t *)&__stack; start++)
+ *start = 0xCD;
+}
+#endif
+
+// eof halWdtInit.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/i2c.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/i2c.c
new file mode 100644
index 00000000..f118194e
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/i2c.c
@@ -0,0 +1,90 @@
+/**************************************************************************//**
+ \file i2c.c
+
+ \brief Provides the functionality of TWI.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <i2c.h>
+#include <halTaskManager.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Inits TWI module. Setup teh speed of TWI.
+Parameters:
+ i2cMode - the speed of TWI.
+Returns:
+ none.
+******************************************************************************/
+void halInitI2c(HAL_i2cMode_t *i2cMode)
+{
+ TWCR = 0x00;
+ TWSR = HAL_I2C_PRESCALER; // prescaler
+ // Set bit rate
+ TWBR = i2cMode->clockrate;
+}
+
+/******************************************************************************
+Interrupt handler.
+******************************************************************************/
+ISR(TWI_vect)
+{
+ BEGIN_MEASURE
+ switch (TWSR & 0xF8)
+ {
+ case TWS_START:
+ case TWS_RSTART:
+ halSendStartDoneI2c();
+ break;
+
+ case TWS_MT_SLA_ACK:
+ case TWS_MT_DATA_ACK:
+ halWriteDoneI2c();
+ break;
+
+ case TWS_BUSERROR:
+ case TWS_MT_SLA_NACK:
+ case TWS_MT_DATA_NACK:
+ case TWS_MR_SLA_NACK:
+ halI2cBusReset();
+ break;
+
+ case TWS_MR_SLA_ACK:
+ halMasterReadWriteAddressAckI2c();
+ break;
+
+ case TWS_MR_DATA_ACK:
+ halReadDoneI2c(halReadByteI2c());
+ break;
+
+ case TWS_MR_DATA_NACK:
+ halReadLastByteDoneI2c(halReadByteI2c());
+ break;
+
+ default:
+ break;
+ }
+ END_MEASURE(HALISR_TWI_TIME_LIMIT)
+}
+// eof i2c.c
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/wdt.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/wdt.c
new file mode 100644
index 00000000..d9c5a859
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/wdt.c
@@ -0,0 +1,79 @@
+/**************************************************************************//**
+ \file wdt.c
+
+ \brief Implementation of WDT interrupt handler.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#include <wdtCtrl.h>
+#include <atomic.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+void (*halWdtCallback)(void) = NULL;
+
+/*******************************************************************************
+Registers WDT fired callback.
+Parameters:
+ wdtCallback - callback.
+Returns:
+ none.
+*******************************************************************************/
+void HAL_RegisterWdtCallback(void (*wdtCallback)(void))
+{
+ halWdtCallback = wdtCallback;
+}
+
+/*******************************************************************************
+Starts WDT with interval.
+Parameters:
+ interval - interval.
+Returns:
+ none.
+*******************************************************************************/
+void HAL_StartWdt(HAL_WdtInterval_t interval)
+{
+ uint8_t i = 0;
+
+ if (halWdtCallback)
+ i = (1<<WDE) | (1 << WDIE) | interval;
+ else
+ i = (1<<WDE) | interval;
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ wdt_reset();
+ WDTCSR |= (1<<WDCE) | (1<<WDE);
+ WDTCSR = i;
+ END_MEASURE(HALATOM_STARTWDT_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+}
+
+/*******************************************************************************
+Interrupt handler.
+*******************************************************************************/
+ISR(WDT_vect)
+{
+ if (NULL != halWdtCallback)
+ halWdtCallback();
+ wdt_enable(0);
+ for (;;);
+}
+//eof wdt.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halMacIsr.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halMacIsr.h
new file mode 100644
index 00000000..127af1ee
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halMacIsr.h
@@ -0,0 +1,77 @@
+/**************************************************************************//**
+ \file halMacIsr.h
+
+ \brief Declaration mac timer interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 14/01/08 A. Mandychev - Created.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALMACISR_H
+#define _HALMACISR_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halAppClock.h>
+
+/******************************************************************************
+ Types section.
+******************************************************************************/
+typedef enum
+{
+ HAL_RTIMER_REPEAT_MODE = 0,
+ HAL_RTIMER_ONE_SHOT_MODE = 1,
+ HAL_RTIMER_STOPPED_MODE = 2,
+} HAL_RTimerMode_t;
+
+typedef struct // Timer description.
+{
+ volatile HAL_RTimerMode_t mode; // Mode.
+ volatile uint16_t period; // Period of the timer.
+ volatile uint16_t nextEvent; // Counter of periods.
+} RTimerDescr_t;
+
+/******************************************************************************
+ External variables.
+******************************************************************************/
+// Do not use it in other files.
+extern RTimerDescr_t __rtimer;
+
+/******************************************************************************
+ Prototypes section.
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer and RF ext. interrupts.
+******************************************************************************/
+void HAL_InitMacIsr(void);
+
+/******************************************************************************
+ Starts RTimer. Function should be invoked in critical section.
+ Parameters:
+ source - source of invocation.
+ mode - RTimer mode.
+ period - RTimer period.
+******************************************************************************/
+bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period);
+
+/******************************************************************************
+ Stops RTimer. Function should be invoked in critical section.
+******************************************************************************/
+void HAL_StopRtimer(void);
+
+#endif /* _HALMACISR_H */
+
+// eof halMacIsr.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfCtrl.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfCtrl.h
new file mode 100644
index 00000000..7b6534d2
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfCtrl.h
@@ -0,0 +1,127 @@
+/**************************************************************************//**
+ \file halRfCtrl.h
+
+ \brief Types and constants declaration for IEEE802.15.4 PHY implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 28/05/07 ALuzhetsky - Created.
+ 06/08/07 A. Mandychev, E. Ivanov - Modified.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFCTRL_H
+#define _HALRFCTRL_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <types.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef enum
+{
+ HAL_FREQ_NOCLK,
+ HAL_FREQ_1MHZ,
+ HAL_FREQ_2MHZ,
+ HAL_FREQ_4MHZ,
+ HAL_FREQ_8MHZ,
+ HAL_FREQ_16MHZ
+} HalSysFreq_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Delay in us
+******************************************************************************/
+void HAL_Delay(uint8_t us);
+
+/******************************************************************************
+ Clears the irq.
+******************************************************************************/
+void HAL_ClearRfIrqFlag(void);
+
+/******************************************************************************
+ Enables the irq.
+******************************************************************************/
+void HAL_EnableRfIrq(void);
+
+/******************************************************************************
+ Disables the irq.
+******************************************************************************/
+uint8_t HAL_DisableRfIrq(void);
+
+/******************************************************************************
+ Sets SLP_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfSlpTr(void);
+
+/******************************************************************************
+ Clears SLP_TR pin to 0.
+******************************************************************************/
+void HAL_ClearRfSlpTr(void);
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeInRfSlpTr(void);
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeOutRfSlpTr(void);
+
+/******************************************************************************
+ Sets RST_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfRst(void);
+
+/******************************************************************************
+ Clears RST_TR pin to 1.
+******************************************************************************/
+void HAL_ClearRfRst(void);
+
+/******************************************************************************
+ Init pins that controls RF chip.
+******************************************************************************/
+void HAL_InitRfPins(void);
+
+/******************************************************************************
+ Inits Atmega IRQ pin.
+******************************************************************************/
+void HAL_InitRfIrq(void);
+
+/******************************************************************************
+ Returns current frequency code.
+******************************************************************************/
+HalSysFreq_t HAL_GetRfFreq(void);
+
+/**************************************************************************//**
+ \brief Wait for when radio will be waked up.
+
+ \param none.
+ \return none.
+******************************************************************************/
+void halWaitRadio(void);
+
+/**************************************************************************//**
+ \brief Enables RX TX indicator for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxTxSwitcher(void);
+
+#endif /* _HALRFCTRL_H */
+
+// eof halRfCtrl.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfPio.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfPio.h
new file mode 100644
index 00000000..87ac6c88
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfPio.h
@@ -0,0 +1,43 @@
+/**************************************************************************//**
+ \file halRfPio.h
+
+ \brief AT86RF230 control pins declarations.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFPIO_H
+#define _HALRFPIO_H
+
+#include <gpio.h>
+
+// Macros for the RF_SLP_TR pin manipulation.
+HAL_ASSIGN_PIN(RF_SLP_TR, B, 4);
+// Macros for the RF_RST pin manipulation.
+HAL_ASSIGN_PIN(RF_RST, B, 5);
+// Macros for the RF_IRQ pin manipulation.
+HAL_ASSIGN_PIN(RF_IRQ, D, 4);
+// Macros for the SPI_CS pin manipulation.
+HAL_ASSIGN_PIN(SPI_CS, B, 0);
+// Macros for the SPI_SCK pin manipulation.
+HAL_ASSIGN_PIN(SPI_SCK, B, 1);
+// Macros for the SPI_MOSI pin manipulation.
+HAL_ASSIGN_PIN(SPI_MOSI, B, 2);
+// Macros for the SPI_MISO pin manipulation.
+HAL_ASSIGN_PIN(SPI_MISO, B, 3);
+// Macros for the RF_TST pin manipulation.
+HAL_ASSIGN_PIN(RF_TST, B, 6);
+#endif /* _HALRFPIO_H */
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfSpi.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfSpi.h
new file mode 100644
index 00000000..663ae50b
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/include/halRfSpi.h
@@ -0,0 +1,80 @@
+/***************************************************************************//**
+ \file halRfSpi.h
+
+ \brief SPI interface routines header.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFSPI_H
+#define _HALRFSPI_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <types.h>
+#include <atomic.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Performs initialization of SPI interface.
+******************************************************************************/
+void HAL_InitRfSpi(void);
+
+/******************************************************************************
+ Writes/reads byte to/from SPI.
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+uint8_t HAL_WriteByteRfSpi(uint8_t value);
+
+/******************************************************************************
+ Deselects a slave device.
+******************************************************************************/
+void HAL_DeselectRfSpi(void);
+
+/******************************************************************************
+ Selects a slave device.
+******************************************************************************/
+void HAL_SelectRfSpi(void);
+
+/******************************************************************************
+ Inline function (to use in critical sections)
+ Writes/reads byte to/from SPI.
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+INLINE uint8_t HAL_WriteByteInlineRfSpi(uint8_t value)
+{
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ SPDR = value; // Write data.
+ asm("nop"); // This "nop" tunes up the "while" to reduce time for SPIF flag
+ // detecting.
+ while (!(SPSR&(1 << SPIF)));
+ END_MEASURE(HALATOM_WRITEBYTE_RFSPI_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+ return SPDR;
+}
+
+
+#endif /* _HALRFSPI_H */
+
+//eof halRfSpi.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halMacIsr.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halMacIsr.c
new file mode 100644
index 00000000..e622372d
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halMacIsr.c
@@ -0,0 +1,152 @@
+/**************************************************************************//**
+ \file halMacIsr.c
+
+ \brief mac interrupts implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 14/01/08 A. Mandychev - Created.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halMacIsr.h>
+#include <halRfCtrl.h>
+#include <atomic.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#if defined(HAL_3d6864MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period >> 1)
+#elif defined(HAL_4MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period >> 1)
+#elif defined(HAL_7d3728MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period)
+#elif defined(HAL_8MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period)
+#endif
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+RTimerDescr_t __rtimer;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer.
+******************************************************************************/
+void HAL_InitMacIsr(void);
+
+/******************************************************************************
+ Redirect interrupt event depending on the TrxState.
+ Parameters: none.
+ Returns: none.
+******************************************************************************/
+void phyDispatcheRTimerEvent(void);
+
+/******************************************************************************
+ Redirect interrupt event depending on the TrxState.
+ Parameters: none.
+ Returns: none.
+******************************************************************************/
+void phyDispatcheRfInterrupt(void);
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer.
+******************************************************************************/
+void HAL_InitMacIsr(void)
+{
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+ HAL_InitRfIrq();
+}
+
+/******************************************************************************
+ Starts RTimer. Function should be invoked in critical section.
+ Parameters:
+ source - source of invocation.
+ mode - RTimer mode.
+ period - RTimer period.
+******************************************************************************/
+bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period)
+{
+ if (HAL_RTIMER_STOPPED_MODE != __rtimer.mode)
+ return false;
+
+ __rtimer.period = HAL_RTIMER_INTERVAL_CALCULATE(period);
+ __rtimer.mode = mode;
+ __rtimer.nextEvent = TCNT4 + __rtimer.period;
+ if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
+ __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
+ OCR4B = __rtimer.nextEvent;
+ // clear possible interrupt by setting logical one.
+ TIFR4 = (1 << OCF4B);
+ // enable interrupt
+ TIMSK4 |= (1 << OCIE4B);
+ return true;
+}
+
+/******************************************************************************
+ Stops RTimer. Function should be invoked in critical section.
+******************************************************************************/
+void HAL_StopRtimer(void)
+{
+ // clear possible interrupt
+ TIFR4 &= ~(1 << OCF4B);
+ // disable interrupt
+ TIMSK4 &= ~(1 << OCIE4B);
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+}
+
+/******************************************************************************
+ Output compare unit (channel B) interrupt handler.
+******************************************************************************/
+ISR(TIMER4_COMPB_vect)
+{
+ BEGIN_MEASURE
+ if (HAL_RTIMER_ONE_SHOT_MODE == __rtimer.mode)
+ {
+ TIMSK4 &= ~(1 << OCIE4B);
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+ }
+ else
+ {
+ __rtimer.nextEvent += __rtimer.period;
+ if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
+ __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
+ OCR4B = __rtimer.nextEvent;
+ }
+ phyDispatcheRTimerEvent();
+ END_MEASURE(HALISR_TIMER3_COMPA_TIME_LIMIT)
+}
+
+/****************************************************************
+ Interrupt service routine.
+ Do not move this ISR! It could be omitted in your project.
+****************************************************************/
+ISR(TIMER1_CAPT_vect)
+{
+ BEGIN_MEASURE
+ phyDispatcheRfInterrupt();
+ END_MEASURE(HALISR_PHYDISPATCH_RFINT_TIME_LIMIT)
+}
+
+// eof halMacIsr.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfCtrl.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfCtrl.c
new file mode 100644
index 00000000..ffbd38b5
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfCtrl.c
@@ -0,0 +1,214 @@
+/**************************************************************************//**
+ \file halRfCtrl.c
+
+ \brief mac pin interface implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 28/05/07 ALuzhetsky - Created.
+ 06/08/07 A. Mandychev, E. Ivanov - Modified.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <halW1.h>
+#include <halRfPio.h>
+#include <halRfSpi.h>
+#include <halRfCtrl.h>
+#include <halClkCtrl.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define RF_REGISTER_READ_ACCESS_MODE 0x80
+#define RF_REGISTER_TRX_STATUS_ADDRESS 0x01
+#define RF_TRX_OFF_STATE 0x08
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+#ifdef _HAL_RF_RX_TX_INDICATOR_
+/**************************************************************************//**
+\brief Turn on pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
+the radio transceiver.
+******************************************************************************/
+void phyRxTxSwitcherOn(void);
+
+#endif //_HAL_RF_RX_TX_INDICATOR_
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Delay in us
+******************************************************************************/
+void HAL_Delay(uint8_t us)
+{
+ __delay_us(us);
+}
+
+/******************************************************************************
+ Clears the irq.
+******************************************************************************/
+void HAL_ClearRfIrqFlag(void)
+{
+ TIFR1 = 1 << ICF1;
+}
+
+/******************************************************************************
+ Enables the irq.
+******************************************************************************/
+void HAL_EnableRfIrq(void)
+{
+ TIMSK1 |= 1 << ICIE1;
+}
+
+/******************************************************************************
+ Disables the irq.
+******************************************************************************/
+uint8_t HAL_DisableRfIrq(void)
+{
+ uint8_t tmp;
+
+ tmp = TIMSK1 & (1 << ICIE1);
+ TIMSK1 &= ~(1 << ICIE1);
+ return tmp;
+}
+
+/******************************************************************************
+ Sets SLP_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_set();
+}
+
+/******************************************************************************
+ Clears SLP_TR pin to 0.
+******************************************************************************/
+void HAL_ClearRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_clr();
+}
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeInRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_make_in();
+}
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeOutRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_make_out();
+}
+
+/******************************************************************************
+ Sets RST_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfRst(void)
+{
+ GPIO_RF_RST_set();
+}
+
+/******************************************************************************
+ Clears RST_TR pin to 1.
+******************************************************************************/
+void HAL_ClearRfRst(void)
+{
+ GPIO_RF_RST_clr();
+}
+
+/******************************************************************************
+ Init pins that controls RF chip.
+******************************************************************************/
+void HAL_InitRfPins(void)
+{
+ GPIO_RF_TST_make_out();
+ GPIO_RF_TST_clr();
+
+ GPIO_RF_SLP_TR_clr();
+ GPIO_RF_SLP_TR_make_out();
+
+ GPIO_RF_RST_set();
+ GPIO_RF_RST_make_out();
+
+ GPIO_RF_IRQ_make_in();
+}
+
+/******************************************************************************
+ Inits Atmega IRQ pin.
+******************************************************************************/
+void HAL_InitRfIrq(void)
+{
+ OCR1A = 0;
+ TCCR1B = (1 << WGM12) | (1 << CS10); // CTC mode, clock source is not prescaled
+ TCCR1B |= (1 << ICNC1) | (1 << ICES1); // input noise canceler and rising edge are enabled
+}
+
+/******************************************************************************
+ Returns current frequency code.
+******************************************************************************/
+HalSysFreq_t HAL_GetRfFreq(void)
+{
+/* if (INTERNAL_RC == halGetClockSource())
+ {
+ return HAL_FREQ_NOCLK;
+ }
+ else*/
+ {
+ #if (F_CPU == 4000000ul)
+ return HAL_FREQ_4MHZ;
+ #endif
+ #if (F_CPU == 8000000ul)
+ return HAL_FREQ_8MHZ;
+ #endif
+ }
+}
+
+/**************************************************************************//**
+ \brief Wait for when radio will be waked up.
+
+ \param none.
+ \return none.
+******************************************************************************/
+void halWaitRadio(void)
+{
+ uint8_t tempValue = 0;
+
+ do {
+ HAL_SelectRfSpi();
+ HAL_WriteByteRfSpi(RF_REGISTER_READ_ACCESS_MODE | RF_REGISTER_TRX_STATUS_ADDRESS);
+ tempValue = HAL_WriteByteRfSpi(tempValue);
+ HAL_DeselectRfSpi();
+ } while(RF_TRX_OFF_STATE != tempValue);
+}
+
+/**************************************************************************//**
+ \brief Enables RX TX indicator for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxTxSwitcher(void)
+{
+ #ifdef _HAL_RF_RX_TX_INDICATOR_
+ phyRxTxSwitcherOn();
+ #endif //_HAL_RF_RX_TX_INDICATOR_
+}
+
+//eof halRfCtrl.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfSpi.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfSpi.c
new file mode 100644
index 00000000..053ba68a
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halRfSpi.c
@@ -0,0 +1,90 @@
+/**************************************************************************//**
+ \file halrfSpi.c
+
+ \brief SPI interface routines.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#include <halRfSpi.h>
+#include <halRfPio.h>
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Performs initialization of SPI interface.
+******************************************************************************/
+void HAL_InitRfSpi(void)
+{
+ GPIO_SPI_CS_set();
+ GPIO_SPI_MISO_make_in();
+ GPIO_SPI_MOSI_make_out();
+ GPIO_SPI_SCK_make_out();
+ GPIO_SPI_CS_make_out();
+ SPCR = ((1 << SPE) | (1 << MSTR)); // SPI enable, master mode.
+ SPSR = (1 << SPI2X); // rate = fosc/2
+}
+
+/******************************************************************************
+ Writes/reads byte to/from SPI. CPU clock critical function (4 MHz only).
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+uint8_t HAL_WriteByteRfSpi(uint8_t value)
+{
+ uint8_t tmp8;
+
+ SPDR = value; // Write data.
+ asm("nop"); // 1
+ asm("nop"); // 2
+ asm("nop"); // 3
+ asm("nop"); // 4
+ asm("nop"); // 5
+ asm("nop"); // 5
+ asm("nop"); // 7
+ asm("nop"); // 8
+ asm("nop"); // 9
+ asm("nop"); // 10
+ asm("nop"); // 11
+ asm("nop"); // 12
+ asm("nop"); // 13
+ asm("nop"); // 14
+ asm("nop"); // 15
+ asm("nop"); // 16
+ asm("nop"); // 17
+ tmp8 = SPSR;
+ (void)tmp8;
+ return SPDR;
+}
+
+/******************************************************************************
+ Deselects a slave device.
+******************************************************************************/
+void HAL_DeselectRfSpi(void)
+{
+ GPIO_SPI_CS_set();
+}
+
+/******************************************************************************
+ Selects a slave device.
+******************************************************************************/
+void HAL_SelectRfSpi(void)
+{
+ GPIO_SPI_CS_clr();
+}
+
+// eof halrfSpi.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halUid.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halUid.c
new file mode 100644
index 00000000..21976ce9
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halUid.c
@@ -0,0 +1,87 @@
+/**************************************************************************//**
+ \file halUid.c
+
+ \brief Implementation of UID interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 7/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halRfPio.h>
+#include <halRfSpi.h>
+#include <uid.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+/** \brief uid type. */
+typedef union
+{
+ uint64_t uid;
+ uint8_t array[sizeof(uint64_t)];
+} HalUid_t;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static HalUid_t halUid = {.uid = 0ull};
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Reads uid from external spi eeprom at25010a.
+******************************************************************************/
+void halReadUid(void)
+{
+ uint8_t command = 0x03;
+ uint8_t address = 0;
+ uint8_t itr;
+
+ GPIO_RF_RST_make_out();
+ GPIO_RF_RST_clr();
+ GPIO_SPI_CS_clr();
+
+ HAL_WriteByteRfSpi(command);
+ HAL_WriteByteRfSpi(address);
+ for (itr=0; itr<sizeof(uint64_t); itr++)
+ {
+ halUid.array[itr] = HAL_WriteByteRfSpi(address);
+ }
+
+ GPIO_SPI_CS_set();
+}
+
+/******************************************************************************
+ Returns number which was read from external eeprom.
+ Parameters:
+ id - UID buffer pointer.
+ Returns:
+ 0 - if unique ID has been found without error;
+ -1 - if there are some erros during UID discovery.
+******************************************************************************/
+int HAL_ReadUid(uint64_t *id)
+{
+ if (!id)
+ return -1;
+
+ *id = halUid.uid;
+ return 0;
+}
+
+// eof uid.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halMacIsr.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halMacIsr.h
new file mode 100644
index 00000000..127af1ee
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halMacIsr.h
@@ -0,0 +1,77 @@
+/**************************************************************************//**
+ \file halMacIsr.h
+
+ \brief Declaration mac timer interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 14/01/08 A. Mandychev - Created.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALMACISR_H
+#define _HALMACISR_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halAppClock.h>
+
+/******************************************************************************
+ Types section.
+******************************************************************************/
+typedef enum
+{
+ HAL_RTIMER_REPEAT_MODE = 0,
+ HAL_RTIMER_ONE_SHOT_MODE = 1,
+ HAL_RTIMER_STOPPED_MODE = 2,
+} HAL_RTimerMode_t;
+
+typedef struct // Timer description.
+{
+ volatile HAL_RTimerMode_t mode; // Mode.
+ volatile uint16_t period; // Period of the timer.
+ volatile uint16_t nextEvent; // Counter of periods.
+} RTimerDescr_t;
+
+/******************************************************************************
+ External variables.
+******************************************************************************/
+// Do not use it in other files.
+extern RTimerDescr_t __rtimer;
+
+/******************************************************************************
+ Prototypes section.
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer and RF ext. interrupts.
+******************************************************************************/
+void HAL_InitMacIsr(void);
+
+/******************************************************************************
+ Starts RTimer. Function should be invoked in critical section.
+ Parameters:
+ source - source of invocation.
+ mode - RTimer mode.
+ period - RTimer period.
+******************************************************************************/
+bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period);
+
+/******************************************************************************
+ Stops RTimer. Function should be invoked in critical section.
+******************************************************************************/
+void HAL_StopRtimer(void);
+
+#endif /* _HALMACISR_H */
+
+// eof halMacIsr.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfCtrl.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfCtrl.h
new file mode 100644
index 00000000..846b101c
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfCtrl.h
@@ -0,0 +1,142 @@
+/**************************************************************************//**
+ \file halRfCtrl.h
+
+ \brief Types and constants declaration for IEEE802.15.4 PHY implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 28/05/07 ALuzhetsky - Created.
+ 06/08/07 A. Mandychev, E. Ivanov - Modified.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFCTRL_H
+#define _HALRFCTRL_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <types.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef enum
+{
+ HAL_FREQ_NOCLK,
+ HAL_FREQ_1MHZ,
+ HAL_FREQ_2MHZ,
+ HAL_FREQ_4MHZ,
+ HAL_FREQ_8MHZ,
+ HAL_FREQ_16MHZ
+} HalSysFreq_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Delay in us
+******************************************************************************/
+void HAL_Delay(uint8_t us);
+
+/******************************************************************************
+ Clears the irq.
+******************************************************************************/
+void HAL_ClearRfIrqFlag(void);
+
+/******************************************************************************
+ Enables the irq.
+******************************************************************************/
+void HAL_EnableRfIrq(void);
+
+/******************************************************************************
+ Disables the irq.
+******************************************************************************/
+uint8_t HAL_DisableRfIrq(void);
+
+/******************************************************************************
+ Sets SLP_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfSlpTr(void);
+
+/******************************************************************************
+ Clears SLP_TR pin to 0.
+******************************************************************************/
+void HAL_ClearRfSlpTr(void);
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeInRfSlpTr(void);
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeOutRfSlpTr(void);
+
+/******************************************************************************
+ Sets RST_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfRst(void);
+
+/******************************************************************************
+ Clears RST_TR pin to 1.
+******************************************************************************/
+void HAL_ClearRfRst(void);
+
+/******************************************************************************
+ Init pins that controls RF chip.
+******************************************************************************/
+void HAL_InitRfPins(void);
+
+/******************************************************************************
+ Inits Atmega IRQ pin.
+******************************************************************************/
+void HAL_InitRfIrq(void);
+
+/******************************************************************************
+ Returns current frequency code.
+******************************************************************************/
+HalSysFreq_t HAL_GetRfFreq(void);
+
+/**************************************************************************//**
+ \brief Wait for when radio will be waked up.
+
+ \param none.
+ \return none.
+******************************************************************************/
+void halWaitRadio(void);
+
+/**************************************************************************//**
+ \brief Enables RX TX indicator for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxTxSwitcher(void);
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity option for radio if that is supported.
+******************************************************************************/
+void HAL_InitAntennaDiversity(void);
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in RX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxAntennaDiversity(void);
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in TX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableTxAntennaDiversity(void);
+
+#endif /* _HALRFCTRL_H */
+
+// eof halRfCtrl.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfPio.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfPio.h
new file mode 100644
index 00000000..bbce93f4
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfPio.h
@@ -0,0 +1,41 @@
+/**************************************************************************//**
+ \file halRfPio.h
+
+ \brief AT86RF230 control pins declarations.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFPIO_H
+#define _HALRFPIO_H
+
+#include <gpio.h>
+
+// Macros for the RF_SLP_TR pin manipulation.
+HAL_ASSIGN_PIN(RF_SLP_TR, B, 4);
+// Macros for the RF_RST pin manipulation.
+HAL_ASSIGN_PIN(RF_RST, B, 5);
+// Macros for the RF_IRQ pin manipulation.
+HAL_ASSIGN_PIN(RF_IRQ, D, 0);
+// Macros for the SPI_CS pin manipulation.
+HAL_ASSIGN_PIN(SPI_CS, B, 0);
+// Macros for the SPI_SCK pin manipulation.
+HAL_ASSIGN_PIN(SPI_SCK, B, 1);
+// Macros for the SPI_MOSI pin manipulation.
+HAL_ASSIGN_PIN(SPI_MOSI, B, 2);
+// Macros for the SPI_MISO pin manipulation.
+HAL_ASSIGN_PIN(SPI_MISO, B, 3);
+#endif /* _HALRFPIO_H */
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfSpi.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfSpi.h
new file mode 100644
index 00000000..fe3328a6
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/include/halRfSpi.h
@@ -0,0 +1,79 @@
+/***************************************************************************//**
+ \file halRfSpi.h
+
+ \brief SPI interface routines header.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFSPI_H
+#define _HALRFSPI_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <types.h>
+#include <atomic.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Performs initialization of SPI interface.
+******************************************************************************/
+void HAL_InitRfSpi(void);
+
+/******************************************************************************
+ Writes/reads byte to/from SPI.
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+uint8_t HAL_WriteByteRfSpi(uint8_t value);
+
+/******************************************************************************
+ Deselects a slave device.
+******************************************************************************/
+void HAL_DeselectRfSpi(void);
+
+/******************************************************************************
+ Selects a slave device.
+******************************************************************************/
+void HAL_SelectRfSpi(void);
+
+/******************************************************************************
+ Inline function (to use in critical sections)
+ Writes/reads byte to/from SPI.
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+INLINE uint8_t HAL_WriteByteInlineRfSpi(uint8_t value)
+{
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ SPDR = value; // Write data.
+ asm("nop"); // This "nop" tunes up the "while" to reduce time for SPIF flag
+ // detecting.
+ while (!(SPSR&(1 << SPIF)));
+ END_MEASURE(HALATOM_WRITEBYTE_RFSPI_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+ return SPDR;
+}
+
+#endif /* _HALRFSPI_H */
+
+//eof halRfSpi.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halMacIsr.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halMacIsr.c
new file mode 100644
index 00000000..b468cbdf
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halMacIsr.c
@@ -0,0 +1,152 @@
+/**************************************************************************//**
+ \file halMacIsr.c
+
+ \brief mac interrupts implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 14/01/08 A. Mandychev - Created.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halMacIsr.h>
+#include <halRfCtrl.h>
+#include <atomic.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#if defined(HAL_3d6864MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period >> 1)
+#elif defined(HAL_4MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period >> 1)
+#elif defined(HAL_7d3728MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period)
+#elif defined(HAL_8MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period)
+#endif
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+RTimerDescr_t __rtimer;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer.
+******************************************************************************/
+void HAL_InitMacIsr(void);
+
+/******************************************************************************
+ Redirect interrupt event depending on the TrxState.
+ Parameters: none.
+ Returns: none.
+******************************************************************************/
+void phyDispatcheRTimerEvent(void);
+
+/******************************************************************************
+ Redirect interrupt event depending on the TrxState.
+ Parameters: none.
+ Returns: none.
+******************************************************************************/
+void phyDispatcheRfInterrupt(void);
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer.
+******************************************************************************/
+void HAL_InitMacIsr(void)
+{
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+ HAL_InitRfIrq();
+}
+
+/******************************************************************************
+ Starts RTimer. Function should be invoked in critical section.
+ Parameters:
+ source - source of invocation.
+ mode - RTimer mode.
+ period - RTimer period.
+******************************************************************************/
+bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period)
+{
+ if (HAL_RTIMER_STOPPED_MODE != __rtimer.mode)
+ return false;
+
+ __rtimer.period = HAL_RTIMER_INTERVAL_CALCULATE(period);
+ __rtimer.mode = mode;
+ __rtimer.nextEvent = TCNT4 + __rtimer.period;
+ if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
+ __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
+ OCR4B = __rtimer.nextEvent;
+ // clear possible interrupt by setting logical one.
+ TIFR4 = (1 << OCF4B);
+ // enable interrupt
+ TIMSK4 |= (1 << OCIE4B);
+ return true;
+}
+
+/******************************************************************************
+ Stops RTimer. Function should be invoked in critical section.
+******************************************************************************/
+void HAL_StopRtimer(void)
+{
+ // clear possible interrupt
+ TIFR4 &= ~(1 << OCF4B);
+ // disable interrupt
+ TIMSK4 &= ~(1 << OCIE4B);
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+}
+
+/******************************************************************************
+ Output compare unit (channel B) interrupt handler.
+******************************************************************************/
+ISR(TIMER4_COMPB_vect)
+{
+ BEGIN_MEASURE
+ if (HAL_RTIMER_ONE_SHOT_MODE == __rtimer.mode)
+ {
+ TIMSK4 &= ~(1 << OCIE4B);
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+ }
+ else
+ {
+ __rtimer.nextEvent += __rtimer.period;
+ if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
+ __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
+ OCR4B = __rtimer.nextEvent;
+ }
+ phyDispatcheRTimerEvent();
+ END_MEASURE(HALISR_TIMER3_COMPA_TIME_LIMIT)
+}
+
+/****************************************************************
+ Interrupt service routine.
+ Do not move this ISR! It could be omitted in your project.
+****************************************************************/
+ISR(INT0_vect)
+{
+ BEGIN_MEASURE
+ phyDispatcheRfInterrupt();
+ END_MEASURE(HALISR_PHYDISPATCH_RFINT_TIME_LIMIT)
+}
+
+// eof halMacIsr.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfCtrl.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfCtrl.c
new file mode 100644
index 00000000..c21a9784
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfCtrl.c
@@ -0,0 +1,257 @@
+/**************************************************************************//**
+ \file halRfCtrl.c
+
+ \brief mac pin interface implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 28/05/07 ALuzhetsky - Created.
+ 06/08/07 A. Mandychev, E. Ivanov - Modified.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <halW1.h>
+#include <halRfPio.h>
+#include <halRfSpi.h>
+#include <halRfCtrl.h>
+#include <halClkCtrl.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define RF_REGISTER_READ_ACCESS_MODE 0x80
+#define RF_REGISTER_TRX_STATUS_ADDRESS 0x01
+#define RF_TRX_OFF_STATE 0x08
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+#ifdef _HAL_RF_RX_TX_INDICATOR_
+/**************************************************************************//**
+\brief Turn on pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
+the radio transceiver.
+******************************************************************************/
+void phyRxTxSwitcherOn(void);
+
+#endif //_HAL_RF_RX_TX_INDICATOR_
+
+#ifdef _HAL_ANT_DIVERSITY_
+/**************************************************************************//**
+\brief Enable antenna diversity feature.
+******************************************************************************/
+void phyAntennaDiversityInit(void);
+
+/**************************************************************************//**
+\brief Enable antenna diversity in the receive state of the radio transceiver.
+******************************************************************************/
+void phyRxAntennaDiversity(void);
+
+/**************************************************************************//**
+\brief Enable antenna diversity in the transmit state of the radio transceiver.
+******************************************************************************/
+void phyTxAntennaDiversity(void);
+
+#endif //_HAL_ANT_DIVERSITY_
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Delay in us
+******************************************************************************/
+void HAL_Delay(uint8_t us)
+{
+ __delay_us(us);
+}
+
+/******************************************************************************
+ Clears the irq.
+******************************************************************************/
+void HAL_ClearRfIrqFlag(void)
+{
+ EIFR = 1 << INTF0;
+}
+
+/******************************************************************************
+ Enables the irq.
+******************************************************************************/
+void HAL_EnableRfIrq(void)
+{
+ EIMSK |= 1 << INT0;
+}
+
+/******************************************************************************
+ Disables the irq.
+******************************************************************************/
+uint8_t HAL_DisableRfIrq(void)
+{
+ uint8_t tmp;
+
+ tmp = EIMSK & (1 << INT0);
+ EIMSK &= ~(1 << INT0);
+ return tmp;
+}
+
+/******************************************************************************
+ Sets SLP_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_set();
+}
+
+/******************************************************************************
+ Clears SLP_TR pin to 0.
+******************************************************************************/
+void HAL_ClearRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_clr();
+}
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeInRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_make_in();
+}
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeOutRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_make_out();
+}
+
+/******************************************************************************
+ Sets RST_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfRst(void)
+{
+ GPIO_RF_RST_set();
+}
+
+/******************************************************************************
+ Clears RST_TR pin to 1.
+******************************************************************************/
+void HAL_ClearRfRst(void)
+{
+ GPIO_RF_RST_clr();
+}
+
+/******************************************************************************
+ Init pins that controls RF chip.
+******************************************************************************/
+void HAL_InitRfPins(void)
+{
+ GPIO_RF_SLP_TR_clr();
+ GPIO_RF_SLP_TR_make_out();
+
+ GPIO_RF_RST_set();
+ GPIO_RF_RST_make_out();
+
+ GPIO_RF_IRQ_make_in();
+}
+
+/******************************************************************************
+ Inits Atmega IRQ pin.
+******************************************************************************/
+void HAL_InitRfIrq(void)
+{
+ EICRA |= (1 << ISC01) | (1 << ISC00); // rising edge
+}
+
+/******************************************************************************
+ Returns current frequency code.
+******************************************************************************/
+HalSysFreq_t HAL_GetRfFreq(void)
+{
+/* if (INTERNAL_RC == halGetClockSource())
+ {
+ return HAL_FREQ_NOCLK;
+ }
+ else*/
+ {
+ #if (F_CPU == 4000000ul)
+ return HAL_FREQ_4MHZ;
+ #endif
+ #if (F_CPU == 8000000ul)
+ return HAL_FREQ_8MHZ;
+ #endif
+ }
+}
+
+/**************************************************************************//**
+ \brief Wait for when radio will be waked up.
+
+ \param none.
+ \return none.
+******************************************************************************/
+void halWaitRadio(void)
+{
+ uint8_t tempValue = 0;
+
+ do {
+ HAL_SelectRfSpi();
+ HAL_WriteByteRfSpi(RF_REGISTER_READ_ACCESS_MODE | RF_REGISTER_TRX_STATUS_ADDRESS);
+ tempValue = HAL_WriteByteRfSpi(tempValue);
+ HAL_DeselectRfSpi();
+ } while(RF_TRX_OFF_STATE != tempValue);
+}
+
+/**************************************************************************//**
+ \brief Enables RX TX indicator for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxTxSwitcher(void)
+{
+ #ifdef _HAL_RF_RX_TX_INDICATOR_
+ phyRxTxSwitcherOn();
+ #endif //_HAL_RF_RX_TX_INDICATOR_
+}
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity option for radio if that is supported.
+******************************************************************************/
+void HAL_InitAntennaDiversity(void)
+{
+ #ifdef _HAL_ANT_DIVERSITY_
+ phyAntennaDiversityInit();
+ #endif //_HAL_ANT_DIVERSITY_
+}
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in RX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxAntennaDiversity(void)
+{
+ #ifdef _HAL_ANT_DIVERSITY_
+ phyRxAntennaDiversity();
+ #endif //_HAL_ANT_DIVERSITY_
+}
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in TX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableTxAntennaDiversity(void)
+{
+ #ifdef _HAL_ANT_DIVERSITY_
+ phyTxAntennaDiversity();
+ #endif //_HAL_ANT_DIVERSITY_
+}
+
+//eof halRfCtrl.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfSpi.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfSpi.c
new file mode 100644
index 00000000..053ba68a
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halRfSpi.c
@@ -0,0 +1,90 @@
+/**************************************************************************//**
+ \file halrfSpi.c
+
+ \brief SPI interface routines.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#include <halRfSpi.h>
+#include <halRfPio.h>
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Performs initialization of SPI interface.
+******************************************************************************/
+void HAL_InitRfSpi(void)
+{
+ GPIO_SPI_CS_set();
+ GPIO_SPI_MISO_make_in();
+ GPIO_SPI_MOSI_make_out();
+ GPIO_SPI_SCK_make_out();
+ GPIO_SPI_CS_make_out();
+ SPCR = ((1 << SPE) | (1 << MSTR)); // SPI enable, master mode.
+ SPSR = (1 << SPI2X); // rate = fosc/2
+}
+
+/******************************************************************************
+ Writes/reads byte to/from SPI. CPU clock critical function (4 MHz only).
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+uint8_t HAL_WriteByteRfSpi(uint8_t value)
+{
+ uint8_t tmp8;
+
+ SPDR = value; // Write data.
+ asm("nop"); // 1
+ asm("nop"); // 2
+ asm("nop"); // 3
+ asm("nop"); // 4
+ asm("nop"); // 5
+ asm("nop"); // 5
+ asm("nop"); // 7
+ asm("nop"); // 8
+ asm("nop"); // 9
+ asm("nop"); // 10
+ asm("nop"); // 11
+ asm("nop"); // 12
+ asm("nop"); // 13
+ asm("nop"); // 14
+ asm("nop"); // 15
+ asm("nop"); // 16
+ asm("nop"); // 17
+ tmp8 = SPSR;
+ (void)tmp8;
+ return SPDR;
+}
+
+/******************************************************************************
+ Deselects a slave device.
+******************************************************************************/
+void HAL_DeselectRfSpi(void)
+{
+ GPIO_SPI_CS_set();
+}
+
+/******************************************************************************
+ Selects a slave device.
+******************************************************************************/
+void HAL_SelectRfSpi(void)
+{
+ GPIO_SPI_CS_clr();
+}
+
+// eof halrfSpi.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halUid.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halUid.c
new file mode 100644
index 00000000..21976ce9
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb231_212/src/halUid.c
@@ -0,0 +1,87 @@
+/**************************************************************************//**
+ \file halUid.c
+
+ \brief Implementation of UID interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 7/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halRfPio.h>
+#include <halRfSpi.h>
+#include <uid.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+/** \brief uid type. */
+typedef union
+{
+ uint64_t uid;
+ uint8_t array[sizeof(uint64_t)];
+} HalUid_t;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static HalUid_t halUid = {.uid = 0ull};
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Reads uid from external spi eeprom at25010a.
+******************************************************************************/
+void halReadUid(void)
+{
+ uint8_t command = 0x03;
+ uint8_t address = 0;
+ uint8_t itr;
+
+ GPIO_RF_RST_make_out();
+ GPIO_RF_RST_clr();
+ GPIO_SPI_CS_clr();
+
+ HAL_WriteByteRfSpi(command);
+ HAL_WriteByteRfSpi(address);
+ for (itr=0; itr<sizeof(uint64_t); itr++)
+ {
+ halUid.array[itr] = HAL_WriteByteRfSpi(address);
+ }
+
+ GPIO_SPI_CS_set();
+}
+
+/******************************************************************************
+ Returns number which was read from external eeprom.
+ Parameters:
+ id - UID buffer pointer.
+ Returns:
+ 0 - if unique ID has been found without error;
+ -1 - if there are some erros during UID discovery.
+******************************************************************************/
+int HAL_ReadUid(uint64_t *id)
+{
+ if (!id)
+ return -1;
+
+ *id = halUid.uid;
+ return 0;
+}
+
+// eof uid.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halAtmelUid.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halAtmelUid.h
new file mode 100644
index 00000000..d3931966
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halAtmelUid.h
@@ -0,0 +1,68 @@
+/**************************************************************************//**
+ \file halAtmelUid.h
+
+ \brief The header file describes the UID interface for Atmel MeshBean.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/12/10 A. Malkin - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALATMELUID_H
+#define _HALATMELUID_H
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <gpio.h>
+
+/******************************************************************************
+ Definitions section
+******************************************************************************/
+#define SINGLE_WIRE_SUCCESS_STATUS 0
+#define SINGLE_WIRE_ERROR_STATUS -1
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+/** \brief uid type. */
+typedef union
+{
+ uint64_t uid;
+ uint8_t array[sizeof(uint64_t)];
+} HalUid_t;
+
+/******************************************************************************
+ Inline static functions prototypes section.
+******************************************************************************/
+HAL_ASSIGN_PIN(SINGLE_WIRE, G, 5); // Macros for the Single-wire pin actions.
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Reads Atmel MeshBean UID from ATTiny13A
+
+\param[in] uidBuffer - memory for unique ID.
+
+\return
+ SINGLE_WIRE_SUCCESS_STATUS - if UID read successfully;
+ SINGLE_WIRE_ERROR_STATUS - if error occured during UID read.
+******************************************************************************/
+int halReadAtmelMeshbeanUid(uint8_t *uidBuffer);
+
+#endif /* _HALATMELUID_H */
+
+// eof halAtmelUid.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halMacIsr.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halMacIsr.h
new file mode 100644
index 00000000..127af1ee
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halMacIsr.h
@@ -0,0 +1,77 @@
+/**************************************************************************//**
+ \file halMacIsr.h
+
+ \brief Declaration mac timer interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 14/01/08 A. Mandychev - Created.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALMACISR_H
+#define _HALMACISR_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halAppClock.h>
+
+/******************************************************************************
+ Types section.
+******************************************************************************/
+typedef enum
+{
+ HAL_RTIMER_REPEAT_MODE = 0,
+ HAL_RTIMER_ONE_SHOT_MODE = 1,
+ HAL_RTIMER_STOPPED_MODE = 2,
+} HAL_RTimerMode_t;
+
+typedef struct // Timer description.
+{
+ volatile HAL_RTimerMode_t mode; // Mode.
+ volatile uint16_t period; // Period of the timer.
+ volatile uint16_t nextEvent; // Counter of periods.
+} RTimerDescr_t;
+
+/******************************************************************************
+ External variables.
+******************************************************************************/
+// Do not use it in other files.
+extern RTimerDescr_t __rtimer;
+
+/******************************************************************************
+ Prototypes section.
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer and RF ext. interrupts.
+******************************************************************************/
+void HAL_InitMacIsr(void);
+
+/******************************************************************************
+ Starts RTimer. Function should be invoked in critical section.
+ Parameters:
+ source - source of invocation.
+ mode - RTimer mode.
+ period - RTimer period.
+******************************************************************************/
+bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period);
+
+/******************************************************************************
+ Stops RTimer. Function should be invoked in critical section.
+******************************************************************************/
+void HAL_StopRtimer(void);
+
+#endif /* _HALMACISR_H */
+
+// eof halMacIsr.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfCtrl.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfCtrl.h
new file mode 100644
index 00000000..846b101c
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfCtrl.h
@@ -0,0 +1,142 @@
+/**************************************************************************//**
+ \file halRfCtrl.h
+
+ \brief Types and constants declaration for IEEE802.15.4 PHY implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 28/05/07 ALuzhetsky - Created.
+ 06/08/07 A. Mandychev, E. Ivanov - Modified.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFCTRL_H
+#define _HALRFCTRL_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <types.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef enum
+{
+ HAL_FREQ_NOCLK,
+ HAL_FREQ_1MHZ,
+ HAL_FREQ_2MHZ,
+ HAL_FREQ_4MHZ,
+ HAL_FREQ_8MHZ,
+ HAL_FREQ_16MHZ
+} HalSysFreq_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Delay in us
+******************************************************************************/
+void HAL_Delay(uint8_t us);
+
+/******************************************************************************
+ Clears the irq.
+******************************************************************************/
+void HAL_ClearRfIrqFlag(void);
+
+/******************************************************************************
+ Enables the irq.
+******************************************************************************/
+void HAL_EnableRfIrq(void);
+
+/******************************************************************************
+ Disables the irq.
+******************************************************************************/
+uint8_t HAL_DisableRfIrq(void);
+
+/******************************************************************************
+ Sets SLP_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfSlpTr(void);
+
+/******************************************************************************
+ Clears SLP_TR pin to 0.
+******************************************************************************/
+void HAL_ClearRfSlpTr(void);
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeInRfSlpTr(void);
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeOutRfSlpTr(void);
+
+/******************************************************************************
+ Sets RST_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfRst(void);
+
+/******************************************************************************
+ Clears RST_TR pin to 1.
+******************************************************************************/
+void HAL_ClearRfRst(void);
+
+/******************************************************************************
+ Init pins that controls RF chip.
+******************************************************************************/
+void HAL_InitRfPins(void);
+
+/******************************************************************************
+ Inits Atmega IRQ pin.
+******************************************************************************/
+void HAL_InitRfIrq(void);
+
+/******************************************************************************
+ Returns current frequency code.
+******************************************************************************/
+HalSysFreq_t HAL_GetRfFreq(void);
+
+/**************************************************************************//**
+ \brief Wait for when radio will be waked up.
+
+ \param none.
+ \return none.
+******************************************************************************/
+void halWaitRadio(void);
+
+/**************************************************************************//**
+ \brief Enables RX TX indicator for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxTxSwitcher(void);
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity option for radio if that is supported.
+******************************************************************************/
+void HAL_InitAntennaDiversity(void);
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in RX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxAntennaDiversity(void);
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in TX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableTxAntennaDiversity(void);
+
+#endif /* _HALRFCTRL_H */
+
+// eof halRfCtrl.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfPio.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfPio.h
new file mode 100644
index 00000000..05f3f296
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfPio.h
@@ -0,0 +1,41 @@
+/**************************************************************************//**
+ \file halRfPio.h
+
+ \brief AT86RF230 control pins declarations.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFPIO_H
+#define _HALRFPIO_H
+
+#include <gpio.h>
+
+// Macros for the RF_SLP_TR pin manipulation.
+HAL_ASSIGN_PIN(RF_SLP_TR, B, 4);
+// Macros for the RF_RST pin manipulation.
+HAL_ASSIGN_PIN(RF_RST, A, 7);
+// Macros for the RF_IRQ pin manipulation.
+HAL_ASSIGN_PIN(RF_IRQ, E, 5);
+// Macros for the SPI_CS pin manipulation.
+HAL_ASSIGN_PIN(SPI_CS, B, 0);
+// Macros for the SPI_SCK pin manipulation.
+HAL_ASSIGN_PIN(SPI_SCK, B, 1);
+// Macros for the SPI_MOSI pin manipulation.
+HAL_ASSIGN_PIN(SPI_MOSI, B, 2);
+// Macros for the SPI_MISO pin manipulation.
+HAL_ASSIGN_PIN(SPI_MISO, B, 3);
+#endif /* _HALRFPIO_H */
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfSpi.h b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfSpi.h
new file mode 100644
index 00000000..fe3328a6
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/include/halRfSpi.h
@@ -0,0 +1,79 @@
+/***************************************************************************//**
+ \file halRfSpi.h
+
+ \brief SPI interface routines header.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#ifndef _HALRFSPI_H
+#define _HALRFSPI_H
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <types.h>
+#include <atomic.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Performs initialization of SPI interface.
+******************************************************************************/
+void HAL_InitRfSpi(void);
+
+/******************************************************************************
+ Writes/reads byte to/from SPI.
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+uint8_t HAL_WriteByteRfSpi(uint8_t value);
+
+/******************************************************************************
+ Deselects a slave device.
+******************************************************************************/
+void HAL_DeselectRfSpi(void);
+
+/******************************************************************************
+ Selects a slave device.
+******************************************************************************/
+void HAL_SelectRfSpi(void);
+
+/******************************************************************************
+ Inline function (to use in critical sections)
+ Writes/reads byte to/from SPI.
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+INLINE uint8_t HAL_WriteByteInlineRfSpi(uint8_t value)
+{
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ SPDR = value; // Write data.
+ asm("nop"); // This "nop" tunes up the "while" to reduce time for SPIF flag
+ // detecting.
+ while (!(SPSR&(1 << SPIF)));
+ END_MEASURE(HALATOM_WRITEBYTE_RFSPI_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+ return SPDR;
+}
+
+#endif /* _HALRFSPI_H */
+
+//eof halRfSpi.h
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halAtmelUid.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halAtmelUid.c
new file mode 100644
index 00000000..9c80dcbe
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halAtmelUid.c
@@ -0,0 +1,248 @@
+/**************************************************************************//**
+ \file halAtmelUid.c
+
+ \brief The header file describes the UID interface for Atmel MeshBean.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/12/10 A. Malkin - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halAtmelUid.h>
+#include <halW1.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define UID_LEN 0x08 // size of UID (bytes)
+
+#define SINGLE_WIRE_ZERO_BIT 0x00 // logical level zero
+#define SINGLE_WIRE_ONE_BIT 0x01 // logical level one
+
+#define BIT_DURATION 208 // bit duration - 208 us
+#define BIT_DURATION_HALF BIT_DURATION/2
+#define MAX_START_BIT_WAITING 10 // waiting time(in bit) of Start bit
+
+// Commands for Single-wire UART (Atmel MeshBean)
+#define CMD_SYNCH 0xAA // Synchronization
+#define CMD_READ_MAC64_0 0x60 // Read 64-bit MAC address
+#define NUM_OF_MAC64 0x00 // Number of MAC64 slot
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+static void halWriteSingleWireBit(uint8_t value);
+static void halWriteSingleWire(uint8_t value);
+static int halWaitSingleWireStartBit(void);
+static uint8_t halReadSingleWireBit(void);
+static int halReadSingleWire(uint8_t *data);
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/***************************************************************************//**
+\brief Writes bit to the bus
+
+\param[in]
+ value - byte to write. The bit is placed to position of LSB.
+*******************************************************************************/
+static void halWriteSingleWireBit(uint8_t value)
+{
+ if (value)
+ GPIO_SINGLE_WIRE_set();
+ else
+ GPIO_SINGLE_WIRE_clr();
+
+ __delay_us(BIT_DURATION);
+}
+
+/***************************************************************************//**
+\brief Writes byte to the bus
+
+\param[in]
+ value - byte to write.
+*******************************************************************************/
+static void halWriteSingleWire(uint8_t value)
+{
+ uint8_t i;
+
+ // write Start bit
+ halWriteSingleWireBit(SINGLE_WIRE_ZERO_BIT);
+
+ // write Data
+ for (i = 0; i < UID_LEN; i++)
+ {
+ halWriteSingleWireBit(value & 0x01);
+ value >>= 1;
+ }
+
+ // add 2 Stop bits
+ halWriteSingleWireBit(SINGLE_WIRE_ONE_BIT);
+ halWriteSingleWireBit(SINGLE_WIRE_ONE_BIT);
+}
+
+/***************************************************************************//**
+\brief Wait for Start bit from the bus.
+
+\return
+ SINGLE_WIRE_SUCCESS_STATUS - if Start bit has been found successfully; \n
+ SINGLE_WIRE_ERROR_STATUS - if Start bit has been not found.
+*******************************************************************************/
+static int halWaitSingleWireStartBit(void)
+{
+ uint16_t i;
+
+ for (i = BIT_DURATION * MAX_START_BIT_WAITING; i > 0; i--)
+ {
+ if (!GPIO_SINGLE_WIRE_read())
+ return SINGLE_WIRE_SUCCESS_STATUS;
+ __delay_us(1);
+ }
+
+ return SINGLE_WIRE_ERROR_STATUS;
+}
+
+/***************************************************************************//**
+\brief Reads bit from the bus.
+
+\return
+ Read bit is placed to position of last significant bit.
+*******************************************************************************/
+static uint8_t halReadSingleWireBit(void)
+{
+ uint8_t result;
+
+ // read pin level in half of bit period
+ if (GPIO_SINGLE_WIRE_read())
+ result = SINGLE_WIRE_ONE_BIT;
+ else
+ result = SINGLE_WIRE_ZERO_BIT;
+
+ // wait for bit period before next bit reading
+ __delay_us(BIT_DURATION);
+
+ return result;
+}
+
+/***************************************************************************//**
+\brief Reads byte from the Atmel Single-wire bus.
+
+\param[in]
+ data - byte read from the bus.
+
+\return
+ SINGLE_WIRE_SUCCESS_STATUS - if byte read without error;
+ SINGLE_WIRE_ERROR_STATUS - if there are some errors during byte read.
+*******************************************************************************/
+static int halReadSingleWire(uint8_t *data)
+{
+ uint8_t reg = 0;
+ uint8_t bit;
+ uint8_t i;
+ int result;
+
+ // wait for Start bit of response
+ result = halWaitSingleWireStartBit();
+
+ if (result)
+ return SINGLE_WIRE_ERROR_STATUS;
+
+ // wait for half of bit period before reading Start bit
+ __delay_us(BIT_DURATION_HALF);
+
+ // read Start bit
+ bit = halReadSingleWireBit();
+ if (SINGLE_WIRE_ZERO_BIT != bit)
+ return SINGLE_WIRE_ERROR_STATUS;
+
+ // read byte
+ for (i = 0; i < UID_LEN; i++)
+ {
+ if (SINGLE_WIRE_ONE_BIT == halReadSingleWireBit())
+ reg |= (1 << i);
+ }
+
+ // read and check 1'st Stop bit
+ bit = halReadSingleWireBit();
+ if (SINGLE_WIRE_ONE_BIT != bit)
+ return SINGLE_WIRE_ERROR_STATUS;
+
+ // wait for bit period after reading
+ __delay_us(BIT_DURATION_HALF);
+
+ *data = reg;
+
+ return SINGLE_WIRE_SUCCESS_STATUS;
+}
+
+/**************************************************************************//**
+\brief Reads Atmel MeshBean UID from ATTiny13A
+
+\param[in] uidBuffer - memory for unique ID.
+
+\return
+ SINGLE_WIRE_SUCCESS_STATUS - if UID read successfully;
+ SINGLE_WIRE_ERROR_STATUS - if error occured during UID read.
+******************************************************************************/
+int halReadAtmelMeshbeanUid(uint8_t *uidBuffer)
+{
+ uint8_t i;
+ uint8_t reg = 0;
+
+ // port sets as output.
+ GPIO_SINGLE_WIRE_make_out();
+
+ // send synchronization byte
+ halWriteSingleWire(CMD_SYNCH);
+
+ // write command
+ halWriteSingleWire(CMD_READ_MAC64_0);
+
+ // write UID location
+ halWriteSingleWire(NUM_OF_MAC64);
+
+ // Tri-state (external pullup)
+ GPIO_SINGLE_WIRE_make_in();
+
+ // wait for synchronization
+ if (halReadSingleWire(&reg))
+ return SINGLE_WIRE_ERROR_STATUS;
+
+ if (CMD_SYNCH != reg)
+ return SINGLE_WIRE_ERROR_STATUS;
+
+ // wait for response with same command ID
+ if (halReadSingleWire(&reg))
+ return SINGLE_WIRE_ERROR_STATUS;
+
+ if (CMD_READ_MAC64_0 == reg)
+ {
+ // wait for 8 bytes of UID
+ for (i = UID_LEN; i > 0; i--)
+ {
+ // fill array in reversionary order
+ if (halReadSingleWire(uidBuffer + i - 1))
+ return SINGLE_WIRE_ERROR_STATUS;
+ }
+ }
+ return SINGLE_WIRE_SUCCESS_STATUS;
+}
+
+// eof halAtmelUid.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halMacIsr.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halMacIsr.c
new file mode 100644
index 00000000..7f349660
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halMacIsr.c
@@ -0,0 +1,152 @@
+/**************************************************************************//**
+ \file halMacIsr.c
+
+ \brief mac interrupts implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 14/01/08 A. Mandychev - Created.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halMacIsr.h>
+#include <halRfCtrl.h>
+#include <atomic.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#if defined(HAL_3d6864MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period >> 1)
+#elif defined(HAL_4MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period >> 1)
+#elif defined(HAL_7d3728MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period)
+#elif defined(HAL_8MHz)
+ #define HAL_RTIMER_INTERVAL_CALCULATE(period) (period)
+#endif
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+RTimerDescr_t __rtimer;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer.
+******************************************************************************/
+void HAL_InitMacIsr(void);
+
+/******************************************************************************
+ Redirect interrupt event depending on the TrxState.
+ Parameters: none.
+ Returns: none.
+******************************************************************************/
+void phyDispatcheRTimerEvent(void);
+
+/******************************************************************************
+ Redirect interrupt event depending on the TrxState.
+ Parameters: none.
+ Returns: none.
+******************************************************************************/
+void phyDispatcheRfInterrupt(void);
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Initializes Rtimer.
+******************************************************************************/
+void HAL_InitMacIsr(void)
+{
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+ HAL_InitRfIrq();
+}
+
+/******************************************************************************
+ Starts RTimer. Function should be invoked in critical section.
+ Parameters:
+ source - source of invocation.
+ mode - RTimer mode.
+ period - RTimer period.
+******************************************************************************/
+bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period)
+{
+ if (HAL_RTIMER_STOPPED_MODE != __rtimer.mode)
+ return false;
+
+ __rtimer.period = HAL_RTIMER_INTERVAL_CALCULATE(period);
+ __rtimer.mode = mode;
+ __rtimer.nextEvent = TCNT4 + __rtimer.period;
+ if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
+ __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
+ OCR4B = __rtimer.nextEvent;
+ // clear possible interrupt by setting logical one.
+ TIFR4 = (1 << OCF4B);
+ // enable interrupt
+ TIMSK4 |= (1 << OCIE4B);
+ return true;
+}
+
+/******************************************************************************
+ Stops RTimer. Function should be invoked in critical section.
+******************************************************************************/
+void HAL_StopRtimer(void)
+{
+ // clear possible interrupt
+ TIFR4 &= ~(1 << OCF4B);
+ // disable interrupt
+ TIMSK4 &= ~(1 << OCIE4B);
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+}
+
+/******************************************************************************
+ Output compare unit (channel B) interrupt handler.
+******************************************************************************/
+ISR(TIMER4_COMPB_vect)
+{
+ BEGIN_MEASURE
+ if (HAL_RTIMER_ONE_SHOT_MODE == __rtimer.mode)
+ {
+ TIMSK4 &= ~(1 << OCIE4B);
+ __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
+ }
+ else
+ {
+ __rtimer.nextEvent += __rtimer.period;
+ if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
+ __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
+ OCR4B = __rtimer.nextEvent;
+ }
+ phyDispatcheRTimerEvent();
+ END_MEASURE(HALISR_TIMER3_COMPA_TIME_LIMIT)
+}
+
+/****************************************************************
+ Interrupt service routine.
+ Do not move this ISR! It could be omitted in your project.
+****************************************************************/
+ISR(INT5_vect)
+{
+ BEGIN_MEASURE
+ phyDispatcheRfInterrupt();
+ END_MEASURE(HALISR_PHYDISPATCH_RFINT_TIME_LIMIT)
+}
+
+// eof halMacIsr.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfCtrl.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfCtrl.c
new file mode 100644
index 00000000..8e3232a7
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfCtrl.c
@@ -0,0 +1,281 @@
+/**************************************************************************//**
+ \file halRfCtrl.c
+
+ \brief mac pin interface implementation.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 28/05/07 ALuzhetsky - Created.
+ 06/08/07 A. Mandychev, E. Ivanov - Modified.
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section.
+******************************************************************************/
+#include <halClkCtrl.h>
+#include <halW1.h>
+#include <halRfPio.h>
+#include <halRfCtrl.h>
+#include <halRfSpi.h>
+#include <halClkCtrl.h>
+//#include <phyRxTxIndicator.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define RF_REGISTER_READ_ACCESS_MODE 0x80
+#define RF_REGISTER_TRX_STATUS_ADDRESS 0x01
+#define RF_TRX_OFF_STATE 0x08
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+#ifdef _HAL_RF_RX_TX_INDICATOR_
+/**************************************************************************//**
+\brief Turn on pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
+the radio transceiver.
+******************************************************************************/
+void phyRxTxSwitcherOn(void);
+
+#endif //_HAL_RF_RX_TX_INDICATOR_
+
+#ifdef _HAL_ANT_DIVERSITY_
+/**************************************************************************//**
+\brief Enable antenna diversity feature.
+******************************************************************************/
+void phyAntennaDiversityInit(void);
+
+/**************************************************************************//**
+\brief Enable antenna diversity in the receive state of the radio transceiver.
+******************************************************************************/
+void phyRxAntennaDiversity(void);
+
+/**************************************************************************//**
+\brief Enable antenna diversity in the transmit state of the radio transceiver.
+******************************************************************************/
+void phyTxAntennaDiversity(void);
+
+#endif //_HAL_ANT_DIVERSITY_
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Delay in us
+******************************************************************************/
+void HAL_Delay(uint8_t us)
+{
+ __delay_us(us);
+}
+
+/******************************************************************************
+ Clears the irq.
+******************************************************************************/
+void HAL_ClearRfIrqFlag(void)
+{
+ EIFR = 1 << INTF5;
+}
+
+/******************************************************************************
+ Enables the irq.
+******************************************************************************/
+void HAL_EnableRfIrq(void)
+{
+ EIMSK |= 1 << INT5;
+}
+
+/******************************************************************************
+ Disables the irq.
+******************************************************************************/
+uint8_t HAL_DisableRfIrq(void)
+{
+ uint8_t tmp;
+
+ tmp = EIMSK & (1 << INT5);
+ EIMSK &= ~(1 << INT5);
+ return tmp;
+}
+
+/******************************************************************************
+ Sets SLP_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_set();
+}
+
+/******************************************************************************
+ Clears SLP_TR pin to 0.
+******************************************************************************/
+void HAL_ClearRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_clr();
+}
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeInRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_make_in();
+}
+
+/******************************************************************************
+ Makes SLP_TR pin as input.
+******************************************************************************/
+void HAL_MakeOutRfSlpTr(void)
+{
+ GPIO_RF_SLP_TR_make_out();
+}
+
+/******************************************************************************
+ Sets RST_TR pin to 1.
+******************************************************************************/
+void HAL_SetRfRst(void)
+{
+ GPIO_RF_RST_set();
+}
+
+/******************************************************************************
+ Clears RST_TR pin to 1.
+******************************************************************************/
+void HAL_ClearRfRst(void)
+{
+ GPIO_RF_RST_clr();
+}
+
+/******************************************************************************
+ Init pins that controls RF chip.
+******************************************************************************/
+void HAL_InitRfPins(void)
+{
+ GPIO_RF_SLP_TR_clr();
+ GPIO_RF_SLP_TR_make_out();
+
+ GPIO_RF_RST_set();
+ GPIO_RF_RST_make_out();
+
+ GPIO_RF_IRQ_make_in();
+
+ #ifdef _HAL_USE_AMPLIFIER_
+ // enable power amplifier
+ // make port C pin 1 as output
+ GPIO_POW_AMPLF_SLP_make_out();
+ // set one on pin
+ GPIO_POW_AMPLF_SLP_set();
+ #endif
+}
+
+/******************************************************************************
+ Inits Atmega IRQ pin.
+******************************************************************************/
+void HAL_InitRfIrq(void)
+{
+ EICRB |= (1 << ISC51) | (1 << ISC50); // rising edge
+}
+
+/******************************************************************************
+ Returns current frequency code.
+******************************************************************************/
+HalSysFreq_t HAL_GetRfFreq(void)
+{
+/* if (INTERNAL_RC == halGetClockSource())
+ {
+ return HAL_FREQ_NOCLK;
+ }
+ else*/
+ {
+ #if (F_CPU == 4000000ul)
+ return HAL_FREQ_4MHZ;
+ #endif
+ #if (F_CPU == 8000000ul)
+ return HAL_FREQ_8MHZ;
+ #endif
+ }
+}
+
+/**************************************************************************//**
+ \brief Wait for when radio will be waked up.
+
+ \param none.
+ \return none.
+******************************************************************************/
+void halWaitRadio(void)
+{
+ uint8_t tempValue = 0;
+
+ do {
+ HAL_SelectRfSpi();
+ HAL_WriteByteRfSpi(RF_REGISTER_READ_ACCESS_MODE | RF_REGISTER_TRX_STATUS_ADDRESS);
+ tempValue = HAL_WriteByteRfSpi(tempValue);
+ HAL_DeselectRfSpi();
+ } while(RF_TRX_OFF_STATE != tempValue);
+}
+
+/**************************************************************************//**
+\brief Checks if amplifier is used.
+
+\return true - is used, \n
+ false - is not used.
+******************************************************************************/
+bool HAL_IsAmplifierUsed(void)
+{
+ #ifdef _HAL_USE_AMPLIFIER_
+ return true;
+ #else
+ return false;
+ #endif
+}
+
+/**************************************************************************//**
+ \brief Enables RX TX indicator for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxTxSwitcher(void)
+{
+ #ifdef _HAL_RF_RX_TX_INDICATOR_
+ phyRxTxSwitcherOn();
+ #endif //_HAL_RF_RX_TX_INDICATOR_
+}
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity option for radio if that is supported.
+******************************************************************************/
+void HAL_InitAntennaDiversity(void)
+{
+ #ifdef _HAL_ANT_DIVERSITY_
+ phyAntennaDiversityInit();
+ #endif //_HAL_ANT_DIVERSITY_
+}
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in RX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableRxAntennaDiversity(void)
+{
+ #ifdef _HAL_ANT_DIVERSITY_
+ phyRxAntennaDiversity();
+ #endif //_HAL_ANT_DIVERSITY_
+}
+
+/**************************************************************************//**
+ \brief Enables Antenna diversity in TX mode for radio if that is supported.
+******************************************************************************/
+void HAL_EnableTxAntennaDiversity(void)
+{
+ #ifdef _HAL_ANT_DIVERSITY_
+ phyTxAntennaDiversity();
+ #endif //_HAL_ANT_DIVERSITY_
+}
+
+//eof halRfCtrl.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfSpi.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfSpi.c
new file mode 100644
index 00000000..6edb9d7a
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halRfSpi.c
@@ -0,0 +1,87 @@
+/**************************************************************************//**
+ \file halrfSpi.c
+
+ \brief SPI interface routines.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 A. Luzhetsky - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+#include <halRfSpi.h>
+#include <halRfPio.h>
+
+/******************************************************************************
+ Performs initialization of SPI interface.
+******************************************************************************/
+void HAL_InitRfSpi(void)
+{
+ GPIO_SPI_CS_set();
+ GPIO_SPI_MISO_make_in();
+ GPIO_SPI_MOSI_make_out();
+ GPIO_SPI_SCK_make_out();
+ GPIO_SPI_CS_make_out();
+ SPCR = ((1 << SPE) | (1 << MSTR)); // SPI enable, master mode.
+ SPSR = (1 << SPI2X); // rate = fosc/2
+}
+
+/******************************************************************************
+ Writes/reads byte to/from SPI. CPU clock critical function (4 MHz only).
+ parameters: value - byte to write.
+ Returns: the byte which was read.
+******************************************************************************/
+uint8_t HAL_WriteByteRfSpi(uint8_t value)
+{
+ uint8_t tmp8;
+
+ SPDR = value; // Write data.
+ asm("nop"); // 1
+ asm("nop"); // 2
+ asm("nop"); // 3
+ asm("nop"); // 4
+ asm("nop"); // 5
+ asm("nop"); // 5
+ asm("nop"); // 7
+ asm("nop"); // 8
+ asm("nop"); // 9
+ asm("nop"); // 10
+ asm("nop"); // 11
+ asm("nop"); // 12
+ asm("nop"); // 13
+ asm("nop"); // 14
+ asm("nop"); // 15
+ asm("nop"); // 16
+ asm("nop"); // 17
+ tmp8 = SPSR;
+ (void)tmp8;
+ return SPDR;
+}
+
+/******************************************************************************
+ Deselects a slave device.
+******************************************************************************/
+void HAL_DeselectRfSpi(void)
+{
+ GPIO_SPI_CS_set();
+}
+
+/******************************************************************************
+ Selects a slave device.
+******************************************************************************/
+void HAL_SelectRfSpi(void)
+{
+ GPIO_SPI_CS_clr();
+}
+
+// eof halrfSpi.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halUid.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halUid.c
new file mode 100644
index 00000000..4f135528
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/zigBit/src/halUid.c
@@ -0,0 +1,72 @@
+/**************************************************************************//**
+ \file uid.c
+
+ \brief Implementation of UID interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 7/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <w1.h>
+#include <uid.h>
+#include <halAtmelUid.h>
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static HalUid_t halUid = {.uid = 0ull};
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Reads uid from ds2411 (meshnetics meshbean) or ATTiny13A (atmel meshbean).
+******************************************************************************/
+void halReadUid(void)
+{
+#ifdef _HAL_TINY_UID_
+ // Atmel Single-Wire Software UART UID
+ halReadAtmelMeshbeanUid(halUid.array);
+#else
+ // 1-Wire UID
+ if (W1_SUCCESS_STATUS == HAL_SearchW1Device(W1_ANY_FAMILY, halUid.array, 1, NULL))
+ {
+ *(halUid.array + 7) = *(halUid.array + 0);
+ halUid.uid >>= 8;
+ }
+#endif
+}
+
+/******************************************************************************
+ UID discovery.
+ Parameters:
+ id - UID buffer pointer.
+ Returns:
+ 0 - if unique ID has been found without error;
+ -1 - if there are some errors during UID discovery.
+******************************************************************************/
+int HAL_ReadUid(uint64_t *id)
+{
+ if (!id)
+ return -1;
+
+ *id = halUid.uid;
+ return 0;
+}
+
+// eof uid.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/adc.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/adc.c
new file mode 100644
index 00000000..03598dcd
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/adc.c
@@ -0,0 +1,133 @@
+/**************************************************************************//**
+ \file adc.c
+
+ \brief Implementation of ADC interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <halAdc.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef enum
+{
+ IDLE, // idle
+ DATA, // performs request
+ BUSY // the module is ready to start conversion
+} AdcStates_t;
+
+typedef struct
+{
+ void (*callback)(void); // address of callback
+} HalAdcControl_t;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+AdcStates_t halAdcState = IDLE; // Monitors current state
+HalAdcControl_t halAdcControl;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Opens the ADC to make the measuring on a ADC channel.
+Parameters:
+ param - pointer to parameter structure
+Returns:
+ -1 - unsupported parameter or ADC is busy.
+ 0 - on success.
+******************************************************************************/
+int HAL_OpenAdc(HAL_AdcParams_t *param)
+{
+ if (IDLE != halAdcState)
+ return -1;
+ if (NULL == param)
+ return -1;
+ if (NULL == param->bufferPointer)
+ return -1;
+ if (param->resolution > RESOLUTION_10_BIT)
+ return -1;
+ /* unsupported voltage reference */
+ if (param->voltageReference & 0x3F)
+ return -1;
+ /* adc speed must be only 9600 or 4800 SPS for 10 bit resolution */
+ if ((RESOLUTION_10_BIT == param->resolution) && (param->sampleRate < ADC_9600SPS))
+ return -1;
+
+ halAdcState = BUSY;
+ halOpenAdc(param);
+ halAdcControl.callback = param->callback;
+ return 0;
+}
+
+/******************************************************************************
+Starts ADC with the parameters that were defined at HAL_OpenAdc.
+Parameters:
+ channel - number of channel
+Returns:
+ -1 - the ADC was not opened, unsupported channel number.
+ 0 - on success.
+******************************************************************************/
+int HAL_ReadAdc(HAL_AdcChannelNumber_t channel)
+{
+ if (BUSY != halAdcState)
+ return -1;
+ if (((channel > HAL_ADC_CHANNEL3) && (channel < HAL_ADC_DIFF_CHANNEL0)) || (channel > HAL_ADC_DIFF_CHANNEL7))
+ return -1;
+
+ halAdcState = DATA;
+ halStartAdc(channel);
+ return 0;
+}
+
+/******************************************************************************
+Closes the ADC.
+Parameters:
+ none.
+Returns:
+ -1 - the module was not opened to be used.
+ 0 - on success.
+******************************************************************************/
+int HAL_CloseAdc(void)
+{
+ if (IDLE == halAdcState)
+ return -1;
+
+ halAdcState = IDLE;
+ halCloseAdc();
+ return 0;
+}
+
+/******************************************************************************
+ ADC interrupt handler.
+******************************************************************************/
+void halSigAdcHandler(void)
+{
+ if (DATA == halAdcState)
+ {
+ halAdcState = BUSY;
+ if (NULL != halAdcControl.callback)
+ halAdcControl.callback();
+ }
+}
+// eof adc.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/appTimer.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/appTimer.c
new file mode 100644
index 00000000..45047822
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/appTimer.c
@@ -0,0 +1,169 @@
+/**************************************************************************//**
+ \file appTimer.c
+
+ \brief Implementation of appTimer.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <appTimer.h>
+#include <halAppClock.h>
+#include <halTaskManager.h>
+#include <atomic.h>
+#include <halDbg.h>
+#include <halDiagnostic.h>
+#if defined(_SYSTEM_TIME_ON_SLEEP_TIMER_)
+ #include <halSleepTimerClock.h>
+#endif
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern uint8_t halSleepTimerOvfw;
+extern uint8_t halAppTimeOvfw;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static HAL_AppTimer_t *halAppTimerHead = NULL; // head of appTimer list
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Interrupt handler of appTimer clock.
+******************************************************************************/
+void halAppTimerHandler(void)
+{
+ uint32_t sysTime;
+
+ // search for expired timers and call their callbacks
+ while ( halAppTimerHead
+ && ((sysTime = halGetTimeOfAppTimer()) - halAppTimerHead->service.sysTimeLabel) >= halAppTimerHead->interval)
+ {
+ HAL_AppTimer_t *p = halAppTimerHead;
+ halRemoveTimer(&halAppTimerHead, NULL, p);
+ if (TIMER_REPEAT_MODE == p->mode)
+ {
+ p->service.sysTimeLabel = sysTime;
+ halAddTimer(&halAppTimerHead, p, sysTime);
+ }
+ p->callback();
+ }
+}
+
+/******************************************************************************
+Check if timer is already started.
+Parameters:
+ appTimer - pointer to HAL_AppTimer_t.
+Returns:
+ true - timer specified already started and presents in the system timers queue
+ false - timer is't started yet
+******************************************************************************/
+static bool isTimerAlreadyStarted(HAL_AppTimer_t *appTimer)
+{
+ bool result = false;
+ Timer_t *p; // p is bottom of list
+ p = halAppTimerHead;
+
+ while (NULL != p)
+ {
+ if (p == appTimer)
+ {
+ result = true;
+ assert(false, APPTIMER_MISTAKE);
+ break;
+ }
+ p = (Timer_t *)p->service.next;
+ }
+ return result;
+}
+
+
+/******************************************************************************
+Starts to count an interval.
+Parameters:
+ appTimer - pointer to HAL_AppTimer_t.
+Returns:
+ -1 - pointer is NULL.
+ 0 - success
+******************************************************************************/
+int HAL_StartAppTimer(HAL_AppTimer_t *appTimer)
+{
+ uint32_t sysTime;
+
+ if (!appTimer)
+ return -1;
+
+ if (true == isTimerAlreadyStarted(appTimer))
+ return 0;
+
+ sysTime = halGetTimeOfAppTimer();
+ appTimer->service.next = NULL;
+ appTimer->service.sysTimeLabel = sysTime;
+ halAddTimer((Timer_t**)(&halAppTimerHead), (Timer_t*)appTimer, sysTime);
+ return 0;
+}
+
+/******************************************************************************
+Stops the timer.
+Parameters:
+ appTimer - pointer to HAL_AppTimer_t.
+Returns:
+ -1 there is not the appTimer.
+ 0 - success
+******************************************************************************/
+int HAL_StopAppTimer(HAL_AppTimer_t *appTimer)
+{
+ Timer_t *prev = 0;
+ Timer_t **t = &appTimer;
+
+ if (!appTimer)
+ return -1;
+ if (halAppTimerHead != *t)
+ {
+ if (!(prev = halFindPrevTimer((Timer_t**)(&halAppTimerHead), appTimer)))
+ return -1; // This timer is not in the list
+ }
+ halRemoveTimer((Timer_t**)(&halAppTimerHead), prev, appTimer);
+ return 0;
+}
+
+/**************************************************************************//**
+\brief Gets system time.
+
+\return
+ time since power up in milliseconds(8 bytes).
+******************************************************************************/
+BcTime_t HAL_GetSystemTime(void)
+{
+ BcTime_t sysTime = 0ull;
+
+ #if defined(_SYSTEM_TIME_ON_SLEEP_TIMER_)
+ sysTime = halGetTimeOfSleepTimer();
+ sysTime |= ((BcTime_t)halSleepTimerOvfw << 32);
+ #else
+ sysTime = halGetTimeOfAppTimer();
+ sysTime |= ((BcTime_t)halAppTimeOvfw << 32);
+ #endif
+
+ return sysTime;
+}
+
+// eof appTimer.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/eeprom.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/eeprom.c
new file mode 100644
index 00000000..8e17055b
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/eeprom.c
@@ -0,0 +1,156 @@
+/**************************************************************************//**
+ \file eeprom.c
+
+ \brief Implementation of the EEPROM interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halEeprom.h>
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef enum
+{
+ EEPROM_IDLE = 0,
+ EEPROM_BUSY
+} EepromState_t;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+EepromState_t halEepromState = EEPROM_IDLE; // state
+HAL_EepromParams_t halEepromParams;
+void (*halEepromDone)();
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Reads some number of bytes defined by HAL_EepromParams_t from the EEPROM.
+Parameters:
+ params - address of HAL_EepromParams_t defined by user.
+ readDone - callback method
+Returns:
+ 0 - success.
+ -1 - the EEPROM has request that was not completed,
+ number of byte to read too much.
+ -2 - eeprom is busy
+******************************************************************************/
+int HAL_ReadEeprom(HAL_EepromParams_t *params, void (*readDone)())
+{
+ uint16_t i;
+
+ if (EEPROM_IDLE != halEepromState)
+ return -2;
+ if (NULL == params)
+ return -1;
+ if ((uint16_t)(params->address + params->length) > EEPROM_DATA_MEMORY_SIZE)
+ return -1;
+
+ halEepromState = EEPROM_BUSY;
+ halEepromDone = readDone;
+ halEepromParams = *params;
+ halWaitEepromReady(); // wait for completion of previous operation
+ for (i = 0; i < halEepromParams.length; i++)
+ {
+ halEepromParams.data[i] = halReadEeprom(halEepromParams.address++);
+ }
+ halEepromParams.length = 0;
+ halPostTask3(HAL_EE_READY);
+
+ return 0;
+}
+
+/******************************************************************************
+Writes number of bytes defined by HAL_EepromParams_t to EEPROM.
+By writeDone parameter user can control if write operation will be asynchronous
+or synchronous.
+Parameters:
+ params - address of HAL_EepromParams_t defined by user.
+ writeDone - address of callback. if writeDone is NULL write operation will be
+ synchronous.
+Returns:
+ 0 - success.
+ -1 - the EEPROM has request that was not completed,
+ number of byte to write too much.
+ -2 - eeprom is busy
+******************************************************************************/
+int HAL_WriteEeprom(HAL_EepromParams_t *params, void (*writeDone)())
+{
+ uint16_t i;
+
+ if (EEPROM_IDLE != halEepromState)
+ return -2;
+ if (NULL == params)
+ return -1;
+ if ((uint16_t)(params->address + params->length) > EEPROM_DATA_MEMORY_SIZE)
+ return -1;
+
+ halEepromState = EEPROM_BUSY;
+ halEepromParams = *params;
+ halEepromDone = writeDone;
+ if (halEepromDone)
+ {// asynchronous operation
+ halEepromWrite(HAL_EEPROM_WRITE_MASK_INT, halEepromParams.address++, *halEepromParams.data++);
+ halEepromParams.length--;
+ return 0;
+ }
+ for (i = 0; i < halEepromParams.length; i++)
+ {
+ halEepromWrite(HAL_EEPROM_WRITE_MASK, halEepromParams.address++, *halEepromParams.data++);
+ }
+ halWaitEepromReady(); // wait for completion of previous write
+ halEepromState = EEPROM_IDLE;
+
+ return 0;
+}
+
+/******************************************************************************
+Checks the eeprom state.
+
+Returns:
+ true - eeprom is busy;
+ false - eeprom is free;
+******************************************************************************/
+bool HAL_IsEepromBusy(void)
+{
+ if (EEPROM_BUSY == halEepromState)
+ return true;
+ else
+ return false;
+}
+
+/******************************************************************************
+Interrupt handler about write completion to EEPROM.
+******************************************************************************/
+void halSigEepromReadyHandler(void)
+{
+ if (!halEepromParams.length)
+ {
+ halEepromState = EEPROM_IDLE;
+ if (NULL != halEepromDone)
+ halEepromDone();
+ return;
+ }
+ halEepromWrite(HAL_EEPROM_WRITE_MASK_INT, halEepromParams.address++, *halEepromParams.data++);
+ halEepromParams.length--;
+}
+//eof eeprom.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/halTaskManager.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/halTaskManager.c
new file mode 100644
index 00000000..2c647084
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/halTaskManager.c
@@ -0,0 +1,305 @@
+/**************************************************************************//**
+ \file halTaskManager.c
+
+ \brief Implemenattion of HAL task manager.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halTaskManager.h>
+#include <atomic.h>
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+void (* extHandler)(void);
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief Interrupt handler of appTimer clock.
+******************************************************************************/
+void halAppTimerHandler(void);
+
+/**************************************************************************//**
+\brief HAL USART task. Exact ection depends on USART internal task.
+******************************************************************************/
+void halSigUsartHandler(void);
+
+/**************************************************************************//**
+\brief twi end of sending handler
+******************************************************************************/
+void halSig2WireSerialHandler(void);
+
+/**************************************************************************//**
+\brief Interrupt handler about write completion to EEPROM.
+******************************************************************************/
+void halSigEepromReadyHandler(void);
+
+/**************************************************************************//**
+\brief Interrupt handler about sleep interval was completed.
+******************************************************************************/
+void halAsyncTimerHandler(void);
+
+/***************************************************************************//**
+\brief Shutdown system. \n
+ NOTES: \n
+ the application should be sure the poweroff will not be \n
+ interrupted after the execution of the sleep().
+*******************************************************************************/
+void halPowerOff(void);
+
+/**************************************************************************//**
+\brief ADC interrupt handler.
+******************************************************************************/
+void halSigAdcHandler(void);
+
+/**************************************************************************//**
+\brief Endpoint interrupt handler.
+******************************************************************************/
+void halEndPointHandler(void);
+
+/**************************************************************************//**
+\brief usb suspend interrupt handler.
+******************************************************************************/
+void halSuspendHandler(void);
+
+/**************************************************************************//**
+\brief usb resume interrupt handler.
+******************************************************************************/
+void halResumeHandler(void);
+
+/**************************************************************************//**
+\brief usb bus reset interrupt handler.
+******************************************************************************/
+void halBusResetHandler(void);
+
+/**************************************************************************//**
+\brief Handler for task manager. It is executed when system has waked up.
+******************************************************************************/
+void halWakeupHandler(void);
+
+/**************************************************************************//**
+\brief Security Module request handler.
+******************************************************************************/
+void halSmRequestHandler(void);
+
+/**************************************************************************//**
+\brief Synchronization system time which based on sleep timer.
+******************************************************************************/
+void halSleepSystemTimeSynchronize(void);
+
+/**************************************************************************//**
+\brief Slave spi reception complete interrupt handler.
+******************************************************************************/
+void halSpiRxByteComplete(void);
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+volatile uint8_t halTaskFlags0 = 0;
+volatile uint8_t halTaskFlags1 = 0;
+volatile uint8_t halTaskFlags2 = 0;
+volatile uint8_t halTaskFlags3 = 0;
+volatile uint8_t halTaskFlags4 = 0;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+HAL task handler.
+******************************************************************************/
+void HAL_TaskHandler()
+{
+ if (0)
+ {
+ }
+#ifdef HAL_USE_TIMER2_COMPA
+ else if (halTaskFlags0 & HAL_ASYNC_TIMER)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags0 &= (~HAL_ASYNC_TIMER);
+ ATOMIC_SECTION_LEAVE
+ halAsyncTimerHandler();
+ }
+#endif // HAL_USE_TIMER2_COMPA
+
+#ifdef HAL_USE_TIMER2_COMPA
+ else if (halTaskFlags0 & HAL_SYNC_SLEEP_TIME)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags0 &= (~HAL_SYNC_SLEEP_TIME);
+ ATOMIC_SECTION_LEAVE
+ halSleepSystemTimeSynchronize();
+ }
+#endif // HAL_USE_TIMER2_COMPA
+
+#ifdef HAL_USE_SPI
+ else if (halTaskFlags2 & HAL_TASK_SPI)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags2 &= (~HAL_TASK_SPI);
+ ATOMIC_SECTION_LEAVE
+ halSpiRxByteComplete();
+ }
+#endif // HAL_USE_SPI
+
+#ifdef HAL_USE_USART
+ else if (halTaskFlags2 & HAL_TASK_USART)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags2 &= (~HAL_TASK_USART);
+ ATOMIC_SECTION_LEAVE
+ halSigUsartHandler();
+ }
+#endif // HAL_USE_USART
+
+#ifdef HAL_USE_ADC
+ else if (halTaskFlags3 & HAL_ADC)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_ADC);
+ ATOMIC_SECTION_LEAVE
+ halSigAdcHandler();
+ }
+#endif // HAL_USE_ADC
+
+#ifdef HAL_USE_EE_READY
+ else if (halTaskFlags3 & HAL_EE_READY)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_EE_READY);
+ ATOMIC_SECTION_LEAVE
+ halSigEepromReadyHandler();
+ }
+#endif // HAL_USE_EE_READY
+
+#ifdef HAL_USE_USB
+ else if (halTaskFlags3 & HAL_USB_ENDPOINTS)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_USB_ENDPOINTS);
+ ATOMIC_SECTION_LEAVE
+ halEndPointHandler();
+ }
+#endif // HAL_USE_USB
+
+#ifdef HAL_USE_USB
+ else if (halTaskFlags3 & HAL_USB_SUSPEND)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_USB_SUSPEND);
+ ATOMIC_SECTION_LEAVE
+ halSuspendHandler();
+ }
+#endif // HAL_USE_USB
+
+#ifdef HAL_USE_USB
+ else if (halTaskFlags3 & HAL_USB_RESUME)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_USB_RESUME);
+ ATOMIC_SECTION_LEAVE
+ halResumeHandler();
+ }
+#endif // HAL_USE_USB
+
+#ifdef _HAL_HW_AES_
+ else if (halTaskFlags3 & HAL_SM_REQ)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_SM_REQ);
+ ATOMIC_SECTION_LEAVE
+ halSmRequestHandler();
+ }
+#endif // _HAL_HW_AES_
+
+#ifdef HAL_USE_USB
+ else if (halTaskFlags3 & HAL_USB_BUS_RESET)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags3 &= (~HAL_USB_BUS_RESET);
+ ATOMIC_SECTION_LEAVE
+ halBusResetHandler();
+ }
+#endif // HAL_USE_USB
+
+#ifdef HAL_USE_WAKEUP
+ else if (halTaskFlags4 & HAL_WAKEUP)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags4 &= (~HAL_WAKEUP);
+ ATOMIC_SECTION_LEAVE
+ halWakeupHandler();
+ }
+#endif
+
+#ifdef HAL_USE_TWI
+ else if (halTaskFlags4 & HAL_TWI)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags4 &= (~HAL_TWI);
+ ATOMIC_SECTION_LEAVE
+ halSig2WireSerialHandler();
+ }
+#endif // HAL_USE_TWI
+
+#ifdef HAL_USE_TIMER4_COMPA
+ else if (halTaskFlags4 & HAL_TIMER4_COMPA)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags4 &= (~HAL_TIMER4_COMPA);
+ ATOMIC_SECTION_LEAVE
+ halAppTimerHandler();
+ }
+#endif // HAL_USE_TIMER4_COMPA
+
+#ifdef HAL_USE_SLEEP
+ else if (halTaskFlags4 & HAL_SLEEP)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags4 &= (~HAL_SLEEP);
+ ATOMIC_SECTION_LEAVE
+ halPowerOff();
+ }
+#endif // HAL_USE_SLEEP
+
+#ifdef HAL_USE_EXT_HANDLER
+ else if (halTaskFlags4 & HAL_EXT_HANDLER)
+ {
+ ATOMIC_SECTION_ENTER
+ halTaskFlags4 &= (~HAL_EXT_HANDLER);
+ ATOMIC_SECTION_LEAVE
+ if (extHandler)
+ extHandler();
+ }
+#endif // HAL_USE_EXT_HANDLER
+
+ if (halTaskFlags0 ||
+ halTaskFlags1 ||
+ halTaskFlags2 ||
+ halTaskFlags3 ||
+ halTaskFlags4)
+ {
+ SYS_PostTask(HAL_TASK_ID);
+ }
+}
+
+// eof halTaskManager.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/i2cPacket.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/i2cPacket.c
new file mode 100644
index 00000000..340981e1
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/i2cPacket.c
@@ -0,0 +1,371 @@
+/**************************************************************************//**
+ \file i2cPacket.c
+
+ \brief Provides the functionality for the writing and the reading \n
+ of packets through the TWI.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <types.h>
+#include <i2cPacket.h>
+#include <i2c.h>
+#include <halTaskManager.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+/* states of the i2c transaction */
+#define I2C_CLOSE 0
+#define I2C_IDLE 1
+#define I2C_WRITE_IADDR_WRITE_DATA 2
+#define I2C_WRITE_IADDR_READ_DATA 3
+#define I2C_WRITE_DATA 4
+#define I2C_READ_DATA 5
+#define I2C_TRANSAC_SUCCESS 6
+#define I2C_TRANSAC_FAIL 7
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef struct
+{
+ volatile uint8_t* data; // bytes to write to the i2c bus
+ volatile uint8_t length; // length in bytes of the request
+ volatile uint8_t index; // current index of read/write byte
+ volatile uint8_t addr; // destination address
+ volatile uint32_t intAddress; // internal address inner i2c device
+ void (*done)(bool result); // callback
+} HalI2cPacketControl_t;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+// current state of the i2c request
+volatile uint8_t halI2cPacketState = I2C_CLOSE;
+HalI2cPacketControl_t halI2cPacketControl;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Resets TWI bus and i2c HAL.
+******************************************************************************/
+void halI2cBusReset(void)
+{
+ halI2cPacketState = I2C_TRANSAC_FAIL;
+ halResetI2c();
+ halPostTask4(HAL_TWI);
+}
+
+/******************************************************************************
+Opens resource.
+Parameters:
+ i2cMode - pointer to clock rate structure.
+Returns:
+ Returns:
+ -1 - resource was opened or pointer is NULL
+ 0 - success.
+******************************************************************************/
+int HAL_OpenI2cPacket(HAL_i2cMode_t *i2cMode)
+{
+ if (NULL == i2cMode)
+ return -1;
+ if (I2C_CLOSE == halI2cPacketState)
+ {
+ halInitI2c(i2cMode);
+ halI2cPacketState = I2C_IDLE;
+ halI2cPacketControl.index = 0;
+ return 0;
+ }
+ return -1;
+}
+
+/******************************************************************************
+Closes resource.
+Returns:
+ -1 - resource was not opened.
+ 0 - success.
+******************************************************************************/
+int HAL_CloseI2cPacket(void)
+{
+ if (I2C_CLOSE != halI2cPacketState)
+ {
+ halI2cPacketControl.done = NULL;
+ halI2cPacketState = I2C_CLOSE;
+ return 0;
+ }
+ return -1;
+}
+
+/******************************************************************************
+Writes the series of bytes out to the TWI bus.
+Parameters:
+ param - pointer to HAL_I2cParams_t structure
+Returns:
+ 0 - the bus is free and the request is accepted.
+ -1 - other case.
+******************************************************************************/
+int HAL_WriteI2cPacket(HAL_I2cParams_t *param)
+{
+ if ((I2C_IDLE == halI2cPacketState) && param)
+ {
+ halI2cPacketControl.addr = param->id;
+ halI2cPacketControl.data = param->data;
+ halI2cPacketControl.index = param->lengthAddr;
+ halI2cPacketControl.length = param->length;
+ halI2cPacketControl.done = param->f;
+ halI2cPacketControl.intAddress = param->internalAddr;
+ }
+ else
+ {
+ return -1;
+ }
+
+ if (HAL_NO_INTERNAL_ADDRESS == halI2cPacketControl.index)
+ halI2cPacketState = I2C_WRITE_DATA;
+ else
+ halI2cPacketState = I2C_WRITE_IADDR_WRITE_DATA;
+ halSendStartI2c();
+ return 0;
+}
+
+/******************************************************************************
+Reads the series of bytes out to the TWI bus.
+Parameters:
+ param - pointer to HAL_I2cParams_t structure
+Returns:
+ 0 - the bus is free and the request is accepted.
+ -1 - other case.
+******************************************************************************/
+int HAL_ReadI2cPacket(HAL_I2cParams_t *param)
+{
+ if ((I2C_IDLE == halI2cPacketState) && param)
+ {
+ halI2cPacketControl.addr = param->id;
+ halI2cPacketControl.index = param->lengthAddr;
+ halI2cPacketControl.length = param->length;
+ halI2cPacketControl.data = param->data;
+ halI2cPacketControl.done = param->f;
+ halI2cPacketControl.intAddress = param->internalAddr;
+ }
+ else
+ {
+ return -1;
+ }
+
+ if (HAL_NO_INTERNAL_ADDRESS == halI2cPacketControl.index)
+ halI2cPacketState = I2C_READ_DATA;
+ else
+ halI2cPacketState = I2C_WRITE_IADDR_READ_DATA;
+ halSendStartI2c();
+ return 0;
+}
+
+/******************************************************************************
+Notification about the start condition was sent.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void halSendStartDoneI2c(void)
+{
+ if ((I2C_WRITE_IADDR_WRITE_DATA == halI2cPacketState) ||
+ (I2C_WRITE_IADDR_READ_DATA == halI2cPacketState) ||
+ (I2C_WRITE_DATA == halI2cPacketState))
+ {
+ halWriteI2c(((halI2cPacketControl.addr << 1) + 0));
+ }
+ else if (I2C_READ_DATA == halI2cPacketState)
+ {
+ halWriteI2c(((halI2cPacketControl.addr << 1) + 1));
+ }
+ else
+ { // abnormal
+ halI2cBusReset();
+ }
+}
+
+/******************************************************************************
+Sending data to i2c bus. If last byte then send stop condition and post task.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void halWriteData(void)
+{
+ if (halI2cPacketControl.index < halI2cPacketControl.length)
+ {
+ halWriteI2c(halI2cPacketControl.data[halI2cPacketControl.index++]);
+ }
+ else
+ {
+ halI2cPacketState = I2C_TRANSAC_SUCCESS;
+ halSendStopI2c();
+ halPostTask4(HAL_TWI);
+ }
+}
+
+/******************************************************************************
+Sending internal device address to i2c bus. If address is sent then switch i2c
+hal state.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void halWriteInternalAddress(void)
+{
+ uint8_t data;
+
+ data = (uint8_t)(halI2cPacketControl.intAddress >> --halI2cPacketControl.index * 8);
+ halWriteI2c(data);
+
+ if (0 == halI2cPacketControl.index)
+ {
+ if (I2C_WRITE_IADDR_WRITE_DATA == halI2cPacketState)
+ halI2cPacketState = I2C_WRITE_DATA;
+ else
+ halI2cPacketState = I2C_READ_DATA;
+ }
+}
+
+/******************************************************************************
+Notification that byte was written to the TWI.
+Parameters:
+ result - contains result of previous operation.
+Returns:
+ none.
+******************************************************************************/
+void halWriteDoneI2c(void)
+{
+ if (I2C_WRITE_DATA == halI2cPacketState)
+ {
+ halWriteData();
+ }
+ else if ((I2C_WRITE_IADDR_WRITE_DATA == halI2cPacketState) || (I2C_WRITE_IADDR_READ_DATA == halI2cPacketState))
+ {
+ halWriteInternalAddress();
+ }
+ else if (I2C_READ_DATA == halI2cPacketState)
+ {
+ halSendStartI2c();
+ }
+ else
+ { // abnormal
+ halI2cBusReset();
+ }
+}
+
+/******************************************************************************
+Notification that address byte was written to the TWI and was read ACK.
+Starts reading data.
+Parameters:
+ none.
+Returns:
+ none.
+******************************************************************************/
+void halMasterReadWriteAddressAckI2c(void)
+{
+ if (I2C_READ_DATA == halI2cPacketState)
+ {
+ if (1 == halI2cPacketControl.length)
+ halReadI2c(false); // send nack
+ else
+ halReadI2c(true); // send ack
+ }
+ else
+ { // abnormal
+ halI2cBusReset();
+ }
+}
+
+/******************************************************************************
+Notification that byte was read from the TWI.
+Parameters:
+ data - contains byte that was read.
+Returns:
+ none.
+******************************************************************************/
+void halReadDoneI2c(uint8_t data)
+{
+ if (I2C_READ_DATA == halI2cPacketState)
+ {
+ halI2cPacketControl.data[halI2cPacketControl.index++] = data;
+ if (halI2cPacketControl.index < (halI2cPacketControl.length - 1))
+ halReadI2c(true); // send ACK
+ else
+ halReadI2c(false); // send NACK
+ }
+ else
+ { // abnormal
+ halI2cBusReset();
+ }
+}
+
+/******************************************************************************
+Notification that last byte was read from the TWI. Needs send STOP condition
+on bus.
+Parameters:
+ data - contains byte that was read.
+Returns:
+ none.
+******************************************************************************/
+void halReadLastByteDoneI2c(uint8_t data)
+{
+ if (I2C_READ_DATA == halI2cPacketState)
+ {
+ halI2cPacketControl.data[halI2cPacketControl.index++] = data;
+ halI2cPacketState = I2C_TRANSAC_SUCCESS;
+ halSendStopI2c();
+ halPostTask4(HAL_TWI);
+ }
+ else
+ { // abnormal
+ halI2cBusReset();
+ }
+}
+
+/******************************************************************************
+Waits for end of sending and calls user's callback
+******************************************************************************/
+void halSig2WireSerialHandler(void)
+{
+ if (halI2cPacketControl.done)
+ {
+ if (I2C_TRANSAC_SUCCESS == halI2cPacketState)
+ {
+ halWaitEndOfStopStation();
+ halI2cPacketState = I2C_IDLE;
+ halI2cPacketControl.done(true);
+ }
+ else
+ {
+ halI2cPacketState = I2C_IDLE;
+ halI2cPacketControl.done(false);
+ }
+ }
+ else
+ {
+ halI2cPacketState = I2C_IDLE;
+ }
+}
+// eof i2cPacket.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/irq.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/irq.c
new file mode 100644
index 00000000..c681ba74
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/irq.c
@@ -0,0 +1,143 @@
+/**************************************************************************//**
+ \file irq.c
+
+ \brief Implementation of IRQ interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halIrq.h>
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern IrqCallback_t IrqCallbackList[HAL_NUM_IRQ_LINES];
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Registers user's irqNumber interrupt
+ Parameters:
+ irqNumber - IRQ number
+ irqMode - Interrupt sence control
+ f - user's interrupt handler. Handler must be executed less than 100 us.
+ Returns:
+ -1 - if irqNumber is out of range, not valid irq mode, invalid interrupt handler
+ or such interrupt has been already registered.
+ 0 - otherwise.
+******************************************************************************/
+int HAL_RegisterIrq(HAL_IrqNumber_t irqNumber, HAL_IrqMode_t irqMode, void (*f)(void))
+{
+ uint8_t irqOffsetNumber = irqNumber - HAL_FIRST_VALID_IRQ;
+
+ // irqNumber is out of range
+ if (irqOffsetNumber >= HAL_NUM_IRQ_LINES)
+ return -1;
+ // Such interrupt has been already register
+ if (NULL != IrqCallbackList[irqOffsetNumber])
+ return -1;
+ // not valid irq mode
+ if (IRQ_HIGH_LEVEL == irqMode)
+ return -1;
+ // invalid interrupt handler
+ if (NULL == f)
+ return -1;
+
+ /* type HAL_IrqMode_t has not valid IRQ_HIGH_LEVEL state,
+ * because for saving to register all states need decrement after IRQ_LOW_LEVEL state. */
+ if (IRQ_LOW_LEVEL == irqMode)
+ halSetIrqConfig(irqNumber, irqMode);
+ else
+ halSetIrqConfig(irqNumber, irqMode - 1);
+
+ IrqCallbackList[irqOffsetNumber] = f;
+ return 0;
+}
+
+/******************************************************************************
+ Enables irqNumber interrupt
+ Parameters:
+ irqNumber - IRQ number
+ Returns:
+ -1 - if irqNumber is out of range or has not been
+ registered yet.
+ 0 - otherwise.
+******************************************************************************/
+int HAL_EnableIrq(HAL_IrqNumber_t irqNumber)
+{
+ uint8_t irqOffsetNumber = irqNumber - HAL_FIRST_VALID_IRQ;
+ // irqNumber is out of range
+ if (irqOffsetNumber >= HAL_NUM_IRQ_LINES)
+ return -1;
+ // Interrupt has not been opened yet
+ if (NULL == IrqCallbackList[irqOffsetNumber])
+ return -1;
+ halEnableIrqInterrupt(irqNumber);
+ return 0;
+}
+
+/******************************************************************************
+ Disables irqNumber interrupt
+ Parameters:
+ irqNumber - IRQ number
+ Returns:
+ -1 - if irqNumber is out of range or has not been
+ registered yet.
+ 0 - otherwise.
+******************************************************************************/
+int HAL_DisableIrq(HAL_IrqNumber_t irqNumber)
+{
+ uint8_t irqOffsetNumber = irqNumber - HAL_FIRST_VALID_IRQ;
+ // irqNumber is out of range
+ if (irqOffsetNumber >= HAL_NUM_IRQ_LINES)
+ return -1;
+ // Interrupt has not been opened yet
+ if (NULL == IrqCallbackList[irqOffsetNumber])
+ return -1;
+ halDisableIrqInterrupt(irqNumber);
+ return 0;
+}
+
+/******************************************************************************
+ Unregisters user's irqNumber interrupt
+ Parameters:
+ irqNumber - IRQ number
+ Returns:
+ -1 - if irqNumber is out of range or has not been
+ registered yet.
+ 0 - otherwise.
+******************************************************************************/
+int HAL_UnregisterIrq(HAL_IrqNumber_t irqNumber)
+{
+ uint8_t irqOffsetNumber = irqNumber - HAL_FIRST_VALID_IRQ;
+ // irqNumber is out of range
+ if (irqOffsetNumber >= HAL_NUM_IRQ_LINES)
+ return -1;
+ // Interrupt has not been opened yet
+ if (NULL == IrqCallbackList[irqOffsetNumber])
+ return -1;
+ // Disable external interrupt request
+ halDisableIrqInterrupt(irqNumber);
+ halClrIrqConfig(irqNumber);
+ IrqCallbackList[irqOffsetNumber] = NULL;
+ return 0;
+}
+
+// eof irq.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/pwm.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/pwm.c
new file mode 100644
index 00000000..ad7b18a2
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/pwm.c
@@ -0,0 +1,165 @@
+/**************************************************************************//**
+ \file pwm.c
+
+ \brief Implementation of PWM interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/11/08 A. Taradov - Created
+ 5/04/11 A.Razinkov - Refactored
+*******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halPwm.h>
+#include <pwm.h>
+
+/******************************************************************************
+ Defines section
+******************************************************************************/
+
+/******************************************************************************
+ Constants section
+******************************************************************************/
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+
+/**************************************************************************//**
+\brief Initializes the PWM.
+
+\param [in] pwmUnit - PWM unit number.
+ Equal to ID of Timer/Counter witch serves PWM module.
+
+\return operation status
+******************************************************************************/
+int HAL_OpenPwm(HAL_PwmUnit_t pwmUnit)
+{
+ /* Check PWM unit */
+ if ((PWM_UNIT_1 == pwmUnit) || (PWM_UNIT_3 == pwmUnit))
+ halOpenPwm(pwmUnit);
+ else
+ return PWM_INVALID_UNIT_STATUS;
+
+ return PWM_SUCCESS_STATUS;
+}
+
+/**************************************************************************//**
+\brief Starts PWM on specified channel.
+
+\param [in] descriptor - PWM channel descriptor.
+
+\return operation status
+******************************************************************************/
+int HAL_StartPwm(HAL_PwmDescriptor_t *descriptor)
+{
+ /* Invalid PWM channel specified */
+ if (PWM_INVALID_CHANNEL <= descriptor->channel)
+ return PWM_INVALID_CHANNEL_STATUS;
+ /* Check PWM unit */
+ if ((PWM_UNIT_1 == descriptor->unit) || (PWM_UNIT_3 == descriptor->unit))
+ {
+ halPreparePwmChannelAccess(descriptor);
+ halStartPwm(descriptor);
+ }
+ else
+ return PWM_INVALID_UNIT_STATUS;
+
+ return PWM_SUCCESS_STATUS;
+}
+
+/**************************************************************************//**
+\brief Stops PWM on specified channel.
+
+\param [in] descriptor - PWM channel descriptor.
+
+\return operation status
+******************************************************************************/
+int HAL_StopPwm(HAL_PwmDescriptor_t *descriptor)
+{
+ /* Invalid PWM channel specified */
+ if (PWM_INVALID_CHANNEL <= descriptor->channel)
+ return PWM_INVALID_CHANNEL_STATUS;
+ else
+ halStopPwm(descriptor);
+
+ return PWM_SUCCESS_STATUS;
+}
+
+/**************************************************************************//**
+\brief Sets base frequency of module. Common for all module channels.
+
+\param [in] pwmUnit - PWM unit number. Equal to corresponding Timer/Counter ID.
+\param [in] top - value for the TOP register.
+\param [in] prescaler - clock prescaler.
+
+\return operation status
+******************************************************************************/
+int HAL_SetPwmFrequency(HAL_PwmUnit_t pwmUnit, uint16_t top, HAL_PwmPrescaler_t prescaler)
+{
+ /* Check prescaler value */
+ if (PWM_PRESCALER_INVALID <= prescaler)
+ return PWM_INVALID_PRESCALER_STATUS;
+ else halSetPwmFrequency(pwmUnit, top, prescaler);
+
+ return PWM_SUCCESS_STATUS;
+}
+
+/**************************************************************************//**
+\brief Sets compare value for the PWM channel.
+
+\param [in] descriptor - PWM channel descriptor.
+
+\return operation status
+******************************************************************************/
+int HAL_SetPwmCompareValue(HAL_PwmDescriptor_t *descriptor, uint16_t cmpValue)
+{
+ /* Invalid PWM channel specified */
+ if (PWM_INVALID_CHANNEL <= descriptor->channel)
+ return PWM_INVALID_CHANNEL_STATUS;
+ /* Check PWM unit */
+ if ((PWM_UNIT_1 == descriptor->unit) || (PWM_UNIT_3 == descriptor->unit))
+ halSetPwmCompareValue(descriptor, cmpValue);
+ else
+ return PWM_INVALID_UNIT_STATUS;
+
+ return PWM_SUCCESS_STATUS;
+}
+
+/**************************************************************************//**
+\brief Closes the PWM.
+
+\param [in] pwmUnit - PWM unit number.
+ Equal to ID of Timer/Counter witch serves PWM module.
+
+\return operation status
+******************************************************************************/
+int HAL_ClosePwm(HAL_PwmUnit_t pwmUnit)
+{
+ /* Check PWM unit */
+ if ((PWM_UNIT_1 == pwmUnit) || (PWM_UNIT_3 == pwmUnit))
+ halClosePwm(pwmUnit);
+ else
+ return PWM_INVALID_UNIT_STATUS;
+
+ return PWM_SUCCESS_STATUS;
+}
+
+// eof pwm.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/resetReason.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/resetReason.c
new file mode 100644
index 00000000..0fecaa11
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/resetReason.c
@@ -0,0 +1,59 @@
+/**************************************************************************//**
+ \file halWdtInit.c
+
+ \brief Implementation of the reset reason interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 5/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <resetReason.h>
+#include <types.h>
+#include <wdtCtrl.h>
+
+/******************************************************************************
+ External variables section
+******************************************************************************/
+extern uint8_t halResetReason; // contains the reset reason
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Returns the reset reason.
+Parameters:
+ none.
+Returns:
+ The reason of reset.
+******************************************************************************/
+HAL_ResetReason_t HAL_ReadResetReason(void)
+{
+ return (HAL_ResetReason_t)halResetReason;
+}
+
+/******************************************************************************
+Software reset.
+******************************************************************************/
+void HAL_WarmReset(void)
+{
+
+ halResetReason = TEMP_WARM_RESET;
+ wdt_enable(0);
+ while(1);
+}
+//eof resetReason.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleep.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleep.c
new file mode 100644
index 00000000..1ad72df2
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleep.c
@@ -0,0 +1,79 @@
+/**************************************************************************//**
+ \file sleep.c
+
+ \brief The implementation of common sleep and wake up.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 1/12/09 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halSleep.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define HAL_NULL_POINTER -1
+#define HAL_SLEEP_TIMER_HAS_ALREADY_STARTED -3
+#define HAL_SLEEP_TIMER_IS_BUSY -2
+#define HAL_SLEEP_SYSTEM_HAS_ALREADY_STARTED -3
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+HalSleepControl_t halSleepControl =
+{
+ .wakeupStation = HAL_ACTIVE_MODE,
+ .sleepTimerState = HAL_SLEEP_TIMER_IS_STOPPED
+};
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief Starts sleep timer and HAL sleep. When system is wake up send callback
+\param[in]
+ sleepParam - pointer to sleep structure.
+\return
+ -1 - bad pointer, \n
+ -2 - sleep timer is busy, \n
+ -3 - sleep system has been started.
+ 0 - success.
+******************************************************************************/
+int HAL_StartSystemSleep(HAL_Sleep_t *sleepParam)
+{
+ HAL_SleepTimer_t sleepTimer;
+ int sleepTimerStatus;
+
+ if (!sleepParam)
+ return HAL_NULL_POINTER;
+
+ halSleepControl.callback = sleepParam->callback;
+ sleepTimer.interval = sleepParam->sleepTime;
+ sleepTimer.mode = TIMER_ONE_SHOT_MODE;
+ sleepTimer.callback = NULL;
+
+ sleepTimerStatus = HAL_StartSleepTimer(&sleepTimer);
+ if ((HAL_NULL_POINTER == sleepTimerStatus) || (HAL_SLEEP_TIMER_HAS_ALREADY_STARTED == sleepTimerStatus))
+ return HAL_SLEEP_TIMER_IS_BUSY;
+
+ if (-1 == HAL_Sleep())
+ return HAL_SLEEP_SYSTEM_HAS_ALREADY_STARTED;
+
+ return 0;
+}
+
+//eof sleep.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleepTimer.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleepTimer.c
new file mode 100644
index 00000000..f83d2446
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleepTimer.c
@@ -0,0 +1,126 @@
+/**************************************************************************//**
+ \file sleepTimer.c
+
+ \brief The implementation of the sleep timer.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <sleepTimer.h>
+#include <halSleepTimerClock.h>
+#include <halSleep.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define HAL_NULL_POINTER -1
+#define HAL_TIME_CAN_NOT_BE_COUNTED -2
+#define HAL_SLEEP_TIMER_HAS_ALREADY_STARTED -3
+#define HAL_SLEEP_TIMER_HAS_ALREADY_STOPPED -1
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern HalSleepControl_t halSleepControl;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Starts sleep timer. Interval must be greater one tick time.
+ Parameters:
+ sleepTimer - address of the HAL_SleepTimer_t.
+ Returns:
+ -1 - NULL pointer, \n
+ -2 - interval can not be counted out, \n
+ -3 - sleep timer has already started. \n
+ 0 - otherwise.
+******************************************************************************/
+int HAL_StartSleepTimer(HAL_SleepTimer_t *sleepTimer)
+{
+ uint32_t tempValue;
+
+ if (!sleepTimer)
+ return HAL_NULL_POINTER;
+
+ // Convert millisecond interval to the sleep timer ticks.
+ tempValue = (halSleepTimerFrequency() * sleepTimer->interval) / 1000ul;
+ if (!tempValue)
+ return HAL_TIME_CAN_NOT_BE_COUNTED;// Can't count out interval
+
+ if (HAL_SLEEP_TIMER_IS_STARTED == halSleepControl.sleepTimerState) // there is active timer
+ return HAL_SLEEP_TIMER_HAS_ALREADY_STARTED;
+
+ halSleepControl.sleepTimerState = HAL_SLEEP_TIMER_IS_STARTED;
+ halSleepControl.sleepTimer = *sleepTimer;
+ // Start asynchronous timer2.
+ halSetSleepTimerInterval(tempValue);
+ return 0;
+}// end sleepTimer_start
+
+/******************************************************************************
+Removes timer.
+Parameters:
+ sleepTimer - is not used now. For capabilities for old version.
+Returns:
+ -1 - there is no active sleep timer.
+ 0 - otherwise.
+******************************************************************************/
+int HAL_StopSleepTimer(HAL_SleepTimer_t *sleepTimer)
+{
+ (void)sleepTimer;
+
+ // there is no active timer
+ if (HAL_SLEEP_TIMER_IS_STOPPED == halSleepControl.sleepTimerState)
+ return HAL_SLEEP_TIMER_HAS_ALREADY_STOPPED;
+ halClearTimeControl();
+ halSleepControl.sleepTimerState = HAL_SLEEP_TIMER_IS_STOPPED;
+
+ return 0;
+}
+
+/******************************************************************************
+Interrupt handler about sleep interval was completed.
+******************************************************************************/
+void halAsyncTimerHandler(void)
+{
+ // there isn't work timer
+ if (HAL_SLEEP_TIMER_IS_STOPPED == halSleepControl.sleepTimerState)
+ return;
+
+ if (TIMER_REPEAT_MODE == halSleepControl.sleepTimer.mode)
+ {
+ if (halSleepControl.sleepTimer.callback)
+ halSleepControl.sleepTimer.callback();
+
+ // user can stop timer in callback
+ if (HAL_SLEEP_TIMER_IS_STOPPED == halSleepControl.sleepTimerState)
+ return;
+
+ halSetSleepTimerInterval(halSleepControl.sleepTimer.interval);
+ }
+ else
+ {
+ halSleepControl.sleepTimerState = HAL_SLEEP_TIMER_IS_STOPPED;
+ if (halSleepControl.sleepTimer.callback)
+ halSleepControl.sleepTimer.callback();
+ }
+}
+
+//eof sleepTimer.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/spi.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/spi.c
new file mode 100644
index 00000000..cd4c55b0
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/spi.c
@@ -0,0 +1,602 @@
+/**************************************************************************//**
+ \file spi.c
+
+ \brief Implementation of USART SPI mode, hardware-independent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 29/05/08 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <spi.h>
+
+/******************************************************************************
+ Global functions prototypes section
+******************************************************************************/
+void halSetUsartSpiConfig(HAL_SpiDescriptor_t *descriptor);
+void halSetSlaveSpiConfig(HAL_SpiDescriptor_t *descriptor);
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern HAL_UsartDescriptor_t *halPointDescrip[NUM_USART_CHANNELS];
+#if defined(ATMEGA128RFA1)
+ extern HAL_SpiDescriptor_t *halRealSpiDescripPointer;
+ extern volatile uint8_t rxSlaveBuffer[HAL_SPI_RX_BUFFER_LENGTH];
+#endif
+
+/******************************************************************************
+ Static functions prototypes section
+******************************************************************************/
+static bool isClosedPd(void *pointer);
+static bool isOpenedPd(void *pointer);
+static int halCheckUsartDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *));
+static int halOpenUsartSpi(HAL_SpiDescriptor_t *descriptor);
+static int halCloseUsartSpi(HAL_SpiDescriptor_t *descriptor);
+static int halFillServiceInfo(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length, uint8_t transac);
+static int halWriteUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
+static int halReadUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
+#if defined(ATMEGA128RFA1)
+static int halCheckSpiDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *));
+static int halOpenRealSpi(HAL_SpiDescriptor_t *descriptor);
+static int halCloseRealSpi(HAL_SpiDescriptor_t *descriptor);
+static int halWriteRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
+static int halReadRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
+#endif
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief Predicate for check of closed station.
+\param[in]
+ pointer - pointer to some descriptor.
+\return
+ true - interface is closed;
+ false - interface is not closed.
+******************************************************************************/
+static bool isClosedPd(void *pointer)
+{
+ return pointer ? false : true;
+}
+
+/**************************************************************************//**
+\brief Predicate for check of opened station.
+\param[in]
+ pointer - pointer to some descriptor.
+\return
+ true - interface is opened;
+ false - interface is not opened.
+******************************************************************************/
+static bool isOpenedPd(void *pointer)
+{
+ return pointer ? true : false;
+}
+
+/**************************************************************************//**
+\brief Check correctenss of the usart spi descriptor.
+\param[in]
+ descriptor - pointer to the usart spi descriptor.
+\param[in]
+ predicate - check station predicate.
+\return
+ interface index - interface is opened;
+ -1 - interface is not opened.
+******************************************************************************/
+static int halCheckUsartDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *))
+{
+ int i;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (false == predicate((void *)halPointDescrip[i]))
+ return -1;
+
+ return i;
+}
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Check correctenss of the real spi descriptor.
+\param[in]
+ descriptor - pointer to the real spi descriptor.
+\param[in]
+ predicate - check station predicate.
+\return
+ 0 - interface is opened;
+ -1 - interface is not opened.
+******************************************************************************/
+static int halCheckSpiDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *))
+{
+ if (NULL == descriptor)
+ return -1;
+
+ if (SPI_CHANNEL_2 != descriptor->tty)
+ return -1;
+
+ if (false == predicate((void *)halRealSpiDescripPointer))
+ return -1;
+
+ return 0;
+}
+#endif
+
+/**************************************************************************//**
+\brief Configure usart in mspi mode.
+\param[in]
+ descriptor - pointer to the usart spi descriptor.
+\return
+ -1 - there not are free resources.
+ 0 - SPI channel is ready.
+******************************************************************************/
+static int halOpenUsartSpi(HAL_SpiDescriptor_t *descriptor)
+{
+ int i; // Descriptor index
+
+ i = halCheckUsartDescriptor(descriptor, isClosedPd);
+ if (-1 == i)
+ return -1;
+
+ if (NULL != descriptor->callback)
+ {
+ descriptor->spiDescriptor.txCallback = descriptor->callback;
+ descriptor->spiDescriptor.tty = descriptor->tty;
+ }
+
+ halPointDescrip[i] = &descriptor->spiDescriptor;
+ halSetUsartSpiConfig(descriptor);
+ return 0;
+}
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Configure spi.
+\param[in]
+ descriptor - pointer to the spi descriptor.
+\return
+ -1 - there are no free resources.
+ 0 - SPI channel is ready.
+******************************************************************************/
+static int halOpenRealSpi(HAL_SpiDescriptor_t *descriptor)
+{
+ if (-1 == halCheckSpiDescriptor(descriptor, isClosedPd))
+ return -1;
+
+ halRealSpiDescripPointer = descriptor;
+ halRealSpiDescripPointer->spiDescriptor.service.txPointOfRead = 0;
+ halRealSpiDescripPointer->spiDescriptor.service.txPointOfWrite = 0;
+ halRealSpiDescripPointer->spiDescriptor.service.rxPointOfRead = 0;
+ halRealSpiDescripPointer->spiDescriptor.service.rxPointOfWrite = 0;
+ halSetSlaveSpiConfig(descriptor);
+ return 0;
+}
+#endif
+
+/**************************************************************************//**
+\brief Open the SPI interface.
+\param[in]
+ descriptor - pointer to the spi descriptor.
+\return
+ -1 - there not are free resources.
+ 0 - SPI channel is ready.
+******************************************************************************/
+int HAL_OpenSpi(HAL_SpiDescriptor_t *descriptor)
+{
+#if defined(ATMEGA128RFA1)
+ if (SPI_CHANNEL_2 == descriptor->tty)
+ return halOpenRealSpi(descriptor);
+ else
+#endif
+ return halOpenUsartSpi(descriptor);
+}
+
+/**************************************************************************//**
+\brief Clear the usart channel and pins.
+\param[in]
+ descriptor - pointer to the spi descriptor.
+\return
+ 0 - success
+ -1 - channel was not opened.
+******************************************************************************/
+static int halCloseUsartSpi(HAL_SpiDescriptor_t *descriptor)
+{
+ int i;
+
+ i = halCheckUsartDescriptor(descriptor, isOpenedPd);
+ if (-1 == i)
+ return -1;
+
+ halPointDescrip[i] = NULL;
+ halClearUsartSpi(descriptor->tty);
+ return 0;
+}
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Clear the spi channel and pins.
+\param[in]
+ descriptor - pointer to the spi descriptor.
+\return
+ Returns 0 on success or -1 if channel was not opened.
+******************************************************************************/
+static int halCloseRealSpi(HAL_SpiDescriptor_t *descriptor)
+{
+ if (-1 == halCheckSpiDescriptor(descriptor, isClosedPd))
+ return -1;
+
+ halRealSpiDescripPointer = NULL;
+ halClearRealSpi();
+ return 0;
+}
+#endif
+
+/**************************************************************************//**
+\brief Close the SPI channel and pins.
+\param[in]
+ descriptor - pointer to the spi descriptor.
+\return
+ Returns 0 on success or -1 if channel was not opened.
+******************************************************************************/
+int HAL_CloseSpi(HAL_SpiDescriptor_t *descriptor)
+{
+#if defined(ATMEGA128RFA1)
+ if (SPI_CHANNEL_2 == descriptor->tty)
+ return halCloseRealSpi(descriptor);
+ else
+#endif
+ return halCloseUsartSpi(descriptor);
+}
+
+/**************************************************************************//**
+\brief Fill service structure for bus transaction.
+\param[in]
+ descriptor - pointer to the spi descriptor.
+\param[in]
+ buffer -pointer to data buffer.
+\param[in]
+ length - length of the data buffer.
+\param[in]
+ transac - bus transaction type.
+\return
+ -1 - interface is busy;
+ 0 - success.
+******************************************************************************/
+static int halFillServiceInfo(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length, uint8_t transac)
+{
+ HalUsartService_t *halBufferControl;
+
+ halBufferControl = &descriptor->spiDescriptor.service;
+ if (halBufferControl->txPointOfWrite != halBufferControl->txPointOfRead)
+ return -1; // there is unsent data
+
+ descriptor->spiDescriptor.txBuffer = buffer;
+ descriptor->spiDescriptor.txBufferLength = 0;
+ halBufferControl->txPointOfWrite = length;
+ halBufferControl->txPointOfRead = 0;
+ descriptor->spiDescriptor.rxBuffer = buffer;
+ descriptor->spiDescriptor.flowControl = transac;
+ return 0;
+}
+
+/**************************************************************************//**
+\brief Writes a length bytes to the usart. \n
+ Callback function will be used to notify about the finishing transmitting.
+\param[in]
+ descriptor - pointer to spi descriptor
+\param[in]
+ buffer - pointer to application data buffer;
+\param[in]
+ length - number bytes for transfer;
+\return
+ -1 - spi module was not opened, there is unsent data, pointer to the data or
+ the length are zero; \n
+ 0 - on success or a number; \n
+ Number of written bytes if the synchronous method is used(callback is NULL).
+******************************************************************************/
+static int halWriteUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ int i;
+
+ if (!buffer || !length)
+ return -1;
+
+ i = halCheckUsartDescriptor(descriptor, isOpenedPd);
+ if (-1 == i)
+ return -1;
+
+ if (&descriptor->spiDescriptor != halPointDescrip[i])
+ return -1; // incorrect descriptor
+
+ if (NULL != descriptor->callback)
+ {
+ if (-1 == halFillServiceInfo(descriptor, buffer, length, USART_SPI_WRITE_MODE))
+ return -1;
+
+ halEnableUsartSpiRxcInterrupt(descriptor->tty);
+ halEnableUsartSpiDremInterrupt(descriptor->tty);
+ return 0;
+ }
+ else
+ {
+ return halSyncUsartSpiWriteData(descriptor->tty, buffer, length);
+ }
+}
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Writes a length bytes to the spi. \n
+\param[in]
+ descriptor - pointer to spi descriptor
+\param[in]
+ buffer - pointer to application data buffer;
+\param[in]
+ length - number bytes for transfer;
+\return
+ -1 - spi module was not opened, there is unsent data, pointer to the data or
+ the length are zero; \n
+ 0 - on success or a number; \n
+******************************************************************************/
+static int halWriteRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ if (!buffer || !length)
+ return -1;
+
+ if (-1 == halCheckSpiDescriptor(descriptor, isOpenedPd))
+ return -1;
+
+ if (descriptor != halRealSpiDescripPointer)
+ return -1; // incorrect descriptor
+
+ if (-1 == halFillServiceInfo(descriptor, buffer, length, USART_SPI_WRITE_MODE))
+ return -1;
+
+ halSendSpiByte(*buffer);
+ return 0;
+}
+#endif
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Put next byte to the spi.
+******************************************************************************/
+void halSpiTxByteComplete(void)
+{
+ HalUsartService_t *halBufferControl;
+
+ halBufferControl = &halRealSpiDescripPointer->spiDescriptor.service;
+
+ if (halBufferControl->txPointOfWrite != halBufferControl->txPointOfRead)
+ halSendSpiByte(halRealSpiDescripPointer->spiDescriptor.txBuffer[++halBufferControl->txPointOfRead]);
+}
+#endif
+
+/**************************************************************************//**
+\brief Writes a length bytes to the SPI. \n
+ Callback function will be used to notify about the finishing transmitting.
+ (only for master spi)
+\param[in]
+ descriptor - pointer to spi descriptor
+\param[in]
+ buffer - pointer to application data buffer;
+\param[in]
+ length - number bytes for transfer;
+\return
+ -1 - spi module was not opened, there is unsent data, pointer to the data or
+ the length are zero; \n
+ 0 - on success or a number; \n
+ Number of written bytes if the synchronous method is used(callback is NULL), \n
+ only for master spi.
+******************************************************************************/
+int HAL_WriteSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+#if defined(ATMEGA128RFA1)
+ if (SPI_CHANNEL_2 == descriptor->tty)
+ return halWriteRealSpi(descriptor, buffer, length);
+ else
+#endif
+ return halWriteUsartSpi(descriptor, buffer, length);
+}
+
+/**************************************************************************//**
+\brief Reads a number of bytes from the usart.\n
+ Callback function will be used to notify when the activity is finished.\n
+ The read data is placed to the buffer.
+\param[in]
+ descriptor - pointer to HAL_SpiDescriptor_t structure
+\param[in]
+ buffer - pointer to the application data buffer
+\param[in]
+ length - number of bytes to transfer
+\return
+ -1 - spi module was not opened, or there is unsent data, or the pointer to
+ data or the length are NULL; \n
+ 0 - success; \n
+ Number of written bytes if the synchronous method is used(callback is NULL).
+******************************************************************************/
+static int halReadUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ HAL_UsartDescriptor_t *spiDescrip;
+ int i;
+
+ if (!buffer || !length)
+ return -1;
+
+ i = halCheckUsartDescriptor(descriptor, isOpenedPd);
+ if (-1 == i)
+ return -1;
+
+ spiDescrip = &descriptor->spiDescriptor;
+ if (spiDescrip != halPointDescrip[i])
+ return -1; // incorrect descriptor
+
+ if (NULL != descriptor->callback)
+ {
+ if (-1 == halFillServiceInfo(descriptor, buffer, length, USART_SPI_READ_MODE))
+ return -1;
+
+ halEnableUsartSpiRxcInterrupt(descriptor->tty);
+ halEnableUsartSpiDremInterrupt(descriptor->tty);
+ return 0;
+ }
+ else
+ {
+ return halSyncUsartSpiReadData(descriptor->tty, buffer, length);
+ }
+}
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Reads a number of bytes from spi internal buffer and places them to the buffer.
+\param[in]
+ descriptor - pointer to HAL_SpiDescriptor_t structure
+\param[in]
+ buffer - pointer to the application data buffer
+\param[in]
+ length - number of bytes to transfer
+\return
+ -1 - spi module was not opened, or there is unsent data, or the pointer to
+ data or the length are NULL; \n
+ Number of read bytes from spi internal buffer.
+******************************************************************************/
+static int halReadRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ uint16_t wasRead = 0;
+ uint16_t poW;
+ uint16_t poR;
+ HalUsartService_t *halBufferControl;
+
+ if (!buffer || !length)
+ return -1;
+
+ if (-1 == halCheckSpiDescriptor(descriptor, isOpenedPd))
+ return -1;
+
+ if (descriptor != halRealSpiDescripPointer)
+ return -1; // incorrect descriptor
+
+ halBufferControl = &halRealSpiDescripPointer->spiDescriptor.service;
+ ATOMIC_SECTION_ENTER
+ poW = halBufferControl->rxPointOfWrite;
+ poR = halBufferControl->rxPointOfRead;
+ ATOMIC_SECTION_LEAVE
+
+ while ((poR != poW) && (wasRead < length))
+ {
+ buffer[wasRead] = rxSlaveBuffer[poR];
+ if (HAL_SPI_RX_BUFFER_LENGTH == ++poR)
+ poR = 0;
+ wasRead++;
+ }
+
+ ATOMIC_SECTION_ENTER
+ halBufferControl->rxPointOfRead = poR;
+ halBufferControl->rxBytesInBuffer -= wasRead;
+ ATOMIC_SECTION_LEAVE
+
+ return wasRead;
+}
+#endif
+
+/**************************************************************************//**
+\brief For master : writes a number of bytes to the spi.\n
+ Callback function will be used to notify when the activity is finished.\n
+ The read data is placed to the buffer. \n
+ For slave: reads a number of bytes from internal spi buffer and writes them \n
+ to application buffer.
+\param[in]
+ descriptor - pointer to HAL_SpiDescriptor_t structure
+\param[in]
+ buffer - pointer to the application data buffer
+\param[in]
+ length - number of bytes to transfer
+\return
+ -1 - spi module was not opened, or there is unsent data, or the pointer to
+ data or the length are NULL; \n
+ 0 - success for master; \n
+ Number of written bytes if the synchronous method is used(callback is NULL) for master \n
+ or number of read bytes from internal buffer to the application buffer for slave.
+******************************************************************************/
+int HAL_ReadSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+#if defined(ATMEGA128RFA1)
+ if (SPI_CHANNEL_2 == descriptor->tty)
+ return halReadRealSpi(descriptor, buffer, length);
+ else
+#endif
+ return halReadUsartSpi(descriptor, buffer, length);
+}
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Puts the received byte to the cyclic buffer.
+
+\param[in]
+ data - data to put.
+******************************************************************************/
+void halSpiRxBufferFiller(uint8_t data)
+{
+ uint16_t old;
+ HalUsartService_t *halBufferControl;
+
+ if (NULL == halRealSpiDescripPointer)
+ {// abnormal
+ halClearRealSpi();
+ return;
+ }
+
+ halBufferControl = &halRealSpiDescripPointer->spiDescriptor.service;
+ old = halBufferControl->rxPointOfWrite;
+
+ if (HAL_SPI_RX_BUFFER_LENGTH == ++halBufferControl->rxPointOfWrite)
+ halBufferControl->rxPointOfWrite = 0;
+
+ if (halBufferControl->rxPointOfWrite == halBufferControl->rxPointOfRead)
+ { // Buffer full.
+ halBufferControl->rxPointOfWrite = old;
+ return;
+ } // Buffer full.
+
+ rxSlaveBuffer[old] = data;
+ halBufferControl->rxBytesInBuffer++;
+}
+#endif
+
+#if defined(ATMEGA128RFA1)
+/**************************************************************************//**
+\brief Slave spi reception complete interrupt handler.
+******************************************************************************/
+void halSpiRxByteComplete(void)
+{
+ uint16_t number;
+
+ ATOMIC_SECTION_ENTER
+ number = halRealSpiDescripPointer->spiDescriptor.service.rxBytesInBuffer;
+ ATOMIC_SECTION_LEAVE
+
+ if (number)
+ if (NULL != halRealSpiDescripPointer->slave_callback)
+ halRealSpiDescripPointer->slave_callback(number);
+}
+#endif
+
+// eof spi.c
+
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/timer.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/timer.c
new file mode 100644
index 00000000..ca36cca9
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/timer.c
@@ -0,0 +1,114 @@
+/**************************************************************************//**
+ \file timer.c
+
+ \brief Functions to manipulate by timers list.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 7/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <bcTimer.h>
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Adds timer to the timer's list.
+Parameters:
+ head - address of pointer to head of the timers list.
+ timer - address of timer that must be added to the list.
+ sysTime - current time.
+Returns:
+ none.
+******************************************************************************/
+void halAddTimer(Timer_t **head, Timer_t *new, uint32_t sysTime)
+{
+ if (!*head)
+ {
+ *head = new;
+ return;
+ }
+
+ Timer_t *it, *prev = NULL;
+ for (it = *head; it; it = it->service.next)
+ {
+ uint32_t remain = it->service.sysTimeLabel + it->interval - sysTime;
+ if ((remain < INT32_MAX) && (remain >= new->interval))
+ break;
+ prev = it;
+ }
+ if (it == *head)
+ {
+ new->service.next = *head;
+ *head = new;
+ }
+ else
+ {
+ prev->service.next = new;
+ new->service.next = it;
+ }
+}
+
+/******************************************************************************
+Removes timer from the timers list.
+Parameters:
+ head - address of pointer to head of the timers list.
+ prev - address of the timer before the timer that must be removed from the list.
+ p - address of timer that must be removed from the list.
+Returns:
+ pointer to next cell or pointer to head if deleting is head
+******************************************************************************/
+Timer_t* halRemoveTimer(Timer_t **head, Timer_t *prev, Timer_t *p)
+{
+ Timer_t *t;
+
+ if (p == *head)
+ {// removing first element of list
+ t = p->service.next;
+ p->service.next = 0;
+ *head = t;
+ return *head;
+ }
+ else
+ {
+ prev->service.next = p->service.next;
+ p->service.next = 0;
+ return prev->service.next;
+ }
+}
+
+/******************************************************************************
+The search of the timer in the timers list before one.
+Parameters:
+ head - address of pointer to head of the timers list.
+Returns:
+ pointer to saerching timer
+******************************************************************************/
+Timer_t *halFindPrevTimer(Timer_t **head, Timer_t *p)
+{
+ Timer_t *t = *head;
+
+ for (; t ;)
+ {
+ if (t->service.next == p)
+ return t;
+ t = t->service.next;
+ }
+ return NULL;
+}
+//eof timer.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usart.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usart.c
new file mode 100644
index 00000000..53a63927
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usart.c
@@ -0,0 +1,1061 @@
+/**************************************************************************//**
+\file usart.c
+
+\brief USART implementation. Asynchronous mode.
+
+\author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+\internal
+ History:
+ 29/05/07 E. Ivanov - Created
+ 18/02/09 A. Luzhetsky - Corretced.
+*******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halDbg.h>
+#include <usart.h>
+#include <appTimer.h>
+#include <halDiagnostic.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define HANDLERS_GET(A, I) memcpy_P(A, &halUsartHandlers[I], sizeof(HalUsartTask_t))
+/** \brief Amount of reserved bytes in received buffer. Some clients (PC Windows for example)
+ send few more bytes after CTS setting, so we need to reserve some space for them.
+ Reserved space = Buffer Size / 2^BUFFER_RESERV. */
+#define BUFFER_RESERV 1
+#define USART_HW_CONTROLLER_TIMER_PERIOD 10
+#if defined(_USE_USART_ERROR_EVENT_)
+ #define HAL_BM_FRAME_ERROR (1 << 4)
+ #define HAL_BM_DATA_OVERRUN (1 << 3)
+ #define HAL_BM_PARITY_ERROR (1 << 2)
+#endif
+#if NUM_USART_CHANNELS == 0
+ #error 'USART channels is not alowed.'
+#endif
+
+/******************************************************************************
+ Types definition section
+******************************************************************************/
+/**************************************************************************//**
+ \brief HAL USART tasks bit mask.
+******************************************************************************/
+typedef volatile uint8_t HalUsartTaskBitMask_t;
+
+/**************************************************************************//**
+ \brief HAL USART task type declaration.
+******************************************************************************/
+typedef void (* HalUsartTask_t)(void);
+
+/******************************************************************************
+ Global functions prototypes section
+******************************************************************************/
+void halSigUsartHandler(void);
+void halSetUsartConfig(HAL_UsartDescriptor_t *usartmode);
+void halPostUsartTask(HalUsartTaskId_t taskId);
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ void hwControlPinsPollCallback(void);
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+/******************************************************************************
+ Static function prototypes section
+******************************************************************************/
+#if defined(HAL_USE_USART_CHANNEL_0)
+ static void halUsartTaskUsart0Dre(void);
+ static void halUsartTaskUsart0Txc(void);
+ static void halUsartTaskUsart0Rxc(void);
+ #if defined(_USE_USART_ERROR_EVENT_)
+ static void halUsartTaskUsart0Err(void);
+ #endif
+#endif
+
+#if defined(HAL_USE_USART_CHANNEL_1)
+ static void halUsartTaskUsart1Dre(void);
+ static void halUsartTaskUsart1Txc(void);
+ static void halUsartTaskUsart1Rxc(void);
+ #if defined(_USE_USART_ERROR_EVENT_)
+ static void halUsartTaskUsart1Err(void);
+ #endif
+#endif
+
+static void halUsartHwController(UsartChannel_t tty);
+static void halSigUsartReceptionComplete(UsartChannel_t tty);
+static void halSetUsartClockPinDirection(HAL_UsartDescriptor_t *descriptor);
+
+/******************************************************************************
+ Static variables section
+******************************************************************************/
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ static HAL_AppTimer_t halUsartAppTimer;
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+HAL_UsartDescriptor_t *halPointDescrip[NUM_USART_CHANNELS] =
+{
+ #if defined(HAL_USE_USART_CHANNEL_0)
+ NULL,
+ #endif
+ #if defined(HAL_USE_USART_CHANNEL_1)
+ NULL
+ #endif
+};
+static volatile HalUsartTaskBitMask_t halUsartTaskBitMask = 0; // HAL USART tasks' bit mask.
+static const HalUsartTask_t PROGMEM_DECLARE(halUsartHandlers[HAL_USART_TASKS_NUMBER]) =
+{
+ #if defined(HAL_USE_USART_CHANNEL_0)
+ halUsartTaskUsart0Dre,
+ halUsartTaskUsart0Txc,
+ halUsartTaskUsart0Rxc,
+ #if defined(_USE_USART_ERROR_EVENT_)
+ halUsartTaskUsart0Err,
+ #endif
+ #endif
+
+ #if defined(HAL_USE_USART_CHANNEL_1)
+ halUsartTaskUsart1Dre,
+ halUsartTaskUsart1Txc,
+ halUsartTaskUsart1Rxc,
+ #if defined(_USE_USART_ERROR_EVENT_)
+ halUsartTaskUsart1Err,
+ #endif
+ #endif
+}; // List Of possible HAL USART tasks.
+
+/******************************************************************************
+ DTR service
+******************************************************************************/
+volatile bool halEnableDtrWakeUp = false;
+void (* dtrWakeUpCallback)(void) = NULL;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief HAL USART task. Exact action depends on USART internal task.
+******************************************************************************/
+void halSigUsartHandler(void)
+{
+ HalUsartTask_t handler;
+ HalUsartTaskBitMask_t mask = 1;
+ HalUsartTaskId_t index = 0;
+
+ for ( ; index < HAL_USART_TASKS_NUMBER; index++, mask <<= 1)
+ {
+ if (halUsartTaskBitMask & mask)
+ {
+ ATOMIC_SECTION_ENTER
+ halUsartTaskBitMask ^= mask;
+ ATOMIC_SECTION_LEAVE
+ HANDLERS_GET(&handler, index);
+ handler();
+ }
+ }
+}
+
+/**************************************************************************//**
+\brief Posts specific USART task.
+
+\param[in]
+ taskId - unique identifier of the task to be posted.
+******************************************************************************/
+void halPostUsartTask(HalUsartTaskId_t taskId)
+{
+ halUsartTaskBitMask |= (HalUsartTaskBitMask_t)1 << taskId;
+ halPostTask2(HAL_TASK_USART);
+}
+
+/**************************************************************************//**
+\brief Puts the byte received to the cyclic buffer.
+
+\param[in]
+ tty - channel number.
+\param[in]
+ data - data to put.
+******************************************************************************/
+void halUsartRxBufferFiller(UsartChannel_t tty, uint8_t data)
+{
+ uint16_t old;
+ uint8_t i;
+ HalUsartService_t *halUsartControl;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(tty);
+ if (NULL == halPointDescrip[i])
+ {// abnormal
+ halDisableUsartRxcInterrupt(tty); // disable usart
+ return;
+ }
+
+ if (halPointDescrip[i]->flowControl & USART_SPI_WRITE_MODE)
+ return;
+
+ if (halPointDescrip[i]->flowControl & USART_SPI_READ_MODE)
+ { // For spi mode.
+ *(uint8_t*)(halPointDescrip[i]->rxBuffer) = data;
+ halPointDescrip[i]->rxBuffer++;
+ return;
+ } // For spi mode.
+
+ halUsartControl = &halPointDescrip[i]->service;
+ if (NULL != halPointDescrip[i]->rxBuffer)
+ {
+ old = halUsartControl->rxPointOfWrite;
+
+ if (++halUsartControl->rxPointOfWrite == halPointDescrip[i]->rxBufferLength)
+ halUsartControl->rxPointOfWrite = 0;
+
+ if (halUsartControl->rxPointOfWrite == halUsartControl->rxPointOfRead)
+ { // Buffer full.
+ halUsartControl->rxPointOfWrite = old;
+ return;
+ } // Buffer full.
+
+ halPointDescrip[i]->rxBuffer[old] = data;
+ halUsartControl->rxBytesInBuffer++;
+
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ if ((halPointDescrip[i]->flowControl & USART_FLOW_CONTROL_HARDWARE) && (HW_CONTROL_PINS_PORT_ASSIGNMENT == halPointDescrip[i]->tty))
+ {
+ if (halUsartControl->rxBytesInBuffer > (halPointDescrip[i]->rxBufferLength >> BUFFER_RESERV))
+ GPIO_USART_CTS_set();// CTS_ON
+ }
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+ }
+}
+
+#if defined(_USE_USART_ERROR_EVENT_)
+/**************************************************************************//**
+\brief Save status register for analyzing of the error reason.
+
+\param[in]
+ tty - channel number.
+\param[in]
+ status - usart status register.
+******************************************************************************/
+void halUsartSaveErrorReason(UsartChannel_t tty, uint8_t status)
+{
+ HalUsartService_t *halUsartControl;
+ uint8_t i;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(tty);
+ if (NULL == halPointDescrip[i])
+ {// abnormal
+ halDisableUsartRxcInterrupt(tty); // disable usart
+ return;
+ }
+
+ halUsartControl = &halPointDescrip[i]->service;
+ halUsartControl->errorReason = status;
+}
+#endif
+
+/**************************************************************************//**
+\brief Registers uasrt's event handlers. Performs configuration
+of usart registers. Performs configuration of RTS, CTS and DTR pins.
+
+\param[in]
+ descriptor - pointer to HAL_UsartDescriptor_t structure
+
+\return
+ Returns positive usart descriptor on success or -1 in cases: \n
+ - bad usart channel. \n
+ - unsupported parameters. \n
+ - the channel was already opened. \n
+ - there are not enough resources. \n
+******************************************************************************/
+int HAL_OpenUsart(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i; // Descriptor index
+
+ if (NULL == descriptor)
+ return -1;
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ if ((descriptor->flowControl & USART_FLOW_CONTROL_HARDWARE) &&
+ (HW_CONTROL_PINS_PORT_ASSIGNMENT != descriptor->tty))
+ return -1; // Hardware control cannot be used for this channel.
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (NULL != halPointDescrip[i])
+ return -1; // Channel is already opened.
+
+ halPointDescrip[i] = descriptor;
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ if (HW_CONTROL_PINS_PORT_ASSIGNMENT == descriptor->tty)
+ {
+ if (descriptor->flowControl & USART_DTR_CONTROL)
+ GPIO_USART_DTR_make_in();
+ if (descriptor->flowControl & USART_FLOW_CONTROL_HARDWARE)
+ {
+ GPIO_USART_CTS_make_out();
+ GPIO_USART_RTS_make_in();
+ if (NULL == descriptor->rxBuffer)
+ GPIO_USART_CTS_set(); // CTS_ON
+ else
+ GPIO_USART_CTS_clr(); // CTS_OFF
+ }
+ }
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+ if (USART_MODE_SYNC == descriptor->mode)
+ halSetUsartClockPinDirection(descriptor);
+
+ descriptor->service.txPointOfRead = 0;
+ descriptor->service.txPointOfWrite = 0;
+ if (NULL == descriptor->rxBuffer)
+ descriptor->rxBufferLength = 0;
+ if (NULL == descriptor->txBuffer)
+ descriptor->txBufferLength = 0;
+ descriptor->service.rxPointOfRead = 0;
+ descriptor->service.rxPointOfWrite = 0;
+ descriptor->service.usartShiftRegisterEmpty = 1;
+
+ halSetUsartConfig(descriptor);
+
+ return descriptor->tty;
+}
+
+/**************************************************************************//**
+\brief Frees the usart channel and pins, if hardware flow control was used.
+
+\param[in]
+ descriptor - the usart descriptor.
+\return
+ 0 on success, \n
+ -1 if bad descriptor or channel is already closed.
+******************************************************************************/
+int HAL_CloseUsart(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i;
+
+ if (NULL == descriptor)
+ return -1;
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (NULL == halPointDescrip[i])
+ return -1; // Channel is already closed.
+
+ halCloseUsart(halPointDescrip[i]->tty);
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ if (halPointDescrip[i]->flowControl & USART_FLOW_CONTROL_HARDWARE)
+ GPIO_USART_CTS_make_in();
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+ if (USART_MODE_SYNC == halPointDescrip[i]->mode)
+ {
+ halPointDescrip[i]->syncMode = USART_CLK_MODE_SLAVE;
+ halSetUsartClockPinDirection(halPointDescrip[i]);
+ }
+ halPointDescrip[i] = NULL;
+
+ return 0;
+}
+
+/**************************************************************************//**
+\brief Controls RTS and DTR the pins and makes decision if the usart can transmit
+ byte.
+
+\param[in]
+ tty - channel number.
+******************************************************************************/
+static void halUsartHwController(UsartChannel_t tty)
+{
+ uint8_t i;
+ HalUsartService_t *halUsartControl;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(tty);
+ if (NULL == halPointDescrip[i])
+ return; // Port closed.
+
+ halUsartControl = &halPointDescrip[i]->service;
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ if (HW_CONTROL_PINS_PORT_ASSIGNMENT == tty)
+ {
+ uint8_t hw1 = 0;
+ uint8_t hw2 = 0;
+
+ if (halPointDescrip[i]->flowControl & USART_DTR_CONTROL)
+ hw1 = GPIO_USART_DTR_read();
+
+ if (halPointDescrip[i]->flowControl & USART_FLOW_CONTROL_HARDWARE)
+ hw2 = GPIO_USART_RTS_read();
+
+ if (hw1 || hw2)
+ {
+ halUsartAppTimer.interval = USART_HW_CONTROLLER_TIMER_PERIOD;
+ halUsartAppTimer.mode = TIMER_ONE_SHOT_MODE;
+ halUsartAppTimer.callback = hwControlPinsPollCallback;
+ HAL_StartAppTimer(&halUsartAppTimer);
+ return;
+ }
+ }
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+ uint16_t poW;
+ uint16_t poR;
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ poW = halUsartControl->txPointOfWrite;
+ poR = halUsartControl->txPointOfRead;
+ END_MEASURE(HAL_USART_HW_CONTROLLER_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ if (poW != poR)
+ {
+ halSendUsartByte(tty, halPointDescrip[i]->txBuffer[poR++]);
+ if (poR == halPointDescrip[i]->txBufferLength)
+ poR = 0;
+ halEnableUsartDremInterrupt(tty);
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ halUsartControl->txPointOfRead = poR;
+ END_MEASURE(HAL_USART_HW_CONTROLLER_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ }
+ else
+ {
+ // data register empty interrupt was disabled
+ halEnableUsartTxcInterrupt(tty);// TX Complete interrupt enable
+ }
+}
+
+/**************************************************************************//**
+\brief Writes a number of bytes to a usart channel.
+txCallback function will be used to notify when the transmission is finished.
+If hardware flow control is used for transmitting then RTS and DTR pins will
+be tested during transmission.
+
+\param[in]
+ descriptor - pointer to HAL_UsartDescriptor_t structure;
+
+\param[in]
+ buffer - pointer to the application data buffer;
+
+\param[in]
+ length - number of bytes to transfer;
+
+\return
+ -1 - bad descriptor; \n
+ Number of bytes placed to the buffer - success.
+******************************************************************************/
+int HAL_WriteUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ uint8_t i;
+ uint16_t poW;
+ uint16_t poR;
+ uint16_t old;
+ uint16_t wasWrote = 0;
+ bool needStartTrmt = false;
+ HalUsartService_t *halUsartControl;
+
+ if (NULL == descriptor)
+ return -1;
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+ if (!buffer || !length)
+ return -1;
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ halUsartControl = &descriptor->service;
+ if (0 == descriptor->txBufferLength)
+ { // Callback mode
+ if (halUsartControl->txPointOfWrite != halUsartControl->txPointOfRead)
+ return -1; // there is unsent data
+ descriptor->txBuffer = buffer;
+ halUsartControl->txPointOfWrite = length;
+ halUsartControl->txPointOfRead = 0;
+ needStartTrmt = true;
+ wasWrote = length;
+ } // Callback mode.
+ else
+ { // Polling mode.
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ poW = halUsartControl->txPointOfWrite;
+ poR = halUsartControl->txPointOfRead;
+ END_MEASURE(HALATOM_WRITE_USART_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ if (poW == poR)
+ needStartTrmt = true; // Buffer empty.
+
+ while (wasWrote < length)
+ {
+ old = poW;
+
+ if (++poW == descriptor->txBufferLength)
+ poW = 0;
+
+ if (poW == poR)
+ { // Buffer full.
+ poW = old;
+ break;
+ } // Buffer full.
+
+ descriptor->txBuffer[old] = buffer[wasWrote++];
+ }
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ halUsartControl->txPointOfWrite = poW;
+ END_MEASURE(HALATOM_WRITE_USART_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+ } // Polling mode
+
+ if (needStartTrmt)
+ {
+ halUsartControl->usartShiftRegisterEmpty = 0; // Buffer and shift register is full
+ // Enable interrupt. Transaction will be launched in the callback.
+ halEnableUsartDremInterrupt(descriptor->tty);
+ }
+
+ return wasWrote;
+}
+
+/*************************************************************************//**
+\brief Reads length bytes from usart and places ones to buffer.
+
+\param[in]
+ descriptor - usart descriptor;
+\param[out]
+ buffer - pointer to a application buffer;
+\param[in]
+ length - the number of bytes which should be placed to buffer
+
+\return
+ -1 - bad descriptor, bad number to read or number of bytes that \n
+ were placed to buffer.
+*****************************************************************************/
+int HAL_ReadUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ uint8_t i = 0;
+ uint16_t wasRead = 0;
+ uint16_t poW;
+ uint16_t poR;
+ HalUsartService_t *halUsartControl;
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ uint16_t number;
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+ if (NULL == descriptor)
+ return -1;
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+ if (!buffer || !length)
+ return -1;
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ halUsartControl = &halPointDescrip[i]->service;
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ poW = halUsartControl->rxPointOfWrite;
+ poR = halUsartControl->rxPointOfRead;
+ END_MEASURE(HALATOM_READ_USART_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ while ((poR != poW) && (wasRead < length))
+ {
+ buffer[wasRead] = descriptor->rxBuffer[poR];
+ if (++poR == descriptor->rxBufferLength)
+ poR = 0;
+ wasRead++;
+ }
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ halUsartControl->rxPointOfRead = poR;
+ halUsartControl->rxBytesInBuffer -= wasRead;
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ number = halUsartControl->rxBytesInBuffer;
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+ END_MEASURE(HALATOM_READ_USART_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+ if ((HW_CONTROL_PINS_PORT_ASSIGNMENT == descriptor->tty) && (descriptor->flowControl & USART_FLOW_CONTROL_HARDWARE))
+ if (number <= (descriptor->rxBufferLength >> BUFFER_RESERV))
+ GPIO_USART_CTS_clr();
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+ return wasRead;
+}
+
+/**************************************************************************//**
+\brief Forbids to the host data transmiting. Only HW_CONTROL_PINS_PORT_ASSIGNMENT
+ port can be used for hardware flow control.
+
+\param[in]
+ descriptor - usart descriptor.
+
+\return
+ -1 - bad descriptor, bad usart, unsupported mode;
+ 0 - on success.
+******************************************************************************/
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+int HAL_OnUsartCts(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ if (HW_CONTROL_PINS_PORT_ASSIGNMENT != descriptor->tty)
+ return -1;
+
+ GPIO_USART_CTS_set();// CTS_ON
+
+ return 0;
+}
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+/**************************************************************************//**
+\brief Allows to transfer a host data. Only HW_CONTROL_PINS_PORT_ASSIGNMENT
+can be used for hardware flow control.
+
+\param[in]
+ descriptor - usart descriptor.
+
+\return
+ -1 - bad descriptor, bad usart, unsupported mode;
+ 0 - on success.
+******************************************************************************/
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+int HAL_OffUsartCts(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ if (HW_CONTROL_PINS_PORT_ASSIGNMENT != descriptor->tty)
+ return -1;
+
+ GPIO_USART_CTS_clr(); // CTS_OFF
+
+ return 0;
+}
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+/**************************************************************************//**
+\brief Fills UsartHardwareControl_t variable by potential of RTS pin. Only
+ HW_CONTROL_PINS_PORT_ASSIGNMENT can be used for hardware flow control.
+
+\param[in]
+ descriptor - usart descriptor.
+\return
+ -1 - bad descriptor, bad usart, unsupported mode;
+ 0 - on success.
+******************************************************************************/
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+int HAL_ReadUsartRts(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ if (HW_CONTROL_PINS_PORT_ASSIGNMENT != descriptor->tty)
+ return -1;
+
+ return GPIO_USART_RTS_read();
+}
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+/**************************************************************************//**
+\brief Fills UsartHardwareControl_t variable by potential of DTR pin. Only
+ HW_CONTROL_PINS_PORT_ASSIGNMENT can be used for hardware flow control.
+
+\param[in]
+ descriptor - usart descriptor.
+\return
+ -1 - bad descriptor, bad usart, unsupported mode;
+ 0 - on success.
+******************************************************************************/
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+int HAL_ReadUsartDtr(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ if (HW_CONTROL_PINS_PORT_ASSIGNMENT != descriptor->tty)
+ return -1;
+
+ return GPIO_USART_DTR_read();
+}
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+// Interrupt handlers
+/**************************************************************************//**
+\brief Hardware Control pins polling timer callback.
+******************************************************************************/
+#ifdef HW_CONTROL_PINS_PORT_ASSIGNMENT
+void hwControlPinsPollCallback(void)
+{
+ halUsartHwController(HW_CONTROL_PINS_PORT_ASSIGNMENT);
+}
+#endif // HW_CONTROL_PINS_PORT_ASSIGNMENT
+
+/**************************************************************************//**
+\brief Transmission complete interrupt handler.
+
+\param[in]
+ tty - USART channel identifier.
+******************************************************************************/
+void halSigUsartTransmissionComplete(UsartChannel_t tty)
+{
+ uint8_t i;
+ HalUsartService_t *halUsartControl;
+ uint16_t poW;
+ uint16_t poR;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(tty);
+ if (NULL == halPointDescrip[i])
+ {
+ assert(false, USARTC_HALSIGUSARTTRANSMISSIONCOMPLETE_0);
+ return; // Descriptor with "tty" channel is not found.
+ }
+
+ halUsartControl = &halPointDescrip[i]->service;
+
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ poW = halUsartControl->txPointOfWrite;
+ poR = halUsartControl->txPointOfRead;
+ END_MEASURE(HAL_USART_TRANS_COMPLETE_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ if (poW == poR)
+ halUsartControl->usartShiftRegisterEmpty = 1; // Buffer is empty, shift register is empty too.
+
+ if (0 == halPointDescrip[i]->txBufferLength)
+ halPointDescrip[i]->txBuffer = NULL; // nulling pointer for callback mode
+
+ if (NULL != halPointDescrip[i]->txCallback)
+ halPointDescrip[i]->txCallback();
+}
+
+/**************************************************************************//**
+\brief Reception complete interrupt handler.
+
+\param[in]
+ tty - USART channel identifier.
+******************************************************************************/
+static void halSigUsartReceptionComplete(UsartChannel_t tty)
+{
+ uint8_t i;
+ HalUsartService_t *halUsartControl;
+ uint16_t number;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(tty);
+ if (NULL == halPointDescrip[i])
+ {
+ assert(false, USARTC_HALSIGUSARTRECEPTIONCOMPLETE_0);
+ return; // Descriptor with "tty" channel is not found.
+ }
+
+ if (halPointDescrip[i]->flowControl & (USART_SPI_READ_MODE | USART_SPI_WRITE_MODE))
+ return; // for spi mode
+
+ halUsartControl = &halPointDescrip[i]->service;
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ number = halUsartControl->rxBytesInBuffer;
+ END_MEASURE(HALATOM_USART_RX_COMPLETE_TIME_LIMIT)
+ ATOMIC_SECTION_LEAVE
+
+ if (number)
+ if (NULL != halPointDescrip[i]->rxCallback)
+ halPointDescrip[i]->rxCallback(number);
+}
+
+#if defined(_USE_USART_ERROR_EVENT_)
+/**************************************************************************//**
+\brief Error occurred action handler.
+
+\param[in]
+ tty - USART channel identifier.
+******************************************************************************/
+static void halSigUsartErrorOccurred(UsartChannel_t tty)
+{
+ uint8_t i;
+ HalUsartService_t *halUsartControl;
+ UsartErrorReason_t errReason = FRAME_ERROR;
+
+ i = HAL_GET_INDEX_BY_CHANNEL(tty);
+ if (NULL == halPointDescrip[i])
+ {
+ assert(false, USARTC_HALSIGUSARTERROROCCURED_0);
+ return; // Descriptor with "tty" channel is not found.
+ }
+
+ halUsartControl = &halPointDescrip[i]->service;
+ if (halUsartControl->errorReason & HAL_BM_FRAME_ERROR)
+ errReason = FRAME_ERROR;
+ else if (halUsartControl->errorReason & HAL_BM_DATA_OVERRUN)
+ errReason = DATA_OVERRUN;
+ else if (halUsartControl->errorReason & HAL_BM_PARITY_ERROR)
+ errReason = PARITY_ERROR;
+ else
+ {
+ assert(false, USARTC_HALUNKNOWNERRORREASON_0);
+ }
+
+ if (NULL != halPointDescrip[i]->errCallback)
+ halPointDescrip[i]->errCallback(errReason);
+}
+#endif
+
+/**************************************************************************//**
+\brief Enables DTR wake up.
+
+\param[in] callback - callback method pointer.
+******************************************************************************/
+void HAL_EnableDtrWakeUp(void (* callback)(void))
+{
+ dtrWakeUpCallback = callback;
+ halEnableDtrWakeUp = true;
+}
+
+/**************************************************************************//**
+\brief Disables DTR wake up.
+******************************************************************************/
+void HAL_DisableDtrWakeUp(void)
+{
+ halEnableDtrWakeUp = false;
+}
+
+/**************************************************************************//**
+\brief Checks the status of tx buffer.
+
+\param[in]
+ descriptor - pointer to HAL_UsartDescriptor_t structure;
+
+\return
+ -1 - bad descriptor, no tx buffer; \n
+ 1 - tx buffer is empty; \n
+ 0 - tx buffer is not empty;
+******************************************************************************/
+int HAL_IsTxEmpty(HAL_UsartDescriptor_t *descriptor)
+{
+ uint8_t i;
+ HalUsartService_t *halUsartControl;
+ uint16_t poW;
+ uint16_t poR;
+
+ if (NULL == descriptor)
+ return -1;
+ if (false == halIsUsartChannelCorrect(descriptor->tty))
+ return -1;
+ i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
+ if (descriptor != halPointDescrip[i])
+ return -1; // Channel is not opened.
+
+ halUsartControl = &halPointDescrip[i]->service;
+ ATOMIC_SECTION_ENTER
+ BEGIN_MEASURE
+ poW = halUsartControl->txPointOfWrite;
+ poR = halUsartControl->txPointOfRead;
+ END_MEASURE(HAL_USART_TX_EMPTY_LIMIT)
+ ATOMIC_SECTION_LEAVE
+ if (poW == poR)
+ return halUsartControl->usartShiftRegisterEmpty;
+ return 0;
+}
+
+/**************************************************************************//**
+\brief Checks the channel number.
+
+\param[in]
+ channel - channel to be verified.
+
+\return
+ true if channel is possible, \n
+ false otherwise.
+******************************************************************************/
+bool halIsUsartChannelCorrect(UsartChannel_t channel)
+{
+ switch (channel)
+ {
+#ifdef USART_CHANNEL_0
+ case USART_CHANNEL_0:
+#endif // USART_CHANNEL_0
+#ifdef USART_CHANNEL_1
+ case USART_CHANNEL_1:
+#endif // USART_CHANNEL_0
+#if defined(USART_CHANNEL_0) || defined(USART_CHANNEL_1)
+ return true;
+#endif
+ default:
+ return false;
+ }
+}
+
+/**************************************************************************//**
+\brief Set clock pin direction for synchronous mode.
+
+\param[in]
+ descriptor - pointer to usart channel descriptor.
+******************************************************************************/
+static void halSetUsartClockPinDirection(HAL_UsartDescriptor_t *descriptor)
+{
+ if (USART_CLK_MODE_MASTER == descriptor->syncMode)
+ {
+ switch (descriptor->tty)
+ {
+#ifdef USART_CHANNEL_0
+ case USART_CHANNEL_0:
+ GPIO_USART0_EXTCLK_make_out();
+ break;
+#endif // USART_CHANNEL_0
+#ifdef USART_CHANNEL_1
+ case USART_CHANNEL_1:
+ GPIO_USART1_EXTCLK_make_out();
+ break;
+#endif // USART_CHANNEL_1
+ default:
+ break;
+ }
+ }
+ else
+ {
+ switch (descriptor->tty)
+ {
+#ifdef USART_CHANNEL_0
+ case USART_CHANNEL_0:
+ GPIO_USART0_EXTCLK_make_in();
+ GPIO_USART0_EXTCLK_make_pullup();
+ break;
+#endif // USART_CHANNEL_0
+#ifdef USART_CHANNEL_1
+ case USART_CHANNEL_1:
+ GPIO_USART1_EXTCLK_make_in();
+ GPIO_USART1_EXTCLK_make_pullup();
+ break;
+#endif // USART_CHANNEL_1
+ default:
+ break;
+ }
+ }
+}
+
+#if defined(HAL_USE_USART_CHANNEL_0)
+/**************************************************************************//**
+\brief Wrapper for data empty handler for usart channel 0
+******************************************************************************/
+static void halUsartTaskUsart0Dre(void)
+{
+ halUsartHwController(USART_CHANNEL_0);
+}
+
+/**************************************************************************//**
+\brief Wrapper for transmit complete handler for usart channel 0
+******************************************************************************/
+static void halUsartTaskUsart0Txc(void)
+{
+ halSigUsartTransmissionComplete(USART_CHANNEL_0);
+}
+
+/**************************************************************************//**
+\brief Wrapper for receive complete handler for usart channel 0
+******************************************************************************/
+static void halUsartTaskUsart0Rxc(void)
+{
+ halSigUsartReceptionComplete(USART_CHANNEL_0);
+}
+
+#if defined(_USE_USART_ERROR_EVENT_)
+/**************************************************************************//**
+\brief Wrapper for error occurred handler for usart channel 0
+******************************************************************************/
+static void halUsartTaskUsart0Err(void)
+{
+ halSigUsartErrorOccurred(USART_CHANNEL_0);
+}
+#endif // defined(_USE_USART_ERROR_EVENT_)
+#endif // defined(HAL_USE_USART_CHANNEL_0)
+
+#if defined(HAL_USE_USART_CHANNEL_1)
+/**************************************************************************//**
+\brief Wrapper for data empty handler for usart channel 1
+******************************************************************************/
+static void halUsartTaskUsart1Dre(void)
+{
+ halUsartHwController(USART_CHANNEL_1);
+}
+
+/**************************************************************************//**
+\brief Wrapper for transmit complete handler for usart channel 1
+******************************************************************************/
+static void halUsartTaskUsart1Txc(void)
+{
+ halSigUsartTransmissionComplete(USART_CHANNEL_1);
+}
+
+/**************************************************************************//**
+\brief Wrapper for receive complete handler for usart channel 0
+******************************************************************************/
+static void halUsartTaskUsart1Rxc(void)
+{
+ halSigUsartReceptionComplete(USART_CHANNEL_1);
+}
+
+#if defined(_USE_USART_ERROR_EVENT_)
+/**************************************************************************//**
+\brief Wrapper for error occurred handler for usart channel 1
+******************************************************************************/
+static void halUsartTaskUsart1Err(void)
+{
+ halSigUsartErrorOccurred(USART_CHANNEL_1);
+}
+#endif // defined(_USE_USART_ERROR_EVENT_)
+#endif // defined(HAL_USE_USART_CHANNEL_1)
+//eof usart.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usb.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usb.c
new file mode 100644
index 00000000..b164f1dc
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/usb.c
@@ -0,0 +1,435 @@
+/**************************************************************************//**
+ \file usb.c
+
+ \brief Implementation of usb hardware independent module.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 17/07/08 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halUsb.h>
+#include <usb.h>
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+// user's request handler
+extern void (* sendReqToUpper)(uint8_t *req);
+// Holds the internal state for each endpoint of the UDP
+extern UsbEndpoint_t endpoints[USB_END_POINTS_NUMBER];
+// Device current state
+extern volatile uint8_t deviceState;
+// Previous device current state
+extern volatile uint8_t previousDeviceState;
+// pointer to request memory. Memory allocate by user.
+extern uint8_t *requestMemory;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+void (* resumeCallback)(void) = NULL;
+void (* suspendCallback)(void) = NULL;
+void (* endOfBusResetCallback)(void) = NULL;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+ Registers user's end of bus reset handler
+
+Parameters:
+ f - pointer to user's callback
+******************************************************************************/
+void HAL_RegisterEndOfBusResetHandler(void (* f)(void))
+{
+ endOfBusResetCallback = f;
+}
+
+/******************************************************************************
+ Registers user's resume handler
+
+Parameters:
+ f - pointer to user's callback
+******************************************************************************/
+void HAL_RegisterResumeHandler(void (* f)(void))
+{
+ resumeCallback = f;
+}
+
+/******************************************************************************
+ Registers user's suspend handler
+
+Parameters:
+ f - pointer to user's callback
+******************************************************************************/
+void HAL_RegisterSuspendHandler(void (* f)(void))
+{
+ suspendCallback = f;
+}
+
+/******************************************************************************
+ Registers user's request handler
+
+Parameters:
+ f - pointer to user's callback
+******************************************************************************/
+void HAL_RegisterRequestHandler(void (* f)(uint8_t *req))
+{
+ sendReqToUpper = f;
+}
+
+/******************************************************************************
+Configures an endpoint according to its Endpoint Descriptor.
+
+Parameters:
+ descriptor - Pointer to an Endpoint descriptor.
+******************************************************************************/
+void HAL_ConfigureEndpoint(HAL_UsbEndPointDescptr_t *descriptor)
+{
+ UsbEndpoint_t *endpoint;
+ uint8_t eptnum;
+ uint8_t type;
+ HAL_UsbEndPointDirect_t direction;
+
+ // NULL descriptor -> Control endpoint 0
+ if (NULL == descriptor)
+ {
+ eptnum = USB_END_POINT_0;
+ endpoint = &(endpoints[USB_END_POINT_0]);
+ type = EP_CONTROL;
+ direction = EP_OUT;
+ endpoint->size = UDP_ENDPOINTS_MAXPACKETSIZE(USB_END_POINT_0);
+ }
+ else
+ {
+ eptnum = descriptor->bEndpointAddress & 0x0F;
+ endpoint = &(endpoints[eptnum]);
+ type = descriptor->bmAttributes & 0x03;
+ if (descriptor->bEndpointAddress & 0x80)
+ direction = EP_IN;
+ else
+ direction = EP_OUT;
+ endpoint->size = descriptor->wMaxPacketSize;
+ }
+
+ // Abort the current transfer is the endpoint was configured and in
+ // Write or Read state
+ if ((UDP_ENDPOINT_RECEIVING == endpoint->state) || (UDP_ENDPOINT_SENDING == endpoint->state))
+ halEndOfTransfer(eptnum, STATUS_RESET);
+
+ endpoint->state = UDP_ENDPOINT_IDLE;
+ halConfigureEndpoint(eptnum, type, direction);
+}
+
+/******************************************************************************
+Sends data through a USB endpoint. Sets up the transfer descriptor,
+writes one or two data payloads (depending on the number of FIFO bank
+for the endpoint) and then starts the actual transfer. The operation is
+complete when all the data has been sent.
+
+*If the size of the buffer is greater than the size of the endpoint
+(or twice the size if the endpoint has two FIFO banks), then the buffer
+must be kept allocated until the transfer is finished*. This means that
+it is not possible to declare it on the stack (i.e. as a local variable
+of a function which returns after starting a transfer).
+
+Parameters:
+ eptnum - Endpoint number.
+ data - Pointer to a buffer with the data to send.
+ size - Size of the data buffer.
+ callback - Optional callback function to invoke when the transfer is complete.
+ argument - Optional argument to the callback function.
+
+Returns:
+ STATUS_SUCCESS if the transfer has been started; otherwise, the
+ corresponding error status code.
+******************************************************************************/
+uint8_t HAL_UsbWrite(uint8_t eptnum, void *data, uint32_t size, TransferCallback_t callback, void *argument)
+{
+ UsbEndpoint_t *endpoint = &(endpoints[eptnum]);
+ UsbTransfer_t *transfer = &(endpoint->transfer);
+
+ // Check that the endpoint is in Idle state
+ if (UDP_ENDPOINT_IDLE != endpoint->state)
+ return STATUS_BUSY;
+
+ // Setup the transfer descriptor
+ transfer->data = (void *) data;
+ transfer->remaining = size;
+ transfer->buffered = 0;
+ transfer->transferred = 0;
+ transfer->callback = callback;
+ transfer->argument = argument;
+
+ // Send the first packet
+ endpoint->state = UDP_ENDPOINT_SENDING;
+ halStartUsbWrite(eptnum);
+
+ // Enable interrupt on endpoint
+ halEnableEndPointTxInterrupt(eptnum);
+
+ return STATUS_SUCCESS;
+}
+
+/******************************************************************************
+Reads incoming data on an USB endpoint This methods sets the transfer
+descriptor and activate the endpoint interrupt. The actual transfer is
+then carried out by the endpoint interrupt handler. The Read operation
+finishes either when the buffer is full, or a short packet (inferior to
+endpoint maximum size) is received.
+
+*The buffer must be kept allocated until the transfer is finished*.
+
+Parameters:
+ eptnum - Endpoint number.
+ data - Pointer to a data buffer.
+ size - Size of the data buffer in bytes.
+ callback - Optional end-of-transfer callback function.
+ argument - Optional argument to the callback function.
+
+Returns:
+ STATUS_SUCCESS if the read operation has been started; otherwise,
+ the corresponding error code.
+******************************************************************************/
+uint8_t HAL_UsbRead(uint8_t eptnum, void *data, uint32_t size, TransferCallback_t callback, void *argument)
+{
+ UsbEndpoint_t *endpoint = &(endpoints[eptnum]);
+ UsbTransfer_t *transfer = &(endpoint->transfer);
+
+ if (NULL == data)
+ return STATUS_ABORTED;
+
+ // Return if the endpoint is not in IDLE state
+ if (UDP_ENDPOINT_IDLE != endpoint->state)
+ return STATUS_BUSY;
+
+ // Endpoint enters Receiving state
+ endpoint->state = UDP_ENDPOINT_RECEIVING;
+
+ // Set the transfer descriptor
+ transfer->data = data;
+ transfer->remaining = size;
+ transfer->buffered = 0;
+ transfer->transferred = 0;
+ transfer->callback = callback;
+ transfer->argument = argument;
+
+ // Enable interrupt on endpoint
+ halEnableEndPointRxInterrupt(eptnum);
+
+ return STATUS_SUCCESS;
+}
+
+/******************************************************************************
+Sets the HALT feature on the given endpoint (if not already in this state).
+
+Parameters:
+ eptnum - Endpoint number.
+******************************************************************************/
+void HAL_Halt(uint8_t eptnum)
+{
+ UsbEndpoint_t *endpoint = &(endpoints[eptnum]);
+
+ // Check that endpoint is enabled and not already in Halt state
+ if ((UDP_ENDPOINT_DISABLED != endpoint->state) && (UDP_ENDPOINT_HALTED != endpoint->state))
+ {
+ // Abort the current transfer if necessary
+ halEndOfTransfer(eptnum, STATUS_ABORTED);
+
+ halEndpointHaltState(eptnum);
+
+ // Enable the endpoint interrupt
+ halEnableEndPointStallInterrupt(eptnum);
+ }
+}
+
+/******************************************************************************
+Clears the Halt feature on the given endpoint.
+
+Parameters:
+ eptnum - Endpoint number.
+******************************************************************************/
+void HAL_Unhalt(uint8_t eptnum)
+{
+ UsbEndpoint_t *endpoint = &(endpoints[eptnum]);
+
+ // Check if the endpoint is enabled
+ if (UDP_ENDPOINT_DISABLED != endpoint->state)
+ {
+ // Return endpoint to Idle state
+ endpoint->state = UDP_ENDPOINT_IDLE;
+
+ halEndpointUnHaltState(eptnum);
+ }
+}
+
+/******************************************************************************
+Returns the current Halt status of an endpoint.
+
+Parameters:
+ eptnum - Endpoint number.
+
+Returns:
+ 1 - if the endpoint is currently halted;
+ 0 - otherwise.
+******************************************************************************/
+uint8_t HAL_IsHalted(uint8_t eptnum)
+{
+ UsbEndpoint_t *endpoint = &(endpoints[eptnum]);
+ if (UDP_ENDPOINT_HALTED == endpoint->state)
+ return 1;
+ else
+ return 0;
+}
+
+/******************************************************************************
+Causes the given endpoint to acknowledge the next packet it receives with
+a STALL handshake.
+
+Parameters:
+ eptnum - Endpoint number.
+
+Returns:
+ STATUS_SUCCESS or STATUS_BUSY.
+******************************************************************************/
+uint8_t HAL_Stall(uint8_t eptnum)
+{
+ UsbEndpoint_t *endpoint = &(endpoints[eptnum]);
+
+ // Check that endpoint is in Idle state
+ if (UDP_ENDPOINT_IDLE != endpoint->state)
+ {
+ return STATUS_BUSY;
+ }
+
+ halSendStallToHost(eptnum);
+ // Enable the endpoint interrupt
+ halEnableEndPointStallInterrupt(eptnum);
+
+ return STATUS_SUCCESS;
+}
+
+/******************************************************************************
+Sets the device address to the given value.
+
+Parameters:
+ address - New device address.
+******************************************************************************/
+void HAL_SetAddress(uint8_t *address)
+{
+ halSetUsbAddress(*address);
+ // If the address is 0, the device returns to the Default state
+ if (*address)
+ deviceState = DEVICE_PREADDRESSED;
+ // If the address is non-zero, the device enters the Address state
+ else
+ deviceState = DEVICE_DEFAULT;
+}
+
+/******************************************************************************
+Sets the current device configuration.
+
+Parameters:
+ cfgnum - Configuration number to set.
+******************************************************************************/
+void HAL_SetConfiguration(uint8_t cfgnum)
+{
+ // If the configuration number if non-zero, the device enters the
+ // Configured state
+ if (cfgnum)
+ {
+ deviceState = DEVICE_CONFIGURED;
+ }
+ // If the configuration number is zero, the device goes back to the Address
+ // state
+ else
+ {
+ deviceState = DEVICE_ADDRESS;
+ // Abort all transfers
+ halDisableEndpoints();
+ }
+}
+
+/******************************************************************************
+Initializes the USB driver.
+
+Parameters:
+ reqMem - Memory for usb request. Memory allocate by user.
+******************************************************************************/
+void HAL_UsbInit(uint8_t *reqMem)
+{
+ requestMemory = reqMem;
+
+ // Reset endpoint structures
+ halResetEndpoints();
+
+ // Device is in the Attached state
+ deviceState = DEVICE_SUSPENDED;
+ previousDeviceState = DEVICE_POWERED;
+ halInitUsbDevice();
+ halUsbInterrupt();
+}
+
+/******************************************************************************
+Returns the current state of the USB device.
+
+Returns:
+ Device current state.
+******************************************************************************/
+uint8_t HAL_GetState(void)
+{
+ return deviceState;
+}
+
+/******************************************************************************
+Endpoint interrupt handler.
+******************************************************************************/
+void halEndPointHandler(void)
+{
+ halCommonEndpointHandler();
+}
+
+/******************************************************************************
+usb suspend interrupt handler.
+******************************************************************************/
+void halSuspendHandler(void)
+{
+ if (NULL != suspendCallback)
+ suspendCallback();
+}
+
+/******************************************************************************
+usb resume interrupt handler.
+******************************************************************************/
+void halResumeHandler(void)
+{
+ if (NULL != resumeCallback)
+ resumeCallback();
+}
+
+/******************************************************************************
+usb bus reset interrupt handler.
+******************************************************************************/
+void halBusResetHandler(void)
+{
+ halEndOfBusResetHandler();
+ if (NULL != endOfBusResetCallback)
+ endOfBusResetCallback();
+}
+
+// eof usb.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/w1.c b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/w1.c
new file mode 100644
index 00000000..99b92d1a
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/w1.c
@@ -0,0 +1,382 @@
+/**************************************************************************//**
+ \file w1.c
+
+ \brief The implementation of the 1-wire interface.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 10/12/07 A. Khromykh - Created
+ ******************************************************************************/
+/******************************************************************************
+ * WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK. *
+ * EXPERT USERS SHOULD PROCEED WITH CAUTION. *
+ ******************************************************************************/
+
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <halW1.h>
+#include <w1.h>
+#include <atomic.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#define HAL_ROM_BYTE_LEN 8
+#define HAL_ROM_BIT_LEN 64
+#define HAL_W1_CRC_POLINOM 0x8C
+#define HAL_SEARCH_ROM_CMD 0xF0
+#define HAL_SEARCH_ALRM_CMD 0xEC
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/*******************************************************************************
+ 1-Wire search procedure for all devices discovering
+ Parameters:
+ cmd - ROM function command.
+ family - 8-bit family code.
+ data - pointer of SRAM where are stored the 8 bytes ROM
+ codes returned by the devices.
+ count - number of devices wish to find.
+ actCount - number of devices have been found.
+ Returns:
+ W1_SUCCESS_STATUS - if at least one device has been found.
+ W1_NO_DEVICE_STATUS - if there are no any devices presented
+ on the bus with specified family code.
+ W1_INVALID_CRC - if during searching invalid CRC has
+ been read and no devices with
+ spicified family code has been found.
+*******************************************************************************/
+W1Status_t halCommSearchW1(uint8_t cmd,
+ uint8_t family,
+ uint8_t *data,
+ uint8_t count,
+ uint8_t *actCount);
+
+/*******************************************************************************
+ 1-Wire search procedure for one device discovering
+ Parameters:
+ cmd - ROM function command.
+ Returns:
+ W1_SUCCESS_STATUS - if device has been found, ROM number
+ in ROM_NO buffer.
+ W1_NO_DEVICE_STATUS - if device has not been found.
+ W1_INVALID_CRC - if during searching invalid CRC has
+ been read.
+*******************************************************************************/
+W1Status_t halSearchW1(uint8_t cmd);
+
+/*******************************************************************************
+ Calculating 1-Wire 8-bit CRC
+ Parameters:
+ data - data buffer pointer.
+ length - data length.
+ Returns:
+ CRC value based on polynomial x^8 + x^5 + x^4 + 1
+*******************************************************************************/
+uint8_t halW1CRC(uint8_t *data, uint8_t length);
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+// 8-byte buffer that contains the current ROM registration
+// number discovered
+static uint8_t ROM_NO[HAL_ROM_BYTE_LEN];
+// Bit index that identifies from which bit the next search
+// discrepancy check should start
+static uint8_t LastDiscrepancy;
+// Flag to indicate previos search was the last device
+static uint8_t LastDeviceFlag;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/*******************************************************************************
+ 1-Wire search procedure with search ROM command only
+ Parameters:
+ family - 8-bit family code.
+ data - pointer of SRAM where are stored the 8 bytes ROM
+ codes returned by the devices.
+ count - number of devices wish to find.
+ actCount - number of devices have been found.
+ Returns:
+ W1_SUCCESS_STATUS - if at least one device has been found.
+ W1_NO_DEVICE_STATUS - if there are no any devices presented
+ on the bus with specified family code.
+ W1_INVALID_CRC - if during searching invalid CRC has
+ been read and no devices with
+ spicified family code has been found.
+*******************************************************************************/
+W1Status_t HAL_SearchW1Device(uint8_t family,
+ uint8_t *data,
+ uint8_t count,
+ uint8_t *actCount)
+{
+ return halCommSearchW1(HAL_SEARCH_ROM_CMD, family, data, count, actCount);
+}
+
+/*******************************************************************************
+ 1-Wire search procedure with alarm search command only
+ Parameters:
+ family - 8-bit family code.
+ data - pointer of SRAM where are stored the 8 bytes ROM
+ codes returned by the devices.
+ count - number of devices wish to find.
+ actCount - number of devices have been found.
+ Returns:
+ W1_SUCCESS_STATUS - if at least one device has been found.
+ W1_NO_DEVICE_STATUS - if there are no any devices presented
+ on the bus with specified family code.
+ W1_INVALID_CRC - if during searching invalid CRC has
+ been read and no devices with
+ spicified family code has been found.
+*******************************************************************************/
+W1Status_t HAL_AlarmSearchW1Device(uint8_t family,
+ uint8_t *data,
+ uint8_t count,
+ uint8_t *actCount)
+{
+ return halCommSearchW1(HAL_SEARCH_ALRM_CMD, family, data, count, actCount);
+}
+
+/*******************************************************************************
+ 1-Wire search procedure for all devices discovering
+ Parameters:
+ cmd - ROM function command.
+ family - 8-bit family code.
+ data - pointer of SRAM where are stored the 8 bytes ROM
+ codes returned by the devices.
+ count - number of devices wish to find.
+ actCount - number of devices have been found.
+ Returns:
+ W1_SUCCESS_STATUS - if at least one device has been found.
+ W1_NO_DEVICE_STATUS - if there are no any devices presented
+ on the bus with specified family code.
+ W1_INVALID_CRC - if during searching invalid CRC has
+ been read and no devices with
+ spicified family code has been found.
+*******************************************************************************/
+W1Status_t halCommSearchW1(uint8_t cmd,
+ uint8_t family,
+ uint8_t *data,
+ uint8_t count,
+ uint8_t *actCount)
+{
+ W1Status_t w1_result;
+ uint8_t i;
+
+ // Reset the searh state
+ LastDiscrepancy = 0;
+ LastDeviceFlag = 0;
+ // Setup family code
+ if (W1_ANY_FAMILY != family)
+ {
+ ROM_NO[0] = family;
+ LastDiscrepancy = HAL_ROM_BIT_LEN + 1;
+ for (i = 1; i < HAL_ROM_BYTE_LEN; i++) ROM_NO[i] = 0;
+ }
+ i = 0;
+ // Discovering
+ do
+ {
+ w1_result = halSearchW1(cmd);
+ if (W1_SUCCESS_STATUS != w1_result)
+ break;
+ // There is some device with specified family code
+ if ((ROM_NO[0] == family) || (W1_ANY_FAMILY == family))
+ {
+ *(uint64_t *)(data + 8 * i) = *(uint64_t *)ROM_NO;
+ i++;
+ }
+ else // There are not any devices with specified family code
+ {
+ w1_result = W1_NO_DEVICE_STATUS;
+ break;
+ }
+ }
+ while (!LastDeviceFlag && (i < count));
+
+ if (NULL != actCount)
+ *actCount = i;
+
+ if (i != 0)
+ return W1_SUCCESS_STATUS;
+ else
+ return w1_result;
+}
+
+/*******************************************************************************
+ 1-Wire search procedure for one device discovering
+ Parameters:
+ cmd - ROM function command.
+ Returns:
+ W1_SUCCESS_STATUS - if device has been found, ROM number
+ in ROM_NO buffer.
+ W1_NO_DEVICE_STATUS - if device has not been found.
+ W1_INVALID_CRC - if during searching invalid CRC has
+ been read.
+*******************************************************************************/
+W1Status_t halSearchW1(uint8_t cmd)
+{
+ uint8_t id_bit_number;
+ uint8_t last_zero, rom_byte_number;
+ uint8_t id_bit, cmp_id_bit;
+ uint8_t rom_byte_mask, search_direction;
+
+ // initialize for search
+ id_bit_number = 1;
+ last_zero = 0;
+ rom_byte_number = 0;
+ rom_byte_mask = 1;
+
+ // 1-Wire reset
+ ATOMIC_SECTION_ENTER
+ id_bit = halResetW1();
+ ATOMIC_SECTION_LEAVE
+ if (W1_NO_DEVICE_STATUS == id_bit)
+ {
+ LastDiscrepancy = 0;
+ LastDeviceFlag = 0;
+ return W1_NO_DEVICE_STATUS;
+ }
+
+ // issue the search command
+ ATOMIC_SECTION_ENTER
+ halWriteW1(cmd);
+ ATOMIC_SECTION_LEAVE
+ // search 64-bit uniqued registration number
+ do
+ {
+ // read a bit and its complement
+ ATOMIC_SECTION_ENTER
+ id_bit = halReadW1Bit();
+ cmp_id_bit = halReadW1Bit();
+ ATOMIC_SECTION_LEAVE
+ // check for no devices on 1-wire
+ if ((1 == id_bit) && (1 == cmp_id_bit))
+ return W1_NO_DEVICE_STATUS;
+
+ // all devices coupled have 0 or 1
+ if (id_bit != cmp_id_bit)
+ {
+ search_direction = id_bit;
+ // there is descepancy
+ }
+ else
+ {
+ // if this discrepancy if before the Last Discrepancy
+ // on a previous next then pick the same as last time (old branch)
+ if (id_bit_number < LastDiscrepancy)
+ search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
+ // if equal to last pick 1, if not then pick 0 (new branch)
+ else
+ search_direction = (id_bit_number == LastDiscrepancy);
+ // if 0 was picked then record its position in LastZero
+ if (0 == search_direction)
+ last_zero = id_bit_number;
+ }
+
+ if (1 == search_direction)
+ ROM_NO[rom_byte_number] |= rom_byte_mask;
+ else
+ ROM_NO[rom_byte_number] &= ~rom_byte_mask;
+ ATOMIC_SECTION_ENTER
+ halWriteW1bit(search_direction);
+ ATOMIC_SECTION_LEAVE
+ id_bit_number++;
+ rom_byte_mask <<= 1;
+
+ if (rom_byte_mask == 0)
+ {
+ rom_byte_number++;
+ rom_byte_mask = 1;
+ }
+ } while (rom_byte_number < HAL_ROM_BYTE_LEN);
+
+ // Invalid CRC
+ if (halW1CRC(ROM_NO, HAL_ROM_BYTE_LEN))
+ return W1_INVALID_CRC;
+
+ LastDiscrepancy = last_zero;
+ // check for last device
+ if (0 == LastDiscrepancy)
+ LastDeviceFlag = 1;
+
+ return W1_SUCCESS_STATUS;
+}
+
+/*******************************************************************************
+ Calculating 1-Wire 8-bit CRC
+ Parameters:
+ data - data buffer pointer.
+ length - data length.
+ Returns:
+ CRC value based on polynomial x^8 + x^5 + x^4 + 1
+*******************************************************************************/
+uint8_t halW1CRC(uint8_t *data, uint8_t length)
+{
+ uint8_t reg;
+ uint8_t b;
+
+ for (reg = 0; length > 0; length--, data++)
+ {
+ for (b = 0; b < 8; b++)
+ {
+ if (((*data >> b) & 1) ^ (reg & 1))
+ {
+ reg >>= 1;
+ reg ^= HAL_W1_CRC_POLINOM;
+ }
+ else
+ reg >>= 1;
+ }
+ }
+ return reg;
+}
+
+/*******************************************************************************
+Resets all devices connected to the bus.
+Parameters:
+ none.
+Returns:
+ 0 - there are some devices at the bus.
+ 1 - there are not any devices at the bus.
+*******************************************************************************/
+uint8_t HAL_ResetW1(void)
+{
+ return halResetW1();
+};
+
+/*******************************************************************************
+Writes byte to the bus
+Parameters:
+ value - byte to write.
+Returns:
+ none.
+*******************************************************************************/
+void HAL_WriteW1(uint8_t value)
+{
+ halWriteW1(value);
+}
+
+/*******************************************************************************
+Reads byte from the bus.
+Parameters:
+ none.
+Returns:
+ byte read from the bus.
+*******************************************************************************/
+uint8_t HAL_ReadW1(void)
+{
+ return halReadW1();
+}
+
+// eof halW1.c