【FRDM-MCXN947】 P3T1755温度传感器测试
[复制链接]
读取P3T1755温度传感器的值,通过串口打印输出。
一、硬件部分
P3T1755电路图部分
二、接口和时钟配置
2.1、配置I3C接口
2.2、配置I3C时钟
也可以在程序中配置,配置代码
/* Attach PLL0 clock to I3C, 150MHz / 6 = 25MHz. */
CLOCK_SetClkDiv(kCLOCK_DivI3c1FClk, 6U);
CLOCK_AttachClk(kPLL0_to_I3C1FCLK);
三、程序部分
3.1、fsl_p3t1755.c
/*
* Copyright 2022, 2024 NXP
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "fsl_p3t1755.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/*******************************************************************************
* Code
******************************************************************************/
status_t P3T1755_WriteReg(p3t1755_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize)
{
status_t result;
result = handle->writeTransfer(handle->sensorAddress, regAddress, regData, dataSize);
return (result == kStatus_Success) ? result : kStatus_Fail;
}
status_t P3T1755_ReadReg(p3t1755_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize)
{
status_t result;
result = handle->readTransfer(handle->sensorAddress, regAddress, regData, dataSize);
return (result == kStatus_Success) ? result : kStatus_Fail;
}
status_t P3T1755_Init(p3t1755_handle_t *handle, p3t1755_config_t *config)
{
assert(handle != NULL);
assert(config != NULL);
assert(config->writeTransfer != NULL);
assert(config->readTransfer != NULL);
handle->writeTransfer = config->writeTransfer;
handle->readTransfer = config->readTransfer;
handle->sensorAddress = config->sensorAddress;
return kStatus_Success;
}
status_t P3T1755_ReadTemperature(p3t1755_handle_t *handle, double *temperature)
{
status_t result = kStatus_Success;
uint8_t data[2];
uint16_t temp;
result = P3T1755_ReadReg(handle, P3T1755_TEMPERATURE_REG, &data[0], 2);
if (result == kStatus_Success)
{
temp = (((uint16_t)data[0] << 8U) | (uint16_t)data[1]) >> 4U;
*temperature = (double)temp * 0.0625;
}
return result;
}
3.2、fsl_p3t1755.h
/*
* Copyright 2022 NXP
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _FSL_P3T1755_H_
#define _FSL_P3T1755_H_
#include "fsl_common.h"
/* Registers. */
#define P3T1755_TEMPERATURE_REG (0x00U)
#define P3T1755_CONFIG_REG (0x01U)
/*! [url=home.php?mod=space&uid=159083]@brief[/url] Define sensor access function. */
typedef status_t (*sensor_write_transfer_func_t)(uint8_t deviceAddress,
uint32_t regAddress,
uint8_t *regData,
size_t dataSize);
typedef status_t (*sensor_read_transfer_func_t)(uint8_t deviceAddress,
uint32_t regAddress,
uint8_t *regData,
size_t dataSize);
typedef struct _p3t1755_handle
{
sensor_write_transfer_func_t writeTransfer;
sensor_read_transfer_func_t readTransfer;
uint8_t sensorAddress;
} p3t1755_handle_t;
typedef struct _p3t1755_config
{
sensor_write_transfer_func_t writeTransfer;
sensor_read_transfer_func_t readTransfer;
uint8_t sensorAddress;
} p3t1755_config_t;
#if defined(__cplusplus)
extern "C" {
#endif
/*!
* @brief Create handle for P3T1755, reset the sensor per user configuration.
*
* @param p3t1755_handle The pointer to #p3t1755_handle_t.
*
* [url=home.php?mod=space&uid=784970]@return[/url] kStatus_Success if success or kStatus_Fail if error.
*/
status_t P3T1755_Init(p3t1755_handle_t *handle, p3t1755_config_t *config);
/*!
* @brief Write Register with register data buffer.
*
* @param handle The pointer to #p3t1755_handle_t.
* @param regAddress register address to write.
* @param regData The pointer to data buffer to be write to the reg.
* @param dataSize Size of the regData.
*
* @return kStatus_Success if success or kStatus_Fail if error.
*/
status_t P3T1755_WriteReg(p3t1755_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize);
/*!
* @brief Read Register to speficied data buffer.
*
* @param handle The pointer to #p3t1755_handle_t.
* @param regAddress register address to read.
* @param regData The pointer to data buffer to store the read out data.
* @param dataSize Size of the regData to be read.
*
* @return kStatus_Success if success or kStatus_Fail if error.
*/
status_t P3T1755_ReadReg(p3t1755_handle_t *handle, uint32_t regAddress, uint8_t *regData, size_t dataSize);
/*!
* @brief Read temperature data.
*
* @param handle The pointer to #p3t1755_handle_t.
* @param temperature The pointer to temperature data.
*
* @return kStatus_Success if success or kStatus_Fail if error.
*/
status_t P3T1755_ReadTemperature(p3t1755_handle_t *handle, double *temperature);
#if defined(__cplusplus)
}
#endif /* __cplusplus */
#endif /* _FSL_P3T1755_H_ */
3.3、p3t1755.c
#include "main.h"
/*******************************************************************************
* Prototypes
******************************************************************************/
static void i3c_master_callback(I3C_Type *base, i3c_master_handle_t *handle, status_t status, void *userData);
/*******************************************************************************
* Variables
******************************************************************************/
volatile status_t g_completionStatus;
volatile bool g_masterCompletionFlag;
i3c_master_handle_t g_i3c_m_handle;
p3t1755_handle_t p3t1755Handle;
const i3c_master_transfer_callback_t masterCallback = {
.slave2Master = NULL, .ibiCallback = NULL, .transferComplete = i3c_master_callback};
/*******************************************************************************
* Code
******************************************************************************/
static void i3c_master_callback(I3C_Type *base, i3c_master_handle_t *handle, status_t status, void *userData)
{
if (status == kStatus_Success)
{
g_masterCompletionFlag = true;
}
g_completionStatus = status;
}
status_t I3C_WriteSensor(uint8_t deviceAddress, uint32_t regAddress, uint8_t *regData, size_t dataSize)
{
status_t result = kStatus_Success;
i3c_master_transfer_t masterXfer = {0};
uint32_t timeout = 0U;
masterXfer.slaveAddress = deviceAddress;
masterXfer.direction = kI3C_Write;
masterXfer.busType = kI3C_TypeI3CSdr;
masterXfer.subaddress = regAddress;
masterXfer.subaddressSize = 1;
masterXfer.data = regData;
masterXfer.dataSize = dataSize;
masterXfer.flags = kI3C_TransferDefaultFlag;
g_masterCompletionFlag = false;
g_completionStatus = kStatus_Success;
result = I3C_MasterTransferNonBlocking(EXAMPLE_MASTER, &g_i3c_m_handle, &masterXfer);
if (kStatus_Success != result)
{
return result;
}
while (!g_masterCompletionFlag)
{
timeout++;
if ((g_completionStatus != kStatus_Success) || (timeout > I3C_TIME_OUT_INDEX))
{
break;
}
}
if (timeout == I3C_TIME_OUT_INDEX)
{
result = kStatus_Timeout;
}
result = g_completionStatus;
return result;
}
status_t I3C_ReadSensor(uint8_t deviceAddress, uint32_t regAddress, uint8_t *regData, size_t dataSize)
{
status_t result = kStatus_Success;
i3c_master_transfer_t masterXfer = {0};
uint32_t timeout = 0U;
masterXfer.slaveAddress = deviceAddress;
masterXfer.direction = kI3C_Read;
masterXfer.busType = kI3C_TypeI3CSdr;
masterXfer.subaddress = regAddress;
masterXfer.subaddressSize = 1;
masterXfer.data = regData;
masterXfer.dataSize = dataSize;
masterXfer.flags = kI3C_TransferDefaultFlag;
g_masterCompletionFlag = false;
g_completionStatus = kStatus_Success;
result = I3C_MasterTransferNonBlocking(EXAMPLE_MASTER, &g_i3c_m_handle, &masterXfer);
if (kStatus_Success != result)
{
return result;
}
while (!g_masterCompletionFlag)
{
timeout++;
if ((g_completionStatus != kStatus_Success) || (timeout > I3C_TIME_OUT_INDEX))
{
break;
}
}
if (timeout == I3C_TIME_OUT_INDEX)
{
result = kStatus_Timeout;
}
result = g_completionStatus;
return result;
}
status_t p3t1755_set_dynamic_address(void)
{
status_t result = kStatus_Success;
i3c_master_transfer_t masterXfer = {0};
uint8_t g_master_txBuff[1];
/* Reset dynamic address. */
g_master_txBuff[0] = CCC_RSTDAA;
masterXfer.slaveAddress = 0x7E;
masterXfer.data = g_master_txBuff;
masterXfer.dataSize = 1;
masterXfer.direction = kI3C_Write;
masterXfer.busType = kI3C_TypeI3CSdr;
masterXfer.flags = kI3C_TransferDefaultFlag;
result = I3C_MasterTransferBlocking(EXAMPLE_MASTER, &masterXfer);
if (result != kStatus_Success)
{
return result;
}
/* Assign dynmic address. */
memset(&masterXfer, 0, sizeof(masterXfer));
g_master_txBuff[0] = CCC_SETDASA;
masterXfer.slaveAddress = 0x7E;
masterXfer.data = g_master_txBuff;
masterXfer.dataSize = 1;
masterXfer.direction = kI3C_Write;
masterXfer.busType = kI3C_TypeI3CSdr;
masterXfer.flags = kI3C_TransferNoStopFlag;
result = I3C_MasterTransferBlocking(EXAMPLE_MASTER, &masterXfer);
if (result != kStatus_Success)
{
return result;
}
memset(&masterXfer, 0, sizeof(masterXfer));
g_master_txBuff[0] = SENSOR_ADDR << 1;
masterXfer.slaveAddress = SENSOR_SLAVE_ADDR;
masterXfer.data = g_master_txBuff;
masterXfer.dataSize = 1;
masterXfer.direction = kI3C_Write;
masterXfer.busType = kI3C_TypeI3CSdr;
masterXfer.flags = kI3C_TransferDefaultFlag;
return I3C_MasterTransferBlocking(EXAMPLE_MASTER, &masterXfer);
}
void init_p3t1755(void)
{
status_t result = kStatus_Success;
i3c_master_config_t masterConfig;
p3t1755_config_t p3t1755Config;
// /* Attach PLL0 clock to I3C, 150MHz / 6 = 25MHz. */
// CLOCK_SetClkDiv(kCLOCK_DivI3c1FClk, 6U);
// CLOCK_AttachClk(kPLL0_to_I3C1FCLK);
init_p3t1755_pins();
PRINTF("\r\nI3C master read sensor data example.\r\n");
I3C_MasterGetDefaultConfig(&masterConfig);
masterConfig.baudRate_Hz.i2cBaud = EXAMPLE_I2C_BAUDRATE;
masterConfig.baudRate_Hz.i3cPushPullBaud = EXAMPLE_I3C_PP_BAUDRATE;
masterConfig.baudRate_Hz.i3cOpenDrainBaud = EXAMPLE_I3C_OD_BAUDRATE;
masterConfig.enableOpenDrainStop = false;
masterConfig.disableTimeout = true;
I3C_MasterInit(EXAMPLE_MASTER, &masterConfig, I3C_MASTER_CLOCK_FREQUENCY);
/* Create I3C handle. */
I3C_MasterTransferCreateHandle(EXAMPLE_MASTER, &g_i3c_m_handle, &masterCallback, NULL);
result = p3t1755_set_dynamic_address();
if (result != kStatus_Success)
{
PRINTF("\r\nP3T1755 set dynamic address failed.\r\n");
}
p3t1755Config.writeTransfer = I3C_WriteSensor;
p3t1755Config.readTransfer = I3C_ReadSensor;
p3t1755Config.sensorAddress = SENSOR_ADDR;
P3T1755_Init(&p3t1755Handle, &p3t1755Config);
}
void p3t1755_test(void)
{
status_t result = kStatus_Success;
double temperature;
while(1)
{
result = P3T1755_ReadTemperature(&p3t1755Handle, &temperature);
if (result != kStatus_Success)
{
PRINTF("\r\nP3T1755 read temperature failed.\r\n");
}
else
{
PRINTF("Temperature:%0.1f \r\n", temperature);
}
SysTick_Delay_ms(500);
}
}
3.4、p3t1755.h
#ifndef __P3T1755_H
#define __P3T1755_H
#define EXAMPLE_MASTER I3C1
#define I3C_MASTER_CLOCK_FREQUENCY CLOCK_GetI3cClkFreq(1)
#define SENSOR_SLAVE_ADDR 0x48U
#define I3C_TIME_OUT_INDEX 100000000U
#define SENSOR_ADDR 0x08U
#define CCC_RSTDAA 0x06U
#define CCC_SETDASA 0x87
#ifndef EXAMPLE_I2C_BAUDRATE
#define EXAMPLE_I2C_BAUDRATE 400000
#endif
#ifndef EXAMPLE_I3C_OD_BAUDRATE
#define EXAMPLE_I3C_OD_BAUDRATE 1500000
#endif
#ifndef EXAMPLE_I3C_PP_BAUDRATE
#define EXAMPLE_I3C_PP_BAUDRATE 4000000
#endif
void init_p3t1755(void);
void p3t1755_test(void);
#endif
3.5、main.c
#include "main.h"
int main(void)
{
uint8_t i;
CLOCK_EnableClock(kCLOCK_Gpio0);
BOARD_InitDEBUG_UARTPins();
BOARD_PowerMode_OD();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
init_utick();
SysTick_Init();
init_p3t1755();
init_led();
init_key();
p3t1755_test();
while (1)
{
SysTick_Delay_ms(500);
led_red_tog();
}
}
3.6、main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "fsl_port.h"
#include "fsl_lpi2c.h"
#include "fsl_common.h"
#include "fsl_lpspi.h"
#include "fsl_lpspi_edma.h"
#include "fsl_utick.h"
#include "led/led.h"
#include "systick/systick.h"
#include "key/key.h"
#include "fsl_p3t1755.h"
#include "fsl_i3c.h"
#include "p3t1755/p3t1755.h"
#include "utick/utick.h"
#endif
四、运行结果
程序下载后,串口输出温度值
|