Page 2 of 5
Re: ULP global variables
Posted: Wed Oct 04, 2017 12:43 pm
by urbanze
Code in ulp
.global x
x: .long 0
.global entry
entry:
move r0, x
ld r0, r0, x
add r0, r0, 1
halt
I comment before with another two tests, and anyone doenst worked
Should be e.g.:
.global x
x: .long 0
.global entry
entry:
move r1, x // load address of x in r1
ld r0, r1, 0 // load data from x in r0
add r0, r0, 1 // increment r0
st r0,r1,0 // store r0 as new content of x
halt
Well, doenst work too. Variable still in value declared in C
Images:
https://i.imgur.com/0sWX7Z6.png https://i.imgur.com/Tr9yMz7.png
Code in C:
Code: Select all
#include <C:\msys32\ESP32\ESP32\components\arduino\cores\esp32\Arduino.h>
#include <C:\msys32\ESP32\ESP32\components\arduino\libraries\WiFi\src\WiFi.h>
#include <C:\msys32\ESP32\esp-idf\components\freertos\include\freertos\FreeRTOS.h>
#include <C:\msys32\ESP32\esp-idf\components\freertos\include\freertos\task.h>
#include <C:\msys32\ESP32\esp-idf\components\esp32\include\esp_system.h>
#include <C:\msys32\ESP32\esp-idf\components\ulp\include\esp32\ulp.h>
#include <C:\msys32\ESP32\esp-idf\components\ulp\ulp.c>
#include <C:\msys32\ESP32\ESP32\build\main\ulp_main.h>
extern uint32_t ulp_x = 3;
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 ulp();
extern "C" void app_main()
{
initArduino();
Serial.begin(115200);
ulp();
Serial.printf("\nClock:%d RAM: %d", ESP.getCpuFreqMHz(), ESP.getFreeHeap());
esp_deep_sleep(15000000);
}
void ulp()
{
Serial.printf("\nULP_X: %d", ulp_x & UINT16_MAX);
delay(100);
ulp_load_binary(0, ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t));
delay(100);
Serial.printf("\nULP_X: %d", ulp_x & UINT16_MAX);
}
Code in assembly:
Code: Select all
.global x
x: .long 0
.global entry
entry:
move r1, x // load address of x in r1
ld r0, r1, 0 // load data from x in r0
add r0, r0, 1 // increment r0
st r0,r1,0 // store r0 as new content of x
halt
Re: ULP global variables
Posted: Wed Oct 04, 2017 12:43 pm
by urbanze
Code in ulp
.global x
x: .long 0
.global entry
entry:
move r0, x
ld r0, r0, x
add r0, r0, 1
halt
I comment before with another two tests, and anyone doenst worked
Should be e.g.:
.global x
x: .long 0
.global entry
entry:
move r1, x // load address of x in r1
ld r0, r1, 0 // load data from x in r0
add r0, r0, 1 // increment r0
st r0,r1,0 // store r0 as new content of x
halt
Well, doenst work too. Variable still in value declared in C
Images:
https://i.imgur.com/0sWX7Z6.png https://i.imgur.com/Tr9yMz7.png
Code in C:
Code: Select all
#include <C:\msys32\ESP32\ESP32\components\arduino\cores\esp32\Arduino.h>
#include <C:\msys32\ESP32\ESP32\components\arduino\libraries\WiFi\src\WiFi.h>
#include <C:\msys32\ESP32\esp-idf\components\freertos\include\freertos\FreeRTOS.h>
#include <C:\msys32\ESP32\esp-idf\components\freertos\include\freertos\task.h>
#include <C:\msys32\ESP32\esp-idf\components\esp32\include\esp_system.h>
#include <C:\msys32\ESP32\esp-idf\components\ulp\include\esp32\ulp.h>
#include <C:\msys32\ESP32\esp-idf\components\ulp\ulp.c>
#include <C:\msys32\ESP32\ESP32\build\main\ulp_main.h>
extern uint32_t ulp_x = 1;
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 ulp();
extern "C" void app_main()
{
initArduino();
Serial.begin(115200);
ulp();
Serial.printf("\nClock:%d RAM: %d", ESP.getCpuFreqMHz(), ESP.getFreeHeap());
esp_deep_sleep(15000000);
}
void ulp()
{
Serial.printf("\nULP_X: %d", ulp_x & UINT16_MAX);
delay(100);
ulp_load_binary(0, ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t));
delay(100);
Serial.printf("\nULP_X: %d", ulp_x & UINT16_MAX);
}
Code in assembly:
Code: Select all
.global x
x: .long 0
.global entry
entry:
move r1, x // load address of x in r1
ld r0, r1, 0 // load data from x in r0
add r0, r0, 1 // increment r0
st r0,r1,0 // store r0 as new content of x
halt
Re: ULP global variables
Posted: Wed Oct 04, 2017 1:31 pm
by krzychb
I see the ulp binary code (including the "x" variable definition) being loaded from scratch to the ESP32's RTC_SLOW_MEM memory on each wakeup .
Instead I would use technique shown
here to load it only once on hard reset.
Otherwise this variable will be reset on each wakeup.
Re: ULP global variables
Posted: Wed Oct 04, 2017 2:15 pm
by urbanze
I see the ulp binary code (including the "x" variable definition) being loaded from scratch to the ESP32's RTC_SLOW_MEM memory on each wakeup .
Instead I would use technique shown
here to load it only once on hard reset.
Otherwise this variable will be reset on each wakeup.
Yes! but I try to just change variable while all cpu's running and my codes and code by tomtor, not working. When I do this, I will "play with sleep".
Why variables won't changing??? I do right

