Skip to content

Commit f1ba61e

Browse files
committed
[bsp/es32f0334] add spi/i2c/pwm drivers
1 parent 79fd4d3 commit f1ba61e

File tree

19 files changed

+906
-32
lines changed

19 files changed

+906
-32
lines changed

bsp/es32f0334/.config

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,30 @@ CONFIG_BSP_USING_GPIO=y
324324
# CONFIG_BSP_USING_UART0 is not set
325325
CONFIG_BSP_USING_UART1=y
326326

327+
#
328+
# SPI Drivers
329+
#
330+
# CONFIG_BSP_USING_SPI0 is not set
331+
# CONFIG_BSP_USING_SPI1 is not set
332+
333+
#
334+
# I2C Drivers
335+
#
336+
# CONFIG_BSP_USING_I2C0 is not set
337+
# CONFIG_BSP_USING_I2C1 is not set
338+
339+
#
340+
# PWM Drivers
341+
#
342+
# CONFIG_BSP_USING_PWM0 is not set
343+
# CONFIG_BSP_USING_PWM1 is not set
344+
# CONFIG_BSP_USING_PWM2 is not set
345+
# CONFIG_BSP_USING_PWM3 is not set
346+
327347
#
328348
# Onboard Peripheral Drivers
329349
#
350+
# CONFIG_BSP_USING_SPI_FLASH is not set
330351

331352
#
332353
# Offboard Peripheral Drivers

bsp/es32f0334/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ES-PDS-ES32F0334-V1.1
2020
该开发板常用 **板载资源** 如下:
2121

2222
- MCU:ES32F0334LT,主频 48MHz,32KB SRAM,256KB FLASH,54 GPIOs
23-
- 外部 FLASHMX25L64(SPI,16MB)、EEPROM(24c04)
23+
- 外部模块:SPI FLASH (MX25L64,8MB)、I2C EEPROM (M24C04,512B)
2424
- 常用外设
2525
- LED:2个,(PA12/PC12)
2626
- 液晶屏:1个
@@ -35,13 +35,13 @@ ES-PDS-ES32F0334-V1.1
3535

3636
| **板载外设** | **支持情况** | **备注** |
3737
| :---------------- | :----------: | :------------------------------------|
38-
38+
| SPI FLASH | 支持 | SPI0 |
3939
| **片上外设** | **支持情况** | **备注** |
40-
| :---------------- | :----------: | :------------------------------------|
4140
| GPIO | 支持 | 54 GPIOs |
4241
| UART | 支持 | UART0/1 |
43-
44-
| **扩展模块** | **支持情况** | **备注** |
42+
| SPI | 支持 | SPI0/1 |
43+
| I2C | 支持 | I2C0/1 |
44+
| PWM | 支持 | PWM0/1/2/3 |
4545

4646
更多详细信息请咨询[上海东软载波微电子技术支持](http://www.essemi.com/)
4747

bsp/es32f0334/drivers/Kconfig

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,64 @@ menu "Hardware Drivers Config"
1919

2020
endmenu
2121

22+
menu "SPI Drivers"
23+
config BSP_USING_SPI0
24+
bool "Enable SPI0 BUS PB03/PB04/PB05(CLK/MISO/MOSI)"
25+
select RT_USING_SPI
26+
select RT_USING_PIN
27+
default n
28+
29+
config BSP_USING_SPI1
30+
bool "Enable SPI1 BUS PB13/PB14/PB15(CLK/MISO/MOSI)"
31+
select RT_USING_SPI
32+
select RT_USING_PIN
33+
default n
34+
endmenu
35+
36+
menu "I2C Drivers"
37+
config BSP_USING_I2C0
38+
bool "Enable I2C0 BUS PB08/PB09(SCL/SDA)"
39+
select RT_USING_I2C
40+
default n
41+
config BSP_USING_I2C1
42+
bool "Enable I2C1 BUS PB10/PB11(SCL/SDA)"
43+
select RT_USING_I2C
44+
default n
45+
endmenu
46+
47+
menu "PWM Drivers"
48+
config BSP_USING_PWM0
49+
bool "Using PWM0 PA08/PA09/PA10/PA11"
50+
select RT_USING_PWM
51+
default n
52+
53+
config BSP_USING_PWM1
54+
bool "Using PWM1 PB06/PB07/PB08"
55+
select RT_USING_PWM
56+
default n
57+
58+
config BSP_USING_PWM2
59+
bool "Using PWM2 PA00/PA01"
60+
select RT_USING_PWM
61+
default n
62+
63+
config BSP_USING_PWM3
64+
bool "Using PWM3 PC06/PC07"
65+
select RT_USING_PWM
66+
default n
67+
endmenu
2268
endmenu
2369

2470
menu "Onboard Peripheral Drivers"
2571

72+
config BSP_USING_SPI_FLASH
73+
bool "Enable SPI FLASH (W25Q64 spi0)"
74+
select BSP_USING_SPI
75+
select BSP_USING_SPI0
76+
select RT_USING_SFUD
77+
select RT_SFUD_USING_SFDP
78+
default n
79+
2680
endmenu
2781

2882
menu "Offboard Peripheral Drivers"

bsp/es32f0334/drivers/SConscript

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ if GetDepend('RT_USING_PIN'):
1515
if GetDepend('BSP_USING_UART0') or GetDepend('BSP_USING_UART1'):
1616
src += ['drv_uart.c']
1717

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']
29+
30+
# add pwm driver code
31+
if GetDepend('BSP_USING_PWM0') or GetDepend('BSP_USING_PWM1') or GetDepend('BSP_USING_PWM2') or GetDepend('BSP_USING_PWM3'):
32+
src += ['drv_pwm.c']
33+
1834
CPPPATH = [cwd]
1935
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
2036

bsp/es32f0334/drivers/board.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void SystemClock_Config(void)
4545
/* hosc 12MHz, from hosc/3 pll to 48MHz */
4646
cmu_pll1_config(CMU_PLL1_INPUT_HOSC_3, CMU_PLL1_OUTPUT_48M);
4747

48-
/* MCLK 48MHz*/
48+
/* MCLK 48MHz */
4949
cmu_clock_config(CMU_CLOCK_PLL1, 48000000);
5050
}
5151

