Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Ze-Hou
Copy link
Contributor

@Ze-Hou Ze-Hou commented Oct 25, 2025

Requirement: The BSP for the k230 platform in the RT-Thread repository
does not yet have an I2C driver.

Solution: Provide I2C drivers for the k230 platform in the RT-Thread
repository.
1. support i2c master mode
2. support i2c slave mode

拉取/合并请求描述:(PR description)

[

庐山派,测试 k230 i2c 从机使用CH341T模块进行通信测试,k230 i2c 主机使用AT24C08作为从机进行通信测试。

为什么提交这份PR (why to submit this PR)

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions github-actions bot added the BSP label Oct 25, 2025
@github-actions
Copy link

github-actions bot commented Oct 25, 2025

📌 Code Review Assignment

🏷️ Tag: bsp_k230

Reviewers: unicornx

Changed Files (Click to expand)
  • bsp/k230/.ci/attachconfig/ci.attachconfig.yml
  • bsp/k230/.config
  • bsp/k230/board/Kconfig
  • bsp/k230/drivers/interdrv/i2c/SConscript
  • bsp/k230/drivers/interdrv/i2c/drv_i2c.c
  • bsp/k230/drivers/interdrv/i2c/drv_i2c.h
  • bsp/k230/drivers/utest/SConscript
  • bsp/k230/drivers/utest/test_i2c.c
  • bsp/k230/rtconfig.h

📊 Current Review Status (Last Updated: 2025-10-27 16:24 CST)

  • unicornx Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@github-actions
Copy link

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:master
  • 设置PR number为 \ Set the PR number to:10851
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 master 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the master branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@Ze-Hou Ze-Hou force-pushed the master branch 3 times, most recently from 5548acf to 4784db2 Compare October 27, 2025 01:24
@supperthomas
Copy link
Member

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

@supperthomas supperthomas requested a review from Copilot October 27, 2025 01:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds I2C driver support for the K230 platform, enabling both master and slave modes. The implementation includes hardware register definitions, core driver functionality, and comprehensive test cases.

Key Changes

  • Added I2C hardware register definitions and driver implementation supporting both master and slave modes
  • Implemented slave mode with EEPROM emulation accessible via device file system
  • Added comprehensive unit tests for both master and slave modes with hardware validation

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
bsp/k230/rtconfig.h Enabled I2C configuration options
bsp/k230/drivers/utest/test_i2c.c Added I2C driver test cases for master/slave modes
bsp/k230/drivers/utest/SConscript Updated build script to include I2C tests
bsp/k230/drivers/interdrv/i2c/drv_i2c.h Added hardware register structure definitions
bsp/k230/drivers/interdrv/i2c/drv_i2c.c Implemented I2C driver core functionality
bsp/k230/drivers/interdrv/i2c/SConscript Added I2C driver build configuration
bsp/k230/board/Kconfig Added Kconfig options for I2C device configuration
bsp/k230/.config Updated configuration to enable I2C support

if (pfd.revents & POLLIN)
{
int n = read(fd, buffer, TEST_BUFFER_SIZE);
LOG_I("slave buffer data:\n", n);
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LOG_I format string contains a placeholder (\n) but an unused argument 'n' is passed. Either remove the argument 'n' or add a proper format specifier like 'Received %d bytes:\n'.

English: The format string and arguments don't match - 'n' is passed but never used in the format string.
中文:格式字符串和参数不匹配 - 传递了参数 'n' 但在格式字符串中未使用。

Suggested change
LOG_I("slave buffer data:\n", n);
LOG_I("slave buffer data: received %d bytes\n", n);

Copilot uses AI. Check for mistakes.

static ssize_t eeprom_write(struct dfs_file *file, const void *buffer, size_t size, off_t *pos)
{
if (*pos>= file->vnode->size)
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space before the comparison operator. Should be '*pos >= file->vnode->size' for consistent formatting.

English: Spacing issue - there should be a space before '>=' operator.
中文:空格问题 - '>=' 运算符前应该有空格。

Suggested change
if (*pos>= file->vnode->size)
if (*pos >= file->vnode->size)

Copilot uses AI. Check for mistakes.
Comment on lines 542 to 544
static off_t eeprom_lseek(struct dfs_file *file, off_t offset, int wherece)
{
(void)wherece;
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter name 'wherece' appears to be a typo. Should likely be 'whence' (standard POSIX lseek parameter name).

English: Corrected spelling of 'wherece' to 'whence'.
中文:将 'wherece' 的拼写更正为 'whence'。

Suggested change
static off_t eeprom_lseek(struct dfs_file *file, off_t offset, int wherece)
{
(void)wherece;
static off_t eeprom_lseek(struct dfs_file *file, off_t offset, int whence)
{
(void)whence;

Copilot uses AI. Check for mistakes.

case I2C_SLAVE_READ_PROCESSED:
/* The previous byte made it to the bus, get next one */
_i2c_slave_read_add_increment(eeprom, val);
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing 'break' statement between case statements. Case I2C_SLAVE_READ_PROCESSED will fall through to I2C_SLAVE_READ_REQUESTED, which may be intentional but should be documented with a /* fall through */ comment if so.

English: Missing break statement causes fall-through behavior that should be explicitly documented.
中文:缺少 break 语句导致向下贯穿行为,如果这是有意的应明确注释。

Suggested change
_i2c_slave_read_add_increment(eeprom, val);
_i2c_slave_read_add_increment(eeprom, val);
/* fall through */

Copilot uses AI. Check for mistakes.
if(eeprom->poll_event & POLLIN)
{
rt_wqueue_wakeup(&eeprom->device.wait_queue, (void*)POLLIN);
}
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing 'break' statement between case statements. Case I2C_SLAVE_STOP will fall through to I2C_SLAVE_WRITE_REQUESTED, which may cause unintended behavior.

English: Missing break statement causes fall-through from I2C_SLAVE_STOP to I2C_SLAVE_WRITE_REQUESTED.
中文:缺少 break 语句导致从 I2C_SLAVE_STOP 向下贯穿到 I2C_SLAVE_WRITE_REQUESTED。

Suggested change
}
}
break;

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,11 @@
# RT-Thread building script for RTC component
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment incorrectly states 'RTC component' but this is the I2C component build script.

English: Comment should reference I2C component, not RTC component.
中文:注释应该引用 I2C 组件,而不是 RTC 组件。

Suggested change
# RT-Thread building script for RTC component
# RT-Thread building script for I2C component

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@Ze-Hou
Copy link
Contributor Author

Ze-Hou commented Oct 27, 2025

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

K230 好像还没有attachconfig

@supperthomas
Copy link
Member

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

K230 好像还没有attachconfig

https://club.rt-thread.org/ask/article/5c41835bb8ff9b41.html 可以加一下很简单。

@Ze-Hou
Copy link
Contributor Author

Ze-Hou commented Oct 27, 2025

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

K230 好像还没有attachconfig

https://club.rt-thread.org/ask/article/5c41835bb8ff9b41.html 可以加一下很简单。

好的

@unicornx
Copy link
Contributor

unicornx commented Oct 27, 2025

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

K230 好像还没有attachconfig

https://club.rt-thread.org/ask/article/5c41835bb8ff9b41.html 可以加一下很简单。

这个改动看上去和 i2c 驱动开发没有直接关系啊,我建议另外起一个 pr 改动

我已经新提了一个 issue #10857 来 track 这个事情。本 PR 先确保 i2c 驱动的工作。

@unicornx unicornx self-requested a review October 27, 2025 05:21
@unicornx unicornx added the BSP: K230 BSP related with K230 label Oct 27, 2025
@supperthomas
Copy link
Member

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

K230 好像还没有attachconfig

https://club.rt-thread.org/ask/article/5c41835bb8ff9b41.html 可以加一下很简单。

这个改动看上去和 i2c 驱动开发没有直接关系啊,我建议另外起一个 pr 改动

我已经新提了一个 issue #10857 来 track 这个事情。本 PR 先确保 i2c 驱动的工作。

有关系的,可以只加一个i2c的driver的配置,加了Kconfig之后,对应的配置如何配置,让ci知道,从而ci帮忙编译到i2c的driver,这是可以让ci进行编译检查,从而防止编译不过的代码流入到上游代码中,也防止遗漏文件。
而且加了yml之后,其他人就知道开哪些配置可以使用和测试i2c driver。而且在你PR的时候,肯定最清楚当前的Kconfig如何配置才能使用i2c drvier。这个时候写是最有效的,放到以后写可能就不知道如何设置了。而且这个已经很简单了。

@Ze-Hou
Copy link
Contributor Author

Ze-Hou commented Oct 27, 2025

请参考https://club.rt-thread.org/ask/article/d273bbcd1f8779bc.html 添加yaml文件

K230 好像还没有attachconfig

https://club.rt-thread.org/ask/article/5c41835bb8ff9b41.html 可以加一下很简单。

这个改动看上去和 i2c 驱动开发没有直接关系啊,我建议另外起一个 pr 改动
我已经新提了一个 issue #10857 来 track 这个事情。本 PR 先确保 i2c 驱动的工作。

有关系的,可以只加一个i2c的driver的配置,加了Kconfig之后,对应的配置如何配置,让ci知道,从而ci帮忙编译到i2c的driver,这是可以让ci进行编译检查,从而防止编译不过的代码流入到上游代码中,也防止遗漏文件。 而且加了yml之后,其他人就知道开哪些配置可以使用和测试i2c driver。而且在你PR的时候,肯定最清楚当前的Kconfig如何配置才能使用i2c drvier。这个时候写是最有效的,放到以后写可能就不知道如何设置了。而且这个已经很简单了。

要不这样,两位老师,我先把上述格式问题修改了,然后提交了pr,再通过#10857 来增加,i2c的编译本地已经测试过的,这样能较好的区分问题

@supperthomas
Copy link
Member

这样,就加下面这个文件就行了。我帮你写了。

bsp/k230/.ci/attachconfig/ci.attachconfig.yml

devices.i2c:
    kconfig:
      - CONFIG_BSP_USING_I2C=y
      - CONFIG_BSP_USING_I2C1=y

*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个license?哪里的?是符合你的需求吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个license?哪里的?是符合你的需求吗

许可是,从k230的其他driver搬的

@Ze-Hou
Copy link
Contributor Author

Ze-Hou commented Oct 27, 2025

这样,就加下面这个文件就行了。我帮你写了。

bsp/k230/.ci/attachconfig/ci.attachconfig.yml

devices.i2c:
    kconfig:
      - CONFIG_BSP_USING_I2C=y
      - CONFIG_BSP_USING_I2C1=y

好的,这次我先把i2c的加上

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (2)

bsp/k230/drivers/utest/test_i2c.c:1

  • English: The comments "write data" and "recv addr byte" use inconsistent comment styles. According to RT-Thread coding standards, use /* */ style comments instead of // for code comments.
    中文:注释 "write data" 和 "recv addr byte" 使用了不一致的注释风格。根据 RT-Thread 编码标准,应使用 /* */ 风格的注释,而不是 //
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd

bsp/k230/drivers/utest/test_i2c.c:1

  • English: The comments "write data" and "recv addr byte" use inconsistent comment styles. According to RT-Thread coding standards, use /* */ style comments instead of // for code comments.
    中文:注释 "write data" 和 "recv addr byte" 使用了不一致的注释风格。根据 RT-Thread 编码标准,应使用 /* */ 风格的注释,而不是 //
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd

LOG_E("%s: rt_device_register error %d\n", __func__, ret);
return -RT_ERROR;
}
device->fops = &eeprom_fops;
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English: The device->fops field is assigned twice (lines 581 and 588). The first assignment is redundant and should be removed.
中文:device->fops 字段被赋值两次(第 581 行和第 588 行)。第一次赋值是冗余的,应该删除。

Copilot uses AI. Check for mistakes.
* spklen = 0~50ns
*/

spklen = 10 / (1e9 / dev->clock);
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English: The calculation 10 / (1e9 / dev->clock) involves floating-point division that could be optimized to dev->clock * 10 / 1e9 to avoid two division operations and improve performance.
中文:计算 10 / (1e9 / dev->clock) 涉及浮点除法,可以优化为 dev->clock * 10 / 1e9 以避免两次除法操作并提高性能。

Suggested change
spklen = 10 / (1e9 / dev->clock);
spklen = dev->clock * 10 / 1e9;

Copilot uses AI. Check for mistakes.
@Ze-Hou Ze-Hou requested a review from Copilot October 27, 2025 07:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (2)

bsp/k230/drivers/utest/test_i2c.c:1

  • English: C++ style comments (//) should be replaced with C-style comments (/* /) to maintain consistency with RT-Thread coding standards and the rest of the codebase which uses C89/C90 style comments.
    中文:应使用 C 风格注释(/
    */)替换 C++ 风格注释(//),以保持与 RT-Thread 编码标准和代码库其余部分(使用 C89/C90 风格注释)的一致性。
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd

bsp/k230/drivers/utest/test_i2c.c:1

  • English: C++ style comments (//) should be replaced with C-style comments (/* /) to maintain consistency with RT-Thread coding standards and the rest of the codebase which uses C89/C90 style comments.
    中文:应使用 C 风格注释(/
    */)替换 C++ 风格注释(//),以保持与 RT-Thread 编码标准和代码库其余部分(使用 C89/C90 风格注释)的一致性。
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd

Comment on lines +16 to +17
help
Select the I2C0 device mode to be used.
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English: The help text should be bilingual (English and Chinese) to maintain consistency with RT-Thread's internationalization practices, similar to other BSP configurations in the project.
中文:帮助文本应使用双语(英文和中文)以保持与 RT-Thread 国际化实践的一致性,类似于项目中其他 BSP 配置。

Copilot uses AI. Check for mistakes.
case I2C_SLAVE_READ_PROCESSED:
/* The previous byte made it to the bus, get next one */
_i2c_slave_read_add_increment(eeprom, val);
/* fall through */
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] English: Consider using attribute((fallthrough)) or the C23 [[fallthrough]] attribute for explicit fallthrough annotation in switch statements, which is more modern and compiler-recognized than comments.
中文:考虑使用 attribute((fallthrough)) 或 C23 [[fallthrough]] 属性来显式标注 switch 语句中的穿透,这比注释更现代且能被编译器识别。

Copilot uses AI. Check for mistakes.
{
rt_wqueue_wakeup(&eeprom->device.wait_queue, (void*)POLLIN);
}
/* fall through */
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] English: Consider using attribute((fallthrough)) or the C23 [[fallthrough]] attribute for explicit fallthrough annotation in switch statements, which is more modern and compiler-recognized than comments.
中文:考虑使用 attribute((fallthrough)) 或 C23 [[fallthrough]] 属性来显式标注 switch 语句中的穿透,这比注释更现代且能被编译器识别。

Suggested change
/* fall through */
__attribute__((fallthrough));

Copilot uses AI. Check for mistakes.
    Requirement: The BSP for the k230 platform in the RT-Thread repository
    does not yet have an I2C driver.

    Solution: Provide I2C drivers for the k230 platform in the RT-Thread
    repository.
    1. support i2c master mode
    2. support i2c slave mode

Signed-off-by: Ze-Hou <[email protected]>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment on lines +648 to +653
// write data
_i2c_slave_write(eeprom, val);
}
else
{
// recv addr byte
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English: C++-style comments (//) are used instead of C-style comments (/* /). For consistency with RT-Thread coding standards and the rest of the codebase, use C-style comments.
中文:使用了 C++ 风格的注释(//)而不是 C 风格的注释(/
*/)。为了与 RT-Thread 编码标准和代码库的其余部分保持一致,请使用 C 风格的注释。

Suggested change
// write data
_i2c_slave_write(eeprom, val);
}
else
{
// recv addr byte
/* write data */
_i2c_slave_write(eeprom, val);
}
else
{
/* recv addr byte */

Copilot uses AI. Check for mistakes.
Comment on lines +648 to +653
// write data
_i2c_slave_write(eeprom, val);
}
else
{
// recv addr byte
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English: C++-style comments (//) are used instead of C-style comments (/* /). For consistency with RT-Thread coding standards and the rest of the codebase, use C-style comments.
中文:使用了 C++ 风格的注释(//)而不是 C 风格的注释(/
*/)。为了与 RT-Thread 编码标准和代码库的其余部分保持一致,请使用 C 风格的注释。

Suggested change
// write data
_i2c_slave_write(eeprom, val);
}
else
{
// recv addr byte
/* write data */
_i2c_slave_write(eeprom, val);
}
else
{
/* recv addr byte */

Copilot uses AI. Check for mistakes.
@Ze-Hou Ze-Hou requested a review from supperthomas October 27, 2025 08:32
#
CONFIG_RT_USING_SAL=y
CONFIG_SAL_INTERNET_CHECK=y
CONFIG_SOCKET_TABLE_STEP_LEN=4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个选项为何会新增?是因为执行 scons --menuconfig 自动加上的还是自己手动加上的结果?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这确认了,就是自动加的

# CONFIG_RT_USING_I2C is not set
CONFIG_RT_USING_I2C=y
# CONFIG_RT_I2C_DEBUG is not set
CONFIG_RT_USING_I2C_BITOPS=y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONFIG_RT_USING_I2C 和 CONFIG_RT_USING_I2C_BITOPS 为何会新增?是不是本地测试后 commit 时忘记清除了?
请最终提交时确保回退所有 .configrtconfig.h 的改动,然后只执行过一次 scons --menuconfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个我提交之前把.config重置了,然后打开menuconfig,然后保存一下

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个我提交之前把.config重置了,然后打开menuconfig,然后保存一下

你想说明什么问题我没有看懂,但实际问题就是这些 I2C 的配置默认就是关的,你这里开了是不对的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提交之前回退过,可能是又测试过,我改了之后处理一下

Select the I2C0 device mode to be used.

config BSP_USING_I2C0_MASTER
bool "Enable I2C0 Master Mode"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议去掉这里提示里的 I2C0,此时处于 I2C0 的域中不会歧义,二来提示语也可以简短些。

下同,其他也一并修改。

另外有个疑问,既然这里 Mode 是一个简单的二选一类型,要么 master,要么 slave,为何不用简单的 bool 型,用 choice + 两个 config 看上去比较啰嗦,而且定义了两个 BSP_USING_I2C0_MASTER 和 BSP_USING_I2C0_SLAVE 也容易引起歧义,譬如现在搜索 drv_i2c.c 是找不到 BSP_USING_I2C0_MASTER 的,只有 BSP_USING_I2C0_SLAVE,让人有些迷惑。

直接用 bool 会简单很多,譬如:

if BSP_USING_I2C0
    config BSP_I2C0_MASTER
    bool "Master OR Slave"
    default y
    help
    Selected if Master, else Slave
endif

代码中只要判断 #ifdef BSP_I2C0_MASTER, 这个和目前代码中的逻辑也一致,但不会引入二意性

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

struct _i2c_speed_cfg speed_cfg;
};

static rt_size_t i2c_get_timer(rt_size_t base)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数名建议都加上 “k230_” 的前缀。

另外不要定义 "rt_" 开头的函数名,譬如 “rt_hw_i2c_init” 避免和内核中 core 部分的函数重名。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个rt_size_t不是type吗

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个rt_size_t不是type吗

我是说函数名,你和我说 rt_size_t 是数据类型,没看懂你想说什么?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对啊,我除了rt_hw_i2c_init以 rt_ 开头了,这个是rtt要求的 rt_hw_”外设“_init的格式啊
image

rt_uint8_t* buffer;
rt_uint8_t ptr;
rt_bool_t flag_recv_ptr;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

针对 I2C 驱动中的 slave 模式我存在疑问:

  • 我看了一下 K230 的 TRM,在 I2C 章节并没有提 Slave 模式的支持,只说实现了 Master 模式,这个请确认一下。
  • 对于目前代码中的 Slave 模式,对 i2c_slave_eeprom 也存在疑问,这是一个芯片中真实存在的东西还是一个模拟的设备?我看了一下 cannan 的原始代码,有一行注释 “// simulate block device” ,再结合代码感觉就是内存中模拟了一个假设备,什么也没有做,完全是为了配合测试?
  • 如果 slave 连接的实际上就是个假设备,再结合 TRM 上并没有声明支持 Slave,我觉得最终的代码中就应该删除 slave 相关代码,请确认这个问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数据手册中I2C控制器支持主从模式,代码中的那个缓冲区是用于从机的,但是这个缓冲区又注册了dfs file,可以通过文件系统读写这个缓冲区,这个是参考cannan旧分支的代码

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

相当于使用ram模拟的,如果不要从机的我就把从机的删除,只保留主机的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我理解的cannan旧分支的意思是当使用从机是,得到的数据可以通过文件系统操作

Copy link
Contributor

@unicornx unicornx Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数据手册中I2C控制器支持主从模式,代码中的那个缓冲区是用于从机的,但是这个缓冲区又注册了dfs file,可以通过文件系统读写这个缓冲区,这个是参考cannan旧分支的代码

image

我没看到说支持 slave 模式,k230 采用的 dw 的 IP 也许是支持 slave mode,但从 k230 自己的手册上看他并没有用到。而且 canaan 原先的参考代码只是模拟了一个 eprom 设备,有什么实用价值吗?dfs 那个是次要问题,可以先不讨论。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Slave State Machine: Follows the protocol for a slave and monitors bus for address match.
Master State Machine: Generates the I2C protocol for the master transfers.

这是手册中说的

@unicornx
Copy link
Contributor

@Ze-Hou 另外补充一下,更新代码时请时刻保持基于最新的 master,我看你的开发分支有点旧了。

@Ze-Hou
Copy link
Contributor Author

Ze-Hou commented Oct 28, 2025

@Ze-Hou 另外补充一下,更新代码时请时刻保持基于最新的 master,我看你的开发分支有点旧了。

好的

@Ze-Hou Ze-Hou closed this Oct 28, 2025
@Ze-Hou Ze-Hou mentioned this pull request Oct 28, 2025
10 tasks
@unicornx unicornx mentioned this pull request Nov 3, 2025
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: K230 BSP related with K230 BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants