Skip to content

Instantly share code, notes, and snippets.

@toyowata
Last active January 22, 2016 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toyowata/c5ff7472cc27eddf189a to your computer and use it in GitHub Desktop.
Save toyowata/c5ff7472cc27eddf189a to your computer and use it in GitHub Desktop.
LowerPower ticker implementation for LPC1768
/*
* Copyright (c) 2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cmsis.h"
#include "device.h"
#if DEVICE_LOWPOWERTIMER
#include "lp_ticker_api.h"
#include "sleep_api.h"
#include "objects.h"
static uint32_t lp_ticker_inited = 0;
static uint32_t lp_compare_value = 0xFFFFFFFFLu;
static volatile uint32_t overflows = 0;
#define LP_TIMER ((LPC_RIT_TypeDef *)LPC_RIT_BASE)
#define LP_TIMER_IRQn RIT_IRQn
static void lptmr_isr(void);
static void lptmr_isr(void)
{
LP_TIMER->RICOUNTER = 0;
LP_TIMER->RICTRL |= 1;
overflows++;
}
void lp_ticker_init(void)
{
if (lp_ticker_inited) {
return;
}
lp_ticker_inited = 1;
LPC_SC->PCONP |= (1 << 16); // Enable RIT power/clock
LPC_SC->PCLKSEL1 |= (0x3 << 26);
LP_TIMER->RICTRL = 0xC; // set default value
LP_TIMER->RICOMPVAL = lp_compare_value;
LP_TIMER->RIMASK = 0;
LP_TIMER->RICOUNTER = 0;// counter reset
LP_TIMER->RICTRL |= 1; // enable interrupt
NVIC_SetVector(LP_TIMER_IRQn, (uint32_t)lptmr_isr);
NVIC_EnableIRQ(LP_TIMER_IRQn);
}
uint32_t lp_ticker_read()
{
if (!lp_ticker_inited) {
lp_ticker_init();
}
return LP_TIMER->RICOUNTER;
}
uint32_t lp_ticker_get_overflows_counter(void)
{
return overflows;
}
uint32_t lp_ticker_get_compare_match(void)
{
return LP_TIMER->RICOMPVAL;
}
void lp_ticker_set_interrupt(uint32_t now, uint32_t time)
{
now = lp_ticker_read();
(void)now;
LP_TIMER->RICOMPVAL = (time);
}
void lp_ticker_sleep_until(uint32_t now, uint32_t time)
{
lp_ticker_set_interrupt(now, time);
sleep_t sleep_obj;
mbed_enter_sleep(&sleep_obj);
mbed_exit_sleep(&sleep_obj);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment