mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-01-18 11:43:34 +08:00
[add] 添加 DAC、ADC、SPI 驱动
This commit is contained in:
parent
ddc5a84dc9
commit
ec311ee500
@ -95,6 +95,52 @@ menu "Hardware Drivers Config"
|
||||
endchoice
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_SPI
|
||||
bool "Enable SPI BUS"
|
||||
default n
|
||||
select RT_USING_SPI
|
||||
if BSP_USING_SPI
|
||||
config BSP_SPI_USING_DTC_DMA
|
||||
bool "Enable SPI DTC transfers data without using the CPU."
|
||||
default n
|
||||
|
||||
config BSP_USING_SPI0
|
||||
bool "Enable SPI0 BUS"
|
||||
default n
|
||||
|
||||
config BSP_USING_SPI1
|
||||
bool "Enable SPI1 BUS"
|
||||
default n
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_ADC
|
||||
bool "Enable ADC"
|
||||
default n
|
||||
select RT_USING_ADC
|
||||
if BSP_USING_ADC
|
||||
config BSP_USING_ADC0
|
||||
bool "Enable ADC0"
|
||||
default n
|
||||
|
||||
config BSP_USING_ADC1
|
||||
bool "Enable ADC1"
|
||||
default n
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_DAC
|
||||
bool "Enable DAC"
|
||||
default n
|
||||
select RT_USING_DAC
|
||||
if BSP_USING_DAC
|
||||
config BSP_USING_DAC0
|
||||
bool "Enable DAC0"
|
||||
default n
|
||||
|
||||
config BSP_USING_DAC1
|
||||
bool "Enable DAC1"
|
||||
default n
|
||||
endif
|
||||
|
||||
endmenu
|
||||
|
||||
menu "Board extended module Drivers"
|
||||
|
@ -29,6 +29,15 @@ if GetDepend(['RT_USING_I2C', 'RT_USING_I2C_BITOPS']):
|
||||
if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'):
|
||||
src += ['drv_soft_i2c.c']
|
||||
|
||||
if GetDepend(['RT_USING_SPI']):
|
||||
src += ['drv_spi.c']
|
||||
|
||||
if GetDepend(['RT_USING_ADC']):
|
||||
src += ['drv_adc.c']
|
||||
|
||||
if GetDepend(['RT_USING_DAC']):
|
||||
src += ['drv_dac.c']
|
||||
|
||||
path = [cwd]
|
||||
path += [cwd + '/config']
|
||||
|
||||
|
@ -21,6 +21,14 @@ extern "C" {
|
||||
#ifdef SOC_SERIES_R7FA6M4AF
|
||||
#include "ra6m4/uart_config.h"
|
||||
|
||||
#ifdef RT_USING_ADC
|
||||
#include "ra6m4/adc_config.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_DAC
|
||||
#include "ra6m4/dac_config.h"
|
||||
#endif
|
||||
|
||||
#endif/* SOC_SERIES_R7FA6M4AF */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
41
bsp/ra6m4-cpk/drivers/config/ra6m4/adc_config.h
Normal file
41
bsp/ra6m4-cpk/drivers/config/ra6m4/adc_config.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-08-19 Mr.Tiger first version
|
||||
*/
|
||||
|
||||
#ifndef __ADC_CONFIG_H__
|
||||
#define __ADC_CONFIG_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "hal_data.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
|
||||
struct ra_adc_map
|
||||
{
|
||||
char name;
|
||||
const adc_cfg_t *g_cfg;
|
||||
const adc_instance_ctrl_t *g_ctrl;
|
||||
const adc_channel_cfg_t *g_channel_cfg;
|
||||
};
|
||||
|
||||
struct ra_dev
|
||||
{
|
||||
rt_adc_device_t ra_adc_device_t;
|
||||
struct ra_adc_map *ra_adc_dev;
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
41
bsp/ra6m4-cpk/drivers/config/ra6m4/dac_config.h
Normal file
41
bsp/ra6m4-cpk/drivers/config/ra6m4/dac_config.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-08-19 Mr.Tiger first version
|
||||
*/
|
||||
|
||||
#ifndef __DAC_CONFIG_H__
|
||||
#define __DAC_CONFIG_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "hal_data.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_DAC0) && defined(BSP_USING_DAC1)
|
||||
struct ra_dac_map
|
||||
{
|
||||
char name;
|
||||
const struct st_dac_cfg *g_cfg;
|
||||
const struct st_dac_instance_ctrl *g_ctrl;
|
||||
};
|
||||
|
||||
struct ra_dac_dev
|
||||
{
|
||||
rt_dac_device_t ra_dac_device_t;
|
||||
struct ra_dac_map *ra_dac_map_dev;
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
132
bsp/ra6m4-cpk/drivers/drv_adc.c
Normal file
132
bsp/ra6m4-cpk/drivers/drv_adc.c
Normal file
@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-08-19 Mr.Tiger first version
|
||||
*/
|
||||
|
||||
#include "drv_config.h"
|
||||
#ifdef RT_USING_ADC
|
||||
|
||||
// #define DRV_DEBUG
|
||||
#define DBG_TAG "drv.adc"
|
||||
#ifdef DRV_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* DRV_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct ra_adc_map ra_adc[] =
|
||||
{
|
||||
#if defined(BSP_USING_ADC0)
|
||||
{'0', &g_adc0_cfg, &g_adc0_ctrl, &g_adc0_channel_cfg},
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_ADC1)
|
||||
{'1', &g_adc1_cfg, &g_adc1_ctrl, &g_adc1_channel_cfg},
|
||||
#endif
|
||||
};
|
||||
|
||||
#if defined(BSP_USING_ADC0)
|
||||
struct rt_adc_device adc0_device;
|
||||
struct ra_dev _ra_adc0_device = {.ra_adc_device_t = &adc0_device, .ra_adc_dev = &ra_adc[0]};
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_ADC1)
|
||||
struct rt_adc_device adc1_device;
|
||||
struct ra_dev _ra_adc1_device = {.ra_adc_device_t = &adc1_device, .ra_adc_dev = &ra_adc[1]};
|
||||
#endif
|
||||
|
||||
static rt_err_t ra_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct ra_adc_map *adc = (struct ra_adc_map *)device->parent.user_data;
|
||||
/**< start adc*/
|
||||
if (enabled)
|
||||
{
|
||||
if (FSP_SUCCESS != R_ADC_ScanStart((adc_ctrl_t *)adc->g_ctrl))
|
||||
{
|
||||
LOG_E("start adc%c failed.", adc->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/**< stop adc*/
|
||||
if (FSP_SUCCESS != R_ADC_ScanStop((adc_ctrl_t *)adc->g_ctrl))
|
||||
{
|
||||
LOG_E("stop adc%c failed.", adc->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t ra_adc_close(struct rt_adc_device *device)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct ra_adc_map *adc = (struct ra_adc_map *)(struct ra_adc_map *)device->parent.user_data;
|
||||
if (FSP_SUCCESS != R_ADC_Close((adc_ctrl_t *)adc->g_ctrl))
|
||||
{
|
||||
LOG_E("close adc%c failed.", adc->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t ra_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct ra_adc_map *adc = (struct ra_adc_map *)device->parent.user_data;
|
||||
if (RT_EOK != R_ADC_Read32((adc_ctrl_t *)adc->g_ctrl, channel, value))
|
||||
{
|
||||
LOG_E("get adc value failed.\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static const struct rt_adc_ops ra_adc_ops =
|
||||
{
|
||||
.enabled = ra_adc_enabled,
|
||||
.convert = ra_get_adc_value,
|
||||
};
|
||||
|
||||
static int ra_adc_init(void)
|
||||
{
|
||||
#if defined(BSP_USING_ADC0)
|
||||
R_ADC_Open((adc_ctrl_t *)_ra_adc0_device.ra_adc_dev->g_ctrl,
|
||||
(adc_cfg_t const * const)_ra_adc0_device.ra_adc_dev->g_cfg);
|
||||
|
||||
R_ADC_ScanCfg((adc_ctrl_t *)_ra_adc0_device.ra_adc_dev->g_ctrl,
|
||||
(adc_cfg_t const * const)_ra_adc0_device.ra_adc_dev->g_channel_cfg);
|
||||
|
||||
if (RT_EOK != rt_hw_adc_register(_ra_adc0_device.ra_adc_device_t, "adc0", &ra_adc_ops, (void *)_ra_adc0_device.ra_adc_dev))
|
||||
{
|
||||
LOG_E("adc0 register failed");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_ADC1)
|
||||
R_ADC_Open((adc_ctrl_t *)_ra_adc1_device.ra_adc_dev->g_ctrl,
|
||||
(adc_cfg_t const * const)_ra_adc1_device.ra_adc_dev->g_cfg);
|
||||
|
||||
R_ADC_ScanCfg((adc_ctrl_t *)_ra_adc1_device.ra_adc_dev->g_ctrl,
|
||||
(adc_cfg_t const * const)_ra_adc1_device.ra_adc_dev->g_channel_cfg);
|
||||
|
||||
if (RT_EOK != rt_hw_adc_register(_ra_adc1_device.ra_adc_device_t, "adc1", &ra_adc_ops, (void *)_ra_adc1_device.ra_adc_dev))
|
||||
{
|
||||
LOG_E("adc1 register failed");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_BOARD_EXPORT(ra_adc_init);
|
||||
#endif
|
113
bsp/ra6m4-cpk/drivers/drv_dac.c
Normal file
113
bsp/ra6m4-cpk/drivers/drv_dac.c
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-08-19 Mr.Tiger first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "drv_config.h"
|
||||
#ifdef RT_USING_DAC
|
||||
|
||||
//#define DRV_DEBUG
|
||||
#define DBG_TAG "drv.dac"
|
||||
#ifdef DRV_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* DRV_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct ra_dac_map ra_dac[] =
|
||||
{
|
||||
#ifdef BSP_USING_DAC0
|
||||
{'0', &g_dac0_cfg, &g_dac0_ctrl},
|
||||
#endif
|
||||
#ifdef BSP_USING_DAC1
|
||||
{'1', &g_dac1_cfg, &g_dac1_ctrl},
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef BSP_USING_DAC0
|
||||
struct rt_dac_device dac0_device;
|
||||
struct ra_dac_dev _ra_dac0_device = {.ra_dac_device_t = &dac0_device, .ra_dac_map_dev = &ra_dac[0]};
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_DAC1
|
||||
struct rt_dac_device dac1_device;
|
||||
struct ra_dac_dev _ra_dac1_device = {.ra_dac_device_t = &dac1_device, .ra_dac_map_dev = &ra_dac[1]};
|
||||
#endif
|
||||
|
||||
rt_err_t ra_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct ra_dac_map *dac = (struct ra_dac_map *)device->parent.user_data;
|
||||
if (FSP_SUCCESS != R_DAC_Stop((dac_ctrl_t *)dac->g_ctrl))
|
||||
{
|
||||
LOG_E("dac%c stop failed.", dac->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t ra_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct ra_dac_map *dac = (struct ra_dac_map *)device->parent.user_data;
|
||||
if (FSP_SUCCESS != R_DAC_Start((dac_ctrl_t *)dac->g_ctrl))
|
||||
{
|
||||
LOG_E("dac%c start failed.", dac->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t ra_dac_write(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
struct ra_dac_map *dac = (struct ra_dac_map *)device->parent.user_data;
|
||||
if (FSP_SUCCESS != R_DAC_Write((dac_ctrl_t *)dac->g_ctrl, *value))
|
||||
{
|
||||
LOG_E("dac%c set value failed.", dac->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
struct rt_dac_ops ra_dac_ops =
|
||||
{
|
||||
.disabled = ra_dac_disabled,
|
||||
.enabled = ra_dac_enabled,
|
||||
.convert = ra_dac_write,
|
||||
};
|
||||
|
||||
static int ra_dac_init(void)
|
||||
{
|
||||
#ifdef BSP_USING_DAC0
|
||||
_ra_dac0_device.ra_dac_device_t->ops = &ra_dac_ops;
|
||||
R_DAC_Open((dac_ctrl_t *)_ra_dac0_device.ra_dac_map_dev->g_ctrl, (dac_cfg_t const *)_ra_dac0_device.ra_dac_map_dev->g_cfg);
|
||||
if (FSP_SUCCESS != rt_hw_dac_register(_ra_dac0_device.ra_dac_device_t, "dac0", &ra_dac_ops, (void *)_ra_dac0_device.ra_dac_map_dev))
|
||||
{
|
||||
LOG_E("dac0 register failed");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_DAC1
|
||||
_ra_dac1_device.ra_dac_device_t->ops = &ra_dac_ops;
|
||||
R_DAC_Open((dac_ctrl_t *)_ra_dac1_device.ra_dac_map_dev->g_ctrl, (dac_cfg_t const *) _ra_dac1_device.ra_dac_map_dev->g_cfg);
|
||||
if (FSP_SUCCESS != rt_hw_dac_register(_ra_dac1_device.ra_dac_device_t, "dac1", &ra_dac_ops, (void *)_ra_dac1_device.ra_dac_map_dev))
|
||||
{
|
||||
LOG_E("dac1 register failed");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_DEVICE_EXPORT(ra_dac_init);
|
||||
|
||||
#endif
|
235
bsp/ra6m4-cpk/drivers/drv_spi.c
Normal file
235
bsp/ra6m4-cpk/drivers/drv_spi.c
Normal file
@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-08-23 Mr.Tiger first version
|
||||
*/
|
||||
/**< Note : Turn on any DMA mode and all SPIs will turn on DMA */
|
||||
|
||||
#include "drv_spi.h"
|
||||
|
||||
#ifdef RT_USING_SPI
|
||||
|
||||
//#define DRV_DEBUG
|
||||
#define DBG_TAG "drv.spi"
|
||||
#ifdef DRV_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* DRV_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
static struct ra_spi_handle spi_handle[] =
|
||||
{
|
||||
#ifdef BSP_USING_SPI0
|
||||
{.bus_name = "spi0", .spi_ctrl_t = &g_spi0_ctrl, .spi_cfg_t = &g_spi0_cfg,},
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_SPI1
|
||||
{.bus_name = "spi1", .spi_ctrl_t = &g_spi1_ctrl, .spi_cfg_t = &g_spi1_cfg,},
|
||||
#endif
|
||||
};
|
||||
|
||||
static struct ra_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = {0};
|
||||
|
||||
void g_spi0_callback(spi_callback_args_t *p_args)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
|
||||
{
|
||||
LOG_D("SPI0 cb");
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
void g_spi1_callback(spi_callback_args_t *p_args)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
|
||||
{
|
||||
LOG_D("SPI1 cb");
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
|
||||
{
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(device->parent.user_data != NULL);
|
||||
RT_ASSERT(send_buf != NULL);
|
||||
RT_ASSERT(len > 0);
|
||||
rt_err_t err = RT_EOK;
|
||||
struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
|
||||
spi_dev->cs_pin = *(rt_uint32_t *)device->parent.user_data;
|
||||
|
||||
/**< Configure Select Line */
|
||||
R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_HIGH);
|
||||
|
||||
/* Start a write transfer */
|
||||
R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_LOW);
|
||||
|
||||
/**< send msessage */
|
||||
err = R_SPI_Write((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, send_buf, len, spi_dev->rt_spi_cfg_t->data_width);
|
||||
if (RT_EOK != err)
|
||||
{
|
||||
LOG_E("%s write failed.", spi_dev->ra_spi_handle_t->bus_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
|
||||
{
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(device->parent.user_data != NULL);
|
||||
RT_ASSERT(recv_buf != NULL);
|
||||
RT_ASSERT(len > 0);
|
||||
rt_err_t err = RT_EOK;
|
||||
struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
|
||||
spi_dev->cs_pin = *(rt_uint32_t *)device->parent.user_data;
|
||||
|
||||
/**< Configure Select Line */
|
||||
R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_HIGH);
|
||||
|
||||
/* Start read transfer */
|
||||
R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_LOW);
|
||||
|
||||
/**< receive message */
|
||||
err = R_SPI_Read((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, recv_buf, len, spi_dev->rt_spi_cfg_t->data_width);
|
||||
if (RT_EOK != err)
|
||||
{
|
||||
LOG_E("\n%s write failed.\n", spi_dev->ra_spi_handle_t->bus_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
|
||||
{
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(message != NULL);
|
||||
RT_ASSERT(message->length > 0);
|
||||
rt_err_t err = RT_EOK;
|
||||
struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
|
||||
|
||||
/**< write and receive message */
|
||||
err = R_SPI_WriteRead((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, message->send_buf, message->recv_buf, message->length, spi_dev->rt_spi_cfg_t->data_width);
|
||||
if (RT_EOK != err)
|
||||
{
|
||||
LOG_E("%s write and read failed.", spi_dev->ra_spi_handle_t->bus_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return message->length;
|
||||
}
|
||||
|
||||
/**< init spi TODO : MSB does not support modification */
|
||||
static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
|
||||
struct rt_spi_configuration *configuration)
|
||||
{
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(configuration != NULL);
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
|
||||
spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
|
||||
|
||||
/**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
|
||||
rt_uint8_t data_width = configuration->data_width / 8;
|
||||
RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
|
||||
configuration->data_width = configuration->data_width / 8;
|
||||
spi_dev->rt_spi_cfg_t = configuration;
|
||||
|
||||
spi_extended_cfg_t *spi_cfg = (spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
|
||||
|
||||
/**< Configure Select Line */
|
||||
R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_HIGH);
|
||||
|
||||
/**< config bitrate */
|
||||
R_SPI_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, &spi_cfg->spck_div);
|
||||
|
||||
/**< init */
|
||||
err = R_SPI_Open((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, (spi_cfg_t const * const)spi_dev->ra_spi_handle_t->spi_cfg_t);
|
||||
/* handle error */
|
||||
if (RT_EOK != err)
|
||||
{
|
||||
LOG_E("%s init failed.", spi_dev->ra_spi_handle_t->bus_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_uint32_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
|
||||
{
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(message != NULL);
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
if (message->length <= 0)
|
||||
{
|
||||
LOG_E("buf length err.");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
|
||||
{
|
||||
/**< receive message */
|
||||
err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
|
||||
}
|
||||
else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
|
||||
{
|
||||
/**< send message */
|
||||
err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
|
||||
}
|
||||
else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
|
||||
{
|
||||
/**< send and receive message */
|
||||
err = ra_write_read_message(device, message);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static const struct rt_spi_ops ra_spi_ops =
|
||||
{
|
||||
.configure = ra_hw_spi_configure,
|
||||
.xfer = ra_spixfer,
|
||||
};
|
||||
|
||||
void rt_hw_spi_device_attach(struct rt_spi_device *device, const char *device_name, const char *bus_name, void *user_data)
|
||||
{
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(device_name != NULL);
|
||||
RT_ASSERT(bus_name != NULL);
|
||||
RT_ASSERT(user_data != NULL);
|
||||
|
||||
rt_err_t err = rt_spi_bus_attach_device(device, device_name, bus_name, user_data);
|
||||
if (RT_EOK != err)
|
||||
{
|
||||
LOG_E("%s attach failed.", bus_name);
|
||||
}
|
||||
}
|
||||
|
||||
int ra_hw_spi_init(void)
|
||||
{
|
||||
for (rt_uint8_t spi_index = 0; spi_index < sizeof(spi_handle) / sizeof(spi_handle[0]); spi_index++)
|
||||
{
|
||||
spi_config[spi_index].ra_spi_handle_t = &spi_handle[spi_index];
|
||||
|
||||
/**< register spi bus */
|
||||
rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].bus, spi_handle[spi_index].bus_name, &ra_spi_ops);
|
||||
if (RT_EOK != err)
|
||||
{
|
||||
LOG_E("%s bus register failed.", spi_config[spi_index].ra_spi_handle_t->bus_name);
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_BOARD_EXPORT(ra_hw_spi_init);
|
||||
#endif /* RT_USING_SPI */
|
51
bsp/ra6m4-cpk/drivers/drv_spi.h
Normal file
51
bsp/ra6m4-cpk/drivers/drv_spi.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-08-23 Mr.Tiger first version
|
||||
*/
|
||||
|
||||
#ifndef __DRV_SPI_H__
|
||||
#define __DRV_SPI_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "hal_data.h"
|
||||
#include "board.h"
|
||||
#include <rthw.h>
|
||||
#include <drv_common.h>
|
||||
#include <drv_config.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef R_SPI_H
|
||||
struct ra_spi_handle
|
||||
{
|
||||
const char *bus_name;
|
||||
const spi_cfg_t *spi_cfg_t;
|
||||
const spi_instance_ctrl_t *spi_ctrl_t;
|
||||
};
|
||||
|
||||
struct ra_spi
|
||||
{
|
||||
rt_uint32_t cs_pin;
|
||||
struct ra_spi_handle *ra_spi_handle_t;
|
||||
struct rt_spi_configuration *rt_spi_cfg_t;
|
||||
struct rt_spi_bus bus;
|
||||
};
|
||||
#endif
|
||||
|
||||
void rt_hw_spi_device_attach(struct rt_spi_device *device, const char *device_name, const char *bus_name, void *user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/* stm32 spi dirver class */
|
||||
|
||||
#endif /*__DRV_SPI_H__ */
|
Loading…
x
Reference in New Issue
Block a user