请问在使用serial_v2的时候,这个问题正常吗?还是说我的理解存在错误?
原代码
static rt_size_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
RT_ASSERT(dev != RT_NULL);
if (size == 0) return 0;
serial = (struct rt_serial_device *)dev;
RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
if (tx_fifo->activated == RT_TRUE) return 0;
tx_fifo->activated = RT_TRUE;
rt_size_t length = size;
rt_size_t offset = 0;
while (size)
{
tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb),
(rt_uint8_t *)buffer + offset,
size);
offset += tx_fifo->put_size;
size -= tx_fifo->put_size;
serial->ops->transmit(serial,
(rt_uint8_t *)buffer + offset,
tx_fifo->put_size,
RT_SERIAL_TX_BLOCKING);
rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
}
return length;
}
异常点在于offset,尚未发出去,就递增了。
offset += tx_fifo->put_size;
size -= tx_fifo->put_size;
引导调用发送接口的时候,buffer + offset指针偏移的位置不正确。
serial->ops->transmit(serial,
(rt_uint8_t *)buffer + offset,
tx_fifo->put_size,
RT_SERIAL_TX_BLOCKING);
修改后代号,数据发送正常。
static rt_size_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
RT_ASSERT(dev != RT_NULL);
if (size == 0) return 0;
serial = (struct rt_serial_device *)dev;
RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
if (tx_fifo->activated == RT_TRUE) return 0;
tx_fifo->activated = RT_TRUE;
rt_size_t length = size;
rt_size_t offset = 0;
while (size)
{
tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb),
(rt_uint8_t *)buffer + offset,
size);
serial->ops->transmit(serial,
(rt_uint8_t *)buffer + offset,
tx_fifo->put_size,
RT_SERIAL_TX_BLOCKING);