void platform_rtc_init(void)
{
  RTC_InitTypeDef RTC_InitStruct;
  
  RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
  
  /* RTC ticks every second */
  RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
  RTC_InitStruct.RTC_SynchPrediv = 0xFF;
  
  /* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
  /* RTC clock source configuration ------------------------------------------*/
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
  
  RTC_Init( &RTC_InitStruct );
  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  
  /* RTC configuration -------------------------------------------------------*/
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  
#ifdef USE_RTC_BKP
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) {
    /* set it to 12:20:30 08/04/2013 monday */
    MicoRtcSetTime(&mico_default_time);
    RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP);
  }
#else
  /* write default application time inside rtc */
  MicoRtcSetTime(&mico_default_time);
#endif
  
}
Пример #2
0
//================================
static int rtc_set( lua_State* L )
{
  mico_rtc_time_t time;
  uint16_t yr;
  
  if (!lua_istable(L, 1)) {
    l_message( NULL, "table arg needed" );
    lua_pushinteger(L,0);
    return 1;
  }
  if (lua_objlen( L, 1 ) != 7) {
    l_message( NULL, "sec,min,hour,wday,date,month,year expected" );
    lua_pushinteger(L,0);
    return 1;
  }
  lua_rawgeti( L, 1, 1 );
  time.sec = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 1, 2 );
  time.min = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 1, 3 );
  time.hr = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 1, 4 );
  time.weekday = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 1, 5 );
  time.date = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 1, 6 );
  time.month = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 1, 7 );
  yr = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  if (yr >= 2000) yr = yr - 2000;
  time.year = (uint8_t)yr;

  if( MicoRtcSetTime(&time) == kNoErr ){
    lua_pushinteger(L,1);
  }else {
    l_message( NULL, "RTC unsupported"); 
    lua_pushinteger(L,0);
  }  
  return 1;
}
Пример #3
0
static int rtc_set( lua_State* L )
{
   mico_rtc_time_t time;
   
   time.sec = luaL_checkinteger( L, 1 );
   time.min = luaL_checkinteger( L, 2 );
   time.hr = luaL_checkinteger( L, 3 );
   time.weekday = luaL_checkinteger( L, 4 );
   time.date = luaL_checkinteger( L, 5 );
   time.month = luaL_checkinteger( L, 6 );
   time.year = luaL_checkinteger( L, 7 );

   if( MicoRtcSetTime(&time) == kNoErr ){
    lua_pushstring(L,"OK");
   }else {
     lua_pushstring(L, "RTC function unsupported"); 
   }  
  return 1;
}
void RTC_Wakeup_init(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;
  RTC_InitTypeDef RTC_InitStruct;
  
  RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
  
  /* RTC ticks every second */
  RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
  RTC_InitStruct.RTC_SynchPrediv = 0xFF;
  
  RTC_Init( &RTC_InitStruct );
  
  /* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
  /* RTC clock source configuration ------------------------------------------*/
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
#ifdef USE_RTC_BKP
  PWR_BackupRegulatorCmd(ENABLE);
#endif
  
  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  
  /* RTC configuration -------------------------------------------------------*/
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  
  RTC_WakeUpCmd( DISABLE );
  EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE );
  PWR_ClearFlag(PWR_FLAG_WU);
  RTC_ClearFlag(RTC_FLAG_WUTF);
  
  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div2);
  
  EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE );
  EXTI_InitStructure.EXTI_Line = RTC_INTERRUPT_EXTI_LINE;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  
  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  RTC_ITConfig(RTC_IT_WUT, DISABLE);
  
  /* Prepare Stop-Mode but leave disabled */
  PWR_ClearFlag(PWR_FLAG_WU);
  PWR->CR |= PWR_CR_LPDS;
  PWR->CR &= (unsigned long)(~(PWR_CR_PDDS));
  SCB->SCR |= ((unsigned long)SCB_SCR_SLEEPDEEP_Msk);
  
#ifdef USE_RTC_BKP
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) {
    /* set it to 12:20:30 08/04/2013 monday */
    MicoRtcSetTime(&mico_default_time);
    RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP);
  }
#else
  //#ifdef RTC_ENABLED
  /* application must have wiced_application_default_time structure declared somewhere, otherwise it wont compile */
  /* write default application time inside rtc */
  MicoRtcSetTime(&mico_default_time);
  //#endif /* RTC_ENABLED */
