Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drivers/auxdisplay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/drivers/auxdisplay.h)

zephyr_library()
zephyr_library_sources_ifdef(CONFIG_USERSPACE auxdisplay_handlers.c)
zephyr_library_sources_ifdef(CONFIG_AUXDISPLAY_GPIO_7SEG auxdisplay_gpio_7seg.c)
zephyr_library_sources_ifdef(CONFIG_AUXDISPLAY_HD44780 auxdisplay_hd44780.c)
zephyr_library_sources_ifdef(CONFIG_AUXDISPLAY_ITRON auxdisplay_itron.c)
zephyr_library_sources_ifdef(CONFIG_AUXDISPLAY_JHD1313 auxdisplay_jhd1313.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/auxdisplay/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module = AUXDISPLAY
module-str = auxdisplay
source "subsys/logging/Kconfig.template.log_config"

source "drivers/auxdisplay/Kconfig.gpio"
source "drivers/auxdisplay/Kconfig.hd44780"
source "drivers/auxdisplay/Kconfig.itron"
source "drivers/auxdisplay/Kconfig.jhd1313"
Expand Down
8 changes: 8 additions & 0 deletions drivers/auxdisplay/Kconfig.gpio
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024-2025 Chen Xingyu <[email protected]>
# SPDX-License-Identifier: Apache-2.0

config AUXDISPLAY_GPIO_7SEG
bool "Common GPIO-driven 7-Segment Display"
default y
depends on DT_HAS_GPIO_7_SEGMENT_ENABLED
select GPIO
266 changes: 266 additions & 0 deletions drivers/auxdisplay/auxdisplay_gpio_7seg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
/*
* Copyright (c) 2024-2025 Chen Xingyu <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT gpio_7_segment

#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/auxdisplay.h>
#include <zephyr/drivers/gpio.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(auxdisplay_gpio_7seg, CONFIG_AUXDISPLAY_LOG_LEVEL);

static const uint8_t DIGITS[] = {
[0] = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5),
[1] = BIT(1) | BIT(2),
[2] = BIT(0) | BIT(1) | BIT(3) | BIT(4) | BIT(6),
[3] = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(6),
[4] = BIT(1) | BIT(2) | BIT(5) | BIT(6),
[5] = BIT(0) | BIT(2) | BIT(3) | BIT(5) | BIT(6),
[6] = BIT(0) | BIT(2) | BIT(3) | BIT(4) | BIT(5) | BIT(6),
[7] = BIT(0) | BIT(1) | BIT(2),
[8] = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) | BIT(6),
[9] = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(5) | BIT(6),
};

#define DP (BIT(7))
#define BLANK (0x00)

struct auxdisplay_gpio_7seg_data {
struct k_timer timer;
uint32_t refresh_pos;
int16_t cursor_x;
int16_t cursor_y;
};

struct auxdisplay_gpio_7seg_config {
struct auxdisplay_capabilities capabilities;
const struct gpio_dt_spec *segment_gpios;
const struct gpio_dt_spec *digit_gpios;
uint32_t segment_count;
uint32_t digit_count;
uint32_t refresh_period_ms;
uint8_t *buffer;
};

static int auxdisplay_gpio_7seg_display_on(const struct device *dev)
{
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;
struct auxdisplay_gpio_7seg_data *data = dev->data;

data->refresh_pos = 0;
k_timer_start(&data->timer, K_NO_WAIT, K_MSEC(cfg->refresh_period_ms));

return 0;
}

static int auxdisplay_gpio_7seg_display_off(const struct device *dev)
{
struct auxdisplay_gpio_7seg_data *data = dev->data;

k_timer_stop(&data->timer);

return 0;
}

static int auxdisplay_gpio_7seg_cursor_position_set(const struct device *dev,
enum auxdisplay_position type, int16_t x,
int16_t y)
{
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;
struct auxdisplay_gpio_7seg_data *data = dev->data;

if (type == AUXDISPLAY_POSITION_RELATIVE) {
x += data->cursor_x;
y += data->cursor_y;
} else if (type == AUXDISPLAY_POSITION_RELATIVE_DIRECTION) {
return -EINVAL;
}

if (x < 0 || y < 0) {
return -EINVAL;
} else if (x >= cfg->capabilities.columns || y >= cfg->capabilities.rows) {
return -EINVAL;
}

data->cursor_x = x;
data->cursor_y = y;

return 0;
}

static int auxdisplay_gpio_7seg_cursor_position_get(const struct device *dev, int16_t *x,
int16_t *y)
{
struct auxdisplay_gpio_7seg_data *data = dev->data;

*x = data->cursor_x;
*y = data->cursor_y;

return 0;
}

static int auxdisplay_gpio_7seg_capabilities_get(const struct device *dev,
struct auxdisplay_capabilities *capabilities)
{
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;

memcpy(capabilities, &cfg->capabilities, sizeof(struct auxdisplay_capabilities));

return 0;
}

static int auxdisplay_gpio_7seg_clear(const struct device *dev)
{
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;
struct auxdisplay_gpio_7seg_data *data = dev->data;

memset(cfg->buffer, 0, cfg->digit_count);
data->refresh_pos = 0;
data->cursor_x = 0;
data->cursor_y = 0;

return 0;
}

static int auxdisplay_gpio_7seg_write(const struct device *dev, const uint8_t *ch, uint16_t len)
{
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;
struct auxdisplay_gpio_7seg_data *data = dev->data;
uint32_t i, cursor;

for (i = 0; i < len; i++) {
cursor = data->cursor_y * cfg->capabilities.columns + data->cursor_x;

/*
* Special case where the decimal point should be added to the
* previous digit
*/
if (ch[i] == '.' && cursor > 0) {
cfg->buffer[cursor - 1] |= DP;
continue;
}

if (cursor >= cfg->digit_count) {
break;
}

if (ch[i] >= '0' && ch[i] <= '9') {
cfg->buffer[cursor] = DIGITS[ch[i] - '0'];
} else if (ch[i] == '.') {
/* Leading dot, leave the digit blank */
cfg->buffer[cursor] = DP;
} else {
cfg->buffer[cursor] = BLANK;
}

/* Move the cursor */
if (data->cursor_x < cfg->capabilities.columns - 1) {
data->cursor_x++;
} else if (data->cursor_y < cfg->capabilities.rows - 1) {
data->cursor_x = 0;
data->cursor_y++;
}
}

