Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46254178
rt-thread
提交
6c7ae389
R
rt-thread
项目概览
weixin_46254178
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6c7ae389
编写于
2月 15, 2021
作者:
Thomas_Fly
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[bsp/max32660] add the driver of spi
上级
bfde3803
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
324 addition
and
0 deletion
+324
-0
bsp/maxim/MAX32660_EVSYS/board/Kconfig
bsp/maxim/MAX32660_EVSYS/board/Kconfig
+19
-0
bsp/maxim/libraries/HAL_Drivers/drv_spi.c
bsp/maxim/libraries/HAL_Drivers/drv_spi.c
+231
-0
bsp/maxim/libraries/HAL_Drivers/drv_spi.h
bsp/maxim/libraries/HAL_Drivers/drv_spi.h
+71
-0
bsp/maxim/libraries/MAX32660PeriphDriver/SConscript
bsp/maxim/libraries/MAX32660PeriphDriver/SConscript
+3
-0
未找到文件。
bsp/maxim/MAX32660_EVSYS/board/Kconfig
浏览文件 @
6c7ae389
...
...
@@ -43,6 +43,25 @@ menu "On-chip Peripheral Drivers"
depends on BSP_USING_UART1 && RT_SERIAL_USING_DMA
default n
endif
config BSP_USING_SPI
bool "Enable SPI"
select RT_USING_SPI
default n
if BSP_USING_SPI
config BSP_USING_SPI0
bool "Enable SPI0 bus [MISO P0.4;MOSI P0.5;SCL P0.6;SS P0.7]"
default y
config BSP_USING_SPI1
bool "Enable SPI1 bus [MISO P0.0;MOSI P0.1;SCL P0.2;SS P0.3]"
default n
if BSP_USING_SPI1
config BSP_USING_SPI1A
bool "Use SPI1A. [MISO P0.10;MOSI P0.11;SCL P0.12;SS P0.13]"
default n
endif
endif
config BSP_USING_ON_CHIP_FLASH
select PKG_USING_FAL
bool "Enable on-chip FLASH"
...
...
bsp/maxim/libraries/HAL_Drivers/drv_spi.c
0 → 100644
浏览文件 @
6c7ae389
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-02-14 supperthomas first version
*/
#include <stdint.h>
#include <string.h>
#include "board.h"
#include "drv_spi.h"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
#define LOG_TAG "drv.spi"
#ifdef BSP_USING_SPI
#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
static
struct
mcu_drv_spi_config
spi_config
[]
=
{
#ifdef BSP_USING_SPI0
MCU_SPI0_CONFIG
,
#endif
#ifdef BSP_USING_SPI1
MCU_SPI1_CONFIG
,
#endif
};
static
struct
mcu_drv_spi
spi_bus_obj
[
sizeof
(
spi_config
)
/
sizeof
(
spi_config
[
0
])];
/**
* @brief This function config spi bus
* @param device
* @param configuration
* @retval RT_EOK / RT_ERROR
*/
static
rt_err_t
spi_configure
(
struct
rt_spi_device
*
device
,
struct
rt_spi_configuration
*
configuration
)
{
RT_ASSERT
(
device
!=
RT_NULL
);
RT_ASSERT
(
device
->
bus
!=
RT_NULL
);
RT_ASSERT
(
device
->
bus
->
parent
.
user_data
!=
RT_NULL
);
RT_ASSERT
(
configuration
!=
RT_NULL
);
struct
mcu_drv_spi
*
tmp_spi
;
tmp_spi
=
rt_container_of
(
device
->
bus
,
struct
mcu_drv_spi
,
spi_bus
);
int
mode
;
///init
switch
(
configuration
->
mode
&
RT_SPI_MODE_3
)
{
case
RT_SPI_MODE_0
/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */
:
case
RT_SPI_MODE_1
/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */
:
case
RT_SPI_MODE_2
/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */
:
case
RT_SPI_MODE_3
/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */
:
mode
=
configuration
->
mode
&
RT_SPI_MODE_3
;
break
;
default:
LOG_E
(
"spi_configure mode error %x
\n
"
,
configuration
->
mode
);
return
RT_ERROR
;
}
tmp_spi
->
spixfer_req
.
width
=
SPI17Y_WIDTH_1
;
tmp_spi
->
spixfer_req
.
bits
=
configuration
->
data_width
;
tmp_spi
->
spixfer_req
.
ssel
=
0
;
tmp_spi
->
spixfer_req
.
deass
=
1
;
tmp_spi
->
spixfer_req
.
tx_num
=
0
;
tmp_spi
->
spixfer_req
.
rx_num
=
0
;
tmp_spi
->
spixfer_req
.
callback
=
NULL
;
LOG_D
(
"spi init mode:%d, rate:%d"
,
mode
,
configuration
->
max_hz
);
if
(
SPI_Init
(
tmp_spi
->
spi_instance
,
mode
,
configuration
->
max_hz
)
!=
0
)
{
LOG_E
(
"Error configuring SPI
\n
"
);
while
(
1
)
{}
}
//init
return
RT_EOK
;
}
static
rt_uint32_t
spixfer
(
struct
rt_spi_device
*
device
,
struct
rt_spi_message
*
message
)
{
RT_ASSERT
(
device
!=
RT_NULL
);
RT_ASSERT
(
device
->
bus
!=
RT_NULL
);
RT_ASSERT
(
device
->
bus
->
parent
.
user_data
!=
RT_NULL
);
int
ret
=
0
;
struct
mcu_drv_spi
*
tmp_spi
;
tmp_spi
=
rt_container_of
(
device
->
bus
,
struct
mcu_drv_spi
,
spi_bus
);
tmp_spi
->
spixfer_req
.
tx_data
=
message
->
send_buf
;
tmp_spi
->
spixfer_req
.
rx_data
=
message
->
recv_buf
;
tmp_spi
->
spixfer_req
.
len
=
message
->
length
;
ret
=
SPI_MasterTrans
(
tmp_spi
->
spi_instance
,
&
tmp_spi
->
spixfer_req
);
if
(
ret
==
E_NO_ERROR
)
{
return
message
->
length
;
}
else
{
LOG_E
(
"spixfer faild, ret %d"
,
ret
);
return
0
;
}
}
/* spi bus callback function */
static
const
struct
rt_spi_ops
nrfx_spi_ops
=
{
.
configure
=
spi_configure
,
.
xfer
=
spixfer
,
};
/*spi bus init*/
static
int
rt_hw_spi_bus_init
(
void
)
{
rt_err_t
result
=
RT_ERROR
;
for
(
int
i
=
0
;
i
<
sizeof
(
spi_config
)
/
sizeof
(
spi_config
[
0
]);
i
++
)
{
spi_bus_obj
[
i
].
spi_instance
=
spi_config
[
i
].
spi_instance
;
spi_bus_obj
[
i
].
spi_bus
.
parent
.
user_data
=
&
spi_config
[
i
];
//SPI INSTANCE
result
=
rt_spi_bus_register
(
&
spi_bus_obj
[
i
].
spi_bus
,
spi_config
[
i
].
bus_name
,
&
nrfx_spi_ops
);
RT_ASSERT
(
result
==
RT_EOK
);
}
return
result
;
}
int
rt_hw_spi_init
(
void
)
{
return
rt_hw_spi_bus_init
();
}
INIT_BOARD_EXPORT
(
rt_hw_spi_init
);
/**
* Attach the spi device to SPI bus, this function must be used after initialization.
*/
rt_err_t
rt_hw_spi_device_attach
(
const
char
*
bus_name
,
const
char
*
device_name
,
rt_uint32_t
cs_pin
)
{
RT_ASSERT
(
bus_name
!=
RT_NULL
);
RT_ASSERT
(
device_name
!=
RT_NULL
);
RT_ASSERT
(
cs_pin
!=
RT_NULL
);
rt_err_t
result
;
struct
rt_spi_device
*
spi_device
;
/* attach the device to spi bus*/
spi_device
=
(
struct
rt_spi_device
*
)
rt_malloc
(
sizeof
(
struct
rt_spi_device
));
RT_ASSERT
(
spi_device
!=
RT_NULL
);
/* initialize the cs pin */
result
=
rt_spi_bus_attach_device
(
spi_device
,
device_name
,
bus_name
,
(
void
*
)
cs_pin
);
if
(
result
!=
RT_EOK
)
{
LOG_E
(
"%s attach to %s faild, %d"
,
device_name
,
bus_name
,
result
);
result
=
RT_ERROR
;
}
/* TODO: SET THE GPIO */
RT_ASSERT
(
result
==
RT_EOK
);
return
result
;
}
#endif
/* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
#endif
/*BSP_USING_SPI*/
#define SPI_DEVICE_BUS "spi0"
#define SPI_DEVICE_NAME "spi01"
#define TEST_LEN 10
uint8_t
rx_data
[
TEST_LEN
];
uint8_t
tx_data
[
TEST_LEN
];
static
void
spi_sample
(
int
argc
,
char
*
argv
[])
{
struct
rt_spi_device
*
spi_dev
;
char
name
[
RT_NAME_MAX
];
rt_kprintf
(
"
\n
************** SPI Loopback Demo ****************
\n
"
);
rt_kprintf
(
"This example configures the SPI to send data between the MISO (P0.4) and
\n
"
);
rt_kprintf
(
"MOSI (P0.5) pins. Connect these two pins together.
\n
"
);
for
(
int
j
=
0
;
j
<
TEST_LEN
;
j
++
)
{
tx_data
[
j
]
=
j
;
}
if
(
argc
==
2
)
{
rt_strncpy
(
name
,
argv
[
1
],
RT_NAME_MAX
);
}
else
{
rt_strncpy
(
name
,
SPI_DEVICE_NAME
,
RT_NAME_MAX
);
}
/* ?? spi ???????? */
spi_dev
=
(
struct
rt_spi_device
*
)
rt_device_find
(
name
);
if
(
!
spi_dev
)
{
rt_kprintf
(
"spi sample run failed! can't find %s device!
\n
"
,
name
);
}
else
{
rt_spi_transfer
(
spi_dev
,
tx_data
,
rx_data
,
TEST_LEN
);
for
(
int
i
=
0
;
i
<
TEST_LEN
;
i
++
)
{
rt_kprintf
(
" 0x%02x, "
,
rx_data
[
i
]);
}
}
}
MSH_CMD_EXPORT
(
spi_sample
,
spi
sample
);
static
int
rt_hw_spi_sample_init
(
void
)
{
struct
rt_spi_device
*
spi_dev
;
rt_hw_spi_device_attach
(
SPI_DEVICE_BUS
,
SPI_DEVICE_NAME
,
PIN_0
);
spi_dev
=
(
struct
rt_spi_device
*
)
rt_device_find
(
SPI_DEVICE_NAME
);
struct
rt_spi_configuration
spi_cfg
=
{
.
mode
=
0
,
.
data_width
=
8
,
.
max_hz
=
1000000
,
};
rt_spi_configure
(
spi_dev
,
&
spi_cfg
);
return
RT_EOK
;
}
INIT_COMPONENT_EXPORT
(
rt_hw_spi_sample_init
);
\ No newline at end of file
bsp/maxim/libraries/HAL_Drivers/drv_spi.h
0 → 100644
浏览文件 @
6c7ae389
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-02-14 supperthomas first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rthw.h>
#include "spi.h"
#ifndef __DRV_SPI_H_
#define __DRV_SPI_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 MCU_SPI0_CONFIG \
{ \
.bus_name = "spi0", \
.spi_instance = SPI0A, \
}
#endif
#ifdef BSP_USING_SPI1
#ifdef BSP_USING_SPI1A //The SPI1A is conflit with UART1 TX RX P0.10 P0.11
#define MCU_SPI1_CONFIG \
{ \
.bus_name = "spi1", \
.spi_instance = SPI1A \
}
#else
#define MCU_SPI1_CONFIG \
{ \
.bus_name = "spi1", \
.spi_instance = SPI1B \
}
#endif
//TODO LIST SPI1A SPI1B
#endif
struct
mcu_drv_spi_config
{
char
*
bus_name
;
spi_type
spi_instance
;
};
struct
mcu_drv_spi
{
spi_type
spi_instance
;
spi_req_t
spixfer_req
;
struct
rt_spi_configuration
*
cfg
;
struct
rt_spi_bus
spi_bus
;
};
#endif
/*__DRV_SPI_H_*/
bsp/maxim/libraries/MAX32660PeriphDriver/SConscript
浏览文件 @
6c7ae389
...
...
@@ -30,6 +30,9 @@ if GetDepend(['RT_USING_I2C']):
if
GetDepend
([
'RT_USING_SPI'
]):
src
+=
[
'Source/spi.c'
]
src
+=
[
'Source/spi17y.c'
]
src
+=
[
'Source/spimss.c'
]
if
GetDepend
([
'RT_USING_RTC'
]):
src
+=
[
'Source/rtc.c'
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录