Ejemplo n.º 1
0
int timer_init(tim_t dev, unsigned long freq, timer_cb_t callback, void *arg)
{
    TIMER_TypeDef *pre, *tim;

    /* test if given timer device is valid */
    if (dev >= TIMER_NUMOF) {
        return -1;
    }

    /* save callback */
    isr_ctx[dev].cb = callback;

    /* get timers */
    pre = timer_config[dev].prescaler.dev;
    tim = timer_config[dev].timer.dev;

    /* enable clocks */
    CMU_ClockEnable(cmuClock_HFPER, true);
    CMU_ClockEnable(timer_config[dev].prescaler.cmu, true);
    CMU_ClockEnable(timer_config[dev].timer.cmu, true);

    /* reset and initialize peripherals */
    EFM32_CREATE_INIT(init_pre, TIMER_Init_TypeDef, TIMER_INIT_DEFAULT,
        .conf.enable = false,
        .conf.prescale = timerPrescale1
    );
    EFM32_CREATE_INIT(init_tim, TIMER_Init_TypeDef, TIMER_INIT_DEFAULT,
        .conf.enable = false,
        .conf.clkSel = timerClkSelCascade
    );

    TIMER_Reset(tim);
    TIMER_Reset(pre);

    TIMER_Init(tim, &init_tim.conf);
    TIMER_Init(pre, &init_pre.conf);

    /* configure the prescaler top value */
    uint32_t freq_timer = CMU_ClockFreqGet(timer_config[dev].prescaler.cmu);
    uint32_t top = (
        freq_timer / TIMER_Prescaler2Div(init_pre.conf.prescale) / freq) - 1;

    TIMER_TopSet(pre, top);
    TIMER_TopSet(tim, 0xffff);

    /* enable interrupts for the channels */
    TIMER_IntClear(tim, TIMER_IFC_CC0 | TIMER_IFC_CC1 | TIMER_IFC_CC2);
    TIMER_IntEnable(tim, TIMER_IEN_CC0 | TIMER_IEN_CC1 | TIMER_IEN_CC2);

    NVIC_ClearPendingIRQ(timer_config[dev].irq);
    NVIC_EnableIRQ(timer_config[dev].irq);

    /* start the timers */
    TIMER_Enable(tim, true);
    TIMER_Enable(pre, true);

    return 0;
}
Ejemplo n.º 2
0
/**
 *    \brief    Initializes SysTick.
 */
static void _hal_tcInit(void)
{
    uint32_t l_ticks;
    TIMER_TypeDef* ps_timer = TIMER1;
    TIMER_Init_TypeDef s_timerInit = TIMER_INIT_DEFAULT;

    s_timerInit.enable = true;
    s_timerInit.prescale = timerPrescale2;
    s_timerInit.riseAction = timerInputActionReloadStart;

    /* caluclate ticks */
    l_ticks = SystemHFClockGet() / 2 / 1000;

    /* configure timer for 1ms */
    TIMER_TopSet( ps_timer, l_ticks );
    /* enable timer interrupts */
    NVIC_DisableIRQ( TIMER1_IRQn );
    NVIC_ClearPendingIRQ( TIMER1_IRQn );
    NVIC_EnableIRQ( TIMER1_IRQn );
    TIMER_IntEnable( ps_timer, TIMER_IEN_OF );

    /* initialize and start timer */
    TIMER_Init( ps_timer, &s_timerInit );

} /* _hal_tcInit() */
Ejemplo n.º 3
0
void timerSetup(void){
	  /* Enable clock for TIMER0 module */
	  CMU_ClockEnable(cmuClock_TIMER0, true);

	  /* Select TIMER0 parameters */
	  TIMER_Init_TypeDef timerInit;

	  timerInit.enable = true;
	  timerInit.debugRun = true;
	  timerInit.prescale = timerPrescale1024;
	  timerInit.clkSel = timerClkSelHFPerClk;
	  timerInit.fallAction = timerInputActionNone;
	  timerInit.riseAction = timerInputActionNone;
	  timerInit.mode       = timerModeUp;
	  timerInit.dmaClrAct  = false;
	  timerInit.quadModeX4 = false;
	  timerInit.oneShot    = false;
	  timerInit.sync       = false;




	    /* Enable TIMER0 interrupt vector in NVIC */
	    NVIC_EnableIRQ(TIMER0_IRQn);

	    /* Set TIMER Top value */
	    TIMER_TopSet(TIMER0, TOP);

	    /* Configure TIMER */
	     TIMER_Init(TIMER0, &timerInit);


}
Ejemplo n.º 4
0
/***************************************************************************//**
 * @brief
 *   Activate the hardware timer used to pace the 1 millisecond timer system.
 *
 * @details
 *   Call this function whenever the HFPERCLK frequency is changed.
 *   This function is initially called by HOST and DEVICE stack xxxx_Init()
 *   functions.
 ******************************************************************************/
