Skip to content

Conversation

@CYFS3
Copy link
Contributor

@CYFS3 CYFS3 commented Dec 24, 2025

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

[

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

测试用例

#include <rtthread.h>
#include <rtdevice.h>

#define CAN_DEV_NAME       "can0"      // The name of the CAN device

static rt_device_t can_dev;            // CAN device handle
static struct rt_semaphore rx_sem;     // Semaphore for message reception

// Callback function for CAN reception
static rt_err_t can_rx_callback(rt_device_t dev, rt_size_t size)
{
    // The CAN interrupt calls this callback when data is received.
    // Release the semaphore to notify the receiving thread.
    rt_sem_release(&rx_sem);
    return RT_EOK;
}

static void can_rx_thread(void *parameter)
{
    rt_err_t res;
    struct rt_can_msg rx_msg = {0};

    // Set the receive callback function
    rt_device_set_rx_indicate(can_dev, can_rx_callback);

#ifdef RT_CAN_USING_HDR
    // Example of configuring multiple hardware filters
    struct rt_can_filter_item items[] =
    {
        // Filter 1: Match standard frames with IDs from 0x100 to 0x1FF. hdr_index will be -1.
        RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
        // Filter 2: Match standard frames with IDs from 0x300 to 0x3FF. hdr_index will be -1.
        RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
        // Filter 3: Exactly match standard frame with ID 0x211. hdr_index will be -1.
        RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7FF, RT_NULL, RT_NULL),
        // Filter 4: Exactly match standard frame with ID 0x486 using a helper macro. hdr_index will be -1.
        RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL),
        // Filter 5: Exactly match standard frame with ID 0x555 and explicitly assign it to filter bank #7.
        // This uses direct struct initialization: {id, ide, rtr, mode, mask, hdr_bank}.
        {0x555, 0, 0, 0, 0x7FF, 7}
    };
    // Create the filter configuration structure with 5 active filters.
    struct rt_can_filter_config cfg = {sizeof(items)/sizeof(items[0]), 1, items};
    // Set the hardware filters for the CAN device.
    res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
    RT_ASSERT(res == RT_EOK);
#endif

    // Some drivers might require an explicit start command.
    // This is driver-specific.
    rt_uint32_t cmd_arg = 1; // Argument to enable the controller
    res = rt_device_control(can_dev, RT_CAN_CMD_START, &cmd_arg);
    RT_ASSERT(res == RT_EOK);

    while (1)
    {
        // Block and wait for the semaphore, which is released by the receive callback.
        rt_sem_take(&rx_sem, RT_WAITING_FOREVER);

        // Read one frame of data from the CAN device's general message queue.
        rx_msg.hdr_index = -1;
        rt_device_read(can_dev, 0, &rx_msg, sizeof(rx_msg));

        // Print the received message's ID and data.
        rt_kprintf("Received a message. ID: 0x%x, Data: ", rx_msg.id);
        for (int i = 0; i < rx_msg.len; i++)
        {
            rt_kprintf("%02x ", rx_msg.data[i]);
        }
        rt_kprintf("\n");
    }
}

