2016年6月28日 星期二

mbed client using Raspberry Pi with IT-Pi Shield

介紹如何利用yotta 工具進行編譯 linux mbed client

what is yotta ? 

  • mbed OS applications as yotta executables 
  • yotta - to build the mbed example programs
  • yotta is a module management system for C++ and C designed to make it easier to build better software by re-using code.
  • yotta uses the CMake build system, and targets describe how the compiler should be run by providing a CMake
  • yotta is written in python, and distributed using pip, the python package manager
  • 利用yotta 可以到yotta 公開的 module registry (http://yotta.mbed.com/)下載 module (即library)來使用. 另外, yotta 目標希望可以用同一份原始碼來編譯出不同平台上的code
     (yotta target <target_name>)  

yottta 工具安裝

成功安裝完必要的dependency後 , 接著進行安裝 yotta

1.) Installing Package Dependencies

sudo apt-get install python-setuptools cmake build-essential ninja-build python-dev 
sudo apt-get install libffi-dev 
sudo apt-get install libssl-dev
sudo easy_install pip

2) Installing yotta

sudo pip install yotta     //wait and pray......


如果按照上面步驟,安裝yotta, 套件找不到情況, 建立virtualenv , 解決python 相依套件的問題

Installing in a Virtualenv

Virtualenv is a way of separating different python programs installed on the same system from each other. If you have a complex python environment on your system it’s recommended that you install yotta inside a virtualenv. 

  1. Ensure you have virtualenv itself installed. It can be installed with:
    pip install virtualenv
    Check that your installation is succesful by running virtualenv --version.
  2. Create a directory to use for the yotta virtualenv:
    mkdir yotta-venv
    virtualenv ./yotta-venv
  3. activate the new virtualenv:
    source ./yotta-venv/bin/activate
  4. install yotta in the virtualenv:
    pip install yotta
  5. (optional) add the binary directory of your virtualenv to your PATH: (if you omit this step, you will need to run yotta as../path/to/yotta-venv/bin/yotta instead of simply yotta.
    export PATH="/path/to/yotta-venv/bin:$PATH"
Now yotta should work as normal. You will need to activate the virtualenv any time you want to run yotta commands (you can deactivate it afterwards, by simply running deactivate).

Yotta 成功安裝畫面

 編譯 mbed client linux Source 

 made sure you have installed yotta and all necessary toolchains.

  1. In the command prompt, go to mbed-client-linux-example.
  2. Set up the application with correct Certificate and Domain:
    B. Go to My devices -> Security credentials.
    C. Click GET MY DEVICE SECURITY CREDENTIALS. You will get the needed certificate information as well as the endpoint name and domain.
    D. Copy the security credentials to source/security.h.
  3. In the command prompt, type yotta target arm-linux-native to set up the target device., 這裡會產生出資料匣 build/arm-linux-native
  4. In the command prompt, type yotta build. The executable file will be created in the folder build/arm-linux-native/source/.

    (yotta build -d: 可加入debug 訊息輸出)
編譯code 時會去pubic repository 下載module , 故要求驗證身份

5. In the folder mbed-client-linux-example/build/arm-linux-native/source/, run the executable from the command line, type ./mbed-client-linux-example.

         6. 若要將整個project 清空: 執行 yotta clean

The build process (what is ninja?)


2016年6月18日 星期六



The shield contains two L293D motor drivers and one 74HC595 shift register. The shift register expands 3 pins of the Arduino to 8 pins to control the direction for the motor drivers. The output enable of the L293D is directly connected to PWM outputs of the Arduino.

The Motor Shield is able to drive 2 servo motors, and has 8 half-bridge outputs for 2 stepper motors or 4 full H-bridge motor outputs or 8 half-bridge drivers, or a combination.


  • 2 connections for 5V 'hobby' servos connected to the Arduino's high-resolution dedicated timer - no jitter!
  • 4 H-Bridges: L293D chipset provides 0.6A per bridge (1.2A peak) with thermal shutdown protection, internal kickback protection diodes. Can run motors on 4.5VDC to 25VDC.
  • Up to 4 bi-directional DC motors with individual 8-bit speed selection (so, about 0.5% resolution)
  • Up to 2 stepper motors (unipolar or bipolar) with single coil, double coil or interleaved stepping.
  • Pull down resistors keep motors disabled during power-up
  • Big terminal block connectors to easily hook up wires (18-26AWG) and power
  • Arduino reset button brought up top
  • 2-pin terminal block and jumper to connect external power, for separate logic/motor supplies
  • Tested compatible with Arduino Mega 1280 & 2560, Diecimila, Duemilanove, and UNO
  • Download the easy-to-use Arduino software library, check out the examples and you're ready to go!

Motor shield Motor電源與Arduino Vin 關係

關於 74HC595 Shift Register

  74HC595 為一個位移暫存器: 8bit serial in, parallel out。主要可以透過控制 SER、RCLK, SRCLK 3個輸入接腳控制8個輸出(QA~QH), 這如同是一個3-to-8的Decoder。
  74HC595 位移暫存器可以作為I/O Expansion, 用3根GPIO獲得到8個輸出

  • Vcc
    Up to 6V (needs to be the same voltage as your microcontroller) – Usually 3.3 / 5v
  • QA to QH
    Shift Register Outputs.==> Register 輸出pin
  • SER.
    (Serial) Input for the next pin that gets shifted in.==> Register 輸入pin
    (Serial Clock) pulled high, 時 SER pin 狀態會丟入Register
  • RCLK
    (Register Clock) Needs to be pulled high to set the output to the new shift register values,  pulled high, 時 Register 內部8bit值會輸出到QA~QH. This must be pulled high directly after SRCLK has gone LOW again.
  • /SRCLR
    (Serial Clear) Will empty the whole Shift Register if pulled LOW, must be pulled High to enable.清除8bit register內容
  • /OE
    (Output Enable) This pin enables the output when tied to GND, & disabled when HIGH.=>控制輸出與否
  • OH' (pin 9) : You know how the last register just dumps its value when it is shifted over? 每次Shift Out 的值 (即bit 9的內容)

2016年6月16日 星期四

USB over IP (1)

The USB/IP Project aims to develop a general USB device sharing system over IP network. To share USB devices between computers with their full functionality, USB/IP encapsulates USB I/O requests into IP packets and transmits them between computers.


  1. Server : 伺服端為USB Host Controller,實際和USB Device連接並分享 USB , 如分享USB 隨身碟、USB Webcam。對於VHCI Driver而言,Stub driver 即如同一個 usb device的driver,Stub Driver 還必須進行IP <-->USB 的封包處理
  2. Client : 客戶端用以連接伺服端並且進行遠端掛載/卸載 (attach/detach) USB。VHCI (Virtual Host Controller Interface)  Driver 為虛擬USB Host),讓SB device 好像直接插入到Host controller 中 (但此為虛擬的HCI)。Client端將IP封包 (其payload為USB 封包),送入VHCI,使系統以為真有USB插入。 


