2017年3月10日星期五

BASH reference

#!/bin/bash    #tells *nix BASH should be used to run it

Special Parameters
$#    #Store the number of arguments passed from the command line
$1    #first parameter
$?    #Store the exit status of the last executed command
$_    #Print the last argument of the previous command
$$    #Return the process ID of the shell
$!     #Return the process ID of the last executed background process

${#var}    #Number of characters in $var
${#array} #The length of the first element in the array.

${para}  #same as $para. May be used for concatenating variables with strings.
${para-default},${para:-default}   #if para not set, use default
${para=default},${para:=default}    #if para not set, set it to default
${parameter+alt_value}, ${parameter:+alt_value}    #If parameter set, use alt_value, else use null string.
${parameter?err_msg}, ${parameter:?err_msg}If parameter set, use it, else print err_msg and abort the script with an exit status of 1.

The : makes a difference only when parameter has been declared and is null


$*     #* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@    #@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

For more details, please check:
http://tldp.org/LDP/abs/html/special-chars.html

[ is a synonym for test command. Even if it is built in to the shell it creates a new process.

[[ is a new improved version of it, which is a keyword, not a program.

Conditional Expression
[expr1 -ne expr2]    #Return true if expr1 is not equal to expr2
[expr1 -eq expr2]    #Return true if expr1 is equal to expr2
[expr1 -gt expr2]    #Return true if expr1 is greater than expr2
[expr1 -ge expr2]    #Return true if expr1 is greater than or equal to expr2
[expr1 -lt expr2]     #Return true if expr1 is less than expr 2
[expr1 -le expr2]    #Return true if expr1 is less than or equal to expr2
[-z  expr]    #Return true if the expression is null or empty
[expr =~ regular_expr]    #Return true if the regular expression is matched.
[expr1 -a expr2],[expr1]&&[expr2]    #Return true if both the expression is and
[expr1 -o expr2],[expr1]||[expr2]   #Return true if either of the expr1 or expr2  is true

[-a filepath],[-e filePath]    #Return true if file exists
[-f  filepath]   #Return true if it is file
[-d directory]    #Return true if it is directory
[-L filepath],[-h filePath]    #Return true if file is a symbolic link
[-S socket]    #Return true if file exists and socket file
[-b filepath]  #Return true if file is a block device
[-c filepath]  #Return true if file is a char device

[-r filepath]   #Return true if file is readable
[-w filepath]    #Return true if file is writable
[-x filepath]    #Return true if file is executable
[-u filepath]    #Return true if SUID is set
[-g filepath]    #Return true if SGID is set
[-k filepath]    #Return true if sticky bit is set
[-s filepath]    #Return true if file exists and has a size greater than 0

Number Notation
echo $((0xFFFF))          #Hex, Display 65535
echo $((032))                 #Octal, Output 26
echo $((2#11111111))  #Binary, Output 255

Frequency Used Command
source filepath    #executes the contents of a script in the current shell

2017年3月7日星期二

Dump the symbol table

objdump -TC ld-linux-x86-64.so.2

What's inside initrd (x86_64)

1 File Only - GenuineIntel.bin
Suspect extraction failed.
Get the answer from stackexchange with few modification.

http://unix.stackexchange.com/questions/163346/why-is-it-that-my-initrd-only-has-one-directory-namely-kernel

$file initrd.img
initrd.img: ASCII cpio archive (SVR4 with no CRC)
$mkdir initTree && cd initTree
$cpio -idv < ../initrd.img
`-- kernel
    `-- x86
        `-- microcode
            `-- GenuineIntel.bin
The cpio block skip method given doesn't work reliably. That's because the initrd images didn't have both archives concatenated on a 512 byte boundary.

Instead, do this:

apt-get install binwalk
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             ASCII cpio archive (SVR4 with no CRC), file name: "kernel", file name length: "0x00000007", file size: "0x00000000"
120           0x78            ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86", file name length: "0x0000000B", file size: "0x00000000"
244           0xF4            ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode", file name length: "0x00000015", file size: "0x00000000"
376           0x178           ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode/GenuineIntel.bin", file name length: "0x0000002A", file size: "0x00005400"
22032         0x5610          ASCII cpio archive (SVR4 with no CRC), file name: "TRAILER!!!", file name length: "0x0000000B", file size: "0x00000000"
22528         0x5800          gzip compressed data, from Unix, last modified: 2017-03-06 14:00:21
10181078      0x9B59D6        MySQL ISAM index file Version 3
 
dd if=initrd.img bs=22528 skip=1 | gunzip | cpio -id
1778+1 records in
1778+1 records out
40073444 bytes (40 MB, 38 MiB) copied, 3.15216 s, 12.7 MB/s
209980 blocks
 ls -l
total 39208
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 bin
drwxr-xr-x  3 yip yip     4096 Mar  7 21:36 conf
drwxr-xr-x 10 yip yip     4096 Mar  7 21:36 etc
-rwxr-xr-x  1 yip yip     6907 Mar  7 21:36 init
-rw-r--r--  1 yip yip 40095972 Mar  7 21:30 initrd.img
drwxr-xr-x  9 yip yip     4096 Mar  7 21:36 lib
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 lib64
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 run
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 sbin
drwxr-xr-x  7 yip yip     4096 Mar  7 21:36 scripts
drwxr-xr-x  4 yip yip     4096 Mar  7 21:36 usr
drwxr-xr-x  4 yip yip     4096 Mar  7 21:36 var

2015年10月29日星期四

USBUART module in PSoC5

I currently try to use the CYPRESS PSoC5 (CY8C5888P) chip to capture the signals and send to PC for further analysing in real time. Found that the USBUART module inside this chip is petty good.

Advantages:
1) Automatically generate the INF file. (Act as Virtual COM Port in PC, use usbser.dll, CDC Device)

2) Great data throughput. It can reach minimum ~1.7Mb/s. (Manual DMA mode + triple buffering were used to handle the sampling data, act as Virtual COM Port in PC).

3) Built-in configuration panel. Easy to use.

4) The source code which is generated by PSoC creator is tiny and clear (well optimised). Easy to understand.

5) Examples and Documents are provided.


In order to archive high data throughput, the key points are:
1) use DMA mode if possible.

2) Familiar with HDL is an advantage. You can describe your hardware module using schematic / HDL in this chip.

