I traced code deep into hardware setting code and change ESP8266 I2C from GPIO-4/5 to ESP07S GPIO-2/14. I noticed that this core use software to simulate I2C protocol, and figured that change made from 4/5 to 2/14 by way of pins_arduino.h
( C:\Users\ryo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\variants\nodemcu ) and recompile that. I2C use 2/14 pin works perfectly. Thing getting weird after I moved I2C from 4/5 to 2/14. pin 4/5 become no used. I tried to use it as GPIO, therefore, I set it as INPUT pin and connected them to push buttons, but it is not what I expected.
#define GPIO_4 4
void setup(){
Serial.begin(115200);
pinMode(GPIO_4, INPUT);
}
void loop(){
if(digitalRead(GPIO_4) == LOW) { Serial.println("BTN1 pressed"); }
delay(2000);
}
No matter I change code to detect HIGH or LOW. Nothing happened ! ( I connected button to pull low to GND if pushed.)
After that I study an excel file which define all GPIOs inside eagle_soc.h
( C:\Users\ryo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\tools\sdk\include ) which has a macro to redefine pin functions. For GPIO-4 it has two functions. one is FUNC_GPIO4(0), the other is FUNC_CLK_XTAL(1)
#define PERIPHS_IO_MUX_GPIO4_U (PERIPHS_IO_MUX + 0x3C)
#define FUNC_GPIO4 0
#define FUNC_CLK_XTAL 1
#define PERIPHS_IO_MUX_GPIO5_U (PERIPHS_IO_MUX + 0x40)
#define FUNC_GPIO5 0
#define FUNC_CLK_RTC 1
#define PIN_PULLUP_DIS(PIN_NAME) CLEAR_PERI_REG_MASK(PIN_NAME, PERIPHS_IO_MUX_PULLUP)
#define PIN_PULLUP_EN(PIN_NAME) SET_PERI_REG_MASK(PIN_NAME, PERIPHS_IO_MUX_PULLUP)
#define PIN_FUNC_SELECT(PIN_NAME, FUNC) do { \
WRITE_PERI_REG(PIN_NAME, \
(READ_PERI_REG(PIN_NAME) \
& (~(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S))) \
|( (((FUNC&BIT2)<<2)|(FUNC&0x3))<<PERIPHS_IO_MUX_FUNC_S) ); \
} while (0)
//}}
Therefore, I use following code tried to re-define pin 4 but without any luck. PLEASE HELP !!!!!!
#define GPIO_4 4
void setup(){
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U , FUNC_GPIO4); <== add
PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO4_U); <== add
Serial.begin(115200);
pinMode(GPIO_4, INPUT);
}
void loop(){
if(digitalRead(GPIO_4) == LOW) { Serial.println("BTN1 pressed"); }
delay(2000);
}