edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
Robert Fletcher
Published

STM32H743IIK6 Sensor Gateway with Ethernet Data Logging

The project demonstrates how the STM32H743IIK6 platform can combine real-time data acquisition, local storage and network communication...

IntermediateWork in progress34
STM32H743IIK6 Sensor Gateway with Ethernet Data Logging

Things used in this project

Hardware components

STM32H743IIK6
Main Cortex-M7 microcontroller
×1
3.3 V LDO / DC-DC regulator
MCU and peripheral power
×1
LAN8742A
10/100-Mbit Ethernet interface
×1
RJ45 Ethernet connector with magnetics
Network connection
×1
microSD card socket
Local data storage
×1
microSD card
Measurement log storage
×1
3-axis digital sensor
Example vibration/motion input
×1
Analog sensor / 0–3.3 V signal source
ADC measurement
×1
External temperature sensor
Environmental measurement
×1
Status LEDs
Power, logging and Ethernet status
×3
Push button
Start/stop logging
×1
8–25 MHz crystal/oscillator
System clock
×1
USB connector
Debug/programming or USB interface
×1
Decoupling capacitors
Local power filtering
×10
10 Β΅F bulk capacitors
Supply stabilization
×10
SWD connector
Programming and debugging
×1

Software apps and online services

STM32CubeIDE
firmware development, compilation and debugging
STM32CubeMX
MCU pin, clock and peripheral configuration
STM32CubeH7 HAL/LL drivers
peripheral software
FreeRTOS
task scheduling and separation of acquisition, logging and networking
LwIP
Ethernet/TCP/IP networking
FatFs
microSD file-system support
GitHub
source-code version control
Wireshark
Ethernet packet inspection and debugging
Serial terminal
UART diagnostic output

Hand tools and fabrication machines

Hot-air rework station
Soldering iron (generic)
Soldering iron (generic)
Digital multimeter
Oscilloscope
Logic analyzer
SWD programmer
Bench power supply
USB-to-UART adapter
Ethernet cable
PCB assembly equipment
PCB stencil and solder paste
Reflow oven

Story

Read more

Schematics

MCU core

Ethernet interface

microSD interface

Analog acquisition

Digital sensor

Code

Code

C/C++
/*
 * STM32H743IIK6 Industrial Sensor Gateway
 *
 * Function:
 * - Acquire an analog sensor signal using ADC1
 * - Transfer ADC samples using DMA
 * - Convert ADC readings to voltage
 * - Calculate the average sensor voltage
 * - Send measurement results through UART
 *
 * Target:
 * STM32H743IIK6
 *
 * Development environment:
 * STM32CubeIDE / STM32CubeH7 HAL
 */

#include "main.h"
#include <stdio.h>
#include <string.h>

/* -------------------------------------------------------------------------- */
/* Configuration                                                              */
/* -------------------------------------------------------------------------- */

#define ADC_BUFFER_SIZE     256U
#define VREF_VOLTAGE        3.3f
#define ADC_MAX_VALUE       65535.0f

/* -------------------------------------------------------------------------- */
/* External peripheral handles generated by STM32CubeMX                      */
/* -------------------------------------------------------------------------- */

extern ADC_HandleTypeDef hadc1;
extern UART_HandleTypeDef huart3;

/* -------------------------------------------------------------------------- */
/* Global variables                                                           */
/* -------------------------------------------------------------------------- */

static uint16_t adc_buffer[ADC_BUFFER_SIZE];

volatile uint8_t adc_conversion_complete = 0;
volatile uint8_t adc_conversion_error = 0;

/* -------------------------------------------------------------------------- */
/* Function prototypes                                                        */
/* -------------------------------------------------------------------------- */

static float ADC_ToVoltage(uint16_t adc_value);
static float CalculateAverageVoltage(void);
static void PrintMeasurement(void);
static void Error_Handler_Local(void);

/* -------------------------------------------------------------------------- */
/* ADC conversion complete callback                                           */
/* -------------------------------------------------------------------------- */

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
    if (hadc->Instance == ADC1)
    {
        adc_conversion_complete = 1;
    }
}

/* -------------------------------------------------------------------------- */
/* ADC error callback                                                         */
/* -------------------------------------------------------------------------- */

void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
{
    if (hadc->Instance == ADC1)
    {
        adc_conversion_error = 1;
    }
}

