BT817 widget tour page 1, slider at 74

STM32 Bridgetek BT817: Display over SPI with Touch Widgets

Disclosure: Bridgetek provided the IDK-BT817-70A kit used in this STM32 Bridgetek BT817 tutorial free of charge with no obligations. Bridgetek did not fund this tutorial nor did they review or approve its content, and every setting, problem and result described here comes from my own bench.

Overview

This STM32 Bridgetek BT817 tutorial connects a NUCLEO-H753ZI to the ME817EV board of the Bridgetek IDK-BT817-70A kit over plain SPI and draws its own touch screens on the 7-inch, 800 x 480 DP-0701-01A panel. There is no EVE Screen Designer in this part: every screen is written in C with Bridgetek’s EVE library, which the project carries as a separate library folder, STM32_Bridgetek_EVE_API.

The firmware does two things. First it runs a short start-up check of its own, with no library involved: it resets the BT817 on its PD# pin, starts its clock with host commands, waits for it to boot and reads back REG_ID, REG_CPURESET and the chip ID, printing each step on the serial terminal. If the wiring is wrong, this is where you find out, with a message that says what failed. Then it hands over to the library and runs a two-page widget tour: a button with a press counter, an OFF/ON toggle with an indicator light, a slider that drives a gauge and a progress bar, a keypad, an analog clock and a dial, and a final pop-up.

Driving the STM32 Bridgetek BT817 pair is different from driving a framebuffer controller. The STM32 never draws pixels. It sends a short list of commands each frame (“a button here”, “a gauge there”) and the BT817 renders the whole screen by itself. The BT817 also keeps no state for its widgets: the STM32 owns every value, and the touch screen only tells it which widget is under the finger. Understanding that split is most of the work, so this tutorial spends time on it.

Start with Bridgetek BT817 EVE Kit Setup with EVE Screen Designer, which sets up the kit, its switches and the touch cable and proves the panel works from a PC. Related tutorials: STM32 RA8875 Library: 7-Inch TFT LCD on NUCLEO-F439ZI drives a 7-inch panel through a framebuffer controller instead, and the NUCLEO-H753ZI pinout page shows where every pin used here is on the board.

What You Will Learn

  • How to wire the ME817EV host connector CN8 to a NUCLEO-H753ZI on SPI1, with PD# and INT#, and set the ME817EV’s switch and jumper for a microcontroller host
  • How the BT817 starts up: the PD# reset, the host commands that start its clock, the boot time, and the three reads that prove it is alive
  • How to add Bridgetek’s EVE library to an STM32CubeIDE project as its own folder, with the compiler settings that select the BT817 and the panel
  • How an EVE screen is built: one co-processor command list per frame, ROM fonts and built-in widgets, and nothing drawn by the STM32 itself
  • How touch works with EVE tags and the touch tracker, why the STM32 owns all widget state, and why a press should count only on the first touch
  • Two traps in CMD_TEXT: %d needs EVE_OPT_FORMAT, and %s must be formatted on the STM32
  • What the start-up looks like on a logic analyzer, and what a missing answer on MISO looks like

Prerequisites

You should be comfortable creating and building an STM32CubeIDE project and have a working knowledge of C and of SPI (clock, MOSI, MISO and a chip select). The Bridgetek BT817 kit should already be assembled and working as described in Bridgetek BT817 EVE Kit Setup with EVE Screen Designer, including the touch cable, because this part does not repeat that. Debug output is redirected to printf over USART1 through an external FTDI USB-to-serial adapter, per this site’s standard debug setup, and viewed in any serial terminal at 19200 baud.

Materials List

  • NUCLEO-H753ZI development board
  • Bridgetek IDK-BT817-70A kit: the ME817EV board, the VA-1001-01A LCD adapter and the DP-0701-01A 7-inch capacitive touch panel, assembled as in Part 1
  • USB-C cable and a 5 V USB supply (or a PC port) for the ME817EV’s CN11 connector
  • FTDI USB-to-serial adapter, for USART1 debug output
  • USB cable for the Nucleo’s ST-LINK connector
  • Wiring for the connections shown in the schematic below, including a short, heavy ground connection between the two boards
  • Optional: a logic analyzer, to look at the start-up on the SPI lines

Board Diagram

STM32H753ZI

The ME817EV Host Connector CN8

In Part 1 the BT817 was driven by the PC through the FT4222H bridge on the ME817EV. For an STM32 host, the same BT817 lines come out on the 2 x 8 header CN8, and switch SW2 decides which of the two hosts is connected. Pin 1 is SCK, top left in the photo, and every pin has its name printed beside it.

This tutorial uses eight of the sixteen pins:

CN8 pinSignalDirectionNUCLEO-H753ZI
1SCKSTM32 to BT817PA5 (SPI1_SCK)
2CS#STM32 to BT817PB6 (GPIO output, label EVE CS#)
3MISOBT817 to STM32PA6 (SPI1_MISO)
4MOSISTM32 to BT817PB5 (SPI1_MOSI)
11, 12GNDGND
13PD#STM32 to BT817PD15 (GPIO output, label EVE PD#)
14INT#BT817 to STM32PF3 (EXTI3, label EVE_INT, not used by this firmware yet)
5 to 10, 15, 16IO2, IO3, GPIO and 5V_HOSTnot connected

Set the ME817EV for a microcontroller host and its own power supply:

  • SW2 all OFF. This disconnects the FT4222H (the PC) from the BT817 host lines and hands them to CN7/CN8.
  • JP1 on 1-2. The ME817EV takes its power from the USB-C connector CN11. Leave CN8 pin 10 (5V_HOST) unconnected: the Nucleo does not power the display.
  • Connect the grounds properly. The two boards have separate supplies, and the backlight draws most of the ME817EV’s current. Besides the two CN8 ground pins, add a short, heavy ground connection between the ME817EV and the Nucleo. On my bench, random SPI errors came down to exactly this.

PD# is the BT817’s power-down input: low holds the chip in reset, high lets it run. The firmware starts it low and pulses it to reset the BT817 at every start. INT# is an open-drain interrupt output from the BT817 (for touch events, for example); it is wired and configured with a pull-up so later parts can use it, but this firmware polls the touch registers instead.

The Bridgetek documents for this part, copied here so the links keep working. The ME817EV datasheet has the CN8 pinout, SW2 and JP1:

