Skip to content

Commit

Permalink
CYD add Qwiic classes for P1/P3/CN1, invert LEDs and buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Dec 14, 2024
1 parent b1c2dc4 commit 3bfca52
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 35 deletions.
13 changes: 9 additions & 4 deletions boards/ESP32_CYD.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
'CRYPTO','SHA256','SHA512',
'TLS',
'TELNET',
'TERMINAL',
# 'NEOPIXEL',
'FILESYSTEM',
'BLUETOOTH',
Expand All @@ -72,7 +73,8 @@
'DEFINES+=-DESP_STACK_SIZE=25000',
'DEFINES+=-DJSVAR_MALLOC', # Allocate space for variables at jsvInit time
'DEFINES+=-DESPR_GRAPHICS_INTERNAL -DESPR_GRAPHICS_SELF_INIT', # ensure graphics instantiates itself
'DEFINES+=-DUSE_FONT_6X8 -DSPISENDMANY_BUFFER_SIZE=1600',
'DEFINES+=-DUSE_FONT_6X8 -DSPISENDMANY_BUFFER_SIZE=1600 -DLCD_SPI_BITRATE=32000000 -DESPR_TERMNINAL_NO_SCROLL',
'WRAPPERSOURCES += libs/misc/jswrap_esp32_cyd.c libs/misc/jswrap_qwiic.c',
'ESP32_FLASH_MAX=1572864'
]
}
Expand All @@ -99,10 +101,10 @@
};
devices = {
#https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/PINS.md
'LED1' : { 'pin' : 'D4' }, # FIXME swap polarity of LEDs
'LED1' : { 'pin' : 'D4' },
'LED2' : { 'pin' : 'D16' },
'LED3' : { 'pin' : 'D17' },
'BTN1' : { 'pin' : 'D0', "inverted":1, 'pinstate' : 'IN_PULLUP' }, # FIXME swap polarity of button
'BTN1' : { 'pin' : 'D0' },
'SD' : { 'pin_cs' : 'D5',
'pin_di' : 'D23',
'pin_do' : 'D19',
Expand Down Expand Up @@ -216,7 +218,10 @@ def get_pins():
pinutils.findpin(pins, "PD25", True)["functions"]["DAC_OUT1"]=0;
pinutils.findpin(pins, "PD26", True)["functions"]["DAC_OUT2"]=0;

pinutils.findpin(pins, "PD0", True)["functions"]["LED_1"]=0;
pinutils.findpin(pins, "PD4", True)["functions"]["NEGATED"]=0; # LED
pinutils.findpin(pins, "PD16", True)["functions"]["NEGATED"]=0;
pinutils.findpin(pins, "PD17", True)["functions"]["NEGATED"]=0;
pinutils.findpin(pins, "PD0", True)["functions"]["NEGATED"]=0; # BTN1

pinutils.findpin(pins, "PD10", True)["functions"]["USART0_TX"]=0;
pinutils.findpin(pins, "PD16", True)["functions"]["USART2_RX"]=0;
Expand Down
2 changes: 1 addition & 1 deletion boards/JOLTJS.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'DEFINES+=-DUSE_FONT_6X8 -DGRAPHICS_PALETTED_IMAGES -DGRAPHICS_ANTIALIAS -DESPR_PBF_FONTS',
'BOOTLOADER_SETTINGS_FAMILY=NRF52840',
'INCLUDE += -I$(ROOT)/libs/jolt.js',
'WRAPPERSOURCES += libs/joltjs/jswrap_jolt.c libs/joltjs/jswrap_qwiic.c',
'WRAPPERSOURCES += libs/joltjs/jswrap_jolt.c libs/misc/jswrap_qwiic.c',
'NRF_SDK15=1',
]
}
Expand Down
25 changes: 17 additions & 8 deletions libs/graphics/lcd_spi_unbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#include "jswrap_graphics.h"
#include "jshardware.h"

static int _pin_mosi;
static int _pin_clk;
static int _pin_cs;
static int _pin_dc;
static Pin _pin_mosi;
static Pin _pin_clk;
static Pin _pin_cs;
static Pin _pin_dc;
static int _colstart;
static int _rowstart;
static int _lastx=-1;
Expand Down Expand Up @@ -79,13 +79,17 @@ void lcdSendInitCmd_SPILCD() {
}

