-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhal.h
857 lines (767 loc) · 36.8 KB
/
hal.h
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
//#############################################################################
// $Copyright:
// Copyright (C) 2017-2023 Texas Instruments Incorporated - http://www.ti.com/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################
#ifndef HAL_DM_H
#define HAL_DM_H
// Contains public interface to various functions related to the HAL object
// **************************************************************************
// the includes
// platforms
#include "hal_obj.h"
//HAL HAL
//@{
#ifdef __cplusplus
extern "C" {
#endif
// **************************************************************************
// the defines
// Trip Zones all interrupt
#define HAL_TZ_INTERRUPT_ALL EPWM_TZ_INTERRUPT_DCBEVT2 \
+ EPWM_TZ_INTERRUPT_DCBEVT1 \
+ EPWM_TZ_INTERRUPT_DCAEVT2 \
+ EPWM_TZ_INTERRUPT_DCAEVT1 \
+ EPWM_TZ_INTERRUPT_OST \
+ EPWM_TZ_INTERRUPT_CBC
// Defines the comparator number for current protection
#define HAL_NUM_CMPSS_CURRENT 3
#define MTR_1_PGA_GAIN PGA_GAIN_12
#define MTR_1_DAC_VALUE 2048
#define MTR_1_CMPSS_DACH_VALUE 2048+1024+512
#define MTR_1_CMPSS_DACL_VALUE 2048-1024-512
#define MTR_2_PGA_GAIN PGA_GAIN_12
#define MTR_2_DAC_VALUE 2048
#define MTR_2_CMPSS_DACH_VALUE 2048+1024+512
#define MTR_2_CMPSS_DACL_VALUE 2048-1024-512
#define MTR_1_TZ_SIGNAL EPWM_TZ_SIGNAL_OSHT2
#define MTR_1_XBAR_IMPUT XBAR_INPUT2
#define MTR_1_DCTRIPIN EPWM_DC_COMBINATIONAL_TRIPIN7 \
| EPWM_DC_COMBINATIONAL_TRIPIN8 \
| EPWM_DC_COMBINATIONAL_TRIPIN9
#define MTR_2_TZ_SIGNAL EPWM_TZ_SIGNAL_OSHT3
#define MTR_2_XBAR_IMPUT XBAR_INPUT3
#define MTR_2_DCTRIPIN EPWM_DC_COMBINATIONAL_TRIPIN10 \
| EPWM_DC_COMBINATIONAL_TRIPIN11 \
| EPWM_DC_COMBINATIONAL_TRIPIN12
// *********** MOTOR 1 ***********
// Defines the gpio for start command
#define M1_HAL_START_CMD_GPIO 25
// Defines the gpio for stop command
#define M1_HAL_STOP_CMD_GPIO 25
// Defines the gpio for the nFAULT of Power Module device
#define M1_HAL_PM_nFAULT_GPIO 40
// Defines the gpio for the OCTW of Power Module device
#define M1_HAL_PM_nOCTW_GPIO 40
// Defines the gpio for the SPI_CS of DRV device
#define M1_HAL_DRV_SPI_CS_GPIO 57
// Defines the gpio for the enable gate of DRV device
#define M1_HAL_DRV_EN_GATE_GPIO 13
// Defines the PWM deadband falling edge delay count (system clocks)
#define M1_HAL_PWM_DBFED_CNT 1
// Defines the PWM deadband rising edge delay count (system clocks)
#define M1_HAL_PWM_DBRED_CNT 1
// *********** MOTOR 2 ***********
// Defines the gpio for start command
#define M2_HAL_START_CMD_GPIO 32
// Defines the gpio for stop command
#define M2_HAL_STOP_CMD_GPIO 32
// Defines the gpio for the nFAULT of Power Module device
#define M2_HAL_PM_nFAULT_GPIO 29
// Defines the gpio for the OCTW of Power Module device
#define M2_HAL_PM_nOCTW_GPIO 29
// Defines the gpio for the SPI_CS of DRV device
#define M2_HAL_DRV_SPI_CS_GPIO 27
// Defines the gpio for the enable gate of DRV device
#define M2_HAL_DRV_EN_GATE_GPIO 28
// Defines the PWM deadband falling edge delay count (system clocks)
#define M2_HAL_PWM_DBFED_CNT 1
// Defines the PWM deadband rising edge delay count (system clocks)
#define M2_HAL_PWM_DBRED_CNT 1
// *********** LED ***********
// Defines the function to turn LEDs off
#define HAL_turnLEDOff HAL_setGPIOHigh
// Defines the function to turn LEDs on
#define HAL_turnLEDOn HAL_setGPIOLow
// Defines the function to toggle LEDs
#define HAL_toggleLED HAL_toggleGPIO
// **************************************************************************
// the typedefs
// Enumeration for the LED numbers
typedef enum
{
HAL_GPIO_LED1 = 23, // GPIO pin number for LaunchPad LED 4
HAL_GPIO_LED2 = 34, // GPIO pin number for LaunchPad LED 5
HAL_GPIO_LEDBOOSTXL1 = 25, // GPIO pin number for BOOSTXL #1 LED
HAL_GPIO_LEDBOOSTXL2 = 32, // GPIO pin number for BOOSTXL #2 LED
HAL_GPIO_ISR = 30, // GPIO pin number for ISR Executing Time
HAL_GPIO_BML = 31 // GPIO pin number for Main Loop Executing Time
} HAL_LEDNumber_e;
// Enumeration for the sensor types
typedef enum
{
HAL_SENSORTYPE_CURRENT = 0, // Enumeration for current sensor
HAL_SENSORTYPE_VOLTAGE = 1 // Enumeration for voltage sensor
} HAL_SensorType_e;
// Enumeration for the QEP setup
typedef enum
{
HAL_QEP_QEP1 = 0, // Select QEP1
HAL_QEP_QEP2 = 1 // Select QEP2
} HAL_QEPSelect_e;
// Enumeration for the CPU Timer
typedef enum
{
HAL_CPU_TIMER0 = 0, // Select CPU Timer0
HAL_CPU_TIMER1 = 1, // Select CPU Timer1
HAL_CPU_TIMER2 = 2, // Select CPU Timer2
} HAL_CPUTimerNum_e;
// Enumeration for the Motor numbers
typedef enum
{
HAL_MTR_1 = 0,
HAL_MTR_2 = 1
} HAL_MotorNum_e;
// **************************************************************************
// the globals
extern __interrupt void mainISR(void);
// CLA Tasks
extern __interrupt void task_initModules(void);
extern __interrupt void task_mainISR(void);
extern __interrupt void task_mainLoop(void);
extern __interrupt void cla1Task4(void);
extern __interrupt void cla1Task5(void);
extern __interrupt void cla1Task6(void);
extern __interrupt void cla1Task7(void);
extern __interrupt void cla1Task8(void);
extern __interrupt void cla_EST_run_BackgroundTask(void);
// **************************************************************************
// the function prototypes
// Acknowledges an interrupt from the ADC so that another ADC interrupt can
// happen again.
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] adcIntNum The interrupt number
static inline void HAL_ackADCInt(HAL_Handle handle,const ADC_IntNumber adcIntNum)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// clear the ADC interrupt flag
// RB2
ADC_clearInterruptStatus(obj->adcHandle[2], adcIntNum); // ADCC
// Acknowledge interrupt from PIE group 1
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP1);
return;
} // end of HAL_ackADCInt() function
// Executes calibration routines
// \details Values for offset and gain are programmed into OTP memory at
// the TI factory. This calls and internal function that programs
// these offsets and gains into the ADC registers.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_cal(HAL_Handle handle);
// Forces a CLA task
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] taskFlags The task to be forced
static inline void HAL_forceCLATasks(HAL_Handle handle, const uint16_t taskFlags)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// force task
CLA_forceTasks(obj->claHandle, taskFlags);
return;
} // end of HAL_forceCLATasks() function
// Gets the run status of the specified CLA task
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] taskNumber The task to check the run status on
static inline bool
HAL_getCLATaskRunStatus(HAL_Handle handle, const CLA_TaskNumber taskNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// return the run status of the specified task
return(CLA_getTaskRunStatus(obj->claHandle, taskNumber));
} // end of HAL_getCLATaskRunStatus() function
// Disables global interrupts
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_disableGlobalInts(HAL_Handle handle);
// Enables the ADC interrupts
// \details Enables the ADC interrupt in the PIE, and CPU. Enables the
// interrupt to be sent from the ADC peripheral.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableADCInts(HAL_Handle handle);
// Enables the ADC interrupts without CPU interrupts
// \details Enables the ADC interrupts to only trigger CLA, and without
// interrupting the CPU
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableADCIntsToTriggerCLA(HAL_Handle handle);
extern void HAL_enableSCIInts(HAL_Handle handle);
// Enables the debug interrupt
// \details The debug interrupt is used for the real-time debugger. It is
// not needed if the real-time debugger is not used. Clears
// bit 1 of ST1.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableDebugInt(HAL_Handle handle);
// Enables the 8320/8301 device
// \details Provides the correct timing to enable the drv8320
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableDRV(HAL_MTR_Handle handle);
// Enables global interrupts
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableGlobalInts(HAL_Handle handle);
// Gets the current scale factor
// \param[in] handle The hardware abstraction layer (HAL) handle
// \return The current scale factor
static inline float32_t HAL_getCurrentScaleFactor(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
return(obj->current_sf);
} // end of HAL_getCurrentScaleFactor() function
// Gets the PWM duty cycle times
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pDutyCycles A pointer to memory for the duty cycle durations
static inline void HAL_getDutyCycles(HAL_MTR_Handle handle,uint16_t *pDutyCycles)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
pDutyCycles[0] = EPWM_getCounterCompareValue(obj->pwmHandle[0],
EPWM_COUNTER_COMPARE_A);
pDutyCycles[1] = EPWM_getCounterCompareValue(obj->pwmHandle[1],
EPWM_COUNTER_COMPARE_A);
pDutyCycles[2] = EPWM_getCounterCompareValue(obj->pwmHandle[2],
EPWM_COUNTER_COMPARE_A);
return;
} // end of HAL_getDutyCycles() function
// Gets the number of current sensors
// \param[in] handle The hardware abstraction layer (HAL) handle
// \return The number of current sensors
static inline uint16_t HAL_getNumCurrentSensors(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
return(obj->numCurrentSensors);
} // end of HAL_getNumCurrentSensors() function
// Gets the number of voltage sensors
// \param[in] handle The hardware abstraction layer (HAL) handle
// \return The number of voltage sensors
static inline uint16_t HAL_getNumVoltageSensors(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
return(obj->numVoltageSensors);
} // end of HAL_getNumVoltageSensors() function
// Gets the pwm enable status
// \param[in] handle The hardware abstraction layer (HAL) handle
// \return The pwm enable
static inline bool HAL_getPwmEnableStatus(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
return(obj->flagEnablePWM);
} // end of HAL_getPwmStatus() function
// Gets the voltage scale factor
// \param[in] handle The hardware abstraction layer (HAL) handle
// \return The voltage scale factor
static inline float32_t HAL_getVoltageScaleFactor(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
return(obj->voltage_sf);
} // end of HAL_getVoltageScaleFactor() function
// Configures the fault protection logic
// \details Sets up the trip zone inputs so that when a comparator
// signal from outside the micro-controller trips a fault,
// the EPWM peripheral blocks will force the
// power switches into a high impedance state.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupFaults(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
// Initializes the hardware abstraction layer (HAL) object
// \details Initializes all handles to the microcontroller peripherals.
// Returns a handle to the HAL object.
// \param[in] pMemory A pointer to the memory for the hardware abstraction layer object
// \param[in] numBytes The number of bytes allocated for the hardware abstraction layer object, bytes
// \return The hardware abstraction layer (HAL) object handle
extern HAL_Handle HAL_init(void *pMemory,const size_t numBytes);
// Initializes the hardware abstraction layer (HAL) object
// \details Initializes all handles to the microcontroller peripherals.
// Returns a handle to the HAL_MTR object.
// \param[in] pMemory A pointer to the memory for the hardware abstraction layer object
// \param[in] numBytes The number of bytes allocated for the hardware abstraction layer object, bytes
// \return The hardware abstraction layer (HAL_MTR) object handle
extern HAL_MTR_Handle HAL_MTR_init(void *pMemory,
const size_t numBytes,
const HAL_MotorNum_e motorNum);
// Initializes the interrupt vector table
// \details Points the ISR to the function mainISR.
// \param[in] handle The hardware abstraction layer (HAL) handle
static inline void HAL_initIntVectorTable(HAL_Handle handle)
{
Interrupt_register(INT_ADCC1, &mainISR);
return;
} // end of HAL_initIntVectorTable() function
// Reads the ADC data with offset
// \details Reads in the ADC result registers and scales the values
// according to the settings in user_m1.h or user_m2.h.
// The structure gAdcData holds three phase voltages,
// three line currents, and one DC bus voltage.
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pADCData A pointer to the ADC data buffer
static inline void HAL_readADCDataWithOffsets(HAL_Handle handle,
HAL_MTR_Handle mtrHandle,
HAL_ADCData_t *pADCData,
const HAL_MotorNum_e motorNum)
{
HAL_Obj *obj = (HAL_Obj *)handle;
float32_t current_sf = -HAL_getCurrentScaleFactor(mtrHandle);
float32_t voltage_sf = HAL_getVoltageScaleFactor(mtrHandle);
if(motorNum == HAL_MTR_1)
{
pADCData->I_A.value[0] = ((float32_t)ADC_readResult(obj->adcResult[0], ADC_SOC_NUMBER0)) * current_sf; // I_A = PGA5_OUT -> A14
pADCData->I_A.value[1] = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER0)) * current_sf; // I_B = PGA3_OUT -> B10/C7
pADCData->I_A.value[2] = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER0)) * current_sf; // I_C = PGA1_OUT -> A11/B7
pADCData->V_V.value[0] = ((float32_t)ADC_readResult(obj->adcResult[0], ADC_SOC_NUMBER1)) * voltage_sf; // V_A = A5
pADCData->V_V.value[1] = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER1)) * voltage_sf; // V_B = B0
pADCData->V_V.value[2] = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER1)) * voltage_sf; // V_C = C2
pADCData->dcBus_V = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER2)) * voltage_sf; // V_DC = B1
}
else if(motorNum == HAL_MTR_2)
{
pADCData->I_A.value[0] = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER3)) * current_sf; // I_A = PGA2_OUT -> A12/B9
pADCData->I_A.value[1] = ((float32_t)ADC_readResult(obj->adcResult[0], ADC_SOC_NUMBER2)) * current_sf; // I_B = PGA6_OUT -> A15
pADCData->I_A.value[2] = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER2)) * current_sf; // I_C = PGA4_OUT -> B11/C9
pADCData->V_V.value[0] = ((float32_t)ADC_readResult(obj->adcResult[0], ADC_SOC_NUMBER3)) * voltage_sf; // V_A = A6
pADCData->V_V.value[1] = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER4)) * voltage_sf; // V_B = B6
pADCData->V_V.value[2] = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER3)) * voltage_sf; // V_C = C14
pADCData->dcBus_V = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER4)) * voltage_sf; // V_DC = C1
}
return;
} // end of HAL_readADCDataWithOffsets() function
// Reads the ADC data without offsets
// \details Reads in the ADC result registers and scales the values
// according to the settings in user_m1.h or user_m2.h.
// The structure gAdcData holds three phase voltages,
// three line currents, and one DC bus voltage.
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pADCData A pointer to the ADC data buffer
static inline void HAL_readADCDataWithoutOffsets(HAL_Handle handle,
HAL_MTR_Handle mtrHandle,
HAL_ADCData_t *pADCData)
{
HAL_Obj *obj = (HAL_Obj *)handle;
float32_t current_sf = -HAL_getCurrentScaleFactor(mtrHandle);
float32_t voltage_sf = HAL_getVoltageScaleFactor(mtrHandle);
pADCData->I_A.value[0] = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER0)) * current_sf;
pADCData->I_A.value[1] = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER0)) * current_sf;
pADCData->I_A.value[2] = ((float32_t)ADC_readResult(obj->adcResult[0], ADC_SOC_NUMBER0)) * current_sf;
pADCData->V_V.value[0] = ((float32_t)ADC_readResult(obj->adcResult[2], ADC_SOC_NUMBER1)) * voltage_sf;
pADCData->V_V.value[1] = ((float32_t)ADC_readResult(obj->adcResult[0], ADC_SOC_NUMBER1)) * voltage_sf;
pADCData->V_V.value[2] = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER1)) * voltage_sf;
pADCData->dcBus_V = ((float32_t)ADC_readResult(obj->adcResult[1], ADC_SOC_NUMBER2)) * voltage_sf;
return;
} // end of HAL_readADCDataWithOffsets() function
// Reads the timer count
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] timerNumber The timer number, 0,1 or 2
// \return The timer count
static inline uint32_t
HAL_readTimerCnt(HAL_Handle handle,const uint16_t timerNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
uint32_t timerCnt = CPUTimer_getTimerCount(obj->timerHandle[timerNumber]);
return(timerCnt);
} // end of HAL_readTimerCnt() function
// Sets the GPIO pin high
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] gpioNumber The GPIO number
static inline void HAL_setGPIOHigh(HAL_Handle handle,const uint32_t gpioNumber)
{
// set GPIO high
GPIO_writePin(gpioNumber, 1);
return;
} // end of HAL_setGPIOHigh() function
// Read the GPIO pin
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] gpioNumber The GPIO number
// \return The GPIO pin
static inline uint32_t
HAL_readGPIOData(HAL_Handle handle,const uint32_t gpioNumber)
{
uint32_t gpioPinData;
// set GPIO high
gpioPinData = GPIO_readPin(gpioNumber);
return(gpioPinData);
} // end of HAL_readGPIOData() function
// Sets the GPIO pin low
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] gpioNumber The GPIO number
static inline void HAL_setGPIOLow(HAL_Handle handle,const uint32_t gpioNumber)
{
// set GPIO low
GPIO_writePin(gpioNumber, 0);
return;
} // end of HAL_setGPIOLow() function
// Sets the value of the internal DAC of the high comparator
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] cmpssNumber The CMPSS number
// \param[in] dacValue The DAC value of the high comparator
static inline void HAL_setCMPSSDACValueHigh(HAL_MTR_Handle handle,
const uint16_t cmpssNumber, uint16_t dacValue)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
// set GPIO low
CMPSS_setDACValueHigh(obj->cmpssHandle[cmpssNumber], dacValue);
return;
} // end of HAL_setCMPSSDACValueHigh() function
// Sets the value of the internal DAC of the low comparator
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] cmpssNumber The CMPSS number
// \param[in] dacValue The DAC value of the low comparator
static inline void HAL_setCMPSSDACValueLow(HAL_MTR_Handle handle,
const uint16_t cmpssNumber, uint16_t dacValue)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
// set GPIO low
CMPSS_setDACValueLow(obj->cmpssHandle[cmpssNumber], dacValue);
return;
} // end of HAL_setCMPSSDACValueLow() function
// Sets the number of voltage sensors
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] numVoltageSensors The number of voltage sensors
static inline void HAL_setNumVoltageSensors(HAL_MTR_Handle handle,const uint16_t numVoltageSensors)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
obj->numVoltageSensors = numVoltageSensors;
return;
} // end of HAL_setNumVoltageSensors() function
// Sets the number of current sensors
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] numCurrentSensors The number of current sensors
static inline void HAL_setNumCurrentSensors(HAL_MTR_Handle handle,const uint16_t numCurrentSensors)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
obj->numCurrentSensors = numCurrentSensors;
return;
} // end of HAL_setNumCurrentSensors() function
// Sets the hardware abstraction layer parameters
// \details Sets up the microcontroller peripherals. Creates all of the scale
// factors for the ADC voltage and current conversions. Sets the initial
// offset values for voltage and current measurements.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setParams(HAL_Handle handle);
// Sets the hardware abstraction layer parameters
// \details Sets up the microcontroller peripherals. Creates all of the scale
// factors for the ADC voltage and current conversions. Sets the initial
// offset values for voltage and current measurements.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_MTR_setParams(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
// Sets up the ADCs (Analog to Digital Converters)
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupADCs(HAL_Handle handle);
// Sets up the ADCs (Analog to Digital Converters)
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_runADCZeroOffsetCalibration(uint32_t base);
// Sets up the PGAs (Programmable Gain Amplifiers)
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupPGAs(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
// Sets up the CMPSSs (Comparator Subsystems)
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupCMPSSs(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
// Sets up the DACs (Buffered Digital-to-Analog Converter)
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupDACs(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
// Sets up the clocks
// \details Sets up the micro-controller's main oscillator
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupClks(HAL_Handle handle);
// Sets up the GATE object
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupGate(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
// Sets up the GPIO (General Purpose I/O) pins
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupGPIOs(HAL_Handle handle);
// Sets up the FLASH.
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupFlash(HAL_Handle handle);
// Sets up the CLA
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupCLA(HAL_Handle handle);
// Sets up the peripheral clocks
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupPeripheralClks(HAL_Handle handle);
// Sets up the PIE (Peripheral Interrupt Expansion)
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupPIE(HAL_Handle handle);
// Sets up the PWMs (Pulse Width Modulators)
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] systemFreq_MHz The system frequency, MHz
extern void HAL_setupPWMDACs(HAL_Handle handle,
const float32_t systemFreq_MHz);
// Sets up the QEP peripheral
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupQEP(HAL_MTR_Handle handle);
// Sets up the SCIA
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupSCIA(HAL_Handle handle);
// Sets up the SPI
// \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupSPI(HAL_MTR_Handle handle);
// Sets up the timers
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] systemFreq_MHz The system frequency, MHz
extern void HAL_setupTimers(HAL_Handle handle,const float32_t systemFreq_MHz);
// Sets up the timers
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] cpuTimerNumber The CPU timer number
bool HAL_getTimerStatus(HAL_Handle halHandle, const uint16_t cpuTimerNumber);
// Sets up the timers
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] cpuTimerNumber The CPU timer number
void HAL_clearTimerFlag(HAL_Handle halHandle, const uint16_t cpuTimerNumber);
// Sets up the DMA for datalog
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] dmaChNumber The DMC Channel Number
// \param[in] dlogDestAddr The Datalog buffer dest address
// \param[in] dlogSrcAddr The Datalog buffer src address
void HAL_setupDlogWithDMA(HAL_Handle handle, const uint16_t dmaChNumber,
const void *dlogDestAddr, const void *dlogSrcAddr);
// reset the DMA for datalog
static inline void HAL_resetDlogWithDMA(void)
{
DMA_initController();
return;
}
// Force trig the DMA channel for datalog
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] dmaChNumber The DMC Channel Number
static inline void HAL_trigDlogWithDMA(HAL_Handle handle, const uint16_t DMAChannel)
{
HAL_Obj *obj = (HAL_Obj *)handle;
DMA_startChannel(obj->dmaChHandle[DMAChannel]);
DMA_forceTrigger(obj->dmaChHandle[DMAChannel]);
return;
} // end of HAL_trigDlogWithDMA() function
// Toggles the GPIO pin
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] gpioNumber The GPIO number
static inline void HAL_toggleGPIO(HAL_Handle handle,const uint32_t gpioNumber)
{
// set GPIO high
GPIO_togglePin(gpioNumber);
return;
} // end of HAL_toggleGPIO() function
// Writes DAC data to the PWM comparators for DAC output
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pPWMDACData The pointer to the DAC data
static inline void HAL_writePWMDACData(HAL_Handle handle, HAL_PWMDACData_t *pPWMDACData)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// convert values from float to unit16
uint16_t cnt;
float32_t period;
float32_t dacData_sat_dc;
float32_t dacData;
float32_t value;
period = (float32_t)pPWMDACData->periodMax;
for(cnt=0;cnt<4;cnt++)
{
dacData = (*pPWMDACData->ptrData[cnt]);
dacData_sat_dc = dacData * pPWMDACData->gain[cnt] +
pPWMDACData->offset[cnt];
value = dacData_sat_dc * period;
pPWMDACData->cmpValue[cnt] = (int16_t)MATH_sat(value, period, 0);
}
// write the DAC data
if(obj->pwmDACHandle[PWMDAC_NUMBER_1])
{
// write the PWM data value
EPWM_setCounterCompareValue(obj->pwmDACHandle[PWMDAC_NUMBER_1],
EPWM_COUNTER_COMPARE_A,
pPWMDACData->cmpValue[0]);
}
if(obj->pwmDACHandle[PWMDAC_NUMBER_2])
{
// write the PWM data value
EPWM_setCounterCompareValue(obj->pwmDACHandle[PWMDAC_NUMBER_2],
EPWM_COUNTER_COMPARE_B,
pPWMDACData->cmpValue[1]);
}
if(obj->pwmDACHandle[PWMDAC_NUMBER_3])
{
// write the PWM data value
EPWM_setCounterCompareValue(obj->pwmDACHandle[PWMDAC_NUMBER_3],
EPWM_COUNTER_COMPARE_A,
pPWMDACData->cmpValue[2]);
}
if(obj->pwmDACHandle[PWMDAC_NUMBER_4])
{
// write the PWM data value
EPWM_setCounterCompareValue(obj->pwmDACHandle[PWMDAC_NUMBER_4],
EPWM_COUNTER_COMPARE_A,
pPWMDACData->cmpValue[3]);
}
return;
} // end of HAL_writeDacData() function
// Writes DAC data to the PWM comparators for DAC output
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pPWMDACData The pointer to the DAC data
void HAL_setPWMDACParameters(HAL_Handle handle, HAL_PWMDACData_t *pPWMDACData);
// \brief
// \param[in]
// \param[in]
void HAL_clearDataRAM(void *pMemory, uint16_t lengthMemory);
// Reads PWM period register
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pwmNumber The PWM number
// \return The PWM period value
static inline uint16_t
HAL_readPWMPeriod(HAL_MTR_Handle handle,const uint16_t pwmNumber)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
// the period value to be returned
uint16_t pwmPeriodValue;
pwmPeriodValue = EPWM_getTimeBasePeriod(obj->pwmHandle[pwmNumber]);
return(pwmPeriodValue);
} // end of HAL_readPWMPeriod() function
// Writes PWM data to the PWM comparators for motor control
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] pPWMData The pointer to the PWM data
static inline void HAL_writePWMData(HAL_MTR_Handle handle,const HAL_PWMData_t *pPWMData)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
uint16_t pwmCnt;
for(pwmCnt=0;pwmCnt<3;pwmCnt++)
{
// compute the value
float32_t period =
(float32_t)(EPWM_getTimeBasePeriod(obj->pwmHandle[pwmCnt]));
float32_t V_pu = -pPWMData->Vabc_pu.value[pwmCnt]; // Negative
float32_t V_sat_pu = MATH_sat(V_pu,0.5,-0.5); // -0.5~0.5
float32_t V_sat_dc_pu = V_sat_pu + 0.5; // 0~1.0
int16_t pwmValue = (int16_t)(V_sat_dc_pu * period); //
// write the PWM data value
EPWM_setCounterCompareValue(obj->pwmHandle[pwmCnt],
EPWM_COUNTER_COMPARE_A,
pwmValue);
}
return;
} // end of HAL_writePWMData() function
// Enables the PWM devices
// \details Turns on the outputs of the EPWM peripheral which will allow
// the power switches to be controlled.
// \param[in] handle The hardware abstraction layer (HAL) handle
static inline void HAL_enablePWM(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
EPWM_clearTripZoneFlag(obj->pwmHandle[0], HAL_TZ_INTERRUPT_ALL);
EPWM_clearTripZoneFlag(obj->pwmHandle[1], HAL_TZ_INTERRUPT_ALL);
EPWM_clearTripZoneFlag(obj->pwmHandle[2], HAL_TZ_INTERRUPT_ALL);
obj->flagEnablePWM = true;
return;
} // end of HAL_enablePWM() function
// Disables the PWM device
// \details Turns off the outputs of the EPWM peripherals which will put
// the power switches into a high impedance state.
// \param[in] handle The hardware abstraction layer (HAL) handle
static inline void HAL_disablePWM(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
EPWM_forceTripZoneEvent(obj->pwmHandle[0], EPWM_TZ_FORCE_EVENT_OST);
EPWM_forceTripZoneEvent(obj->pwmHandle[1], EPWM_TZ_FORCE_EVENT_OST);
EPWM_forceTripZoneEvent(obj->pwmHandle[2], EPWM_TZ_FORCE_EVENT_OST);
obj->flagEnablePWM = false;
return;
} // end of HAL_disablePWM() function
// Sets up the PWMs (Pulse Width Modulators)
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] systemFreq_MHz The system frequency, MHz
// \param[in] pwmPeriod_usec The PWM period, usec
// \param[in] numPWMTicksPerISRTick The number of PWM clock ticks per ISR clock tick
extern void HAL_setupPWMs(HAL_MTR_Handle handle,
const HAL_MotorNum_e motorNum);
#ifdef DRV8320_SPI
// Writes data to the driver
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] drv8320SPIVars SPI variables
void HAL_writeDRVData(HAL_MTR_Handle handle, DRV8320_SPIVars_t *drv8320SPIVars);
// Reads data from the driver
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] drv8320SPIVars SPI variables
void HAL_readDRVData(HAL_MTR_Handle handle, DRV8320_SPIVars_t *drv8320SPIVars);
// Sets up the SPI interface for the driver
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] drv8320SPIVars SPI variables
extern void HAL_setupDRVSPI(HAL_MTR_Handle handle,
DRV8320_SPIVars_t *drv8320SPIVars);
#endif // DRV8320_SPI
#ifdef DRV8301_SPI
// Writes data to the driver
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] Spi_8301_Vars SPI variables
void HAL_writeDRVData(HAL_MTR_Handle handle, DRV8301_SPIVars_t *Spi_8301_Vars);
// Reads data from the driver
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] Spi_8301_Vars SPI variables
void HAL_readDRVData(HAL_MTR_Handle handle, DRV8301_SPIVars_t *Spi_8301_Vars);
// Sets up the SPI interface for the driver
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] Spi_8301_Vars SPI variables
extern void HAL_setupDRVSPI(HAL_MTR_Handle handle, DRV8301_SPIVars_t *Spi_8301_Vars);
#endif
// Sets the current scale factor in the hal
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] current_sf The current scale factor
static inline void HAL_setCurrentScaleFactor(HAL_MTR_Handle handle, const float32_t current_sf)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
obj->current_sf = current_sf;
return;
} // end of HAL_setCurrentScaleFactor() function
// Sets the voltage scale factor in the hal
// \param[in] handle The hardware abstraction layer (HAL) handle
// \param[in] voltage_sf The voltage scale factor
static inline void HAL_setVoltageScaleFactor(HAL_MTR_Handle handle, const float32_t voltage_sf)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
obj->voltage_sf = voltage_sf;
return;
} // end of HAL_setVoltageScaleFactor() function
static inline uint16_t HAL_getTripFaults(HAL_MTR_Handle handle)
{
HAL_MTR_Obj *obj = (HAL_MTR_Obj *)handle;
uint16_t tripFault = 0;
tripFault = (EPWM_getTripZoneFlagStatus(obj->pwmHandle[0]) &
(EPWM_TZ_FLAG_OST | EPWM_TZ_FLAG_DCAEVT1 | EPWM_TZ_FLAG_DCAEVT2)) |
(EPWM_getTripZoneFlagStatus(obj->pwmHandle[1]) &
(EPWM_TZ_FLAG_OST | EPWM_TZ_FLAG_DCAEVT1 | EPWM_TZ_FLAG_DCAEVT2)) |
(EPWM_getTripZoneFlagStatus(obj->pwmHandle[2]) &
(EPWM_TZ_FLAG_OST | EPWM_TZ_FLAG_DCAEVT1 | EPWM_TZ_FLAG_DCAEVT2));
return(tripFault);
}
#ifdef __cplusplus
}
#endif // extern "C"
//@} // ingroup
#endif // end of HAL_DM_H definition