The BT817 datasheet has the host commands, the SPI memory read and write format and the start-up sequence used by the start-up check:

The programming guide describes the display list, the co-processor commands and the widgets used by the widget tour:

How the BT817 Draws a Screen

Before the code, the model, because it is not what most people expect from a display controller:

  • A screen is a command list. The STM32 writes co-processor commands into the BT817’s command FIFO over SPI: CMD_DLSTART, a clear, then one command per item (CMD_BUTTON, CMD_GAUGE, CMD_TEXT and so on), then CMD_SWAP. The co-processor turns them into a display list, and the BT817 renders that list on every frame of the panel.
  • The widgets and fonts are inside the BT817. Buttons, toggles, sliders, gauges, dials, clocks, keypads and the ROM fonts 16 to 34 are built in, so only a few bytes per widget cross the SPI bus. Custom fonts and images have to be loaded into the BT817’s RAM or kept in the ME817EV’s flash; this tutorial uses none.
  • The BT817 keeps no widget state. A toggle is only a picture of a toggle in the position the STM32 asked for. Nothing in the BT817 flips it when you touch it, and nothing tells the STM32 “the toggle changed”. The STM32 keeps every value, changes it when touched, and draws the whole screen again with the new value, about 60 times a second here.
  • Touch reports a tag. Before each widget the STM32 writes a tag, a number from 1 to 255 (EVE_TAG()). The BT817 remembers which tag every pixel belongs to and reports the tag under the finger in REG_TOUCH_TAG. For a slider or a dial, the touch tracker (CMD_TRACK) also reports the position along the slider or the angle around the dial, in REG_TRACKER.

So the firmware’s loop is: read the touch registers, update its own values, build and send the next screen.

Project Structure

The project’s own files are one download and the EVE library is another, so the library can be shared with other projects (see Getting the Library below). After you unpack the source zip and the library zip into one project folder the tree looks like this, with the library folder marked:

H753_CPP_SPI_BT817_EVE_02/
├── STM32_Bridgetek_EVE_API/             <- from the STM32_Bridgetek_EVE_API zip
│   ├── include/
│   │   ├── BT81x.h
│   │   ├── BT82x.h
│   │   ├── EVE.h
│   │   ├── EVE_config.h
│   │   ├── FT80x.h
│   │   ├── FT81x.h
│   │   ├── FT8xx.h
│   │   ├── HAL.h
│   │   ├── MCU.h
│   │   └── Platform.h
│   ├── ports/
│   │   └── eve_arch_stm32/
│   │       ├── EVE_MCU_STM32CUBE.c
│   │       ├── EVE_MCU_STM32CUBE_QUADSPI.c
│   │       ├── EVE_MCU_STM32CUBE_SPI.c
│   │       └── README.md
│   ├── source/
│   │   ├── EVE_API.c
│   │   └── EVE_HAL.c
│   ├── LICENSE
│   ├── README.md
│   └── UPSTREAM_README.md
├── Core/
│   ├── Inc/
│   │   ├── main.h
│   │   └── widgetTour.h
│   └── Src/
│       ├── main.c
│       └── widgetTour.c
├── H753_CPP_SPI_BT817_EVE_02.ioc
├── H753_CPP_SPI_BT817_EVE_02.pdf
├── H753_CPP_SPI_BT817_EVE_02.txt
├── LICENSE
├── README.md
├── STM32H753ZITX_FLASH.ld
└── STM32H753ZITX_RAM.ld

Hardware Configuration / Pinouts

Overview

The STM32 Bridgetek BT817 project uses SPI1 for the BT817, two GPIO outputs for its chip select and power-down pins, one EXTI input for its interrupt pin, and USART1 for the terminal.

SignalNUCLEO-H753ZI pinNotes
ME817EV CN8-1 SCKPA5SPI1_SCK, 12 Mbit/s
ME817EV CN8-3 MISOPA6SPI1_MISO
ME817EV CN8-4 MOSIPB5SPI1_MOSI
ME817EV CN8-2 CS#PB6GPIO output, starts high (not selected)
ME817EV CN8-13 PD#PD15GPIO output, starts low (BT817 held in reset)
ME817EV CN8-14 INT#PF3EXTI3, falling edge, pull-up; not used yet
ME817EV CN8-11, CN8-12 GNDGNDPlus a short, heavy ground connection between the boards
FTDI RX / TXPA9 / PA10USART1_TX / USART1_RX, debug output at 19200 baud

The NUCLEO-H753ZI pinout page shows where these pins are on the board.

FTDI Pinouts

FTDI to USB Pinout from right to left

  • Pin 1 – GND
  • Pin 4 – TX
  • Pin 5 – RX
  • USB Mini – Connect to PC via USB cable

Make sure the jumper is set to 5V — the FTDI board is powered from the USB mini cable, and a computer’s USB port supplies 5 V. The FTDI TX goes to the Nucleo’s PA10 (USART1_RX) and the FTDI RX to PA9 (USART1_TX).

FTDI USB-to-serial adapter used for the debug output
FTDI USB-to-serial adapter pin outs, used for the debug output

Schematic

If the schematic is too small to read clearly, open the full-size image in a new tab and use Ctrl-Scroll to zoom in. A vector (SVG) version is also available.

Pinouts & Configurations

The complete CubeMX pin assignment and peripheral configuration is captured in the IOC report below.

Project Setup

Create a new STM32CubeIDE project for the NUCLEO-H753ZI. In the .ioc pinout view:

  1. Enable SPI1 as Full-Duplex Master on PA5 (SCK), PA6 (MISO) and PB5 (MOSI), Hardware NSS Signal disabled, 8-bit data, MSB first, clock polarity low and clock phase on the first edge (SPI mode 0, the CubeMX defaults). Set the prescaler to 16, which gives 12 Mbit/s here.
  2. Set PB6 as GPIO_Output with the user label EVE CS# and output level High, so the BT817 is not selected at start-up.
  3. Set PD15 as GPIO_Output with the user label EVE PD# and output level Low, so the BT817 stays in reset until the firmware starts it.
  4. Set PF3 as GPIO_EXTI3 with the user label EVE_INT, falling edge, pull-up. The firmware does not use it yet.
  5. Enable USART1 in Asynchronous mode on PA9/PA10 at 19200 baud for the printf redirect.
  6. Generate the code.

