Thursday, May 11, 2017

install cross-compiler for raspberry pi 3 in Arch Linux

install cross-compiler for raspberry pi 3 in Arch Linux
pacman -S aarch64-linux-gnu-gcc
install bcm2835-1.52
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.52.tar.gz
tar xzvf bcm2835-1.52.tar.gz
cd bcm2835-1.52
./configure CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ CPP=aarch64-linux-gnu-cpp --host=aarch64
make

Sunday, January 5, 2014

go 的反射以及injection 设计

函数拦截设计
func Inject(fptr,target interface{},before,after func([]reflect.Value)[]reflect.Value){
        wrap := func(in []reflect.Value) []reflect.Value{
                var b []reflect.Value
                if before == nil {
                        b = in
                } else {
                        b = before(in)
                }
                v := reflect.ValueOf(target)
                r := v.Call(b)
                if after == nil {
                        return r
                } else {
                        return after(r)
                }
        }
        fn := reflect.ValueOf(fptr).Elem()
        v := reflect.MakeFunc(fn.Type(),wrap)
        fn.Set(v)
}
使用实例:
func say(word string)string{
        fmt.Println(word)
        return word
}
func main(){
        var impl func(string)string
        m :=xx{}

        Inject(&impl,say,
                func(in []reflect.Value)[]reflect.Value{
                        fmt.Println("before")
                        return in
                },
                func(out []reflect.Value)[]reflect.Value{
                        fmt.Println("after")
                        vo := "new"
                        return []reflect.Value{reflect.ValueOf(vo)}
                })
        s:=impl("old")
}
运行结果:
before
old
after
new

Monday, September 23, 2013

在linux安装 CH340/341

安装CH340/341的串口驱动时,在网上搜索到很多要自己下载源代码编译安装的方法。其实最新版本的ubuntu已经完美支持。 第一步,先找到USB设备,使用命令
lsusb
Bus 001 Device 002: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 007 Device 003: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 008 Device 002: ID 1bcf:0002 Sunplus Innovation Technology Inc. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
找到usbserial设备的信息,这里是bus 007 Bus 007 Device 003: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter 然后装入usbserial模块, 根据结果修改vendor和product参数
sudo modprobe usbserial vendor=0x1a86 product=0x7523
最后看看是否成功安装
lsmod | grep usbserial
返回结果
usbserial               37161   1 ch341
有上面一行信息意味安装成功 参考:http://forums.reprap.org/read.php?12,4546

Thursday, May 16, 2013

quine

python
a='a=%s; print( a %% repr(a))'; print( a % repr(a))
go
/* Go quine */
package main
import "fmt"
func main() {
 fmt.Printf("%s%c%s%c\n", q, 0x60, q, 0x60)
}
var q = `/* Go quine */
package main
import "fmt"
func main() {
 fmt.Printf("%s%c%s%c\n", q, 0x60, q, 0x60)
}
var q = `
refer to http://research.swtch.com/zip

Friday, April 26, 2013

compiler optimizations

编译优化
有一下一段简单代码
#define LED2=0x04;
#define LED3=0x40;
void toggle_led(void){
static int idx=0;
    if((++idx)&1)
        __REG_B(GPKDAT) = LED2;
    else
        __REG_B(GPKDAT) = LED3;
}
优化编译后反汇编得到的代码是:
//omit the code of testing idx,
        movne r2,#LED2
        ldrne r3,=GPKDAT
        strbne r2,[r3]
        moveq r2,#LED3
        ldreq r3,=GPKDAT
        strbeq r2,[r3]
        bx lr
我估计在编译的时候,这里的语句没办法优化因此会 3,4,6,7 行的多余出来。其实这4行用2行代码就可以了。 修改了C代码为
#define LED2=0x04;
#define LED3=0x40;
void toggle(void){
        static int idx=0;
        unigned char temp;
        if((++idx)&1)
                temp = LED2;
        else
                temp = LED3;
        __REG_B(GPKDAT) = temp;
}
代码编译后反编译:
//omit the code of testing idx
        moveq r3,#LED2
        movne r3,#LED3
        ldr  r2,=GPKDAT
        strb r3,[r2]

估计是因为宏__REG_B中使用了volatile,因此对无法代码进行优化。

Thursday, April 25, 2013

纪念驾照考试全通

最后科目4以92分优异的成绩通过。好像月底可以拿驾照。