Skip to content

Commit 51ce37d

Browse files
committed
Changed pin definition + added example
1 parent cd31356 commit 51ce37d

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

Diff for: cores/esp32/esp32-hal-gpio.c

+8-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "esp32-hal-gpio.h"
1616
#include "hal/gpio_hal.h"
1717
#include "soc/soc_caps.h"
18-
#include "pins_arduino.h"
1918

2019
// It fixes lack of pin definition for S3 and for any future SoC
2120
// this function works for ESP32, ESP32-S2 and ESP32-S3 - including the C3, it will return -1 for any pin
@@ -92,17 +91,13 @@ static InterruptHandle_t __pinInterruptHandlers[SOC_GPIO_PIN_COUNT] = {0,};
9291

9392
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
9493
{
95-
log_d("pin %d", pin);
9694
#ifdef BOARD_HAS_NEOPIXEL
97-
log_d("BOARD_HAS_NEOPIXEL");
9895
if (pin == LED_BUILTIN){
99-
log_d("pin == LED_BUILTIN; call __pinMode(%d)", NEOPIXEL_PIN);
100-
__pinMode(NEOPIXEL_PIN, mode);
96+
__pinMode(LED_BUILTIN-SOC_GPIO_PIN_COUNT, mode);
10197
return;
10298
}
10399
#endif
104100

105-
log_d("Normal operation");
106101
if (!GPIO_IS_VALID_GPIO(pin)) {
107102
log_e("Invalid pin selected");
108103
return;
@@ -137,26 +132,27 @@ extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
137132
}
138133
}
139134

135+
#ifdef BOARD_HAS_NEOPIXEL
140136
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val){
141137
rmt_data_t led_data[24];
142138
static rmt_obj_t* rmt_send = NULL;
143139
static bool initialized = false;
144140

145141
uint8_t _pin;
146142
if(pin == LED_BUILTIN){
147-
_pin = NEOPIXEL_PIN;
143+
_pin = LED_BUILTIN-SOC_GPIO_PIN_COUNT;
148144
}else{
149145
_pin = pin;
150146
}
151147

152148
if(!initialized){
153-
if((rmt_send = rmtInit(_pin, RMT_TX_MODE, RMT_MEM_64)) == NULL){
149+
if((rmt_send = rmtInit(_pin, RMT_TX_MODE, RMT_MEM_64)) == NULL){
154150
log_e("RGB LED driver initialization failed!");
155151
rmt_send = NULL;
156152
return;
157-
}
158-
rmtSetTick(rmt_send, 100);
159-
initialized = true;
153+
}
154+
rmtSetTick(rmt_send, 100);
155+
initialized = true;
160156
}
161157

162158
int color[] = {green_val, red_val, blue_val}; // Color coding is in order GREEN, RED, BLUE
@@ -181,6 +177,7 @@ void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_v
181177
}
182178
rmtWrite(rmt_send, led_data, 24);
183179
}
180+
#endif
184181

185182
extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val)
186183
{

Diff for: cores/esp32/esp32-hal-gpio.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern "C" {
2626

2727
#include "esp32-hal.h"
2828
#include "soc/soc_caps.h"
29+
#include "pins_arduino.h"
2930

3031
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
3132
#define NUM_OUPUT_PINS 46
@@ -71,7 +72,9 @@ extern "C" {
7172
#define digitalPinToDacChannel(pin) (((pin) == DAC_CHANNEL_1_GPIO_NUM)?0:((pin) == DAC_CHANNEL_2_GPIO_NUM)?1:-1)
7273

7374
void pinMode(uint8_t pin, uint8_t mode);
74-
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
75+
#ifdef BOARD_HAS_NEOPIXEL
76+
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
77+
#endif
7578
void digitalWrite(uint8_t pin, uint8_t val);
7679
int digitalRead(uint8_t pin);
7780

Diff for: libraries/ESP32/examples/GPIO/BlinkRGB/BlinkRGB.ino

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
BlinkRGB
3+
4+
Demonstrates usage of onboard RGB LED on some ESP dev boards.
5+
6+
Calling digitalWrite(LED_BUILTIN, HIGH) will use hidden RGB driver.
7+
8+
RGBLedWrite demonstrates controll of each channel:
9+
void RGBLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val)
10+
11+
WARNING: After using digitalWrite to drive RGB LED it will be impossible to drive the same pin
12+
with normal HIGH/LOW level
13+
*/
14+
//#define LED_BRIGHTNESS 64 // Change white brightness (max 255)
15+
16+
// the setup function runs once when you press reset or power the board
17+
18+
void setup() {
19+
// No need to initialize the RGB LED
20+
}
21+
22+
// the loop function runs over and over again forever
23+
void loop() {
24+
digitalWrite(LED_BUILTIN, HIGH); // Turn the RGB LED white
25+
delay(1000);
26+
log_w("LED LOW");
27+
digitalWrite(LED_BUILTIN, LOW); // Turn the RGB LED off
28+
delay(1000);
29+
30+
RGBLedWrite(LED_BUILTIN,255,0,0); // Red
31+
delay(1000);
32+
RGBLedWrite(LED_BUILTIN,0,255,0); // Green
33+
delay(1000);
34+
RGBLedWrite(LED_BUILTIN,0,0,255); // Blue
35+
delay(1000);
36+
RGBLedWrite(LED_BUILTIN,0,0,0); // Off / black
37+
delay(1000);
38+
}

Diff for: variants/esp32c3/pins_arduino.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
#define NUM_DIGITAL_PINS 22
99
#define NUM_ANALOG_INPUTS 6
1010

11-
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+1; // non-existing pin
11+
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+8;
1212
#define BUILTIN_LED LED_BUILTIN // backward compatibility
1313
#define LED_BUILTIN LED_BUILTIN
1414
#define BOARD_HAS_NEOPIXEL
15-
#define NEOPIXEL_PIN 8 // Actual NeoPixel GPIO pin
1615
#define LED_BRIGHTNESS 64
1716

1817
#define analogInputToDigitalPin(p) (((p)<NUM_ANALOG_INPUTS)?(analogChannelToDigitalPin(p)):-1)

Diff for: variants/esp32s2/pins_arduino.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
#define NUM_DIGITAL_PINS 48
99
#define NUM_ANALOG_INPUTS 20
1010

11-
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+1; // non-existing pin
11+
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+18; // GPIO pin for Saola-1 & DevKitM-1 = 18
12+
//static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+45; // GPIO pin for Kaluga = 45
1213
#define BUILTIN_LED LED_BUILTIN // backward compatibility
1314
#define LED_BUILTIN LED_BUILTIN
1415
#define BOARD_HAS_NEOPIXEL
15-
#define NEOPIXEL_PIN 18 // Actual NeoPixel GPIO pin for Saola-1 & DevKitM-1
16-
//#define NEOPIXEL_PIN 45 // Actual NeoPixel GPIO pin for Kaluga
1716
#define LED_BRIGHTNESS 64
1817

1918
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)

Diff for: variants/esp32s3/pins_arduino.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313

1414
// Some boards have too low voltage on this pin (board design bug)
1515
// Use different pin with 3V and connect with 48
16-
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+1; // non-existing pin
16+
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+48;
1717
#define BUILTIN_LED LED_BUILTIN // backward compatibility
1818
#define LED_BUILTIN LED_BUILTIN
1919
#define BOARD_HAS_NEOPIXEL
20-
#define NEOPIXEL_PIN 48 // Actual NeoPixel GPIO pin
2120
#define LED_BRIGHTNESS 64
2221

2322
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)

0 commit comments

Comments
 (0)