Commit 27861f4f authored by Projects's avatar Projects

rtc: Sends tick events. Sleep mode has been temporarily reduced to EM1.

parent 44cb96b6
/*
* Copyright (C) 2014 Julian Lewis
* @author Matthieu Cattin <matthieu.cattin@cern.ch>
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief Real time clock routines.
*/
#include "rtc.h"
#include <em_burtc.h>
#include <em_rmu.h>
#include <em_cmu.h>
#define LFXO_FREQUENCY 32768
#define WAKEUP_INTERVAL_MS 1200
#define BURTC_COUNT_BETWEEN_WAKEUP (((LFXO_FREQUENCY * WAKEUP_INTERVAL_MS) / 1000)-1)
struct rtc_time time = {0, 0};
__attribute__((weak))
void BURTC_IRQHandler(void)
{
rtc_tick();
BURTC_IntClear(BURTC_IFC_COMP0);
}
void rtc_init(void)
{
// Enable LFXO and select it for LFA branch
CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
// Enable low energy clocking module clock
CMU_ClockEnable(cmuClock_CORELE, true);
// Enable BURTC registers access
RMU_ResetControl(rmuResetBU, false);
BURTC_Init_TypeDef burtcInit = BURTC_INIT_DEFAULT;
burtcInit.enable = true; // Enable BURTC after initialization
burtcInit.mode = burtcModeEM3; // BURTC is enabled in EM0-EM3
burtcInit.debugRun = false; // Counter shall keep running during debug halt.
burtcInit.clkSel = burtcClkSelLFXO; // Select LFXO as clock source
burtcInit.clkDiv = burtcClkDiv_1; // Clock prescaler
burtcInit.lowPowerComp = 0; // Number of least significantt clock bits to ignore in low power mode
burtcInit.timeStamp = true; // Enable time stamp on entering backup power domain
burtcInit.compare0Top = true; // Clear counter on compare match
burtcInit.lowPowerMode = burtcLPDisable; // Low power operation mode, requires LFXO or LFRCO
BURTC_CompareSet(0, BURTC_COUNT_BETWEEN_WAKEUP); // Set top value for comparator
// Enabling Interrupt from BURTC
NVIC_EnableIRQ(BURTC_IRQn);
BURTC_IntEnable(BURTC_IF_COMP0); // Enable compare interrupt flag
// Initialize BURTC
BURTC_Init(&burtcInit);
}
struct rtc_time rtc_get_time(void)
{
return time;
}
void rtc_set_time(struct rtc_time current)
{
time = current;
}
void rtc_tick(void)
{
++time.epoch;
time.msecs += 200;
if(time.msecs == 1000) {
++time.epoch;
time.msecs = 0;
}
}
/*
* Copyright (C) 2014 Julian Lewis
* @author Matthieu Cattin <matthieu.cattin@cern.ch>
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief Real time clock routines.
*/
#ifndef RTC_H
#define RTC_H
struct rtc_time {
///> Seconds since 01-01-1970 00:00
unsigned int epoch;
///> Milliseconds
unsigned int msecs;
};
/**
* @brief Setup backup RTC
* Using LFRCO clock source and enabling interrupt on COMP0 match
*/
void rtc_init(void);
/**
* @brief Returns the current time.
*/
struct rtc_time rtc_get_time(void);
/**
* @brief Sets the time.
*/
void rtc_set_time(struct rtc_time current);
/**
* @brief Function to be called on every timer tick in the interrupt
* servicer routine.
*/
void rtc_tick(void);
#endif /* RTC_H */
......@@ -32,7 +32,8 @@
*/
enum event_type {
BUTTON_PRESSED,
SENSOR
SENSOR,
RTC_TICK
};
/**
......
......@@ -137,6 +137,7 @@ C_SRC += \
../common/emdrv/sleep/src/sleep.c \
../common/drivers/lcd.c \
../common/drivers/buttons.c \
../common/drivers/rtc.c \
../common/gfx/font_helv11.c \
../common/gfx/font_helv17.c \
../common/gfx/font_helv17b.c \
......
......@@ -89,7 +89,7 @@ extern "C" {
/* Available options when configUSE_TICKLESS_IDLE set to 1
* or configUSE_SLEEP_MODE_IN_IDLE set to 1 :
* 1 - EM1, 2 - EM2, 3 - EM3, timer in EM3 is not very accurate*/
#define configSLEEP_MODE ( 3 )
#define configSLEEP_MODE ( 1 )
/* Definition used only if configUSE_TICKLESS_IDLE == 0 */
#define configUSE_SLEEP_MODE_IN_IDLE ( 1 )
......
......@@ -28,7 +28,10 @@
#include <apps/application.h>
#include <event.h>
#include <drivers/rtc.h>
#include <em_gpio.h>
#include <em_burtc.h>
static portBASE_TYPE gpio_irq_dispatcher(uint32_t flags)
{
......@@ -49,7 +52,7 @@ static portBASE_TYPE gpio_irq_dispatcher(uint32_t flags)
default: return xHigherPriorityTaskWoken;
}
// Post the byte to the back of the queue
// Post the event to the back of the queue
xQueueSendToBackFromISR(appQueue, &evt, &xHigherPriorityTaskWoken);
return xHigherPriorityTaskWoken;
......@@ -81,3 +84,22 @@ void GPIO_ODD_IRQHandler(void)
portEND_SWITCHING_ISR(gpio_irq_dispatcher(iflags));
}
void BURTC_IRQHandler(void)
{
// We have not woken a task at the start of the ISR
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
rtc_tick();
// Fill the event data
struct event evt;
evt.type = RTC_TICK;
// Post the byte to the back of the queue
xQueueSendToBackFromISR(appQueue, &evt, &xHigherPriorityTaskWoken);
BURTC_IntClear(BURTC_IFC_COMP0);
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
}
......@@ -32,6 +32,7 @@
#include <apps/menu.h>
#include <drivers/buttons.h>
#include <drivers/lcd.h>
#include <drivers/rtc.h>
#include <gfx/ui.h>
int main(void)
......@@ -44,6 +45,7 @@ int main(void)
CMU_ClockEnable(cmuClock_GPIO, true);
buttons_init();
rtc_init();
lcd_init();
ui_init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment