MotorWare f2806x Module API Documentation
drv8312kit_revD/f28x/f2806x/src/hal.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2012, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
36 
37 
38 // **************************************************************************
39 // the includes
40 
41 // drivers
42 
43 // modules
44 
45 // platforms
46 #include "hal.h"
47 #include "user.h"
48 #include "hal_obj.h"
49 
50 #ifdef FLASH
51 #pragma CODE_SECTION(HAL_setupFlash,"ramfuncs");
52 #endif
53 
54 // **************************************************************************
55 // the defines
56 
57 
58 // **************************************************************************
59 // the globals
60 
62 
63 
64 // **************************************************************************
65 // the functions
66 
67 void HAL_cal(HAL_Handle handle)
68 {
69  HAL_Obj *obj = (HAL_Obj *)handle;
70 
71 
72  // enable the ADC clock
73  CLK_enableAdcClock(obj->clkHandle);
74 
75 
76  // Run the Device_cal() function
77  // This function copies the ADC and oscillator calibration values from TI reserved
78  // OTP into the appropriate trim registers
79  // This boot ROM automatically calls this function to calibrate the interal
80  // oscillators and ADC with device specific calibration data.
81  // If the boot ROM is bypassed by Code Composer Studio during the development process,
82  // then the calibration must be initialized by the application
83  ENABLE_PROTECTED_REGISTER_WRITE_MODE;
84  (*Device_cal)();
85  DISABLE_PROTECTED_REGISTER_WRITE_MODE;
86 
87  // run offsets calibration in user's memory
88  HAL_AdcOffsetSelfCal(handle);
89 
90  // run oscillator compensation
91  HAL_OscTempComp(handle);
92 
93  // disable the ADC clock
94  CLK_disableAdcClock(obj->clkHandle);
95 
96  return;
97 } // end of HAL_cal() function
98 
99 
101 {
102  HAL_Obj *obj = (HAL_Obj *)handle;
103  uint16_t Temperature;
104 
105  // disable the ADCs
106  ADC_disable(obj->adcHandle);
107 
108  // power up the bandgap circuit
109  ADC_enableBandGap(obj->adcHandle);
110 
111  // set the ADC voltage reference source to internal
112  ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
113 
114  // enable the ADC reference buffers
115  ADC_enableRefBuffers(obj->adcHandle);
116 
117  // Set main clock scaling factor (max45MHz clock for the ADC module)
118  ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
119 
120  // power up the ADCs
121  ADC_powerUp(obj->adcHandle);
122 
123  // enable the ADCs
124  ADC_enable(obj->adcHandle);
125 
126  // enable non-overlap mode
127  ADC_enableNoOverlapMode(obj->adcHandle);
128 
129  // connect channel A5 internally to the temperature sensor
130  ADC_setTempSensorSrc(obj->adcHandle, ADC_TempSensorSrc_Int);
131 
132  // set SOC0 channel select to ADCINA5
133  ADC_setSocChanNumber(obj->adcHandle, ADC_SocNumber_0, ADC_SocChanNumber_A5);
134 
135  // set SOC0 acquisition period to 26 ADCCLK
136  ADC_setSocSampleDelay(obj->adcHandle, ADC_SocNumber_0, ADC_SocSampleDelay_64_cycles);
137 
138  // connect ADCINT1 to EOC0
139  ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC0);
140 
141  // clear ADCINT1 flag
142  ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
143 
144  // enable ADCINT1
145  ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
146 
147  // force start of conversion on SOC0
148  ADC_setSocFrc(obj->adcHandle, ADC_SocFrc_0);
149 
150  // wait for end of conversion
151  while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
152 
153  // clear ADCINT1 flag
154  ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
155 
156  Temperature = ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
157 
158  HAL_osc1Comp(handle, Temperature);
159 
160  HAL_osc2Comp(handle, Temperature);
161 
162  return;
163 } // end of HAL_OscTempComp() function
164 
165 
166 void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
167 {
168  int16_t compOscFineTrim;
169  HAL_Obj *obj = (HAL_Obj *)handle;
170 
171  ENABLE_PROTECTED_REGISTER_WRITE_MODE;
172 
173  compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc1FineTrimSlope()
175 
176  if(compOscFineTrim > 31)
177  {
178  compOscFineTrim = 31;
179  }
180  else if(compOscFineTrim < -31)
181  {
182  compOscFineTrim = -31;
183  }
184 
185  OSC_setTrim(obj->oscHandle, OSC_Number_1, HAL_getOscTrimValue(getOsc1CoarseTrim(), compOscFineTrim));
186 
187  DISABLE_PROTECTED_REGISTER_WRITE_MODE;
188 
189  return;
190 } // end of HAL_osc1Comp() function
191 
192 
193 void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
194 {
195  int16_t compOscFineTrim;
196  HAL_Obj *obj = (HAL_Obj *)handle;
197 
198  ENABLE_PROTECTED_REGISTER_WRITE_MODE;
199 
200  compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc2FineTrimSlope()
202 
203  if(compOscFineTrim > 31)
204  {
205  compOscFineTrim = 31;
206  }
207  else if(compOscFineTrim < -31)
208  {
209  compOscFineTrim = -31;
210  }
211 
212  OSC_setTrim(obj->oscHandle, OSC_Number_2, HAL_getOscTrimValue(getOsc2CoarseTrim(), compOscFineTrim));
213 
214  DISABLE_PROTECTED_REGISTER_WRITE_MODE;
215 
216  return;
217 } // end of HAL_osc2Comp() function
218 
219 
220 uint16_t HAL_getOscTrimValue(int16_t coarse, int16_t fine)
221 {
222  uint16_t regValue = 0;
223 
224  if(fine < 0)
225  {
226  regValue = ((-fine) | 0x20) << 9;
227  }
228  else
229  {
230  regValue = fine << 9;
231  }
232 
233  if(coarse < 0)
234  {
235  regValue |= ((-coarse) | 0x80);
236  }
237  else
238  {
239  regValue |= coarse;
240  }
241 
242  return regValue;
243 } // end of HAL_getOscTrimValue() function
244 
245 
247 {
248  HAL_Obj *obj = (HAL_Obj *)handle;
249  uint16_t AdcConvMean;
250 
251  // disable the ADCs
252  ADC_disable(obj->adcHandle);
253 
254  // power up the bandgap circuit
255  ADC_enableBandGap(obj->adcHandle);
256 
257  // set the ADC voltage reference source to internal
258  ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
259 
260  // enable the ADC reference buffers
261  ADC_enableRefBuffers(obj->adcHandle);
262 
263  // Set main clock scaling factor (max45MHz clock for the ADC module)
264  ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
265 
266  // power up the ADCs
267  ADC_powerUp(obj->adcHandle);
268 
269  // enable the ADCs
270  ADC_enable(obj->adcHandle);
271 
272  //Select VREFLO internal connection on B5
273  ADC_enableVoltRefLoConv(obj->adcHandle);
274 
275  //Select channel B5 for all SOC
276  HAL_AdcCalChanSelect(handle, ADC_SocChanNumber_B5);
277 
278  //Apply artificial offset (+80) to account for a negative offset that may reside in the ADC core
279  ADC_setOffTrim(obj->adcHandle, 80);
280 
281  //Capture ADC conversion on VREFLO
282  AdcConvMean = HAL_AdcCalConversion(handle);
283 
284  //Set offtrim register with new value (i.e remove artical offset (+80) and create a two's compliment of the offset error)
285  ADC_setOffTrim(obj->adcHandle, 80 - AdcConvMean);
286 
287  //Select external ADCIN5 input pin on B5
288  ADC_disableVoltRefLoConv(obj->adcHandle);
289 
290  return;
291 } // end of HAL_AdcOffsetSelfCal() function
292 
293 
294 void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber)
295 {
296  HAL_Obj *obj = (HAL_Obj *)handle;
297 
298  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,chanNumber);
299  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,chanNumber);
300  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,chanNumber);
301  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,chanNumber);
302  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,chanNumber);
303  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,chanNumber);
304  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,chanNumber);
305  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,chanNumber);
306  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_8,chanNumber);
307  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_9,chanNumber);
308  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_10,chanNumber);
309  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_11,chanNumber);
310  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_12,chanNumber);
311  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_13,chanNumber);
312  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_14,chanNumber);
313  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_15,chanNumber);
314 
315  return;
316 } // end of HAL_AdcCalChanSelect() function
317 
318 
320 {
321  HAL_Obj *obj = (HAL_Obj *)handle;
322  uint16_t index, SampleSize, Mean;
323  uint32_t Sum;
324  ADC_SocSampleDelay_e ACQPS_Value;
325 
326  index = 0; //initialize index to 0
327  SampleSize = 256; //set sample size to 256 (**NOTE: Sample size must be multiples of 2^x where is an integer >= 4)
328  Sum = 0; //set sum to 0
329  Mean = 999; //initialize mean to known value
330 
331  //Set the ADC sample window to the desired value (Sample window = ACQPS + 1)
332  ACQPS_Value = ADC_SocSampleDelay_7_cycles;
333 
334  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ACQPS_Value);
335  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ACQPS_Value);
336  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ACQPS_Value);
337  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ACQPS_Value);
338  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ACQPS_Value);
339  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ACQPS_Value);
340  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ACQPS_Value);
341  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ACQPS_Value);
342  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_8,ACQPS_Value);
343  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_9,ACQPS_Value);
344  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_10,ACQPS_Value);
345  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_11,ACQPS_Value);
346  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_12,ACQPS_Value);
347  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_13,ACQPS_Value);
348  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_14,ACQPS_Value);
349  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_15,ACQPS_Value);
350 
351  // Enabled ADCINT1 and ADCINT2
352  ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
353  ADC_enableInt(obj->adcHandle, ADC_IntNumber_2);
354 
355  // Disable continuous sampling for ADCINT1 and ADCINT2
356  ADC_setIntMode(obj->adcHandle, ADC_IntNumber_1, ADC_IntMode_EOC);
357  ADC_setIntMode(obj->adcHandle, ADC_IntNumber_2, ADC_IntMode_EOC);
358 
359  //ADCINTs trigger at end of conversion
360  ADC_setIntPulseGenMode(obj->adcHandle, ADC_IntPulseGenMode_Prior);
361 
362  // Setup ADCINT1 and ADCINT2 trigger source
363  ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC6);
364  ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_2, ADC_IntSrc_EOC14);
365 
366  // Setup each SOC's ADCINT trigger source
367  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_0, ADC_Int2TriggersSOC);
368  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_1, ADC_Int2TriggersSOC);
369  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_2, ADC_Int2TriggersSOC);
370  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_3, ADC_Int2TriggersSOC);
371  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_4, ADC_Int2TriggersSOC);
372  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_5, ADC_Int2TriggersSOC);
373  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_6, ADC_Int2TriggersSOC);
374  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_7, ADC_Int2TriggersSOC);
375  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_8, ADC_Int1TriggersSOC);
376  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_9, ADC_Int1TriggersSOC);
377  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_10, ADC_Int1TriggersSOC);
378  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_11, ADC_Int1TriggersSOC);
379  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_12, ADC_Int1TriggersSOC);
380  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_13, ADC_Int1TriggersSOC);
381  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_14, ADC_Int1TriggersSOC);
382  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_15, ADC_Int1TriggersSOC);
383 
384  // Delay before converting ADC channels
385  usDelay(ADC_DELAY_usec);
386 
387  ADC_setSocFrcWord(obj->adcHandle, 0x00FF);
388 
389  while( index < SampleSize )
390  {
391  //Wait for ADCINT1 to trigger, then add ADCRESULT0-7 registers to sum
392  while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
393 
394  //Must clear ADCINT1 flag since INT1CONT = 0
395  ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
396 
397  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
398  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_1);
399  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_2);
400  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_3);
401  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_4);
402  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_5);
403  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_6);
404  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_7);
405 
406  //Wait for ADCINT2 to trigger, then add ADCRESULT8-15 registers to sum
407  while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_2) == 0){}
408 
409  //Must clear ADCINT2 flag since INT2CONT = 0
410  ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_2);
411 
412  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_8);
413  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_9);
414  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_10);
415  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_11);
416  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_12);
417  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_13);
418  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_14);
419  Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_15);
420 
421  index+=16;
422 
423  } // end data collection
424 
425  //Disable ADCINT1 and ADCINT2 to STOP the ping-pong sampling
426  ADC_disableInt(obj->adcHandle, ADC_IntNumber_1);
427  ADC_disableInt(obj->adcHandle, ADC_IntNumber_2);
428 
429  //Calculate average ADC sample value
430  Mean = Sum / SampleSize;
431 
432  // Clear start of conversion trigger
433  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_0, ADC_NoIntTriggersSOC);
434  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_1, ADC_NoIntTriggersSOC);
435  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_2, ADC_NoIntTriggersSOC);
436  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_3, ADC_NoIntTriggersSOC);
437  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_4, ADC_NoIntTriggersSOC);
438  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_5, ADC_NoIntTriggersSOC);
439  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_6, ADC_NoIntTriggersSOC);
440  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_7, ADC_NoIntTriggersSOC);
441  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_8, ADC_NoIntTriggersSOC);
442  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_9, ADC_NoIntTriggersSOC);
443  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_10, ADC_NoIntTriggersSOC);
444  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_11, ADC_NoIntTriggersSOC);
445  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_12, ADC_NoIntTriggersSOC);
446  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_13, ADC_NoIntTriggersSOC);
447  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_14, ADC_NoIntTriggersSOC);
448  ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_15, ADC_NoIntTriggersSOC);
449 
450  //return the average
451  return(Mean);
452 } // end of HAL_AdcCalConversion() function
453 
454 
456 {
457  HAL_Obj *hal = (HAL_Obj *)halHandle;
458 
459 
460  WDOG_disable(hal->wdogHandle);
461 
462 
463  return;
464 } // end of HAL_disableWdog() function
465 
466 
468 {
469  HAL_Obj *obj = (HAL_Obj *)handle;
470 
471 
472  CPU_disableGlobalInts(obj->cpuHandle);
473 
474  return;
475 } // end of HAL_disableGlobalInts() function
476 
477 
479 {
480  HAL_Obj *obj = (HAL_Obj *)handle;
481 
482 
483  // enable the PIE interrupts associated with the ADC interrupts
484  PIE_enableAdcInt(obj->pieHandle,ADC_IntNumber_1);
485 
486 
487  // enable the ADC interrupts
488  ADC_enableInt(obj->adcHandle,ADC_IntNumber_1);
489 
490 
491  // enable the cpu interrupt for ADC interrupts
492  CPU_enableInt(obj->cpuHandle,CPU_IntNumber_10);
493 
494  return;
495 } // end of HAL_enableAdcInts() function
496 
497 
499 {
500  HAL_Obj *obj = (HAL_Obj *)handle;
501 
502 
503  CPU_enableDebugInt(obj->cpuHandle);
504 
505  return;
506 } // end of HAL_enableDebugInt() function
507 
508 
510 {
511  HAL_Obj *obj = (HAL_Obj *)handle;
512 
513 
514  CPU_enableGlobalInts(obj->cpuHandle);
515 
516  return;
517 } // end of HAL_enableGlobalInts() function
518 
519 
521 {
522  HAL_Obj *obj = (HAL_Obj *)handle;
523 
524 
525  PIE_enablePwmInt(obj->pieHandle,PWM_Number_1);
526 
527 
528  // enable the interrupt
529  PWM_enableInt(obj->pwmHandle[PWM_Number_1]);
530 
531 
532  // enable the cpu interrupt for EPWM1_INT
533  CPU_enableInt(obj->cpuHandle,CPU_IntNumber_3);
534 
535  return;
536 } // end of HAL_enablePwmInt() function
537 
538 
540 {
541  HAL_Obj *obj = (HAL_Obj *)handle;
542  uint_least8_t cnt;
543 
544 
545  // Configure Trip Mechanism for the Motor control software
546  // -Cycle by cycle trip on CPU halt
547  // -One shot fault trip zone
548  // These trips need to be repeated for EPWM1 ,2 & 3
549  for(cnt=0;cnt<3;cnt++)
550  {
551  PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ6_NOT);
552 
553  PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_OneShot_TZ3_NOT);
554 
555  PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_OneShot_TZ2_NOT);
556 
557  // What do we want the OST/CBC events to do?
558  // TZA events can force EPWMxA
559  // TZB events can force EPWMxB
560 
561  PWM_setTripZoneState_TZA(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
562  PWM_setTripZoneState_TZB(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
563  }
564 
565  return;
566 } // end of HAL_setupFaults() function
567 
568 
569 HAL_Handle HAL_init(void *pMemory,const size_t numBytes)
570 {
571  uint_least8_t cnt;
572  HAL_Handle handle;
573  HAL_Obj *obj;
574 
575 
576  if(numBytes < sizeof(HAL_Obj))
577  return((HAL_Handle)NULL);
578 
579 
580  // assign the handle
581  handle = (HAL_Handle)pMemory;
582 
583 
584  // assign the object
585  obj = (HAL_Obj *)handle;
586 
587 
588  // initialize the watchdog driver
589  obj->wdogHandle = WDOG_init((void *)WDOG_BASE_ADDR,sizeof(WDOG_Obj));
590 
591 
592  // disable watchdog
593  HAL_disableWdog(handle);
594 
595 
596  // initialize the ADC
597  obj->adcHandle = ADC_init((void *)ADC_BASE_ADDR,sizeof(ADC_Obj));
598 
599 
600  // initialize the clock handle
601  obj->clkHandle = CLK_init((void *)CLK_BASE_ADDR,sizeof(CLK_Obj));
602 
603 
604  // initialize the CPU handle
605  obj->cpuHandle = CPU_init(&cpu,sizeof(cpu));
606 
607 
608  // initialize the FLASH handle
609  obj->flashHandle = FLASH_init((void *)FLASH_BASE_ADDR,sizeof(FLASH_Obj));
610 
611 
612  // initialize the GPIO handle
613  obj->gpioHandle = GPIO_init((void *)GPIO_BASE_ADDR,sizeof(GPIO_Obj));
614 
615 
616  // initialize the current offset estimator handles
617  for(cnt=0;cnt<USER_NUM_CURRENT_SENSORS;cnt++)
618  {
619  obj->offsetHandle_I[cnt] = OFFSET_init(&obj->offset_I[cnt],sizeof(obj->offset_I[cnt]));
620  }
621 
622 
623  // initialize the voltage offset estimator handles
624  for(cnt=0;cnt<USER_NUM_VOLTAGE_SENSORS;cnt++)
625  {
626  obj->offsetHandle_V[cnt] = OFFSET_init(&obj->offset_V[cnt],sizeof(obj->offset_V[cnt]));
627  }
628 
629 
630  // initialize the oscillator handle
631  obj->oscHandle = OSC_init((void *)OSC_BASE_ADDR,sizeof(OSC_Obj));
632 
633 
634  // initialize the PIE handle
635  obj->pieHandle = PIE_init((void *)PIE_BASE_ADDR,sizeof(PIE_Obj));
636 
637 
638  // initialize the PLL handle
639  obj->pllHandle = PLL_init((void *)PLL_BASE_ADDR,sizeof(PLL_Obj));
640 
641 
642  // initialize the SPIA handle
643  obj->spiAHandle = SPI_init((void *)SPIA_BASE_ADDR,sizeof(SPI_Obj));
644 
645 
646 
647  // initialize PWM handles
648  obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM1_BASE_ADDR,sizeof(PWM_Obj));
649  obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM2_BASE_ADDR,sizeof(PWM_Obj));
650  obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM3_BASE_ADDR,sizeof(PWM_Obj));
651 
652 
653  // initialize PWM DAC handles
654  obj->pwmDacHandle[0] = PWMDAC_init((void *)PWM_ePWM6_BASE_ADDR,sizeof(PWM_Obj));
655  obj->pwmDacHandle[1] = PWMDAC_init((void *)PWM_ePWM5_BASE_ADDR,sizeof(PWM_Obj));
656  obj->pwmDacHandle[2] = PWMDAC_init((void *)PWM_ePWM4_BASE_ADDR,sizeof(PWM_Obj));
657 
658 
659  // initialize power handle
660  obj->pwrHandle = PWR_init((void *)PWR_BASE_ADDR,sizeof(PWR_Obj));
661 
662 
663  // initialize timer handles
664  obj->timerHandle[0] = TIMER_init((void *)TIMER0_BASE_ADDR,sizeof(TIMER_Obj));
665  obj->timerHandle[1] = TIMER_init((void *)TIMER1_BASE_ADDR,sizeof(TIMER_Obj));
666  obj->timerHandle[2] = TIMER_init((void *)TIMER2_BASE_ADDR,sizeof(TIMER_Obj));
667 
668 #ifdef QEP
669  // initialize QEP driver
670  obj->qepHandle[0] = QEP_init((void*)QEP1_BASE_ADDR,sizeof(QEP_Obj));
671 #endif
672 
673  return(handle);
674 } // end of HAL_init() function
675 
676 
677 void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams)
678 {
679  uint_least8_t cnt;
680  HAL_Obj *obj = (HAL_Obj *)handle;
681  _iq beta_lp_pu = _IQ(pUserParams->offsetPole_rps/(float_t)pUserParams->ctrlFreq_Hz);
682 
683 
684  HAL_setNumCurrentSensors(handle,pUserParams->numCurrentSensors);
685  HAL_setNumVoltageSensors(handle,pUserParams->numVoltageSensors);
686 
687 
688  for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
689  {
690  HAL_setOffsetBeta_lp_pu(handle,HAL_SensorType_Current,cnt,beta_lp_pu);
693  }
694 
695 
696  for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
697  {
698  HAL_setOffsetBeta_lp_pu(handle,HAL_SensorType_Voltage,cnt,beta_lp_pu);
701  }
702 
703 
704  // disable global interrupts
705  CPU_disableGlobalInts(obj->cpuHandle);
706 
707 
708  // disable cpu interrupts
709  CPU_disableInts(obj->cpuHandle);
710 
711 
712  // clear cpu interrupt flags
713  CPU_clearIntFlags(obj->cpuHandle);
714 
715 
716  // setup the clocks
717  HAL_setupClks(handle);
718 
719 
720  // Setup the PLL
721  HAL_setupPll(handle,PLL_ClkFreq_90_MHz);
722 
723 
724  // setup the PIE
725  HAL_setupPie(handle);
726 
727 
728  // run the device calibration
729  HAL_cal(handle);
730 
731 
732  // setup the peripheral clocks
733  HAL_setupPeripheralClks(handle);
734 
735 
736  // setup the GPIOs
737  HAL_setupGpios(handle);
738 
739 
740  // setup the flash
741  HAL_setupFlash(handle);
742 
743 
744  // setup the ADCs
745  HAL_setupAdcs(handle);
746 
747 
748  // setup the PWMs
749  HAL_setupPwms(handle,
750  (float_t)pUserParams->systemFreq_MHz,
751  pUserParams->pwmPeriod_usec,
752  USER_NUM_PWM_TICKS_PER_ISR_TICK);
753 
754 #ifdef QEP
755  // setup the QEP
756  HAL_setupQEP(handle,HAL_Qep_QEP1);
757 #endif
758 
759 
760  // setup the spiA
761  HAL_setupSpiA(handle);
762 
763 
764  // setup the PWM DACs
765  HAL_setupPwmDacs(handle);
766 
767 
768  // setup the timers
769  HAL_setupTimers(handle,
770  (float_t)pUserParams->systemFreq_MHz);
771 
772 
773  // set the default current bias
774  {
775  uint_least8_t cnt;
776  _iq bias = _IQ12mpy(ADC_dataBias,_IQ(pUserParams->current_sf));
777 
778  for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
779  {
780  HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
781  }
782  }
783 
784 
785  // set the current scale factor
786  {
787  _iq current_sf = _IQ(pUserParams->current_sf);
788 
789  HAL_setCurrentScaleFactor(handle,current_sf);
790  }
791 
792 
793  // set the default voltage bias
794  {
795  uint_least8_t cnt;
796  _iq bias = _IQ(0.0);
797 
798  for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
799  {
800  HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
801  }
802  }
803 
804 
805  // set the voltage scale factor
806  {
807  _iq voltage_sf = _IQ(pUserParams->voltage_sf);
808 
809  HAL_setVoltageScaleFactor(handle,voltage_sf);
810  }
811 
812  return;
813 } // end of HAL_setParams() function
814 
815 
817 {
818  HAL_Obj *obj = (HAL_Obj *)handle;
819 
820 
821  // disable the ADCs
822  ADC_disable(obj->adcHandle);
823 
824 
825  // power up the bandgap circuit
826  ADC_enableBandGap(obj->adcHandle);
827 
828 
829  // set the ADC voltage reference source to internal
830  ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
831 
832 
833  // enable the ADC reference buffers
834  ADC_enableRefBuffers(obj->adcHandle);
835 
836 
837  // Set main clock scaling factor (max45MHz clock for the ADC module)
838  ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
839 
840 
841  // power up the ADCs
842  ADC_powerUp(obj->adcHandle);
843 
844 
845  // enable the ADCs
846  ADC_enable(obj->adcHandle);
847 
848 
849  // set the ADC interrupt pulse generation to prior
850  ADC_setIntPulseGenMode(obj->adcHandle,ADC_IntPulseGenMode_Prior);
851 
852 
853  // set the temperature sensor source to external
854  ADC_setTempSensorSrc(obj->adcHandle,ADC_TempSensorSrc_Ext);
855 
856 
857  // configure the interrupt sources
858  ADC_disableInt(obj->adcHandle,ADC_IntNumber_1);
859  ADC_setIntMode(obj->adcHandle,ADC_IntNumber_1,ADC_IntMode_ClearFlag);
860  ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_1,ADC_IntSrc_EOC7);
861 
862 
863  // configure the SOCs for drv8312kit_revD
864  // EXT IA-FB
865  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A1);
866  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM1_ADCSOCA);
867  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
868 
869  // EXT IA-FB
870  // Duplicate conversion due to ADC Initial Conversion bug (SPRZ342)
871  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A1);
872  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM1_ADCSOCA);
873  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
874 
875  // EXT IB-FB
876  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B5);
877  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM1_ADCSOCA);
878  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
879 
880  // EXT IC-FB
881  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A5);
882  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM1_ADCSOCA);
883  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
884 
885  // ADC-Vhb1
886  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B7);
887  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM1_ADCSOCA);
888  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
889 
890  // ADC-Vhb2
891  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A7);
892  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM1_ADCSOCA);
893  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
894 
895  // ADC-Vhb3
896  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B4);
897  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM1_ADCSOCA);
898  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
899 
900  // VDCBUS
901  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_B2);
902  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM1_ADCSOCA);
903  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
904 
905  return;
906 } // end of HAL_setupAdcs() function
907 
908 
910 {
911  HAL_Obj *obj = (HAL_Obj *)handle;
912 
913 
914  // enable internal oscillator 1
915  CLK_enableOsc1(obj->clkHandle);
916 
917  // set the oscillator source
918  CLK_setOscSrc(obj->clkHandle,CLK_OscSrc_Internal);
919 
920  // disable the external clock in
921  CLK_disableClkIn(obj->clkHandle);
922 
923  // disable the crystal oscillator
924  CLK_disableCrystalOsc(obj->clkHandle);
925 
926  // disable oscillator 2
927  CLK_disableOsc2(obj->clkHandle);
928 
929  // set the low speed clock prescaler
930  CLK_setLowSpdPreScaler(obj->clkHandle,CLK_LowSpdPreScaler_SysClkOut_by_1);
931 
932  // set the clock out prescaler
933  CLK_setClkOutPreScaler(obj->clkHandle,CLK_ClkOutPreScaler_SysClkOut_by_1);
934 
935  return;
936 } // end of HAL_setupClks() function
937 
938 
940 {
941  HAL_Obj *obj = (HAL_Obj *)handle;
942 
943 
944  FLASH_enablePipelineMode(obj->flashHandle);
945 
946  FLASH_setNumPagedReadWaitStates(obj->flashHandle,FLASH_NumPagedWaitStates_3);
947 
948  FLASH_setNumRandomReadWaitStates(obj->flashHandle,FLASH_NumRandomWaitStates_3);
949 
950  FLASH_setOtpWaitStates(obj->flashHandle,FLASH_NumOtpWaitStates_5);
951 
952  FLASH_setStandbyWaitCount(obj->flashHandle,FLASH_STANDBY_WAIT_COUNT_DEFAULT);
953 
954  FLASH_setActiveWaitCount(obj->flashHandle,FLASH_ACTIVE_WAIT_COUNT_DEFAULT);
955 
956  return;
957 } // HAL_setupFlash() function
958 
959 
961 {
962  HAL_Obj *obj = (HAL_Obj *)handle;
963 
964 
965  // PWM1
966  GPIO_setMode(obj->gpioHandle,GPIO_Number_0,GPIO_0_Mode_EPWM1A);
967 
968  // PWM2
969  GPIO_setMode(obj->gpioHandle,GPIO_Number_1,GPIO_1_Mode_EPWM1B);
970 
971  // PWM3
972  GPIO_setMode(obj->gpioHandle,GPIO_Number_2,GPIO_2_Mode_EPWM2A);
973 
974  // PWM4
975  GPIO_setMode(obj->gpioHandle,GPIO_Number_3,GPIO_3_Mode_EPWM2B);
976 
977  // PWM5
978  GPIO_setMode(obj->gpioHandle,GPIO_Number_4,GPIO_4_Mode_EPWM3A);
979 
980  // PWM6
981  GPIO_setMode(obj->gpioHandle,GPIO_Number_5,GPIO_5_Mode_EPWM3B);
982 
983  // PWM-DAC4
984  GPIO_setMode(obj->gpioHandle,GPIO_Number_6,GPIO_6_Mode_EPWM4A);
985 
986  // Input
987  GPIO_setMode(obj->gpioHandle,GPIO_Number_7,GPIO_7_Mode_GeneralPurpose);
988 
989  // PWM-DAC3
990  GPIO_setMode(obj->gpioHandle,GPIO_Number_8,GPIO_8_Mode_ADCSOCAO_NOT);
991 
992  // No Connection
993  GPIO_setMode(obj->gpioHandle,GPIO_Number_9,GPIO_9_Mode_GeneralPurpose);
994 
995  // PWM-DAC1
996  GPIO_setMode(obj->gpioHandle,GPIO_Number_10,GPIO_10_Mode_EPWM6A);
997 
998  // PWM-DAC2
999  GPIO_setMode(obj->gpioHandle,GPIO_Number_11,GPIO_11_Mode_EPWM6B);
1000 
1001  // FAULTn
1002  GPIO_setMode(obj->gpioHandle,GPIO_Number_12,GPIO_12_Mode_GeneralPurpose);
1003  GPIO_setLow(obj->gpioHandle,GPIO_Number_12);
1004  GPIO_setDirection(obj->gpioHandle,GPIO_Number_12,GPIO_Direction_Output);
1005 
1006  // OCTWn
1007  GPIO_setMode(obj->gpioHandle,GPIO_Number_13,GPIO_13_Mode_TZ2_NOT);
1008 
1009  // FAULTn
1010  GPIO_setMode(obj->gpioHandle,GPIO_Number_14,GPIO_14_Mode_TZ3_NOT);
1011 
1012  // LED2
1013  GPIO_setMode(obj->gpioHandle,GPIO_Number_15,GPIO_15_Mode_GeneralPurpose);
1014 
1015  // Set Qualification Period for GPIO16-23, 22*2*(1/90MHz) = 0.48us
1016  GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_16,22);
1017 
1018  // SPI-SIMO
1019  GPIO_setMode(obj->gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);
1020 
1021  // SPI-SOMI
1022  GPIO_setMode(obj->gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);
1023 
1024  // SPI-CLK
1025  GPIO_setMode(obj->gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);
1026 
1027  // SPI-STE
1028  GPIO_setMode(obj->gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);
1029 
1030 #ifdef QEP
1031  // EQEPA
1032  GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_EQEP1A);
1033  GPIO_setQualification(obj->gpioHandle,GPIO_Number_20,GPIO_Qual_Sample_3);
1034 
1035  // EQEPB
1036  GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_EQEP1B);
1037  GPIO_setQualification(obj->gpioHandle,GPIO_Number_21,GPIO_Qual_Sample_3);
1038 
1039  // STATUS
1040  GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
1041 
1042  // EQEP1I
1043  GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_EQEP1I);
1044  GPIO_setQualification(obj->gpioHandle,GPIO_Number_23,GPIO_Qual_Sample_3);
1045 #else
1046  // EQEPA
1047  GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_GeneralPurpose);
1048 
1049  // EQEPB
1050  GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_GeneralPurpose);
1051 
1052  // STATUS
1053  GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
1054 
1055  // EQEP1I
1056  GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_GeneralPurpose);
1057 #endif
1058 
1059  // SPI SIMO B
1060  GPIO_setMode(obj->gpioHandle,GPIO_Number_24,GPIO_24_Mode_GeneralPurpose);
1061 
1062  // SPI SOMI B
1063  GPIO_setMode(obj->gpioHandle,GPIO_Number_25,GPIO_25_Mode_GeneralPurpose);
1064 
1065  // SPI CLK B
1066  GPIO_setMode(obj->gpioHandle,GPIO_Number_26,GPIO_26_Mode_GeneralPurpose);
1067 
1068  // SPI CSn B
1069  GPIO_setMode(obj->gpioHandle,GPIO_Number_27,GPIO_27_Mode_GeneralPurpose);
1070 
1071  // No Connection
1072  GPIO_setMode(obj->gpioHandle,GPIO_Number_28,GPIO_28_Mode_GeneralPurpose);
1073 
1074  // No Connection
1075  GPIO_setMode(obj->gpioHandle,GPIO_Number_29,GPIO_29_Mode_GeneralPurpose);
1076 
1077  // No Connection
1078  GPIO_setMode(obj->gpioHandle,GPIO_Number_30,GPIO_30_Mode_GeneralPurpose);
1079 
1080  // ControlCARD LED2
1081  GPIO_setMode(obj->gpioHandle,GPIO_Number_31,GPIO_31_Mode_GeneralPurpose);
1082  GPIO_setLow(obj->gpioHandle,GPIO_Number_31);
1083  GPIO_setDirection(obj->gpioHandle,GPIO_Number_31,GPIO_Direction_Output);
1084 
1085  // No Connection
1086  GPIO_setMode(obj->gpioHandle,GPIO_Number_32,GPIO_32_Mode_GeneralPurpose);
1087 
1088  // No Connection
1089  GPIO_setMode(obj->gpioHandle,GPIO_Number_33,GPIO_33_Mode_GeneralPurpose);
1090 
1091  // ControlCARD LED3
1092  GPIO_setMode(obj->gpioHandle,GPIO_Number_34,GPIO_34_Mode_GeneralPurpose);
1093  GPIO_setLow(obj->gpioHandle,GPIO_Number_34);
1094  GPIO_setDirection(obj->gpioHandle,GPIO_Number_34,GPIO_Direction_Output);
1095 
1096  // JTAG
1097  GPIO_setMode(obj->gpioHandle,GPIO_Number_35,GPIO_35_Mode_JTAG_TDI);
1098  GPIO_setMode(obj->gpioHandle,GPIO_Number_36,GPIO_36_Mode_JTAG_TMS);
1099  GPIO_setMode(obj->gpioHandle,GPIO_Number_37,GPIO_37_Mode_JTAG_TDO);
1100  GPIO_setMode(obj->gpioHandle,GPIO_Number_38,GPIO_38_Mode_JTAG_TCK);
1101 
1102  // DRV8301 Enable
1103  GPIO_setMode(obj->gpioHandle,GPIO_Number_39,GPIO_39_Mode_GeneralPurpose);
1104 
1105  // CAP1
1106  GPIO_setMode(obj->gpioHandle,GPIO_Number_40,GPIO_40_Mode_GeneralPurpose);
1107 
1108  // CAP2
1109  GPIO_setMode(obj->gpioHandle,GPIO_Number_41,GPIO_41_Mode_GeneralPurpose);
1110 
1111  // CAP3
1112  GPIO_setMode(obj->gpioHandle,GPIO_Number_42,GPIO_42_Mode_GeneralPurpose);
1113 
1114  // DC_CAL
1115  GPIO_setMode(obj->gpioHandle,GPIO_Number_43,GPIO_43_Mode_GeneralPurpose);
1116 
1117  // No Connection
1118  GPIO_setMode(obj->gpioHandle,GPIO_Number_44,GPIO_44_Mode_GeneralPurpose);
1119 
1120  // No Connection
1121  GPIO_setMode(obj->gpioHandle,GPIO_Number_50,GPIO_50_Mode_GeneralPurpose);
1122 
1123  // No Connection
1124  GPIO_setMode(obj->gpioHandle,GPIO_Number_51,GPIO_51_Mode_GeneralPurpose);
1125 
1126  // No Connection
1127  GPIO_setMode(obj->gpioHandle,GPIO_Number_52,GPIO_52_Mode_GeneralPurpose);
1128 
1129  // No Connection
1130  GPIO_setMode(obj->gpioHandle,GPIO_Number_53,GPIO_53_Mode_GeneralPurpose);
1131 
1132  // No Connection
1133  GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_GeneralPurpose);
1134 
1135  // No Connection
1136  GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_GeneralPurpose);
1137 
1138  // No Connection
1139  GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_GeneralPurpose);
1140 
1141  // No Connection
1142  GPIO_setMode(obj->gpioHandle,GPIO_Number_57,GPIO_57_Mode_GeneralPurpose);
1143 
1144  // No Connection
1145  GPIO_setMode(obj->gpioHandle,GPIO_Number_58,GPIO_58_Mode_GeneralPurpose);
1146 
1147  return;
1148 } // end of HAL_setupGpios() function
1149 
1150 
1152 {
1153  HAL_Obj *obj = (HAL_Obj *)handle;
1154 
1155 
1156  PIE_disable(obj->pieHandle);
1157 
1158  PIE_disableAllInts(obj->pieHandle);
1159 
1160  PIE_clearAllInts(obj->pieHandle);
1161 
1162  PIE_clearAllFlags(obj->pieHandle);
1163 
1164  PIE_setDefaultIntVectorTable(obj->pieHandle);
1165 
1166  PIE_enable(obj->pieHandle);
1167 
1168  return;
1169 } // end of HAL_setupPie() function
1170 
1171 
1173 {
1174  HAL_Obj *obj = (HAL_Obj *)handle;
1175 
1176 
1177  CLK_enableAdcClock(obj->clkHandle);
1178 
1179  CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_1);
1180  CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_2);
1181  CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_3);
1182 
1183  CLK_enableEcap1Clock(obj->clkHandle);
1184 
1185  CLK_disableEcanaClock(obj->clkHandle);
1186 
1187 #ifdef QEP
1188  CLK_enableEqep1Clock(obj->clkHandle);
1189  CLK_disableEqep2Clock(obj->clkHandle);
1190 #endif
1191 
1192  CLK_enablePwmClock(obj->clkHandle,PWM_Number_1);
1193  CLK_enablePwmClock(obj->clkHandle,PWM_Number_2);
1194  CLK_enablePwmClock(obj->clkHandle,PWM_Number_3);
1195  CLK_enablePwmClock(obj->clkHandle,PWM_Number_4);
1196  CLK_enablePwmClock(obj->clkHandle,PWM_Number_5);
1197  CLK_enablePwmClock(obj->clkHandle,PWM_Number_6);
1198  CLK_enablePwmClock(obj->clkHandle,PWM_Number_7);
1199 
1200  CLK_disableHrPwmClock(obj->clkHandle);
1201 
1202  CLK_disableI2cClock(obj->clkHandle);
1203 
1204  CLK_disableLinAClock(obj->clkHandle);
1205 
1206  CLK_disableClaClock(obj->clkHandle);
1207 
1208  CLK_enableSciaClock(obj->clkHandle);
1209 
1210  CLK_enableSpiaClock(obj->clkHandle);
1211  CLK_disableSpibClock(obj->clkHandle);
1212 
1213  CLK_enableTbClockSync(obj->clkHandle);
1214 
1215  return;
1216 } // end of HAL_setupPeripheralClks() function
1217 
1218 
1219 void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq)
1220 {
1221  HAL_Obj *obj = (HAL_Obj *)handle;
1222 
1223 
1224  // make sure PLL is not running in limp mode
1225  if(PLL_getClkStatus(obj->pllHandle) != PLL_ClkStatus_Normal)
1226  {
1227  // reset the clock detect
1228  PLL_resetClkDetect(obj->pllHandle);
1229 
1230  // ???????
1231  asm(" ESTOP0");
1232  }
1233 
1234 
1235  // Divide Select must be ClkIn/4 before the clock rate can be changed
1236  if(PLL_getDivideSelect(obj->pllHandle) != PLL_DivideSelect_ClkIn_by_4)
1237  {
1238  PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_4);
1239  }
1240 
1241 
1242  if(PLL_getClkFreq(obj->pllHandle) != clkFreq)
1243  {
1244  // disable the clock detect
1245  PLL_disableClkDetect(obj->pllHandle);
1246 
1247  // set the clock rate
1248  PLL_setClkFreq(obj->pllHandle,clkFreq);
1249  }
1250 
1251 
1252  // wait until locked
1253  while(PLL_getLockStatus(obj->pllHandle) != PLL_LockStatus_Done) {}
1254 
1255 
1256  // enable the clock detect
1257  PLL_enableClkDetect(obj->pllHandle);
1258 
1259 
1260  // set divide select to ClkIn/2 to get desired clock rate
1261  // NOTE: clock must be locked before setting this register
1262  PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_2);
1263 
1264  return;
1265 } // end of HAL_setupPll() function
1266 
1267 
1269  const float_t systemFreq_MHz,
1270  const float_t pwmPeriod_usec,
1271  const uint_least16_t numPwmTicksPerIsrTick)
1272 {
1273  HAL_Obj *obj = (HAL_Obj *)handle;
1274  uint16_t halfPeriod_cycles = (uint16_t)(systemFreq_MHz*pwmPeriod_usec) >> 1;
1275  uint_least8_t cnt;
1276 
1277 
1278  // turns off the outputs of the EPWM peripherals which will put the power switches
1279  // into a high impedance state.
1280  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_1]);
1281  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_2]);
1282  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_3]);
1283 
1284  for(cnt=0;cnt<3;cnt++)
1285  {
1286  // setup the Time-Base Control Register (TBCTL)
1287  PWM_setCounterMode(obj->pwmHandle[cnt],PWM_CounterMode_UpDown);
1288  PWM_disableCounterLoad(obj->pwmHandle[cnt]);
1289  PWM_setPeriodLoad(obj->pwmHandle[cnt],PWM_PeriodLoad_Immediate);
1290  PWM_setSyncMode(obj->pwmHandle[cnt],PWM_SyncMode_EPWMxSYNC);
1291  PWM_setHighSpeedClkDiv(obj->pwmHandle[cnt],PWM_HspClkDiv_by_1);
1292  PWM_setClkDiv(obj->pwmHandle[cnt],PWM_ClkDiv_by_1);
1293  PWM_setPhaseDir(obj->pwmHandle[cnt],PWM_PhaseDir_CountUp);
1294  PWM_setRunMode(obj->pwmHandle[cnt],PWM_RunMode_FreeRun);
1295 
1296  // setup the Timer-Based Phase Register (TBPHS)
1297  PWM_setPhase(obj->pwmHandle[cnt],0);
1298 
1299  // setup the Time-Base Counter Register (TBCTR)
1300  PWM_setCount(obj->pwmHandle[cnt],0);
1301 
1302  // setup the Time-Base Period Register (TBPRD)
1303  // set to zero initially
1304  PWM_setPeriod(obj->pwmHandle[cnt],0);
1305 
1306  // setup the Counter-Compare Control Register (CMPCTL)
1307  PWM_setLoadMode_CmpA(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
1308  PWM_setLoadMode_CmpB(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
1309  PWM_setShadowMode_CmpA(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
1310  PWM_setShadowMode_CmpB(obj->pwmHandle[cnt],PWM_ShadowMode_Immediate);
1311 
1312  // setup the Action-Qualifier Output A Register (AQCTLA)
1313  PWM_setActionQual_CntUp_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Set);
1314  PWM_setActionQual_CntDown_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Clear);
1315 
1316  // setup the Action-qualifier Continuous Software Force Register (AQCSFRC)
1317  PWM_setActionQualContSWForce_PwmB(obj->pwmHandle[cnt],PWM_ActionQualContSWForce_Set);
1318 
1319  // setup the Dead-Band Generator Control Register (DBCTL)
1320  PWM_setDeadBandOutputMode(obj->pwmHandle[cnt],PWM_DeadBandOutputMode_Bypass);
1321 
1322  // setup the PWM-Chopper Control Register (PCCTL)
1323  PWM_disableChopping(obj->pwmHandle[cnt]);
1324 
1325  // setup the Trip Zone Select Register (TZSEL)
1326  PWM_disableTripZones(obj->pwmHandle[cnt]);
1327  }
1328 
1329 
1330  // setup the Event Trigger Selection Register (ETSEL)
1331  PWM_disableInt(obj->pwmHandle[PWM_Number_1]);
1332  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualZero);
1333  PWM_enableSocAPulse(obj->pwmHandle[PWM_Number_1]);
1334 
1335 
1336  // setup the Event Trigger Prescale Register (ETPS)
1337  if(numPwmTicksPerIsrTick == 3)
1338  {
1339  PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_ThirdEvent);
1340  PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_ThirdEvent);
1341  }
1342  else if(numPwmTicksPerIsrTick == 2)
1343  {
1344  PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_SecondEvent);
1345  PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_SecondEvent);
1346  }
1347  else
1348  {
1349  PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_FirstEvent);
1350  PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_FirstEvent);
1351  }
1352 
1353 
1354  // setup the Event Trigger Clear Register (ETCLR)
1355  PWM_clearIntFlag(obj->pwmHandle[PWM_Number_1]);
1356  PWM_clearSocAFlag(obj->pwmHandle[PWM_Number_1]);
1357 
1358  // first step to synchronize the pwms
1359  CLK_disableTbClockSync(obj->clkHandle);
1360 
1361  // since the PWM is configured as an up/down counter, the period register is set to one-half
1362  // of the desired PWM period
1363  PWM_setPeriod(obj->pwmHandle[PWM_Number_1],halfPeriod_cycles);
1364  PWM_setPeriod(obj->pwmHandle[PWM_Number_2],halfPeriod_cycles);
1365  PWM_setPeriod(obj->pwmHandle[PWM_Number_3],halfPeriod_cycles);
1366 
1367  // last step to synchronize the pwms
1368  CLK_enableTbClockSync(obj->clkHandle);
1369 
1370  return;
1371 } // end of HAL_setupPwms() function
1372 
1373 #ifdef QEP
1374 void HAL_setupQEP(HAL_Handle handle,HAL_QepSelect_e qep)
1375 {
1376  HAL_Obj *obj = (HAL_Obj *)handle;
1377 
1378 
1379  // hold the counter in reset
1380  QEP_reset_counter(obj->qepHandle[qep]);
1381 
1382  // set the QPOSINIT register
1383  QEP_set_posn_init_count(obj->qepHandle[qep], 0);
1384 
1385  // disable all interrupts
1386  QEP_disable_all_interrupts(obj->qepHandle[qep]);
1387 
1388  // clear the interrupt flags
1389  QEP_clear_all_interrupt_flags(obj->qepHandle[qep]);
1390 
1391  // clear the position counter
1392  QEP_clear_posn_counter(obj->qepHandle[qep]);
1393 
1394  // setup the max position
1395  QEP_set_max_posn_count(obj->qepHandle[qep], (4*USER_MOTOR_ENCODER_LINES)-1);
1396 
1397  // setup the QDECCTL register
1398  QEP_set_QEP_source(obj->qepHandle[qep], QEP_Qsrc_Quad_Count_Mode);
1399  QEP_disable_sync_out(obj->qepHandle[qep]);
1400  QEP_set_swap_quad_inputs(obj->qepHandle[qep], QEP_Swap_Not_Swapped);
1401  QEP_disable_gate_index(obj->qepHandle[qep]);
1402  QEP_set_ext_clock_rate(obj->qepHandle[qep], QEP_Xcr_2x_Res);
1403  QEP_set_A_polarity(obj->qepHandle[qep], QEP_Qap_No_Effect);
1404  QEP_set_B_polarity(obj->qepHandle[qep], QEP_Qbp_No_Effect);
1405  QEP_set_index_polarity(obj->qepHandle[qep], QEP_Qip_No_Effect);
1406 
1407  // setup the QEPCTL register
1408  QEP_set_emu_control(obj->qepHandle[qep], QEPCTL_Freesoft_Unaffected_Halt);
1409  QEP_set_posn_count_reset_mode(obj->qepHandle[qep], QEPCTL_Pcrm_Max_Reset);
1410  QEP_set_strobe_event_init(obj->qepHandle[qep], QEPCTL_Sei_Nothing);
1411  QEP_set_index_event_init(obj->qepHandle[qep], QEPCTL_Iei_Nothing);
1412  QEP_set_index_event_latch(obj->qepHandle[qep], QEPCTL_Iel_Rising_Edge);
1413  QEP_set_soft_init(obj->qepHandle[qep], QEPCTL_Swi_Nothing);
1414  QEP_disable_unit_timer(obj->qepHandle[qep]);
1415  QEP_disable_watchdog(obj->qepHandle[qep]);
1416 
1417  // setup the QPOSCTL register
1418  QEP_disable_posn_compare(obj->qepHandle[qep]);
1419 
1420  // setup the QCAPCTL register
1421  QEP_disable_capture(obj->qepHandle[qep]);
1422 
1423  // renable the position counter
1424  QEP_enable_counter(obj->qepHandle[qep]);
1425 
1426 
1427  return;
1428 }
1429 #endif
1430 
1431 
1433 {
1434  HAL_Obj *obj = (HAL_Obj *)handle;
1435 
1436  SPI_reset(obj->spiAHandle);
1437  SPI_setClkPolarity(obj->spiAHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
1438  SPI_disableLoopBack(obj->spiAHandle);
1439  SPI_setCharLength(obj->spiAHandle,SPI_CharLength_16_Bits);
1440 
1441  SPI_setMode(obj->spiAHandle,SPI_Mode_Slave);
1442  SPI_setClkPhase(obj->spiAHandle,SPI_ClkPhase_Delayed);
1443  SPI_enableTx(obj->spiAHandle);
1444 
1445  SPI_enableChannels(obj->spiAHandle);
1446  SPI_enableTxFifoEnh(obj->spiAHandle);
1447  SPI_enableTxFifo(obj->spiAHandle);
1448  SPI_setTxDelay(obj->spiAHandle,0);
1449  SPI_clearTxFifoInt(obj->spiAHandle);
1450  SPI_enableRxFifo(obj->spiAHandle);
1451 
1452 //not needed for slave mode SPI_setBaudRate(obj->spiAHandle,(SPI_BaudRate_e)(0x0003));
1453  SPI_setSuspend(obj->spiAHandle,SPI_TxSuspend_free);
1454  SPI_enable(obj->spiAHandle);
1455 
1456  return;
1457 } // end of HAL_setupSpiA() function
1458 
1460 {
1461  HAL_Obj *obj = (HAL_Obj *)handle;
1462  uint16_t halfPeriod_cycles = 512; // 3000->10kHz, 1500->20kHz, 1000-> 30kHz, 500->60kHz
1463  uint_least8_t cnt;
1464 
1465 
1466  for(cnt=0;cnt<3;cnt++)
1467  {
1468  // initialize the Time-Base Control Register (TBCTL)
1469  PWMDAC_setCounterMode(obj->pwmDacHandle[cnt],PWM_CounterMode_UpDown);
1470  PWMDAC_disableCounterLoad(obj->pwmDacHandle[cnt]);
1471  PWMDAC_setPeriodLoad(obj->pwmDacHandle[cnt],PWM_PeriodLoad_Immediate);
1472  PWMDAC_setSyncMode(obj->pwmDacHandle[cnt],PWM_SyncMode_EPWMxSYNC);
1473  PWMDAC_setHighSpeedClkDiv(obj->pwmDacHandle[cnt],PWM_HspClkDiv_by_1);
1474  PWMDAC_setClkDiv(obj->pwmDacHandle[cnt],PWM_ClkDiv_by_1);
1475  PWMDAC_setPhaseDir(obj->pwmDacHandle[cnt],PWM_PhaseDir_CountUp);
1476  PWMDAC_setRunMode(obj->pwmDacHandle[cnt],PWM_RunMode_FreeRun);
1477 
1478  // initialize the Timer-Based Phase Register (TBPHS)
1479  PWMDAC_setPhase(obj->pwmDacHandle[cnt],0);
1480 
1481  // setup the Time-Base Counter Register (TBCTR)
1482  PWMDAC_setCount(obj->pwmDacHandle[cnt],0);
1483 
1484  // Initialize the Time-Base Period Register (TBPRD)
1485  // set to zero initially
1486  PWMDAC_setPeriod(obj->pwmDacHandle[cnt],0);
1487 
1488  // initialize the Counter-Compare Control Register (CMPCTL)
1489  PWMDAC_setLoadMode_CmpA(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
1490  PWMDAC_setLoadMode_CmpB(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
1491  PWMDAC_setShadowMode_CmpA(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
1492  PWMDAC_setShadowMode_CmpB(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
1493 
1494  // Initialize the Action-Qualifier Output A Register (AQCTLA)
1495  PWMDAC_setActionQual_CntUp_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
1496  PWMDAC_setActionQual_CntDown_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
1497 
1498  // account for EPWM6B
1499  if(cnt == 0)
1500  {
1501  PWMDAC_setActionQual_CntUp_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
1502  PWMDAC_setActionQual_CntDown_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
1503  }
1504 
1505  // Initialize the Dead-Band Control Register (DBCTL)
1506  PWMDAC_disableDeadBand(obj->pwmDacHandle[cnt]);
1507 
1508  // Initialize the PWM-Chopper Control Register (PCCTL)
1509  PWMDAC_disableChopping(obj->pwmDacHandle[cnt]);
1510 
1511  // Initialize the Trip-Zone Control Register (TZSEL)
1512  PWMDAC_disableTripZones(obj->pwmDacHandle[cnt]);
1513 
1514  // Initialize the Trip-Zone Control Register (TZCTL)
1515  PWMDAC_setTripZoneState_TZA(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1516  PWMDAC_setTripZoneState_TZB(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1517  PWMDAC_setTripZoneState_DCAEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1518  PWMDAC_setTripZoneState_DCAEVT2(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1519  PWMDAC_setTripZoneState_DCBEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1520  }
1521 
1522  // since the PWM is configured as an up/down counter, the period register is set to one-half
1523  // of the desired PWM period
1524  PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_1],halfPeriod_cycles);
1525  PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_2],halfPeriod_cycles);
1526  PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_3],halfPeriod_cycles);
1527 
1528  return;
1529 } // end of HAL_setupPwmDacs() function
1530 
1531 
1532 void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz)
1533 {
1534  HAL_Obj *obj = (HAL_Obj *)handle;
1535  uint32_t timerPeriod_cnts = (uint32_t)(systemFreq_MHz * (float_t)1000000.0) - 1;
1536 
1537  // use timer 0 for frequency diagnostics
1538  TIMER_setDecimationFactor(obj->timerHandle[0],0);
1539  TIMER_setEmulationMode(obj->timerHandle[0],TIMER_EmulationMode_RunFree);
1540  TIMER_setPeriod(obj->timerHandle[0],timerPeriod_cnts);
1541  TIMER_setPreScaler(obj->timerHandle[0],0);
1542 
1543  // use timer 1 for CPU usage diagnostics
1544  TIMER_setDecimationFactor(obj->timerHandle[1],0);
1545  TIMER_setEmulationMode(obj->timerHandle[1],TIMER_EmulationMode_RunFree);
1546  TIMER_setPeriod(obj->timerHandle[1],timerPeriod_cnts);
1547  TIMER_setPreScaler(obj->timerHandle[1],0);
1548 
1549  return;
1550 } // end of HAL_setupTimers() function
1551 
1552 // end of file
static void HAL_setNumVoltageSensors(HAL_Handle handle, const uint_least8_t numVoltageSensors)
Sets the number of voltage sensors.
#define FP_ROUND
Defines the quantity added to Q15 numbers before converting to integer to round the number...
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.
OFFSET_Obj offset_V[3]
the voltage offset objects
void HAL_disableGlobalInts(HAL_Handle handle)
Disables global interrupts.
static void HAL_setCurrentScaleFactor(HAL_Handle handle, const _iq current_sf)
Sets the current scale factor in the hardware abstraction layer.
#define getOsc2CoarseTrim()
Defines the oscillator 2 coarse trim.
#define _IQ(A)
#define getOsc1FineTrimSlope()
The following functions return reference values stored in OTP.
void HAL_OscTempComp(HAL_Handle handle)
Executes the oscillator 1 and 2 calibration functions.
#define getOsc1FineTrimOffset()
Defines the oscillator 1 fine trim at high temp.
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.
CLK_Handle clkHandle
the clock handle
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.
uint_least16_t systemFreq_MHz
Defines the system clock frequency, MHz.
Defines a structure for the user parameters.
uint_least8_t numCurrentSensors
Defines the number of current sensors.
void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber)
Selects the analog channel used for calibration.
HAL_QepSelect_e
Enumeration for the QEP setup.
#define getOsc2FineTrimOffset()
Defines the oscillator 2 fine trim at high temp.
void HAL_setupPeripheralClks(HAL_Handle handle)
Sets up the peripheral clocks.
void HAL_setupTimers(HAL_Handle handle, const float_t systemFreq_MHz)
Sets up the timers.
long _iq
void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 2 calibration based on input sample.
void HAL_setupPll(HAL_Handle handle, const PLL_ClkFreq_e clkFreq)
Sets up the PLL (Phase Lock Loop)
FLASH_Handle flashHandle
the flash handle
void HAL_setupGpios(HAL_Handle handle)
Sets up the GPIO (General Purpose I/O) pins.
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.
void HAL_setupFlash(HAL_Handle handle)
Sets up the FLASH.
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_setVoltageScaleFactor(HAL_Handle handle, const _iq voltage_sf)
Sets the voltage scale factor in the hardware abstraction layer.
#define _IQ12mpy(A, B)
struct _HAL_Obj_ * HAL_Handle
Defines the HAL handle.
void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 1 calibration based on input sample.
float_t voltage_sf
Defines the voltage scale factor for the system.
void usDelay(const uint_least32_t delay_usec)
Provides a prescribes micro-second delay.
uint_least8_t numVoltageSensors
Defines the number of voltage 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.
SPI_Handle spiAHandle
the SPI handle
float_t offsetPole_rps
Defines the pole location for the voltage and current offset estimation, rad/s.
#define getRefTempOffset()
Defines the ADC reading of temperature sensor at reference temperature for compensation.
static void HAL_setNumCurrentSensors(HAL_Handle handle, const uint_least8_t numCurrentSensors)
Sets the number of current sensors.
float_t pwmPeriod_usec
Defines the Pulse Width Modulation (PWM) period, usec.
#define getOsc1CoarseTrim()
Defines the oscillator 1 coarse trim.
static uint_least8_t HAL_getNumVoltageSensors(HAL_Handle handle)
Gets the number of voltage sensors.
static uint_least8_t HAL_getNumCurrentSensors(HAL_Handle handle)
Gets the number of current sensors.
float_t current_sf
Defines the current scale factor for the system.
WDOG_Handle wdogHandle
the watchdog handle
void HAL_AdcOffsetSelfCal(HAL_Handle handle)
Executes the offset calibration of the ADC.
#define FP_SCALE
Defines used in oscillator calibration functions.
OSC_Handle oscHandle
the oscillator handle
ADC_Handle adcHandle
the ADC handle
HAL_Handle HAL_init(void *pMemory, const size_t numBytes)
Initializes the hardware abstraction layer (HAL) object.
CPU_Handle cpuHandle
the CPU handle
uint_least32_t ctrlFreq_Hz
Defines the controller frequency, Hz.
void HAL_cal(HAL_Handle handle)
Executes calibration routines.
HAL_Obj hal
Defines the HAL object.
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_setBias(HAL_Handle handle, const HAL_SensorType_e sensorType, uint_least8_t sensorNumber, const _iq bias)
Sets the ADC bias value.
uint16_t HAL_getOscTrimValue(int16_t coarse, int16_t fine)
Converts coarse and fine oscillator trim values into a single 16bit word value.
OFFSET_Obj offset_I[3]
the current offset objects
OFFSET_Handle OFFSET_init(void *pMemory, const size_t numBytes)
Initializes the offset.
Definition: 32b/offset.c:70
PLL_Handle pllHandle
the PLL handle
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)
void HAL_disableWdog(HAL_Handle halHandle)
Disables the watch dog.
OFFSET_Handle offsetHandle_V[3]
the handles for the voltage offset estimators
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
#define OSC_POSTRIM
Defines the amount to add to Q16.15 fixed point number to shift from a fine trim range of...
#define getOsc2FineTrimSlope()
Defines the slope used to compensate oscillator 2 (fine trim steps / ADC code). Stored.