Example #1
0
/**
* This brings up enough clocks to allow the processor to run quickly while initialising memory.
* Other platform specific clock init can be done in init_platform() or init_architecture()
*/
WEAK void init_clocks( void ){
/** This brings up enough clocks to allow the processor to run quickly while initialising memory.
* Other platform specific clock init can be done in init_platform() or init_architecture() */
//LPC54xx clock initialized in SystemInit().
#ifdef BOOTLOADER

  LPC_SYSCTL->SYSAHBCLKCTRL[0] |= 0x00000018; // Magicoe
#if defined(__FPU_PRESENT) && __FPU_PRESENT == 1
	fpuInit();
#endif

#if defined(NO_BOARD_LIB)
	/* Chip specific SystemInit */
	Chip_SystemInit();
#else
	/* Enable RAM 2 clock */
	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SRAM2);
	/* Board specific SystemInit */
	Board_SystemInit(); //init pin muxing and clock.
#endif
      LPC_SYSCTL->SYSAHBCLKCTRL[0] |= 0x00000018; // Magicoe
      
      Chip_SYSCTL_PowerUp(PDRUNCFG_PD_IRC_OSC_EN|PDRUNCFG_PD_IRC_EN);
      /* Configure PIN0.21 as CLKOUT with pull-up, monitor the MAINCLK on scope */
      Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 21, IOCON_MODE_PULLUP | IOCON_FUNC1 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF);
      Chip_Clock_SetCLKOUTSource(SYSCTL_CLKOUTSRC_RTC, 1);
      Chip_Clock_EnableRTCOsc();
      Chip_RTC_Init(LPC_RTC);
      Chip_RTC_Enable1KHZ(LPC_RTC);
      Chip_RTC_Enable(LPC_RTC);
#endif
}
/* Initialize the RTC peripheral */
void Chip_RTC_Init(LPC_RTC_T *pRTC)
{
	Chip_Clock_RTCEnable();

	/* 2-Second delay after enabling RTC clock */
	LPC_ATIMER->DOWNCOUNTER = 2048;
	while (LPC_ATIMER->DOWNCOUNTER);

	/* Disable RTC */
	Chip_RTC_Enable(pRTC, DISABLE);

	/* Disable Calibration */
	Chip_RTC_CalibCounterCmd(pRTC, DISABLE);

	/* Reset RTC Clock */
	Chip_RTC_ResetClockTickCounter(pRTC);

	/* Clear counter increment and alarm interrupt */
	pRTC->ILR = RTC_IRL_RTCCIF | RTC_IRL_RTCALF;
	while (pRTC->ILR != 0) {}

	/* Clear all register to be default */
	pRTC->CIIR = 0x00;
	pRTC->AMR = 0xFF;
	pRTC->CALIBRATION = 0x00;
}
Example #3
0
/* common clock initialisation function
 * This brings up enough clocks to allow the processor to run quickly while initialising memory.
 * Other platform specific clock init can be done in init_platform() or init_host_mcu()
 */
WEAK void platform_init_system_clocks( void )
{

    /* CPU clock source starts with IRC */
    Chip_Clock_SetMainPllSource( SYSCTL_PLLCLKSRC_IRC );
    Chip_Clock_SetCPUClockSource( SYSCTL_CCLKSRC_SYSCLK );

    /* Enable main oscillator used for PLLs */
    LPC_SYSCTL->SCS = SYSCTL_OSCEC;
    while ( ( LPC_SYSCTL->SCS & SYSCTL_OSCSTAT ) == 0 )
    {
    }

    /* PLL0 clock source is 12MHz oscillator, PLL1 can only be the
     main oscillator */
    Chip_Clock_SetMainPllSource( SYSCTL_PLLCLKSRC_MAINOSC );

    /* Setup PLL0 for a 480MHz clock. It is divided by CPU Clock Divider to create CPU Clock.
     Input clock rate (FIN) is main oscillator = 12MHz
     FCCO is selected for PLL Output and it must be between 275 MHz to 550 MHz.
     FCCO = (2 * M * FIN) / N = integer multiplier of CPU Clock (120MHz) = 480MHz
     N = 1, M = 480 * 1/(2*12) = 20 */
    Chip_Clock_SetupPLL( SYSCTL_MAIN_PLL, PLL_M_CONSTANT - 1, PLL_N_CONSTANT - 1 );/* Multiply by PLL_M_CONSTANT, Divide by PLL_N_CONSTANT */

    /* Enable PLL0 */
    Chip_Clock_EnablePLL( SYSCTL_MAIN_PLL, SYSCTL_PLL_ENABLE );

    /* Change the CPU Clock Divider setting for the operation with PLL0.
     Divide value = (480/120) = 4 */
    Chip_Clock_SetCPUClockDiv( 3 ); /* pre-minus 1 */

    while ( !Chip_Clock_IsMainPLLLocked( ) )
    {
    }

    /* Connect PLL0 */
    Chip_Clock_EnablePLL( SYSCTL_MAIN_PLL, SYSCTL_PLL_ENABLE | SYSCTL_PLL_CONNECT );

    /* Wait for PLL0 to be connected */
    while ( !Chip_Clock_IsMainPLLConnected( ) )
    {
    }

    /* Setup FLASH access to 5 clocks (120MHz clock) */
    Chip_FMC_SetFLASHAccess( FLASHTIM_120MHZ_CPU );

    /* Enable peripheral base clocks*/
    Chip_Clock_SetPCLKDiv( SYSCTL_PCLK_SPI, SYSCTL_CLKDIV_1 );

    Chip_RTC_Enable( LPC_RTC, ENABLE );
    Chip_Clock_SetCLKOUTSource( SYSCTL_CLKOUTSRC_RTC, 1 );
    Chip_Clock_EnableCLKOUT( );
}
Example #4
0
void Board_RTC_setTime(uint32_t hr,uint32_t min, uint32_t sec, uint32_t day, uint32_t mon, uint32_t yr,uint32_t dayOfWeek)
{
        RTC_TIME_T rtc;
        rtc.time[RTC_TIMETYPE_SECOND] = sec;
        rtc.time[RTC_TIMETYPE_MINUTE] = min;
        rtc.time[RTC_TIMETYPE_HOUR] = hr;
        rtc.time[RTC_TIMETYPE_DAYOFMONTH] = day;
        rtc.time[RTC_TIMETYPE_MONTH] = mon;
        rtc.time[RTC_TIMETYPE_YEAR] = yr;
        rtc.time[RTC_TIMETYPE_DAYOFWEEK] = dayOfWeek;
        Chip_RTC_SetFullTime(LPC_RTC, &rtc);
        Chip_RTC_Enable(LPC_RTC, ENABLE);
}
Example #5
0
/**
 * @brief	Event router interrupt handler
 * @return	Nothing
 */
