[RVB2601 Creative Application Development] Simulating UART 2
[Copy link]
[RVB2601 Creative Application Development] Simulating UART 1 https://en.eeworld.com/bbs/thread-1199753-1-1.html
Continuing from the last article, I worked until 11pm yesterday and got up at 6am. Until now, I have finally finished writing the reception and it is ok after some debugging.
Compared with sending, receiving requires IO interrupts and timer data reception, which is relatively more complicated. I spent a lot of time debugging because I didn't know enough about the judgment of the serial port start bit. Here is the successful function attached:
//发送一个字符
void send_Char(uint8_t c)
{
aos_msleep(1);
io_uart0_tx_write_Low;//发送起始位 高拉低电
io_uart_para.vm_uart_tx_byte = c;
io_uart_para.vm_uart_tx_bit = 0;
io_uart_para.vm_uart_dir = VM_SEND;
io_uart0_rx_timer_enable();
while(io_uart_para.vm_uart_dir == IDLE);
}
//发送字串符
void send_str(uint8_t buf[],uint8_t len)
{
uint8_t i;
for(i<0; i<len; i++){
send_Char(buf[i]);
}
}
//定时器回调函数
void timer1_handler(void *arg)
{
if(io_uart_para.vm_uart_dir == VM_SEND)//如果是发送
{
if(io_uart_para.vm_uart_tx_bit <=8 ) //如果发送不足8位
{
if((io_uart_para.vm_uart_tx_byte & 0x01) == 0x01 ){
io_uart0_tx_write_High;
}else{
io_uart0_tx_write_Low;
}
io_uart_para.vm_uart_tx_byte>>=1;
}
else{
if(io_uart_para.vm_uart_tx_bit > 8){
io_uart0_tx_write_High;
//io_uart0_rx_timer_disable();
io_uart_para.vm_uart_tx_flag = 0; //设置为发送完毕
io_uart_para.vm_uart_dir = IDLE;
}
}
io_uart_para.vm_uart_tx_bit ++;
}
else if(io_uart_para.vm_uart_rx_irq == VM_REVC)
{
if(io_uart_para.vm_uart_rx_count == 0)//处理起始位
{
if(io_uart0_rx_ReadPin() == 0)//起始位为0时,清零接收缓冲器,准备接收数据位
{
io_uart_para.vm_uart_rx_count ++;
io_uart_para.vm_uart_tx_byte = 0;
//关闭接收IO中断
io_uart0_rx_IO_disable();
}
else //起始位不为 0 时,中止接收
{
io_uart_para.vm_uart_rx_irq = IDLE;
io_uart0_rx_IO_enable();
}
}
else if(io_uart_para.vm_uart_rx_count <= 8)
{
io_uart_para.vm_uart_rx_byte>>=1;
if(io_uart0_rx_ReadPin() == 1)
{
io_uart_para.vm_uart_rx_byte |= 0x80;
}
io_uart_para.vm_uart_rx_count ++;
}
else //停止位处理
{
io_uart0_rx_IO_enable();
io_uart_para.vm_uart_rx_count = 0;
io_uart_para.vm_uart_rx_irq = IDLE;
io_uart_para.vm_uart_rx_buf[io_uart_para.vm_uart_rx_flag] = io_uart_para.vm_uart_rx_byte;
io_uart_para.vm_uart_rx_flag = io_uart_para.vm_uart_rx_flag +1;
}
}
}
//io_uart0rx回调函数
static void io_uart0_rx_interrupt_handler(csi_gpio_pin_t *pin,void *arg)
{
if(csi_gpio_pin_read(&io_uart0_rx)==GPIO_PIN_LOW)
{
io_uart_para.vm_uart_rx_irq = VM_REVC;//接收标志
io_uart0_rx_IO_disable();
//关掉接收中断
}
}
The effect is as follows:
The effect is good, there is no packet loss or garbled code. But this is still the ideal situation of sending and receiving. I don't know if there will be any bugs when used in the project. Welcome to give your advice.
Attached files:
main.c
(5.42 KB, downloads: 0)
|