|
| 1 | +########### |
| 2 | +Peripherals |
| 3 | +########### |
| 4 | + |
| 5 | +Introduction |
| 6 | +------------ |
| 7 | + |
| 8 | +This is a basic introduction on how the peripherals works on the ESP32. This tutorial can be used to understand |
| 9 | +on how to define the peripheral usage cross the pin definition on each of them. |
| 10 | + |
| 11 | +In some microcontrollers architecture, the peripherals are attached to specific pins and cannot be redefined to another one. |
| 12 | + |
| 13 | +For example. |
| 14 | + |
| 15 | +The *XYZ* MCU defines that the I2C peripheral SDA signal is the IO5 on the physical pin 10 and the SCL is on the IO6 and physical pin 11. |
| 16 | + |
| 17 | +This means that in your hardware project, you **NEED** to use these pins as the I2C ant this cannot be changed due the internal architecture. |
| 18 | +In this case, you must be very careful during the hardware design to not commit any mistake by switching the SDA and SCL connections. This |
| 19 | +will be impossible to solve by firmware. |
| 20 | + |
| 21 | +GPIO Matrix and Pin Mux |
| 22 | +----------------------- |
| 23 | + |
| 24 | +The ESP32 architecture includes the capability of configuring some peripherals to any of the GPIOs pins, managed by the `IO MUX GPIO`_. |
| 25 | +This capability, in sum, means that we can route the internal peripheral into a different phisycal pin using the IO MUX and the GPIO Matrix. |
| 26 | + |
| 27 | +.. figure:: ../_static/tutorials/peripherals/tutorial_peripheral_diagram.png |
| 28 | + :align: center |
| 29 | + :width: 600 |
| 30 | + :figclass: align-center |
| 31 | + |
| 32 | +It means that in the scenario of the *XYZ* MCU, on the ESP32 we can use any of the GPIOs to route the SDA (input/output) and the SCL (output). |
| 33 | + |
| 34 | +To use this functionality, we must be aware of some precautions: |
| 35 | + |
| 36 | +* Some of the GPIOs are INPUT only. |
| 37 | +* Some peripherals has output signals and must be used on GPIO's capable to be configured as OUTPUT. |
| 38 | +* Some peripherals, mostly the high speed ones, ADC, DAC, Touch, and JTAG use dedicated GPIOs pins. |
| 39 | + |
| 40 | +.. warning:: |
| 41 | + Before assigning the peripheral pins in your design, double check if the pins you're using are capable for that. |
| 42 | + The input only pins cannot be used for peripherals that requires output or input/output signals. |
| 43 | + |
| 44 | +The biggest advantage of this is the fact that we don't need to be fully dependent on the physical pin, since we can change according to our needs |
| 45 | +and this can facilitate on the hardware design routing or in some cases, fix some pin swap mistake during the hardware design phase. |
| 46 | + |
| 47 | +Peripherals |
| 48 | +----------- |
| 49 | + |
| 50 | +Here is the basic peripheral list present on the ESP32. The peripheral list may vary from each ESP32 SoC family. |
| 51 | +To see all peripherals available on the ESP32-S2 and ESP32-C3, check each of the datasheet. |
| 52 | + |
| 53 | +Peripheral Table |
| 54 | +**************** |
| 55 | + |
| 56 | +============================== =================================== |
| 57 | +Type Function |
| 58 | +============================== =================================== |
| 59 | +ADC Dedicated GPIOs |
| 60 | +DAC Dedicated GPIOs |
| 61 | +Touch Sensor Dedicated GPIOs |
| 62 | +JTAG Dedicated GPIOs |
| 63 | +SD/SDIO/MMC HostController Dedicated GPIOs |
| 64 | +Motor PWM Any GPIO |
| 65 | +SDIO/SPI SlaveController Dedicated GPIOs |
| 66 | +UART Any GPIO |
| 67 | +I2C Any GPIO |
| 68 | +I2S Any GPIO |
| 69 | +LED PWM Any GPIO |
| 70 | +RMT Any GPIO |
| 71 | +GPIO Any GPIO |
| 72 | +Parallel QSPI Dedicated GPIOs |
| 73 | +EMAC Dedicated GPIOs |
| 74 | +Pulse Counter Any GPIO |
| 75 | +TWAI Any GPIO |
| 76 | +============================== =================================== |
| 77 | + |
| 78 | +This table is present on each datasheet provided by Espressif. |
| 79 | + |
| 80 | +Usage Examples |
| 81 | +-------------- |
| 82 | + |
| 83 | +On the Arduino Uno, we have the I2C pins defined by hardware, A4 is the SDA and A5 the SCL. In this case we do not need to set |
| 84 | +those pins in the ```Wire.begin();``` function, because they are already into the Wire library. |
| 85 | + |
| 86 | +.. code-block:: arduino |
| 87 | +
|
| 88 | + void setup() |
| 89 | + { |
| 90 | + Wire.begin(); // join i2c bus (address optional for master) |
| 91 | + } |
| 92 | +
|
| 93 | +Now for the ESP32, the default pins for the I2C GPIO21 for SDA and GPIO22 for SCL, but we can use a different pin as alternative for the |
| 94 | +default ones. |
| 95 | +To change the pins, we must call the ```Wire.setPins(int sda, int scl);``` function before calling ```Wire.begin();```. |
| 96 | + |
| 97 | +.. code-block:: arduino |
| 98 | +
|
| 99 | + void setup() |
| 100 | + { |
| 101 | + Wire.setPins(sda_pin, scl_pin); |
| 102 | + Wire.begin(); // join i2c bus (address optional for master) |
| 103 | + } |
| 104 | +
|
| 105 | +A similar approach also applies for the other peripherals. |
| 106 | + |
| 107 | +Resources |
| 108 | +--------- |
| 109 | + |
| 110 | +* `ESP32 Datasheet`_ (Datasheet) |
| 111 | +* `ESP32-S2 Datasheet`_ (Datasheet) |
| 112 | +* `ESP32-C3 Datasheet`_ (Datasheet) |
| 113 | + |
| 114 | +.. _Espressif Systems: https://www.espressif.com |
| 115 | +.. _Espressif Product Selector: https://products.espressif.com/ |
| 116 | +.. _ESP32 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf |
| 117 | +.. _ESP32-S2 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf |
| 118 | +.. _ESP32-C3 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf |
| 119 | +.. _IO MUX GPIO: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf#iomuxgpio |
| 120 | + |
0 commit comments