void EVRT_IRQHandler(void)
{
	if (Chip_EVRT_IsSourceInterrupting(EVRT_SRC_WAKEUP0)) {
		Chip_EVRT_ClrPendIntSrc(EVRT_SRC_WAKEUP0);
		NVIC_DisableIRQ(EVENTROUTER_IRQn);
	}

	if (Chip_EVRT_IsSourceInterrupting(EVRT_SRC_RTC)) {
		if (Chip_RTC_GetIntPending(LPC_RTC, RTC_INT_ALARM)) {
			Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_ALARM);
			Chip_EVRT_ClrPendIntSrc(EVRT_SRC_RTC);
			Chip_RTC_Enable(LPC_RTC, DISABLE);
			NVIC_DisableIRQ(EVENTROUTER_IRQn);
		}
	}
}
Example #6
0
void Board_RTC_calibration(uint32_t value)
{
	uint8_t calibDir = RTC_CALIB_DIR_FORWARD;
	if(value<0)
	{
		calibDir = RTC_CALIB_DIR_BACKWARD;
		value=value*(-1);
	}

	if(value>131072)
		value=131072;

	Chip_RTC_CalibConfig(LPC_RTC, value, calibDir);
	Chip_RTC_CalibCounterCmd(LPC_RTC, ENABLE);
	Chip_RTC_Enable(LPC_RTC, ENABLE);
}
/* Wecan Modify platform_rtc_init 2015.06.10 */
OSStatus platform_rtc_init(void)
{
#ifdef MICO_ENABLE_MCU_RTC
        /* CLKOUT = 32K_Osc */
	Chip_IOCON_PinMuxSet(LPC_IOCON, platform_gpio_pins[CLKOUT].port , platform_gpio_pins[CLKOUT].pin_number, (IOCON_MODE_PULLUP | IOCON_FUNC1 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF));
	Chip_Clock_SetCLKOUTSource(SYSCON_CLKOUTSRC_RTC, 1);

	/* Turn on the RTC 32K Oscillator */
	Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_32K_OSC);

	/* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate()	*/
	Chip_Clock_EnableRTCOsc();

	/* Initialize RTC driver (enables RTC clocking) */
	Chip_RTC_Init(LPC_RTC);

	/* Enable RTC as a peripheral wakeup event */
	//Chip_SYSCON_EnsableWakeup(SYSCON_STARTER_RTC);

	/* RTC reset */
	Chip_RTC_Reset(LPC_RTC);

	/* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */
	Chip_RTC_Disable(LPC_RTC);
        platform_rtc_set_time(&default_rtc_time);

	//Chip_RTC_SetCount(LPC_RTC, 0);

	/* Set a long alarm time so the interrupt won't trigger */
	//Chip_RTC_SetAlarm(LPC_RTC, (Chip_RTC_GetCount(LPC_RTC) + 5));

	/* Enable RTC and high resolution timer - this can be done in a single call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */
	//Chip_RTC_Enable1KHZ(LPC_RTC);
	Chip_RTC_Enable(LPC_RTC);

	/* Clear latched RTC interrupt statuses */
	Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));


  return kNoErr;
#else
  return kUnsupportedErr;
#endif
}
Example #8
0
// Set the current time given a time string in the format
// 2015-02-21 05:57:00
void setTime(char *timeStr){
	// Enable and configure Real-Time Clock
	Chip_Clock_EnableRTCOsc();
	Chip_RTC_Init(LPC_RTC);

	// Set time in human readable form
	struct tm tm;
	time_t t = 0;
	tm = *localtime(&t);
	sscanf(timeStr, "%u-%u-%u %u:%u:%u",
		&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday), &(tm.tm_hour), &(tm.tm_min), &(tm.tm_sec));
	tm.tm_year -= 1900;
	tm.tm_mon -= 1;
	tm.tm_isdst = 0; // No DST accounting
	t = mktime(&tm);

	Chip_RTC_Reset(LPC_RTC); // Set then clear SWRESET bit
	Chip_RTC_SetCount(LPC_RTC, t); // Set the time
	Chip_RTC_Enable(LPC_RTC); // Start counting
	log_string("Time Set");
}
/* Initialize the RTC peripheral */
void Chip_RTC_Init(LPC_RTC_T *pRTC)
{
	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_RTC);

	/* Disable RTC */
	Chip_RTC_Enable(pRTC, DISABLE);

	/* Disable Calibration */
	Chip_RTC_CalibCounterCmd(pRTC, DISABLE);

	/* Reset RTC Clock */
	Chip_RTC_ResetClockTickCounter(pRTC);

	/* Clear counter increment and alarm interrupt */
	pRTC->ILR = RTC_IRL_RTCCIF | RTC_IRL_RTCALF;
	while (pRTC->ILR != 0) {}

	/* Clear all register to be default */
	pRTC->CIIR = 0x00;
	pRTC->AMR = 0xFF;
	pRTC->CALIBRATION = 0x00;
}
Example #10
0
/*
 * @Brief: Configure RTC peripheral.
 * @param  RTC_t rtc: RTC structure
 * @return bool_t true (1) if config it is ok
 */
