快速入門:建置 Blink 應用程式Quickstart: Build the Blink application

本快速入門示範如何在 Azure Sphere 裝置上進行應用程式開發,以及如何建置和偵錯高層級應用程式。This quickstart shows how to enable application development on an Azure Sphere device and how to build and debug a high-level application. 它會使用 Blink 範例,這是 Azure Sphere SDK 的一部分。It uses the Blink sample, which is part of the Azure Sphere SDK. Blink 範例會使 LED 閃爍,以便您可以確認 Azure Sphere 裝置和工具是否已正確安裝與設定。The Blink sample blinks an LED, so that you can verify that the Azure Sphere device and tools are installed and set up correctly.

必要條件Prerequisites

本快速入門中的步驟假設:The steps in this quickstart assume that:

  • 您的 Azure Sphere 裝置已連接到電腦Your Azure Sphere device is connected to your PC
  • 您已完成安裝 Azure Sphere 的所有步驟You have completed all the steps to install Azure Sphere

重要

這些指示假設您使用的是遵循 MT3620 參考面板設計 (RBD) 的硬體,例如 Seeed MT3620 開發套件。These instructions assume that you are using hardware that follows the MT3620 reference board design (RBD), such as the Seeed MT3620 Development Kit. 如果您使用的是不同的 Azure Sphere 硬體,請參閱製造商的說明文件,以了解是否已公開 GPIO 以及如何存取它。If you are using different Azure Sphere hardware, consult the manufacturer's documentation to find out whether the GPIO is exposed and how to access it. 您可能需要更新範例程式碼和 app_manifest.json 檔案的 "Gpio" 欄位以使用不同的 GPIO。You might need to update the sample code and the "Gpio" field of the app_manifest.json file to use a different GPIO.

準備您的裝置以便進行開發和偵錯Prepare your device for development and debugging

您必須先啟用開發和偵錯,才可以在 Azure Sphere 裝置上建置範例應用程式或開發新的應用程式。Before you can build a sample application on your Azure Sphere device or develop new applications for it, you must enable development and debugging. 根據預設,Azure Sphere 裝置「已鎖定」;也就是,不允許從電腦載入開發中的應用程式,而且不允許進行應用程式偵錯。By default, Azure Sphere devices are "locked"; that is, they do not allow applications under development to be loaded from a PC, and they do not allow debugging of applications. 準備裝置以便進行偵錯,會移除這項限制。Preparing the device for debugging removes this restriction.

azsphere device prep-debug 命令會將裝置設定為接受來自 PC 的應用程式進行偵錯,並將偵錯伺服器載入到裝置。The azsphere device prep-debug command configures the device to accept applications from a PC for debugging and loads the debugging server onto the device. 它也會將裝置指派到不允許無線 (OTA) 應用程式更新的 裝置群組It also assigns the device to a device group that does not allow over-the-air (OTA) application updates. 在應用程式開發和偵錯期間,您應該將裝置留在此群組中,這樣 OTA 應用程式更新就不會覆寫開發中的應用程式。During application development and debugging, you should leave the device in this group so that OTA application updates do not overwrite the application under development.

準備您的裝置To prep your device

  1. 確定 Azure Sphere 裝置已連接到您的電腦,而您的電腦已連線到網際網路。Make sure that your Azure Sphere device is connected to your PC, and your PC is connected to the internet.

  2. 在 Azure Sphere 開發人員命令提示字元視窗中,輸入下列命令:In an Azure Sphere Developer Command Prompt window, type the following command:

    azsphere device prep-debug
    

    您應該會看到如下所示的輸出:You should see output similar to the following:

    Getting device capability configuration for application development.
    Downloading device capability configuration for device ID '<device ID>'.
    Successfully downloaded device capability configuration.
    Successfully wrote device capability configuration file 'C:\Users\user\AppData\Local\Temp\tmpD732.tmp'.
    Setting device group ID 'a6df7013-c7c2-4764-8424-00cbacb431e5' for device with ID '<device ID>'.
    Successfully disabled over-the-air updates.
    Enabling application development capability on attached device.
    Applying device capability configuration to device.
    Successfully applied device capability configuration to device.
    The device is rebooting.
    Installing debugging server to device.
    Deploying 'C:\Program Files (x86)\Microsoft Azure Sphere SDK\DebugTools\gdbserver.imagepackage' to the attached device.
    Image package 'C:\Program Files (x86)\Microsoft Azure Sphere SDK\DebugTools\gdbserver.imagepackage' has been deployed to the attached device.
    Application development capability enabled.
    Successfully set up device '<device ID>' for application development, and disabled over-the-air updates.
    Command completed successfully in 00:00:38.3299276.
    
    

The device remains enabled for debugging and closed to OTA application updates until you explicitly change it. To disable debugging and allow application updates, use the azsphere device prep-field command.

Build and run the Blink sample

  1. Start Visual Studio and open the Azure Sphere Blink template. Enter a name and location for the project and select OK.

    The project opens with two windows in the editor: main.c and a Visual Studio overview page that has the same name as the project.

  2. In Visual Studio, select View>Output to display the Output window.

  3. Ensure that your board is connected to your PC by USB. Then select Remote GDB Debugger from the menu bar or press F5.

    Remote GDB Debugger button

  4. If you are prompted to build the project, select Yes. Visual Studio compiles the application, creates an image package, sideloads it onto the board, and starts it in debug mode. Sideloading means that the application is delivered directly from the PC over a wired connection, rather than delivered over the air (OTA) by Wi-Fi.

    提示

    Note the path in the Build output, which indicates the location of the output image package on your PC. You'll use the image package later in the Deployment Quickstart.

  5. By default, the Output window shows output from Device Output. To see messages from the debugger, select Debug from the Show output from: dropdown menu. You can also inspect the program disassembly, registers, or memory through the Debug>Windows menu.

  6. When you run the program, you should see an LED blink green.

Next steps

This quickstart showed how to prepare your Azure Sphere device for debugging and how to build and debug an application.

Next: