avatar🌌
零醇零醇の小屋

樱花树下,再续前缘

鹤望兰无凋零之时,比翼鸟永世长存

rk3506:点亮 OLED 和 MAX98357,实现 Bad Apple!!

上回书说到 彼时的rk3506 内部有设备树 外部有电路
老东西们聚在一起 要干一场大的
传说 题目在出征之时 曾言道——(三行全部划去)(/ω\)

串台了 本期要聊的是 点亮oled和max98357 来实现老东西们最爱的badapple ( ゚∀゚)o彡°


第一部分 I2C ​

在点亮oled之前 我们先来学习i2c写法 (`・ω・´)
在我stm32的入门教程里 有写到过 这里不讲底层时序了 直接从代码开始切入
这里采用的江协大人的oled驱动 稍加魔改

丢弃init、start和stop函数

修改:

c
void OLED_WriteCommand(uint8_t Command)
{
    uint8_t buf[2] = {0x00, Command};
    write(fd, buf, 2);
}
void OLED_WriteData(uint8_t Data)
{
    uint8_t buf[2] = {0x40, Data};
    write(fd, buf, 2);
}

修改 void OLED_Init 函数,变为:

c
void OLED_Init(void)
{
    usleep(100000);
    fd = open("/dev/i2c-0", O_RDWR);
    if (fd < 0) {
        perror("open /dev/i2c-0 failed");
        return;
    }
    if (ioctl(fd, I2C_SLAVE, 0x3c) < 0) {
        perror("ioctl I2C_SLAVE failed");
        close(fd);
        fd = -1;
        return;
    }

    OLED_WriteCommand(0xAE); // 关闭显示
    OLED_WriteCommand(0xD5); // 设置时钟分频/振荡器频率
    OLED_WriteCommand(0xF0); // 提高内部刷新率,减少动画撕裂
    OLED_WriteCommand(0xA8); // 多路复用率
    OLED_WriteCommand(0x3F);
    OLED_WriteCommand(0xD3); // 显示偏移
    OLED_WriteCommand(0x00);
    OLED_WriteCommand(0x40); // 显示开始行
    OLED_WriteCommand(0xA1); // 左右正常
    OLED_WriteCommand(0xC8); // 上下正常
    OLED_WriteCommand(0xDA); // COM引脚配置
    OLED_WriteCommand(0x12);
    OLED_WriteCommand(0x81); // 对比度
    OLED_WriteCommand(0xCF);
    OLED_WriteCommand(0xD9); // 预充电周期
    OLED_WriteCommand(0xF1);
    OLED_WriteCommand(0xDB); // VCOMH
    OLED_WriteCommand(0x30);
    OLED_WriteCommand(0xA4); // 全屏点亮跟随RAM
    OLED_WriteCommand(0xA6); // 正常显示
    OLED_WriteCommand(0x8D); // 充电泵
    OLED_WriteCommand(0x14);
    OLED_WriteCommand(0xAF); // 开启显示

    OLED_Clear();
}

补充:

c
void OLED_WriteDataBuf(const uint8_t *Data, uint16_t Length)
{
    uint8_t buf[129];
    buf[0] = 0x40; // 首字节固定为数据标志

    while (Length > 0) {
        uint16_t chunk = (Length > 128) ? 128 : Length;
        memcpy(buf + 1, Data, chunk);
        write(fd, buf, chunk + 1);
        Data += chunk;
        Length -= chunk;
    }
}

void OLED_DrawFrame(const uint8_t *Frame)
{
    for (uint8_t page = 0; page < 8; page++) {
        OLED_SetCursor(page, 0);
        OLED_WriteDataBuf(Frame + page * 128, 128);
    }
}

就可以了 (・ω< )★

(完整 OLED.c、OLED.h 和字模库放文章最后了,方便查阅 (^▽^))


第二部分 I2S ​

处理完屏幕代码之后 我们需要来处理max98357
max98357是一个i2s驱动的dac功放 这里我们使用它连接喇叭

首先利用设备树检查有多少i2s接口:

bash
reiatsushi@reiatsushi-rk3506-debian:~$ grep -E -A 15 "^[[:space:]]+(sai|i2s)@" ~/dtb_lab/fdt.dts

sai@ff310000 {
compatible = "rockchip,rk3506-sai", "rockchip,sai-v1";
reg = <0xff310000 0x1000>;
interrupts = <0x00 0x33 0x04>;
clocks = <0x02 0x9f 0x02 0xa0>;
clock-names = "mclk", "hclk";
dmas = <0x22 0x03 0xff2880a4 0x4000000 0x00 0x00 0x22 0x02 0xff2880a4 0x2000000 0x00 0x00>;
dma-names = "tx", "rx";
resets = <0x02 0xe2 0x02 0xe3>;
reset-names = "m", "h";
#sound-dai-cells = <0x00>;
sound-name-prefix = "SAI1";
pinctrl-names = "default", "sleep";
pinctrl-0 = <0x37 0x38 0x39 0x3a>;
status = "okay";
pinctrl-1 = <0x3b 0x3c 0x3d 0x3e>;
--
sai@ff4a0000 {
compatible = "rockchip,rk3506-sai", "rockchip,sai-v1";
reg = <0xff4a0000 0x1000>;
interrupts = <0x00 0x35 0x04>;
clocks = <0x02 0xc4 0x02 0xc3>;
clock-names = "mclk", "hclk";
dmas = <0x22 0x06 0x00 0x00 0x00 0x00 0x22 0x07 0x00 0x00 0x00 0x00>;
dma-names = "tx", "rx";
resets = <0x02 0x127 0x02 0x126>;
reset-names = "m", "h";
#sound-dai-cells = <0x00>;
sound-name-prefix = "SAI3";
pinctrl-names = "default";
pinctrl-0 = <0x52 0x53 0x54 0x55>;
status = "disabled";
phandle = <0xa3>;
--
sai@ff4a8000 {
compatible = "rockchip,rk3506-sai", "rockchip,sai-v1";
reg = <0xff4a8000 0x1000>;
interrupts = <0x00 0x36 0x04>;
clocks = <0x02 0xc8 0x02 0xc7>;
clock-names = "mclk", "hclk";
dmas = <0x22 0x08 0x00 0x00 0x00 0x00>;
dma-names = "rx";
resets = <0x02 0x12b 0x02 0x12a>;
reset-names = "m", "h";
#sound-dai-cells = <0x00>;
sound-name-prefix = "SAI4";
status = "okay";
phandle = <0x9e>;
};

这里可以看到有几个sai控制器 我们这里选择第一个:

bash
reiatsushi@reiatsushi-rk3506-debian:~$ grep -B 2 -A 25 "es8388-sound\|rockchip-es8388\|simple-audio-card" ~/dtb_lab/fdt.dts
        mmc@ff480000 {
                compatible = "rockchip,rk3506-dw-mshc", "rockchip,rk3288-dw-mshc";
                reg = <0xff480000 0x4000>;
                interrupts = <0x00 0x56 0x04>;

        acodec-sound {
                compatible = "simple-audio-card";
                simple-audio-card,name = "rockchip,acodec";
                simple-audio-card,format = "i2s";
                simple-audio-card,mclk-fs = <0x400>;
                simple-audio-card,bitclock-master = <0x9d>;
                simple-audio-card,frame-master = <0x9d>;

                simple-audio-card,cpu {
                        sound-dai = <0x9e>;
                };

                simple-audio-card,codec {
                        sound-dai = <0x9f>;
                        phandle = <0x9d>;
                };
        };

        adc-keys {
                compatible = "adc-keys";
                io-channels = <0xa0 0x01>;
                io-channel-names = "buttons";
                keyup-threshold-microvolt = <0x1b7740>;
                poll-interval = <0x64>;

                vol-up-key {
                        label = "volume up";
                        linux,code = <0x73>;
                        press-threshold-microvolt = <0x3e80>;
                };

                vol-down-key {
                        label = "volume down";
                        linux,code = <0x72>;
                        press-threshold-microvolt = <0x668a0>;
                };

                menu-key {
--
        };

        es8388-sound {
                status = "okay";
                compatible = "rockchip,multicodecs-card";
                rockchip,card-name = "rockchip-es8388";
                rockchip,pre-power-on-delay-ms = <0x1e>;
                rockchip,post-power-down-delay-ms = <0x28>;
                rockchip,format = "i2s";
                rockchip,mclk-fs = <0x100>;
                rockchip,cpu = <0xa5>;
                rockchip,codec = <0xa6>;
                rockchip,audio-routing = "Headphone", "LOUT1", "Headphone", "ROUT1", "Headphone", "Headphone Power", "Headphone", "Headphone Power", "LINPUT1", "Main Mic", "LINPUT2", "Main Mic", "RINPUT1", "Headset Mic", "RINPUT2", "Headset Mic";
                pinctrl-names = "default";
                pinctrl-0 = <0xa7>;
                hp-det-gpio = <0x19 0x06 0x00>;
                hp-con-gpio = <0x19 0x07 0x00>;
                keyup-threshold-microvolt = <0x1b7740>;
                poll-interval = <0x64>;

                play-pause-key {
                        label = "playpause";
                        linux,code = <0xa4>;
                        press-threshold-microvolt = <0x7d0>;
                };
        };

        fiq-debugger {
                compatible = "rockchip,fiq-debugger";
                rockchip,serial-id = <0x00>;
                rockchip,wake-irq = <0x00>;
reiatsushi@reiatsushi-rk3506-debian:~$ grep -E -A 6 "rm_io(6|7|16|17|24) \{" ~/dtb_lab/fdt.dts
                rm_io6 {

                        rm-io6-pwm0-ch3 {
                                rockchip,pins = <0x00 0x06 0x30 0x78>;
                                phandle = <0x6a>;
                        };
                };
--
                rm_io16 {

                        rm-io16-uart2-rx {
                                rockchip,pins = <0x00 0x10 0x13 0x72>;
                                phandle = <0x1f>;
                        };

--
                rm_io17 {

                        rm-io17-uart2-tx {
                                rockchip,pins = <0x00 0x11 0x12 0x6f>;
                                phandle = <0x1e>;
                        };

这里我们利用 RM_IO6、RM_IO7 和 RM_IO13。

首先禁用板上自带的声卡:

bash
sed -i '/^[[:space:]]*es8388-sound {/,/compatible = "rockchip,multicodecs-card";/s/status = "okay";/status = "disabled";/' fdt.dts

禁用 RM_IO6 的 pwm:

bash
sed -i '/^[[:space:]]*pwm@ff933000 {/,/};/s/status = "okay";/status = "disabled";/' fdt.dts

插入 max98357 虚拟 codec 和声卡节点:

bash
sed -i '/^[[:space:]]*es8388-sound {/i \
\tmax98357-codec {\
\t\tcompatible = "rockchip,dummy-codec";\
\t\t#sound-dai-cells = <0x00>;\
\t\tstatus = "okay";\
\t\tphandle = <0x203>;\
\t};\
\tmax98357-sound {\
\t\tcompatible = "simple-audio-card";\
\t\tsimple-audio-card,name = "max98357a";\
\t\tsimple-audio-card,format = "i2s";\
\t\tsimple-audio-card,mclk-fs = <0x100>;\
\t\tsimple-audio-card,bitclock-master = <0x204>;\
\t\tsimple-audio-card,frame-master = <0x204>;\
\t\tstatus = "okay";\
\t\tsimple-audio-card,cpu {\
\t\t\tsound-dai = <0xa5>;\
\t\t\tphandle = <0x204>;\
\t\t};\
\t\tsimple-audio-card,codec {\
\t\t\tsound-dai = <0x203>;\
\t\t};\
\t};' fdt.dts

插入 3 个 RM_IO 的 SAI1 引脚复用定义:

bash
sed -i '/^[[:space:]]*rm_io6 {/i \
\t\tmax98357_sai1_pins {\
\t\t\trm-io6-sai1-sclk {\
\t\t\t\trockchip,pins = <0x00 0x06 0x4b 0x6f>;\
\t\t\t\tphandle = <0x200>;\
\t\t\t};\
\t\t\trm-io7-sai1-lrck {\
\t\t\t\trockchip,pins = <0x00 0x07 0x4c 0x6f>;\
\t\t\t\tphandle = <0x201>;\
\t\t\t};\
\t\t\trm-io13-sai1-sdo0 {\
\t\t\t\trockchip,pins = <0x00 0x0d 0x4e 0x6f>;\
\t\t\t\tphandle = <0x202>;\
\t\t\t};\
\t\t};' fdt.dts

将 sai@ff310000 切换到新引脚并删除 sleep 配置:

bash
sed -i '/^[[:space:]]*sai@ff310000 {/,/};/{
    s/pinctrl-names = "default", "sleep";/pinctrl-names = "default";/
    s/pinctrl-0 = <0x37 0x38 0x39 0x3a>;/pinctrl-0 = <0x200 0x201 0x202>;/
    /pinctrl-1 = <0x3b 0x3c 0x3d 0x3e>;/d
}' fdt.dts

编译:

bash
dtc -I dts -O dtb -o fdt_new.dtb fdt.dts 2>&1 | grep -iE "error|graph_endpoint"

刷写:

bash
mkimage -f boot.its -E -p 0x800 boot_new.img && \
sudo dd if=boot_new.img of=/dev/mmcblk0p3 bs=1M conv=fsync && \
sync && \
sudo poweroff

然后接线 (。・ω・。)ノ♡

OLED 接线:

  • /dev/i2c-0 SDA(数据):GPIO1_B2,RM_IO25,<0x01 0x0a> → 第 1 组,第 10 号脚(B2)
  • /dev/i2c-0 SCL(时钟):GPIO1_B3,RM_IO26,<0x01 0x0b> → 第 1 组,第 11 号脚(B3)
  • VCC → 3.3V
  • GND → GND

MAX98357 接线:

  • VIN → 5V
  • GND → GND
  • BCLK → RM_IO6
  • LRC → RM_IO7
  • DIN → RM_IO13
  • SD → 3.3V
  • GAIN → 5V

接好后上电!


第三部分 准备视频 ​

首先安装 ffmpeg:

bash
sudo apt install -y ffmpeg

分离音频:

bash
ffmpeg -y -i badapple.mp4 -vn -acodec pcm_s16le -ar 48000 -ac 2 badapple.wav

分离视频:

bash
ffmpeg -y -i badapple.mp4 -vf "scale=128:64,fps=60" -f rawvideo -pix_fmt gray badapple.raw

视频格式转换 raw2oled.c:

bash
cat << 'EOF' > raw2oled.c
#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main(void) {
    FILE *fin = fopen("badapple.raw", "rb");
    FILE *fout = fopen("badapple.bin", "wb");
    if (!fin || !fout) { perror("open file"); return 1; }

    uint8_t raw[128 * 64];
    uint8_t oled[1024];
    long frames = 0;

    while (fread(raw, 1, sizeof(raw), fin) == sizeof(raw)) {
        memset(oled, 0, sizeof(oled));
        for (int page = 0; page < 8; page++) {
            for (int x = 0; x < 128; x++) {
                uint8_t byte = 0;
                for (int bit = 0; bit < 8; bit++) {
                    if (raw[(page * 8 + bit) * 128 + x] > 127)
                        byte |= (1 << bit);
                }
                oled[page * 128 + x] = byte;
            }
        }
        fwrite(oled, 1, sizeof(oled), fout);
        frames++;
    }

    fclose(fin);
    fclose(fout);
    printf("转换完成,共 %ld 帧\n", frames);
    return 0;
}
EOF

编译并运行:

bash
gcc raw2oled.c -o raw2oled
./raw2oled && rm -f badapple.raw
ls -l

准备 badapple.c:

bash
cat << 'EOF' > badapple.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/wait.h>
#include "OLED.h"

#define FPS 60.0
static pid_t audio_pid = -1;
static volatile int running = 1;

void handle_sigint(int sig) {
    (void)sig;
    running = 0;
}

double now_sec(void) {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec + ts.tv_nsec * 1e-9;
}

int main(void) {
    signal(SIGINT, handle_sigint);
    signal(SIGTERM, handle_sigint);

    FILE *fp = fopen("badapple.bin", "rb");
    if (!fp) { perror("open badapple.bin"); return 1; }

    fseek(fp, 0, SEEK_END);
    long total_frames = ftell(fp) / 1024;
    rewind(fp);

    OLED_Init();

    audio_pid = fork();
    if (audio_pid == 0) {
        execlp("aplay", "aplay", "-q", "-D", "plughw:1,0", "badapple.wav", NULL);
        _exit(1);
    }

    uint8_t frame[1024];
    double start = now_sec();
    long last_frame = -1;

    while (running) {
        long target = (long)((now_sec() - start) * FPS);
        if (target >= total_frames) break;
        if (target == last_frame) {
            usleep(1000);
            continue;
        }
        fseek(fp, target * 1024L, SEEK_SET);
        if (fread(frame, 1, 1024, fp) != 1024) break;
        OLED_DrawFrame(frame);
        last_frame = target;
    }

    if (audio_pid > 0) {
        kill(audio_pid, SIGTERM);
        waitpid(audio_pid, NULL, 0);
    }
    OLED_Clear();
    fclose(fp);
    return 0;
}
EOF

第四部分 bad apple ​

编译 badapple.c(记得带上 OLED.c):

bash
gcc badapple.c OLED.c -o badapple

执行:

bash
sudo ./badapple

最后的最后:
Enjoy Your Bad Apple!!! (。・ω・。)ノ♡

图1图2


附录:OLED 驱动完整源码 ​

OLED.h ​

c
#ifndef __OLED_H
#define __OLED_H

#include <stdint.h>

void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawFrame(const uint8_t *Frame);
void OLED_ShowChar(uint8_t Line, uint8_t Column, const char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, const char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, const uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, const int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, const uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, const uint32_t Number, uint8_t Length);

#endif

OLED.c ​

c
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include "OLED.h"
#include "OLED_Font.h"

static int fd = -1;

void OLED_WriteCommand(uint8_t Command)
{
    if (fd < 0) return;
    uint8_t buf[2] = {0x00, Command};
    write(fd, buf, 2);
}

void OLED_WriteData(uint8_t Data)
{
    if (fd < 0) return;
    uint8_t buf[2] = {0x40, Data};
    write(fd, buf, 2);
}

void OLED_WriteDataBuf(const uint8_t *Data, uint16_t Length)
{
    if (fd < 0) return;
    uint8_t buf[129];
    buf[0] = 0x40;

    while (Length > 0) {
        uint16_t chunk = (Length > 128) ? 128 : Length;
        memcpy(buf + 1, Data, chunk);
        write(fd, buf, chunk + 1);
        Data += chunk;
        Length -= chunk;
    }
}

void OLED_SetCursor(uint8_t Y, uint8_t X)
{
    if (fd < 0) return;
    uint8_t cmd[6] = {
        0x00,
        0x20, 0x02,                        // 切回页寻址模式,确保字符定位准确
        (uint8_t)(0xB0 | Y),               // 设置Y位置
        (uint8_t)(0x10 | ((X & 0xF0) >> 4)), // 设置X高4位
        (uint8_t)(0x00 | (X & 0x0F))       // 设置X低4位
    };
    write(fd, cmd, 6);
}

void OLED_DrawFrame(const uint8_t *Frame)
{
    if (fd < 0) return;

    /* 切换水平寻址并复位全屏窗口(0~127列,0~7页),防止画面错位 */
    uint8_t cmd[9] = {
        0x00,
        0x20, 0x00,        // 水平寻址模式
        0x21, 0x00, 0x7F,  // 列范围 0~127
        0x22, 0x00, 0x07   // 页范围 0~7
    };
    write(fd, cmd, 9);

    /* 整帧 1024 字节一次发出 */
    uint8_t buf[1025];
    buf[0] = 0x40;
    memcpy(buf + 1, Frame, 1024);
    write(fd, buf, 1025);
}

void OLED_Clear(void)
{
    uint8_t zeros[1024] = {0};
    OLED_DrawFrame(zeros);
}

void OLED_ShowChar(uint8_t Line, uint8_t Column, const char Char)
{
    OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);
    OLED_WriteDataBuf(&OLED_F8x16[Char - ' '][0], 8);

    OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);
    OLED_WriteDataBuf(&OLED_F8x16[Char - ' '][8], 8);
}

void OLED_ShowString(uint8_t Line, uint8_t Column, const char *String)
{
    uint8_t i;
    for (i = 0; String[i] != '\0'; i++) {
        OLED_ShowChar(Line, Column + i, String[i]);
    }
}

uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
    uint32_t Result = 1;
    while (Y--) {
        Result *= X;
    }
    return Result;
}

void OLED_ShowNum(uint8_t Line, uint8_t Column, const uint32_t Number, uint8_t Length)
{
    uint8_t i;
    for (i = 0; i < Length; i++) {
        OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
    }
}

void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, const int32_t Number, uint8_t Length)
{
    uint8_t i;
    uint32_t Number1;
    if (Number >= 0) {
        OLED_ShowChar(Line, Column, '+');
        Number1 = Number;
    } else {
        OLED_ShowChar(Line, Column, '-');
        Number1 = -Number;
    }
    for (i = 0; i < Length; i++) {
        OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
    }
}

void OLED_ShowHexNum(uint8_t Line, uint8_t Column, const uint32_t Number, uint8_t Length)
{
    uint8_t i, SingleNumber;
    for (i = 0; i < Length; i++) {
        SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
        if (SingleNumber < 10) {
            OLED_ShowChar(Line, Column + i, SingleNumber + '0');
        } else {
            OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
        }
    }
}

void OLED_ShowBinNum(uint8_t Line, uint8_t Column, const uint32_t Number, uint8_t Length)
{
    uint8_t i;
    for (i = 0; i < Length; i++) {
        OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
    }
}

void OLED_Init(void)
{
    usleep(100000);
    fd = open("/dev/i2c-0", O_RDWR);
    if (fd < 0) {
        perror("open /dev/i2c-0 failed");
        return;
    }
    if (ioctl(fd, I2C_SLAVE, 0x3c) < 0) {
        perror("ioctl I2C_SLAVE failed");
        close(fd);
        fd = -1;
        return;
    }

    OLED_WriteCommand(0xAE); // 关闭显示
    OLED_WriteCommand(0xD5); // 设置时钟分频/振荡器频率
    OLED_WriteCommand(0xF0); // 提高内部刷新率,减少动画撕裂
    OLED_WriteCommand(0xA8); // 多路复用率
    OLED_WriteCommand(0x3F);
    OLED_WriteCommand(0xD3); // 显示偏移
    OLED_WriteCommand(0x00);
    OLED_WriteCommand(0x40); // 显示开始行
    OLED_WriteCommand(0xA1); // 左右正常
    OLED_WriteCommand(0xC8); // 上下正常
    OLED_WriteCommand(0xDA); // COM引脚配置
    OLED_WriteCommand(0x12);
    OLED_WriteCommand(0x81); // 对比度
    OLED_WriteCommand(0xCF);
    OLED_WriteCommand(0xD9); // 预充电周期
    OLED_WriteCommand(0xF1);
    OLED_WriteCommand(0xDB); // VCOMH
    OLED_WriteCommand(0x30);
    OLED_WriteCommand(0xA4); // 全屏点亮跟随RAM
    OLED_WriteCommand(0xA6); // 正常显示
    OLED_WriteCommand(0x8D); // 充电泵
    OLED_WriteCommand(0x14);
    OLED_WriteCommand(0xAF); // 开启显示

    OLED_Clear();
}

OLED_Font.h ​

c
#ifndef __OLED_FONT_H
#define __OLED_FONT_H

/*OLED字模库,宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16]=
{
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//  0
	
	0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
	
	0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
	
	0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
	0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
	
	0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
	0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
	
	0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
	0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
	
	0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
	0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
	
	0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
	
	0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
	0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
	
	0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
	0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
	
	0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
	0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
	
	0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
	0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
	
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
	
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
	
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
	
	0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
	0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
	
	0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
	0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
	
	0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
	0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
	
	0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
	0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
	
	0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
	0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
	
	0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
	0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
	
	0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
	0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
	
	0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
	0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
	
	0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
	0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
	
	0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
	0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
	
	0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
	0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
	
	0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
	0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
	
	0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
	0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
	
	0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
	0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
	
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
	0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
	
	0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
	0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
	
	0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
	0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
	
	0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
	0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
	
	0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
	0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
	
	0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
	0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
	
	0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
	0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
	
	0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
	0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
	
	0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
	0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
	
	0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
	0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
	
	0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
	0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
	
	0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
	0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
	
	0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
	0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
	
	0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
	0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
	
	0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
	0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
	
	0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
	0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
	
	0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
	0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
	
	0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
	0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
	
	0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
	0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
	
	0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
	0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
	
	0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
	0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
	
	0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
	0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
	
	0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
	0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
	
	0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
	0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
	
	0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
	0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
	
	0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
	0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
	
	0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
	0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
	
	0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
	0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
	
	0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
	0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
	
	0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
	0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
	
	0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
	0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
	
	0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
	
	0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
	0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
	
	0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
	
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
	
	0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
	
	0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
	0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
	
	0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
	0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
	
	0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
	0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
	
	0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
	0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
	
	0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
	0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
	
	0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
	0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
	
	0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
	0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
	
	0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
	0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
	
	0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
	0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
	
	0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
	0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//j 74
	
	0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
	0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
	
	0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
	0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
	
	0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
	0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
	
	0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
	0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
	
	0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
	0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
	
	0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
	0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
	
	0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
	0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
	
	0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
	0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
	
	0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
	0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
	
	0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
	0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
	
	0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
	0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
	
	0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
	0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
	
	0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
	0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
	
	0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
	0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
	
	0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
	0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
	
	0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
	0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
	
	0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
	0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
	
	0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
	
	0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
	0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
	
	0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};

#endif
rk3506:初识设备树