bool_t rtcConfig( RTC_t * rtc ){
   
   bool_t ret_val = 1;
   
   static bool_t init;
   RTC_TIME_T rtcTime;

   if( init ){
      /* Already initialized */
      ret_val = 0;
   } else {
      
      /* RTC Block section ------------------------- */
      Chip_RTC_Init(LPC_RTC);

      /* Set current time for RTC */
      /* Current time is 22:00:00 , 2016-07-02 */
      /*
      rtcTime.time[RTC_TIMETYPE_SECOND]     = 0;
      rtcTime.time[RTC_TIMETYPE_MINUTE]     = 0;
      rtcTime.time[RTC_TIMETYPE_HOUR]       = 22;
      rtcTime.time[RTC_TIMETYPE_DAYOFMONTH] = 2;
      rtcTime.time[RTC_TIMETYPE_MONTH]      = 7;
      rtcTime.time[RTC_TIMETYPE_YEAR]       = 2016;
      Chip_RTC_SetFullAlarmTime(LPC_RTC, &rtcTime);
      */
      rtcWrite( rtc );

      /* Enable rtc (starts increase the tick counter 
         and second counter register) */
      Chip_RTC_Enable(LPC_RTC, ENABLE);
      
      init = 1;
   }

   return ret_val;
}
Example #11
0
/**
 * @brief	Main program body
 * @return	int
 */
int main(void)
{
	CHIP_PMU_MCUPOWER_T crntPowerSetting;

	/* Setup SystemCoreClock and any needed board code */
	SystemCoreClockUpdate();
	Board_Init();
	Board_LED_Set(0, true);

	/* Clear any previously set deep power down and sleep flags */
	Chip_PMU_ClearSleepFlags(LPC_PMU, PMU_PCON_SLEEPFLAG | PMU_PCON_DPDFLAG);
	/* Enable the RTC oscillator, oscillator rate can be determined by
	   calling Chip_Clock_GetRTCOscRate() */
	Chip_Clock_EnableRTCOsc();

	/* Initialize RTC driver (enables RTC clocking) */
	Chip_RTC_Init(LPC_RTC);

	/* RTC reset */
	Chip_RTC_Reset(LPC_RTC);

	/* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you
	   need to disable it before setting the initial RTC count. */
	Chip_RTC_Disable(LPC_RTC);
	Chip_RTC_SetCount(LPC_RTC, 0);

	/* Set a long alarm time so the interrupt won't trigger */
	Chip_RTC_SetAlarm(LPC_RTC, 1000);

	/* Enable RTC */
	Chip_RTC_Enable(LPC_RTC);

	/* Clear latched RTC interrupt statuses */
	Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));

	/* Enable RTC interrupt */
	NVIC_EnableIRQ(RTC_ALARM_IRQn);

	/* Enable RTC alarm interrupt */
	Chip_RTC_EnableWakeup(LPC_RTC, RTC_CTRL_ALARMDPD_EN);

	/* Output example's activity banner */
	DEBUGSTR("\r\n");
	DEBUGSTR("-----------------------------------------------------------------\r\n");
#ifdef RESET_POWER_CYCLE_COUNT
	ProcessCycleCounter();
	DEBUGOUT("Power Control Example\r\n");
#else
	DEBUGOUT("Power Control Example   Cycle Count: %d\r\n", ProcessCycleCounter());
#endif
	DEBUGSTR("  System will cycle through SLEEP, DEEP SLEEP, POWER\r\n");
	DEBUGSTR("  DOWN, and DEEP POWER DOWN power states\r\n");
	DEBUGSTR("-----------------------------------------------------------------\r\n\r\n");

	/* Setup alarm, process next power state then wait for alarm to wake-up system */
	crntPowerSetting = PMU_MCU_SLEEP;
	while (1) {
		/* Set alarm to wakeup in POWER_CYCLE_SEC_DELAY seconds */
		Chip_RTC_SetAlarm(LPC_RTC, Chip_RTC_GetCount(LPC_RTC) + POWER_CYCLE_SEC_DELAY);

		/* Enter first (or next) power state */
		ProcessPowerState(crntPowerSetting);

		/* Inc current power setting and test for overflow */
		if (crntPowerSetting == PMU_MCU_DEEP_PWRDOWN) {
			/* Reset to lowest power setting */
			crntPowerSetting = PMU_MCU_SLEEP;
		}
		else {
			crntPowerSetting++;
		}
	}

	return 0;
}
Example #12
0
/**
 * @brief	Main program body
 * @return	int
 */
