0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nRF Connect SDK:SPIM(V2.0+ 改版)

Posted at

過去の記事

以前にもSPIMの使い方の記事を書きました。

この時のSDKはまだV1.xだったこともあってSegger Embedded Studio(以下SES)での開発でした。

SESでの開発が関係あるのかは検証しておりませんが・・・。

VSCodeで動かしてみる

V2.0以降はVSCodeしかサポートしていないこともあって改めて検証してみます。と言っても普通に考えれば動くはずなんですけどね・・・けどね・・・動かないやん!(笑)
image.png

コンパイルできない

エラーメッセージを見てみるとNRFX_SPIM_INSTANCE(0)が見つからないらしい。んなアホな!
image.png

Kconfigを見てみる

V2.0になってからKconfigが結構な勢いで刷新されたので、もしかしたら設定が何か変わっているのかな、と思って確認してみました。
image.png
なんだこれ、グレーアウトしているということは使えないということ?どういうことなんだ・・・?

どうやらこういうことらしい

ここで紹介しているサンプルコードは全てNordicの評価ボードを使用しています。そのNordicの評価ボードのDT(Device Tree)で定義されているのが原因のようです。具体的には

52840dk.dts
&spi0 {
	compatible = "nordic,nrf-spi";
	/* Cannot be used together with i2c0. */
	/* status = "okay"; */
	pinctrl-0 = <&spi0_default>;
	pinctrl-1 = <&spi0_sleep>;
	pinctrl-names = "default", "sleep";
};

&spi1 {
	compatible = "nordic,nrf-spi";
	status = "okay";
	pinctrl-0 = <&spi1_default>;
	pinctrl-1 = <&spi1_sleep>;
	pinctrl-names = "default", "sleep";
};

&spi2 {
	compatible = "nordic,nrf-spi";
	status = "disabled";
	pinctrl-0 = <&spi2_default>;
	pinctrl-1 = <&spi2_sleep>;
	pinctrl-names = "default", "sleep";
};

の中にある

compatible = "nordic,nrf-spi";

があるため、SPIMではなくSPIとしてペリフェラルが定義されているようです。つまり、Nordicの評価ボードを使うのであればSPIMは3しか使えないということになります。
ということで修正してみました。

main.c
/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/zephyr.h>
#include <nrfx_spim.h>

#define SPIM_SCLK         (22)
#define SPIM_MOSI         (23)
#define SPIM_MISO         (24)
#define SPIM_SS           (25)

static const nrfx_spim_t m_spim3 = NRFX_SPIM_INSTANCE(3);

static uint8_t m_spim_tx_buffer[32], m_spim_rx_buffer[32];

void main(void)
{
    nrfx_err_t err_code;
    size_t tx_length, rx_length;

    // Initialize
    const nrfx_spim_config_t spim3_config = NRFX_SPIM_DEFAULT_CONFIG(SPIM_SCLK, SPIM_MOSI, SPIM_MISO, SPIM_SS);
    err_code = nrfx_spim_init(&m_spim3, &spim3_config, NULL, NULL);
    if (err_code != NRFX_SUCCESS)
    {
        printk("SPIM Initialize failed with:%d\n", err_code);
    }

    while (1)
    {
        // Read from SPI Device
        m_spim_tx_buffer[0] = 0xff;
        m_spim_tx_buffer[1] = 0x80;
        tx_length = 2;
        rx_length = 14;
        nrfx_spim_xfer_desc_t spim_xfer_desc = NRFX_SPIM_XFER_TRX(m_spim_tx_buffer, tx_length, m_spim_rx_buffer, rx_length);
        err_code = nrfx_spim_xfer(&m_spim3, &spim_xfer_desc, 0);
        if (err_code != NRFX_SUCCESS)
        {
            printk("SPI Error with:%d\n", err_code);
        }
        
        // Wait
        k_msleep(1000);
    }
}

これで無事にコンパイルが通りました。めでたしめでたし。

カスタムボードを作れば?

今回の問題を起こしているのが標準で存在しているNordic評価ボードのDTということは・・・ピコーン!自分でDTを作ればいいのでは?
はい、その通りです。先ほどの

compatible = "nordic,nrf-spi";

さえなくなればよいのです。ということで早速カスタムボードを作ってみます。カスタムボードの作り方は以前にも紹介しています。いや、紹介というレベルの記事じゃないですが(笑)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?