summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIhorNehrutsa <Ihor.Nehrutsa@gmail.com>2023-09-01 18:50:17 +0300
committerDamien George <damien@micropython.org>2023-09-04 23:21:50 +1000
commit1a5bfa502471c07f44997d05e242aae93687dfe9 (patch)
treedf977a75b2b172be1213d17011aef41d458a4463
parent304f13a74ee5909321a67cf538d3a50d85de1e82 (diff)
docs/esp32/tutorial: Add example for pin access via registers.
Synchronous access to pins directly via registers. Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
-rwxr-xr-xdocs/esp32/tutorial/img/mem32_gpio_output.jpgbin0 -> 204577 bytes
-rw-r--r--docs/esp32/tutorial/peripheral_access.rst80
2 files changed, 80 insertions, 0 deletions
diff --git a/docs/esp32/tutorial/img/mem32_gpio_output.jpg b/docs/esp32/tutorial/img/mem32_gpio_output.jpg
new file mode 100755
index 000000000..554420287
--- /dev/null
+++ b/docs/esp32/tutorial/img/mem32_gpio_output.jpg
Binary files differ
diff --git a/docs/esp32/tutorial/peripheral_access.rst b/docs/esp32/tutorial/peripheral_access.rst
index 3304c341d..ecdec101f 100644
--- a/docs/esp32/tutorial/peripheral_access.rst
+++ b/docs/esp32/tutorial/peripheral_access.rst
@@ -42,3 +42,83 @@ for this:
The MCPWM0 peripheral is in bit position 17 of the above two registers, hence
the value of ``DPORT_PWM0_CLK_EN``.
+
+Synchronous access to pins directly via registers
+-------------------------------------------------
+
+The following code shows how to access pins directly via registers. It has been
+tested on a generic ESP32 board. It configures pins 16, 17, 32 and 33 in output
+mode via registers, and switches pin output values via registers. Pins 16 and
+17 are switched simultaneously.
+
+.. code-block:: python3
+
+ from micropython import const
+ from machine import mem32, Pin
+
+ GPIO_OUT_REG = const(0x3FF44004) # GPIO 0-31 output register
+ GPIO_OUT1_REG = const(0x3FF44010) # GPIO 32-39 output register
+
+ GPIO_ENABLE_REG = const(0x3FF44020) # GPIO 0-31 output enable register
+ GPIO_ENABLE1_REG = const(0x3FF4402C) # GPIO 32-39 output enable register
+
+ M16 = 1 << 16 # Pin(16) bit mask
+ M17 = 1 << 17 # Pin(17) bit mask
+
+ M32 = 1 << (32-32) # Pin(32) bit mask
+ M33 = 1 << (33-32) # Pin(33) bit mask
+
+ # Enable pin output mode like
+ # p16 = Pin(16, mode=Pin.OUT)
+ # p17 = Pin(17, mode=Pin.OUT)
+ # p32 = Pin(32, mode=Pin.OUT)
+ # p33 = Pin(33, mode=Pin.OUT)
+ mem32[GPIO_ENABLE_REG] = mem32[GPIO_ENABLE_REG] | M16 | M17
+ mem32[GPIO_ENABLE1_REG] = mem32[GPIO_ENABLE1_REG] | M32 | M33
+
+ print(hex(mem32[GPIO_OUT_REG]), hex(mem32[GPIO_OUT1_REG]))
+
+ # Set outputs to 1 like
+ # p16(1)
+ # p17(1)
+ # p32(1)
+ # p33(1)
+ mem32[GPIO_OUT_REG] = mem32[GPIO_OUT_REG] | M16 | M17
+ mem32[GPIO_OUT1_REG] = mem32[GPIO_OUT1_REG] | M32 | M33
+
+ print(hex(mem32[GPIO_OUT_REG]), hex(mem32[GPIO_OUT1_REG]))
+
+ # Set outputs to 0 like
+ # p16(0)
+ # p17(0)
+ # p32(0)
+ # p33(0)
+ mem32[GPIO_OUT_REG] = mem32[GPIO_OUT_REG] & ~(M16 | M17)
+ mem32[GPIO_OUT1_REG] = mem32[GPIO_OUT1_REG] & ~(M32 | M33)
+
+ print(hex(mem32[GPIO_OUT_REG]), hex(mem32[GPIO_OUT1_REG]))
+
+ while True:
+ # Set outputs to 1
+ mem32[GPIO_OUT_REG] = mem32[GPIO_OUT_REG] | M16 | M17
+ mem32[GPIO_OUT1_REG] = mem32[GPIO_OUT1_REG] | M32 | M33
+
+ # Set outputs to 0
+ mem32[GPIO_OUT_REG] = mem32[GPIO_OUT_REG] & ~(M16 | M17)
+ mem32[GPIO_OUT1_REG] = mem32[GPIO_OUT1_REG] & ~(M32 | M33)
+
+
+Output is::
+
+ 0x0 0x0
+ 0x30000 0x3
+ 0x0 0x0
+
+Pins 16 and 17 are switched synchronously:
+
+.. image:: img/mem32_gpio_output.jpg
+
+Same image on pins 32 and 33.
+
+Note that pins 34-36 and 39 are inputs only. Also pins 1 and 3 are Tx, Rx of the REPL UART,
+pins 6-11 are connected to the built-in SPI flash.