Thursday, November 08, 2007

Indent - Beautify your C source code

Introduction

GNU Indent is a GNU tool used to make code easier to read. It can also convert from one style of writing C to another.

Default Parameters file

indent will use the ~/.indent.pro file as the default parameter file. My content of the .indent.pro is:
-bad -bap -bbb -bbo -nbc -br -bli0 -bls -c33 -cd33 -ncdb -ncdw -ce -cli0 -cp33 -cs -d0 -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l75 -lp -npcs -nprs -npsl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -ut
Reference:
http://linux.die.net/man/1/indent

Monday, November 05, 2007

ARM GCC inline assembly

Introduction

Just now I want to print the ARM CP15 registers' value in a C function, I know I need the inline assembly technology. After read some codes in linux kernel and searched the internet, I have got some simple idea.

Examples
Read ARM ID code:
u32 val;
__asm__ __volatile__ ("mrc p15, 0, %0, c0, c0, 0" : "=r" (val));
printk("ARM ID code: 0x%x\n", val);
Write TTB:
u32 val
/*... */
__asm__ __volatile__ ("mcr p15, 0, %0, c2, c0, 0" : : "r" (val));
For more information, see:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

ARM coprocessor CP15 Summary

Introduction
* CP15 is a coprocessor of ARM chip, it used to configure and control the ARM core modules - The caches, Tightly-Coupled Memories (TCMs), Memory Management Unit (MMU), and most other system options.

Instructions
* You can only access CP15 registers with MRC and MCR instructions in a privileged mode.
mcr{cond} p15, <Opcode_1>, <Rd>, <CRn>, <CRm>, <Opcode_2>
mrc{cond} p15, <Opcode_1>, <Rd>, <CRn>, <CRm>, <Opcode_2>
- The Opcode_1 and Opcode_2 field Should Be Zero except when the values specified are used to select the desired operations.
- The CRm field should be c0 except when the the register specified are used to select the desired behavior.

Registers
* c0~c15
- c0: used to read ID code/Cache type/TCM status
> read ID code:
mrc p15, 0, <Rd>, c0, c0, 0
> read Cache type:
mrc p15, 0, <Rd>, c0, c0, 1
> read TCM status:
mrc p15, 0, <Rd>, c0, c0, 2
- c1: used to enable/disable caches and MMU
> read control register:
mrc p15, 0, <Rd>, c1, c0, 0
> write control register:
mcr p15, 0, <Rd>, c1, c0, 0
- c2: r/w Translation Table Base (TTB)
mrc p15, 0, <Rd>, c2, c0, 0; read TTBR
mcr p15, 0, <Rd>, c2, c0, 0; write TTBR
- c3: Domain access control
mrc p15, 0, <Rd>, c3, c0, 0; read domain access permissions
mcr p15, 0, <Rd>, c3, c0, 0; write domain access permissions
- c4: Reserved in ARM926EJ-S
- c5: Data fault status/Instruction fault status
- c6: Fault address
- c7: Cache operations
mrc p15, 0, <Rd>, c7, c7, 0; Invalidate ICache and DCache
mcr p15, 0, <Rd>, c7, c5, 0; Invalidate ICache
mcr p15, 0, <Rd>, c7, c5, 1; Invalidate ICache single entry (MVA)
mcr p15, 0, <Rd>, c7, c5, 2; Invalidate ICache single entry (Set/Way)
mcr p15, 0, <Rd>, c7, c13, 1; Prefetch ICache line (MVA)
mcr p15, 0, <Rd>, c7, c6, 0; Invalidate DCache
mcr p15, 0, <Rd>, c7, c6, 1; Invalidate DCache single entry (MVA)
mcr p15, 0, <Rd>, c7, c6, 2; Invalidate DCache single entry (Set/Way)
mcr p15, 0, <Rd>, c7, c10, 1; Clean DCache single entry (MVA)
mcr p15, 0, <Rd>, c7, c10, 2; Clean DCache single entry (Set/Way)
mrc p15, 0, <Rd>, c7, c10, 3; Test and clean DCache
mcr p15, 0, <Rd>, c7, c14, 1; Clean and invalidate DCache entry (MVA)
mcr p15, 0, <Rd>, c7, c14, 2; Clean and invaliate DCache entry (Set/Way)
mrc p15, 0, <Rd>, c7, c14, 3; Test, clean, and invalidate DCache
mcr p15, 0, <Rd>, c7, c10, 4; Drain write buffer
mcr p15, 0, <Rd>, c7, c0, 4; Wait for interrupt
> Clean the entire DCache:
tc_loop:
mrc p15, 0, r15, c7, c10, 3; test and clean, pc is not changed by this instrunction
bne tc_loop
> Clean and invalidate the entire DCache:
tci_loop:
mrc p15, 0, r15, c7, c14, 3; test clean and invalidate
bne tci_loop
- c8: (W) TLB operations
- c9: Cache lockdown/TCM region
mrc p15, 0, <Rd>, c9, c0, 0; read DCache lockdown register
mcr p15, 0, <Rd>, c9, c0, 0; write DCache lockdown register
mrc p15, 0, <Rd>, c9, c0, 1; read ICache lockdown register
mcr p15, 0, <Rd>, c9, c0, 1; write ICache lockdown register
- c10: TLB lockdown
- c11/c12: Reserved in ARM926EJ-S
- c13: FCSE PID/Context ID
- c14: Reserved in ARM926EJ-S
- c15: Test configuration