Skip to content

Commit

Permalink
ESP32: Support HW
Browse files Browse the repository at this point in the history
  • Loading branch information
tekka007 committed May 26, 2017
1 parent 97ed21e commit 56e4df3
Show file tree
Hide file tree
Showing 23 changed files with 709 additions and 31 deletions.
22 changes: 19 additions & 3 deletions MyConfig.h
@@ -1,4 +1,4 @@
/*
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
Expand Down Expand Up @@ -1088,7 +1088,6 @@
*/
#define MY_SIGNAL_REPORT_ENABLED


/**************************************
* Ethernet Gateway Transport Defaults
***************************************/
Expand All @@ -1098,7 +1097,7 @@
//#define MY_GATEWAY_ENC28J60
//#define MY_GATEWAY_ESP8266
//#define MY_GATEWAY_LINUX

//#define MY_GATEWAY_ESP32
/**
* @def MY_PORT
* @brief The Ethernet TCP/UDP port to open on controller or gateway.
Expand Down Expand Up @@ -1221,6 +1220,23 @@
#define MY_ESP8266_SERIAL_MODE SERIAL_FULL
#endif

/**********************************
* ESP32 Defaults
***********************************/

/**
* @def MY_ESP32_SERIAL_MODE
* @brief Serial modes: SERIAL_FULL, SERIAL_RX_ONLY, SERIAL_TX_ONLY
*
* SERIAL_FULL: Default mode.
* SERIAL_TX_ONLY: allows to use RX (GPIO3) as a general purpose input/output.
* SERIAL_RX_ONLY: allows to use TX (GPIO1) as a general purpose input/output.
*/

#ifndef MY_ESP32_SERIAL_MODE
#define MY_ESP32_SERIAL_MODE SERIAL_FULL
#endif

/**************************************
* Linux Settings
***************************************/
Expand Down
13 changes: 8 additions & 5 deletions MySensors.h
Expand Up @@ -45,8 +45,7 @@
* @def MY_NODE_TYPE
* @brief Contain a string describing the class of sketch/node (gateway/repeater/node).
*/

#if defined(MY_GATEWAY_SERIAL) || defined(MY_GATEWAY_W5100) || defined(MY_GATEWAY_ENC28J60) || defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_LINUX) || defined(MY_GATEWAY_MQTT_CLIENT)
#if defined(MY_GATEWAY_SERIAL) || defined(MY_GATEWAY_W5100) || defined(MY_GATEWAY_ENC28J60) || defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) || defined(MY_GATEWAY_LINUX) || defined(MY_GATEWAY_MQTT_CLIENT)
#define MY_GATEWAY_FEATURE
#define MY_IS_GATEWAY (true)
#define MY_NODE_TYPE "GW"
Expand Down Expand Up @@ -91,6 +90,8 @@
// HARDWARE
#if defined(ARDUINO_ARCH_ESP8266)
#include "hal/architecture/MyHwESP8266.cpp"
#elif defined(ARDUINO_ARCH_ESP32)
#include "hal/architecture/MyHwESP32.cpp"
#elif defined(ARDUINO_ARCH_AVR)
#include "drivers/AVR/DigitalWriteFast/digitalWriteFast.h"
#include "hal/architecture/MyHwAVR.cpp"
Expand Down Expand Up @@ -230,8 +231,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#if !defined(MY_PORT)
#error You must define MY_PORT (controller or gatway port to open)
#endif
#if defined(MY_GATEWAY_ESP8266)
// GATEWAY - ESP8266
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
// GATEWAY - ESP8266 / ESP32
#include "core/MyGatewayTransportEthernet.cpp"
#elif defined(MY_GATEWAY_LINUX)
// GATEWAY - Generic Linux
Expand Down Expand Up @@ -312,7 +313,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
// RAM ROUTING TABLE
#if defined(MY_RAM_ROUTING_TABLE_FEATURE) && defined(MY_REPEATER_FEATURE)
// activate feature based on architecture
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(__linux__)
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(__linux__)
#define MY_RAM_ROUTING_TABLE_ENABLED
#elif defined(ARDUINO_ARCH_AVR)
#if defined(__avr_atmega1280__) || defined(__avr_atmega1284__) || defined(__avr_atmega2560__)
Expand Down Expand Up @@ -425,6 +426,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#if !defined(MY_CORE_ONLY)
#if defined(ARDUINO_ARCH_ESP8266)
#include "hal/architecture/MyMainESP8266.cpp"
#elif defined(ARDUINO_ARCH_ESP32)
#include "hal/architecture/MyMainESP32.cpp"
#elif defined(ARDUINO_ARCH_NRF5)
#include "hal/architecture/MyMainNRF5.cpp"
#elif defined(__linux__)
Expand Down
2 changes: 2 additions & 0 deletions core/MyCapabilities.h
Expand Up @@ -68,6 +68,8 @@
#define MY_CAP_ARCH "N"
#elif defined(ARDUINO_ARCH_ESP8266)
#define MY_CAP_ARCH "E"
#elif defined(ARDUINO_ARCH_ESP32)
#define MY_CAP_ARCH "F"
#elif defined(ARDUINO_ARCH_AVR)
#define MY_CAP_ARCH "A"
#else
Expand Down
36 changes: 27 additions & 9 deletions core/MyGatewayTransportEthernet.cpp
@@ -1,4 +1,4 @@
/*
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
Expand Down Expand Up @@ -40,7 +40,7 @@ typedef struct {
uint8_t idx;
} inputBuffer;

#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
// Some re-defines to make code more readable below
#define EthernetServer WiFiServer
#define EthernetClient WiFiClient
Expand All @@ -65,7 +65,7 @@ EthernetServer _ethernetServer(_ethernetGatewayPort);
#if defined(MY_GATEWAY_CLIENT_MODE)
static EthernetClient client = EthernetClient();
static inputBuffer inputString;
#elif defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_LINUX)
#elif defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) || defined(MY_GATEWAY_LINUX)
static EthernetClient clients[MY_GATEWAY_MAX_CLIENTS];
static bool clientsConnected[MY_GATEWAY_MAX_CLIENTS];
static inputBuffer inputString[MY_GATEWAY_MAX_CLIENTS];
Expand Down Expand Up @@ -103,7 +103,7 @@ bool gatewayTransportInit(void)
#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_ESP8266_SSID)
// Turn off access point
WiFi.mode (WIFI_STA);
WiFi.mode(WIFI_STA);
#if defined(MY_ESP8266_HOSTNAME)
WiFi.hostname(MY_ESP8266_HOSTNAME);
#endif
Expand All @@ -118,6 +118,24 @@ bool gatewayTransportInit(void)
MY_SERIALDEVICE.print(F("IP: "));
MY_SERIALDEVICE.println(WiFi.localIP());
#endif
#elif defined(MY_GATEWAY_ESP32)
#if defined(MY_ESP32_SSID)
// Turn off access point
WiFi.mode(WIFI_STA);
#if defined(MY_ESP32_HOSTNAME)
WiFi.setHostname(MY_ESP32_HOSTNAME);
#endif
#ifdef MY_IP_ADDRESS
WiFi.config(_ethernetGatewayIP, _gatewayIp, _subnetIp);
#endif
(void)WiFi.begin(MY_ESP32_SSID, MY_ESP32_PASSWORD);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
wait(500);
MY_SERIALDEVICE.print(F("."));
}
MY_SERIALDEVICE.print(F("IP: "));
MY_SERIALDEVICE.println(WiFi.localIP());
#endif
#elif defined(MY_GATEWAY_LINUX)
// Nothing to do here
#else
Expand All @@ -135,7 +153,7 @@ bool gatewayTransportInit(void)
MY_SERIALDEVICE.println(Ethernet.localIP());
// give the Ethernet interface a second to initialize
delay(1000);
#endif /* MY_GATEWAY_ESP8266 */
#endif /* MY_GATEWAY_ESP8266 / MY_GATEWAY_ESP32 */

#if defined(MY_GATEWAY_CLIENT_MODE)
#if defined(MY_GATEWAY_LINUX) && defined(MY_IP_ADDRESS)
Expand Down Expand Up @@ -212,7 +230,7 @@ bool gatewayTransportSend(MyMessage &message)
#endif
#else
// Send message to connected clients
#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) {
if (clients[i] && clients[i].connected()) {
nbytes += clients[i].write((uint8_t*)_ethernetMsg, strlen(_ethernetMsg));
Expand All @@ -226,7 +244,7 @@ bool gatewayTransportSend(MyMessage &message)
return (nbytes > 0);
}

