mirror of
https://github.com/apache/nuttx.git
synced 2026-05-12 01:42:49 +08:00
mpfs: add i2c driver
This adds mpfs i2c driver. Signed-off-by: Eero Nurkkala <eero.nurkkala@offcode.fi>
This commit is contained in:
committed by
Xiang Xiao
parent
361703a881
commit
1bce864ef7
@@ -58,7 +58,7 @@ Peripheral Support NOTES
|
||||
GPIO Yes
|
||||
MMUART Yes Uart mode only
|
||||
SPI Yes
|
||||
I2C No
|
||||
I2C Yes
|
||||
Timers No
|
||||
Watchdog No
|
||||
RTC No
|
||||
|
||||
@@ -93,6 +93,14 @@ config MPFS_UART4
|
||||
select ARCH_HAVE_SERIAL_TERMIOS
|
||||
select MPFS_HAVE_UART4
|
||||
|
||||
config MPFS_I2C0
|
||||
bool "I2C 0"
|
||||
default n
|
||||
|
||||
config MPFS_I2C1
|
||||
bool "I2C 1"
|
||||
default n
|
||||
|
||||
endmenu
|
||||
|
||||
menu "MPFS Others"
|
||||
|
||||
@@ -65,3 +65,7 @@ endif
|
||||
ifeq ($(CONFIG_SPI),y)
|
||||
CHIP_CSRCS += mpfs_spi.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_I2C),y)
|
||||
CHIP_CSRCS += mpfs_i2c.c
|
||||
endif
|
||||
|
||||
Executable
+60
@@ -0,0 +1,60 @@
|
||||
/****************************************************************************
|
||||
* arch/risc-v/src/mpfs/hardware/mpfs_i2c.h
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __ARCH_RISCV_SRC_MPFS_HARDWARE_MPFS_I2C_H
|
||||
#define __ARCH_RISCV_SRC_MPFS_HARDWARE_MPFS_I2C_H
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define MPFS_I2C_CTRL_CR2 7
|
||||
#define MPFS_I2C_CTRL_ENS1 6
|
||||
#define MPFS_I2C_CTRL_STA 5
|
||||
#define MPFS_I2C_CTRL_STO 4
|
||||
#define MPFS_I2C_CTRL_SI 3
|
||||
#define MPFS_I2C_CTRL_AA 2
|
||||
#define MPFS_I2C_CTRL_CR1 1
|
||||
#define MPFS_I2C_CTRL_CR0 0
|
||||
|
||||
#define MPFS_I2C_CTRL_CR2_MASK (1 << 7)
|
||||
#define MPFS_I2C_CTRL_ENS1_MASK (1 << 6)
|
||||
#define MPFS_I2C_CTRL_STA_MASK (1 << 5)
|
||||
#define MPFS_I2C_CTRL_STO_MASK (1 << 4)
|
||||
#define MPFS_I2C_CTRL_SI_MASK (1 << 3)
|
||||
#define MPFS_I2C_CTRL_AA_MASK (1 << 2)
|
||||
#define MPFS_I2C_CTRL_CR1_MASK (1 << 1)
|
||||
#define MPFS_I2C_CTRL_CR0_MASK (1 << 0)
|
||||
|
||||
#define MPFS_I2C_ST_RESET_ACTIVATED 0xD0 /* Master reset is activated */
|
||||
#define MPFS_I2C_ST_RX_DATA_NACK 0x58 /* Data received, NACK sent */
|
||||
#define MPFS_I2C_ST_RX_DATA_ACK 0x50 /* Data received, ACK sent */
|
||||
#define MPFS_I2C_ST_SLAR_NACK 0x48 /* SLA+R sent, NACK'ed */
|
||||
#define MPFS_I2C_ST_SLAR_ACK 0x40 /* SLA+R sent, ACK'ed */
|
||||
#define MPFS_I2C_ST_LOST_ARB 0x38 /* Master lost arbitration */
|
||||
#define MPFS_I2C_ST_TX_DATA_NACK 0x30 /* Data sent, NACK'ed */
|
||||
#define MPFS_I2C_ST_TX_DATA_ACK 0x28 /* Data sent, ACK'ed */
|
||||
#define MPFS_I2C_ST_SLAW_NACK 0x20 /* SLA + W sent, nack received */
|
||||
#define MPFS_I2C_ST_SLAW_ACK 0x18 /* SLA + W sent, ack received */
|
||||
#define MPFS_I2C_ST_RESTART 0x10 /* Repeated start */
|
||||
#define MPFS_I2C_ST_START 0x08 /* Start condition sent */
|
||||
#define MPFS_I2C_ST_BUS_ERROR 0x00 /* Bus error */
|
||||
|
||||
#endif /* __ARCH_RISCV_SRC_MPFS_HARDWARE_MPFS_I2C_H */
|
||||
Executable
+810
File diff suppressed because it is too large
Load Diff
Executable
+93
@@ -0,0 +1,93 @@
|
||||
/****************************************************************************
|
||||
* arch/risc-v/src/mpfs/mpfs_i2c.h
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __ARCH_RISCV_SRC_MPFS_I2C_H
|
||||
#define __ARCH_RISCV_SRC_MPFS_I2C_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/i2c/i2c_master.h>
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mpfs_i2cbus_initialize
|
||||
*
|
||||
* Description:
|
||||
* Initialize the selected I2C port. And return a pointer to an unique
|
||||
* instance of struct i2c_master_s. This function may be called to obtain
|
||||
* multiple instances of the interface.
|
||||
*
|
||||
* Input Parameters:
|
||||
* port - Port number of the I2C interface to be initialized.
|
||||
*
|
||||
* Returned Value:
|
||||
* Pointer to valid I2C device structure is returned on success.
|
||||
* A NULL pointer is returned on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct i2c_master_s *mpfs_i2cbus_initialize(int port);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mpfs_i2cbus_uninitialize
|
||||
*
|
||||
* Description:
|
||||
* De-initialize the selected I2C port and power down the device.
|
||||
*
|
||||
* Input Parameters:
|
||||
* dev - Device structure as returned by
|
||||
* mpfs_i2cbus_initialize()
|
||||
*
|
||||
* Returned Value:
|
||||
* OK is returned on success. ERROR is returned when internal reference
|
||||
* count mismatches or dev points to invalid hardware device.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int mpfs_i2cbus_uninitialize(struct i2c_master_s *dev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#undef EXTERN
|
||||
|
||||
#endif /* __ASSEMBLY__ */
|
||||
#endif /* __ARCH_RISCV_SRC_MPFS_I2C_H */
|
||||
@@ -26,6 +26,10 @@ ifeq ($(CONFIG_LIB_BOARDCTL),y)
|
||||
CSRCS += mpfs_appinit.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_I2C),y)
|
||||
CSRCS += mpfs_i2c.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ARCH_LEDS),y)
|
||||
CSRCS += mpfs_autoleds.c
|
||||
endif
|
||||
|
||||
@@ -48,6 +48,17 @@ int mpfs_bringup(void)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
#if defined(CONFIG_I2C_DRIVER)
|
||||
/* Configure I2C peripheral interfaces */
|
||||
|
||||
ret = mpfs_board_i2c_init();
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
syslog(LOG_ERR, "Failed to initialize I2C driver: %d\n", ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_FS_PROCFS
|
||||
/* Mount the procfs file system */
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/****************************************************************************
|
||||
* boards/risc-v/mpfs/icicle/src/mpfs_i2c.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <debug.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <nuttx/i2c/i2c_master.h>
|
||||
|
||||
#include "mpfs_i2c.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: board_i2c_init
|
||||
*
|
||||
* Description:
|
||||
* Configure the I2C driver.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) is returned on success; A negated errno value is returned
|
||||
* to indicate the nature of any failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int mpfs_board_i2c_init(void)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
#if defined(CONFIG_MPFS_I2C0) || defined(CONFIG_MPFS_I2C1)
|
||||
#ifdef CONFIG_I2C_DRIVER
|
||||
int bus = 0;
|
||||
#endif
|
||||
FAR struct i2c_master_s *i2c;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_MPFS_I2C0
|
||||
i2c = mpfs_i2cbus_initialize(0);
|
||||
|
||||
if (i2c == NULL)
|
||||
{
|
||||
i2cerr("ERROR: Failed to init I2C0 interface\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_I2C_DRIVER
|
||||
ret = i2c_register(i2c, bus++);
|
||||
if (ret < 0)
|
||||
{
|
||||
i2cerr("ERROR: Failed to register I2C0 driver: %d\n", ret);
|
||||
mpfs_i2cbus_uninitialize(i2c);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_MPFS_I2C1
|
||||
i2c = mpfs_i2cbus_initialize(1);
|
||||
|
||||
if (i2c == NULL)
|
||||
{
|
||||
i2cerr("ERROR: Failed to init I2C1 interface\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_I2C_DRIVER
|
||||
ret = i2c_register(i2c, bus);
|
||||
if (ret < 0)
|
||||
{
|
||||
i2cerr("ERROR: Failed to register I2C1 driver: %d\n", ret);
|
||||
mpfs_i2cbus_uninitialize(i2c);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -43,5 +43,6 @@
|
||||
|
||||
int mpfs_bringup(void);
|
||||
int mpfs_board_spi_init(void);
|
||||
int mpfs_board_i2c_init(void);
|
||||
|
||||
#endif /* __BOARDS_RISCV_ICICLE_MPFS_SRC_MPFSICICLE_H */
|
||||
|
||||
Reference in New Issue
Block a user