ESP-32 register contents printing

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Ralston
Posts: 3
Joined: Sat Apr 09, 2022 4:29 pm

ESP-32 register contents printing

Post by Ralston » Sat Apr 09, 2022 8:19 pm

I don't know how to use MicroPython to format the print() output.

After reading a 32 bit register, I can print the hex or bin with

print("SEL0 ",hex(mem32[RTC_CNTL_SOC_CLK_SEL] & 0xffffffff)) with a result of: SEL0 0x29580010
print("SEL0 ",bin(mem32[RTC_CNTL_SOC_CLK_SEL] & 0xffffffff)) with a result of: SEL0 0b101001010110000000000000010000

If the most significant bits are zero, they don't print.

Questions:

1. How do I get a print of all 32 bits, in either hex or bin?

2. When getting a print of all 32 bits in binary, is there some way to format the output to look like this:

0b 0010 1001 0101 1000 0000 0000 0001 0000

3. Is there some way to "extract" and then display only the desired bits, in this case bits 27-28:

SEL0 = 01

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ESP-32 register contents printing

Post by dhylands » Sun Apr 10, 2022 3:37 am

You can use the format command to print stuff. To print binary, you'd do something like:

Code: Select all

print('Num = {:b}'.format(3))
which will output:

Code: Select all

Num = 11
. If you wanted to print 8 bits, with leading zeros, then you'd use this:

Code: Select all

>>> print('Num = {:08b}'.format(3))
Num = 00000011
I've never seen anything to stick a space in every 4th position (as part of format). You could do something like this:

Code: Select all

>>> s = '{:032b}'.format(1234)
>>> ' '.join(s[i:i+4] for i in range(0,len(s),4))
'0000 0000 0000 0000 0000 0100 1101 0010'
To extract bits 27 & 28, you could do something like this:

Code: Select all

>>> num = 0x29580010
>>> SEL0 = (num >> 27) & 3
>>> print('SEL0 = {:02b}'.format(SEL0))
SEL0 = 01

Ralston
Posts: 3
Joined: Sat Apr 09, 2022 4:29 pm

Re: ESP-32 register contents printing

Post by Ralston » Sun Apr 10, 2022 7:27 pm

Thank you dhylands, these are very useful clues for me to pursue as a newbie.

It's nice to see those 32 bits with this example: print('SEL0 = {:032b}'.format(11110000)) to get:
SEL0 = 00000000101010011000011001110000

and if I leave off the '0' on 032b, then I get what look to be the correct number of spaces, so I'll be including the zero.


This example below seems to work, after I convert the string to an integer:

SEL0 = int(bin(mem32[RTC_CNTL_SOC_CLK_SEL] & 0xffffffff))
print('SEL0 = {:032b}'.format(SEL0))

this result is what I wanted: SEL0 = 00100001010110000000000000010000

Again, thank you

Ralston
Posts: 3
Joined: Sat Apr 09, 2022 4:29 pm

Re: ESP-32 register contents printing

Post by Ralston » Tue Apr 12, 2022 11:55 pm

And thank you for this code to print 8 groups of 4 digits, making it very easy to translate back and forth between hex and binary.
Very useful.

>>> s = '{:032b}'.format(1234)
>>> ' '.join(s[i:i+4] for i in range(0,len(s),4))
'0000 0000 0000 0000 0000 0100 1101 0010'

Post Reply