zephyr stm32f4驱动 oled VGM128064E0W01 使用spi驱动
1.工程目录结构
2.设备树app.overlay
/ {
chosen {
zephyr,console = &usart2;
zephyr,shell-uart = &usart2;
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,ccm = &ccm0;
zephyr,display = &vgm128064;
};
leds {
compatible = "gpio-leds";
led: led {
gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>;
label = "User LED";
};
};
aliases {
oled = &vgm128064;
};
};
&usart2 {
status = "okay";
pinctrl-names = "default";
current-speed = <2000000>;
};
&spi2 {
status = "okay";
/* 添加必需的pinctrl-0属性 */
pinctrl-0 = <&spi2_sck_pb13 &spi2_mosi_pb15 &oled_reset_pb11 &oled_dc_pb10 &oled_cs_pb12>;
pinctrl-names = "default";
cs-gpios = <&gpiob 12 GPIO_ACTIVE_LOW>;
vgm128064: vgm128064@0{
compatible = "sitronix,st7567";
reg = <0x2>;
spi-max-frequency = <10000000>;
spi-cpha;
spi-cpol;
spi-hold-cs;
width = <128>;
height = <64>;
column-offset = <0>;
line-offset = <0>;
regulation-ratio = <7>;
reset-gpios = <&gpiob 11 GPIO_ACTIVE_LOW>;
data-cmd-gpios=<&gpiob 10 GPIO_ACTIVE_HIGH>;
status = "okay"; /* 必须设置为okay才能启用节点 */
};
};
/* 完整的引脚控制配置 */
&pinctrl {
/* SPI2 SCK (PB13) */
spi2_sck_pb13: spi2_sck_pb13 {
pinmux = <STM32_PINMUX('B', 13, AF5)>; // AF5 = SPI2_SCK
slew-rate = "very-high-speed";
};
/* SPI2 MOSI (PB15) */
spi2_mosi_pb15: spi2_mosi_pb15 {
pinmux = <STM32_PINMUX('B', 15, AF5)>; // AF5 = SPI2_MOSI
slew-rate = "very-high-speed";
};
/* OLED控制引脚 */
oled_reset_pb11: oled_reset_pb11 {
pinmux = <STM32_PINMUX('B', 11, GPIO)>;
output-high;
};
oled_dc_pb10: oled_dc_pb10 {
pinmux = <STM32_PINMUX('B', 10, GPIO)>;
output-high;
};
oled_cs_pb12: oled_cs_pb12 {
pinmux = <STM32_PINMUX('B', 12, AF5)>;
slew-rate = "very-high-speed";
};
};
/* 禁用可能冲突的TIMER2 */
&timers2 {
status = "disabled";
};
3.工程配置prj.conf
# 调试
CONFIG_LOG=y
# SPI配置
CONFIG_SPI=y
CONFIG_SPI_STM32=y
CONFIG_SPI_STM32_DMA=y
CONFIG_DISPLAY_LOG_LEVEL_DBG=y
CONFIG_SPI_LOG_LEVEL_DBG=y
CONFIG_LOG_BUFFER_SIZE=8192
CONFIG_GPIO=y
# 显示驱动
CONFIG_DISPLAY=y
CONFIG_ST7567=y
CONFIG_DISPLAY=y
CONFIG_CHARACTER_FRAMEBUFFER=y
CONFIG_CHARACTER_FRAMEBUFFER_USE_DEFAULT_FONTS=y
# 显示配置
# CONFIG_DISPLAY=y
# CONFIG_DISPLAY_SSD1306=y
# CONFIG_DISPLAY_SSD1306_SPI=y
# 字体和缓冲区配置
CONFIG_HEAP_MEM_POOL_SIZE=2048
CONFIG_MAIN_STACK_SIZE=2048
3.驱动文件oled_font.c
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/display.h>
#include <zephyr/display/cfb.h> // 包含CFB库头文件
#include <zephyr/kernel.h>
// 获取 SPI 屏幕设备
static const struct device *get_display_device(void)
{
// 设备标签需与设备树中 匹配
const struct device *dev = DEVICE_DT_GET(DT_ALIAS(oled));//DEVICE_DT_GET_ANY(sitronix_ssd7567);//
if (!device_is_ready(dev)) {
printf("SPI 屏幕设备未就绪!\n");
return NULL;
}
return dev;
}
void test_display(void) {
const struct device *display_dev = get_display_device();
// 检查显示设备是否就绪
if (!device_is_ready(display_dev)) {
return;
}
// 初始化CFB库
if (cfb_framebuffer_init(display_dev) != 0) {
return; // 初始化失败
}
// 设置字体(0为默认字体索引)
cfb_framebuffer_set_font(display_dev, 0);
// 清屏
cfb_framebuffer_clear(display_dev, true);
// 在坐标(10, 10)处显示字符串
cfb_print(display_dev, "hello, world!", 10, 10);
cfb_framebuffer_finalize(display_dev);
k_msleep(2000);
cfb_framebuffer_invert(display_dev);
// 刷新显示(将数据写入硬件)
cfb_framebuffer_finalize(display_dev);
}
4.驱动.h文件
#ifndef OLED_HHH__
#define OLED_HHH__
#include <stdint.h>
#include <stdbool.h>
void test_display(void);
#endif // OLED_FONT_H
5.main.c
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include "oled_font.h"
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
/*
* A build error on this line means your board is unsupported.
* See the sample documentation for information on how to fix this.
*/
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
int ret;
bool led_state = true;
if (!gpio_is_ready_dt(&led)) {
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
return 0;
}
k_msleep(SLEEP_TIME_MS);
k_msleep(SLEEP_TIME_MS);
test_display();
while (1) {
ret = gpio_pin_toggle_dt(&led);
if (ret < 0) {
return 0;
}
led_state = !led_state;
printf("LED state: %s\n", led_state ? "ON" : "OFF");
k_msleep(SLEEP_TIME_MS);
}
return 0;
}
6.完整项目
通过网盘分享的文件:zephyr 12864.zip
链接: https://pan.baidu.com/s/1a524HLCb_4qIfb6YEL2weA 提取码: hqe5