void USBTIMER_Init( void )
{
  uint32_t freq;
  TIMER_Init_TypeDef timerInit     = TIMER_INIT_DEFAULT;
  TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;

  freq = CMU_ClockFreqGet( cmuClock_HFPER );
  ticksPrMs = ( freq + 500 ) / 1000;
  ticksPr1us = ( freq + 500000 ) / 1000000;
  ticksPr10us = ( freq + 50000 ) / 100000;
  ticksPr100us = ( freq + 5000 ) / 10000;

  timerCCInit.mode = timerCCModeCompare;
  CMU_ClockEnable( TIMER_CLK, true );
  TIMER_TopSet( TIMER, 0xFFFF );
  TIMER_InitCC( TIMER, 0, &timerCCInit );
  TIMER_Init( TIMER, &timerInit );

#if ( NUM_QTIMERS > 0 )
  TIMER_IntClear( TIMER, 0xFFFFFFFF );
  TIMER_IntEnable( TIMER, TIMER_IEN_CC0 );
  TIMER_CompareSet( TIMER, 0, TIMER_CounterGet( TIMER ) + ticksPrMs );
  NVIC_ClearPendingIRQ( TIMER_IRQ );
  NVIC_EnableIRQ( TIMER_IRQ );
#endif /* ( NUM_QTIMERS > 0 ) */
}
Ejemplo n.º 5
0
void initTimer()
{
  TIMER_Init_TypeDef initValues = TIMER_INIT_DEFAULT;

  /* Enable clock for TIMER0 */
  CMU_ClockEnable(cmuClock_TIMER0, true);
  CMU_ClockEnable(cmuClock_I2C0, true);

  /* Enable underflow and overflow interrupt for TIMER0*/
  TIMER_IntEnable(TIMER0, TIMER_IF_OF);

  /* Enable TIMER0 interrupt vector in NVIC */
  NVIC_EnableIRQ(TIMER0_IRQn);

  /* Set TIMER0 Top value */
  //TIMER_TopSet(TIMER0, 27342);
  TIMER_TopSet(TIMER0, 7000);

  /* Initialize TIMER0 in Up mode with 1024x prescaling */
  initValues.prescale = timerPrescale1024;
  initValues.mode     = timerModeUp;
  TIMER_Init(TIMER0, &initValues);

  /* Start TIMER0 */
  TIMER0->CMD = TIMER_CMD_START;
}
Ejemplo n.º 6
0
void PWM_SetPeriod(uint32_t period, uint32_t clockFreq)
{
	uint32_t TopValue = clockFreq / period;
	TIMER_TopSet(TIMER0, TopValue);
	g_PWM_MainClockFrequency = clockFreq;
	g_PWM_Period = period;
}
Ejemplo n.º 7
0
int main(void) 
{
	
	CHIP_Init();

	if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1) ;

  BSP_Init(BSP_INIT_DEFAULT);
	BSP_LedsSet(0);

  BSP_PeripheralAccess(BSP_AUDIO_IN, true);
  BSP_PeripheralAccess(BSP_AUDIO_OUT, true);

  RTCDRV_Trigger(1000, NULL);
  EMU_EnterEM2(true);

  initSource();

	setupCMU();
  setupDMA();
  
  //setupADC();
  //setupDAC();

  //setupDMAInput();
  //setupDMAOutput();

	//setupDMASplit();
	//setupDMAMerge();

  ADCConfig();
  DACConfig();

  TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
  TIMER_TopSet(TIMER0, CMU_ClockFreqGet(cmuClock_HFPER) / SAMPLE_RATE);
  TIMER_Init(TIMER0, &timerInit);

	Delay(100);
	BSP_LedsSet(3);
	Delay(500);
	BSP_LedsSet(0);
	Delay(100);

	while(1) {
		volatile bool result = test();
		if (result) {
			BSP_LedsSet(0x00FF);
		} else {
			BSP_LedsSet(0xFF00);			
		}
		Delay(1000);
    BSP_LedsSet(0x0);    
    Delay(1000);
	}

}
Ejemplo n.º 8
0
void setupTIMER( void )
{
	TIMER_Init_TypeDef init = TIMER_INIT_DEFAULT;

	init.mode = timerModeUpDown;

	TIMER_TopSet( TIMER0, CMU_ClockFreqGet(cmuClock_HFPER) / 22050 );
	TIMER_Init( TIMER0, &init );

}
Ejemplo n.º 9
0
//================================================================================
// TIMER1_enter_DefaultMode_from_RESET
//================================================================================
extern void TIMER1_enter_DefaultMode_from_RESET(void) {

	// $[TIMER1 initialization]
	TIMER_Init_TypeDef init = TIMER_INIT_DEFAULT;

	init.enable = 0;//初始化完成后不使能
	init.debugRun = 0;
	init.dmaClrAct = 0;
	init.sync = 0;
	init.clkSel = timerClkSelHFPerClk;//HFPERCLK时钟
	init.prescale = timerPrescale2;   //2分频
	init.fallAction = timerInputActionNone;
	init.riseAction = timerInputActionNone;
	init.mode = timerModeUp;
	init.quadModeX4 = 0;
	init.oneShot = 0;

	// [TIMER1 initialization]$

	// $[TIMER1 CC0 init]
	TIMER_InitCC_TypeDef initCC0 = TIMER_INITCC_DEFAULT;

	initCC0.prsInput = false;
	initCC0.edge = timerEdgeBoth;
	initCC0.mode = timerCCModePWM;
	initCC0.eventCtrl = timerEventEveryEdge;
	initCC0.filter = 0;
	initCC0.cofoa = timerOutputActionNone;
	initCC0.cufoa = timerOutputActionNone;
	initCC0.cmoa = timerOutputActionToggle;
	initCC0.coist = 0;
	initCC0.outInvert = 0;
	TIMER_InitCC(TIMER1, 0, &initCC0);
	// [TIMER1 CC0 init]$

	/* Route CC0 to location 0 (PC11) and enable pin */
	TIMER1->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_LOCATION_LOC0);

	/* Set Top Value */
	TIMER_TopSet(TIMER0, 8000000/PWM_FREQ); //PWM频率设定,此处8M是HFPERCLK频率,待定

	/* Set compare value starting at 0 - it will be incremented in the interrupt handler */
	TIMER_CompareBufSet(TIMER0, 0, 0);

	/* Enable overflow interrupt */
	TIMER_IntEnable(TIMER0, TIMER_IF_OF);

	/* Enable TIMER0 interrupt vector in NVIC */
	NVIC_EnableIRQ(TIMER0_IRQn);

	TIMER_Init(TIMER1, &init);
}
Ejemplo n.º 10
0
void fd_led_wake(void) {
    fd_lp55231_power_on();
    fd_lp55231_wake();

    CMU_ClockEnable(cmuClock_TIMER0, true);

    TIMER_InitCC_TypeDef timer_initcc = TIMER_INITCC_DEFAULT;
    timer_initcc.cmoa = timerOutputActionToggle;
    timer_initcc.mode = timerCCModePWM;
    TIMER_InitCC(TIMER0, /* channel */ 1, &timer_initcc);
    TIMER_InitCC(TIMER0, /* channel */ 2, &timer_initcc);

    TIMER0->ROUTE = TIMER_ROUTE_CC1PEN | TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC4;

    TIMER_TopSet(TIMER0, TOP);
    TIMER_CompareSet(TIMER0, /* channel */ 1, TOP);
    TIMER_CompareSet(TIMER0, /* channel */ 2, TOP);

    TIMER_Init_TypeDef timer_init = TIMER_INIT_DEFAULT;
    TIMER_Init(TIMER0, &timer_init);

    CMU_ClockEnable(cmuClock_TIMER3, true);

    TIMER_InitCC(TIMER3, /* channel */ 1, &timer_initcc);
    TIMER_InitCC(TIMER3, /* channel */ 2, &timer_initcc);

    TIMER3->ROUTE = TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC0;

    TIMER_TopSet(TIMER3, TOP);
    TIMER_CompareSet(TIMER3, /* channel */ 1, TOP);
    TIMER_CompareSet(TIMER3, /* channel */ 2, TOP);

    TIMER_IntEnable(TIMER3, TIMER_IF_CC1 | TIMER_IF_OF);

    NVIC_EnableIRQ(TIMER3_IRQn);

    TIMER_Init(TIMER3, &timer_init);
}
Ejemplo n.º 11
0
/***************************************************************************//**
* @brief
*   Configure Timer device
*
* @details
*
* @note
*
* @param[in] dev
*   Pointer to device descriptor
*
* @param[in] cmd
*   Timer control command
*
* @param[in] args
*   Arguments
*
* @return
*   Error code
******************************************************************************/
static rt_err_t rt_hs_timer_control (
    rt_device_t     dev,
    rt_uint8_t      cmd,
    void            *args)
{
    RT_ASSERT(dev != RT_NULL);

    struct efm32_timer_device_t *timer;

    timer = (struct efm32_timer_device_t *)(dev->user_data);

    switch (cmd)
    {
    case RT_DEVICE_CTRL_SUSPEND:
        /* Suspend device */
        dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
        TIMER_Enable(timer->timer_device, false);
        break;

    case RT_DEVICE_CTRL_RESUME:
        /* Resume device */
        dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
        TIMER_Enable(timer->timer_device, true);
        break;

    case RT_DEVICE_CTRL_TIMER_PERIOD:
        {
            /* change device setting */
            struct efm32_timer_control_t *control;
            rt_uint32_t running;

            control = (struct efm32_timer_control_t *)args;
            running = timer->timer_device->STATUS & 0x00000001;

            TIMER_Enable(timer->timer_device, false);
            timer->timer_device->CNT = _TIMER_CNT_RESETVALUE;
            TIMER_TopSet(timer->timer_device, TIMER_TopCalculate(control->period));
            timer->hook.cbFunc = control->hook.cbFunc;
            timer->hook.userPtr = control->hook.userPtr;
            if (running)
            {
                TIMER_Enable(timer->timer_device, true);
            }
        }
        break;
    }

    return RT_EOK;
}
Ejemplo n.º 12
0
// Private function prototypes -----------------------------------------------
// Private functions ---------------------------------------------------------
// Exported functions --------------------------------------------------------
uint8_t bsp_tick_timer_init(void)
{
    uint8_t ret = EXIT_SUCCESS;
    uint32_t timer_freq;
    uint32_t timer_top_count;

    TIMER_Reset(TIMER0);

    // Configure a system timer for tracking a tick counter.
    CMU_ClockEnable(cmuClock_TIMER0, true);

    // Set the timer configuration
    s_timer_init.enable     = false;                // Enable timer when init complete.
    s_timer_init.debugRun   = true;                 // Stop counter during debug halt.

    s_timer_init.prescale   = timer_prescale;       // Prescale by 16 (2MHz clock)
    s_timer_init.clkSel     = timerClkSelHFPerClk;  // Select HFPER clock.

    s_timer_init.count2x    = false;                // Not 2x count mode.
    s_timer_init.ati        = false;                // No ATI.

    s_timer_init.fallAction = timerInputActionNone; // No action on falling input edge.
    s_timer_init.riseAction = timerInputActionNone; // No action on rising input edge.

    s_timer_init.mode       = timerModeUp;          // Up-counting.
    s_timer_init.dmaClrAct  = false;                // Do not clear DMA requests when DMA channel is active.
    s_timer_init.quadModeX4 = false;                // Select X2 quadrature decode mode (if used).

    s_timer_init.oneShot    = false;                // Disable one shot.
    s_timer_init.sync       = false;                // Not started/stopped/reloaded by other timers.

    // Configure TIMER
    TIMER_Init(TIMER0, &s_timer_init);

    // Set counter top for tick rate
    timer_freq = CMU_ClockFreqGet(cmuClock_TIMER0) / (1 << timer_prescale);
    timer_top_count = (timer_freq / TIMER_TICK_RATE_HZ) - 1;
    TIMER_TopSet(TIMER0, timer_top_count);

    return ret;
}
Ejemplo n.º 13
0
static void init_analog_switches(void)
{
  CMU_ClockEnable(cmuClock_GPIO, true);
  CMU_ClockEnable(cmuClock_TIMER1, true);

  port_init(analog_switches, sizeof(analog_switches)/sizeof(port_init_t));

  TIMER_Init(T2_TIMER, & t2_timer_init);
  TIMER_InitCC(T2_TIMER, T2_TIMER_CC, & t2_timer_cc_init);
  T2_TIMER->ROUTE = TIMER_ROUTE_CC1PEN | (T2_TIMER_LOC << _TIMER_ROUTE_LOCATION_SHIFT);
  T2_TIMER->CTRL |= TIMER_CTRL_RSSCOIST;

  uint16_t t1_to_t2_delay = ROUND_F_TO_I(T1_TO_T2_DELAY * CMU_ClockFreqGet(cmuClock_TIMER1));
  uint16_t timer_max_count = ROUND_F_TO_I((T1_TO_T2_DELAY + T2_PULSE_TIME) * CMU_ClockFreqGet(cmuClock_TIMER1));
#if 0
  printf("t1_to_t2_delay:  %" PRIu16 "\r\n", t1_to_t2_delay);
  printf("timer_max_count: %" PRIu16 "\r\n", timer_max_count);
#endif
  TIMER_CompareSet(T2_TIMER, T2_TIMER_CC, t1_to_t2_delay);
  TIMER_TopSet(T2_TIMER, timer_max_count);
}
Ejemplo n.º 14
0
/**************************************************************************//**
 * @brief Configure TIMER to trigger ADC through PRS at a set sample rate
 *****************************************************************************/
