FC2カウンター FPGAの部屋 Vitis 2019.2 で自作カーネルを使用してストーミング接続を試す6(AXI4 StreamにFIFOを追加)
fc2ブログ

FPGAやCPLDの話題やFPGA用のツールの話題などです。 マニアックです。 日記も書きます。

FPGAの部屋

FPGAの部屋の有用と思われるコンテンツのまとめサイトを作りました。Xilinx ISEの初心者の方には、FPGAリテラシーおよびチュートリアルのページをお勧めいたします。

Vitis 2019.2 で自作カーネルを使用してストーミング接続を試す6(AXI4 StreamにFIFOを追加)

Vitis 2019.2 で自作カーネルを使用してストーミング接続を試す5(ChipScope で波形を確認する2)”の続き。

前回は、設定を行ってChipScope 波形を観察した結果、dma_write カーネルが動作していないようだということが分かった。今回は、同時に動作できるカーネルが 2 個である場合は、途中に画像全部以上の FIFO があれば、最初の dma_read カーネルが終了し、dma_write カーネルが起動できて、つまり、全てのカーネルが動作するのではないか?という仮説に基づいて krnl_lap_filter カーネルに 4096 深度の FIFO を追加してみよう。

最初に、今の krnl_lap_filter.cpp を合成した結果を示す。
streaming_lap_filter_40_200207.png

FIFO を入れた krnl_lap_filter2.cpp のソースコードを示す。

// krnl_lap_filter2.cpp
// 2020/02/07 by marsee

#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include <stdint.h>

// RGBからYへの変換
// RGBのフォーマットは、{8'd0, R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y =  0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int32_t conv_rgb2y(int32_t rgb){
    int32_t r, g, b, y_f;
    int32_t y;

    b = rgb & 0xff;
    g = (rgb>>8) & 0xff;
    r = (rgb>>16) & 0xff;

    y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
    y = y_f >> 8; // 256で割る

    return(y);
}

// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1  8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int32_t laplacian_fil(int32_t x0y0, int32_t x1y0, int32_t x2y0, int32_t x0y1,
        int32_t x1y1, int32_t x2y1, int32_t x0y2, int32_t x1y2, int32_t x2y2)
{
    int32_t y;

    y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
    if (y<0)
        y = -y;
    else if (y>255)
        y = 255;
    return(y);
}

extern "C" {
void krnl_lap_filter(hls::stream<ap_axiu<32,0,0,0> >& ins, hls::stream<ap_axiu<32,0,0,0> >& outs,
        int32_t x_size, int32_t y_size){
#pragma HLS DATAFLOW
#pragma HLS INTERFACE s_axilite port=y_size bundle=control
#pragma HLS INTERFACE s_axilite port=x_size bundle=control
#pragma HLS INTERFACE axis register both port=outs
#pragma HLS INTERFACE axis register both port=ins
#pragma HLS INTERFACE s_axilite port=return bundle=control

    hls::stream<ap_axiu<32,0,0,0> > buf;
#pragma HLS STREAM variable=buf depth=4096 dim=1

    ap_axiu<32,0,0,0> pix;
    ap_axiu<32,0,0,0> lap;

    int32_t line_buf[2][1920]; // supported HD resolution
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
#pragma HLS resource variable=line_buf core=RAM_2P

    int32_t pix_mat[3][3];
#pragma HLS array_partition variable=pix_mat complete

    int32_t lap_fil_val;

    LOOP_X1 : for (int y=0; y<y_size; y++){
#pragma HLS LOOP_TRIPCOUNT min=48 max=600
        LOOP_Y1 : for (int x=0; x<x_size; x++){
#pragma HLS LOOP_TRIPCOUNT min=64 max=800
#pragma HLS PIPELINE II=1
            ins >> pix;
            buf << pix;
        }
    }

    LOOP_X2 : for (int y=0; y<y_size; y++){
#pragma HLS LOOP_TRIPCOUNT min=48 max=600
        LOOP_Y2 : for (int x=0; x<x_size; x++){
#pragma HLS LOOP_TRIPCOUNT min=64 max=800
#pragma HLS PIPELINE II=1
            buf >> pix; // AXI4-Stream からの入力

            Loop4 : for (int k=0; k<3; k++){
                Loop5 : for (int m=0; m<2; m++){
#pragma HLS UNROLL
                    pix_mat[k][m] = pix_mat[k][m+1];
                }
            }
            pix_mat[0][2] = line_buf[0][x];
            pix_mat[1][2] = line_buf[1][x];

            int32_t y_val = conv_rgb2y(pix.data);
            pix_mat[2][2] = y_val;

            line_buf[0][x] = line_buf[1][x];    // 行の入れ替え
            line_buf[1][x] = y_val;

            lap_fil_val = laplacian_fil(    pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
                                            pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
                                            pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
            lap.data = (lap_fil_val<<16)+(lap_fil_val<<8)+lap_fil_val; // RGB同じ値を入れる

            if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
                lap.data = 0;

            if (x==(x_size-1) && y==(y_size-1)) // フレームの最後で TLAST をアサートする
                lap.last = 1;
            else
                lap.last = 0;

            outs << lap;    // ストリームへ出力
        }
    }

    /*LOOP_WAIT_LAST: while(pix.last == 0) { // last が 1 になるまで待つ
#pragma HLS PIPELINE II=1
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
        ins >> pix;
    }; */
}
}


これを C コードの合成した結果を示す。
streaming_lap_filter_41_200207.png

BlockRAM が 2 個から 13 個に増えている。

Vitis 2019.2 の GUI で streaming_lap_filter2 を作成して、krnl_lap_filter.cpp だけを作成した krnl_lap_filter2.cpp のコードに入れ替えてビルドした。ビルドできた。
streaming_lap_filter_42_200208.png

Ultra96-V2 を起動して、zcol ドライバをロードlした。

Vitis GUI で Run Configuration を作って、ホスト・アプリケーションを起動したが、同様に途中で止まってしまった。
streaming_lap_filter_43_200208.png

波形を示す。
streaming_lap_filter_44_200208.png

これでも動作しないとすると、違う原因なのか?
カーネルを 2 個にまとめて、やってみよう。
  1. 2020年02月08日 04:28 |
  2. Vitis
  3. | トラックバック:0
  4. | コメント:0

コメント

コメントの投稿


管理者にだけ表示を許可する

トラックバック URL
https://marsee101.blog.fc2.com/tb.php/4787-fb63225e
この記事にトラックバックする(FC2ブログユーザー)