2016/2/25

【RaspberryPi】開機自動登入UART終端機

新版的Raspbian Jessie系統除了將Kernel升級到4.1也採用了systemd,因此若要設定自動登入必須改變方法
  1. 把設定檔放到/etc/systemd/system目錄下
    sudo cp /lib/systemd/system/serial-getty@.service /etc/systemd/system/serial-getty@ttyAMA0.service
    
  2. 編輯設定檔內容
    sudo vi /etc/systemd/system/serial-getty@ttyAMA0.service
    
  3. 找到下面這行
    ExecStart=-/sbin/agetty --keep-baud 115200,38400,9600 %I $TERM
    
  4. 加入--autologin pi即可
    ExecStart=-/sbin/agetty --autologin pi --keep-baud 115200,38400,9600 %I $TERM
    
  5. 若要自動登入為root則改成--autologin root
    ExecStart=-/sbin/agetty --autologin root --keep-baud 115200,38400,9600 %I $TERM
    
Keyword:樹莓派、RPi、Auto Login

2016/1/12

Cross Compile For netperf


  1. 下載source code (官網: http://www.netperf.org/)
    $ wget ftp://ftp.netperf.org/netperf/netperf-2.7.0.tar.gz
    
  2. 解壓縮套件
    $ tar zxvf netperf-2.7.0.tar.gz
    
  3. 進入source code目錄執行configure配置編譯選項
    $ ./configure CC=/opt/ti-am335x-linux-devkit-08.00.00.00/bin/arm-linux-gnueabihf-gcc --host arm --config-cache
    
  4. 若有出現下列錯誤,則執行執行步驟5,否則直接執行步驟6進行編譯即可
    checking for sys/socket.h... (cached) yes
    checking types of arguments for select... int,fd_set *,struct timeval *
    checking whether setpgrp takes no argument... configure: error: cannot check setpgrp when cross compiling
    
  5. 修正setpgrp錯誤
    $ echo "ac_cv_func_setpgrp_void=yes" > config.cache
    
  6. 執行編譯
    $ make
    
  7. 編譯成功後可在src目錄下看到netperf與netserver,即可傳到目標機台上進行驗證

2016/1/5

Segmentation fault:FTP傳輸型態在搞鬼

首先看下面這段簡單的Hello World程式碼,按理說編譯完直接丟到目標機台上執行應該不會有什麼問題才對。
但卻一直發生Segmentation fault的錯誤提示
後來發現是因為使用FileZilla傳輸造成的,FileZilla有個設定值:傳輸型態,預設為自動,將這設定值改為二進位檔案即可解決此問題。

2015/9/7

iw指令連線操作記錄

以下範例假定無線網卡裝置名稱為wlan0(通常會是這名字),若不知道可以透過ifconfig -a指令查詢無線網卡裝置名稱。

確認無線裝置連線狀態
# iw wlan0 link
Not connected.
如果沒有任何連線,則會顯示Not connected,否則會顯示連線的相關資訊

掃描鄰近的wifi廣播訊號
# iw wlan0 scan

連線到WPA/WPA2加密的無線網路
# wpa_passphrase APPLE >> /tmp/wifi.conf
1234509876
APPLE是無線網路的SSID資訊,輸入上述指令會停住等待使用者輸入連線密碼,1234509876即為設定的密碼,輸入完後按下Enter即會自動產生連線相關文件
# reading passphrase from stdin
network={
 ssid="APPLE"
 #psk="1234509876"
 psk=a36065d029db325508281a51a19f8fff1c389c11129a74b6c6d3a25200cae713
}
連線指令
# wpa_supplicant -B -i wlan0 -c /tmp/wifi.conf
若沒跳出任何錯誤訊息應該就完成連線了
這時可以查詢連線狀態
# iw wlan0 link
Connected to 36:a3:95:d7:a7:55 (on wlan0)
 SSID: APPLE
 freq: 2437
 RX: 59639 bytes (440 packets)
 TX: 12188 bytes (76 packets)
 signal: -58 dBm
 tx bitrate: 65.0 MBit/s MCS 7

 bss flags: short-preamble short-slot-time
 dtim period: 3
 beacon int: 100

要求DHCP伺服器配發動態IP
# dhclient wlan0

2015/7/20

在Linux系統上製作Windows的USB安裝隨身碟(EFI支援的GPT格式)

不論在Linux或是Windows上要製作Linux安裝隨身碟都有現成套件可以輕易完成(如UNetbootin),但反過來要在Linux系統上製作Windows安裝隨身碟就沒這麼容易了,但也只需要幾個步驟就可以了!
目前還是EFI跟Legacy BIOS的接棒時期,安裝隨身碟也就分為EFI支援的GPT格式跟Legacy BIOS的MBR格式,這篇文章方法僅適用GPT格式。

先安裝gparted套件對隨身碟進行磁區分割
sudo apt-get install gparted
sudo gparted
  1. 在右上角下拉式選單中,選擇隨身碟裝置。
  2. 卸載隨身碟裝置
    功能列選單:Partition > Unmount
  3. 建立Partition表格
    功能列選單:Device > Create Partition Table ...
    選擇gpt
  4. 建立分割區
    功能列選單:Partition > New
    選擇fat32
  5. 儲存變更
    Ctrl + Enter
  6. 關閉gparted
掛載windows安裝印象檔(*.iso)並將內容複製到隨身碟
sudo mkdir /mnt/iso /mnt/usb
sudo mount *.iso /mnt/iso
sudo mount /dev/sdXY /mnt/usb
sudo cp -R /mnt/iso/* /mnt/usb

完成上述步驟後就可以重新開機,並將開機裝置指定到隨身碟,就會進入Windows安裝界面了。目前驗證過Windows7(64-bits)與Windows8(64-bits)均能成功進入安裝畫面。

2015/7/16

【治具】RS232 迴路測試每根Pin腳功能

RS232是個歷久不衰的訊號通訊協議,雖然在主流電腦上已經很難看見其蹤跡,但在伺服器或是嵌入式裝置乃至於近年很夯的物連網裝置都很容易見到,這篇文章主要記錄如何設計硬體治具並撰寫對應測試程式。

首先可以先參考RS232每根Pin腳的功能用途,網路上有大量文章介紹,就不再贅述,可以參考連結:Ethernut RS-232 Primer

先看做好的治具成品圖

治具硬體Rework
  1. 使用4.7kOhm電阻將TxD與RxD串接起來,如果只需要驗證資料傳輸只需做這部分重工即可,大部分情況下(未使用Hardware Flow Control)其他Pin腳都不會用到。
  2. 使用4.7kOhm電阻將DCD與DSR串接起來。
  3. 將DSR跟DTR直接短路串接。
  4. 使用4.7kOhm電阻將RI與CTS串接起來。
  5. 將CTS跟RTS直接短路串接。
測試概念
  1. 從TX發送資料,硬體TxD與RxD已經串接,因此會收到送出資料,確認收到資料並比對資料內容即可確認TxD與RxD腳位功能是否正常。
  2. 硬體RTS與CTS及RI串接,因此設置/重置RTS訊號,再讀取CTS與RI訊號即可確認這三根腳位功能是否正常。
  3. 硬體DTR與DSR及DCD串接,因此設置/重置DTR訊號,再讀取DSR與DCD訊號即可確認這三根腳位功能是否正常。
程式碼實做
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// File name: uart.c
// gcc -o uart uart.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>

int main (int argc, char *argv[])
{
 int ret = 0;
 int len;
 unsigned char buf[16] = {0xAA, 0x55, 0x00, 0xFF,
    0x5A, 0xA5, 0xF0, 0x0F,
    0x01, 0x02, 0x04, 0x08,
    0x10, 0x20, 0x40, 0x80};
 unsigned char buf_rev[16];
 int fd;
 FILE *fp = NULL;
 int status;
 struct termios tio;
 
 if (argc <= 1) {
  fprintf(stderr, "Usage: %s <dev>\n", argv[0]);
  ret = 255;
  goto end;
 }

 fp = fopen (argv[1], "w+");
 if (fp == NULL) {
  puts ("Fail to open device");
  ret = 255;
  goto end;
 }
 
 printf ("+ TX: ");
 fflush (fp);
 len = fwrite (buf, sizeof (char), 16, fp);
 fflush (fp);
 if (len < 16) {
  puts ("Fail to transfer data");
  ret = 255;
  goto clean;
 }
 puts ("Pass");

 printf ("+ RX: ");
 len = fread (buf_rev, sizeof (char), 16, fp);
 if (len < 16) {
  puts ("Fail to receive data");
  ret = 255;
  goto clean;
 } else if (memcmp (buf, buf_rev, 16) != 0) {
  puts ("Check data fail");
  ret = 255;
  goto clean;
 }
 puts ("Pass");
 fclose (fp);
 fp = NULL;

 if ( (fd = open (argv[1], O_RDWR)) < 0 ) {
  puts ("Fail to open device");
  ret = 255;
  goto end;
 }

 tcgetattr(fd, &tio);
 printf ("RTS -> CTS - RI: ");
 status |= TIOCM_RTS;
 ioctl(fd, TIOCMSET, &status);
 ioctl(fd, TIOCMGET, &status);
 ret = status & (TIOCM_CTS | TIOCM_RNG); // Should be 0xA0

 status &= ~TIOCM_RTS;
 ioctl(fd, TIOCMSET, &status);
 ioctl(fd, TIOCMGET, &status);
 if ( (status & (TIOCM_CTS | TIOCM_RNG)) || ret != 0xA0) { // Status should be 0x00, false
  puts ("FAIL");
  if (ret == 0x0)
   puts ("  --> Case1: RTS fail; Case2: CTR & RI fail");
  else if (ret == 0x80)
   puts ("  --> CTS fail");
  else if (ret == 0x20)
   puts ("  --> RI fail");
  else
   puts ("  -> RTS fail");
  ret = 6;
  goto clean;
 } else {
  puts ("PASS");
  ret = 0;
 }

 printf ("DTR -> DSR - DCD: ");
 status |= TIOCM_DTR;
 ioctl(fd, TIOCMSET, &status);
 ioctl(fd, TIOCMGET, &status);
 ret = status & (TIOCM_DSR | TIOCM_CAR); // Should be 0x140
 status &= ~TIOCM_DTR;
 ioctl(fd, TIOCMSET, &status);
 ioctl(fd, TIOCMGET, &status);
 if ( (status & (TIOCM_DSR | TIOCM_CAR)) || ret != 0x140) { // Status should be 0x00, false
  puts ("FAIL");
  if (ret == 0x0)
   puts ("  --> Case1: DTR fail; Case2: DSR & DCD fail");
  else if (ret == 0x100)
   puts ("  --> DCD fail");
  else if (ret == 0x40)
   puts ("  --> DSR fail");
  else
   puts ("  -> RTS fail");
  ret = 7;
  goto clean;
 } else {
  puts ("PASS");
  ret = 0;
 }

clean:
 if (fp)
  fclose (fp);
 if (fd > 0)
  close (fd);
end:
 printf("Return Code: %d\n", ret);
 return ret;
}

參考連結

2015/7/8

Trace shell script的小技巧

參考上圖,有三個script,先執行test1.sh,會帶起test2.sh;test2.sh會再帶起test3.sh,如果從test1.sh開始了解程式架構,很容易就可以得知彼此之間的執行關係;但有時候線頭只抓到test2.sh或是test3.sh時就麻煩了。
  在Linux下可以使用bash maintain的$PPID變數再搭配ps指令就可以慢慢推敲出script file的呼叫關係。

直接看範例:
#!/bin/sh
# file: test1.sh

echo $0
echo "Execute by `ps -o comm -h --pid $PPID`"
echo ===========
./test2.sh

#!/bin/sh
# file: test2.sh

echo $0
echo "Execute by `ps -o comm -h --pid $PPID`"
echo ===========
./test3.sh

#!/bin/sh
# file: test3.sh

echo $0
echo "Execute by `ps -o comm -h --pid $PPID`"
echo ===========

執行結果:
./test1.sh
Execute by bash
===========
./test2.sh
Execute by test1.sh
===========
./test3.sh
Execute by test2.sh
===========