看老代码发现的bug。修改波特率的时候,先关闭串口设备,然后再打开,打开的时候多加了一个RT_DEVICE_FLAG_DMA_TX,此时,rt_device_write(env_dev, 0, string, strlen(string));这样最基础的发送函数都发不出来。
初始化时代码如下 struct serial_configure pconfig;
/* 初始化串口 /
if (env_dev == RT_NULL)
{
env_dev = rt_device_find(ENV_NAME);
RT_ASSERT(env_dev);
pconfig.baud_rate = BAUD_RATE_9600;
pconfig.data_bits = DATA_BITS_8;
pconfig.stop_bits = STOP_BITS_1;
pconfig.parity = PARITY_NONE;
pconfig.bit_order = BIT_ORDER_LSB;
pconfig.invert = NRZ_NORMAL;
pconfig.bufsz = 4096;
pconfig.reserved = 0;
/ 设置串口参数 */
rt_device_control(env_dev, RT_DEVICE_CTRL_CONFIG, &pconfig);
/*设置串口的回调函数 */
rt_device_set_rx_indicate(env_dev, env_recv_ind);
}
// if(RT_EOK ==rt_device_open(env_dev, RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX))
if(RT_EOK ==rt_device_open(env_dev, RT_DEVICE_FLAG_INT_RX ))
{
LOG_D("Screen serial open success.\r\n");
// return RT_EOK;
}
else
{
LOG_D("Screen serial open failure.\r\n");
// return RT_ERROR;
}
修改波特率时的代码如下 tmp_baud = (tmp_baud == 1)?2:1;
struct serial_configure pconfig;
if (tmp_baud == 1)
{
pconfig.baud_rate = BAUD_RATE_9600;
}
else if (tmp_baud == 2)
{
pconfig.baud_rate = BAUD_RATE_115200;
}
sys_para.env_bdrate = tmp_baud;//修改全局变量
// USART3->BRR =
pconfig.data_bits = DATA_BITS_8;
pconfig.stop_bits = STOP_BITS_1;
pconfig.parity = PARITY_NONE;
pconfig.bit_order = BIT_ORDER_LSB;
pconfig.invert = NRZ_NORMAL;
pconfig.bufsz = 4096;
pconfig.reserved = 0;
rt_device_close(env_dev);
/* 设置串口参数 */
rt_device_control(env_dev, RT_DEVICE_CTRL_CONFIG, &pconfig);
// rt_device_open(env_dev, RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX);
rt_device_open(env_dev, RT_DEVICE_FLAG_INT_RX );
打开的时候rt_device_open(env_dev, RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX); 此时旧无法发送。 很奇怪,env_dev是串口3,也没有和别的DMA有冲突啊,就算有冲突也可以仲裁啊。谢谢 补充一下发送的内容 //数组是全局变量
static const rt_uint8_t cmd1[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x02, 0x65, 0xCB};//light
static const rt_uint8_t cmd2[] = {0x02, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xF9};//ch5
static const rt_uint8_t cmd3[] = {0x03, 0x03, 0x00, 0x02, 0x00, 0x01, 0x24, 0x28};//h2s
static const rt_uint8_t cmd4[] = {0x04, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x5E};//pm2.5
//发送部分代码
//根据索引
if (index == 0)
{
rt_kprintf("write cmd1\n");
rt_device_write(serial, 0, cmd1, 8);
}
else if (index == 1)
{
rt_kprintf("write cmd2\n");
rt_device_write(serial, 0, cmd2, 8);
}
else if (index == 2)
{
rt_kprintf("write cmd3\n");
rt_device_write(serial, 0, cmd3, 8);
}
else if (index == 3)
{
rt_kprintf("write cmd4\n");
rt_device_write(serial, 0, cmd4, 8);
}
index++;
if (index == 4)
{
index = 0;
}
|