跳转至

rocketpi_aht30_hardware_i2c_base

rocketpi_aht30_hardware_i2c_base

效果展示

image-20251116004609250

功能说明

面向 RocketPI STM32F401RE 开发板的 AHT30温湿度 演示工程。主要特性:

  • 驱动AHT30在串口上打印温湿度。
  • 提供 driver_aht30 基础驱动。
  • driver_aht30_test 直接调用测试,自主选择轮询时间。

硬件连接

  • AHT32 SCL :PB8
  • AHT32 SDA:PB9
  • 电源:接3.3v或5v

CubeMX配置

硬件i2c配置

  • (无特别说明不配置dma与中断)

image-20251116002705838

image-20251116002929989

usart配置

image-20251116004045780

image-20251116003215745

驱动以及测试代码

Core/Src/main.c
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "driver_aht30_test.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

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

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  printf("Initializing AHT30...\r\n");
  if (aht30_init() != HAL_OK)
  {
    printf("AHT30 init failed\r\n");
  }
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    (void)aht30_test_log_measurement();
    HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 84;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&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_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
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\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
bsp/aht30/driver_aht30.c
#include "driver_aht30.h"

#include "i2c.h"

/**
 * @file driver_aht30.c
 * @brief AHT30 temperature/humidity sensor driver implementation over hardware I2C.
 * @author rocket
 * @copyright Copyright (c) 2025 rocket. Authorized use only.
 */

#define AHT30_CMD_TRIGGER     0xACU /**< Measurement trigger command ID. */
#define AHT30_CMD_CONFIG_0    0x33U /**< Default trigger configuration byte 0. */
#define AHT30_CMD_CONFIG_1    0x00U /**< Default trigger configuration byte 1. */
#define AHT30_CMD_RESET       0xBAU /**< Soft-reset command ID. */
#define AHT30_STATUS_BUSY     0x80U /**< Status bit indicating an ongoing measurement. */

/**
 * @brief Issue a soft reset over I2C.
 * @return HAL status returned by HAL_I2C_Master_Transmit.
 */
static HAL_StatusTypeDef aht30_soft_reset(void)
{
    uint8_t cmd = AHT30_CMD_RESET;
    return HAL_I2C_Master_Transmit(&hi2c1, AHT30_I2C_ADDRESS, &cmd, 1U, HAL_MAX_DELAY);
}

/**
 * @brief Convert raw 20-bit humidity and temperature samples to engineering units.
 * @param raw Input buffer read from the sensor.
 * @param temperature_c Output temperature in Celsius.
 * @param humidity_pct Output relative humidity percentage.
 */
static void aht30_convert_samples(const uint8_t raw[6], float *temperature_c, float *humidity_pct)
{
    uint32_t raw_humidity = ((uint32_t)raw[1] << 12)
                          | ((uint32_t)raw[2] << 4)
                          | (uint32_t)(raw[3] >> 4);

    uint32_t raw_temperature = (((uint32_t)raw[3] & 0x0FU) << 16)
                             | ((uint32_t)raw[4] << 8)
                             | (uint32_t)raw[5];

    *humidity_pct = (raw_humidity * 100.0f) / 1048576.0f;
    *temperature_c = (raw_temperature * 200.0f) / 1048576.0f - 50.0f;
}

/**
 * @brief Perform the basic initialization sequence (power-on delay + soft reset).
 */
HAL_StatusTypeDef aht30_init(void)
{
    HAL_Delay(AHT30_POWER_ON_DELAY);
    HAL_StatusTypeDef status = aht30_soft_reset();
    HAL_Delay(AHT30_POST_RESET_DELAY);
    return status;
}

/**
 * @brief Trigger one measurement and retrieve the raw 6-byte payload.
 */
HAL_StatusTypeDef aht30_read_raw(uint8_t raw[6])
{
    uint8_t cmd[3] = {AHT30_CMD_TRIGGER, AHT30_CMD_CONFIG_0, AHT30_CMD_CONFIG_1};
    HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(&hi2c1, AHT30_I2C_ADDRESS, cmd, sizeof(cmd), HAL_MAX_DELAY);
    if (status != HAL_OK) {
        return status;
    }

    HAL_Delay(AHT30_MEASUREMENT_DELAY);
    status = HAL_I2C_Master_Receive(&hi2c1, AHT30_I2C_ADDRESS, raw, 6U, HAL_MAX_DELAY);
    if (status != HAL_OK) {
        return status;
    }

    if ((raw[0] & AHT30_STATUS_BUSY) != 0U) {
        return HAL_BUSY;
    }

    return HAL_OK;
}

/**
 * @brief Read and convert a temperature/humidity sample.
 */
HAL_StatusTypeDef aht30_read(float *temperature_c, float *humidity_pct)
{
    if ((temperature_c == NULL) || (humidity_pct == NULL)) {
        return HAL_ERROR;
    }

    uint8_t raw[6] = {0};
    HAL_StatusTypeDef status = aht30_read_raw(raw);
    if (status != HAL_OK) {
        return status;
    }

    aht30_convert_samples(raw, temperature_c, humidity_pct);
    return HAL_OK;
}
bsp/aht30/driver_aht30.h
#pragma once

