Skip to content

Commit 4fcf744

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

7 files changed

Lines changed: 205 additions & 0 deletions

File tree

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
10+
static int init_gd32_at24c02(void)
11+
{
12+
rt_err_t result = RT_EOK;
13+
14+
gd32_at24c02 = at24cxx_init(AT24C02_I2C_NAME, AT24C02_ADDR_INPUT);
15+
16+
if (gd32_at24c02 == RT_NULL)
17+
{
18+
rt_kprintf("AT24C02 initialization failed\n");
19+
return RT_ERROR;
20+
}
21+
22+
result = at24cxx_check(gd32_at24c02);
23+
24+
if (result == RT_ERROR)
25+
{
26+
rt_kprintf("AT24C02 check failed\n");
27+
return RT_ERROR;
28+
}
29+
30+
return RT_EOK;
31+
}
32+
33+
34+
INIT_DEVICE_EXPORT(init_gd32_at24c02);
35+
36+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
15+
16+
17+
#endif // AT24C02_H
18+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
11+
#define AT24C02_I2C_NAME "i2c0"
12+
#define TEST_DATA "WELCOM TO RTT"
13+
14+
static at24cxx_device_t dev = RT_NULL;
15+
16+
17+
static rt_err_t test_at24c02_init(void)
18+
{
19+
rt_err_t result = RT_EOK;
20+
uint8_t AddrInput = 0x0;
21+
22+
dev = at24cxx_init(AT24C02_I2C_NAME, AddrInput);
23+
if (dev == RT_NULL)
24+
{
25+
LOG_E("AT24C02 initialization failed\n");
26+
result = RT_ERROR;
27+
}
28+
29+
return result;
30+
}
31+
32+
static void test_at24c02_example(void)
33+
{
34+
uint8_t write_buffer[] = TEST_DATA;
35+
int data_size = sizeof(write_buffer);
36+
rt_err_t result = RT_EOK;
37+
38+
uint8_t read_buffer[50] = {0};
39+
/* 写入数据 */
40+
result = at24cxx_write(dev, 0, write_buffer, data_size);
41+
if (result == RT_ERROR)
42+
{
43+
LOG_E("Failed to write data to AT24C02\n");
44+
}
45+
LOG_I("Successfully wrote to AT24C02: %s\n", write_buffer);
46+
47+
/* 读取数据 */
48+
result = at24cxx_read(dev, 0, read_buffer, data_size);
49+
if (result == RT_ERROR)
50+
{
51+
LOG_E("Failed to read data from AT24C02\n");
52+
}
53+
LOG_I("Successfully read from AT24C02: %s\n", read_buffer);
54+
55+
uassert_str_equal(write_buffer, read_buffer);
56+
57+
/* 检查数据 */
58+
result = at24cxx_check(dev);
59+
60+
uassert_true(result == RT_EOK);
61+
62+
return;
63+
64+
}
65+
66+
67+
68+
static rt_err_t test_at24c02_deinit(void)
69+
{
70+
71+
if (dev != RT_NULL)
72+
{
73+
at24cxx_deinit(dev);
74+
dev = RT_NULL;
75+
return RT_EOK;
76+
}
77+
78+
return RT_ERROR;
79+
80+
}
81+
82+
83+
static void test_case(void)
84+
{
85+
UTEST_UNIT_RUN(test_at24c02_example);
86+
}
87+
88+
UTEST_TC_EXPORT(test_case, "bsp.gd32.port.at24c02" , test_at24c02_init, test_at24c02_deinit, 100);
89+
90+
91+
#endif

0 commit comments

Comments
 (0)