Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ESP32/ESP32-S3/ESP32-C3 #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,41 @@ platformio run

When you run ```platformio run``` for the first time, it will download the toolchains and all necessary libraries automatically.

## Note: if build fails with "AttributeError: module 'enum' has no attribute 'IntFlag'", try "pip3 uninstall -y enum34"
Note: if build fails with `AttributeError: module 'enum' has no attribute 'IntFlag'`, try `pip3 uninstall -y enum34`

### Useful commands:
### Useful commands: ESP12e

* ```platformio run``` - process/build all targets
* ```platformio run -e esp12e``` - process/build just the ESP12e target (the NodeMcu v2, Adafruit HUZZAH, etc.)
* ```platformio run -e esp12e -t upload``` - build and upload firmware to embedded board
* ```platformio run -t clean``` - clean project (remove compiled files)

The resulting image(s) can be found in the directory ```.pioenvs``` created during the build process.
### Useful commands: ESP-01
* ```platformio run``` - process/build all targets
* ```platformio run -e esp01_1m``` - process/build just the ESP01 target with 1MB [Amazon ESP-01](https://www.amazon.com/gp/product/B01EA3UJJ4)
* ```platformio run -e esp01_1m -t upload``` - build and upload firmware to embedded board
* ```platformio run -t clean``` - clean project (remove compiled files)

### Useful commands: ESP32-WROOM (ESP32-WROOM-32UE)

* ```platformio run -e espwroom32 -t upload```
* ```platformio run -e espwroom32``` - process/build just the ESP32 target

### Useful commands: ESP32-S3 or ESP32-S3
* ```platformio run -e esp32-s3-devkitc-1 -t upload```
* ```platformio run -e esp32-s3-devkitc-1 ``` - process/build just the ESP32 target

### Useful commands: ESP32-S3-MINI or ESP32-S3-MINI [Mouser](https://www.mouser.com/ProductDetail/356-ESP32S3DEVKTM1N8)
* ```platformio run -e esp32-s3-devkitm-1 -t upload```
* ```platformio run -e esp32-s3-devkitm-1 ``` - process/build just the ESP32 target

### Useful commands: ESP32-C3-MINI
* ```platformio run -e esp32-c3-devkitm-1 -t upload```
* ```platformio run -e esp32-c3-devkitm-1 ``` - process/build just the ESP32 target

resulting image(s) can be found in the directory ```.pioenvs``` created during the build process.

### Platform [Details](https://github.com/RealFlightSystems/mavesp8266/wiki/Espressif-WLAN-Mavlink-Router-and-Bridge-Development)

### MavLink Submodule

Expand All @@ -46,6 +71,8 @@ User level (as well as wiring) instructions can be found here: https://pixhawk.o

* Resetting to Defaults: In case you change the parameters and get locked out of the module, all the parameters can be reset by bringing the GPIO02 pin low (Connect GPIO02 pin to GND pin).

Get the ESP-01 adapter board here on [Amazon](https://www.amazon.com/gp/product/B07Q17XJ36/); commonly called an "ESP01 programmer", this one has a little rocker switch on the side (UART side for serial TTL debugging by AT commands, PROG for firmware programming) and a yellow pin header which allows you plugin the ESP01 module without any wires. It defaults to 115200 and enumerates under Ubuntu as a ch341-uart converter.

### MavLink Protocol

The MavESP8266 handles its own set of parameters and commands. Look at the [PARAMETERS](PARAMETERS.md) page for more information.
Expand Down
78 changes: 78 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
[version]
build_flags = !echo "-DPIO_SRC_REV="$(git rev-parse HEAD) "-DPIO_BUILD_DATE="$(date +%%Y-%%m-%%d) "-DPIO_BUILD_TIME="$(date +%%H:%%M:%%S)

;https://www.espressif.com/en/products/devkits
;ESP8266 Series
[env:esp12e]
platform = [email protected]
framework = arduino
Expand All @@ -40,3 +42,79 @@ platform = [email protected]
framework = arduino
board = esp01
build_flags = ${version.build_flags} -DPIO_BOARD=esp01

; https://www.espressif.com/en/products/devkits
; ESP32 Series
; Modules: ESP32-WROOM-32E, ESP32-WROOM-32UE, ESP32­-WROOM­-DA, ESP32-WROVER-E, ESP32-WROVER-IE
[env:espwroom32]
platform = espressif32
;platform_packages = [email protected]
framework = arduino
board = esp32dev
;board = esp32thing_plus
;upload_port = /dev/ttyUSB0
;monitor_speed = 115200
;upload_speed = 115200
;upload_flags = -b 115200
debug_tool = esp-prog
upload_protocol=esp-prog


; https://www.espressif.com/en/products/devkits
; ESP32-S3 Series
; ESP32-S3 Module - 8MB FLASH/2MB PSRAM, 8 MB FLASH/8MB PSRAM, 32MB FLASH/8MB SRAM
; Modules: ESP32-S3-WROOM-1, ESP32-S3-WROOM-1U, ESP32-S3-WROOM-2, ESP32-S3-WROOM-2U
[env:esp32-s3-devkitc-1]
; direct download link
;platform = https://github.com/tasmota/platform-espressif32/releases/download/v.2.0.5/platform-espressif32-v.2.0.5.zip
platform = [email protected]
board = esp32-s3-devkitc-1
framework = arduino
;upload_protocol = esptool
; see compile flags
;build_flags = -v
; change microcontroller
;board_build.mcu = esp32s3
; change MCU frequency
;board_build.f_cpu = 240000000L


; ESP32-S3 Module - 8MB FLASH, 320kb built-in PSRAM
; Modules: ESP32-S3-MINI-1, ESP32-S3-MINI-1U
[env:esp32-s3-devkitm-1]
;platform = [email protected]
platform = https://github.com/platformio/platform-espressif32.git
framework = arduino
board = esp32-s3-devkitc-1
;debug_tool = esp_usb_jtag
;upload_protocol = esp_usb_jtag
;debug_tool = esp-prog
;upload_protocol=esp-prog


; New ESP32-C3 Module (RISC-V)
[env:esp32-c3-devkitm-1]
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
platform_packages =
toolchain-riscv-esp
framework-arduinoespressif32@https://github.com/espressif/arduino-esp32.git#2.0.1
platformio/tool-esptoolpy @ https://github.com/tasmota/esptool/releases/download/v3.2/esptool-v3.2.zip
framework = arduino
board = esp32dev
board_build.mcu = esp32c3
board_build.partitions = huge_app.csv
board_build.variant = esp32c3
board_build.f_cpu = 160000000L
board_build.f_flash = 80000000L
board_build.flash_mode = dio
board_build.arduino.ldscript = esp32c3_out.ld
build_unflags =
-DARDUINO_ESP32_DEV
-DARDUINO_VARIANT="esp32"
build_flags =
-DARDUINO_ESP32C3_DEV
-DARDUINO_VARIANT="esp32c3"
-DARDUINO_USB_CDC_ON_BOOT=0
build_type = debug
monitor_speed = 115200
;upload_port = /dev/ttyACM0
56 changes: 53 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@
#include "mavesp8266_httpd.h"
#include "mavesp8266_component.h"

#if !defined(ARDUINO_ESP32_DEV) && !defined(ARDUINO_ESP32S3_DEV) && !defined(ARDUINO_ESP32C3_DEV)
#include <ESP8266mDNS.h>
#else
/* ESP32 */
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
#include <esp_event.h>
#include <esp_event_loop.h>
#include <esp_wifi.h>
#include <esp_wifi_types.h>
#include <esp_err.h>
#include <mdns.h>
#endif

#define GPIO02 2

Expand Down Expand Up @@ -109,7 +122,11 @@ void wait_for_client() {
#ifdef ENABLE_DEBUG
int wcount = 0;
#endif
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
uint8 client_count = WiFi.softAPgetStationNum();
#else
uint8 client_count = wifi_softap_get_station_num();
#endif
while (!client_count) {
#ifdef ENABLE_DEBUG
Serial1.print(".");
Expand All @@ -119,7 +136,11 @@ void wait_for_client() {
}
#endif
delay(1000);
#if defined(ARDUINO_ESP32_DEV)|| defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
client_count = WiFi.softAPgetStationNum();
#else
client_count = wifi_softap_get_station_num();
#endif
}
DEBUG_LOG("Got %d client(s)\n", client_count);
}
Expand All @@ -129,7 +150,11 @@ void wait_for_client() {
void reset_interrupt(){
Parameters.resetToDefaults();
Parameters.saveAllToEeprom();
#if defined(ARDUINO_ESP32_DEV)|| defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
ESP.restart();
#else
ESP.reset();
#endif
}

//---------------------------------------------------------------------------------
Expand All @@ -155,8 +180,16 @@ void setup() {

if(Parameters.getWifiMode() == WIFI_MODE_STA){
//-- Connect to an existing network
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV)
WiFi.mode(WIFI_MODE_STA);
#else
WiFi.mode(WIFI_STA);
#endif
#if defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
WiFi.config(Parameters.getWifiStaIP(), Parameters.getWifiStaGateway(), Parameters.getWifiStaSubnet(), IPAddress((uint32_t)0), IPAddress((uint32_t)0));
#else
WiFi.config(Parameters.getWifiStaIP(), Parameters.getWifiStaGateway(), Parameters.getWifiStaSubnet(), 0U, 0U);
#endif
WiFi.begin(Parameters.getWifiStaSsid(), Parameters.getWifiStaPassword());

//-- Wait a minute to connect
Expand All @@ -178,20 +211,37 @@ void setup() {

if(Parameters.getWifiMode() == WIFI_MODE_AP){
//-- Start AP
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
WiFi.mode(WIFI_MODE_AP); /* Default to WPA2 */
#else
WiFi.mode(WIFI_AP);
WiFi.encryptionType(AUTH_WPA2_PSK);
WiFi.encryptionType(AUTH_WPA2_PSK);
#endif
WiFi.softAP(Parameters.getWifiSsid(), Parameters.getWifiPassword(), Parameters.getWifiChannel());
localIP = WiFi.softAPIP();
wait_for_client();
}

//-- Boost power to Max
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
{
int8_t power;
esp_wifi_get_max_tx_power(&power);
esp_wifi_set_max_tx_power(power);
}
#else
WiFi.setOutputPower(20.5);
#endif
//-- MDNS
char mdsnName[256];
sprintf(mdsnName, "MavEsp8266-%d",localIP[3]);
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
sprintf(mdsnName, "MavEsp32-%d",localIP[3]);
mdns_init();
mdns_service_add(NULL, "http", "tcp", 80, NULL, 0);
#else
sprintf(mdsnName, "MavEsp8266-%d",localIP[3]);
MDNS.begin(mdsnName);
MDNS.addService("http", "tcp", 80);
#endif
//-- Initialize Comm Links
DEBUG_LOG("Start WiFi Bridge\n");
DEBUG_LOG("Local IP: %s\n", localIP.toString().c_str());
Expand Down
9 changes: 9 additions & 0 deletions src/mavesp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ MavESP8266Log::log(const char *format, ...) {
va_list arg;
va_start(arg, format);
char temp[1024];
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
size_t len = vsnprintf(temp, 1024, format, arg);
#else
size_t len = ets_vsnprintf(temp, 1024, format, arg);
#endif

#ifdef ENABLE_DEBUG
Serial1.print(temp);
#endif
Expand Down Expand Up @@ -160,7 +165,11 @@ MavESP8266Log::getLog(uint32_t* pStart, uint32_t* pLen) {
uint32_t
MavESP8266Log::getLogSize()
{
#if defined(ARDUINO_ESP32C3_DEV)
return std::min<uint32_t>(_log_position, _buffer_size);
#else
return min(_log_position, _buffer_size);
#endif
}

//---------------------------------------------------------------------------------
Expand Down
22 changes: 20 additions & 2 deletions src/mavesp8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,35 @@
#ifndef MAVESP8266_H
#define MAVESP8266_H

/*
* https://www.espressif.com/en/products/devkits
* ESP32 Series
* Modules: ESP32-WROOM-32E, ESP32-WROOM-32UE, ESP32­-WROOM­-DA, ESP32-WROVER-E, ESP32-WROVER-IE
*
* ESP32-S3 Series
* Modules: ESP32-S3-WROOM-1, ESP32-S3-WROOM-1U, ESP32-S3-WROOM-2, ESP32-S3-WROOM-2U
*
*/
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined (ARDUINO_ESP32C3_DEV)
#include <WiFi.h>
#include <Arduino.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <WiFiClient.h>
#include <WiFiUdp.h>

#undef F
#include <ardupilotmega/mavlink.h>


#if !defined(ARDUINO_ESP32_DEV) && !defined(ARDUINO_ESP32S3_DEV) && !defined(ARDUINO_ESP32C3_DEV)
extern "C" {
// Espressif SDK
#include "user_interface.h"
}
#endif

class MavESP8266Parameters;
class MavESP8266Component;
Expand All @@ -64,8 +82,8 @@ class MavESP8266GCS;

//-- TODO: This needs to come from the build system
#define MAVESP8266_VERSION_MAJOR 1
#define MAVESP8266_VERSION_MINOR 2
#define MAVESP8266_VERSION_BUILD 3
#define MAVESP8266_VERSION_MINOR 3
#define MAVESP8266_VERSION_BUILD 0
#define MAVESP8266_VERSION ((MAVESP8266_VERSION_MAJOR << 24) & 0xFF00000) | ((MAVESP8266_VERSION_MINOR << 16) & 0x00FF0000) | (MAVESP8266_VERSION_BUILD & 0xFFFF)

//-- Debug sent out to Serial1 (GPIO02), which is TX only (no RX).
Expand Down
4 changes: 4 additions & 0 deletions src/mavesp8266_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,9 @@ MavESP8266Component::_wifiReboot(MavESP8266Bridge* sender)
{
_sendStatusMessage(sender, MAV_SEVERITY_NOTICE, "Rebooting WiFi Bridge.");
delay(50);
#if defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32C3_DEV)
ESP.restart();
#else
ESP.reset();
#endif
}
Loading