diff --git a/part2_architecture/lab1-2_rtc/main/main.c b/part2_architecture/lab1-2_rtc/main/main.c index 3c13be8..4699bb0 100644 --- a/part2_architecture/lab1-2_rtc/main/main.c +++ b/part2_architecture/lab1-2_rtc/main/main.c @@ -6,64 +6,133 @@ ****************************************************************************/ /** - * @file lab2-1_main.c + * @file lab1-2-1_main.c * @author Fabrice Muller * @date 12 Sep. 2020 - * @brief File containing the lab2-1 of Part 2. + * @brief File containing the lab1-2 of Part 2. * * @see https://github.com/fmuller-pns/esp32-vscode-project-template */ #include +#include #include +#include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_spi_flash.h" #include "esp32/clk.h" -#include "soc/cpu.h" -#include "esp_heap_caps.h" #include "esp_log.h" +#include "sys/time.h" +#include "soc/rtc.h" +#include "soc/cpu.h" + +static const char* TAG = "SYSTIME"; + /** * @brief Starting point function * */ -void app_main(void) { - /* Print chip information */ +void app_main(void) +{ + /* Print RTC information */ esp_chip_info_t chip_info; esp_chip_info(&chip_info); - printf("=== FLASH Memory ===\n"); + printf("RTC clock frequency : %f KHz\n", (float)(rtc_clk_slow_freq_get_hz()/1000.0F)); - int sizeOfFlash = spi_flash_get_chip_size() / (1024 * 1024); - if (chip_info.features & CHIP_FEATURE_EMB_FLASH) - printf(" - Embedded Flash (%d MiB)\n", sizeOfFlash); - else - printf(" - External Flash (%d MiB)\n", sizeOfFlash); + /***********************************************/ + /* System time - gettimeofday() function */ + printf("\nSYSTEM DATE/TIME\n"); - printf("=== DRAM/IRAM Memory ===\n"); + struct timeval tv1,tv2,tv3,tv4 = {0}; - int size = heap_caps_get_free_size(MALLOC_CAP_EXEC); - printf("DRAM run executable code = %d Bytes (%d KiB)\n", size, size/1024); + if (gettimeofday(&tv1, NULL) != 0) { + ESP_LOGE(TAG, "gettimeofday() failed!"); + return; + } + ets_delay_us(1000); + gettimeofday(&tv2, NULL); + ets_delay_us(10000); + gettimeofday(&tv3, NULL); + struct timeval tv = {.tv_sec = 197130, .tv_usec=0}; + struct timezone utc = {0,0}; + settimeofday(&tv, &utc); + gettimeofday(&tv4, NULL); - size = heap_caps_get_free_size(MALLOC_CAP_32BIT); - printf("DRAM run executable code 32 bit = %d Bytes (%d KiB)\n", size, size/1024); + /* Display times and diff time */ + // TO DO + printf("Tv 1 : %ld sec and Tv1 %ld usec\n", tv1.tv_sec, tv1.tv_usec); + printf("Tv 1 : %ld sec and Tv2 %ld usec\n", tv1.tv_sec, tv2.tv_usec); + printf("Tv 1 : %ld sec and Tv3 %ld usec\n", tv1.tv_sec, tv3.tv_usec); + printf("Tv 1 : %ld sec and Tv4 %ld usec\n", tv1.tv_sec, tv4.tv_usec); - /* Free Heap size */ - int DRam = heap_caps_get_free_size(MALLOC_CAP_8BIT); - int IRam = heap_caps_get_free_size(MALLOC_CAP_32BIT) - heap_caps_get_free_size(MALLOC_CAP_8BIT); - int DRAMLargestFreeBlock = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); - printf("IRAM = %d bytes (%d KiB)\n", IRam, IRam/1024); - printf("DRAM (Total) = %d Bytes (%d KiB)\n", DRam, DRam/1024); - size = xPortGetFreeHeapSize(); - printf("DRAM Free Heap Size : %d Bytes (%d KiB)\n", size, size/1024); + /***********************************************/ + /* System time - time.h library */ + + static const char* DATE_TIME_FORMAT = "%a %d %b %Y %H:%M:%S %Z"; + + /* Get Current time (first solution) */ + time_t now = 0; + time(&now); + + /* Display date/time (first solution) */ + struct tm timeinfo = {0}; + localtime_r(&now, &timeinfo); + printf("The local date and time is: %s\n", asctime(&timeinfo)); + + /* Waiting for 6 seconds */ + printf("Waiting for 6 seconds ...\n"); + usleep(6000000); + + /* Get Current time (second solution) */ + now = time(NULL); + + /* Display date/time (second solution) */ + localtime_r(&now, &timeinfo); + char strftime_buf[32]; + strftime(strftime_buf, sizeof(strftime_buf), DATE_TIME_FORMAT, &timeinfo); + printf("SYSTEM date/time: %s\n", strftime_buf); + + /* Set New Date/Time */ + timeinfo.tm_year = 2020 - 1900; /* Year - 1900. */ + timeinfo.tm_mon = 9 - 1; /* Month. [0-11] */ + timeinfo.tm_mday = 19; /* Day. [1-31] */ + timeinfo.tm_hour = 9; /* Hours. [0-23] */ + timeinfo.tm_min = 12; /* Minutes. [0-59] */ + timeinfo.tm_sec = 29; /* Seconds. [0-60] */ + timeinfo.tm_isdst = 0; /* DST. (Daylight saving time) [-1/0/1] */ + time_t newTime = mktime(&timeinfo); + if(newTime == -1 ) { + ESP_LOGE(TAG, "Unable to make time using mktime()\n"); + return; + } + struct timeval tv_now = {.tv_sec = newTime, .tv_usec=0}; + struct timezone utc_now = {0,0}; + settimeofday(&tv_now, &utc_now); + + /* Get Current time (first solution) and display it (first solution) */ + time(&now); + localtime_r(&now, &timeinfo); + printf("The NEW local date and time is: %s\n", asctime(&timeinfo)); + + /* Waiting for 10 seconds */ + usleep(10000000); + + /* Get Current time (first solution) and display it (first solution) */ + time(&now); + localtime_r(&now, &timeinfo); + printf("The local date and time is: %s\n", asctime(&timeinfo)); + + /* Using gettimeofday() function */ + gettimeofday(&tv_now, NULL); + printf("tv_sec: %ld sec., tv_usec: %ld us\n", (long)tv_now.tv_sec, (long)tv_now.tv_usec); - printf("DRAM (Largest free block) = %d Bytes (%d KiB)\n", DRAMLargestFreeBlock, DRAMLargestFreeBlock/1024); - } -