From 4c9e17e0a16266523e5d89640f756a1a0ad2d8e3 Mon Sep 17 00:00:00 2001 From: NitiKaur Date: Thu, 26 Aug 2021 08:49:10 +0530 Subject: [PATCH] docs/esp32/tutorial: Add an example of peripheral control via regs. --- docs/esp32/tutorial/index.rst | 1 + docs/esp32/tutorial/peripheral_access.rst | 44 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 docs/esp32/tutorial/peripheral_access.rst 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 ``__. 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``.