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

Loading...
Searching...
No Matches
dev_iso_voltage_sense.c
1/*
2 * File: dev_iso_voltage_sense.c
3 * Author: M70027
4 *
5 * Created on Jan 17, 2024, 13:30 PM
6 */
7
8#include "dev_iso_voltage_sense.h"
9#include "../sources/fault/fault_common.h"
10
11
18 dev_iso_voltage_data_t dev_iso_voltage_data;
19 dev_iso_voltage_data_t* dev_iso_voltage_data_ptr = &dev_iso_voltage_data;
20
21
28static uint16_t interrupt_counter = 0;
29
30
37 FAULT_OBJ_T Vin_Fault_Min;
38
39
46 FAULT_OBJ_T Vin_Fault_Max;
47
48
51void Dev_Iso_Voltage_Sens_Callback(void);
52void Dev_Iso_Voltage_Sens_Calculate_Average(void);
53uint16_t Iso_Calculate_Average(uint16_t * buffer, uint16_t length);
54void default_faultcallback_max(FAULT_OBJ_T* faultInput);
55void default_faultcallback_min(FAULT_OBJ_T* faultInput);
56
57
61
70void Dev_Iso_Voltage_Sens_Init(){
71 dev_iso_voltage_data.over_voltage_flag = 0;
72 dev_iso_voltage_data.under_voltage_flag = 0;
73 dev_iso_voltage_data.no_signal_flag = 0;
74 dev_iso_voltage_data.buffer_full = 0;
75 dev_iso_voltage_data.voltage_buffer_idx = 0;
76
77 FAULT_Init(
78 &Vin_Fault_Min,
79 UV_THRESHOLD_RAW,
80 UV_LOWER_THRESHOLD_WITH_HYST,
81 FAULT_PERSISTENCE_COUNT,
82 FAULT_PERSISTENCE_COUNT
83 ) ;
84
85 FAULT_Init(
86 &Vin_Fault_Max,
87 OV_THRESHOLD_RAW,
88 OV_UPPER_THRESHOLD_WITH_HYST,
89 FAULT_PERSISTENCE_COUNT,
90 FAULT_PERSISTENCE_COUNT ) ;
91
92 InputCapture_CallbackRegister(Dev_Iso_Voltage_Sens_Callback);
93}
94
104void Dev_Iso_Voltage_Task_1ms(void) {
105
106 if (interrupt_counter < INTERRUPT_COUNTS_MIN || interrupt_counter > INTERRUPT_COUNTS_MAX)
107 dev_iso_voltage_data.no_signal_flag = 1;
108 else
109 dev_iso_voltage_data.no_signal_flag = 0;
110
111 interrupt_counter = 0;
112 //GPIO_1_Toggle();
113}
114
115
123void Dev_Reset_Average_Buffer() {
124 dev_iso_voltage_data.voltage_buffer_idx = 0;
125 dev_iso_voltage_data.buffer_full = 0;
126}
127
128
136uint16_t Dev_Get_Iso_Voltage_avg(void)
137{
138 if (dev_iso_voltage_data.buffer_full)
139 return Iso_Calculate_Average(dev_iso_voltage_data.voltage_buffer, MAX_NUM_SAMPLES_ISO_BUFFER);
140 else
141 return 0;
142}
143
144
151uint16_t Dev_Get_Iso_Voltage(void)
152{
153 return dev_iso_voltage_data.voltage_raw;
154}
155
156
157
164uint8_t Dev_Iso_Voltage_Is_OV(void)
165{
166 return dev_iso_voltage_data.over_voltage_flag;
167}
168
169
176uint8_t Dev_Iso_Voltage_Is_UV(void)
177{
178 return dev_iso_voltage_data.under_voltage_flag;
179}
180
181
185
202void Dev_Iso_Voltage_Sens_Callback() {
203 uint8_t i = 0;
204 static uint16_t vdd_sens_buff[CCP_INPUT_CAPTURE_BUFFER];
205
206 if (InputCapture_bufferoverflow)
207 {
208 i = 0;
209 while ((!InputCapture_BufferEmpty) || (i < CCP_INPUT_CAPTURE_BUFFER))
210 {
211 vdd_sens_buff[i++] = CCP_BUF_L;
212 }
213 dev_iso_voltage_data.voltage_raw = vdd_sens_buff[2] - vdd_sens_buff[1];
214 dev_iso_voltage_data.voltage_buffer[dev_iso_voltage_data.voltage_buffer_idx++] = dev_iso_voltage_data.voltage_raw ;
215
216 CCP_TIMER_HIGH_REG = 0;
217 CCP_TIMER_LOW_REG = 0;
218 }
219 CCP_INTERRUPT_FLAG = 0;
220
221 interrupt_counter++ ;
222
223 if (dev_iso_voltage_data.voltage_buffer_idx >= (MAX_NUM_SAMPLES_ISO_BUFFER - 1)) {
224 dev_iso_voltage_data.voltage_buffer_idx = 0;
225 dev_iso_voltage_data.buffer_full = 1;
226 }
227
228 if(FAULT_CheckMax(&Vin_Fault_Max, dev_iso_voltage_data.voltage_raw, NULL ))
229 {
230 dev_iso_voltage_data.over_voltage_flag = 1; //for overvoltage
231 }
232 else
233 {
234 dev_iso_voltage_data.over_voltage_flag = 0; //for overvoltage
235 }
236
237 if(FAULT_CheckMin(&Vin_Fault_Min, dev_iso_voltage_data.voltage_raw, NULL ))
238 {
239 dev_iso_voltage_data.under_voltage_flag = 1; //for overvoltage
240 }
241 else
242 {
243 dev_iso_voltage_data.under_voltage_flag = 0; //for overvoltage
244 }
245}
246
247
257uint16_t Iso_Calculate_Average(uint16_t * buffer, uint16_t length) {
258 uint16_t index = 0;
259 uint32_t sum = 0;
260 for (index = 0; index <length; index++)
261 sum +=buffer[index];
262 return (sum >> MAX_NUM_SAMPLES_ISO_BUFFER_AS_EXP_OF_TWO);
263}
264
FAULT_CheckMax
bool FAULT_CheckMax(FAULT_OBJ_T *faultInput, int16_t faultSource, FAULT_CALLBACK callback)
This function compares the fault source value with the threshold limits and sets (or) resets the faul...
Definition fault_common.c:126
FAULT_CheckMin
bool FAULT_CheckMin(FAULT_OBJ_T *faultInput, int16_t faultSource, FAULT_CALLBACK callback)
This function compares the fault source value with the threshold limits and sets (or) resets the faul...
Definition fault_common.c:184
FAULT_Init
void FAULT_Init(FAULT_OBJ_T *faultInput, int16_t threshold, int16_t hysLimit, uint16_t thresholdCnt, uint32_t hysCnt)
This function initializes the fault object data structure with the values that required to detect a f...
Definition fault_common.c:27
FAULT_OBJ_T
The Fault object data structure contains a list of elements/variables that are used to detect/check a...
Definition fault_common.h:23
dev_iso_voltage_data_t
a custom data type for isolated voltage measurement