Skip to content

Commit 8eb7e02

Browse files
authored
Merge pull request #2367 from wangyq2018/es32f0654
[bsp/es32f0654]add spi/i2c drivers
2 parents 92bd298 + 064da1f commit 8eb7e02

File tree

19 files changed

+593
-95
lines changed

19 files changed

+593
-95
lines changed

bsp/es32f0654/.config

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ CONFIG_FINSH_CMD_SIZE=80
9494
# CONFIG_FINSH_USING_AUTH is not set
9595
CONFIG_FINSH_USING_MSH=y
9696
CONFIG_FINSH_USING_MSH_DEFAULT=y
97-
# CONFIG_FINSH_USING_MSH_ONLY is not set
97+
CONFIG_FINSH_USING_MSH_ONLY=y
9898
CONFIG_FINSH_ARG_MAX=10
9999

100100
#
@@ -126,6 +126,7 @@ CONFIG_RT_USING_PIN=y
126126
# CONFIG_RT_USING_SPI is not set
127127
# CONFIG_RT_USING_WDT is not set
128128
# CONFIG_RT_USING_AUDIO is not set
129+
# CONFIG_RT_USING_SENSOR is not set
129130

130131
#
131132
# Using WiFi
@@ -324,9 +325,22 @@ CONFIG_BSP_USING_GPIO=y
324325
CONFIG_BSP_USING_UART2=y
325326
# CONFIG_BSP_USING_UART3 is not set
326327

328+
#
329+
# SPI Drivers
330+
#
331+
# CONFIG_BSP_USING_SPI0 is not set
332+
# CONFIG_BSP_USING_SPI1 is not set
333+
334+
#
335+
# I2C Drivers
336+
#
337+
# CONFIG_BSP_USING_I2C0 is not set
338+
# CONFIG_BSP_USING_I2C1 is not set
339+
327340
#
328341
# Onboard Peripheral Drivers
329342
#
343+
# CONFIG_BSP_USING_SPI_FLASH is not set
330344

331345
#
332346
# Offboard Peripheral Drivers

bsp/es32f0654/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ ES-PDS-ES32F0654-V1.0
3232
本 BSP 目前对外设的支持情况如下:
3333

3434
| **板载外设** | **支持情况** | **备注** |
35-
| :---------------- | :----------: | :------------------------------------ |
36-
| GPIO | 支持 | 54 GPIOs |
37-
| UART | 支持 | UART0/1/2/3 |
35+
| :---------------- | :----------: | :------------------------------------|
36+
| SPI FLASH | 支持 | |
3837

3938
| **片上外设** | **支持情况** | **备注** |
39+
| :---------------- | :----------: | :------------------------------------|
40+
| GPIO | 支持 | 54 GPIOs |
41+
| UART | 支持 | UART0/1/2/3 |
42+
| SPI | 支持 | SPI0/1 |
43+
| I2C | 支持 | I2C0/1 |
4044

4145
| **扩展模块** | **支持情况** | **备注** |
4246

bsp/es32f0654/applications/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/*
2-
* File : main.c
32
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
43
*
54
* SPDX-License-Identifier: Apache-2.0
65
*
76
* Change Logs:
87
* Date Author Notes
9-
* 2019-01-28 wangyq first implementation
8+
* 2019-01-28 wangyq the first version
109
*/
1110

1211
#include <rtthread.h>

bsp/es32f0654/drivers/Kconfig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,43 @@ menu "Hardware Drivers Config"
2828
default n
2929
endmenu
3030

31+
menu "SPI Drivers"
32+
config BSP_USING_SPI0
33+
bool "Enable SPI0 BUS PB03/PB04/PB05(CLK/MISO/MOSI)"
34+
select RT_USING_SPI
35+
select RT_USING_PIN
36+
default n
37+
38+
config BSP_USING_SPI1
39+
bool "Enable SPI1 BUS PB13/PB14/PB15(CLK/MISO/MOSI)"
40+
select RT_USING_SPI
41+
select RT_USING_PIN
42+
default n
43+
endmenu
44+
45+
menu "I2C Drivers"
46+
config BSP_USING_I2C0
47+
bool "Enable I2C0 BUS PB08/PB09(SCL/SDA)"
48+
select RT_USING_I2C
49+
default n
50+
config BSP_USING_I2C1
51+
bool "Enable I2C1 BUS PB10/PB11(SCL/SDA)"
52+
select RT_USING_I2C
53+
default n
54+
endmenu
55+
3156
endmenu
3257

