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