Commit 515e5834 authored by Projects's avatar Projects

backlight: Driver, basic functions.

parent a3d6a733
/*
* Copyright (C) 2014 Julian Lewis
* @author Maciej Suminski <maciej.suminski@cern.ch>
* @author Matthieu Cattin <matthieu.cattin@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 Backlight control.
*/
#include "backlight.h"
#include <em_cmu.h>
#include <em_gpio.h>
#include <em_timer.h>
static int backlight_level;
void backlight_init(void)
{
CMU_ClockEnable(cmuClock_TIMER1, true);
// Configure backlight LED pins as outputs
GPIO_PinModeSet(gpioPortE, 11, gpioModePushPull, 0);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0);
// Select CC channel parameters
TIMER_InitCC_TypeDef timerCCInit =
{
.eventCtrl = timerEventEveryEdge,
.edge = timerEdgeBoth,
.prsSel = timerPRSSELCh1,
.cufoa = timerOutputActionNone,
.cofoa = timerOutputActionNone,
.cmoa = timerOutputActionToggle,
.mode = timerCCModePWM,
.filter = false,
.prsInput = false,
.coist = false,
.outInvert = false,
};
// Configure CC channel 1 & 2
TIMER_InitCC(TIMER1, 1, &timerCCInit);
TIMER_InitCC(TIMER1, 2, &timerCCInit);
// Route CC1 to location 1 (PE11) and enable pin
TIMER1->ROUTE |= (TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC1);
// Route CC2 to location 1 (PE12) and enable pin
TIMER1->ROUTE |= (TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC1);
// Select timer parameters
TIMER_Init_TypeDef timerInit =
{
.enable = true,
.debugRun = true,
.prescale = timerPrescale64,
.clkSel = timerClkSelHFPerClk,
.fallAction = timerInputActionNone,
.riseAction = timerInputActionNone,
.mode = timerModeUp,
.dmaClrAct = false,
.quadModeX4 = false,
.oneShot = false,
.sync = false,
};
// Set Top Value (determines PWM frequency)
// TODO the top value may need adjustments if HFPER changes
// TIMER_TopSet(TIMER1, CMU_ClockFreqGet(cmuClock_HFPER) / 100000));
TIMER_TopSet(TIMER1, 100);
backlight_set_level(0);
// Configure timer
TIMER_Init(TIMER1, &timerInit);
}
void backlight_set_level(int level)
{
const int BL_MAX_LEVEL = 100;
if(level <= 0) {
TIMER_Enable(TIMER1, false);
level = 0; // in case level is negative
} else {
// Reenable the timer, if it was turned off previously
if(backlight_level == 0)
TIMER_Enable(TIMER1, true);
if(level > BL_MAX_LEVEL)
level = BL_MAX_LEVEL;
TIMER_CompareBufSet(TIMER1, 1, level);
TIMER_CompareBufSet(TIMER1, 2, level);
}
backlight_level = level;
}
unsigned int backlight_get_level(void)
{
return backlight_level;
}
/*
* Copyright (C) 2014 Julian Lewis
* @author Maciej Suminski <maciej.suminski@cern.ch>
* @author Matthieu Cattin <matthieu.cattin@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 Backlight control.
*/
#ifndef BACKLIGHT_H
#define BACKLIGHT_H
/**
* @brief Initialize backlight control.
*/
void backlight_init(void);
/**
* @brief Sets the backlight brightness.
* @param level is the brightness level to be set. Valid values are in the
* 0-100 range. 0 stands for completely turned off, 100 is full brightness.
*/
void backlight_set_level(int level);
/**
* @brief Returns the current level of backlight.
*/
unsigned int backlight_get_level(void);
#endif /* BACKLIGHT_H */
......@@ -139,6 +139,7 @@ C_SRC += \
../common/emlib/src/em_usart.c \
../common/emdrv/sleep/src/sleep.c \
../common/drivers/lcd.c \
../common/drivers/backlight.c \
../common/drivers/buttons.c \
../common/drivers/buzzer.c \
../common/drivers/rtc.c \
......
......@@ -26,6 +26,7 @@
#include "application.h"
#include "widgets/status_bar.h"
#include "drivers/backlight.h"
#include "drivers/buzzer.h"
#include "drivers/vibra.h"
......@@ -47,16 +48,18 @@ static void widget_event(struct ui_widget *w, const struct event *evt)
switch(evt->type) {
case BUTTON_PRESSED:
switch(evt->data.button) {
case BUT_TR:
strcpy(message, "top right");
case BUT_TR: // top right button
backlight_set_level(backlight_get_level() + 10);
sprintf(message, "top right (bl: %d)", backlight_get_level());
break;
case BUT_BR:
strcpy(message, "bottom right");
case BUT_BR: // bottom right button
backlight_set_level(backlight_get_level() - 10);
sprintf(message, "bot right (bl: %d)", backlight_get_level());
break;
case BUT_BL:
strcpy(message, "bottom left");
case BUT_BL: // bottom left button
strcpy(message, "bot left");
break;
case BUT_TL: // this should not happen, it is handled
......
......@@ -30,6 +30,7 @@
#include <sleep.h>
#include <apps/app_list.h>
#include <drivers/backlight.h>
#include <drivers/buttons.h>
#include <drivers/buzzer.h>
#include <drivers/lcd.h>
......@@ -46,6 +47,7 @@ int main(void)
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
backlight_init();
buttons_init();
buzzer_init();
vibra_init();
......@@ -53,9 +55,6 @@ int main(void)
lcd_init();
ui_init();
GPIO_PinModeSet(gpioPortE, 11, gpioModePushPull, 0);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0);
// Initialize SLEEP driver, no callbacks are used
SLEEP_Init(NULL, NULL);
#if (configSLEEP_MODE < 3)
......
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