#include <stdint.h>

#include "stm32f4xx_hal.h"

/**
 * @file driver_aht30.h
 * @brief Hardware I2C driver for the AHT30 temperature and humidity sensor.
 * @author rocket
 * @copyright Copyright (c) 2025 rocket. Authorized use only.
 */

#define AHT30_I2C_ADDRESS        (0x38U << 1) /**< 7-bit address shifted for HAL usage. */
#define AHT30_MEASUREMENT_DELAY  80U          /**< Typical measurement conversion delay (ms). */
#define AHT30_POWER_ON_DELAY     20U          /**< Minimum wait after power-up before commands (ms). */
#define AHT30_POST_RESET_DELAY   20U          /**< Wait time after issuing a soft reset (ms). */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Initialize the sensor by issuing a soft reset and respecting timing constraints.
 * @return HAL_OK on success, otherwise a HAL error/busy code from the I2C transaction.
 */
HAL_StatusTypeDef aht30_init(void);

/**
 * @brief Trigger a measurement and read the raw 6-byte payload.
 * @param raw Pointer to a 6-byte buffer to receive the sensor output.
 * @return HAL_OK on success, HAL_BUSY if the sensor reports busy state, otherwise HAL error codes.
 */
HAL_StatusTypeDef aht30_read_raw(uint8_t raw[6]);

/**
 * @brief Read and convert the current temperature and humidity.
 * @param temperature_c Output pointer for the temperature in degrees Celsius.
 * @param humidity_pct Output pointer for the relative humidity in percent.
 * @return HAL_OK on success, HAL_BUSY while the sensor is measuring, or HAL error codes on failure.
 */
HAL_StatusTypeDef aht30_read(float *temperature_c, float *humidity_pct);

#ifdef __cplusplus
}
#endif
bsp/aht30/driver_aht30_test.c
#include "driver_aht30_test.h"

#include <stdio.h>
#include <stdlib.h>

/**
 * @file driver_aht30_test.c
 * @brief Logging helpers and measurement demos for the AHT30 driver.
 * @author rocket
 * @copyright Copyright (c) 2025 rocket. Authorized use only.
 */

/**
 * @brief Print a formatted HAL status code with a short prefix.
 * @param phase Text describing the failed operation.
 * @param status HAL status returned by the driver.
 */
static void aht30_test_print_status(const char *phase, HAL_StatusTypeDef status)
{
    printf("AHT30 %s error (status=%d)\r\n", phase, (int)status);
}

/**
 * @brief Take one measurement, convert it, and print the human-readable results.
 * @return HAL_OK on success, HAL_BUSY if sensor is measuring, or HAL error codes on failure.
 */
HAL_StatusTypeDef aht30_test_log_measurement(void)
{
    float temperature = 0.0f;
    float humidity = 0.0f;
    HAL_StatusTypeDef status = aht30_read(&temperature, &humidity);
    if (status == HAL_OK) {
        int16_t temp10 = (int16_t)(temperature * 10.0f);
        uint16_t hum10 = (uint16_t)(humidity * 10.0f);
        printf("AHT30 -> T=%d.%01dC  RH=%d.%01d%%\r\n",
               temp10 / 10, abs(temp10 % 10),
               hum10 / 10, hum10 % 10);
    } else if (status == HAL_BUSY) {
        printf("AHT30 measurement busy\r\n");
    } else {
        aht30_test_print_status("read", status);
    }

    return status;
}

/**
 * @brief Request the raw 6-byte payload and dump it for debugging.
 * @return HAL_OK when data is captured, HAL_BUSY if the device is still measuring, otherwise HAL error codes.
 */
HAL_StatusTypeDef aht30_test_log_raw(void)
{
    uint8_t raw[6] = {0};
    HAL_StatusTypeDef status = aht30_read_raw(raw);
    if (status == HAL_OK) {
        printf("AHT30 raw -> %02X %02X %02X %02X %02X %02X (cal=%s)\r\n",
               raw[0], raw[1], raw[2], raw[3], raw[4], raw[5],
               ((raw[0] & 0x08U) != 0U) ? "yes" : "no");
    } else if (status == HAL_BUSY) {
        printf("AHT30 raw request busy\r\n");
    } else {
        aht30_test_print_status("raw", status);
    }

    return status;
}
bsp/aht30/driver_aht30_test.h
#pragma once

#include "driver_aht30.h"

/**
 * @file driver_aht30_test.h
 * @brief Convenience helpers for exercising and logging the AHT30 driver.
 * @author rocket
 * @copyright Copyright (c) 2025 rocket. Authorized use only.
 */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Take a measurement and print the converted values via printf.
 * @return HAL_OK when the sample is valid, HAL_BUSY if the sensor is still measuring, otherwise HAL error codes.
 */
HAL_StatusTypeDef aht30_test_log_measurement(void);

/**
 * @brief Read the raw frame and dump the status/byte stream for debugging.
 * @return HAL_OK when the raw bytes are retrieved, HAL_BUSY if the sensor is still measuring, otherwise HAL error codes.
 */
HAL_StatusTypeDef aht30_test_log_raw(void);

#ifdef __cplusplus
}
#endif