Skip to content

bsp/nrf5x: nrf52840添加spim驱动 #10576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

HaryyZ
Copy link

@HaryyZ HaryyZ commented Aug 2, 2025

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

[

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

在我的一个项目中,需要nrf52840的spi接口写入大量的数据,需要用到DMA,但是目前的bsp中只有spi的驱动。

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

我自己根据rtt的spi风格,自行编写了spim的驱动。

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

  • BSP:bsp/nrf5x/nrf52840
  • .config:需要添加

  • menuconfig BSP_USING_SPIM
    bool "Enable SPIM"
    select RT_USING_SPIM
    default n

      if BSP_USING_SPIM
          config BSP_USING_SPIM0
              bool "Enable SPIM0 bus"
              default y
          if BSP_USING_SPIM0
              config NRFX_SPIM0_ENABLED
                  int "Enable SPI0 instance"
                  default 1
              config BSP_SPIM0_SCK_PIN
                  int "SPIM0 sck pin number set"
                  range 0 47
                  default 28
              config BSP_SPIM0_MOSI_PIN
                  int "SPIM0 mosi pin number set"
                  range 0 47
                  default 29
              config BSP_SPIM0_MISO_PIN
                  int "SPIM0 miso pin number set"
                  range 0 47
                  default 30
              config BSP_SPIM0_SS_PIN
                  int "SPIM0 ss pin number set"
                  range 0 47
                  default 31
          endif
    
          config BSP_USING_SPIM1
              bool "Enable SPIM1 bus"
              default n
          if BSP_USING_SPIM1
              config BSP_SPIM1_SCK_PIN
                  int "SPIM1 sck pin number set"
                  range 0 47
                  default 28
              config BSP_SPIM1_MOSI_PIN
                  int "SPIM1 mosi pin number set"
                  range 0 47
                  default 29
              config BSP_SPIM1_MISO_PIN
                  int "SPIM1 miso pin number set"
                  range 0 47
                  default 30
              config BSP_SPIM1_SS_PIN
                  int "SPIM1 ss pin number set"
                  range 0 47
                  default 31
          endif
    
          config BSP_USING_SPIM2
              bool "Enable SPIM2 bus"
              default n
          if BSP_USING_SPIM2
              config BSP_SPIM2_SCK_PIN
                  int "SPIM2 sck pin number set"
                  range 0 47
                  default 28
              config BSP_SPIM2_MOSI_PIN
                  int "SPIM2 mosi pin number set"
                  range 0 47
                  default 29
              config BSP_SPIM2_MISO_PIN
                  int "SPIM2 miso pin number set"
                  range 0 47
                  default 30
              config BSP_SPIM2_SS_PIN
                  int "SPIM2 ss pin number set"
                  range 0 47
                  default 31
          endif
          
    
      config BSP_USING_SPIM3
          bool "Enable SPIM3 bus"
          default n
      if BSP_USING_SPIM3
          config BSP_SPIM3_SCK_PIN
              int "SPIM3 sck pin number set"
              range 0 47
              default 28
          config BSP_SPIM3_MOSI_PIN
              int "SPIM3 mosi pin number set"
              range 0 47
              default 29
          config BSP_SPIM3_MISO_PIN
              int "SPIM3 miso pin number set"
              range 0 47
              default 30
          config BSP_SPIM3_SS_PIN
              int "SPIM3 ss pin number set"
              range 0 47
              default 31
          endif
    
      endif
    

]

当前拉取/合并请求的状态 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/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@CLAassistant
Copy link

CLAassistant commented Aug 2, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the BSP label Aug 2, 2025
Copy link

github-actions bot commented Aug 2, 2025

📌 Code Review Assignment

🏷️ Tag: bsp_nrf5x

Reviewers: ReviewSun

