UKW-Radio: STM32F411 + RDA5807M + TFT
STM32 + RDA5807M: Sendersuche, RDS, Ton.
- STM32F411
- ST7735 TFT
- RDA5807M

So funktioniert es
Kleiner UKW-Empfänger: STM32F411 (Cortex-M4, Zephyr-Firmware, Quelle firmware/fmradio) steuert per I²C das Tuner-Modul RDA5807M und zeigt auf dem ST7735-TFT Frequenz, RDS-Sendername, Programmart, Radiotext, Empfangsstärke (dBµV + Balken), Stereo und Lautstärke. Taster SW1 = Sendersuchlauf ab, SW2 = auf, SW3 = leiser, SW4 = lauter (gedrückt halten wiederholt). Fünf Sender zwischen 88,4 und 104,6 MHz mit eigenem Programm: Klassik, Jazz, Pop, Nachrichten, Rock. ▶ Ton spielt den Kopfhörer J1. Antenne ANT1 löschen = 25 dB weniger Empfang, schwache Sender rauschen. Senderliste im Parameter von U2 änderbar.
Verdrahtung
Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.
| Netz | Pins |
|---|---|
| GND | ANT1 EarthBT1 −C1 2C2 2C3 2C4 2C5 2DS1 GNDJ1 ⏚R3 2SW1 2SW2 2SW3 2SW4 2U1 GNDU2 GNDU4 GND |
| 3V3 | C2 1C3 1C4 1DS1 VCCDS1 LEDR1 1R2 1U1 VDDU2 VCCU4 OUT |
| VBAT | BT1 +C1 1U4 INU4 EN |
| VCAP1 | C5 1U1 VCAP1 |
| ANT | ANT1 ANTU2 ANT |
| AUD_L | J1 LU2 LOUT |
| AUD_R | J1 RU2 ROUT |
| BOOT0 | R3 1U1 BOOT0 |
| BTN_SEEK_DN | SW1 1U1 PC0 |
| BTN_SEEK_UP | SW2 1U1 PC1 |
| BTN_VOL_DN | SW3 1U1 PC2 |
| BTN_VOL_UP | SW4 1U1 PC3 |
| MOSI | DS1 SDA/MOSIU1 PA7 |
| SCK | DS1 SCKU1 PA5 |
| SCL | R2 2U1 PB8U2 SCL |
| SDA | R1 2U1 PB9U2 SDA |
| TFT_CS | DS1 CSU1 PB6 |
| TFT_DC | DS1 DCU1 PA8 |
| TFT_RES | DS1 RESU1 PA9 |
- ANT1
- Drahtantenne
- SW1
- Taster
- SW2
- Taster
- SW3
- Taster
- SW4
- Taster
Bauteile
Firmware
src/main.c · 426 Zeilen · die ersten 120 hier, alles im Editor
/*
* FM radio - Zephyr firmware for an STM32F411 + RDA5807M FM tuner + ST7735S TFT (Circuit Lab).
* Board target nucleo_f411re (96 MHz Cortex-M4F).
*
* Hardware:
* ST7735S 128x160 TFT on SPI1: SCK PA5, MOSI PA7, CS PB6, D/C PA8, RES PA9
* RDA5807M module (RRD-102) on I2C1 400 kHz: SCL PB8, SDA PB9 (4.7 k pull-ups)
* Buttons to GND (internal pull-ups): PC0 seek down, PC1 seek up, PC2 volume -, PC3 volume +
* Console: USART2 TX PA2
*
* The RDA5807M is driven through its sequential I2C interface (address 0x10): writes
* start at register 02h, reads at 0Ah (status, RSSI, RDS blocks A..D).
* Display: frequency, RDS station name (group 0A), programme type, radiotext (group 2A),
* signal strength (RSSI dBuV + bars), stereo indicator and volume.
*/
#include <zephyr/kernel.h>
#include <zephyr/version.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/sys/printk.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
extern const uint8_t font5x7[95][5];
#define PIN_DC 8 /* PA8 */
#define PIN_RES 9 /* PA9 */
static const struct device *const gpioa = DEVICE_DT_GET(DT_NODELABEL(gpioa));
static const struct device *const gpioc = DEVICE_DT_GET(DT_NODELABEL(gpioc));
static const struct device *const spi = DEVICE_DT_GET(DT_NODELABEL(spi1));
static const struct device *const i2c = DEVICE_DT_GET(DT_NODELABEL(i2c1));
static const struct spi_config cfg = {
.frequency = 6000000,
.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8),
.cs = { .gpio = { .port = DEVICE_DT_GET(DT_NODELABEL(gpiob)), .pin = 6, .dt_flags = GPIO_ACTIVE_LOW }, .delay = 0, .cs_is_gpio = true },
};
/* ------------------------------------------------------------------ display */
#define W 128
#define H 160
#define RGB(r, g, b) ((((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | ((b) >> 3))
#define C_BLACK 0x0000
#define C_WHITE 0xFFFF
#define C_GREY RGB(140, 140, 140)
#define C_DKGREY RGB(50, 50, 50)
#define C_BAR RGB(20, 60, 160)
#define C_BG RGB(6, 10, 24)
#define C_YELLOW RGB(255, 210, 0)
#define C_GREEN RGB(60, 230, 90)
#define C_CYAN RGB(90, 220, 255)
#define C_RED RGB(255, 70, 70)
#define C_ORANGE RGB(255, 150, 40)
static void spi_out(const uint8_t *b, size_t n)
{
const struct spi_buf sb = {.buf = (void *)b, .len = n};
const struct spi_buf_set ss = {&sb, 1};
spi_write(spi, &cfg, &ss);
}
static void cmd(uint8_t c, const uint8_t *p, size_t n)
{
gpio_pin_set_raw(gpioa, PIN_DC, 0);
spi_out(&c, 1);
if (n) { gpio_pin_set_raw(gpioa, PIN_DC, 1); spi_out(p, n); }
}
static void window(int x, int y, int w, int h)
{
uint8_t ca[4] = {0, x, 0, x + w - 1}, ra[4] = {0, y, 0, y + h - 1};
cmd(0x2A, ca, 4);
cmd(0x2B, ra, 4);
cmd(0x2C, NULL, 0);
gpio_pin_set_raw(gpioa, PIN_DC, 1);
}
static uint8_t px[W * 24 * 2];
static void fill(int x, int y, int w, int h, uint16_t c)
{
window(x, y, w, h);
int rows = sizeof(px) / 2 / w;
for (int i = 0; i < w * rows; i++) { px[2 * i] = c >> 8; px[2 * i + 1] = c & 0xFF; }
while (h > 0) { int n = MIN(h, rows); spi_out(px, n * w * 2); h -= n; }
}
/* text box: w x h pixels at (x, y), font scaled by s, aligned 0 left / 1 centre / 2 right */
static void text(int x, int y, int w, int h, int s, uint16_t fg, uint16_t bg, int align, const char *str)
{
int len = strlen(str), tw = len * 6 * s - s;
int ox = align == 1 ? (w - tw) / 2 : align == 2 ? w - tw : 1;
int oy = (h - 7 * s) / 2;
for (int yy = 0; yy < h; yy++) {
for (int xx = 0; xx < w; xx++) {
bool on = false;
int gx = xx - ox, gy = yy - oy;
if (gx >= 0 && gy >= 0 && gy < 7 * s) {
int cx = gx / (6 * s), col = (gx % (6 * s)) / s;
if (cx < len && col < 5) {
char ch = str[cx];
if (ch >= 32 && ch < 127) on = (font5x7[ch - 32][col] >> (gy / s)) & 1;
}
}
uint16_t c = on ? fg : bg;
size_t o = (yy * w + xx) * 2;
px[o] = c >> 8; px[o + 1] = c & 0xFF;
}
}
window(x, y, w, h);
spi_out(px, w * h * 2);
}
/* horizontal bar graph: n of max segments lit */
static void bar(int x, int y, int w, int h, int n, int max, uint16_t on, uint16_t off)
{
int seg = w / max;
for (int yy = 0; yy < h; yy++)
for (int xx = 0; xx < w; xx++) {
int i = xx / seg;Im Simulator öffnen