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