硬件:JZ2440

实现功能:用putchr()函数实现printf()

start.s

nand.c

uart.c

uart.h

my_stdio.c

my_stdio.h

main.c

start.s源码:

.extern main
.text
.global _start
_start:
ldr sp,=
bl watchdog_init
bl clock_init
bl nand_init
bl sdram_init ldr r0,=0x30000000
mov r1,#
mov r2,#(*)
bl nand_read ldr sp,=0x34000000
ldr lr,=main_return
ldr pc,=main
main_return:
b main_return

init.c

//watchdog
#define WTCON (*(volatile unsigned long *)0x53000000) //clock
#define LOCKTIME (*(volatile unsigned long *)0x4c000000)
#define MPLLCON (*(volatile unsigned long *)0x4c000004)
#define CLKDIVN (*(volatile unsigned long *)0x4c000014) //sdram
#define BWSCON (*(volatile unsigned long *)0x48000000)
#define BANKCON0 (*(volatile unsigned long *)0x48000004)
#define BANKCON1 (*(volatile unsigned long *)0x48000008)
#define BANKCON2 (*(volatile unsigned long *)0x4800000c)
#define BANKCON3 (*(volatile unsigned long *)0x48000010)
#define BANKCON4 (*(volatile unsigned long *)0x48000014)
#define BANKCON5 (*(volatile unsigned long *)0x48000018)
#define BANKCON6 (*(volatile unsigned long *)0x4800001c)
#define BANKCON7 (*(volatile unsigned long *)0x48000020)
#define REFRESH (*(volatile unsigned long *)0x48000024)
#define BANKSIZE (*(volatile unsigned long *)0x48000028)
#define MRSRB6 (*(volatile unsigned long *)0x4800002c)
#define MRSRB7 (*(volatile unsigned long *)0x48000030) void watchdog_init(void)
{
WTCON=;
} void clock_init(void)
{
CLKDIVN=0x03;
__asm__
(
"mrc p15,0,r1,c1,c0,0\n"
"orr r1,r1,#0xc0000000\n"
"mcr p15,0,r1,c1,c0,0\n"
);
MPLLCON=((0x5c<<)|(0x01<<)|(0x02));
} void sdram_init(void)
{
volatile unsigned long *sdram_base=(volatile unsigned long *)0x48000000;
sdram_base[] = 0x22011110;
sdram_base[] = 0x00000700;
sdram_base[] = 0x00000700;
sdram_base[] = 0x00000700;
sdram_base[] = 0x00000700;
sdram_base[] = 0x00000700;
sdram_base[] = 0x00000700;
sdram_base[] = 0x00018005;
sdram_base[] = 0x00018005;
sdram_base[] = 0x008C04F4;
sdram_base[] = 0x000000B1;
sdram_base[] = 0x00000030;
sdram_base[] = 0x00000030; }

nand.c

#define LARGER_NAND_PAGE

