1.main.c
#include "uart.h"
void delay(volatile int d)
{
while(d--);
}
int main(void)
{
char c;
uart_init();
while(1)
{
c = getchar();
putchar(c);
putchar(c+1);
}
return 0;
}
2.uart.c
#include "uart.h"
typedef struct {
volatile unsigned int URXD;
unsigned char RESERVED_0[60];
volatile unsigned int UTXD;
unsigned char RESERVED_1[60];
volatile unsigned int UCR1;
volatile unsigned int UCR2;
volatile unsigned int UCR3;
volatile unsigned int UCR4;
volatile unsigned int UFCR;
volatile unsigned int USR1;
volatile unsigned int USR2;
volatile unsigned int UESC;
volatile unsigned int UTIM;
volatile unsigned int UBIR;
volatile unsigned int UBMR;
volatile unsigned int UBRC;
volatile unsigned int ONEMS;
volatile unsigned int UTS;
volatile unsigned int UMCR;
} UART_Type;
void uart_init(void)
{
volatile unsigned int *IOMUXC_SW_MUX_CTL_PAD_UART1_TX_DATA ;
volatile unsigned int *IOMUXC_SW_MUX_CTL_PAD_UART1_RX_DATA ;
volatile unsigned int *IOMUXC_UART1_RX_DATA_SELECT_INPUT ;
volatile unsigned int *CCM_CSCDR1;
volatile unsigned int *CCM_CCGR5;
UART_Type *uart1 = (UART_Type *)0x02020000;
IOMUXC_SW_MUX_CTL_PAD_UART1_TX_DATA = (volatile unsigned int *)(0x20E0084);
IOMUXC_SW_MUX_CTL_PAD_UART1_RX_DATA = (volatile unsigned int *)(0x20E0088);
IOMUXC_UART1_RX_DATA_SELECT_INPUT = (volatile unsigned int *)(0x20E0624);
CCM_CSCDR1 = (volatile unsigned int *)(0x020C4024);
CCM_CCGR5 = (volatile unsigned int *)(0x020C407C);
*CCM_CSCDR1 &= ~((1<<6) | (0x3f));
*CCM_CCGR5 |= (3<<24);
*IOMUXC_SW_MUX_CTL_PAD_UART1_TX_DATA &= ~0xf;
*IOMUXC_SW_MUX_CTL_PAD_UART1_RX_DATA &= ~0xf;
*IOMUXC_UART1_RX_DATA_SELECT_INPUT |= 3;
uart1->UFCR |= (5<<7);
uart1->UBIR = 15;
uart1->UBMR = 693;
uart1->UCR2 |= (1<<14) | (0<<8) | (0<<6) | (1<<5) | (1<<2) | (1<<1);
uart1->UCR3 |= (1<<2);
uart1->UCR1 |= (1<<0);
}
int getchar(void)
{
UART_Type *uart1 = (UART_Type *)0x02020000;
while((uart1->USR2 & (1<<0)) == 0);
return uart1->URXD;
}
char putchar(char c)
{
UART_Type *uart1 = (UART_Type *)0x02020000;
while((uart1->USR2 & (1<<3)) == 0);
uart1->UTXD = c;
return c;
}
3.uart.h
#ifndef _UART_H
#define _UART_H
void uart_init(void);
int getchar(void);
char putchar(char c);
#endif
3.Makefile
PREFIX=arm-linux-gnueabihf-
CC=$(PREFIX)gcc
LD=$(PREFIX)ld
AR=$(PREFIX)ar
OBJCOPY=$(PREFIX)objcopy
OBJDUMP=$(PREFIX)objdump
led.img : start.S uart.c main.c
$(CC) -nostdlib -g -c -o start.o start.S
$(CC) -nostdlib -g -c -o uart.o uart.c
$(CC) -nostdlib -g -c -o main.o main.c
$(LD) -T imx6ull.lds -g start.o uart.o main.o -o led.elf
$(OBJCOPY) -O binary -S led.elf led.bin
$(OBJDUMP) -D -m arm led.elf > led.dis
mkimage -n ./tools/imximage.cfg.cfgtmp -T imximage -e 0x80100000 -d led.bin led.imx
dd if=/dev/zero of=1k.bin bs=1024 count=1
cat 1k.bin led.imx > led.img
clean:
rm -f led.dis led.bin led.elf led.imx led.img *.o
|