From 74db7a9da0c90bf83692047c5984605eeb78352e Mon Sep 17 00:00:00 2001 From: Tom Ball Date: Fri, 2 Feb 2024 19:01:55 -0800 Subject: [PATCH] multibutton working! --- libs/st7735/config_nrf.cpp | 40 ----- libs/st7735/config_nrf.h | 50 ++---- libs/st7735/panic.cpp | 345 ------------------------------------- libs/st7735/pxt.json | 1 - pxtarget.json | 4 +- 5 files changed, 17 insertions(+), 423 deletions(-) delete mode 100644 libs/st7735/config_nrf.cpp delete mode 100644 libs/st7735/panic.cpp diff --git a/libs/st7735/config_nrf.cpp b/libs/st7735/config_nrf.cpp deleted file mode 100644 index ef51b469947..00000000000 --- a/libs/st7735/config_nrf.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "pxt.h" - -#include "config_nrf.h" - -namespace pxt { -static CODAL_PIN **pinPtrs; -static uint8_t numPinPtrs; -static uint8_t pinPos[DEV_NUM_PINS]; - -CODAL_PIN *myGetPin(int id) { - - id &= CFG_PIN_NAME_MSK; - - if (id >= DEV_NUM_PINS) - soft_panic(PANIC_NO_SUCH_PIN); - - // we could use lookupComponent() here - it would be slightly slower - - int ptr = pinPos[id]; - if (ptr == 0) { - pinPtrs = (CODAL_PIN **)realloc(pinPtrs, (numPinPtrs + 1) * sizeof(void *)); - bool isAnalog = IS_ANALOG_PIN(id); - // GCTODO - pinPtrs[numPinPtrs++] = - new CODAL_PIN(DEVICE_ID_IO_P0 + id, (PinName)id, - isAnalog ? PIN_CAPABILITY_AD : PIN_CAPABILITY_DIGITAL); - ptr = numPinPtrs; - pinPos[id] = ptr; - } - return pinPtrs[ptr - 1]; -} - -CODAL_PIN *myLookupPin(int pinName) { - if (pinName < 0 || pinName == 0xff) - return NULL; - pinName &= CFG_PIN_NAME_MSK; - return myGetPin(pinName); -} - -} \ No newline at end of file diff --git a/libs/st7735/config_nrf.h b/libs/st7735/config_nrf.h index 4efa4fc495f..b7bc35f61d1 100644 --- a/libs/st7735/config_nrf.h +++ b/libs/st7735/config_nrf.h @@ -1,6 +1,9 @@ #include "NRF52Pin.h" #include "NRF52SPI.h" +#define CODAL_PIN NRF52Pin +#define CODAL_SPI NRF52SPI + #define MY_DISPLAY_WIDTH 160 #define MY_DISPLAY_HEIGHT 128 #define MY_DISPLAY_TYPE 4242 // smart display @@ -8,18 +11,18 @@ #define MY_DISPLAY_CFG1 0x00000603 #define MY_DISPLAY_CFG2 8 -#define MY_PIN_BTNMX_LATCH 9 // DAL.P0_9 -#define MY_PIN_BTNMX_CLOCK 32 // DAL.P1_0 -#define MY_PIN_BTNMX_DATA 1 // DAL.P0_1 +#define MY_PIN_BTNMX_LATCH &uBit.io.P9 // 9 // DAL.P0_9 EC P9 +#define MY_PIN_BTNMX_CLOCK &uBit.io.P20 // 32 // DAL.P1_0 EC P20 +#define MY_PIN_BTNMX_DATA &uBit.io.P14 // 1 // DAL.P0_1 EC P14 -#define MY_PIN_DISPLAY_SCK 17 // DAL.P0_17 -#define MY_PIN_DISPLAY_MOSI 13 // DAL.P0_13 -#define MY_PIN_DISPLAY_MISO 1 // DAL.P0_1 -#define MY_PIN_DISPLAY_BL 26 // DAL.P0_26 -#define MY_PIN_DISPLAY_DC 10 // DAL.P0_10 -#define MY_PIN_DISPLAY_RST 34 // DAL.P1_2 -#define MY_PIN_DISPLAY_CS 0xff // not connected -#define MY_PIN_LED 0xff // not connected +#define MY_PIN_DISPLAY_SCK &uBit.io.P13 // 17 // DAL.P0_17 EC P13 +#define MY_PIN_DISPLAY_MOSI &uBit.io.P15 // 13 // DAL.P0_13 EC P15 +#define MY_PIN_DISPLAY_MISO &uBit.io.P14 // 1 // DAL.P0_1 EC P14 +#define MY_PIN_DISPLAY_BL &uBit.io.P19 // 26 // DAL.P0_26 EC P19 +#define MY_PIN_DISPLAY_DC &uBit.io.P8 // 10 // DAL.P0_10 EC P8 +#define MY_PIN_DISPLAY_RST &uBit.io.P16 // DAL.P1_2 EC P6 +#define MY_PIN_DISPLAY_CS ((CODAL_PIN*)NULL) // 0xff // not connected +#define MY_PIN_LED ((CODAL_PIN*)NULL) // 0xff // not connected // #define CFG_PIN_NAME_MSK 0xffff #undef DEV_NUM_PINS @@ -30,37 +33,14 @@ #undef PIN #undef LOOKUP_PIN #define PIN(name) MY_PIN_##name -#define LOOKUP_PIN(name) pxt::myLookupPin(PIN(name)) - -#define DEV_PWM_PINS 0x0000ffffffffULL // all pins are PWM pins it seems -#define DEV_AIN_PINS 0x0000f000001fULL - -// Codal doesn't yet distinguish between PWM and AIN -#define DEV_ANALOG_PINS (DEV_PWM_PINS | DEV_AIN_PINS) - -#undef IS_ANALOG_PIN -#define IS_ANALOG_PIN(id) ((DEV_ANALOG_PINS >> (id)) & 1) - -#define CODAL_PIN NRF52Pin -#define CODAL_SPI NRF52SPI -//#define CODAL_I2C NRF52I2C -//#define CODAL_TIMER Timer -//#define CODAL_SERIAL NRF52Serial +#define LOOKUP_PIN(name) PIN(name) // pxt::myLookupPin(PIN(name)) #define PXT_INTERNAL_KEY_UP 2050 #define PXT_INTERNAL_KEY_DOWN 2051 #define DEVICE_ID_FIRST_BUTTON 4000 -typedef CODAL_PIN *DigitalInOutPin; -typedef CODAL_PIN *AnalogInOutPin; -typedef CODAL_PIN *AnalogInPin; -typedef CODAL_PIN *AnalogOutPin; -typedef CODAL_PIN *PwmPin; -typedef CODAL_PIN *PwmOnlyPin; namespace pxt { - CODAL_PIN *myGetPin(int id); - CODAL_PIN *myLookupPin(int pinName); uint32_t readButtonMultiplexer(int bits); void disableButtonMultiplexer(); } diff --git a/libs/st7735/panic.cpp b/libs/st7735/panic.cpp deleted file mode 100644 index 0bf02b476aa..00000000000 --- a/libs/st7735/panic.cpp +++ /dev/null @@ -1,345 +0,0 @@ -#include "pxt.h" - -#include "Pin.h" -#define PinCompat codal::Pin - -#include "config_nrf.h" - -// This adds about 1.2k of binary size, but allows for displaying -// panic codes regardless of heap state etc. with IRQs disabled. - -#define ST7735_NOP 0x00 -#define ST7735_SWRESET 0x01 -#define ST7735_RDDID 0x04 -#define ST7735_RDDST 0x09 - -#define ST7735_SLPIN 0x10 -#define ST7735_SLPOUT 0x11 -#define ST7735_PTLON 0x12 -#define ST7735_NORON 0x13 - -#define ST7735_INVOFF 0x20 -#define ST7735_INVON 0x21 -#define ST7735_DISPOFF 0x28 -#define ST7735_DISPON 0x29 -#define ST7735_CASET 0x2A -#define ST7735_RASET 0x2B -#define ST7735_RAMWR 0x2C -#define ST7735_RAMRD 0x2E - -#define ST7735_PTLAR 0x30 -#define ST7735_COLMOD 0x3A -#define ST7735_MADCTL 0x36 - -#define ST7735_FRMCTR1 0xB1 -#define ST7735_FRMCTR2 0xB2 -#define ST7735_FRMCTR3 0xB3 -#define ST7735_INVCTR 0xB4 - -#define ST7735_GMCTRP1 0xE0 -#define ST7735_GMCTRN1 0xE1 - -#define MADCTL_MY 0x80 -#define MADCTL_MX 0x40 -#define MADCTL_MV 0x20 -#define MADCTL_ML 0x10 -#define MADCTL_RGB 0x00 -#define MADCTL_BGR 0x08 -#define MADCTL_MH 0x04 - -namespace _pxt_panic { - -// target_panic has been called -static bool panicMode = false; - -#define DELAY 0x80 - -class ST7735 { - Pin *sck, *mosi, *dc, *cs; - - void sendBytes(uint8_t *ptr, uint32_t len); - void sendCmd(uint8_t *buf, int len); - void sendCmdSeq(const uint8_t *buf); - void configure(uint8_t madctl, uint32_t frmctr1); - - public: - uint16_t width, height; - void fill(int color, int numpixels); - void setAddrWindow(int x, int y, int w, int h); - void init(); - void drawNumber(int idx, int x, int y, int color); -}; - -// clang-format off -static const uint8_t initCmds[] = { - ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay - 120, // 150 ms delay - ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay - 120, // 500 ms delay - ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay - ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay: - 0x03, // 12-bit color - - ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay - 10, // 10 ms delay - ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay - 10, - 0, 0 // END -}; -// clang-format on - -static const uint8_t numbers[] = { - 0x06, 0x09, 0x09, 0x09, 0x06, // 0 - 0x04, 0x06, 0x04, 0x04, 0x0e, // 1 - 0x07, 0x08, 0x06, 0x01, 0x0f, // 2 - 0x0f, 0x08, 0x04, 0x09, 0x06, // 3 - 0x0c, 0x0a, 0x09, 0x1f, 0x08, // 4 - 0x1f, 0x01, 0x0f, 0x10, 0x0f, // 5 - 0x08, 0x04, 0x0e, 0x11, 0x0e, // 6 - 0x1f, 0x08, 0x04, 0x02, 0x01, // 7 - 0x0e, 0x11, 0x0e, 0x11, 0x0e, // 8 - 0x0e, 0x11, 0x0e, 0x04, 0x02, // 9 - 0x11, 0x00, 0x0e, 0x1b, 0x11, // :( - // 0x11, 0x04, 0x04, 0x0a, 0x11, // :( -}; - -void ST7735::sendBytes(uint8_t *ptr, uint32_t len) { - uint8_t mask = 0, b; - for (;;) { - if (!mask) { - if (!len--) - break; - mask = 0x80; - b = *ptr++; - } - mosi->setDigitalValue((b & mask) ? 1 : 0); - sck->setDigitalValue(1); - mask >>= 1; - sck->setDigitalValue(0); - } -} - -void ST7735::fill(int color, int numpixels) { - uint8_t cmd[20] = {ST7735_RAMWR}; - sendCmd(cmd, 1); - - dc->setDigitalValue(1); - if (cs) - cs->setDigitalValue(0); - - cmd[0] = color >> 4; - cmd[1] = (color << 4) | (color >> 8); - cmd[2] = color; - - int n = (numpixels + 1) >> 1; - while (n--) { - sendBytes(cmd, 3); - } - - if (cs) - cs->setDigitalValue(1); -} - -void ST7735::sendCmd(uint8_t *buf, int len) { - // make sure cmd isn't on stack - dc->setDigitalValue(0); - if (cs) - cs->setDigitalValue(0); - sendBytes(buf, 1); - dc->setDigitalValue(1); - len--; - buf++; - if (len > 0) - sendBytes(buf, len); - if (cs) - cs->setDigitalValue(1); -} - -static void busy_wait_us(int ms) { - target_wait_us(ms); - /* - // this is for 120MHz - while (ms--) { - for (int i = 0; i < 30; ++i) - asm volatile("nop"); - } - */ -} - -void ST7735::sendCmdSeq(const uint8_t *buf) { - uint8_t cmdBuf[32]; - - while (*buf) { - cmdBuf[0] = *buf++; - int v = *buf++; - int len = v & ~DELAY; - // note that we have to copy to RAM - memcpy(cmdBuf + 1, buf, len); - sendCmd(cmdBuf, len + 1); - buf += len; - if (v & DELAY) { - busy_wait_us(1000 * *buf++); - } - } -} - -void ST7735::setAddrWindow(int x, int y, int w, int h) { - w += x - 1; - h += y - 1; - uint8_t cmd0[] = {ST7735_RASET, 0, (uint8_t)x, (uint8_t)(w >> 8), (uint8_t)w}; - uint8_t cmd1[] = {ST7735_CASET, 0, (uint8_t)y, (uint8_t)(h >> 8), (uint8_t)h}; - sendCmd(cmd1, sizeof(cmd1)); - sendCmd(cmd0, sizeof(cmd0)); -} - -void ST7735::init() { - mosi = LOOKUP_PIN(DISPLAY_MOSI); - sck = LOOKUP_PIN(DISPLAY_SCK); - cs = LOOKUP_PIN(DISPLAY_CS); - dc = LOOKUP_PIN(DISPLAY_DC); - auto bl = LOOKUP_PIN(DISPLAY_BL); - auto rst = LOOKUP_PIN(DISPLAY_RST); - - if (cs) - cs->setDigitalValue(1); - dc->setDigitalValue(1); - - if (bl) { - bl->setDigitalValue(1); - } - - if (rst) { - rst->setDigitalValue(0); - busy_wait_us(20 * 1000); - rst->setDigitalValue(1); - busy_wait_us(20 * 1000); - } - - uint32_t cfg0 = MY_DISPLAY_CFG0; - uint32_t frmctr1 = MY_DISPLAY_CFG1; - auto madctl = cfg0 & 0xff; - - sendCmdSeq(initCmds); - configure(madctl, frmctr1); - - width = MY_DISPLAY_WIDTH; - height = MY_DISPLAY_HEIGHT; -} - -void ST7735::configure(uint8_t madctl, uint32_t frmctr1) { - uint8_t cmd0[] = {ST7735_MADCTL, madctl}; - uint8_t cmd1[] = {ST7735_FRMCTR1, (uint8_t)(frmctr1 >> 16), (uint8_t)(frmctr1 >> 8), - (uint8_t)frmctr1}; - sendCmd(cmd0, sizeof(cmd0)); - sendCmd(cmd1, cmd1[3] == 0xff ? 3 : 4); -} - -#define SIZE 4 - -void ST7735::drawNumber(int idx, int x, int y, int color) { - const uint8_t *src = &numbers[idx * 5]; - for (int i = 0; i < 5; i++) { - uint8_t ch = *src++; - for (int j = 0; j < 5; j++) { - if (ch & (1 << j)) { - setAddrWindow(x + j * SIZE, y + i * SIZE, SIZE - 1, SIZE - 1); - fill(color, (SIZE - 1) * (SIZE - 1)); - } - } - } -} - -static void drawPanic(int code) { - if (!LOOKUP_PIN(DISPLAY_MOSI)) - return; - - ST7735 display; - - display.init(); - display.setAddrWindow(0, 0, display.width, display.height); - display.fill(0, display.width * display.height); - - display.drawNumber(10, 70, 20, 0xf00); - int x = 50; - int y = 60; - display.drawNumber((code / 100) % 10, x, y, 0xff0); - x += 24; - display.drawNumber((code / 10) % 10, x, y, 0xff0); - x += 24; - display.drawNumber((code / 1) % 10, x, y, 0xff0); - x += 24; -} - -extern "C" __attribute__((weak)) void platform_panic(int statusCode) {} - -// is is already defined in pxt-microbit - -/* -extern "C" void target_panic(int statusCode) { - __disable_irq(); // low-level disable - target_disable_irq(); // make sure they stay disabled in DMESG() - - DMESG("*** CODAL PANIC : [%d]", statusCode); - - __disable_irq(); // disable IRQ after DMESG() again just to make sure - - if (panicMode) { - // avoid recursive panic invocation - while (1) { - } - } - - gcFreeze(); - - // remember first panic code - panicMode = true; - - drawPanic(statusCode); - - platform_panic(statusCode); - - auto led = LOOKUP_PIN(LED); - - const int unit = 100000; - const int dit = unit; - const int dat = 3 * unit; - const int intra = unit; - const int inter = 2 * unit; - const int word = 6 * unit; - while (1) { - if (led) { - // SOS - // . . . - for (int i = 0; i < 3; ++i) { - led->setDigitalValue(1); - busy_wait_us(dit); - led->setDigitalValue(0); - busy_wait_us(intra); - } - // inter character space - busy_wait_us(inter); - // - - - - for (int i = 0; i < 3; ++i) { - led->setDigitalValue(1); - busy_wait_us(dat); - led->setDigitalValue(0); - busy_wait_us(intra); - } - // inter character space - busy_wait_us(inter); - // . . . - for (int i = 0; i < 3; ++i) { - led->setDigitalValue(1); - busy_wait_us(dit); - led->setDigitalValue(0); - busy_wait_us(intra); - } - // inter character space - busy_wait_us(word); - } - } -} - */ - - -} // namespace _pxt_panic \ No newline at end of file diff --git a/libs/st7735/pxt.json b/libs/st7735/pxt.json index 8cfe20f1dcc..dd2b87f8d57 100644 --- a/libs/st7735/pxt.json +++ b/libs/st7735/pxt.json @@ -2,7 +2,6 @@ "name": "st7735", "files": [ "config_nrf.h", - "config_nrf.cpp", "screen.cpp", "image.cpp", "image.ts", diff --git a/pxtarget.json b/pxtarget.json index 7f8353427b2..cc63400dc3d 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -196,8 +196,8 @@ "buildEngine": "codal", "codalTarget": { "name": "codal-microbit-v2", - "url": "https://github.com/lancaster-university/codal-microbit-v2", - "branch": "v0.2.63", + "url": "https://github.com/tballmsft/codal-microbit-v2", + "branch": "dmesgon", "type": "git" }, "codalBinary": "MICROBIT",