Another question on my ulp code (the sleep time is 5 seconds). It should read RTC ticks and save them into sdata[0], sdata[1], sdata[2], but unluckily it doesn't since it always prints a strange value which is always the same (there is also an I2C read for BME280, but I skipped it since it does not work, the ESP32 never gets awaken by wake instruction near is_rdy_for_wakeup, now I want to fix the RTC part).
Code: Select all
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc_io_reg.h"
#include "soc/soc_ulp.h"
#include "soc/sens_reg.h"
#include "soc/rtc_i2c_reg.h"
#include "globaldef.h"
/* Define variables, which go into .bss section (zero-initialized data) */
.bss
.global samplecount
samplecount: .long 0 // long is 32bit in ulp
// rtc 48 bit diventano 96, 3 int da 32 (ognuno memorizza i 16 bit)
//pressione 20 bit diventano 64, 2 int da 32 (uno i primi sedici, l'altro i rimanenti 4)
//temperatura 20 bit diventano 64, 2 int da 32 (uno i primi sedici, l'altro i rimanenti 4)
//umidità 16 bit 2 byte diventa 32 bit, 1 int da 32.
.global sdata
sdata: .fill MAX_BME280_SAMPLES*(4*3+4*2+4*2+4*1) // 3 int for RTC 2 int for pressure 2 int for temperature 1 int for humidity total 32 bytes
/* Code goes into .text section */
.text
.global entry
entry:
// Release hold on pins, so that we can manipulate them
WRITE_RTC_REG(RTC_IO_TOUCH_PAD1_REG, RTC_IO_TOUCH_PAD1_HOLD_S, RTC_IO_TOUCH_PAD1_HOLD_M, 0);
WRITE_RTC_REG(RTC_IO_TOUCH_PAD0_REG, RTC_IO_TOUCH_PAD0_HOLD_S, RTC_IO_TOUCH_PAD0_HOLD_M, 0);
// Select SDA/SCL pins to version 0 TOUCH0 and TOUCH1 (version 1 TOUCH2 and TOUCH3)
WRITE_RTC_REG(RTC_IO_SAR_I2C_IO_REG, RTC_IO_SAR_I2C_SDA_SEL_S, RTC_IO_SAR_I2C_SDA_SEL_M, 0);
WRITE_RTC_REG(RTC_IO_SAR_I2C_IO_REG, RTC_IO_SAR_I2C_SCL_SEL_S, RTC_IO_SAR_I2C_SCL_SEL_M, 0);
// Set slave address for i2c device we are talking to (ADDR1_REG contains both ADDR0 and ADDR1)
WRITE_RTC_REG(SENS_SAR_SLAVE_ADDR1_REG, SENS_I2C_SLAVE_ADDR0_S, SENS_I2C_SLAVE_ADDR0_M, I2C_ADDR);
WRITE_RTC_REG(SENS_SAR_SLAVE_ADDR1_REG, SENS_I2C_SLAVE_ADDR1_S, SENS_I2C_SLAVE_ADDR1_M, I2C_ADDR);
// Set SCL speed to 100khz
WRITE_RTC_REG(RTC_I2C_SCL_LOW_PERIOD_REG, RTC_I2C_SCL_LOW_PERIOD_S, RTC_I2C_SCL_LOW_PERIOD_M, 40);
WRITE_RTC_REG(RTC_I2C_SCL_HIGH_PERIOD_REG, RTC_I2C_SCL_HIGH_PERIOD_S, RTC_I2C_SCL_HIGH_PERIOD_M, 40);
// SDA duty (delay) cycles from falling edge of SCL when SDA changes.
WRITE_RTC_REG(RTC_I2C_SDA_DUTY_REG, RTC_I2C_SDA_DUTY_S, RTC_I2C_SDA_DUTY_M, 16);
// Number of cycles after start/stop condition
WRITE_RTC_REG(RTC_I2C_SCL_START_PERIOD_REG, RTC_I2C_SCL_START_PERIOD_S, RTC_I2C_SCL_START_PERIOD_M, 30);
WRITE_RTC_REG(RTC_I2C_SCL_STOP_PERIOD_REG, RTC_I2C_SCL_STOP_PERIOD_S, RTC_I2C_SCL_STOP_PERIOD_M, 44);
// cycles before timeout
WRITE_RTC_REG(RTC_I2C_TIMEOUT_REG, RTC_I2C_TIMEOUT_S, RTC_I2C_TIMEOUT_M, 10000);
// Set mode to master
WRITE_RTC_REG(RTC_I2C_CTRL_REG, RTC_I2C_MS_MODE_S, RTC_I2C_MS_MODE_M, 1);
main:
// Main code
move R3,sdata
// READ RTC 48 bit counter
WRITE_RTC_FIELD(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_UPDATE, 1)
waitSampleRtc:
READ_RTC_FIELD(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_VALID)
jumpr waitSampleRtc, 0, EQ
READ_RTC_REG(RTC_CNTL_TIME1_REG, 0, 16) //[47:32]
st R0,R3,0 // sdata[0]
READ_RTC_REG(RTC_CNTL_TIME0_REG, 16, 16) //[31:16]
st R0,R3,4*1 // sdata[1]
READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16) //[15:0]
st R0,R3,4*2 // sdata[2]
// samplecount++
move r2,samplecount
ld r0,r2,0 // r0=m[samplecount]
add r0,r0,1 // r0=r0+1
st r0,r2,0 // m[samplecount]=r0
// Wake up ESP32
is_rdy_for_wakeup_top:
READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP) // Read RTC_CNTL_RDY_FOR_WAKEUP bit
AND r0, r0, 1
JUMP is_rdy_for_wakeup_top, eq // Retry until the bit is set
WAKE // Trigger wake up
halt
jump entry
// Read BME280
i2c_wr 0xF2,1,2,0,0 // oversampling x1 in 0xF2 register for humidity
i2c_wr 0xF4,0x25,7,0,0 // oversampling x1 in 0xF4 register for pressure and humidity and mode = forced
i2c_wr 0xF5,0,7,0,0 // disables SPI, no IIR filter, 0.5 standby in config register 0xF5
waitSampleBme:
i2c_rd 0xF3,7,0,0 // read status register 0xF3
and r0,r0,8 // mask bit 3
jumpr waitSampleBme,0,GT
// Read pressure
i2c_rd 0xf7,7,0,0 // read reg 0xF7 pressure[19:12]
lsh r1,r0,8 // r1=r0<<8
and r1,r1,0xFF00 // mask upper 8 bits
i2c_rd 0xf8, 7, 0, 0 // read reg 0xF8 pressure [11:4]
and r0,r0,0xFF // mask first 8bit
or r1,r0,r1 // r1= r0|r1
st r1,r3,4*3 // sdata[3]=pressure [19:12]|pressure[11:4]
i2c_rd 0xf9, 3, 0, 0 // read reg 0xF9 pressure[3:0]
st r0, r3, 4*4 // sdata[4]=pressure [3:0]
// Read temp
i2c_rd 0xfa, 7, 0, 0 // read reg 0xFA temp [19:12]
lsh r1,r0,8 // r1=r0<<8
and r1,r1,0xFF00 // mask upper 8 bits
i2c_rd 0xfb, 7, 0, 0 // read reg 0xFB temp [11:4]
and r0,r0,0xFF // mask first 8bit
or r1,r0,r1 // r1= r0|r1
st r1,r3,4*5 // sdata[5]=temp [19:12]|temp[11:4]
i2c_rd 0xfc, 3, 0, 0 // read reg 0xFc temp[3:0]
st r0, r3, 4*6 // sdata[6]=temp [3:0]
// Read humidity
i2c_rd 0xfd, 7, 0, 0 // read reg 0xFd humidity [15:8]
lsh r1,r0,8 // r1=r0<<8
and r1,r1,0xFF00 // mask upper 8 bits
i2c_rd 0xfe, 7, 0, 0 // read reg 0xFe humidity [7:0]
and r0,r0,0xFF // mask first 8bit
or r1,r0,r1 // r1= r0|r1
st r1,r3,4*7 // sdata[7]=humidity [15:8]|temp[7:0]
// samplecount++
move r2,samplecount
ld r0,r2,0 // r0=m[samplecount]
add r0,r0,1 // r0=r0+1
st r0,r2,0 // m[samplecount]=r0
// Wake up ESP32
is_rdy_for_wakeup:
READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP) // Read RTC_CNTL_RDY_FOR_WAKEUP bit
AND r0, r0, 1
JUMP is_rdy_for_wakeup, eq // Retry until the bit is set
WAKE // Trigger wake up
halt
jump entry
/* end the program */
.global exit
exit:
halt
Sample count value is increased, so it's ok. Do you have any idea ?