The following code is used to time bringing a pin high/low for ~5.3 microseconds. send_command is given a number that is converted to binary pulses. (i.e. 170 -> 10101010 -> -_-_-_-_). So... bit-banging. This is running on the esp8266 but I haven't tried it on other boards. I have been unsuccessful using time.usleep with viper to get accurate timing (it takes too long) but this code "works" and gives me the resolution I need. This seems to abuse assumptions about the cpu cycles but I am curious if there an equivalent that I am missing from not having read enough documentation.
Questions I have:
Is this code portable to other micropython boards? What are the downsides to setting the bit 16 times? Am I missing an obvious viper function that does this already?
disable_irq() is called before this code executes.
Code: Select all
@micropython.viper
def send_command(data:int):
GPIO_OUT = ptr32(0x60000300) # GPIO base register
for i in range(8):
if data & 0x80:
for _ in range(16):
GPIO_OUT[1] = 0x10 # set bit 4
else:
for _ in range(16):
GPIO_OUT[2] = 0x10 # clear bit 4
data <<= 0x1
GPIO_OUT[2] = 0x10 # clear bit 4 at end
Edit: typos