The labels matter. CubeMX turns EVE CS# and EVE PD# into the macros EVE_CS__Pin, EVE_CS__GPIO_Port, EVE_PD__Pin and EVE_PD__GPIO_Port in main.h, and the library’s STM32 port uses exactly those names to drive the two pins. With any other label the library does not compile.

Getting the Library

The source download contains only this project’s own files. Bridgetek’s EVE library is shared code, so it is its own download, just like its own repository:

  1. Extract the source zip over the project, then extract the library zip into the project root (the folder that contains Core/). You should now have STM32_Bridgetek_EVE_API/ next to Core/, exactly as in the Project Structure tree above. Let CubeIDE overwrite the files it generated.
  2. In CubeIDE, right-click the project and choose Refresh (F5).
  3. Make the library folder a source folder: Project → Properties → C/C++ General → Paths and Symbols → Source Location → Add Folder, then tick STM32_Bridgetek_EVE_API. A source folder shows a small gear on its icon in the Project Explorer.
  4. Open Project → Properties → C/C++ Build → Settings. Under both MCU GCC Compiler → Include paths and MCU G++ Compiler → Include paths, add ${workspace_loc:/${ProjName}/STM32_Bridgetek_EVE_API/include}. Do this for both the Debug and Release configurations.
  5. In the same Settings page, under MCU GCC Compiler → Preprocessor (and MCU G++ Compiler → Preprocessor), add the four defined symbols in the table below, again for Debug and Release.
  6. Build the project. If you see “EVE.h: No such file or directory”, the include path in step 4 is missing or was added to only one compiler or one configuration. If the build ends with “undefined reference to EVE_Init”, the folder in step 3 is not a source folder. An error such as “MODULE_TYPE must be configured” or “EVE_DISP_* parameters must be configured” means a symbol from step 5 is missing or misspelled.
SymbolWhat it selects
FT8XX_TYPE=BT817The EVE chip: the BT817 and its register map
PANEL_TYPE=DP_0701_01AThe panel: 800 x 480 and its timing (pixel clock, porches, sync widths)
PLATFORM_STM32_CUBE=1The STM32Cube port, single-channel SPI (4 would select QUADSPI)
PLATFORM_STM32_CUBE_CHANNEL=1SPI1, so the port uses the hspi1 handle that CubeMX generates

The library is Bridgetek’s portable EVE library, Eve-MCU-Dev, cut down to what an STM32Cube project needs (MIT licence, Bridgetek’s copyright kept). Its README lists the two changes made for this setup. The important one: Bridgetek’s STM32 port changed the SPI prescaler itself during start-up, which overrode the CubeMX setting; this copy always keeps the speed set in CubeMX. The library is kept as its own git repository; in my project it is a git submodule, which keeps its history separate from the project’s.

Code Walkthrough

How the STM32 Bridgetek BT817 Project Fits Together

Three pieces, each in its own place:

  • main.c: the CubeMX set-up, the start-up check (eveChipIdTest()), which talks to the BT817 directly over SPI with no library, and the calls into the widget tour.
  • widgetTour.c / widgetTour.h: the two pages, touch handling and the pop-up, written with the library’s EVE_ functions and macros.
  • STM32_Bridgetek_EVE_API (library): starts the BT817 (EVE_Init()), sets up the panel timing, and turns calls like EVE_CMD_BUTTON() into SPI writes to the command FIFO.

Everything in main.c lives inside the CubeMX USER CODE blocks, so regenerating the project does not remove it. After the peripheral init, main() clears the terminal, runs the start-up check, then starts the widget tour, which never returns:

C
  /* USER CODE BEGIN 2 */

  printf("\x1b[2J\x1b[H");	// Clear the dumb terminal screen

  /* Start-up check: prints REG_ID, REG_CPURESET and the chip ID, stops on failure. */
  if (runChipIdTest)
  {
    eveChipIdTest();
  }

  /* Part 3: our own screen, drawn in code with the EVE library. */
  widgetTourInit();
  widgetTourRun();

runChipIdTest is a static const bool near the top of main.c, true by default. Set it to false to skip the check once the hardware is known to work; the library resets and starts the BT817 again in EVE_Init() either way.

The Start-Up Check: SPI Transactions

Every exchange with the BT817 is one SPI transaction: CS# low, the bytes out and in at the same time, CS# high. eveSpiTransaction() does exactly that, and retries the whole transaction on a transient error (busy, timeout or a transfer-error flag), up to three times. A failure that cannot be fixed by trying again, or three failed attempts, prints the HAL status and the SPI error code and stops in Error_Handler():

C
/**
 * @brief Runs one complete SPI transaction with the BT817 (CS low, transfer, CS high).
 *        Retries the whole transaction on transient errors, up to EVE_SPI_RETRIES,
 *        then reports the reason and calls Error_Handler().
 * @param tx Bytes to send (must hold length bytes).
 * @param rx Buffer for the bytes clocked back (must hold length bytes).
 * @param length Number of bytes to clock.
 * @note Not thread-safe or ISR-safe: blocking HAL_SPI_TransmitReceive on hspi1
 *       and the EVE_CS pin, with no locking. Called from main() before the loop.
 */
static void eveSpiTransaction(const uint8_t *tx, uint8_t *rx, uint16_t length)
{
  HAL_StatusTypeDef status = HAL_ERROR;

  for (uint32_t attempt = 1U; attempt <= EVE_SPI_RETRIES; attempt++)
  {
    HAL_GPIO_WritePin(EVE_CS__GPIO_Port, EVE_CS__Pin, GPIO_PIN_RESET);
    status = HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)tx, rx, length, EVE_SPI_TIMEOUT_MS);
    HAL_GPIO_WritePin(EVE_CS__GPIO_Port, EVE_CS__Pin, GPIO_PIN_SET);

    if (status == HAL_OK)
    {
      return;
    }
    printf("EVE SPI: attempt %lu of %u failed (HAL status %d, SPI error 0x%08lX)\r\n",
           (unsigned long)attempt, EVE_SPI_RETRIES, (int)status,
           (unsigned long)HAL_SPI_GetError(&hspi1));
    if (!eveSpiIsTransient(status))
    {
      break;
    }
  }
  printf("EVE SPI: giving up, the transfer did not complete.\r\n");
  Error_Handler();
}

