Simple Makefile used for SDCC 8051 C Compiler

因工作需要,做了個簡單的 makefile

#
# SDCC Makefile
#
# Memory Layout
#   Program Size: 4KB
#   Internal Mem: 256B
#   External Mem: 4KB


# ------------------------------------------------------
# PATH

INCDIR  = ../inc
SRCDIR  = ./src
OBJDIR  = ./obj

LIBDIR  =  

# ------------------------------------------------------
# UTILITY

UTILDIR = ../../util
HEX2BIN = $(UTILDIR)/hex2bin/bin/hex2bin

# ------------------------------------------------------
# Target and Source

TARGET = prom
OBJ = 
	$(OBJDIR)/printf.c.rel      
	$(OBJDIR)/debug.c.rel      
	$(OBJDIR)/gpio.c.rel       
	$(OBJDIR)/spi.c.rel        
	$(OBJDIR)/s25fl040a.c.rel  
	$(OBJDIR)/main.c.rel       
	$(OBJDIR)/crtstart.asm.rel

# ------------------------------------------------------
# SDCC

CC = sdcc
AS = sdas8051

# ------------------------------------------------------
# Memory Layout

# PRG Size = 4K Bytes
CODE_SIZE = --code-loc 0x0000 --code-size 4096

# INT-MEM Size = 256 Bytes
IRAM_SIZE = --idata-loc 0x0000  --iram-size 256

# EXT-MEM Size = 4K Bytes
XRAM_SIZE = --xram-loc 0x0000 --xram-size 4096

# ------------------------------------------------------
# MCS51 Options

# Memory Model (small, medium, large, huge)
MODEL  = medium

#LIBS    = 
#LIBPATH = -L $(LIBDIR)

#DEBUG = --debug
AFLAGS = -l -s
CFLAGS = -I./inc -I$(INCDIR) -mmcs51 --model-$(MODEL) --out-fmt-ihx --no-xinit-opt $(DEBUG)
LFLAGS = $(LIBPATH) $(LIBS) -mmcs51 --model-$(MODEL) $(CODE_SIZE) $(IRAM_SIZE) $(XRAM_SIZE) --out-fmt-ihx  $(DEBUG)

# ------------------------------------------------------
# S = @

.PHONY: all util clean
.SECONDARY:

all: util $(OBJDIR)/$(TARGET).memh

util:
	(cd $(UTILDIR)/hex2bin; make)

%.memh: %.bin
	$(S) hexdump -v -e '"@%04.4_ax " 16/1 "%02x " "n"' $^ > $@

%.bin: %.hex
	$(S) $(HEX2BIN) -s 0 -l 1000 -p ff $^

%.hex: %.ihx
	$(S) packihx $^ > $@

%.ihx: $(OBJ)
	$(S) $(CC) -o $@ $(LFLAGS) $^

$(OBJDIR)/%.c.rel: $(SRCDIR)/%.c
	$(S) $(CC) -o $@ $(CFLAGS) -c $^

$(OBJDIR)/%.asm.rel: $(SRCDIR)/%.asm
	$(S) $(AS) $(AFLAGS) -o $@ $^ 

clean:
	$(S) rm -rf $(OBJDIR)/*
	(cd $(UTILDIR)/hex2bin; make clean)

hex2bin 的用法

直接執行不加參數即可得到使用方法:

$./hex2bin 
hex2bin v1.0.10, Copyright (C) 2012 Jacques Pelletier & contributors


usage: hex2bin [OPTIONS] filename
Options:
  -s [address]  Starting address in hex (default: 0)
  -l [length]   Maximal Length (Starting address + Length -1 is Max Address)
                File will be filled with Pattern until Max Address is reached
                Length must be a power of 2 in hexadecimal:
                     Hex      Decimal
                    1000   =    4096   (4ki)
                    2000   =    8192   (8ki)
                    4000   =   16384  (16ki)
                    8000   =   32768  (32ki)
                   10000   =   65536  (64ki)
                   20000   =  131072 (128ki)
                   40000   =  262144 (256ki)
                   80000   =  524288 (512ki)
                  100000   = 1048576   (1Mi)
                  200000   = 2097152   (2Mi)
                  400000   = 4194304   (4Mi)
                  800000   = 8388608   (8Mi)
  -e [ext]      Output filename extension (without the dot)
  -c            Enable hex file checksum verification
  -p [value]    Pad-byte value in hex (default: ff)

  -k [0|1|2]    Select checksum type
                       0 = 8-bit,
                       1 = 16-bit little endian,
                       2 = 16-bit big endian
  -r [start] [end]     Range to compute checksum over (default is min and max addresses)
  -f [address] [value] Address and value of checksum to force

舉來說,我用 sdcc 編譯所得到的檔案為 hello.ihx (Intel-HEX format),
為了得到 hello.bin 所用命令如下:

$ hex2bin -s 0000 -l 1000 -p ff hello.ihx
hex2bin v1.0.10, Copyright (C) 2012 Jacques Pelletier & contributors

Lowest address  = 00000000
Highest address = 00000FFF
Pad Byte        = FF
8-bit Checksum = 1E
$  

“-s 0000 -l 1000 -p ff” 說明了:
起始位置: 0x0000,長度:0x1000,HEX File 中未定義的的值一律填入 0xff
並自動計算出 8-bit checksum = 0x1E (只是將 bin file 中所有的值加總)

有另外一種用法,
如果你想指定 8-bit checksum 的值(例如0x55),
例如 boot loader 從 Serial Flash 將 hello.bin 讀出來後,
要先做簡單的 checksum 檢查無誤之後,才進行下一個動作,

命令如下;

$ hex2bin -s 0000 -l 1000 -p ff -k 0 -f 0fff 55 hello.ihx
hex2bin v1.0.10, Copyright (C) 2012 Jacques Pelletier & contributors

Lowest address  = 00000000
Highest address = 00000FFF
Pad Byte        = FF
8-bit Checksum = 25
Addr 00000FFF set to 2F
$ 

這命令會在 位置 0x0fff 填入 0x2f
使得 8-bit checksim 從原先的 0x25 變成指定的 0x55

compile 32-bit hex2bin application on 64-bit Linux OS

因工作上需要 hex2bin 的工具 又懶得自己寫
在網路上發現一個寫的還不錯的 hex2bin source code : hex2bin-1.0.9

其中包含了兩個程式 hex2bin and mot2bin
但使用時出現了錯誤的情形,
原來是 compile 時必須是 32-bit 架構,而我卻是在 64-bit Linux 下編譯的,
為了解決這問題,
首先安裝 32-bit gcc compiler

$ sudo zypper in libstdc++47-devel-32bit
$ sudo zypper in gcc47-32bit 

接下來修改 hex2bin 的 Makefile,在 gcc 後面加上 -m32 參數
重新編譯後即可正常使用!

OpenVPN and NAT

在公司用 OpenSuSE 架設了一個 OpenVPN Server
做好之後,發現從家裡除了 VPN Server 之外
其他與 VPN Server 同一網段的機器都連不上
後來發現,原來還需要設定 NAT!
首先編輯 NAT 設定擋

$ vi /root/openvpn_nat.sh
#! /bin/bash

# 清除所有規則
iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -Z

# 定義防火牆政策
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT

# 讓 OpenVPN 能進去 LAN
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -s 10.8.0.0/24 -j MASQUERADE

測試OK,其他的機器都可以連上了!
接下來更改設定擋,以後不需要在手動啟動了

$ vi /etc/init.d/boot.local
# NAT for OpenVPN
/root/openvpn_nat.sh

重新開機後,測試OK!
搞定,收工!