Skip to content

Commit c9c1189

Browse files
Merge pull request #65 from adafruit/pb-s3-drive-strength
ESP32-S3: fix GPIO strength, dial down bit clock to ~17.8 MHz
2 parents 9a76ee2 + 4a23a0b commit c9c1189

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit Protomatter
2-
version=1.6.0
2+
version=1.6.1
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
55
sentence=A library for Adafruit RGB LED matrices.

src/arch/esp32-s3.h

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
#if defined(CONFIG_IDF_TARGET_ESP32S3)
2727

28+
#define GPIO_DRIVE_STRENGTH GPIO_DRIVE_CAP_3
29+
#define LCD_CLK_PRESCALE 9 // 8, 9, 10 allowed. Bit clock = 160 MHz / this.
30+
2831
#if defined(CIRCUITPY) // COMPILING FOR CIRCUITPYTHON --------------------
2932
#include "components/esp_rom/include/esp_rom_sys.h"
3033
#include "components/heap/include/esp_heap_caps.h"
@@ -62,24 +65,25 @@
6265
// dmaSetupTime (measured in blast_byte()) measures the number of timer
6366
// cycles to set up and trigger the DMA transfer...
6467
static uint32_t dmaSetupTime = 100;
65-
// ...then, the version of _PM_timerGetCount() here uses that figure as a
66-
// starting point, plus the known constant DMA transfer speed (20 MHz) and
67-
// timer frequency (40 MHz), i.e. 2 cycles/column, to return a fair estimate
68-
// of the one-scanline transfer time, from which everything is extrapolated:
68+
// ...then, the version of _PM_timerGetCount() here uses that as a starting
69+
// point, plus the known constant DMA xfer speed (160/LCD_CLK_PRESCALE MHz)
70+
// and timer frequency (40 MHz), to return an estimate of the one-scanline
71+
// transfer time, from which everything is extrapolated:
6972
IRAM_ATTR inline uint32_t _PM_timerGetCount(Protomatter_core *core) {
7073
// Time estimate seems to come in a little high, so the -10 here is an
7174
// empirically-derived fudge factor that may yield ever-so-slightly better
7275
// refresh in some edge cases. If visual glitches are encountered, might
7376
// need to dial back this number a bit or remove it.
74-
return dmaSetupTime + core->chainBits * 2 - 10;
77+
return dmaSetupTime + core->chainBits * 40 * LCD_CLK_PRESCALE / 160 - 10;
7578
}
7679
// Note that dmaSetupTime can vary from line to line, potentially influenced
7780
// by interrupts, nondeterministic DMA channel clearing times, etc., which is
7881
// why we don't just use a constant value. Each scanline might show for a
7982
// slightly different length of time, but duty cycle scales with this so it's
8083
// perceptually consistent; don't see bright or dark rows.
8184

82-
#define _PM_minMinPeriod (200 + (uint32_t)core->chainBits * 2)
85+
#define _PM_minMinPeriod \
86+
(200 + (uint32_t)core->chainBits * 40 * LCD_CLK_PRESCALE / 160)
8387

8488
#if (ESP_IDF_VERSION_MAJOR == 5)
8589
#include <esp_private/periph_ctrl.h>
@@ -125,10 +129,11 @@ IRAM_ATTR static void blast_long(Protomatter_core *core, uint32_t *data) {}
125129
static void pinmux(int8_t pin, uint8_t signal) {
126130
esp_rom_gpio_connect_out_signal(pin, signal, false, false);
127131
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[pin], PIN_FUNC_GPIO);
128-
gpio_set_drive_capability((gpio_num_t)pin, GPIO_DRIVE_CAP_MAX);
132+
gpio_set_drive_capability((gpio_num_t)pin, GPIO_DRIVE_STRENGTH);
129133
}
130134

