summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c
diff options
context:
space:
mode:
authorFlorent Duchon2012-12-26 17:36:00 +0100
committerFlorent Duchon2013-02-13 21:21:12 +0100
commitb24866225a6301d3a663f874725e83c012dc25d3 (patch)
treeca527a2aab9abcdfbaf244c53ca63f0c531892b0 /digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c
parent2ba279f4eb2f23fa08a7c13465d16ae6ba5d0f96 (diff)
digital/beacon: add bitcloud stack into common directory digital/zigbit
Diffstat (limited to 'digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c')
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c124
1 files changed, 124 insertions, 0 deletions
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