Reading BT817 memory has a fixed format (datasheet section 4.1.3): three address bytes with the top two bits 00 for a read, one dummy byte, then the data. eveReadMemory() checks its arguments, builds that header and copies the data out of the received bytes:

C
/**
 * @brief Reads bytes from BT817 memory: 3 address bytes (top two bits 00 = read),
 *        one dummy byte, then the data (datasheet 4.1.3).
 * @param address 22-bit BT817 memory address.
 * @param data Destination for the bytes read.
 * @param length Number of bytes to read, 1 to EVE_MAX_READ_BYTES.
 * @note Not thread-safe or ISR-safe (see eveSpiTransaction()).
 */
static void eveReadMemory(uint32_t address, uint8_t *data, uint16_t length)
{
  uint8_t tx[4U + EVE_MAX_READ_BYTES] = { 0 };
  uint8_t rx[4U + EVE_MAX_READ_BYTES] = { 0 };

  if ((data == NULL) || (length == 0U) || (length > EVE_MAX_READ_BYTES))
  {
    printf("eveReadMemory: bad argument (data %p, length %u)\r\n", (void *)data, length);
    Error_Handler();
    return;
  }
  tx[0] = (uint8_t)((address >> 16) & 0x3FU);
  tx[1] = (uint8_t)(address >> 8);
  tx[2] = (uint8_t)address;
  /* tx[3] = dummy byte, then zeros while the data is clocked in */
  eveSpiTransaction(tx, rx, (uint16_t)(4U + length));
  memcpy(data, &rx[4], length);
}

The Start-Up Check: Reset, Clock and Boot

eveChipIdTest() follows the BT817’s start-up sequence from the datasheet:

  • Reset. PD# low for 20 ms, then high (the datasheet asks for at least 5 ms).
  • Clock. Four host commands, each three bytes: CLKEXT (0x44, use the external 12 MHz crystal), CLKSEL (0x61 0x46, a system clock of 6 x 12 = 72 MHz), RST_PULSE (0x68) and ACTIVE (0x00). This is the order Bridgetek’s own EVE_HAL.c uses.
  • Boot. Booting takes up to 300 ms, and until it is done only REG_ID and REG_CPURESET may be read. The check polls REG_ID until it reads 0x7C.
  • Engines. REG_CPURESET has one bit per engine: bit 0 the co-processor, bit 1 the touch engine, bit 2 the audio engine. All three are running when it reads 0. The touch engine waits for the panel’s touch controller, so the check allows up to 3 seconds.
  • Chip ID. The four bytes at 0x0C0000 read 08 17 01 00 on a BT817 (a BT818 reads 08 18 01 00).
C
/**
 * @brief BT817 bring-up test: resets the chip on PD#, starts its clock, waits for
 *        boot, then checks REG_ID (0x7C), REG_CPURESET (0) and the chip ID (08 17 01 00).
 *        Prints PASS, or the reason for the failure and calls Error_Handler().
 * @note Not thread-safe or ISR-safe: blocking SPI and HAL_Delay(); call once from
 *       main() after MX_GPIO_Init() and MX_SPI1_Init(), before the main loop.
 */
static void eveChipIdTest(void)
{
  static const uint8_t expectedChipId[4] = { 0x08U, 0x17U, 0x01U, 0x00U };
  uint8_t value = 0U;
  uint8_t chipId[4] = { 0U };

  printf("BT817 SPI chip-ID test (H753_CPP_SPI_BT817_EVE_02)\r\n");

  /* 1. Hardware reset: PD# low (MX_GPIO_Init() already starts it low), >= 5 ms (4.9.4) */
  HAL_GPIO_WritePin(EVE_PD__GPIO_Port, EVE_PD__Pin, GPIO_PIN_RESET);
  HAL_Delay(20U);
  HAL_GPIO_WritePin(EVE_PD__GPIO_Port, EVE_PD__Pin, GPIO_PIN_SET);
  HAL_Delay(20U);

  /* 2. Clock enable (4.2.3): CLKEXT, CLKSEL (72 MHz), RST_PULSE, then ACTIVE, the order
   *    Bridgetek's EVE_HAL.c uses (CLKEXT itself can trigger a system reset);
   *    wait >= 20 ms (4.9.3.5) */
  eveHostCommand(EVE_CMD_CLKEXT, 0x00U);
  eveHostCommand(EVE_CMD_CLKSEL, EVE_CLKSEL_72MHZ);
  eveHostCommand(EVE_CMD_RST_PULSE, 0x00U);
  eveHostCommand(EVE_CMD_ACTIVE, 0x00U);
  HAL_Delay(20U);

  /* 3. Boot takes up to 300 ms; only REG_ID and REG_CPURESET may be read (4.9.4) */
  if (!eveWaitForRegister(EVE_REG_ID, EVE_REG_ID_VALUE, EVE_BOOT_TIMEOUT_MS, &value))
  {
    printf("FAIL: REG_ID reads 0x%02X, expected 0x%02X within %u ms.\r\n",
           value, EVE_REG_ID_VALUE, EVE_BOOT_TIMEOUT_MS);
    evePrintNoAnswerHelp(value);
    Error_Handler();
    return;
  }
  printf("REG_ID       = 0x%02X  OK\r\n", value);

  /* REG_CPURESET bits: 0 = coprocessor, 1 = touch engine, 2 = audio engine (AN_033) */
  uint32_t engineStart = HAL_GetTick();
  if (!eveWaitForRegister(EVE_REG_CPURESET, 0x00U, EVE_ENGINE_TIMEOUT_MS, &value))
  {
    printf("FAIL: REG_CPURESET reads 0x%02X, expected 0x00 within %u ms"
           " (bit0 coprocessor, bit1 touch, bit2 audio still in reset).\r\n",
           value, EVE_ENGINE_TIMEOUT_MS);
    Error_Handler();
    return;
  }
  printf("REG_CPURESET = 0x%02X  OK (after %lu ms)\r\n", value,
         (unsigned long)(HAL_GetTick() - engineStart));

  /* 4. Chip ID at 0C0000h (5.2) */
  eveReadMemory(EVE_RAM_CHIPID, chipId, (uint16_t)sizeof(chipId));
  printf("Chip ID      = %02X %02X %02X %02X\r\n", chipId[0], chipId[1], chipId[2], chipId[3]);
  if (memcmp(chipId, expectedChipId, sizeof(chipId)) != 0)
  {
    printf("FAIL: expected 08 17 01 00 (BT817). A BT818 reads 08 18 01 00.\r\n");
    Error_Handler();
    return;
  }
  printf("PASS: BT817 found over SPI.\r\n");
}

