added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
50:a417edff4437
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:9b334a45a8ff 1 /***************************************************************************//**
bogdanm 0:9b334a45a8ff 2 * @file lp_ticker.c
bogdanm 0:9b334a45a8ff 3 *******************************************************************************
bogdanm 0:9b334a45a8ff 4 * @section License
bogdanm 0:9b334a45a8ff 5 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
bogdanm 0:9b334a45a8ff 6 *******************************************************************************
bogdanm 0:9b334a45a8ff 7 *
bogdanm 0:9b334a45a8ff 8 * Permission is granted to anyone to use this software for any purpose,
bogdanm 0:9b334a45a8ff 9 * including commercial applications, and to alter it and redistribute it
bogdanm 0:9b334a45a8ff 10 * freely, subject to the following restrictions:
bogdanm 0:9b334a45a8ff 11 *
bogdanm 0:9b334a45a8ff 12 * 1. The origin of this software must not be misrepresented; you must not
bogdanm 0:9b334a45a8ff 13 * claim that you wrote the original software.
bogdanm 0:9b334a45a8ff 14 * 2. Altered source versions must be plainly marked as such, and must not be
bogdanm 0:9b334a45a8ff 15 * misrepresented as being the original software.
bogdanm 0:9b334a45a8ff 16 * 3. This notice may not be removed or altered from any source distribution.
bogdanm 0:9b334a45a8ff 17 *
bogdanm 0:9b334a45a8ff 18 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
bogdanm 0:9b334a45a8ff 19 * obligation to support this Software. Silicon Labs is providing the
bogdanm 0:9b334a45a8ff 20 * Software "AS IS", with no express or implied warranties of any kind,
bogdanm 0:9b334a45a8ff 21 * including, but not limited to, any implied warranties of merchantability
bogdanm 0:9b334a45a8ff 22 * or fitness for any particular purpose or warranties against infringement
bogdanm 0:9b334a45a8ff 23 * of any proprietary rights of a third party.
bogdanm 0:9b334a45a8ff 24 *
bogdanm 0:9b334a45a8ff 25 * Silicon Labs will not be liable for any consequential, incidental, or
bogdanm 0:9b334a45a8ff 26 * special damages, or any other relief, or for any claim by any third party,
bogdanm 0:9b334a45a8ff 27 * arising from your use of this Software.
bogdanm 0:9b334a45a8ff 28 *
bogdanm 0:9b334a45a8ff 29 ******************************************************************************/
bogdanm 0:9b334a45a8ff 30
bogdanm 0:9b334a45a8ff 31 #include "device.h"
bogdanm 0:9b334a45a8ff 32 #if DEVICE_LOWPOWERTIMER
bogdanm 0:9b334a45a8ff 33
bogdanm 0:9b334a45a8ff 34 #include "rtc_api.h"
bogdanm 0:9b334a45a8ff 35 #include "rtc_api_HAL.h"
bogdanm 0:9b334a45a8ff 36 #include "lp_ticker_api.h"
bogdanm 0:9b334a45a8ff 37
bogdanm 0:9b334a45a8ff 38 void lp_ticker_init()
bogdanm 0:9b334a45a8ff 39 {
bogdanm 0:9b334a45a8ff 40 rtc_init_real(RTC_INIT_LPTIMER);
bogdanm 0:9b334a45a8ff 41 rtc_set_comp0_handler((uint32_t)lp_ticker_irq_handler);
bogdanm 0:9b334a45a8ff 42 }
bogdanm 0:9b334a45a8ff 43
bogdanm 0:9b334a45a8ff 44 void lp_ticker_set_interrupt(timestamp_t timestamp)
bogdanm 0:9b334a45a8ff 45 {
bogdanm 0:9b334a45a8ff 46 uint64_t timestamp_ticks;
bogdanm 0:9b334a45a8ff 47 uint64_t current_ticks = RTC_CounterGet();
bogdanm 0:9b334a45a8ff 48 timestamp_t current_time = ((uint64_t)(current_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT));
bogdanm 0:9b334a45a8ff 49
bogdanm 0:9b334a45a8ff 50
bogdanm 0:9b334a45a8ff 51 /* calculate offset value */
bogdanm 0:9b334a45a8ff 52 timestamp_t offset = timestamp - current_time;
bogdanm 0:9b334a45a8ff 53 if(offset > 0xEFFFFFFF) offset = 100;
bogdanm 0:9b334a45a8ff 54
bogdanm 0:9b334a45a8ff 55 /* map offset to RTC value */
bogdanm 0:9b334a45a8ff 56 // ticks = offset * RTC frequency div 1000000
bogdanm 0:9b334a45a8ff 57 timestamp_ticks = ((uint64_t)offset * (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) / 1000000;
bogdanm 0:9b334a45a8ff 58 timestamp_ticks += current_ticks;
bogdanm 0:9b334a45a8ff 59
bogdanm 0:9b334a45a8ff 60 /* RTC has 24 bit resolution */
bogdanm 0:9b334a45a8ff 61 timestamp_ticks &= 0xFFFFFF;
bogdanm 0:9b334a45a8ff 62
bogdanm 0:9b334a45a8ff 63 /* check for RTC limitation */
bogdanm 0:9b334a45a8ff 64 if((timestamp_ticks - RTC_CounterGet()) >= 0x800000) timestamp_ticks = RTC_CounterGet() + 2;
bogdanm 0:9b334a45a8ff 65
bogdanm 0:9b334a45a8ff 66 /* Set callback */
bogdanm 0:9b334a45a8ff 67 RTC_FreezeEnable(true);
bogdanm 0:9b334a45a8ff 68 RTC_CompareSet(0, (uint32_t)timestamp_ticks);
bogdanm 0:9b334a45a8ff 69 RTC_IntEnable(RTC_IF_COMP0);
bogdanm 0:9b334a45a8ff 70 RTC_FreezeEnable(false);
bogdanm 0:9b334a45a8ff 71 }
bogdanm 0:9b334a45a8ff 72
bogdanm 0:9b334a45a8ff 73 inline void lp_ticker_disable_interrupt()
bogdanm 0:9b334a45a8ff 74 {
bogdanm 0:9b334a45a8ff 75 RTC_IntDisable(RTC_IF_COMP0);
bogdanm 0:9b334a45a8ff 76 }
bogdanm 0:9b334a45a8ff 77
bogdanm 0:9b334a45a8ff 78 inline void lp_ticker_clear_interrupt()
bogdanm 0:9b334a45a8ff 79 {
bogdanm 0:9b334a45a8ff 80 RTC_IntClear(RTC_IF_COMP0);
bogdanm 0:9b334a45a8ff 81 }
bogdanm 0:9b334a45a8ff 82
bogdanm 0:9b334a45a8ff 83 timestamp_t lp_ticker_read()
bogdanm 0:9b334a45a8ff 84 {
bogdanm 0:9b334a45a8ff 85 uint64_t ticks_temp;
bogdanm 0:9b334a45a8ff 86 uint64_t ticks = RTC_CounterGet();
bogdanm 0:9b334a45a8ff 87
bogdanm 0:9b334a45a8ff 88 /* ticks = counter tick value
bogdanm 0:9b334a45a8ff 89 * timestamp = value in microseconds
bogdanm 0:9b334a45a8ff 90 * timestamp = ticks * 1.000.000 / RTC frequency
bogdanm 0:9b334a45a8ff 91 */
bogdanm 0:9b334a45a8ff 92
bogdanm 0:9b334a45a8ff 93 ticks_temp = (ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT);
bogdanm 0:9b334a45a8ff 94 return (timestamp_t) (ticks_temp & 0xFFFFFFFF);
bogdanm 0:9b334a45a8ff 95 }
bogdanm 0:9b334a45a8ff 96
bogdanm 0:9b334a45a8ff 97 #endif