ESP32 Module - smartel99/NilaiTFO GitHub Wiki

EspModule

  • Location: shared/interfaces/Esp32Module.*
  • Brief: Interface with the ESP32 serving as the bluetooth controller of the system.
  • Configuration: Connected to the ESP32 through a UART module, as well as through ESP32_EN , ESP32_BOOT , ESP32_TPOUT and ESP32_TPIN.

Uses:

  • UartModule
    • For communication
  • Pin
    • To control the various I/O

Types

enum class ESP32::BootMode

Enum listing the boot modes of the ESP32.

Members:

  • Bootloader = 0
  • Normal = 1

ESP32::Pins

Structure containing the control pins of the ESP32 module.

Members:

  • Pin enable: Enable pin of the ESP32. When high, the ESP32 is enabled.
  • Pin boot: Boot selection pin. When high, normal boot. When low, runs the bootloader.
  • Pin tpout: Heartbeat pin from the ESP32 to the STM32.
  • Pin tpin: Debug signal from the STM32 to the ESP32, currently not used for any purpose.

Operation

Initialisation

The initialisation sequence consists of the following steps:

  1. Initialize the UART module
  2. Set the ESP32_EN pin LOW
  3. Set the ESP32_BOOT pin HIGH
  4. Set the ESP32_EN pin HIGH
  5. Do the POST

POST

To pass the POST, the following list of actions must all succeed:

  1. Successfully initialised the UART peripheral
  2. ESP32_TPOUT is HIGH
  3. Send the device name to be used to the ESP32.
  4. The ESP32 responds with "OK" after receiving it's name.

Run Loop

During the run loop of this module, the ESP32_TPOUT pin is monitored to make sure it is still in a HIGH state. If it isn't, the ESP32 is reset by toggling the ESP32_EN pin LOW then HIGH again. The device name needs to be sent again when the ESP32 is reset.

Logging

Function Event Message Appears On Debug Port Appears in Log File
TODO

Theory of Operation

Once initialized, this module acts like a regular UART module.

This module is basically just a wrapper around a UartModule handle.

Public Functions

EspModule::EspModule(const std::string& label, UartModule* uart, const std::string& deviceName, const ESP32::Pins& pins)

Constructor of the module.

Parameters:

  • const std::string& label: label to be used by the module
  • UartModule* uart: Pointer to the UART port to use
  • const std::string& deviceName: String to use as the device name
  • const ESP32::Pins& pins: Structure containing the control pins of the ESP32

void EspModule::Enable()

Enables the ESP32.

Parameters: None

Returns: None

void EspModule::Disable()

Disables the ESP32.

Parameters: None

Returns: None

bool EspModule::IsEnabled()

Check if the ESP32 is currently enabled.

Parameters: None

Returns: True if the ESP32 is enabled, false otherwise

void EspModule::SetBootMode(ESP32::BootMode mode)

Set the boot mode of the ESP32.

For this to be of any use, you must follow these steps:

  1. Disable the ESP32
  2. Set the boot mode
  3. Enable the ESP32

Parameters:

  • ESP32::BootMode mode: The desired boot mode

Returns: None

bool EspModule::ProgramEsp(const std::string& filepath = "esp32.bin")

Programs the ESP32 with the specified binary file located on the SD card.

If the file cannot be found on the SD card, nothing is done.

To program the ESP32, you must follow these steps:

  1. Disable the ESP32
  2. Set the boot mode to ESP32::BootMode::Bootloader
  3. Enable the ESP32
  4. Program the ESP32
  5. Disable the ESP32
  6. Set the boot mode back to ESP32::BootMode::Normal
  7. Enable the ESP32

To minimize the risk of failure at any steps of the programming process, this function works in the following way:

  1. Check if the file specified in parameter exists on the SD card
  2. If it exists, open it and verify the checksum
  3. If the checksum is valid, set the ESP32 into bootloader mode
  4. Program the ESP32 with the binary file

Note: This function is completely disabled if the filesystem module is not enabled.

