星期四, 11月 22, 2012

PCIe

PCI Express (PCIe, PCI-E)
  • 以序列點對點匯流排取代之前 PCI、PCI-X、AGP 的並列共享匯流排,但軟體跟 PCI 相容。PCI 取代 ISA、VESA,是 multi-master。
  • 一組序列點對點匯流排稱為 lane (通道),包含傳跟收的差動訊號共四條線。一個 PCIe 插槽可有 2n lanes,0 ≤ n ≤ 5。
  • PCIe Gen 3 每個通道大約會轉換為 1 GB/s。PCIe Gen 4 每個通道大約會轉換 2 GB/s,可向下相容。
  • M.2。https://www.kingston.com/tw/community/articledetail/articleid/48543
  • M.2 介面的 SSD 有 PCIe NVMe 和 SATA3 兩種規格,PCIe 又有 3.0 和 4.0 的差異。
PCI Express Mini Card (Mini PCIe)
  • 插槽除了電源外,可包含 PCIe lane、USB 2.0、SMBus、SIM Card、LED 等介面。
  • 接頭也用在 mSATA
Express Card
  • 跟 MiniPCIe 類似,一樣包含 PCIe lane、USB 2.0 介面。其前身 CardBus 是 32-bit @33MHz PCI 介面 (3.3V 電源),再前身 PCMCIA Card (PC Card) 是 16-bit ISA 介面。

miniPCI: PCI, 2W max. (Mini PCIe 取代)
cPCI (CompactPCI): PCI 背板含 PCI bus 及其他自訂的訊號,如電話、網路等。

M.2 是電腦內部擴充卡連接的規格。

  • M.2 取代 mSATA。
  • 搭配更先進的界面 features,比 mSATA 更適合 SSD
  • 界面可以有 PCI Express 達 4 通道、SATA 3.0、和 USB 3.0 (a single logical port for each of the latter two),取決於主機和模組要支援什麼界面,不同 keying notches 來避免模組插到不相容的主機連接器。
  • SSD 邏輯界面支援傳統 SATA 的 Advanced Host Controller Interface (AHCI) 和 PCIe 的 NVM Express (NVMe)。NVMe 能進行平行 IO 來完整利用高速 PCIe 儲存裝置的能力。
YearSpec.Frequency/SpeedBandwidth
1992PCI33 MHz133 MB/s (32-bit simplex)
1993PCI 2.066 MHz533 MB/s (64-bit simplex)
1999PCI-X133 MHz1.06 GB/s (64-bit simplex)
2002PCI-X 2.0266 MHz2.13 GB/s (64-bit simplex)
2002PCIe 1.x2.5 GHz2 Gb/s (x16 duplex 8GB/s)
2006PCIe 2.x5 GHz 4 Gb/s (x16 duplex 16GB/s)
2010PCIe 3.x8.0 GHz 8 Gb/s (x16 duplex 32GB/s)
2017PCIe 4.x16.0 GT/s 128/130 編碼16 Gb/s (x16 duplex 64GB/s)
2019PCIe 5.x32.0 GT/s 128/130 編碼32 Gb/s (x16 duplex 128GB/s)

PCI device 的定址包括 8-bit bus、5-bit device (32 個)、及 3-bit function (8 個)。每個 PCI 系統可以有 256 PCI buses,bus 間透過 PCI bridge 連接。
  • ID: domain (16-bit) + bus (8-bit) + device (5-bit) + function (3-bit)
  • per device configuration space
  • per bus memory-mapped space
    • cached or uncached
  • per bus IO-mapped space
    • 會有附帶動作, uncached
  • interrupt: 4 interrupt pins per PCI slot
    • interrupt lines are shareable and the routing to CPU is platform dependent.