131135
#if defined(ARDUINO) // COMPILING FOR ARDUINO ------------------------------
136+
132137
// LCD_CAM requires a complete replacement of the "blast" functions in order
133138
// to use the DMA-based peripheral.
134139
#define _PM_CUSTOM_BLAST // Disable blast_*() functions in core.c
@@ -172,11 +177,17 @@ void _PM_timerInit(Protomatter_core *core) {
172177
esp_rom_delay_us(100);
173178

174179
// Configure LCD clock
175-
LCD_CAM.lcd_clock.clk_en = 1; // Enable clock
176-
LCD_CAM.lcd_clock.lcd_clk_sel = 3; // PLL160M source
177-
LCD_CAM.lcd_clock.lcd_clkm_div_a = 1; // 1/1 fractional divide,
178-
LCD_CAM.lcd_clock.lcd_clkm_div_b = 1; // plus '7' below yields...
179-
LCD_CAM.lcd_clock.lcd_clkm_div_num = 7; // 1:8 prescale (20 MHz CLK)
180+
LCD_CAM.lcd_clock.clk_en = 1; // Enable clock
181+
LCD_CAM.lcd_clock.lcd_clk_sel = 3; // PLL160M source
182+
LCD_CAM.lcd_clock.lcd_clkm_div_a = 1; // 1/1 fractional divide,
183+
LCD_CAM.lcd_clock.lcd_clkm_div_b = 1; // plus prescale below yields...
184+
#if LCD_CLK_PRESCALE == 8
185+
LCD_CAM.lcd_clock.lcd_clkm_div_num = 7; // 1:8 prescale (20 MHz CLK)
186+
#elif LCD_CLK_PRESCALE == 9
187+
LCD_CAM.lcd_clock.lcd_clkm_div_num = 8; // 1:9 prescale (17.8 MHz CLK)
188+
#else
189+
LCD_CAM.lcd_clock.lcd_clkm_div_num = 9; // 1:10 prescale (16 MHz CLK)
190+
#endif
180191
LCD_CAM.lcd_clock.lcd_ck_out_edge = 0; // PCLK low in first half of cycle
181192
LCD_CAM.lcd_clock.lcd_ck_idle_edge = 0; // PCLK low idle
182193
LCD_CAM.lcd_clock.lcd_clk_equ_sysclk = 1; // PCLK = CLK (ignore CLKCNT_N)
@@ -212,10 +223,10 @@ void _PM_timerInit(Protomatter_core *core) {
212223
for (int i = 0; i < 6; i++)
213224
pinmux(core->rgbPins[i], signal[i]);
214225
pinmux(core->clockPin, LCD_PCLK_IDX);
215-
gpio_set_drive_capability(core->latch.pin, GPIO_DRIVE_CAP_MAX);
216-
gpio_set_drive_capability(core->oe.pin, GPIO_DRIVE_CAP_MAX);
226+
gpio_set_drive_capability(core->latch.pin, GPIO_DRIVE_STRENGTH);
227+
gpio_set_drive_capability(core->oe.pin, GPIO_DRIVE_STRENGTH);
217228
for (uint8_t i = 0; i < core->numAddressLines; i++) {
218-
gpio_set_drive_capability(core->addr[i].pin, GPIO_DRIVE_CAP_MAX);
229+
gpio_set_drive_capability(core->addr[i].pin, GPIO_DRIVE_STRENGTH);
219230
}
220231

221232
// Disable LCD_CAM interrupts, clear any pending interrupt
@@ -256,6 +267,7 @@ void _PM_timerInit(Protomatter_core *core) {
256267
}
257268

258269
#elif defined(CIRCUITPY) // COMPILING FOR CIRCUITPYTHON --------------------
270+
259271
// LCD_CAM requires a complete replacement of the "blast" functions in order
260272
// to use the DMA-based peripheral.
261273
#define _PM_CUSTOM_BLAST // Disable blast_*() functions in core.c
@@ -312,8 +324,14 @@ static void _PM_timerInit(Protomatter_core *core) {
312324
LCD_CAM.lcd_clock.clk_en = 1; // Enable clock
313325
LCD_CAM.lcd_clock.lcd_clk_sel = 3; // PLL160M source
314326
LCD_CAM.lcd_clock.lcd_clkm_div_a = 1; // 1/1 fractional divide,
315-
LCD_CAM.lcd_clock.lcd_clkm_div_b = 1; // plus '7' below yields...
327+
LCD_CAM.lcd_clock.lcd_clkm_div_b = 1; // plus prescale below yields...
328+
#if LCD_CLK_PRESCALE == 8
316329
LCD_CAM.lcd_clock.lcd_clkm_div_num = 7; // 1:8 prescale (20 MHz CLK)
330+
#elif LCD_CLK_PRESCALE == 9
331+
LCD_CAM.lcd_clock.lcd_clkm_div_num = 8; // 1:9 prescale (17.8 MHz CLK)
332+
#else
333+
LCD_CAM.lcd_clock.lcd_clkm_div_num = 9; // 1:10 prescale (16 MHz CLK)
334+
#endif
317335
LCD_CAM.lcd_clock.lcd_ck_out_edge = 0; // PCLK low in first half of cycle
318336
LCD_CAM.lcd_clock.lcd_ck_idle_edge = 0; // PCLK low idle
319337
LCD_CAM.lcd_clock.lcd_clk_equ_sysclk = 1; // PCLK = CLK (ignore CLKCNT_N)
@@ -349,10 +367,10 @@ static void _PM_timerInit(Protomatter_core *core) {
349367
for (int i = 0; i < 6; i++)
350368
pinmux(core->rgbPins[i], signal[i]);
351369
pinmux(core->clockPin, LCD_PCLK_IDX);
352-
gpio_set_drive_capability(core->latch.pin, GPIO_DRIVE_CAP_MAX);
353-
gpio_set_drive_capability(core->oe.pin, GPIO_DRIVE_CAP_MAX);
370+
gpio_set_drive_capability(core->latch.pin, GPIO_DRIVE_STRENGTH);
371+
gpio_set_drive_capability(core->oe.pin, GPIO_DRIVE_STRENGTH);
354372
for (uint8_t i = 0; i < core->numAddressLines; i++) {
355-
gpio_set_drive_capability(core->addr[i].pin, GPIO_DRIVE_CAP_MAX);
373+
gpio_set_drive_capability(core->addr[i].pin, GPIO_DRIVE_STRENGTH);
356374
}
357375

358376
// Disable LCD_CAM interrupts, clear any pending interrupt

0 commit comments

Comments
 (0)