You are on page 1of 7

/

**************************************************************************//**
Name: Ramnarayan Krishnamurthy
Date: 1/27/2016
ECEN 5023: Assignment 1
Write and debug a Low Energy program with the following requirements:
1) LED blinks with the period of 2 seconds
2) LED is on for 2.5% of the time or 2.5% duty cycle

Reference: main_letimer_pwm_pulse.c from

the Application Note AN0026

*****************************************************************************/

#include "mbed.h"
#include "em_chip.h"
#include "em_emu.h"
#include "em_pcnt.h"
#include "em_letimer.h"

/*Time Period and LED on Period input for the compare registers */
#define DESIREDPERIOD 2000
/*For a duty cycle of 2.5% = 2.5/100 * 2000 = 50 */
#define LEDONDUTYCYCLE 50

void BSP_TraceSwoSetup(void)
{
/* Enable GPIO clock */
CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;

/* Enable Serial wire output pin */


GPIO->ROUTE |= GPIO_ROUTE_SWOPEN;

/* Set correct location */


/* This location is valid for GG, LG and WG! */
GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) |
GPIO_ROUTE_SWLOCATION_LOC0;

/* Enable output on correct pin. */


/* This pin is valid for GG, LG and WG! */
GPIO->P[5].MODEL &= ~(_GPIO_P_MODEL_MODE2_MASK);
GPIO->P[5].MODEL |= GPIO_P_MODEL_MODE2_PUSHPULL;

/* Enable debug clock AUXHFRCO */


CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN;

/* Wait until clock is ready */


while (!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY)) ;

/* Enable trace in core debug */


CoreDebug->DHCSR |= CoreDebug_DHCSR_C_DEBUGEN_Msk;
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;

/* Enable PC and IRQ sampling output */


DWT->CTRL = 0x400113FF;

/* Set TPIU prescaler to 16. */


TPI->ACPR = 15;

/* Set protocol to NRZ */


TPI->SPPR = 2;

/* Disable continuous formatting */


TPI->FFCR = 0x100;

/* Unlock ITM and output data */


ITM->LAR = 0xC5ACCE55;
ITM->TCR = 0x10009;

/* ITM Channel 0 is used for UART output */


ITM->TER |= (1UL << 0);
}

/* Defining LED outputs */

DigitalOut myLed0(LED0);
DigitalOut myLed1(LED1);

Serial pc(USBTX, USBRX);

/*Interrupt Service Routine for LETIMER */

void LETIMER0_IRQHandler(void)
{
/* Clear LETIMER0 comp0 interrupt flag */

LETIMER_IntClear(LETIMER0, LETIMER_IF_COMP0 | LETIMER_IF_COMP1);

/*Toggle the LED0*/

myLed0=!myLed0;

//Wake up the chip from sleep


}

int main(void)
{
/* Chip errata */

CHIP_Init();

/* The intial condition can be switched to 0 for a duty cycle of 100present cycle*/
myLed0=1;

/* Select LFRCO as clock source for LFA */

CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_ULFRCO);

/* Enabling all necessary clocks */

CMU_ClockEnable(cmuClock_CORELE, true); /* Enable CORELE clock */


CMU_ClockEnable(cmuClock_GPIO, true); /* Enable clock for GPIO module */
CMU_ClockEnable(cmuClock_LETIMER0, true);

LETIMER_IntEnable(LETIMER0, LETIMER_IF_COMP0|LETIMER_IF_COMP1);

/* Enable TIMER0 interrupt vector in NVIC */

NVIC_EnableIRQ(LETIMER0_IRQn);

/* enable GPIO C pin 4 as output */

GPIO_PinModeSet(gpioPortC, 4, gpioModePushPull, 0);

/*Route LETIMER to location 3 and enable outputs */

LETIMER0->ROUTE = LETIMER_ROUTE_LOCATION_LOC3 | LETIMER_ROUTE_OUT0PEN;

/*COMP0 contains the time period and


COMP1 contains the active (Active LED on ) period */

LETIMER_CompareSet(LETIMER0, 0, DESIREDPERIOD);
LETIMER_CompareSet(LETIMER0, 1, LEDONDUTYCYCLE);

/* Repetition values must be nonzero so that the outputs


return switch between idle and active state */

LETIMER_RepeatSet(LETIMER0, 0, 0x01);
LETIMER_RepeatSet(LETIMER0, 0, 0x01);

/* Set configurations for LETIMER0 */

const LETIMER_Init_TypeDef letimerInit =


{
.enable = true, /* Start counting when init completed*/
.debugRun = false, /* Counter shall not keep running during debug
halt. */
.rtcComp0Enable = false, /* Start counting on RTC COMP0 match. */
.rtcComp1Enable = false, /* Don't start counting on RTC COMP1
match. */
.comp0Top = true, /* Load COMP0 register into CNT when counter
underflows. COMP is used as TOP */
.bufTop = false, /* Don't load COMP1 into COMP0 when REP0 reaches
0. */
.out0Pol = 0, /* Idle value for output 0. */
.out1Pol = 0, /* Idle value for output 1. */
.ufoa0 = letimerUFOAPwm, /* PwM output on output 0 */
.ufoa1 = letimerUFOANone, /* No output on output 1*/
.repMode = letimerRepeatFree /* Count while REP != 0 */
};

/* Initialize LETIMER0 */

LETIMER_Init(LETIMER0, &letimerInit);

/*Debug printf statement*/


pc.printf("Initialization Successful\n\r");

/* Infinite blink loop */

while (1)

{
/*Go to EM3 and wakes up when interrupted*/

EMU_EnterEM3(false);

}
}

You might also like