ARM 系列 -- FS2410 開發(fā)板上通過串口實現 printf
一、目的
到目前為止我們所編寫的程序都是直接燒到裸板(FS2410)上運行,沒有借助操作系統(tǒng),如果哪個環(huán)節(jié)出錯了,就只能揣測代碼的邏輯,無法借助 GDB 調試,這無形增加了編寫代碼的難度,如果任意時刻我們能把某個變量的值打印出來多好啊...呵呵, 你也許有同樣的困惑,上一個實驗我們對 UART 串口編程實現了對超級終端接收和發(fā)送數據,也許我們可以編寫一個類似 C 語言里的 printf,作用就是向上位機的超級終端發(fā)送我們指定的任何數據。懷著這樣美好的愿望,隨我一起踏上征程吧。
二、代碼
很多代碼是在前面幾個實驗的基礎上進行整理復用之,更多細節(jié)請參考前面隨筆,這里
僅附簡略注解。
@ 文件 head.s
@ 作用:關閉看門狗、SDRAM 的初始化設置、搬移 Nand Flash 4K 以后
@ 的代碼到 SDRAM 的指定位置、執(zhí)行 SDRAM 中的代碼
.text
.global _start
_start:
ldr r0, =0x53000000@ Close Watch Dog Timer
mov r1, #0x0
str r1, [r0]
bl memory_setup@ Initialize memory setting
bl flash_to_sdram@ Copy code to sdram
ldr sp, =0x34000000@ Set stack pointer
ldr pc, =main @ execute the code in SDRAM
@ 文件 mem.s
@ 作用:SDRAM 的初始化設置
@ 關于初始化的更多細節(jié),請參考我前面的隨筆
.global memory_setup
memory_setup:
mov r1, #0x48000000
adrl r2, mem_cfg_val
add r3, r1, #13*4
1:
@ write initial values to registers
ldr r4, [r2], #4
str r4, [r1], #4
cmp r1, r3
bne 1b
mov pc, lr
.align 4
mem_cfg_val:
.long 0x22111110 @ BWSCON
.long 0x00000700 @ BANKCON0
.long 0x00000700 @ BANKCON1
.long 0x00000700 @ BANKCON2
.long 0x00000700 @ BANKCON3
.long 0x00000700 @ BANKCON4
.long 0x00000700 @ BANKCON5
.long 0x00018005 @ BANKCON6
.long 0x00018005 @ BANKCON7 9bit
.long 0x008e07a3 @ REFRESH
.long 0x000000b2 @ BANKSIZE
.long 0x00000030 @ MRSRB6
.long 0x00000030 @ MRSRB7
@ 文件 flash.s
@ 作用:設置 Nand Flash 的控制寄存器、讀取 Nand Flash
@ 中的代碼到 SDRAM 的指定位置,更多細節(jié)請參考我前面的隨筆
.equ NFCONF, 0x4e000000
.equ NFCMD, 0x4e000004
.equ NFADDR, 0x4e000008
.equ NFDATA, 0x4e00000c
.equ NFSTAT, 0x4e000010
.equ NFECC, 0x4e000014
.global flash_to_sdram
flash_to_sdram:
@ Save return addr
mov r10,lr
@ Initialize Nand Flash
mov r0,#NFCONF
ldr r1,=0xf830
str r1,[r0]
@ First reset and enable Nand Flash
ldr r1,[r0]
bic r1, r1, #0x800
str r1,[r0]
ldr r2,=NFCMD
mov r3,#0xff
str r3,[r2]
@ for delay
mov r3, #0x0a
1:
subs r3, r3, #1
bne 1b
@ Wait until Nand Flash bit0 is 1
wait_nfstat:
ldr r2,=NFSTAT
ldr r3,[r2]
tst r3,#0x01
beq wait_nfstat
@ Disable Nand Flash
ldr r0,=NFCONF
ldr r1,[r0]
orr r1,r1,#0x8000
str r1,[r0]
@ Initialzie stack
ldr sp,=4096
@ Set arguments and call
@ function nand_read defined in nand_read.c
ldr r0,=0x30000000
mov r1,#0
mov r2,#1024*40
bl nand_read
@ return
mov pc,r10
/* 文件 nand_read.c
* 作用:從 Nand Flash 中讀取一塊數據到 SDRAM 中的指定位置
*/
#define NFCONF (*(volatile unsigned long *)0x4e000000)
#define NFCMD (*(volatile unsigned long *)0x4e000004)
#define NFADDR (*(volatile unsigned long *)0x4e000008)
#define NFDATA (*(volatile unsigned long *)0x4e00000c)
#define NFSTAT (*(volatile unsigned long *)0x4e000010)
#define NFECC (*(volatile unsigned long *)0x4e000014)
#define NAND_SECTOR_SIZE 512
#define NAND_BLOCK_MASK 0x1ff
void wait_idle() {
int i;
for (i = 0; i < 50000; ++i) ;
}
int nand_read(unsigned char *buf, unsigned long start_addr, int size){
int i, j;
/*
* detect the argument
*/
if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
return -1;
}
/* chip Enable */
NFCONF &= ~0x800;
for (i=0; i<10; i++) {
;
}
for (i=start_addr; i < (start_addr + size); i+=NAND_SECTOR_SIZE) {
NFCMD = 0;
/* Write Address */
NFADDR = i & 0xff;
NFADDR = (i >> 9) & 0xff;
NFADDR = (i >> 17) & 0xff;
NFADDR = (i >> 25) & 0xff;
wait_idle();
for(j=0; j < NAND_SECTOR_SIZE; j++) {
*buf++ = (NFDATA & 0xff);
}
}
NFCONF |= 0x800; /* chip disable */
return 0;
}
/* 頭文件 serl.h
* 作用:定義相關寄存器、UART 初始化函數聲明、串口讀寫函數的聲明
*/
#ifndef __SERL_H__
#define __SERL_H__
#define GPHCON (*(volatile unsigned long *)0x56000070)
/* PORT PULL-UP REGISTER*/
#define GPHUP (*(volatile unsigned long *)0x56000078)
/* UART FIFO control register 0*/
#define UFCON0 (*(volatile unsigned long *)0x50000008)
/* UART line control register 0*/
#define ULCON0 (*(volatile unsigned long *)0x50000000)
/* UART CONTROL REGISTER 0*/
#define UCON0 (*(volatile unsigned long *)0x50000004)
/* UART modem control register 0*/
#define UMCON0 (*(volatile unsigned long *)0x5000000C)
/* UART baud rate divisor register 0*/
#define UBRDIV0 (*(volatile unsigned long *)0x50000028)
/* UART TX/RX STATUS REGISTER 0*/
#define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
#define UTXH0 (*(volatile unsigned char *)0x50000020)
#define URXH0 (*(volatile unsigned char *)0x50000024)
#define TXD0_READY 0x2
#define RXD0_READY 0x1
void init_uart();
unsigned char uart_getc();
void uart_putc(unsigned char ch);
void uart_puts(unsigned char* src);
#endif