Serial communication based on interrupt/DMA mode

1. Introduction to serial communication protocol and RS-232 and the working principle of USB/TTL to 232 module

1. Serial protocol and RS-232 standard:

(1) Serial port protocol:
Serial Communication is a very commonly used serial communication method between devices. Because it is simple and convenient, most electronic devices support this communication method. Electronic engineers often use this communication method to output debugging when debugging equipment. information.
In computer science, most complex problems can be simplified through layers. For example, the chip is divided into the core layer and on-chip peripherals; the STM32 standard library is the software layer between the registers and user code. We also understand the communication protocol in a layered manner. The most basic thing is to divide it into the physical layer and the protocol layer.
(2) RS-232 standard:
RS-232 (Recommended Standard 232) is a commonly used serial communication standard used for data transmission between computers and external devices. It defines specifications for electrical characteristics, signal transmission methods, and interface connections.

Electrical characteristics
The RS-232 standard specifies the electrical characteristics of communication lines, including signal levels, baud rates, and clock frequencies. According to the RS-232 standard, signal levels are divided into positive and negative types, which represent logic 1 and logic 0 respectively. Generally, a positive level represents a logic 0 and a negative level represents a logic 1. Baud rate refers to the rate of data transmission, usually expressed in bits transmitted per second (bps). Clock frequency refers to the frequency of the clock signal used in transmission.

Signal transmission method
The RS-232 standard uses asynchronous transmission, that is, there is no fixed time interval between each data byte. Data transmission is based on data frames, and each data frame includes start bits, data bits, check bits and stop bits. The start bit is used to identify the beginning of the data frame, and the stop bit is used to identify the end of the data frame. Data bits are used to transmit actual data, and parity bits are used to detect errors in data transmission.

Serial protocol and RS-232 standard:

Serial protocol:

Serial communication is a type of communication through serial data transmission. It uses a pair of pins (transmit line TX and receive line RX) for one-way or two-way data transmission.
Common serial port protocols include UART (Universal Asynchronous Receiver and Transmit) and USART (Universal Synchronous Asynchronous Receiver and Transmit), which define the format, rate, start bit, stop bit and other parameters of data transmission.
RS-232 standard:

RS-232 (Recommended Standard 232) is a standard widely used for serial communication. It defines the electrical characteristics, signal polarity, mechanical interface, etc. for serial communication between connected devices.
The RS-232 standard specifies common serial port parameters, such as baud rate, data bits, stop bits, and parity.
The difference between RS232 level and TTL level:

RS232 levels usually use negative logic, that is, high level represents logic 0 and low level represents logic 1. The TTL level is usually positive logic, with high level representing logic 1 and low level representing logic 0.
The voltage range of RS232 levels is larger, generally -15V to +15V, while the voltage range of TTL levels is usually 0V to 5V.
The connectors used by the RS232 standard are usually DB-9 or DB-25, while TTL usually uses 0.1-inch pitch pins.
The working principle of the “USB/TTL to 232” module (taking the CH340 chip module as an example):

CH340 chip:

CH340 is a USB to serial port chip, often used to make USB to TTL (UART) modules, such as USB/TTL to 232 modules.
It provides a bridging function between USB and serial ports, allowing the computer to communicate with serial devices through the USB interface.
working principle:

When connecting the USB/TTL to 232 module to the USB port of the computer, the CH340 chip will be activated.
CH340 communicates with the computer through the USB interface, and the driver recognizes it as a virtual serial port device.
The user sends data to the virtual serial port through the computer’s serial port terminal or software, and the CH340 chip converts the data into a serial port signal and outputs it to the TX pin of the module through TTL level.
In the receiving direction, the CH340 chip receives the TTL level serial port signal, converts it into a USB-recognizable signal, and sends it to the computer through the USB interface.
The advantage of this module is that it facilitates connecting serial devices to the computer without requiring the computer to have a physical serial port. It is often used for debugging and communicating with embedded systems, microcontrollers and other devices. Users can simulate a serial port on the computer through the USB/TTL to 232 module, making communication with serial devices easier.

Introduction to DMA

DMA, or Direct Memory Access, is a data transmission technology in computer systems that aims to improve the efficiency and performance of data transmission. DMA allows peripherals (such as hard drives, network adapters, graphics cards, etc.) to directly access system memory without the intervention of the central processing unit (CPU), thereby reducing the burden on the CPU.

2. Serial communication based on HAL library interrupt mode

Create project
Configure GPIO: PA0. If you are only completing serial communication, this step can be skipped. However, according to the experimental requirements, in order to distinguish the opening and closing of serial communication, an LED light must be used to display it.
Configure USART1
Generate keil code, the code is as follows

#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
 

void SystemClock_Config(void);
uint8_t flag=1;
uint8_t rx_buf[6];//array for receiving serial port data storage
int strEqual(char rcData[6],char rcData2[6])
{<!-- -->
for(uint8_t i = 0 ; i < 6 ; i + + ){<!-- -->
if (rcData[i] != rcData2[i]) return 0;
}
return 1;
}
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{<!-- -->
//When the input command is "stop!", send a prompt and change flag=0
if(strEqual(rx_buf,"stop!"))
{<!-- -->
flag=0;
}
\t
//When the input command is "start", send a prompt and change flag=1
else if(strEqual(rx_buf,"start"))
{<!-- -->
flag=1;
}
HAL_UART_Receive_DMA( & amp;huart1,(uint8_t*)rx_buf,5);
}
int main(void)
{<!-- -->
  HAL_Init();
  uint8_t message[] = "hello windows!\\
"; //Define the data sending array
  SystemClock_Config();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  HAL_UART_Receive_DMA( & amp;huart1,(uint8_t*)rx_buf,5);//Set the data received by DMA to be stored in rx_buf
  while (1)
  {<!-- -->
      if(flag==1)
{<!-- -->
HAL_UART_Transmit_DMA( & amp;huart1, (uint8_t *)message, sizeof(message));
HAL_Delay(600);
}
  }
}

void SystemClock_Config(void)
{<!-- -->
  RCC_OscInitTypeDef RCC_OscInitStruct = {<!-- -->0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {<!-- -->0};
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig( & amp;RCC_OscInitStruct) != HAL_OK)
  {<!-- -->
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  if (HAL_RCC_ClockConfig( & amp;RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {<!-- -->
    Error_Handler();
  }
}

void Error_Handler(void)
{<!-- -->
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {<!-- -->
  }
  /* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
  * @brief Reports the name of the source file and the source line number
  * where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{<!-- -->
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\\
", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */


Experimental results

Enter keil simulation
Debug page settings
Simulation results

Experimental summary

Serial communication based on interrupt (Interrupt) and DMA (Direct Memory Access) is a common data transmission method in computer systems. The application of these two methods in serial communication has different characteristics and advantages. In interrupt-based serial communication, in the interrupt mode, when the serial port receives data, an interrupt signal will be triggered. The interrupt controller will interrupt the normal execution of the CPU. It will jump to a specific interrupt handler, and the real-time processing of the received data provides better flexibility. The DMA-based serial communication DMA method can significantly reduce the CPU overhead, allowing the CPU to focus on other tasks. Improve the overall performance of the system and reduce latency. The configuration of DMA can be relatively complex. You need to ensure the correct configuration of the DMA controller and avoid problems such as DMA conflicts. Choosing the appropriate method depends on the specific needs of the application and system performance requirements.