Skip to content

Commit 4e264cd

Browse files
committed
Add an example for measuring the onboard peripherals of the I2C board
1 parent 4d063ac commit 4e264cd

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

bsp/gd32/risc-v/gd32vw553h-eval/board/Kconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ endmenu
108108

109109
menu "Board extended module Drivers"
110110

111+
menuconfig BSP_USING_AT24C02
112+
bool "Enable AT24C02 I2C0( SCL[PA2 : 2] SDA[PA3 : 3] )"
113+
default n
114+
select PKG_USING_AT24CXX
115+
select PKG_AT24CXX_EE_TYPE_AT24C02
116+
117+
if BSP_USING_AT24C02
118+
119+
config BSP_USING_AT24C02_UTEST
120+
bool "Enable the utest of AT24C02"
121+
default n
122+
select RT_USING_UTEST
123+
select RT_USING_ULOG
124+
select ULOG_USING_ISR_LOG
125+
126+
config BSP_USING_AT24C02_INIT
127+
bool "Init the model and check it"
128+
default y
129+
130+
endif
111131
endmenu
112132

113133
endmenu

bsp/gd32/risc-v/gd32vw553h-eval/board/SConscript

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ if rtconfig.PLATFORM in ['gcc']:
2424
CPPDEFINES = ['GD32VW553H_EVAL']
2525
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
2626

27+
list = os.listdir(cwd)
28+
for item in list:
29+
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
30+
group = group + SConscript(os.path.join(item, 'SConscript'))
31+
32+
2733
Return('group')

bsp/gd32/risc-v/gd32vw553h-eval/board/linker_scripts/link.lds

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ SECTIONS
6262
__rt_init_end = .;
6363
. = ALIGN(4);
6464

65+
/* section information for utest */
66+
. = ALIGN(4);
67+
__rt_utest_tc_tab_start = .;
68+
KEEP(*(UtestTcTab))
69+
__rt_utest_tc_tab_end = .;
70+
6571
/* section information for modules */
6672
. = ALIGN(4);
6773
__rtmsymtab_start = .;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
from building import *
3+
4+
objs = []
5+
cwd = GetCurrentDir()
6+
7+
# add general drivers
8+
src = []
9+
path = [cwd]
10+
11+
if GetDepend(['BSP_USING_AT24C02']):
12+
path += [cwd + "/at24c02"]
13+
14+
if GetDepend(['BSP_USING_AT24C02_UTEST']):
15+
src += ["./at24c02/test_at24c02.c"]
16+
17+
if GetDepend(['BSP_USING_AT24C02_INIT']):
18+
src += ['./at24c02/at24c02.c']
19+
20+
CPPDEFINES = ['GD32VW553H_EVAL']
21+
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
22+
23+
list = os.listdir(cwd)
24+
for item in list:
25+
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
26+
group = group + SConscript(os.path.join(item, 'SConscript'))
27+
28+
Return('group')
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "rtconfig.h"
2+
3+
#ifdef BSP_USING_AT24C02_INIT
4+
5+
#include "at24c02.h"
6+
7+
at24cxx_device_t gd32_at24c02 = RT_NULL;
8+
9+
static int init_gd32_at24c02(void)
10+
{
11+
rt_err_t result = RT_EOK;
12+
13+
gd32_at24c02 = at24cxx_init(AT24C02_I2C_NAME, AT24C02_ADDR_INPUT);
14+
15+
if (gd32_at24c02 == RT_NULL)
16+
{
17+
rt_kprintf("AT24C02 initialization failed\n");
18+
return RT_ERROR;
19+
}
20+
21+
result = at24cxx_check(gd32_at24c02);
22+
23+
if (result == RT_ERROR)
24+
{
25+
rt_kprintf("AT24C02 check failed\n");
26+
return RT_ERROR;
27+
}
28+
29+
return RT_EOK;
30+
}
31+
32+
33+
INIT_DEVICE_EXPORT(init_gd32_at24c02);
34+
35+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef AT24C02_H
2+
#define AT24C02_H
3+
4+
#include <rtthread.h>
5+
#include "at24cxx.h"
6+
7+
8+
#define AT24C02_I2C_NAME "i2c0"
9+
#define AT24C02_ADDR_INPUT 0x0
10+
11+
12+
extern at24cxx_device_t gd32_at24c02;
13+
14+
#endif // AT24C02_H
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "rtconfig.h"
2+
3+
#ifdef BSP_USING_AT24C02
4+
5+
#include "at24cxx.h"
6+
#include <rtthread.h>
7+
#include "utest.h"
8+
9+
10+
#define AT24C02_I2C_NAME "i2c0"
11+
#define TEST_DATA "WELCOM TO RTT"
12+
13+
static at24cxx_device_t dev = RT_NULL;
14+
15+
static rt_err_t test_at24c02_init(void)
16+
{
17+
rt_err_t result = RT_EOK;
18+
uint8_t AddrInput = 0x0;
19+
20+
dev = at24cxx_init(AT24C02_I2C_NAME, AddrInput);
21+
if (dev == RT_NULL)
22+
{
23+
LOG_E("AT24C02 initialization failed\n");
24+
result = RT_ERROR;
25+
}
26+
27+
return result;
28+
}
29+
30+
static void test_at24c02_example(void)
31+
{
32+
uint8_t write_buffer[] = TEST_DATA;
33+
int data_size = sizeof(write_buffer);
34+
rt_err_t result = RT_EOK;
35+
36+
uint8_t read_buffer[50] = {0};
37+
/* 写入数据 */
38+
result = at24cxx_write(dev, 0, write_buffer, data_size);
39+
if (result == RT_ERROR)
40+
{
41+
LOG_E("Failed to write data to AT24C02\n");
42+
}
43+
LOG_I("Successfully wrote to AT24C02: %s\n", write_buffer);
44+
45+
/* 读取数据 */
46+
result = at24cxx_read(dev, 0, read_buffer, data_size);
47+
if (result == RT_ERROR)
48+
{
49+
LOG_E("Failed to read data from AT24C02\n");
50+
}
51+
LOG_I("Successfully read from AT24C02: %s\n", read_buffer);
52+
53+
uassert_str_equal(write_buffer, read_buffer);
54+
55+
/* 检查数据 */
56+
result = at24cxx_check(dev);
57+
58+
uassert_true(result == RT_EOK);
59+
60+
return;
61+
62+
}
63+
64+
static rt_err_t test_at24c02_deinit(void)
65+
{
66+
67+
if (dev != RT_NULL)
68+
{
69+
at24cxx_deinit(dev);
70+
dev = RT_NULL;
71+
return RT_EOK;
72+
}
73+
74+
return RT_ERROR;
75+
76+
}
77+
78+
static void test_case(void)
79+
{
80+
UTEST_UNIT_RUN(test_at24c02_example);
81+
}
82+
83+
UTEST_TC_EXPORT(test_case, "bsp.gd32.port.at24c02" , test_at24c02_init, test_at24c02_deinit, 100);
84+
85+
#endif

0 commit comments

Comments
 (0)