Jump & run: STM32F411 + colour TFT
Platform game in C on a Cortex-M4: arrow keys and space.
- STM32F411
- ST7735 TFT
- CH9350L
Open in the simulatorNo account needed: press Simulate and it runs.

How it works
A side-scrolling platformer in bare-metal C on an STM32F411 (Cortex-M4) with the 1.8" ST7735S colour TFT mounted in landscape. Arrow keys run, space jumps (or click SW1-SW3). Collect coins, jump over gaps, reach the flag. The display scrolls in hardware (SCRLAR/VSCSAD): each frame sends only the new columns and the player over SPI, and the core sleeps between frames. The source is in U1: change it in the code view (level, speed, colours) and run again.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | C1 2C2 2C3 2C5 2C6 2C7 2C8 2DS1 GNDJ1 GNDKB1 GNDR3 2SW1 2SW2 2SW3 2U1 GNDU2 GNDU3 GNDU3 RSTV1 − |
| 3V3 | C2 1C3 1DS1 VCCDS1 LEDU1 VDDU2 OUT |
| V5 | C1 1C6 1C8 1J1 VBUSKB1 VBUSU2 INU2 ENU3 VIN5V1 + |
| VCAP1 | C5 1U1 VCAP1 |
| BOOT0 | R3 1U1 BOOT0 |
| BTN_JUMP | SW3 1U1 PC2 |
| BTN_LEFT | SW1 1U1 PC0 |
| BTN_RIGHT | SW2 1U1 PC1 |
| KBD_3V3 | C7 1U3 VDD33 |
| KBD_RX | U1 PA3U3 TXD |
| MOSI | DS1 SDA/MOSIU1 PA7 |
| SCK | DS1 SCKU1 PA5 |
| TFT_CS | DS1 CSU1 PA4 |
| TFT_DC | DS1 DCU1 PA6 |
| TFT_RES | DS1 RESU1 PA1 |
| UART_TX | U1 PA2U3 RXD |
| USB_DM | J1 D−KB1 D−U3 DM (port 1 D−) |
| USB_DP | J1 D+KB1 D+U3 DP (port 1 D+) |
- R3
- Resistor
- SW1
- Push button
- SW2
- Push button
- SW3
- Push button
Parts
Firmware
sketch.ino · 558 lines · the first 120 here, all of it in the editor
// Jump & run on an STM32F411 with a 1.8" ST7735S TFT (landscape, 160 x 128, RGB565).
//
// Wiring: SPI1 SCK PA5, MOSI PA7 · TFT CS PA4, DC PA6, RES PA1 · buttons to GND with pull-ups:
// left PC0, right PC1, jump PC2 · USB keyboard on a CH9350L host module: its TXD to PA3
// (USART2 RX), 115200 8N1. In the simulator your keyboard is that USB keyboard: arrows or A / D
// run, space, W or arrow up jump.
//
// Speed: the display scrolls in hardware (ST7735S SCRLAR/VSCSAD). Per frame only the columns
// that come into view and the places where a sprite was or is now are sent over SPI, a few
// hundred bytes instead of 40 kB for a full frame. Between frames the core sleeps (WFI).
#include "clab.h"
#define TFT_CS PA(4)
#define TFT_DC PA(6)
#define TFT_RES PA(1)
#define BTN_L PC(0)
#define BTN_R PC(1)
#define BTN_J PC(2)
#define W 144 // playfield width; the 16 px strip on the left is the fixed status bar
#define BAR 16
#define H 128
#define TS 8 // tile size
#define MAP_W 240 // level width in tiles (1920 px)
#define MAP_H 16
#define MAXE 18 // enemies in the level
// ---- SPI1 and the display ---------------------------------------------------------------------
static void spi_init(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
// PA5 SCK, PA7 MOSI: alternate function 5
GPIOA->MODER = (GPIOA->MODER & ~((3u << 10) | (3u << 14))) | (2u << 10) | (2u << 14);
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~((15u << 20) | (15u << 28))) | (5u << 20) | (5u << 28);
GPIOA->OSPEEDR |= (3u << 10) | (3u << 14);
SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI; // mode 0, f_PCLK / 2
SPI1->CR1 |= SPI_CR1_SPE;
}
static inline void spi_byte(uint8_t b) {
while (!(SPI1->SR & SPI_SR_TXE)) {}
*(volatile uint8_t *)&SPI1->DR = b;
}
static inline void spi_done(void) { while (SPI1->SR & SPI_SR_BSY) {} }
static void cmd(uint8_t c) { spi_done(); pin_write(TFT_DC, false); spi_byte(c); spi_done(); pin_write(TFT_DC, true); }
static inline void data(uint8_t d) { spi_byte(d); }
static void cmd16x2(uint8_t c, uint16_t a, uint16_t b) { cmd(c); data(a >> 8); data(a); data(b >> 8); data(b); }
static void tft_init(void) {
pin_output(TFT_CS); pin_output(TFT_DC); pin_output(TFT_RES);
pin_write(TFT_CS, true);
pin_write(TFT_RES, false); delay_ms(1); pin_write(TFT_RES, true); delay_ms(130);
pin_write(TFT_CS, false);
cmd(0x01); delay_ms(130); // SWRESET
cmd(0x11); delay_ms(130); // SLPOUT
cmd(0x3A); data(0x05); // COLMOD 16 bit
cmd(0x36); data(0xA8); // MADCTL MY | MV | BGR: landscape, BGR colour filter panel
// SCRLAR: lines 0..143 scroll (the playfield), lines 144..159 stay: in landscape the status bar
cmd16x2(0x33, 0, W); data(0); data(BAR);
cmd(0x29); // DISPON
}
/** hardware scroll so that world column `cam` is at the left edge */
static void scroll_to(int cam) {
uint16_t ssa = (uint16_t)((W - (cam % W)) % W);
cmd(0x37); data(ssa >> 8); data(ssa);
}
// ---- level -----------------------------------------------------------------------------------
enum { SKY, GROUND, GRASS, BRICK, COIN, FLAG, POLE, QBOX, USED, WATER, BUSH, FLOWER, CASTLE };
static uint8_t map[MAP_W][MAP_H];
static uint8_t gtop[MAP_W]; // ground row of each column (MAP_H over water)
static uint32_t seed;
static int rnd(int n) { seed = seed * 1103515245u + 12345u; return (int)((seed >> 16) % (uint32_t)n); }
/** small hash for decoration (clouds, pebbles, grass) that never changes */
static uint32_t hash(uint32_t x) { x ^= x >> 16; x *= 0x7feb352du; x ^= x >> 15; x *= 0x846ca68bu; return x ^ (x >> 16); }
// enemies: slimes crawl (stomp them), bats flutter (stomp them), hedgehogs have spikes (jump over)
enum { E_SLIME, E_BAT, E_SPIKY };
typedef struct { int x, y, vx, alive, ox, oy, type, bx, by, ph, oph; } Enemy; // x, y in pixels
static Enemy en[MAXE];
static int nen;
static int castle_x, castle_y; // castle at the end (pixels)
static void make_level(void) {
seed = 12345; nen = 0;
int ground = 13;
for (int x = 0; x < MAP_W; x++) {
if (x > 12 && x < MAP_W - 24 && rnd(100) < 6) { // a gap with water at the bottom
int len = 2 + rnd(2);
for (int k = 0; k < len && x < MAP_W; k++, x++) { for (int y = 0; y < MAP_H; y++) map[x][y] = y >= 14 ? WATER : SKY; gtop[x] = MAP_H; }
x--; continue;
}
if (x > 12 && x < MAP_W - 24 && rnd(100) < 10) { ground += rnd(3) - 1; if (ground < 9) ground = 9; if (ground > 13) ground = 13; }
gtop[x] = (uint8_t)ground;
for (int y = 0; y < MAP_H; y++) map[x][y] = y < ground ? SKY : y == ground ? GRASS : GROUND;
if (x > 3 && x < MAP_W - 20 && rnd(100) < 16) map[x][ground - 1] = rnd(3) ? BUSH : FLOWER;
}
// brick platforms 4-5 tiles above the ground in front (one jump), ?-boxes in them, coins on top;
// now and then a second one higher up
for (int x = 18; x < MAP_W - 26; x += 8 + rnd(7)) {
int g = gtop[x] < MAP_H ? gtop[x] : 13, y = g - 4 - rnd(2), len = 3 + rnd(4);
for (int k = 0; k < len; k++) {
map[x + k][y] = rnd(5) == 0 ? QBOX : BRICK;
if (map[x + k][y] == BRICK && rnd(3)) map[x + k][y - 1] = COIN;
}
if (rnd(3) == 0 && y >= 6) {
int x2 = x + len - 1;
for (int k = 0; k < 3; k++) { map[x2 + k][y - 4] = k == 1 ? QBOX : BRICK; map[x2 + k][y - 5] = COIN; }
}
}
// the goal: a pole with a pennant, then the castle
int g = gtop[MAP_W - 14];
for (int y = 3; y < g; y++) map[MAP_W - 14][y] = POLE;
map[MAP_W - 14][3] = FLAG;
castle_x = (MAP_W - 9) * TS; castle_y = (g - 5) * TS;
for (int x = MAP_W - 9; x < MAP_W - 4; x++) for (int y = g - 5; y < g; y++) map[x][y] = CASTLE;
// enemies, away from the start
for (int x = 26; x < MAP_W - 26 && nen < MAXE; x += 10 + rnd(8)) {
int kind = rnd(10);
Enemy *e = &en[nen];
if (kind < 3) { // bat: flies a wave above the groundOpen in the simulator