使用RTT(Real-Time Terminal)作为高性能的日志输出工具

转自: https://devzone.nordicsemi.com/tutorials/6/


使用方式总结:


1.解压NordicSemiconductor.zip于某一位置。

2.工程中添加RTT文件夹,添加NordicSemiconductor中的SEGGER_RTT_printf.c/SEGGER_RTT.c/RTT_Syscalls_KEIL.c

3.options for Target -> C/C++ -> include Paths 中添加:
\NordicSemiconductor\RTT
\NordicSemiconductor\Syscalls

4.在使用的文件中添加:#include "SEGGER_RTT.h"

5.使用如下:
SEGGER_RTT_printf(0, "on_ble_evt 0x%02x\r\n", p_ble_evt->header.evt_id);


详细介绍:

Requirements

  • J-Link software pack v4.98
  • RTT-files
  • Keil uVision 5.14

Adding RTT files to project

As with the previous tutorials, this tutorial will be based on the ble_app_uart project, but any other project can be used.

  • Open the ble_app_uart example as explained in tutorial 1

  • Download the zip file with the required RTT files as linked above, and unzip it

  • Copy the RTT and Syscalls folder from the zip file into C:\Keil_v5\ARM\Pack\NordicSemiconductor

  • In Keil, go to Project -> Options for target

  • In the C/C++ tab, add C:\Keil_v5\ARM\Pack\NordicSemiconductor\RTT to the include path

  • Include SEGGER_RTT.h to the project by writing #include "SEGGER_RTT.h" near the top of the main.c file in the project

Now we have the header file for the RTT functions but we also have to add the .c files to the project.

  • In Keil, right-click on the project-folder and click Add Group. Name it RTT

  • Right click on the new group and select Add existing files to group 'RTT'

  • Navigate to C:\Keil_v5\ARM\Pack\NordicSemiconductor\RTT and add SEGGER_RTT.c

You should now be able to send simple strings through the RTT interface. In the main function in the main file, add the following line right before the main loop:

SEGGER_RTT_WriteString(0, "Hello World!\n");

The first argument is which channel to write the text to. You can read more about channels on the SEGGER webapge, but the default channel is 0. Compile the code to make sure everything went ok.



Opening a Real Time Terminal

Now that the code is sending output to our RTT, we have to be able to read it. There are several ways this can be done, as summarized on the segger webpage. The easiest way is to use the J-Link RTT Viewer that comes with the J-link software package.

  • Open J-Link RTT Viewer. You will then see the image below. If you have more than one device connected, check "Serial no." and enter the serial number of the device you wish to connect to.

  • Click Ok. The screen below will then appear.

Now, load the code you compiled above onto your device, and you should see the text "Hello World!" appear. Notice if you open Termite you will also see the text "Start.." which is still printed on the serial port as before. You can now use the ble_app_uart project as intended, but also send text to RTT in order to debug. Keep in mind that SEGGER_RTT_WriteString() is much faster than printf, so you can safely call this function without it affecting the real time properties of your application.



Sending text to target

You can also use RTT to send text to your device, as an alternative to UART. Modify the main loop in the ble_app_uart project so it looks like below. Remember to also include nrf_delay.h to use the delay function.

char c = 0;
for (;;)
{
    c = SEGGER_RTT_WaitKey(); // will block until data is available
    if(c == 'r'){
        SEGGER_RTT_WriteString(0, "Resetting..\n");
        nrf_delay_ms(1000);
        sd_nvic_SystemReset();
    }
    //power_manage();
}
  • Compile and flash the code onto the device

  • In RTT Viewer, go to Input -> Sending and click Send on Enter. (Otherwise it sends on every key you press, which can be annoying)

  • Write an 'r' in the textfield and hit enter. You should now see the board resetting.



More advanced printing

So far we have only looked at unformatted printing, using SEGGER_RTT_WriteString(). A more powerful function is the SEGGER_RTT_printf() function. To be able to use it, we must modify the project a bit more.

  • Add the file SEGGER_RTT_printf.c to the project, just like you added SEGGER_RTT.c above

  • In the same way, add RTT_Syscalls_KEIL.c, fromC:\Keil_v5\ARM\Pack\NordicSemiconductor\Syscalls

  • Right-click nRF_Libraries and click Options

  • In the list of Software Components, click retarget, and then the remove-button

  • Now go to Project -> Options for target, and uncheck "Use MicroLIB"

  • Edit the main loop so it looks like below:
char c = 0; 
for (;;) {
    c = SEGGER_RTT_WaitKey(); // will block until data is available
    if(c == 'r'){
        SEGGER_RTT_printf(0, "%sResetting in %d second..%s\n", RTT_CTRL_BG_BRIGHT_RED, 1, RTT_CTRL_RESET);
        nrf_delay_ms(1000);
        sd_nvic_SystemReset();
    }
    //power_manage();
}

Compile and run the project. Notice that the output can be color-coded to allow for a visually appealing debug output. In RTT-viewer, go to Terminal 0 to see the colored output.

Notice that the printf function also directs its output to RTT now. The ble_app_uart project will still work as intended though, because it uses app_uart_put which outputs to the serial port.

An example on how to use the printf function:

SEGGER_RTT_printf(0, "variable value: %d\n", variable);

where the first parameter specifies that the output should be printed to Terminal 0 in the J-Link RTT Viewer, second parameter is the string to print, and the value of "variable" is inserted for "%d"

For more information on RTT and the various functions, you can read more in chapter 10 in the J-Link manual.


你可能感兴趣的:(BLE)