aimdevel’s blog

勉強したことを書きます

Raspberry Pi PicoとZephyr OSを使用したI2C LCD1602の実験

概要

前回から引き続き、ラズパイ入門キット「SunFounder Da Vinci Kit for Raspberry Pi」に入っている機器をRaspberr Pi Pico + Zephyr OSで動かしてみた。
今回は、I2C LCD1602を試した。

環境

I2C LCD1602を使用する。 Raspberr Pi Picoとを以下のように接続する。接続方法はSunFounderのチュートリアルを参考にした。

使用方法

以前と同様に作成したソースコードを以下に格納したので、これを使用する。

GitHub - aimdevel/my-zephyr-applications

Zephyrの開発環境構築については公式ドキュメントを参照。

Getting Started Guide — Zephyr Project Documentation

環境をセットアップ

前の記事と同様。

 west init -m https://github.com/aimdevel/my-zephyr-applications.git --mr main my-workspace
cd my-workspace/
west update

この手順はこのリポジトリを利用するときに1回だけ実施すればよい。

ビルド

west build -b rpi_pico apps/apps/i2c_lcd1602/

別のアプリのビルド結果が残っている場合、buildディレクトリを削除してから上記のコマンドを実行すること。

基板に焼いて動作確認

ディスプレイに以下の2つが交互に表示される。

Zephyr OS
I2C LCD1602
Smaple code
is running!

ソースコード説明

作成したdevicetree

作成したdevicetreeは以下。

my-zephyr-applications/apps/i2c_lcd1602/boards/rpi_pico.overlay at main · aimdevel/my-zephyr-applications · GitHub

全体は以下のようになっている。単純なi2cデバイスとして定義してある。compatibleは適当なものを指定しただけで、ほかのものでもよいはず。

&i2c0 {
    status = "okay";
    i2c_lcd1602: i2c_lcd1602@27 {
        compatible = "zephyr,i2c-target-eeprom";
        reg = <0x27>;
    };
};

このdevicetreeで重要なのは、i2c_lcd1602: i2c_lcd1602@27 {@27と、reg = <0x27>;の部分で、i2c lcd1602のスレーブアドレスを表している。
他の部分は適当で問題ないと思われる。

ソースコード

使用したコードは以下。

my-zephyr-applications/apps/i2c_lcd1602/src/main.c at main · aimdevel/my-zephyr-applications · GitHub

このソースコードは、sunfounderのサンプルコードを改変して作成した。
もとにしたコードは以下。

davinci-kit-for-raspberry-pi/c/1.1.7/1.1.7_Lcd1602.c at master · sunfounder/davinci-kit-for-raspberry-pi · GitHub

おおきな変更個所は、i2cを使用する関数をwiringPiI2Cからzephyr OSのものに置き換えたところだけで、ほかの部分はほぼそのまま使用できた。

i2cに関連する箇所は以下。

static const struct i2c_dt_spec lcd1602 = 
                I2C_DT_SPEC_GET(DT_NODELABEL(i2c_lcd1602));

devicetreeから情報を取得する。

i2c_write_dt(&lcd1602, &temp, 1);

tempの内容を1バイト書き込む。

まとめ

Zephyr OSでi2cを使用してlcd1602を動かした。

参考

GitHub - zephyrproject-rtos/zephyr: Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.

Zephyr Project Documentation — Zephyr Project Documentation

1.1.7 I2C LCD1602 — SunFounder davinci-kit-for-raspberry-pi ドキュメント

Raspberry Pi – I2C~LCD1602 – TauStation

https://www.handsontec.com/dataspecs/module/I2C_1602_LCD.pdf

https://www.handsontec.com/dataspecs/module/I2C_1602_LCD.pdf