3) In the PC side, choose the right RS-232 terminal.

I have tried ReadTerm and Bray++ terminal.
I will give Bray++ terminal as 3.5/5
and RealTerm as 4.5/5 (-0.5 because of the user interface...)

I found that some received "block" are missing in Bray++ terminal (It does not support >1,000,000bps well ?!).
RealTerm does not have this issue so far.


2015年10月16日星期五

Capture USB traffic in windows 8

USBPCap + Wireshark

1) Execute USBPcapCMD.exe, check the target device node.
2) Select the target device node, start to capture the file (.pcap).
3) View the pcap file by using Wireshark

For more information, please check:
http://desowin.org/usbpcap/tour.html

2015年9月26日星期六

Build the u-boot for raspberry pi 2

Cross compile in ubuntu environment.

1) Download required arm compiler.
I choose the most common one for testing only (not the optimized one).
sudo apt-get install gcc-arm-linux-gnueabi

ncurses-dev is required if you want to config the option by using 'make menuconfig'.
sudo apt-get install ncurses-dev


Get the u-boot package
2) git clone git://git.denx.de/u-boot.git

build the u-boot backage
3) make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- rpi_2_defconfig
Optional) make menuconfig

4) make -j8 V=1
Option) make -j8 V=1 2>&1 | tee build.log | less
Option) make -j8 SHELL='sh -x'
V=1: Output more message during build


u-boot.bin will be generated under the u-boot root directory.

Copy the u-boot.bin to the micro SD-CARD

2015年9月11日星期五

Connect STM32F103V8 with ECLIPSE+GDB+JLINK

Use SWD debugging interface.

1) Start the JLINK GDB Server
cd "c:\Program Files (x86)\SEGGER\JLinkARM_V440"
JLinkGDBServer.exe -if swd



2) In the debug configuration:



monitor flash download = 1
monitor flash breakpoints = 1

monitor reg r13 = (0x00000000)
monitor reg pc = (0x00000004)

2015年8月21日星期五

ESP8266 Extend AT command set - Part1

I need to try their SDK example first in order to extend the AT command set.

1) Download the required tools
  a) Download the Lubuntu VirtualBox image which is provided by Espressif.
     (Provide compiler tool-chain already)
  b) Download the latest SDK.
  c) Download the FLASH programming tools. (For Windows)