#define GSTATUS1        (*(volatile unsigned int *)0x560000B0)
#define BUSY 1 #define NAND_SECTOR_SIZE 512
#define NAND_BLOCK_MASK (NAND_SECTOR_SIZE - 1) #define NAND_SECTOR_SIZE_LP 2048
#define NAND_BLOCK_MASK_LP (NAND_SECTOR_SIZE_LP - 1) typedef unsigned int S3C24X0_REG32; /* NAND FLASH (see S3C2410 manual chapter 6) */
typedef struct {
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFECC;
} S3C2410_NAND; /* NAND FLASH (see S3C2440 manual chapter 6, www.100ask.net) */
typedef struct {
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCONT;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFMECCD0;
S3C24X0_REG32 NFMECCD1;
S3C24X0_REG32 NFSECCD;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFESTAT0;
S3C24X0_REG32 NFESTAT1;
S3C24X0_REG32 NFMECC0;
S3C24X0_REG32 NFMECC1;
S3C24X0_REG32 NFSECC;
S3C24X0_REG32 NFSBLK;
S3C24X0_REG32 NFEBLK;
} S3C2440_NAND; typedef struct {
void (*nand_reset)(void);
void (*wait_idle)(void);
void (*nand_select_chip)(void);
void (*nand_deselect_chip)(void);
void (*write_cmd)(int cmd);
void (*write_addr)(unsigned int addr);
unsigned char (*read_data)(void);
}t_nand_chip; static S3C2410_NAND * s3c2410nand = (S3C2410_NAND *)0x4e000000;
static S3C2440_NAND * s3c2440nand = (S3C2440_NAND *)0x4e000000; static t_nand_chip nand_chip; /* 供外部调用的函数 */
void nand_init(void);
void nand_read(unsigned char *buf, unsigned long start_addr, int size); /* NAND Flash操作的总入口, 它们将调用S3C2410或S3C2440的相应函数 */
static void nand_reset(void);
static void wait_idle(void);
static void nand_select_chip(void);
static void nand_deselect_chip(void);
static void write_cmd(int cmd);
static void write_addr(unsigned int addr);
static unsigned char read_data(void); /* S3C2410的NAND Flash处理函数 */
static void s3c2410_nand_reset(void);
static void s3c2410_wait_idle(void);
static void s3c2410_nand_select_chip(void);
static void s3c2410_nand_deselect_chip(void);
static void s3c2410_write_cmd(int cmd);
static void s3c2410_write_addr(unsigned int addr);
static unsigned char s3c2410_read_data(); /* S3C2440的NAND Flash处理函数 */
static void s3c2440_nand_reset(void);
static void s3c2440_wait_idle(void);
static void s3c2440_nand_select_chip(void);
static void s3c2440_nand_deselect_chip(void);
static void s3c2440_write_cmd(int cmd);
static void s3c2440_write_addr(unsigned int addr);
static unsigned char s3c2440_read_data(void); /* S3C2410的NAND Flash操作函数 */ /* 复位 */
static void s3c2410_nand_reset(void)
{
s3c2410_nand_select_chip();
s3c2410_write_cmd(0xff); // 复位命令
s3c2410_wait_idle();
s3c2410_nand_deselect_chip();
} /* 等待NAND Flash就绪 */
static void s3c2410_wait_idle(void)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFSTAT;
while(!(*p & BUSY))
for(i=; i<; i++);
} /* 发出片选信号 */
static void s3c2410_nand_select_chip(void)
{
int i;
s3c2410nand->NFCONF &= ~(<<);
for(i=; i<; i++);
} /* 取消片选信号 */
static void s3c2410_nand_deselect_chip(void)
{
s3c2410nand->NFCONF |= (<<);
} /* 发出命令 */
static void s3c2410_write_cmd(int cmd)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFCMD;
*p = cmd;
} /* 发出地址 */
static void s3c2410_write_addr(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFADDR; *p = addr & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
} /* 读取数据 */
static unsigned char s3c2410_read_data(void)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFDATA;
return *p;
} /* S3C2440的NAND Flash操作函数 */ /* 复位 */
static void s3c2440_nand_reset(void)
{
s3c2440_nand_select_chip();
s3c2440_write_cmd(0xff); // 复位命令
s3c2440_wait_idle();
s3c2440_nand_deselect_chip();
} /* 等待NAND Flash就绪 */
static void s3c2440_wait_idle(void)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFSTAT;
while(!(*p & BUSY))
for(i=; i<; i++);
} /* 发出片选信号 */
static void s3c2440_nand_select_chip(void)
{
int i;
s3c2440nand->NFCONT &= ~(<<);
for(i=; i<; i++);
} /* 取消片选信号 */
static void s3c2440_nand_deselect_chip(void)
{
s3c2440nand->NFCONT |= (<<);
} /* 发出命令 */
static void s3c2440_write_cmd(int cmd)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFCMD;
*p = cmd;
} /* 发出地址 */
static void s3c2440_write_addr(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR; *p = addr & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
} static void s3c2440_write_addr_lp(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR;
int col, page; col = addr & NAND_BLOCK_MASK_LP;
page = addr / NAND_SECTOR_SIZE_LP; *p = col & 0xff; /* Column Address A0~A7 */
for(i=; i<; i++);
*p = (col >> ) & 0x0f; /* Column Address A8~A11 */
for(i=; i<; i++);
*p = page & 0xff; /* Row Address A12~A19 */
for(i=; i<; i++);
*p = (page >> ) & 0xff; /* Row Address A20~A27 */
for(i=; i<; i++);
*p = (page >> ) & 0x03; /* Row Address A28~A29 */
for(i=; i<; i++);
} /* 读取数据 */
static unsigned char s3c2440_read_data(void)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFDATA;
return *p;
} /* 在第一次使用NAND Flash前,复位一下NAND Flash */
static void nand_reset(void)
{
nand_chip.nand_reset();
} static void wait_idle(void)
{
nand_chip.wait_idle();
} static void nand_select_chip(void)
{
int i;
nand_chip.nand_select_chip();
for(i=; i<; i++);
} static void nand_deselect_chip(void)
{
nand_chip.nand_deselect_chip();
} static void write_cmd(int cmd)
{
nand_chip.write_cmd(cmd);
}
static void write_addr(unsigned int addr)
{
nand_chip.write_addr(addr);
} static unsigned char read_data(void)
{
return nand_chip.read_data();
} /* 初始化NAND Flash */
void nand_init(void)
{
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0 /* 判断是S3C2410还是S3C2440 */
if ((GSTATUS1 == 0x32410000) || (GSTATUS1 == 0x32410002))
{
nand_chip.nand_reset = s3c2410_nand_reset;
nand_chip.wait_idle = s3c2410_wait_idle;
nand_chip.nand_select_chip = s3c2410_nand_select_chip;
nand_chip.nand_deselect_chip = s3c2410_nand_deselect_chip;
nand_chip.write_cmd = s3c2410_write_cmd;
nand_chip.write_addr = s3c2410_write_addr;
nand_chip.read_data = s3c2410_read_data; /* 使能NAND Flash控制器, 初始化ECC, 禁止片选, 设置时序 */
s3c2410nand->NFCONF = (<<)|(<<)|(<<)|(TACLS<<)|(TWRPH0<<)|(TWRPH1<<);
}
else
{
nand_chip.nand_reset = s3c2440_nand_reset;
nand_chip.wait_idle = s3c2440_wait_idle;
nand_chip.nand_select_chip = s3c2440_nand_select_chip;
nand_chip.nand_deselect_chip = s3c2440_nand_deselect_chip;
nand_chip.write_cmd = s3c2440_write_cmd;
#ifdef LARGER_NAND_PAGE
nand_chip.write_addr = s3c2440_write_addr_lp;
#else
nand_chip.write_addr = s3c2440_write_addr;
#endif
nand_chip.read_data = s3c2440_read_data; /* 设置时序 */
s3c2440nand->NFCONF = (TACLS<<)|(TWRPH0<<)|(TWRPH1<<);
/* 使能NAND Flash控制器, 初始化ECC, 禁止片选 */
s3c2440nand->NFCONT = (<<)|(<<)|(<<);
} /* 复位NAND Flash */
nand_reset();
} /* 读函数 */
void nand_read(unsigned char *buf, unsigned long start_addr, int size)
{
int i, j; #ifdef LARGER_NAND_PAGE
if ((start_addr & NAND_BLOCK_MASK_LP) || (size & NAND_BLOCK_MASK_LP)) {
return ; /* 地址或长度不对齐 */
}
#else
if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
return ; /* 地址或长度不对齐 */
}
#endif /* 选中芯片 */
nand_select_chip(); for(i=start_addr; i < (start_addr + size);) {
/* 发出READ0命令 */
write_cmd(); /* Write Address */
write_addr(i);
#ifdef LARGER_NAND_PAGE
write_cmd(0x30);
#endif
wait_idle(); #ifdef LARGER_NAND_PAGE
for(j=; j < NAND_SECTOR_SIZE_LP; j++, i++) {
#else
for(j=; j < NAND_SECTOR_SIZE; j++, i++) {
#endif
*buf = read_data();
buf++;
}
} /* 取消片选信号 */
nand_deselect_chip(); return ;
}

