【先楫HPM6750EVK2测评】硬件I2C驱动温湿度传感器SHT40
[复制链接]
盛思锐官方提供了SHT40的驱动参考:
将驱动文件添加到i2c master poll例程里面:
首先修改i2c驱动管脚:
驱动实现如下:
主要是利用SDK提供的i2c_init_master、i2c_master_write和i2c_master_read函数实现i2c初始化、数据读写:
#include "sensirion_i2c_hal.h"
#include "sensirion_common.h"
#include "sensirion_config.h"
#include <stdio.h>
#include "board.h"
#include "hpm_clock_drv.h"
#include "hpm_i2c_drv.h"
#define TEST_I2C BOARD_APP_I2C_BASE
#define TEST_I2C_CLOCK_NAME BOARD_APP_I2C_CLK_NAME
int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx)
{
/* TODO:IMPLEMENT or leave empty if all sensors are located on one single
* bus
*/
return NO_ERROR;
}
void sensirion_i2c_hal_init(void)
{
hpm_stat_t stat;
i2c_config_t config;
uint32_t freq;
init_i2c_pins(TEST_I2C);
config.i2c_mode = i2c_mode_normal;
config.is_10bit_addressing = false;
freq = clock_get_frequency(TEST_I2C_CLOCK_NAME);
stat = i2c_init_master(TEST_I2C, freq, &config);
if (stat != status_success)
{
return stat;
}
}
void sensirion_i2c_hal_free(void)
{
/* TODO:IMPLEMENT or leave empty if no resources need to be freed */
}
/**
* Execute one read transaction on the I2C bus, reading a given number of bytes.
* If the device does not acknowledge the read command, an error shall be
* returned.
*
* @param address 7-bit I2C address to read from
* @param data pointer to the buffer where the data is to be stored
* @param count number of bytes to read from I2C and store in the buffer
* @returns 0 on success, error code otherwise
*/
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count)
{
if (status_success != i2c_master_read(TEST_I2C, address, data,count))
{
printf("Master read failed\n");
while (1);
}
return NO_ERROR;
}
/**
* Execute one write transaction on the I2C bus, sending a given number of
* bytes. The bytes in the supplied buffer must be sent to the given address. If
* the slave device does not acknowledge any of the bytes, an error shall be
* returned.
*
* @param address 7-bit I2C address to write to
* @param data pointer to the buffer containing the data to write
* @param count number of bytes to read from the buffer and send over I2C
* @returns 0 on success, error code otherwise
*/
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data, uint16_t count)
{
if (status_success != i2c_master_write(TEST_I2C, address, data, count))
{
printf("Master write failed");
while (1);
}
return NO_ERROR;
}
/**
* Sleep for a given number of microseconds. The function should delay the
* execution for at least the given time, but may also sleep longer.
*
* Despite the unit, a <10 millisecond precision is sufficient.
*
* @param useconds the sleep time in microseconds
*/
void sensirion_i2c_hal_sleep_usec(uint32_t useconds)
{
clock_cpu_delay_us(useconds);
}
测试程序:
#include <stdio.h>
#include "sensirion_common.h"
#include "sensirion_i2c_hal.h"
#include "sht4x_i2c.h"
int sht40_test(void)
{
int16_t error = 0;
sensirion_i2c_hal_init();
uint32_t serial_number;
error = sht4x_serial_number(&serial_number);
if (error)
{
printf("Error executing sht4x_serial_number(): %i\n", error);
}
else
{
printf("Serial number: %u\n", serial_number);
}
// Start Measurement
for (;;)
{
int32_t temperature;
int32_t humidity;
// Read Measurement
error = sht4x_measure_high_precision(&temperature, &humidity);
if (error)
{
printf("Error executing sht4x_measure_high_precision(): %i\n",error);
}
else
{
printf("Temperature: %0.2f °C Humidity: %0.2f %%RH\n",temperature / 1000.0f, humidity / 1000.0f);
}
sensirion_i2c_hal_sleep_usec(1000000);
}
return 0;
}
主函数里面调用测试程序:
/*
* Copyright (c) 2022 HPMicro
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include <stdio.h>
#include "board.h"
#include "hpm_clock_drv.h"
#include "hpm_i2c_drv.h"
extern int sht40_test(void);
int main(void)
{
board_init();
sht40_test();
return 0;
}
将传感器连接到i2c接口上
效果:
|