static void System_Time_Init(void) { /* RTC Block section ------------------------------------------------------ */ // Init RTC module RTC_Init(LPC_RTC); /* Disable RTC interrupt */ NVIC_DisableIRQ(RTC_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(RTC_IRQn, ((0x01<<3)|0x01)); /* Enable rtc (starts increase the tick counter and second counter register) */ RTC_ResetClockTickCounter(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); RTC_CalibCounterCmd(LPC_RTC, DISABLE); /* Set current time for RTC */ // Current time is 06:45:00PM, 2011-03-25 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MINUTE, 45); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_HOUR, 15); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MONTH, 3); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_YEAR, 2014); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH, 30); return; }
void rt_hw_rtc_init(void) { rtc.type = RT_Device_Class_RTC; RTC_Init(LPC_RTC); if (RTC_ReadGPREG(LPC_RTC,0) != FIRST_DATA) { rt_kprintf("rtc is not configured\n"); rt_kprintf("please configure with set_date and set_time\n"); } else { } RTC_Cmd(LPC_RTC,ENABLE); /* register rtc device */ rtc.init = RT_NULL; rtc.open = rt_rtc_open; rtc.close = RT_NULL; rtc.read = rt_rtc_read; rtc.write = RT_NULL; rtc.control = rt_rtc_control; /* no private */ rtc.user_data = RT_NULL; rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR); #ifdef RT_USING_FINSH { extern void list_date(void); list_date(); } #endif return; }
/*********************************************************************//** * @brief RTC(Real-time clock) interrupt handler * @param[in] none * @return None **********************************************************************/ void RTC_IRQHandler(void) { if(RTC_GetIntPending(LPC_RTC, RTC_INT_ALARM)) { RTC_ClearIntPending(LPC_RTC, RTC_INT_ALARM); RTC_Cmd(LPC_RTC, DISABLE); NVIC_DisableIRQ(RTC_IRQn); } }
static void setupHardware(void) { // Se configuran los perifericos por defecto SystemInit(); // Se inicializa las task de comunicaciones // taskPcCommunicationInit(); semSdCardAccess = xSemaphoreCreateMutex(); xSemaphoreGive(semSdCardAccess); // SD Card LPC_GPIO0->FIODIR |= 1<<22; LPC_GPIO0->FIOCLR |= 1<<22; LPC_SC->PCLKSEL0 &= ~(3<<2); LPC_SC->PCLKSEL0 |= 1<<2; /* * Default values for the SPI clock * Use 400 kHz during init and 1 MHz during data transfer * * These values are believed to be reasonably safe values. */ SetSPIClocks(KHZ(400), MHZ(1)); f_mount(0, &fs); LPC_GPIO2->FIODIR |= (1<<0); // red LPC_GPIO2->FIODIR |= (1<<1); // green LPC_GPIO2->FIOCLR = (1<<0); // red LPC_GPIO2->FIOCLR = (1<<1); // green RTC_Init(LPC_RTC); /* Enable rtc (starts increase the tick counter and second counter register) */ RTC_ResetClockTickCounter(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); RTC_CalibCounterCmd(LPC_RTC, DISABLE); /* Set current time for RTC */ // Current time is 8:00:00PM, 2009-04-24 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MINUTE, 0); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_HOUR, 0); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MONTH, 12); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_YEAR, 2012); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH, 19); /* Set ALARM time for second */ RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 10); }
void RTC_time_Init(void){ // _DBG("{"); // Init RTC module RTC_Init(LPC_RTC); // Disable RTC interrupt NVIC_DisableIRQ(RTC_IRQn); NVIC_SetPriority(RTC_IRQn, 8); // set according to main.c RTC_Cmd(LPC_RTC, ENABLE); update_time(); // RTC_CalibCounterCmd(LPC_RTC, DISABLE); // RTC_WriteGPREG(LPC_RTC, 4, 0x55);//force clock set //Set time if no data in GPREG if (!(RTC_ReadGPREG(LPC_RTC, 4)==(0xaa))) { // xprintf(INFO "Setting time to" " (%s:%d)\n",_F_,_L_);//_DBG("[INFO]-Set time");_DBG(" (");_DBG(__FILE__);_DBG(":");_DBD16(__LINE__);_DBG(" (");_DBG(__FILE__);_DBG(":");_DBD16(__LINE__);_DBG(")\r\n"); xprintf(INFO "Setting time to %s %s" " (%s:%d)\n",__DATE__,__TIME__,_F_,_L_);//_DBG("[INFO]-Set time");_DBG(" (");_DBG(__FILE__);_DBG(":");_DBD16(__LINE__);_DBG(" (");_DBG(__FILE__);_DBG(":");_DBD16(__LINE__);_DBG(")\r\n"); // delay_ms(1000); // _DBG("[INFO]-__DATE__=");_DBG(__DATE__);_DBG(", __TIME__=");_DBG(__TIME__);_DBG(" (");_DBG(__FILE__);_DBG(":");_DBD16(__LINE__);_DBG(")\r\n"); // Enable rtc (starts increase the tick counter and second counter register) RTC_ResetClockTickCounter(LPC_RTC); // yyyy mm dd Dom Dow ss mm hh st // RTC_time_SetTime(2012, 6, 11, 1, 163, 10, 50, 20, 00); RTC_set_default_time_to_compiled(); RTC_WriteGPREG(LPC_RTC, 4, 0xaa); } ///* yearlyCheck(); weeklyCheck(); dailyCheck(); hourlyCheck(); minutelyCheck(); secondlyCheck(); RTC_print_time(); //*/ // time_t seconds; // seconds = time (NULL); // uint8_t test[56]; // xprintf ("{testing} %ld hours since January 1, 1970\r\n", Getunix()); // scanf (test); // Enable 1 sec interrupt RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); // Enable RTC interrupt NVIC_EnableIRQ(RTC_IRQn); // _DBG("."); // _DBG("}"); }
/*********************************************************************//** * @brief c_entry: Main program body * @param[in] None * @return int **********************************************************************/ int c_entry (void) { /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* In this example: * Suppose that the RTC need periodically adjust after each 5 second. * And the time counter need by incrementing the counter by 2 instead of 1 * We will observe timer counter after calibration via serial display */ // Init RTC module RTC_Init(LPC_RTC); /* Enable rtc (starts increase the tick counter and second counter register) */ RTC_ResetClockTickCounter(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); //Set current time = 0 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0); /* Setting Timer calibration * Calibration value = 5s; * Direction = Forward calibration * So after each 5s, calibration logic can periodically adjust the time counter by * incrementing the counter by 2 instead of 1 */ RTC_CalibConfig(LPC_RTC, 5, RTC_CALIB_DIR_FORWARD); RTC_CalibCounterCmd(LPC_RTC, ENABLE); /* Set the CIIR for second counter interrupt*/ RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); /* Enable RTC interrupt */ NVIC_EnableIRQ(RTC_IRQn); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main program body * @param[in] None * @return int **********************************************************************/ int c_entry (void) { /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* Initialize and configure RTC */ RTC_Init(LPC_RTC); RTC_ResetClockTickCounter(LPC_RTC); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0); /* Set alarm time = 5s. * So, after each 5s, RTC will generate and wake-up system * out of Deep PowerDown mode. */ RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 5); RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, DISABLE); /* Set the AMR for 5s match alarm interrupt */ RTC_AlarmIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); RTC_ClearIntPending(LPC_RTC, RTC_INT_ALARM); _DBG_("Press '1' to enter system in Deep PowerDown mode"); while(_DG !='1'); RTC_Cmd(LPC_RTC, ENABLE); NVIC_EnableIRQ(RTC_IRQn); _DBG_("Enter Deep PowerDown mode..."); _DBG_("Wait 5s, RTC will wake-up system...\n\r"); // Enter target power down mode CLKPWR_DeepPowerDown(); while(1); return 1; }
// Inicializa módulo RTC void rtc_init(void) { RTC_Init(LPC_RTC); CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCRTC, ENABLE); // Configura RTC para interrumpir cada 1 segundo RTC_CntIncrIntConfig(LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); // Limpia las interrupciones del RTC RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE); // Habilita las interrupciones del RTC en NVIC NVIC_EnableIRQ((IRQn_Type) RTC_IRQn); // Habilita el módulo RTC --> comienza a contar el tiempo RTC_Cmd(LPC_RTC, ENABLE); NVIC_SetPriority((IRQn_Type) RTC_IRQn, 6); }
static void init_RTC() { /* Enable GPIO register interface clock */ LPC_CCU1->CLK_M4_GPIO_CFG |= 1; while (!(LPC_CCU1->CLK_M4_GPIO_STAT & 1)) ; /* RTC Block section ------------------------------------------------------ */ /* Init RTC module */ RTC_Init(LPC_RTC); /* Set ALARM time for second */ RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 30); /* Set the AMR for 30s match alarm interrupt */ RTC_AlarmIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); /* Set the CIIR for minute counter interrupt*/ RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_MINUTE, ENABLE); /* Enable rtc (starts increase the tick counter and second counter register) */ RTC_Cmd(LPC_RTC, ENABLE); }
/*********************************************************************//** * @brief c_entry: Main program body * @param[in] None * @return None **********************************************************************/ void c_entry (void) { uint32_t pre_secval = 0, inc = 0, calib_cnt = 0; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* In this example: * Suppose that the RTC need periodically adjust after each 5 second. * And the time counter need by incrementing the counter by 2 instead of 1 * We will observe timer counter after calibration via serial display */ // Init RTC module RTC_Init(LPC_RTC); /* Enable rtc (starts increase the tick counter and second counter register) */ RTC_ResetClockTickCounter(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); //Set current time = 0 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0); /* Setting Timer calibration * Calibration value = 6s; * Direction = Forward calibration * So after each 6s, calibration logic can periodically adjust the time counter by * incrementing the counter by 2 instead of 1 */ RTC_CalibConfig(LPC_RTC, 6, RTC_CALIB_DIR_FORWARD); RTC_CalibCounterCmd(LPC_RTC, ENABLE); /* Set the CIIR for second counter interrupt*/ RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); /* Enable RTC interrupt */ NVIC_EnableIRQ(RTC_IRQn); /* Loop forever */ while(1) { if(pre_secval != secval) { if(pre_secval > secval) inc = 60 + secval - pre_secval; else inc = secval - pre_secval; if(inc > 1) { _DBG ("Second: "); _DBD(secval); _DBG("--> Increase ");_DBD(inc); _DBG(" after ");_DBD(calib_cnt);_DBG(" seconds"); _DBG_(""); calib_cnt = 0; } else { _DBG ("Second: "); _DBD(secval); _DBG_(""); calib_cnt++; } pre_secval = secval; } } }
/*********************************************************************//** * @brief c_entry: Main RTC program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { RTC_TIME_Type RTCFullTime; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* RTC Block section ------------------------------------------------------ */ // Init RTC module RTC_Init(LPC_RTC); /* Disable RTC interrupt */ NVIC_DisableIRQ(RTC_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(RTC_IRQn, ((0x01<<3)|0x01)); /* Enable rtc (starts increase the tick counter and second counter register) */ RTC_ResetClockTickCounter(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); RTC_CalibCounterCmd(LPC_RTC, DISABLE); /* Set current time for RTC */ // Current time is 8:00:00PM, 2009-04-24 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MINUTE, 0); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_HOUR, 20); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MONTH, 4); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_YEAR, 2009); RTC_SetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH, 24); /* Set ALARM time for second */ RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 10); // Get and print current time RTC_GetFullTime (LPC_RTC, &RTCFullTime); _DBG( "Current time set to: "); _DBD((RTCFullTime.HOUR)); _DBG (":"); _DBD ((RTCFullTime.MIN)); _DBG (":"); _DBD ((RTCFullTime.SEC)); _DBG(" "); _DBD ((RTCFullTime.DOM)); _DBG("/"); _DBD ((RTCFullTime.MONTH)); _DBG("/"); _DBD16 ((RTCFullTime.YEAR)); _DBG_(""); _DBG("Second ALARM set to "); _DBD (RTC_GetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND)); _DBG_("s"); /* Set the CIIR for second counter interrupt*/ RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); /* Set the AMR for 10s match alarm interrupt */ RTC_AlarmIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE); /* Enable RTC interrupt */ NVIC_EnableIRQ(RTC_IRQn); /* Loop forever */ while(1); return 1; }
int main(void) { //Variable Declarations initSolenoid(); /* initialize solenoid valve */ TIM_TIMERCFG_Type timerCfg; initTimeStruct(); RTC_TIME_Type* watertime = malloc(sizeof(RTC_TIME_Type)); uint8 fed = 0; uint8 watered = 0; watertime->HOUR = 5; watertime->MIN = 0; //Initialize timer0 for delays TIM_ConfigStructInit(TIM_TIMER_MODE, &timerCfg); /* initialize timer config struct */ TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timerCfg); /* initialize timer0 */ //Initialize Real Time Clock RTC_Init(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); RTC_ResetClockTickCounter(LPC_RTC); // Initialize Peripherals INIT_SDRAM(); /* initialize SDRAM */ servoInit(); /* initialize FSR servo motor for panning camera */ initStepper(); /* initialize stepper motor for dispensing food */ initFSR(); /* initialize force sensitive resistor circuit for food and water full signals */ initWiFi(AUTO_CONNECT); /* initialize WiFi module -- must be attached*/ audio_initialize(); audio_reset(); //audio_test(); audio_setupMP3(); int i = 0, retval; uint32 length; /* length variable for photo */ printf("Entering while loop\n\r"); //audio_storeVoice(); // Enter an infinite loop while(1) { if(STATE == DISPENSING_FOOD){ printf("Entering food dispense state\n\r"); /* Execute commands to dispense food */ //spinUntilFull(); spinStepper(300); reverseSpin(250); STATE = CONNECTED; } if(STATE == DISPENSING_WATER){ printf("Entering water dispense state\n\r"); /* Execute commands to dispense water */ fillWater(); STATE = CONNECTED; } if(STATE == CAPTURING){ printf("Entering camera taking state\n\r"); /* Initialize camera and set it up to take a picture */ if(cameraInit()) printf("Camera not initialized!\n\r"); retval = stopFrame(); length = getBufferLength(); printf("length: %i\n\r", length); /* Send length to Android application */ int temp_len = length; while(temp_len){ uart1PutChar(temp_len % 10); temp_len = temp_len / 10; } /* Send photo and finish set up */ getAndSendPhoto(length); resumeFrame(); STATE = CONNECTED; } if(STATE == TALKING1){ audio_playVoice(1); STATE = CONNECTED; } if(STATE == TALKING2){ audio_playVoice(2); STATE = CONNECTED; } if(STATE == TALKING3){ audio_playVoice(3); STATE = CONNECTED; } if(STATE == PAN_LEFT){ /* Execute commands to pan servo left */ panServo(LEFT); STATE = CONNECTED; } if(STATE == PAN_RIGHT){ /* Execute commands to pan servo right */ panServo(RIGHT); STATE = CONNECTED; } if(STATE == SCHEDULING){ /* Execute commands to schedule a feeding time */ STATE = CONNECTED; } /* Scheduling */ RTC_GetFullTime(LPC_RTC, time); //Fill water bowl at predetermined time if (time->HOUR == watertime->HOUR + 1 && watered == 1) watered = 0; if (watertime->HOUR == time->HOUR && watertime->MIN < time->MIN && watered == 0) { fillWater(); watered = 1; } //Feed dog on schedule if any cannot feed dog two consecutive hours for(i = 0; i < scheduled_feeds; i++) { if (time->HOUR == feedtime[i]->HOUR + 1 && fed == 1) fed = 0; if (feedtime[i]->HOUR == time->HOUR && feedtime[i]->MIN < time->MIN && fed == 0) { spinUntilFull(); fed = 1; } } } return 0; }
/********************************************************************************************************* * @brief Calendar program. * @retval None ********************************************************************************************************/ void Calendar(void) { CKCU_APBPerip1ClockConfig(CKCU_APBEN1_RTC, ENABLE); if(PWRCU_CheckReadyAccessed() != PWRCU_OK) { while(1); } /* Init LED3 and USART */ LED_Configuration(); USART_Configuration(); /* Enable NVIC RTC interrupt */ NVIC_EnableIRQ(RTC_IRQn); /* Check if the Power On Reset flag is set */ if(PWRCU_GetFlagStatus() == PWRCU_FLAG_BAKPOR) { printf("\r\n\n Power On Reset occurred...."); } if(PWRCU_ReadBackupRegister(PWRCU_BAKREG_0) != 0x5A5A) { u32 wInitTime = 0; /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("\r\n\n RTC not yet configured...."); /* RTC Configuration */ RTC_Configuration(); printf("\r\n RTC configured...."); /* Adjust time by values entred by the user on the hyperterminal, Then store the init time to PWRCU_BAKREG_1. */ wInitTime = Time_Regulate() ; PWRCU_WriteBackupRegister(PWRCU_BAKREG_1, wInitTime); /* Reset RTC Counter when Time is 23:59:59 */ RTC_SetCompare(86400 - wInitTime) ; PWRCU_WriteBackupRegister(PWRCU_BAKREG_0, 0x5A5A); /* Enable RTC */ RTC_Cmd(ENABLE) ; } else { printf("\r\n No need to configure RTC...."); } /* Display current time in infinite loop */ printf("\n\r"); while (1) { /* If 1s has paased */ if(gwTimeDisplay == 1) { /* Display current time. Current time is sum of the RTC counter value and the init time(stored in PWRCU_BAKREG_1 register). The init time (PWRCU_BAKREG_1 register) will be clear if the RTC Match flag(CMFLAG) is set. Refer to RTC_IRQHandler. */ Time_Display(RTC_GetCounter() + PWRCU_ReadBackupRegister(PWRCU_BAKREG_1)); gwTimeDisplay = 0; } } }