Detailed debugging tutorial of CuteCom serial port under Ubuntu

I.MX6ULL embedded development learning-serial debugging

1. Installation of serial debugging assistant under Ubuntu

  During the embedded development and learning process, I learned the chapter of serial port debugging. Previously, there was a corresponding serial port debugging interface during Win10 operation. You can realize serial communication when you install a serial port driver and see COM3 in the computer device port. Therefore, I hope to have a serial port debugging interface when learning the serial port experiment in the Ubantu system.

1. Installation of CuteCom serial port debugging tool

   To install, first download the software. Both the terminal and the software center can be downloaded. You can download it directly by searching for CuteCom in the Ubuntu software store, or you can enter the following commands through the terminal to download.

sudo apt-get install cutecom

After downloading and installing, you can search through the software center to open the icon (may not work normally because you do not have root permissions), so it is recommended to use the command line to open the terminal input, enter:

sudo cutecom

After opening the serial port graphical interface:
Insert picture description here

2. Configure CuteCom serial port debugging tool

   Just like win, we also need to find the corresponding serial port. When we actually use it, most of them use USB to serial port. That is ttyUSB*. You can plug in and unplug the USB-to-serial cable, and then check the kernel print:

dmesg | grep ttyS*   

Insert picture description here
Compare the plug-in/out print information before and after to find the corresponding serial port.
  In embedded development, most of them use USB to serial port. That is ttyUSB*. But in CuteCom, the strange thing is that the serial port ttyUSB0 cannot be found in the device, so here we need to do it ourselvesManually enter to addOwn port. As shown in the figure, I added the serial port of my computer myself, and the baud rate is set to 115200.

Insert picture description here

If it prompts Could not open /dev/ttyUSB0, this is because the read and write permissions of ttyUSB0 are not enough, just change the permissions.

sudo chmod 666 /dev/ttyUSB0

3. Debug CuteCom

  After confirming, select Device, set the baud rate, parity, etc. of the device that needs to communicate, and click Open to open the serial port. If you need to send hexadecimal data, just select input in the lower right corner of the interface. After everything is ready, you can communicate with the lower computer!
Insert picture description here

Two, i.MX6ULL programming

1. UART principle

  UART is a kind of serial port. Its working principle is to transmit data bit by bit, and use a wire for sending and receiving. Therefore, it only needs three wires to connect to the outside world through the UART interface: TXD (transmit), RXD (receive) ) And GND (ground wire). The general interface levels of UART are TTL and RS-232. Generally, there are pins such as TXD and RXD on the development board. Low level of these pins indicates logic 0, and high level indicates logic 1, which is TTL level. RS-232 adopts differential line, -3~-15V means logic 1, +3~+15V means logic 0.
  Idle bit: When the data line is in the idle state, it is in the logic "1" state, which is high level, which means that there is no data line idle and no data transmission.
  Start bit: When you want to transmit data, first transmit a logic "0", that is, pull the data line low to indicate the start of data transmission.
  Data bit: The data bit is the actual data to be transmitted. The number of data bits can be selected from 5 to 8 bits. We generally transmit data in bytes, 8 bits per byte, so the data bits are usually 8 bits. The low bit is transmitted first, and the high bit is transmitted last.
  Parity check bit: This is used to perform parity check on the number of "1" bits in the data, and the parity check function may not be used.
  Stop bit: The data transmission complete flag bit. The number of stop bits can be 1 bit, 1.5 bit or 2 high level. Generally, 1 stop bit is selected.
  Baud rate: The baud rate is the rate of UART data transmission, that is, the number of data bits transmitted per second, generally 9600, 19200, 115200, etc.

2. UART code articles

  Here only talk about the part of the i.MX6ULL serial port.
  Write two open and close serial port functions and soft reset function through the register UCR1.

//关闭串口函数
void uart_disable(UART_Type *base)  
{
    
    
    base->UCR1 &= ~(1<<0);   //关闭串口1
}

