Commit da1cce83 authored by Matthieu Cattin's avatar Matthieu Cattin Committed by Projects

sw: add clock display, use buttons to increase/decrease backlight intensity.

parent 3b33d355
*~
\#*\#
.*
\ No newline at end of file
.*
*.o
......@@ -146,11 +146,13 @@ C_SRC += \
../common/emlib/src/em_assert.c \
../common/emlib/src/em_cmu.c \
../common/emlib/src/em_emu.c \
../common/emlib/src/em_rmu.c \
../common/emlib/src/em_gpio.c \
../common/emlib/src/em_i2c.c \
../common/emlib/src/em_int.c \
../common/emlib/src/em_system.c \
../common/emlib/src/em_rtc.c \
../common/emlib/src/em_burtc.c \
../common/emlib/src/em_usart.c \
../common/emlib/src/em_timer.c \
../common/emlib/src/em_prs.c \
......@@ -158,13 +160,20 @@ C_SRC += \
../common/gfx/font_helv11.c \
../common/gfx/font_helv17.c \
../common/gfx/font_helv17b.c \
../common/gfx/font_helv22b.c \
../common/gfx/font_helv29.c \
../common/gfx/font_helv38b.c \
../common/gfx/font_luct38.c \
../common/gfx/font_xm4x5.c \
../common/gfx/font_xm4x6.c \
../common/gfx/font_xm5x8.c \
../common/gfx/font_xm16x25b.c \
../common/udelay.c \
../common/delay.c \
../common/drivers/i2cdrv.c \
../common/drivers/light_sensor.c \
../common/drivers/altimeter.c \
../common/drivers/max17047.c \
main.c
#../common/drivers/display.c \
#../common/drivers/displayls013b7dh03.c \
......
......@@ -35,6 +35,11 @@
#include <drivers/light_sensor.h>
#include <gfx/graphics.h>
#include <drivers/altimeter.h>
#include <drivers/max17047.h>
#include <em_rtc.h>
#include <em_burtc.h>
#include <em_rmu.h>
#include "em_emu.h"
#include "em_prs.h"
......@@ -43,70 +48,84 @@
#include "em_chip.h"
#define PWM_FREQ 10000
#define BZ_PWM_FREQ 10000
#define BZ_TOP 92
#define BL_PWM_FREQ 10000
void TIMER1_IRQHandler(void)
{
uint32_t compareValue;
#define LFXO_FREQUENCY 32768
#define WAKEUP_INTERVAL_MS 1200
#define BURTC_COUNT_BETWEEN_WAKEUP (((LFXO_FREQUENCY * WAKEUP_INTERVAL_MS) / 1000)-1)
/* Clear flag for TIMER1 overflow interrupt */
TIMER_IntClear(TIMER1, TIMER_IF_OF);
compareValue = TIMER_CaptureGet(TIMER1, 1);
/* increment duty-cycle or reset if reached TOP value */
if( compareValue == TIMER_TopGet(TIMER1))
{
TIMER_CompareBufSet(TIMER1, 1, 0);
TIMER_CompareBufSet(TIMER1, 2, 0);
}
else
{
compareValue += 10;
TIMER_CompareBufSet(TIMER1, 1, compareValue);
TIMER_CompareBufSet(TIMER1, 2, compareValue);
}
}
static uint16_t milliseconds = 0;
static uint8_t minutes = 45;
static uint8_t hours = 10;
static bool lcd_update_flag;
/**
* @brief Main function
* @brief BURTC Interrupt Handler clears the flag
*/
int main(void)
void BURTC_IRQHandler(void)
{
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
int x, y, i;
char str[20];
uint8_t err;
double temp = 0;
double pressure = 0;
double altitude = 0;
double lux;
/* Clear interrupt source */
BURTC_IntClear(BURTC_IFC_COMP0);
CHIP_Init();
milliseconds += 1200;
// Enable HFXO and select it for HF branch
CMU_OscillatorEnable(cmuOsc_HFXO, true, true);
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1);
if(milliseconds == 60000)
{
milliseconds = 0;
minutes += 1;
}
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
if(minutes == 60)
{
minutes = 0;
hours += 1;
}
if(hours == 24)
{
hours = 0;
}
CMU_ClockEnable(cmuClock_TIMER1, true);
lcd_update_flag = true;
// Backlight LEDs
GPIO_PinModeSet(gpioPortE, 11, gpioModePushPull, 0);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0);
}
lcd_init();
I2CDRV_Init(&i2cInit);
/**
* @brief Setup BURTC
* Using LFRCO clock source and enabling interrupt on COMP0 match
*/
void setupBurtc(void)
{
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);
}
err = alti_init();
void timer1Init(void)
{
CMU_ClockEnable(cmuClock_TIMER1, true);
// Select CC channel parameters
TIMER_InitCC_TypeDef timerCCInit =
......@@ -125,20 +144,20 @@ int main(void)
};
// Configure CC channel 1
TIMER_InitCC(TIMER1, 1, &timerCCInit);
//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);
//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);
// Set Top Value
TIMER_TopSet(TIMER1, CMU_ClockFreqGet(cmuClock_HFPER)/PWM_FREQ);
TIMER_TopSet(TIMER1, CMU_ClockFreqGet(cmuClock_HFPER)/BL_PWM_FREQ);
// Set compare value starting at 0 - it will be incremented in the interrupt handler
TIMER_CompareBufSet(TIMER1, 1, 0);
//TIMER_CompareBufSet(TIMER1, 1, 0);
TIMER_CompareBufSet(TIMER1, 2, 0);
// Select timer parameters
......@@ -158,55 +177,247 @@ int main(void)
};
// Enable overflow interrupt
TIMER_IntEnable(TIMER1, TIMER_IF_OF);
//TIMER_IntEnable(TIMER1, TIMER_IF_OF);
// Enable TIMER1 interrupt vector in NVIC
NVIC_EnableIRQ(TIMER1_IRQn);
//NVIC_EnableIRQ(TIMER1_IRQn);
// Configure timer
TIMER_Init(TIMER1, &timerInit);
}
void TIMER1_IRQHandler(void)
{
uint32_t compareValue;
/* Clear flag for TIMER1 overflow interrupt */
TIMER_IntClear(TIMER1, TIMER_IF_OF);
compareValue = TIMER_CaptureGet(TIMER1, 1);
/* increment duty-cycle or reset if reached TOP value */
if( compareValue == TIMER_TopGet(TIMER1))
{
//TIMER_CompareBufSet(TIMER1, 1, 0);
TIMER_CompareBufSet(TIMER1, 2, 0);
}
else
{
compareValue += 10;
//TIMER_CompareBufSet(TIMER1, 1, compareValue);
TIMER_CompareBufSet(TIMER1, 2, compareValue);
}
}
void timer2Init(void)
{
/* Enable clock for TIMER2 module */
CMU_ClockEnable(cmuClock_TIMER2, true);
/* Select CC channel parameters */
TIMER_InitCC_TypeDef timerCCInit =
{
.cufoa = timerOutputActionNone,
.cofoa = timerOutputActionToggle,
.cmoa = timerOutputActionNone,
.mode = timerCCModeCompare,
.filter = false,
.prsInput = false,
.coist = false,
.outInvert = false,
};
/* Configure CC channel 1 */
TIMER_InitCC(TIMER2, 1, &timerCCInit);
/* Route CC1 to location 0 (PA9) and enable pin */
TIMER2->ROUTE |= (TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC0);
/* Set Top Value */
TIMER_TopSet(TIMER2, BZ_TOP);
/* Select timer parameter */
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,
};
/* Configure timer */
TIMER_Init(TIMER2, &timerInit);
}
/**
* @brief Main function
*/
int main(void)
{
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
int x, y, i;
char str[20];
uint8_t err;
double temp = 0;
double pressure = 0;
double altitude = 0;
double lux;
uint16_t fg_var16;
uint8_t fg_var8;
int8_t fg_temp;
int16_t fg_i;
uint32_t compareValue;
CHIP_Init();
// Enable LFXO and select it for LFA branch
CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
// Enable HFXO and select it for HF branch
CMU_OscillatorEnable(cmuOsc_HFXO, true, true);
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
// Enable low energy clocking module clock
CMU_ClockEnable(cmuClock_CORELE, true);
// Enable BURTC registers access
RMU_ResetControl(rmuResetBU, false);
// BURTC setup
setupBurtc();
// Setup SysTick Timer for 1 msec interrupts
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
// Configure backlight LED pins as outputs
GPIO_PinModeSet(gpioPortE, 11, gpioModePushPull, 0);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0);
// Configure switches pins as inputs without pull-up/down (-> external pull-up)
GPIO_PinModeSet(gpioPortA, 0, gpioModeInput, 0); // Top right switch
GPIO_PinModeSet(gpioPortA, 8, gpioModeInput, 0); // Bottom right switch
GPIO_PinModeSet(gpioPortC, 7, gpioModeInput, 0); // Top left switch
GPIO_PinModeSet(gpioPortC, 6, gpioModeInput, 0); // Bottom left switch
// Configure vibrating motor enable pin as push/pull output
GPIO_PinModeSet(gpioPortA, 3, gpioModePushPull, 0);
// Configure buzzer pin as push/pull output
GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 0);
timer1Init();
timer2Init();
lcd_init();
I2CDRV_Init(&i2cInit);
err = alti_init();
err = max17047_set_config(0x2250);
//GPIO_PinOutSet(gpioPortE, 11);
//GPIO_PinOutSet(gpioPortE, 12);
//box(0, 0, 128, 128, 1);
while(1)
{
err = light_sensor_get_lux(&lux);
sprintf(str, "light: %3.3f lux", lux);
text(&font_helv11, 5, 10, str);
// Top left switch
if ( !GPIO_PinInGet(gpioPortC, 7) ) {
compareValue = TIMER_CaptureGet(TIMER1, 2);
/* increment duty-cycle if smaller than TOP value */
if( compareValue < TIMER_TopGet(TIMER1))
{
compareValue += 100;
//TIMER_CompareBufSet(TIMER1, 1, compareValue);
TIMER_CompareBufSet(TIMER1, 2, compareValue);
}
Delay(100);
}
// Bottom left switch
if ( !GPIO_PinInGet(gpioPortC, 6) ) {
compareValue = TIMER_CaptureGet(TIMER1, 2);
/* decrement duty-cycle if bigger than 0 */
if( compareValue > 0)
{
compareValue -= 100;
//TIMER_CompareBufSet(TIMER1, 1, compareValue);
TIMER_CompareBufSet(TIMER1, 2, compareValue);
}
Delay(100);
}
err = alti_get_temp_pressure(&temp, &pressure, true);
sprintf(str, "temp: %f C", temp);
text(&font_helv11, 5, 30, str);
sprintf(str, "pressure: %f mbar", pressure);
text(&font_helv11, 5, 40, str);
// Top right switch
if ( GPIO_PinInGet(gpioPortA, 0) ) {
TIMER_Enable(TIMER2, false);
}
else {
TIMER_Enable(TIMER2, true);
}
err = alti_mbar2altitude(pressure, &altitude);
sprintf(str, "altitude: %f m", altitude);
text(&font_helv11, 5, 50, str);
// Bottom right switch
if ( GPIO_PinInGet(gpioPortA, 8) ) {
GPIO_PinOutClear(gpioPortA, 3);
}
else {
GPIO_PinOutSet(gpioPortA, 3);
}
sprintf(str, "clock: %d",CMU_ClockFreqGet(cmuClock_HFPER));
text(&font_helv11, 5, 60, str);
//lcd_update();
for(x=0; x<128; x++)
if(lcd_update_flag)
{
for(y=0; y<128; y++)
{
lcd_toggle_pixel(x, y);
}
}
lcd_update();
Delay(1000);
box(0, 0, 128, 128, 0);
//lcd_clear();
}
lcd_update_flag = false;
err = light_sensor_get_lux(&lux);
err = alti_get_temp_pressure(&temp, &pressure, true);
err = alti_mbar2altitude(pressure, &altitude);
fg_i = max17047_get_current();
fg_var8 = max17047_get_charge();
lcd_clear();
sprintf(str, "%02d:%02d", hours, minutes);
text(&font_helv38b, 5, 20, str);
sprintf(str, "%02d.%1d", milliseconds/1000, (milliseconds%1000)/100);
text(&font_helv22b, 89, 34, str);
sprintf(str, "light: %3.3f lux", lux);
text(&font_helv11, 5, 60, str);
sprintf(str, "temp: %f C", temp);
text(&font_helv11, 5, 70, str);
sprintf(str, "pressure: %f mbar", pressure);
text(&font_helv11, 5, 80, str);
sprintf(str, "altitude: %f m", altitude);
text(&font_helv11, 5, 90, str);
sprintf(str, "fg current: %d mA", fg_i);
text(&font_helv11, 5, 100, str);
sprintf(str, "fg charge: %d", fg_var8);
text(&font_helv11, 5, 110, str);
sprintf(str, "backlight: %d", compareValue * 100 / TIMER_TopGet(TIMER1));
text(&font_helv11, 5, 5, str);
lcd_update();
}
}
}
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