int main(void)
{
	uint32_t rtcCount;

	/* Starting time for this example used to set the RTC */
	struct tm startTime = {
		56,			/* tm_sec, seconds after the minute, 0 to 59 */
		59,			/* tm_min, minutes after the hour, 0 to 59 */
		23,			/* tm_hour, hours since midnight, 0 to 23 */
		25,			/* tm_mday, day of the month, 1 to 31 */
		9,			/* tm_mon, months since January, 0 to 11 */
		(2014 - TM_YEAR_BASE),	/* tm_year, years since base year (1900) */

		/* The following 3 fields are not used by the ConvertTimeRtc() function,
		   while the ConvertRtcTime() fucntion will generate data in them from an
		   RTC tick (except tm_isdst) */
		0,			/* tm_wday, days since Sunday, 0 to 6 */
		0,			/* tm_yday, days since January 1, 0 to 365 */
		0			/* tm_isdst, Daylight Savings Time flag */
	};

	/* Setup SystemCoreClock and any needed board code */
	SystemCoreClockUpdate();
	Board_Init();
	Board_LED_Set(0, false);

	/* Turn on the RTC 32K Oscillator */
	Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_32K_OSC);

	/* Enable the RTC oscillator, oscillator rate can be determined by
	   calling Chip_Clock_GetRTCOscRate()	*/
	Chip_Clock_EnableRTCOsc();

	/* Initialize RTC driver (enables RTC clocking) */
	Chip_RTC_Init(LPC_RTC);

	/* RTC reset */
	Chip_RTC_Reset(LPC_RTC);

	/* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you
	   need to disable it before setting the initial RTC count. */
	Chip_RTC_Disable(LPC_RTC);

	/* COnvert tm structure to RTC tick count */
	ConvertTimeRtc(&startTime, &rtcCount);
	Chip_RTC_SetCount(LPC_RTC, rtcCount);

	/* Set alarm to wakeup in 5s */
	DEBUGOUT("Setting alarm to trigger in 5s\r\n");
	rtcCount += 5;
	Chip_RTC_SetAlarm(LPC_RTC, rtcCount);

	/* Enable RTC */
	Chip_RTC_Enable(LPC_RTC);

	/* Clear latched RTC interrupt statuses */
	Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));

	/* Enable RTC interrupt */
	NVIC_EnableIRQ(RTC_IRQn);

	/* Enable RTC alarm interrupt */
	Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN));

	/* Sleep and do all the work in the RTC interrupt handler */
	while (1) {
		/* 10 high resolution ticks that get slower each tick */
		if (rtcAlarm) {
			/* Will not reset alarm */
			DEBUGOUT("Alarm triggered, alarm reset to trigger in 5s\r\n");
			rtcCount = Chip_RTC_GetCount(LPC_RTC) + 5;
			Chip_RTC_SetAlarm(LPC_RTC, rtcCount);
			rtcAlarm = false;
		}

		/* Show RTC time in UT format */
		showTime(Chip_RTC_GetCount(LPC_RTC));

		/* Sleep while waiting for alarm */
		__WFI();
	}
	return 0;
}
Example #13
0
File: rtc.c Project: 0xBADCA7/lk
/**
 * @brief	Main program body
 * @return	int
 */
int main(void)
{
	int stateCounter = 0;
	uint32_t ticks = 0;

	/* Setup SystemCoreClock and any needed board code */
	SystemCoreClockUpdate();
	Board_Init();

	/* Enable the RTC oscillator, oscillator rate can be determined by
	   calling Chip_Clock_GetRTCOscRate()	*/
	Chip_Clock_EnableRTCOsc();

	/* Initialize RTC driver (enables RTC clocking) */
	Chip_RTC_Init(LPC_RTC);

	/* Enable RTC as a peripheral wakeup event */
	Chip_SYSCTL_EnableERP1PeriphWakeup(SYSCTL_ERP1_WAKEUP_RTCALARMINT |
									   SYSCTL_ERP1_WAKEUP_RTCWAKEINT);

	/* RTC reset */
	Chip_RTC_Reset(LPC_RTC);

	/* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you
	   need to disable it before setting the initial RTC count. */
	Chip_RTC_Disable(LPC_RTC);
	Chip_RTC_SetCount(LPC_RTC, 0);

	/* Set a long alarm time so the interrupt won't trigger */
	Chip_RTC_SetAlarm(LPC_RTC, 1000);

	/* Enable RTC and high resolution timer - this can be done in a single
	   call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */
	Chip_RTC_Enable1KHZ(LPC_RTC);
	Chip_RTC_Enable(LPC_RTC);

	/* Clear latched RTC interrupt statuses */
	Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));

	/* Enable RTC wake and alarm interrupts */
	NVIC_EnableIRQ(RTC_ALARM_IRQn);
	NVIC_EnableIRQ(RTC_WAKE_IRQn);

	/* Enable RTC alarm interrupt */
	Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN));

	/* Sleep and do all the work in the RTC interrupt handler */
	while (1) {
		ticks = 0;
		DEBUGOUT("Tick number: %d, 1KHZ int:%d, alarm int:%d\r\n",
				 stateCounter, rtcWake, rtcAlarm);
		rtcWake = rtcAlarm = false;

		/* 10 high resolution ticks that get slower each tick */
		if (stateCounter < 10) {
			/* Wakeup in 300, 400, 500, etc. milliSeconds */
			Chip_RTC_SetWake(LPC_RTC, (300 + (stateCounter * 100)));

			stateCounter++;
		}
		else {
			DEBUGOUT("Setting alarm to wake up in 4s\r\n");

			/* Set alarm to wakeup in 4 seconds */
			Chip_RTC_SetAlarm(LPC_RTC, Chip_RTC_GetCount(LPC_RTC) + 4);

			stateCounter = 0;
		}

		Chip_SYSCTL_SetWakeup(~(SYSCTL_SLPWAKE_IRCOUT_PD | SYSCTL_SLPWAKE_IRC_PD |
								SYSCTL_SLPWAKE_FLASH_PD | SYSCTL_SLPWAKE_SYSOSC_PD | SYSCTL_SLPWAKE_SYSPLL_PD));
		Chip_SYSCTL_EnableERP1PeriphWakeup(SYSCTL_ERP1_WAKEUP_RTCALARMINT | SYSCTL_ERP1_WAKEUP_RTCWAKEINT);
		
		/* Delay to allow serial transmission to complete*/
		while(ticks++ < 10000) {}

		/* Enter MCU Deep Sleep mode */
		Chip_PMU_DeepSleepState(LPC_PMU);
	}

	return 0;
}
Example #14
0
/**
 * @brief	Main entry point
 * @return	Nothing
 */
