uart(4)-输入命令来控制灯亮灭

来源:互联网 发布:3d美工 编辑:程序博客网 时间:2024/06/05 08:32

输入一个"led_on",就打开四展灯;输入一个"led_off",就关闭四展灯;

************************************************************************************

test.c文件如下:

#include "uart.h"
void putchar(unsigned char s1);
void puts(char *s1);
unsigned char getchar();
void gets(unsigned char *buff);
void led_on();
void led_off();
int my_strcmp(char *s1,char *s2);
void *my_memset(char *s,int c,unsigned int n);
int (*printf)(char *,...) = (void *)0x57e11d4c;
int main()
{

char c;

char buffer[300];

char *buff = my_memset(buffer,0,300);



//*(volatile unsigned long *)0x56000000 = hander_irq;

    //cpsr打开中断

          __asm__ __volatile__(
                  "mrs r0,cpsr\n"
                  "bic r0,r0,#0x80\n"
                  "msr cpsr,r0\n"
                  :
                  :
                  :"r0","memory"
          );

                  "mrs r0,cpsr\n"

                  "bic r0,r0,#0x80\n"

                  "msr cpsr,r0\n"

                  :

                  :

                  :"r0","memory"

          );

          //led

          GPMCON = (1<<0)|(1<<4)|(1<<8)|(1<<12);

          GPMDAT = 0xf;


//URAT

ULCON0 = 3;

UCON0 = 1|(1<<2)|(2<<10);

/*

DIV_VAL = (PCLK / (bps x 16 ) ) −1

=(66M/(115200*16))-1

=34.8

x/16 = 0.8

x = 16*0.8

x = 12.8 ~ 13

*/

UBRDIV0 = 34;

UDIVSLOT0 =0xDFDD;



puts("-------------my_uboot---------------\n");

while(1)

{

puts("you-boot #\n");

// c =getchar();

// putchar(c);

gets(buff);

if(!my_strcmp("led_on",buff))

led_on();

if(!my_strcmp("led_off",buff))

led_off();

puts(buff);

putchar('\r');

putchar('\n');

buff = my_memset(buffer,0,300);


}


return 0;



}
void putchar(unsigned char s1){

while(1){

if(UTRSTAT0&(1<<1))//when empty ,we write 

break;

}

UTXH0 = s1;

}
void puts(char *s1){

while(*s1)

{

if(*s1 == '\n')

putchar('\r');

putchar(*s1++); 

}

}


unsigned char getchar(){

unsigned char c=0;

while(1)

{

if(UTRSTAT0 & (1<<0))

break;

}

c = URXH0;

return c;

}
void gets(unsigned char *buff){

char c = 0;

while(1)

{

c = getchar();

if(c == '\r')

break;

*buff++  = c;

}

//*buff = '\n';

}
void led_on(){
         GPMDAT = 0;
}
void led_off(){
        GPMDAT = 0xf;
}
int my_strcmp(char *s1,char *s2){


while((*s1++ == *s2++)&&(*s1 != 0)&&(*s2 != 0))

{

;

}

if((*s1 == 0)&&(*s2== 0))

return 0;

return 1;

}
void *my_memset(char *s,int c,unsigned int n)
{

unsigned int i=0;

while(i <= n)

{

*(s+i) = c;

i++;

}

return s;

}


*******************************************************************************************

vector.s文件如下:

b reset

b und

b swi

b pre_abt

b data_abt

.word 0x0

b irq

b firq





reset:
und:

mov sp,#0x56000000

mov ip,sp

stmdb sp!,{fp,ip,lr}

sub fp,ip,#4


ldr r0,=string_und

ldr r1,printf

blx r1




mov sp,#0x56000000

ldmdb sp,{fp,sp,pc}^

swi:


mov sp,#0x56000000

mov ip,sp

stmdb sp!,{fp,ip,lr}

sub fp,ip,#4


ldr r0,=string_swi

ldr r1,printf

blx r1




mov sp,#0x56000000

ldmdb sp,{fp,sp,pc}^

pre_abt:


data_abt:

mov sp,#0x56000000

mov ip,sp

sub lr,lr,#4

stmdb sp!,{fp,ip,lr}

sub fp,ip,#4


ldr r0,=string_pre_abt

ldr r1,printf

blx r1



mov sp,#0x56000000

ldmdb sp,{fp,sp,pc}^

irq:

mov sp,#0x56000000

mov ip,sp

sub lr,lr,#4

stmdb sp!,{fp,ip,lr}

sub fp,ip,#4



mov r0,#0x56000000

ldr r1,[r0]

blx r1





mov sp,#0x56000000

ldmdb sp,{fp,sp,pc}^



firq:
printf:

.word 0x57e11d4c



string_und:

.asciz "hello undefine \n"

.align 2

string_swi:

