Merge pull request #840 from caogos/master

[BSP] Add I2C driver for Loongson 1C.
This commit is contained in:
Bernard Xiong 2017-09-06 15:52:47 +08:00 committed by GitHub
commit 646102a1d4
6 changed files with 555 additions and 2 deletions

View File

@ -211,8 +211,8 @@ unsigned int gpio_get(unsigned int gpio)
*/ */
void gpio_set_irq_type(unsigned int gpio, gpio_irq_type_t type) void gpio_set_irq_type(unsigned int gpio, gpio_irq_type_t type)
{ {
volatile unsigned int *int_pol; // 中断极性选择寄存器 volatile unsigned int *int_pol = NULL; // 中断极性选择寄存器
volatile unsigned int *int_edge; // 中断边沿选择寄存器 volatile unsigned int *int_edge = NULL; // 中断边沿选择寄存器
unsigned int port = GPIO_GET_PORT(gpio); unsigned int port = GPIO_GET_PORT(gpio);
unsigned int pin = GPIO_GET_PIN(gpio); unsigned int pin = GPIO_GET_PIN(gpio);
@ -257,6 +257,9 @@ void gpio_set_irq_type(unsigned int gpio, gpio_irq_type_t type)
*int_pol &= ~(1 << pin); *int_pol &= ~(1 << pin);
*int_edge &= ~(1 << pin); *int_edge &= ~(1 << pin);
break; break;
default:
break;
} }
return ; return ;

View File

@ -0,0 +1,373 @@
/*
* File : ls1c_i2c.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-06 first version
*/
// 封装硬件i2c接口
#include "ls1c_public.h"
#include "ls1c_regs.h"
#include "ls1c_clock.h"
#include "ls1c_i2c.h"
#include "ls1c_delay.h"
/*
* I2C各个寄存器相对基地址的偏移
*
*
*/
#define LS1C_I2C_PRER_LOW_OFFSET (0) // 分频锁存器低字节寄存器偏移
#define LS1C_I2C_PRER_HIGH_OFFSET (1) // 分频锁存器高字节寄存器偏移
#define LS1C_I2C_CONTROL_OFFSET (2) // 控制寄存器偏移
#define LS1C_I2C_DATA_OFFSET (3) // 发送数据寄存器和接收数据寄存器的偏移是相同的
#define LS1C_I2C_CMD_OFFSET (4) // 命令寄存器偏移,只写
#define LS1C_I2C_STATUS_OFFSET (4) // 状态寄存器偏移,只读
// 控制寄存器的位域
#define LS1C_I2C_CONTROL_EN (0x80) // i2c模块使能
#define LS1C_I2C_CONTROL_IEN (0x40) // 中断使能
// 命令寄存器的位域
#define LS1C_I2C_CMD_START (0x90) // 产生START信号
#define LS1C_I2C_CMD_STOP (0x40) // 产生STOP信号
#define LS1C_I2C_CMD_READ (0x20) // 产生读信号即产生ACK信号
#define LS1C_I2C_CMD_WRITE (0x10) // 产生写信号
#define LS1C_I2C_CMD_READ_ACK (0x20) // 产生ACK信号与读信号相同
#define LS1C_I2C_CMD_READ_NACK (0x28) // 产生NACK信号
#define LS1C_I2C_CMD_IACK (0x00) // 产生中断应答信号
// 状态寄存器的位域
#define LS1C_I2C_STATUS_IF (0x01) // 中断标志位
#define LS1C_I2C_STATUS_TIP (0x02) // 指示传输的过程。1正在传输0传输完成
#define LS1C_I2C_STATUS_ARBLOST (0x20) // I2C核失去I2C总线的控制权
#define LS1C_I2C_STATUS_BUSY (0x40) // I2C总线忙标志
#define LS1C_I2C_STATUS_NACK (0x80) // 应答位标志。1没收到应答位0收到应答位
/*
* i2c模块的基地址
* @I2Cx I2C模块的编号
*/
void *i2c_get_base(ls1c_i2c_t I2Cx)
{
void *base = NULL;
switch (I2Cx)
{
case LS1C_I2C_0:
base = (void *)LS1C_I2C0_BASE;
break;
case LS1C_I2C_1:
base = (void *)LS1C_I2C1_BASE;
break;
case LS1C_I2C_2:
base = (void *)LS1C_I2C2_BASE;
break;
default:
base = NULL;
break;
}
return base;
}
/*
*
* @i2c_info_p i2c模块信息
* @cmd
*/
void i2c_cmd(ls1c_i2c_info_t *i2c_info_p, unsigned char cmd)
{
void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
reg_write_8(cmd, i2c_base + LS1C_I2C_CMD_OFFSET);
return ;
}
/*
* START命令START信号
* @i2c_info_p i2c模块信息
*/
void i2c_cmd_start(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd(i2c_info_p, LS1C_I2C_CMD_START);
return ;
}
/*
* STOP命令STOP信号
* @i2c_info_p i2c模块信息
*/
void i2c_cmd_stop(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd(i2c_info_p, LS1C_I2C_CMD_STOP);
return ;
}
/*
*
* @i2c_info_p i2c模块信息
*/
void i2c_cmd_write(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd(i2c_info_p, LS1C_I2C_CMD_WRITE);
return ;
}
/*
* ack命令ack信号
* @i2c_info_p i2c模块信息
*/
void i2c_cmd_read_ack(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd(i2c_info_p, LS1C_I2C_CMD_READ_ACK);
return ;
}
/*
* nack命令,nack信号
* @i2c_info_p i2c模块信息
*/
void i2c_cmd_read_nack(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd(i2c_info_p, LS1C_I2C_CMD_READ_NACK);
return ;
}
/*
*
* @i2c_info_p i2c模块信息
*/
void i2c_cmd_iack(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd(i2c_info_p, LS1C_I2C_CMD_IACK);
return ;
}
/*
*
* @i2c_info_p i2c模块信息
* @ret
*/
unsigned char i2c_get_status(ls1c_i2c_info_t *i2c_info_p)
{
void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
return reg_read_8(i2c_base + LS1C_I2C_STATUS_OFFSET);
}
/*
* Poll the i2c status register until the specified bit is set.
* Returns 0 if timed out (100 msec).
* @i2c_info_p i2c模块信息
* @bit
* @ret true or false
*/
int i2c_poll_status(ls1c_i2c_info_t *i2c_info_p, unsigned long bit)
{
int loop_cntr = 20000;
do {
delay_us(1);
} while ((i2c_get_status(i2c_info_p) & bit) && (0 < --loop_cntr));
return (0 < loop_cntr);
}
/*
* i2c模块
* @i2c_info_p i2c模块的信息
*/
void i2c_init(ls1c_i2c_info_t *i2c_info_p)
{
void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
unsigned long i2c_clock = i2c_info_p->clock;
unsigned char ctrl = reg_read_8(i2c_base + LS1C_I2C_CONTROL_OFFSET);
unsigned long prescale = 0;
/* make sure the device is disabled */
ctrl = ctrl & ~(LS1C_I2C_CONTROL_EN | LS1C_I2C_CONTROL_IEN);
reg_write_8(ctrl, i2c_base + LS1C_I2C_CONTROL_OFFSET);
// 设置时钟
i2c_clock = MIN(i2c_clock, LS1C_I2C_CLOCK_MAX); // 限制在最大允许范围内
prescale = clk_get_apb_rate();
prescale = (prescale / (5 * i2c_clock)) - 1;
reg_write_8(prescale & 0xff, i2c_base + LS1C_I2C_PRER_LOW_OFFSET);
reg_write_8(prescale >> 8, i2c_base + LS1C_I2C_PRER_HIGH_OFFSET);
// 使能
i2c_cmd_iack(i2c_info_p);
ctrl = ctrl | LS1C_I2C_CONTROL_EN;
reg_write_8(ctrl, i2c_base + LS1C_I2C_CONTROL_OFFSET);
return ;
}
/*
* ()ACK信号
* @i2c_info_p i2c模块信息
* @ret LS1C_I2C_ACK or LS1C_I2C_NACK
*/
ls1c_i2c_ack_t i2c_receive_ack(ls1c_i2c_info_t *i2c_info_p)
{
ls1c_i2c_ack_t ret = LS1C_I2C_NACK;
if (LS1C_I2C_STATUS_NACK & i2c_get_status(i2c_info_p))
{
ret = LS1C_I2C_NACK;
}
else
{
ret = LS1C_I2C_ACK;
}
return ret;
}
/*
*
* @i2c_info_p i2c模块信息
* @buf
* @len
*/
ls1c_i2c_ret_t i2c_receive_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *buf, int len)
{
void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
int i = 0;
for (i=0; i<len; i++)
{
// 开始接收
if (i != (len - 1))
i2c_cmd_read_ack(i2c_info_p);
else
i2c_cmd_read_nack(i2c_info_p);
// 等待,直到接收完成
if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_TIP))
return LS1C_I2C_RET_TIMEOUT;
// 读取数据,并保存
*buf++ = reg_read_8(i2c_base + LS1C_I2C_DATA_OFFSET);
}
return LS1C_I2C_RET_OK;
}
/*
* START信号和地址
* @i2c_info_p i2c模块信息
* @slave_addr
* @direction ()
*/
ls1c_i2c_ret_t i2c_send_start_and_addr(ls1c_i2c_info_t *i2c_info_p,
unsigned char slave_addr,
ls1c_i2c_direction_t direction)
{
void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
unsigned char data = 0;
// 等待i2c总线空闲
if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_BUSY))
return LS1C_I2C_RET_TIMEOUT;
// 填充地址到数据寄存器
data = (slave_addr << 1) | ((LS1C_I2C_DIRECTION_READ == direction) ? 1 : 0);
reg_write_8(data , i2c_base + LS1C_I2C_DATA_OFFSET);
// 开始发送
i2c_cmd_start(i2c_info_p);
// 等待,直到发送完成
if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_TIP))
return LS1C_I2C_RET_TIMEOUT;
return LS1C_I2C_RET_OK;
}
/*
*
* @i2c_info_p i2c模块信息
* @data
* @len
*/
ls1c_i2c_ret_t i2c_send_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *data, int len)
{
void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
int i = 0;
for (i=0; i<len; i++)
{
// 将一个字节数据写入数据寄存器
reg_write_8(*data++, i2c_base + LS1C_I2C_DATA_OFFSET);
// 开始发送
reg_write_8(LS1C_I2C_CMD_WRITE, i2c_base + LS1C_I2C_CMD_OFFSET);
// 等待,直到发送完成
if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_TIP))
return LS1C_I2C_RET_TIMEOUT;
// 读取应答信号
if (LS1C_I2C_ACK != i2c_receive_ack(i2c_info_p))
return len;
}
return LS1C_I2C_RET_OK;
}
/*
* STOP信号
* @i2c_info_p i2c模块信息
*/
void i2c_send_stop(ls1c_i2c_info_t *i2c_info_p)
{
i2c_cmd_stop(i2c_info_p);
return ;
}

