ULP global variables

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: ULP global variables

Postby urbanze » Wed Oct 04, 2017 12:43 pm

tomtor wrote:
urbanze wrote:
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 :evil:

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

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: ULP global variables

Postby urbanze » Wed Oct 04, 2017 12:43 pm

urbanze wrote:
tomtor wrote:
urbanze wrote:
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 :evil:

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

User avatar
ESP_krzychb
Posts: 400
Joined: Sat Oct 01, 2016 9:05 am
Contact:

Re: ULP global variables

Postby ESP_krzychb » Wed Oct 04, 2017 1:31 pm

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.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: ULP global variables

Postby urbanze » Wed Oct 04, 2017 2:15 pm

krzychb wrote: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 :(

User avatar
ESP_krzychb
Posts: 400
Joined: Sat Oct 01, 2016 9:05 am
Contact:

Re: ULP global variables

Postby ESP_krzychb » Wed Oct 04, 2017 2:53 pm

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.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: ULP global variables

Postby urbanze » Wed Oct 04, 2017 2:56 pm

krzychb wrote: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.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: ULP global variables

Postby ESP_igrr » Wed Oct 04, 2017 3:19 pm

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.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: ULP global variables

Postby urbanze » Wed Oct 04, 2017 3:40 pm

ESP_igrr wrote: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:

Image


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? :lol: :twisted:

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ULP global variables

Postby WiFive » Wed Oct 04, 2017 5:59 pm

Maybe you should remove global initialization of ulp_x and only set value if it is a cold boot.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: ULP global variables

Postby urbanze » Wed Oct 04, 2017 6:21 pm

WiFive wrote: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!!!!! :evil: :cry:

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
Image

Who is online

Users browsing this forum: No registered users and 192 guests