MotorWare f2806x Module API Documentation
drv8312kit_revD/f28x/f2806x/src/hal.h
Go to the documentation of this file.
1 #ifndef _HAL_H_
2 #define _HAL_H_
3 /* --COPYRIGHT--,BSD
4  * Copyright (c) 2012, Texas Instruments Incorporated
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * * Neither the name of Texas Instruments Incorporated nor the names of
19  * its contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  * --/COPYRIGHT--*/
34 
40 
41 
42 // **************************************************************************
43 // the includes
44 
45 
46 // modules
47 
48 
49 // platforms
50 #include "hal_obj.h"
52 
53 
58 
59 
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
66 // **************************************************************************
67 // the defines
68 
69 #define Device_cal (void (*)(void))0x3D7C80
70 
73 #define FP_SCALE 32768
74 
76 #define FP_ROUND FP_SCALE/2
77 
81 #define OSC_POSTRIM 32
82 #define OSC_POSTRIM_OFF FP_SCALE*OSC_POSTRIM
83 
85 
87 #define getOsc1FineTrimSlope() (*(int16_t (*)(void))0x3D7E90)()
88 
90 #define getOsc1FineTrimOffset() (*(int16_t (*)(void))0x3D7E93)()
91 
93 #define getOsc1CoarseTrim() (*(int16_t (*)(void))0x3D7E96)()
94 
97 #define getOsc2FineTrimSlope() (*(int16_t (*)(void))0x3D7E99)()
98 
100 #define getOsc2FineTrimOffset() (*(int16_t (*)(void))0x3D7E9C)()
101 
103 #define getOsc2CoarseTrim() (*(int16_t (*)(void))0x3D7E9F)()
104 
106 #define getRefTempOffset() (*(int16_t (*)(void))0x3D7EA2)()
107 
110 #define HAL_PWM_DBFED_CNT 1
111 
112 
115 #define HAL_PWM_DBRED_CNT 1
116 
117 
120 #define HAL_turnLedOff HAL_setGpioLow
121 
122 
125 #define HAL_turnLedOn HAL_setGpioHigh
126 
127 
130 #define HAL_toggleLed HAL_toggleGpio
131 
132 // **************************************************************************
133 // the typedefs
134 
135 
138 typedef enum
139 {
143 
144 
147 typedef enum
148 {
149  HAL_Gpio_LED2=GPIO_Number_31,
150  HAL_Gpio_LED3=GPIO_Number_34
152 
153 
156 typedef enum
157 {
161 
162 
163 // **************************************************************************
164 // the globals
165 
166 extern interrupt void mainISR(void);
167 
168 
169 // **************************************************************************
170 // the function prototypes
171 
172 
177 static inline void HAL_acqAdcInt(HAL_Handle handle,const ADC_IntNumber_e intNumber)
178 {
179  HAL_Obj *obj = (HAL_Obj *)handle;
180 
181 
182  // clear the ADC interrupt flag
183  ADC_clearIntFlag(obj->adcHandle,intNumber);
184 
185 
186  // Acknowledge interrupt from PIE group 10
187  PIE_clearInt(obj->pieHandle,PIE_GroupNumber_10);
188 
189  return;
190 } // end of HAL_acqAdcInt() function
191 
192 
197 static inline void HAL_acqPwmInt(HAL_Handle handle,const PWM_Number_e pwmNumber)
198 {
199  HAL_Obj *obj = (HAL_Obj *)handle;
200 
201 
202  // clear the PWM interrupt flag
203  PWM_clearIntFlag(obj->pwmHandle[pwmNumber]);
204 
205 
206  // clear the SOCA flag
207  PWM_clearSocAFlag(obj->pwmHandle[pwmNumber]);
208 
209 
210  // Acknowledge interrupt from PIE group 3
211  PIE_clearInt(obj->pieHandle,PIE_GroupNumber_3);
212 
213  return;
214 } // end of HAL_acqPwmInt() function
215 
216 
222 extern void HAL_cal(HAL_Handle handle);
223 
224 
227 extern void HAL_disableGlobalInts(HAL_Handle handle);
228 
229 
232 extern void HAL_disableWdog(HAL_Handle handle);
233 
234 
239 static inline void HAL_disablePwm(HAL_Handle handle)
240 {
241  HAL_Obj *obj = (HAL_Obj *)handle;
242 
243  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_1]);
244  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_2]);
245  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_3]);
246 
247  return;
248 } // end of HAL_disablePwm() function
249 
250 
255 extern void HAL_enableAdcInts(HAL_Handle handle);
256 
257 
263 extern void HAL_enableDebugInt(HAL_Handle handle);
264 
265 
268 extern void HAL_enableGlobalInts(HAL_Handle handle);
269 
270 
275 static inline void HAL_enablePwm(HAL_Handle handle)
276 {
277  HAL_Obj *obj = (HAL_Obj *)handle;
278 
279  PWM_clearOneShotTrip(obj->pwmHandle[PWM_Number_1]);
280  PWM_clearOneShotTrip(obj->pwmHandle[PWM_Number_2]);
281  PWM_clearOneShotTrip(obj->pwmHandle[PWM_Number_3]);
282 
283  return;
284 } // end of HAL_enablePwm() function
285 
286 
289 extern void HAL_enablePwmInt(HAL_Handle handle);
290 
291 
296 static inline ADC_SocSampleDelay_e HAL_getAdcSocSampleDelay(HAL_Handle handle,
297  const ADC_SocNumber_e socNumber)
298 {
299  HAL_Obj *obj = (HAL_Obj *)handle;
300 
301  return(ADC_getSocSampleDelay(obj->adcHandle,socNumber));
302 } // end of HAL_getAdcSocSampleDelay() function
303 
304 
313 static inline _iq HAL_getBias(HAL_Handle handle,
314  const HAL_SensorType_e sensorType,
315  uint_least8_t sensorNumber)
316 {
317  HAL_Obj *obj = (HAL_Obj *)handle;
318  _iq bias = _IQ(0.0);
319 
320  if(sensorType == HAL_SensorType_Current)
321  {
322  bias = obj->adcBias.I.value[sensorNumber];
323  }
324  else if(sensorType == HAL_SensorType_Voltage)
325  {
326  bias = obj->adcBias.V.value[sensorNumber];
327  }
328 
329  return(bias);
330 } // end of HAL_getBias() function
331 
332 
341 {
342  HAL_Obj *obj = (HAL_Obj *)handle;
343 
344  return(obj->current_sf);
345 } // end of HAL_getCurrentScaleFactor() function
346 
347 
351 static inline uint_least8_t HAL_getNumCurrentSensors(HAL_Handle handle)
352 {
353  HAL_Obj *obj = (HAL_Obj *)handle;
354 
355 
356  return(obj->numCurrentSensors);
357 } // end of HAL_getNumCurrentSensors() function
358 
359 
363 static inline uint_least8_t HAL_getNumVoltageSensors(HAL_Handle handle)
364 {
365  HAL_Obj *obj = (HAL_Obj *)handle;
366 
367 
368  return(obj->numVoltageSensors);
369 } // end of HAL_getNumVoltageSensors() function
370 
371 
380  const HAL_SensorType_e sensorType,
381  const uint_least8_t sensorNumber)
382 {
383  HAL_Obj *obj = (HAL_Obj *)handle;
384 
385  _iq beta_lp_pu = _IQ(0.0);
386 
387  if(sensorType == HAL_SensorType_Current)
388  {
389  beta_lp_pu = OFFSET_getBeta(obj->offsetHandle_I[sensorNumber]);
390  }
391  else if(sensorType == HAL_SensorType_Voltage)
392  {
393  beta_lp_pu = OFFSET_getBeta(obj->offsetHandle_V[sensorNumber]);
394  }
395 
396  return(beta_lp_pu);
397 } // end of HAL_getOffsetBeta_lp_pu() function
398 
399 
407 static inline _iq HAL_getOffsetValue(HAL_Handle handle,
408  const HAL_SensorType_e sensorType,
409  const uint_least8_t sensorNumber)
410 {
411  HAL_Obj *obj = (HAL_Obj *)handle;
412 
413  _iq offset = _IQ(0.0);
414 
415  if(sensorType == HAL_SensorType_Current)
416  {
417  offset = OFFSET_getOffset(obj->offsetHandle_I[sensorNumber]);
418  }
419  else if(sensorType == HAL_SensorType_Voltage)
420  {
421  offset = OFFSET_getOffset(obj->offsetHandle_V[sensorNumber]);
422  }
423 
424  return(offset);
425 } // end of HAL_getOffsetValue() function
426 
427 
436 {
437  HAL_Obj *obj = (HAL_Obj *)handle;
438 
439  return(obj->voltage_sf);
440 } // end of HAL_getVoltageScaleFactor() function
441 
442 
449 extern void HAL_setupFaults(HAL_Handle handle);
450 
451 
458 extern HAL_Handle HAL_init(void *pMemory,const size_t numBytes);
459 
460 
464 static inline void HAL_initIntVectorTable(HAL_Handle handle)
465  {
466  HAL_Obj *obj = (HAL_Obj *)handle;
467  PIE_Obj *pie = (PIE_Obj *)obj->pieHandle;
468 
469 
470  ENABLE_PROTECTED_REGISTER_WRITE_MODE;
471 
472  pie->ADCINT1 = &mainISR;
473 
474  DISABLE_PROTECTED_REGISTER_WRITE_MODE;
475 
476  return;
477  } // end of HAL_initIntVectorTable() function
478 
479 
487 static inline void HAL_readAdcData(HAL_Handle handle,HAL_AdcData_t *pAdcData)
488 {
489  HAL_Obj *obj = (HAL_Obj *)handle;
490 
491  _iq value;
492  _iq current_sf = -HAL_getCurrentScaleFactor(handle);
493  _iq voltage_sf = HAL_getVoltageScaleFactor(handle);
494 
495 
496  // convert current A
497  // sample the first sample twice due to errata sprz342f, ignore the first sample
498  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_1);
499  value = _IQ12mpy(value,current_sf) - obj->adcBias.I.value[0]; // divide by 2^numAdcBits = 2^12
500  pAdcData->I.value[0] = value;
501 
502  // convert current B
503  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_2);
504  value = _IQ12mpy(value,current_sf) - obj->adcBias.I.value[1]; // divide by 2^numAdcBits = 2^12
505  pAdcData->I.value[1] = value;
506 
507  // convert current C
508  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_3);
509  value = _IQ12mpy(value,current_sf) - obj->adcBias.I.value[2]; // divide by 2^numAdcBits = 2^12
510  pAdcData->I.value[2] = value;
511 
512  // convert voltage A
513  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_4);
514  value = _IQ12mpy(value,voltage_sf) - obj->adcBias.V.value[0]; // divide by 2^numAdcBits = 2^12
515  pAdcData->V.value[0] = value;
516 
517  // convert voltage B
518  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_5);
519  value = _IQ12mpy(value,voltage_sf) - obj->adcBias.V.value[1]; // divide by 2^numAdcBits = 2^12
520  pAdcData->V.value[1] = value;
521 
522  // convert voltage C
523  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_6);
524  value = _IQ12mpy(value,voltage_sf) - obj->adcBias.V.value[2]; // divide by 2^numAdcBits = 2^12
525  pAdcData->V.value[2] = value;
526 
527  // read the dcBus voltage value
528  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_7); // divide by 2^numAdcBits = 2^12
529  value = _IQ12mpy(value,voltage_sf);
530  pAdcData->dcBus = value;
531 
532  return;
533 } // end of HAL_readAdcData() function
534 
535 
543 static inline void HAL_readAdcDataWithOffsets(HAL_Handle handle,HAL_AdcData_t *pAdcData)
544 {
545  HAL_Obj *obj = (HAL_Obj *)handle;
546 
547  _iq value;
548  _iq current_sf = -HAL_getCurrentScaleFactor(handle);
549  _iq voltage_sf = HAL_getVoltageScaleFactor(handle);
550 
551 
552  // convert current A
553  // sample the first sample twice due to errata sprz342f, ignore the first sample
554  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_1);
555  value = _IQ12mpy(value,current_sf);
556  pAdcData->I.value[0] = value;
557 
558  // convert current B
559  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_2);
560  value = _IQ12mpy(value,current_sf);
561  pAdcData->I.value[1] = value;
562 
563  // convert current C
564  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_3);
565  value = _IQ12mpy(value,current_sf);
566  pAdcData->I.value[2] = value;
567 
568  // convert voltage A
569  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_4);
570  value = _IQ12mpy(value,voltage_sf);
571  pAdcData->V.value[0] = value;
572 
573  // convert voltage B
574  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_5);
575  value = _IQ12mpy(value,voltage_sf);
576  pAdcData->V.value[1] = value;
577 
578  // convert voltage C
579  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_6);
580  value = _IQ12mpy(value,voltage_sf);
581  pAdcData->V.value[2] = value;
582 
583  // read the dcBus voltage value
584  value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_7);
585  value = _IQ12mpy(value,voltage_sf);
586  pAdcData->dcBus = value;
587 
588  return;
589 } // end of HAL_readAdcDataWithOffsets() function
590 
591 
596 static inline uint32_t HAL_readTimerCnt(HAL_Handle handle,const uint_least8_t timerNumber)
597 {
598  HAL_Obj *obj = (HAL_Obj *)handle;
599  uint32_t timerCnt = TIMER_getCount(obj->timerHandle[timerNumber]);
600 
601  return(timerCnt);
602 } // end of HAL_readTimerCnt() function
603 
604 
608 static inline void HAL_reloadTimer(HAL_Handle handle,const uint_least8_t timerNumber)
609 {
610  HAL_Obj *obj = (HAL_Obj *)handle;
611 
612  // reload the specified timer
613  TIMER_reload(obj->timerHandle[timerNumber]);
614 
615  return;
616 } // end of HAL_reloadTimer() function
617 
618 
622 static inline void HAL_startTimer(HAL_Handle handle,const uint_least8_t timerNumber)
623 {
624  HAL_Obj *obj = (HAL_Obj *)handle;
625 
626  // start the specified timer
627  TIMER_start(obj->timerHandle[timerNumber]);
628 
629  return;
630 } // end of HAL_startTimer() function
631 
632 
636 static inline void HAL_stopTimer(HAL_Handle handle,const uint_least8_t timerNumber)
637 {
638  HAL_Obj *obj = (HAL_Obj *)handle;
639 
640  // stop the specified timer
641  TIMER_stop(obj->timerHandle[timerNumber]);
642 
643  return;
644 } // end of HAL_stopTimer() function
645 
646 
651 static inline void HAL_setTimerPeriod(HAL_Handle handle,const uint_least8_t timerNumber, const uint32_t period)
652 {
653  HAL_Obj *obj = (HAL_Obj *)handle;
654 
655  // set the period
656  TIMER_setPeriod(obj->timerHandle[timerNumber], period);
657 
658  return;
659 } // end of HAL_setTimerPeriod() function
660 
661 
666 static inline uint32_t HAL_getTimerPeriod(HAL_Handle handle,const uint_least8_t timerNumber)
667 {
668  HAL_Obj *obj = (HAL_Obj *)handle;
669 
670  uint32_t timerPeriod = TIMER_getPeriod(obj->timerHandle[timerNumber]);
671 
672  return(timerPeriod);
673 } // end of HAL_getTimerPeriod() function
674 
675 
680 static inline void HAL_setAdcSocSampleDelay(HAL_Handle handle,
681  const ADC_SocNumber_e socNumber,
682  const ADC_SocSampleDelay_e sampleDelay)
683 {
684  HAL_Obj *obj = (HAL_Obj *)handle;
685 
686  ADC_setSocSampleDelay(obj->adcHandle,socNumber,sampleDelay);
687 
688  return;
689 } // end of HAL_setAdcSocSampleDelay() function
690 
691 
697 static inline void HAL_setBias(HAL_Handle handle,
698  const HAL_SensorType_e sensorType,
699  uint_least8_t sensorNumber,
700  const _iq bias)
701 {
702  HAL_Obj *obj = (HAL_Obj *)handle;
703 
704 
705  if(sensorType == HAL_SensorType_Current)
706  {
707  obj->adcBias.I.value[sensorNumber] = bias;
708  }
709  else if(sensorType == HAL_SensorType_Voltage)
710  {
711  obj->adcBias.V.value[sensorNumber] = bias;
712  }
713 
714  return;
715 } // end of HAL_setBias() function
716 
717 
723 static inline void HAL_setGpioHigh(HAL_Handle handle,const GPIO_Number_e gpioNumber)
724 {
725  HAL_Obj *obj = (HAL_Obj *)handle;
726 
727 
728  // set GPIO high
729  GPIO_setHigh(obj->gpioHandle,gpioNumber);
730 
731  return;
732 } // end of HAL_setGpioHigh() function
733 
734 
739 static inline bool HAL_readGpio(HAL_Handle handle,const GPIO_Number_e gpioNumber)
740 {
741  HAL_Obj *obj = (HAL_Obj *)handle;
742 
743  // read GPIO
744  return(GPIO_read(obj->gpioHandle,gpioNumber));
745 } // end of HAL_readGpio() function
746 
747 
753 static inline void HAL_toggleGpio(HAL_Handle handle,const GPIO_Number_e gpioNumber)
754 {
755  HAL_Obj *obj = (HAL_Obj *)handle;
756 
757 
758  // set GPIO high
759  GPIO_toggle(obj->gpioHandle,gpioNumber);
760 
761  return;
762 } // end of HAL_setGpioHigh() function
763 
764 
770 static inline void HAL_setGpioLow(HAL_Handle handle,const GPIO_Number_e gpioNumber)
771 {
772  HAL_Obj *obj = (HAL_Obj *)handle;
773 
774 
775  // set GPIO low
776  GPIO_setLow(obj->gpioHandle,gpioNumber);
777 
778  return;
779 } // end of HAL_setGpioLow() function
780 
781 
785 static inline void HAL_setCurrentScaleFactor(HAL_Handle handle,const _iq current_sf)
786 {
787  HAL_Obj *obj = (HAL_Obj *)handle;
788 
789 
790  obj->current_sf = current_sf;
791 
792  return;
793 } // end of HAL_setCurrentScaleFactor() function
794 
795 
799 static inline void HAL_setNumCurrentSensors(HAL_Handle handle,const uint_least8_t numCurrentSensors)
800 {
801  HAL_Obj *obj = (HAL_Obj *)handle;
802 
803 
804  obj->numCurrentSensors = numCurrentSensors;
805 
806  return;
807 } // end of HAL_setNumCurrentSensors() function
808 
809 
813 static inline void HAL_setNumVoltageSensors(HAL_Handle handle,const uint_least8_t numVoltageSensors)
814 {
815  HAL_Obj *obj = (HAL_Obj *)handle;
816 
817 
818  obj->numVoltageSensors = numVoltageSensors;
819 
820  return;
821 } // end of HAL_setNumVoltageSensors() function
822 
823 
829 static inline void HAL_setOffsetBeta_lp_pu(HAL_Handle handle,
830  const HAL_SensorType_e sensorType,
831  const uint_least8_t sensorNumber,
832  const _iq beta_lp_pu)
833 {
834  HAL_Obj *obj = (HAL_Obj *)handle;
835 
836  if(sensorType == HAL_SensorType_Current)
837  {
838  OFFSET_setBeta(obj->offsetHandle_I[sensorNumber],beta_lp_pu);
839  }
840  else if(sensorType == HAL_SensorType_Voltage)
841  {
842  OFFSET_setBeta(obj->offsetHandle_V[sensorNumber],beta_lp_pu);
843  }
844 
845  return;
846 } // end of HAL_setOffsetBeta_lp_pu() function
847 
848 
854 static inline void HAL_setOffsetInitCond(HAL_Handle handle,
855  const HAL_SensorType_e sensorType,
856  const uint_least8_t sensorNumber,
857  const _iq initCond)
858 {
859  HAL_Obj *obj = (HAL_Obj *)handle;
860 
861  if(sensorType == HAL_SensorType_Current)
862  {
863  OFFSET_setInitCond(obj->offsetHandle_I[sensorNumber],initCond);
864  }
865  else if(sensorType == HAL_SensorType_Voltage)
866  {
867  OFFSET_setInitCond(obj->offsetHandle_V[sensorNumber],initCond);
868  }
869 
870  return;
871 } // end of HAL_setOffsetInitCond() function
872 
873 
879 static inline void HAL_setOffsetValue(HAL_Handle handle,
880  const HAL_SensorType_e sensorType,
881  const uint_least8_t sensorNumber,
882  const _iq value)
883 {
884  HAL_Obj *obj = (HAL_Obj *)handle;
885 
886  if(sensorType == HAL_SensorType_Current)
887  {
888  OFFSET_setOffset(obj->offsetHandle_I[sensorNumber],value);
889  }
890  else if(sensorType == HAL_SensorType_Voltage)
891  {
892  OFFSET_setOffset(obj->offsetHandle_V[sensorNumber],value);
893  }
894 
895  return;
896 } // end of HAL_setOffsetValue() function
897 
898 
902 static inline void HAL_setVoltageScaleFactor(HAL_Handle handle,const _iq voltage_sf)
903 {
904  HAL_Obj *obj = (HAL_Obj *)handle;
905 
906  obj->voltage_sf = voltage_sf;
907 
908  return;
909 } // end of HAL_setVoltageScaleFactor() function
910 
911 
918 extern void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams);
919 
920 
923 extern void HAL_setupAdcs(HAL_Handle handle);
924 
925 
929 extern void HAL_setupClks(HAL_Handle handle);
930 
931 
934 extern void HAL_setupFlash(HAL_Handle handle);
935 
936 
939 extern void HAL_setupGpios(HAL_Handle handle);
940 
941 
944 extern void HAL_setupPeripheralClks(HAL_Handle handle);
945 
946 
949 extern void HAL_setupPie(HAL_Handle handle);
950 
951 
955 extern void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq);
956 
957 
963 extern void HAL_setupPwms(HAL_Handle handle,
964  const float_t systemFreq_MHz,
965  const float_t pwmPeriod_usec,
966  const uint_least16_t numPwmTicksPerIsrTick);
967 
968 
971 extern void HAL_setupPwmDacs(HAL_Handle handle);
972 
973 
976 extern void HAL_setupQEP(HAL_Handle handle,HAL_QepSelect_e qep);
977 
978 
981 extern void HAL_setupSpiA(HAL_Handle handle);
982 
983 
987 void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz);
988 
989 
994 static inline void HAL_updateAdcBias(HAL_Handle handle)
995 {
996  uint_least8_t cnt;
997  HAL_Obj *obj = (HAL_Obj *)handle;
998  _iq bias;
999 
1000 
1001  // update the current bias
1002  for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
1003  {
1004  bias = HAL_getBias(handle,HAL_SensorType_Current,cnt);
1005 
1006  bias += OFFSET_getOffset(obj->offsetHandle_I[cnt]);
1007 
1008  HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
1009  }
1010 
1011 
1012  // update the voltage bias
1013  for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
1014  {
1015  bias = HAL_getBias(handle,HAL_SensorType_Voltage,cnt);
1016 
1017  bias += OFFSET_getOffset(obj->offsetHandle_V[cnt]);
1018 
1019  HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
1020  }
1021 
1022  return;
1023 } // end of HAL_updateAdcBias() function
1024 
1025 
1029 static inline void HAL_writeDacData(HAL_Handle handle,HAL_DacData_t *pDacData)
1030 {
1031  HAL_Obj *obj = (HAL_Obj *)handle;
1032 
1033  // convert values from _IQ to _IQ15
1034  int16_t dacValue_1 = (int16_t)_IQtoIQ15(pDacData->value[0]);
1035  int16_t dacValue_2 = (int16_t)_IQtoIQ15(pDacData->value[1]);
1036  int16_t dacValue_3 = (int16_t)_IQtoIQ15(pDacData->value[2]);
1037  int16_t dacValue_4 = (int16_t)_IQtoIQ15(pDacData->value[3]);
1038 
1039  // write the DAC data
1040  PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_1],dacValue_1);
1041  PWMDAC_write_CmpB(obj->pwmDacHandle[PWMDAC_Number_1],dacValue_2);
1042  PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_2],dacValue_3);
1043  PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_3],dacValue_4);
1044 
1045  return;
1046 } // end of HAL_writeDacData() function
1047 
1048 
1052 static inline void HAL_writePwmData(HAL_Handle handle,HAL_PwmData_t *pPwmData)
1053 {
1054  uint_least8_t cnt;
1055  HAL_Obj *obj = (HAL_Obj *)handle;
1056  PWM_Obj *pwm;
1057  _iq period;
1058  _iq pwmData_neg;
1059  _iq pwmData_sat;
1060  _iq pwmData_sat_dc;
1061  _iq value;
1062  uint16_t value_sat;
1063 
1064  for(cnt=0;cnt<3;cnt++)
1065  {
1066  pwm = (PWM_Obj *)obj->pwmHandle[cnt];
1067  period = (_iq)pwm->TBPRD;
1068  pwmData_neg = _IQmpy(pPwmData->Tabc.value[cnt],_IQ(-1.0));
1069  pwmData_sat = _IQsat(pwmData_neg,_IQ(0.5),_IQ(-0.5));
1070  pwmData_sat_dc = pwmData_sat + _IQ(0.5);
1071  value = _IQmpy(pwmData_sat_dc, period);
1072  value_sat = (uint16_t)_IQsat(value, period, _IQ(0.0));
1073 
1074  // write the PWM data
1075  PWM_write_CmpA(obj->pwmHandle[cnt],value_sat);
1076  }
1077 
1078  return;
1079 } // end of HAL_writePwmData() function
1080 
1081 
1086 static inline uint16_t HAL_readPwmCmpA(HAL_Handle handle,const PWM_Number_e pwmNumber)
1087 {
1088  HAL_Obj *obj = (HAL_Obj *)handle;
1089 
1090  // the compare value to be returned
1091  uint16_t pwmValue;
1092 
1093  pwmValue = PWM_get_CmpA(obj->pwmHandle[pwmNumber]);
1094 
1095  return(pwmValue);
1096 } // end of HAL_readPwmCmpA() function
1097 
1098 
1103 static inline uint16_t HAL_readPwmCmpAM(HAL_Handle handle,const PWM_Number_e pwmNumber)
1104 {
1105  HAL_Obj *obj = (HAL_Obj *)handle;
1106 
1107  // the compare value to be returned
1108  uint16_t pwmValue;
1109 
1110  pwmValue = PWM_get_CmpAM(obj->pwmHandle[pwmNumber]);
1111 
1112  return(pwmValue);
1113 } // end of HAL_readPwmCmpAM() function
1114 
1115 
1120 static inline uint16_t HAL_readPwmCmpB(HAL_Handle handle,const PWM_Number_e pwmNumber)
1121 {
1122  HAL_Obj *obj = (HAL_Obj *)handle;
1123 
1124  // the compare value to be returned
1125  uint16_t pwmValue;
1126 
1127  pwmValue = PWM_get_CmpB(obj->pwmHandle[pwmNumber]);
1128 
1129  return(pwmValue);
1130 } // end of HAL_readPwmCmpB() function
1131 
1132 
1137 static inline uint16_t HAL_readPwmPeriod(HAL_Handle handle,const PWM_Number_e pwmNumber)
1138 {
1139  HAL_Obj *obj = (HAL_Obj *)handle;
1140 
1141  // the period value to be returned
1142  uint16_t pwmPeriodValue;
1143 
1144  pwmPeriodValue = PWM_getPeriod(obj->pwmHandle[pwmNumber]);
1145 
1146  return(pwmPeriodValue);
1147 } // end of HAL_readPwmPeriod() function
1148 
1149 
1150 static inline void HAL_setTrigger(HAL_Handle handle,const SVGENCURRENT_IgnoreShunt_e ignoreShunt,const int16_t minwidth, const int16_t cmpOffset)
1151 {
1152  HAL_Obj *obj = (HAL_Obj *)handle;
1153 
1154  PWM_Obj *pwm1 = (PWM_Obj *)obj->pwmHandle[PWM_Number_1];
1155  PWM_Obj *pwm2 = (PWM_Obj *)obj->pwmHandle[PWM_Number_2];
1156  PWM_Obj *pwm3 = (PWM_Obj *)obj->pwmHandle[PWM_Number_3];
1157  PWM_Obj *pwm;
1158 
1159  uint16_t nextPulse1 = (pwm1->CMPA + pwm1->CMPAM) / 2;
1160  uint16_t nextPulse2 = (pwm2->CMPA + pwm2->CMPAM) / 2;
1161  uint16_t nextPulse3 = (pwm3->CMPA + pwm3->CMPAM) / 2;
1162  uint16_t pwmCMPA1 = pwm1->CMPA;
1163  uint16_t pwmCMPA2 = pwm2->CMPA;
1164  uint16_t pwmCMPA3 = pwm3->CMPA;
1165 
1166  int16_t offset;
1167 
1168  if(ignoreShunt == use_all)
1169  {
1170  if((nextPulse1 <= nextPulse2) && (nextPulse1 <= nextPulse3))
1171  {
1172  pwm = pwm1;
1173  }
1174  else if((nextPulse2 <= nextPulse1) && (nextPulse2 <= nextPulse3))
1175  {
1176  pwm = pwm2;
1177  }
1178  else
1179  {
1180  pwm = pwm3;
1181  }
1182  }
1183  else if(ignoreShunt == ignore_a)
1184  {
1185  offset = pwmCMPA1 + cmpOffset;
1186  }
1187  else if(ignoreShunt == ignore_b)
1188  {
1189  offset = pwmCMPA2 + cmpOffset;
1190  }
1191  else if(ignoreShunt == ignore_c)
1192  {
1193  offset = pwmCMPA3 + cmpOffset;
1194  }
1195  else if(ignoreShunt == ignore_ab)
1196  {
1197  if(pwmCMPA1 > pwmCMPA2)
1198  {
1199  offset = pwmCMPA1 + cmpOffset;
1200  }
1201  else
1202  {
1203  offset = pwmCMPA2 + cmpOffset;
1204  }
1205  }
1206  else if(ignoreShunt == ignore_ac)
1207  {
1208  if(pwmCMPA1 > pwmCMPA3)
1209  {
1210  offset = pwmCMPA1 + cmpOffset;
1211  }
1212  else
1213  {
1214  offset = pwmCMPA3 + cmpOffset;
1215  }
1216  }
1217  else // when ignoreShunt == ignore_bc
1218  {
1219  if(pwmCMPA2 > pwmCMPA3)
1220  {
1221  offset = pwmCMPA2 + cmpOffset;
1222  }
1223  else
1224  {
1225  offset = pwmCMPA3 + cmpOffset;
1226  }
1227  }
1228 
1229 
1230  if(ignoreShunt == use_all)
1231  {
1232  if(pwm->CMPAM >= (pwm->CMPA + pwm->DBFED))
1233  {
1234  pwm1->CMPB = (pwm->CMPAM - (pwm->CMPA + pwm->DBFED)) / 2 + 1;
1235  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualCmpBDecr);
1236  }
1237  else
1238  {
1239  pwm1->CMPB = ((pwm->CMPA + pwm->DBFED) - pwm->CMPAM ) / 2 + 1;
1240  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualCmpBIncr);
1241  }
1242  }
1243  else
1244  {
1245  pwm1->CMPB = offset;
1246  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualCmpBIncr);
1247  }
1248 
1249  return;
1250 } // end of HAL_setTrigger() function
1251 
1252 #ifdef QEP
1253 static inline uint32_t HAL_getQepPosnCounts(HAL_Handle handle)
1257 {
1258  HAL_Obj *obj = (HAL_Obj *)handle;
1259  QEP_Obj *qep = (QEP_Obj *)obj->qepHandle[0];
1260 
1261  return qep->QPOSCNT;
1262 }
1263 
1264 
1268 static inline uint32_t HAL_getQepPosnMaximum(HAL_Handle handle)
1269 {
1270  HAL_Obj *obj = (HAL_Obj *)handle;
1271  QEP_Obj *qep = (QEP_Obj *)obj->qepHandle[0];
1272 
1273  return qep->QPOSMAX;
1274 }
1275 #endif
1276 
1280 void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber);
1281 
1282 
1286 uint16_t HAL_AdcCalConversion(HAL_Handle handle);
1287 
1288 
1291 void HAL_AdcOffsetSelfCal(HAL_Handle handle);
1292 
1293 
1299 uint16_t HAL_getOscTrimValue(int16_t coarse, int16_t fine);
1300 
1301 
1304 void HAL_OscTempComp(HAL_Handle handle);
1305 
1306 
1309 void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample);
1310 
1311 
1314 void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample);
1315 
1316 
1317 #ifdef __cplusplus
1318 }
1319 #endif // extern "C"
1320 
1322 #endif // end of _HAL_H_ definition
1323 
1324 
OFFSET_Handle offsetHandle_I[3]
the handles for the current offset estimators
void HAL_setupQEP(HAL_Handle handle, HAL_QepSelect_e qep)
Sets up the QEP peripheral.
static void HAL_readAdcData(HAL_Handle handle, HAL_AdcData_t *pAdcData)
Reads the ADC data.
static void HAL_setBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber, const _iq bias)
Sets the ADC bias value.
void HAL_enableGlobalInts(HAL_Handle handle)
Enables global interrupts.
void HAL_enableAdcInts(HAL_Handle handle)
Enables the ADC interrupts.
static void HAL_initIntVectorTable(HAL_Handle handle)
Initializes the interrupt vector table.
void HAL_disableGlobalInts(HAL_Handle handle)
Disables global interrupts.
static _iq HAL_getBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber)
Gets the ADC bias value.
#define _IQtoIQ15(A)
#define _IQ(A)
static void HAL_setNumVoltageSensors(HAL_Handle handle, const uint_least8_t numVoltageSensors)
Sets the number of voltage sensors.
static _iq HAL_getVoltageScaleFactor(HAL_Handle handle)
Gets the voltage scale factor.
static bool HAL_readGpio(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Reads the specified GPIO pin.
void HAL_OscTempComp(HAL_Handle handle)
Executes the oscillator 1 and 2 calibration functions.
Ignore the B phase shunt measurement.
Definition: svgen_current.h:72
static void HAL_toggleGpio(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Toggles the GPIO pin.
_iq value[3]
Definition: 32b/math.h:261
interrupt void mainISR(void)
static _iq HAL_getOffsetBeta_lp_pu(HAL_Handle handle, const HAL_SensorType_e sensorType, const uint_least8_t sensorNumber)
Gets the value used to set the low pass filter pole for offset estimation.
Ignore the AC phase shunt measurement.
Definition: svgen_current.h:75
static void HAL_enablePwm(HAL_Handle handle)
Enables the PWM devices.
GPIO pin number for ControlCARD LED 3.
static _iq HAL_getCurrentScaleFactor(HAL_Handle handle)
Gets the current scale factor.
Defines a structure for the user parameters.
Ignore the A phase shunt measurement.
Definition: svgen_current.h:71
void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber)
Selects the analog channel used for calibration.
_iq current_sf
the current scale factor, amps_pu/cnt
HAL_QepSelect_e
Enumeration for the QEP setup.
void HAL_setupPeripheralClks(HAL_Handle handle)
Sets up the peripheral clocks.
HAL_SensorType_e
Enumeration for the sensor types.
static void HAL_setTrigger(HAL_Handle handle, const SVGENCURRENT_IgnoreShunt_e ignoreShunt, const int16_t minwidth, const int16_t cmpOffset)
void HAL_setupTimers(HAL_Handle handle, const float_t systemFreq_MHz)
Sets up the timers.
void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 2 calibration based on input sample.
long _iq
void HAL_setupPll(HAL_Handle handle, const PLL_ClkFreq_e clkFreq)
Sets up the PLL (Phase Lock Loop)
HAL_AdcData_t adcBias
the ADC bias
Enumeration for current sensor.
static void HAL_setTimerPeriod(HAL_Handle handle, const uint_least8_t timerNumber, const uint32_t period)
Sets the timer period.
static uint16_t HAL_readPwmCmpA(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM compare register A.
static ADC_SocSampleDelay_e HAL_getAdcSocSampleDelay(HAL_Handle handle, const ADC_SocNumber_e socNumber)
Gets the ADC delay value.
void HAL_setupGpios(HAL_Handle handle)
Sets up the GPIO (General Purpose I/O) pins.
static void HAL_setOffsetInitCond(HAL_Handle handle, const HAL_SensorType_e sensorType, const uint_least8_t sensorNumber, const _iq initCond)
Sets the offset initial condition value for offset estimation.
void HAL_setupFlash(HAL_Handle handle)
Sets up the FLASH.
Ignore the AB phase shunt measurement.
Definition: svgen_current.h:74
void OFFSET_setInitCond(OFFSET_Handle handle, const _iq initCond)
Set the initial condition of the integrator or the value of y[n-1].
Definition: 32b/offset.c:102
static void HAL_writeDacData(HAL_Handle handle, HAL_DacData_t *pDacData)
Writes DAC data to the PWM comparators for DAC (digital-to-analog conversion) output.
void HAL_enablePwmInt(HAL_Handle handle)
Enables the PWM interrupt.
void HAL_setupClks(HAL_Handle handle)
Sets up the clocks.
void HAL_setupFaults(HAL_Handle handle)
Configures the fault protection logic.
static void HAL_setNumCurrentSensors(HAL_Handle handle, const uint_least8_t numCurrentSensors)
Sets the number of current sensors.
static void HAL_disablePwm(HAL_Handle handle)
Disables the PWM device.
MATH_vec3 Tabc
the PWM time-durations for each motor phase
static void HAL_setOffsetBeta_lp_pu(HAL_Handle handle, const HAL_SensorType_e sensorType, const uint_least8_t sensorNumber, const _iq beta_lp_pu)
Sets the value used to set the low pass filter pole for offset estimation.
#define _IQmpy(A, B)
#define _IQ12mpy(A, B)
static void HAL_stopTimer(HAL_Handle handle, const uint_least8_t timerNumber)
Stops the timer.
static void HAL_setCurrentScaleFactor(HAL_Handle handle, const _iq current_sf)
Sets the current scale factor in the hardware abstraction layer.
static void HAL_setGpioHigh(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Sets the GPIO pin high.
static uint_least8_t HAL_getNumCurrentSensors(HAL_Handle handle)
Gets the number of current sensors.
void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 1 calibration based on input sample.
static void HAL_updateAdcBias(HAL_Handle handle)
Updates the ADC bias values.
static void HAL_acqAdcInt(HAL_Handle handle, const ADC_IntNumber_e intNumber)
Acknowledges an interrupt from the ADC so that another ADC interrupt can happen again.
static _iq HAL_getOffsetValue(HAL_Handle handle, const HAL_SensorType_e sensorType, const uint_least8_t sensorNumber)
Gets the offset value.
void OFFSET_setOffset(OFFSET_Handle handle, _iq offsetValue)
Sets the offset value.
Definition: 32b/offset.c:113
Contains the public interface to the Svgen Current module routines.
uint_least8_t numCurrentSensors
the number of current sensors
void HAL_setupPwms(HAL_Handle handle, const float_t systemFreq_MHz, const float_t pwmPeriod_usec, const uint_least16_t numPwmTicksPerIsrTick)
Sets up the PWMs (Pulse Width Modulators)
void HAL_setupPie(HAL_Handle handle)
Sets up the PIE (Peripheral Interrupt Expansion)
void HAL_enableDebugInt(HAL_Handle handle)
Enables the debug interrupt.
SVGENCURRENT_IgnoreShunt_e
Definition: svgen_current.h:68
static void HAL_startTimer(HAL_Handle handle, const uint_least8_t timerNumber)
Starts the timer.
_iq voltage_sf
the voltage scale factor, volts_pu/cnt
static uint_least8_t HAL_getNumVoltageSensors(HAL_Handle handle)
Gets the number of voltage sensors.
static void HAL_setGpioLow(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Sets the GPIO pin low.
Use all shunt measurements.
Definition: svgen_current.h:70
void HAL_AdcOffsetSelfCal(HAL_Handle handle)
Executes the offset calibration of the ADC.
static uint16_t HAL_readPwmCmpAM(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM compare mirror register A.
void OFFSET_setBeta(OFFSET_Handle handle, const _iq beta)
Sets the beta offset filter coefficient.
Definition: 32b/offset.c:88
static uint16_t HAL_readPwmPeriod(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM period register.
ADC_Handle adcHandle
the ADC handle
HAL_Handle HAL_init(void *pMemory, const size_t numBytes)
Initializes the hardware abstraction layer (HAL) object.
static void HAL_setVoltageScaleFactor(HAL_Handle handle, const _iq voltage_sf)
Sets the voltage scale factor in the hardware abstraction layer.
static void HAL_reloadTimer(HAL_Handle handle, const uint_least8_t timerNumber)
Reloads the timer.
static void HAL_writePwmData(HAL_Handle handle, HAL_PwmData_t *pPwmData)
Writes PWM data to the PWM comparators for motor control.
static void HAL_readAdcDataWithOffsets(HAL_Handle handle, HAL_AdcData_t *pAdcData)
Reads the ADC data.
void HAL_cal(HAL_Handle handle)
Executes calibration routines.
_iq OFFSET_getBeta(OFFSET_Handle handle)
Gets the beta offset filter coefficient.
Definition: 32b/offset.c:58
Enumeration for voltage sensor.
uint16_t HAL_AdcCalConversion(HAL_Handle handle)
Reads the converted value from the selected calibration channel.
static uint16_t HAL_readPwmCmpB(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM compare register B.
void HAL_setupPwmDacs(HAL_Handle handle)
Sets up the PWM DACs (Pulse Width Modulator Digital to Analof Converters)
GPIO pin number for ControlCARD LED 2.
static uint32_t HAL_readTimerCnt(HAL_Handle handle, const uint_least8_t timerNumber)
Reads the timer count.
uint16_t HAL_getOscTrimValue(int16_t coarse, int16_t fine)
Converts coarse and fine oscillator trim values into a single 16bit word value.
#define _IQsat(A, Pos, Neg)
void HAL_setupSpiA(HAL_Handle handle)
Sets up the spiA peripheral.
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
Sets the hardware abstraction layer parameters.
void HAL_setupAdcs(HAL_Handle handle)
Sets up the ADCs (Analog to Digital Converters)
uint_least8_t numVoltageSensors
the number of voltage sensors
HAL_LedNumber_e
Enumeration for the LED numbers.
void HAL_disableWdog(HAL_Handle halHandle)
Disables the watch dog.
static uint32_t HAL_getTimerPeriod(HAL_Handle handle, const uint_least8_t timerNumber)
Gets the timer period.
OFFSET_Handle offsetHandle_V[3]
the handles for the voltage offset estimators
static _iq OFFSET_getOffset(OFFSET_Handle handle)
Gets the offset value.
Definition: 32b/offset.h:115
static void HAL_acqPwmInt(HAL_Handle handle, const PWM_Number_e pwmNumber)
Acknowledges an interrupt from the PWM so that another PWM interrupt can happen again.
static void HAL_setAdcSocSampleDelay(HAL_Handle handle, const ADC_SocNumber_e socNumber, const ADC_SocSampleDelay_e sampleDelay)
Sets the ADC SOC sample delay value.
Ignore the C phase shunt measurement.
Definition: svgen_current.h:73
float float_t
Defines the portable data type for 32 bit, signed floating-point data.
Definition: types.h:121
Defines the hardware abstraction layer (HAL) data.
GPIO_Handle gpioHandle
the GPIO handle
static void HAL_setOffsetValue(HAL_Handle handle, const HAL_SensorType_e sensorType, const uint_least8_t sensorNumber, const _iq value)
Sets the initial offset value for offset estimation.