1420 views|2 replies

6821

Posts

11

Resources
The OP
 

[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)



This post is from Domestic Chip Exchange

Latest reply

This is a bit NB, you need to fully understand the hardware   Details Published on 2022-4-15 20:54
 
 

2w

Posts

74

Resources
2
 

Hard:)

This post is from Domestic Chip Exchange
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

280

Posts

7

Resources
3
 

This is a bit NB, you need to fully understand the hardware

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list