MotorWare f2806x Module API Documentation

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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