s5pv210 uart完善更新

昨天傻了 所以弄的挺乱的 这里主要做了一些改进 主要是完善地址表
其他不多说了 直接上代码吧

//cpu_io.h 主要负责读取寄存器值

#ifndef _S5PV210_CPU_LCQ_H
#define _S5PV210_CPU_LCQ_H
#define   _REG(adr)     (*(volatile unsigned int *)(adr))

#define   readb(adr)      (*(volatile unsigned char *)(adr))
#define   readw(adr)     (*(volatile unsigned short *)(adr))
#define   readl(adr)       (*(volatile unsigned int *)(adr))

#define writeb(value,adr)   ((*(volatile unsigned char *)(adr))=(value))  
#define writew(value,adr)   ((*(volatile unsigned short *)(adr))=(value))   
#define writel(value,adr)   ((*(volatile unsigned int*)(adr))=(value))  
#endif 

//GPIO.H //地址表 方便以后使用

#ifndef _ASM_ARCH_GPIO_LCQ_H
#define _ASM_ARCH_GPIO_LCQ_H

#define S5PV210_GPIOBASE  (0XE0200000)
#define S5PV210_UARTBASE  (0xE2900000)

struct s5pc210_gpio_bank {
    unsigned int con;
    unsigned int dat;
    unsigned int pud;
    unsigned int drv;
    unsigned int con_pdn;
    unsigned int pud_pdn;
    unsigned char res1[8];
};

struct s5pv210_gpio {
    struct s5pc210_gpio_bank  gpio_a0;
    struct s5pc210_gpio_bank  gpio_a1;
    struct s5pc210_gpio_bank  gpio_b;
    struct s5pc210_gpio_bank  gpio_c0;
    struct s5pc210_gpio_bank  gpio_c1;
    struct s5pc210_gpio_bank  gpio_d0;
    struct s5pc210_gpio_bank  gpio_d1;
    struct s5pc210_gpio_bank  gpio_e0;
    struct s5pc210_gpio_bank  gpio_e1;
    struct s5pc210_gpio_bank  gpio_f0;
    struct s5pc210_gpio_bank  gpio_f1;
    struct s5pc210_gpio_bank  gpio_f2;
    struct s5pc210_gpio_bank  gpio_f3;
    struct s5pc210_gpio_bank  gpio_g0;
    struct s5pc210_gpio_bank  gpio_g1;
    struct s5pc210_gpio_bank  gpio_g2;
    struct s5pc210_gpio_bank  gpio_g3;
    struct s5pc210_gpio_bank  gpio_i;
    struct s5pc210_gpio_bank  gpio_j0;
    struct s5pc210_gpio_bank  gpio_j1;
    struct s5pc210_gpio_bank  gpio_j2;
    struct s5pc210_gpio_bank  gpio_j3;
    struct s5pc210_gpio_bank  gpio_j4;
};


struct s5pc210_uart_bank
{
    unsigned int ulcon;
    unsigned int ucon;
    unsigned int ufcon;
    unsigned int umcon;
    unsigned int utrstat;
    unsigned int uerstat;
    unsigned int ufstat;
    unsigned int umstat;
    unsigned int utxh;
    unsigned int urxh;
    unsigned int ubrdiv;
    unsigned int udivslot;
    unsigned int uintp;
    unsigned int uintsp;
    unsigned int uimtm;
    unsigned char res1[4];
    unsigned char res2[0x3c0]
}; 

struct s5pc210_uart
{
    struct s5pc210_uart_bank UART1;
    struct s5pc210_uart_bank UART2;
    struct s5pc210_uart_bank UART3;
};

#endif

//UART.H

#ifndef _UART_LCQ_H
#define _UART_LCQ_H

void uart2_init(void);

void uart2_sendchar(char c);

void uart2_sendstr(const char *str);

void uart1_init(void);

void uart1_sendchar(char c);

void uart1_sendstr(const char *str);

#endif

//uart.c 波特率都是115200

#include "uart.h"
#include "cpu_io.h"
#include "gpio.h"

void uart1_init(void)
{

    struct s5pc210_uart *uart_base = (struct s5pc210_uart *)S5PV210_UARTBASE;
    struct s5pv210_gpio *gpio_base = (struct s5pv210_gpio *)S5PV210_GPIOBASE;
    unsigned int var;
      //引脚配置
    var = readl(&gpio_base->gpio_a0.con);
    var &= ~0xff;
    var  |= 0x22;
    writel(var, &gpio_base->gpio_a0.con);

    //uart 参数配置
    _REG(&uart_base->UART1.ulcon) = 0x3;  /*8位1位停止位*/
    _REG(&uart_base->UART1.ucon) = 0x5;/*使能发送接收*/
    _REG(&uart_base->UART1.ubrdiv) = 35;
    _REG(&uart_base->UART1.udivslot) = 0x80;
}

void uart1_sendchar(char c)
{
    struct s5pc210_uart *uart_base = (struct s5pc210_uart *)S5PV210_UARTBASE;
    //等待上次发送完成
    while(!(_REG(&uart_base->UART1.utrstat)&(0x1<<2)));
    //发送
    _REG(&uart_base->UART1.utxh) =c;
}

void uart1_sendstr(const char *str)
{
    while(*str)
    {
        uart1_sendchar(*str);
        str++;
    }
}
char uart1_getchar(void)
{
    struct s5pc210_uart *uart_base = (struct s5pc210_uart *)S5PV210_UARTBASE;
    //等待上次发送完成
    while(!(_REG(&uart_base->UART1.utrstat)&(0x1<<0)));
    //发送
    return _REG(&uart_base->UART1.utxh);
}


void uart2_init(void)
{

    struct s5pc210_uart *uart_base = (struct s5pc210_uart *)S5PV210_UARTBASE;
    struct s5pv210_gpio *gpio_base = (struct s5pv210_gpio *)S5PV210_GPIOBASE;
    unsigned int var;
      //引脚配置 
    var = readl(&gpio_base->gpio_a0.con);
    var &= ~0xff<<16;
    var  |= 0x22<<16;
    writel(var, &gpio_base->gpio_a0.con);

    //uart 参数配置
    _REG(&uart_base->UART2.ulcon) = 0x3;  /*8位1位停止位*/
    _REG(&uart_base->UART2.ucon) = 0x5;/*使能发送接收*/
    _REG(&uart_base->UART2.ubrdiv) = 35;  /* 这两位设置波特率 */
    _REG(&uart_base->UART2.udivslot) = 0x80;
}

void uart2_sendchar(char c)
{
    struct s5pc210_uart *uart_base = (struct s5pc210_uart *)S5PV210_UARTBASE;
    //等待上次发送完成
    while(!(_REG(&uart_base->UART2.utrstat)&(0x1<<2)));
    //发送
    _REG(&uart_base->UART2.utxh) =c;
}

void uart2_sendstr(const char *str)
{
    while(*str)
    {
        uart2_sendchar(*str);
        str++;
    }
}
char uart2_getchar(void)
{
    struct s5pc210_uart *uart_base = (struct s5pc210_uart *)S5PV210_UARTBASE;
    //等待上次发送完成
    while(!(_REG(&uart_base->UART1.utrstat)&(0x1<<0)));
    //发送
    return _REG(&uart_base->UART1.utxh);
}

你可能感兴趣的:(arm)