summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281
diff options
context:
space:
mode:
Diffstat (limited to 'digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281')
-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
68 files changed, 8510 insertions, 0 deletions
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