#if (defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_LINUX)) && !defined(MY_GATEWAY_CLIENT_MODE)
#if (defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) || defined(MY_GATEWAY_LINUX)) && !defined(MY_GATEWAY_CLIENT_MODE)
bool _readFromClient(uint8_t i)
{
while (clients[i].connected() && clients[i].available()) {
Expand Down Expand Up @@ -344,7 +362,7 @@ bool gatewayTransportAvailable(void)
return true;
}
#else
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_LINUX)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) || defined(MY_GATEWAY_LINUX)
// ESP8266: Go over list of clients and stop any that are no longer connected.
// If the server has a new client connection it will be assigned to a free slot.
bool allSlotsOccupied = true;
Expand Down Expand Up @@ -421,7 +439,7 @@ MyMessage& gatewayTransportReceive(void)
return _ethernetMsg;
}

#if !defined(MY_IP_ADDRESS) && !defined(MY_GATEWAY_ESP8266) && !defined(MY_GATEWAY_LINUX)
#if !defined(MY_IP_ADDRESS) && !defined(MY_GATEWAY_ESP8266) && !defined(MY_GATEWAY_ESP32) && !defined(MY_GATEWAY_LINUX)
void gatewayTransportRenewIP(void)
{
/* renew/rebind IP address
Expand Down
6 changes: 3 additions & 3 deletions core/MyGatewayTransportMQTTClient.cpp
Expand Up @@ -26,7 +26,7 @@
IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
#endif

#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
#define EthernetClient WiFiClient
#if defined(MY_IP_ADDRESS)
IPAddress _gatewayIp(MY_IP_GATEWAY_ADDRESS);
Expand Down Expand Up @@ -96,7 +96,7 @@ bool reconnectMQTT(void)

bool gatewayTransportConnect(void)
{
#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
while (WiFi.status() != WL_CONNECTED) {
wait(500);
MY_SERIALDEVICE.print(F("."));
Expand Down Expand Up @@ -137,7 +137,7 @@ bool gatewayTransportInit(void)

_MQTT_client.setCallback(incomingMQTT);

#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
// Turn off access point
WiFi.mode(WIFI_STA);
#if defined(MY_ESP8266_HOSTNAME)
Expand Down
5 changes: 3 additions & 2 deletions core/MySensorsCore.cpp
@@ -1,4 +1,4 @@
/*
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
Expand Down Expand Up @@ -32,7 +32,8 @@
#endif


// message buffers
// message

MyMessage _msg; // Buffer for incoming messages
MyMessage _msgTmp; // Buffer for temporary messages (acks and nonces among others)

Expand Down
2 changes: 1 addition & 1 deletion core/MyTransport.cpp
@@ -1,4 +1,4 @@
/*
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
Expand Down
133 changes: 133 additions & 0 deletions drivers/ESP32/EEPROM/EEPROM.cpp
@@ -0,0 +1,133 @@
/*
EEPROM.cpp - ESP31B EEPROM emulation
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the ESP31B core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "Arduino.h"
#include "EEPROM.h"

extern "C" {
#include "esp_spi_flash.h"
}

EEPROMClass::EEPROMClass(uint32_t sector)
: _sector(sector)
, _data(0)
, _size(0)
, _dirty(false)
{
}

void EEPROMClass::begin(size_t size)
{
if (size <= 0) {
return;
}
if (size > SPI_FLASH_SEC_SIZE) {
size = SPI_FLASH_SEC_SIZE;
}

size = (size + 3) & (~3);

if (_data) {
delete[] _data;
}

_data = new uint8_t[size];
_size = size;

noInterrupts();
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
interrupts();
}

void EEPROMClass::end()
{
if (!_size) {
return;
}

commit();
if(_data) {
delete[] _data;
}
_data = 0;
_size = 0;
}


uint8_t EEPROMClass::read(int address)
{
if (address < 0 || (size_t)address >= _size) {
return 0;
}
if(!_data) {
return 0;
}

return _data[address];
}

void EEPROMClass::write(int address, uint8_t value)
{
if (address < 0 || (size_t)address >= _size) {
return;
}
if(!_data) {
return;
}

_data[address] = value;
_dirty = true;
}

bool EEPROMClass::commit()
{
bool ret = false;
if (!_size) {
return false;
}
if(!_dirty) {
return true;
}
if(!_data) {
return false;
}

noInterrupts();
if(spi_flash_erase_sector(_sector) == ESP_OK) {
if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data),
_size) == ESP_OK) {
_dirty = false;
ret = true;
}
}
interrupts();

return ret;
}

uint8_t * EEPROMClass::getDataPtr()
{
_dirty = true;
return &_data[0];
}

extern "C" uint32_t _SPIFFS_end;
EEPROMClass EEPROM(SPIFFS_END_ADDRESS / SPI_FLASH_SEC_SIZE);

0 comments on commit 56e4df3

Please sign in to comment.