Skip to content

Commit

Permalink
Support Esp8266 PHY settings runtime modification (#2830)
Browse files Browse the repository at this point in the history
The initial PHY data is used initialize an ESP8266 chip and controls its behaviour.
This is stored in the ``phy_init`` partition and is loaded at startup.

Sometimes you may want to change that data for a certain ROM on the device without changing it for all ROMs on the device.

To do this, build with ``ENABLE_CUSTOM_PHY=1`` and add code to your application:

```
#include <esp_phy.h>

void customPhyInit(PhyInitData data)
{
   // Use methods of `data` to modify as required
 data.set_vdd33_const(0xff);
}
```

This PR supersedes #1491 and provides a more C++ mechanism for adjusting the configuration at runtime.
  • Loading branch information
mikee47 authored Jun 24, 2024
1 parent 9a097d8 commit cb8adf4
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 6 deletions.
24 changes: 24 additions & 0 deletions Sming/Arch/Esp8266/Components/esp8266/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,27 @@ support code.

Sming uses libraries from the ESP8266 NON-OS SDK version 3, imported as a submodule.
The header and linker files are provided by this Component.


.. envvar:: ENABLE_CUSTOM_PHY

Default: undefined (off)

The ``phy_init`` partition contains data which the ESP8266 SDK uses to initialise WiFi hardware at startup.

You may want to change settings for a certain ROM on the device without changing it for all ROMs on the device.
To do this, build with ``ENABLE_CUSTOM_PHY=1`` and add code to your application::

#include <esp_phy.h>

void customPhyInit(PhyInitData data)
{
// Use methods of `data` to modify as required
data.set_vdd33_const(0xff);
}

See :cpp:struct:`PhyInitData` for further details.


.. doxygenstruct:: PhyInitData
:members:
12 changes: 11 additions & 1 deletion Sming/Arch/Esp8266/Components/esp8266/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ ifeq ($(DISABLE_WIFI),1)
COMPONENT_DEPENDS += esp-lwip
endif


COMPONENT_RELINK_VARS += ENABLE_CUSTOM_PHY
ENABLE_CUSTOM_PHY ?= 0
ifeq ($(ENABLE_CUSTOM_PHY),1)
COMPONENT_CXXFLAGS += -DENABLE_CUSTOM_PHY=1
LDFLAGS += $(call Wrap,register_chipv6_phy)
endif


$(FLASH_INIT_DATA): $(SDK_BASE)/.submodule
$(Q) cp -f $(@D)/esp_init_data_default_v08.bin $@

Expand All @@ -29,7 +38,8 @@ export SDK_LIBDIR

COMPONENT_DOXYGEN_INPUT := \
include/gpio.h \
include/pwm.h
include/pwm.h \
include/esp_phy.h

# Crash handler hooks this so debugger can be invoked
EXTRA_LDFLAGS := $(call Wrap,system_restart_local)
Expand Down
43 changes: 43 additions & 0 deletions Sming/Arch/Esp8266/Components/esp8266/esp8266_phy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Adapted from Arduino for Sming.
Original copyright note is kept below.
phy.c - ESP8266 PHY initialization data
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 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
*/

// #ifdef ENABLE_CUSTOM_PHY

#include "include/esp_phy.h"
#include <Storage.h>
#include <esp_attr.h>
#include <sys/pgmspace.h>
#include <string.h>

extern "C" {
extern int __wrap_register_chipv6_phy(uint8_t* initData);
extern int __real_register_chipv6_phy(uint8_t* initData);
}

int ICACHE_RAM_ATTR __wrap_register_chipv6_phy(uint8_t* initData)
{
if(initData != NULL) {
PhyInitData data{initData};
customPhyInit(data);
}
return __real_register_chipv6_phy(initData);
}

// #endif /* ENABLE_CUSTOM_PHY */
Loading

0 comments on commit cb8adf4

Please sign in to comment.