2016年11月22日 星期二

如何安裝 Fasteners workbench

Fasteners workbench 是 FreeCAD 的一個外掛功能,其目的是繪製各種工程規格的螺絲及螺帽。截至 FreeCAD 0.16 版為止,Fasteners workbench 並沒有內建在 FreeCAD 中,必須自行下載安裝,因此我們將介紹如何下載、安裝 Fasteners workbench 以供 繪製螺絲。首先請至 Fasteners workbench 的 Git Hub 網頁 https://github.com/shaise/FreeCAD_FastenersWB

點選 Clone or Download 然後再點選 Download ZIP,此時會下載一個名為 FreeCAD_FastenersWB-master.zip 的檔案在 ~/Downloads 目錄下。

接著可以連至其官網看如何安裝,官網位置是:
http://theseger.com/projects/2015/06/fasteners-workbench-for-freecad/

以 Linux 來說,其安裝流程如下:

mkdir -p ~/.FreeCAD/Mod
cd ~/.FreeCAD/Mod
unzip ~/Downloads/FreeCAD_FastenersWB-master.zip

此時會建立一個 ~/.FreeCAD/Mod/FreeCAD_FastenersWB-master 目錄,我們把 FreeCAD 關掉重開,會出現一行訊息 Fasteners workbench Loaded 如下:


$freecad
FreeCAD 0.16, Libs: 0.16R5480 (Git)
© Juergen Riegel, Werner Mayer, Yorik van Havre 2001-2015
  #####                 ####  ###   ####
  #                    #      # #   #   #
  #     ##  #### ####  #     #   #  #   #
  ####  # # #  # #  #  #     #####  #   #
  #     #   #### ####  #    #     # #   #
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

Fasteners workbench Loaded

接下來就可以在 workbench 選單中看到 Fasteners 了。

2016年11月20日 星期日

如何準備 sd.img (2016 版)

如何準備 sd.img (2016 版)

先前我們所準備的 sd.img 已經接近可以開機的階段,現在只要將開機核心及 bootloader
放至 sd.img 的第一個分割區即可進行開機。底下是 sd.img 的分割區及內容:

sd.img +- part1-+
       |        + MLO         -> 不是每塊 ARM 版都有,主要適用於 TI OMAP 系列
       |        + u-boot      -> 在編譯完之 u-boot-VERSION 目錄下
       |        + u-boot.bin  -> 在編譯完之 u-boot-VERSION 目錄下
       |        + uImage      -> 在編譯完之 linux-VERSION/arch/arm/boot 目錄下
       +- part2-+
                + roofs       -> 先前以 debootstrap 作出來的內容

把上述內容準備好之後,我們可以執行 runsd.sh,其內容如下:

MACHINE=vexpress-a9
SD_IMAGE=sd.img
MEMORY=256

qemu-system-arm -machine $MACHINE -m $MEMORY \
  -nographic \
  -drive file=./$SD_IMAGE,if=sd,format=raw,index=0 \
  -kernel ./bootloader/u-boot-2016.09/u-boot \

然後將 sd.img 完全解除掛載,再執行 sh runsd.sh,我們可以先放著給它跑,最終會當掉,或是在倒數計時階段按 Enter 暫停下來。暫停後會出現 u-boot 的提示符號,如底下所示:

Hit any key to stop autoboot:  0
=> 

請執行 bdinfo 如底下所示:

=> bdinfo
arch_number = 0x000008E0
boot_params = 0x60002000
DRAM bank   = 0x00000000
-> start    = 0x60000000
-> size     = 0x10000000
DRAM bank   = 0x00000001
-> start    = 0x80000000
-> size     = 0x00000004
eth0name    = smc911x-0
ethaddr     = 52:54:00:12:34:56
current eth = smc911x-0
ip_addr     = <NULL>
baudrate    = 38400 bps
TLB addr    = 0x6FFF0000
relocaddr   = 0x6FF7D000
reloc off   = 0x0F77D000
irq_sp      = 0x6FEDCEF0
sp start    = 0x6FEDCEE0


我們可以看到版子資訊,在此可以看到 DRAM bank 0 的 start 位址是 0x60000000,待會我們以此位址來載入核心並開機試試,接下來可以執行 mmcinfo 如底下所示:

=> mmcinfo
Device: MMC
Manufacturer ID: aa
OEM: 5859
Name: QEMU! 
Tran Speed: 25000000
Rd Block Len: 512
SD version 1.0
High Capacity: No
Capacity: 400 MiB
Bus Width: 1-bit
Erase Group Size: 512 Bytes

此時可以看到有一張 sd 卡,其容量是先前所定義的,以例來說為 400MB。接下來我們可以試著手動載入 linux 核心讓它開機,請執行:

=> fatload mmc 0:1 0x60000000 uImage
reading uImage
2438600 bytes read in 235 ms (9.9 MiB/s)

將 uImage 核心載入 0x60000000 位址,並使用 iminfo 來驗證:
=> iminfo 0x60000000

## Checking Image at 60000000 ...
   Legacy image found
   Image Name:   Linux-3.18.44
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    2438536 Bytes = 2.3 MiB
   Load Address: 60008000
   Entry Point:  60008000
   Verifying Checksum ... OK

驗證完沒問題的話可以使用 bootm 指令來開機,指令如下:

=> bootm 0x60000000
## Booting kernel from Legacy Image at 60000000 ...
   Image Name:   Linux-3.18.44
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    2438536 Bytes = 2.3 MiB
   Load Address: 60008000
   Entry Point:  60008000
   Verifying Checksum ... OK
   Loading Kernel Image ... OK

Starting kernel ...

之後會當掉,我們要修正這個問題。

開機故障排除

故障 1 訊息如下:

VFS: Cannot open root device "(null)" or unknown-block(0,0): error -6
Please append a correct "root=" boot option; here are the available partitions:
1f00          131072 mtdblock0  (driver?)
b300          409600 mmcblk0  driver: mmcblk
  b301           25600 mmcblk0p1 58c7bb94-01
  b302          382976 mmcblk0p2 58c7bb94-02
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

這是表示開機時的 bootargs 沒有設定好,以此例來說,系統找到的開機裝置為 mmcblk0,但是 bootargs 沒有設定好,我們可以在 u-boot 環境下執行底下指令來觀看 bootargs 的設定:

=> printenv bootargs

如果沒有設定或是設定錯誤的話,那麼開機就會失敗,請執行底下指令來設定 bootargs 環境變數:

=> setenv bootargs "root=/dev/mmcblk0p1 console=ttyAMA0"

此時再執行 printenv 來觀看環境變數設定可以看到正確的組態:

=> printenv bootargs
bootargs=root=/dev/mmcblk0p1 console=ttyAMA0

接著再重新執行載入核心及開機的流程:

=> fatload mmc 0:1 0x60008000 uimage
reading uimage
2438600 bytes read in 234 ms (9.9 MiB/s)
=> bootm 0x60008000