View File

@ -0,0 +1,134 @@
/*
* File : ls1c_i2c.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-06 first version
*/
// 硬件i2c接口的头文件
#ifndef __OPENLOONGSON_I2C_H
#define __OPENLOONGSON_I2C_H
// I2C的时钟频率
#define LS1C_I2C_CLOCK_DEFAULT (100000) // Hz. 默认频率
#define LS1C_I2C_CLOCK_MAX (400000) // Hz, max 400 Kbits/sec
// I2C模块编号
typedef enum
{
LS1C_I2C_0 = 0,
LS1C_I2C_1,
LS1C_I2C_2
}ls1c_i2c_t;
// I2C数据传输方向
typedef enum
{
LS1C_I2C_DIRECTION_WRITE = 0, // 主机向从机写信息
LS1C_I2C_DIRECTION_READ, // 主机向从机读信息
}ls1c_i2c_direction_t;
// 硬件I2C信息
typedef struct
{
ls1c_i2c_t I2Cx; // i2c模块编号
unsigned long clock; // i2c时钟频率单位hz
}ls1c_i2c_info_t;
// I2C应答
typedef enum
{
LS1C_I2C_ACK = 0, // 收到应答
LS1C_I2C_NACK = 1, // 没收到应答
}ls1c_i2c_ack_t;
// 函数返回值
typedef enum
{
LS1C_I2C_RET_OK = 0, // OK
LS1C_I2C_RET_TIMEOUT, // 超时
}ls1c_i2c_ret_t;
/*
* i2c模块
* @i2c_info_p i2c模块的信息
*/
void i2c_init(ls1c_i2c_info_t *i2c_info_p);
/*
* ()ACK信号
* @i2c_info_p i2c模块信息
* @ret LS1C_I2C_ACK or LS1C_I2C_NACK
*/
ls1c_i2c_ack_t i2c_receive_ack(ls1c_i2c_info_t *i2c_info_p);
/*
*
* @i2c_info_p i2c模块信息
* @buf
* @len
*/
ls1c_i2c_ret_t i2c_receive_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *buf, int len);
/*
* START信号和地址
* @i2c_info_p i2c模块信息
* @slave_addr
* @direction ()
*/
ls1c_i2c_ret_t i2c_send_start_and_addr(ls1c_i2c_info_t *i2c_info_p,
unsigned char slave_addr,
ls1c_i2c_direction_t direction);
/*
*
* @i2c_info_p i2c模块信息
* @data
* @len
*/
ls1c_i2c_ret_t i2c_send_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *data, int len);
/*
* STOP信号
* @i2c_info_p i2c模块信息
*/
void i2c_send_stop(ls1c_i2c_info_t *i2c_info_p);
#endif

View File

@ -54,6 +54,28 @@ unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit)
} }
/*
* 8bit()
* @data
* @addr
*/
void reg_write_8(unsigned char data, volatile unsigned char *addr)
{
*addr = data;
}
/*
* 8bit()
* @addr
* @ret
*/
unsigned char reg_read_8(volatile unsigned char *addr)
{
return (*addr);
}
/* /*
* 32bit的数据 * 32bit的数据
* @data * @data

View File

@ -57,6 +57,22 @@ void reg_clr_one_bit(volatile unsigned int *reg, unsigned int bit);
unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit); unsigned int reg_get_bit(volatile unsigned int *reg, unsigned int bit);
/*
* 8bit()
* @data
* @addr
*/
void reg_write_8(unsigned char data, volatile unsigned char *addr);
/*
* 8bit()
* @addr
* @ret
*/
unsigned char reg_read_8(volatile unsigned char *addr);
/* /*
* 32bit的数据 * 32bit的数据
* @data * @data

View File

@ -111,6 +111,11 @@
#define LS1C_INT4_EDGE (0xbfd010b4) #define LS1C_INT4_EDGE (0xbfd010b4)
// I2C寄存器
#define LS1C_I2C0_BASE (0xbfe58000)
#define LS1C_I2C1_BASE (0xbfe68000)
#define LS1C_I2C2_BASE (0xbfe70000)
#endif #endif