nRF NRF_LOG
NRF_LOGでシリアル経由で文字列をPCに送って表示させる。
...\examples\peripheral\template_project
をexamplesの下に作ったmyprojectなりフォルダーにコピー。
...\examples\myprojects\template_project\pca10056\blank\sesの下のtemplate_pca10056.emProjectを開く。flash_placement.xmlのsize"0x04"の処理を行っておく。
インクルードファイル
おまじないと思って、
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
のヘッダーファイルをロードする。
左側のnRF_Logには2つのcファイルが入っているが、追加する。
projectファイル
右クリックをしてAdd Existing File...を選択。
.....\components\libraries\log\srcの階層に行き、全部で7つのcファイルがあるが、それを全部選択する。2つすでに存在しているものはdupulicateするか?と聞かれるので、NOを押す。
さっきは既存のフォルダにcファイルを足したが、nRF_Segger_RTTというフォルダを作る。
/../external/segger_rtt/SEGGER_RTT.c"
/../external/segger_rtt/SEGGER_RTT_Syscalls_SES.c" />
/../external/segger_rtt/SEGGER_RTT_printf.c"
の三つのcファイルを追加する。
sdk_config.hファイル
sdk_config.hを開く。
Ctrl+Fを押して、nrf_logを検索する。
#ifndef NRF_LOG_ENABLED
#define NRF_LOG_ENABLED 0
#endif
の部分を
#define NRF_LOG_ENABLED 1
に変更する
次に文字列として、
”NRF_LOG_BACKEND_UART_TX_PIN”を検索する。
no matchと表示されると設定の記述がないので、UARTの設定を書き込む。
// <h> nRF_Log
//==========================================================
// <e> NRF_LOG_ENABLED - nrf_log - Logger
//==========================================================
#ifndef NRF_LOG_ENABLED
#define NRF_LOG_ENABLED 1
#endif
NRF_LOG_BACKEND_UART_ENABLEDを検索。no matchの場合は以下を足し、ある場合は以下の設定に。
無い場合は、// <h> nRF_Log のすぐ下に
//==========================================================
// <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
//==========================================================
#ifndef NRF_LOG_BACKEND_RTT_ENABLED
#define NRF_LOG_BACKEND_RTT_ENABLED 1
#endif
// <o> NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings.
// <i> Size of the buffer is a trade-off between RAM usage and processing.
// <i> if buffer is smaller then strings will often be fragmented.
// <i> It is recommended to use size which will fit typical log and only the
// <i> longer one will be fragmented.
#ifndef NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE
#define NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE 64
#endif
// <o> NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS - Period before retrying writing to RTT
#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS
#define NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS 1
#endif
// <o> NRF_LOG_BACKEND_RTT_TX_RETRY_CNT - Writing to RTT retries.
// <i> If RTT fails to accept any new data after retries
// <i> module assumes that host is not active and on next
// <i> request it will perform only one write attempt.
// <i> On successful writing, module assumes that host is active
// <i> and scheme with retry is applied again.
#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_CNT
#define NRF_LOG_BACKEND_RTT_TX_RETRY_CNT 3
#endif
// </e>
// <e> NRF_LOG_BACKEND_UART_ENABLED - nrf_log_backend_uart - Log UART backend
//==========================================================
#ifndef NRF_LOG_BACKEND_UART_ENABLED
#define NRF_LOG_BACKEND_UART_ENABLED 1
#endif
// <o> NRF_LOG_BACKEND_UART_TX_PIN - UART TX pin
#ifndef NRF_LOG_BACKEND_UART_TX_PIN
#define NRF_LOG_BACKEND_UART_TX_PIN 43
#endif
// <o> NRF_LOG_BACKEND_UART_BAUDRATE - Default Baudrate
// <323584=> 1200 baud
// <643072=> 2400 baud
// <1290240=> 4800 baud
// <2576384=> 9600 baud
// <3862528=> 14400 baud
// <5152768=> 19200 baud
// <7716864=> 28800 baud
// <10289152=> 38400 baud
// <15400960=> 57600 baud
// <20615168=> 76800 baud
// <30801920=> 115200 baud
// <61865984=> 230400 baud
// <67108864=> 250000 baud
// <121634816=> 460800 baud
// <251658240=> 921600 baud
// <268435456=> 1000000 baud
#ifndef NRF_LOG_BACKEND_UART_BAUDRATE
#define NRF_LOG_BACKEND_UART_BAUDRATE 30801920
#endif
// <o> NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings.
// <i> Size of the buffer is a trade-off between RAM usage and processing.
// <i> if buffer is smaller then strings will often be fragmented.
// <i> It is recommended to use size which will fit typical log and only the
// <i> longer one will be fragmented.
#ifndef NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE
#define NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE 64
#endif
#ifndef NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE
#define NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE 64
#endif
// </e>
#define NRF_LOG_BACKEND_UART_TX_PINの部分は、TXピンを繋ぎこむ番号にしてやる。XAIOここでは43(P1.11)にした。
ファイルの末尾に、
// <<< end of configuration section >>>
#endif //SDK_CONFIG_H
とあるのでその上に、
//==========================================================
// <h> nRF_Segger_RTT
//==========================================================
// <h> segger_rtt - SEGGER RTT
//==========================================================
// <o> SEGGER_RTT_CONFIG_BUFFER_SIZE_UP - Size of upstream buffer.
// <i> Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE
// <i> or this value is actually used. It depends on which one is bigger.
#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 512
#endif
// <o> SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Size of upstream buffer.
#ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS
#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2
#endif
// <o> SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of upstream buffer.
#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16
#endif
// <o> SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Size of upstream buffer.
#ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS
#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2
#endif
// <o> SEGGER_RTT_CONFIG_DEFAULT_MODE - RTT behavior if the buffer is full.
// <i> The following modes are supported:
// <i> - SKIP - Do not block, output nothing.
// <i> - TRIM - Do not block, output as much as fits.
// <i> - BLOCK - Wait until there is space in the buffer.
// <0=> SKIP
// <1=> TRIM
// <2=> BLOCK_IF_FIFO_FULL
#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE
#define SEGGER_RTT_CONFIG_DEFAULT_MODE 0
#endif
// </h>
//==========================================================
// </h>
//==========================================================
コピーして配置
インクルードパス
../../../../../../integration/nrfx/legacy;
../../../../../../modules/nrfx/hal;
../../../../../../modules/nrfx/drivers/include;
../../../../../../external/segger_rtt;
があるか確認する。無い場合は足す。Templateからの場合/external/segger_rttがないので追加する。
main関数
../../../../../../integration/nrfx/legacy;
main()のすぐ下に、
//Initilization of NRF_LOG
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
を置いて初期化をする。
#include "nrf_delay.h"// for using nrf_delay
を上の方のincludeのところにおいて、
whileの中を書き換える
while (true)
{
printf("Hallo printf.\n\r");
NRF_LOG_INFO("Hello NRF_LOG.\n");
NRF_LOG_FLUSH();
nrf_delay_ms(500);
}
NRF_LOG_FLUSH();はバッファーを排出するコマンド。
コメント
コメントを投稿