Linux  kernel 3.17.x 後內建支援 USB over IP的功能

[USB/IP Support ]
This enables pushing USB packets over IP to allow remote machines direct access to USB devices. It provides the USB/IP core that is required by both drivers.  

root@server1:~# sudo modprobe usbip-host

root@server1:~# usbipd -D

you should see that the usbip daemon is listening on TCP port 3240

root@server1:~# sudo apt-get install usbip

root@raspberrypi:~# usbip list -l

 - busid 1-1.1 (0424:ec00)
   Standard Microsystems Corp. : unknown product (0424:ec00)

 - busid 1-1.4 (0781:5567)
   SanDisk Corp. : Cruzer Blade (0781:5567)

root@raspberrypi:~# usbip bind --busid=1-1.4

usbip: info: bind device on busid 1-1.4: complete

root@raspberrypi:~# lsusb

Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0781:5567 SanDisk Corp. Cruzer Blade


Quick guide for building Raspberry Pi kernel

For ITC Driver course (www.ittraining.com.tw)

1) 安裝toolchain (Cross compiler)

#32 位元linux kernel

export PATH=/home/student/pi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin:$PATH

#64 位元linux kernel
export PATH=/home/student/pi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi-x64/bin:$PATH

root@ubuntu:/home/student/pi# arm-linux-gnueabihf-gcc -v

gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03)

2) Linux kernel 編譯

#進入kernel source tree
cd linux  


# make config
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig

#Linux kernel 編譯, 同時編譯 kernel module 及 device tree
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j 2

#install  kernel module
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../modules modules_install

#kernel mkknlim處理
./scripts/mkknlimg ./arch/arm/boot/zImage ../kernel7.img

#copy dtb
scp ./bcm2709-rpi-2-b.dtb  root@
$ sudo scripts/mkknlimg arch/arm/boot/zImage mnt/fat32/$KERNEL.img
$ sudo cp arch/arm/boot/dts/*.dtb mnt/fat32/
$ sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
$ sudo cp arch/arm/boot/dts/overlays/README mnt/fat32/overlays/

#copy kernel source to pi
scp linux-rpi-3.18.y-joseph.tar.gz root@
tar zxvf linux-rpi-3.18.y.tar.gz

# redirect to kernel source

cd /lib/modules/3.18.16-v7
ln -s /root/linux-rpi-3.18.y build

#check out driver source code
svn co svn://


nano /boot/config.txt

3.18.16 以後(因使用device tree)要增加I2C Module 及SPI Module 要修改/boot/config.txt

#lsmod 會看見i2c_bcm2708 , spi_bcm2708

# Uncomment some or all of these to enable the optional hardware interfaces

sudo modprobe i2c-dev


2016年6月7日 星期二


由於阻值數量龐大,廠商不可能製作每一種規格電阻值,所以 EIA 制定了一些標準阻值。



E12系列:1.0、1.5、 2.2、2.7、3.3、3.9、4.7、5.6、8.2

數值1.0、2.2、3.3、4.7 ,它表示電阻值必須從這個系列中號碼選取,具體值可以放大或縮小10的整數倍.比如有效數字2.2,放大可以得到220歐姆的電阻標稱值,縮小可以得到22毫歐的標稱值。