When REG_ID never reads 0x7C, the check prints the last value read and a short checklist of the switch, jumper and wiring settings. A value of 0x00 or 0xFF means nothing is driving MISO at all, which is a wiring or power problem, not a timing problem. The logic analyzer section at the end shows what this looks like.

Starting the BT817 with the Library

widgetTourInit() hands over to the library. EVE_Init() resets the BT817 again, starts its clocks and programs the panel timing selected by PANEL_TYPE. Then the touch screen is calibrated and the slider and the dial are registered with the touch tracker:

C
/**
 * @brief  Starts the BT817 and prepares the widget tour.
 * @details Initialises the EVE library (BT817 reset, clocks, panel timing),
 *          runs the touch calibration and registers the slider and dial trackers.
 *          Stops in Error_Handler() with a message if the BT817 is not found.
 * @note   Not thread-safe or ISR-safe (uses the SPI bus).
 */
void widgetTourInit(void)
{
  if (EVE_Init() != 0)
  {
    printf("EVE: EVE_Init() failed, BT817 not found or not supported.\r\n");
    Error_Handler();
  }
  calibrateTouch();
  registerTrackers();
  printf("Widget tour running.\r\n");
}

/**
 * @brief  Runs the BT817 touch calibration (tap the three dots).
 * @details The transform is not stored yet, so it runs after every reset.
 * @note   Not thread-safe or ISR-safe (uses the SPI bus).
 */
static void calibrateTouch(void)
{
  EVE_LIB_BeginCoProList();
  EVE_CMD_DLSTART();
  EVE_CLEAR_COLOR_RGB(0, 0, 0);
  EVE_CLEAR(1, 1, 1);
  EVE_COLOR_RGB(255, 255, 255);
  EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, EVE_DISP_HEIGHT / 2, FONT_WIDGET,
               EVE_OPT_CENTER, "Tap the dots to calibrate touch");
  EVE_CMD_CALIBRATE(0);
  EVE_LIB_EndCoProList();
  awaitCoPro("touch calibration");
}

/**
 * @brief  Registers the slider and the dial with the touch tracker (CMD_TRACK).
 * @details While a tracked widget is touched, REG_TRACKER reports its tag in
 *          the low 16 bits and the position (0..65535) in the high 16 bits:
 *          along the slider, or the angle around the dial.
 * @note   Not thread-safe or ISR-safe (uses the SPI bus).
 */
static void registerTrackers(void)
{
  EVE_LIB_BeginCoProList();
  EVE_CMD_TRACK(SLIDER_X, SLIDER_Y, SLIDER_W, SLIDER_H, TAG_SLIDER);
  /* A width and height of 1 makes a rotary track around the dial centre. */
  EVE_CMD_TRACK(DIAL_X, ROUND_Y, 1, 1, TAG_DIAL);
  EVE_LIB_EndCoProList();
  awaitCoPro("tracker set-up");
}

CMD_CALIBRATE shows three dots one after the other and waits for a tap on each; the BT817 works out the touch transform from them and keeps it in its REG_TOUCH_TRANSFORM_A to F registers. The transform is not saved anywhere yet, so the calibration runs after every reset. CMD_TRACK with the slider’s rectangle makes a linear track, and with a width and height of 1 at the dial’s centre a rotary one.

Every command list ends with a wait until the co-processor has worked through it. EVE_LIB_AwaitCoProEmpty() returns non-zero when the co-processor raised an exception, which means the BT817 could not run the list and stopped drawing; the firmware prints which step it was and stops:

C
/**
 * @brief  Waits for the co-processor to finish the current command list.
 * @param  step Short description of what was sent, printed if it fails.
 * @details A co-processor exception means the BT817 could not run the list,
 *          so the program cannot continue: the reason is printed and
 *          Error_Handler() is called (coding rules 2 and 3).
 * @note   Not thread-safe or ISR-safe (uses the SPI bus).
 */
static void awaitCoPro(const char *step)
{
  if (EVE_LIB_AwaitCoProEmpty() != 0)
  {
    printf("EVE: co-processor exception after %s.\r\n", step);
    Error_Handler();
  }
}

Tags and Screen State

These are the tags and the state the widget tour keeps. Each widget gets its own tag; the keypad keys use their own character as the tag, '0' (48) to '9' (57), which CMD_KEYS does by itself. Every value a widget shows lives in a static variable on the STM32:

C

/** @name Touch tags
 *  Values written with EVE_TAG() and read back from REG_TOUCH_TAG / REG_TRACKER.
 *  @{ */
#define TAG_NONE        0U   /**< Nothing touched */
#define TAG_BUTTON      1U   /**< "Press me" button */
#define TAG_TOGGLE      2U   /**< On/off toggle */
#define TAG_SLIDER      3U   /**< Slider (tracked) */
#define TAG_NEXT        4U   /**< "Next >" button (page 1 to page 2) */
#define TAG_BACK        5U   /**< "< Back" button (page 2 to page 1) */
#define TAG_CLEAR       6U   /**< "Clear" button for the typed digits */
#define TAG_DIAL        7U   /**< Dial (rotary tracked) */
#define TAG_DONE        8U   /**< "Done" button (page 2), opens the pop-up */


/** @brief Number of completed "Press me" presses. */
static uint32_t pressCount = 0U;
/** @brief Toggle state; true = ON (LED lit). */
static bool toggleOn = false;
/** @brief Slider value, 0..SLIDER_RANGE; also shown by the gauge and progress bar. */
static uint16_t sliderValue = 42U;
/** @brief Tag touched in the previous frame, used to detect a new press. */
static uint8_t previousTag = TAG_NONE;
/** @brief Page currently shown. */
static WidgetPage currentPage = PAGE_WIDGETS;
/** @brief Digits typed on the keypad (page 2), NUL-terminated. */
static char typedDigits[TYPED_MAX + 1U] = "";
/** @brief Number of digits in typedDigits. */
static uint8_t typedLength = 0U;
/** @brief Dial position, 0..65535 (one full turn). */
static uint16_t dialValue = 0U;
/** @brief true once Done was tapped: the "Completed" pop-up stays until reset. */
static bool popupShown = false;

