Dear Christian Walther,
It is so much grateful for me to have your reply again.
I think I am understanding "WRITE_RTC_REG", thanks to Christian.
As you said the ESP32 technical reference manual is so helpful for to understand ULP.
I am using ESP32-PICO-V3-02, so I've download "
https://www.espressif.com/sites/default ... ual_en.pdf" it and looked up all the values.
I followed the
chapter 30.6.1 Configuring RTC_I2C,I've come to this code,
Code: Select all
source = """
#define DR_REG_RTCIO_BASE 0x3ff48400
#define DR_REG_RTC_I2C_BASE 0x3ff48C00
#define DR_REG_SENS_BASE 0x3ff48800
#define SENS_SAR_SLAVE_ADDR1_REG 0x3FF4883C
#define SENS_I2C_SLAVE_ADDR0 0x000007FF
#define SENS_I2C_SLAVE_ADDR0_S 11
#define SENS_I2C_SLAVE_ADDR1_S 0
#define RTC_IO_SAR_I2C_IO_REG 0x3FF484C4
#define RTC_IO_SAR_I2C_SDA_SEL_S 30
#define RTC_IO_SAR_I2C_SCL_SEL_S 28
#define RTC_I2C_SCL_LOW_PERIOD_REG 0x3FF48C00
#define RTC_I2C_SCL_HIGH_PERIOD_REG 0x3FF48C38
#define RTC_I2C_SCL_LOW_PERIOD_S 0
#define RTC_I2C_SCL_HIGH_PERIOD_S 0
#define RTC_I2C_SDA_DUTY_REG 0x3FF48C30
#define RTC_I2C_SDA_DUTY_S 0
#define RTC_I2C_SCL_START_PERIOD_REG 0x3FF48C40
#define RTC_I2C_SCL_START_PERIOD_S 0
#define RTC_I2C_SCL_STOP_PERIOD_REG 0x3FF48C44
#define RTC_I2C_SCL_STOP_PERIOD_S 0
#define RTC_I2C_TIMEOUT_REG 0x3FF48C0C
#define RTC_I2C_TIMEOUT_S 0
#define RTC_I2C_CTRL_REG 0x3FF48C04
#define RTC_I2C_MS_MODE_S 4
#define DR_REG_RTCIO_BASE 0x3ff48400
#define RTC_IO_TOUCH_PAD0_REG 0x3ff48494
#define RTC_IO_TOUCH_PAD1_REG 0x3ff48498
#define RTC_IO_TOUCH_PAD2_REG 0x3ff4849c
#define RTC_IO_TOUCH_PAD3_REG 0x3ff484a0
#define RTC_IO_TOUCH_PAD0_FUN_SEL_S 17
#define RTC_IO_TOUCH_PAD1_FUN_SEL_S 17
data: .long 0
value: .long 0
.global entry
entry:
# Select SDA/SCL pins to version 1, TOUCH2 and TOUCH3 (version 0 is TOUCH0 and TOUCH1)
WRITE_RTC_REG(RTC_IO_SAR_I2C_IO_REG, RTC_IO_SAR_I2C_SDA_SEL_S, 2, 0)
WRITE_RTC_REG(RTC_IO_SAR_I2C_IO_REG, RTC_IO_SAR_I2C_SCL_SEL_S, 2, 0)
WRITE_RTC_REG(RTC_IO_TOUCH_PAD0_REG, RTC_IO_TOUCH_PAD0_FUN_SEL_S, 2, 3)
WRITE_RTC_REG(RTC_IO_TOUCH_PAD1_REG, RTC_IO_TOUCH_PAD1_FUN_SEL_S, 2, 3)
# WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value)
WRITE_RTC_REG(SENS_SAR_SLAVE_ADDR1_REG, SENS_I2C_SLAVE_ADDR0_S, 11, 0x76)
# Set SCL speed to 100khz
WRITE_RTC_REG(RTC_I2C_SCL_LOW_PERIOD_REG, RTC_I2C_SCL_LOW_PERIOD_S, 19, 40)
WRITE_RTC_REG(RTC_I2C_SCL_HIGH_PERIOD_REG, RTC_I2C_SCL_HIGH_PERIOD_S, 20, 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, 20, 16)
# Number of cycles after start/stop condition
WRITE_RTC_REG(RTC_I2C_SCL_START_PERIOD_REG, RTC_I2C_SCL_START_PERIOD_S, 20, 30)
WRITE_RTC_REG(RTC_I2C_SCL_STOP_PERIOD_REG, RTC_I2C_SCL_STOP_PERIOD_S, 20, 44)
# cycles before timeout
WRITE_RTC_REG(RTC_I2C_TIMEOUT_REG, RTC_I2C_TIMEOUT_S, 20, 200)
# Set mode to master
WRITE_RTC_REG(RTC_I2C_CTRL_REG, RTC_I2C_MS_MODE_S, 1, 1)
JUMP running
reads:
ADD r0, r0, 1 # r0 += 1
I2C_RD 0xa2, 7, 0, 0 # r0 = 0x171 //sensor returns 0x171
ADD r0, r0, 1 # r0 += 1
ST r0, r1, 4 # mem32[r1+4] = r0 // 0x172
JUMP exit
running:
MOVE r3, data # r3 = 0 //(data)
LD r2, r3, 0 # [r3+0]) = r2
ADD r2, r2, 1 # r2 += 1
ST r2, r3, 0 # [r3+0] = r2
JUMP reads
exit:
HALT
"""
from esp32_ulp import src_to_binary
from esp32 import ULP
from machine import mem32
binary = src_to_binary(source)
load_addr, entry_addr = 0, 8
ULP_MEM_BASE = 0x50000000
ULP_DATA_MASK = 0xFFFF
ulp = ULP()
ulp.set_wakeup_period(0, 1000000)
ulp.load_binary(load_addr, binary)
mem32[ULP_MEM_BASE + load_addr] = 0x1000
ulp.run(entry_addr)
while True:
res1 = hex(mem32[ULP_MEM_BASE + load_addr] & ULP_DATA_MASK)
res2 = hex(mem32[ULP_MEM_BASE + load_addr+4] & ULP_DATA_MASK)
print(res1,res2)
I am expecting,
0x1001 , 0x172
0x1002 , 0x172
0x1003 , 0x172
......
result is,
0x1001 , 0x0
0x1001 , 0x0
0x1001 , 0x0
......
Just for getting one bit data from I2C, generates that too long codes
it freezed after I2C_RD, it shows keep count up++ without I2C_RD line.
(it is hard to debug ULP, I've added a counter, but after read I2C value it is not counting up..)
I've read the references carefully, refered another c++ examples, tried different GPIO(TOUCH2,3)
,but It did not work. MCU doesn't lie to me, there is obviously something wrong,
but I don't know which I am missing ...
I think I might give up using
ThomasWaldmann/py-esp32-ulp ...