summaryrefslogtreecommitdiff
path: root/bsp/microchip/common/applications/i2c_demo.c
blob: afc7d47b86d222680cabbf70d7004084d2880e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date        Author    Email                    Notes
 * 2022-04-16  Kevin.Liu [email protected] First Release
 */

#include <rtthread.h>

#include <atmel_start.h>

#include "i2c_demo.h"

#ifdef SAM_I2C_EXAMPLE

#define I2C_AT24MAC_PGMAXSZ  (16+1)
#define CONF_AT24MAC_ADDRESS  0x57

/**
 * @brief    Call this function will run I2C test code.
 *
 * @note     Test code will try to read/write external EEPROM.
 *
 * @param    None.
 *
 * @return   RT_OK or -RT_ERROR.
 */

rt_err_t i2c_demo_run(void)
{
    rt_uint8_t addr = 0x20;
    rt_int32_t len;
    rt_uint8_t i2ctx[I2C_AT24MAC_PGMAXSZ];
    rt_uint8_t i2crx[I2C_AT24MAC_PGMAXSZ];

    for (len = 1; len < I2C_AT24MAC_PGMAXSZ; len++)
    {
        i2ctx[len] = (rt_uint8_t)(len + 0x20);
    }

    /* enable I2C master and set slave address before use I2C driver module */
    i2c_m_sync_enable(&I2C_0);
    i2c_m_sync_set_slaveaddr(&I2C_0, CONF_AT24MAC_ADDRESS, I2C_M_SEVEN);

    /* write 16bytes data to address 0x20 - I2C slave address + random address + write data[0]...[n] */
    i2ctx[0] = addr; /* Refer to AT24MAC data sheet, first byte is page address. */
    io_write(&(I2C_0.io), i2ctx, I2C_AT24MAC_PGMAXSZ);

    /* Refer to data sheet, for random read, should send read address first. */
    io_write(&(I2C_0.io), &addr, 1);

    /* Then start I2C read after send I2C slave address first */
    io_read(&(I2C_0.io), &i2crx[1], 16);
#ifndef RT_USING_FINSH
    rt_kprintf("i2crx[0]=0x%02X i2crx[15]=0x%02X\r\n", i2crx[0], i2crx[15]);
#endif

    return RT_EOK;
}
#endif

/*@}*/