void setupAdc(void)
{
  ADC_Init_TypeDef        adcInit       = ADC_INIT_DEFAULT;
  ADC_InitSingle_TypeDef  adcInitSingle = ADC_INITSINGLE_DEFAULT;
  
  /* Configure ADC single mode to sample Ref/2 */
  adcInit.prescale = ADC_PrescaleCalc(7000000, 0); /* Set highest allowed prescaler */
  ADC_Init(ADC0, &adcInit);
  
  adcInitSingle.input     =  adcSingleInpVrefDiv2;  /* Reference */
  adcInitSingle.prsEnable = true;                  
  adcInitSingle.prsSel    = adcPRSSELCh0;           /* Triggered by PRS CH0 */
  ADC_InitSingle(ADC0, &adcInitSingle);
  
  /* Connect PRS channel 0 to TIMER overflow */
  PRS_SourceSignalSet(0, PRS_CH_CTRL_SOURCESEL_TIMER0, PRS_CH_CTRL_SIGSEL_TIMER0OF, prsEdgeOff);
  
  /* Configure TIMER to trigger 100 kHz sampling rate */
  TIMER_TopSet(TIMER0,  CMU_ClockFreqGet(cmuClock_TIMER0)/ADCSAMPLESPERSEC);
  TIMER_Enable(TIMER0, true);
}
Ejemplo n.º 15
0
/***************************************************************************//**
 * @brief
 *  Start the TIMER1 to generate a 50% duty cycle output.
 *
 * @param[in] frequency
 *  The output frequency in Hz.
 ******************************************************************************/
void TD_TIMER_Start(uint32_t frequency)
{
	uint32_t top;

	top = CMU_ClockFreqGet(cmuClock_TIMER1);
	top = top / frequency;

	// Enable clock for TIMER1 module
	CMU_ClockEnable(cmuClock_TIMER1, true);

	// Configure CC channel 0
	TIMER_InitCC(TIMER1, 0, &timerCCInit);

	// Route CC0 to location 0 (PC13) and enable pin
	//TIMER1->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_LOCATION_LOC0);

	// Set Top Value
	TIMER_TopSet(TIMER1, top);

	// Set compare value starting at 0 - it will be incremented in the interrupt handler
	TIMER_CompareBufSet(TIMER1, 0, top >> 1);

	// Configure timer
	TIMER_Init(TIMER1, &timerInit);

	// Enable overflow interrupt
	TIMER_IntEnable(TIMER1, TIMER_IF_OF);

	// Disable interrupts
	//TIMER_IntDisable(TIMER1, TIMER_IF_OF);

	// Enable TIMER1 interrupt vector in NVIC
	NVIC_EnableIRQ(TIMER1_IRQn);

	// Enable timer
	TIMER_Enable(TIMER1, true);
	TD_TIMER_Enabled = true;
}
Ejemplo n.º 16
0
/**************************************************************************//**
 * @brief This function enables the 100us timer for HRM.
 *****************************************************************************/
