I'm trying to access the system time from the ULP. My idea for later is to have the main SoC get the real world time via NTP and store into the RTC slow memory whatever information required to allow the ULP to compute the real world time from the RTC Timer/RTC_SLOW_CLK.
But I'm already having trouble to read the RTC Timer in ULP. I put a minimal example on GitHub: https://github.com/heiko-r/esp32_ulp_timer
In short, this is my main.c:
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp32/ulp.h"
#include "ulp_main.h"
extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
void app_main(void)
{
printf("Hello world!\n");
/* Load ULP binary */
esp_err_t err = ulp_load_binary(0, ulp_main_bin_start,
(ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
ESP_ERROR_CHECK(err);
/* Set ULP wake up period to T = 20ms. */
ulp_set_wakeup_period(0, 20000);
/* Start ULP program */
err = ulp_run(&ulp_entry - RTC_SLOW_MEM);
ESP_ERROR_CHECK(err);
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
while (true) {
vTaskDelay(xDelay);
printf("time_reg_1: %10d\n", ulp_time_reg_1 & UINT16_MAX);
printf("time_reg_2: %10d\n", ulp_time_reg_2 & UINT16_MAX);
printf("time_reg_3: %10d\n", ulp_time_reg_3 & UINT16_MAX);
}
}
Code: Select all
#include "soc/rtc_cntl_reg.h"
#include "soc/soc_ulp.h"
/* Define variables to go into the .bss section (zero-initialised data) */
.bss
.global time_reg_1
time_reg_1:
.long 0
.global time_reg_2
time_reg_2:
.long 0
.global time_reg_3
time_reg_3:
.long 0
/* Code goes into the .text section */
.text
.global entry
entry:
READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16)
MOVE R2, time_reg_1
ST R0, R2, 0
REG_RD 0x3FF48010, 31, 16
MOVE R2, time_reg_2
ST R0, R2, 0
REG_RD 0x3FF48014, 15, 0
MOVE R2, time_reg_3
ST R0, R2, 0
HALT
Code: Select all
Hello world!
time_reg_1: 54298
time_reg_2: 0
time_reg_3: 0
time_reg_1: 54298
time_reg_2: 0
time_reg_3: 0
time_reg_1: 54298
time_reg_2: 0
time_reg_3: 0