//打开串口函数
void uart_enable(UART_Type *base)  //打开串口函数
{
    
    
    base->UCR1 |= (1<<0);   //打开串口1
}
//软复位
void uart_softreset(UART_Type *base)
{
    
    
    base->UCR2 &= ~(1<<0); /* 复位 UART */
    while((base->UCR2 & 0x1) == 0); /* 等待复位完成 */
}

  Initialize the IO pins of RX and TX through library functions.

//串口IO初始化
void uart_io_init(void)
{
    
    
    //配置TX,RX引脚的服用io口和电器属性

    IOMUXC_SetPinMux(IOMUXC_UART1_TX_DATA_UART1_TX, 0);                    //TX脚IO复用配置,....
    IOMUXC_SetPinMux(IOMUXC_UART1_RX_DATA_UART1_RX, 0);                    //RX脚IO复用配置,....
    IOMUXC_SetPinConfig(IOMUXC_UART1_TX_DATA_UART1_TX, 0x10B0);            //TX脚电器属性
    IOMUXC_SetPinConfig(IOMUXC_UART1_RX_DATA_UART1_RX, 0x10B0);            //RX脚电器属性
}

  Serial port initialization function

//串口初始化
void uart_init(void)
{
    
    
    uart_io_init();  //串口IO初始化
   
    uart_disable(UART1); /* 先关闭 UART1 */
    uart_softreset(UART1); //软复位
    UART1->UCR1 = 0; /* 先清除 UCR1 寄存器 */
    UART1->UCR1 &= ~(1<<14); /* 关闭自动波特率检测 */

    UART1->UCR2 |= (1<<14) | (1<<5) | (1<<2) | (1<<1); //  设置 UART 的 UCR2 寄存器,设置字长,停止位,校验模式,关闭硬件流控
    UART1->UCR3 |= 1<<2; /* UCR3 的 bit2 必须为 1 */
/*
* 设置波特率
* 波特率计算公式:Baud Rate = Ref Freq / (16 * (UBMR + 1)/(UBIR+1)) 
* 如果要设置波特率为 115200,那么可以使用如下参数:
* Ref Freq = 80M 也就是寄存器 UFCR 的 bit9:7=101, 表示 1 分频
* UBMR = 3124
* UBIR = 71
* 因此波特率= 80000000/(16 * (3124+1)/(71+1))
* = 80000000/(16 * 3125/72) 
* = (80000000*72) / (16*3125) 
* = 115200
*/
    UART1->UFCR = 5<<7; /* ref freq 等于 ipg_clk/1=80Mhz */
    UART1->UBIR = 71;
    UART1->UBMR = 3124;

#if 0
uart_setbaudrate(UART1, 115200, 80000000); /* 设置波特率 */
#endif
 
uart_enable(UART1); /* 使能串口 */

}

  Finally, send characters and accept character functions.

//通过串口发送字符
void put_char(unsigned char c)
{
    
    
    while(((UART1->USR2 >> 3) &0X01) == 0);/* 等待上一次发送完成 */
    UART1->UTXD = c & 0XFF; /* 发送数据 */
}

//通过串口接受数据
unsigned char get_char(void)
{
    
    
    while(((UART1->USR2) &0X01) == 0);/* 等待上一次发送完成 */
    return UART1->URXD; /* 接受数据 */
}

//通过串口发送一串字符
void put_string(char *str)
{
    
    
    char *p = str;
    while(*p)
    {
    
    
        put_char(*p++);
    }
}

  Writing verification of main function

while(1)			
	{
    
    	
		// state = !state;
		// led_switch(LED0,state);
		// delay_ms(1000);
		
		
		put_string("wang zong gong niu bi !");
		
		put_string("\r\n\r\n");
		delay_ms(1000);
		a = get_char();
		put_char(a);	//回显功能
		put_string("\r\n\r\n");
}

  Download the program to verify, the result is correct!
Insert picture description here

Guess you like

Origin blog.csdn.net/XZDMEN/article/details/109047712