void _cleanEasyLinkResource( mico_Context_t * const inContext ) { (void)inContext; if(easylinkClient_fd != -1) SocketClose(&easylinkClient_fd); /*module should power down under default setting*/ MICORemoveNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler ); MICORemoveNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler ); MICORemoveNotification( mico_notify_EASYLINK_WPS_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); MICORemoveNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); MICORemoveNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)EasyLinkNotify_SYSWillPowerOffHandler ); mico_rtos_deinit_semaphore(&easylink_sem); easylink_sem = NULL; }
void _cleanEasyLinkResource( mico_Context_t * const inContext ) { if(inContext->micoStatus.easylinkClient_fd!=-1) close(inContext->micoStatus.easylinkClient_fd); /*module should power down under default setting*/ MICORemoveNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler ); MICORemoveNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler ); MICORemoveNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); MICORemoveNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); mico_rtos_deinit_semaphore(&inContext->micoStatus.easylink_sem); inContext->micoStatus.easylink_sem = NULL; if(httpHeader) free(httpHeader); mico_stop_timer(&_Led_EL_timer); }
OSStatus host_platform_bus_deinit( void ) { OSStatus result; uint32_t a; result = mico_rtos_deinit_semaphore( &sdio_transfer_finished_semaphore ); platform_mcu_powersave_disable(); /* Disable SPI and SPI DMA */ sdio_disable_bus_irq( ); SDIO_ClockCmd( DISABLE ); SDIO_SetPowerState( SDIO_PowerState_OFF ); SDIO_DeInit( ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_SDIO, DISABLE ); #ifdef SDIO_1_BIT platform_gpio_deinit( &wifi_sdio_pins[WIFI_PIN_SDIO_IRQ] ); platform_gpio_irq_disable( &wifi_sdio_pins[WIFI_PIN_SDIO_IRQ] ); #endif for ( a = 0; a < WIFI_PIN_SDIO_MAX; a++ ) { platform_gpio_deinit( &wifi_sdio_pins[ a ] ); } #if defined ( MICO_WIFI_USE_GPIO_FOR_BOOTSTRAP ) platform_gpio_deinit( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_0] ); platform_gpio_deinit( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_1] ); #endif /* Turn off SDIO IRQ */ NVIC_DisableIRQ( SDIO_IRQ_CHANNEL ); NVIC_DisableIRQ( DMA2_3_IRQ_CHANNEL ); platform_mcu_powersave_enable(); return result; }
/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ uint32_t GetLSIFrequency(void) { NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_ClocksTypeDef RCC_ClockFreq; mico_rtos_init_semaphore(&_measureLSIComplete_SEM, 1); /* Enable the LSI oscillator ************************************************/ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} /* TIM5 configuration *******************************************************/ /* Enable TIM5 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); /* Connect internally the TIM5_CH4 Input Capture to the LSI clock output */ TIM_RemapConfig(TIM5, TIM5_LSI); /* Configure TIM5 presclaer */ TIM_PrescalerConfig(TIM5, 0, TIM_PSCReloadMode_Immediate); /* TIM5 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM5 CH4 The Rising edge is used as active edge, The TIM5 CCR4 is used to compute the frequency value ------------------------------------------------------------ */ TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM5, &TIM_ICInitStructure); /* Enable TIM5 Interrupt channel */ NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable TIM5 counter */ TIM_Cmd(TIM5, ENABLE); /* Reset the flags */ TIM5->SR = 0; /* Enable the CC4 Interrupt Request */ TIM_ITConfig(TIM5, TIM_IT_CC4, ENABLE); /* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler()) *********/ mico_rtos_get_semaphore(&_measureLSIComplete_SEM, MICO_WAIT_FOREVER); mico_rtos_deinit_semaphore( &_measureLSIComplete_SEM ); _measureLSIComplete_SEM = NULL; /* Deinitialize the TIM5 peripheral registers to their default reset values */ TIM_ITConfig(TIM5, TIM_IT_CC4, DISABLE); TIM_DeInit(TIM5); NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8; NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStructure); /* Compute the LSI frequency, depending on TIM5 input clock frequency (PCLK1)*/ /* Get SYSCLK, HCLK and PCLKx frequency */ RCC_GetClocksFreq(&RCC_ClockFreq); /* Get PCLK1 prescaler */ if ((RCC->CFGR & RCC_CFGR_PPRE1) == 0) { /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */ return ((RCC_ClockFreq.PCLK1_Frequency / PeriodValue) * 8); } else { /* PCLK1 prescaler different from 1 => TIMCLK = 2 * PCLK1 */ return (((2 * RCC_ClockFreq.PCLK1_Frequency) / PeriodValue) * 8) ; } }
OSStatus platform_uart_deinit( platform_uart_driver_t* driver ) { uint8_t uart_number; OSStatus err = kNoErr; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ), exit, err = kParamErr); uart_number = platform_uart_get_port_number( driver->peripheral->port ); /* Disable USART */ USART_Cmd( driver->peripheral->port, DISABLE ); /* Deinitialise USART */ USART_DeInit( driver->peripheral->port ); /************************************************************************** * De-initialise STM32 DMA and interrupt **************************************************************************/ /* Deinitialise DMA streams */ DMA_DeInit( driver->peripheral->tx_dma_config.stream ); DMA_DeInit( driver->peripheral->rx_dma_config.stream ); /* Disable TC (transfer complete) interrupt at the source */ DMA_ITConfig( driver->peripheral->tx_dma_config.stream, DMA_INTERRUPT_FLAGS, DISABLE ); DMA_ITConfig( driver->peripheral->rx_dma_config.stream, DMA_INTERRUPT_FLAGS, DISABLE ); /* Disable transmit DMA interrupt at Cortex-M3 */ NVIC_DisableIRQ( driver->peripheral->tx_dma_config.irq_vector ); /************************************************************************** * De-initialise STM32 USART interrupt **************************************************************************/ USART_ITConfig( driver->peripheral->port, USART_IT_RXNE, DISABLE ); /* Disable UART interrupt vector on Cortex-M3 */ NVIC_DisableIRQ( driver->peripheral->rx_dma_config.irq_vector ); /* Disable registers clocks */ uart_peripheral_clock_functions[uart_number]( uart_peripheral_clocks[uart_number], DISABLE ); #ifndef NO_MICO_RTOS mico_rtos_deinit_semaphore( &driver->rx_complete ); mico_rtos_deinit_semaphore( &driver->tx_complete ); mico_rtos_deinit_mutex( &driver->tx_mutex ); #else driver->rx_complete = false; driver->tx_complete = false; #endif driver->rx_size = 0; driver->tx_size = 0; driver->last_transmit_result = kNoErr; driver->last_receive_result = kNoErr; exit: platform_mcu_powersave_enable(); return err; }
void NTPClient_thread(void *inContext) { ntp_log_trace(); OSStatus err = kUnknownErr; mico_Context_t *Context = inContext; 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; socklen_t sockLen; char timeString[40]; /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler ); 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; if(_wifiConnected == false){ mico_rtos_get_semaphore(&_wifiConnected_sem, MICO_WAIT_FOREVER); mico_thread_msleep(50); } 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); while(1) { err = gethostbyname(NTP_Server, (uint8_t *)ipstr, 16); 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_log("Time Synchronoused, %s",asctime(localtime(¤t))); PlatformRTCWrite( localtime(¤t) ); goto exit; } } exit: if( err!=kNoErr )ntp_log("Exit: NTP client exit with err = %d", err); MICORemoveNotification( 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; }
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(¤t)),ntp_time_zone, NTP_Server); //currentTime = localtime(¤t); currentTime = gmtime(¤t); 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; }