FRDM RW612 - zephyr rtosでspiを利用してmicro sdcardを使用してみる。
-
はじめに
NXPの無線通信対応マイコン評価ボード「FRDM-RW612
および、マイクロSDカードスロットDIP化キットを用いてzephyr rtosのアプリケーションからspiを利用してマイクロsdカードを使用した際の記録メモです。
-
参照資料
-
ArduinoでmicroSDカードを使う
https://qiita.com/hikoalpha/items/c7812a34182db07036ef -
FRDM-RW612 Board User Manual
https://www.mouser.com/pdfDocs/NXP_FRDM-RW612_UM.pdf?srsltid=AfmBOor_wIVRnkosiXMg6zJELUkDcBcYkFjMHsk1NeP21g12clr5lkM1
-
-
確認時に使用したzephyr rtosのサンプルアプリケーション
samples/hello_worldに対して変更を加えて確認
build
west build -b frdm_rw612 samples/hello_world -p always
書き込み
sudo env "PATH=/usr/local/LinkServer_26.3.123:$PATH" $(which west) flash -r linkserver
- 機器の接続
FRDM-RW612はポート電圧3.3Vで利用できるためレベルシフタは用いない
接続先
| SPI | FRDM RW612 | microSD |
|---|---|---|
| MOSI | J1-2 | DAT0 |
| MISO | J1-4 | CMD |
| CS | J2-6 | CD/DAT3 |
| SCK | J2-12 | CLK |
| GND | J2-14 | VSS |
| 3.3V | J2-16 | VDD |
- prj.confの変更
samples/hello_world/prj.conf
#nothing here
+CONFIG_MAIN_STACK_SIZE=4096
+CONFIG_LOG=y
+CONFIG_LOG_MODE_IMMEDIATE=y
+CONFIG_SPI=y
+CONFIG_DISK_ACCESS=y
+CONFIG_SDHC=y
+CONFIG_FILE_SYSTEM=y
+CONFIG_FAT_FILESYSTEM_ELM=y
- dtsの追加
samples/boards/frdm_rw612.overlay
&flexcomm1 {
cs-gpios = <&hsgpio0 6 GPIO_ACTIVE_LOW>;
sdhc0: sdhc@0 {
compatible = "zephyr,sdhc-spi-slot";
reg = <0>;
status = "okay";
spi-max-frequency = <25000000>;
mmc0: mmc {
compatible = "zephyr,sdmmc-disk";
disk-name = "SD";
status = "okay";
};
};
};
- main.cの変更
samples/hello_world/src/main.c
#include <stdio.h>
+#include <zephyr/kernel.h>
+#include <zephyr/device.h>
+#include <zephyr/storage/disk_access.h>
+#include <zephyr/fs/fs.h>
+#include <ff.h>
+static FATFS fat_fs;
+static struct fs_mount_t mp = {
+ .type = FS_FATFS,
+ .fs_data = &fat_fs,
+ .mnt_point = "/SD:",
+};
int main(void)
{
+ struct fs_file_t file;
+ int rc;
printf("Hello World! %s\n", CONFIG_BOARD_TARGET);
+ printk("\n*** SD Card SPI Test on FRDM-RW612 ***\n");
+
+ rc = fs_mount(&mp);
+ if (rc < 0) {
+ printk("Error: Failed to mount SD card (%d)\n", rc);
+ printk("Please check wiring and ensure SD card is formatted as FAT32.\n");
+ return rc;
+ }
+
+ fs_file_t_init(&file);
+ rc = fs_open(&file, "/SD:/hello.txt", FS_O_CREATE | FS_O_WRITE);
+ if(rc < 0) {
+ printk("Error: Failed to open/create file (%d\n)", rc);
+ return rc;
+ }
+
+ const char *text = "Hello from Zephyr RTOS on FRDM-RW612!\n";
+ rc = fs_write(&file, text, strlen(text));
+ if(rc < 0) {
+ printk("Error: Failed to write to file (%d)", rc);
+ } else {
+ printk("Success: Wrote %d byte to /SD:hello.txt\n", rc);
+ }
+
+ fs_close(&file);
+ printk("Test completed.\n");
- アプリケーション実行時のメッセージ出力
microSDカードには、
hello.txtというファイルにHello from Zephyr RTOS on FRDM-RW612!が書き込まれる。
*** Booting Zephyr OS build v4.4.0-8011-gb01be6b7b16e ***
Hello World! frdm_rw612/rw612
*** SD Card SPI Test on FRDM-RW612 ***
Success: Wrote 38 byte to /SD:hello.txt
Test completed.

