Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing UART Communication Between Jetson Nano and STM32

Tech 2

Required Components

Jetson Nano, STM32F103C8T6, UART interface.

Hardware Wiring

Refer to the Jetson Nano pinout diagram for general pin configuration. For UART communication, use pins 8 (TX) and 10 (RX) on the Jetson Nano. Ensure a common ground connection between the Jetson Nano and STM32 to enable data transmission.

For the STM32, this example uses USART1 with pins A9 (TX) and A10 (RX). Adjust based on your specific setup if using a different USART.

Connect as follows:

  • Jetson Nano pin 8 (TX) to STM32 pin A10 (RX)
  • Jetson Nano pin 10 (RX) to STM32 pin A9 (TX)
  • Connect GND pins of both boards together.

Key Concepts: Data Formatting

When developing with OpenCV on Jetson Nano, sending structured data packets is recommended. For example:

def transmit_value(value):
    packet = struct.pack("<BBhB", 0x2C, 0x12, int(value), 0x5B)
    serial_port.write(packet)

The format string "<BBhB" defines the data types and byte order in the packet, similar to variable declarations in C. This is crucial for ensuring data integrity, especially with 32-bit microcontrollers like STM32, where data may exceed 8 bits (e.g., values over 255). Choose appropriate format specifiers based on your data requirements. Common specifiers include:

  • 'B': unsigned char (1 byte)
  • 'h': short (2 bytes)
  • 'i': int (4 bytes)
  • 'f': float (4 bytes)
  • 'd': double (8 bytes)

Code Implementation

Jetson Nano Side

# coding=utf-8
import cv2
import struct
import serial

serial_port = serial.Serial("/dev/ttyTHS1", 115200)

def transmit_value(value):
    packet = struct.pack("<BBhB", 0x2C, 0x12, int(value), 0x5B)
    serial_port.write(packet)

while True:
    data_value = 1
    transmit_value(data_value)
    key = cv2.waitKey(30) & 0xFF
    if key == 27:  # Exit on ESC key
        break

cv2.destroyAllWindows()

Notes:

  • The # coding=utf-8 line is optional unless using Chinese comments in auto-start scripts.
  • Import struct and serial libraries for data packing and serial communication.
  • Configure the serial port with serial.Serial("/dev/ttyTHS1", 115200), ensuring the baud rate matches the STM32 setup.
  • Grant serial port permissions by running sudo chmod 777 /dev/ttyTHS1 in the terminal.

STM32 Side

uint16_t x_coordinate, y_coordinate;
uint8_t reception_flag = 0;

void USART1_IRQHandler(void) {
    uint8_t received_byte;
    uint8_t index;
    static uint8_t buffer_index = 0;
    static uint16_t receive_buffer[20] = {0};
    static uint8_t reception_state = 0;

    reception_flag = 0;
    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        received_byte = USART_ReceiveData(USART1);

        if (reception_state == 0 && received_byte == 0x2C) {
            reception_state = 1;
            receive_buffer[buffer_index++] = received_byte;
        } else if (reception_state == 1 && received_byte == 0x12) {
            reception_state = 2;
            receive_buffer[buffer_index++] = received_byte;
        } else if (reception_state == 2) {
            receive_buffer[buffer_index++] = received_byte;

            if (buffer_index >= 20 || received_byte == 0x5B) {
                reception_state = 3;
                reception_flag = 1;
                x_coordinate = (receive_buffer[buffer_index - 4] << 8) + (receive_buffer[buffer_index - 5]);
                y_coordinate = (receive_buffer[buffer_index - 2] << 8) + (receive_buffer[buffer_index - 3]);
                OLED_ShowNum(2, 1, x_coordinate, 3);
                OLED_ShowNum(3, 1, y_coordinate, 3);
            }
        } else if (reception_state == 3) {
            if (receive_buffer[buffer_index - 1] == 0x5B) {
                USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
                reception_flag = 0;
                buffer_index = 0;
                reception_state = 0;
                USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
            } else {
                reception_state = 0;
                buffer_index = 0;
                for (index = 0; index < 10; index++) {
                    receive_buffer[index] = 0x00;
                }
            }
        } else {
            reception_state = 0;
            buffer_index = 0;
            for (index = 0; index < 20; index++) {
                receive_buffer[index] = 0x00;
            }
        }
    }
}

Notes:

  • Configure USART1 appropriately (not detailed here as its assumed knowledge).
  • Variables like x_coordinate and y_coordinate are defined globally for access in other functions; use extern if needed in main().
  • Key processing lines combine low and high bytes: x_coordinate = (receive_buffer[buffer_index - 4] << 8) + (receive_buffer[buffer_index - 5]);.
  • Understand buffer indexing: buffer_index increments after storing data, affecting offsets like buffer_index - 4 for byte reconstruction.

Conclusion

UART communication between Jetson Nano and STM32 follows standard practices. Experimentation and reference to documentation will help in adapting this setup to specific projects.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.