uart.c 源码:

#include"uart.h"

//uart
#define ULCON0 (*(volatile unsigned long *)0x50000000)
#define UCON0 (*(volatile unsigned long *)0x50000004)
#define UFCON0 (*(volatile unsigned long *)0x50000008)
#define UMCON0 (*(volatile unsigned long *)0x5000000c)
#define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
#define UERSTAT0 (*(volatile unsigned long *)0x50000014)
#define UTXH0 (*(volatile unsigned char *)0x50000020)
#define URXH0 (*(volatile unsigned char *)0x50000024)
#define UBRDIV0 (*(volatile unsigned long *)0x50000028) //GPH
#define GPHCON (*(volatile unsigned long *)0x56000070)
#define GPHDAT (*(volatile unsigned long *)0x56000074)
#define GPHUP (*(volatile unsigned long *)0x56000078) void uart0_init(void)
{
GPHCON|=0xa0;
GPHUP=0x0c; ULCON0=0x03;
UCON0=0x05;
UFCON0=0x00;
UMCON0=0x00;
UBRDIV0=((/(*))-);
} unsigned char my_getchar(void)
{
while(!(UTRSTAT0 & 0x01));
return URXH0;
} void my_putchar(unsigned char date)
{
while(!(UTRSTAT0 & 0x02));
UTXH0=date;
} unsigned char getc(void)
{
while(!(UTRSTAT0 & 0x01));
return URXH0;
} void putc(unsigned char c)
{
while(!(UTRSTAT0 & 0x02));
UTXH0=c;
}

