This commit is contained in:
2025-06-01 17:10:23 +02:00
commit 98ac20f729
14 changed files with 318 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_sources(app PRIVATE gpio_visualizer.cpp)

View File

@@ -0,0 +1,89 @@
#include "gpio_visualizer.hpp"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/sys/printk.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/drivers/display.h>
#define GPIO_VISUALIZER_STACK_SIZE 64 * 1024 * 1024 // 64 MB
#define GPIO_VISUALIZER_PRIORITY 10
#define NR_OF_GPIO_PORTS 9
#define NR_OF_GPIO_PINS_PER_PORT 16
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 240
#define DISPLAY_COLOR_DEPTH 4
#define GPIO_PIXEL_SIZE 20
K_THREAD_STACK_DEFINE(gpio_visualizer__thread_stack, GPIO_VISUALIZER_STACK_SIZE);
struct k_thread gpio_visualizer_thread_data;
const struct device *gpioa = device_get_binding("GPIOA");
const struct device *gpiob = device_get_binding("GPIOB");
const struct device *gpioc = device_get_binding("GPIOC");
const struct device *gpiod = device_get_binding("GPIOD");
const struct device *gpioe = device_get_binding("GPIOE");
const struct device *gpiof = device_get_binding("GPIOF");
const struct device *gpiog = device_get_binding("GPIOG");
const struct device *gpioh = device_get_binding("GPIOH");
const struct device *gpioi = device_get_binding("GPIOI");
void draw_led(uint8_t port, uint8_t pin, uint8_t state, uint8_t *framebuf) {
uint32_t x_base = GPIO_PIXEL_SIZE * pin;
uint32_t y_base = GPIO_PIXEL_SIZE * port;
for (uint32_t i = 0; i < GPIO_PIXEL_SIZE * GPIO_PIXEL_SIZE; i++) {
uint32_t x = x_base + (i % GPIO_PIXEL_SIZE);
uint32_t y = y_base + (i / GPIO_PIXEL_SIZE);
framebuf[x * DISPLAY_COLOR_DEPTH + 2 + DISPLAY_WIDTH * DISPLAY_COLOR_DEPTH * y] = state * 0xFF;
}
}
void gpio_visualizer_thread(void *ARG1, void *ARG2, void *ARG3) {
const struct device* gpio_ports[NR_OF_GPIO_PORTS] = {gpioa, gpiob, gpioc, gpiod, gpioe, gpiof, gpiog, gpioh, gpioi};
const struct device *gpio_display = device_get_binding("GPIO DISPLAY");
struct display_buffer_descriptor desc;
uint8_t buf[DISPLAY_WIDTH * DISPLAY_HEIGHT * DISPLAY_COLOR_DEPTH] = {0};
desc.buf_size = sizeof(buf);
desc.width = DISPLAY_WIDTH;
desc.height = DISPLAY_HEIGHT;
desc.pitch = DISPLAY_WIDTH;
for (uint32_t i = 0; i < desc.buf_size; i++) {
if (i % DISPLAY_COLOR_DEPTH == DISPLAY_COLOR_DEPTH - 1) {
buf[i] = 0xFF;
}
}
display_set_contrast(gpio_display, 100);
display_blanking_off(gpio_display);
int running = 1;
while (running) {
for (uint8_t port_idx = 0; port_idx < NR_OF_GPIO_PORTS; port_idx++) {
for (uint8_t pin_idx = 0; pin_idx < NR_OF_GPIO_PINS_PER_PORT; pin_idx++) {
uint8_t state = gpio_emul_output_get(gpio_ports[port_idx], pin_idx);
draw_led(port_idx, pin_idx, state, buf);
}
}
display_write(gpio_display, 0, 0, &desc, buf);
k_sleep(K_MSEC(16));
}
}
void spawn_gpio_visualizer_thread(void) {
k_thread_create(&gpio_visualizer_thread_data, gpio_visualizer__thread_stack,
GPIO_VISUALIZER_STACK_SIZE,
gpio_visualizer_thread,
NULL, NULL, NULL,
GPIO_VISUALIZER_PRIORITY, 0, K_NO_WAIT);
}

View File

@@ -0,0 +1 @@
void spawn_gpio_visualizer_thread(void);

25
src/main.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/sys/printk.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <rcl/rcl.h>
#if defined(CONFIG_BOARD_NATIVE_SIM)
#include "gpio_visualizer.hpp"
#endif
int main(void)
{
#if defined(CONFIG_BOARD_NATIVE_SIM)
spawn_gpio_visualizer_thread();
#endif
while (1) {
printk("Hello World! %s\n", CONFIG_BOARD_TARGET);
k_sleep(K_SECONDS(2));
}
return 0;
}