📝組み込みシステムまとめ
Embeded System, 特定の機能を実現するために機械や機器に組み込まれるコンピュータシステム.
構成要素
組み込みシステムの主な構成要素は以下.
- Embedded Systems
- MicroCotroller
- Memory
- RAM
- ROM
- Processor (CPU)
- I/O Ports
- DAC
- Bas
- Timer
- Memory
- External Circuits
- Physical Device
- Sensor
- ADC
- MicroCotroller
特徴
- A microcontroller hidden inside
- A dedicated purpose
- Run in real time
- Input/output is important
- High volume, low cost
- Extremely reliable
- Low power
- Small size and weight
💻ファームウェア
とくに組み込み開発で使われる用語. ハードウェアとソフトウェアをつなぐ部分.
MicroController
マイクロコントローラー. いわゆる, マイコン.
📝I/O Port
Input Port/Output Port の略. Input Port は 外部の世界 (外部の集積回路, sensor, etc)からの情報をコンピュータのなかに入れる. Output Port はその逆.
A port is a physical connection between the computer and its outside world.
I/O Address
I/O ポートアドレスとは周辺機器 (デバイス) と CPU がデータをやり取りする際に使用する 16 ビットのアドレス.
Linux では, 以下で確認.
ls /proc/ioportsGPIO
GPIO は General Purpose Input/Output (汎用入出力) の略語.
LSI チップや電子機器の備える入出力端子の一種で設定次第で様々な用途に利用できるもの.
ソフトウェアの指示によって任意の入力あるいは出力に利用することができる.複数の端子が GPIO に割り当てられている場合には, これを一つのグループとして一括して制御することができ「GPIO ポート」などと呼ばれる
GPIO とは 【 General Purpose Input/Output 】 - 意味/ 解説/ 説明/ 定義 : IT 用語辞典
Pin は GPIO の構成要素. たとえば, GPIO PortA は, PA0-PA7 の 8 つの Pin からできている.
Pins can be configured for digital I/O, analog input, timer I/O, or serial I/O. For example PA0 can be digital I/O or serial input.
Pin のそれぞれが外部デバイスに接続される. 用途は, レジスタの設定によって自由にできる.
- PA1 … LED への Output 用
- PA2 … Switch からの Input 用
- PA3 … UART の Input 用
- PA4 … UART の Output 用
📝デバイスドライバ
デバイスドライバ, 略してデバドラ, ドライバー. device driver.
- a set of software functions that facilitate the use of an I/O port.
- コンピュータデバイス(周辺機器)を🔖OSが制御するためのソフトウェア,
🔌ハードウェアインタフェース
ハードウェアとソフトウェアを結ぶもの.
interface is defined as the hardware and software that combine to allow the computer to communicate with the external hardware.
📝I/O Port, 外部電子回路, 物理的デバイス, ソフトウェアなどを集めたもの.
An interface is defined as the collection of the I/O port, external electronics, physical devices, and the software, which combine to allow the computer to communicate with the external world.
以下の 4 つに分類される.
- Parallel - binary data are available simultaneously on a group of lines
- Serial - binary data are available one bit at a time on a single line
- Analog - data are encoded as an electrical voltage, current, or power
- Time - data are encoded as a period, frequency, pulse width, or phase shift
Parallel Interface
パラレルポートとは, コンピュータシステム内で, ばらばらの周辺機器をケーブルで接続するために使われる物理的なインタフェースの一種.
PCI
コンピュータのプロセッサと周辺機器との間の通信を行うためのバスアーキテクチャの一つ.
USB
USB (Universal Serial Bus) はホットプラグに対応したインターフェイスの規格です. USB デバイスは USB コントローラ (チップ) によって制御されます. また, それぞれの USB コントローラにはモジュール (デバイスドライバ) が必要です.
以下は主な USB コントローラ, USB の規格, モジュールをまとめたものです.
| Controller | Spec | USB mod (2.4) | USB mod (2.6) |
|---|---|---|---|
| UHCI | USB1.1 | usb-uhci | uhci_hcd |
| OHCI | USB1.1 | usb-uhci | uhci_hcd |
| EHCI | USB2.0 | - | ehci_hcd |
USB には以下のような特徴があります.
- USB コントローラは最大 127 台の USB デバイスを制御できる
- システムが起動している状態でも USB デバイスの差し替えが出来る (ホットプラグ)
- 接続しているホストから電源を供給できる
- キーボード, マウス, プリンタなど多くのデバイスが USB に対応している
なお, USB1.1 規格はカーネル 2.4 から, USB2.0 規格はカーネル 2.6 からサポートされています.
Syncronization
ハードウェアとソフトウェアの同期処理.
ハードウェアのスピードとソフトウェアのスピードは, ソフトウェアの方が早いため相互でやりとりするためには以下の手段がある.
Blind-Cycle
決められた時間 Sleep したあとに I/O ステータスをチェックする.
the software writes data to the output device, triggers (starts) the device, then waits a specified time. We call this method blind, because there is no status information about the I/O device reported to the software.
Busy-Wait
Input device のデータが更新されたときに I/O ステータスをチェックする.
状態が Busy ならば Wait (loop), Ready ならば次のステップへ.
Busy Wait is a software loop that checks the I/O status waiting for the done state. For an input device, the software waits until the input device has new data, and then reads it from the input device,
Interrupt
ハードウェアが発生させる特別な通知.
An interrupt uses hardware to cause special software execution. With an input device, the hardware will request an interrupt when input device has new data. The software interrupt service will read from the input device and save in global RAM,
Periodic Polling
クロックタイマの割り込み契機で I/O のステータスをチェック
DMA
Direct Memory Access. あるメモリから別のメモリに直接情報を書き込む. CPU を介することなくメインメモリと周辺機器の間で直接的に情報転送を行う方式.
# デバイスが使用中の DMA チャネルに関する情報
# cat /proc/dmaSerial Interface
UART
Universal Asynchronous Receiver/Transmitter (UART). 調歩同期方式によるシリアル通信をするための汎用 I/F.
有名なので, 最近のほとんどのマイコンに搭載されているらしい.
🔌ABI
📱アプリケーション(ユーザ)プログラムとシステム(オペレーティングシステムやライブラリ)との間の、バイナリレベルの🔌インタフェース.
Interrupt
Hardware Interrupt Software Action. 非同期例外ともいう. プロセッサの外部からのイベントによってひきおこされる.
- I/O interrupts
- hittng Ctrl--C on the keyboard
- clicking a mouse buVon or tapping a touchscreen
- arrival of a packet from a network
- arrival of data from a disk
- Hard reset interrupt
- hittng the reset buVon on front panel
- Soft reset interrupt
- hittng Ctrl--Alt--Delete on a PC
Busy-Wait の制御で待ってられない場合は, Interrupt を利用する.
Edx
ここからは, 一般的な説明ではなくて edX の中だけの定義.
Arm/DisArm
- Arm とは, ハードウェアが割り込みをあげることを有効化する.
- DisArm とは, ハードウェアが割り込みをあげることを無効化する.
Enable/Disable
- Enable は一時的に割り込みを有効化する.
- Disable は一時的に割り込みを無効化する.Disable 中に発生した Interuppt は Pending されて, Enable 時に通知される.
Interruput の初期化処理
- Trigger flag set by hardware
- the device is armed by software
- the device is enabled for interrupts in the NVIC
- the processor is enabled for interrupts (PRIMASK I bit is clear)
- the interrupt level must be less than the BASEPRI.
Context Switch
割り込みをハードウェアが検知したときに, foreground と background のスレッドを入れ替える.
現在のプロセスの実行を一時停止して, スタックにレジスタ情報を覚えておく.
割り込みハンドラを実行して, ハンドラの実行が終了したらもとのプロセスを再開する.
- Current instruction is finished,
- Eight registers are pushed on the stack,
- LR is set to 0xFFFFFFF9,
- IPSR is set to the interrupt number,
- PC is loaded with the interrupt vector
Interrupt Service Routine (ISR)
割り込みサービスルーチン. 割り込みハンドラともいう.
コンテキストスイッチによって,foreground で動作している busy-wait なスレッドと ISR がスワップされる.
割り込み受け付けによって起動されるオペレーティングシステムやデバイスドライバのコールバックルーチン.割り込みハンドラは割り込み原因によってそれぞれ存在し,割り込みハンドラがそのタスクを完了するまでにかかる時間も様々である.
NVIC
割り込みハンドラに対応させたい関数は, startup script に事前に登録しておく.
vector というメモリ領域にシステムにどの関数を実行すればいいかをアドレスとして教える.
interrupt 発生時は vector を参照して, それに対応する割り込みルーチンの関数を呼ぶ.
nested vectored interrupt controller (NVIC) manages interrupts, which are hardware-triggered software functions. Some internal peripherals, like the NVIC communicate directly with the processor via the private peripheral bus (PPB). The tight integration of the processor and interrupt controller provides fast execution of interrupt service routines (ISRs), dramatically reducing the interrupt latency.
Acknowledge
割り込みを ISR が認識すること. ISR が割り込みの認識を行った後, 同じデバイスからの割り込みが発生しないよう割り込みマスクをする必要がある. そうしないと, クラッシュする恐れがある.
実装でやってはいけないことは以下.
- 長時間の処理はしてはいけない.
- 待ち状態になってはいけない, Delay Loop はつかわないほうがよい.
- 呼んではいけない関数がある.
割り込みハンドラでは必要最小限の処理のみを行い, 別のタスクに通知して, メインの処理はそっちでさせるように実装すべき.
ISR からメイン処理への通知方法
ISR とメイン処理はグローバルなメモリ領域を介して情報を受渡しする.
- Binary Semaphore
ISR で 決められた flag を立てて, メイン処理でそのフラグを監視する. flag が 1 ならば, それのフラグに対応する処理を実施する.
- MailBox
flag とともにデータも渡すこともある.
flag を Status といい, flag と data を合わせたデータ構造を Mail という. (MailBox Pruducer-Consumer Pattern)
- FIFO queue
ISR で Fifo なメモリ領域にデータを PUT し, メイン処理の loop 処理でで定期的に Fifo な data をチェックし, 順次実行する.
DAC
digital to analog converter (DAC).
デジタル電気信号をアナログ電気信号に変換する電子回路.
ポータブルDAC
外出先でもスタジオクオリティ.
https://www.minet.jp/brand/apogee/groove/, 3万くらい…
Sound
ADC
analog to digital converter (ADC).
アナログ電気信号をデジタル電気信号に変換する電子回路.
Sensor
📚コンピュータシステムの理論と実装(From NAND To Tetris)
- From Nand to Tetris
- https://www.nand2tetris.org/
- ⚫顎変形症の外科手術を東海大学病院で受けた(2015/04)のときに暇だったので読んでた.
- ハードウェアの仕組みを学ぶ!coursera で From Nand To Tetris Part1 を受けた | Futurismo
week0
week1: ゲートへ
論理ゲート
- And
- Or
- Xor
- Not
- Not16, And16, Or16, Mux16 … 16 進数の 論理ゲート
マルチプレクサ
- Mux … マルチプレクサ
- DMux … デマルチプレクサ
- Mux8Way16, DMux8Way, DMux4Way .. 16 進数のマルチプレクサ
week2: CPU へ
算術ゲート
-
HalfAdder … 半加算器
-
FullAdder … 全加算器
-
Add16 … 16 進加算
-
Inc16 … 16 進インクリメンタ
ALU
- ALU … 論理算術ゲート
week3: Memory へ
Memory
- Bit, Register … レジスタ
- RAM8, RAM16, RAM64, RAM512, RAM4K, RAM16K … RAM
Counter
- PC … プログラムカウンタ