Sapphire SoC DS Sapphire SoC UG Sapphire HP SoC DS Sapphire HP SoC UG RISC-V Embedded IDE UG Board Support Package
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
18/* -----------------------------------------------------------------------------*/
19/* Common API: Helper
20/* -----------------------------------------------------------------------------*/
21
23{
24 u8 data = 0;
25 if (temp && temp->inst) {
26 i2c_setMux(temp->inst, 0x0F);
27 i2c_readData_b(temp->inst, reg, &data, 1);
28 }
29 return data;
30}
31
32u8 temp_writeTempReg(temp_sensor_instance_t *temp, const u8 reg, const u8 data)
33{
34 if (temp && temp->inst) {
35 i2c_setMux(temp->inst, 0x0F);
36 i2c_writeData_b(temp->inst, reg, (u8*)&data, 1);
37 return TEMP_SENSOR_OK;
38 }
39 return TEMP_SENSOR_SKIP;
40}
41
42float calculate_temp_celsius(u8 hb, u8 lb, u8 is_extended) {
43 int16_t integer_part = hb;
44
45 // Apply Offset (Extended: -64 to 191)
46 if (is_extended) {
47 integer_part -= 64;
48 }
49
50 // Calculate Fractional Part (0.125 steps)
51 float fractional = (lb >> 5) * 0.125f;
52 return (float)integer_part + fractional;
53}
54
55u8 encode_temp_limit(float val, u8 is_extended, u8 *out_hb, u8 *out_lb) {
56 // Handle Offset
57 if (is_extended) {
58 val += 64.0f;
59 }
60
61 // Extract Integer Part
62 int integer_part = (int)val;
63 if (integer_part < 0) integer_part = 0; // Clamp min
64 if (integer_part > 255) integer_part = 255; // Clamp max
65 *out_hb = (u8)integer_part;
66
67 // Extract Fractional Part
68 float fraction = val - integer_part;
69 if (fraction < 0) fraction = 0;
70 u8 frac_bits = (u8)(fraction * 8.0f);
71 *out_lb = (frac_bits << 5);
72 return TEMP_SENSOR_OK;
73}
void i2c_setMux(i2c_instance_t *inst, const uint8_t cr)
Set I2C MUX control register.
Definition i2c.c:307
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:219
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:253
@ TEMP_SENSOR_SKIP
Skip the function *‍/.
Definition temp_sensor.h:63
@ TEMP_SENSOR_OK
Successful Operation *‍/.
Definition temp_sensor.h:60
u8 temp_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:32
u8 temp_readTempReg(temp_sensor_instance_t *temp, const u8 reg)
Read a register from the Temperature Sensor.
Definition temp_sensor.c:22
u8 encode_temp_limit(float val, u8 is_extended, u8 *out_hb, u8 *out_lb)
Encode temperature limit into high and low byte format.
Definition temp_sensor.c:55
float calculate_temp_celsius(u8 hb, u8 lb, u8 is_extended)
Calculate temperature in Celsius from high and low byte format.
Definition temp_sensor.c:42
struct temp_sensor_instance temp_sensor_instance_t
Forward declaration of Temperature Sensor instance.
i2c_instance_t * inst
Pointer to I2C instance *‍/.
Temperature Sensor driver API definitions.
uint8_t u8
Definition type.h:26