Reading Touch

readTouch() runs once per frame, before the screen is drawn. It reads REG_TOUCH_TAG and REG_TRACKER and updates the state:

C
/**
 * @brief  Reads the touch registers and updates the screen state.
 * @details A press of a button, the toggle or a key counts once, on the frame
 *          where the finger first touches it (nothing touched the frame before). The slider and the dial follow the
 *          finger while touched. Once Done was tapped, nothing reacts.
 * @note   Not thread-safe or ISR-safe (uses the SPI bus and static state).
 */
static void readTouch(void)
{
  uint8_t tag = EVE_LIB_MemRead8(EVE_REG_TOUCH_TAG);
  uint32_t tracker = EVE_LIB_MemRead32(EVE_REG_TRACKER);
  /* A press counts only when the finger first touches the screen (no touch in
   * the previous frame). Otherwise, when a page changes under a finger that is
   * still down (Next and Done share a position), the button drawn under it
   * would be "pressed" too. */
  bool newPress = (previousTag == TAG_NONE) && (tag != TAG_NONE);

  if (popupShown)
  {
    /* The tour is finished: nothing reacts any more (until reset). */
    previousTag = tag;
    return;
  }

  if (newPress && (tag == TAG_DONE))
  {
    popupShown = true;
  }
  else if (newPress && (tag == TAG_NEXT))
  {
    currentPage = PAGE_MORE;
  }
  else if (newPress && (tag == TAG_BACK))
  {
    currentPage = PAGE_WIDGETS;
  }
  else if (newPress && (tag == TAG_BUTTON))
  {
    pressCount++;
  }
  else if (newPress && (tag == TAG_TOGGLE))
  {
    toggleOn = !toggleOn;
  }
  else if (newPress && (tag == TAG_CLEAR))
  {
    typedLength = 0U;
    typedDigits[0] = '\0';
  }
  else if (newPress && (tag >= (uint8_t)'0') && (tag <= (uint8_t)'9') && (typedLength < TYPED_MAX))
  {
    typedDigits[typedLength] = (char)tag;
    typedLength++;
    typedDigits[typedLength] = '\0';
  }

  if ((tracker & 0xFFFFUL) == TAG_SLIDER)
  {
    sliderValue = (uint16_t)(((tracker >> 16) * SLIDER_RANGE) / TRACKER_FULL);
  }
  else if ((tracker & 0xFFFFUL) == TAG_DIAL)
  {
    dialValue = (uint16_t)(tracker >> 16);
  }
  previousTag = tag;
}

Two details are worth reading slowly:

  • A press counts only on the first touch. newPress is true only on the frame where the finger lands, when the previous frame had no touch. Without this, holding the button would count up 60 times a second. There is a subtler reason too: Next on page 1 and Done on page 2 sit in the same place, so a finger that is still down after the page changes would otherwise press Done straight away.
  • Drag widgets use the tracker, not the tag. While the slider or the dial is touched, REG_TRACKER holds its tag in the low 16 bits and the position (0 to 65535) in the high 16 bits. The slider value is scaled to 0..100; the dial value is kept as it is and turned into degrees when it is drawn.

Because the STM32 owns the state, this is also the place to act on a change, for example to switch a real output when the toggle changes. Compare each value with the value you last acted on, and act once per change.

Drawing a Frame

drawScreen() builds one complete screen as one co-processor command list: start a new display list, clear it to a dark background, draw the page (or the final pop-up), then EVE_DISPLAY() and CMD_SWAP to show it. widgetTourRun() loops forever: read touch, draw, wait 15 ms, which gives about 60 frames a second:

C
/**
 * @brief  Builds and shows one complete frame of the widget tour.
 * @note   Not thread-safe or ISR-safe (uses the SPI bus).
 */
static void drawScreen(void)
{
  EVE_LIB_BeginCoProList();
  EVE_CMD_DLSTART();
  EVE_CLEAR_COLOR_RGB(10, 14, 28);
  EVE_CLEAR(1, 1, 1);

  if (popupShown)
  {
    /* Final screen: only the pop-up on the plain background. */
    drawPopup();
  }
  else if (currentPage == PAGE_WIDGETS)
  {
    drawHeader("BT817 Widget Tour - NUCLEO-H753ZI");
    drawButton();
    drawToggle();
    drawSliderGroup();

    EVE_TAG_MASK(0);
    EVE_COLOR_RGB(160, 170, 190);
    EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, EVE_DISP_HEIGHT - 20, FONT_SMALL, EVE_OPT_CENTER,
                 "Touch the button, the toggle and the slider");
  }
  else
  {
    drawHeader("Widget Tour - page 2");
    drawPageMore();
  }

  EVE_DISPLAY();
  EVE_CMD_SWAP();
  EVE_LIB_EndCoProList();
  awaitCoPro("screen update");
}
C
/**
 * @brief  Runs the widget tour forever: read touch, redraw, short pause.
 * @details Does not return.
 * @note   Not thread-safe or ISR-safe (uses the SPI bus and static state).
 */
void widgetTourRun(void)
{
  for (;;)
  {
    readTouch();
    drawScreen();
    HAL_Delay(FRAME_DELAY_MS);
  }
}

EVE_TAG_MASK(0) and EVE_TAG_MASK(1) appear all through the drawing code. With the mask at 0, what is drawn does not overwrite the tag of the pixels, so labels, the title bar and the gauge do not steal touches from the widgets around them.

Page 1: Button, Toggle, Slider, Gauge and Progress Bar

The button is drawn flat (EVE_OPT_FLAT) while its tag is touched, which is the “pressed” look. The count next to it is a CMD_TEXT with %d, and CMD_TEXT fills in %d only with the EVE_OPT_FORMAT option; without it the text appears exactly as written. The toggle is drawn in the position the STM32 says (0 or 65535), and the round indicator next to it is a point drawn in green or dark grey:

C
/**
 * @brief  Draws the "Press me" button and the press count.
 * @note   Call between EVE_LIB_BeginCoProList() and EVE_LIB_EndCoProList().
 */
