i2c_demo.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include <rtthread.h>
  11. #include <atmel_start.h>
  12. #include "i2c_demo.h"
  13. #ifdef SAM_I2C_EXAMPLE
  14. #define I2C_AT24MAC_PGMAXSZ (16+1)
  15. #define CONF_AT24MAC_ADDRESS 0x57
  16. /**
  17. * @brief Call this function will run I2C test code.
  18. *
  19. * @note Test code will try to read/write external EEPROM.
  20. *
  21. * @param None.
  22. *
  23. * @return RT_OK or -RT_ERROR.
  24. */
  25. rt_err_t i2c_demo_run(void)
  26. {
  27. rt_uint8_t addr = 0x20;
  28. rt_int32_t len;
  29. rt_uint8_t i2ctx[I2C_AT24MAC_PGMAXSZ];
  30. rt_uint8_t i2crx[I2C_AT24MAC_PGMAXSZ];
  31. for (len = 1; len < I2C_AT24MAC_PGMAXSZ; len++)
  32. {
  33. i2ctx[len] = (rt_uint8_t)(len + 0x20);
  34. }
  35. /* enable I2C master and set slave address before use I2C driver module */
  36. i2c_m_sync_enable(&I2C_0);
  37. i2c_m_sync_set_slaveaddr(&I2C_0, CONF_AT24MAC_ADDRESS, I2C_M_SEVEN);
  38. /* write 16bytes data to address 0x20 - I2C slave address + random address + write data[0]...[n] */
  39. i2ctx[0] = addr; /* Refer to AT24MAC data sheet, first byte is page address. */
  40. io_write(&(I2C_0.io), i2ctx, I2C_AT24MAC_PGMAXSZ);
  41. /* Refer to data sheet, for random read, should send read address first. */
  42. io_write(&(I2C_0.io), &addr, 1);
  43. /* Then start I2C read after send I2C slave address first */
  44. io_read(&(I2C_0.io), &i2crx[1], 16);
  45. #ifndef RT_USING_FINSH
  46. rt_kprintf("i2crx[0]=0x%02X i2crx[15]=0x%02X\r\n", i2crx[0], i2crx[15]);
  47. #endif
  48. return RT_EOK;
  49. }
  50. #endif
  51. /*@}*/