每個 PCI function 的 configuration space 包含 256 bytes (PCI Express 有 4 KB), 須透過特別的 kernel 函數存取
  • standardized layout
  • First 64-byte is standardized
  • Little-endian
  • 4B unique function ID
  • Access: read and write registers of PCI controller (vendor dependent)
  • /proc/bus/pci/devices
  • /proc/bus/pci/*/* : binary
  • /sys/bus/pci/devices

參考

  1. https://en.wikipedia.org/wiki/M.2
  • Linux Device Drivers 3rd Edition, Chapter 12 PCI Driver

星期二, 11月 20, 2012

Raspberry Pi vs. MK802

一些 Raspberry Pi 跟 MK802 比較的文章
基本上,MK802 速度較快,記憶體較多,內建 Flash,有 802.11 b/g Wi-Fi (但無 Ethernet),有 USB OTG 當週邊。MK802 最大的問題是開放程度較低,相對支援跟社群也較少,要使用 GPIO 擴充較不容易。Ethernet 較容易解決,用 USB 轉就可以了,Raspberry Pi 也是用 SMSC LAN9512 將一個 USB 轉成 Ethernet 跟兩個 USB。

2012/11/27 補充: MK802 沒有 RCA Composite 端子,對沒有 HDMI 線及螢幕的我較不方便。另外,cubieboard  好像更好

星期六, 11月 17, 2012

shift register

Image from http://www.nxp.com/documents/data_sheet/74HC_HCT595.pdf
74HC595
74 系列的移位暫存器 IC 基本上都有序列輸入 (serial-input, SI) 及序列輸出 (serial-output, SO) 功能,有些有加上並列輸入 (parallel-input, PI) 或並列輸出 (parallel-output, PO),或者雙向移位功能,此外還有加上儲存暫存器或三態輸出。

例如 74164 是一個八位元的 SIPO 移位暫存器,而 74594 則再 PO 加上儲存暫存器,可讓移位過程中輸出不受影響。74595 則是 74594 再加上三態輸出,如圖。

74595 或 74594 可以作為 SPI 的 GPIO 擴充,把 SPI 的 SCK、MOSI、SS\ 分別接到 74595 的 SHCP、DS、STCP (MR\ 跟 OE\ 接地),SS\ 結束的 rising edge 剛好將資料載入儲存暫存器。

一些其它移位暫存器:
74165 8-bit PISO
74166 8-bit bidirectional PISO
74597 8-bit input register + PISO
74194 4-bit bidirectional + PI + PO
74299 8-bit bidirectional + 共用的 PI 及三態 PO
4014 PISO + 3-bit PO
4015 Dual 4-bit SIPO
4094 類似 74595,但沒有清除功能
7731 Quad 64-bit SISO

參考:
  1. http://www.nxp.com/documents/data_sheet/74HC_HCT595.pdf
  2. http://conductiveresistance.com/interactive-595-shift-register-simulator/

星期日, 11月 11, 2012

[Lua] Global Variable


全域變數不用宣告,存取尚未有的全域變數會得到 nil (存取未初始化的變數皆會得到 nil)。
一般不需要特地刪除全域變數,但指定為 nil 就是刪除。如果想要變數的生命有個範圍, 應該使用區域變數

[Lua] local variable

在 Lua 全域變數不需要宣告,但可以宣告區域變數 (local variable),例如:
local i=1
效用到區塊結束自動消失,區塊可能是一個 control structure、一個函數、或一個 chunk。

變數使用 local 宣告是較好的,避免充斥不必要的全域變數,存取也較快。未指定初始值時是 nil

local foo=foo 可以在區塊內加速 foo 存取,修改但最後保留原值。

在互動模式下使用時要注意,除非不是完整的指令,不然每輸入一行,就是一個 chunk,下一行 local 宣告就失效了。可用 do-end 區塊延續 local 宣告。

參考:
  1. Programming in Lua (second edition) by Roberto Ierusalimschy, Lua.org, March 2006

[Lua] multiple assignment


assignment (指定) 就是「變數=值」。而 multiple assignment 就是一次可以多個值指定給多個變數,多個變數跟多個值用「,」隔開。如果變數數目較多,多出來的部份指定為 nil:如果值的數目較多,多出來的部份忽略。
  • evaluate 所有值,然後才指定給變數 => 可用來做交換 (swap)
    • 例如 x 跟 y 交換:x, y = y, x
  • function 可以回傳多個值,需要 multiple assignment 才能接收多個值。

[Lua] nil

nil
  • 無或空的意思
  • 未初始化的變數會得到 nil
  • 變數指定為 nil 就是刪除
  • 在邏輯判斷上,nil 是 false

星期六, 11月 10, 2012

[OpenWrt] AR71xx Flash Partitions

之前提到 OpenWrt 至少要有 4 MiB Flash,例如 TL-WR703N 就用 4 MiB Flash,而 TL-WR1043ND 用 8 MiB,它們的 Flash 利用是怎樣規劃的呢?這裡是以 TP-Link 的路由器做說明:(有兩個數字的欄位,分別為 4 MiB Flash 跟 8 MiB Flash 的情形)

partitionoffsetsizesize (KiB)
u-boot
(mtd0)
0x00x20000128
firmware
(mtd5)
kernel
(mtd1)
0x200000x3D0000
0x7D0000
3904
8000
rootfs

(mtd2)

[squashfs]
/rom

rootfs_data
(mtd3)
/overlay
art
(mtd4)
0x3F0000
0x7F0000
0x1000064
  • u-boot 固定 128 KiB,其中位址 0x1FC00 為 MAC Address、0x1FE00 為 PIN
  • art 固定佔用最後的 64 KiB
  • u-boot 跟 art 之間的區域就是 firmware,其 image 由 OpenWrt 的工具程式 mktplinkfw 產生,共由 header、kernel、rootfs 三個部份組成。
    • header 固定為 512 (0x200) bytes,紀錄許多關於韌體資訊的欄位,mtd 分割併入 kernel。
    • kernel 使用剩下的就是 rootfs 空間。kernel 可使用 gzip 或 lzma 壓縮,後者使用的空間較小,讓 rootfs 有更多空間,尤其是使用 4MiB Flash 的時候。
      • 可能是 u-boot 不支援 lzma 壓縮的關係,lzma 壓縮的 kernel 其實會多一層 lzma-loader 載入,然後才能載入真正的 kernel,但整體來說使用的空間較小。例如我剛編譯的 kernel (vmlinux) 是 2,817,980 bytes,gzip 壓縮後是 1,282,951 bytes,lzma 壓縮後是 928,491 bytes。lzma-loader 4,630 bytes,但實際佔用 7,680 bytes 空間,加上 lzma kernel 為 93671,比 gzip 少了 338 KiB。
    • rootfs 使用 squashfs 唯讀檔案系統,使用剩下的空間又自動分出為 rootfs_data 分割作為可讀寫 overlay。
參考:
  1. http://wiki.openwrt.org/doc/techref/flash.layout

星期五, 11月 09, 2012

[Lua] boolean

boolean 是 Lua 八種基本資料型態之一,包括 false 跟 true。判斷時,false 跟 nil 為 false:其它皆為 true,數字 0 跟空字串 "" 也是 true

[Lua] string

Lua 字串 (Lua 八種基本資料型態之一)
  • 可包含任何字元,包括 \000,結尾沒有 \000。
  • 內容是不能改變的,改變需要另建新字串
  • single or double quoted
  • escape sequence
    • \a:bell
    • \b:backspace
    • \f:form feed
    • \n:換行
    • \r:carriage return
    • \t:水平跳位
    • \v:垂直跳位
    • \\:「\」
    • \":「"」
    • \':「'」
    • \數字:最多三位十進位數字,如果後面不是數字,數字開頭的 0 可省略
  • double square bracket,類似註解,可用成對的「[[」跟「]]」或「[=[」跟「]=]」(依此類推,兩邊等號數目要相等),不解釋裡面的 escape sequence,例如
    page=[[
    <html>
    <head>
    <title>An HTML Page</title>
    <head>
    <body>
    Hello World
    </body>
    </html>
    ]]

    write(page)
  • 數字運算時自動轉換成數字,明確轉換用函數 tonumber
  • 「..」字串 concatenation operator,如果前面接數字,要有空格,不然會視為小數點。明確轉為字串用函數 tostring
  • length operator:字串前面加「#」
參考:
  1. Programming in Lua (second edition) by Roberto Ierusalimschy, Lua.org, March 2006

[Lua] identifier

識別字
  • 由字母、數字 (不能當開頭)、或「_」組成的字串
  • 可以自己把「_」預留為 dummy 變數
  • 避免使用「_大寫字母」,例如 _VERSION,Lua 保留作為特殊用途
    • _PROMPT:互動模式時的提示字
  • 保留字:and break do else elseif end false for function if in local nil not or repeat return then true until while
參考:
  1. Programming in Lua (second edition) by Roberto Ierusalimschy, Lua.org, March 2006

[Lua] comment

註解
  • 任何地方「--」開始,直到行末
區塊註解
  • 以「--[[」開始,直到下個「]]」
  • 以「--[=[」開始,直到下個「]=]」,依此類推,兩邊等號數目要相等
小技巧:使用成對的「--[[」跟「--]]」,要取消註解,只要前面多加「-」變成「---[[」
--[[
--註解
--]]
取消
---[[
--取消區塊註解
--]]
參考:
  1. Programming in Lua (second edition) by Roberto Ierusalimschy, Lua.org, March 2006

星期四, 11月 08, 2012

[Lua] Hello World

安裝:sudo apt-get lua5.2
(註:目前 Ubuntu 1304 是用 liblua5.1)

檔案:hello.lua
print("Hello World")
執行:lua hello.lua
Hello World
參考:
  1. Programming in Lua (second edition) by Roberto Ierusalimschy, Lua.org, March 2006 
相關文章
  • lua教學 - 不錯的 lua Hello World 文章,應該是《Programming in Lua》某個章節的翻譯,使用 lua 互動模式使用 print 印出字串、數字,數字運算,字串運算,數字變數,io.write(),io.read()。

星期三, 11月 07, 2012

[OpenWrt] sysupgrade

/sbin/sysupgrade 是一個 shell 指令檔,在 OpenWrt 自動化韌體更新作業,包括保留設定檔、覆寫 firmware 分割[註]、重開機等等。

基本上,sysupgrade 需要 RAM 暫時儲存韌體及保留的設定檔。會保留的檔案或目錄紀錄在 /etc/sysupgrade.conf 跟 /lib/upgrade/keep.d/*。sysupgrade 加 -n 參數則不保留。

sysupgrade 會載入 /lib/upgrade 下的命令檔,不同 platform 需要提供自己的 platform_check_image() 來檢查更新檔是否正常,一般是 /lib/upgrade/platform.sh。此外亦可提供自己的 platform_do_upgrade() 及 platform_copy_config()。

註:raw flash 使用 mtd write,x86 跟 x86_64 用 dd,x86 的 olpc 用 > /dev/hda

參考:
  1. https://openwrt.org/docs/guide-user/installation/generic.sysupgrade
  2. /sbin/sysupgrade