static void startTimer ()
{
  // Enable clock for TIMER module
  CMU_ClockEnable(CLK_HRM_TIMER1, true);
  CMU_ClockEnable(CLK_HRM_TIMER2, true);
  TIMER_Reset(HRM_TIMER1);
  TIMER_Reset(HRM_TIMER2);
  // Select TIMER parameters
  TIMER_Init_TypeDef timerInit1 =
  {
    .enable     = true,
    .debugRun   = true,
    .prescale   = timerPrescale1,
    .clkSel     = timerClkSelHFPerClk,
    .fallAction = timerInputActionNone,
    .riseAction = timerInputActionNone,
    .mode       = timerModeUp,
    .dmaClrAct  = false,
    .quadModeX4 = false,
    .oneShot    = false,
    .sync       = false,
  };
  TIMER_Init_TypeDef timerInit2 =
    {
      .enable     = true,
      .debugRun   = true,
      .prescale   = timerPrescale1,
      .clkSel     = timerClkSelCascade,
      .fallAction = timerInputActionNone,
      .riseAction = timerInputActionNone,
      .mode       = timerModeUp,
      .dmaClrAct  = false,
      .quadModeX4 = false,
      .oneShot    = false,
      .sync       = false,
    };
  // Set TIMER Top value
  TIMER_TopSet(HRM_TIMER1, CMU_ClockFreqGet(cmuClock_HF)/1/10000); /*overflow every 100us*/
  TIMER_TopSet(HRM_TIMER2, 0xffff); /*max 16 bits*/
  // Configure TIMER
  TIMER_Init(HRM_TIMER1, &timerInit1);
  TIMER_Init(HRM_TIMER2, &timerInit2);
}

/**************************************************************************//**
 * @brief Initiate the Heart Rate Monitor
 *****************************************************************************/
void HeartRateMonitor_Init( Si114xPortConfig_t* i2c, HeartRateMonitor_Config_t configSelect )
{
  // Setup SysTick Timer for 10msec interrupts.
  if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 100)) while (1) ;
   // Setup Timer as 100 usec counter.  The HRM API requires a 100usec timestamp.
  startTimer ();
  dataStorage.spo2 = &spo2Data;
  dataStorage.hrm = &hrmDataStorage;
  hrmHandle = &dataStorage;

  si114xhrm_Initialize(i2c, 0, &hrmHandle);
  if (configSelect == BIOMETRIC_EXP)
  {
    si114xhrm_Configure(hrmHandle, &biometricEXPHRMConfig);
    currentHRMConfig = &biometricEXPHRMConfig;
  }
  else if (configSelect == SI1143_PS)
  {
    si114xhrm_Configure(hrmHandle, &Si1143PsHRMConfig);
    currentHRMConfig = &Si1143PsHRMConfig;
  }
  else if (configSelect == SI1147_PS)
  {
    si114xhrm_Configure(hrmHandle, &Si1147PsHRMConfig);
    currentHRMConfig = &Si1147PsHRMConfig;
  }

  //turn off LEDs
  setRedLED (false);
  setGreenLED (false);
  bufferOverrunError = false;
  displayHeartRateValue = 0;
  displaySpo2Value = 0;
  HeartRateMonitor_UpdateDisplay(HRM_STATE_NOSIGNAL, false);
}

/**************************************************************************//**
 * @brief Check for samples in irq queue
 *****************************************************************************/
bool HeartRateMonitor_SamplesPending ()
{
  HANDLE si114xHandle;
  si114xhrm_GetLowLevelHandle(hrmHandle, &si114xHandle);
  return (Si114xIrqQueueNumentries(si114xHandle)>0);
}

/**************************************************************************//**
 * @brief Reset inactive timer values
 *****************************************************************************/
static void resetInactiveTimer()
{
  enableInactiveTimer = false;
  inactiveTimeout = false;
  inactiveTimerCounter = 0;
}
Ejemplo n.º 17
0
void backlight_init()
{
	/*init and setup PA9(Backlight) and PA8(Motor) to GPIO output and PWM*/
	/* Enable clock for TIMER0 module */
  	CMU_ClockEnable(cmuClock_TIMER2, true);
  	/* Set  location 4 pin (PD7) as output */
  	GPIO_PinModeSet(gpioPortA, 8, gpioModePushPull, 0);
  	GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 0);  	

  	/*PA8 PA9 at TIMER2 Location0 CC0(PA8) and CC1(PA9)*/
  	/* Select CC channel parameters */
  	TIMER_InitCC_TypeDef timerCCInit =
  	{
		.eventCtrl  = timerEventEveryEdge,
		.edge       = timerEdgeBoth,
		.prsSel     = timerPRSSELCh0,
		.cufoa      = timerOutputActionNone,
		.cofoa      = timerOutputActionNone,
		.cmoa       = timerOutputActionToggle,
		.mode       = timerCCModePWM,
		.filter     = false,
		.prsInput   = false,
		.coist      = false,
		.outInvert  = false,
  	};
  	
  	/* Configure CC channel 0 CC0 for PA8 */		
  	TIMER_InitCC(TIMER2, 0, &timerCCInit);
	/* Configure CC channel 1 CC0 for PA9 */	
	timerCCInit.prsSel = timerPRSSELCh1;	
  	TIMER_InitCC(TIMER2, 1, &timerCCInit);  	
  	
  	/* Route CC0/CC1 to location 0 (PA8/PA9) and enable pin */ 
	TIMER2->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC0);
  	  	
  	 /* Set Top Value */  	   	
  	 TIMER_TopSet(TIMER2, TOP);  	
  
  	/*Set CCVB = 0 to TIMER2_CC0 and TIMER2_CC1 */
  	TIMER_CompareBufSet(TIMER2, 0, 0);
  	TIMER_CompareBufSet(TIMER2, 1, 0);
	  	
	/* 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,
  	};
  	
  	/* Enable overflow interrupt */
  	TIMER_IntEnable(TIMER2, TIMER_IF_OF);  		  	
  
	/* Enable TIMER2 interrupt vector in NVIC */
  	NVIC_EnableIRQ(TIMER2_IRQn);  
  	
  	/* Configure timer */
  	TIMER_Init(TIMER2, &timerInit);  	
	light_state = MOTOR_ON | LIGHT_ON;
	LIGHTLEVEL = 0;
}

void backlight_shutdown()
{
 	TIMER_CompareBufSet(TIMER2, 0, 0);	
  	TIMER_CompareBufSet(TIMER2, 1, 0);
  	GPIO_PinOutClear(gpioPortA, 8);		
  	GPIO_PinOutClear(gpioPortA, 9);		
  	light_state = 0;
  	TIMER_Enable(TIMER2,false);
  	CMU_ClockEnable(cmuClock_TIMER2, false);
  		
}

void light_stop(void *ptr)
{
	if (LIGHTLEVEL > 2)
  	{
    		LIGHTLEVEL--;
    		clock_time_t length = (clock_time_t)ptr;
    		ctimer_set(&light_timer, length, light_stop, (void*)length);
  	}
  	else
  	{	
  		TIMER_CompareBufSet(TIMER2, 1, 0);	  	
  		GPIO_PinOutClear(gpioPortA, 9);		
  		light_state &= ~LIGHT_ON;
  		if(light_state == 0)
  		{
  			TIMER_Enable(TIMER2,false);	
  			CMU_ClockEnable(cmuClock_TIMER2, false);
  		}	
  	}		
}