static void drawButton(void)
{
  EVE_CMD_FGCOLOR(0x2060C0UL);
  EVE_TAG(TAG_BUTTON);
  EVE_CMD_BUTTON(BUTTON_X, BUTTON_Y, BUTTON_W, BUTTON_H, FONT_WIDGET,
                 (previousTag == TAG_BUTTON) ? EVE_OPT_FLAT : 0U, "Press me");
  EVE_TAG_MASK(0);
  EVE_COLOR_RGB(255, 255, 255);
  /* EVE_OPT_FORMAT makes the BT817 fill in %d (without it the text is shown as is). */
  EVE_CMD_TEXT(COUNT_X, BUTTON_Y + (BUTTON_H / 2), FONT_WIDGET,
               EVE_OPT_CENTERY | EVE_OPT_FORMAT, "Count: %d", (int)pressCount);
  EVE_TAG_MASK(1);
}

/**
 * @brief  Draws the toggle and the LED it controls.
 * @note   Call between EVE_LIB_BeginCoProList() and EVE_LIB_EndCoProList().
 */
static void drawToggle(void)
{
  EVE_CMD_FGCOLOR(0xC0C0C0UL);
  EVE_CMD_BGCOLOR(0x404040UL);
  EVE_TAG(TAG_TOGGLE);
  EVE_CMD_TOGGLE(TOGGLE_X, TOGGLE_Y, TOGGLE_W, FONT_WIDGET, 0U,
                 toggleOn ? 65535U : 0U, "OFF\xffON");

  EVE_TAG_MASK(0);
  if (toggleOn)
  {
    EVE_COLOR_RGB(40, 230, 60);
  }
  else
  {
    EVE_COLOR_RGB(50, 60, 50);
  }
  EVE_POINT_SIZE(LED_RADIUS * 16);
  EVE_BEGIN(EVE_BEGIN_POINTS);
  EVE_VERTEX2F(LED_X * 16, LED_Y * 16);
  EVE_END();
  EVE_TAG_MASK(1);
}

The slider, the gauge, the number and the progress bar all show the same sliderValue. That is the whole trick of “linked” widgets in EVE: there is nothing to link, they are simply drawn from the same variable:

C
/**
 * @brief  Draws the gauge, the slider with its value and the progress bar.
 * @note   Call between EVE_LIB_BeginCoProList() and EVE_LIB_EndCoProList().
 */
static void drawSliderGroup(void)
{
  EVE_TAG_MASK(0);
  EVE_COLOR_RGB(255, 255, 255);
  EVE_CMD_BGCOLOR(0x202840UL);
  EVE_CMD_GAUGE(GAUGE_X, GAUGE_Y, GAUGE_RADIUS, 0U, 10U, 5U, sliderValue, SLIDER_RANGE);

  EVE_CMD_TEXT(SLIDER_X, SLIDER_Y - 36, FONT_SMALL, 0U, "Slider");
  EVE_CMD_NUMBER(VALUE_X, SLIDER_Y + (SLIDER_H / 2), FONT_WIDGET, EVE_OPT_CENTERY, sliderValue);
  EVE_CMD_TEXT(SLIDER_X, PROGRESS_Y - 36, FONT_SMALL, 0U, "Progress");
  EVE_CMD_BGCOLOR(0x404040UL);
  EVE_CMD_PROGRESS(SLIDER_X, PROGRESS_Y, SLIDER_W, SLIDER_H, 0U, sliderValue, SLIDER_RANGE);

  EVE_TAG_MASK(1);
  EVE_TAG(TAG_SLIDER);
  EVE_CMD_FGCOLOR(0x2060C0UL);
  EVE_CMD_SLIDER(SLIDER_X, SLIDER_Y, SLIDER_W, SLIDER_H, 0U, sliderValue, SLIDER_RANGE);
}

Page 2: Keypad, Clock and Dial

CMD_KEYS draws a row of keys from a string and gives each key its own character as its tag; adding the pressed character to the options draws that key pressed. The analog clock shows the time since reset from HAL_GetTick(), and the dial shows its angle in degrees:

C
/**
 * @brief  Draws page 2: typed digits with a Clear button, the keypad, an
 *         analog clock (time since reset) and a dial with its angle.
 * @note   Call between EVE_LIB_BeginCoProList() and EVE_LIB_EndCoProList().
 */
static void drawPageMore(void)
{
  uint32_t seconds = HAL_GetTick() / 1000UL;
  char typedLine[sizeof("Typed: ") + TYPED_MAX];
  uint8_t pressedKey = ((previousTag >= (uint8_t)'0') && (previousTag <= (uint8_t)'9')) ? previousTag : 0U;

  EVE_TAG_MASK(0);
  EVE_COLOR_RGB(255, 255, 255);
  /* Build the line on the STM32: with EVE_OPT_FORMAT, %s would send the STM32
   * address of the string, which the BT817 reads as its own memory address
   * (co-processor exception). %d is fine because the number itself is sent. */
  (void)snprintf(typedLine, sizeof(typedLine), "Typed: %s", typedDigits);
  EVE_CMD_TEXT(KEYS_X, TYPED_Y, FONT_TITLE, EVE_OPT_CENTERY, typedLine);
  EVE_TAG_MASK(1);

  EVE_CMD_FGCOLOR(0x2060C0UL);
  EVE_TAG(TAG_CLEAR);
  EVE_CMD_BUTTON(CLEAR_X, TYPED_Y - (NAV_H / 2), CLEAR_W, NAV_H, FONT_WIDGET,
                 (previousTag == TAG_CLEAR) ? EVE_OPT_FLAT : 0U, "Clear");

  /* Each key's tag is its own character; adding the pressed character to the
   * options draws that key pressed. */
  EVE_CMD_KEYS(KEYS_X, KEYS_Y, KEYS_W, KEYS_H, FONT_WIDGET, pressedKey, KEYPAD_KEYS);

  EVE_TAG_MASK(0);
  EVE_CMD_BGCOLOR(0x202840UL);
  EVE_CMD_CLOCK(CLOCK_X, ROUND_Y, ROUND_RADIUS, 0U,
                (uint16_t)((seconds / 3600UL) % 12UL), (uint16_t)((seconds / 60UL) % 60UL),
                (uint16_t)(seconds % 60UL), 0U);
  EVE_COLOR_RGB(255, 255, 255);
  EVE_CMD_TEXT(DIAL_TEXT_X, ROUND_Y, FONT_WIDGET, EVE_OPT_CENTERY | EVE_OPT_FORMAT,
               "Dial: %d deg", (int)(((uint32_t)dialValue * 360UL) / TRACKER_FULL));
  EVE_TAG_MASK(1);

  EVE_TAG(TAG_DIAL);
  EVE_CMD_FGCOLOR(0x2060C0UL);
  EVE_CMD_DIAL(DIAL_X, ROUND_Y, ROUND_RADIUS, 0U, dialValue);
}

