Embedded Systems Week 8 Experiment: Serial Communication Based on Interrupt/DMA Mode

1 Experimental task:

(1) Understand the serial port protocol and RS-232 standard

(2) And the difference between RS232 level and TTL level;

(3) Understand the working principle of the “USB/TTL to 232” module (taking the CH340 chip module as an example). Use the HAL library (or standard library) method to set the USART1 baud rate to 115200, 1 stop bit, and no parity bit. Use interrupt mode and DMA mode to complete the following tasks:

1. The STM32 system continuously sends “hello windows!” to the host computer (win10);

2. When the host computer sends the character “stop” to stm32, stm32 pauses sending “hello windows!”; after sending a character “start”, stm32 continues to send;

(1) Serial protocol and RS-232 standard

1. What is serial communication protocol?

Serial communication refers to two or more devices using serial ports to send and receive bytes bit by bit. You can use one wire to send data while using another wire to receive data. The serial communication protocol is the protocol commonly followed during serial communication. The content of the protocol is the meaning represented by each bit. Commonly used serial communication protocols include the following
1 RS-232 (ANSI/EIA-232 standard) only supports point-to-point, maximum distance 50 feet. The maximum speed is 128000 bit/s. The farther the distance, the slower the speed. Supports full duplex (can send and receive at the same time).
2 RS-422 (EIA RS-422-AStandard), supports point-to-multiple connection of up to 10 receivers on a balanced bus to increase the transmission rate to 10Mbps and extend the transmission distance to 4000 feet (about 1219 meters), so at 100kbps rate Within, the transmission distance is the largest. Supports full duplex (can send and receive at the same time).
3 RS-485 (EIA-485 standard) is an improvement of RS-422. It supports many-to-many (2-wire connection), increased from 10 to 32, and can use more than 4000 feet of wire for serial communication. The maximum rate is 10Mbps. Supports full duplex (can send and receive at the same time). When connected with 2 wires, it is half-duplex.

2.RS-232
The RS-232 standard interface (also known as EIA RS-232) is one of the commonly used serial communication interface standards. It was established in 1970 by the Electronic Industries Association (EIA) in conjunction with Bell Systems, modem manufacturers and computer terminal manufacturers. Jointly formulated in 2008, its full name is “Technical Standard for Serial Binary Data Exchange Interface between Data Terminal Equipment (DTE) and Data Communications Equipment (DCE)”.

(2) The difference between RS232 level and TTL level

(1) TTL level standard

Output L: <0.8V; H: >2.4V.

Input L: <1.2V; H: >2.0V

The output low level of TTL devices should be less than 0.8V, and the high level should be greater than 2.4V. Input, if it is lower than 1.2V, it is considered to be 0, and if it is higher than 2.0, it is considered to be 1. Therefore, the low-level noise margin of the TTL level input is only (0.8-0)/2=0.4V, and the high-level noise margin is (5-2.4)/2=1.3V.

(2), RS232 standard

On the TXD and RXD data lines:
(1) Logic 1 is a voltage of -3~-15V
(2) Logic 0 is a voltage of 3~15V
On control lines such as RTS, CTS, DSR, DTR and DCD:
(1) The signal is valid (ON state) with a voltage of 3~15V
(2) The signal is invalid (OFF state) with a voltage of -3~-15V
This is specified by the communication protocol RS-232.
RS-232: Standard serial port, the most commonly used serial communication interface. There are three types (A, B and C), which use different voltages to indicate on and off. The most widely used one is RS-232C, which defines the mark(on) bit voltage to be between -3V and -12V, and the space(off) bit voltage to be between +3V and +12V. The maximum transmission distance is about 15 meters, and the maximum speed is 20kb/s. RS-232 is designed for point-to-point (that is, only one pair of receiving and transmitting devices) communication, and its driver load is 3~7kΩ. So RS-232 is suitable for communication between local devices.

(3) Understand the working principle of the “USB/TTL to 232” module (taking the CH340 chip module as an example)

CH340 is a USB bus adapter chip that realizes USB to serial port, USB to IrDA infrared or USB to printing port. In order to increase the long-distance transmission and anti-interference capabilities of serial communication, the RS-232 standard uses -15V to represent logic 1 and + 15V to represent logic 0. The MH340 chip is often used to convert USB/TTL and RS-232 level signals.

CH340 working principle diagram

Use the HAL library (or standard library) method to set the USART1 baud rate to 115200, 1 stop bit, and no parity bit. Use interrupt mode and DMA mode to complete the following tasks:

1. The STM32 system continuously sends “hello windows!” to the host computer (win10);

2. When the host computer sends the character “stop” to stm32, stm32 pauses sending “hello windows!”; after sending a character “start”, stm32 continues to send;

#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 effect

Without an oscilloscope, you can use Keil’s software simulation logic analyzer function to observe the serial port output waveform, analyze whether the timing status is correct, and calculate the actual baud rate.

Use Keil’s software to simulate the logic analyzer function to observe the serial port output waveform

Reference article:
Link: LED running water lamp based on standard peripheral library_Alone–Ruan Zeyu’s blog-CSDN blog