예제 #1
0
파일: rtc.c 프로젝트: SergeyPopovGit/MICO
//====================================
static int rtc_standby( lua_State* L )
{
  char buff[60];

  uint8_t mode = luaL_checkinteger( L, 1 );
  if (mode > 1) {
    l_message( NULL, "mode has to be 0 or 1" );
    return 0;
  }
  if (mode==1 && use_wwdg == 0) {
    l_message(NULL,"IWDG active, cannot enter STOP mode."); 
    return 0;
  }
  int nsec = luaL_checkinteger( L, 2 );
  if ((nsec < 1) || (nsec > 84559)) {
    l_message(NULL,"wrong interval (1~84599)"); 
    return 0;
  }

  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);

  platform_rtc_time_t time;
  uint32_t currentSecond;
  RTC_AlarmTypeDef  RTC_AlarmStructure;

  platform_rtc_get_time(&time);
  currentSecond = time.hr*3600 + time.min*60 + time.sec;
  currentSecond += nsec;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_HourFormat_24;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = currentSecond/3600%24;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = currentSecond/60%60;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = currentSecond%60;
  RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay ;

  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);

  // Enable RTC Alarm A Interrupt
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  // Enable the Alarm A
  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
  /* Clear Alarm A pending bit */
  /* Clear RTC Alarm Flag */ 
  RTC_ClearFlag(RTC_FLAG_ALRAF);
  RTC_ClearFlag(RTC_IT_ALRA);

  if (mode == 0) sprintf(buff,"Going to STANDBY MODE...\r\n");
  else if (mode == 1) sprintf(buff,"Going to STOP MODE...\r\n");
  l_message(NULL,buff);
  sprintf(buff,"Wake up in %d second(s)\r\n", nsec);
  l_message(NULL,buff);

  //mico_rtos_suspend_all_thread();
  if (mode == 0) {
    PWR_EnterSTANDBYMode();
    // RESET
  }
  else if (mode == 1) {
    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
    // restore clocks
    init_clocks();
  }
  //mico_rtos_resume_all_thread();

  // *** Back from stop ***
  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);
  luaWdgReload();

  l_message(NULL,"Back from power save mode.");
  return 0;
}
예제 #2
0
파일: rtc.c 프로젝트: SergeyPopovGit/MICO
//=========================================
static int rtc_standbyUntil( lua_State* L )
{
  uint8_t h,m,s;
  RTC_AlarmTypeDef  RTC_AlarmStructure;
  char buff[64];
  
  uint8_t mode = luaL_checkinteger( L, 1 );
  if (mode > 1) {
    l_message( NULL, "mode has to be 0 or 1" );
    return 0;
  }
  if (mode==1 && use_wwdg == 0) {
    l_message(NULL,"IWDG active, cannot enter STOP mode."); 
    return 0;
  }
  
  if (!lua_istable(L, 2)) {
    l_message( NULL, "table arg needed" );
    return 0;
  }
  if (lua_objlen( L, 2 ) != 3) {
    l_message( NULL, "hour,minute,second expected" );
    return 0;
  }
  lua_rawgeti( L, 2, 1 );
  h = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 2, 2 );
  m = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 2, 3 );
  s = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );

  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);

  RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_HourFormat_24;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = h;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = m;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = s;
  RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay ;

  RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
  /* Disable the Alarm A */
  RTC_ITConfig(RTC_IT_ALRA, DISABLE);
  /* Clear RTC Alarm Flag */ 
  RTC_ClearFlag(RTC_FLAG_ALRAF);
  RTC_ClearFlag(RTC_IT_ALRA);

  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);

  /* Enable RTC Alarm A Interrupt: this Interrupt will wake-up the system from
     STANDBY mode (RTC Alarm IT not enabled in NVIC) */
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);

  /* Enable the Alarm A */
  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);

  if (mode == 0) sprintf(buff,"Going to STANDBY MODE...\r\n");
  else if (mode == 1) sprintf(buff,"Going to STOP MODE...\r\n");
  l_message(NULL,buff);
  sprintf(buff,"Wake up at %02d:%02d:%02d\r\n", RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours, RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes, RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds);
  l_message(NULL,buff);

  //mico_rtos_suspend_all_thread();
  if (mode == 0) {
    PWR_EnterSTANDBYMode();
  }
  else if (mode == 1) {
    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
    init_clocks();
  }
  //mico_rtos_resume_all_thread();

  // *** Back from stop ***
  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);
  luaWdgReload();

  l_message(NULL,"Back from power save mode.");
  return 0;
}
예제 #3
0
void TM_RTC_SetAlarm(TM_RTC_Alarm_t Alarm, TM_RTC_AlarmTime_t* DataTime, TM_RTC_Format_t format) {
	RTC_AlarmTypeDef RTC_AlarmStruct;
	
	/* Disable alarm first */
	TM_RTC_DisableAlarm(Alarm);
	
	/* Set RTC alarm settings */
	/* Set alarm time */
	RTC_AlarmStruct.RTC_AlarmTime.RTC_Hours = DataTime->hours;
	RTC_AlarmStruct.RTC_AlarmTime.RTC_Minutes = DataTime->minutes;
	RTC_AlarmStruct.RTC_AlarmTime.RTC_Seconds = DataTime->seconds;
	RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
	
	/* Alarm type is every week the same day in a week */
	if (DataTime->alarmtype == TM_RTC_AlarmType_DayInMonth) {
		/* Alarm trigger every week the same day in a week */
		RTC_AlarmStruct.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_WeekDay;
		
		/* Week day can be between 1 and 7 */
		if (DataTime->day == 0) {
			RTC_AlarmStruct.RTC_AlarmDateWeekDay = 1;
		} else if (DataTime->day > 7) {
			RTC_AlarmStruct.RTC_AlarmDateWeekDay = 7;
		} else {
			RTC_AlarmStruct.RTC_AlarmDateWeekDay = DataTime->day;
		}
	} else { /* Alarm type is every month the same day */
		/* Alarm trigger every month the same day in a month */
		RTC_AlarmStruct.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
	
		/* Month day can be between 1 and 31 */
		if (DataTime->day == 0) {
			RTC_AlarmStruct.RTC_AlarmDateWeekDay = 1;
		} else if (DataTime->day > 31) {
			RTC_AlarmStruct.RTC_AlarmDateWeekDay = 31;
		} else {
			RTC_AlarmStruct.RTC_AlarmDateWeekDay = DataTime->day;
		}
	}

	switch (Alarm) {
		case TM_RTC_Alarm_A:		
			/* Configure the RTC Alarm A */
			RTC_SetAlarm(format, RTC_Alarm_A, &RTC_AlarmStruct);
		
			/* Enable Alarm A */
			RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
			
			/* Enable Alarm A interrupt */
			RTC_ITConfig(RTC_IT_ALRA, ENABLE);
		
			/* Clear Alarm A pending bit */
			RTC_ClearFlag(RTC_IT_ALRA);
			break;
		case TM_RTC_Alarm_B:
			/* Configure the RTC Alarm B */
			RTC_SetAlarm(format, RTC_Alarm_B, &RTC_AlarmStruct);
		
			/* Enable Alarm B */
			RTC_AlarmCmd(RTC_Alarm_B, ENABLE);
		
			/* Enable Alarm B interrupt */
			RTC_ITConfig(RTC_IT_ALRB, ENABLE);
		
			/* Clear Alarm B pending bit */
			RTC_ClearFlag(RTC_IT_ALRB);
			break;
		default:
			break;
	}
}