int main(void)
{
	RTC_TIME_T FullTime;

	SystemCoreClockUpdate();
	Board_Init();

	fIntervalReached  = 0;
	fAlarmTimeMatched = 0;
	On0 = On1 = false;
	Board_LED_Set(2, false);

	DEBUGSTR("The RTC operates on a 1 Hz clock.\r\n" \
			 "Register writes can take up to 2 cycles.\r\n"	\
			 "It will take a few seconds to fully\r\n" \
			 "initialize it and start it running.\r\n\r\n");

	DEBUGSTR("We'll print a timestamp every 5 seconds.\r\n"	\
			 "...and another when the alarm occurs.\r\n");

	Chip_RTC_Init(LPC_RTC);

	/* Set current time for RTC 2:00:00PM, 2012-10-05 */
	FullTime.time[RTC_TIMETYPE_SECOND]  = 0;
	FullTime.time[RTC_TIMETYPE_MINUTE]  = 0;
	FullTime.time[RTC_TIMETYPE_HOUR]    = 14;
	FullTime.time[RTC_TIMETYPE_DAYOFMONTH]  = 5;
	FullTime.time[RTC_TIMETYPE_DAYOFWEEK]   = 5;
	FullTime.time[RTC_TIMETYPE_DAYOFYEAR]   = 279;
	FullTime.time[RTC_TIMETYPE_MONTH]   = 10;
	FullTime.time[RTC_TIMETYPE_YEAR]    = 2012;

	Chip_RTC_SetFullTime(LPC_RTC, &FullTime);

	/* Set ALARM time for 17 seconds from time */
	FullTime.time[RTC_TIMETYPE_SECOND]  = 17;
	Chip_RTC_SetFullAlarmTime(LPC_RTC, &FullTime);

	/* Set the RTC to generate an interrupt on each second */
	Chip_RTC_CntIncrIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC, ENABLE);

	/* Enable matching for alarm for second, minute, hour fields only */
	Chip_RTC_AlarmIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC | RTC_AMR_CIIR_IMMIN | RTC_AMR_CIIR_IMHOUR, ENABLE);

	/* Clear interrupt pending */
	Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE | RTC_INT_ALARM);

	/* Enable RTC interrupt in NVIC */
	NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);

	/* Enable RTC (starts increase the tick counter and second counter register) */
	Chip_RTC_Enable(LPC_RTC, ENABLE);

	/* Loop forever */
	while (1) {
		if (fIntervalReached) {	/* Every 5s */
			fIntervalReached = 0;

			On1 = (bool) !On1;
			Board_LED_Set(1, On1);

			/* read and display time */
			Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
			showTime(&FullTime);
		}

		if (fAlarmTimeMatched) {
			fAlarmTimeMatched = false;

			/* announce event */
			DEBUGSTR("ALARM triggered!\r\n");
			Board_LED_Set(2, true);

			/* read and display time */
			Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
			showTime(&FullTime);
		}
	}
}
Example #15
0
/**
 * @brief	Main program body
 * @return	int
 */
int main(void)
{
	int stateCounter = 0;

	/* Setup SystemCoreClock and any needed board code */
	SystemCoreClockUpdate();
	Board_Init();
	Board_LED_Set(0, false);

	/* Turn on the RTC 32K Oscillator by clearing the power down bit */
	if ( !Is_Chip_SYSCTL_PowerUp(PDRUNCFG_PD_32K_OSC) ) {
		Chip_SYSCTL_PowerUp(PDRUNCFG_PD_32K_OSC);
	}

	/* Enable the RTC oscillator, oscillator rate can be determined by
	   calling Chip_Clock_GetRTCOscRate()	*/
	Chip_Clock_EnableRTCOsc();

	/* Initialize RTC driver (enables RTC clocking) */
	Chip_RTC_Init(LPC_RTC);

	/* Enable RTC as a peripheral wakeup event */
	Chip_SYSCTL_EnableWakeup(STARTERP0_RTC);

	/* RTC reset */
	Chip_RTC_Reset(LPC_RTC);

	/* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you
	   need to disable it before setting the initial RTC count. */
	Chip_RTC_Disable(LPC_RTC);
	Chip_RTC_SetCount(LPC_RTC, 0);

	/* Set a long alarm time so the interrupt won't trigger */
	Chip_RTC_SetAlarm(LPC_RTC, 1000);

	/* Enable RTC and high resolution timer - this can be done in a single
	   call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */
	Chip_RTC_Enable1KHZ(LPC_RTC);
	Chip_RTC_Enable(LPC_RTC);

	/* Clear latched RTC interrupt statuses */
	Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));

	/* Enable RTC interrupt */
	NVIC_EnableIRQ(RTC_IRQn);

	/* Enable RTC alarm interrupt */
	Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN));

	/* Sleep and do all the work in the RTC interrupt handler */
	while (1) {
		DEBUGOUT("Tick number: %d, 1KHZ int:%d, alarm int:%d\r\n",
				 stateCounter, rtcWake, rtcAlarm);
		rtcWake = rtcAlarm = false;

		/* 10 high resolution ticks that get slower each tick */
		if (stateCounter < 10) {
			/* Wakeup in 300, 400, 500, etc. milliSeconds */
			Chip_RTC_SetWake(LPC_RTC, (300 + (stateCounter * 100)));
			stateCounter++;
		}
		else {
			DEBUGOUT("Setting alarm to wake up in 4s\r\n");
			/* Set alarm to wakeup in 4 seconds */
			Chip_RTC_SetAlarm(LPC_RTC, Chip_RTC_GetCount(LPC_RTC) + 4);
			stateCounter = 0;
		}
		// FIXME - Use ROM API's to put chip to power down modes
		/* LPC_PWRD_API->power_mode_configure(PMU_DEEP_POWERDOWN, PDRUNCFG_PD_32K_OSC, 1); */
		__WFI();
	}
	return 0;
}
/**
 * @brief	main routine for example_lwip_tcpecho_freertos_18xx43xx
 * @return	Function should not exit
 */
int main(void)
{
	prvSetupHardware();

	Chip_RTC_Init();

	/*Set current time for RTC 8:59:20PM, 2016-04-11 MONDAY 102*/
	FullTime.time[RTC_TIMETYPE_SECOND]  	= 40;
	FullTime.time[RTC_TIMETYPE_MINUTE]  	= 59;
	FullTime.time[RTC_TIMETYPE_HOUR]    	= 8;
	FullTime.time[RTC_TIMETYPE_DAYOFMONTH] 	= 11;
	FullTime.time[RTC_TIMETYPE_DAYOFWEEK]	= 1;
	FullTime.time[RTC_TIMETYPE_DAYOFYEAR]	= 102;
	FullTime.time[RTC_TIMETYPE_MONTH]   	= 04;
	FullTime.time[RTC_TIMETYPE_YEAR]    	= 2016;

	Chip_RTC_SetFullTime(&FullTime);

	/* Set the RTC to generate an interrupt on each second */
	Chip_RTC_CntIncrIntConfig(RTC_AMR_CIIR_IMSEC, ENABLE);

	/* Enable matching for alarm for second, minute, hour fields only */
	Chip_RTC_AlarmIntConfig(RTC_AMR_CIIR_IMSEC | RTC_AMR_CIIR_IMMIN | RTC_AMR_CIIR_IMHOUR, ENABLE);

	/* Enable RTC interrupt in NVIC */
	NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);

	/* Enable RTC (starts increase the tick counter and second counter register) */
	Chip_RTC_Enable(ENABLE);


	//DIFFERENZA TRA GIORNI
	IP_RTC_TIME_T data1 = FullTime, data2, diffDate;
	uint32_t nGiorniDifferenza;
	data2.time[RTC_TIMETYPE_SECOND]  	= 40;
	data2.time[RTC_TIMETYPE_MINUTE]  	= 59;
	data2.time[RTC_TIMETYPE_HOUR]    	= 7;//23;//8;
	data2.time[RTC_TIMETYPE_DAYOFMONTH] = 12;
	data2.time[RTC_TIMETYPE_DAYOFWEEK]	= 2;
	data2.time[RTC_TIMETYPE_DAYOFYEAR]	= 103;
	data2.time[RTC_TIMETYPE_MONTH]   	= 04;
	data2.time[RTC_TIMETYPE_YEAR]    	= 2016;//2017;

	diffDate = differenzaDate(data2, data1);

	nGiorniDifferenza = differenzaGiorni(data2, data1);

	uint8_t counter = 0;
	ptrMedic = inizializzaListaMed();
	for(counter = 0; counter < maxR; counter++){
		if (l_tabellaMedicine[counter][65]==0) { //se piĆ¹ volte al giorno == 0
			if (l_tabellaMedicine[counter][59]==0) {

				ptrMedic = inserisciInTestaListaMed(ptrMedic, l_tabellaMedicine[counter]);

			} else {
				if (l_tabellaMedicine[counter][58] == 0) { //DA ASSUMERE

					ptrMedic = inserisciInTestaListaMed(ptrMedic, l_tabellaMedicine[counter]);

					l_tabellaMedicine[counter][58] ++;
				} else {
					l_tabellaMedicine[counter][58] ++;
					if (l_tabellaMedicine[counter][58] = l_tabellaMedicine[counter][59]) {
						l_tabellaMedicine[counter][58] = 0;
					}

				}
			}
		} else {
			//conta quante volte prendere la medicina
			if (l_tabellaMedicine[counter][65]==1) {
				//controlla la ripetizione
				Chip_RTC_GetFullTime(&FullTime);
				IP_RTC_TIME_T dataIn;
				dataIn.time[RTC_TIMETYPE_DAYOFMONTH] = l_tabellaMedicine[counter][43];
				dataIn.time[RTC_TIMETYPE_MONTH] = l_tabellaMedicine[counter][44];
				dataIn.time[RTC_TIMETYPE_YEAR] = (l_tabellaMedicine[counter][45]<<8) | l_tabellaMedicine[counter][46];

				int oraInizio = l_tabellaMedicine[counter][51];
				int intervalloOre = l_tabellaMedicine[counter][57];
				if (FullTime.time[RTC_TIMETYPE_DAYOFMONTH]==dataIn.time[RTC_TIMETYPE_DAYOFMONTH] && FullTime.time[RTC_TIMETYPE_MONTH]==dataIn.time[RTC_TIMETYPE_MONTH] && FullTime.time[RTC_TIMETYPE_YEAR]==dataIn.time[RTC_TIMETYPE_YEAR]) {
					//E' il giorno di inizio e lascio l'ora iniziale invariata
					oraInizio = oraInizio;
				} else {
					int go = 1;
					while (go){
						if ((oraInizio-intervalloOre)>=0) {
							oraInizio = oraInizio-intervalloOre;
						} else {
							go = 0;
						}
					}
				}
				int temp = oraInizio;
				int contatore = 1;
				while ((temp+intervalloOre) < 24) {
					temp = temp+intervalloOre;
					contatore++;
				}
				int c = 0;
				IP_RTC_TIME_T oraMed;
				oraMed.time[RTC_TIMETYPE_HOUR] = oraInizio;
				oraMed.time[RTC_TIMETYPE_MINUTE] = l_tabellaMedicine[counter][52];
				oraMed.time[RTC_TIMETYPE_SECOND] = l_tabellaMedicine[counter][53];
				for (c=0; c<contatore; c++) {

					ptrMedic = inserisciInTestaListaMedOra(ptrMedic, l_tabellaMedicine[counter], oraMed);

					oraMed.time[RTC_TIMETYPE_HOUR] += intervalloOre;
				}

			} else {
				//orari custom
				int n = l_tabellaMedicine[counter][65];
				int c = 0;
				IP_RTC_TIME_T oraMed;
				oraMed.time[RTC_TIMETYPE_HOUR] = l_tabellaMedicine[counter][51];
				oraMed.time[RTC_TIMETYPE_MINUTE] = l_tabellaMedicine[counter][52];
				oraMed.time[RTC_TIMETYPE_SECOND] = l_tabellaMedicine[counter][53];

				ptrMedic = inserisciInTestaListaMedOra(ptrMedic, l_tabellaMedicine[counter], oraMed);

				for (c=0; c<n-1; c++) {
					oraMed.time[RTC_TIMETYPE_HOUR] = l_tabellaMedicine[counter][66+3*c];
					oraMed.time[RTC_TIMETYPE_MINUTE] = l_tabellaMedicine[counter][67+3*c];
					oraMed.time[RTC_TIMETYPE_SECOND] = l_tabellaMedicine[counter][68+3*c];

					ptrMedic = inserisciInTestaListaMedOra(ptrMedic, l_tabellaMedicine[counter], oraMed);

				}

			}
		}
	}
	ptrMedic = ordinamento_lista_med(ptrMedic);

	//Inizio dall'orario attuale e non considero tutti i medicinali precedenti in giornata
	IP_RTC_TIME_T oraAllarme;
	oraAllarme.time[RTC_TIMETYPE_HOUR]  = ptrMedic->oraA;
	oraAllarme.time[RTC_TIMETYPE_MINUTE]  = ptrMedic->minA;
	oraAllarme.time[RTC_TIMETYPE_SECOND]  = ptrMedic->secA;
	while (orarioDopoOrario(FullTime, oraAllarme)) {
		ptrMedic = ptrMedic->next;
		oraAllarme.time[RTC_TIMETYPE_HOUR]  = ptrMedic->oraA;
		oraAllarme.time[RTC_TIMETYPE_MINUTE]  = ptrMedic->minA;
		oraAllarme.time[RTC_TIMETYPE_SECOND]  = ptrMedic->secA;
	}

	FullTime.time[RTC_TIMETYPE_HOUR]  = oraAllarme.time[RTC_TIMETYPE_HOUR];//ptrMedic->oraA;
	FullTime.time[RTC_TIMETYPE_MINUTE]  = oraAllarme.time[RTC_TIMETYPE_MINUTE];//ptrMedic->minA;
	FullTime.time[RTC_TIMETYPE_SECOND]  = oraAllarme.time[RTC_TIMETYPE_SECOND];//ptrMedic->secA;
	Chip_RTC_SetFullAlarmTime(&FullTime);


