-
-
Save hasegaw/9f677da6cfaf631245757072efffa558 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# VDMA Test\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"モジュールのロード" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import pynq.drivers.xlnk\n", | |
"from pynq.mmio import MMIO\n", | |
"\n", | |
"import numpy as np\n", | |
"import cv2\n", | |
"from IPython.display import Image" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"連続した物理メモリを確保するためのアロケータを準備" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<pynq.drivers.xlnk.xlnk object at 0x2bc27d10>\n" | |
] | |
} | |
], | |
"source": [ | |
"mmu = pynq.drivers.xlnk.xlnk()\n", | |
"print(mmu)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1280x720x3 をアロケート\n", | |
"確保したメモリの仮想アドレス、物理アドレスを表示" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"buf = mmu.cma_alloc(1280*720*3)\n", | |
"buf_phy = pynq.drivers.xlnk.libxlnk.cma_get_phy_addr(buf)\n", | |
"print(\"virtal\", buf, \"physical\", hex(buf_phy))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"VDMA IPのレジスタに対するMMIOを得る" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<pynq.mmio.MMIO object at 0x2bc27bb0>\n" | |
] | |
} | |
], | |
"source": [ | |
"m = MMIO(0x43000000, 0xFF)\n", | |
"print(m)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"VDMA IPのレジスタ設定" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"m.write(0x30, 0x8b) # Init VDMA\n", | |
"m.write(0xAC, buf_phy) # buf LSB\n", | |
"m.write(0xA8, 1280 * 3) # stride\n", | |
"m.write(0xA4, 1280 * 3) # bytes per line" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"画像の縦の長さを書き込んだ瞬間に DMA がはじまる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"m.write(0xA0, 720)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"バッファの先頭を表示。何も書き込まれていなければ多分 00,00,00, ..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"b'\\x19\\x19\\x1b\\x17\\x18\\x19\\x19\\x1a\\x1c\\x18\\x19\\x19\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x14\\x18\\x19\\x1a\\x19\\x19\\x1c\\x19\\x19\\x1d\\x18\\x19\\x19\\x18\\x19\\x19\\x18\\x19\\x19\\x18\\x19\\x19\\x19\\x19\\x19\\x19'" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mmu.cma_get_buffer(buf,64)[:]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"画像を uint8 のアレイと見なして目的解像度・震度で reshape し OpenCV で扱えるフォーマットに変換\n", | |
"表示してみる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"img = np.frombuffer(mmu.cma_get_buffer(buf,1280*720*3), dtype=np.uint8).reshape((720, 1280, 3))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"cv2.imwrite('/tmp/png.png', img)\n", | |
"Image(filename='/tmp/png.png')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"img" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment