LPC1114+CMSIS Libraryでprintfを使ってみた
LPC11xx_cmsis2_Libに実装されているUARTSend関数を使ってシリアル出力していたが
文字列以外の表示が面倒なのでprintfを使えるようにした。
今の所2種類の方法で動いたので両方メモしておく。
試した環境
- LPCXpresso_7.7.2_379
- LPCXpresso1114_cmsis2.zipをインポート済み
C:\nxp\LPCXpresso_7.7.2_379\lpcxpresso\Examples\NXP\LPC1000\LPC11xxにある - Redlib(none)
- LPC-Link2(Semihostingを使う際は必要)
Semihostingを使う
LPC-Link2があるならSemihostingを使うとLPCXpresso上でprintfの結果が見える。
使い方も簡単でNew project
でC Project (Semihosted)
を選ぶだけ。
出てきたサンプルコードを実行するとLPCXpresso上のConsoleにHello Worldが表示される。
syscallを実装してUART経由でprintfを使う
New project
->C Project
で作成したプロジェクト上でprintfを呼び出すと
Redlib(none)ではSystemcallが実装されていないのでリンク時エラーが発生する。
c:/nxp/lpcxpresso_7.7.2_379/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv6-m\libcr_c.a(fpprintf.o): In function `printf': fpprintf.c:(.text.printf+0x38): undefined reference to `__sys_write' c:/nxp/lpcxpresso_7.7.2_379/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv6-m\libcr_c.a(_deferredlazyseek.o): In function `__flsbuf': _deferredlazyseek.c:(.text.__flsbuf+0x88): undefined reference to `__sys_istty' c:/nxp/lpcxpresso_7.7.2_379/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv6-m\libcr_c.a(_writebuf.o): In function `_Cwritebuf': _writebuf.c:(.text._Cwritebuf+0x16): undefined reference to `__sys_flen' _writebuf.c:(.text._Cwritebuf+0x26): undefined reference to `__sys_seek' _writebuf.c:(.text._Cwritebuf+0x3a): undefined reference to `__sys_write' c:/nxp/lpcxpresso_7.7.2_379/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv6-m\libcr_c.a(alloc.o): In function `_Csys_alloc': alloc.c:(.text._Csys_alloc+0xe): undefined reference to `__sys_write' alloc.c:(.text._Csys_alloc+0x12): undefined reference to `__sys_appexit' c:/nxp/lpcxpresso_7.7.2_379/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv6-m\libcr_c.a(fseek.o): In function `fseek': fseek.c:(.text.fseek+0x18): undefined reference to `__sys_istty' fseek.c:(.text.fseek+0x3c): undefined reference to `__sys_flen' collect2.exe: error: ld returned 1 exit status
Systemcallの定義はlibconfig-arm.hに記述されているので適当に実装してみた。
以下サンプルコード
#ifdef __USE_CMSIS #include "LPC11xx.h" #endif #include <stdlib.h> #include <stdio.h> #include <string.h> #include "uart.h" #include <cr_section_macros.h> // TODO: insert other include files here // TODO: insert other definitions and declarations here void uart_putc(const char); ////////////////////////// Systemcall ////////////////////////// int __sys_write(int iFileHandle, char *pcBuffer, int iLength) { int n; for (n = 0; n < iLength; n++) { if (pcBuffer[n] == '\n'){ uart_putc('\r'); } uart_putc(pcBuffer[n]); } return iLength; } int __sys_istty(int handle) { return 1; } int __sys_flen(int handle) { return 0; } int __sys_seek(int handle, int pos) { return 0; } void __sys_appexit (void) { } ////////////////////////// UART //////////////////////// // send a character via TXD void uart_putc(const char c) { // Wait for TX buffer empty while (!(LPC_UART->LSR & LSR_THRE)); // Put a character LPC_UART->THR = c; } ////////////////////////// Main ////////////////////////// int main(void) { UARTInit(115200); // Force the counter to be placed into memory volatile static int i = 0 ; // Enter an infinite loop, just incrementing a counter while(1) { i++ ; if (i % 1000000 == 0){ printf("i = %d(dec) ",i); printf("%08x(hex)\n",i); } } return 0 ; }
uart_putc関数はエレキジャックのHPにあったコード*1を
そのまま使わせて頂きました。
実行するとUARTからprintfの結果が送られてくる。
i = 1000000(dec) 000f4240(hex) i = 2000000(dec) 001e8480(hex) i = 3000000(dec) 002dc6c0(hex) i = 4000000(dec) 003d0900(hex)
scanf
printfが動作したので、ついでにscanfも実装しようとしたがうまく動かなかった。
__sys_read
と__sys_read_c
を追加で実装したが
デバック実行でブレイクポイントを設定しても止まらなかった。
Redlib内の動きが分かれば良いのだが
ソースコードは公開されていない為scanfの実装はあきらめる。
参考にしたサイト
CypressでEZ-USB FX3 のキットを買ってみた
ZedboardとPC間で高速大容量通信の実験をしようと思いUSB3.0のキットを買ったので購入方法などのメモ
買ったのはCypressの「EZ-USB FX3 SuperSpeed Explorer Kit」$49と安価なのでこれにした。
CYUSB3KIT - 003 EZ - USB® FX3™ SuperSpeed Explorer Kit - Cypress
ただし、このキットはピンヘッダで外部と接続する為そのままではZedboardには接続できない。
CypressでFMCやHSMCに変換するボードが別途販売しているのでそれも買う必要がある。
CYUSB3ACC - 006 HSMC Interconnect Board for the EZ - USB® FX3™ SuperSpeed Explorer Kit - Cypress
CYUSB3ACC - 005 FMC Interconnect Board for the EZ - USB® FX3™ SuperSpeed Explorer Kit - Cypress
今回はAlteraSocでも試せる様に両方入手した。
Explorer KitはDigikeyで販売しているが、FPGAに接続するボードは見つからなかったのですべてCyptessから直接購入した。
Cypressで購入するにはそのままカートに追加していけば良い。
購入にはユーザーアカウントが必要だが、ZedBoardでUARTを使う為Cypressでドライバをダウンロード
した際に作成していたのでそのアカウントを使用した。
送料は$29か$39を選べ$39だと早く到着する様です。今回は$39にしてみた。
支払いはクレジットカードが使えたので簡単に決済できた。
注文を12/1のお昼頃にして、12/5帰宅するとFedExの不在票が入っていて明日、日通にて再配達すると書いてあり12/6に受け取った。
Explorer Kitの箱
ZedboardにFMEボードを介して接続した写真
Simple framebufferを有効にする
Linux上からディスプレイに出力する方法を調べているとSimple framebufferという物がある様なのでqemu上で試したメモ。
qemuは前回のqemu2svの環境を流用。
qemu側の変更点
sysbus_create_simple("hw_if", 0xE1000000, NULL);
kernel側の変更点
make menuconfig
を実行
Simple framebuffer supportを有効にする。
Device Drivers→Graphics support→Support for frame buffer deviceと選択する。
device treeに以下を追加
ps7_fb: framebuffer@E1000000 { compatible = "simple-framebuffer"; reg = <0xE1000000 (800 * 600 * 2)>; width = <800>; height = <600>; stride = <(800 * 2)>; format = "r5g6b5"; };
ひな形は↓にあった。
Documentation/devicetree/bindings/video/simple-framebuffer.txt
qemu起動
起動後にdmesg
を実行するとログ内でsimple framebufferが認識しているのがわかる。
simple-framebuffer e1000000.framebuffer: framebuffer at 0xe1000000, 0xea600 bytes, mapped to 0xf010000 0 simple-framebuffer e1000000.framebuffer: format=r5g6b5, mode=800x600x16, linelength=1600
また、/dev/fb0
が追加されている。
もうちょっと確認する
hw_if.cを修正する。
hw/char/hw_if.c
static void hw_if_write(void *opaque, hwaddr offset,uint64_t value, unsigned size){ if (value != 0) { printf("write::%lx %lx %x\n",offset >> 2,value,size); } }
value != 0
がないと黒のドットがすべてprintされるので0以外を出力している。
この状態でecho -ne "a" > /dev/fb0
とやってみた。
zynq> echo -ne "a" > /dev/fb0 write::0 61 1
0x61はASCIIコードで"a"なのでライトできてる様です。
実機に実装する際はデュアルポートメモリを置いて片方は0xE1000000にバスからライトして片方はVGAなりHDMIなりに同期信号付けて渡せば動作しそう。
リフレッシュレートの設定が見つからないけどLinuxは同期をどうしてるんだろう?書けるだけ書いているのかな?
qemu2svを試してみた-3-
qemu2svを試してみた-2- - 日曜技術者のメモの続き
プログラムを書いてWrite/Readテストをした
qemu⇔svの通信テスト
通信プログラムの作成
qemuからsvへアクセスする為のプログラムを作成した。
sv側にライトしてからリードするだけのプログラム。
- main.c
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/mman.h> #include <unistd.h> #define PAGE_SIZE (4*1024) #define BLOCK_SIZE (4*1024) int main(void){ int mem_fd; void *hw_if_map; volatile unsigned int* phy_pt; unsigned int ret; int i; if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { printf("can't open /dev/mem \n"); return 0; } hw_if_map = mmap( NULL, //Any adddress in our space will do BLOCK_SIZE, //Map length PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory MAP_SHARED, //Shared with other processes mem_fd, //File to map 0xe0004000 //Offset to GPIO peripheral ); close(mem_fd); phy_pt = hw_if_map; for (i = 0 ;i < 0x100 ; i = i + 4){ printf("CPU Write Address = 0x%x data = 0x%x\n",0xe0004000+i,i + 0x10 ); *(phy_pt+i) = (0x10 + i); printf("CPU Read Address = 0x%x\n",0xe0004000+i); ret = *(phy_pt+i); printf("CPU Read Data = 0x%x\n",ret); if (ret != (0x10 + i)){ printf("Error::not compare\n"); return 0; } } return 1; }
ビルド
XilinxSDKの中にあるarm-xilinx-linux-gnueabi-gcc
を使ってビルドした。
/opt/Xilinx/SDK/2014.2/gnu/arm/lin/bin/arm-xilinx-linux-gnueabi-gcc -o ./main main.
QEMU Linuxに転送
QEMU上ではFTPサーバーが動作しているのでFTPを使って送付する。
ビルドしたmain
と同じフォルダに以下テキストを作成。
- ftp.txt
open localhost 10021 user root root passive put main close
作成後ftp -n < ftp.txt
を実行する事でQEMU Linuxのルートディレクトリにファイルが送られる。
動作確認。
QEMU Linux上にmainがある事が確認できたらchmod +x main
で実行権限を与えておく。
- QEMU側
zynq> ./main CPU Write Address = 0xe0004000 data = 0x10 CPU Read Address = 0xe0004000 CPU Read Data = 0x10 CPU Write Address = 0xe0004004 data = 0x14 CPU Read Address = 0xe0004004 CPU Read Data = 0x14 ・・・ CPU Write Address = 0xe00040f8 data = 0x108 CPU Read Address = 0xe00040f8 CPU Read Data = 0x108 CPU Write Address = 0xe00040fc data = 0x10c CPU Read Address = 0xe00040fc CPU Read Data = 0x10c zynq>
- sv側
# Start server connect... # sv::write::rw_packet.offset = 0x00000000 rw_packet.data = 0x00000010 # # sv::read::rw_packet.offset = 0x00000000 rw_packet.data = 0x00000010 # # sv::write::rw_packet.offset = 0x00000010 rw_packet.data = 0x00000014 # # sv::read::rw_packet.offset = 0x00000010 rw_packet.data = 0x00000014 # # sv::write::rw_packet.offset = 0x00000020 rw_packet.data = 0x00000018 # # sv::read::rw_packet.offset = 0x00000020 rw_packet.data = 0x00000018 ・・・ # sv::write::rw_packet.offset = 0x000003e0 rw_packet.data = 0x00000108 # # sv::read::rw_packet.offset = 0x000003e0 rw_packet.data = 0x00000108 # # sv::write::rw_packet.offset = 0x000003f0 rw_packet.data = 0x0000010c # # sv::read::rw_packet.offset = 0x000003f0 rw_packet.data = 0x0000010c #
ちゃんとアクセスしている様に見えるがアドレスが違うと思い調べた結果、sv側のoffsetを2ビット右シフトする事でアドレスが一致しました。
QEMUを終了するにはQEMUのターミナル上でCtrl+A
を入力した後x
を入力すると終了します。
(その際sv側も勝手に終了する。)
終了する際に気になったのはQEMU側を終了際にsv側にアクセスが発生する様です。 以下終了時のログには0x00000000へ2回リードが発生している。
# sv::read::rw_packet.offset = 0x000003f0 rw_packet.data = 0x0000010c # # sv::read::rw_packet.offset = 0x00000000 rw_packet.data = 0x00000010 # # sv::read::rw_packet.offset = 0x00000000 rw_packet.data = 0x00000010 # # SV::connection faild # # ** Note: $finish : mem_model.sv(47) # Time: 95 ps Iteration: 1 Instance: /mem_model [ginnyu-tei@localhost svdpi_test]$
そこそこ簡単にQEMUとSVが通信できました。
後シミュレーション時間が同期できればおもしろいかなー
qemu2svを試してみた-2-
qemu2svを試してみた-1- - 日曜技術者のメモの続き
Device Treeの準備と動作確認
Linux
Device Tree
Linuxカーネルに付属しているDevice Treeを使うとqemu上でブートはするがネットワークが繋がらなかった。
色々試した結果zynq-zed.dts
を修正する事で接続できた。
- zynq-zed.dtsを以下の様に修正
chosen { /*bootargs = "console=ttyPS0,115200 root=/dev/ram rw earlyprintk";*/ bootargs = "console=ttyPS0,115200 root=/dev/ram rw earlyprintk ip=:::::eth0:dhcp"; linux,stdout-path = "/amba@0/serial@e0001000"; } ;
修正後dtsからdtbを生成する。
zynq_linux/linux-xlnx/scripts/dtc/dtc -I dts -O dtb -o zynq-zed.dtb zynq-zed.dts
QEMU
スタートスクリプト
- start_qemu.sh
#!/bin/sh qemu-system-arm -M xilinx-zynq-a9 -m 1024 -serial null -serial mon:stdio \ -dtb ./zynq-zed.dts -smp 1 -nographic \ -kernel ./zImage \ -initrd filesystem/ramdisk.img \ -net nic,model=cadence_gem \ -net user \ -tftp ~/ \ -redir tcp:10023::23 -redir tcp:10080::80 -redir tcp:10022::22 -redir tcp:10021::21 -redir tcp:1234::1234
A9 MPコアなので-smp 2
だと思うのだが、そうするとQEMUがエラーになったので-smp 1
にしている。
-redir
のオプションはポートフォワーディングで<tcp or udp> : <ホスト側ポート> :: <QEMU上OSのポート>
となっている。
-initrd
でしていしているramdisk.imgはhttp://www.xilinx.com/member/zynq_linux/zynq_linux.tar.gzの中にあるファイルをそのまま
使っている。
動作テスト
OSブート
ターミナルを二つ用意し、一つのターミナルで`./start_qemu.sh
を実行後もう一つのターミナルでsv側のmake
を実行するとLinuxがブートする。
以下にログを貼っておく。
- qemu側
[ginnyu-tei@localhost zynq_linux]$ ./start_qemu.sh [hw_if] Socket is listening for connections... Please start the rtl simulation. Timeout after 15 sec Warning: nic cadence_gem.1 has no peer console [ttyPS0] enabled console [ttyPS0] enabled bootconsole [earlycon0] disabled bootconsole [earlycon0] disabled xdevcfg f8007000.ps7-dev-cfg: ioremap 0xf8007000 to f0066000 [drm] Initialized drm 1.1.0 20060810 brd: module loaded loop: module loaded m25p80 spi0.0: SPI transfer timed out m25p80: probe of spi0.0 failed with error -110 e1000e: Intel(R) PRO/1000 Network Driver - 2.3.2-k e1000e: Copyright(c) 1999 - 2013 Intel Corporation. libphy: XEMACPS mii bus: probed xemacps e000b000.ps7-ethernet: invalid address, use assigned xemacps e000b000.ps7-ethernet: MAC updated ea:05:dc:07:2d:7b xemacps e000b000.ps7-ethernet: pdev->id -1, baseaddr 0xe000b000, irq 54 ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver ehci-pci: EHCI PCI platform driver zynq-dr e0002000.ps7-usb: Unable to init USB phy, missing? usbcore: registered new interface driver usb-storage mousedev: PS/2 mouse device common for all mice i2c /dev entries driver zynq-edac f8006000.ps7-ddrc: ecc not enabled cpufreq_cpu0: failed to get cpu0 regulator: -19 Xilinx Zynq CpuIdle Driver started sdhci: Secure Digital Host Controller Interface driver [ginnyu-tei@localhost zynq_linux]$ ./start_qemu.sh [hw_if] Socket is listening for connections... Please start the rtl simulation. Timeout after 15 sec Warning: nic cadence_gem.1 has no peer console [ttyPS0] enabled console [ttyPS0] enabled bootconsole [earlycon0] disabled bootconsole [earlycon0] disabled xdevcfg f8007000.ps7-dev-cfg: ioremap 0xf8007000 to f0066000 [drm] Initialized drm 1.1.0 20060810 brd: module loaded loop: module loaded m25p80 spi0.0: SPI transfer timed out m25p80: probe of spi0.0 failed with error -110 e1000e: Intel(R) PRO/1000 Network Driver - 2.3.2-k e1000e: Copyright(c) 1999 - 2013 Intel Corporation. libphy: XEMACPS mii bus: probed xemacps e000b000.ps7-ethernet: invalid address, use assigned xemacps e000b000.ps7-ethernet: MAC updated ae:93:fe:cd:f7:8a xemacps e000b000.ps7-ethernet: pdev->id -1, baseaddr 0xe000b000, irq 54 ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver ehci-pci: EHCI PCI platform driver zynq-dr e0002000.ps7-usb: Unable to init USB phy, missing? usbcore: registered new interface driver usb-storage mousedev: PS/2 mouse device common for all mice i2c /dev entries driver zynq-edac f8006000.ps7-ddrc: ecc not enabled cpufreq_cpu0: failed to get cpu0 regulator: -19 Xilinx Zynq CpuIdle Driver started sdhci: Secure Digital Host Controller Interface driver sdhci: Copyright(c) Pierre Ossman sdhci-pltfm: SDHCI platform and OF driver helper mmc0: no vqmmc regulator found mmc0: no vmmc regulator found mmc0: SDHCI controller on e0100000.ps7-sdio [e0100000.ps7-sdio] using ADMA usbcore: registered new interface driver usbhid usbhid: USB HID core driver TCP: cubic registered NET: Registered protocol family 17 zynq_pm_remap_ocm: OCM pool is not available zynq_pm_late_init: Unable to map OCM. Registering SWP/SWPB emulation handler regulator-dummy: disabling drivers/rtc/hctosys.c: unable to open rtc device (rtc0) Sending DHCP requests ., OK IP-Config: Got DHCP answer from 10.0.2.2, my address is 10.0.2.15 IP-Config: Complete: device=eth0, hwaddr=ae:93:fe:cd:f7:8a, ipaddr=10.0.2.15, mask=255.255.255.0, gw=10.0.2.2 host=10.0.2.15, domain=, nis-domain=(none) bootserver=10.0.2.2, rootserver=10.0.2.2, rootpath= nameserver0=10.0.2.3 ALSA device list: No soundcards found. RAMDISK: ext2 filesystem found at block 0 RAMDISK: Loading 8192KiB [1 disk] into ram disk... done. VFS: Mounted root (ext2 filesystem) on device 1:0. devtmpfs: mounted Freeing unused kernel memory: 184K (c05f9000 - c0627000) Starting rcS... ++ Mounting filesystem ++ Setting up mdev ++ Starting telnet daemon ++ Starting http daemon ++ Starting ftp daemon ++ Starting dropbear (ssh) daemon random: dropbear urandom read with 0 bits of entropy available xemacps e000b000.ps7-ethernet: Set clk to 123809522 Hz xemacps e000b000.ps7-ethernet: link up (1000/FULL) rcS Complete zynq>
- sv側
[ginnyu-tei@localhost svdpi_test]$ make vlib work vlog -sv -dpiheader dpiheader.h mem_model.sv Model Technology ModelSim ALTERA vlog 10.1d Compiler 2012.11 Nov 2 2012 -- Compiling package mem_model_sv_unit -- Compiling module mem_model Top level modules: mem_model gcc -c -g -fpic -m32 -I/mnt/sdb1/altera/13.1/modelsim_ase//include mem_model.c gcc -O0 -shared -m32 mem_model.o -o svdpi.so vsim -c -sv_lib svdpi mem_model -do "run -all; quit " Reading /mnt/sdb1/altera/13.1/modelsim_ase/tcl/vsim/pref.tcl # 10.1d # vsim -do {run -all; quit } -c -sv_lib svdpi mem_model # Loading /var/tmp/ginnyu-tei@localhost.localdomain_dpi_13953/linuxpe_gcc-4.5.0/export_tramp.so # Loading sv_std.std # Loading work.mem_model_sv_unit # Loading work.mem_model # Compiling /var/tmp/ginnyu-tei@localhost.localdomain_dpi_13953/linuxpe_gcc-4.5.0/exportwrapper.c # Loading /var/tmp/ginnyu-tei@localhost.localdomain_dpi_13953/linuxpe_gcc-4.5.0/dpi_auto_compile.so # Loading ./svdpi.so # run -all # Start server connect...
また長くなったので次回
qemu2svを試してみた-1-
qemuとSystemVerilogを接続してシミュレーションをするqemu2svがgithubにて公開されていたので試してみた
qemu2sv
qemu2svの入手&ビルド
git clone https://github.com/SVHackathon2014B/qemu2sv.git
qemu2svのビルドは付属しているREADME.mdの通りやればビルドできた。
ただ、qemuをビルドする為にいくつかのパッケージをインストールした。
sudo yum install pixman-devel sudo yum install flex bison sudo yum install glib
Linux
ソースコードの入手
zynq qemu用のLinuxはxilinxが公開している。
http://www.xilinx.com/member/zynq_linux/zynq_linux.tar.gz *1
Xilinx Wiki - QEMUに書いてある手順で実行したがブートしなかったのでカーネルをビルドした。
zynq用のLinuxはgithubにあるのでclone
Windowsでcloneする場合
cloneする際に改行コードをCRLFに変える設定があり有効であればlinuxをビルドできない
(defconfigでエラーになる)
git config --list | grep autocrlf
を実行してtrueであれば
git config --global core.autocrlf input
を実行する。
最新のコードだと動かなかったのでタグを指定して古いバージョンにする。
(xilinx-v2014.2.01で動作を確認)
checkoutで良いがソースコードを別PCでビルドしたかったのでarchiveにした。
git archive --format tar --prefix=export/ xilinx-v2014.2.01 | gzip > export.tar.gz
ビルド
export.tar.gzを解凍後ビルドする。
gccはxilinxSDKに付属しているarm-xilinx-linux-gnueabi-gccを使った。
make ARCH=arm xilinx_zynq_defconfig export CROSS_COMPILE=/opt/Xilinx/SDK/2014.2/gnu/arm/lin/bin/arm-xilinx-linux-gnueabi- make ARCH=arm
ビルド後以下ファイルを使う。
- arch/arm/boot/dts/zynq-zed.dts
- arch/arm/boot/zImage
続きは次回
*1:
ブログ書いてる途中で見つけましたがxilinxのフォーラムに新しいQEMU用Linuxが公開されている様です。
(これで動くかは試してません)
Getting started with Zynq 14.5 and QEMU - Xilinx User Community Forums