/* Reset the refresh position */
data->refresh_pos = 0;

return 0;
}

static void auxdisplay_gpio_7seg_timer_expiry_fn(struct k_timer *timer)
{
const struct device *dev = k_timer_user_data_get(timer);
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;
struct auxdisplay_gpio_7seg_data *data = dev->data;

/* Turn off the current digit and move to the next one */
gpio_pin_set_dt(&cfg->digit_gpios[data->refresh_pos], 0);
data->refresh_pos = (data->refresh_pos + 1) % cfg->digit_count;

/* Set the segments for the new digit */
for (uint32_t i = 0; i < cfg->segment_count; i++) {
gpio_pin_set_dt(&cfg->segment_gpios[i], cfg->buffer[data->refresh_pos] & BIT(i));
}

/* Turn on the new digit */
gpio_pin_set_dt(&cfg->digit_gpios[data->refresh_pos], 1);
}

static void auxdisplay_gpio_7seg_timer_stop_fn(struct k_timer *timer)
{
const struct device *dev = k_timer_user_data_get(timer);
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;

/* Turn off all digits */
for (uint32_t i = 0; i < cfg->digit_count; i++) {
gpio_pin_set_dt(&cfg->digit_gpios[i], 0);
}
}

static int auxdisplay_gpio_7seg_init(const struct device *dev)
{
const struct auxdisplay_gpio_7seg_config *cfg = dev->config;
struct auxdisplay_gpio_7seg_data *data = dev->data;

for (uint32_t i = 0; i < cfg->segment_count; i++) {
gpio_pin_configure_dt(&cfg->segment_gpios[i], GPIO_OUTPUT_INACTIVE);
}

for (uint32_t i = 0; i < cfg->digit_count; i++) {
gpio_pin_configure_dt(&cfg->digit_gpios[i], GPIO_OUTPUT_INACTIVE);
}

k_timer_init(&data->timer, auxdisplay_gpio_7seg_timer_expiry_fn,
auxdisplay_gpio_7seg_timer_stop_fn);
k_timer_user_data_set(&data->timer, (void *)dev);

auxdisplay_gpio_7seg_display_on(dev);

return 0;
}

static DEVICE_API(auxdisplay, auxdisplay_gpio_7seg_api) = {
.display_on = auxdisplay_gpio_7seg_display_on,
.display_off = auxdisplay_gpio_7seg_display_off,
.cursor_position_set = auxdisplay_gpio_7seg_cursor_position_set,
.cursor_position_get = auxdisplay_gpio_7seg_cursor_position_get,
.capabilities_get = auxdisplay_gpio_7seg_capabilities_get,
.clear = auxdisplay_gpio_7seg_clear,
.write = auxdisplay_gpio_7seg_write,
};