Note how the typed digits are shown. %s with EVE_OPT_FORMAT does not work for a string in the STM32’s memory: the BT817 expects the argument to be an address in its own memory, reads nonsense from there and raises a co-processor exception. %d works because the number itself is sent. So the line is formatted on the STM32 with snprintf() and sent as plain text.

Things to Know

  • Calibrate carefully. The three calibration dots set the touch transform for the whole screen. Tap the centre of each dot with a fingertip and lift cleanly. A sloppy tap skews the transform; the error is small in the middle of the screen and largest in the corners, so the big widgets still work while the small Next button in the top-right corner misses. If that happens, press reset and calibrate again.
  • SPI speed. SPI1 runs at 12 Mbit/s (prescaler 16), which was reliable on my wiring, while 24 Mbit/s was not. Bridgetek’s programming guide (BRT_AN_033) asks for an SPI clock below 11 MHz until the BT817’s clock is running; if your start-up is unreliable, try prescaler 32 (6 Mbit/s).
  • Ground. The ME817EV and the Nucleo have separate supplies. A weak ground between them shows up as random SPI errors and false co-processor exceptions, not as a clean failure.
  • CMD_SPINNER is not used. It animates by itself until CMD_STOP, which does not fit a screen that is rebuilt every frame.
  • Not thread-safe. The start-up check and the widget tour make blocking SPI calls on hspi1 and keep their state in static variables, with no lock. Call them from one context only, never from an interrupt handler.

Running It: STM32 Bridgetek BT817 on the Board

Set the ME817EV as described above (SW2 all OFF, JP1 on 1-2), power it from CN11, build and flash the project and open a serial terminal at 19200 baud. The start-up check prints each step and hands over to the widget tour:

C
BT817 SPI chip-ID test (H753_CPP_SPI_BT817_EVE_02)
REG_ID       = 0x7C  OK
REG_CPURESET = 0x00  OK (after 0 ms)
Chip ID      = 08 17 01 00
PASS: BT817 found over SPI.
Widget tour running.

The panel then shows the calibration screen. Tap each dot as it appears; the dots come up one after the other in different places on the screen.

Page 1 comes up with the slider at its start value of 42. Tap Press me to count, tap the toggle to switch the indicator, and drag the slider: the gauge, the number and the progress bar follow it.

Next > opens page 2. Type digits on the keypad (Clear empties the line), and turn the dial with a finger. The clock shows the time since the last reset.

Done ends the tour with a pop-up. Nothing reacts after that; press the Nucleo’s reset button to start over (with a new calibration).

Logic Analyzer Captures

These captures show the start-up check on the SPI lines at 12 Mbit/s. The channels are MOSI, MISO, Enable (CS#), PD#, INT# and Clock (SCK). They were taken with an earlier build of the same start-up check that did not send CLKSEL yet; the current code sends 0x61 0x46 0x00 between CLKEXT and RST_PULSE, so you will see one more three-byte transaction there. Everything else is identical.

The overview: PD# low for 20 ms, then high, then the host commands and the first reads.

The host commands, three bytes each with CS# low around each one. While the STM32 sends the command, a live BT817 answers 0x00 0x4A 0x43 on MISO: a good sign that it is powered and wired, before a single register has been read.

While the BT817 boots, REG_ID (address 0x302000, sent as 0x30 0x20 0x00, then the dummy byte) reads 0x00, and the check keeps polling:

Once it has booted, the same read returns 0x7C:

Next, REG_CPURESET (address 0x302020). The first reads come back non-zero while engines are still held in reset:

Until all three engines run and it reads 0x00:

Finally the chip ID, four bytes from 0x0C0000:

And this is what a failure looks like. Here the BT817 is not answering because of a wiring fault. The STM32 still sends the REG_ID read, but nothing answers: MISO stays low apart from a short spike (crosstalk from the neighbouring line), the data byte reads 0x00, and after 300 ms the check prints FAIL: REG_ID reads 0x00, expected 0x7C and stops. A MISO line that never shows the 0x4A 0x43 answer during the address bytes points at the wiring or the ME817EV’s power and switches, not at the firmware.

Project Downloads

The complete project source code used in this tutorial is available for download. This includes all necessary files to build and run the project, along with supporting documentation.

  • Full STM32CubeIDE Project (Source + Configuration)
  • Doxygen Documentation (docs/html/index.html)
  • Supporting Files: the STM32_Bridgetek_EVE_API library, as its own zip (see Getting the Library above)

Documentation

This project includes full Doxygen-generated documentation for all custom source files.

The documentation covers the project’s own files (main.c, widgetTour.c and widgetTour.h); Bridgetek’s library is third-party code and is not part of it. It is included in the Doxygen documentation download above and can be viewed locally by opening:

H753_CPP_SPI_BT817_EVE_02/docs/html/index.html

in a web browser.

The documentation provides detailed descriptions of functions, data structures, and module interactions to assist with understanding and extending the project.

What’s Next

The next part goes one level deeper: instead of ready-made widgets, it draws its own graphics with the BT817’s display-list primitives (points, lines, rectangles, colour and transparency, clipping) and shows when to use a widget and when to draw your own. A later part moves the same board to QSPI.

If you have questions or run into trouble getting the boards programmed and talking to each other, post in the Tutorial Support forum and I will work through it with you. If project source is not linked in the tutorial, it may be available on request — use the email contact option in the site footer.

Tutorials and code are provided as-is, without warranty. See the Disclaimer.

MicroControllersTech Tutorial Feedback Anonymous User

Did you find this tutorial helpful?*

Did you find this tutorial helpful?*

Why do you think this tutorial is not helpful?*

Why do you think this tutorial is not helpful?*

Please give us a short explanation

Please give us a short explanation

How easy was this tutorial to follow?*

How easy was this tutorial to follow?*

What other tutorial/topic would you like to see next?

What other tutorial/topic would you like to see next?

We noticed that you are not an authenticated user, would you like to register?*

We noticed that you are not an authenticated user, would you like to register?*

Source Post

Source Post