Re: ULP global variables
Posted: Wed Oct 04, 2017 2:53 pm
by krzychb
Ah, sorry. Now I see you are printing the value before and after loading the ulp code.
I will look into it sometime later today.
Re: ULP global variables
Posted: Wed Oct 04, 2017 2:56 pm
by urbanze
Ah, sorry. Now I see you are printing the value before and after loading the ulp code.
I will look into it sometime later today.
Thank you for attencion! I still working in this. And after, I will try to "blink" some pin's from ulp too, if you can help me later with this, I'd really appreciate it.
Re: ULP global variables
Posted: Wed Oct 04, 2017 3:19 pm
by ESP_igrr
Are you sure the compiler (on the main application side) is not optimizing the second load of ulp_x away? If it does, then casting ulp_x to a volatile variable might help.
Re: ULP global variables
Posted: Wed Oct 04, 2017 3:40 pm
by urbanze
Are you sure the compiler (on the main application side) is not optimizing the second load of ulp_x away? If it does, then casting ulp_x to a volatile variable might help.
Well, that did not work, either. When i put volatile prefix, compiler give me a error:
Then, I open ulp_main.h and see "global variables"
Code in ulp_main.h
Code: Select all
// Variable definitions for ESP32ULP
// This file is generated automatically by esp32ulp_mapgen.py utility
#pragma once
extern uint32_t ulp_x;
extern volatile uint32_t ulp_entry;
Then, I changed "extern uint32_t ulp_x;" to "extern volatile uint32_t ulp_x;". Code compile and upload, but variable still not changing. What hell is wrong?

Re: ULP global variables
Posted: Wed Oct 04, 2017 5:59 pm
by WiFive
Maybe you should remove global initialization of ulp_x and only set value if it is a cold boot.
Re: ULP global variables
Posted: Wed Oct 04, 2017 6:21 pm
by urbanze
Maybe you should remove global initialization of ulp_x and only set value if it is a cold boot.
I removed declaration in C "extern uint32_t ulp_x" and "make clean". After this, I re-compiled/uploaded and in ulp_main.h still had global variables, maybe is code from assembly. Variable doenst change, AGAIN!!!!!
See, before ulp_run, variable = 0. After ulp_run, still = 0.
Code in assembly:
Code: Select all
.global x
x: .long 0
.global entry
entry:
move r1, x
ld r0, r1, 0
add r0, r0, 5
st r0,r1,0
halt
