rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

fix(serial_v2): tx_fifo->rb.buffer_ptr not init when RT_DEVICE_FLAG_DMA_TX and RT_SERIAL_TX_BLOCKING

Open ComerLater opened this issue 1 year ago • 3 comments

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

[

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

serial_v2驱动框架,rt_serial_tx_enable()函数,在 DMA阻塞发送(RT_DEVICE_FLAG_DMA_TX and RT_SERIAL_TX_BLOCKING) 的时候,使用rt_malloc分配了tx_fifo内存,但是并未对tx_fifo->rb.buffer_ptr指针进行初始化(rt_malloc不会自动初始化内存),因此可能导致buffer_ptr是一个随机指针。但是rt_serail_write()函数需要根据tx_fifo->rb.buffer_ptr是否为RT_NULL来判断是调用_serial_fifo_tx_blocking_nbuf()还是_serial_fifo_tx_blocking_buf()

由于tx_fifo->rb.buffer_ptr可能是一个随机值(不一定为RT_NULL=0),导致RT_DEVICE_FLAG_DMA_TX and RT_SERIAL_TX_BLOCKING模式下调用了_serial_fifo_tx_blocking_buf(),但是rb没有真正开辟内存空间,导致内存异常

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

rt_serial_tx_enable(),在DMA阻塞发送分支下,对tx_fifo->rb.buffer_ptr进行初始化,赋值为RT_NULL。

/* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
 * the control() API is called to configure the serial device */
tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc
        (sizeof(struct rt_serial_tx_fifo));
RT_ASSERT(tx_fifo != RT_NULL);

/* Init rb.buffer_ptr to RT_NULL, in rt_serial_write() need check it
 * otherwise buffer_ptr maybe a random value, as rt_malloc not init memory */
tx_fifo->rb.buffer_ptr = RT_NULL;
serial->serial_tx = tx_fifo;

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

  • BSP: bsp/stm32/stm32h743-atk-apollo
  • .config:
  • action:

]

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

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

  • [ ] 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • [x] 本拉取/合并请求是一个成熟版本 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:

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

ComerLater avatar Dec 22 '23 17:12 ComerLater

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Dec 22 '23 17:12 CLAassistant

请您签署一下github CLA @drjlab

kurisaW avatar Dec 28 '23 09:12 kurisaW

请您签署一下github CLA @drjlab

已经完成github CLA签署了,谢谢

ComerLater avatar Dec 29 '23 03:12 ComerLater