Azure SphereのMutable storage

18.11で64Kバイトまでの永続データを保存できる、Mutable storageというBETA機能が追加されました。

18.11全容についてはこちら

ちょっと使ってみようかなと思い、試してみました。

サンプルコード

Mutable storageを削除して、10回書き込みしてみます。
それぞれの処理時間をロジックアナライザで測定するので、GPIO0にHIGH/LOWを出力しましょう。
Mutable storageの関数の定義は、applibs/stoage.hです。
app_manifest.jsonにMutableStorageの設定が必要なので忘れずに。
また、コーディングの手間を減らすために、GPIOはeasyioライブラリを使いました。

#include <applibs/storage.h>
int main(int argc, char *argv[])
{
    Log_Debug("Application starting.\n");

    void* led;
    void* button;
    led = DigitalOut_new(0, 0);
    button = DigitalIn_new(MT3620_RDB_BUTTON_A);

    Log_Debug("Please push button A.\n");
    while (DigitalIn_read(button) == 1) {}
    Log_Debug("Executing...\n");

    static uint8_t data[1];
    for (int i = 0; i < sizeof(data); i++) data[i] = (uint8_t)(i % 256);

    DigitalOut_write(led, 1);  // GPIO0 = HIGH
    Storage_DeleteMutableFile();
    DigitalOut_write(led, 0);  // GPIO0 = LOW
    wait_ms(10);               // Sleep 10msec.

    for (int i = 0; i < 10; i++) {
        DigitalOut_write(led, 1);  // GPIO0 = HIGH

        int fd = Storage_OpenMutableFile();
        assert(fd >= 0);
        int size = write(fd, data, sizeof(data));
        assert(size == sizeof(data));
        close(fd);

        DigitalOut_write(led, 0);  // GPIO0 = LOW
        wait_ms(10);               // Sleep 10msec.
    }

    Log_Debug("Application exiting.\n");
    return 0;
}
{
  "SchemaVersion": 1,
  "Name" : "Mt3620App1",
  "ComponentId" : "19c6015f-0e17-49c5-b687-xxxxxxxxxxxx",
  "EntryPoint": "/bin/app",
  "CmdArgs": [],
  "Capabilities": {
    "AllowedConnections": [],
    "AllowedTcpServerPorts": [],
    "AllowedUdpServerPorts": [],
    "Gpio": [ 0, 12 ],
    "Uart": [],
    "WifiConfig": false,
    "NetworkConfig": false,
    "SystemTime": false,
    "MutableStorage": {
      "SizeKB": 64
    }
  }
}

ロジックアナライザ測定

いつものロジアナ(saleae logic)で、GPIO0を測定します。
本来、Groveシールドは必要ありませんが、、、ピン番号探すのが面倒だったので、GroveシールドとGroveジャンパケーブルを使って接続しました。

f:id:matsujirushix:20181216183333p:plain

測定した波形は、こんな感じ。

f:id:matsujirushix:20181216183501p:plain

測定結果

書き込むサイズを1~64Kに変化させて測った結果、

  • 削除は40ミリ秒
  • 初回書き込みは117~1827ミリ秒
  • 2回目以降書き込みは40~5975ミリ秒

でした。

f:id:matsujirushix:20181216183606p:plain

まとめ

  • Mutable storage、BETAだけど動いた。
  • 2回目以降の書き込みが遅い。書き込むときは削除→書き込みが良さそう。
  • 書き込み時間はデータ量にだいたい比例。ざっくりと30ミリ秒/バイト。