int can_sample(int argc, char *argv[])
{
    rt_err_t res;
    rt_thread_t thread;
    char can_name[RT_NAME_MAX];

    // Allow specifying the CAN device name from the command line, e.g., "can_sample can2"
    if (argc == 2)
    {
        rt_strncpy(can_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
    }

    // Find the CAN device by name
    can_dev = rt_device_find(can_name);
    if (!can_dev)
    {
        rt_kprintf("find device %s failed!\n", can_name);
        return -RT_ERROR;
    }

    // Initialize the receive semaphore
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);

    // Open the CAN device in interrupt-driven TX/RX mode
    res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
    RT_ASSERT(res == RT_EOK);


    // Create and start the data receiving thread
    thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        rt_kprintf("create can_rx thread failed!\n");
        return -RT_ERROR;
    }

    rt_kprintf("CAN device %s opened successfully.\n", can_name);

    // --- Demonstrate Blocking Send ---
    struct rt_can_msg blocking_msg = {0};
    blocking_msg.id = 0x78;
    blocking_msg.ide = RT_CAN_STDID;
    blocking_msg.rtr = RT_CAN_DTR;
    blocking_msg.len = 8;
    // The `nonblocking` flag is 0 by default for blocking mode.
    for(int i = 0; i < 8; i++) blocking_msg.data[i] = i;

    rt_kprintf("Attempting to send a message in BLOCKING mode...\n");
    if (rt_device_write(can_dev, 0, &blocking_msg, sizeof(blocking_msg)) == sizeof(blocking_msg))
    {
        rt_kprintf("Blocking message sent successfully.\n");
    }
    else
    {
        rt_kprintf("Blocking message send failed.\n");
    }

    rt_thread_mdelay(100); // Wait a moment for clarity in the log

    // --- Demonstrate Non-Blocking Send ---
    struct rt_can_msg nonblocking_msg = {0};
    nonblocking_msg.id = 0x79;
    nonblocking_msg.ide = RT_CAN_STDID;
    nonblocking_msg.rtr = RT_CAN_DTR;
    nonblocking_msg.len = 4;
    nonblocking_msg.data[0] = 0xDE;
    nonblocking_msg.data[1] = 0xAD;
    nonblocking_msg.data[2] = 0xBE;
    nonblocking_msg.data[3] = 0xEF;
    nonblocking_msg.nonblocking = 1; // <-- Key: Set the non-blocking flag

    rt_kprintf("Attempting to send a message in NON-BLOCKING mode...\n");
    if (rt_device_write(can_dev, 0, &nonblocking_msg, sizeof(nonblocking_msg)) == sizeof(nonblocking_msg))
    {
        rt_kprintf("Non-blocking message was accepted (sent or enqueued).\n");
    }
    else
    {
        rt_kprintf("Non-blocking send failed (buffer was full).\n");
    }

    return res;
}
// Export the function to the MSH command line
MSH_CMD_EXPORT(can_sample, can device usage example);


image

你的解决方案是什么 (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自查

@CYFS3 CYFS3 requested a review from Rbb666 as a code owner December 24, 2025 05:30
@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:mcxa346_can
  • 设置PR number为 \ Set the PR number to:11082
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 mcxa346_can 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the mcxa346_can 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!

@github-actions github-actions bot added BSP: NXP Code related with NXP BSP labels Dec 24, 2025
@github-actions
Copy link

📌 Code Review Assignment

🏷️ Tag: bsp_mcxa

Reviewers: @hywing

Changed Files (Click to expand)
  • bsp/nxp/mcx/mcxa/Libraries/drivers/SConscript
  • bsp/nxp/mcx/mcxa/Libraries/drivers/drv_can.c
  • bsp/nxp/mcx/mcxa/frdm-mcxa346/board/Kconfig
  • bsp/nxp/mcx/mcxa/frdm-mcxa346/board/MCUX_Config/board/pin_mux.c

📊 Current Review Status (Last Updated: 2025-12-24 13:30 CST)


📝 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.

@Rbb666 Rbb666 requested a review from kurisaW December 25, 2025 00:43
PORT_SetPinConfig(PORT3, 28U, &port3_28_pin73_config);


const port_pin_config_t port1_0_pin135_config = {/* Internal pull-up/down resistor is disabled */
Copy link
Member

Choose a reason for hiding this comment

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

这段配置的删除会不会对SPI外设使能有所影响

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这段配置的删除会不会对SPI外设使能有所影响

我看kconfig中也没有spi0的配置我就全删掉了,如果要保留的话得注意引脚会有冲突

@kurisaW
Copy link
Member

kurisaW commented Dec 25, 2025

@CYFS3 有时间的话可以帮忙看下nxp smart ci报错的原因吗,可以在linux下拉一下工具链然后本地测试看看是不是缺少依赖项使能

@CYFS3
Copy link
Contributor Author

CYFS3 commented Dec 25, 2025

@CYFS3 有时间的话可以帮忙看下nxp smart ci报错的原因吗,可以在linux下拉一下工具链然后本地测试看看是不是缺少依赖项使能

好的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: NXP Code related with NXP BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants