1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date        Author    Email                    Notes
8  * 2022-04-16  Kevin.Liu kevin.liu.mchp@gmail.com First Release
9  */
10 
11 #include <rtthread.h>
12 
13 #include <atmel_start.h>
14 
15 #include "i2c_demo.h"
16 
17 #ifdef SAM_I2C_EXAMPLE
18 
19 #define I2C_AT24MAC_PGMAXSZ  (16+1)
20 #define CONF_AT24MAC_ADDRESS  0x57
21 
22 /**
23  * @brief    Call this function will run I2C test code.
24  *
25  * @note     Test code will try to read/write external EEPROM.
26  *
27  * @param    None.
28  *
29  * @return   RT_OK or -RT_ERROR.
30  */
31 
i2c_demo_run(void)32 rt_err_t i2c_demo_run(void)
33 {
34     rt_uint8_t addr = 0x20;
35     rt_int32_t len;
36     rt_uint8_t i2ctx[I2C_AT24MAC_PGMAXSZ];
37     rt_uint8_t i2crx[I2C_AT24MAC_PGMAXSZ];
38 
39     for (len = 1; len < I2C_AT24MAC_PGMAXSZ; len++)
40     {
41         i2ctx[len] = (rt_uint8_t)(len + 0x20);
42     }
43 
44     /* enable I2C master and set slave address before use I2C driver module */
45     i2c_m_sync_enable(&I2C_0);
46     i2c_m_sync_set_slaveaddr(&I2C_0, CONF_AT24MAC_ADDRESS, I2C_M_SEVEN);
47 
48     /* write 16bytes data to address 0x20 - I2C slave address + random address + write data[0]...[n] */
49     i2ctx[0] = addr; /* Refer to AT24MAC data sheet, first byte is page address. */
50     io_write(&(I2C_0.io), i2ctx, I2C_AT24MAC_PGMAXSZ);
51 
52     /* Refer to data sheet, for random read, should send read address first. */
53     io_write(&(I2C_0.io), &addr, 1);
54 
55     /* Then start I2C read after send I2C slave address first */
56     io_read(&(I2C_0.io), &i2crx[1], 16);
57 #ifndef RT_USING_FINSH
58     rt_kprintf("i2crx[0]=0x%02X i2crx[15]=0x%02X\r\n", i2crx[0], i2crx[15]);
59 #endif
60 
61     return RT_EOK;
62 }
63 #endif
64 
65 /*@}*/
66