Parameters:

  • const std::string& filepath: The path of the binary file to program the ESP32 with, defaults to "esp32.bin"

Returns: True if the programming was successful, false otherwise

void EspModule::Transmit(const char* msg, size_t len)

Wrapper around UartModule::Transmit(const char* msg, size_t len).

Parameters:

  • const char* msg: The packet to send.
  • size_t len: The number of bytes to send.

Returns: None

void EspModule::Transmit(const std::string& msg)

Wrapper around UartModule::Transmit(const std::string& msg).

Parameters:

  • const std::string& msg: The packet to send.

Returns: None

void EspModule::Transmit(const std::vector<uint8_t>& msg)

Wrapper around UartModule::Transmit(const std::vector<uint8_t>& msg).

Parameters:

  • const std::vector<uint8_t>& msg: The packet to send.

Returns: None

size_t EspModule::GetNumberOfWaitingFrames() const

Wrapper around UartModule::GetNumberOfWaitingFrames().

Parameters: None

Returns: The number of frames waiting to be read.

CEP_UART::Frame EspModule::Receive()

Wrapper around UartModule::Receive().

Parameters: None

Returns: The received frame.

void EspModule::SetExpectedRxLen(size_t len)

Wrapper around UartModule::SetExpectedRxLen(size_t len).

Parameters: The number of bytes that are expected to be received

Returns: None

void EspModule::ClearExpectedRxLen()

Wrapper around UartModule::ClearExpectedRxLen().

Parameters: None

Returns: None

void EspModule::SetFrameReceiveCpltCallback(const std::function<void()>& cb)

Wrapper around UartModule::SetFrameReceiveCpltCallback(const std::function<void()>& cb).

Parameters:

  • const std::function<void()>& cb: The function to be called once a frame has been received.

Returns: None

void EspModule::ClearFrameReceiveCpltCallback()

Wrapper around UartModule::ClearFrameReceiveCpltCallback().

Parameters: None

Returns: None

void EspModule::SetStartOfFrameSequence()

  • void EspModule::SetStartOfFrameSequence(const char* sof, size_t len)
  • void EspModule::SetStartOfFrameSequence(const std::string& sof)
  • void EspModule::SetStartOfFrameSequence(const std::vector<uint8_t>& sof) Wrapper around UartModule::SetStartOfFrameSequence().

Parameters:

  • void EspModule::SetStartOfFrameSequence(const char* sof, size_t len):
    • const char* sof: The start of frame
    • size_t len: The number of bytes in the start of frame
  • void EspModule::SetStartOfFrameSequence(const std::string& sof):
    • const std::string& sof: The start of frame
  • void EspModule::SetStartOfFrameSequence(const std::vector<uint8_t>& sof):
    • const std::vector<uint8_t>& sof: The start of frame

Returns: None

void EspModule::ClearStartOfFrameSequence()

Wrapper around UartModule::ClearStartOfFrameSequence().

Parameters: None

Returns: None

void EspModule::SetEndOfFrameSequence()

  • void EspModule::SetEndOfFrameSequence(const char* sof, size_t len)
  • void EspModule::SetEndOfFrameSequence(const std::string& sof)
  • void EspModule::SetEndOfFrameSequence(const std::vector<uint8_t>& sof) Wrapper around UartModule::SetEndOfFrameSequence().

Parameters:

  • void EspModule::SetEndOfFrameSequence(const char* sof, size_t len):
    • const char* sof: The end of frame
    • size_t len: The number of bytes in the end of frame
  • void EspModule::SetEndOfFrameSequence(const std::string& sof):
    • const std::string& sof: The end of frame
  • void EspModule::SetEndOfFrameSequence(const std::vector<uint8_t>& sof):
    • const std::vector<uint8_t>& sof: The end of frame

Returns: None

void EspModule::ClearEndOfFrameSequence()

Wrapper around UartModule::ClearEndOfFrameSequence().

Parameters: None

Returns: None

Misc

⚠️ **GitHub.com Fallback** ⚠️