#define AUXDISPLAY_GPIO_7SEG_INST(n) \
static struct auxdisplay_gpio_7seg_data auxdisplay_gpio_7seg_data_##n; \
\
static const struct gpio_dt_spec auxdisplay_gpio_7seg_segment_gpios_##n[] = { \
DT_INST_FOREACH_PROP_ELEM_SEP(n, segment_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,))}; \
\
static const struct gpio_dt_spec auxdisplay_gpio_7seg_digit_gpios_##n[] = { \
DT_INST_FOREACH_PROP_ELEM_SEP(n, digit_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,))}; \
\
static uint8_t auxdisplay_gpio_7seg_buffer_##n[DT_INST_PROP_LEN(n, digit_gpios)]; \
\
static const struct auxdisplay_gpio_7seg_config auxdisplay_gpio_7seg_config_##n = { \
.capabilities = \
{ \
.columns = DT_INST_PROP(n, columns), \
.rows = DT_INST_PROP(n, rows), \
}, \
.segment_gpios = auxdisplay_gpio_7seg_segment_gpios_##n, \
.segment_count = DT_INST_PROP_LEN(n, segment_gpios), \
.digit_gpios = auxdisplay_gpio_7seg_digit_gpios_##n, \
.digit_count = DT_INST_PROP_LEN(n, digit_gpios), \
.refresh_period_ms = DT_INST_PROP(n, refresh_period_ms), \
.buffer = auxdisplay_gpio_7seg_buffer_##n, \
}; \
\
DEVICE_DT_INST_DEFINE(n, auxdisplay_gpio_7seg_init, NULL, &auxdisplay_gpio_7seg_data_##n, \
&auxdisplay_gpio_7seg_config_##n, POST_KERNEL, \
CONFIG_AUXDISPLAY_INIT_PRIORITY, &auxdisplay_gpio_7seg_api);

DT_INST_FOREACH_STATUS_OKAY(AUXDISPLAY_GPIO_7SEG_INST)
88 changes: 88 additions & 0 deletions dts/bindings/auxdisplay/gpio-7-segment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright (c) 2024-2025 Chen Xingyu <[email protected]>
# SPDX-License-Identifier: Apache-2.0

description: |
Common GPIO-driven 7-Segment Display

A 7-segment display is a form of electronic display device for displaying
decimal numerals that is an alternative to the more complex dot matrix
displays. Seven-segment displays are widely used in digital clocks, electronic
meters, basic calculators, and other electronic devices that display numerical
information.

This driver supports 7-segment displays driven by GPIOs, in a widely used
circuit design either common anode or common cathode.

In common anode design, digit-gpios and segment-gpios are configured as active
high and active low respectively, meaning that the current flows from
digit-gpios to segment-gpios. Vice versa for common cathode.

Example:

#include <zephyr/dt-bindings/gpio/gpio.h>

/ {
auxdisplay_0: digi-display {
compatible = "gpio-7-segment";
columns = <3>;
rows = <1>;
segment-gpios = <&gpio0 0 GPIO_ACTIVE_LOW>, /* A */
<&gpio0 1 GPIO_ACTIVE_LOW>, /* B */
<&gpio0 2 GPIO_ACTIVE_LOW>, /* C */
<&gpio0 3 GPIO_ACTIVE_LOW>, /* D */
<&gpio0 4 GPIO_ACTIVE_LOW>, /* E */
<&gpio0 5 GPIO_ACTIVE_LOW>, /* F */
<&gpio0 6 GPIO_ACTIVE_LOW>, /* G */
<&gpio0 7 GPIO_ACTIVE_LOW>; /* DP */
digit-gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>, /* DIG1 */
<&gpio1 1 GPIO_ACTIVE_HIGH>, /* DIG2 */
<&gpio1 2 GPIO_ACTIVE_HIGH>; /* DIG3 */
refresh-period-ms = <1>;
};
};

compatible: "gpio-7-segment"

include: auxdisplay-device.yaml

properties:
segment-gpios:
type: phandle-array
required: true
description: |
GPIOs used to control the segments.

Segments are usually labeled A to G, and DP for the decimal point. The
GPIOs must be listed in the order A, B, C, D, E, F, G and an optional DP.
The following diagram shows the segment layout:

-- A --
| |
F B
| |
-- G --
| |
E C
| |
-- D -- (DP)

The number of GPIOs must be 7 for a 7-segment display, or 8 for a 7-segment
display with a decimal point.

digit-gpios:
type: phandle-array
required: true
description: |
GPIOs used to control the digits (the common anodes or cathodes).

The number of GPIOs must match the number of digits.

refresh-period-ms:
type: int
required: true
description: |
The refresh period in milliseconds.

This is the time between the display of each digit. The refresh period
must be long enough to allow the segments to be driven and the digit to
be selected.
8 changes: 8 additions & 0 deletions samples/drivers/auxdisplay_digits/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(auxdisplay_digits)

target_sources(app PRIVATE src/main.c)
Loading