@@ -58,14 +58,8 @@ void SystemClock_Config(void)
5858
*******************************************************************************/
5959
void SysTick_Configuration(void)
6060
{
61-
rt_uint32_t _mclk;
62-
rt_uint32_t _sys_div = READ_BITS(CMU->CFGR, CMU_CFGR_SYSDIV_MSK, CMU_CFGR_SYSDIV_POSS);
63-
64-
/* get hrc clock*/
65-
_mclk = cmu_get_clock();
66-
67-
/* SYSCLK = MCLK/SYSDIV */
68-
SysTick_Config(_mclk / (RT_TICK_PER_SECOND << _sys_div));
61+
/* ticks = sysclk / RT_TICK_PER_SECOND */
62+
SysTick_Config(cmu_get_sys_clock() / RT_TICK_PER_SECOND);
6963
}
7064

7165
/**
@@ -108,3 +102,22 @@ void rt_hw_board_init(void)
108102
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
109103
#endif
110104
}
105+
106+
/**
107+
* This function will delay for some us.
108+
*
109+
* @param us the delay time of us
110+
*/
111+
void rt_hw_us_delay(rt_uint32_t us)
112+
{
113+
unsigned int start, now, delta, reload, us_tick;
114+
start = SysTick->VAL;
115+
reload = SysTick->LOAD;
116+
us_tick = cmu_get_sys_clock() / 1000000UL;
117+
do
118+
{
119+
now = SysTick->VAL;
120+
delta = start > now ? start - now : reload + start - now;
121+
}
122+
while (delta < us_tick * us);
123+
}

bsp/es32f0334/drivers/drv_gpio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ rt_err_t es32f0_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
257257
{
258258
return RT_ENOSYS;
259259
}
260-
/**pin no. convert to dec no.**/
260+
/* pin no. convert to dec no. */
261261
for (irqindex = 0; irqindex < 16; irqindex++)
262262
{
263263
if ((0x01 << irqindex) == index->pin)
@@ -327,7 +327,7 @@ rt_err_t es32f0_pin_irq_enable(struct rt_device *device, rt_base_t pin,
327327
const struct pin_irq_map *irqmap;
328328
rt_base_t level;
329329
rt_int32_t irqindex = -1;
330-
/*Configure GPIO_InitStructure & EXTI_InitStructure*/
330+
/* Configure GPIO_InitStructure & EXTI_InitStructure */
331331
gpio_init_t gpio_initstruct;
332332
exti_init_t exti_initstruct;
333333
exti_initstruct.filter = DISABLE;
@@ -341,7 +341,7 @@ rt_err_t es32f0_pin_irq_enable(struct rt_device *device, rt_base_t pin,
341341
}
342342
if (enabled == PIN_IRQ_ENABLE)
343343
{
344-
/**pin no. convert to dec no.**/
344+
/* pin no. convert to dec no. */
345345
for (irqindex = 0; irqindex < 16; irqindex++)
346346
{
347347
if ((0x01 << irqindex) == index->pin)
@@ -421,7 +421,7 @@ INIT_BOARD_EXPORT(rt_hw_pin_init);
421421
rt_inline void pin_irq_hdr(uint16_t GPIO_Pin)
422422
{
423423
uint16_t irqno;
424-
/**pin no. convert to dec no.**/
424+
/* pin no. convert to dec no. */
425425
for (irqno = 0; irqno < 16; irqno++)
426426
{
427427
if ((0x01 << irqno) == GPIO_Pin)

bsp/es32f0334/drivers/drv_i2c.c

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

bsp/es32f0334/drivers/drv_i2c.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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-03-19 wangyq the first version
9+
*/
10+
11+
#ifndef DRV_I2C_H__
12+
#define DRV_I2C_H__
13+
14+
int rt_hw_i2c_init(void);
15+
16+
#endif

0 commit comments

Comments
 (0)