void backlight_on(uint8_t level, clock_time_t length)
{
	CMU_ClockEnable(cmuClock_TIMER2, true);  	
	TIMER_Enable(TIMER2,true);	
  	if (level > 8) level = 8;

  	if (level == 0)
  	{
    		TIMER_CompareBufSet(TIMER2, 1, 0);	
  	}
  	else
  	{
  		LIGHTLEVEL = level * 2;
  		TIMER_CompareBufSet(TIMER2, 1, ( LIGHTLEVEL * 100));	
    		if (length > 0)
      			ctimer_set(&light_timer, length, light_stop, (void*)(CLOCK_SECOND/8));
  	}
}
Ejemplo n.º 18
0
int main(void)
{
	CHIP_Init();

	CMU_ClockEnable(cmuClock_GPIO, true);
	CMU_ClockEnable(cmuClock_TIMER1, true);
	CMU_ClockEnable(cmuClock_TIMER3, true);

	// Set up TIMER1 for timekeeping
	TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
	timerInit.prescale = timerPrescale1024;

	TIMER_IntEnable(TIMER1, TIMER_IF_OF);

	// Enable TIMER1 interrupt vector in NVIC
	NVIC_EnableIRQ(TIMER1_IRQn);

	// Set TIMER Top value
	TIMER_TopSet(TIMER1, ONE_SECOND_TIMER_COUNT);

	TIMER_Init(TIMER1, &timerInit);

	// Wait for the timer to get going
	while (TIMER1->CNT == 0) ;

	// Enable LED output
	GPIO_PinModeSet(LED_PORT, LED_PIN, gpioModePushPull, 0);

	// Create the object initializer for LED PWM
	TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
	timerCCInit.mode = timerCCModePWM;
	timerCCInit.cmoa = timerOutputActionToggle;

	// Configure TIMER3 CC channel 2
	TIMER_InitCC(TIMER3, TIMER_CHANNEL, &timerCCInit);

	// Route CC2 to location 1 (PE3) and enable pin for cc2
	TIMER3->ROUTE |= (TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC1);

	// Set Top Value
	TIMER_TopSet(TIMER3, TIMER_TOP);

	// Set the PWM duty cycle here!
	TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, 0);

	// Create a timerInit object, based on the API default
	TIMER_Init_TypeDef timerInit2 = TIMER_INIT_DEFAULT;
	timerInit2.prescale = timerPrescale256;

	TIMER_Init(TIMER3, &timerInit2);

	enum mode_values { RAMPING_UP, HIGH, RAMPING_DOWN, LOW};

	// Check for properly sized constants
	uint16_t delta = MAX_BRIGHTNESS - MIN_BRIGHTNESS;
	if ( delta == 0 || RAMP_UP_TIME_MS % delta || RAMP_DOWN_TIME_MS % delta)
	{
		DEBUG_BREAK
	}

	// Set the initial condition
	uint16_t mode = RAMPING_UP;
	uint32_t time_step = RAMP_UP_TIME_MS / delta;
	uint16_t brightness = MIN_BRIGHTNESS;
	TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, brightness);

	uint64_t mode_timeout = set_ms_timeout(RAMP_UP_TIME_MS);

	while (1)
	{
		switch (mode)
		{
			case RAMPING_UP:
				delay_ms(time_step);
				brightness++;
				TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, brightness);
				if (expired_ms(mode_timeout))
				{
					mode = HIGH;
					mode_timeout = set_ms_timeout(HIGH_DURATION_MS);
				}
				break;
			case HIGH:
				if (expired_ms(mode_timeout))
				{
					mode = RAMPING_DOWN;
					time_step = RAMP_DOWN_TIME_MS / delta;
					mode_timeout = set_ms_timeout(RAMP_DOWN_TIME_MS);
				}
				break;
			case RAMPING_DOWN:
				delay_ms(time_step);
				brightness--;
				TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, brightness);
				if (expired_ms(mode_timeout))
				{
					mode = LOW;
					mode_timeout = set_ms_timeout(LOW_DURATION_MS);
				}
				break;
			case LOW:
				if (expired_ms(mode_timeout))
				{
					mode = RAMPING_UP;
					time_step = RAMP_UP_TIME_MS / delta;
					mode_timeout = set_ms_timeout(RAMP_UP_TIME_MS);
				}
				break;
		}
	}
}
Ejemplo n.º 19
0
void initPWM() // Brightness should be less than TIMER_TOP (1024)
{
    /* Enable clocks */
    CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
    CMU_ClockEnable(cmuClock_TIMER1, true);
    
    /* Set pins */
    GPIO_PinModeSet(gpioPortE,10,gpioModePushPull,1);
    GPIO_PinModeSet(gpioPortE,11,gpioModePushPull,1);
    
    /* Select CC channel parameters */
    TIMER_InitCC_TypeDef timerCCInit =
    {
        .eventCtrl  = timerEventEveryEdge,
        .edge       = timerEdgeBoth,
        .prsSel     = timerPRSSELCh0,
        .cufoa      = timerOutputActionNone,
        .cofoa      = timerOutputActionNone,
        .cmoa       = timerOutputActionToggle,
        .mode       = timerCCModePWM,
        .filter     = false,
        .prsInput   = false,
        .coist      = false,
        .outInvert  = false,
    };
    
    /* Configure CC channels */
    TIMER_InitCC(TIMER1, 0, &timerCCInit);
    TIMER_InitCC(TIMER1, 1, &timerCCInit);
    
    /* Set which pins will be set by the timer */
    TIMER1->ROUTE = TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC1;
    
    /* Set Top Value */
    TIMER_TopSet(TIMER1, TIMER_TOP);
    
    /* Set compare value starting at top - it will be incremented in the interrupt handler */
    TIMER_CompareBufSet(TIMER1, 0, TIMER_TOP + 1);
    TIMER_CompareBufSet(TIMER1, 1, TIMER_TOP + 1);
    
    /* Select timer parameters */
    TIMER_Init_TypeDef timerInit =
    {
        .enable     = true,
        .debugRun   = false,
        .prescale   = timerPrescale16,
        .clkSel     = timerClkSelHFPerClk,
        .fallAction = timerInputActionNone,
        .riseAction = timerInputActionNone,
        .mode       = timerModeUp,
        .dmaClrAct  = false,
        .quadModeX4 = false,
        .oneShot    = false,
        .sync       = false,
    };
    
    /* Enable overflow interrupt */
    TIMER_IntEnable(TIMER1, TIMER_IF_OF);
    
    
    TIMER_IntClear(TIMER1, TIMER_IF_OF);
    
    /* Enable TIMER1 interrupt vector in NVIC */
    NVIC_EnableIRQ(TIMER1_IRQn);
    
    /* Configure timer */
    TIMER_Init(TIMER1, &timerInit);
}
Ejemplo n.º 20
0
/*******************************************************************************
 * @brief Initialize TIMER1 in Up/Down Count mode with interrupts on overflow
 *
 ******************************************************************************/