#endif
  
}
Пример #5
0
void NTPClient_thread(void *arg)
{
  ntp_log_trace();
  OSStatus err = kUnknownErr;
  UNUSED_PARAMETER( arg );
  
  int  Ntp_fd = -1;
  fd_set readfds;
  struct timeval_t t ;
  struct sockaddr_t addr;
  socklen_t addrLen;	
  char ipstr[16];
  unsigned int trans_sec, current;
  struct NtpPacket outpacket ,inpacket;
  struct tm *currentTime;
  mico_rtc_time_t time;
  LinkStatusTypeDef wifi_link;
  int contry = 0;
  
  /* Regisist notifications */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler, NULL );
  require_noerr( err, exit ); 
 
  memset(&outpacket,0x0,sizeof(outpacket));
  memset(&inpacket,0x0,sizeof(inpacket));

  outpacket.flags = NTP_Flags;
  outpacket.stratum = NTP_Stratum;
  outpacket.poll = NTP_Poll;
  outpacket.precision = NTP_Precision;
  outpacket.root_delay = NTP_Root_Delay;
  outpacket.root_dispersion = NTP_Root_Dispersion;
  
  err = micoWlanGetLinkStatus( &wifi_link );
  require_noerr( err, exit );

  if( wifi_link.is_connected == true )
    _wifiConnected = true;
  
  if(_wifiConnected == false)
    mico_rtos_get_semaphore(&_wifiConnected_sem, MICO_WAIT_FOREVER);
  
  Ntp_fd = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
  require_action(IsValidSocket( Ntp_fd ), exit, err = kNoResourcesErr );
  addr.s_ip = INADDR_ANY; 
  addr.s_port = 45000;
  err = bind(Ntp_fd, &addr, sizeof(addr));
  err = kNoErr;
  require_noerr(err, exit);

   contry=0;
   while(1) {
     //err = gethostbyname((char *)NTP_Server, (uint8_t *)ipstr, 16);
     err = gethostbyname(NTP_Server, (uint8_t *)ipstr, 16);
     contry+=1;
     if (contry > 4) { require_noerr(err, exit); }
     else { require_noerr(err, ReConnWithDelay); }
     ntp_log("NTP server address: %s",ipstr);
     break;

   ReConnWithDelay:
     mico_thread_sleep(5);
   }

  addr.s_ip = inet_addr(ipstr);
  addr.s_port = NTP_Port;

  t.tv_sec = 5;
  t.tv_usec = 0;
  
  while(1) {
    require_action(sendto(Ntp_fd, &outpacket,sizeof(outpacket), 0, &addr, sizeof(addr)), exit, err = kNotWritableErr);

    FD_ZERO(&readfds);
    FD_SET(Ntp_fd, &readfds);

    select(1, &readfds, NULL, NULL, &t);
    
    if(FD_ISSET(Ntp_fd, &readfds))
    {
      require_action(recvfrom(Ntp_fd, &inpacket, sizeof(struct NtpPacket), 0, &addr, &addrLen)>=0, exit, err = kNotReadableErr);
      trans_sec = inpacket.trans_ts_sec;
      trans_sec = ntohl(trans_sec);
      current = trans_sec - UNIX_OFFSET + (ntp_time_zone*3600);
      ntp_log("Time Synchronised, %s, tz=%d, from %s\n\r",asctime(gmtime(&current)),ntp_time_zone, NTP_Server);

      //currentTime = localtime(&current);
      currentTime = gmtime(&current);
      time.sec = currentTime->tm_sec;
      time.min = currentTime->tm_min ;
      time.hr = currentTime->tm_hour;

      time.date = currentTime->tm_mday;
      time.weekday = currentTime->tm_wday;
      time.month = currentTime->tm_mon + 1;
      time.year = (currentTime->tm_year + 1900)%100;
      MicoRtcSetTime( &time );
      goto exit;
    }
  }
exit:
    if( err!=kNoErr )ntp_log("Exit: NTP client exit with err = %d", err);
    mico_system_notify_remove( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler );
    if(_wifiConnected_sem) mico_rtos_deinit_semaphore(&_wifiConnected_sem);
    SocketClose(&Ntp_fd);
    mico_rtos_delete_thread(NULL);
    return;
}