//	CICLO FOR PER LEGGERE TUTTI I BIT DI UN BYTE
//	unsigned bit=0, n_bits = 4u, *bits = (unsigned*)malloc(sizeof(unsigned) * n_bits);
//	for (bit = 0; bit < n_bits; ++bit, input >>1)
//		bits[bit] = input & 1;


	uint8_t i;
	for (i=0;i<35;i++){
		//char t = (char)m1[4+i];
		//nomeMed[i]=t;
		arrNomiMed[i]=l_tabellaMedicine[0][4+i];
	}
	nomeMed=&arrNomiMed;



	vSemaphoreCreateBinary( xSemaDataAvail );
	vSemaphoreCreateBinary( xSemaGUIend );

/*	xTaskCreate(vTSCTask, (signed char *) "vTSCTask",
				configMINIMAL_STACK_SIZE, NULL, TASK_PRIO_TOUCHSCREEN,
				(xTaskHandle *) NULL); */

/* BASIC ONE
	xTaskCreate(vLcdTask, (signed char *) "vLCDTask",
				configMINIMAL_STACK_SIZE * 4, NULL, TASK_PRIO_LCD,
				(xTaskHandle *) NULL); */

	xTaskCreate(vLcdTaskNew, (signed char *) "vLCDTaskNew",
				configMINIMAL_STACK_SIZE * 4, NULL, TASK_PRIO_LCD,
				(xTaskHandle *) NULL);

	/* Add another thread for initializing physical interface. This
	   is delayed from the main LWIP initialization. */
/*	xTaskCreate(vSetupIFTask, (signed char *) "SetupIFx",
				configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 1UL),
				(xTaskHandle *) NULL); */

	xTaskCreate(vSetupPollTask, (signed char *) "SetupPoll",
				configMINIMAL_STACK_SIZE*4, NULL, (tskIDLE_PRIORITY + 1UL),
				(xTaskHandle *) NULL);

