How to read data from the on-chip temperature Sensor in circuitpython
Closed this issue · 3 comments
HonestQiao commented
SeasonMay commented
I have the same question,official examples provide codes concerning temperature which use the library of“sensor”,but i cannot find this lib in my circuitpython .bin file(version 8.2.9)
HonestQiao commented
I checked the datasheet of efr32mg24 and learned that it may be necessary to read register data from memory mapping. However, circuitpython on efr32mg24 does not have a memoryio module!
HonestQiao commented
We have found a suitable way to read it.
Compile a circuitpython by yourself and enable uctypes.
Use the version I compiled:
adafruit-circuitpython-explorerkit_xg24_brd2703a-en_US-9.0.0-alpha-uctypes.zip
import uctypes
from uctypes import BF_POS, BF_LEN
from uctypes import UINT32, BFUINT32, BFUINT16, BFUINT8, struct
import time
# EMU base address
EMU_ADDR_BASE =0x50004000
# EMU Temperature Register offset
EMU_ADDR_TEMP_OFFSET = 0x088
# EMU Status Register offset
EMU_ADDR_STATUS_OFFSET = 0x084
# EMU Command Register offset
EMU_ADDR_CMD_OFFSET = 0x070
# EMU Temperature and Status data struct
EMU_LAYOUT = {
"EMU_TEMP": (
EMU_ADDR_TEMP_OFFSET,
{
"TEMP": 2 << BF_POS | 9 << BF_LEN | BFUINT32,
"TEMPLSB": 0 << BF_POS | 2 << BF_LEN | BFUINT32,
"TEMPAVG": 18 << BF_POS | 9 << BF_LEN | BFUINT32,
"TEMPAVGLSB": 16 << BF_POS | 2 << BF_LEN | BFUINT32,
}),
"EMU_STATUS": (
EMU_ADDR_STATUS_OFFSET,
{
"TEMPACTIVE": 2 << BF_POS | 1 << BF_LEN | BFUINT8,
"TEMPAVGACTIVE": 3 << BF_POS | 1 << BF_LEN | BFUINT8,
}),
}
# print size of EMU_LAYOUT
#print(uctypes.sizeof(EMU_LAYOUT))
# EMU_CMD map
emu_cmd = uctypes.bytearray_at(EMU_ADDR_BASE+EMU_ADDR_CMD_OFFSET, 1)
while True:
# send EMU_CMD_TEMPAVGREQ command
emu_cmd[0] = emu_cmd[0] | 0x10
# emu map
emu = struct(EMU_ADDR_BASE, EMU_LAYOUT)
# print status of EMU Temperature sensor
#print(emu.EMU_STATUS.TEMPACTIVE, emu.EMU_STATUS.TEMPAVGACTIVE)
# print currect Temperature value
T1 = emu.EMU_TEMP.TEMP
T2 = emu.EMU_TEMP.TEMPLSB
Tk = T1 + T2/4
Tc = Tk - 273.15
#print([T1, T2, Tk, Tc])
# print avg value of Temperature
T1 = emu.EMU_TEMP.TEMPAVG
T2 = emu.EMU_TEMP.TEMPAVGLSB
Tavgk = T1 + T2/4
Tavgc = Tavgk - 273.15
#print([T1, T2, Tavgk, Tavgc])
print("Temp avg: %0.2f, Temp curr: %0.2f" % (Tavgc, Tc))
time.sleep(0.5)