Changed Files (Click to expand)
  • bsp/nrf5x/libraries/drivers/drv_adc.c
  • bsp/nrf5x/libraries/drivers/drv_adc.h
  • bsp/nrf5x/libraries/drivers/drv_flash.c
  • bsp/nrf5x/libraries/drivers/drv_gpio.c
  • bsp/nrf5x/libraries/drivers/drv_gpio.h
  • bsp/nrf5x/libraries/drivers/drv_hwtimer.c
  • bsp/nrf5x/libraries/drivers/drv_i2c.c
  • bsp/nrf5x/libraries/drivers/drv_i2c.h
  • bsp/nrf5x/libraries/drivers/drv_pwm.c
  • bsp/nrf5x/libraries/drivers/drv_qspi_flash.c
  • bsp/nrf5x/libraries/drivers/drv_rtc.c
  • bsp/nrf5x/libraries/drivers/drv_spi.c
  • bsp/nrf5x/libraries/drivers/drv_spi.h
  • bsp/nrf5x/libraries/drivers/drv_spim.c
  • bsp/nrf5x/libraries/drivers/drv_spim.h
  • bsp/nrf5x/libraries/drivers/drv_uart.c
  • bsp/nrf5x/libraries/drivers/drv_uarte.c
  • bsp/nrf5x/libraries/drivers/drv_uarte.h
  • bsp/nrf5x/libraries/drivers/drv_wdt.c
  • bsp/nrf5x/libraries/drivers/nrfx_spim.c
  • bsp/nrf5x/libraries/drivers/nrfx_spim.h
  • bsp/nrf5x/nrf52840/board/Kconfig

📊 Current Review Status (Last Updated: 2025-08-03 13:59 CST)

  • ReviewSun 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.

@HaryyZ HaryyZ changed the title 1002 nrf52840添加spim驱动 Aug 3, 2025
@HaryyZ
Copy link
Author

HaryyZ commented Aug 3, 2025

尊敬的审查者,您好!
我已根据之前的反馈对 PR 进行了更新,主要包括:

修复了代码格式问题:tab转为空格;

调整了头文件的包含路径:删除了不必要的头文件;

重新编译一边,并完成了相关功能的测试。
希望您能抽空审查,感谢您的辛勤工作与支持!🙏

@supperthomas supperthomas changed the title nrf52840添加spim驱动 bsp/nrf5x: nrf52840添加spim驱动 Aug 4, 2025
@supperthomas
Copy link
Member

请在bsp/nrf5x/nrf52840/.ci/attachconfig/ci.attachconfig.yml 中添加测试的配置

@supperthomas
Copy link
Member

CI 问题也要处理一下

@ReviewSun
Copy link
Contributor

描述规范请修改

@HaryyZ
Copy link
Author

HaryyZ commented Aug 4, 2025

  1. 我找到了ci.attachconfig.yml,截取了一段,我是添加了spim的设备驱动,请问一下这个文件怎么修改:
    devices.i2c:
    kconfig:

    • CONFIG_BSP_USING_I2C=y
      devices.spi:
      kconfig:
    • CONFIG_RT_USING_SPI=y
    • CONFIG_BSP_USING_SPI=y
      devices.uart:
      kconfig:
    • CONFIG_BSP_USING_UART=y
      devices.watchdog:
      kconfig:
    • CONFIG_BSP_USING_WDT=y
  2. 另外ci检查出的错误的位置是bsp/nrf5x/libraries/drivers/drv_hwtimer.c at line 141:
    经过检查,问题可能是在该文件102行的timer_callback,问题有两个:
    我在nrf5x文件夹下,用全局搜索struct rt_hwtimer_device,没有找到这个结构体的定义。
    timer_info没有声明,但是在LOG_D中使用。

@HaryyZ
Copy link
Author

HaryyZ commented Aug 4, 2025

