MotorWare f2806x Module API Documentation
hvkit_rev1p1/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 (uint16_t)(2.0 * (float_t)USER_SYSTEM_FREQ_MHz) // 2 usec
111 
112 
115 #define HAL_PWM_DBRED_CNT (uint16_t)(2.0 * (float_t)USER_SYSTEM_FREQ_MHz) // 2 usec
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 
740 static inline void HAL_toggleGpio(HAL_Handle handle,const GPIO_Number_e gpioNumber)
741 {
742  HAL_Obj *obj = (HAL_Obj *)handle;
743 
744 
745  // set GPIO high
746  GPIO_toggle(obj->gpioHandle,gpioNumber);
747 
748  return;
749 } // end of HAL_setGpioHigh() function
750 
751 
757 static inline void HAL_setGpioLow(HAL_Handle handle,const GPIO_Number_e gpioNumber)
758 {
759  HAL_Obj *obj = (HAL_Obj *)handle;
760 
761 
762  // set GPIO low
763  GPIO_setLow(obj->gpioHandle,gpioNumber);
764 
765  return;
766 } // end of HAL_setGpioLow() function
767 
768 
772 static inline void HAL_setCurrentScaleFactor(HAL_Handle handle,const _iq current_sf)
773 {
774  HAL_Obj *obj = (HAL_Obj *)handle;
775 
776 
777  obj->current_sf = current_sf;
778 
779  return;
780 } // end of HAL_setCurrentScaleFactor() function
781 
782 
786 static inline void HAL_setNumCurrentSensors(HAL_Handle handle,const uint_least8_t numCurrentSensors)
787 {
788  HAL_Obj *obj = (HAL_Obj *)handle;
789 
790 
791  obj->numCurrentSensors = numCurrentSensors;
792 
793  return;
794 } // end of HAL_setNumCurrentSensors() function
795 
796 
800 static inline void HAL_setNumVoltageSensors(HAL_Handle handle,const uint_least8_t numVoltageSensors)
801 {
802  HAL_Obj *obj = (HAL_Obj *)handle;
803 
804 
805  obj->numVoltageSensors = numVoltageSensors;
806 
807  return;
808 } // end of HAL_setNumVoltageSensors() function
809 
810 
816 static inline void HAL_setOffsetBeta_lp_pu(HAL_Handle handle,
817  const HAL_SensorType_e sensorType,
818  const uint_least8_t sensorNumber,
819  const _iq beta_lp_pu)
820 {
821  HAL_Obj *obj = (HAL_Obj *)handle;
822 
823  if(sensorType == HAL_SensorType_Current)
824  {
825  OFFSET_setBeta(obj->offsetHandle_I[sensorNumber],beta_lp_pu);
826  }
827  else if(sensorType == HAL_SensorType_Voltage)
828  {
829  OFFSET_setBeta(obj->offsetHandle_V[sensorNumber],beta_lp_pu);
830  }
831 
832  return;
833 } // end of HAL_setOffsetBeta_lp_pu() function
834 
835 
841 static inline void HAL_setOffsetInitCond(HAL_Handle handle,
842  const HAL_SensorType_e sensorType,
843  const uint_least8_t sensorNumber,
844  const _iq initCond)
845 {
846  HAL_Obj *obj = (HAL_Obj *)handle;
847 
848  if(sensorType == HAL_SensorType_Current)
849  {
850  OFFSET_setInitCond(obj->offsetHandle_I[sensorNumber],initCond);
851  }
852  else if(sensorType == HAL_SensorType_Voltage)
853  {
854  OFFSET_setInitCond(obj->offsetHandle_V[sensorNumber],initCond);
855  }
856 
857  return;
858 } // end of HAL_setOffsetInitCond() function
859 
860 
866 static inline void HAL_setOffsetValue(HAL_Handle handle,
867  const HAL_SensorType_e sensorType,
868  const uint_least8_t sensorNumber,
869  const _iq value)
870 {
871  HAL_Obj *obj = (HAL_Obj *)handle;
872 
873  if(sensorType == HAL_SensorType_Current)
874  {
875  OFFSET_setOffset(obj->offsetHandle_I[sensorNumber],value);
876  }
877  else if(sensorType == HAL_SensorType_Voltage)
878  {
879  OFFSET_setOffset(obj->offsetHandle_V[sensorNumber],value);
880  }
881 
882  return;
883 } // end of HAL_setOffsetValue() function
884 
885 
889 static inline void HAL_setVoltageScaleFactor(HAL_Handle handle,const _iq voltage_sf)
890 {
891  HAL_Obj *obj = (HAL_Obj *)handle;
892 
893  obj->voltage_sf = voltage_sf;
894 
895  return;
896 } // end of HAL_setVoltageScaleFactor() function
897 
898 
905 extern void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams);
906 
907 
910 extern void HAL_setupAdcs(HAL_Handle handle);
911 
912 
916 extern void HAL_setupClks(HAL_Handle handle);
917 
918 
921 extern void HAL_setupFlash(HAL_Handle handle);
922 
923 
926 extern void HAL_setupGpios(HAL_Handle handle);
927 
928 
931 extern void HAL_setupPeripheralClks(HAL_Handle handle);
932 
933 
936 extern void HAL_setupPie(HAL_Handle handle);
937 
938 
942 extern void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq);
943 
944 
950 extern void HAL_setupPwms(HAL_Handle handle,
951  const float_t systemFreq_MHz,
952  const float_t pwmPeriod_usec,
953  const uint_least16_t numPwmTicksPerIsrTick);
954 
955 
958 extern void HAL_setupPwmDacs(HAL_Handle handle);
959 
960 
963 extern void HAL_setupQEP(HAL_Handle handle,HAL_QepSelect_e qep);
964 
965 
969 void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz);
970 
971 
976 static inline void HAL_updateAdcBias(HAL_Handle handle)
977 {
978  uint_least8_t cnt;
979  HAL_Obj *obj = (HAL_Obj *)handle;
980  _iq bias;
981 
982 
983  // update the current bias
984  for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
985  {
986  bias = HAL_getBias(handle,HAL_SensorType_Current,cnt);
987 
988  bias += OFFSET_getOffset(obj->offsetHandle_I[cnt]);
989 
990  HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
991  }
992 
993 
994  // update the voltage bias
995  for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
996  {
997  bias = HAL_getBias(handle,HAL_SensorType_Voltage,cnt);
998 
999  bias += OFFSET_getOffset(obj->offsetHandle_V[cnt]);
1000 
1001  HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
1002  }
1003 
1004  return;
1005 } // end of HAL_updateAdcBias() function
1006 
1007 
1011 static inline void HAL_writeDacData(HAL_Handle handle,HAL_DacData_t *pDacData)
1012 {
1013  HAL_Obj *obj = (HAL_Obj *)handle;
1014 
1015  // convert values from _IQ to _IQ15
1016  int16_t dacValue_1 = (int16_t)_IQtoIQ15(pDacData->value[0]);
1017  int16_t dacValue_2 = (int16_t)_IQtoIQ15(pDacData->value[1]);
1018  int16_t dacValue_3 = (int16_t)_IQtoIQ15(pDacData->value[2]);
1019  int16_t dacValue_4 = (int16_t)_IQtoIQ15(pDacData->value[3]);
1020 
1021  // write the DAC data
1022  PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_1],dacValue_1);
1023  PWMDAC_write_CmpB(obj->pwmDacHandle[PWMDAC_Number_1],dacValue_2);
1024  PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_2],dacValue_3);
1025  PWMDAC_write_CmpA(obj->pwmDacHandle[PWMDAC_Number_3],dacValue_4);
1026 
1027  return;
1028 } // end of HAL_writeDacData() function
1029 
1030 
1034 static inline void HAL_writePwmData(HAL_Handle handle,HAL_PwmData_t *pPwmData)
1035 {
1036  uint_least8_t cnt;
1037  HAL_Obj *obj = (HAL_Obj *)handle;
1038  PWM_Obj *pwm;
1039  _iq period;
1040  _iq pwmData_neg;
1041  _iq pwmData_sat;
1042  _iq pwmData_sat_dc;
1043  _iq value;
1044  uint16_t value_sat;
1045 
1046  for(cnt=0;cnt<3;cnt++)
1047  {
1048  pwm = (PWM_Obj *)obj->pwmHandle[cnt];
1049  period = (_iq)pwm->TBPRD;
1050  pwmData_neg = _IQmpy(pPwmData->Tabc.value[cnt],_IQ(-1.0));
1051  pwmData_sat = _IQsat(pwmData_neg,_IQ(0.5),_IQ(-0.5));
1052  pwmData_sat_dc = pwmData_sat + _IQ(0.5);
1053  value = _IQmpy(pwmData_sat_dc, period);
1054  value_sat = (uint16_t)_IQsat(value, period, _IQ(0.0));
1055 
1056  // write the PWM data
1057  PWM_write_CmpA(obj->pwmHandle[cnt],value_sat);
1058  }
1059 
1060  return;
1061 } // end of HAL_writePwmData() function
1062 
1063 
1068 static inline uint16_t HAL_readPwmCmpA(HAL_Handle handle,const PWM_Number_e pwmNumber)
1069 {
1070  HAL_Obj *obj = (HAL_Obj *)handle;
1071 
1072  // the compare value to be returned
1073  uint16_t pwmValue;
1074 
1075  pwmValue = PWM_get_CmpA(obj->pwmHandle[pwmNumber]);
1076 
1077  return(pwmValue);
1078 } // end of HAL_readPwmCmpA() function
1079 
1080 
1085 static inline uint16_t HAL_readPwmCmpAM(HAL_Handle handle,const PWM_Number_e pwmNumber)
1086 {
1087  HAL_Obj *obj = (HAL_Obj *)handle;
1088 
1089  // the compare value to be returned
1090  uint16_t pwmValue;
1091 
1092  pwmValue = PWM_get_CmpAM(obj->pwmHandle[pwmNumber]);
1093 
1094  return(pwmValue);
1095 } // end of HAL_readPwmCmpAM() function
1096 
1097 
1102 static inline uint16_t HAL_readPwmCmpB(HAL_Handle handle,const PWM_Number_e pwmNumber)
1103 {
1104  HAL_Obj *obj = (HAL_Obj *)handle;
1105 
1106  // the compare value to be returned
1107  uint16_t pwmValue;
1108 
1109  pwmValue = PWM_get_CmpB(obj->pwmHandle[pwmNumber]);
1110 
1111  return(pwmValue);
1112 } // end of HAL_readPwmCmpB() function
1113 
1114 
1119 static inline uint16_t HAL_readPwmPeriod(HAL_Handle handle,const PWM_Number_e pwmNumber)
1120 {
1121  HAL_Obj *obj = (HAL_Obj *)handle;
1122 
1123  // the period value to be returned
1124  uint16_t pwmPeriodValue;
1125 
1126  pwmPeriodValue = PWM_getPeriod(obj->pwmHandle[pwmNumber]);
1127 
1128  return(pwmPeriodValue);
1129 } // end of HAL_readPwmPeriod() function
1130 
1131 
1132 static inline void HAL_setTrigger(HAL_Handle handle,const SVGENCURRENT_IgnoreShunt_e ignoreShunt,const int16_t minwidth, const int16_t cmpOffset)
1133 {
1134  HAL_Obj *obj = (HAL_Obj *)handle;
1135 
1136  PWM_Obj *pwm1 = (PWM_Obj *)obj->pwmHandle[PWM_Number_1];
1137  PWM_Obj *pwm2 = (PWM_Obj *)obj->pwmHandle[PWM_Number_2];
1138  PWM_Obj *pwm3 = (PWM_Obj *)obj->pwmHandle[PWM_Number_3];
1139  PWM_Obj *pwm;
1140 
1141  uint16_t nextPulse1 = (pwm1->CMPA + pwm1->CMPAM) / 2;
1142  uint16_t nextPulse2 = (pwm2->CMPA + pwm2->CMPAM) / 2;
1143  uint16_t nextPulse3 = (pwm3->CMPA + pwm3->CMPAM) / 2;
1144  uint16_t pwmCMPA1 = pwm1->CMPA;
1145  uint16_t pwmCMPA2 = pwm2->CMPA;
1146  uint16_t pwmCMPA3 = pwm3->CMPA;
1147 
1148  int16_t offset;
1149 
1150  if(ignoreShunt == use_all)
1151  {
1152  if((nextPulse1 <= nextPulse2) && (nextPulse1 <= nextPulse3))
1153  {
1154  pwm = pwm1;
1155  }
1156  else if((nextPulse2 <= nextPulse1) && (nextPulse2 <= nextPulse3))
1157  {
1158  pwm = pwm2;
1159  }
1160  else
1161  {
1162  pwm = pwm3;
1163  }
1164  }
1165  else if(ignoreShunt == ignore_a)
1166  {
1167  offset = pwmCMPA1 + cmpOffset;
1168  }
1169  else if(ignoreShunt == ignore_b)
1170  {
1171  offset = pwmCMPA2 + cmpOffset;
1172  }
1173  else if(ignoreShunt == ignore_c)
1174  {
1175  offset = pwmCMPA3 + cmpOffset;
1176  }
1177  else if(ignoreShunt == ignore_ab)
1178  {
1179  if(pwmCMPA1 > pwmCMPA2)
1180  {
1181  offset = pwmCMPA1 + cmpOffset;
1182  }
1183  else
1184  {
1185  offset = pwmCMPA2 + cmpOffset;
1186  }
1187  }
1188  else if(ignoreShunt == ignore_ac)
1189  {
1190  if(pwmCMPA1 > pwmCMPA3)
1191  {
1192  offset = pwmCMPA1 + cmpOffset;
1193  }
1194  else
1195  {
1196  offset = pwmCMPA3 + cmpOffset;
1197  }
1198  }
1199  else // when ignoreShunt == ignore_bc
1200  {
1201  if(pwmCMPA2 > pwmCMPA3)
1202  {
1203  offset = pwmCMPA2 + cmpOffset;
1204  }
1205  else
1206  {
1207  offset = pwmCMPA3 + cmpOffset;
1208  }
1209  }
1210 
1211 
1212  if(ignoreShunt == use_all)
1213  {
1214  if(pwm->CMPAM >= (pwm->CMPA + pwm->DBFED))
1215  {
1216  pwm1->CMPB = (pwm->CMPAM - (pwm->CMPA + pwm->DBFED)) / 2 + 1;
1217  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualCmpBDecr);
1218  }
1219  else
1220  {
1221  pwm1->CMPB = ((pwm->CMPA + pwm->DBFED) - pwm->CMPAM ) / 2 + 1;
1222  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualCmpBIncr);
1223  }
1224  }
1225  else
1226  {
1227  pwm1->CMPB = offset;
1228  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualCmpBIncr);
1229  }
1230 
1231  return;
1232 } // end of HAL_setTrigger() function
1233 
1234 #ifdef QEP
1235 static inline uint32_t HAL_getQepPosnCounts(HAL_Handle handle)
1239 {
1240  HAL_Obj *obj = (HAL_Obj *)handle;
1241  QEP_Obj *qep = (QEP_Obj *)obj->qepHandle[0];
1242 
1243  return qep->QPOSCNT;
1244 }
1245 
1246 
1250 static inline uint32_t HAL_getQepPosnMaximum(HAL_Handle handle)
1251 {
1252  HAL_Obj *obj = (HAL_Obj *)handle;
1253  QEP_Obj *qep = (QEP_Obj *)obj->qepHandle[0];
1254 
1255  return qep->QPOSMAX;
1256 }
1257 #endif
1258 
1262 void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber);
1263 
1264 
1268 uint16_t HAL_AdcCalConversion(HAL_Handle handle);
1269 
1270 
1273 void HAL_AdcOffsetSelfCal(HAL_Handle handle);
1274 
1275 
1281 uint16_t HAL_getOscTrimValue(int16_t coarse, int16_t fine);
1282 
1283 
1286 void HAL_OscTempComp(HAL_Handle handle);
1287 
1288 
1291 void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample);
1292 
1293 
1296 void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample);
1297 
1298 
1299 #ifdef __cplusplus
1300 }
1301 #endif // extern "C"
1302 
1304 #endif // end of _HAL_H_ definition
1305 
1306 
static void HAL_setTrigger(HAL_Handle handle, const SVGENCURRENT_IgnoreShunt_e ignoreShunt, const int16_t minwidth, const int16_t cmpOffset)
static void HAL_setAdcSocSampleDelay(HAL_Handle handle, const ADC_SocNumber_e socNumber, const ADC_SocSampleDelay_e sampleDelay)
Sets the ADC SOC sample delay value.
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.
void HAL_enableGlobalInts(HAL_Handle handle)
Enables global interrupts.
void HAL_enableAdcInts(HAL_Handle handle)
Enables the ADC interrupts.
void HAL_disableGlobalInts(HAL_Handle handle)
Disables global interrupts.
#define _IQtoIQ15(A)
static void HAL_initIntVectorTable(HAL_Handle handle)
Initializes the interrupt vector table.
#define _IQ(A)
static void HAL_startTimer(HAL_Handle handle, const uint_least8_t timerNumber)
Starts the timer.
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
_iq value[3]
Definition: 32b/math.h:261
static uint16_t HAL_readPwmCmpAM(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM compare mirror register A.
interrupt void mainISR(void)
Ignore the AC phase shunt measurement.
Definition: svgen_current.h:75
GPIO pin number for ControlCARD LED 3.
Defines a structure for the user parameters.
Ignore the A phase shunt measurement.
Definition: svgen_current.h:71
static ADC_SocSampleDelay_e HAL_getAdcSocSampleDelay(HAL_Handle handle, const ADC_SocNumber_e socNumber)
Gets the ADC delay value.
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_setNumVoltageSensors(HAL_Handle handle, const uint_least8_t numVoltageSensors)
Sets the number of voltage sensors.
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.
void HAL_setupGpios(HAL_Handle handle)
Sets up the GPIO (General Purpose I/O) pins.
static void HAL_readAdcData(HAL_Handle handle, HAL_AdcData_t *pAdcData)
Reads the ADC data.
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
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.
MATH_vec3 Tabc
the PWM time-durations for each motor phase
static _iq HAL_getVoltageScaleFactor(HAL_Handle handle)
Gets the voltage scale factor.
#define _IQmpy(A, B)
#define _IQ12mpy(A, B)
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.
void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 1 calibration based on input sample.
static _iq HAL_getCurrentScaleFactor(HAL_Handle handle)
Gets the current scale factor.
static void HAL_readAdcDataWithOffsets(HAL_Handle handle, HAL_AdcData_t *pAdcData)
Reads the ADC data.
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.
static void HAL_setNumCurrentSensors(HAL_Handle handle, const uint_least8_t numCurrentSensors)
Sets the number of current sensors.
SVGENCURRENT_IgnoreShunt_e
Definition: svgen_current.h:68
static void HAL_enablePwm(HAL_Handle handle)
Enables the PWM devices.
static void HAL_setBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber, const _iq bias)
Sets the ADC bias value.
_iq voltage_sf
the voltage scale factor, volts_pu/cnt
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 uint16_t HAL_readPwmPeriod(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM period register.
Use all shunt measurements.
Definition: svgen_current.h:70
void HAL_AdcOffsetSelfCal(HAL_Handle handle)
Executes the offset calibration of the ADC.
static uint32_t HAL_getTimerPeriod(HAL_Handle handle, const uint_least8_t timerNumber)
Gets the timer period.
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.
static void HAL_setTimerPeriod(HAL_Handle handle, const uint_least8_t timerNumber, const uint32_t period)
Sets the timer period.
static uint32_t HAL_readTimerCnt(HAL_Handle handle, const uint_least8_t timerNumber)
Reads the timer count.
static uint16_t HAL_readPwmCmpB(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM compare register B.
static void HAL_writePwmData(HAL_Handle handle, HAL_PwmData_t *pPwmData)
Writes PWM data to the PWM comparators for motor control.
void OFFSET_setBeta(OFFSET_Handle handle, const _iq beta)
Sets the beta offset filter coefficient.
Definition: 32b/offset.c:88
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_updateAdcBias(HAL_Handle handle)
Updates the ADC bias values.
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_setGpioHigh(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Sets the GPIO pin high.
static _iq HAL_getOffsetValue(HAL_Handle handle, const HAL_SensorType_e sensorType, const uint_least8_t sensorNumber)
Gets the offset value.
static void HAL_setGpioLow(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Sets the GPIO pin low.
static uint16_t HAL_readPwmCmpA(HAL_Handle handle, const PWM_Number_e pwmNumber)
Reads PWM compare register A.
static void HAL_stopTimer(HAL_Handle handle, const uint_least8_t timerNumber)
Stops the timer.
static uint_least8_t HAL_getNumVoltageSensors(HAL_Handle handle)
Gets the number of voltage sensors.
void HAL_cal(HAL_Handle handle)
Executes calibration routines.
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.
_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.
void HAL_setupPwmDacs(HAL_Handle handle)
Sets up the PWM DACs (Pulse Width Modulator Digital to Analof Converters)
static void HAL_toggleGpio(HAL_Handle handle, const GPIO_Number_e gpioNumber)
Toggles the GPIO pin.
GPIO pin number for ControlCARD LED 2.
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)
static void HAL_setCurrentScaleFactor(HAL_Handle handle, const _iq current_sf)
Sets the current scale factor in the hardware abstraction layer.
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)
static _iq HAL_getBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber)
Gets the ADC bias value.
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.
static uint_least8_t HAL_getNumCurrentSensors(HAL_Handle handle)
Gets the number of current sensors.
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.
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
Ignore the C phase shunt measurement.
Definition: svgen_current.h:73
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.
static void HAL_reloadTimer(HAL_Handle handle, const uint_least8_t timerNumber)
Reloads the timer.
static void HAL_disablePwm(HAL_Handle handle)
Disables the PWM device.
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_setVoltageScaleFactor(HAL_Handle handle, const _iq voltage_sf)
Sets the voltage scale factor in the hardware abstraction layer.