-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhal.h
1240 lines (920 loc) · 41.5 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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _HAL_H_
#define _HAL_H_
/* --COPYRIGHT--,BSD
* Copyright (c) 2012, Texas Instruments Incorporated
* All rights reserved.
*
* 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.
* --/COPYRIGHT--*/
//! \file solutions/instaspin_foc/boards/boostxldrv8301_revB/f28x/f2802xF/src/hal.h
//! \brief Contains public interface to various functions related
//! to the HAL object
//!
//! (C) Copyright 2011, Texas Instruments, Inc.
// **************************************************************************
// the includes
// modules
// platforms
#include "hal_obj.h"
//!
//!
//! \defgroup HAL HAL
//!
//@{
#ifdef __cplusplus
extern "C" {
#endif
// **************************************************************************
// the defines
//! \brief Defines LAUNCHPAD which is needed to blink LED
#define LAUNCHPAD
//! \brief Defines that a DRV8301 chip SPI port is used on the board.
#define DRV8301_SPI
#define Device_cal (void (*)(void))0x3D7C80
//! \brief Defines used in oscillator calibration functions
//! \brief Defines the scale factor for Q15 fixed point numbers (2^15)
#define FP_SCALE 32768
//! \brief Defines the quantity added to Q15 numbers before converting to integer to round the number
#define FP_ROUND FP_SCALE/2
//! \brief Defines the amount to add to Q16.15 fixed point number to shift from a fine trim range of
//! \brief (-31 to 31) to (1 to 63). This guarantees that the trim is positive and can
//! \brief therefore be efficiently rounded
#define OSC_POSTRIM 32
#define OSC_POSTRIM_OFF FP_SCALE*OSC_POSTRIM
//! \brief The following functions return reference values stored in OTP.
//! \brief Defines the slope used to compensate oscillator 1 (fine trim steps / ADC code). Stored in fixed point Q15 format
#define getOsc1FineTrimSlope() (*(int16_t (*)(void))0x3D7E90)()
//! \brief Defines the oscillator 1 fine trim at high temp
#define getOsc1FineTrimOffset() (*(int16_t (*)(void))0x3D7E93)()
//! \brief Defines the oscillator 1 coarse trim
#define getOsc1CoarseTrim() (*(int16_t (*)(void))0x3D7E96)()
//! \brief Defines the slope used to compensate oscillator 2 (fine trim steps / ADC code). Stored
//! \brief in fixed point Q15 format.
#define getOsc2FineTrimSlope() (*(int16_t (*)(void))0x3D7E99)()
//! \brief Defines the oscillator 2 fine trim at high temp
#define getOsc2FineTrimOffset() (*(int16_t (*)(void))0x3D7E9C)()
//! \brief Defines the oscillator 2 coarse trim
#define getOsc2CoarseTrim() (*(int16_t (*)(void))0x3D7E9F)()
//! \brief Defines the ADC reading of temperature sensor at reference temperature for compensation
#define getRefTempOffset() (*(int16_t (*)(void))0x3D7EA2)()
//! \brief Defines the PWM deadband falling edge delay count (system clocks)
//!
#define HAL_PWM_DBFED_CNT 1 //
//! \brief Defines the PWM deadband rising edge delay count (system clocks)
//!
#define HAL_PWM_DBRED_CNT 1 //
//! \brief Defines the function to turn LEDs off
//!
#define HAL_turnLedOff HAL_setGpioLow
//! \brief Defines the function to turn LEDs on
//!
#define HAL_turnLedOn HAL_setGpioHigh
//! \brief Defines the function to turn LEDs on
//!
#define HAL_toggleLed HAL_toggleGpio
// **************************************************************************
// the typedefs
typedef enum
{
HAL_Gpio_LED2=GPIO_Number_0 //!< GPIO pin number. LaunchPad uses PWM pins for LEDs
} HAL_LedNumber_e;
typedef enum
{
HAL_SensorType_Current=0, //!< Enumeration for current sensor
HAL_SensorType_Voltage //!< Enumeration for voltage sensor
} HAL_SensorType_e;
// **************************************************************************
// the globals
extern interrupt void mainISR(void);
extern interrupt void SCI_RX_ISR(void);
// **************************************************************************
// the function prototypes
//! \brief 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] intNumber The interrupt number
static inline void HAL_acqAdcInt(HAL_Handle handle,const ADC_IntNumber_e intNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// clear the ADC interrupt flag
ADC_clearIntFlag(obj->adcHandle,intNumber);
// Acknowledge interrupt from PIE group 10
PIE_clearInt(obj->pieHandle,PIE_GroupNumber_10);
return;
} // end of HAL_acqAdcInt() function
//! \brief Acknowledges an interrupt from the PWM so that another PWM interrupt can
//! happen again.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] pwmNumber The PWM number
static inline void HAL_acqPwmInt(HAL_Handle handle,const PWM_Number_e pwmNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// clear the PWM interrupt flag
PWM_clearIntFlag(obj->pwmHandle[pwmNumber]);
// clear the SOCA flag
PWM_clearSocAFlag(obj->pwmHandle[pwmNumber]);
// Acknowledge interrupt from PIE group 3
PIE_clearInt(obj->pieHandle,PIE_GroupNumber_3);
return;
} // end of HAL_acqPwmInt() function
//! \brief 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);
//! \brief Disables global interrupts
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_disableGlobalInts(HAL_Handle handle);
//! \brief Disables the watch dog
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_disableWdog(HAL_Handle handle);
//! \brief 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_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *)handle;
PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_1]);
PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_2]);
PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_3]);
return;
} // end of HAL_disablePwm() function
//! \brief 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);
//! \brief 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);
//! \brief Enables global interrupts
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableGlobalInts(HAL_Handle handle);
//! \brief Enables the 8301 device
//! \details Provides the correct timing to enable the drv8301
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enableDrv(HAL_Handle handle);
//! \brief 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_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *)handle;
PWM_clearOneShotTrip(obj->pwmHandle[PWM_Number_1]);
PWM_clearOneShotTrip(obj->pwmHandle[PWM_Number_2]);
PWM_clearOneShotTrip(obj->pwmHandle[PWM_Number_3]);
return;
} // end of HAL_enablePwm() function
//! \brief Enables the PWM interrupt
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_enablePwmInt(HAL_Handle handle);
//! \brief Gets the ADC delay value
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] socNumber The ADC SOC number
//! \return The ADC delay value
static inline ADC_SocSampleDelay_e HAL_getAdcSocSampleDelay(HAL_Handle handle,
const ADC_SocNumber_e socNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
return(ADC_getSocSampleDelay(obj->adcHandle,socNumber));
} // end of HAL_getAdcSocSampleDelay() function
//! \brief Gets the ADC bias value
//! \details The ADC bias contains the feedback circuit's offset and bias.
//! Bias is the mathematical offset used when a bi-polar signal
//! is read into a uni-polar ADC.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \return The ADC bias value
static inline _iq HAL_getBias(HAL_Handle handle,
const HAL_SensorType_e sensorType,
uint_least8_t sensorNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
_iq bias = _IQ(0.0);
if(sensorType == HAL_SensorType_Current)
{
bias = obj->adcBias.I.value[sensorNumber];
}
else if(sensorType == HAL_SensorType_Voltage)
{
bias = obj->adcBias.V.value[sensorNumber];
}
return(bias);
} // end of HAL_getBias() function
//! \brief Gets the current scale factor
//! \details The current scale factor is defined as
//! USER_ADC_FULL_SCALE_CURRENT_A/USER_IQ_FULL_SCALE_CURRENT_A.
//! This scale factor is not used when converting between PU amps
//! and real amps.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \return The current scale factor
static inline _iq HAL_getCurrentScaleFactor(HAL_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *)handle;
return(obj->current_sf);
} // end of HAL_getCurrentScaleFactor() function
//! \brief Gets the number of current sensors
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \return The number of current sensors
static inline uint_least8_t HAL_getNumCurrentSensors(HAL_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *)handle;
return(obj->numCurrentSensors);
} // end of HAL_getNumCurrentSensors() function
//! \brief Gets the number of voltage sensors
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \return The number of voltage sensors
static inline uint_least8_t HAL_getNumVoltageSensors(HAL_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *)handle;
return(obj->numVoltageSensors);
} // end of HAL_getNumVoltageSensors() function
//! \brief Gets the value used to set the low pass filter pole for offset estimation
//! \details An IIR single pole low pass filter is used to find the feedback circuit's
//! offsets. This function returns the value of that pole.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \return The value used to set the low pass filter pole, pu
static inline _iq HAL_getOffsetBeta_lp_pu(HAL_Handle handle,
const HAL_SensorType_e sensorType,
const uint_least8_t sensorNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
_iq beta_lp_pu = _IQ(0.0);
if(sensorType == HAL_SensorType_Current)
{
beta_lp_pu = OFFSET_getBeta(obj->offsetHandle_I[sensorNumber]);
}
else if(sensorType == HAL_SensorType_Voltage)
{
beta_lp_pu = OFFSET_getBeta(obj->offsetHandle_V[sensorNumber]);
}
return(beta_lp_pu);
} // end of HAL_getOffsetBeta_lp_pu() function
//! \brief Gets the offset value
//! \details The offsets that are calculated during the feedback circuits calibrations
//! are returned from the IIR filter object.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \return The offset value
static inline _iq HAL_getOffsetValue(HAL_Handle handle,
const HAL_SensorType_e sensorType,
const uint_least8_t sensorNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
_iq offset = _IQ(0.0);
if(sensorType == HAL_SensorType_Current)
{
offset = OFFSET_getOffset(obj->offsetHandle_I[sensorNumber]);
}
else if(sensorType == HAL_SensorType_Voltage)
{
offset = OFFSET_getOffset(obj->offsetHandle_V[sensorNumber]);
}
return(offset);
} // end of HAL_getOffsetValue() function
//! \brief Gets the voltage scale factor
//! \details The voltage scale factor is defined as
//! USER_ADC_FULL_SCALE_VOLTAGE_V/USER_IQ_FULL_SCALE_VOLTAGE_V.
//! This scale factor is not used when converting between PU volts
//! and real volts.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \return The voltage scale factor
static inline _iq HAL_getVoltageScaleFactor(HAL_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *)handle;
return(obj->voltage_sf);
} // end of HAL_getVoltageScaleFactor() function
//! \brief 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_Handle handle);
//! \brief 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);
//! \brief 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)
{
HAL_Obj *obj = (HAL_Obj *)handle;
PIE_Obj *pie = (PIE_Obj *)obj->pieHandle;
ENABLE_PROTECTED_REGISTER_WRITE_MODE;
pie->ADCINT1 = &mainISR;
pie->SCIRXINTA = &SCI_RX_ISR;
DISABLE_PROTECTED_REGISTER_WRITE_MODE;
return;
} // end of HAL_initIntVectorTable() function
//! \brief Reads the ADC data
//! \details Reads in the ADC result registers, adjusts for offsets, and
//! scales the values according to the settings in user.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_readAdcData(HAL_Handle handle,HAL_AdcData_t *pAdcData)
{
HAL_Obj *obj = (HAL_Obj *)handle;
_iq value;
_iq current_sf = HAL_getCurrentScaleFactor(handle);
_iq voltage_sf = HAL_getVoltageScaleFactor(handle);
// convert current A
// sample the first sample twice due to errata sprz342f, ignore the first sample
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_1);
value = _IQ12mpy(value,current_sf) - obj->adcBias.I.value[0]; // divide by 2^numAdcBits = 2^12
pAdcData->I.value[0] = value;
// convert current B
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_2);
value = _IQ12mpy(value,current_sf) - obj->adcBias.I.value[1]; // divide by 2^numAdcBits = 2^12
pAdcData->I.value[1] = value;
// convert current C
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_3);
value = _IQ12mpy(value,current_sf) - obj->adcBias.I.value[2]; // divide by 2^numAdcBits = 2^12
pAdcData->I.value[2] = value;
// convert voltage A
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_4);
value = _IQ12mpy(value,voltage_sf) - obj->adcBias.V.value[0]; // divide by 2^numAdcBits = 2^12
pAdcData->V.value[0] = value;
// convert voltage B
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_5);
value = _IQ12mpy(value,voltage_sf) - obj->adcBias.V.value[1]; // divide by 2^numAdcBits = 2^12
pAdcData->V.value[1] = value;
// convert voltage C
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_6);
value = _IQ12mpy(value,voltage_sf) - obj->adcBias.V.value[2]; // divide by 2^numAdcBits = 2^12
pAdcData->V.value[2] = value;
// read the dcBus voltage value
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_7); // divide by 2^numAdcBits = 2^12
value = _IQ12mpy(value,voltage_sf);
pAdcData->dcBus = value;
return;
} // end of HAL_readAdcData() function
//! \brief 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 uint_least8_t timerNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
uint32_t timerCnt = TIMER_getCount(obj->timerHandle[timerNumber]);
return(timerCnt);
} // end of HAL_readTimerCnt() function
//! \brief Reloads the timer
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] timerNumber The timer number, 0,1 or 2
static inline void HAL_reloadTimer(HAL_Handle handle,const uint_least8_t timerNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// reload the specified timer
TIMER_reload(obj->timerHandle[timerNumber]);
return;
} // end of HAL_reloadTimer() function
//! \brief Sets up the GATE object
//! \param[in] handle The hardware abstraction layer (HAL) handle
void HAL_setupGate(HAL_Handle handle);
//! \brief Starts the timer
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] timerNumber The timer number, 0,1 or 2
static inline void HAL_startTimer(HAL_Handle handle,const uint_least8_t timerNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// start the specified timer
TIMER_start(obj->timerHandle[timerNumber]);
return;
} // end of HAL_startTimer() function
//! \brief Stops the timer
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] timerNumber The timer number, 0,1 or 2
static inline void HAL_stopTimer(HAL_Handle handle,const uint_least8_t timerNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// stop the specified timer
TIMER_stop(obj->timerHandle[timerNumber]);
return;
} // end of HAL_stopTimer() function
//! \brief Sets the timer period
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] timerNumber The timer number, 0,1 or 2
//! \param[in] period The timer period
static inline void HAL_setTimerPeriod(HAL_Handle handle,const uint_least8_t timerNumber, const uint32_t period)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// set the period
TIMER_setPeriod(obj->timerHandle[timerNumber], period);
return;
} // end of HAL_setTimerPeriod() function
//! \brief Gets the timer period
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] timerNumber The timer number, 0,1 or 2
//! \return The timer period
static inline uint32_t HAL_getTimerPeriod(HAL_Handle handle,const uint_least8_t timerNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
uint32_t timerPeriod = TIMER_getPeriod(obj->timerHandle[timerNumber]);
return(timerPeriod);
} // end of HAL_getTimerPeriod() function
//! \brief Sets the ADC SOC sample delay value
//! \param[in] handle The hardware abstraction layer handle
//! \param[in] socNumber The SOC number
//! \param[in] sampleDelay The delay value for the ADC
static inline void HAL_setAdcSocSampleDelay(HAL_Handle handle,
const ADC_SocNumber_e socNumber,
const ADC_SocSampleDelay_e sampleDelay)
{
HAL_Obj *obj = (HAL_Obj *)handle;
ADC_setSocSampleDelay(obj->adcHandle,socNumber,sampleDelay);
return;
} // end of HAL_setAdcSocSampleDelay() function
//! \brief Sets the ADC bias value
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \param[in] bias The ADC bias value
static inline void HAL_setBias(HAL_Handle handle,
const HAL_SensorType_e sensorType,
uint_least8_t sensorNumber,
const _iq bias)
{
HAL_Obj *obj = (HAL_Obj *)handle;
if(sensorType == HAL_SensorType_Current)
{
obj->adcBias.I.value[sensorNumber] = bias;
}
else if(sensorType == HAL_SensorType_Voltage)
{
obj->adcBias.V.value[sensorNumber] = bias;
}
return;
} // end of HAL_setBias() function
//! \brief Sets the GPIO pin high
//! \details Takes in the enumeration GPIO_Number_e and sets that 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 GPIO_Number_e gpioNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// set GPIO high
GPIO_setHigh(obj->gpioHandle,gpioNumber);
return;
} // end of HAL_setGpioHigh() function
//! \brief Sets up the timers
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] systemFreq_MHz The system frequency, MHz
void HAL_setupTimers(HAL_Handle handle,const uint_least16_t systemFreq_MHz);
//! \brief Toggles the GPIO pin
//! \details Takes in the enumeration GPIO_Number_e and toggles that 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 GPIO_Number_e gpioNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// set GPIO high
GPIO_toggle(obj->gpioHandle,gpioNumber);
return;
} // end of HAL_setGpioHigh() function
//! \brief Sets the GPIO pin low
//! \details Takes in the enumeration GPIO_Number_e and clears that 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 GPIO_Number_e gpioNumber)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// set GPIO low
GPIO_setLow(obj->gpioHandle,gpioNumber);
return;
} // end of HAL_setGpioLow() function
//! \brief Sets the current scale factor in the hardware abstraction layer
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] current_sf The current scale factor
static inline void HAL_setCurrentScaleFactor(HAL_Handle handle,const _iq current_sf)
{
HAL_Obj *obj = (HAL_Obj *)handle;
obj->current_sf = current_sf;
return;
} // end of HAL_setCurrentScaleFactor() function
//! \brief 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_Handle handle,const uint_least8_t numCurrentSensors)
{
HAL_Obj *obj = (HAL_Obj *)handle;
obj->numCurrentSensors = numCurrentSensors;
return;
} // end of HAL_setNumCurrentSensors() function
//! \brief 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_Handle handle,const uint_least8_t numVoltageSensors)
{
HAL_Obj *obj = (HAL_Obj *)handle;
obj->numVoltageSensors = numVoltageSensors;
return;
} // end of HAL_setNumVoltageSensors() function
//! \brief Sets the value used to set the low pass filter pole for offset estimation
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \param[in] beta_lp_pu The value used to set the low pass filter pole, pu
static inline void HAL_setOffsetBeta_lp_pu(HAL_Handle handle,
const HAL_SensorType_e sensorType,
const uint_least8_t sensorNumber,
const _iq beta_lp_pu)
{
HAL_Obj *obj = (HAL_Obj *)handle;
if(sensorType == HAL_SensorType_Current)
{
OFFSET_setBeta(obj->offsetHandle_I[sensorNumber],beta_lp_pu);
}
else if(sensorType == HAL_SensorType_Voltage)
{
OFFSET_setBeta(obj->offsetHandle_V[sensorNumber],beta_lp_pu);
}
return;
} // end of HAL_setOffsetBeta_lp_pu() function
//! \brief Sets the offset initial condition value for offset estimation
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \param[in] initCond The initial condition value
static inline void HAL_setOffsetInitCond(HAL_Handle handle,
const HAL_SensorType_e sensorType,
const uint_least8_t sensorNumber,
const _iq initCond)
{
HAL_Obj *obj = (HAL_Obj *)handle;
if(sensorType == HAL_SensorType_Current)
{
OFFSET_setInitCond(obj->offsetHandle_I[sensorNumber],initCond);
}
else if(sensorType == HAL_SensorType_Voltage)
{
OFFSET_setInitCond(obj->offsetHandle_V[sensorNumber],initCond);
}
return;
} // end of HAL_setOffsetInitCond() function
//! \brief Sets the initial offset value for offset estimation
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] sensorType The sensor type
//! \param[in] sensorNumber The sensor number
//! \param[in] value The initial offset value
static inline void HAL_setOffsetValue(HAL_Handle handle,
const HAL_SensorType_e sensorType,
const uint_least8_t sensorNumber,
const _iq value)
{
HAL_Obj *obj = (HAL_Obj *)handle;
if(sensorType == HAL_SensorType_Current)
{
OFFSET_setOffset(obj->offsetHandle_I[sensorNumber],value);
}
else if(sensorType == HAL_SensorType_Voltage)
{
OFFSET_setOffset(obj->offsetHandle_V[sensorNumber],value);
}
return;
} // end of HAL_setOffsetValue() function
//! \brief Sets the voltage scale factor in the hardware abstraction layer
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] voltage_sf The voltage scale factor
static inline void HAL_setVoltageScaleFactor(HAL_Handle handle,const _iq voltage_sf)
{
HAL_Obj *obj = (HAL_Obj *)handle;
obj->voltage_sf = voltage_sf;
return;
} // end of HAL_setVoltageScaleFactor() function
//! \brief 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
//! \param[in] pUserParams The pointer to the user parameters
extern void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams);
//! \brief Sets up the ADCs (Analog to Digital Converters)
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupAdcs(HAL_Handle handle);
//! \brief 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);
//! \brief 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);
//! \brief Setup GPIO 0 and 1 as outputs for the LaunchPad project lab 1 experiment
//! \details Since the LaunchPad uses the same GPIO pins for LEDs as PWM pins,
//! the GPIO 0 and 1 are setup as an extra step. GPIO 1 will be
//! kept low so that there is no possibility of shoot-through conduction
//! with the high side switch that is controlled by GPIO0.
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupLaunchPadGpio0and1(HAL_Handle handle);
//! \brief Sets up the FLASH.
extern void HAL_setupFlash(HAL_Handle handle);
//! \brief Sets up the peripheral clocks
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupPeripheralClks(HAL_Handle handle);
//! \brief Sets up the PIE (Peripheral Interrupt Expansion)
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupPie(HAL_Handle handle);
//! \brief Sets up the PLL (Phase Lock Loop)
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] clkFreq The clock frequency
extern void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq);
//! \brief 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_Handle handle,
const uint_least16_t systemFreq_MHz,
const float_t pwmPeriod_usec,
const uint_least16_t numPwmTicksPerIsrTick);
//! \brief Sets up the spiA peripheral
//! \param[in] handle The hardware abstraction layer (HAL) handle
extern void HAL_setupSpiA(HAL_Handle handle);
//! \brief Updates the ADC bias values
//! \details This function is called before the motor is started. It sets the voltage
//! and current measurement offsets.
//! \param[in] handle The hardware abstraction layer (HAL) handle
static inline void HAL_updateAdcBias(HAL_Handle handle)
{
uint_least8_t cnt;
HAL_Obj *obj = (HAL_Obj *)handle;
_iq bias;
// update the current bias
for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
{
bias = HAL_getBias(handle,HAL_SensorType_Current,cnt);
bias += OFFSET_getOffset(obj->offsetHandle_I[cnt]);
HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
}
// update the voltage bias
for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
{
bias = HAL_getBias(handle,HAL_SensorType_Voltage,cnt);
bias += OFFSET_getOffset(obj->offsetHandle_V[cnt]);
HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
}
return;
} // end of HAL_updateAdcBias() function
//! \brief Writes DAC data to the PWM comparators for DAC (digital-to-analog conversion) output
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] pDacData The pointer to the DAC data
static inline void HAL_writeDacData(HAL_Handle handle,HAL_DacData_t *pDacData)
{
HAL_Obj *obj = (HAL_Obj *)handle;
// convert values from _IQ to _IQ15
int16_t dacValue_1 = (int16_t)_IQtoIQ15(pDacData->value[0]);
int16_t dacValue_2 = (int16_t)_IQtoIQ15(pDacData->value[1]);
int16_t dacValue_3 = (int16_t)_IQtoIQ15(pDacData->value[2]);
int16_t dacValue_4 = (int16_t)_IQtoIQ15(pDacData->value[3]);
// write the DAC data
PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_1],dacValue_1);
PWMDAC_write_CmpB(obj->pwmDacHandle[PWMDAC_Number_1],dacValue_2);
PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_2],dacValue_3);
PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_3],dacValue_4);
return;
} // end of HAL_writeDacData() function
//! \brief 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_Handle handle,HAL_PwmData_t *pPwmData)
{
uint_least8_t cnt;
HAL_Obj *obj = (HAL_Obj *)handle;
PWM_Obj *pwm;
_iq period;
_iq pwmData_neg;
_iq pwmData_sat;
_iq pwmData_sat_dc;
_iq value;
uint16_t value_sat;
for(cnt=0;cnt<3;cnt++)