void InitTimer1(void)
{

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

  /* Enable clock for TIMER1 */
  CMU_ClockEnable( cmuClock_TIMER1, true );


  /* Set CC0 location 3 pin (PD1) as output */
  GPIO_PinModeSet( DL_PORT, DL_PIN, gpioModePushPull, DL_OFF_LVL  );
  /* Set CC1 location 3 pin (PD2) as output */
  GPIO_PinModeSet( DH_PORT, DH_PIN, gpioModePushPull, DH_OFF_LVL);

  #define TOP         (F_HFXO/F_TX)/2   // timer 0 top value use F_HFXO, F_TX

  /* Configuring the Capture-Compare-Channels */

  /* Configure CC channel 0 */
  #define CC_CH_0     0
  TIMER1->CC[CC_CH_0].CTRL = DL_CC_STOP;
  #define CC_VAL_CH0  TOP/4
  TIMER_CompareSet( TIMER1, CC_CH_0, CC_VAL_CH0 );

  /* Configure CC channel 1 */
  #define CC_CH_1     1
  TIMER1->CC[CC_CH_1].CTRL = DH_CC_STOP;
  #define CC_VAL_CH1  TOP/4*3
  TIMER_CompareSet( TIMER1, CC_CH_1, CC_VAL_CH1 );

  /* Configure CC channel 2 */
  #define CC_CH_2     2
  TIMER1->CC[CC_CH_2].CTRL = CC2_STOP;
  #define CC_VAL_CH2  TOP/2
  TIMER_CompareSet( TIMER1, CC_CH_2, CC_VAL_CH2 );


  /* Enable overflow interrupt for TIMER1*/
  TIMER_IntEnable( TIMER1, TIMER_IEN_CC2 );


  /* Clear pending TIMER1 interrupts */
  NVIC_ClearPendingIRQ( TIMER1_IRQn );
  /* Enable TIMER1 interrupt vector in NVIC */
  NVIC_EnableIRQ( TIMER1_IRQn );


  /* Route CC0 and CC1 to location 4 (PD6 PD7) and enable pins */
  TIMER1->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC4);


  /* Set Top Value */
  TIMER_TopSet( TIMER1, TOP );

  /* Select timer parameters */
  TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;

  timerInit.enable      = false;                   // if timer is active after init
  // timerInit.enable      = true;                   // if timer is active after init
  timerInit.debugRun    = true;                   // if timer runs when CPU in debugmode
  timerInit.prescale    = timerPrescale1;         // prescaler of Timer
  timerInit.clkSel      = timerClkSelHFPerClk;    // CLK source select
  timerInit.fallAction  = timerInputActionNone;   // counter action if falling edge on input
  timerInit.riseAction  = timerInputActionNone;   // counter action if rising edge on input
  timerInit.mode        = timerModeUpDown;        // Mode of timer
  timerInit.dmaClrAct   = false;                  // DMA mode clear or active
  timerInit.quadModeX4  = false;                  // Quadrature decode mode
  timerInit.oneShot     = false;                  // determine if only one count cycle
  timerInit.sync        = false;                  // Start/stop/reload by other timers


  /* Initialize and Configure timer */
  TIMER_Init( TIMER1, &timerInit );

  /* Start timer */

} // END: InitTimer
Ejemplo n.º 21
0
void InitAudioPWM(void)
{
    CMU_ClockEnable(cmuClock_TIMER1, true);
    /* Select CC channel parameters */
    TIMER_InitCC_TypeDef timerCCInit =
    {
        .eventCtrl  = timerEventEveryEdge,
        .edge       = timerEdgeBoth,
        .prsSel     = timerPRSSELCh0,
        .cufoa      = timerOutputActionNone,
        .cofoa      = timerOutputActionNone,
        .cmoa       = timerOutputActionToggle,
        .mode       = timerCCModePWM,
        .filter     = false,
        .prsInput   = false,
        .coist      = false,
        .outInvert  = false,
    };

    /* Configure CC channel 0 */
    TIMER_InitCC(TIMER1, 0, &timerCCInit);
    TIMER_InitCC(TIMER1, 1, &timerCCInit);
    //TIMER_InitCC(TIMER0, 2, &timerCCInit);

//  TIMER3->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC0);

    TIMER1->ROUTE = TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC1;
    /* Set Top Value */
    TIMER_TopSet(TIMER1, 255);//384

    /* Set compare value starting at top - it will be incremented in the interrupt handler */
    TIMER_CompareBufSet(TIMER1, 0, 256);//385
    TIMER_CompareBufSet(TIMER1, 1, 256);//385
    //TIMER_CompareBufSet(TIMER3, 2, RGB_PWM_TIMER_TOP + 1);

    /* Select timer parameters */
    TIMER_Init_TypeDef timerInit =
    {
        .enable     = true,
        .debugRun   = false,
        .prescale   = timerPrescale8,
        .clkSel     = timerClkSelHFPerClk,
        .fallAction = timerInputActionNone,
        .riseAction = timerInputActionNone,
        .mode       = timerModeUp,
        .dmaClrAct  = false,
        .quadModeX4 = false,
        .oneShot    = false,
        .sync       = false,
    };

    ///* Enable overflow interrupt */
    TIMER_IntEnable(TIMER1, TIMER_IF_OF);


    TIMER_IntClear(TIMER1, TIMER_IF_OF);
    /* Enable TIMER0 interrupt vector in NVIC */
    NVIC_EnableIRQ(TIMER1_IRQn);

    /* Configure timer */
    TIMER_Init(TIMER1, &timerInit);
}