3358
menu "Onboard Peripheral Drivers"
3459

60+
config BSP_USING_SPI_FLASH
61+
bool "Enable SPI FLASH (W25Q64 spi0)"
62+
select BSP_USING_SPI
63+
select BSP_USING_SPI0
64+
select RT_USING_SFUD
65+
select RT_SFUD_USING_SFDP
66+
default n
67+
3568
endmenu
3669

3770
menu "Offboard Peripheral Drivers"

bsp/es32f0654/drivers/SConscript

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@ src = Split('''
77
board.c
88
''')
99

10-
# add gpio drivers.
10+
# add gpio code
1111
if GetDepend('RT_USING_PIN'):
1212
src += ['drv_gpio.c']
13-
if GetDepend(['RT_USING_SERIAL']):
14-
src += ['drv_usart.c']
13+
14+
# add serial driver code
15+
if GetDepend('BSP_USING_UART0') or GetDepend('BSP_USING_UART1') or GetDepend('BSP_USING_UART2') or GetDepend('BSP_USING_UART3'):
16+
src += ['drv_uart.c']
17+
18+
# add spi driver code
19+
if GetDepend('BSP_USING_SPI0') or GetDepend('BSP_USING_SPI1'):
20+
src += ['drv_spi.c']
21+
22+
# add i2c driver code
23+
if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'):
24+
src += ['drv_i2c.c']
25+
26+
# add spi flash driver code
27+
if GetDepend('BSP_USING_SPI_FLASH'):
28+
src += ['drv_spiflash.c']
1529

1630
CPPPATH = [cwd]
1731
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)

bsp/es32f0654/drivers/board.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*
2-
* File : board.c
32
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
43
*
54
* SPDX-License-Identifier: Apache-2.0
@@ -12,7 +11,7 @@
1211
#include <rthw.h>
1312
#include <rtthread.h>
1413
#include "board.h"
15-
#include "drv_usart.h"
14+
#include "drv_uart.h"
1615
#include "drv_gpio.h"
1716
#include <ald_cmu.h>
1817
#include <ald_gpio.h>

bsp/es32f0654/drivers/board.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*
2-
* File : board.h
32
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
43
*
54
* SPDX-License-Identifier: Apache-2.0

bsp/es32f0654/drivers/drv_gpio.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*
2-
* File : drv_gpio.c
32
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
43
*
54
* SPDX-License-Identifier: Apache-2.0

bsp/es32f0654/drivers/drv_gpio.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*
2-
* File : drv_gpio.h
32
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
43
*
54
* SPDX-License-Identifier: Apache-2.0

