test_i2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <rtthread.h>
  31. #include <rtdevice.h>
  32. #include <utest.h>
  33. #include <string.h>
  34. #include "drv_i2c.h"
  35. #include "drv_pinctrl.h"
  36. #include "drv_gpio.h"
  37. /*
  38. * 测试K230 I2C的主从机通信,在这里采用I2C0作为测试对象
  39. *
  40. * 硬件平台:
  41. * 测试的硬件平台为庐山派开发板,使用的I2C0引脚是GPIO048(SCL)
  42. * 和GPIO049(SDA)。
  43. *
  44. * 测试说明:
  45. * 1. 测试I2C0主机模式
  46. * 主机模式下,主机向从机发送16字节数据(不包括写读地址),
  47. * 然后再读取回来进行校验,共执行两次,分别是400kHz和1MHz速率。
  48. * 注:使用的从机为AT24C08 EEPROM,设备地址为0x50。
  49. */
  50. #define I2C_NAME "i2c0"
  51. #define TARGET_ADDR 0x50
  52. #define TEST_BUFFER_SIZE 16
  53. #define I2C_SCL_PIN 48
  54. #define I2C_SDA_PIN 49
  55. #define I2C_SCL_PIN_AF IOMUX_FUNC4
  56. #define I2C_SDA_PIN_AF IOMUX_FUNC4
  57. static void test_i2c0_deinit_pin(void)
  58. {
  59. k230_pinctrl_set_function(I2C_SCL_PIN, IOMUX_FUNC1);
  60. k230_pinctrl_set_function(I2C_SDA_PIN, IOMUX_FUNC1);
  61. k230_pinctrl_set_oe(I2C_SCL_PIN, 0);
  62. k230_pinctrl_set_oe(I2C_SDA_PIN, 0);
  63. k230_pinctrl_set_ie(I2C_SCL_PIN, 1);
  64. k230_pinctrl_set_ie(I2C_SDA_PIN, 1);
  65. kd_pin_mode(I2C_SCL_PIN, GPIO_DM_INPUT);
  66. kd_pin_mode(I2C_SDA_PIN, GPIO_DM_INPUT);
  67. }
  68. static void test_i2c0_init_pin(void)
  69. {
  70. k230_pinctrl_set_function(I2C_SCL_PIN, I2C_SCL_PIN_AF); // I2C0_SCL
  71. k230_pinctrl_set_function(I2C_SDA_PIN, I2C_SDA_PIN_AF); // I2C0_SDA
  72. k230_pinctrl_set_oe(I2C_SCL_PIN, 1);
  73. k230_pinctrl_set_oe(I2C_SDA_PIN, 1);
  74. k230_pinctrl_set_ie(I2C_SCL_PIN, 1);
  75. k230_pinctrl_set_ie(I2C_SDA_PIN, 1);
  76. }
  77. static int test_i2c_check_pin(void)
  78. {
  79. test_i2c0_deinit_pin();
  80. if(kd_pin_read(I2C_SCL_PIN) != 1 || kd_pin_read(I2C_SDA_PIN) != 1)
  81. {
  82. LOG_W("i2c bus is not idle, try to recover it.");
  83. k230_pinctrl_set_oe(I2C_SCL_PIN, 1);
  84. kd_pin_mode(I2C_SCL_PIN, GPIO_DM_OUTPUT);
  85. for(rt_uint8_t i = 0; i < 9; i++)
  86. {
  87. kd_pin_write(I2C_SCL_PIN, 0);
  88. rt_hw_us_delay(2);
  89. kd_pin_write(I2C_SCL_PIN, 1);
  90. rt_hw_us_delay(2);
  91. }
  92. k230_pinctrl_set_oe(I2C_SCL_PIN, 0);
  93. kd_pin_mode(I2C_SCL_PIN, GPIO_DM_INPUT);
  94. }
  95. if(kd_pin_read(I2C_SCL_PIN) != 1 || kd_pin_read(I2C_SDA_PIN) != 1)
  96. {
  97. LOG_E("i2c bus recover failed");
  98. return -RT_ERROR;
  99. }
  100. LOG_I("i2c bus(pin: %u, %u) is idle, init i2c bus pin", I2C_SCL_PIN, I2C_SDA_PIN);
  101. test_i2c0_init_pin();
  102. return RT_EOK;
  103. }
  104. static void _test_i2c0_master(rt_uint8_t *buffer_w, rt_uint8_t *buffer_r, rt_uint32_t size, rt_uint32_t speed)
  105. {
  106. rt_err_t ret = RT_EOK;
  107. struct rt_i2c_bus_device *dev;
  108. struct rt_i2c_msg msgs[2];
  109. dev = rt_i2c_bus_device_find(I2C_NAME);
  110. uassert_not_null(dev);
  111. rt_i2c_control(dev, RT_I2C_DEV_CTRL_CLK, (void *)&speed);
  112. msgs[0].addr = TARGET_ADDR;
  113. msgs[0].flags = RT_I2C_WR;
  114. msgs[0].buf = buffer_w;
  115. msgs[0].len = size + 1;
  116. if(rt_i2c_transfer(dev, msgs, 1) != 1)
  117. {
  118. LOG_E("i2c transfer failed");
  119. uassert_true(0);
  120. }
  121. rt_thread_mdelay(10);
  122. msgs[0].addr = TARGET_ADDR;
  123. msgs[0].flags = RT_I2C_WR | RT_I2C_NO_STOP;
  124. msgs[0].buf = &buffer_r[0];
  125. msgs[0].len = 1;
  126. msgs[1].addr = TARGET_ADDR;
  127. msgs[1].flags = RT_I2C_RD | RT_I2C_NO_START;
  128. msgs[1].buf = &buffer_r[1];
  129. msgs[1].len = size;
  130. if(rt_i2c_transfer(dev, msgs, 2) != 2)
  131. {
  132. LOG_E("i2c transfer failed");
  133. uassert_true(0);
  134. }
  135. LOG_I("Read data:\n");
  136. for(rt_uint8_t i = 1; i < size + 1; i++)
  137. {
  138. LOG_I("0x%02X ", buffer_r[i]);
  139. }
  140. uassert_buf_equal(buffer_w + 1, buffer_r + 1, size);
  141. }
  142. static void test_i2c0_master(void)
  143. {
  144. rt_uint8_t buffer_w[TEST_BUFFER_SIZE + 1];
  145. rt_uint8_t buffer_r[TEST_BUFFER_SIZE + 1];
  146. rt_uint32_t size = TEST_BUFFER_SIZE;
  147. rt_uint32_t speed = 400000; // 400kHz
  148. memset(buffer_w + 1, 0xAA, TEST_BUFFER_SIZE);
  149. buffer_w[0] = 0x00; // memory address
  150. memset(buffer_r, 0x00, TEST_BUFFER_SIZE + 1);
  151. _test_i2c0_master(buffer_w, buffer_r, size, speed);
  152. speed = 1000000; // 1MHz
  153. memset(buffer_w + 1, 0x55, TEST_BUFFER_SIZE);
  154. buffer_w[0] = 0x00; // memory address
  155. memset(buffer_r, 0x00, TEST_BUFFER_SIZE + 1);
  156. _test_i2c0_master(buffer_w, buffer_r, size, speed);
  157. }
  158. static void testcase(void)
  159. {
  160. LOG_I("This is a i2c test case.\n");
  161. UTEST_UNIT_RUN(test_i2c0_master);
  162. }
  163. static rt_err_t utest_tc_init(void)
  164. {
  165. return test_i2c_check_pin();
  166. }
  167. static rt_err_t utest_tc_cleanup(void)
  168. {
  169. LOG_I("i2c bus pin deinit.\n");
  170. test_i2c0_deinit_pin();
  171. return RT_EOK;
  172. }
  173. UTEST_TC_EXPORT(testcase, "bsp.k230.drivers.i2c", utest_tc_init, utest_tc_cleanup, 100);