Created Wednesday 22 November 2023
Configuring MCU
- Set MCU debug / clock options
- Add some GPIO output for LED output (we will togle it when MCU receives 0x01)
- Enable UART ports (set port speed to 921600 bps if you want to use loopback.exe from the video)
- Enable DMA TX and RX channels for each UART port
- Set RX channel mode to circular for each UART port
- Enable UART global interrupt for each UART port
- Generate code (ALT+K)
Adding library
- Download uart library from here
- Copy uart.c and uart.h to core/src folder of your project (the same directory as main.c)
- Open uart.h, uncomment #define STM32H7
- Add includes to /* USER CODE BEGIN Includes */:
#include "uart.h" #include "math.h" // for round
- Add code to /* USER CODE BEGIN PV */:
#define UART_BUFFER_SIZE 128 #define TEMP_BUFFER_SIZE 100 #define PORT_NUM 5 __IO uint8_t uartBuffer[PORT_NUM][UART_BUFFER_SIZE]; __IO uint8_t tempBuffer[PORT_NUM][TEMP_BUFFER_SIZE];
- Add code right before /* USER CODE END 2 */:
COM_Init(0, &huart1, (uint8_t *) &uartBuffer[0], UART_BUFFER_SIZE, 100); COM_Init(1, &huart2, (uint8_t *) &uartBuffer[1], UART_BUFFER_SIZE, 100); COM_Init(2, &huart3, (uint8_t *) &uartBuffer[2], UART_BUFFER_SIZE, 100); COM_Init(3, &huart4, (uint8_t *) &uartBuffer[3], UART_BUFFER_SIZE, 100); COM_Init(4, &huart5, (uint8_t *) &uartBuffer[4], UART_BUFFER_SIZE, 100); float floatTemp[PORT_NUM]={-60.3, 18.0, 121.5, 30, 40 }; // -60.3, 18, 121.5, 30, 40 Deg C
- Add code to while(1) loop:
uint8_t header; uint8_t length; for (int portNum=0;portNum<PORT_NUM;portNum++) { COM_Select(portNum); if (COM_ReadFast((uint8_t *)&header,1)) switch(header) { case 0x01: { uint8_t answer=0x55; COM_Write((uint8_t *)&answer,1); break; // command 0x01 } case 0x02: { int32_t intTemp=round(floatTemp[portNum]*1000); COM_Write((uint8_t *)&intTemp,4); break; // command 0x02 } case 0x03: { COM_Read((uint8_t *)&length,1); COM_Read((uint8_t *)&tempBuffer[portNum],length); // should check length here COM_Write((uint8_t *)&tempBuffer[portNum],length); break; // command 0x03 } } }
- Everything is ready! You can build and flash it to STM32H7 MCU, run loopback.exe and see if it connects.