2) According to the readme file, copy the SDK to the share folder.
e.g. Place in D:\Share
After that, setup the share folder path in VirtualBox (D:\Share <-> Share).
VBoxImage default username: esp8266
VBoxImage default password: espressif
{SDK} means ~/Share/esp_iot_sdk_v1.3.0

3) Copy {SDK}/examples/at/  to  {SDK}/app/

4) In {SDK}/app/user/user_main.c
modify to
void ICACHE_FLASH_ATTR
at_testCmdTest(uint8_t id)
{
    uint8 buffer[32] = {0};

    os_sprintf(buffer, "%s\r\n", "my at_testCmdTest");
    at_port_print(buffer);
    at_response_ok();
}

4) cd {SDK}/app/

5) ./gen_misc.sh
Select below options:
boot_v1.2+
user1.bin
spi speed: default
spi mode: default
spi size and map: 1024KB( 512KB+ 512KB)

6) Download the user1.1024.new.?.bin to the FLASH ---> 0x1000


7) Try to run AT+TEST=? in CuteCOM.
it should return my at_testCmdTest.

ESP8266 - A Great Product

In ESP8266 SDK,
Something that interests me:
1) Support RTC.
2) SDK provides several functions to get the time using SNTP protocol.
3) Support SSL (Only allow single client connection only)
The default value of maximum SSL packet size is 2KB.
The maximum size packet can be adjusted.

It is good enough for a single IOT SOC chip (Just HKD 12)...

Then, focus on the SSL, found one more interesting thing...
Someone lists out the object symbols in his blog:)
http://41j.com/blog/2015/01/esp8266-sdk-library-symbols/

Many standardized cryptography algorithms (RSA, SHA1, MD5, AES..) have already provided in the object library files,
but they are not explicitly exposed by the vendor.

In other words, once we disassemble the code,
and trace how the parameters passed in these functions (XTENSA ABI).
Actually, it is not necessary to re-implement the cryptography function (greatly reduce the code size).

ESP8266 Initial Test

The module I use:  ESP-8266 ESP-01

1) Wire Connection
Since the USB to UART TTL module may not provide sufficient power for ESP-01,
use external power supply is recommended.

3.3V <-> ESP8266 VCC
3.3V <-> ESP8266 CH_PD
USB to UART TTL module RX <-> ESP8266 TX
USB to UART TTL module TX <-> ESP8266 RX
GND <-> USB to UART TTL module GND <-> ESP8266 GND

*: GPIO0 should be connected to GND if we want to update the firmware. Reboot is required before update.
For general purpose, GPIO0 can be float.

==========================================

2) Testing

In Linux environment, CuteCOM is recommended for testing the communication.

Below is the setup procedures:

Device: /dev/ttyUSB0
Baud rate: 115200
Data bits: 8
Stop bits: 1
Parity: None
Handshake: uncheck both
linefeed: default should be set to CR LF (sometimes you need to switch to use HEX Input format e.g. send raw byte)

Open the COM port and listen the response.
reboot the ESP-01 module.
It should return [Ready] string.

P.S.:
Please update to the latest firmware before use:
The latest firmware uses 115200, 8N1, AT COMMAND LINE END - CR LF for communication.
Before update the firmware, we need to download the SDK and the FLASH PROGRAMMING TOOLS first.

More details can be found in SDK documentation.



In CuteCOM, enter the following commands.
Input AT
response OK means success.

Input AT+CWMODE=1
For STATION MODE only . (3=STATION MODE+ SOFTAP MODE)

Input AT+CWLAP
List available access point.

============================================

3) NTP Client Implementation

Get current time stamp using NTP

#RESET
AT+RST

#GET FIRMWARE VER
#AT+GMR

#SET TO STATION MODE
AT+CWMODE=1

#LIST AP
AT+CWLAP

#CONNECT ACCESS POINT
AT+CWJAP="SSID","Password"

#MAKE CONNECTION.
#參考自:https://github.com/sandeepmistry ... lient/NTPClient.ino
#Other NTP Server might not work...
AT+CIPSTART="UDP","129.6.15.28",123

#SEND NTP PACKET
AT+CIPSEND=48

IN CuteCOM, change to HEX input.
Input the following:
E30006EC0000000000000000314E31340000000000000000000000000000000000000000000000000000000000000000

It should return 48 bytes packet.