MotorWare f2806x Module API Documentation
boostxldrv8301_revB/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  DRV8301_enable(obj->drv8301Handle);
514 
515  return;
516 } // end of HAL_enableDrv() function
517 
518 
520 {
521  HAL_Obj *obj = (HAL_Obj *)handle;
522 
523 
524  CPU_enableGlobalInts(obj->cpuHandle);
525 
526  return;
527 } // end of HAL_enableGlobalInts() function
528 
529 
531 {
532  HAL_Obj *obj = (HAL_Obj *)handle;
533 
534 #ifdef J5
535  PIE_enablePwmInt(obj->pieHandle,PWM_Number_4);
536 #else
537  PIE_enablePwmInt(obj->pieHandle,PWM_Number_1);
538 #endif
539 
540 
541 
542  // enable the interrupt
543  PWM_enableInt(obj->pwmHandle[0]);
544 
545 
546  // enable the cpu interrupt for EPWMx_INT
547  CPU_enableInt(obj->cpuHandle,CPU_IntNumber_3);
548 
549  return;
550 } // end of HAL_enablePwmInt() function
551 
552 
554 {
555  HAL_Obj *obj = (HAL_Obj *)handle;
556 
557 
558  PIE_enableTimer0Int(obj->pieHandle);
559 
560 
561  // enable the interrupt
562  TIMER_enableInt(obj->timerHandle[0]);
563 
564 
565  // enable the cpu interrupt for TINT0
566  CPU_enableInt(obj->cpuHandle,CPU_IntNumber_1);
567 
568  return;
569 } // end of HAL_enablePwmInt() function
570 
571 
573 {
574  HAL_Obj *obj = (HAL_Obj *)handle;
575  uint_least8_t cnt;
576 
577 
578  // Configure Trip Mechanism for the Motor control software
579  // -Cycle by cycle trip on CPU halt
580  // -One shot fault trip zone
581  // These trips need to be repeated for EPWM1 ,2 & 3
582  for(cnt=0;cnt<3;cnt++)
583  {
584  PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ6_NOT);
585 
586  PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ3_NOT);
587 
588  PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ2_NOT);
589 
590  // What do we want the OST/CBC events to do?
591  // TZA events can force EPWMxA
592  // TZB events can force EPWMxB
593 
594  PWM_setTripZoneState_TZA(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
595  PWM_setTripZoneState_TZB(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
596  }
597 
598  return;
599 } // end of HAL_setupFaults() function
600 
601 
602 HAL_Handle HAL_init(void *pMemory,const size_t numBytes)
603 {
604  uint_least8_t cnt;
605  HAL_Handle handle;
606  HAL_Obj *obj;
607 
608 
609  if(numBytes < sizeof(HAL_Obj))
610  return((HAL_Handle)NULL);
611 
612 
613  // assign the handle
614  handle = (HAL_Handle)pMemory;
615 
616 
617  // assign the object
618  obj = (HAL_Obj *)handle;
619 
620 
621  // initialize the watchdog driver
622  obj->wdogHandle = WDOG_init((void *)WDOG_BASE_ADDR,sizeof(WDOG_Obj));
623 
624 
625  // disable watchdog
626  HAL_disableWdog(handle);
627 
628 
629  // initialize the ADC
630  obj->adcHandle = ADC_init((void *)ADC_BASE_ADDR,sizeof(ADC_Obj));
631 
632 
633  // initialize the clock handle
634  obj->clkHandle = CLK_init((void *)CLK_BASE_ADDR,sizeof(CLK_Obj));
635 
636 
637  // initialize the CPU handle
638  obj->cpuHandle = CPU_init(&cpu,sizeof(cpu));
639 
640 
641  // initialize the FLASH handle
642  obj->flashHandle = FLASH_init((void *)FLASH_BASE_ADDR,sizeof(FLASH_Obj));
643 
644 
645  // initialize the GPIO handle
646  obj->gpioHandle = GPIO_init((void *)GPIO_BASE_ADDR,sizeof(GPIO_Obj));
647 
648 
649  // initialize the current offset estimator handles
650  for(cnt=0;cnt<USER_NUM_CURRENT_SENSORS;cnt++)
651  {
652  obj->offsetHandle_I[cnt] = OFFSET_init(&obj->offset_I[cnt],sizeof(obj->offset_I[cnt]));
653  }
654 
655 
656  // initialize the voltage offset estimator handles
657  for(cnt=0;cnt<USER_NUM_VOLTAGE_SENSORS;cnt++)
658  {
659  obj->offsetHandle_V[cnt] = OFFSET_init(&obj->offset_V[cnt],sizeof(obj->offset_V[cnt]));
660  }
661 
662 
663  // initialize the oscillator handle
664  obj->oscHandle = OSC_init((void *)OSC_BASE_ADDR,sizeof(OSC_Obj));
665 
666 
667  // initialize the PIE handle
668  obj->pieHandle = PIE_init((void *)PIE_BASE_ADDR,sizeof(PIE_Obj));
669 
670 
671  // initialize the PLL handle
672  obj->pllHandle = PLL_init((void *)PLL_BASE_ADDR,sizeof(PLL_Obj));
673 
674 
675  // initialize the SPI handles
676  obj->spiAHandle = SPI_init((void *)SPIA_BASE_ADDR,sizeof(SPI_Obj));
677  obj->spiBHandle = SPI_init((void *)SPIB_BASE_ADDR,sizeof(SPI_Obj));
678 
679 
680  // initialize PWM handles
681 #ifdef J5
682  obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM4_BASE_ADDR,sizeof(PWM_Obj));
683  obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM5_BASE_ADDR,sizeof(PWM_Obj));
684  obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM6_BASE_ADDR,sizeof(PWM_Obj));
685 #else
686  obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM1_BASE_ADDR,sizeof(PWM_Obj));
687  obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM2_BASE_ADDR,sizeof(PWM_Obj));
688  obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM3_BASE_ADDR,sizeof(PWM_Obj));
689 #endif
690 
691 
692  // initialize PWM DAC handles
693  obj->pwmDacHandle[0] = PWMDAC_init((void *)PWM_ePWM7_BASE_ADDR,sizeof(PWM_Obj));
694  obj->pwmDacHandle[1] = PWMDAC_init((void *)PWM_ePWM8_BASE_ADDR,sizeof(PWM_Obj));
695 
696 
697  // initialize power handle
698  obj->pwrHandle = PWR_init((void *)PWR_BASE_ADDR,sizeof(PWR_Obj));
699 
700 
701  // initialize timer handles
702  obj->timerHandle[0] = TIMER_init((void *)TIMER0_BASE_ADDR,sizeof(TIMER_Obj));
703  obj->timerHandle[1] = TIMER_init((void *)TIMER1_BASE_ADDR,sizeof(TIMER_Obj));
704  obj->timerHandle[2] = TIMER_init((void *)TIMER2_BASE_ADDR,sizeof(TIMER_Obj));
705 
706 
707  // initialize drv8301 interface
708  obj->drv8301Handle = DRV8301_init(&obj->drv8301,sizeof(obj->drv8301));
709 
710 
711 #ifdef QEP
712  // initialize QEP driver
713  obj->qepHandle[0] = QEP_init((void*)QEP1_BASE_ADDR,sizeof(QEP_Obj));
714  obj->qepHandle[1] = QEP_init((void*)QEP2_BASE_ADDR,sizeof(QEP_Obj));
715 #endif
716 
717  return(handle);
718 } // end of HAL_init() function
719 
720 
721 void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams)
722 {
723  uint_least8_t cnt;
724  HAL_Obj *obj = (HAL_Obj *)handle;
725  _iq beta_lp_pu = _IQ(pUserParams->offsetPole_rps/(float_t)pUserParams->ctrlFreq_Hz);
726 
727 
728  HAL_setNumCurrentSensors(handle,pUserParams->numCurrentSensors);
729  HAL_setNumVoltageSensors(handle,pUserParams->numVoltageSensors);
730 
731 
732  for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
733  {
734  HAL_setOffsetBeta_lp_pu(handle,HAL_SensorType_Current,cnt,beta_lp_pu);
737  }
738 
739 
740  for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
741  {
742  HAL_setOffsetBeta_lp_pu(handle,HAL_SensorType_Voltage,cnt,beta_lp_pu);
745  }
746 
747 
748  // disable global interrupts
749  CPU_disableGlobalInts(obj->cpuHandle);
750 
751 
752  // disable cpu interrupts
753  CPU_disableInts(obj->cpuHandle);
754 
755 
756  // clear cpu interrupt flags
757  CPU_clearIntFlags(obj->cpuHandle);
758 
759 
760  // setup the clocks
761  HAL_setupClks(handle);
762 
763 
764  // Setup the PLL
765  HAL_setupPll(handle,PLL_ClkFreq_90_MHz);
766 
767 
768  // setup the PIE
769  HAL_setupPie(handle);
770 
771 
772  // run the device calibration
773  HAL_cal(handle);
774 
775 
776  // setup the peripheral clocks
777  HAL_setupPeripheralClks(handle);
778 
779 
780  // setup the GPIOs
781  HAL_setupGpios(handle);
782 
783 
784  // setup the flash
785  HAL_setupFlash(handle);
786 
787 
788  // setup the ADCs
789  HAL_setupAdcs(handle);
790 
791 
792  // setup the PWMs
793  HAL_setupPwms(handle,
794  (float_t)pUserParams->systemFreq_MHz,
795  pUserParams->pwmPeriod_usec,
796  USER_NUM_PWM_TICKS_PER_ISR_TICK);
797 
798 #ifdef QEP
799  // setup the QEP
800  HAL_setupQEP(handle,HAL_Qep_QEP1);
801  HAL_setupQEP(handle,HAL_Qep_QEP2);
802 #endif
803 
804  // setup the spiA
805  HAL_setupSpiA(handle);
806 
807 
808  // setup the spiB
809  HAL_setupSpiB(handle);
810 
811 
812  // setup the PWM DACs
813  HAL_setupPwmDacs(handle);
814 
815 
816  // setup the timers
817  HAL_setupTimers(handle,
818  (float_t)pUserParams->systemFreq_MHz);
819 
820 
821  // setup the drv8301 interface
822  HAL_setupGate(handle);
823 
824 
825  // set the default current bias
826  {
827  uint_least8_t cnt;
828  _iq bias = _IQ12mpy(ADC_dataBias,_IQ(pUserParams->current_sf));
829 
830  for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
831  {
832  HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
833  }
834  }
835 
836 
837  // set the current scale factor
838  {
839  _iq current_sf = _IQ(pUserParams->current_sf);
840 
841  HAL_setCurrentScaleFactor(handle,current_sf);
842  }
843 
844 
845  // set the default voltage bias
846  {
847  uint_least8_t cnt;
848  _iq bias = _IQ(0.0);
849 
850  for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
851  {
852  HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
853  }
854  }
855 
856 
857  // set the voltage scale factor
858  {
859  _iq voltage_sf = _IQ(pUserParams->voltage_sf);
860 
861  HAL_setVoltageScaleFactor(handle,voltage_sf);
862  }
863 
864  return;
865 } // end of HAL_setParams() function
866 
867 
869 {
870  HAL_Obj *obj = (HAL_Obj *)handle;
871 
872 
873  // disable the ADCs
874  ADC_disable(obj->adcHandle);
875 
876 
877  // power up the bandgap circuit
878  ADC_enableBandGap(obj->adcHandle);
879 
880 
881  // set the ADC voltage reference source to internal
882  ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
883 
884 
885  // enable the ADC reference buffers
886  ADC_enableRefBuffers(obj->adcHandle);
887 
888 
889  // Set main clock scaling factor (max45MHz clock for the ADC module)
890  ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
891 
892 
893  // power up the ADCs
894  ADC_powerUp(obj->adcHandle);
895 
896 
897  // enable the ADCs
898  ADC_enable(obj->adcHandle);
899 
900 
901  // set the ADC interrupt pulse generation to prior
902  ADC_setIntPulseGenMode(obj->adcHandle,ADC_IntPulseGenMode_Prior);
903 
904 
905  // set the temperature sensor source to external
906  ADC_setTempSensorSrc(obj->adcHandle,ADC_TempSensorSrc_Ext);
907 
908 
909  // configure the interrupt sources
910  ADC_disableInt(obj->adcHandle,ADC_IntNumber_1);
911  ADC_setIntMode(obj->adcHandle,ADC_IntNumber_1,ADC_IntMode_ClearFlag);
912  ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_1,ADC_IntSrc_EOC7);
913 
914 #ifdef J5
915  //configure the SOCs for boostxldrv8301_revB on J5 Connection
916  // EXT IA-FB
917  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A3);
918  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM4_ADCSOCA);
919  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
920 
921  // EXT IA-FB
922  // Duplicate conversion due to ADC Initial Conversion bug (SPRZ342)
923  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A3);
924  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM4_ADCSOCA);
925  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
926 
927  // EXT IB-FB
928  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B3);
929  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM4_ADCSOCA);
930  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
931 
932  // EXT IC-FB
933  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A4);
934  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM4_ADCSOCA);
935  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
936 
937  // ADC-Vhb1
938  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B4);
939  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM4_ADCSOCA);
940  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
941 
942  // ADC-Vhb2
943  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A5);
944  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM4_ADCSOCA);
945  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
946 
947  // ADC-Vhb3
948  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B5);
949  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM4_ADCSOCA);
950  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
951 
952  // VDCBUS
953  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_B7);
954  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM4_ADCSOCA);
955  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
956 #else
957  //configure the SOCs for boostxldrv8301_revB on J1 Connection
958  // EXT IA-FB
959  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A0);
960  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM1_ADCSOCA);
961  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
962 
963  // EXT IA-FB
964  // Duplicate conversion due to ADC Initial Conversion bug (SPRZ342)
965  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A0);
966  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM1_ADCSOCA);
967  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
968 
969  // EXT IB-FB
970  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B0);
971  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM1_ADCSOCA);
972  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
973 
974  // EXT IC-FB
975  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A1);
976  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM1_ADCSOCA);
977  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
978 
979  // ADC-Vhb1
980  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B1);
981  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM1_ADCSOCA);
982  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
983 
984  // ADC-Vhb2
985  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A2);
986  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM1_ADCSOCA);
987  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
988 
989  // ADC-Vhb3
990  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B2);
991  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM1_ADCSOCA);
992  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
993 
994  // VDCBUS
995  ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_A7);
996  ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM1_ADCSOCA);
997  ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
998 #endif
999  return;
1000 } // end of HAL_setupAdcs() function
1001 
1002 
1004 {
1005  HAL_Obj *obj = (HAL_Obj *)handle;
1006 
1007 
1008  // enable internal oscillator 1
1009  CLK_enableOsc1(obj->clkHandle);
1010 
1011  // set the oscillator source
1012  CLK_setOscSrc(obj->clkHandle,CLK_OscSrc_Internal);
1013 
1014  // disable the external clock in
1015  CLK_disableClkIn(obj->clkHandle);
1016 
1017  // disable the crystal oscillator
1018  CLK_disableCrystalOsc(obj->clkHandle);
1019 
1020  // disable oscillator 2
1021  CLK_disableOsc2(obj->clkHandle);
1022 
1023  // set the low speed clock prescaler
1024  CLK_setLowSpdPreScaler(obj->clkHandle,CLK_LowSpdPreScaler_SysClkOut_by_1);
1025 
1026  // set the clock out prescaler
1027  CLK_setClkOutPreScaler(obj->clkHandle,CLK_ClkOutPreScaler_SysClkOut_by_1);
1028 
1029  return;
1030 } // end of HAL_setupClks() function
1031 
1032 
1034 {
1035  HAL_Obj *obj = (HAL_Obj *)handle;
1036 
1037 
1038  FLASH_enablePipelineMode(obj->flashHandle);
1039 
1040  FLASH_setNumPagedReadWaitStates(obj->flashHandle,FLASH_NumPagedWaitStates_3);
1041 
1042  FLASH_setNumRandomReadWaitStates(obj->flashHandle,FLASH_NumRandomWaitStates_3);
1043 
1044  FLASH_setOtpWaitStates(obj->flashHandle,FLASH_NumOtpWaitStates_5);
1045 
1046  FLASH_setStandbyWaitCount(obj->flashHandle,FLASH_STANDBY_WAIT_COUNT_DEFAULT);
1047 
1048  FLASH_setActiveWaitCount(obj->flashHandle,FLASH_ACTIVE_WAIT_COUNT_DEFAULT);
1049 
1050  return;
1051 } // HAL_setupFlash() function
1052 
1053 
1055 {
1056  HAL_Obj *obj = (HAL_Obj *)handle;
1057 
1058  DRV8301_setGpioHandle(obj->drv8301Handle,obj->gpioHandle);
1059 #ifdef J5
1060  DRV8301_setSpiHandle(obj->drv8301Handle,obj->spiBHandle);
1061  DRV8301_setGpioNumber(obj->drv8301Handle,GPIO_Number_52);
1062 #else
1063  DRV8301_setSpiHandle(obj->drv8301Handle,obj->spiAHandle);
1064  DRV8301_setGpioNumber(obj->drv8301Handle,GPIO_Number_50);
1065 #endif
1066 
1067  return;
1068 } // HAL_setupGate() function
1069 
1070 
1072 {
1073  HAL_Obj *obj = (HAL_Obj *)handle;
1074 
1075 
1076  // PWM1
1077  GPIO_setMode(obj->gpioHandle,GPIO_Number_0,GPIO_0_Mode_EPWM1A);
1078 
1079  // PWM2
1080  GPIO_setMode(obj->gpioHandle,GPIO_Number_1,GPIO_1_Mode_EPWM1B);
1081 
1082  // PWM3
1083  GPIO_setMode(obj->gpioHandle,GPIO_Number_2,GPIO_2_Mode_EPWM2A);
1084 
1085  // PWM4
1086  GPIO_setMode(obj->gpioHandle,GPIO_Number_3,GPIO_3_Mode_EPWM2B);
1087 
1088  // PWM5
1089  GPIO_setMode(obj->gpioHandle,GPIO_Number_4,GPIO_4_Mode_EPWM3A);
1090 
1091  // PWM6
1092  GPIO_setMode(obj->gpioHandle,GPIO_Number_5,GPIO_5_Mode_EPWM3B);
1093 
1094  // PWM4A
1095  GPIO_setMode(obj->gpioHandle,GPIO_Number_6,GPIO_6_Mode_EPWM4A);
1096 
1097  // PWM4B
1098  GPIO_setMode(obj->gpioHandle,GPIO_Number_7,GPIO_7_Mode_EPWM4B);
1099 
1100  // PWM5A
1101  GPIO_setMode(obj->gpioHandle,GPIO_Number_8,GPIO_8_Mode_EPWM5A);
1102 
1103  // PWM5B
1104  GPIO_setMode(obj->gpioHandle,GPIO_Number_9,GPIO_9_Mode_EPWM5B);
1105 
1106  // PWM6A
1107  GPIO_setMode(obj->gpioHandle,GPIO_Number_10,GPIO_10_Mode_EPWM6A);
1108 
1109  // PWM6B
1110  GPIO_setMode(obj->gpioHandle,GPIO_Number_11,GPIO_11_Mode_EPWM6B);
1111 
1112  // GPIO
1113  GPIO_setMode(obj->gpioHandle,GPIO_Number_12,GPIO_12_Mode_GeneralPurpose);
1114 
1115  // GPIO
1116  GPIO_setMode(obj->gpioHandle,GPIO_Number_13,GPIO_13_Mode_GeneralPurpose);
1117 
1118  // SPIB CLK
1119  GPIO_setMode(obj->gpioHandle,GPIO_Number_14,GPIO_14_Mode_SPICLKB);
1120 
1121  // UARTB RX
1122  GPIO_setMode(obj->gpioHandle,GPIO_Number_15,GPIO_15_Mode_SCIRXDB);
1123 
1124  // Set Qualification Period for GPIO16-23, 22*2*(1/90MHz) = 0.48us
1125  GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_16,22);
1126 
1127  // SPIA SIMO
1128  GPIO_setMode(obj->gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);
1129 
1130  // SPIA SOMI
1131  GPIO_setMode(obj->gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);
1132 
1133  // SPIA CLK
1134  GPIO_setMode(obj->gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);
1135 
1136  // SPIA CS
1137  GPIO_setMode(obj->gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);
1138 
1139 #ifdef QEP
1140  // EQEP1A
1141  GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_EQEP1A);
1142  GPIO_setQualification(obj->gpioHandle,GPIO_Number_20,GPIO_Qual_Sample_3);
1143 
1144  // EQEP1B
1145  GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_EQEP1B);
1146  GPIO_setQualification(obj->gpioHandle,GPIO_Number_21,GPIO_Qual_Sample_3);
1147 
1148  // GPIO
1149  GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
1150 
1151  // EQEP1I
1152  GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_EQEP1I);
1153  GPIO_setQualification(obj->gpioHandle,GPIO_Number_23,GPIO_Qual_Sample_3);
1154 #else
1155  // GPIO
1156  GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_GeneralPurpose);
1157 
1158  // GPIO
1159  GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_GeneralPurpose);
1160 
1161  // GPIO
1162  GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
1163 
1164  // GPIO
1165  GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_GeneralPurpose);
1166 #endif
1167 
1168  // SPIB SIMO
1169  GPIO_setMode(obj->gpioHandle,GPIO_Number_24,GPIO_24_Mode_SPISIMOB);
1170 
1171  // SPIB SOMI
1172  GPIO_setMode(obj->gpioHandle,GPIO_Number_25,GPIO_25_Mode_SPISOMIB);
1173 
1174  // GPIO
1175  GPIO_setMode(obj->gpioHandle,GPIO_Number_26,GPIO_26_Mode_GeneralPurpose);
1176 
1177  // SPIB CS
1178  GPIO_setMode(obj->gpioHandle,GPIO_Number_27,GPIO_27_Mode_SPISTEB_NOT);
1179 
1180  // OCTWn
1181  GPIO_setMode(obj->gpioHandle,GPIO_Number_28,GPIO_28_Mode_TZ2_NOT);
1182 
1183  // FAULTn
1184  GPIO_setMode(obj->gpioHandle,GPIO_Number_29,GPIO_29_Mode_TZ3_NOT);
1185 
1186  // CAN RX
1187  GPIO_setMode(obj->gpioHandle,GPIO_Number_30,GPIO_30_Mode_CANRXA);
1188 
1189  // CAN TX
1190  GPIO_setMode(obj->gpioHandle,GPIO_Number_31,GPIO_31_Mode_CANTXA);
1191 
1192  // I2C Data
1193  GPIO_setMode(obj->gpioHandle,GPIO_Number_32,GPIO_32_Mode_SDAA);
1194 
1195  // I2C Clock
1196  GPIO_setMode(obj->gpioHandle,GPIO_Number_33,GPIO_33_Mode_SCLA);
1197 
1198  // LED D9
1199  GPIO_setMode(obj->gpioHandle,GPIO_Number_34,GPIO_34_Mode_GeneralPurpose);
1200  GPIO_setLow(obj->gpioHandle,GPIO_Number_34);
1201  GPIO_setDirection(obj->gpioHandle,GPIO_Number_34,GPIO_Direction_Output);
1202 
1203  // JTAG
1204  GPIO_setMode(obj->gpioHandle,GPIO_Number_35,GPIO_35_Mode_JTAG_TDI);
1205  GPIO_setMode(obj->gpioHandle,GPIO_Number_36,GPIO_36_Mode_JTAG_TMS);
1206  GPIO_setMode(obj->gpioHandle,GPIO_Number_37,GPIO_37_Mode_JTAG_TDO);
1207  GPIO_setMode(obj->gpioHandle,GPIO_Number_38,GPIO_38_Mode_JTAG_TCK);
1208 
1209  // LED D10
1210  GPIO_setMode(obj->gpioHandle,GPIO_Number_39,GPIO_39_Mode_GeneralPurpose);
1211  GPIO_setLow(obj->gpioHandle,GPIO_Number_39);
1212  GPIO_setDirection(obj->gpioHandle,GPIO_Number_39,GPIO_Direction_Output);
1213 
1214  // DAC1
1215  GPIO_setMode(obj->gpioHandle,GPIO_Number_40,GPIO_40_Mode_EPWM7A);
1216 
1217  // DAC2
1218  GPIO_setMode(obj->gpioHandle,GPIO_Number_41,GPIO_41_Mode_EPWM7B);
1219 
1220  // DAC3
1221  GPIO_setMode(obj->gpioHandle,GPIO_Number_42,GPIO_42_Mode_EPWM8A);
1222 
1223  // DAC4
1224  GPIO_setMode(obj->gpioHandle,GPIO_Number_43,GPIO_43_Mode_EPWM8B);
1225 
1226  // GPIO
1227  GPIO_setMode(obj->gpioHandle,GPIO_Number_44,GPIO_44_Mode_GeneralPurpose);
1228 
1229  // Set Qualification Period for GPIO50-55, 22*2*(1/90MHz) = 0.48us
1230  GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_50,22);
1231 
1232  // DRV8301 Enable Gate
1233  GPIO_setMode(obj->gpioHandle,GPIO_Number_50,GPIO_50_Mode_GeneralPurpose);
1234  GPIO_setLow(obj->gpioHandle,GPIO_Number_50);
1235  GPIO_setDirection(obj->gpioHandle,GPIO_Number_50,GPIO_Direction_Output);
1236 
1237  // DRV8301 DC Calibration
1238  GPIO_setMode(obj->gpioHandle,GPIO_Number_51,GPIO_51_Mode_GeneralPurpose);
1239  GPIO_setLow(obj->gpioHandle,GPIO_Number_51);
1240  GPIO_setDirection(obj->gpioHandle,GPIO_Number_51,GPIO_Direction_Output);
1241 
1242  // DRV8301 Enable Gate
1243  GPIO_setMode(obj->gpioHandle,GPIO_Number_52,GPIO_52_Mode_GeneralPurpose);
1244  GPIO_setLow(obj->gpioHandle,GPIO_Number_52);
1245  GPIO_setDirection(obj->gpioHandle,GPIO_Number_52,GPIO_Direction_Output);
1246 
1247  // DRV8301 Device Calibration
1248  GPIO_setMode(obj->gpioHandle,GPIO_Number_53,GPIO_53_Mode_GeneralPurpose);
1249  GPIO_setLow(obj->gpioHandle,GPIO_Number_53);
1250  GPIO_setDirection(obj->gpioHandle,GPIO_Number_53,GPIO_Direction_Output);
1251 
1252  // Set Qualification Period for GPIO56-58, 22*2*(1/90MHz) = 0.48us
1253  GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_56,22);
1254 
1255 #ifdef QEP
1256  // EQEP2A
1257  GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_EQEP2A);
1258  GPIO_setQualification(obj->gpioHandle,GPIO_Number_54,GPIO_Qual_Sample_3);
1259 
1260  // EQEP2B
1261  GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_EQEP2B);
1262  GPIO_setQualification(obj->gpioHandle,GPIO_Number_55,GPIO_Qual_Sample_3);
1263 
1264  // EQEP2I
1265  GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_EQEP2I);
1266  GPIO_setQualification(obj->gpioHandle,GPIO_Number_56,GPIO_Qual_Sample_3);
1267 #else
1268  // GPIO
1269  GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_GeneralPurpose);
1270 
1271  // GPIO
1272  GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_GeneralPurpose);
1273 
1274  // GPIO
1275  GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_GeneralPurpose);
1276 #endif
1277 
1278  // No Connection
1279  GPIO_setMode(obj->gpioHandle,GPIO_Number_57,GPIO_57_Mode_GeneralPurpose);
1280 
1281  // UARTB TX
1282  GPIO_setMode(obj->gpioHandle,GPIO_Number_58,GPIO_58_Mode_SCITXDB);
1283 
1284  return;
1285 } // end of HAL_setupGpios() function
1286 
1287 
1289 {
1290  HAL_Obj *obj = (HAL_Obj *)handle;
1291 
1292 
1293  PIE_disable(obj->pieHandle);
1294 
1295  PIE_disableAllInts(obj->pieHandle);
1296 
1297  PIE_clearAllInts(obj->pieHandle);
1298 
1299  PIE_clearAllFlags(obj->pieHandle);
1300 
1301  PIE_setDefaultIntVectorTable(obj->pieHandle);
1302 
1303  PIE_enable(obj->pieHandle);
1304 
1305  return;
1306 } // end of HAL_setupPie() function
1307 
1308 
1310 {
1311  HAL_Obj *obj = (HAL_Obj *)handle;
1312 
1313 
1314  CLK_enableAdcClock(obj->clkHandle);
1315 
1316  CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_1);
1317  CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_2);
1318  CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_3);
1319 
1320  CLK_disableEcap1Clock(obj->clkHandle);
1321 
1322  CLK_enableEcanaClock(obj->clkHandle);
1323 
1324 #ifdef QEP
1325  CLK_enableEqep1Clock(obj->clkHandle);
1326  CLK_enableEqep2Clock(obj->clkHandle);
1327 #endif
1328 
1329  CLK_enablePwmClock(obj->clkHandle,PWM_Number_1);
1330  CLK_enablePwmClock(obj->clkHandle,PWM_Number_2);
1331  CLK_enablePwmClock(obj->clkHandle,PWM_Number_3);
1332  CLK_enablePwmClock(obj->clkHandle,PWM_Number_4);
1333  CLK_enablePwmClock(obj->clkHandle,PWM_Number_5);
1334  CLK_enablePwmClock(obj->clkHandle,PWM_Number_6);
1335  CLK_enablePwmClock(obj->clkHandle,PWM_Number_7);
1336  CLK_enablePwmClock(obj->clkHandle,PWM_Number_8);
1337 
1338  CLK_disableHrPwmClock(obj->clkHandle);
1339 
1340  CLK_enableI2cClock(obj->clkHandle);
1341 
1342  CLK_disableLinAClock(obj->clkHandle);
1343 
1344  CLK_disableClaClock(obj->clkHandle);
1345 
1346  CLK_disableSciaClock(obj->clkHandle);
1347  CLK_enableScibClock(obj->clkHandle);
1348 
1349  CLK_enableSpiaClock(obj->clkHandle);
1350  CLK_enableSpibClock(obj->clkHandle);
1351 
1352  CLK_enableTbClockSync(obj->clkHandle);
1353 
1354  return;
1355 } // end of HAL_setupPeripheralClks() function
1356 
1357 
1358 void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq)
1359 {
1360  HAL_Obj *obj = (HAL_Obj *)handle;
1361 
1362 
1363  // make sure PLL is not running in limp mode
1364  if(PLL_getClkStatus(obj->pllHandle) != PLL_ClkStatus_Normal)
1365  {
1366  // reset the clock detect
1367  PLL_resetClkDetect(obj->pllHandle);
1368 
1369  // ???????
1370  asm(" ESTOP0");
1371  }
1372 
1373 
1374  // Divide Select must be ClkIn/4 before the clock rate can be changed
1375  if(PLL_getDivideSelect(obj->pllHandle) != PLL_DivideSelect_ClkIn_by_4)
1376  {
1377  PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_4);
1378  }
1379 
1380 
1381  if(PLL_getClkFreq(obj->pllHandle) != clkFreq)
1382  {
1383  // disable the clock detect
1384  PLL_disableClkDetect(obj->pllHandle);
1385 
1386  // set the clock rate
1387  PLL_setClkFreq(obj->pllHandle,clkFreq);
1388  }
1389 
1390 
1391  // wait until locked
1392  while(PLL_getLockStatus(obj->pllHandle) != PLL_LockStatus_Done) {}
1393 
1394 
1395  // enable the clock detect
1396  PLL_enableClkDetect(obj->pllHandle);
1397 
1398 
1399  // set divide select to ClkIn/2 to get desired clock rate
1400  // NOTE: clock must be locked before setting this register
1401  PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_2);
1402 
1403  return;
1404 } // end of HAL_setupPll() function
1405 
1406 
1408  const float_t systemFreq_MHz,
1409  const float_t pwmPeriod_usec,
1410  const uint_least16_t numPwmTicksPerIsrTick)
1411 {
1412  HAL_Obj *obj = (HAL_Obj *)handle;
1413  uint16_t halfPeriod_cycles = (uint16_t)(systemFreq_MHz*pwmPeriod_usec) >> 1;
1414  uint_least8_t cnt;
1415 
1416 
1417  // turns off the outputs of the EPWM peripherals which will put the power switches
1418  // into a high impedance state.
1419  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_1]);
1420  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_2]);
1421  PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_3]);
1422 
1423  for(cnt=0;cnt<3;cnt++)
1424  {
1425  // setup the Time-Base Control Register (TBCTL)
1426  PWM_setCounterMode(obj->pwmHandle[cnt],PWM_CounterMode_UpDown);
1427  PWM_disableCounterLoad(obj->pwmHandle[cnt]);
1428  PWM_setPeriodLoad(obj->pwmHandle[cnt],PWM_PeriodLoad_Immediate);
1429  PWM_setSyncMode(obj->pwmHandle[cnt],PWM_SyncMode_EPWMxSYNC);
1430  PWM_setHighSpeedClkDiv(obj->pwmHandle[cnt],PWM_HspClkDiv_by_1);
1431  PWM_setClkDiv(obj->pwmHandle[cnt],PWM_ClkDiv_by_1);
1432  PWM_setPhaseDir(obj->pwmHandle[cnt],PWM_PhaseDir_CountUp);
1433  PWM_setRunMode(obj->pwmHandle[cnt],PWM_RunMode_FreeRun);
1434 
1435  // setup the Timer-Based Phase Register (TBPHS)
1436  PWM_setPhase(obj->pwmHandle[cnt],0);
1437 
1438  // setup the Time-Base Counter Register (TBCTR)
1439  PWM_setCount(obj->pwmHandle[cnt],0);
1440 
1441  // setup the Time-Base Period Register (TBPRD)
1442  // set to zero initially
1443  PWM_setPeriod(obj->pwmHandle[cnt],0);
1444 
1445  // setup the Counter-Compare Control Register (CMPCTL)
1446  PWM_setLoadMode_CmpA(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
1447  PWM_setLoadMode_CmpB(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
1448  PWM_setShadowMode_CmpA(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
1449  PWM_setShadowMode_CmpB(obj->pwmHandle[cnt],PWM_ShadowMode_Immediate);
1450 
1451  // setup the Action-Qualifier Output A Register (AQCTLA)
1452  PWM_setActionQual_CntUp_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Set);
1453  PWM_setActionQual_CntDown_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Clear);
1454 
1455  // setup the Dead-Band Generator Control Register (DBCTL)
1456  PWM_setDeadBandOutputMode(obj->pwmHandle[cnt],PWM_DeadBandOutputMode_EPWMxA_Rising_EPWMxB_Falling);
1457  PWM_setDeadBandPolarity(obj->pwmHandle[cnt],PWM_DeadBandPolarity_EPWMxB_Inverted);
1458 
1459  // setup the Dead-Band Rising Edge Delay Register (DBRED)
1460  PWM_setDeadBandRisingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBRED_CNT);
1461 
1462  // setup the Dead-Band Falling Edge Delay Register (DBFED)
1463  PWM_setDeadBandFallingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBFED_CNT);
1464  // setup the PWM-Chopper Control Register (PCCTL)
1465  PWM_disableChopping(obj->pwmHandle[cnt]);
1466 
1467  // setup the Trip Zone Select Register (TZSEL)
1468  PWM_disableTripZones(obj->pwmHandle[cnt]);
1469  }
1470 
1471 
1472  // setup the Event Trigger Selection Register (ETSEL)
1473  PWM_disableInt(obj->pwmHandle[PWM_Number_1]);
1474  PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualZero);
1475  PWM_enableSocAPulse(obj->pwmHandle[PWM_Number_1]);
1476 
1477 
1478  // setup the Event Trigger Prescale Register (ETPS)
1479  if(numPwmTicksPerIsrTick == 3)
1480  {
1481  PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_ThirdEvent);
1482  PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_ThirdEvent);
1483  }
1484  else if(numPwmTicksPerIsrTick == 2)
1485  {
1486  PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_SecondEvent);
1487  PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_SecondEvent);
1488  }
1489  else
1490  {
1491  PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_FirstEvent);
1492  PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_FirstEvent);
1493  }
1494 
1495 
1496  // setup the Event Trigger Clear Register (ETCLR)
1497  PWM_clearIntFlag(obj->pwmHandle[PWM_Number_1]);
1498  PWM_clearSocAFlag(obj->pwmHandle[PWM_Number_1]);
1499 
1500  // first step to synchronize the pwms
1501  CLK_disableTbClockSync(obj->clkHandle);
1502 
1503  // since the PWM is configured as an up/down counter, the period register is set to one-half
1504  // of the desired PWM period
1505  PWM_setPeriod(obj->pwmHandle[PWM_Number_1],halfPeriod_cycles);
1506  PWM_setPeriod(obj->pwmHandle[PWM_Number_2],halfPeriod_cycles);
1507  PWM_setPeriod(obj->pwmHandle[PWM_Number_3],halfPeriod_cycles);
1508 
1509  // last step to synchronize the pwms
1510  CLK_enableTbClockSync(obj->clkHandle);
1511 
1512  return;
1513 } // end of HAL_setupPwms() function
1514 
1515 #ifdef QEP
1516 void HAL_setupQEP(HAL_Handle handle,HAL_QepSelect_e qep)
1517 {
1518  HAL_Obj *obj = (HAL_Obj *)handle;
1519 
1520 
1521  // hold the counter in reset
1522  QEP_reset_counter(obj->qepHandle[qep]);
1523 
1524  // set the QPOSINIT register
1525  QEP_set_posn_init_count(obj->qepHandle[qep], 0);
1526 
1527  // disable all interrupts
1528  QEP_disable_all_interrupts(obj->qepHandle[qep]);
1529 
1530  // clear the interrupt flags
1531  QEP_clear_all_interrupt_flags(obj->qepHandle[qep]);
1532 
1533  // clear the position counter
1534  QEP_clear_posn_counter(obj->qepHandle[qep]);
1535 
1536  // setup the max position
1537  QEP_set_max_posn_count(obj->qepHandle[qep], (4*USER_MOTOR_ENCODER_LINES)-1);
1538 
1539  // setup the QDECCTL register
1540  QEP_set_QEP_source(obj->qepHandle[qep], QEP_Qsrc_Quad_Count_Mode);
1541  QEP_disable_sync_out(obj->qepHandle[qep]);
1542  QEP_set_swap_quad_inputs(obj->qepHandle[qep], QEP_Swap_Not_Swapped);
1543  QEP_disable_gate_index(obj->qepHandle[qep]);
1544  QEP_set_ext_clock_rate(obj->qepHandle[qep], QEP_Xcr_2x_Res);
1545  QEP_set_A_polarity(obj->qepHandle[qep], QEP_Qap_No_Effect);
1546  QEP_set_B_polarity(obj->qepHandle[qep], QEP_Qbp_No_Effect);
1547  QEP_set_index_polarity(obj->qepHandle[qep], QEP_Qip_No_Effect);
1548 
1549  // setup the QEPCTL register
1550  QEP_set_emu_control(obj->qepHandle[qep], QEPCTL_Freesoft_Unaffected_Halt);
1551  QEP_set_posn_count_reset_mode(obj->qepHandle[qep], QEPCTL_Pcrm_Max_Reset);
1552  QEP_set_strobe_event_init(obj->qepHandle[qep], QEPCTL_Sei_Nothing);
1553  QEP_set_index_event_init(obj->qepHandle[qep], QEPCTL_Iei_Nothing);
1554  QEP_set_index_event_latch(obj->qepHandle[qep], QEPCTL_Iel_Rising_Edge);
1555  QEP_set_soft_init(obj->qepHandle[qep], QEPCTL_Swi_Nothing);
1556  QEP_disable_unit_timer(obj->qepHandle[qep]);
1557  QEP_disable_watchdog(obj->qepHandle[qep]);
1558 
1559  // setup the QPOSCTL register
1560  QEP_disable_posn_compare(obj->qepHandle[qep]);
1561 
1562  // setup the QCAPCTL register
1563  QEP_disable_capture(obj->qepHandle[qep]);
1564 
1565  // renable the position counter
1566  QEP_enable_counter(obj->qepHandle[qep]);
1567 
1568 
1569  return;
1570 }
1571 #endif
1572 
1574 {
1575  HAL_Obj *obj = (HAL_Obj *)handle;
1576 
1577  SPI_reset(obj->spiAHandle);
1578  SPI_setMode(obj->spiAHandle,SPI_Mode_Master);
1579  SPI_setClkPolarity(obj->spiAHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
1580  SPI_enableTx(obj->spiAHandle);
1581  SPI_enableTxFifoEnh(obj->spiAHandle);
1582  SPI_enableTxFifo(obj->spiAHandle);
1583  SPI_setTxDelay(obj->spiAHandle,0x0018);
1584  SPI_setBaudRate(obj->spiAHandle,(SPI_BaudRate_e)(0x000d));
1585  SPI_setCharLength(obj->spiAHandle,SPI_CharLength_16_Bits);
1586  SPI_setSuspend(obj->spiAHandle,SPI_TxSuspend_free);
1587  SPI_enable(obj->spiAHandle);
1588 
1589  return;
1590 } // end of HAL_setupSpiA() function
1591 
1592 
1594 {
1595  HAL_Obj *obj = (HAL_Obj *)handle;
1596 
1597  SPI_reset(obj->spiBHandle);
1598  SPI_setMode(obj->spiBHandle,SPI_Mode_Master);
1599  SPI_setClkPolarity(obj->spiBHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
1600  SPI_enableTx(obj->spiBHandle);
1601  SPI_enableTxFifoEnh(obj->spiBHandle);
1602  SPI_enableTxFifo(obj->spiBHandle);
1603  SPI_setTxDelay(obj->spiBHandle,0x0018);
1604  SPI_setBaudRate(obj->spiBHandle,(SPI_BaudRate_e)(0x000d));
1605  SPI_setCharLength(obj->spiBHandle,SPI_CharLength_16_Bits);
1606  SPI_setSuspend(obj->spiBHandle,SPI_TxSuspend_free);
1607  SPI_enable(obj->spiBHandle);
1608 
1609  return;
1610 } // end of HAL_setupSpiB() function
1611 
1612 
1614 {
1615  HAL_Obj *obj = (HAL_Obj *)handle;
1616  uint16_t halfPeriod_cycles = 512; // 3000->10kHz, 1500->20kHz, 1000-> 30kHz, 500->60kHz
1617  uint_least8_t cnt;
1618 
1619 
1620  for(cnt=0;cnt<2;cnt++)
1621  {
1622  // initialize the Time-Base Control Register (TBCTL)
1623  PWMDAC_setCounterMode(obj->pwmDacHandle[cnt],PWM_CounterMode_UpDown);
1624  PWMDAC_disableCounterLoad(obj->pwmDacHandle[cnt]);
1625  PWMDAC_setPeriodLoad(obj->pwmDacHandle[cnt],PWM_PeriodLoad_Immediate);
1626  PWMDAC_setSyncMode(obj->pwmDacHandle[cnt],PWM_SyncMode_EPWMxSYNC);
1627  PWMDAC_setHighSpeedClkDiv(obj->pwmDacHandle[cnt],PWM_HspClkDiv_by_1);
1628  PWMDAC_setClkDiv(obj->pwmDacHandle[cnt],PWM_ClkDiv_by_1);
1629  PWMDAC_setPhaseDir(obj->pwmDacHandle[cnt],PWM_PhaseDir_CountUp);
1630  PWMDAC_setRunMode(obj->pwmDacHandle[cnt],PWM_RunMode_FreeRun);
1631 
1632  // initialize the Timer-Based Phase Register (TBPHS)
1633  PWMDAC_setPhase(obj->pwmDacHandle[cnt],0);
1634 
1635  // setup the Time-Base Counter Register (TBCTR)
1636  PWMDAC_setCount(obj->pwmDacHandle[cnt],0);
1637 
1638  // Initialize the Time-Base Period Register (TBPRD)
1639  // set to zero initially
1640  PWMDAC_setPeriod(obj->pwmDacHandle[cnt],0);
1641 
1642  // initialize the Counter-Compare Control Register (CMPCTL)
1643  PWMDAC_setLoadMode_CmpA(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
1644  PWMDAC_setLoadMode_CmpB(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
1645  PWMDAC_setShadowMode_CmpA(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
1646  PWMDAC_setShadowMode_CmpB(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
1647 
1648  // Initialize the Action-Qualifier Output A Register (AQCTLA)
1649  PWMDAC_setActionQual_CntUp_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
1650  PWMDAC_setActionQual_CntDown_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
1651  PWMDAC_setActionQual_CntUp_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
1652  PWMDAC_setActionQual_CntDown_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
1653 
1654  // Initialize the Dead-Band Control Register (DBCTL)
1655  PWMDAC_disableDeadBand(obj->pwmDacHandle[cnt]);
1656 
1657  // Initialize the PWM-Chopper Control Register (PCCTL)
1658  PWMDAC_disableChopping(obj->pwmDacHandle[cnt]);
1659 
1660  // Initialize the Trip-Zone Control Register (TZSEL)
1661  PWMDAC_disableTripZones(obj->pwmDacHandle[cnt]);
1662 
1663  // Initialize the Trip-Zone Control Register (TZCTL)
1664  PWMDAC_setTripZoneState_TZA(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1665  PWMDAC_setTripZoneState_TZB(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1666  PWMDAC_setTripZoneState_DCAEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1667  PWMDAC_setTripZoneState_DCAEVT2(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1668  PWMDAC_setTripZoneState_DCBEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1669  }
1670 
1671  // since the PWM is configured as an up/down counter, the period register is set to one-half
1672  // of the desired PWM period
1673  PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_1],halfPeriod_cycles);
1674  PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_2],halfPeriod_cycles);
1675 
1676  return;
1677 } // end of HAL_setupPwmDacs() function
1678 
1679 
1680 void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz)
1681 {
1682  HAL_Obj *obj = (HAL_Obj *)handle;
1683  uint32_t timerPeriod_cnts = (uint32_t)(systemFreq_MHz * (float_t)1000000.0) - 1;
1684 
1685  // use timer 0 for frequency diagnostics
1686  TIMER_setDecimationFactor(obj->timerHandle[0],0);
1687  TIMER_setEmulationMode(obj->timerHandle[0],TIMER_EmulationMode_RunFree);
1688  TIMER_setPeriod(obj->timerHandle[0],timerPeriod_cnts);
1689  TIMER_setPreScaler(obj->timerHandle[0],0);
1690 
1691  // use timer 1 for CPU usage diagnostics
1692  TIMER_setDecimationFactor(obj->timerHandle[1],0);
1693  TIMER_setEmulationMode(obj->timerHandle[1],TIMER_EmulationMode_RunFree);
1694  TIMER_setPeriod(obj->timerHandle[1],timerPeriod_cnts);
1695  TIMER_setPreScaler(obj->timerHandle[1],0);
1696 
1697  return;
1698 } // end of HAL_setupTimers() function
1699 
1700 
1701 void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
1702 {
1703  HAL_Obj *obj = (HAL_Obj *)handle;
1704 
1705  DRV8301_writeData(obj->drv8301Handle,Spi_8301_Vars);
1706 
1707  return;
1708 } // end of HAL_writeDrvData() function
1709 
1710 
1711 void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
1712 {
1713  HAL_Obj *obj = (HAL_Obj *)handle;
1714 
1715  DRV8301_readData(obj->drv8301Handle,Spi_8301_Vars);
1716 
1717  return;
1718 } // end of HAL_readDrvData() function
1719 
1720 
1721 void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
1722 {
1723  HAL_Obj *obj = (HAL_Obj *)handle;
1724 
1725  DRV8301_setupSpi(obj->drv8301Handle,Spi_8301_Vars);
1726 
1727  return;
1728 } // end of HAL_setupDrvSpi() function
1729 
1730 
1731 // 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.
#define HAL_PWM_DBFED_CNT
Defines the PWM deadband falling edge delay count (system clocks)
SPI_Handle spiBHandle
the SPI handle
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
void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
Writes data to the driver.
DRV8301_Obj drv8301
the drv8301 interface object
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.
#define HAL_PWM_DBRED_CNT
Defines the PWM deadband rising edge delay count (system 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_enableDrv(HAL_Handle handle)
Enables the 8301 device.
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.
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
Sets up the SPI interface for the driver.
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.
void HAL_setupSpiB(HAL_Handle handle)
Sets up the spiB peripheral.
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.
void HAL_setupGate(HAL_Handle handle)
Sets up the GATE object.
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_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
Reads data from the driver.
DRV8301_Handle drv8301Handle
the drv8301 interface handle
void HAL_setupAdcs(HAL_Handle handle)
Sets up the ADCs (Analog to Digital Converters)
void HAL_enableTimer0Int(HAL_Handle handle)
Enables the Timer 0 interrupt.
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.