/* -------------------------------------------------------------------------- */
/* Convert 16-bit ADC value to voltage                                        */
/* -------------------------------------------------------------------------- */

static float ADC_ToVoltage(uint16_t adc_value)
{
    return ((float)adc_value * VREF_VOLTAGE) / ADC_MAX_VALUE;
}

/* -------------------------------------------------------------------------- */
/* Calculate average voltage                                                  */
/* -------------------------------------------------------------------------- */

static float CalculateAverageVoltage(void)
{
    uint32_t sum = 0;
    uint32_t i;

    for (i = 0; i < ADC_BUFFER_SIZE; i++)
    {
        sum += adc_buffer[i];
    }

    return ADC_ToVoltage(
        (uint16_t)(sum / ADC_BUFFER_SIZE)
    );
}

/* -------------------------------------------------------------------------- */
/* Send measurement information over UART                                     */
/* -------------------------------------------------------------------------- */

static void PrintMeasurement(void)
{
    char message[128];

    float average_voltage = CalculateAverageVoltage();

    int length = snprintf(
        message,
        sizeof(message),
        "STM32H743 Sensor Gateway\r\n"
        "Samples: %u\r\n"
        "Average ADC Voltage: %.3f V\r\n"
        "Timestamp: %lu ms\r\n"
        "-----------------------------\r\n",
        ADC_BUFFER_SIZE,
        average_voltage,
        HAL_GetTick()
    );

    HAL_UART_Transmit(
        &huart3,
        (uint8_t *)message,
        (uint16_t)length,
        HAL_MAX_DELAY
    );
}

/* -------------------------------------------------------------------------- */
/* Local error handler                                                        */
/* -------------------------------------------------------------------------- */

static void Error_Handler_Local(void)
{
    char message[] =
        "ERROR: ADC DMA initialization failed\r\n";

    HAL_UART_Transmit(
        &huart3,
        (uint8_t *)message,
        sizeof(message) - 1,
        HAL_MAX_DELAY
    );

    while (1)
    {
        HAL_Delay(500);
    }
}

/* -------------------------------------------------------------------------- */
/* Main application                                                           */
/* -------------------------------------------------------------------------- */

int main(void)
{
    /* HAL initialization */
    HAL_Init();

    /*
     * SystemClock_Config(), MX_GPIO_Init(),
     * MX_ADC1_Init(), and MX_USART3_UART_Init()
     * are generated by STM32CubeMX.
     */
    SystemClock_Config();

    MX_GPIO_Init();
    MX_ADC1_Init();
    MX_USART3_UART_Init();

    /* Startup message */
    {
        char startup_message[] =
            "\r\n"
            "====================================\r\n"
            " STM32H743IIK6 Sensor Gateway\r\n"
            " ADC + DMA Data Acquisition\r\n"
            "====================================\r\n";

        HAL_UART_Transmit(
            &huart3,
            (uint8_t *)startup_message,
            sizeof(startup_message) - 1,
            HAL_MAX_DELAY
        );
    }

    /*
     * Start ADC conversion using DMA.
     *
     * ADC samples are transferred automatically into
     * adc_buffer[] without CPU intervention for each sample.
     */
    if (HAL_ADC_Start_DMA(
            &hadc1,
            (uint32_t *)adc_buffer,
            ADC_BUFFER_SIZE) != HAL_OK)
    {
        Error_Handler_Local();
    }

    /* Main application loop */
    while (1)
    {
        /*
         * ADC + DMA has completed one buffer.
         */
        if (adc_conversion_complete)
        {
            adc_conversion_complete = 0;

            /*
             * Process the acquired sensor data.
             */
            PrintMeasurement();

            /*
             * In the complete project this section can be replaced
             * with a FreeRTOS queue notification to the logging task.
             */
        }

        /*
         * ADC/DMA error handling.
         */
        if (adc_conversion_error)
        {
            adc_conversion_error = 0;

            HAL_ADC_Stop_DMA(&hadc1);

            Error_Handler_Local();
        }

        /*
         * Small delay prevents excessive UART traffic.
         * ADC acquisition itself is controlled by the ADC/DMA.
         */
        HAL_Delay(100);
    }
}

Credits

Robert Fletcher
20 projects β€’ 1 follower
Senior semiconductor engineer at AVAQ Semiconductor. With extensive experience in electronic components, supply chain solutions & sourcing.

Comments