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
ESP-32 register contents printing
Re: ESP-32 register contents printing
You can use the format command to print stuff. To print binary, you'd do something like: which will output:. If you wanted to print 8 bits, with leading zeros, then you'd use this: I've never seen anything to stick a space in every 4th position (as part of format). You could do something like this:
To extract bits 27 & 28, you could do something like this:
Code: Select all
print('Num = {:b}'.format(3))
Code: Select all
Num = 11
Code: Select all
>>> print('Num = {:08b}'.format(3))
Num = 00000011
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'
Code: Select all
>>> num = 0x29580010
>>> SEL0 = (num >> 27) & 3
>>> print('SEL0 = {:02b}'.format(SEL0))
SEL0 = 01
Re: ESP-32 register contents printing
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
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
Re: ESP-32 register contents printing
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'
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'