uart.h 源码:

#ifndef _UART_H_
#define _UART_H_ void uart0_init(void);
unsigned char my_getchar(void);
void my_putchar(unsigned char date); unsigned char getc(void);
void putc(unsigned char c); #endif

my_stdio.c 源码:

#include"uart.h"
#include"my_stdio.h" typedef char* va_list; #define int_sizeof(type) ((sizeof(int)+sizeof(type)-1)&~(sizeof(int)-1)) #define va_start(para_p,first_para) (para_p=(va_list)&first_para+int_sizeof(first_para))
#define va_arg(para_p,type) (*(type *)((para_p+=int_sizeof(type))-int_sizeof(type)))
#define va_end(para_p) (para_p=(va_list)0) void my_printf(const char *format, ...)
{
va_list temp ;
char *string = format;
va_start(temp, format); while (*string)
{
if (*string == '%')
{
switch (*++string)
{
case 'c': { my_putchar(va_arg(temp,char)); break; }
case 's':
{
char* p = va_arg(temp,char*);
while (*p != ) my_putchar(*p++);
break;
} case 'p':
{
unsigned int addr = (unsigned int)&va_arg(temp, unsigned int), sheft = 0xf0000000;
unsigned char temp = ,count=;
my_putchar('');
my_putchar('x');
while (count<)
{
temp = (unsigned char)(((sheft >> count)&addr) >> ( - count - )) ;
if (temp < ) my_putchar(temp + );
if (temp >= ) my_putchar(temp+-);
count += ;
}
break;
} case 'd':
{
int value = va_arg(temp, int),num=;
if (value == ) my_putchar();
if (value & 0x80000000) //负数
{
my_putchar('-');
num = -;
}
else //正数.;
while (!(value / num))
{
num /= ;
if (num == )
{
my_putchar();
return;
}
}
while (num != )
{
my_putchar(value / num+);
value %= num;
num /= ;
}
break;
} case 'f':
{
char *str = "Sorry: function is not available";
while (*str != ) my_putchar(*str++);
} default: break;
}
}
string++;
}
va_end(temp);
}

my_stdio.h 源码:

#ifndef  _OWN_STDIO_H_
#define _OWN_STDIO_H_ void my_printf(const char *format,...); #endif

main.c 源码:

#include"uart.h"
#include"my_stdio.h"
#include"stdio.h" void raise(int sig_nr){;} int main(void)
{
uart0_init(); printf("%s"," wds_standard ");
my_printf("%s"," zsy_standard "); while()
my_putchar(my_getchar()+); return ;
}

链接脚本 uart.lds :

SECTIONS
{
. = 0x00000000;
.init : AT(){start.o init.o nand.o}
. = 0x30000000;
.text : AT(){*(.text)}
.rodata ALIGN() : AT((LOADADDR(.text)+SIZEOF(.text)+)&~(0x03)) {*(.rodata*)}
.data ALIGN() : AT((LOADADDR(.rodata)+SIZEOF(.rodata)+)&~(0x03)) {*(.data)}
.bss : {*(.bss) *(COMMON)} }

Makefile:

objs:= start.o init.o uart.o nand.o main.o my_stdio.o lib/libc.a

CC            =arm-linux-gcc
LD =arm-linux-ld
AR =arm-linux-ar
OBJCOPY =arm-linux-objcopy
OBJDUMP =arm-linux-objdump INCLUDEDIR :=$(shell pwd)/include
CFLAGS :=-Wall -O2
CPPFLAGS :=-nostdinc -I$(INCLUDEDIR) export CC LD OBJCOPY OBJDUMP INCLUDEDIR CFLAGS CPPFLAGS uart.bin:$(objs)
arm-linux-ld -Tuart.lds -o uart_elf $^ libgcc.a
arm-linux-objcopy -O binary -S uart_elf $@
arm-linux-objdump -D -m arm uart_elf > uart.dis .PHONY:lib/libc.a
lib/libc.a:
cd lib;make;cd .. %.o:%.s
$(CC) $(CPPFLAGS) $(FLAGS)-o $@ -c $< %.o:%.c
$(CC) $(CPPFLAGS) $(FLAGS) -o $@ -c $< clean:
make clean -C lib
rm -f *.o *.bin *.dis uart_elf

