MotorWare f2806x Module API Documentation

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

drv8301kit_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_enableDrv(HAL_Handle handle)
365 {
366 HAL_Obj *obj = (HAL_Obj *)handle;
367
368 DRV8301_enable(obj->drv8301Handle);
369
370 return;
371 } // end of HAL_enableDrv() function
372
373
374 void HAL_enableGlobalInts(HAL_Handle handle)
375 {
376 HAL_Obj *obj = (HAL_Obj *)handle;
377
378
379 CPU_enableGlobalInts(obj->cpuHandle);
380
381 return;
382 } // end of HAL_enableGlobalInts() function
383
384
385 void HAL_enablePwmInt(HAL_Handle handle)
386 {
387 HAL_Obj *obj = (HAL_Obj *)handle;
388
389
390 PIE_enablePwmInt(obj->pieHandle,PWM_Number_1);
391
392
393 // enable the interrupt
394 PWM_enableInt(obj->pwmHandle[PWM_Number_1]);
395
396
397 // enable the cpu interrupt for EPWM1_INT
398 CPU_enableInt(obj->cpuHandle,CPU_IntNumber_3);
399
400 return;
401 } // end of HAL_enablePwmInt() function
402
403
404 HAL_Handle HAL_init(void *pMemory,const size_t numBytes)
405 {
406 HAL_Handle handle;
407 HAL_Obj *obj;
408
409
410 if(numBytes < sizeof(HAL_Obj))
411 return((HAL_Handle)NULL);
412
413
414 // assign the handle
415 handle = (HAL_Handle)pMemory;
416
417
418 // assign the object
419 obj = (HAL_Obj *)handle;
420
421
422 // initialize the watchdog driver
423 obj->wdogHandle = WDOG_init((void *)WDOG_BASE_ADDR,sizeof(WDOG_Obj));
424
425
426 // disable watchdog
427 HAL_disableWdog(handle);
428
429
430 // initialize the ADC
431 obj->adcHandle = ADC_init((void *)ADC_BASE_ADDR,sizeof(ADC_Obj));
432
433
434 // initialize the clock handle
435 obj->clkHandle = CLK_init((void *)CLK_BASE_ADDR,sizeof(CLK_Obj));
436
437
438 // initialize the CPU handle
439 obj->cpuHandle = CPU_init(&cpu,sizeof(cpu));
440
441
442 // initialize the FLASH handle
443 obj->flashHandle = FLASH_init((void *)FLASH_BASE_ADDR,sizeof(FLASH_Obj));
444
445
446 // initialize the GPIO handle
447 obj->gpioHandle = GPIO_init((void *)GPIO_BASE_ADDR,sizeof(GPIO_Obj));
448
449
450 // initialize the oscillator handle
451 obj->oscHandle = OSC_init((void *)OSC_BASE_ADDR,sizeof(OSC_Obj));
452
453
454 // initialize the PIE handle
455 obj->pieHandle = PIE_init((void *)PIE_BASE_ADDR,sizeof(PIE_Obj));
456
457
458 // initialize the PLL handle
459 obj->pllHandle = PLL_init((void *)PLL_BASE_ADDR,sizeof(PLL_Obj));
460
461
462 // initialize the SPIA handle
463 obj->spiAHandle = SPI_init((void *)SPIA_BASE_ADDR,sizeof(SPI_Obj));
464
465
466 // initialize the SPIB handle
467 obj->spiBHandle = SPI_init((void *)SPIB_BASE_ADDR,sizeof(SPI_Obj));
468
469
470 // initialize PWM handles
471 obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM1_BASE_ADDR,sizeof(PWM_Obj));
472 obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM2_BASE_ADDR,sizeof(PWM_Obj));
473 obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM3_BASE_ADDR,sizeof(PWM_Obj));
474
475
476 // initialize PWM DAC handles
477 obj->pwmDacHandle[0] = PWMDAC_init((void *)PWM_ePWM6_BASE_ADDR,sizeof(PWM_Obj));
478 obj->pwmDacHandle[1] = PWMDAC_init((void *)PWM_ePWM5_BASE_ADDR,sizeof(PWM_Obj));
479 obj->pwmDacHandle[2] = PWMDAC_init((void *)PWM_ePWM4_BASE_ADDR,sizeof(PWM_Obj));
480
481
482 // initialize power handle
483 obj->pwrHandle = PWR_init((void *)PWR_BASE_ADDR,sizeof(PWR_Obj));
484
485
486 // initialize timer handles
487 obj->timerHandle[0] = TIMER_init((void *)TIMER0_BASE_ADDR,sizeof(TIMER_Obj));
488 obj->timerHandle[1] = TIMER_init((void *)TIMER1_BASE_ADDR,sizeof(TIMER_Obj));
489 obj->timerHandle[2] = TIMER_init((void *)TIMER2_BASE_ADDR,sizeof(TIMER_Obj));
490
491
492 // initialize drv8301 interface
493 obj->drv8301Handle = DRV8301_init(&obj->drv8301,sizeof(obj->drv8301));
494
495
496 #ifdef QEP
497 // initialize QEP driver
498 obj->qepHandle[0] = QEP_init((void*)QEP1_BASE_ADDR,sizeof(QEP_Obj));
499 obj->qepHandle[1] = QEP_init((void*)QEP2_BASE_ADDR,sizeof(QEP_Obj));
500 #endif
501
502 return(handle);
503 } // end of HAL_init() function
504
505
506 void HAL_OscTempComp(HAL_Handle handle)
507 {
508 HAL_Obj *obj = (HAL_Obj *)handle;
509 uint16_t Temperature;
510
511
512 // disable the ADCs
513 ADC_disable(obj->adcHandle);
514
515 // power up the bandgap circuit
516 ADC_enableBandGap(obj->adcHandle);
517
518 // set the ADC voltage reference source to internal
519 ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
520
521 // enable the ADC reference buffers
522 ADC_enableRefBuffers(obj->adcHandle);
523
524 // Set main clock scaling factor (max45MHz clock for the ADC module)
525 ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
526
527 // power up the ADCs
528 ADC_powerUp(obj->adcHandle);
529
530 // enable the ADCs
531 ADC_enable(obj->adcHandle);
532
533 // enable non-overlap mode
534 ADC_enableNoOverlapMode(obj->adcHandle);
535
536 // connect channel A5 internally to the temperature sensor
537 ADC_setTempSensorSrc(obj->adcHandle, ADC_TempSensorSrc_Int);
538
539 // set SOC0 channel select to ADCINA5
540 ADC_setSocChanNumber(obj->adcHandle, ADC_SocNumber_0, ADC_SocChanNumber_A5);
541
542 // set SOC0 acquisition period to 26 ADCCLK
543 ADC_setSocSampleDelay(obj->adcHandle, ADC_SocNumber_0, ADC_SocSampleDelay_64_cycles);
544
545 // connect ADCINT1 to EOC0
546 ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC0);
547
548 // clear ADCINT1 flag
549 ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
550
551 // enable ADCINT1
552 ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
553
554 // force start of conversion on SOC0
555 ADC_setSocFrc(obj->adcHandle, ADC_SocFrc_0);
556
557 // wait for end of conversion
558 while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
559
560 // clear ADCINT1 flag
561 ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
562
563 Temperature = ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
564
565 HAL_osc1Comp(handle, Temperature);
566
567 HAL_osc2Comp(handle, Temperature);
568
569 return;
570 } // end of HAL_OscTempComp() function
571
572
573 void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
574 {
575 HAL_Obj *obj = (HAL_Obj *)handle;
576 int16_t compOscFineTrim;
577
578
579 ENABLE_PROTECTED_REGISTER_WRITE_MODE;
580
581 compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc1FineTrimSlope()
582 + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc1FineTrimOffset() - OSC_POSTRIM;
583
584 if(compOscFineTrim > 31)
585 {
586 compOscFineTrim = 31;
587 }
588 else if(compOscFineTrim < -31)
589 {
590 compOscFineTrim = -31;
591 }
592
593 OSC_setTrim(obj->oscHandle, OSC_Number_1, HAL_computeOscTrimValue(getOsc1CoarseTrim(), compOscFineTrim));
594
595 DISABLE_PROTECTED_REGISTER_WRITE_MODE;
596
597 return;
598 } // end of HAL_osc1Comp() function
599
600
601 void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
602 {
603 int16_t compOscFineTrim;
604 HAL_Obj *obj = (HAL_Obj *)handle;
605
606
607 ENABLE_PROTECTED_REGISTER_WRITE_MODE;
608
609 compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc2FineTrimSlope()
610 + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc2FineTrimOffset() - OSC_POSTRIM;
611
612 if(compOscFineTrim > 31)
613 {
614 compOscFineTrim = 31;
615 }
616 else if(compOscFineTrim < -31)
617 {
618 compOscFineTrim = -31;
619 }
620
621 OSC_setTrim(obj->oscHandle, OSC_Number_2, HAL_computeOscTrimValue(getOsc2CoarseTrim(), compOscFineTrim));
622
623 DISABLE_PROTECTED_REGISTER_WRITE_MODE;
624
625 return;
626 } // end of HAL_osc2Comp() function
627
628
629 uint16_t HAL_computeOscTrimValue(int16_t coarse, int16_t fine)
630 {
631 uint16_t regValue = 0;
632
633 if(fine < 0)
634 {
635 regValue = ((-fine) | 0x20) << 9;
636 }
637 else
638 {
639 regValue = fine << 9;
640 }
641
642 if(coarse < 0)
643 {
644 regValue |= ((-coarse) | 0x80);
645 }
646 else
647 {
648 regValue |= coarse;
649 }
650
651 return(regValue);
652 } // end of HAL_computeOscTrimValue() function
653
654
655 void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams)
656 {
657 HAL_Obj *obj = (HAL_Obj *)handle;
658
659
660 HAL_setNumCurrentSensors(handle,pUserParams->numCurrentSensors);
661 HAL_setNumVoltageSensors(handle,pUserParams->numVoltageSensors);
662
663
664 // disable global interrupts
665 CPU_disableGlobalInts(obj->cpuHandle);
666
667
668 // disable cpu interrupts
669 CPU_disableInts(obj->cpuHandle);
670
671
672 // clear cpu interrupt flags
673 CPU_clearIntFlags(obj->cpuHandle);
674
675
676 // setup the clocks
677 HAL_setupClks(handle);
678
679
680 // Setup the PLL
681 HAL_setupPll(handle,PLL_ClkFreq_90_MHz);
682
683
684 // setup the PIE
685 HAL_setupPie(handle);
686
687
688 // run the device calibration
689 HAL_cal(handle);
690
691
692 // setup the peripheral clocks
693 HAL_setupPeripheralClks(handle);
694
695
696 // setup the GPIOs
697 HAL_setupGpios(handle);
698
699
700 // setup the flash
701 HAL_setupFlash(handle);
702
703
704 // setup the ADCs
705 HAL_setupAdcs(handle);
706
707
708 // setup the PWMs
709 HAL_setupPwms(handle,
710 pUserParams->systemFreq_MHz,
711 pUserParams->pwmPeriod_usec,
712 USER_NUM_PWM_TICKS_PER_ISR_TICK);
713
714
715 // setup the spiA
716 HAL_setupSpiA(handle);
717
718
719 // setup the spiB
720 HAL_setupSpiB(handle);
721
722
723 // setup the PWM DACs
724 HAL_setupPwmDacs(handle,
725 pUserParams->systemFreq_MHz,
726 (float_t)60.0);
727
728
729 #ifdef QEP
730 // setup the QEP
731 HAL_setupQEP(obj->qepHandle[0],pUserParams,1000);
732 #endif
733
734
735 // setup the timers
736 HAL_setupTimers(handle,
737 pUserParams->systemFreq_MHz);
738
739
740 // setup the drv8301 interface
741 HAL_setupGate(handle);
742
743
744 // set the current scale factor
745 {
746 float_t current_sf = pUserParams->current_sf;
747
748 HAL_setCurrentScaleFactor(handle,current_sf);
749 }
750
751
752 // set the voltage scale factor
753 {
754 float_t voltage_sf = pUserParams->voltage_sf;
755
756 HAL_setVoltageScaleFactor(handle,voltage_sf);
757 }
758
759
760 // set the default current bias
761 {
762 uint_least8_t cnt;
763 float_t current_sf = HAL_getCurrentScaleFactor(handle);
764 float_t bias = (float_t)ADC_dataBias * current_sf;
765
766 for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
767 {
768 HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
769 }
770 }
771
772
773 // set the default voltage bias
774 {
775 uint_least8_t cnt;
776 float_t bias = 0.0;
777
778 for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
779 {
780 HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
781 }
782 }
783
784
785 return;
786 } // end of HAL_setParams() function
787
788
789 void HAL_setupAdcs(HAL_Handle handle)
790 {
791 HAL_Obj *obj = (HAL_Obj *)handle;
792
793
794 // disable the ADCs
795 ADC_disable(obj->adcHandle);
796
797
798 // power up the bandgap circuit
799 ADC_enableBandGap(obj->adcHandle);
800
801
802 // set the ADC voltage reference source to internal
803 ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
804
805
806 // enable the ADC reference buffers
807 ADC_enableRefBuffers(obj->adcHandle);
808
809
810 // Set main clock scaling factor (max45MHz clock for the ADC module)
811 ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
812
813
814 // power up the ADCs
815 ADC_powerUp(obj->adcHandle);
816
817
818 // enable the ADCs
819 ADC_enable(obj->adcHandle);
820
821
822 // set the ADC interrupt pulse generation to prior
823 ADC_setIntPulseGenMode(obj->adcHandle,ADC_IntPulseGenMode_Prior);
824
825
826 // set the temperature sensor source to internal
827 ADC_setTempSensorSrc(obj->adcHandle,ADC_TempSensorSrc_Int);
828
829
830 // configure the interrupt sources
831 ADC_disableInt(obj->adcHandle,ADC_IntNumber_6);
832 ADC_setIntMode(obj->adcHandle,ADC_IntNumber_6,ADC_IntMode_ClearFlag);
833 ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_6,ADC_IntSrc_EOC7);
834
835
836 //configure the SOCs for drv8301kit_revD
837 // EXT IA-FB
838 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A6);
839 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM1_ADCSOCA);
840 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
841
842 // EXT IA-FB
843 // Duplicate conversion due to ADC Initial Conversion bug
844 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A6);
845 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM1_ADCSOCA);
846 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
847
848 // EXT IB-FB
849 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B6);
850 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM1_ADCSOCA);
851 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
852
853 // EXT IC-FB
854 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A0);
855 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM1_ADCSOCA);
856 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
857
858 // ADC-Vhb1
859 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B7);
860 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM1_ADCSOCA);
861 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
862
863 // ADC-Vhb2
864 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A7);
865 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM1_ADCSOCA);
866 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
867
868 // ADC-Vhb3
869 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B4);
870 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM1_ADCSOCA);
871 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
872
873 // VDCBUS
874 ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_B2);
875 ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM1_ADCSOCA);
876 ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
877
878
879 return;
880 } // end of HAL_setupAdcs() function
881
882
883 void HAL_setupClks(HAL_Handle handle)
884 {
885 HAL_Obj *obj = (HAL_Obj *)handle;
886
887
888 // enable internal oscillator 1
889 CLK_enableOsc1(obj->clkHandle);
890
891 // disable the external clock in
892 CLK_disableClkIn(obj->clkHandle);
893
894 // disable the crystal oscillator
895 CLK_disableCrystalOsc(obj->clkHandle);
896
897 // disable oscillator 2
898 CLK_disableOsc2(obj->clkHandle);
899
900 // set the oscillator source
901 CLK_setOscSrc(obj->clkHandle,CLK_OscSrc_Internal);
902
903 // set the low speed clock prescaler
904 CLK_setLowSpdPreScaler(obj->clkHandle,CLK_LowSpdPreScaler_SysClkOut_by_4);
905
906 // set the clock out prescaler
907 CLK_setClkOutPreScaler(obj->clkHandle,CLK_ClkOutPreScaler_SysClkOut_by_1);
908
909 return;
910 } // end of HAL_setupClks() function
911
912
913 void HAL_setupFlash(HAL_Handle handle)
914 {
915 HAL_Obj *obj = (HAL_Obj *)handle;
916
917
918 FLASH_enablePipelineMode(obj->flashHandle);
919
920 FLASH_setNumPagedReadWaitStates(obj->flashHandle,FLASH_NumPagedWaitStates_3);
921
922 FLASH_setNumRandomReadWaitStates(obj->flashHandle,FLASH_NumRandomWaitStates_3);
923
924 FLASH_setOtpWaitStates(obj->flashHandle,FLASH_NumOtpWaitStates_5);
925
926 FLASH_setStandbyWaitCount(obj->flashHandle,FLASH_STANDBY_WAIT_COUNT_DEFAULT);
927
928 FLASH_setActiveWaitCount(obj->flashHandle,FLASH_ACTIVE_WAIT_COUNT_DEFAULT);
929
930 return;
931 } // HAL_setupFlash() function
932
933
934 void HAL_setupFaults(HAL_Handle handle)
935 {
936 HAL_Obj *obj = (HAL_Obj *)handle;
937 uint_least8_t cnt;
938
939
940 // Configure Trip Mechanism for the Motor control software
941 // -Cycle by cycle trip on CPU halt
942 // -One shot fault trip zone
943 // These trips need to be repeated for EPWM1 ,2 & 3
944 for(cnt=0;cnt<3;cnt++)
945 {
946 PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ6_NOT);
947
948 PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_OneShot_TZ1_NOT);
949
950 // What do we want the OST/CBC events to do?
951 // TZA events can force EPWMxA
952 // TZB events can force EPWMxB
953
954 PWM_setTripZoneState_TZA(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
955 PWM_setTripZoneState_TZB(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
956
957 // Clear faults from flip flop
958 GPIO_setLow(obj->gpioHandle,GPIO_Number_9);
959 GPIO_setHigh(obj->gpioHandle,GPIO_Number_9);
960
961 // Clear any spurious fault
962 PWM_clearOneShotTrip(obj->pwmHandle[cnt]);
963 }
964
965 return;
966 } // end of HAL_setupFaults() function
967
968
969 void HAL_setupGate(HAL_Handle handle)
970 {
971 HAL_Obj *obj = (HAL_Obj *)handle;
972
973 DRV8301_setSpiHandle(obj->drv8301Handle,obj->spiBHandle);
974 DRV8301_setGpioHandle(obj->drv8301Handle,obj->gpioHandle);
975 DRV8301_setGpioNumber(obj->drv8301Handle,GPIO_Number_51);
976 } // HAL_setupGate() function
977
978
979 void HAL_setupGpios(HAL_Handle handle)
980 {
981 HAL_Obj *obj = (HAL_Obj *)handle;
982
983
984 // PWM1
985 GPIO_setMode(obj->gpioHandle,GPIO_Number_0,GPIO_0_Mode_EPWM1A);
986
987 // PWM2
988 GPIO_setMode(obj->gpioHandle,GPIO_Number_1,GPIO_1_Mode_EPWM1B);
989
990 // PWM3
991 GPIO_setMode(obj->gpioHandle,GPIO_Number_2,GPIO_2_Mode_EPWM2A);
992
993 // PWM4
994 GPIO_setMode(obj->gpioHandle,GPIO_Number_3,GPIO_3_Mode_EPWM2B);
995
996 // PWM5
997 GPIO_setMode(obj->gpioHandle,GPIO_Number_4,GPIO_4_Mode_EPWM3A);
998
999 // PWM6
1000 GPIO_setMode(obj->gpioHandle,GPIO_Number_5,GPIO_5_Mode_EPWM3B);
1001
1002 // PWM-DAC4
1003 GPIO_setMode(obj->gpioHandle,GPIO_Number_6,GPIO_6_Mode_EPWM4A);
1004
1005 // Input SW1
1006 GPIO_setMode(obj->gpioHandle,GPIO_Number_7,GPIO_7_Mode_GeneralPurpose);
1007 GPIO_setDirection(obj->gpioHandle,GPIO_Number_7,GPIO_Direction_Input);
1008
1009 // ADCSOCAO_NOT
1010 GPIO_setMode(obj->gpioHandle,GPIO_Number_8,GPIO_8_Mode_ADCSOCAO_NOT);
1011
1012 // Input SW2
1013 GPIO_setMode(obj->gpioHandle,GPIO_Number_9,GPIO_9_Mode_GeneralPurpose);
1014 GPIO_setDirection(obj->gpioHandle,GPIO_Number_9,GPIO_Direction_Input);
1015
1016 // PWM-DAC1
1017 GPIO_setMode(obj->gpioHandle,GPIO_Number_10,GPIO_10_Mode_EPWM6A);
1018
1019 // PWM-DAC2
1020 GPIO_setMode(obj->gpioHandle,GPIO_Number_11,GPIO_11_Mode_EPWM6B);
1021
1022 // DRV8301-LED1
1023 GPIO_setMode(obj->gpioHandle,GPIO_Number_12,GPIO_12_Mode_GeneralPurpose);
1024
1025 // OCTWn
1026 GPIO_setMode(obj->gpioHandle,GPIO_Number_13,GPIO_13_Mode_TZ2_NOT);
1027
1028 // FAULTn
1029 GPIO_setMode(obj->gpioHandle,GPIO_Number_14,GPIO_14_Mode_TZ3_NOT);
1030
1031 // LED2
1032 GPIO_setMode(obj->gpioHandle,GPIO_Number_15,GPIO_15_Mode_GeneralPurpose);
1033
1034 // SPI-SIMO
1035 GPIO_setMode(obj->gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);
1036 GPIO_setPullup(obj->gpioHandle,GPIO_Number_16, GPIO_Pullup_Enable);
1037
1038 // SPI-SOMI
1039 GPIO_setMode(obj->gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);
1040 GPIO_setPullup(obj->gpioHandle,GPIO_Number_17, GPIO_Pullup_Enable);
1041
1042 // SPI-CLK
1043 GPIO_setMode(obj->gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);
1044 GPIO_setPullup(obj->gpioHandle,GPIO_Number_18, GPIO_Pullup_Enable);
1045
1046 // SPI-STE
1047 GPIO_setMode(obj->gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);
1048 GPIO_setPullup(obj->gpioHandle,GPIO_Number_19, GPIO_Pullup_Enable);
1049
1050 #ifdef QEP
1051 // EQEPA
1052 GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_EQEP1A);
1053
1054 // EQEPB
1055 GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_EQEP1B);
1056
1057 // STATUS
1058 GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
1059
1060 // EQEP1I
1061 GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_EQEP1I);
1062 #else
1063 // EQEPA
1064 GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_GeneralPurpose);
1065
1066 // EQEPB
1067 GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_GeneralPurpose);
1068
1069 // STATUS
1070 GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
1071
1072 // EQEP1I
1073 GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_GeneralPurpose);
1074 #endif
1075
1076 // SPI SIMO B
1077 GPIO_setMode(obj->gpioHandle,GPIO_Number_24,GPIO_24_Mode_SPISIMOB);
1078
1079 // SPI SOMI B
1080 GPIO_setMode(obj->gpioHandle,GPIO_Number_25,GPIO_25_Mode_SPISOMIB);
1081
1082 // SPI CLK B
1083 GPIO_setMode(obj->gpioHandle,GPIO_Number_26,GPIO_26_Mode_SPICLKB);
1084
1085 // SPI CSn B
1086 GPIO_setMode(obj->gpioHandle,GPIO_Number_27,GPIO_27_Mode_SPISTEB_NOT);
1087
1088 // No Connection
1089 GPIO_setMode(obj->gpioHandle,GPIO_Number_28,GPIO_28_Mode_GeneralPurpose);
1090
1091 // No Connection
1092 GPIO_setMode(obj->gpioHandle,GPIO_Number_29,GPIO_29_Mode_GeneralPurpose);
1093
1094 // DataAcq start test from FTDI cable
1095 GPIO_setMode(obj->gpioHandle,GPIO_Number_30,GPIO_30_Mode_GeneralPurpose);
1096
1097 // ControlCARD LED2
1098 GPIO_setMode(obj->gpioHandle,GPIO_Number_31,GPIO_31_Mode_GeneralPurpose);
1099 GPIO_setLow(obj->gpioHandle,GPIO_Number_31);
1100 GPIO_setDirection(obj->gpioHandle,GPIO_Number_31,GPIO_Direction_Output);
1101
1102 // No Connection
1103 GPIO_setMode(obj->gpioHandle,GPIO_Number_32,GPIO_32_Mode_GeneralPurpose);
1104
1105 // No Connection
1106 GPIO_setMode(obj->gpioHandle,GPIO_Number_33,GPIO_33_Mode_GeneralPurpose);
1107
1108 // ControlCARD LED3
1109 GPIO_setMode(obj->gpioHandle,GPIO_Number_34,GPIO_34_Mode_GeneralPurpose);
1110 GPIO_setLow(obj->gpioHandle,GPIO_Number_34);
1111 GPIO_setDirection(obj->gpioHandle,GPIO_Number_34,GPIO_Direction_Output);
1112
1113 // JTAG
1114 GPIO_setMode(obj->gpioHandle,GPIO_Number_35,GPIO_35_Mode_JTAG_TDI);
1115 GPIO_setMode(obj->gpioHandle,GPIO_Number_36,GPIO_36_Mode_JTAG_TMS);
1116 GPIO_setMode(obj->gpioHandle,GPIO_Number_37,GPIO_37_Mode_JTAG_TDO);
1117 GPIO_setMode(obj->gpioHandle,GPIO_Number_38,GPIO_38_Mode_JTAG_TCK);
1118
1119 // DRV8301 Enable
1120 GPIO_setMode(obj->gpioHandle,GPIO_Number_39,GPIO_39_Mode_GeneralPurpose);
1121
1122 // CAP1
1123 GPIO_setMode(obj->gpioHandle,GPIO_Number_40,GPIO_40_Mode_GeneralPurpose);
1124
1125 // CAP2
1126 GPIO_setMode(obj->gpioHandle,GPIO_Number_41,GPIO_41_Mode_GeneralPurpose);
1127
1128 // CAP3
1129 GPIO_setMode(obj->gpioHandle,GPIO_Number_42,GPIO_42_Mode_GeneralPurpose);
1130
1131 // DC_CAL
1132 GPIO_setMode(obj->gpioHandle,GPIO_Number_43,GPIO_43_Mode_GeneralPurpose);
1133
1134 // No Connection
1135 GPIO_setMode(obj->gpioHandle,GPIO_Number_44,GPIO_44_Mode_GeneralPurpose);
1136
1137 // No Connection
1138 GPIO_setMode(obj->gpioHandle,GPIO_Number_50,GPIO_50_Mode_GeneralPurpose);
1139
1140 // DRV8301 Enable
1141 GPIO_setMode(obj->gpioHandle,GPIO_Number_51,GPIO_51_Mode_GeneralPurpose);
1142 GPIO_setLow(obj->gpioHandle,GPIO_Number_51);
1143 GPIO_setDirection(obj->gpioHandle,GPIO_Number_51,GPIO_Direction_Output);
1144
1145 // No Connection
1146 GPIO_setMode(obj->gpioHandle,GPIO_Number_52,GPIO_52_Mode_GeneralPurpose);
1147
1148 // No Connection
1149 GPIO_setMode(obj->gpioHandle,GPIO_Number_53,GPIO_53_Mode_GeneralPurpose);
1150
1151 // No Connection
1152 GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_GeneralPurpose);
1153
1154 // No Connection
1155 GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_GeneralPurpose);
1156
1157 // No Connection
1158 GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_GeneralPurpose);
1159
1160 // No Connection
1161 GPIO_setMode(obj->gpioHandle,GPIO_Number_57,GPIO_57_Mode_GeneralPurpose);
1162
1163 // No Connection
1164 GPIO_setMode(obj->gpioHandle,GPIO_Number_58,GPIO_58_Mode_GeneralPurpose);
1165
1166 return;
1167 } // end of HAL_setupGpios() function
1168
1169
1170 void HAL_setupPie(HAL_Handle handle)
1171 {
1172 HAL_Obj *obj = (HAL_Obj *)handle;
1173
1174
1175 PIE_disable(obj->pieHandle);
1176
1177 PIE_disableAllInts(obj->pieHandle);
1178
1179 PIE_clearAllInts(obj->pieHandle);
1180
1181 PIE_clearAllFlags(obj->pieHandle);
1182
1183 PIE_setDefaultIntVectorTable(obj->pieHandle);
1184
1185 PIE_enable(obj->pieHandle);
1186
1187 return;
1188 } // end of HAL_setupPie() function
1189
1190
1191 void HAL_setupPeripheralClks(HAL_Handle handle)
1192 {
1193 HAL_Obj *obj = (HAL_Obj *)handle;
1194
1195
1196 CLK_enableAdcClock(obj->clkHandle);
1197
1198 CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_1);
1199 CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_2);
1200 CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_3);
1201
1202 CLK_enableEcap1Clock(obj->clkHandle);
1203
1204 CLK_disableEcanaClock(obj->clkHandle);
1205
1206 #ifdef QEP
1207 CLK_enableEqep1Clock(obj->clkHandle);
1208 #endif
1209
1210 CLK_enablePwmClock(obj->clkHandle,PWM_Number_1);
1211 CLK_enablePwmClock(obj->clkHandle,PWM_Number_2);
1212 CLK_enablePwmClock(obj->clkHandle,PWM_Number_3);
1213 CLK_enablePwmClock(obj->clkHandle,PWM_Number_4);
1214 CLK_enablePwmClock(obj->clkHandle,PWM_Number_5);
1215 CLK_enablePwmClock(obj->clkHandle,PWM_Number_6);
1216 CLK_enablePwmClock(obj->clkHandle,PWM_Number_7);
1217
1218 CLK_disableHrPwmClock(obj->clkHandle);
1219
1220 CLK_disableI2cClock(obj->clkHandle);
1221
1222 CLK_disableLinAClock(obj->clkHandle);
1223
1224 CLK_disableClaClock(obj->clkHandle);
1225
1226 CLK_enableSciaClock(obj->clkHandle);
1227
1228 CLK_enableSpiaClock(obj->clkHandle);
1229
1230 CLK_enableSpibClock(obj->clkHandle);
1231
1232 CLK_enableTbClockSync(obj->clkHandle);
1233
1234 return;
1235 } // end of HAL_setupPeripheralClks() function
1236
1237
1238 void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq)
1239 {
1240 HAL_Obj *obj = (HAL_Obj *)handle;
1241
1242
1243 // make sure PLL is not running in limp mode
1244 if(PLL_getClkStatus(obj->pllHandle) != PLL_ClkStatus_Normal)
1245 {
1246 // reset the clock detect
1247 PLL_resetClkDetect(obj->pllHandle);
1248
1249 // ???????
1250 asm(" ESTOP0");
1251 }
1252
1253
1254 // Divide Select must be ClkIn/4 before the clock rate can be changed
1255 if(PLL_getDivideSelect(obj->pllHandle) != PLL_DivideSelect_ClkIn_by_4)
1256 {
1257 PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_4);
1258 }
1259
1260
1261 if(PLL_getClkFreq(obj->pllHandle) != clkFreq)
1262 {
1263 // disable the clock detect
1264 PLL_disableClkDetect(obj->pllHandle);
1265
1266 // set the clock rate
1267 PLL_setClkFreq(obj->pllHandle,clkFreq);
1268 }
1269
1270
1271 // wait until locked
1272 while(PLL_getLockStatus(obj->pllHandle) != PLL_LockStatus_Done) {}
1273
1274
1275 // enable the clock detect
1276 PLL_enableClkDetect(obj->pllHandle);
1277
1278
1279 // set divide select to ClkIn/2 to get desired clock rate
1280 // NOTE: clock must be locked before setting this register
1281 PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_2);
1282
1283 return;
1284 } // end of HAL_setupPll() function
1285
1286
1287 void HAL_setupPwms(HAL_Handle handle,
1288 const float_t systemFreq_MHz,
1289 const float_t pwmPeriod_usec,
1290 const uint_least16_t numPwmTicksPerIsrTick)
1291 {
1292 HAL_Obj *obj = (HAL_Obj *)handle;
1293 uint16_t halfPeriod_cycles = (uint16_t)(systemFreq_MHz * pwmPeriod_usec / (float_t)2.0);
1294 uint_least8_t cnt;
1295
1296
1297 for(cnt=0;cnt<3;cnt++)
1298 {
1299 // setup the Time-Base Control Register (TBCTL)
1300 PWM_setCounterMode(obj->pwmHandle[cnt],PWM_CounterMode_UpDown);
1301 PWM_disableCounterLoad(obj->pwmHandle[cnt]);
1302 PWM_setPeriodLoad(obj->pwmHandle[cnt],PWM_PeriodLoad_Immediate);
1303 PWM_setSyncMode(obj->pwmHandle[cnt],PWM_SyncMode_EPWMxSYNC);
1304 PWM_setHighSpeedClkDiv(obj->pwmHandle[cnt],PWM_HspClkDiv_by_1);
1305 PWM_setClkDiv(obj->pwmHandle[cnt],PWM_ClkDiv_by_1);
1306 PWM_setPhaseDir(obj->pwmHandle[cnt],PWM_PhaseDir_CountUp);
1307 PWM_setRunMode(obj->pwmHandle[cnt],PWM_RunMode_FreeRun);
1308
1309 // setup the Timer-Based Phase Register (TBPHS)
1310 PWM_setPhase(obj->pwmHandle[cnt],0);
1311
1312 // setup the Time-Base Counter Register (TBCTR)
1313 PWM_setCount(obj->pwmHandle[cnt],0);
1314
1315 // setup the Time-Base Period Register (TBPRD)
1316 // set to zero initially
1317 PWM_setPeriod(obj->pwmHandle[cnt],0);
1318
1319 // setup the Counter-Compare Control Register (CMPCTL)
1320 PWM_setLoadMode_CmpA(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
1321 PWM_setLoadMode_CmpB(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
1322 PWM_setShadowMode_CmpA(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
1323 PWM_setShadowMode_CmpB(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
1324
1325 // setup the Action-Qualifier Output A Register (AQCTLA)
1326 PWM_setActionQual_CntUp_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Set);
1327 PWM_setActionQual_CntDown_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Clear);
1328
1329 // setup the Dead-Band Generator Control Register (DBCTL)
1330 PWM_setDeadBandOutputMode(obj->pwmHandle[cnt],PWM_DeadBandOutputMode_EPWMxA_Rising_EPWMxB_Falling);
1331 PWM_setDeadBandPolarity(obj->pwmHandle[cnt],PWM_DeadBandPolarity_EPWMxB_Inverted);
1332
1333 // setup the Dead-Band Rising Edge Delay Register (DBRED)
1334 PWM_setDeadBandRisingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBRED_CNT);
1335
1336 // setup the Dead-Band Falling Edge Delay Register (DBFED)
1337 PWM_setDeadBandFallingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBFED_CNT);
1338
1339 // setup the PWM-Chopper Control Register (PCCTL)
1340 PWM_disableChopping(obj->pwmHandle[cnt]);
1341
1342 // setup the Trip Zone Select Register (TZSEL)
1343 PWM_disableTripZones(obj->pwmHandle[cnt]);
1344 }
1345
1346
1347 // setup the Event Trigger Selection Register (ETSEL)
1348 PWM_disableInt(obj->pwmHandle[PWM_Number_1]);
1349 PWM_setIntMode(obj->pwmHandle[PWM_Number_1],PWM_IntMode_CounterEqualZero);
1350 PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualZero);
1351 PWM_enableSocAPulse(obj->pwmHandle[PWM_Number_1]);
1352
1353
1354 // setup the Event Trigger Prescale Register (ETPS)
1355 if(numPwmTicksPerIsrTick == 3)
1356 {
1357 PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_ThirdEvent);
1358 PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_ThirdEvent);
1359 }
1360 else if(numPwmTicksPerIsrTick == 2)
1361 {
1362 PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_SecondEvent);
1363 PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_SecondEvent);
1364 }
1365 else
1366 {
1367 PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_FirstEvent);
1368 PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_FirstEvent);
1369 }
1370
1371
1372 // setup the Event Trigger Clear Register (ETCLR)
1373 PWM_clearIntFlag(obj->pwmHandle[PWM_Number_1]);
1374 PWM_clearSocAFlag(obj->pwmHandle[PWM_Number_1]);
1375
1376
1377 // disable the ePWM module time base clock sync signal
1378 // to synchronize all of the PWMs
1379 CLK_disableTbClockSync(obj->clkHandle);
1380
1381
1382 // since the PWM is configured as an up/down counter, the period register is set to one-half
1383 // of the desired PWM period
1384 PWM_setPeriod(obj->pwmHandle[PWM_Number_1],halfPeriod_cycles);
1385 PWM_setPeriod(obj->pwmHandle[PWM_Number_2],halfPeriod_cycles);
1386 PWM_setPeriod(obj->pwmHandle[PWM_Number_3],halfPeriod_cycles);
1387
1388
1389 // enable the ePWM module time base clock sync signal
1390 CLK_enableTbClockSync(obj->clkHandle);
1391
1392 return;
1393 } // end of HAL_setupPwms() function
1394
1395 void HAL_setupSpiA(HAL_Handle handle)
1396 {
1397 HAL_Obj *obj = (HAL_Obj *)handle;
1398
1399 SPI_reset(obj->spiAHandle);
1400 SPI_setClkPolarity(obj->spiAHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
1401 SPI_disableLoopBack(obj->spiAHandle);
1402 SPI_setCharLength(obj->spiAHandle,SPI_CharLength_16_Bits);
1403
1404 SPI_setMode(obj->spiAHandle,SPI_Mode_Slave);
1405
1406 SPI_setClkPhase(obj->spiAHandle,SPI_ClkPhase_Normal);
1407 SPI_enableTx(obj->spiAHandle);
1408
1409 SPI_enableChannels(obj->spiAHandle);
1410 SPI_enableTxFifoEnh(obj->spiAHandle);
1411 SPI_enableTxFifo(obj->spiAHandle);
1412 SPI_setTxDelay(obj->spiAHandle,0);
1413 SPI_clearTxFifoInt(obj->spiAHandle);
1414 SPI_enableRxFifo(obj->spiAHandle);
1415
1416 //not needed for slave mode SPI_setBaudRate(obj->spiAHandle,(SPI_BaudRate_e)(0x0003));
1417 SPI_setSuspend(obj->spiAHandle,SPI_TxSuspend_free);
1418 SPI_enable(obj->spiAHandle);
1419
1420 return;
1421 } // end of HAL_setupSpiA() function
1422
1423 void HAL_setupSpiB(HAL_Handle handle)
1424 {
1425 HAL_Obj *obj = (HAL_Obj *)handle;
1426
1427 SPI_reset(obj->spiBHandle);
1428 SPI_setMode(obj->spiBHandle,SPI_Mode_Master);
1429 SPI_setClkPolarity(obj->spiBHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
1430 SPI_enableTx(obj->spiBHandle);
1431 SPI_enableTxFifoEnh(obj->spiBHandle);
1432 SPI_enableTxFifo(obj->spiBHandle);
1433 SPI_setTxDelay(obj->spiBHandle,0x0018);
1434 SPI_setBaudRate(obj->spiBHandle,(SPI_BaudRate_e)(0x0003));
1435 SPI_setCharLength(obj->spiBHandle,SPI_CharLength_16_Bits);
1436 SPI_setSuspend(obj->spiBHandle,SPI_TxSuspend_free);
1437 SPI_enable(obj->spiBHandle);
1438
1439 return;
1440 } // end of HAL_setupSpiB() function
1441
1442
1443 void HAL_setupPwmDacs(HAL_Handle handle,const float_t systemFreq_MHz,const float_t dacFreq_kHz)
1444 {
1445 HAL_Obj *obj = (HAL_Obj *)handle;
1446 uint16_t halfPeriod_cycles = (uint16_t)(systemFreq_MHz * (float_t)1000.0 / ((float_t)2.0 * dacFreq_kHz));
1447 uint_least8_t cnt;
1448
1449
1450 for(cnt=0;cnt<3;cnt++)
1451 {
1452 // initialize the Time-Base Control Register (TBCTL)
1453 PWMDAC_setCounterMode(obj->pwmDacHandle[cnt],PWM_CounterMode_UpDown);
1454 PWMDAC_disableCounterLoad(obj->pwmDacHandle[cnt]);
1455 PWMDAC_setPeriodLoad(obj->pwmDacHandle[cnt],PWM_PeriodLoad_Immediate);
1456 PWMDAC_setSyncMode(obj->pwmDacHandle[cnt],PWM_SyncMode_EPWMxSYNC);
1457 PWMDAC_setHighSpeedClkDiv(obj->pwmDacHandle[cnt],PWM_HspClkDiv_by_1);
1458 PWMDAC_setClkDiv(obj->pwmDacHandle[cnt],PWM_ClkDiv_by_1);
1459 PWMDAC_setPhaseDir(obj->pwmDacHandle[cnt],PWM_PhaseDir_CountUp);
1460 PWMDAC_setRunMode(obj->pwmDacHandle[cnt],PWM_RunMode_FreeRun);
1461
1462 // initialize the Timer-Based Phase Register (TBPHS)
1463 PWMDAC_setPhase(obj->pwmDacHandle[cnt],0);
1464
1465 // setup the Time-Base Counter Register (TBCTR)
1466 PWMDAC_setCount(obj->pwmDacHandle[cnt],0);
1467
1468 // Initialize the Time-Base Period Register (TBPRD)
1469 // set to zero initially
1470 PWMDAC_setPeriod(obj->pwmDacHandle[cnt],0);
1471
1472 // initialize the Counter-Compare Control Register (CMPCTL)
1473 PWMDAC_setLoadMode_CmpA(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
1474 PWMDAC_setLoadMode_CmpB(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
1475 PWMDAC_setShadowMode_CmpA(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
1476 PWMDAC_setShadowMode_CmpB(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
1477
1478 // Initialize the Action-Qualifier Output A Register (AQCTLA)
1479 PWMDAC_setActionQual_CntUp_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
1480 PWMDAC_setActionQual_CntDown_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
1481
1482 // account for EPWM6B
1483 if(cnt == 0)
1484 {
1485 PWMDAC_setActionQual_CntUp_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
1486 PWMDAC_setActionQual_CntDown_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
1487 }
1488
1489 // Initialize the Dead-Band Control Register (DBCTL)
1490 PWMDAC_disableDeadBand(obj->pwmDacHandle[cnt]);
1491
1492 // Initialize the PWM-Chopper Control Register (PCCTL)
1493 PWMDAC_disableChopping(obj->pwmDacHandle[cnt]);
1494
1495 // Initialize the Trip-Zone Control Register (TZSEL)
1496 PWMDAC_disableTripZones(obj->pwmDacHandle[cnt]);
1497
1498 // Initialize the Trip-Zone Control Register (TZCTL)
1499 PWMDAC_setTripZoneState_TZA(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1500 PWMDAC_setTripZoneState_TZB(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1501 PWMDAC_setTripZoneState_DCAEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1502 PWMDAC_setTripZoneState_DCAEVT2(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1503 PWMDAC_setTripZoneState_DCBEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
1504 }
1505
1506
1507 // disable the ePWM module time base clock sync signal
1508 // to synchronize all of the PWMs
1509 CLK_disableTbClockSync(obj->clkHandle);
1510
1511
1512 // since the PWM is configured as an up/down counter, the period register is set to one-half
1513 // of the desired PWM period
1514 PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_1],halfPeriod_cycles);
1515 PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_2],halfPeriod_cycles);
1516 PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_3],halfPeriod_cycles);
1517
1518
1519 // enable the ePWM module time base clock sync signal
1520 CLK_enableTbClockSync(obj->clkHandle);
1521
1522 return;
1523 } // end of HAL_setupPwmDacs() function
1524
1525
1526 // set up the QEP peripheral
1527 void HAL_setupQEP(QEP_Handle qepHandle,const USER_Params *pUserParams,const uint32_t unitTimerFreq_Hz)
1528 {
1529
1530 uint16_t numEncSlots = pUserParams->motor_numEncSlots;
1531 uint32_t systemFreq_Hz = (uint32_t)(pUserParams->systemFreq_MHz) * 1e6;
1532 uint32_t unitTimerPrd = systemFreq_Hz / unitTimerFreq_Hz;
1533
1534 // hold the counter in reset
1535 QEP_reset_counter(qepHandle);
1536
1537 // set the QPOSINIT register
1538 QEP_set_posn_init_count(qepHandle, 0);
1539
1540 // disable all interrupts
1541 QEP_disable_all_interrupts(qepHandle);
1542
1543 // clear the interrupt flags
1544 QEP_clear_all_interrupt_flags(qepHandle);
1545
1546 // clear the position counter
1547 QEP_clear_posn_counter(qepHandle);
1548
1549 // setup the max position
1550 QEP_set_max_posn_count(qepHandle, (4*numEncSlots)-1);
1551
1552 // setup the QDECCTL register
1553 QEP_set_QEP_source(qepHandle, QEP_Qsrc_Quad_Count_Mode);
1554 QEP_disable_sync_out(qepHandle);
1555 QEP_set_swap_quad_inputs(qepHandle, QEP_Swap_Not_Swapped);
1556 QEP_disable_gate_index(qepHandle);
1557 QEP_set_ext_clock_rate(qepHandle, QEP_Xcr_2x_Res);
1558 QEP_set_A_polarity(qepHandle, QEP_Qap_No_Effect);
1559 QEP_set_B_polarity(qepHandle, QEP_Qbp_No_Effect);
1560 QEP_set_index_polarity(qepHandle, QEP_Qip_No_Effect);
1561
1562 // setup the QEPCTL register
1563 QEP_set_emu_control(qepHandle, QEPCTL_Freesoft_Unaffected_Halt);
1564 QEP_set_posn_count_reset_mode(qepHandle, QEPCTL_Pcrm_Max_Reset);
1565 QEP_set_strobe_event_init(qepHandle, QEPCTL_Sei_Nothing);
1566 QEP_set_index_event_init(qepHandle, QEPCTL_Iei_Nothing);
1567 QEP_set_index_event_latch(qepHandle, QEPCTL_Iel_Rising_Edge);
1568 QEP_set_soft_init(qepHandle, QEPCTL_Swi_Nothing);
1569 QEP_disable_unit_timer(qepHandle);
1570 QEP_disable_watchdog(qepHandle);
1571
1572 // setup the QPOSCTL register
1573 QEP_disable_posn_compare(qepHandle);
1574
1575 // setup the QCAPCTL register for low speed measurement
1576 QEP_enable_capture(qepHandle);
1577 QEP_set_capture_prescale(qepHandle,QCAPCTL_Ccps_Capture_Div_32);
1578 QEP_set_unit_posn_prescale(qepHandle,QCAPCTL_Upps_Div_1_Prescale);
1579
1580 // setup the unit timer for high speed measurement
1581 QEP_enable_unit_timer(qepHandle);
1582 QEP_set_unit_period(qepHandle,unitTimerPrd);
1583 QEP_set_capture_latch_mode(qepHandle,QEPCTL_Qclm_Latch_on_Unit_Timeout);
1584
1585 // renable the position counter
1586 QEP_enable_counter(qepHandle);
1587
1588 return;
1589 } // end of HAL_setupQEP() function
1590
1591
1592 void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz)
1593 {
1594 HAL_Obj *obj = (HAL_Obj *)handle;
1595 uint32_t timerPeriod_cnts = (uint32_t)(systemFreq_MHz * (float_t)1000000.0) - 1;
1596
1597
1598 // use timer 0 for frequency diagnostics
1599 TIMER_setDecimationFactor(obj->timerHandle[0],0);
1600 TIMER_setEmulationMode(obj->timerHandle[0],TIMER_EmulationMode_RunFree);
1601 TIMER_setPeriod(obj->timerHandle[0],timerPeriod_cnts);
1602 TIMER_setPreScaler(obj->timerHandle[0],0);
1603
1604 // use timer 1 for CPU usage diagnostics
1605 TIMER_setDecimationFactor(obj->timerHandle[1],0);
1606 TIMER_setEmulationMode(obj->timerHandle[1],TIMER_EmulationMode_RunFree);
1607 TIMER_setPeriod(obj->timerHandle[1],timerPeriod_cnts);
1608 TIMER_setPreScaler(obj->timerHandle[1],0);
1609
1610 // use timer 2 for DataAcq timer intervals
1611 TIMER_setEmulationMode(obj->timerHandle[2],TIMER_EmulationMode_StopAfterNextDecrement);
1612 TIMER_setPreScaler(obj->timerHandle[2],0);
1613 TIMER_setDecimationFactor(obj->timerHandle[2],(uint16_t)(((uint32_t)(systemFreq_MHz * (float_t)500.0)) & 0xFFFF)); //0.5ms tick
1614 TIMER_setPeriod(obj->timerHandle[2],0xFFFFFFFF);
1615
1616 return;
1617 } // end of HAL_setupTimers() function
1618
1619
1620 void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
1621 {
1622 HAL_Obj *obj = (HAL_Obj *)handle;
1623
1624 DRV8301_writeData(obj->drv8301Handle,Spi_8301_Vars);
1625
1626 return;
1627 } // end of HAL_writeDrvData() function
1628
1629
1630 void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
1631 {
1632 HAL_Obj *obj = (HAL_Obj *)handle;
1633
1634 DRV8301_readData(obj->drv8301Handle,Spi_8301_Vars);
1635
1636 return;
1637 } // end of HAL_readDrvData() function
1638
1639
1640 void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
1641 {
1642 HAL_Obj *obj = (HAL_Obj *)handle;
1643
1644 DRV8301_setupSpi(obj->drv8301Handle,Spi_8301_Vars);
1645
1646 return;
1647 } // end of HAL_setupDrvSpi() function
1648
1649
1650 // 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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:374
HAL_enableAdcInts
void HAL_enableAdcInts(HAL_Handle handle)
Enables the ADC interrupts.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:333
HAL_disableGlobalInts
void HAL_disableGlobalInts(HAL_Handle handle)
Disables global interrupts.
Definition: drv8301kit_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_PWM_DBFED_CNT
#define HAL_PWM_DBFED_CNT
Defines the PWM deadband falling edge delay count (system clocks)
Definition: boostxldrv8301_revB/f28x/f2806x/src/hal.h:113
hal.h
_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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:506
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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1527
_HAL_Obj_::clkHandle
CLK_Handle clkHandle
the clock handle
Definition: boostxldrv8301_revB/f28x/f2806x/src/hal_obj.h:132
HAL_writeDrvData
void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
Writes data to the driver.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1620
_HAL_Obj_::drv8301
DRV8301_Obj drv8301
the drv8301 interface object
Definition: boostxldrv8301_revB/f28x/f2806x/src/hal_obj.h:175
_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: drv8301kit_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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1191
HAL_setupTimers
void HAL_setupTimers(HAL_Handle handle, const float_t systemFreq_MHz)
Sets up the timers.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1592
HAL_osc2Comp
void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 2 calibration based on input sample.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:601
HAL_setupPll
void HAL_setupPll(HAL_Handle handle, const PLL_ClkFreq_e clkFreq)
Sets up the PLL (Phase Lock Loop)
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1238
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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:979
HAL_setupFlash
void HAL_setupFlash(HAL_Handle handle)
Sets up the FLASH.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:913
HAL_enablePwmInt
void HAL_enablePwmInt(HAL_Handle handle)
Enables the PWM interrupt.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:385
HAL_setupClks
void HAL_setupClks(HAL_Handle handle)
Sets up the clocks.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:883
HAL_PWM_DBRED_CNT
#define HAL_PWM_DBRED_CNT
Defines the PWM deadband rising edge delay count (system clocks)
Definition: boostxldrv8301_revB/f28x/f2806x/src/hal.h:118
HAL_setupFaults
void HAL_setupFaults(HAL_Handle handle)
Configures the fault protection logic.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:934
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_enableDrv
void HAL_enableDrv(HAL_Handle handle)
Enables the 8301 device.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:364
HAL_osc1Comp
void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
Executes the oscillator 1 calibration based on input sample.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:573
_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.
HAL_setupDrvSpi
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
Sets up the SPI interface for the driver.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1640
_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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1287
HAL_setupPie
void HAL_setupPie(HAL_Handle handle)
Sets up the PIE (Peripheral Interrupt Expansion)
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1170
HAL_enableDebugInt
void HAL_enableDebugInt(HAL_Handle handle)
Enables the debug interrupt.
Definition: drv8301kit_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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:629
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_obj.h
HAL_setupSpiB
void HAL_setupSpiB(HAL_Handle handle)
Sets up the spiB peripheral.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1423
_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: drv8301kit_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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:404
_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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1443
HAL_cal
void HAL_cal(HAL_Handle handle)
Executes calibration routines.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:274
HAL_setupGate
void HAL_setupGate(HAL_Handle handle)
Sets up the GATE object.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:969
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: drv8301kit_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_::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: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1395
HAL_setParams
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
Sets the hardware abstraction layer parameters.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:655
HAL_readDrvData
void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
Reads data from the driver.
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:1630
_HAL_Obj_::drv8301Handle
DRV8301_Handle drv8301Handle
the drv8301 interface handle
Definition: boostxldrv8301_revB/f28x/f2806x/src/hal_obj.h:174
HAL_setupAdcs
void HAL_setupAdcs(HAL_Handle handle)
Sets up the ADCs (Analog to Digital Converters)
Definition: drv8301kit_revD/f28x/f2806x/src/float/hal.c:789
HAL_disableWdog
void HAL_disableWdog(HAL_Handle halHandle)
Disables the watch dog.
Definition: drv8301kit_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