-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.c
576 lines (506 loc) · 15.6 KB
/
application.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* application.c
*
* Created on: 09.05.2019
* Author: tinivella
*/
#include <stdint.h>
#include "application.h"
/*
* Private functions
*/
/*
* Initialization functions
*/
static int32_t application_initSerial(void);
/*
* Serial driver functions
*/
static int32_t application_processFrame(commSerialFrame_t *frame);
static int32_t application_getFrame(ringbuffer_t *rbuf, commSerialFrame_t *frame);
static int32_t application_putFrame(ringbuffer_t *rbuf, commSerialFrame_t *frame);
/*
* Miscellaneous functions
*/
/*
* Initialization sequence for the control module.
* Initialize all state machines and set them to startup mode.
*/
int32_t application_init(void)
{
extern uint32_t actual_duty_cnt;
extern uint32_t actual_pwm_period;
extern uint32_t actual_phdly_cnt;
extern uint32_t actual_deadtime_cnt;
int32_t err = NO_ERROR;
/* Initialize system monitor handler. */
if (err == NO_ERROR)
{
//here all the measurements are init
}
/* Initialize serial handler. */
if (err == NO_ERROR)
{
err = application_initSerial();
}
/* Initialize variable of application code. */
if (err == NO_ERROR)
{
//Here each FSM is init
actual_duty_cnt = EPWMx_INIT_CMPA;
actual_phdly_cnt = EPWMx_INIT_PHASE;
actual_pwm_period = EPWMx_INIT_PERIOD;
actual_deadtime_cnt = EPWMx_INIT_DEADBAND;
}
/* Error handler for the above */
if (err != NO_ERROR)
{
//application_errorHandler(err);
for (;;);
/* watchdog will bite here */
}
/* From here the global interrupts can get enabled. */
return err;
}
/*
* Initialize serial interface state machine
*/
static int32_t application_initSerial(void)
{
extern commSerialFrame_t serialFrame;
extern commFrameStates_e rxstate;
extern commFrameStates_e txstate;
rxstate = state_idle;
txstate = state_idle;
serialFrame.start = 0U;
serialFrame.command = echo;
serialFrame.checksum = 0U;
serialFrame.checksumFlag = 0U;
serialFrame.transferFlag = 0U;
serialFrame.processedFlag = 1U; /* prevent processing at startup */
return NO_ERROR;
}
/*
* Serial handler:
* - Receive serial data from SCI
* - Transmit serial data to SCI
* -
*/
int32_t application_serialHandler(void)
{
extern commSerialFrame_t serialFrame;
extern ringbuffer_t SCIATXBufferStruct;
extern ringbuffer_t SCIARXBufferStruct;
static int32_t status = 1;
static uint16_t foo = 0;
/* Receive data from SCI in interrupt routine*/
if (ringbuffer_length(&SCIARXBufferStruct) >= 8)
{
/* Process received data (if any) */
application_getFrame(&SCIARXBufferStruct, &serialFrame);
application_processFrame(&serialFrame);
/* Process data for transmitting (if any) */
application_putFrame(&SCIATXBufferStruct, &serialFrame);
}
/* Transmit data to SCI */
if (!ringbuffer_empty(&SCIATXBufferStruct) && (SciaRegs.SCICTL2.bit.TXRDY==1))
{
//TODO: post process
ringbuffer_get(&SCIATXBufferStruct, &foo);
SciaRegs.SCITXBUF=foo; // Send data
}
status = 0;
return status;
}
/*
* Frame processing
*
* Each frame has to be processede only once
*/
int32_t application_processFrame(commSerialFrame_t *frame)
{
extern uint32_t actual_duty_cnt;
extern uint32_t actual_pwm_period;
extern uint32_t actual_phdly_cnt;
extern uint32_t actual_deadtime_cnt;
extern uint32_t new_deadtime_ns;
extern float cla_Vref1;
extern uint32_t new_Vout;
uint32_t new_duty_cnt = 0;
uint32_t new_pwm_period = 0;
uint32_t new_phdly_cnt = 0;
uint32_t new_deadtime_cnt = 0;
array4 temp_4;
uint16_t data_offset = 0;
uint16_t command_offset = 0;
commSerialCommands_t command;
uint16_t * data_address_p;
/*
* Process a single command or loop for multi commands
*/
/* reset variables for multi command */
// data_offset = 0U;
// command_offset = 0U;
while(frame->processedFlag == 0U)
{
if (frame->checksumFlag == 1U)
{
/* Normal operation */
if (frame->length == 4U)
{
data_offset = 0;
command_offset = 0;
command = frame->command;
}
else
{
command = errorLength;
}
}
else
{
command = errorChecksum;
}
/* Set data address pointer to point to first address location */
data_address_p = (frame->data + data_offset);
/* Execute command */
switch(command)
{
case echo:
/* send back same frame */
break;
/*
******************************************************************
* General commands
******************************************************************
*/
case setFrequency:
#ifdef INVERTEDPOWER
new_pwm_period = (EPWM_A_INIT_PERIOD*25/data_address_p[0]);
EPwm1Regs.TBPRD = (uint16_t) new_pwm_period;
EPwm2Regs.TBPRD = (uint16_t) new_pwm_period;
new_duty_cnt = (actual_duty_cnt*new_pwm_period/actual_pwm_period);
EPwm1Regs.CMPB = (uint16_t)new_duty_cnt; // adjust duty for output EPWM1A
EPwm2Regs.CMPB = (uint16_t)new_duty_cnt; // adjust duty for output EPWM2A
new_phdly_cnt = new_pwm_period;
EPwm2Regs.TBPHS.half.TBPHS = new_phdly_cnt;
#else
new_pwm_period = (EPWM_A_INIT_PERIOD*25/data_address_p[0]);
EPwm1Regs.TBPRD = (uint16_t) new_pwm_period;
EPwm2Regs.TBPRD = (uint16_t) new_pwm_period;
new_duty_cnt = (actual_duty_cnt*new_pwm_period/actual_pwm_period);
EPwm1Regs.CMPA.half.CMPA = (uint16_t)new_duty_cnt; // adjust duty for output EPWM1A
EPwm2Regs.CMPA.half.CMPA = (uint16_t)new_duty_cnt; // adjust duty for output EPWM2A
new_phdly_cnt = new_pwm_period;
EPwm2Regs.TBPHS.half.TBPHS = new_phdly_cnt;
#endif
actual_duty_cnt = new_duty_cnt;
actual_pwm_period = new_pwm_period;
actual_phdly_cnt = new_phdly_cnt;
break;
case setDuty:
new_duty_cnt = (uint32_t)(data_address_p[0])*actual_pwm_period/100;
#ifdef INVERTEDPOWER
EPwm1Regs.CMPB = (uint16_t)new_duty_cnt;
EPwm2Regs.CMPB = (uint16_t)new_duty_cnt;
#else
EPwm1Regs.CMPA.half.CMPA = (uint16_t)new_duty_cnt;
EPwm2Regs.CMPA.half.CMPA = (uint16_t)new_duty_cnt;
#endif
actual_duty_cnt = new_duty_cnt;
break;
case setDeadtime:
new_deadtime_ns = ((uint32_t)data_address_p[0]+256*(uint32_t)data_address_p[1]);
new_deadtime_cnt = (uint32_t)( new_deadtime_ns / EPWM_CLK_PERIOD_NS);
EPwm1Regs.DBFED = (uint16_t)new_deadtime_cnt;
EPwm1Regs.DBRED = (uint16_t)new_deadtime_cnt;
EPwm2Regs.DBFED = (uint16_t)new_deadtime_cnt;; // FED = 20 TBCLKs
EPwm2Regs.DBRED = (uint16_t)new_deadtime_cnt;; // RED = 20 TBCLKs
actual_deadtime_cnt = new_deadtime_cnt;
break;
case setOutputVoltage:
new_Vout = (float)((uint32_t)data_address_p[0]+256*(uint32_t)data_address_p[1]);
break;
case enablePhaseAll:
device_setALLDriverEnable();
break;
case disablePhaseAll:
device_setALLDriverDisable();
break;
case enableULS:
device_driverEnableULS();
break;
case enableUHS:
device_driverEnableUHS();
break;
case enableVLS:
device_driverEnableVLS();
break;
case enableVHS:
device_driverEnableVHS();
break;
case disableULS:
device_driverDisableULS();
break;
case disableUHS:
device_driverDisableUHS();
break;
case disableVLS:
device_driverDisableVLS();
break;
case disableVHS:
device_driverDisableVHS();
break;
case disableV:
device_driverDisableVLS();
device_driverDisableVHS();
break;
case disableU:
device_driverDisableULS();
device_driverDisableUHS();
break;
/*
******************************************************************
* ERROR commands
******************************************************************
*/
case errorLength:
frame->command = errorLength;
frame->length = 4;
temp_4.data0 = 0U;
temp_4.data1 = 0U;
temp_4.data2 = 0U;
temp_4.data3 = 0U;
memcpy(frame->data, &temp_4, 4U);
break;
case errorChecksum:
frame->command = errorChecksum;
frame->length = 4;
temp_4.data0 = 0U;
temp_4.data1 = 0U;
temp_4.data2 = 0U;
temp_4.data3 = 0U;
memcpy(frame->data, &temp_4, 4U);
break;
default:
frame->command = multi;
frame->length = 4;
temp_4.data0 = 0U;
temp_4.data1 = 0U;
temp_4.data2 = 0U;
temp_4.data3 = 0U;
memcpy(frame->data, &temp_4, 4U);
break;
} /* end switch */
frame->start = COMM_START;
frame->transferFlag = 1U;
frame->processedFlag = 1U;
command_offset = 0U;
data_offset = 0U;
break; /* break while */
} /* end while */
return 0;
}
/*
* TBD
*/
int32_t application_getFrame(ringbuffer_t *rbuf, commSerialFrame_t *frame)
{
extern commFrameStates_e rxstate;
extern commFrameStates_e txstate;
static int16_t ret = 1;
static uint16_t temp, idx, checksum;
/* While there is data in the ringbuffer, populate frame. */
while(!ringbuffer_empty(rbuf))
{
switch(rxstate)
{
case state_idle:
/* get tail data of RX buffer */
ringbuffer_data(rbuf, &temp, rbuf->tail);
/* check if it is a start byte */
if (temp == COMM_START)
{
rxstate = state_start;
frame->checksumFlag = 0U;
frame->transferFlag = 0U;
frame->processedFlag = 0U;
checksum = 0U;
idx = 0;
}
else
{
/* error, no valid frame, throw data away*/
ringbuffer_get(rbuf, &temp);
}
break;
case state_start:
/* get start byte */
ringbuffer_get(rbuf, &temp);
frame->start = temp;
checksum ^= temp;
rxstate = state_command;
break;
case state_command:
/* get command byte */
ringbuffer_get(rbuf, &temp);
frame->command = (commSerialCommands_t)temp;
checksum ^= temp;
rxstate = state_length;
break;
case state_length:
/* get length byte */
ringbuffer_get(rbuf, &temp);
frame->length = temp;
checksum ^= temp;
rxstate = state_data;
if (frame->length == 0U)
{
rxstate = state_checksum;
}
if (frame->length > COMM_MAX_DATA_LENGTH)
{
/* Error! Data too long for buffer */
frame->command = errorLength;
rxstate = state_abort;
}
break;
case state_data:
while (idx<frame->length)
{
if (ringbuffer_empty(rbuf)) break;
ringbuffer_get(rbuf, &temp);
frame->data[idx] = temp;
checksum ^= temp;
idx++;
}
/* leave state only when all data was received */
if(idx >= (frame->length))
{
rxstate = state_checksum;
}
break;
case state_checksum:
/* get checksum byte */
ringbuffer_get(rbuf, &temp);
frame->checksum = temp;
if (checksum == frame->checksum)
{
frame->checksumFlag = 1U;
ret = 0;
}
else
{
frame->checksumFlag = 0U;
ret = 1;
}
rxstate = state_idle;
break;
case state_abort:
while (!ringbuffer_empty(rbuf))
{
ringbuffer_get(rbuf, &temp);
/* stop at frame start */
ringbuffer_data(rbuf, &temp, rbuf->tail);
if (temp == COMM_START) break;
}
rxstate = state_idle;
break;
}
/* Break while loop */
if (rxstate == state_idle)
{
break;
}
}
return ret;
}
/*
* Decode complete frame into ringbuffer.
*/
int32_t application_putFrame(ringbuffer_t *rbuf, commSerialFrame_t *frame)
{
extern commFrameStates_e rxstate;
extern commFrameStates_e txstate;
int32_t ret = 1;
uint16_t temp, idx, checksum;
if(rbuf && frame)
{
/* While there is data in the ringbuffer, populate frame. */
while(!ringbuffer_full(rbuf))
{
switch(txstate)
{
case state_idle:
/* check if frame is ready to transfer */
if ((frame->transferFlag == 1U))
{
txstate = state_start;
frame->transferFlag = 0U;
checksum = 0U;
idx = 0U;
}
break;
case state_start:
/* put start byte */
temp = frame->start;
checksum ^= temp;
ringbuffer_put(rbuf, temp);
txstate = state_command;
break;
case state_command:
/* put command byte */
temp = (uint16_t)frame->command;
checksum ^= temp;
ringbuffer_put(rbuf, temp);
txstate = state_length;
break;
case state_length:
/* put length byte */
temp = frame->length;
checksum ^= temp;
ringbuffer_put(rbuf, temp);
txstate = state_data;
if (frame->length == 0U)
{
txstate = state_checksum;
}
break;
case state_data:
while(idx < frame->length)
{
if (ringbuffer_full(rbuf)) break;
temp = frame->data[idx];
checksum ^= temp;
ringbuffer_put(rbuf, temp);
idx++;
}
if(idx >= (frame->length))
{
txstate = state_checksum;
}
break;
case state_checksum:
/* put checksum byte */
temp = checksum;
ringbuffer_put(rbuf, temp);
txstate = state_idle;
break;
default:
txstate = state_idle;
break;
}
/* Break while loop */
if (txstate == state_idle)
{
break;
}
}
ret = 0;
}
else /* Null pointer passed */
{
ret = 1;
}
return ret;
}