s5PV210 UART

接着折腾了下串口 只是简单实现发送功能

首先找到引脚关系,这里就不贴原理图了
串口1的引脚是 gpio_a0的低两位
s5PV210 UART_第1张图片
在这里只需要配置 TX 和RX

接下来就是配置串口的参数
ULCONn:数据位 奇偶校验 停止位
s5PV210 UART_第2张图片
跟上位机配置成一样就好
s5PV210 UART_第3张图片
按照这个 普通模式

ulcon = 0b0000011 = 0x03

接着设置模式寄存器 UCON
s5PV210 UART_第4张图片
设置

uncon = 0x0101 =0x05

其他默认都是关的 先不管
接下来配置波特率
s5PV210 UART_第5张图片
s5PV210 UART_第6张图片
s5PV210 UART_第7张图片
时钟频率位于PSYS 66.5M
按照公式
DIV_VAL = (66500000 / (115200 x 16)) -1
= 36.07 -1
= 35.07

0.07*16 = 1.12 == 1
查表
UDIVSLOT=0x80

代码区

//cpu_io.h
#ifndef _S5PV210_CPU_H
#define _S5PV210_CPU_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_H
#define _ASM_ARCH_GPIO_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 char utxh;
    unsigned char res1[3];
    unsigned char urxh;
    unsigned char res2[3];
    unsigned int ubrdiv;
    unsigned short udivslot;
    unsigned char res3[2];
    unsigned char res4[0x3d0];
}; 
#endif

//uart.h
#ifndef _UART_LCQ_H
#define _UART_LCQ_H
void uart0_init(void);
void uart0_sendchar(char c);
void uart0_sendstr(const char *str);
#endif

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

void uart0_init(void)
{
    struct s5pc210_uart_bank *uart_base = (struct s5pc210_uart_bank *)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->ulcon) = 0x3;  /*8位1位停止位*/
    _REG(&uart_base->ucon) = 0x5;/*使能发送接收*/
    _REG(&uart_base->ubrdiv) = 35;
    _REG(&uart_base->udivslot) = 0x80;
}

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

void uart0_sendstr(const char *str)
{
    while(*str)
    {
        uart0_sendchar(*str);
        str++;
    }
}
char uart0_getchar(void)
{
    struct s5pc210_uart_bank *uart_base = (struct s5pc210_uart_bank *)S5PV210_UARTBASE;
    //等待上次发送完成
    while(!(_REG(&uart_base->utrstat)&(0x1<<0)));
    //发送
    return _REG(&uart_base->utxh);
}

今天之所以折腾到现在 是因为我用一个转串口接到一个已经转成串口的引脚上去了 真是脑残

你可能感兴趣的:(arm)