bsp/es32f0654/drivers/drv_i2c.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-01-24 wangyq the first version
9+
*/
10+
11+
#include <rthw.h>
12+
#include <rtthread.h>
13+
#include <rtdevice.h>
14+
#include "board.h"
15+
#include "drv_i2c.h"
16+
#include <ald_i2c.h>
17+
#include <ald_gpio.h>
18+
19+
#ifdef RT_USING_I2C
20+
21+
#define TIMEOUT 0x0FFF
22+
23+
/*define i2c Instance*/
24+
struct rt_i2c_bus_device _i2c_device0;
25+
struct rt_i2c_bus_device _i2c_device1;
26+
i2c_handle_t _h_i2c0, _h_i2c1;
27+
28+
static void _i2c_init(void)
29+
{
30+
gpio_init_t gpio_instruct; //i2c function init
31+
32+
/* Initialize I2C Pin*/
33+
gpio_instruct.mode = GPIO_MODE_OUTPUT;
34+
gpio_instruct.odos = GPIO_PUSH_PULL;
35+
gpio_instruct.pupd = GPIO_PUSH_UP;
36+
gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
37+
gpio_instruct.flt = GPIO_FILTER_DISABLE;
38+
gpio_instruct.type = GPIO_TYPE_CMOS;
39+
gpio_instruct.func = GPIO_FUNC_5;
40+
41+
#ifdef BSP_USING_I2C0
42+
/* Initialize I2C Function */
43+
_h_i2c0.perh = I2C0;
44+
_h_i2c0.init.clk_speed = 100000;
45+
_h_i2c0.init.duty = I2C_DUTYCYCLE_2;
46+
_h_i2c0.init.own_addr1 = 0x0A;
47+
_h_i2c0.init.addr_mode = I2C_ADDR_7BIT;
48+
_h_i2c0.init.general_call = I2C_GENERALCALL_DISABLE;
49+
_h_i2c0.init.no_stretch = I2C_NOSTRETCH_ENABLE;
50+
51+
i2c_reset(&_h_i2c0);
52+
i2c_init(&_h_i2c0);
53+
/* I2C0_SCL->PB8, I2C0_SDA->PB9 */
54+
gpio_init(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, &gpio_instruct);
55+
#endif/*BSP_USING_I2C0*/
56+
57+
#ifdef BSP_USING_I2C1
58+
/* Initialize i2c function */
59+
_h_i2c1.perh = I2C1;
60+
_h_i2c1.init.clk_speed = 100000;
61+
_h_i2c1.init.duty = I2C_DUTYCYCLE_2;
62+
_h_i2c1.init.own_addr1 = 0xA0;
63+
_h_i2c1.init.addr_mode = I2C_ADDR_7BIT;
64+
_h_i2c1.init.general_call = I2C_GENERALCALL_DISABLE;
65+
_h_i2c1.init.no_stretch = I2C_NOSTRETCH_ENABLE;
66+
67+
i2c_reset(&_h_i2c1);
68+
i2c_init(&_h_i2c1);
69+
/* I2C1_SCL->PB10, I2C1_SDA->PB11 */
70+
gpio_init(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, &gpio_instruct);
71+
#endif/*BSP_USING_I2C1*/
72+
}
73+
74+
static rt_size_t es32f0_master_xfer(struct rt_i2c_bus_device *bus,
75+
struct rt_i2c_msg msgs[],
76+
rt_uint32_t num)
77+
{
78+
struct rt_i2c_msg *msg;
79+
rt_uint32_t i;
80+
rt_err_t ret = RT_ERROR;
81+
82+
for (i = 0; i < num; i++)
83+
{
84+
msg = &msgs[i];
85+
if (msg->flags & RT_I2C_RD)
86+
{
87+
if (i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
88+
{
89+
i2c_dbg("i2c bus write failed,i2c bus stop!\n");
90+
goto out;
91+
}
92+
}
93+
else
94+
{
95+
if (i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
96+
{
97+
i2c_dbg("i2c bus write failed,i2c bus stop!\n");
98+
goto out;
99+
}
100+
}
101+
}
102+
103+
ret = i;
104+
105+
out:
106+
i2c_dbg("send stop condition\n");
107+
108+
return ret;
109+
}
110+
111+
const struct rt_i2c_bus_device_ops es32f0_i2c_ops =
112+
{
113+
es32f0_master_xfer,
114+
RT_NULL,
115+
RT_NULL,
116+
};
117+
118+
int rt_hw_i2c_init(void)
119+
{
120+
_i2c_init();
121+
122+
#ifdef BSP_USING_I2C0
123+
rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
124+
_i2c_device0.ops = &es32f0_i2c_ops;
125+
_i2c_device0.priv = &_h_i2c0;
126+
rt_i2c_bus_device_register(&_i2c_device0, "i2c0");
127+
#endif
128+
129+
#ifdef BSP_USING_I2C1
130+
rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
131+
_i2c_device1.ops = &es32f0_i2c_ops;
132+
_i2c_device1.priv = &_h_i2c1;
133+
rt_i2c_bus_device_register(&_i2c_device1, "i2c1");
134+
#endif
135+
136+
return RT_EOK;
137+
}
138+
INIT_DEVICE_EXPORT(rt_hw_i2c_init);
139+
/* end of i2c driver */
140+
#endif

0 commit comments

Comments
 (0)