void getTime(void) { TM_RTC_Time_t datatime; TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); sprintf(buffer,"Current time: %02d:%02d:%02d, %02d.%02d.%02dr.\n\r", datatime.hours, datatime.minutes, datatime.seconds, datatime.date, datatime.month, datatime.year); TM_USART_Puts(MENU_USART,buffer); }
void setDate(void) { TM_RTC_Time_t time; int tmp=0; TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN); tmp = cmdlineGetArgInt(1); if (tmp > 0 && tmp < 32) time.date=tmp; tmp = cmdlineGetArgInt(2); if (tmp > 0 && tmp < 13) time.month=tmp; tmp = cmdlineGetArgInt(3); if (tmp >= 0 && tmp <= 99) time.year=tmp; TM_RTC_SetDateTime(&time, TM_RTC_Format_BIN); conf_bit(SET,SETTINGS_1,DATE_SET); TM_USART_Puts(MENU_USART,"Date has been sucessfully set!\n\r"); }
void setTime(void) { TM_RTC_Time_t time; int tmp=0; TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN); tmp = cmdlineGetArgInt(1); if (tmp > 0 && tmp < 25) time.hours=tmp; tmp = cmdlineGetArgInt(2); if (tmp > 0 && tmp < 61) time.minutes=tmp; tmp = cmdlineGetArgInt(3); if (tmp > 0 && tmp < 61) time.seconds=tmp; TM_RTC_SetDateTime(&time, TM_RTC_Format_BIN); conf_bit(SET,SETTINGS_1,TIME_SET); TM_USART_Puts(MENU_USART,"Time has been sucessfully set!\n\r"); }
/* Called on wakeup interrupt */ void TM_RTC_RequestHandler() { /* Get time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); /* Format time */ sprintf(buf, "%02d.%02d.%04d %02d:%02d:%02d Unix: %u\n", datatime.date, datatime.month, datatime.year + 2000, datatime.hours, datatime.minutes, datatime.seconds, datatime.unix ); /* Send to USART */ TM_USART_Puts(USART3, buf); /* Toggle LED */ TM_DISCO_LedToggle(LED_RED | LED_GREEN); }
void show_sys (void){ config_t conf; TM_RTC_Time_t datatime; conf_load(&conf); conf.phone[GC_PHONE_LENGTH]=0; TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); printf("\n\r------------------System data------------------\n\r"); printf("ID: %d\n\r", conf.id[0]); printf("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\r",conf.mac[0], conf.mac[1], conf.mac[2], conf.mac[3], conf.mac[4], conf.mac[5]); printf("Local IP: %d.%d.%d.%d, port: %d\n\r", conf.local_ip[0], conf.local_ip[1], conf.local_ip[2], conf.local_ip[3], conf.local_port[0]); printf("Remote IP: %d.%d.%d.%d, port: %d\n\r", conf.remote_ip[0], conf.remote_ip[1], conf.remote_ip[2], conf.remote_ip[3], conf.remote_port[0]); printf("Current time: %02d:%02d:%02d\n\r", datatime.hours, datatime.minutes, datatime.seconds); printf("Current date: %02d.%02d.%02dr.\n\r", datatime.date, datatime.month, datatime.year); printf("Central phone number: %s\n\r",conf.phone); printf("Battery voltage: %2.3fV\n\r",GC_BatVoltage()); printf("Latitude: %2.6f\n\r",conf.position_lat.f); printf("Longitude: %2.6f\n\r",conf.position_long.f); show_ddmi(); show_temp_humidity(); }
/** * Write data to SD card * Name: SDCard_writeData * * Description: If the SD card is detected, it is mounted and the correct file is opened * The string is created using the time from the RTC, the classification Value and the data * The file is then appended to include the new line of text * * Arguments: data_type type - enumerated type to determine what type of data is to be written * uint32_t data[] - data array * const char * classificationValue - String of classification result * * Returns: void */ void SDCard_writeData(data_type type, uint32_t data [], const char *classificationValue) { char str[codebookSize * codebookSize * 3] = {0}, fileName[14], time[25]; char contents[codebookSize * codebookSize * 3] = {0}; uint32_t count; uint32_t cnt; int i; /* Only attempt the write function if SD card is inserted */ if (SDCardInserted){ /* Get time and date */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); sprintf(time, "%02d.%02d.%04d %02d:%02d:%02d ", datatime.date, datatime.month, datatime.year + 2000, datatime.hours, datatime.minutes, datatime.seconds ); /* Set filename */ if (type == SMatrix_type){ sprintf(fileName, "SD:SMatrix.txt"); } /* Mount SD card and open file */ if ((fres = f_mount(&fatFs, "SD:", 0)) == FR_OK){ /* If file does not exist then create it */ if ((fres = f_open(&fil, fileName, FA_READ | FA_WRITE)) == FR_NO_FILE){ fres = f_open(&fil, fileName, FA_CREATE_ALWAYS | FA_READ | FA_WRITE); } /* If file does exist navigate to the end of the file */ else { f_read(&fil, str, sizeof(str), &cnt); f_lseek(&fil, cnt); } if (fres != FR_OK){ sprintf(str, "Error Message @%d\n\r", fres); TM_USART_Puts(USART1, str); } else { /* S Matrix data transmission */ if (type == SMatrix_type){ /* Populate string with S Matrix values */ for (i = 0; i < codebookSize; i++){ if (i == 0) { sprintf(contents, "%s%s%u, ", time, classificationValue, ((uint32_t *)data)[i]); } else if (i == 27) { sprintf(contents, "%s%u\n\r", contents, ((uint32_t *)data)[i]); } else { sprintf(contents, "%s%u, ", contents, ((uint32_t *)data)[i]); } } /* Append end of the file */ count = f_puts(contents, &fil); } /* Put to USART */ TM_USART_Puts(USART1, contents); sprintf(str, "Writing is done. Written %d bytes. Error = %d\n\r", count, fres); TM_USART_Puts(USART1, str); f_close(&fil); f_mount(NULL, "SD:", 1); } } else { sprintf(str, "Error Message @%d\n\r", fres); /* Put to USART */ TM_USART_Puts(USART1, str); } } }
uint32_t TM_RTC_Init(TM_RTC_ClockSource_t source) { uint32_t status; TM_RTC_t datatime; /* Set instance */ hRTC.Instance = RTC; hRTC.Init.AsynchPrediv = RTC_ASYNC_PREDIV; hRTC.Init.SynchPrediv = RTC_SYNC_PREDIV; hRTC.Init.HourFormat = RTC_HOURFORMAT_24; hRTC.Init.OutPut = RTC_OUTPUT_DISABLE; hRTC.Init.OutPutType = RTC_OUTPUT_TYPE_PUSHPULL; hRTC.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; /* Enable PWR peripheral clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* Allow access to BKP Domain */ HAL_PWR_EnableBkUpAccess(); /* Get RTC status */ status = HAL_RTCEx_BKUPRead(&hRTC, RTC_STATUS_REG); /* Check if RTC already initialized */ if (status == RTC_STATUS_TIME_OK) { /* Start internal clock if we choose internal clock */ if (source == TM_RTC_ClockSource_Internal) { TM_RTC_Config(TM_RTC_ClockSource_Internal); } /* Wait for RTC APB registers synchronisation (needed after start-up from Reset) */ HAL_RTC_WaitForSynchro(&hRTC); /* Get date and time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); /* Clear reset flags */ __HAL_RCC_CLEAR_RESET_FLAGS(); /* Return OK */ return 1; } else { /* Start RTC clock */ TM_RTC_Config(source); /* Set date */ RTC_DateStruct.Year = 0; RTC_DateStruct.Month = 1; RTC_DateStruct.Date = 1; RTC_DateStruct.WeekDay = RTC_WEEKDAY_TUESDAY; /* Set date */ HAL_RTC_SetDate(&hRTC, &RTC_DateStruct, RTC_FORMAT_BIN); /* Set time */ RTC_TimeStruct.Hours = 0x00; RTC_TimeStruct.Minutes = 0x00; RTC_TimeStruct.Seconds = 0x00; RTC_TimeStruct.TimeFormat = RTC_HOURFORMAT_24; RTC_TimeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; RTC_TimeStruct.StoreOperation = RTC_STOREOPERATION_RESET; /* Set time */ HAL_RTC_SetTime(&hRTC, &RTC_TimeStruct, RTC_FORMAT_BCD); /* Init RTC */ HAL_RTC_Init(&hRTC); /* Save data to backup regiser */ HAL_RTCEx_BKUPWrite(&hRTC, RTC_STATUS_REG, RTC_STATUS_TIME_OK); /* RTC was initialized now */ return 0; } }
uint32_t TM_RTC_Init(TM_RTC_ClockSource_t source) { uint32_t status; uint8_t stat = 1; TM_RTC_Time_t datatime; /* Enable RTC peripheral clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Get RTC status */ status = RTC_ReadBackupRegister(RTC_STATUS_REG); if (status == RTC_STATUS_TIME_OK) { TM_RTC_Status = RTC_STATUS_TIME_OK; /* Start internal clock if we choose internal clock */ if (source == TM_RTC_ClockSource_Internal) { TM_RTC_Config(TM_RTC_ClockSource_Internal); } /* Wait for RTC APB registers synchronisation (needed after start-up from Reset) */ RTC_WaitForSynchro(); /* Clear interrupt flags */ RTC_ClearITPendingBit(RTC_IT_WUT); EXTI_ClearITPendingBit(EXTI_Line22); /* Get date and time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); } else if (status == RTC_STATUS_INIT_OK) { TM_RTC_Status = RTC_STATUS_INIT_OK; /* Start internal clock if we choose internal clock */ if (source == TM_RTC_ClockSource_Internal) { TM_RTC_Config(TM_RTC_ClockSource_Internal); } /* Wait for RTC APB registers synchronisation (needed after start-up from Reset) */ RTC_WaitForSynchro(); /* Clear interrupt flags */ RTC_ClearITPendingBit(RTC_IT_WUT); EXTI_ClearITPendingBit(EXTI_Line22); /* Get date and time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); } else { TM_RTC_Status = RTC_STATUS_ZERO; /* Return status = 0 -> RTC Never initialized before */ stat = RTC_STATUS_ZERO; /* Config RTC */ TM_RTC_Config(source); /* Set date and time */ datatime.date = 1; datatime.day = 1; datatime.month = 1; datatime.year = 0; datatime.hours = 0; datatime.minutes = 0; datatime.seconds = 0; /* Set date and time */ TM_RTC_SetDateTime(&datatime, TM_RTC_Format_BIN); /* Initialized OK */ TM_RTC_Status = RTC_STATUS_INIT_OK; } /* If first time initialized */ if (stat == RTC_STATUS_ZERO) { return 0; } return TM_RTC_Status; }
int main(void) { /* Initialize system */ SystemInit(); /* Init USART6, TX: PC6 for debug */ TM_USART_Init(USART6, TM_USART_PinsPack_1, 115200); /* Enable watchdog, 4 seconds before timeout */ if (TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_4s)) { /* Report to user */ printf("Reset occured because of Watchdog\n"); } /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Initialize button */ TM_DISCO_ButtonInit(); /* Display to user */ printf("Program starting..\n"); /* Initialize RTC with internal clock if not already */ if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) { /* Set default time for RTC */ /* Set date and time if RTC is not initialized already */ TM_RTC_SetDateTimeString("28.02.15.6;23:35:30"); }; /* Initialize ethernet peripheral */ /* All parameters NULL, default options for MAC, static IP, gateway and netmask will be used */ /* They are defined in tm_stm32f4_ethernet.h file */ if (TM_ETHERNET_Init(NULL, NULL, NULL, NULL) == TM_ETHERNET_Result_Ok) { /* Successfully initialized */ TM_DISCO_LedOn(LED_GREEN); } else { /* Unsuccessfull communication */ TM_DISCO_LedOn(LED_RED); } /* Reset watchdog */ TM_WATCHDOG_Reset(); /* Initialize ethernet server if you want use it, server port 80 */ TM_ETHERNETSERVER_Enable(80); /* Set SSI tags, we have 21 SSI tags */ TM_ETHERNETSERVER_SetSSITags(SSI_Tags, 21); /* Set CGI tags, we have 1 CGI handler, for leds only */ TM_ETHERNETSERVER_SetCGIHandlers(CGI_Handlers, 1); /* Read RTC clock */ TM_RTC_GetDateTime(&RTC_Data, TM_RTC_Format_BIN); /* Print current time to USART */ printf("Current date: %02d:%02d:%02d\n", RTC_Data.hours, RTC_Data.minutes, RTC_Data.seconds); /* Reset watchdog */ TM_WATCHDOG_Reset(); while (1) { /* Update ethernet, call this as fast as possible */ TM_ETHERNET_Update(); /* If button pressed, toggle server status */ if (TM_DISCO_ButtonOnPressed()) { /* If server is enabled */ if (TM_ETHERNETSERVER_Enabled()) { /* Disable it */ TM_ETHERNETSERVER_Disable(); /* Print to user */ printf("Server disabled\n"); } else { /* Enable it */ TM_ETHERNETSERVER_Enable(80); /* Print to user */ printf("Server enabled\n"); } } /* Reset watchdog */ TM_WATCHDOG_Reset(); } }
/* SSI server callback, always is called this callback */ uint16_t TM_ETHERNETSERVER_SSICallback(int iIndex, char *pcInsert, int iInsertLen) { uint8_t status; /* Return number of characters written */ if (iIndex < 4) { /* First 4 tags are leds */ /* Get led status */ switch (iIndex) { case 0: /* Green LED */ status = TM_DISCO_LedIsOn(LED_GREEN); break; case 1: /* Orange LED */ status = TM_DISCO_LedIsOn(LED_ORANGE); break; case 2: /* Red LED */ status = TM_DISCO_LedIsOn(LED_RED); break; case 3: /* Blue LED */ status = TM_DISCO_LedIsOn(LED_BLUE); break; default: return 0; } /* Set string according to status */ if (status) { /* Led is ON */ sprintf(pcInsert, "<span class=\"green\">On</span>"); } else { /* Led is OFF */ sprintf(pcInsert, "<span class=\"red\">Off</span>"); } } else if (iIndex == 4) { /* #serv_adr tag is requested */ sprintf(pcInsert, "%d.%d.%d.%d", TM_ETHERNET_GetLocalIP(0), TM_ETHERNET_GetLocalIP(1), TM_ETHERNET_GetLocalIP(2), TM_ETHERNET_GetLocalIP(3)); } else if (iIndex == 5) { /* #clt_a_c tag */ sprintf(pcInsert, "%u", TM_ETHERNETCLIENT_GetConnectionsCount()); } else if (iIndex == 6) { /* #clt_s_c tag */ sprintf(pcInsert, "%u", TM_ETHERNETCLIENT_GetSuccessfullConnectionsCount()); } else if (iIndex == 7) { /* #clt_per tag */ if (TM_ETHERNETCLIENT_GetConnectionsCount() == 0) { strcpy(pcInsert, "0 %"); } else { sprintf(pcInsert, "%f %%", (float)TM_ETHERNETCLIENT_GetSuccessfullConnectionsCount() / (float)TM_ETHERNETCLIENT_GetConnectionsCount() * 100); } } else if (iIndex == 8) { /* #clt_tx tag */ sprintf(pcInsert, "%llu", TM_ETHERNETCLIENT_GetTXBytes()); } else if (iIndex == 9) { /* #clt_rx tag */ sprintf(pcInsert, "%llu", TM_ETHERNETCLIENT_GetRXBytes()); } else if (iIndex == 10) { /* #srv_c tag */ sprintf(pcInsert, "%u", TM_ETHERNETSERVER_GetConnectionsCount()); } else if (iIndex == 11) { /* #srv_tx tag */ sprintf(pcInsert, "%llu", TM_ETHERNETSERVER_GetTXBytes()); } else if (iIndex == 12) { /* #srv_rx tag */ sprintf(pcInsert, "%llu", TM_ETHERNETSERVER_GetRXBytes()); } else if (iIndex == 13) { /* #mac_adr */ sprintf(pcInsert, "%02X-%02X-%02X-%02X-%02X-%02X", TM_ETHERNET_GetMACAddr(0), TM_ETHERNET_GetMACAddr(1), TM_ETHERNET_GetMACAddr(2), TM_ETHERNET_GetMACAddr(3), TM_ETHERNET_GetMACAddr(4), TM_ETHERNET_GetMACAddr(5) ); } else if (iIndex == 14) { /* #gateway */ sprintf(pcInsert, "%d.%d.%d.%d", TM_ETHERNET_GetGateway(0), TM_ETHERNET_GetGateway(1), TM_ETHERNET_GetGateway(2), TM_ETHERNET_GetGateway(3) ); } else if (iIndex == 15) { /* #netmask */ sprintf(pcInsert, "%d.%d.%d.%d", TM_ETHERNET_GetNetmask(0), TM_ETHERNET_GetNetmask(1), TM_ETHERNET_GetNetmask(2), TM_ETHERNET_GetNetmask(3) ); } else if (iIndex == 16) { /* #link */ if (TM_ETHERNET_Is100M()) { strcpy(pcInsert, "100Mbit"); } else { strcpy(pcInsert, "10Mbit"); } } else if (iIndex == 17) { /* #duplex */ if (TM_ETHERNET_IsFullDuplex()) { strcpy(pcInsert, "Full"); } else { strcpy(pcInsert, "Half"); } } else if (iIndex == 18) { /* #hardware */ strcpy(pcInsert, "STM32F4-Discovery"); } else if (iIndex == 19) { /* #rtc_time */ TM_RTC_GetDateTime(&RTC_Data, TM_RTC_Format_BIN); sprintf(pcInsert, "%04d-%02d-%02d %02d:%02d:%02d", RTC_Data.year + 2000, RTC_Data.month, RTC_Data.date, RTC_Data.hours, RTC_Data.minutes, RTC_Data.seconds ); } else if (iIndex == 20) { /* #compiled */ strcpy(pcInsert, __DATE__ " at " __TIME__); } else { /* No valid tag */ return 0; } /* Return number of characters written in buffer */ return strlen(pcInsert); }
int rtc_get(void *data) { TM_RTC_GetDateTime((TM_RTC_t *)data); return 0; }