|  | Dual Active Bridge Development Board (Part-No. ) |
Content |  |  |  |  |  |

Loading...
Searching...
No Matches
dev_temp.c
1
2/*
3 * File: dev_Temp.c
4 * Author: M70027
5 *
6 * Created on Jan 17, 2024, 13:30 PM
7 */
8
15#include "dev_temp.h"
16#include <stdlib.h>
17
18#include "config/hardware.h"
19#include "config/macros.h"
20#include "pwrctrl/pwrctrl_comm_interface.h"
21#include "pwrctrl/pwrctrl.h"
22
23
30 TEMP_SETTINGS_t devTempData;
31 TEMP_SETTINGS_t* devTempDataPtr = &devTempData;
32
33// Private Function Call Prototypes
34static uint16_t Average_Temp_ADC_Samples(void);
35static uint16_t Temp_Calculate_Average(uint16_t * buffer, uint16_t size);
36
37
38
45 void Dev_Temp_Initialize(void){
46 devTempData.OverTemperatureFlag = 0;
47 devTempData.SensorLost = 0;
48 devTempData.AdcReading = 0;
49 devTempData.AdcAverage = 0;
50 devTempData.TemperatureCelcius = 0;
51}
52
53
60 void Dev_Temp_Task_100ms(void){
61
62 Dev_Temp_Get_ADC_Sample();
63 devTempData.AdcAverage = Average_Temp_ADC_Samples();
64}
65
66
73uint16_t Dev_Temp_AverageValue(void){
74 return (devTempData.AdcAverage);
75}
76
87int8_t Dev_Temp_Get_Temperature_Celcius(void){
88
89 static int8_t temperatureCelsius;
90 if (devTempData.AdcAverage == 0)
91 temperatureCelsius = 0;
92 if (devTempData.BufferFull)
93 temperatureCelsius =(int8_t)(__builtin_mulsu(TEMPERATURE_FACTOR, devTempData.AdcAverage) >> 15) + TEMPERATURE_OFFSET;
94 return temperatureCelsius;
95}
96
97
105uint8_t Dev_Temp_Is_Over_Temperature(void) {
106 return devTempData.OverTemperatureFlag;
107}
108
109
117 void Dev_Temp_Get_ADC_Sample(void)
118{
119 // making sure that array does not go out of bounds.
120 if (devTempData.BufferIndex >= (MAX_NUM_SAMPLES_TEMP_BUFFER - 1))
121 {
122 devTempData.BufferIndex = 0;
123 devTempData.BufferFull = 1;
124 }
125
126 // use API from power controller to read temperature
127 devTempData.AdcReading = PwrCtrl_GetAdc_Temperature();
128 devTempData.TempBuffer[devTempData.BufferIndex++] = devTempData.AdcReading;
129
130}
131
132
139 static uint16_t Average_Temp_ADC_Samples(void) {
140 if (devTempData.BufferFull)
141 return Temp_Calculate_Average(devTempData.TempBuffer, MAX_NUM_SAMPLES_TEMP_BUFFER);
142 else
143 return devTempData.AdcAverage;
144}
145
146
153 static uint16_t Temp_Calculate_Average(uint16_t * buffer, uint16_t length) {
154 uint16_t index = 0;
155 uint32_t sum = 0;
156 for (index = 0; index <length; index++)
157 sum +=buffer[index];
158 return (sum >> MAX_NUM_SAMPLES_TEMP_BUFFER_AS_EXP_OF_TWO);
159}
160
161
dev_temp.h
Contains temperature initialization and execution functions.
MAX_NUM_SAMPLES_TEMP_BUFFER
#define MAX_NUM_SAMPLES_TEMP_BUFFER
These have to be renamed to make the temp device driver abstract.
Definition dev_temp.h:35
MAX_NUM_SAMPLES_TEMP_BUFFER_AS_EXP_OF_TWO
#define MAX_NUM_SAMPLES_TEMP_BUFFER_AS_EXP_OF_TWO
for bit shifting
Definition dev_temp.h:36
pwrctrl_comm_interface.h
Contains public functions relevant for power control communication interface.
TEMPERATURE_OFFSET
#define TEMPERATURE_OFFSET
Definition hardware.h:73
TEMPERATURE_FACTOR
#define TEMPERATURE_FACTOR
Definition macros.h:154
Dev_Temp_AverageValue
uint16_t Dev_Temp_AverageValue(void)
Returns the average adc temperature reading.
Definition dev_temp.c:73
Dev_Temp_Is_Over_Temperature
uint8_t Dev_Temp_Is_Over_Temperature(void)
Returns the status of the Over Temperature flag.
Definition dev_temp.c:105
Dev_Temp_Task_100ms
void Dev_Temp_Task_100ms(void)
Executes the Temperature reading every 100ms.
Definition dev_temp.c:60
Dev_Temp_Get_ADC_Sample
void Dev_Temp_Get_ADC_Sample(void)
Gets the temperature ADC sample.
Definition dev_temp.c:117
Dev_Temp_Initialize
void Dev_Temp_Initialize(void)
Initializes the temperature data structure.
Definition dev_temp.c:45
Temp_Calculate_Average
static uint16_t Temp_Calculate_Average(uint16_t *buffer, uint16_t size)
Averages the temperature ADC samples.
Definition dev_temp.c:153
devTempData
TEMP_SETTINGS_t devTempData
Data Object of temperature settings.
Definition dev_temp.c:30
Dev_Temp_Get_Temperature_Celcius
int8_t Dev_Temp_Get_Temperature_Celcius(void)
This converts the raw values to temperature celcius as per device lookup table.
Definition dev_temp.c:87
Average_Temp_ADC_Samples
static uint16_t Average_Temp_ADC_Samples(void)
Returns the temperature average value.
Definition dev_temp.c:139
PwrCtrl_GetAdc_Temperature
uint16_t PwrCtrl_GetAdc_Temperature(void)
API function to get the raw ADC value for temperature.
Definition pwrctrl_comm_interface.c:138
TEMP_SETTINGS_s
A custom data type for temperature measurement.
Definition dev_temp.h:43
TEMP_SETTINGS_s::OverTemperatureFlag
uint8_t OverTemperatureFlag
Over temperature flag.
Definition dev_temp.h:44
TEMP_SETTINGS_s::TempBuffer
uint16_t TempBuffer[MAX_NUM_SAMPLES_TEMP_BUFFER]
buffer for calcualting average.
Definition dev_temp.h:49
TEMP_SETTINGS_s::BufferIndex
uint16_t BufferIndex
buffer index
Definition dev_temp.h:52
TEMP_SETTINGS_s::SensorLost
uint8_t SensorLost
if sensor is present
Definition dev_temp.h:45
TEMP_SETTINGS_s::AdcReading
uint16_t AdcReading
temperature value raw sampled from ADC
Definition dev_temp.h:46
TEMP_SETTINGS_s::AdcAverage
uint16_t AdcAverage
temperature
Definition dev_temp.h:47
TEMP_SETTINGS_s::TemperatureCelcius
uint16_t TemperatureCelcius
temperature celcius.
Definition dev_temp.h:48
TEMP_SETTINGS_s::BufferFull
uint16_t BufferFull
flag to monitor if full then allow averaging.
Definition dev_temp.h:50