add spi twi drv for nrf5340

This commit is contained in:
andrew li authored and Man, Jianting (Meco) committed 2023-01-29 21:30:41 -05:00
1 parent 450cd68a4a
commit 5f4b4ae5f1
3 files changed
+470 -6

No files matched your search

+43 -3
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -7,14 +7,16 @@
* Date Author Notes
* 2020-11-15 xckhmf First Verison
* 2021-11-27 chenyingchun fix _master_xfer bug
*
* 2023-01-28 Andrew add Nrf5340 support
*/
#include <rtdevice.h>
#include <nrfx_twi_twim.h>
#include <nrfx_twim.h>
#include <drv_i2c.h>
#if defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1)
#if defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3)
typedef struct
{
nrf_twim_frequency_t freq;
@@ -43,6 +45,28 @@ static drv_i2c_cfg_t drv_i2c_1 =
};
static struct rt_i2c_bus_device i2c1_bus;
#endif
#ifdef BSP_USING_I2C2
static drv_i2c_cfg_t drv_i2c_2 =
{
.freq = NRF_TWIM_FREQ_400K,
.scl_pin = BSP_I2C2_SCL_PIN,
.sda_pin = BSP_I2C2_SDA_PIN,
.twi_instance = NRFX_TWIM_INSTANCE(2)
};
static struct rt_i2c_bus_device i2c2_bus;
#endif
#ifdef BSP_USING_I2C3
static drv_i2c_cfg_t drv_i2c_3 =
{
.freq = NRF_TWIM_FREQ_400K,
.scl_pin = BSP_I2C3_SCL_PIN,
.sda_pin = BSP_I2C3_SDA_PIN,
.twi_instance = NRFX_TWIM_INSTANCE(3)
};
static struct rt_i2c_bus_device i2c3_bus;
#endif
static int twi_master_init(struct rt_i2c_bus_device *bus)
{
nrfx_err_t rtn;
@@ -128,6 +152,22 @@ int rt_hw_i2c_init(void)
twi_master_init(&i2c1_bus);
rt_i2c_bus_device_register(&i2c1_bus, "i2c1");
#endif
#ifdef BSP_USING_I2C2
i2c2_bus.ops= &_i2c_ops;
i2c2_bus.timeout = 0;
i2c2_bus.priv = (void *)&drv_i2c_2;
twi_master_init(&i2c2_bus);
rt_i2c_bus_device_register(&i2c2_bus, "i2c2");
#endif
#ifdef BSP_USING_I2C3
i2c3_bus.ops= &_i2c_ops;
i2c3_bus.timeout = 0;
i2c3_bus.priv = (void *)&drv_i2c_3;
twi_master_init(&i2c3_bus);
rt_i2c_bus_device_register(&i2c3_bus, "i2c3");
#endif
return 0;
}
File diff suppressed because it is too large. Load diff
+76 -2
View File
@@ -1,11 +1,12 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-05-19 Sherman first version
* 2023-01-28 Andrew add Nrf5340 support
*/
#include <rtthread.h>
@@ -16,6 +17,79 @@
#define __DRV_SPI_H_
#ifdef BSP_USING_SPI
#if defined(SOC_NRF5340)
#include "nrfx_spim.h"
/**
* @brief Attach the spi device to SPI bus, this function must be used after initialization.
* @param bus_name spi bus name "spi0"/"spi1"/"spi2"
* @param device_name spi device name "spi0x"/"spi1x"/"spi2x"
* @param ss_pin spi ss pin number
* @retval RT_ERROR / RT_EOK
*/
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin);
//SPI bus config
#ifdef BSP_USING_SPI0
#define NRFX_SPI0_CONFIG \
{ \
.bus_name = "spi0", \
.spi = NRFX_SPIM_INSTANCE(0) \
}
#endif
#ifdef BSP_USING_SPI1
#define NRFX_SPI1_CONFIG \
{ \
.bus_name = "spi1", \
.spi = NRFX_SPIM_INSTANCE(1) \
}
#endif
#ifdef BSP_USING_SPI2
#define NRFX_SPI2_CONFIG \
{ \
.bus_name = "spi2", \
.spi = NRFX_SPIM_INSTANCE(2) \
}
#endif
#ifdef BSP_USING_SPI3
#define NRFX_SPI3_CONFIG \
{ \
.bus_name = "spi3", \
.spi = NRFX_SPIM_INSTANCE(3) \
}
#endif
#ifdef BSP_USING_SPI4
#define NRFX_SPI4_CONFIG \
{ \
.bus_name = "spi4", \
.spi = NRFX_SPIM_INSTANCE(4) \
}
#endif
struct nrfx_drv_spi_config
{
char *bus_name;
nrfx_spim_t spi;
};
struct nrfx_drv_spi
{
nrfx_spim_t spi; /* nrfx spi driver instance. */
nrfx_spim_config_t spi_config; /* nrfx spi config Configuration */
struct rt_spi_configuration *cfg;
struct rt_spi_bus spi_bus;
};
struct nrfx_drv_spi_pin_config
{
rt_uint8_t sck_pin;
rt_uint8_t mosi_pin;
rt_uint8_t miso_pin;
rt_uint8_t ss_pin;
};
#else
#include "nrfx_spi.h"
/**
@@ -72,6 +146,6 @@ struct nrfx_drv_spi_pin_config
rt_uint8_t miso_pin;
rt_uint8_t ss_pin;
};
#endif
#endif /* BSP_USING_SPI */
#endif /*__DRV_SPI_H_*/