勉強しないとな~blog

ちゃんと勉強せねば…な電気設計エンジニアです。

ZYBOを進める - 10. Xilinx SDKで試しのプログラム2

前回ARMコア上でサンプルプログラムを動かしてみましたが、 自分で一度手を加えてみたいと思います。

BSP確認

Xilinx SDK上でサンプルプロジェクトを作成した際、Board Support Package (BSP)も同時にzybo_test_bspという名前で作成されました。
BSPの中には、Vivado上で作成したハードウェア構成に合わせたドライバが作成されます。
これを開いてみると、"ps_cortexa9_0"とあるので、これも開きます。

f:id:nokixa:20190630010018p:plain

この中には、以下のような内容があります。

  • code
    よく分かりません。中身は空っぽになっています。
  • include
    一番見る必要のあるところです。 PS部のペリフェラルやPL部に接続したモジュールのドライバのヘッダファイルがいろいろ入っています。
    ここを見れば、UARTやGPIO等が扱えます。
  • lib
    ライブラリファイルがここに生成されます。特に見る必要はありません。
  • libsrc
    ドライバのソースコードがここに作成されます。ドライバ関数の実装の詳細を見たいときに見ることがあります。

"include"をさらに開いてみると、色々と入っていますが、今回はこれを見てみます。

f:id:nokixa:20190630124732p:plain

  • xparameters.h
    PS部ペリフェラルとPL部モジュールのアドレスマッピングが記述されています。
  • xgpiops.h
    PS部GPIOのドライバのヘッダファイルです。PS部のGPIOにLEDが接続されていたので、これを使ってLEDのオンオフをします。

サンプルプログラムの変更

サンプルプログラムのhelloworld.cを以下のように変更します。
/*************************************************/ で挟んだ行が追加した点です。

これも参考にしました。↓
embeddedsw/xgpiops_polled_example.c at master · Xilinx/embeddedsw · GitHub

/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"

/*************************************************/
// PS部GPIOのドライバヘッダをインクルード
#include "xgpiops.h"
/*************************************************/

int main()
{
/*************************************************/
    // PS部GPIOの設定の構造体
    XGpioPs_Config *ConfigPtr;
    // PS部GPIOのインスタンス
    XGpioPs Gpio;
/*************************************************/

    init_platform();

/*************************************************/
    // 対象のGPIOの設定を参照 (DEVICE IDはxparameters.hに記載されている)
    ConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
    // インスタンスを初期化
    XGpioPs_CfgInitialize(&Gpio, ConfigPtr, ConfigPtr->BaseAddr);
/*************************************************/

    print("Hello World\n\r");

/*************************************************/
    // MIO7を出力ポートに設定
    XGpioPs_SetDirectionPin(&Gpio, 7, 1);
    // MIO7の出力イネーブル
    XGpioPs_SetOutputEnablePin(&Gpio, 7, 1);

    // LED点滅処理
    u32 CurrentLED = 0;
    while(1){
        // LED点滅間隔待ち
        for(int i = 0; i < 10000000; i++){;}

        // LED点灯状態反転
        XGpioPs_WritePin(&Gpio, 7, (CurrentLED ^= 1));
    }
/*************************************************/

    cleanup_platform();
    return 0;
}

PS部ペリフェラルXilinx製IPでは、他のものでも似たような形式のドライバが用意されています。

ソースコードの変更後、前回と同じ手順でZYBO上で動かすと、MIO7のLEDが点滅します。

f:id:nokixa:20190630144018g:plain

まとめ

ZynqのPS部のコードを自分で書いて動かすことができました。

またまた間が空いてしまいましたが、
次からやっとLinuxの導入を進めていきたいと思います。