1+ #include "rtconfig.h"
2+
3+ #ifdef BSP_USING_AT24C02
4+
5+ #include "utest.h"
6+ #include "at24c02.h"
7+
8+ #define TEST_DATA "WELCOME TO RTT"
9+
10+ static at24cxx_device_t dev = RT_NULL ;
11+
12+ static rt_err_t test_at24c02_init (void )
13+ {
14+ rt_err_t result = RT_EOK ;
15+ uint8_t AddrInput = 0x0 ;
16+
17+ dev = at24cxx_init (AT24C02_I2C_NAME , AddrInput );
18+ if (dev == RT_NULL )
19+ {
20+ LOG_E ("AT24C02 initialization failed\n" );
21+ result = - RT_ERROR ;
22+ }
23+
24+ return result ;
25+ }
26+
27+ static void test_at24c02_example (void )
28+ {
29+ uint8_t write_buffer [] = TEST_DATA ;
30+ int data_size = sizeof (write_buffer );
31+ rt_err_t result = RT_EOK ;
32+
33+ uint8_t read_buffer [50 ] = {0 };
34+
35+ /* 写入数据 */
36+ result = at24cxx_write (dev , 0 , write_buffer , data_size );
37+
38+ if (result != RT_EOK )
39+ {
40+ LOG_E ("Failed to write data to AT24C02\n" );
41+ uassert_true (RT_FALSE );
42+ return ;
43+ }
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+
50+ if (result != RT_EOK )
51+ {
52+ LOG_E ("Failed to read data from AT24C02\n" );
53+ uassert_true (RT_FALSE );
54+ return ;
55+ }
56+
57+ LOG_I ("Successfully read from AT24C02: %s\n" , read_buffer );
58+
59+ uassert_str_equal (write_buffer , read_buffer );
60+
61+ /* 检查数据 */
62+ result = at24cxx_check (dev );
63+
64+ uassert_true (result == RT_EOK );
65+
66+ return ;
67+ }
68+
69+ static rt_err_t test_at24c02_deinit (void )
70+ {
71+
72+ if (dev != RT_NULL )
73+ {
74+ at24cxx_deinit (dev );
75+ dev = RT_NULL ;
76+ return RT_EOK ;
77+ }
78+
79+ return - RT_ERROR ;
80+ }
81+
82+ static void test_case (void )
83+ {
84+ UTEST_UNIT_RUN (test_at24c02_example );
85+ }
86+
87+ UTEST_TC_EXPORT (test_case , "bsp.gd32.port.at24c02" , test_at24c02_init , test_at24c02_deinit , 100 );
88+
89+ #endif
0 commit comments