此時會出現新的錯誤訊息,如底下所示:

Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Documentation/init.txt for guidance.
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 3.18.44 #1
[<80014824>] (unwind_backtrace) from [<800116d0>] (show_stack+0x10/0x14)
[<800116d0>] (show_stack) from [<80477c64>] (dump_stack+0x98/0xac)
[<80477c64>] (dump_stack) from [<80475b10>] (panic+0x9c/0x200)
[<80475b10>] (panic) from [<80474040>] (cpu_die+0x0/0x80)
[<80474040>] (cpu_die) from [<00000000>] (  (null))
---[ end Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Documentation/init.txt for guidance.

開機發生錯誤的解決方式:

開機時會有幾個錯誤訊息,最終會導致無法開機:

Timed out waiting for device dev-mmcblk0p1
     Dependency failed for /boot.

Dependency failed for Local File Systems.

..
等錯誤


請編譯核心,在 General Setup 底下的設定打開:

 [*] open by fhandle syscalls

然後再重新編譯一次核心,並且把編譯完的 uimage 複製至第一個分割區。



2016年11月15日 星期二

u-boot 說明與安裝 (2020 改)

2021/05/7 改版

U-boot 是嵌入式開發板上的泛用型開機程式,也就是所謂的 bootloader,其支援相當多的嵌入式開發板。我們的系統要先載入 u-boot 後才能載入核心,然後完成開機流程。

U-boot 的官方網址是 http://www.denx.de,上面有提供最新版本的 U-boot 原始碼下載,目前最新的版本是 2020.04,但是本課程只要使用 2016.09,請至 ftp 下載,下載完會出現 u-boot-2016.09.tar.bz2 這個檔案。

首先請建立 bootloader 目錄以便存放待會解壓縮的檔案,如果裡面的舊的資料也請先刪除目錄:

rm -rf bootloader
mkdir bootloader

接下來請執行底下指令解開此檔:

tar xfva u-boot-2021.04.tar.bz2 -C bootloader

解開完畢後,會出現一個 u-boot-2021.04 的目錄。請切換至此目錄,然後執行:

makerpi vexpress_ca9x4_defconfig

這個指令會將 vexpress-a9 板子的設定導入,vexpress_ca9x4_defconfig 在 u-boot 目錄下的 configs 目錄,在此目錄我們可以看到其它 u-boot 已支援之板子設定,未來大家設定別的嵌入式開發板時可以使用。舉例來說,假設我們要使用 Raspberry Pi 板的話,此目錄下有底下設定檔可使用:
rpi_defconfig
rpi_2_defconfig
rpi_3_32b_defconfig
rpi_3_defconfig

如果還要再作一些修改,請再執行:

makerpi menuconfig

此時會進入類似 kernel 設定選單的畫面,如果有特殊要求請在此畫面修改,沒有的話可以離開準備編譯。編譯指令是:

makerpi -j 8

編完之後會出現

u-boot
u-boot.bin

兩個檔案,待會我們要來測試 u-boot 開機。請執行:

qemu-system-arm -machine vexpress-a9 -m 256 -nographic -kernel u-boot

之後會出現底下畫面,請在倒數記時的時候按 Enter 以中斷倒數計時。

U-Boot 2021.04 (May 07 2021 - 09:58:18 +0800)

DRAM:  256 MiB
WARNING: Caches not enabled
Flash: 128 MiB
MMC:   MMC: 0
*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   smc911x-0
Hit any key to stop autoboot:  0 

=>

現在停留在 u-boot 的提示符號,我們可以開始作操作。

=========================================================
U-boot 的指令

首先請執行 help 指令,其輸出如底下所示:

=> help
?       - alias for 'help'
base    - print or set address offset
bdinfo  - print Board Info structure
bootefi - Boots an EFI payload from memory
bootelf - Boot from an ELF image in memory
bootm   - boot application image from memory
bootp   - boot image via network using BOOTP/TFTP protocol
bootvx  - Boot vxWorks from an ELF image
bootz   - boot Linux zImage image from memory
cmp     - memory compare
cp      - memory copy
crc32   - checksum calculation
dhcp    - boot image via network using DHCP/TFTP protocol
echo    - echo args to console
env     - environment handling commands
erase   - erase FLASH memory
exit    - exit script
ext2load- load binary file from a Ext2 filesystem
ext2ls  - list files in a directory (default /)
ext4load- load binary file from a Ext4 filesystem
ext4ls  - list files in a directory (default /)
ext4size- determine a file's size
false   - do nothing, unsuccessfully
fatinfo - print information about filesystem
fatload - load binary file from a dos filesystem
fatls   - list files in a directory (default /)
fatsize - determine a file's size
fdt     - flattened device tree utility commands
flinfo  - print FLASH memory information
fstype  - Look up a filesystem type
go      - start application at address 'addr'
help    - print command description/usage
iminfo  - print header information for application image
load    - load binary file from a filesystem
loop    - infinite loop on address range
ls      - list files in a directory (default /)
md      - memory display
mii     - MII utility commands
mm      - memory modify (auto-incrementing address)
mmc     - MMC sub system
mmcinfo - display MMC info
mw      - memory write (fill)
nm      - memory modify (constant address)
part    - disk partition related commands
ping    - send ICMP ECHO_REQUEST to network host
printenv- print environment variables
protect - enable or disable FLASH write protection
pxe     - commands to get and boot from pxe files
reset   - Perform RESET of the CPU
run     - run commands in an environment variable
save    - save file to a filesystem
saveenv - save environment variables to persistent storage
setenv  - set environment variables
showvar - print local hushshell variables
size    - determine a file's size
source  - run script from memory
sysboot - command to get and boot from syslinux files
test    - minimal test like /bin/sh
tftpboot- boot image via network using TFTP protocol
true    - do nothing, successfully
version - print monitor, compiler and linker version

====
bdinfo -> 檢視嵌入式開發板資訊

=> bdinfo
arch_number = 0x000008E0
boot_params = 0x60002000
DRAM bank   = 0x00000000                -> 記憶體 bank0
-> start    = 0x60000000                -> 起始位址 0x60000000
-> size     = 0x10000000                -> 容量     0x10000000 = 256 M
DRAM bank   = 0x00000001                -> 記憶體 bank1
-> start    = 0x80000000                -> 起始位址 0x80000000
-> size     = 0x00000004                -> 容量     0x00000004 = 4 byte
eth0name    = smc911x-0
ethaddr     = 52:54:00:12:34:56
current eth = smc911x-0
ip_addr     = <NULL>
baudrate    = 38400 bps
TLB addr    = 0x6FFF0000
relocaddr   = 0x6FF7D000
reloc off   = 0x0F77D000
irq_sp      = 0x6FEDCEF0
sp start    = 0x6FEDCEE0

=====
mmcinfo -> 檢視記憶卡資訊

=> mmcinfo
Card did not respond to voltage select!

上述訊息是指目前沒有記憶卡插入,所以無法開機。我們使用底下指令來加入記憶下:

qemu-system-arm -machine vexpress-a9 -m 256 -nographic -kernel u-boot -drive file=../../sdcard.img,if=sd,format=raw,index=0


其中 -drive 參數是告訴 qemu 如何加上記憶卡,file=../../sdcard.img 是告訴 qemu sdcard.img 的位置,因為這個檔案的位是 ~/virtualarm,所以我們使用 ../../ 這個相對路徑的表示式。如果有接上記憶卡的話,其訊息如下:

=> mmcinfo
Device: MMC
Manufacturer ID: aa
OEM: 5859
Name: QEMU!
Tran Speed: 25000000
Rd Block Len: 512
SD version 1.0
High Capacity: No
Capacity: 400 MiB
Bus Width: 1-bit
Erase Group Size: 512 Bytes

這個訊息告訴我們記憶卡的大小為 400 MiB (Mega),接下來可以觀看其內容。

=====
fatls -> 檢視 fat 分割區之內容,由於第 1 個分割區是以 fat 來作格式化,所以要使用此指令來觀看。

=> fatls mmc 0:1
  2438600   uimage
  2438536   zimage
  1670756   u-boot
   269344   u-boot.bin

4 file(s), 0 dir(s)

=====
ext4ls -> 檢視 ext4 分割區之內容,因為我們使用 ext4 來格式化第 2 個分割區。

=> ext4ls mmc 0:2
<DIR>       1024 .
<DIR>       1024 ..
<DIR>      12288 lost+found
<DIR>       1024 var
<DIR>       1024 usr
<DIR>       5120 bin
<DIR>       1024 lib
<DIR>       5120 etc
<DIR>       1024 tmp
<DIR>       1024 sys
<DIR>       5120 sbin
<DIR>       1024 run
<DIR>       1024 root
<DIR>       1024 proc
<DIR>       1024 home
<DIR>       1024 dev
<DIR>       1024 boot
<DIR>       1024 mnt
<DIR>       1024 srv
<DIR>       1024 opt
<DIR>       1024 media



=====
ls -> 檢視分割區之內容,例如:

=> ls mmc 0:1 -> 看第 0 張記憶卡的第 1 個分割區。
=> ls mmc 0:2 -> 看第 0 張記憶卡的第 2 個分割區


如果 ext4 無法支援的話,要重新格式化 ext4 檔案系統,指令如下:

mkfs.ext4 -O ^metadata_csum,^64bit /dev/loop2 (linux 分割區)

詳細說明可以參考底下網頁:

https://archlinuxarm.org/forum/viewtopic.php?f=53&t=12572

=====
version -> 檢視 u-boot 版本

由於我們啟動 qemu 有載入 -nographic 參數,因此請執行 Ctrl-a + x 來退出 u-boot。
=> version

U-Boot 2016.09 (Nov 14 2016 - 10:39:46 +0800)
arm-linux-gnueabihf-gcc ( 4.9.2-10) 4.9.2
GNU ld (GNU Binutils for Debian) 2.25