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
EMC1413.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 <math.h>
18
19/* -----------------------------------------------------------------------------*/
20/* Temperature Sensor - Hardware Register Map for EMC1413
21/* -----------------------------------------------------------------------------*/
22
23/* --- Address: ID Register --- */
24#define PRODUCT_ID 0xFD //Should return h'21
25#define MICROCHIP_ID 0xFE //Should return h'5D
26
27/* --- Address: Temperature Data Register --- */
28#define INT_DIODE_HB 0x00
29#define INT_DIODE_LB 0x29
30#define EXT_DIODE1_HB 0x01
31#define EXT_DIODE1_LB 0x10
32#define EXT_DIODE2_HB 0x23
33#define EXT_DIODE2_LB 0x24
34
35/* --- Address: CSR Register --- */
36#define STATUS_REG 0x02
37#define CONFIG_REG 0x03 //Can accessed this at address of 0x09 as well
38#define CONVERT_RATE_REG 0x04 //Can accessed this at address of 0x0A as well
39
40/* --- Address: Limit Register --- */
41#define HIGH_LIMIT_REG_INT_DIODE 0x05 //Can accessed this at address of 0x0B as well
42#define LOW_LIMIT_REG_INT_DIODE 0x06 //Can accessed this at address of 0x0C as well
43#define HIGH_LIMIT_REG_EXT_DIODE1_HB 0x07 //Can accessed this at address of 0x0D as well
44#define LOW_LIMIT_REG_EXT_DIODE1_HB 0x08 //Can accessed this at address of 0x0E as well
45#define HIGH_LIMIT_REG_EXT_DIODE1_LB 0x13
46#define LOW_LIMIT_REG_EXT_DIODE1_LB 0x14
47#define HIGH_LIMIT_REG_EXT_DIODE2_HB 0x15
48#define LOW_LIMIT_REG_EXT_DIODE2_HB 0x16
49#define HIGH_LIMIT_REG_EXT_DIODE2_LB 0x17
50#define LOW_LIMIT_REG_EXT_DIODE2_LB 0x18
51
52/* --- Address: EMC1414 - Ext_Diode 3 Register --- */
53#define HIGH_LIMIT_REG_EXT_DIODE3_HB 0x2C
54#define HIGH_LIMIT_REG_EXT_DIODE3_LB 0x2E
55#define LOW_LIMIT_REG_EXT_DIODE3_HB 0x2D
56#define LOW_LIMIT_REG_EXT_DIODE3_LB 0x2F
57
58/* --- Address: Thermal Limit Register --- */
59#define THERM_LIMIT 0X37
60#define THERM_LIMIT_EXT_DIODE1 0X19
61#define THERM_LIMIT_EXT_DIODE2 0X1A
62#define THERM_LIMIT_EXT_DIODE3 0X30
63#define THERM_LIMIT_INT_DIODE 0X20
64#define THERM_LIMIT_HYSTERESIS 0X21
65
66/* --- Address: Limit Status Register --- */
67#define HIGH_LIMIT_STATUS_REG 0x35
68#define LOW_LIMIT_STATUS_REG 0x36
69#define THERM_LIMIT_STATUS_REG 0x37
70
71/* --- Address: Calibration --- */
72#define BETA_CONFIG_EX1_REG 0x25 //Default: h'08
73#define BETA_CONFIG_EX2_REG 0x26 //Default: h'08
74
75/* --- Address: Other Register --- */
76#define FILTER_CTRL_REG 0x40
77#define EXT_DIODE_FAULT 0x1B
78#define CONSECUTIVE_ALRT_REG 0x70
79#define CHANNEL_MASK_REG 0x1F
80
81/* --- Address: Limit Temperature Data Register --- */
82#define DEFAULT_HIGH_LIMIT_VALUE 0x55
83#define DEFAULT_LOW_LIMIT_VALUE 0x00
84
85
86/* -----------------------------------------------------------------------------*/
87/* Temperature Sensor: Plug & Play Driver
88/* -----------------------------------------------------------------------------*/
90{
91 .getTemp = emc1413_getTemp,
92 .getTempLimit = emc1413_getTempLimit,
93 .setTempRange = emc1413_setTempRange,
94 .setTempLimit = emc1413_setTempLimit,
95 .checkTempRange = emc1413_checkTempRange,
96 .checkTempAlert = emc1413_checkTempAlert,
97
98};
99
100/* -----------------------------------------------------------------------------*/
101/* Temperature Sensor: Register Lookup Table of reg to set/retrieve limit value
102/* -----------------------------------------------------------------------------*/
103static const u8 LIMIT_REGS[3][5] = {
104 // [0] High_HB, [1] High_LB, [2] Low_HB, [3] Low_LB, [4] Therm
108};
109
110/* -----------------------------------------------------------------------------*/
111/* Temperature Sensor: Register Lookup Table of reg to retrieve Temp value
112/* -----------------------------------------------------------------------------*/
113static const u8 REG_HB[] = { INT_DIODE_HB, EXT_DIODE1_HB, EXT_DIODE2_HB };
114static const u8 REG_LB[] = { INT_DIODE_LB, EXT_DIODE1_LB, EXT_DIODE2_LB };
115
116/******************************************************************************
117*
118* @brief This function retrieve temperature data and store it into struct.
119* @param temp Pointer of temp_sensor instance
120* @return Temperature Sensor Status.
121*
122******************************************************************************/
124 u8 is_extended = emc1413_checkTempRange(temp);
125
126 for (int i = 0; i < 3; i++) {
127 u8 hb = temp_readTempReg(temp, REG_HB[i]);
128 u8 lb = temp_readTempReg(temp, REG_LB[i]);
129
130 // Calculate temperature in celsisus and store in struct.
131 temp->temp_value.channels[i].current_temp = calculate_temp_celsius(hb, lb, is_extended);
132 }
133 return TEMP_SENSOR_OK;
134}
135
136
137/******************************************************************************
138*
139* @brief This function retrieve high/low temperature limitand store it into struct.
140* @param temp Pointer of temp_sensor instance
141* @return Temperature Sensor Status.
142*
143******************************************************************************/
145
146 // Check Range Mode (Extended vs Standard)
147 u8 is_extended = emc1413_checkTempRange(temp);
148 float offset = (is_extended) ? 64.0 : 0.0;
149
150 // Iterate through all 3 channels
151 for (int i = 0; i < 3; i++) {
153 // Get the Register Map for this channel from the Lookup Table
154 const u8 *regs = LIMIT_REGS[i];
155
156 // Read High Byte (Integer)
157 u8 hb = (int8_t)temp_readTempReg(temp, regs[0]);
158 // Read Low Byte (Fractional) - Check if register exists (Internal diode has no LB)
159 float fraction = 0.0;
160 if (regs[1] != 0) {
161 u8 raw_lb = temp_readTempReg(temp, regs[1]);
162 fraction = (raw_lb >> 5) * 0.125;
163 }
164
165 // Combine and Apply Offset
166 ch->high_limit = ((float)hb + fraction) - offset;
167
168 // Read High Byte
169 hb = (int8_t)temp_readTempReg(temp, regs[2]);
170
171 // Read Low Byte
172 fraction = 0.0;
173 if (regs[3] != 0) {
174 u8 raw_lb = temp_readTempReg(temp, regs[3]);
175 fraction = (raw_lb >> 5) * 0.125;
176 }
177
178 // Combine and Apply Offset
179 ch->low_limit = ((float)hb + fraction) - offset;
180 }
181 return TEMP_SENSOR_OK;
182}
183
184/******************************************************************************
185*
186* @brief This function configure the range of the temperature
187*
188* @param temp Pointer of temp_sensor instance
189* @param enable_extended If set to 0, means range of temp from 0°C to +127°C
190* If set to 1, means range of temp from -64°C to +191°C
191* @return Temperature Sensor Status.
192*
193******************************************************************************/
195{
196 u8 status = temp_readTempReg(temp, CONFIG_REG);
197 u8 current_bit = (status & 0x04) >> 2;
198
199 if (current_bit != enable_extended) {
200 if (enable_extended) status |= 0x04;
201 else status &= ~0x04;
202 temp_writeTempReg(temp, CONFIG_REG, status);
203 return TEMP_SENSOR_OK;
204 }
205 return TEMP_SENSOR_SKIP;
206}
207
208// --- API: Set Temperature Limit ---
209/******************************************************************************
210*
211* @brief This function set the high/low temperature limit.
212*
213* @param temp Pointer of temp_sensor instance
214* @return Temperature Sensor Status.
215*
216******************************************************************************/
218{
219 u8 is_extended = emc1413_checkTempRange(temp);
220
221 // Iterate through all 3 channels
222 for (int i = 0; i < 3; i++) {
223 u8 hb, lb;
225 const u8 *regs = LIMIT_REGS[i];
226 // Write High Limit
227 encode_temp_limit(ch->high_limit, is_extended, &hb, &lb);
228 temp_writeTempReg(temp, regs[0], hb);
229 if (regs[1] != 0)
230 temp_writeTempReg(temp, regs[1], lb);
231
232 // Write Low Limit
233 encode_temp_limit(ch->low_limit, is_extended, &hb, &lb);
234 temp_writeTempReg(temp, regs[2], hb);
235 if (regs[3] != 0)
236 temp_writeTempReg(temp, regs[3], lb);
237
238 // Update Thermal Limit (Using High Limit Integer)
239 temp_writeTempReg(temp, regs[4], hb);
240 }
241 return TEMP_SENSOR_OK;
242}
243
244/******************************************************************************
245*
246* @brief This function check whether the temperature exceed the low/high limit
247* temperature register.
248* @param temp Pointer of temp_sensor instance
249* @return 0 The temperature of the device still within the acceptable range.
250* @return 4 The temperature of the device exceed above the high temp limit register.
251* @return 5 The temperature of the device exceed below the low temp limit register.
252*
253******************************************************************************/
255 u8 high_status = temp_readTempReg(temp, HIGH_LIMIT_STATUS_REG) & 0x07;
256 u8 low_status = temp_readTempReg(temp, LOW_LIMIT_STATUS_REG) & 0x07;
257
258 if (high_status > 0) return TEMP_SENSOR_HIGH_ALERT; // High Alert
259 if (low_status > 0) return TEMP_SENSOR_LOW_ALERT; // Low Alert
260 return TEMP_SENSOR_OK; // OK
261}
262
263
264/******************************************************************************
265*
266* @brief This function check whether the extended range of temperature is in used.
267* @param temp Pointer of temp_sensor instance
268* @return 0 Default Range of the temperature measurement (0°C to +127°C) is used.
269* @return 1 Extended Range of the temperature measurement (-65°C to +191°C) is used
270*
271******************************************************************************/
273 u8 status = temp_readTempReg(temp, CONFIG_REG);
274 return (status & 0x04) >> 2;
275}
#define LOW_LIMIT_REG_INT_DIODE
Definition EMC1413.c:42
#define HIGH_LIMIT_REG_EXT_DIODE2_LB
Definition EMC1413.c:49
#define INT_DIODE_HB
Definition EMC1413.c:28
#define HIGH_LIMIT_REG_EXT_DIODE1_HB
Definition EMC1413.c:43
#define EXT_DIODE1_HB
Definition EMC1413.c:30
#define HIGH_LIMIT_REG_INT_DIODE
Definition EMC1413.c:41
#define THERM_LIMIT_EXT_DIODE2
Definition EMC1413.c:61
#define LOW_LIMIT_REG_EXT_DIODE2_HB
Definition EMC1413.c:48
#define HIGH_LIMIT_REG_EXT_DIODE2_HB
Definition EMC1413.c:47
#define EXT_DIODE2_HB
Definition EMC1413.c:32
#define LOW_LIMIT_REG_EXT_DIODE2_LB
Definition EMC1413.c:50
#define LOW_LIMIT_REG_EXT_DIODE1_HB
Definition EMC1413.c:44
#define THERM_LIMIT_INT_DIODE
Definition EMC1413.c:63
#define THERM_LIMIT_EXT_DIODE1
Definition EMC1413.c:60
#define EXT_DIODE1_LB
Definition EMC1413.c:31
#define LOW_LIMIT_REG_EXT_DIODE1_LB
Definition EMC1413.c:46
#define LOW_LIMIT_STATUS_REG
Definition EMC1413.c:68
#define HIGH_LIMIT_STATUS_REG
Definition EMC1413.c:67
#define HIGH_LIMIT_REG_EXT_DIODE1_LB
Definition EMC1413.c:45
#define INT_DIODE_LB
Definition EMC1413.c:29
#define EXT_DIODE2_LB
Definition EMC1413.c:33
#define CONFIG_REG
Definition EMC1413.c:37
EMC1413 Temperature Sensor Driver API definitions. This file provides data structures and APIs for co...
const temp_sensor_api_t emc1413_driver
EMC1413 Driver Instance. Point your generic Temperature Sensor pointer to this structure to use the E...
Definition EMC1413.c:89
temp_sensor_status_t emc1413_getTemp(temp_sensor_instance_t *temp)
Get temperature from EMC1413 Temperature Sensor.
Definition EMC1413.c:123
temp_sensor_status_t emc1413_checkTempAlert(temp_sensor_instance_t *temp)
Check for temperature alerts from EMC1413 Temperature Sensor.
Definition EMC1413.c:254
temp_sensor_status_t emc1413_setTempRange(temp_sensor_instance_t *temp, u8 enable_extended)
Set temperature range for EMC1413 Temperature Sensor.
Definition EMC1413.c:194
temp_sensor_status_t emc1413_getTempLimit(temp_sensor_instance_t *temp)
Get temperature limits from EMC1413 Temperature Sensor.
Definition EMC1413.c:144
u8 emc1413_checkTempRange(temp_sensor_instance_t *temp)
Check if temperature is within range for EMC1413 Temperature Sensor.
Definition EMC1413.c:272
temp_sensor_status_t emc1413_setTempLimit(temp_sensor_instance_t *temp)
Set temperature limits for EMC1413 Temperature Sensor.
Definition EMC1413.c:217
temp_sensor_status_t
Temperature Sensor Status List.
Definition temp_sensor.h:59
@ TEMP_SENSOR_SKIP
Skip the function *‍/.
Definition temp_sensor.h:63
@ TEMP_SENSOR_HIGH_ALERT
High Temperature Alert *‍/.
Definition temp_sensor.h:64
@ TEMP_SENSOR_OK
Successful Operation *‍/.
Definition temp_sensor.h:60
@ TEMP_SENSOR_LOW_ALERT
Low Temperature Alert *‍/.
Definition temp_sensor.h:65
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.
Temperature Sensor API structure.
Hold temperature data for a single channel/diode.
float high_limit
High Temperature Limit *‍/.
float current_temp
Current Temperature *‍/.
float low_limit
Low Temperature Limit *‍/.
temp_sensor_channelData_t channels[3]
temp_sensor_data_t temp_value
Temperature Sensor data *‍/.
uint8_t u8
Definition type.h:26