/*
	xTaskCreate(vRTCTask, (signed char *) "RTCTask",
				configMINIMAL_STACK_SIZE*4, NULL, (tskIDLE_PRIORITY + 2UL),
				(xTaskHandle *) NULL);
*/

	/* Start the scheduler */
	vTaskStartScheduler();

	/* Should never arrive here */
	return 1;
}
Example #17
0
/**
 * Power State handler function
 */
static void PMC_PwrState_Handler(uint8_t buffer, uint8_t Wake_RTC)
{
	CHIP_EVRT_SRC_T Evrt_Src;
	CHIP_PMC_PWR_STATE_T Pwr_state;
	uint8_t confirm = 0xFF;

	if (Wake_RTC) {
		/* Configure EVRT_SRC_RTC as wake up signal */
		Evrt_Src = EVRT_SRC_RTC;

		/* Disable interrupt signal from Evrt_Src pin to EVRT */
		Chip_EVRT_SetUpIntSrc(Evrt_Src, DISABLE);

		/* Initialize and configure RTC */
		Chip_RTC_Init(LPC_RTC);
		Chip_RTC_ResetClockTickCounter(LPC_RTC);
		Chip_RTC_SetTime(LPC_RTC, RTC_TIMETYPE_SECOND, 0);

		/* Set alarm time = RTC_ALARM_TIME seconds.
		 * So after each RTC_ALARM_TIME seconds, RTC will generate and wake-up system
		 */
		Chip_RTC_SetAlarmTime(LPC_RTC, RTC_TIMETYPE_SECOND, RTC_ALARM_TIME);
		Chip_RTC_CntIncrIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC, DISABLE);

		/* Set the AMR for RTC_ALARM_TIME match alarm interrupt */
		Chip_RTC_AlarmIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC, ENABLE);
		Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_ALARM);
	}
	else {
		Evrt_Src = EVRT_SRC_WAKEUP0;
	}

	/* Configure wake up signal */
	PMC_Evrt_Configure(Evrt_Src);

	/* Get confirmation from user to continue
	 * Print wake up signal information to user
	 */
	DEBUGOUT(menu3);
	while ( (confirm != 'C') && (confirm != 'c')) {
		confirm = DEBUGIN();
	}

	switch (buffer) {
	case '1':
		DEBUGOUT("Entering 'Sleep' state ...\r\n");
		if (Wake_RTC) {
			DEBUGOUT("Wait for %d seconds, RTC alarm will wake up from 'Sleep' mode \r\n", RTC_ALARM_TIME);
			Chip_RTC_Enable(LPC_RTC, ENABLE);
		}
		else {
			DEBUGOUT("Press WAKEUP0 button/Connect WAKEUP0 pin to 3.3V to exit 'Sleep' mode \r\n");
		}
		Chip_PMC_Sleep();
		Chip_RTC_DeInit(LPC_RTC);
		DEBUGOUT("Woken up \r\n");
		break;

	case '2':
		DEBUGOUT("Entering 'Deep Sleep' state ...\r\n");
		if (Wake_RTC) {
			DEBUGOUT("Wait for %d seconds, RTC alarm will wake up from 'Deep Sleep' mode \r\n", RTC_ALARM_TIME);
			Chip_RTC_Enable(LPC_RTC, ENABLE);
		}
		else {
			DEBUGOUT("Press WAKEUP0 button/Connect WAKEUP0 pin to 3.3V to exit 'Deep Sleep' mode \r\n");
		}
		Pwr_state = PMC_DeepSleep;

		/* Call Pre SleepPowerDown function */
		PMC_Pre_SleepPowerDown();

		/* Goto Deep Sleep mode */
		Chip_PMC_Set_PwrState(Pwr_state);

		/* Call Post Wake up Initialisation function */
		PMC_Post_Wakeup(buffer);
		DEBUGOUT("\r\nWoken up \r\n");
		break;

	case '3':
		DEBUGOUT("Entering 'Power Down' state ...\r\n");
		if (Wake_RTC) {
			DEBUGOUT("Wait for %d seconds, RTC alarm will wake up from 'Power Down' mode \r\n", RTC_ALARM_TIME);
			Chip_RTC_Enable(LPC_RTC, ENABLE);
		}
		else {
			DEBUGOUT("Press WAKEUP0 button/Connect WAKEUP0 pin to 3.3V to exit 'Power Down' mode \r\n");
		}
		Pwr_state = PMC_PowerDown;

		/* Call Pre SleepPowerDown function */
		PMC_Pre_SleepPowerDown();

		/* Goto Deep Sleep mode */
		Chip_PMC_Set_PwrState(Pwr_state);

		/* Call Post Wake up Initialisation function */
		PMC_Post_Wakeup(buffer);

		DEBUGOUT("\r\nWoken up \r\n");
		break;

	case '4':
		DEBUGOUT("Entering 'Deep Power Down' state ...\r\n");
		if (Wake_RTC) {
			DEBUGOUT("Wait for %d seconds, RTC alarm will wake up from 'Deep Power Down' mode \r\n", RTC_ALARM_TIME);
			Chip_RTC_Enable(LPC_RTC, ENABLE);
		}
		else {
			DEBUGOUT("Press WAKEUP0 button/Connect WAKEUP0 pin to 3.3V to exit 'Deep Power Down' mode \r\n");
		}
		Pwr_state = PMC_DeepPowerDown;

		/* Call Pre SleepPowerDown function */
		PMC_Pre_SleepPowerDown();

		/* Goto Deep Sleep mode */
		Chip_PMC_Set_PwrState(Pwr_state);

		/* Wake up from Deep power down state is as good as RESET */
		while (1) {}
		break;

	default:
		break;
	}
}