/***************************************************************************//** * @brief RTC trigger enable * @param msec Enable trigger in msec * @param cb Callback invoked when @p msec elapsed ******************************************************************************/ void RTC_Trigger(uint32_t msec, void (*cb)(void)) { /* Disable RTC - this will also reset the counter. */ RTC_Enable(false); /* Auto init upon first use */ if (!rtcInitialized) { RTC_Setup(); } /* Register callback */ rtcCb = cb; /* Clear interrupt source */ RTC_IntClear(RTC_IF_COMP0); /* Calculate trigger value in ticks based on 32768Hz clock */ RTC_CompareSet(0, (RTC_FREQ * msec) / 1000); /* Enable RTC */ RTC_Enable(true); /* Enable interrupt on COMP0 */ RTC_IntEnable(RTC_IF_COMP0); }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main(void) { /* Chip revision alignment and errata fixes */ CHIP_Init(); /* Set the clock frequency to 11MHz so the ADC can run on the undivided HFCLK */ CMU_HFRCOBandSet(cmuHFRCOBand_11MHz); /* Configure RTC to use LFRCO as clock source */ RTC_Setup(cmuSelect_LFRCO); /* Configure ADC */ ADC_Config(); /* Start ADC sampling, adcFinished is set when sampling is finished. */ /* It is safe to do other stuff or go to EM2 while adc sampling is active. */ /* ADC sampling uses the RTC to trigger new samples. */ startAdcSampling(); /* Wait in EM2 until adc sampling is finished. */ /* Disable interrupts until flag is checked in case loop finishes after flag * check but before sleep command. Device will still wake up on any set IRQ * and any pending interrupts will be handled after interrupts are enabled * again. */ INT_Disable(); while(!adcFinished) { EMU_EnterEM2(false); INT_Enable(); INT_Disable(); } INT_Enable(); /* Finished */ while(1); }
/***************************************************************************//** * @brief RTC trigger enable * @param msec Enable trigger in msec * @param cb Callback invoked when @p msec elapsed ******************************************************************************/ void RTC_Trigger(uint32_t msec, void (*cb)(void)) { /* Auto init if not configured already */ if (!rtcInitialized) { /* Default to LFRCO as clock source */ RTC_Setup(cmuSelect_LFRCO); } /* Register callback */ rtcCb = cb; /* Clear interrupt source */ RTC_IntClear(RTC_IF_COMP0); /* Calculate trigger value in ticks based on 32768Hz clock */ RTC_CompareSet(0, (rtcFreq * msec) / 1000); /* Enable RTC */ RTC_Enable(true); /* Enable interrupt on COMP0 */ RTC_IntEnable(RTC_IF_COMP0); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Configure the USART peripheral ----------------------------------------*/ USART_InitStructure.USART_BRR = 115200; USART_InitStructure.USART_WL = USART_WL_8B; USART_InitStructure.USART_STBits = USART_STBITS_1; USART_InitStructure.USART_Parity = USART_PARITY_RESET; USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE ; USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX; GD_EVAL_COMInit(&USART_InitStructure); printf("\n\r *********************** RTC Calendar Demo ***********************\n\r"); /* Check if RTC has aready been configured */ if (RTC->BKP0R != BKP_VALUE) { RTC_PreConfiguration(); RTC_Setup(); } else { /* Detect the reset source */ if (RCC_GetBitState(RCC_FLAG_POPDRST) != RESET) { printf("\r\n Power On Reset Occurred....\n\r"); } else if (RCC_GetBitState(RCC_FLAG_EPRST) != RESET) { printf("\r\n External Reset Occurred....\n\r"); } printf("\n\r No Need To Configure RTC....\n\r"); #ifdef RTC_CLOCK_SOURCE_LSI RCC_LSI_Enable(ENABLE); #endif RTC_WaitRSF_ToSetAgain(); RTC_ClearBitState(RTC_STR_AF); EXTI_ClearIntBitState(EXTI_LINE17); RTC_ShowTime(); RTC_ShowAlarm(); } RCC_ClearBitState(); /* LED1 GPIO Initialization */ RCC_AHBPeriphClock_Enable(LED1_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = LED1_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure); LED1_GPIO_PORT->BOR = LED1_PIN; /* LED2 GPIO Initialization */ RCC_AHBPeriphClock_Enable(LED2_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = LED2_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure); LED2_GPIO_PORT->BOR = LED2_PIN; /* RTC ALARM Interrupt Configuration */ EXTI_ClearIntBitState(EXTI_LINE17); EXTI_InitStructure.EXTI_LINE = EXTI_LINE17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LINEEnable = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQ = RTC_IRQn; NVIC_InitStructure.NVIC_IRQPreemptPriority = 0; NVIC_InitStructure.NVIC_IRQSubPriority = 0; NVIC_InitStructure.NVIC_IRQEnable = ENABLE; NVIC_Init(&NVIC_InitStructure); while (1) { } }