summaryrefslogtreecommitdiffstatshomepage
path: root/docs/esp32/tutorial
diff options
context:
space:
mode:
authorNitiKaur <nitikaur102@gmail.com>2021-08-26 08:49:10 +0530
committerDamien George <damien@micropython.org>2021-10-14 23:31:45 +1100
commit4c9e17e0a16266523e5d89640f756a1a0ad2d8e3 (patch)
treedfa3cd630f18d70d95ec6e91b47c582bd7f86150 /docs/esp32/tutorial
parent763042a28701ace91e63b263dc0d8faa6b8871a9 (diff)
downloadmicropython-4c9e17e0a16266523e5d89640f756a1a0ad2d8e3.tar.gz
micropython-4c9e17e0a16266523e5d89640f756a1a0ad2d8e3.zip
docs/esp32/tutorial: Add an example of peripheral control via regs.
Diffstat (limited to 'docs/esp32/tutorial')
-rw-r--r--docs/esp32/tutorial/index.rst1
-rw-r--r--docs/esp32/tutorial/peripheral_access.rst44
2 files changed, 45 insertions, 0 deletions
diff --git a/docs/esp32/tutorial/index.rst b/docs/esp32/tutorial/index.rst
index e9cfd9db10..c6242d731f 100644
--- a/docs/esp32/tutorial/index.rst
+++ b/docs/esp32/tutorial/index.rst
@@ -20,3 +20,4 @@ to `<https://www.python.org>`__.
intro.rst
pwm.rst
+ peripheral_access.rst
diff --git a/docs/esp32/tutorial/peripheral_access.rst b/docs/esp32/tutorial/peripheral_access.rst
new file mode 100644
index 0000000000..3304c341de
--- /dev/null
+++ b/docs/esp32/tutorial/peripheral_access.rst
@@ -0,0 +1,44 @@
+Accessing peripherals directly via registers
+============================================
+
+The ESP32's peripherals can be controlled via direct register reads and writes.
+This requires reading the datasheet to know what registers to use and what
+values to write to them. The following example shows how to turn on and change
+the prescaler of the MCPWM0 peripheral.
+
+.. code-block:: python3
+
+ from micropython import const
+ from machine import mem32
+
+ # Define the register addresses that will be used.
+ DR_REG_DPORT_BASE = const(0x3FF00000)
+ DPORT_PERIP_CLK_EN_REG = const(DR_REG_DPORT_BASE + 0x0C0)
+ DPORT_PERIP_RST_EN_REG = const(DR_REG_DPORT_BASE + 0x0C4)
+ DPORT_PWM0_CLK_EN = const(1 << 17)
+ MCPWM0 = const(0x3FF5E000)
+ MCPWM1 = const(0x3FF6C000)
+
+ # Enable CLK and disable RST.
+ print(hex(mem32[DPORT_PERIP_CLK_EN_REG] & 0xffffffff))
+ print(hex(mem32[DPORT_PERIP_RST_EN_REG] & 0xffffffff))
+ mem32[DPORT_PERIP_CLK_EN_REG] |= DPORT_PWM0_CLK_EN
+ mem32[DPORT_PERIP_RST_EN_REG] &= ~DPORT_PWM0_CLK_EN
+ print(hex(mem32[DPORT_PERIP_CLK_EN_REG] & 0xffffffff))
+ print(hex(mem32[DPORT_PERIP_RST_EN_REG] & 0xffffffff))
+
+ # Change the MCPWM0 prescaler.
+ print(hex(mem32[MCPWM0])) # read PWM_CLK_CFG_REG (reset value = 0)
+ mem32[MCPWM0] = 0x55 # change PWM_CLK_PRESCALE
+ print(hex(mem32[MCPWM0])) # read PWM_CLK_CFG_REG
+
+Note that before a peripheral can be used its clock must be enabled and it must
+be taken out of reset. In the above example the following registers are used
+for this:
+
+- ``DPORT_PERI_CLK_EN_REG``: used to enable a peripheral clock
+
+- ``DPORT_PERI_RST_EN_REG``: used to reset (or take out of reset) a peripheral
+
+The MCPWM0 peripheral is in bit position 17 of the above two registers, hence
+the value of ``DPORT_PWM0_CLK_EN``.