void TIMER1_IRQHandler(void)
{
    int audio_Sample = 0;
    if(toPlay > 0)
    {
        audio_Sample = buff[(iterator = next(iterator))];
        toPlay--;
    }
    else
    {
        audio_Sample = 0;
    }
    TIMER_CompareBufSet(TIMER1, 0, (audio_Sample));

    TIMER_IntClear(TIMER1, TIMER_IF_OF);
}
Ejemplo n.º 22
0
/***************************************************************************//**
* @brief
*   Main function. Setup ADC, FFT, clocks, PRS, DMA, Timer,
*   and process FFT forever.
*******************************************************************************/
int main(void)
{
  arm_status status;
  char buf[20];
  bool redraw = false;
  int glibStatus;
  
  DMA_Init_TypeDef   dmaInit;
  TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;

  /* Initialize DVK board register access */
  BSP_Init(BSP_INIT_DEFAULT);

  /* If first word of user data page is non-zero, enable eA Profiler trace */
  BSP_TraceEtmSetup();

  /* Connect audio in to ADC */
  BSP_PeripheralAccess(BSP_AUDIO_IN, true);

  /* Wait a while in order to let signal from audio-in stabilize after */
  /* enabling audio-in peripheral. */
  RTCDRV_Trigger(1000, NULL);
  EMU_EnterEM2(true);
  
  /* Initialize the CFFT/CIFFT module */
  status = arm_rfft_init_f32(&rfft_instance,
                             &cfft_instance,
                             GUITAR_AUDIO_BUFFER_SAMPLES,
                             0,  /* forward transform */
                             1); /* normal, not bitreversed, order */
  
  if (status != ARM_MATH_SUCCESS) {
    /* Error initializing RFFT module. */
    while (1) ;
  }
  
  dataReadyForFFT = false;
  processingFFT   = false;
  
  /* Use the HFXO. We still only manage to process about
   * a third the buffers through FFT
   */
  CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
  
  /* Setup SysTick Timer for 1 msec interrupts  */
  if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000))
  {
    while (1) ;
  }

  /* Enable clocks required */
  CMU_ClockEnable(cmuClock_HFPER, true);
  CMU_ClockEnable(cmuClock_ADC0, true);
  CMU_ClockEnable(cmuClock_PRS, true);
  CMU_ClockEnable(cmuClock_DMA, true);
  CMU_ClockEnable(cmuClock_TIMER0, true);

  NVIC_SetPriority(DMA_IRQn, 0); /* Highest priority */

  /* Configure peripheral reflex system used by TIMER to trigger ADC/DAC */
  guitarPRSConfig(GUITAR_PRS_CHANNEL);

  /* Configure general DMA issues */
  dmaInit.hprot        = 0;
  dmaInit.controlBlock = dmaControlBlock;
  DMA_Init(&dmaInit);

  /* Configure ADC used for audio-in */
  guitarADCConfig();

  /* Trigger sampling according to configured sample rate */
  TIMER_TopSet(TIMER0, CMU_ClockFreqGet(cmuClock_HFPER) / GUITAR_AUDIO_SAMPLE_RATE);
  TIMER_Init(TIMER0, &timerInit);

  /* Wait until we have control over display */
  while(!redraw)
  {
    redraw = TFT_AddressMappedInit();
  }
  
  /* Init graphics context - abort on failure */
  glibStatus = GLIB_contextInit(&gc);
  if (glibStatus != GLIB_OK) while (1) ;
  
  /* Clear the screen */
  gc.backgroundColor = GLIB_rgbColor(0, 0, 0);
  GLIB_clear(&gc);
  
  while (1)
  {
    while (dataReadyForFFT)
    {
      float32_t freq;

      processingFFT = true;
      processFFT();
      dataReadyForFFT = false;
      processingFFT = false;
      
      /* Get frequency and make string with one decimal accuracy */
      freq = getFreq();
      sprintf(buf, "%6.1f", freq);
      
      /* Check if we should control TFT display instead of AEM/board controller */
      redraw = TFT_AddressMappedInit();
      if (redraw)
      {
        gc.foregroundColor = GLIB_rgbColor(220, 220, 220);
        gc.backgroundColor = GLIB_rgbColor(0, 0, 0);

        /* Print the frequency somewhere in the middle of the screen */
        GLIB_drawString(&gc,
                        buf,
                        strlen(buf),
                        100,
                        120,
                        1);
      }
    }
    EMU_EnterEM1();
  }
}
Ejemplo n.º 23
0
int main(void)
{
  DMA_Init_TypeDef   dmaInit;
  TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
  uint32_t           leds;
  static uint16_t    last_buttons = 0;
  uint16_t           buttons;

  /* Initialize DK board register access */
  BSP_Init(BSP_INIT_DEFAULT);

  /* If first word of user data page is non-zero, enable eA Profiler trace */
  BSP_TraceProfilerSetup();

  volume             = 7;
  preampAdjustFactor = preampVolume[ volume ];
  BSP_LedsSet((uint16_t)(0x00003FFF << (15 - volume)));

  /* Connect audio in/out to ADC/DAC */
  BSP_PeripheralAccess(BSP_AUDIO_IN, true);
  BSP_PeripheralAccess(BSP_AUDIO_OUT, true);

  /* Wait a while in order to let signal from audio-in stabilize after */
  /* enabling audio-in peripheral. */
  RTCDRV_Trigger(1000, NULL);
  EMU_EnterEM2(true);

  /* Current example gets by at 14MHz core clock (also with low level of compiler */
  /* optimization). That may however change if modified, consider changing to */
  /* higher HFRCO band or HFXO. */
  /*
   * Use for instance one of below statements to increase core clock.
   * CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
   * CMU_HFRCOBandSet(cmuHFRCOBand_28MHz);
   */

  /* Enable clocks required */
  CMU_ClockEnable(cmuClock_HFPER, true);
  CMU_ClockEnable(cmuClock_ADC0, true);
  CMU_ClockEnable(cmuClock_DAC0, true);
  CMU_ClockEnable(cmuClock_PRS, true);
  CMU_ClockEnable(cmuClock_DMA, true);
  CMU_ClockEnable(cmuClock_TIMER0, true);

  /* Ensure DMA interrupt at higher priority than PendSV. */
  /* (PendSV used to process sampled audio). */
  NVIC_SetPriority(DMA_IRQn, 0);                              /* Highest priority */
  NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1); /* Lowest priority */

  /* Configure peripheral reflex system used by TIMER to trigger ADC/DAC */
  preampPRSConfig(PREAMP_PRS_CHANNEL);

  /* Configure general DMA issues */
  dmaInit.hprot        = 0;
  dmaInit.controlBlock = dmaControlBlock;
  DMA_Init(&dmaInit);

  /* Configure DAC used for audio-out */
  preampDACConfig();

  /* Configure ADC used for audio-in */
  preampADCConfig();

  /* Trigger sampling according to configured sample rate */
  TIMER_TopSet(TIMER0, CMU_ClockFreqGet(cmuClock_HFPER) / PREAMP_AUDIO_SAMPLE_RATE);
  TIMER_Init(TIMER0, &timerInit);

  /* Main loop, only responsible for checking volume */
  while (1)
  {
    /* Triggered to check volume setting? */
    if (preampCheckVolume)
    {
      preampCheckVolume = false;

      /* Calculate new output volume. */

      buttons = BSP_PushButtonsGet() & PB_MASK; /* Check pushbuttons */
      if (buttons != last_buttons)
      {
        if (buttons & BC_UIF_PB2)               /* Increase volume */
        {
          if (volume < VOLUME_MAX)
            volume++;
        }
        else if (buttons & BC_UIF_PB1)          /* Decrease volume */
        {
          if (volume)
            volume--;
        }
        preampAdjustFactor = preampVolume[ volume ];
        last_buttons       = buttons;
      }

      /* Use 14 leftmost leds for volume control indication. Rightmost led is */
      /* used to indicate clipping of audio out signal.                       */

      leds = 0x00003FFF << (15 - volume);

      /* Audio out clipped? */
      if (preampAudioOutClipped)
      {
        preampAudioOutClipped = false;
        leds                 |= 0x0001;
      }
      BSP_LedsSet((uint16_t) leds);
    }

    EMU_EnterEM1();
  }
}
Ejemplo n.º 24
0
int main(void)
{
  DMA_Init_TypeDef dmaInit;
  TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
  SYSTEM_ChipRevision_TypeDef chipRev;
  uint32_t volSample;
  uint32_t vpot;
  uint32_t rpot;
  uint32_t leds;

  /* Chip revision alignment and errata fixes */
  CHIP_Init();

  /* ADC errata for rev B when using VDD as reference, need to multiply */
  /* result by 2 */
  SYSTEM_ChipRevisionGet(&chipRev);
  if ((chipRev.major == 1) && (chipRev.minor == 1))
  {
    preampErrataShift = 1;
  }

  /* Initialize DVK board register access */
  BSP_Init(BSP_INIT_DEFAULT);

  /* If first word of user data page is non-zero, enable eA Profiler trace */
  BSP_TraceProfilerSetup();

  /* Connect potentiometer to EFM32 (and ensure ambient light sensor */
  /* sharing same ADC channel is not enabled). */
  BSP_PeripheralAccess(BSP_AMBIENT, false);
  BSP_PeripheralAccess(BSP_POTMETER, true);

  /* Connect audio in/out to ADC/DAC */
  BSP_PeripheralAccess(BSP_AUDIO_IN, true);
  BSP_PeripheralAccess(BSP_AUDIO_OUT, true);

  /* Wait a while in order to let signal from audio-in stabilize after */
  /* enabling audio-in peripheral. */
  RTCDRV_Trigger(1000, NULL);
  EMU_EnterEM2(true);

  /* Current example gets by at 14MHz core clock (also with low level of compiler */
  /* optimization). That may however change if modified, consider changing to */
  /* higher HFRCO band or HFXO. */
  /*
    Use for instance one of below statements to increase core clock.
    CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
    CMU_HFRCOBandSet(cmuHFRCOBand_28MHz);
  */

  /* Enable clocks required */
  CMU_ClockEnable(cmuClock_HFPER, true);
  CMU_ClockEnable(cmuClock_ADC0, true);
  CMU_ClockEnable(cmuClock_DAC0, true);
  CMU_ClockEnable(cmuClock_PRS, true);
  CMU_ClockEnable(cmuClock_DMA, true);
  CMU_ClockEnable(cmuClock_TIMER0, true);

  /* Ensure DMA interrupt at higher priority than PendSV. */
  /* (PendSV used to process sampled audio). */
  NVIC_SetPriority(DMA_IRQn, 0); /* Highest priority */
  NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1); /* Lowest priority */

  /* Configure peripheral reflex system used by TIMER to trigger ADC/DAC */
  preampPRSConfig(PREAMP_PRS_CHANNEL);

  /* Configure general DMA issues */
  dmaInit.hprot = 0;
  dmaInit.controlBlock = dmaControlBlock;
  DMA_Init(&dmaInit);

  /* Configure DAC used for audio-out */
  preampDACConfig();

  /* Configure ADC used for audio-in */
  preampADCConfig();

  /* Trigger sampling according to configured sample rate */
  TIMER_TopSet(TIMER0, CMU_ClockFreqGet(cmuClock_HFPER) / PREAMP_AUDIO_SAMPLE_RATE);
  TIMER_Init(TIMER0, &timerInit);

  /* Main loop, only responsible for checking volume */
  while (1)
  {
    /* Triggered to check volume setting? */
    if (preampCheckVolume)
    {
      preampCheckVolume = false;

      /* Trigger single conversion, and wait for it to complete */
      ADC_IntClear(ADC0, ADC_IF_SINGLE);
      ADC_Start(ADC0, adcStartSingle);
      while (!(ADC_IntGet(ADC0), ADC_IF_SINGLE))
      {
        /* Just wait for an interrupt to wake us up (DMA interrupts occur */
        /* regularly). Thus, we don't need to enable ADC interrupt. */
        EMU_EnterEM1();
      }
      volSample = ADC_DataSingleGet(ADC0) << preampErrataShift;

      /*
        DVK potentiometer design:

                 | Vdd = 3.3V
                +-+
                | | Rpullup = 10kOhm
                +-+
                 |
                 +------> Vpot (to ADC)
                 |
                +-+
                | | Rpot = 0-100kOhm
                +-+
                 | Gnd

         Vpot = Rpot * Vdd / (Rpullup + Rpot)

         This gives a non-linear voltage level measured by ADC with respect to
         pot meter position. In order to determine the actual Rpot setting, which
         is linear with respect to position, rewrite the above formula to:

         Rpot = Rpullup * Vpot / (Vdd - Vpot)
      */

      /* Vpot sampled with 8 bit, divide by max value */
      vpot = (POTENTIOMETER_VDD_mV * volSample) / 0xff;

      /* Calculate Rpot determining volume */
      rpot = (POTENTIOMETER_PULLUP_OHM * vpot) / (POTENTIOMETER_VDD_mV - vpot);
      /* The potentiometer may not be exact in upper range, make sure we don't use a higher */
      /* value than assumed by defines. */
      if (rpot > POTENTIOMETER_MAX_OHM)
      {
        rpot = POTENTIOMETER_MAX_OHM;
      }

      /* Precalculate adjustment factor to avoid repeated calculation when used. */
      /* Scale down Rpot a bit to use in integer calculation without overflowing 32 bit reg. */
      preampAdjustFactor = rpot / PREAMP_ADJUST_DIVISOR;

      /* Use 14 right leds for volume control indicating. Leftmost led is used to indicate */
      /* clipping of audio out signal (in order to limit volume out). Add half interval */
      /* for improving integer rounding effects. */
      leds = rpot + (POTENTIOMETER_MAX_OHM / (14 * 2));
      leds = (1 << ((14 * leds) / POTENTIOMETER_MAX_OHM)) - 1;

      /* Audio out clipped? */
      if (preampAudioOutClipped)
      {
        preampAudioOutClipped = false;
        leds |= 0x8000;
      }
      BSP_LedsSet((uint16_t)leds);
    }

    EMU_EnterEM1();
  }
}
Ejemplo n.º 25
0
/**************************************************************************//**
 * @brief  Main function
 * Main is called from __iar_program_start, see assembly startup file
 *****************************************************************************/
