RV32 SoC DS UG
High-Perf RV32 SoC DS UG
RV64 SoC DS UG API and Examples
Embedded IDE UG
Loading...
Searching...
No Matches
temp_sensor.c
Go to the documentation of this file.
1
2// Copyright (C) 2013-2026 Efinix Inc. All rights reserved.
3// Full license header bsp/efinix/EfxSapphireSocRV64/include/LICENSE.MD
5
15
17#include <stddef.h>
18/* -----------------------------------------------------------------------------*/
19/* Common API: Helper
20/* -----------------------------------------------------------------------------*/
22
23 if (temp != NULL && temp->drv != NULL && temp->drv->applyConfig != NULL) {
24
25 return temp->drv->applyConfig(temp);
26 }
27
28 return TEMP_SENSOR_ERR;
29}
30
32
33 if (temp != NULL && temp->drv != NULL && temp->drv->enableErrorInterrupt != NULL) {
34 return temp->drv->enableErrorInterrupt(temp);
35 }
36 return TEMP_SENSOR_ERR;
37}
38
40{
41 u8 data = 0;
42 if (temp && temp->inst) {
43 i2c_setMux(temp->inst, 0x0F);
44 i2c_readData_b(temp->inst, reg, &data, 1);
45 }
46 return data;
47}
48
50{
51 if (temp && temp->inst) {
52 i2c_setMux(temp->inst, 0x0F);
53 i2c_writeData_b(temp->inst, reg, (u8*)&data, 1);
54 return TEMP_SENSOR_OK;
55 }
56 return TEMP_SENSOR_SKIP;
57}
58
59float tempSensor_calTempCelsius(u8 hb, u8 lb, u8 is_extended) {
60 int16_t integer_part = hb;
61
62 // Apply Offset (Extended: -64 to 191)
63 if (is_extended) {
64 integer_part -= 64;
65 }
66
67 // Calculate Fractional Part (0.125 steps)
68 float fractional = (lb >> 5) * 0.125f;
69 return (float)integer_part + fractional;
70}
71
72u8 tempSensor_encodeTempLimit(float val, u8 is_extended, u8 *out_hb, u8 *out_lb) {
73 // Handle Offset
74 if (is_extended) {
75 val += 64.0f;
76 }
77
78 // Extract Integer Part
79 int integer_part = (int)val;
80 if (integer_part < 0) integer_part = 0; // Clamp min
81 if (integer_part > 255) integer_part = 255; // Clamp max
82 *out_hb = (u8)integer_part;
83
84 // Extract Fractional Part
85 float fraction = val - integer_part;
86 if (fraction < 0) fraction = 0;
87 u8 frac_bits = (u8)(fraction * 8.0f);
88 *out_lb = (frac_bits << 5);
89 return TEMP_SENSOR_OK;
90}
void i2c_setMux(i2c_instance_t *inst, const uint8_t cr)
Set I2C MUX control register.
Definition i2c.c:314
void i2c_writeData_b(i2c_instance_t *inst, u8 regAddr, u8 *data, u32 length)
Write data with an 8-bit register address over I2C and check rx ack for each transaction.
Definition i2c.c:224
void i2c_readData_b(i2c_instance_t *inst, u8 regAddr, u8 *data, u32 length)
Read data with an 8-bit register address over I2C.
Definition i2c.c:258
temp_sensor_status_t
Temperature Sensor Status List.
Definition temp_sensor.h:91
@ TEMP_SENSOR_SKIP
Skip the function *‍/.
Definition temp_sensor.h:95
@ TEMP_SENSOR_OK
Successful Operation *‍/.
Definition temp_sensor.h:92
@ TEMP_SENSOR_ERR
Failed to retrieve/write value *‍/.
Definition temp_sensor.h:93
temp_sensor_status_t tempSensor_enableErrorInterrupt(temp_sensor_instance_t *temp)
Enable error interrupt for the Temperature Sensor.
Definition temp_sensor.c:31
temp_sensor_status_t tempSensor_applyConfig(temp_sensor_instance_t *temp)
Apply I2C + Temp Sensor configuration.
Definition temp_sensor.c:21
u8 tempSensor_readTempReg(temp_sensor_instance_t *temp, const u8 reg)
Read a register from the Temperature Sensor.
Definition temp_sensor.c:39
u8 tempSensor_writeTempReg(temp_sensor_instance_t *temp, const u8 reg, const u8 data)
Write a value to a register in the Temperature Sensor.
Definition temp_sensor.c:49
u8 tempSensor_encodeTempLimit(float val, u8 is_extended, u8 *out_hb, u8 *out_lb)
Encode temperature limit into high and low byte format.
Definition temp_sensor.c:72
float tempSensor_calTempCelsius(u8 hb, u8 lb, u8 is_extended)
Calculate temperature in Celsius from high and low byte format.
Definition temp_sensor.c:59
struct temp_sensor_instance temp_sensor_instance_t
Forward declaration of Temperature Sensor instance.
temp_sensor_status_t(* applyConfig)(temp_sensor_instance_t *temp)
temp_sensor_status_t(* enableErrorInterrupt)(temp_sensor_instance_t *temp)
const temp_sensor_api_t * drv
Pointer to Temperature Sensor API structure *‍/.
i2c_instance_t * inst
Pointer to I2C instance *‍/.
Temperature Sensor driver API definitions.
uint8_t u8
Definition type.h:30