用uart实现printf函数的更多相关文章

  1. 可变参数列表与printf()函数的实现

    问题 当我们刚开始学习C语言的时候,就接触到printf()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...

  2. 如何在单片机上使用printf函数(printf)(avr)(stm)(lpc)(单片机)(转)

    摘要:    当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上,来判断我们的程序是否按预期的运行,printf函数很好的做到了这一点,它能直接以字符的方式输出变量名和变量的值,printf ...

  3. 如果简化stm32中printf函数的使用——首先重定向

    STM32单片机极简方法 使用宏定义 代替复杂的重定向printf()函数,实现串口打印.(HAL库例程)https://blog.csdn.net/wu10188/article/details/9 ...

  4. 通过串口利用printf函数输出数据

    一.printf函数格式 printf函数具有强大的输出功能 %表示格式化字符串输出 目前printf支持以下格式的输出,例如: printf("%c",a);输出单个字符. pr ...

  5. printf函数

    printf函数的格式及含义 d                    以十进制带符号的形式输出整数(对正数不输出符号) o                    以八进制无符号的形式输出整数(不输出 ...

  6. Linux Linux下特殊的printf函数和fputs函数

    Linux下,printf函数必须以'\n'结尾才会立刻输出到屏幕,如果没有'\n'直到输出缓冲区满了以后才会打印到屏幕上(敲击换行也算),如果需要不换行的输出,一般可以使用write函数代替.'\n ...

  7. 关于printf函数的所思所想

    缘起大一下学期,C语言程序设计徐小青老师的随口一提,经娄嘉鹏老师提醒,我觉得应该自己整理清楚这一问题.涉及网上资料将会标明出处. 关于printf函数的所思所想 * printf的定义 printf( ...

  8. C语言printf()函数:格式化输出函数

    C语言printf()函数:格式化输出函数 头文件:#include <stdio.h> printf()函数是最常用的格式化输出函数,其原型为:     int printf( char ...

  9. 关于printf函数输出先后顺序的讲解!!

    对于printf函数printf("%d%d\n",a,b);函数的实际输出顺序是这样的先计算出b,然后在计算a,接着输出a,最后在输出b:例子如下:#include<ios ...

随机推荐

  1. Json Web Token(JWT)

    Json web token (JWT),是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(Si ...

  2. 响应式编程笔记三:一个简单的HTTP服务器

    # 响应式编程笔记三:一个简单的HTTP服务器 本文我们将继续前面的学习,但将更多的注意力放在用例和编写实际能用的代码上面,而非基本的APIs学习. 我们会看到Reactive是一个有用的抽象 - 对 ...

  3. (原)关于i++和++i的小程序测试

    今天看到一个程序,于是用vs运行了一下,结果出乎我的意料: 代码: ; +(i++))+(+(i++)); i=; +(++i))+(+(++i)); i=; printf("x=%d,y= ...

  4. Python 函数(可变参数)

    在python函数中,可以定义可变参数,顾名思义,可变参数就是,传入的参数是可变的例如,给定一组数字a,b,c...  请计算a2 + b2 + c2 + …… 要定义出这个函数,我们必须确定输入的参 ...

  5. 【PostgresSQL】同时更新两个表

    UPDATE table1 SET column = value FROM table2 WHERE table1.column2 = table2.column2

  6. ios开发之--NSString中substringFromIndex,substringWithRange,substringToIndex方法的使用

    1,substringFromIndex 从索引0开发到到index的后面的字符 2,substringWithRange 取指定range里面的字符,左边为索引index,右边为截取的范围 3,su ...

  7. iOS开发-- TFHpple抓取GB-2312编码的html页面,页面返回编码错误

    今天用TFHpple解析html,发现老报错encoding error : input conversion failed due to input error, bytes  I/O error ...

  8. C语言程序设计--执行命令

    1.system函数 1.1函数原型 int system(char *command); 1.2解释 system()会调用fork()产生子进程,由子进程来调用/bin/sh -c string来 ...

  9. meat http-equiv 属性详解

    转自 http://kinglyhum.iteye.com/blog/827807 http-equiv 属性提供了 content 属性的信息/值的 HTTP 头. http-equiv 属性可用于 ...

  10. 写写我的硕士三年【zz】

    昨天我们组的10bit-40M ADC测出来了,自己终于能松口气,可以无牵无挂的毕业了.晚上老板bg全组毕业生,喝了很多,我对老板说:"这3年在组里,我是把它当作事业来做的!"是的 ...