int motor_init(void)
{
  uint32_t top_value = 0;

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

  /* Enable clock for TIMER0 module */
  CMU_ClockEnable(cmuClock_TIMER0, true);

  /* Set CC1 location 3 pin (PD2) as output */
  //Left wheel
  GPIO_PinModeSet(gpioPortD, 2, gpioModePushPull, 0);
  /* Set CC2 location 3 pin (PD3) as output */
  //Right wheel
  GPIO_PinModeSet(gpioPortD, 3, gpioModePushPull, 0);

  /* Select CC channel parameters */
  TIMER_InitCC_TypeDef timerCCInit =
  {
    .eventCtrl  = timerEventEveryEdge,
    .edge       = timerEdgeBoth,
    .prsSel     = timerPRSSELCh0,
    .cufoa      = timerOutputActionNone,
    .cofoa      = timerOutputActionNone,
    .cmoa       = timerOutputActionToggle,
    .mode       = timerCCModePWM,
    .filter     = false,
    .prsInput   = false,
    .coist      = false,
    .outInvert  = false,
  };

  /* Configure CC channel 0 */
  TIMER_InitCC(TIMER0, 1, &timerCCInit);
  TIMER_InitCC(TIMER0, 2, &timerCCInit);

  /* Route CC0 to location 3 (PD1) and enable pin */
  TIMER0->ROUTE |= (TIMER_ROUTE_CC2PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC3);

  /* Set Top Value */
  TIMER_TopSet(TIMER0, CMU_ClockFreqGet(cmuClock_HFPER)/PWM_FREQ);

  top_value = CMU_ClockFreqGet(cmuClock_HFPER)/PWM_FREQ;

  forward_value = ((18*top_value)/200);
  backward_value = ((12*top_value)/200);
  steady_value = ((15*top_value)/200);

  /* Set compare value starting at 0 - it will be incremented in the interrupt handler */
  TIMER_CompareBufSet(TIMER0, 1, 0);
  TIMER_CompareBufSet(TIMER0, 2, 0);

  /* 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,
  };

  /* Enable overflow interrupt */
  TIMER_IntEnable(TIMER0, TIMER_IF_OF);

  /* Enable TIMER0 interrupt vector in NVIC */
  NVIC_EnableIRQ(TIMER0_IRQn);

  /* Configure timer */
  TIMER_Init(TIMER0, &timerInit);

  return 0;
}