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