Bar Logo Dual Active Bridge Development Board (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
PBV_dab_frame_map.c
1
9#include <xc.h> // include processor files - each processor file is guarded.
10#include <stdint.h> // include standard integer data types
11
12#include <stdio.h>
13#include <string.h>
14//#include <stdlib.h>
15
16#include <stdbool.h> // include standard boolean data types
17#include <stddef.h> // include standard definition data types
18
19// MCC header files
20#include "system/pins.h"
21#include "device/device.h"
22
23// other header files
24#include "PBV_interface.h"
25#include "pwrctrl/pwrctrl_typedef.h"
27#include "pwrctrl/pwrctrl.h"
29#include "config/macros.h"
30#include "config/version.h"
31
32
36#define FIRMWARE_PROTOCOL_ID 0x1000
37#define PBV_TX_PROTOCOL_ID 0x404
38#define PBV_LOG_ID 0x300
39
40// command IDs, first data word in received package
41// use this to decide what action to take when data is received
42#define PBV_CMD_ID_DAB_ON_REV 0x5502
43#define PBV_CMD_ID_DAB_ON 0x5501
44#define PBV_CMD_ID_DAB_OFF 0x5500
45#define PBV_CMD_ID_FREQ_CHANGE 0xBBBB
46#define PBV_CMD_ID_FAN_SPEED 0xCCCC
47
48#define PBV_CMD_ID_VLOOPREV_REF_SET 0xDDDC
49#define PBV_CMD_ID_ILOOP_REF_SET 0xDDDD
50#define PBV_CMD_ID_VLOOP_REF_SET 0xDDDE
51#define PBV_CMD_ID_PLOOP_REF_SET 0xDDDF
52
53#define PBV_CMD_ID_PHASE_CHANGE 0xEE01
54#define PBV_CMD_ID_P2S_PHASE_TARGET 0xEE02
55
// end of pbv-protocol-ids
57
58// static because these are private.
59
60static PBV_Datatype_TX_t appPbvDabTx;
61static PBV_Datatype_RX_t appPbvDabRx;
62static PBV_Datatype_TX_t appPbvDabAscii;
63
64static PBV_Datatype_TX_t * appPbvDabTxPtr = &appPbvDabTx;
65static PBV_Datatype_RX_t * appPbvDabRxPtr = &appPbvDabRx;
66static PBV_Datatype_TX_t * appPbvDabAsciiPtr = &appPbvDabAscii;
67
68uint8_t bufferEightRx[64];
69uint16_t bufferSixteenRx[32];
70
71uint8_t bufferEightTx[64];
72uint16_t bufferSixteenTx[32];
73
74static uint8_t transmitFirmwareId = 1;
75
76
77
78
82void App_PBV_DAB_Build_Frame(void);
83void App_PBV_DAB_Process_Rx_Data(uint16_t * data);
84void protocolID(uint16_t protocol_ID, uint16_t length, uint8_t * data);
85
89
100{
101 appPbvDabTxPtr->PBV_Protcol_ID = PBV_TX_PROTOCOL_ID;
102 appPbvDabTxPtr->PBV_Signal_Ascii = PBV_SIGNAL_MODE;
103 appPbvDabTxPtr->PBV_Message_State = PBV_MESSAGE_INIT;
104 appPbvDabTxPtr->Length = 64;
105
107
108 appPbvDabAsciiPtr->PBV_Protcol_ID = FIRMWARE_PROTOCOL_ID;
109 appPbvDabAsciiPtr->PBV_Signal_Ascii = PBV_ASCII_MODE;
110 appPbvDabAsciiPtr->PBV_Message_State = PBV_MESSAGE_INIT;
111 appPbvDabAsciiPtr->Length = 64;
112
113 App_PBV_Init(appPbvDabTxPtr, appPbvDabAsciiPtr, appPbvDabRxPtr);
114}
115
116
126{
127 static uint32_t tickCounter = 0;
128 // RX handler
129 if (appPbvDabRxPtr->PBV_Message_State == PBV_MESSAGE_RECEIVED)
130 {
131 App_Read_Received_From_PBV(appPbvDabRxPtr);
132 protocolID(appPbvDabRxPtr->PBV_Protcol_ID, appPbvDabRxPtr->Length, appPbvDabRxPtr->Data_Buffer);
133
134 // msg read. Read another
135 App_Receive_From_PBV(appPbvDabRxPtr);
136 }
138 if (++tickCounter > 11)
139 {
140 App_PBV_DAB_Build_Frame();
141 App_Send_To_PBV(appPbvDabTxPtr);
142 tickCounter = 0;
143 }
144}
145
146
155{
156 static uint16_t OneSecCounter;//debug log print demo purpose. Sporadic Resets can be detected by checking this number
157 static uint8_t PBVBuffer[64<<1];//PBV msg buffer. Take care of 64B length boundary when creating messages.
158
159 for(uint16_t i=0; i<(64); i++) PBVBuffer[i]=0;//clear to 0 all 64 bytes
160
161 if (appPbvDabAsciiPtr->PBV_Protcol_ID == FIRMWARE_PROTOCOL_ID)
162 {
163 strcpy((char *)&PBVBuffer[0], (char *)FIRMWARE_VERSION_STRING);
164 strcpy((char *)&PBVBuffer[10], (char *)FIRMWARE_NAME);
165 appPbvDabAsciiPtr->Data_Buffer = &PBVBuffer[0];
166
167 App_Send_To_PBV(appPbvDabAsciiPtr);
168 appPbvDabAsciiPtr->PBV_Protcol_ID = PBV_LOG_ID;
169 transmitFirmwareId = 1;
170 return;
171 }
172
173
174 if (appPbvDabAsciiPtr->PBV_Protcol_ID == PBV_LOG_ID)
175 {
176 if (transmitFirmwareId) App_PBV_Re_Init(appPbvDabAsciiPtr);
177 transmitFirmwareId = 0;
178
179 if(OneSecCounter)
180 {
181 if(!(OneSecCounter%20))
182 {
183 // "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
184 sprintf((char *)&PBVBuffer[0], "\rDAB board heat sink Temperature is %d degree Celsius ", Dev_Temp_Get_Temperature_Celcius() );
185 }
186 else
187 sprintf((char *)&PBVBuffer[0], "\r Dual Active Bridge. 64B fixed length Log. MsgNo %d ", OneSecCounter);
188 }
189 else
190 {
191 sprintf((char *)&PBVBuffer[0], "\r Dual Active Bridge. AFTER RESET SYSTEM STARTUP " );
192 }
193
194 appPbvDabAsciiPtr->Data_Buffer =&PBVBuffer[0];
195 App_Send_To_PBV(appPbvDabAsciiPtr);//64B fixed frame
196
197 OneSecCounter++;
198 }
199}
200
201
209{
210 if (appPbvDabAsciiPtr ->PBV_Message_State == PBV_MESSAGE_TRANSMITTING)
211 {
212 return 0;
213 }
214 return appPbvDabAsciiPtr;
215}
216
217
221
229void App_PBV_DAB_Build_Frame()
230{
231 // Power Board Visualizer can only mask single bits
232 // so create maskable word that can be parsed by GUI by sending the
233 // "2 ^ (state)" instead of "state"
234 bufferSixteenTx[0] = 1<<(Dev_PwrCtrl_Get_State());
235
236 // send back one "flag word" which combines fault and status and enable control flag
237 uint16_t enabled = Dev_PwrCtrl_Get_EnableFlag();
238 uint16_t fault_flags = Fault_GetFlags();
239 uint16_t status_flags = Dev_PwrCtrl_Get_Status();
240 uint16_t PowDir_flags = Dev_PwrCtrl_Get_PowerDir();
241 uint16_t flag_word = enabled + ((status_flags & 0x0003)<<1) + (fault_flags<<3) + (PowDir_flags<<14) ;
242 bufferSixteenTx[1] = flag_word;
243 bufferSixteenTx[2] = Dev_PwrCtrl_GetAveraging_Vprim();
244 bufferSixteenTx[3] = Dev_PwrCtrl_GetAveraging_Vsec();
245 bufferSixteenTx[4] = PwrCtrl_GetAdc_Ipri_ct();
246 bufferSixteenTx[5] = PwrCtrl_GetAdc_Isec_ct();
247 bufferSixteenTx[6] = Dev_PwrCtrl_GetAveraging_Isec();
248 bufferSixteenTx[7] = (uint16_t)(TEMPERATURE_PBV_OFFSET_CELSIUS + (int16_t)Dev_Temp_Get_Temperature_Celcius());
249 bufferSixteenTx[8] = PwrCtrl_GetAdc_Vrail_5V();
250 bufferSixteenTx[9] = PwrCtrl_GetPhase_P2SDegree();
251 bufferSixteenTx[10] = 0;//devFanDataPtr->CurrentSpeedPercent;
252 bufferSixteenTx[11] = 0;//(uint16_t)(TEMPERATURE_PBV_OFFSET_CELSIUS + (int16_t)Dev_Temp_Get_Temperature_Celcius());
253 bufferSixteenTx[12] = Dev_PwrCtrl_Get_Period();
254 bufferSixteenTx[13] = Dev_PwrCtrl_Get_PwmprdTarget();
255 bufferSixteenTx[14] = Dev_PwrCtrl_Get_SecPower();
256
257 PBV_Change_from_Sixteen_to_Eight(bufferSixteenTx, bufferEightTx, 18);
258
259 appPbvDabTxPtr->Data_Buffer = bufferEightTx;
260 appPbvDabTxPtr->Length = 18 * 2 ;
261}
262
263
264
271void App_PBV_DAB_Process_Rx_Data(uint16_t * data)
272{
273 uint16_t cmd_id = data[0];
274 uint16_t control_word = data[1];
275 switch (cmd_id)
276 {
277 case PBV_CMD_ID_DAB_ON: {
278 //Dev_PwrCtrl_SetState(0);
280 break;
281 }
283 //Dev_PwrCtrl_SetState(0);
285 break;
286 }
287 case PBV_CMD_ID_DAB_OFF: {
290 break;
291 }
293 // change target frequency
294 if ((control_word <= MAX_PWM_PERIOD) && (control_word >= MIN_PWM_PERIOD)) {
295 PwrCtrl_SetPeriodTarget(control_word);
296 // when Frequency is changed, control phase will be zero
297 uint16_t controlPhase = 0;
298 PwrCtrl_SetPhaseTarget(controlPhase);
299 }
300 break;
301 }
303 if (control_word < 32767) //TODO: put in proper check here!
304 {
305 PwrCtrl_SetVPriReference(control_word);
306 }
307 break;
308 }
309
311 if (control_word < 32767) //TODO: put in proper check here!
312 {
313 PwrCtrl_SetIReference(control_word);
314 }
315 break;
316 }
318 if (control_word < 32767) //TODO: put in proper check here!
319 {
320 PwrCtrl_SetVSecReference(control_word);
321 }
322 break;
323 }
324
326 if (control_word < 32767) //TODO: put in proper check here!
327 {
328 PwrCtrl_SetPwrReference(control_word);
329 }
330 break;
331 }
332
334 // change target phase
335 #if(OPEN_LOOP_PBV == false)
336 uint16_t controlPhase = (uint16_t)(control_word);
337 PwrCtrl_SetP2SPhaseTarget(controlPhase);
338 #endif
339 break;
340 }
341
343 // change target phase
344 #if(OPEN_LOOP_PBV == true)
345 uint16_t controlPhase = (uint16_t)((control_word)* PHASE_180_SCALER * (Dev_PwrCtrl_Get_DutyCycle()));
346 PwrCtrl_SetPhaseTarget(controlPhase);
347 #endif
348 break;
349 }
350 case PBV_CMD_ID_FAN_SPEED: {
351 Dev_Fan_Set_Speed(control_word);
352 break;
353 }
354
355 default:
356 break;
357 }
358}
359
360
369void protocolID(uint16_t protocol_ID, uint16_t length, uint8_t * data)
370{
371 PBV_Change_from_Eight_to_Sixteen(data, bufferSixteenRx, length);
372 App_PBV_DAB_Process_Rx_Data(bufferSixteenRx);
373}
374
375
376
Contains h files that are part of device layer.
uint16_t Fault_GetFlags(void)
Contains API functions for fault protection.
Contains public functions relevant for power control communication interface.
#define MAX_PWM_PERIOD
This sets the switching period of the converter.
Definition macros.h:40
#define MIN_PWM_PERIOD
This sets the switching period of the converter.
Definition macros.h:41
#define TEMPERATURE_PBV_OFFSET_CELSIUS
To allow the PBV to support up to -40C, an offset was added.
Definition config.h:167
void PBV_Change_from_Sixteen_to_Eight(uint16_t *sixteenPtr, uint8_t *eightPtr, uint16_t length)
This function changes 16 bit data to 8 bit data.
void App_PBV_Init(PBV_Datatype_TX_t *boardToPbv, PBV_Datatype_TX_t *boardToPpvAscii, PBV_Datatype_RX_t *pbvToBoard)
Initializes the PBV init, by linking the application object pointers from the application to CAN or U...
void App_Send_To_PBV(PBV_Datatype_TX_t *ptr)
This function links the data from application object to the CAN or UART object and This function ch...
int App_Read_Received_From_PBV(PBV_Datatype_RX_t *ptr)
This function links the data received by CAN or UART objects to the application object.
void App_PBV_Re_Init(PBV_Datatype_TX_t *ptr)
Reinitializes the objects if any parameters( ID, length, etc) are to be changed to the CAN or UART ob...
void App_Receive_From_PBV(PBV_Datatype_RX_t *ptr)
This function changes the state of the application object. this acts as a trigger for the periodic ta...
void PBV_Change_from_Eight_to_Sixteen(uint8_t *eightPtr, uint16_t *sixteenPtr, uint16_t length)
This function changes 8 bit data to sixteen bit data.
@ PBV_MESSAGE_READY_TO_RECEIVE
Message Reception triggered.
@ PBV_MESSAGE_RECEIVED
Message Received.
@ PBV_ASCII_MODE
Object will transmit Ascii data.
@ PBV_SIGNAL_MODE
Object will transmit/receive signals.
@ PBV_MESSAGE_INIT
Init State. Only Send Messages in this State
@ PBV_MESSAGE_TRANSMITTING
Transmitting Message.
void App_PBV_DAB_Init()
This function initializes the local pbv objects. these objects are then passed on to the app_PBV_init...
#define PBV_CMD_ID_FAN_SPEED
set fan speed
#define PBV_CMD_ID_DAB_ON_REV
turn DAB on - REVERSED discharge mode
#define PBV_CMD_ID_PLOOP_REF_SET
set voltage loop reference
#define PBV_LOG_ID
ID on which log data is sent.
#define PBV_CMD_ID_PHASE_CHANGE
set control phase
void App_PBV_DAB_Task_10ms(void)
10ms PBV task to be execution
#define PBV_CMD_ID_DAB_ON
turn DAB on
#define PBV_CMD_ID_VLOOPREV_REF_SET
set voltage loop reference reverse mode
#define FIRMWARE_PROTOCOL_ID
Firmware ID.
#define PBV_TX_PROTOCOL_ID
ID on which data is transmitted by dsPIC33.
PBV_Datatype_TX_t * App_PB_DAB_Get_TX_ASCII_ptr(void)
This function can be used to send log messages from other files.
#define PBV_CMD_ID_VLOOP_REF_SET
set voltage loop reference
#define PBV_CMD_ID_P2S_PHASE_TARGET
set control phase
#define PBV_CMD_ID_DAB_OFF
turn DAB off
#define PBV_CMD_ID_FREQ_CHANGE
change DAB switching frequency
void App_PBV_DAB_Task_1s(void)
1 second PBV task to be execution
#define PBV_CMD_ID_ILOOP_REF_SET
set current loop reference
void protocolID(uint16_t protocol_ID, uint16_t length, uint8_t *data)
default callback
void App_PBV_DAB_Process_Rx_Data(uint16_t *data)
process received data
void Dev_Fan_Set_Speed(uint8_t target_speed_percent)
Sets the fan speed.
Definition dev_fan.c:159
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
uint16_t Dev_PwrCtrl_Get_State(void)
API function to get the current state of the state machine.
uint16_t Dev_PwrCtrl_GetAveraging_Isec(void)
API function to get the average value of the sampled ADC for secondary current.
uint16_t Dev_PwrCtrl_Get_PowerDir(void)
API function to get the direction of configure power flow.
uint16_t Dev_PwrCtrl_GetAveraging_Vsec(void)
API function to get the average value of the sampled ADC for secondary voltage.
void PwrCtrl_SetPwrReference(uint16_t reference)
API function to set the power controller reference.
uint16_t PwrCtrl_GetPhase_P2SDegree(void)
API function to set the power control State.
uint16_t PwrCtrl_GetAdc_Ipri_ct(void)
API function to get the raw ADC value for primary current transformer current.
uint16_t Dev_PwrCtrl_Get_Period(void)
API function to get the PWM period.
uint16_t Dev_PwrCtrl_GetAveraging_Vprim(void)
API function to get the average value of the sampled ADC for primary voltage.
void PwrCtrl_SetIReference(uint16_t reference)
API function to set the current controller reference.
void PwrCtrl_SetPeriodTarget(uint16_t reference)
API function to set the target period.
uint16_t Dev_PwrCtrl_Get_EnableFlag(void)
API function to get the state of the power control enable bit.
void PwrCtrl_SetP2SPhaseTarget(uint16_t reference)
API function to set the target prim to sec phase which will steer other modulation.
void PwrCtrl_SetVSecReference(uint16_t reference)
API function to set the secondary voltage controller reference.
uint16_t Dev_PwrCtrl_Get_Status(void)
API function to get the power control status.
uint16_t PwrCtrl_GetAdc_Isec_ct(void)
API function to get the raw ADC value for secondary current transformer current.
uint16_t Dev_PwrCtrl_Get_DutyCycle(void)
API function to get the PWM duty cycle.
void PwrCtrl_SetVPriReference(uint16_t reference)
API function to set the primary voltage controller reference, in reverse mode.
uint16_t Dev_PwrCtrl_Get_PwmprdTarget(void)
API function to get the PWM period target.
void PwrCtrl_SetEnable(PWR_CTRL_CHARGE_STATE_t setVal)
API function to set the power control state forward, reverse or stop.
void Dev_PwrCtrl_SetState(uint16_t reference)
API function to set the power control State.
void PwrCtrl_SetPhaseTarget(uint16_t reference)
API function to set the target PWM phase.
uint16_t PwrCtrl_GetAdc_Vrail_5V(void)
API function to get the raw ADC value for 5V rail.
uint16_t Dev_PwrCtrl_Get_SecPower(void)
API function to get the secondary power.
@ PWR_CTRL_CHARGING
power converter is in charging mode
@ PWR_CTRL_DISCHARGING
power converter is in discharging mode
@ PWR_CTRL_STOP
uint32_t PBV_Protcol_ID
Protocol ID.
uint8_t * Data_Buffer
Pointer to the data to be transmitted.
enum PBV_SIGNAL_ASCII_MODE PBV_Signal_Ascii
Object will transmit Signals or ASCII.
PBV_MESSAGE_TX_STATE_t PBV_Message_State
State of the TX object.
uint16_t Length
Length of transmitted message in Bytes.
uint32_t PBV_Protcol_ID
Protocol ID of the received message.
PBV_MESSAGE_RX_STATE_t PBV_Message_State
State of the received message.
uint8_t * Data_Buffer
Received Data pointer.
uint16_t Length
Length of received message in Bytes.