From b24866225a6301d3a663f874725e83c012dc25d3 Mon Sep 17 00:00:00 2001 From: Florent Duchon Date: Wed, 26 Dec 2012 17:36:00 +0100 Subject: digital/beacon: add bitcloud stack into common directory digital/zigbit --- .../HAL/avr/atmega1281/common/src/halAppClock.c | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c (limited to 'digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.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 +#include +#include + +/****************************************************************************** + 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 -- cgit v1.2.3