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
6 changed files with 555 additions and 2 deletions
+5 -2
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)
{
volatile unsigned int *int_pol; // 中断极性选择寄存器
volatile unsigned int *int_edge; // 中断边沿选择寄存器
volatile unsigned int *int_pol = NULL; // 中断极性选择寄存器
volatile unsigned int *int_edge = NULL; // 中断边沿选择寄存器
unsigned int port = GPIO_GET_PORT(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_edge &= ~(1 << pin);
break;
default:
break;
}
return ;
File diff suppressed because it is too large Load Diff
+134
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
+22
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的数据
* @data 待写入的数据
+16
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);
/*
* 向寄存器中写入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的数据
* @data 待写入的数据
+5
View File
@@ -111,6 +111,11 @@
#define LS1C_INT4_EDGE (0xbfd010b4)
// I2C寄存器
#define LS1C_I2C0_BASE (0xbfe58000)
#define LS1C_I2C1_BASE (0xbfe68000)
#define LS1C_I2C2_BASE (0xbfe70000)
#endif