.asciz "hello swi\n"

.align 2

string_pre_abt:

.asciz "hell data_abt\n"

.align 2

string_irq:

.asciz "hello irq ,wang wang wang  \n"

.align 2

*******************************************************************************

uart.h文件如下:
//interrupt
#define VIC0INTENABLE (*(volatile unsigned long *)0x71200010)
#define VIC0INTSELECT (*(volatile unsigned long *)0x7120000C)
#define VICxIRQSTATUS   (*(volatile unsigned long *)0x71200000)
#define VICxRAWINTR (*(volatile unsigned long *)0x71200008)


//模拟watdong
#define VIC0SOFTINT (*(volatile unsigned long *)0x71200018)
#define VIC0SOFTINTCLAR (*(volatile unsigned long *)0x7120001C)
//watdog
#define WTCON    (*(volatile unsigned long *)0x7E004000) 
#define WTDAT    (*(volatile unsigned long *)0x7E004004) 
#define WTCNT    (*(volatile unsigned long *)0x7E004008) 
#define WTCLRINT (*(volatile unsigned long *)0x7E00400C)
//led
#define GPMCON (*(volatile unsigned long *)0x7F008820)
#define GPMDAT (*(volatile unsigned long *)0x7F008824)
//key
#define GPNCON (*(volatile unsigned long *)0x7F008830)
#define GPNDAT (*(volatile unsigned long *)0x7F008834)
//ex_interrupt
#define EINT0CON0    (*(volatile unsigned long *)0x7F008900)
#define EINT0CON1    (*(volatile unsigned long *)0x7F008904)
#define EINT0MASK     (*(volatile unsigned long *)0x7F008920)
#define EINT0PEND    (*(volatile unsigned long *)0x7F008924)
//clock
#define APLLLOCK (*(volatile unsigned long *)0x7E00F000) 
#define MPLLLOCK (*(volatile unsigned long *)0x7E00F004) 
#define EPLLLOCK (*(volatile unsigned long *)0x7E00F008) 
#define APLLCON  (*(volatile unsigned long *)0x7E00F00C) 
#define MPLLCON  (*(volatile unsigned long *)0x7E00F010) 
#define EPLLCON0 (*(volatile unsigned long *)0x7E00F014) 
#define EPLLCON1 (*(volatile unsigned long *)0x7E00F018)




#define CLKSRC       (*(volatile unsigned long *)0x7E00F01C )
#define CLKDIV0      (*(volatile unsigned long *)0x7E00F020 )
#define CLKDIV1      (*(volatile unsigned long *)0x7E00F024 )
#define CLKDIV2      (*(volatile unsigned long *)0x7E00F028 )
#define CLKOUT       (*(volatile unsigned long *)0x7E00F02C )
#define HCLKGATE     (*(volatile unsigned long *)0x7E00F030 )
#define PCLKGATE     (*(volatile unsigned long *)0x7E00F034 )
#define SCLKGATE     (*(volatile unsigned long *)0x7E00F038 )
#define MEM0CLK_GATE (*(volatile unsigned long *)0x7E00F03C)
#define MISCCON (*(volatile unsigned long*)0x7E00F839)


//UART
#define ULCON0   (*(volatile unsigned long *)0x7F005000) 
#define  UCON0   (*(volatile unsigned long *)0x7F005004) 
#define UFCON0   (*(volatile unsigned long *)0x7F005008) 
#define UMCON0   (*(volatile unsigned long *)0x7F00500C) 
#define UTRSTAT0 (*(volatile unsigned long *) 0x7F005010) 
#define UERSTAT0 (*(volatile unsigned long *) 0x7F005014) 
#define UFSTAT0  (*(volatile unsigned long *)0x7F005018) 
#define UMSTAT0  (*(volatile unsigned long *)0x7F00501C) 
#define  UTXH0   (*(volatile unsigned long *)0x7F005020) 
#define  URXH0   (*(volatile unsigned long *)0x7F005024) 
#define UBRDIV0  (*(volatile unsigned long *)0x7F005028) 
#define UDIVSLOT0 (*(volatile unsigned long *)0x7F00502C) 
#define  UINTP0  (*(volatile unsigned long *)0x7F005030) 
#define UINTSP0  (*(volatile unsigned long *)0x7F005034)
#define  UINTM0  (*(volatile unsigned long *)0x7F005038)

*****************************************************************************************

makefile文件如下:





all:

arm-none-linux-gnueabi-gcc -c test.c -o test.o

arm-none-linux-gnueabi-ld -Ttext=0x50000000 test.o -o test

arm-none-linux-gnueabi-objcopy -Ielf32-littlearm -O binary test test.bin

cp test.bin /tftpboot/

clean:

rm -rf *.o *.bin test vector  /tftpboot/*.bin

原创粉丝点击