/// flush chunk buffer to screen
void lcd_flip(JsVar *parent) {
void lcd_flip() {
if(_chunk_index == 0) return;
jshPinSetValue(_pin_cs, 0);
flush_chunk_buffer();
jshPinSetValue(_pin_cs, 1);
}

void graphicsInternalFlip() {
lcd_flip();
}

void jshLCD_SPI_UNBUFInitInfo(JshLCD_SPI_UNBUFInfo *inf) {
inf->pinCS = PIN_UNDEFINED;
inf->pinDC = PIN_UNDEFINED;
Expand Down Expand Up @@ -115,7 +119,7 @@ bool jsspiPopulateOptionsInfo( JshLCD_SPI_UNBUFInfo *inf, JsVar *options){
"generate" : "jswrap_lcd_spi_unbuf_idle"
}*/
bool jswrap_lcd_spi_unbuf_idle() {
lcd_flip(NULL);
lcd_flip();
return false;
}

Expand Down Expand Up @@ -173,7 +177,7 @@ JsVar *jswrap_lcd_spi_unbuf_connect(JsVar *device, JsVar *options) {

// Create 'flip' fn
JsVar *fn;
fn = jsvNewNativeFunction((void (*)(void))lcd_flip, JSWAT_VOID|JSWAT_THIS_ARG);
fn = jsvNewNativeFunction((void (*)(void))lcd_flip, JSWAT_VOID);
jsvObjectSetChildAndUnLock(parent,"flip",fn);

return parent;
Expand Down Expand Up @@ -241,7 +245,7 @@ void disp_spi_transfer_addrwin(int x1, int y1, int x2, int y2) {
}

void lcd_spi_unbuf_setPixel(JsGraphics *gfx, int x, int y, unsigned int col) {
uint16_t color = (col>>8) | (col<<8);
uint16_t color = (col>>8) | (col<<8);
if (x!=_lastx+1 || y!=_lasty) {
jshPinSetValue(_pin_cs, 0);
disp_spi_transfer_addrwin(x, y, gfx->data.width, y+1);
Expand Down Expand Up @@ -272,7 +276,12 @@ void lcd_spi_unbuf_fillRect(JsGraphics *gfx, int x1, int y1, int x2, int y2, uns
_lasty=-1;
}

void lcd_spi_unbuf_scroll(JsGraphics *gfx, int xdir, int ydir, int x1, int y1, int x2, int y2) {
// don't even try scrolling at the moment - we could maybe adjust display registers but then we have to adjust x/y of any subsequent writes
}

void lcd_spi_unbuf_setCallbacks(JsGraphics *gfx) {
gfx->setPixel = lcd_spi_unbuf_setPixel;
gfx->fillRect = lcd_spi_unbuf_fillRect;
gfx->scroll = lcd_spi_unbuf_scroll;
}
77 changes: 77 additions & 0 deletions libs/misc/jswrap_esp32_cyd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2024 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* ESP32 CYD (cheap yellow display) implementation
* ----------------------------------------------------------------------------
*/
#include "jswrap_curio.h"
#include "jshardware.h"
#include "jsparse.h"

/* DO_NOT_INCLUDE_IN_DOCS - this is a special token for common.py */

/*JSON{
"type" : "variable",
"name" : "CN1",
"generate" : "jswrap_cyd_cn1",
"return" : ["JsVar","An object containing the pins for the CN1 connector"],
"return_object" : "Qwiic"
}
*/
JsVar *jswrap_cyd_cn1() {
JsVar *o = jspNewObject(0, "Qwiic");
if (!o) return 0;
jsvObjectSetChildAndUnLock(o, "sda", jsvNewFromPin(QWIIC0_PIN_SDA));
jsvObjectSetChildAndUnLock(o, "scl", jsvNewFromPin(QWIIC0_PIN_SCL));
jsvObjectSetChild(execInfo.root, "CN1", o);
return o;
}
/*JSON{
"type" : "variable",
"name" : "P1",
"generate" : "jswrap_cyd_p1",
"return" : ["JsVar","An object containing the pins for the P1 connector"],
"return_object" : "Qwiic"
}
*/
JsVar *jswrap_cyd_p1() {
JsVar *o = jspNewObject(0, "Qwiic");
if (!o) return 0;
jsvObjectSetChildAndUnLock(o, "sda", jsvNewFromPin(QWIIC1_PIN_SDA));
jsvObjectSetChildAndUnLock(o, "scl", jsvNewFromPin(QWIIC1_PIN_SCL));
jsvObjectSetChild(execInfo.root, "P1", o);
return o;
}
/*JSON{
"type" : "variable",
"name" : "P3",
"generate" : "jswrap_cyd_p3",
"return" : ["JsVar","An object containing the pins for the P3 connector"],
"return_object" : "Qwiic"
}
*/
JsVar *jswrap_cyd_p3() {
JsVar *o = jspNewObject(0, "Qwiic");
if (!o) return 0;
jsvObjectSetChildAndUnLock(o, "sda", jsvNewFromPin(QWIIC3_PIN_SDA));
jsvObjectSetChildAndUnLock(o, "scl", jsvNewFromPin(QWIIC3_PIN_SCL));
jsvObjectSetChildAndUnLock(o, "vcc", jsvNewFromPin(QWIIC3_PIN_VCC));
jsvObjectSetChild(execInfo.root, "P3", o);
return o;
}

/*JSON{
"type" : "init",
"generate" : "jswrap_cyd_init"
}*/
void jswrap_cyd_init() {
jshPinOutput(LED1_PININDEX,0); // LED1 is glowing by default?
jshPinOutput(LED3_PININDEX,0); // LED3 is on by default?
}
20 changes: 20 additions & 0 deletions libs/misc/jswrap_esp32_cyd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2024 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* ESP32 CYD (cheap yellow display)
* ----------------------------------------------------------------------------
*/

#include "jsvar.h"

JsVar *jswrap_cyd_cn1();
JsVar *jswrap_cyd_p1();
JsVar *jswrap_cyd_p3();
void jswrap_cyd_init();
File renamed without changes.
File renamed without changes.
26 changes: 18 additions & 8 deletions targets/esp32/jshardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ void jshPinSetState(
}
gpio_mode_t mode;
gpio_pull_mode_t pull_mode=GPIO_FLOATING;
bool negated = pinInfo[pin].port & JSH_PIN_NEGATED;
switch(state) {
case JSHPINSTATE_GPIO_OUT:
mode = GPIO_MODE_INPUT_OUTPUT;
Expand All @@ -306,18 +307,20 @@ void jshPinSetState(
break;
case JSHPINSTATE_GPIO_IN_PULLUP:
mode = GPIO_MODE_INPUT;
pull_mode=GPIO_PULLUP_ONLY;
pull_mode= negated ? GPIO_PULLDOWN_ONLY : GPIO_PULLUP_ONLY;
break;
case JSHPINSTATE_GPIO_IN_PULLDOWN:
mode = GPIO_MODE_INPUT;
pull_mode=GPIO_PULLDOWN_ONLY;
pull_mode= negated ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY;
break;
case JSHPINSTATE_GPIO_OUT_OPENDRAIN:
mode = GPIO_MODE_INPUT_OUTPUT_OD;
if (negated) jsError( "jshPinSetState: can't do Open Drain on negated pin");
break;
case JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP:
case JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP: // not possible if negated
mode = GPIO_MODE_INPUT_OUTPUT_OD;
pull_mode=GPIO_PULLUP_ONLY;
pull_mode= GPIO_PULLUP_ONLY;
if (negated) jsError( "jshPinSetState: can't do Open Drain on negated pin");
break;
default:
jsError( "jshPinSetState: Unexpected state: %d", state);
Expand Down Expand Up @@ -361,6 +364,7 @@ void jshPinSetValue(
Pin pin, //!< The pin to have its value changed.
bool value //!< The new value of the pin.
) {
if (pinInfo[pin].port & JSH_PIN_NEGATED) value=!value;
gpio_num_t gpioNum = pinToESP32Pin(pin);
#if ESP_IDF_VERSION_MAJOR>=5
gpio_iomux_out(gpioNum,SIG_GPIO_OUT_IDX,0); // reset pin to be GPIO in case it was used as rmt or something else
Expand All @@ -379,22 +383,27 @@ bool CALLED_FROM_INTERRUPT jshPinGetValue( // can be called at interrupt time
Pin pin //!< The pin to have its value read.
) {
gpio_num_t gpioNum = pinToESP32Pin(pin);
bool level = gpio_get_level(gpioNum);
return level;
bool value = gpio_get_level(gpioNum);
if (pinInfo[pin].port & JSH_PIN_NEGATED) value=!value;
return value;
}


JsVarFloat jshPinAnalog(Pin pin) {
if (pinInfo[pin].analog == JSH_ANALOG_NONE)
return NAN;
return (JsVarFloat) readADC(pin) / 4096;
JsVarFloat v = (JsVarFloat) readADC(pin) / 4096;
if (pinInfo[pin].port & JSH_PIN_NEGATED) v=1-v;
return v;
}


int jshPinAnalogFast(Pin pin) {
if (pinInfo[pin].analog == JSH_ANALOG_NONE)
return 0;
return readADC(pin) << 4;
int v = readADC(pin) << 4;
if (pinInfo[pin].port & JSH_PIN_NEGATED) v=65535-v;
return v;
}


Expand All @@ -406,6 +415,7 @@ JshPinFunction jshPinAnalogOutput(Pin pin,
JsVarFloat freq,
JshAnalogOutputFlags flags) { // if freq<=0, the default is used
UNUSED(flags);
if (pinInfo[pin].port & JSH_PIN_NEGATED) value=1-value;
if (value<0) value=0;
if (value>1) value=1;
if (!isfinite(freq)) freq=0;
Expand Down
27 changes: 13 additions & 14 deletions targets/esp32/jshardwareSpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void jshSPISetup(
mosi = inf->pinMOSI != PIN_UNDEFINED ? inf->pinMOSI : 13;
}
else {
dma_chan = 2;
dma_chan = 2;
sck = inf->pinSCK != PIN_UNDEFINED ? inf->pinSCK : 5;
miso = inf->pinMISO != PIN_UNDEFINED ? inf->pinMISO : 19;
mosi = inf->pinMOSI != PIN_UNDEFINED ? inf->pinMOSI : 23;
Expand All @@ -139,9 +139,9 @@ void jshSPISetup(
.queue_size=7, //We want to be able to queue 7 transactions at a time
.flags=flags
};
if(SPIChannels[channelPnt].spi){
SPIChannelReset(channelPnt);
jsWarn("spi was already in use, removed old assignment");
if(SPIChannels[channelPnt].spi) {
SPIChannelReset(channelPnt);
jsWarn("spi was already in use, removed old assignment");
}
esp_err_t ret=spi_bus_initialize(SPIChannels[channelPnt].HOST, &buscfg, dma_chan);
assert(ret==ESP_OK);
Expand Down Expand Up @@ -189,20 +189,19 @@ bool jshSPISendMany(IOEventFlags device, unsigned char *tx, unsigned char *rx, s
return true;
}
jshSPIWait(device);
int channelPnt = getSPIChannelPnt(device);
int channelPnt = getSPIChannelPnt(device);
esp_err_t ret;
memset(&spi_trans, 0, sizeof(spi_trans));
spi_trans.length=count*8;
spi_trans.tx_buffer=tx;
spi_trans.rx_buffer=rx;
memset(&spi_trans, 0, sizeof(spi_trans));
spi_trans.length=count*8;
spi_trans.tx_buffer=tx;
spi_trans.rx_buffer=rx;
spi_Sending = true;
ret=spi_device_queue_trans(SPIChannels[channelPnt].spi, &spi_trans, rx?0:portMAX_DELAY);

ret=spi_device_queue_trans(SPIChannels[channelPnt].spi, &spi_trans, rx?0:portMAX_DELAY);
if (ret != ESP_OK) {
spi_Sending = false;
jsExceptionHere(JSET_INTERNALERROR, "SPI Send Error %d", ret);
return false;
}
jsExceptionHere(JSET_INTERNALERROR, "SPI Send Error %d", ret);
return false;
}
jshSPIWait(device);
if(callback)callback();
return true;
Expand Down

0 comments on commit 3bfca52

Please sign in to comment.