102行-110行:
static void timer_callback(nrf_timer_event_t event_type, void* p_context)
{
rt_hwtimer_t *timer_device = (struct rt_hwtimer_device *)p_context;

/* no matter what event_type is(NRF_TIMER_EVENT_COMPARE0 or others), call same function "rt_device_hwtimer_isr" */
LOG_D("timer_callback event_type = %d, inst_id = %d, cc conunt = %d\r\n",
       event_type, timer_info->timer_inst.instance_id, timer_info->timer_inst.cc_channel_count);
rt_device_hwtimer_isr(timer_device);

}

@supperthomas
Copy link
Member

@supperthomas
Copy link
Member

102行-110行: static void timer_callback(nrf_timer_event_t event_type, void* p_context) { rt_hwtimer_t *timer_device = (struct rt_hwtimer_device *)p_context;

/* no matter what event_type is(NRF_TIMER_EVENT_COMPARE0 or others), call same function "rt_device_hwtimer_isr" */
LOG_D("timer_callback event_type = %d, inst_id = %d, cc conunt = %d\r\n",
       event_type, timer_info->timer_inst.instance_id, timer_info->timer_inst.cc_channel_count);
rt_device_hwtimer_isr(timer_device);

}

CI如果确定不是问题,可以不用修改

@Rbb666 Rbb666 requested a review from Copilot August 5, 2025 11:07
Copy link
Contributor

@Copilot 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 SPIM (SPI Master with EasyDMA) driver support for the nRF52840 BSP, providing DMA functionality for SPI transfers to handle large data volumes efficiently.

  • Adds complete SPIM driver implementation with DMA support for nRF52840
  • Corrects pin range configurations from 31/64 to 47/48 for nRF52840 compatibility
  • Updates copyright years across multiple driver files

Reviewed Changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
bsp/nrf5x/nrf52840/board/Kconfig Adds SPIM configuration options and corrects pin ranges
bsp/nrf5x/libraries/drivers/nrfx_spim.h New SPIM driver header with API definitions
bsp/nrf5x/libraries/drivers/nrfx_spim.c Complete SPIM driver implementation with DMA support
bsp/nrf5x/libraries/drivers/drv_spim.h RT-Thread SPIM driver wrapper header
bsp/nrf5x/libraries/drivers/drv_spim.c RT-Thread SPIM driver wrapper implementation
Various driver files Copyright year updates and minor formatting fixes
Comments suppressed due to low confidence (2)

bsp/nrf5x/nrf52840/board/Kconfig:356

  • Missing 'endif' statement for the SPIM3 configuration block. The SPIM3 section is opened but not properly closed.
        config BSP_USING_SPIM3

bsp/nrf5x/libraries/drivers/drv_spim.c:14

  • The USING_SPI_DMA macro should be defined in a header file or configuration file rather than hardcoded in the source file, to allow for better configurability.
#ifdef BSP_USING_SPIM

range 0 47
default 31
endif

Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

[nitpick] There is an extra blank line after 'endif' that should be removed for consistency with the rest of the configuration structure.

Suggested change

Copilot uses AI. Check for mistakes.

* - SS pin active low
* - over-run character set to 0xFF
* - clock frequency: 4 MHz
* - mode: 0 (SCK active high, sample on leading edge of the clock signa;)
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

Typo in comment: 'signa;)' should be 'signal)'.

Suggested change
* - mode: 0 (SCK active high, sample on leading edge of the clock signa;)
* - mode: 0 (SCK active high, sample on leading edge of the clock signal)

Copilot uses AI. Check for mistakes.

static void finish_transfer(spim_control_block_t * p_cb)
{

#if USING_SPI_DMA
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

The macro USING_SPI_DMA is used but not defined in this file. It should be defined or included from a header file to avoid compilation issues.

Copilot uses AI. Check for mistakes.

struct rt_spi_message * message,
struct rt_spi_device * dev)
{
#define DMA_TRANS_MIN_LEN (20) /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

Magic number 20 for DMA_TRANS_MIN_LEN should be moved to a header file or made configurable, as it's a critical threshold for DMA usage decision.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants