This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Setting up 28069M as spi slave

Other Parts Discussed in Thread: MOTORWARE, CONTROLSUITE, DRV8301

I'm trying to use my Launch XL 28069M as a spi slave to be controlled by an arduino. I'm a beginner in this so I've done my best to read through the user guide here and step through the motorware drivers to infer how it should be implemented, and have come up with almost nothing.

The goal eventually is to use the arduino to command motor data to the c2000, but for now I just want to get the two devices talking. For the arduino I'm sending at 1Mhz, outputting on the falling edge, with normal phase and MSB first. I know the SPI from the arduino is working because I've verified it with another device. 

Here is the code I've created, I'm using the drivers from motorware, and am bypassing the HAL drivers because it added too much complexity, in my opinion, for what I want t do. This code compiles and runs without any infinite loops, but when watching the SPI registers in CCS debugger mode, nothing changes. 

I've also tried sending data using the other function, and the same story happens, nothing changes in the registers and nothing happens when I scope the pins. 

And just for good measure, I have meticulously verified that I'm plugged into the right places. 

#include "sw\drivers\spi\src\32b\f28x\f2806x\spi.h"
#include "sw/drivers/gpio/src/32b/f28x/f2806x/gpio.h"

//globals
SPI_Handle spiHandle;
GPIO_Handle gpioHandle;
//sets up the SPIA bus
void setupSpiA()
{
	spiHandle = SPI_init((void *)SPIA_BASE_ADDR,sizeof(SPI_Obj));
	uint16_t var = 12;
	var++;
	var++;
	SPI_reset(spiHandle);
	SPI_setMode(spiHandle,SPI_Mode_Slave);
	SPI_setClkPolarity(spiHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
	SPI_enableTx(spiHandle);
	SPI_enableTxFifoEnh(spiHandle);
	SPI_enableTxFifo(spiHandle);
	SPI_setTxDelay(spiHandle,0x0018);
	SPI_setBaudRate(spiHandle,(SPI_BaudRate_e)(0x000d));
	SPI_setCharLength(spiHandle,SPI_CharLength_16_Bits);
	SPI_setSuspend(spiHandle,SPI_TxSuspend_free);
	SPI_enable(spiHandle);

  return;
}

void setupGPIOforSpiA(){
	// SPIA SIMO
	gpioHandle = GPIO_init((void *)GPIO_BASE_ADDR,sizeof(GPIO_Obj));


	GPIO_setMode(gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);

	// SPIA SOMI
	GPIO_setMode(gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);

	// SPIA CLK
	GPIO_setMode(gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);

	// SPIA CS
	GPIO_setMode(gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);
}

//function for writing to spi
void sendOverSpi(uint16_t input){
	uint16_t send;
	send = input;

	// reset the Rx fifo pointer to zero
	SPI_resetRxFifo(spiHandle);
	SPI_enableRxFifo(spiHandle);

	// write the command (time N)
	SPI_write(spiHandle,send);

	return;
}

//function for reading from spi
uint16_t getFromSpi(){
	uint16_t ret;

	static volatile uint16_t WaitTimeOut = 0;
	volatile SPI_FifoStatus_e RxFifoCnt = SPI_FifoStatus_Empty;

	// reset the Rx fifo pointer to zero
	SPI_resetRxFifo(spiHandle);
	SPI_enableRxFifo(spiHandle);

	// wait for two words to populate the RX fifo, or a wait timeout will occur
	while((RxFifoCnt < SPI_FifoStatus_1_Word) && (WaitTimeOut < 0xff))
	{
		RxFifoCnt = SPI_getRxFifoStatus(spiHandle);
		WaitTimeOut++;

	}

	//read the spi word
	ret = SPI_read(spiHandle);

	return ret;
}


int main(void) {
	uint16_t thing = 0;
	//setup spi
	setupSpiA();
	thing = 1;
	setupGPIOforSpiA();
	thing = 2;
	while(1){
		thing = getFromSpi();
	}


	return 0;
}

any thoughts?

  • Hi John,

    Have you enabled the peripheral clock to the SPI module?  MotorWare code does this in HAL_setupPeripheralClks().

    void HAL_setupPeripheralClks(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      CLK_enableAdcClock(obj->clkHandle);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_1);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_2);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_3);
      CLK_enableEcap1Clock(obj->clkHandle);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_1);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_2);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_3);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_4);
      CLK_disableHrPwmClock(obj->clkHandle);
      CLK_disableI2cClock(obj->clkHandle);
      CLK_enableSciaClock(obj->clkHandle);
      CLK_enableSpiaClock(obj->clkHandle);
      
      CLK_enableTbClockSync(obj->clkHandle);
      return;
    } // end of HAL_setupPeripheralClks() function

    Jeff

     

    Did a reply answer your question? If yes, please click the "Verify Answer" button located at the bottom of that post.

    C2000 TI Wiki Pages
    TI Forum Sitemap

    ControlSUITE
    Motor Drive & Control (everything)

    CLA FAQs
    Workshop Material!

    InstaSPIN Wiki
    InstaSPIN
    FAST Software Encoder

    InstaSPIN-FOC
    InstaSPIN-MOTION
    InstaSPIN-BLDC

    InstaSPIN-FOC/MOTION User's Guide
    MotorWare 
    InstaSPIN Training

  • Hi Jeff,

    I did not, I didn't know I had to initialize the clock if the device was a slave. I will try that and report back. In the meantime, are there any other portions of my code that jump out as a problem to you?

  • John,

    You must enable to clock to any module on the C28x platform. The clock you are enabling is the clock to allow register writes and reads. You must enable the clock for any peripheral that you wish to use. The SPICLK (the pin) is separate. SPICLK is the clock to the state machine inside of the module itself. you do not need to set the baud rate if you are configured as the slave node.

    I would recommend going through some of the available examples for the device. There are things you may want to configure such as the PLL, interrupts, Flash, etc, depending on your application. Check out controlSUITE for more in depth examples.

    -Mark
  • Hi Mark,

    I decided to try to work with the HAL system by writing my own driver for interfacing with the arduino as a master and my c2000 as a slave, based on the DRV8301 drivers. Although now I can see the SPIDAT register changing in the variable viewer in CCS, the SPIRXBUF register doesn't change, and neither does the SPIRXEMU buffer that was used to read data from the DRV8301.

    In terms of setting up spi, i didn't change anything in the HAL setup other than setting the mode to slave and commenting out the baudrate setting, as i was told above that I shouldn't set that if the c2000 is a slave.

    I've attached the main file, the hal.c file, and the arduino.c and .h file, as well as spi.c and the drv8301 file i referenced from

    Thank you

    /*
     * Arduino.c
     *
     *  Created on: Nov 20, 2015
     *      Author: john
     */
    #include "Arduino.h"
    #include <math.h>
    #include "sw/drivers/drvic/drv8301/src/32b/f28x/f2806x/drv8301.h"
    
    Arduino_Handle Arduino_init(void *pMemory,const size_t numBytes)
    {
      Arduino_Handle handle;
    
    
      if(numBytes < sizeof(Arduino_Obj))
        return((Arduino_Handle)NULL);
    
    
      // assign the handle
      handle = (Arduino_Handle)pMemory;
    
      Arduino_resetRxTimeout(handle);
      Arduino_resetEnableTimeout(handle);
    
    
      return(handle);
    } // end of DRV8301_init() function
    
    void Arduino_setGpioHandle(Arduino_Handle handle,GPIO_Handle gpioHandle)
    {
      Arduino_Obj *obj = (Arduino_Obj *)handle;
    
      // initialize the gpio interface object
      obj->gpioHandle = gpioHandle;
    
      return;
    } // end of DRV8301_setGpioHandle() function
    
    
    void Arduino_setGpioNumber(Arduino_Handle handle,GPIO_Number_e gpioNumber)
    {
     Arduino_Obj *obj = (Arduino_Obj *)handle;
    
      // initialize the gpio interface object
      obj->gpioNumber = gpioNumber;
    
      return;
    } // end of DRV8301_setGpioNumber() function
    
    
    void Arduino_setSpiHandle(Arduino_Handle handle,SPI_Handle spiHandle)
    {
      Arduino_Obj *obj = (Arduino_Obj *)handle;
    
      // initialize the serial peripheral interface object
      obj->spiHandle = spiHandle;
    
      return;
    } // end of DRV8301_setSpiHandle() function
    
    
    uint16_t Arduino_readSpi(Arduino_Handle handle)
    {
      Arduino_Obj *obj = (Arduino_Obj *)handle;
      uint16_t ctrlWord;
      const uint16_t data = 0;
      volatile uint16_t readWord;
      static volatile uint16_t WaitTimeOut = 0;
      volatile SPI_FifoStatus_e RxFifoCnt = SPI_FifoStatus_Empty;
    
    
      // reset the Rx fifo pointer to zero
      SPI_resetRxFifo(obj->spiHandle);
      SPI_enableRxFifo(obj->spiHandle);
    
    /*NEED TO CHANGE IT FROM WRITING FIRST TO WAITING FOR THE MASTER TO COMMAND IT*/
      // write the command
      SPI_write(obj->spiHandle,ctrlWord);
      // dummy write to return the reply from the 8301
      SPI_write(obj->spiHandle,0x0000);
    
      // wait for two words to populate the RX fifo, or a wait timeout will occur
      while((RxFifoCnt < SPI_FifoStatus_2_Words) && (WaitTimeOut < 0xffff))
      {
        RxFifoCnt = SPI_getRxFifoStatus(obj->spiHandle);
    
          if(++WaitTimeOut > 0xfffe)
          {
              obj->RxTimeOut = true;
          }
      }
      /*NEED TO CHANGE IT FROM WRITING FIRST TO WAITING FOR THE MASTER TO COMMAND IT*/
    
      // Read two words, the dummy word and the data
      readWord = SPI_readEmu(obj->spiHandle); //not necessarily sure we need this
      readWord = SPI_readEmu(obj->spiHandle);
    
      return(readWord);
    }
    
    
    void Arduino_writeSpi(Arduino_Handle handle, const uint16_t data)
    {
      Arduino_Obj *obj = (Arduino_Obj *)handle;
    
      // reset the Rx fifo pointer to zero
      SPI_resetRxFifo(obj->spiHandle);
      SPI_enableRxFifo(obj->spiHandle);
    
      // write the command (time N)
      SPI_write(obj->spiHandle,data);
    
    
      return;
    }
    
    
    // end of file
    
    
    

    /* --COPYRIGHT--,BSD
     * Copyright (c) 2015, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * --/COPYRIGHT--*/
    //! \file   drivers/drvic/drv8301/src/32b/f28x/f2806x/drv8301.c
    //! \brief  Contains the various functions related to the DRV8301 object
    //!
    //! (C) Copyright 2015, Texas Instruments, Inc.
    
    
    // **************************************************************************
    // the includes
    
    #include <math.h>
    
    
    // drivers
    #include "sw/drivers/drvic/drv8301/src/32b/f28x/f2806x/drv8301.h"
    
    
    // modules
    
    
    // platforms
    
    
    
    // **************************************************************************
    // the defines
    
    
    // **************************************************************************
    // the globals
    
    
    // **************************************************************************
    // the function prototypes
    
    void DRV8301_enable(DRV8301_Handle handle)
    {
      DRV8301_Obj *obj = (DRV8301_Obj *)handle;
      static volatile uint16_t enableWaitTimeOut;
      uint16_t n = 0;
    
      // Enable the drv8301
      GPIO_setHigh(obj->gpioHandle,obj->gpioNumber);
    
      enableWaitTimeOut = 0;
    
      // Make sure the Fault bit is not set during startup
      while(((DRV8301_readSpi(handle,DRV8301_RegName_Status_1) & DRV8301_STATUS1_FAULT_BITS) != 0) && (enableWaitTimeOut < 1000))
      {
        if(++enableWaitTimeOut > 999)
        {
          obj->enableTimeOut = true;
        }
      }
    
      // Wait for the DRV8301 registers to update
      for(n=0;n<0xffff;n++)
        asm(" NOP");
    
      return;
    }
    
    DRV8301_DcCalMode_e DRV8301_getDcCalMode(DRV8301_Handle handle,const DRV8301_ShuntAmpNumber_e ampNumber)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      if(ampNumber == DRV8301_ShuntAmpNumber_1)
        {
          data &= (~DRV8301_CTRL2_DC_CAL_1_BITS);
    
        }
      else if(ampNumber == DRV8301_ShuntAmpNumber_2)
        {
          data &= (~DRV8301_CTRL2_DC_CAL_2_BITS);
        }
    
      return((DRV8301_DcCalMode_e)data);
    } // end of DRV8301_getDcCalMode() function
    
    
    DRV8301_FaultType_e DRV8301_getFaultType(DRV8301_Handle handle)
    {
      DRV8301_Word_t      readWord;
      DRV8301_FaultType_e faultType = DRV8301_FaultType_NoFault;
    
    
      // read the data
      readWord = DRV8301_readSpi(handle,DRV8301_RegName_Status_1);
    
      if(readWord & DRV8301_STATUS1_FAULT_BITS)
        {
          faultType = (DRV8301_FaultType_e)(readWord & DRV8301_FAULT_TYPE_MASK);
    
          if(faultType == DRV8301_FaultType_NoFault)
            {
              // read the data
              readWord = DRV8301_readSpi(handle,DRV8301_RegName_Status_2);
    
              if(readWord & DRV8301_STATUS2_GVDD_OV_BITS)
                {
                  faultType = DRV8301_FaultType_GVDD_OV;
                }
            }
        }
    
      return(faultType);
    } // end of DRV8301_getFaultType() function
    
    
    uint16_t DRV8301_getId(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Status_2);
    
      // mask bits
      data &= DRV8301_STATUS2_ID_BITS;
    
      return(data);
    } // end of DRV8301_getId() function
    
    
    DRV8301_VdsLevel_e DRV8301_getOcLevel(DRV8301_Handle handle)
    {
      uint16_t data;
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_OC_ADJ_SET_BITS);
    
      return((DRV8301_VdsLevel_e)data);
    } // end of DRV8301_getOcLevel() function
    
    
    DRV8301_OcMode_e DRV8301_getOcMode(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_OC_MODE_BITS);
    
      return((DRV8301_OcMode_e)data);
    } // end of DRV8301_getOcMode() function
    
    
    DRV8301_OcOffTimeMode_e DRV8301_getOcOffTimeMode(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      data &= (~DRV8301_CTRL2_OC_TOFF_BITS);
    
      return((DRV8301_OcOffTimeMode_e)data);
    } // end of DRV8301_getOcOffTimeMode() function
    
    
    DRV8301_OcTwMode_e DRV8301_getOcTwMode(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      data &= (~DRV8301_CTRL2_OCTW_SET_BITS);
    
      return((DRV8301_OcTwMode_e)data);
    } // end of DRV8301_getOcTwMode() function
    
    
    DRV8301_PeakCurrent_e DRV8301_getPeakCurrent(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_GATE_CURRENT_BITS);
    
      return((DRV8301_PeakCurrent_e)data);
    } // end of DRV8301_getPeakCurrent() function
    
    
    DRV8301_PwmMode_e DRV8301_getPwmMode(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_PWM_MODE_BITS);
    
      return((DRV8301_PwmMode_e)data);
    } // end of DRV8301_getPwmMode() function
    
    
    DRV8301_ShuntAmpGain_e DRV8301_getShuntAmpGain(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      data &= (~DRV8301_CTRL2_GAIN_BITS);
    
      return((DRV8301_ShuntAmpGain_e)data);
    } // end of DRV8301_getShuntAmpGain() function
    
    
    DRV8301_Handle DRV8301_init(void *pMemory,const size_t numBytes)
    {
      DRV8301_Handle handle;
    
    
      if(numBytes < sizeof(DRV8301_Obj))
        return((DRV8301_Handle)NULL);
    
    
      // assign the handle
      handle = (DRV8301_Handle)pMemory;
    
      DRV8301_resetRxTimeout(handle);
      DRV8301_resetEnableTimeout(handle);
    
    
      return(handle);
    } // end of DRV8301_init() function
    
    
    void DRV8301_setGpioHandle(DRV8301_Handle handle,GPIO_Handle gpioHandle)
    {
      DRV8301_Obj *obj = (DRV8301_Obj *)handle;
    
      // initialize the gpio interface object
      obj->gpioHandle = gpioHandle;
    
      return;
    } // end of DRV8301_setGpioHandle() function
    
    
    void DRV8301_setGpioNumber(DRV8301_Handle handle,GPIO_Number_e gpioNumber)
    {
      DRV8301_Obj *obj = (DRV8301_Obj *)handle;
    
      // initialize the gpio interface object
      obj->gpioNumber = gpioNumber;
    
      return;
    } // end of DRV8301_setGpioNumber() function
    
    
    void DRV8301_setSpiHandle(DRV8301_Handle handle,SPI_Handle spiHandle)
    {
      DRV8301_Obj *obj = (DRV8301_Obj *)handle;
    
      // initialize the serial peripheral interface object
      obj->spiHandle = spiHandle;
    
      return;
    } // end of DRV8301_setSpiHandle() function
    
    
    bool DRV8301_isFault(DRV8301_Handle handle)
    {
      DRV8301_Word_t readWord;
      bool status=false;
    
    
      // read the data
      readWord = DRV8301_readSpi(handle,DRV8301_RegName_Status_1);
    
      if(readWord & DRV8301_STATUS1_FAULT_BITS)
        {
          status = true;
        }
    
      return(status);
    } // end of DRV8301_isFault() function
    
    
    bool DRV8301_isReset(DRV8301_Handle handle)
    {
      DRV8301_Word_t readWord;
      bool status=false;
    
    
      // read the data
      readWord = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      if(readWord & DRV8301_CTRL1_GATE_RESET_BITS)
        {
          status = true;
        }
    
      return(status);
    } // end of DRV8301_isReset() function
    
    
    uint16_t DRV8301_readSpi(DRV8301_Handle handle,const DRV8301_RegName_e regName)
    {
      DRV8301_Obj *obj = (DRV8301_Obj *)handle;
      uint16_t ctrlWord;
      const uint16_t data = 0;
      volatile uint16_t readWord;
      static volatile uint16_t WaitTimeOut = 0;
      volatile SPI_FifoStatus_e RxFifoCnt = SPI_FifoStatus_Empty;
    
    
      // build the control word
      ctrlWord = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Read,regName,data);
    
      // reset the Rx fifo pointer to zero
      SPI_resetRxFifo(obj->spiHandle);
      SPI_enableRxFifo(obj->spiHandle);
    
    
      // write the command
      SPI_write(obj->spiHandle,ctrlWord);
      // dummy write to return the reply from the 8301
      SPI_write(obj->spiHandle,0x0000);
    
      // wait for two words to populate the RX fifo, or a wait timeout will occur
      while((RxFifoCnt < SPI_FifoStatus_2_Words) && (WaitTimeOut < 0xffff))
      {
        RxFifoCnt = SPI_getRxFifoStatus(obj->spiHandle);
    
          if(++WaitTimeOut > 0xfffe)
          {
              obj->RxTimeOut = true;
          }
      }
    
      // Read two words, the dummy word and the data
      readWord = SPI_readEmu(obj->spiHandle);
      readWord = SPI_readEmu(obj->spiHandle);
    
      return(readWord & DRV8301_DATA_MASK);
    }  // end of DRV8301_readSpi() function
    
    
    void DRV8301_reset(DRV8301_Handle handle)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // set the bits
      data |= DRV8301_CTRL1_GATE_RESET_BITS;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_1,data);
    
      return;
    }  // end of DRV8301_reset() function
    
      
    void DRV8301_setDcCalMode(DRV8301_Handle handle,const DRV8301_ShuntAmpNumber_e ampNumber,const DRV8301_DcCalMode_e mode)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      if(ampNumber == DRV8301_ShuntAmpNumber_1)
        {
          data &= (~DRV8301_CTRL2_DC_CAL_1_BITS);
    
        }
      else if(ampNumber == DRV8301_ShuntAmpNumber_2)
        {
          data &= (~DRV8301_CTRL2_DC_CAL_2_BITS);
        }
    
      // set the bits
      data |= mode;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_2,data);
    
      return;
    } // end of DRV8301_setDcCalMode() function
    
    
    void DRV8301_setOcLevel(DRV8301_Handle handle,const DRV8301_VdsLevel_e VdsLevel)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_OC_ADJ_SET_BITS);
    
      // set the bits
      data |= VdsLevel;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_1,data);
    
      return;
    } // end of DRV8301_setOcLevel() function
    
    
    void DRV8301_setOcMode(DRV8301_Handle handle,const DRV8301_OcMode_e mode)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_OC_MODE_BITS);
    
      // set the bits
      data |= mode;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_1,data);
    
      return;
    } // end of DRV8301_setOcMode() function
    
    
    void DRV8301_setOcOffTimeMode(DRV8301_Handle handle,const DRV8301_OcOffTimeMode_e mode)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      data &= (~DRV8301_CTRL2_OC_TOFF_BITS);
    
      // set the bits
      data |= mode;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_2,data);
    
      return;
    } // end of DRV8301_setOcOffTimeMode() function
    
    
    void DRV8301_setOcTwMode(DRV8301_Handle handle,const DRV8301_OcTwMode_e mode)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      data &= (~DRV8301_CTRL2_OCTW_SET_BITS);
    
      // set the bits
      data |= mode;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_2,data);
    
      return;
    } // end of DRV8301_setOcTwMode() function
    
    
    void DRV8301_setPeakCurrent(DRV8301_Handle handle,const DRV8301_PeakCurrent_e peakCurrent)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_GATE_CURRENT_BITS);
    
      // set the bits
      data |= peakCurrent;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_1,data);
    
      return;
    } // end of DRV8301_setPeakCurrent() function
    
    
    void DRV8301_setPwmMode(DRV8301_Handle handle,const DRV8301_PwmMode_e mode)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);
    
      // clear the bits
      data &= (~DRV8301_CTRL1_PWM_MODE_BITS);
    
      // set the bits
      data |= mode;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_1,data);
    
      return;
    } // end of DRV8301_setPwmMode() function
    
    
    void DRV8301_setShuntAmpGain(DRV8301_Handle handle,const DRV8301_ShuntAmpGain_e gain)
    {
      uint16_t data;
    
    
      // read data
      data = DRV8301_readSpi(handle,DRV8301_RegName_Control_2);
    
      // clear the bits
      data &= (~DRV8301_CTRL2_GAIN_BITS);
    
      // set the bits
      data |= gain;
    
      // write the data
      DRV8301_writeSpi(handle,DRV8301_RegName_Control_2,data);
    
      return;
    } // end of DRV8301_setShuntAmpGain() function
    
    
    void DRV8301_writeSpi(DRV8301_Handle handle, const DRV8301_RegName_e regName,const uint16_t data)
    {
      DRV8301_Obj *obj = (DRV8301_Obj *)handle;
      uint16_t ctrlWord;
    
    
      // build the command
      ctrlWord = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Write,regName,data);
    
      // reset the Rx fifo pointer to zero
      SPI_resetRxFifo(obj->spiHandle);
      SPI_enableRxFifo(obj->spiHandle);
    
      // write the command (time N)
      SPI_write(obj->spiHandle,ctrlWord);
    
    
      return;
    }  // end of DRV8301_writeSpi() function
    
    
    void DRV8301_writeData(DRV8301_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      DRV8301_RegName_e  drvRegName;
      uint16_t drvDataNew;
    
    
      if(Spi_8301_Vars->SndCmd)
      {
        // Update Control Register 1
        drvRegName = DRV8301_RegName_Control_1;
        drvDataNew = Spi_8301_Vars->Ctrl_Reg_1.DRV8301_CURRENT |  \
                     Spi_8301_Vars->Ctrl_Reg_1.DRV8301_RESET   |  \
                     Spi_8301_Vars->Ctrl_Reg_1.PWM_MODE     |  \
                     Spi_8301_Vars->Ctrl_Reg_1.OC_MODE      |  \
                     Spi_8301_Vars->Ctrl_Reg_1.OC_ADJ_SET;
        DRV8301_writeSpi(handle,drvRegName,drvDataNew);
    
        // Update Control Register 2
        drvRegName = DRV8301_RegName_Control_2;
        drvDataNew = Spi_8301_Vars->Ctrl_Reg_2.OCTW_SET      |  \
                     Spi_8301_Vars->Ctrl_Reg_2.GAIN          |  \
                     Spi_8301_Vars->Ctrl_Reg_2.DC_CAL_CH1p2  |  \
                     Spi_8301_Vars->Ctrl_Reg_2.OC_TOFF;
        DRV8301_writeSpi(handle,drvRegName,drvDataNew);
    
        Spi_8301_Vars->SndCmd = false;
      }
    
      return;
    }  // end of DRV8301_writeData() function
    
    
    void DRV8301_readData(DRV8301_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      DRV8301_RegName_e  drvRegName;
      uint16_t drvDataNew;
    
    
      if(Spi_8301_Vars->RcvCmd)
      {
        // Update Status Register 1
        drvRegName = DRV8301_RegName_Status_1;
        drvDataNew = DRV8301_readSpi(handle,drvRegName);
        Spi_8301_Vars->Stat_Reg_1.FAULT = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FAULT_BITS);
        Spi_8301_Vars->Stat_Reg_1.GVDD_UV = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_GVDD_UV_BITS);
        Spi_8301_Vars->Stat_Reg_1.PVDD_UV = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_PVDD_UV_BITS);
        Spi_8301_Vars->Stat_Reg_1.OTSD = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_OTSD_BITS);
        Spi_8301_Vars->Stat_Reg_1.OTW = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_OTW_BITS);
        Spi_8301_Vars->Stat_Reg_1.FETHA_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETHA_OC_BITS);
        Spi_8301_Vars->Stat_Reg_1.FETLA_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETLA_OC_BITS);
        Spi_8301_Vars->Stat_Reg_1.FETHB_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETHB_OC_BITS);
        Spi_8301_Vars->Stat_Reg_1.FETLB_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETLB_OC_BITS);
        Spi_8301_Vars->Stat_Reg_1.FETHC_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETHC_OC_BITS);
        Spi_8301_Vars->Stat_Reg_1.FETLC_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETLC_OC_BITS);
    
        // Update Status Register 2
        drvRegName = DRV8301_RegName_Status_2;
        drvDataNew = DRV8301_readSpi(handle,drvRegName);
        Spi_8301_Vars->Stat_Reg_2.GVDD_OV = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS2_GVDD_OV_BITS);
        Spi_8301_Vars->Stat_Reg_2.DeviceID = (uint16_t)(drvDataNew & (uint16_t)DRV8301_STATUS2_ID_BITS);
    
        // Update Control Register 1
        drvRegName = DRV8301_RegName_Control_1;
        drvDataNew = DRV8301_readSpi(handle,drvRegName);
        Spi_8301_Vars->Ctrl_Reg_1.DRV8301_CURRENT = (DRV8301_PeakCurrent_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_GATE_CURRENT_BITS);
        Spi_8301_Vars->Ctrl_Reg_1.DRV8301_RESET = (DRV8301_Reset_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_GATE_RESET_BITS);
        Spi_8301_Vars->Ctrl_Reg_1.PWM_MODE = (DRV8301_PwmMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_PWM_MODE_BITS);
        Spi_8301_Vars->Ctrl_Reg_1.OC_MODE = (DRV8301_OcMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_OC_MODE_BITS);
        Spi_8301_Vars->Ctrl_Reg_1.OC_ADJ_SET = (DRV8301_VdsLevel_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_OC_ADJ_SET_BITS);
    
        // Update Control Register 2
        drvRegName = DRV8301_RegName_Control_2;
        drvDataNew = DRV8301_readSpi(handle,drvRegName);
        Spi_8301_Vars->Ctrl_Reg_2.OCTW_SET = (DRV8301_OcTwMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL2_OCTW_SET_BITS);
        Spi_8301_Vars->Ctrl_Reg_2.GAIN = (DRV8301_ShuntAmpGain_e)(drvDataNew & (uint16_t)DRV8301_CTRL2_GAIN_BITS);
        Spi_8301_Vars->Ctrl_Reg_2.DC_CAL_CH1p2 = (DRV8301_DcCalMode_e)(drvDataNew & (uint16_t)(DRV8301_CTRL2_DC_CAL_1_BITS | DRV8301_CTRL2_DC_CAL_2_BITS));
        Spi_8301_Vars->Ctrl_Reg_2.OC_TOFF = (DRV8301_OcOffTimeMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL2_OC_TOFF_BITS);
    
        Spi_8301_Vars->RcvCmd = false;
      }
    
      return;
    }  // end of DRV8301_readData() function
    
    
    void DRV8301_setupSpi(DRV8301_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      DRV8301_RegName_e  drvRegName;
      uint16_t drvDataNew;
      uint16_t n;
    
    
      // Update Control Register 1
      drvRegName = DRV8301_RegName_Control_1;
      drvDataNew = (DRV8301_PeakCurrent_0p25_A   | \
                    DRV8301_Reset_Normal         | \
                    DRV8301_PwmMode_Six_Inputs   | \
                    DRV8301_OcMode_CurrentLimit  | \
                    DRV8301_VdsLevel_0p730_V);
      DRV8301_writeSpi(handle,drvRegName,drvDataNew);
    
      // Update Control Register 2
      drvRegName = DRV8301_RegName_Control_2;
      drvDataNew = (DRV8301_OcTwMode_Both        | \
                    DRV8301_ShuntAmpGain_10VpV   | \
                    DRV8301_DcCalMode_Ch1_Load   | \
                    DRV8301_DcCalMode_Ch2_Load   | \
                    DRV8301_OcOffTimeMode_Normal);
      DRV8301_writeSpi(handle,drvRegName,drvDataNew);
    
    
      Spi_8301_Vars->SndCmd = false;
      Spi_8301_Vars->RcvCmd = false;
    
    
      // Wait for the DRV8301 registers to update
      for(n=0;n<100;n++)
        asm(" NOP");
    
    
      // Update Status Register 1
      drvRegName = DRV8301_RegName_Status_1;
      drvDataNew = DRV8301_readSpi(handle,drvRegName);
      Spi_8301_Vars->Stat_Reg_1.FAULT = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FAULT_BITS);
      Spi_8301_Vars->Stat_Reg_1.GVDD_UV = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_GVDD_UV_BITS);
      Spi_8301_Vars->Stat_Reg_1.PVDD_UV = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_PVDD_UV_BITS);
      Spi_8301_Vars->Stat_Reg_1.OTSD = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_OTSD_BITS);
      Spi_8301_Vars->Stat_Reg_1.OTW = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_OTW_BITS);
      Spi_8301_Vars->Stat_Reg_1.FETHA_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETHA_OC_BITS);
      Spi_8301_Vars->Stat_Reg_1.FETLA_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETLA_OC_BITS);
      Spi_8301_Vars->Stat_Reg_1.FETHB_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETHB_OC_BITS);
      Spi_8301_Vars->Stat_Reg_1.FETLB_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETLB_OC_BITS);
      Spi_8301_Vars->Stat_Reg_1.FETHC_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETHC_OC_BITS);
      Spi_8301_Vars->Stat_Reg_1.FETLC_OC = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS1_FETLC_OC_BITS);
    
      // Update Status Register 2
      drvRegName = DRV8301_RegName_Status_2;
      drvDataNew = DRV8301_readSpi(handle,drvRegName);
      Spi_8301_Vars->Stat_Reg_2.GVDD_OV = (bool)(drvDataNew & (uint16_t)DRV8301_STATUS2_GVDD_OV_BITS);
      Spi_8301_Vars->Stat_Reg_2.DeviceID = (uint16_t)(drvDataNew & (uint16_t)DRV8301_STATUS2_ID_BITS);
    
      // Update Control Register 1
      drvRegName = DRV8301_RegName_Control_1;
      drvDataNew = DRV8301_readSpi(handle,drvRegName);
      Spi_8301_Vars->Ctrl_Reg_1.DRV8301_CURRENT = (DRV8301_PeakCurrent_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_GATE_CURRENT_BITS);
      Spi_8301_Vars->Ctrl_Reg_1.DRV8301_RESET = (DRV8301_Reset_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_GATE_RESET_BITS);
      Spi_8301_Vars->Ctrl_Reg_1.PWM_MODE = (DRV8301_PwmMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_PWM_MODE_BITS);
      Spi_8301_Vars->Ctrl_Reg_1.OC_MODE = (DRV8301_OcMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_OC_MODE_BITS);
      Spi_8301_Vars->Ctrl_Reg_1.OC_ADJ_SET = (DRV8301_VdsLevel_e)(drvDataNew & (uint16_t)DRV8301_CTRL1_OC_ADJ_SET_BITS);
    
      // Update Control Register 2
      drvRegName = DRV8301_RegName_Control_2;
      drvDataNew = DRV8301_readSpi(handle,drvRegName);
      Spi_8301_Vars->Ctrl_Reg_2.OCTW_SET = (DRV8301_OcTwMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL2_OCTW_SET_BITS);
      Spi_8301_Vars->Ctrl_Reg_2.GAIN = (DRV8301_ShuntAmpGain_e)(drvDataNew & (uint16_t)DRV8301_CTRL2_GAIN_BITS);
      Spi_8301_Vars->Ctrl_Reg_2.DC_CAL_CH1p2 = (DRV8301_DcCalMode_e)(drvDataNew & (uint16_t)(DRV8301_CTRL2_DC_CAL_1_BITS | DRV8301_CTRL2_DC_CAL_2_BITS));
      Spi_8301_Vars->Ctrl_Reg_2.OC_TOFF = (DRV8301_OcOffTimeMode_e)(drvDataNew & (uint16_t)DRV8301_CTRL2_OC_TOFF_BITS);
    
      return;
    }
    
    
    // end of file
    

    /* --COPYRIGHT--,BSD
     * Copyright (c) 2015, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * --/COPYRIGHT--*/
    //! \file   drivers/spi/src/32b/f28x/f2806x/spi.c
    //! \brief  Contains the various functions related to the 
    //!         serial peripheral interface (SPI) object
    //!
    //! (C) Copyright 2015, Texas Instruments, Inc.
    
    
    // **************************************************************************
    // the includes
    
    #include "sw/drivers/spi/src/32b/f28x/f2806x/spi.h"
    
    
    // **************************************************************************
    // the defines
    
    
    // **************************************************************************
    // the globals
    
    
    // **************************************************************************
    // the functions
    
    void SPI_clearRxFifoOvf(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFRX |= SPI_SPIFFRX_FIFO_OVFCLR_BITS;
    
      return;
    } // end of SPI_clearRxFifoOvf() function
    
    
    void SPI_clearRxFifoInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFRX |= SPI_SPIFFRX_INTCLR_BITS;
    
      return;
    } // end of SPI_clearRxFifoInt() function
    
    
    void SPI_clearTxFifoInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFTX |= SPI_SPIFFTX_INTCLR_BITS;
    
      return;
    } // end of SPI_clearTxFifoInt() function
    
    
    void SPI_disableInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPICTL &= (~SPI_SPICTL_INT_ENA_BITS);
    
      return;
    } // end of SPI_disableInt() function
    
    
    void SPI_disableLoopBack(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPICCR &= (~SPI_SPICCR_SPILBK_BITS);
    
      return;
    } // end of SPI_disableLoopBack() function
    
    
    void SPI_disableOverRunInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
      // clear the bits
      spi->SPICTL &= (~SPI_SPICTL_OVRRUN_INT_ENA_BITS);
    
      return;
    } // end of SPI_disableOverRunInt() function
    
    
    void SPI_disableRxFifoInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPIFFRX &= (~SPI_SPIFFRX_IENA_BITS);
    
      return;
    } // end of SPI_disableRxFifoInt() function
    
    
    void SPI_disableTx(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPICTL &= (~SPI_SPICTL_TALK_BITS);
    
      return;
    } // end of SPI_disableTx() function
    
    
    void SPI_disableTxFifoEnh(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPIFFTX &= (~SPI_SPIFFTX_FIFO_ENA_BITS);
    
      return;
    } // end of SPI_disableTxFifo() function
    
    
    void SPI_disableTxFifoInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPIFFTX &= (~SPI_SPIFFTX_IENA_BITS);
    
      return;
    } // end of SPI_disableTxFifoInt() function
    
    
    void SPI_enable(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPICCR |= SPI_SPICCR_RESET_BITS;
    
      return;
    } // end of SPI_enable() function
    
    
    void SPI_enableChannels(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFTX |= (uint16_t)SPI_SPIFFTX_CHAN_RESET_BITS;
      
      return;
    } // SPI_enableChannels() function
    
    
    void SPI_enableInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPICTL |= SPI_SPICTL_INT_ENA_BITS;
    
      return;
    } // end of SPI_enableInt() function
    
    
    void SPI_enableLoopBack(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPICCR |= SPI_SPICCR_SPILBK_BITS;
    
      return;
    } // end of SPI_enableLoopBack() function
    
    
    void SPI_enableOverRunInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPICTL |= SPI_SPICTL_OVRRUN_INT_ENA_BITS;
    
      return;
    } // end of SPI_enableOverRunInt() function
    
    
    void SPI_enableRxFifo(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFRX |= SPI_SPIFFRX_FIFO_RESET_BITS;
    
      return;
    } // end of SPI_enableRxFifo() function
    
    
    void SPI_enableRxFifoInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFRX |= SPI_SPIFFRX_IENA_BITS;
    
      return;
    } // end of SPI_enableRxFifoInt() function
    
    
    void SPI_enableTx(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPICTL |= SPI_SPICTL_TALK_BITS;
    
      return;
    } // end of SPI_enableTx() function
    
    
    void SPI_enableTxFifo(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFTX |= SPI_SPIFFTX_FIFO_RESET_BITS;
    
      return;
    } // end of SPI_enableTxFifo() function
    
    
    void SPI_enableTxFifoEnh(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFTX |= SPI_SPIFFTX_FIFO_ENA_BITS;
    
      return;
    } // end of SPI_enableTxFifo() function
    
    
    void SPI_enableTxFifoInt(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFTX |= SPI_SPIFFTX_IENA_BITS;
    
      return;
    } // end of SPI_enableTxFifoInt() function
    
    
    SPI_FifoStatus_e SPI_getRxFifoStatus(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // get the status
      SPI_FifoStatus_e status = (SPI_FifoStatus_e)((spi->SPIFFRX) & SPI_SPIFFRX_FIFO_ST_BITS);
    
      return(status);
    } // SPI_getRxFifoStatus() function
    
    
    SPI_FifoStatus_e SPI_getTxFifoStatus(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // get the status
      SPI_FifoStatus_e status = (SPI_FifoStatus_e)(spi->SPIFFTX & SPI_SPIFFTX_FIFO_ST_BITS);
    
      return(status);
    } // SPI_getTxFifoStatus() function
    
    
    SPI_IntFlagStatus_e SPI_getIntFlagStatus(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // get the status
      SPI_IntFlagStatus_e status = (SPI_IntFlagStatus_e)(spi->SPIST & SPI_SPIST_INTFLAG_BITS);
    
      return(status);
    } // SPI_getIntFlagStatus() function
    
    
    SPI_TxBufferStatus_e SPI_getTxBufferStatus(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // get the status
      SPI_TxBufferStatus_e status = (SPI_TxBufferStatus_e)(spi->SPIST & SPI_SPIST_TXBUF_BITS);
    
      return(status);
    } // SPI_getTxBufferStatus() function
    
    
    SPI_Handle SPI_init(void *pMemory,const size_t numBytes)
    {
      SPI_Handle spiHandle;
    
    
      if(numBytes < sizeof(SPI_Obj))
        return((SPI_Handle)NULL);
    
      // assign the handle
      spiHandle = (SPI_Handle)pMemory;
      
      return(spiHandle);
    } // end of SPI_init() function
    
    
    void SPI_reset(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPICCR &= (~SPI_SPICCR_RESET_BITS);
    
      return;
    } // end of SPI_reset() function
    
    
    void SPI_resetChannels(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPIFFTX &= (~SPI_SPIFFTX_CHAN_RESET_BITS);
      
      return;
    } // SPI_resetChannels() function
    
    
    void SPI_resetRxFifo(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPIFFRX &= (~SPI_SPIFFRX_FIFO_RESET_BITS);
    
      return;
    } // end of SPI_resetRxFifo() function
    
    
    void SPI_resetTxFifo(SPI_Handle spiHandle)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPIFFTX &= (~SPI_SPIFFTX_FIFO_RESET_BITS);
    
      return;
    } // end of SPI_resetTxFifo() function
    
    
    void SPI_setBaudRate(SPI_Handle spiHandle,const SPI_BaudRate_e baudRate)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
      // set the bits
      spi->SPIBRR = baudRate;
      return;
    } // end of SPI_setBaudRate() function
    
    
    void SPI_setCharLength(SPI_Handle spiHandle,const SPI_CharLength_e length)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPICCR &= (~SPI_SPICCR_CHAR_LENGTH_BITS);
    
      // set the bits
      spi->SPICCR |= length;
    
      return;
    } // end of SPI_setCharLength() function
    
    
    void SPI_setClkPhase(SPI_Handle spiHandle,const SPI_ClkPhase_e clkPhase)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the value
      spi->SPICTL |= clkPhase;
    
      return;
    } // end of SPI_setClkPhase() function
    
    
    void SPI_setClkPolarity(SPI_Handle spiHandle,const SPI_ClkPolarity_e polarity)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the bits
      spi->SPICCR &= (~SPI_SPICCR_CLKPOL_BITS);
    
      // set the bits
      spi->SPICCR |= polarity;
    
      return;
    } // end of SPI_setClkPolarity() function
    
    
    void SPI_setMode(SPI_Handle spiHandle,const SPI_Mode_e mode)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPICTL |= mode;
    
      return;
    } // end of SPI_setMode() function
    
    
    void SPI_setPriority(SPI_Handle spiHandle,const SPI_Priority_e priority)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the field
      spi->SPIPRI &= (~SPI_SPIPRI_SUSP_BITS);
      // set the bits
      spi->SPIPRI |= priority;
    
      return;
    } // SPI_setPriority() function
    
    
    void SPI_setRxFifoIntLevel(SPI_Handle spiHandle,const SPI_FifoLevel_e fifoLevel)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the value
      spi->SPIFFRX &= (~SPI_SPIFFRX_IL_BITS);
    
      // set the bits
      spi->SPIFFRX |= fifoLevel;
    
      return;
    } // end of SPI_setRxFifoIntLevel() function
    
    
    void SPI_setSteInv(SPI_Handle spiHandle,const SPI_SteInv_e steinv)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
      // clear the field
      spi->SPIPRI &= (~SPI_SPIPRI_STE_INV_BITS);
      // set the bits
      spi->SPIPRI |= steinv;
    
      return;
    } // SPI_setSteInv() function
    
    
    void SPI_setSuspend(SPI_Handle spiHandle,const SPI_EmulationSuspend_e emuSuspend)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
      // clear the field
      spi->SPIPRI &= (~SPI_SPIPRI_SUSP_BITS);
    
      // set the bits
      spi->SPIPRI |= emuSuspend;
    
      return;
    } // SPI_setSuspend() function
    
    
    void SPI_setTriWire(SPI_Handle spiHandle,const SPI_TriWire_e triwire)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
      // clear the field
      spi->SPIPRI &= (~SPI_SPIPRI_TRIWIRE);
      // set the bits
      spi->SPIPRI |= triwire;
    
      return;
    } // SPI_setTriWire() function
    
    
    void SPI_setTxDelay(SPI_Handle spiHandle,const uint_least8_t delay)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // set the bits
      spi->SPIFFCT = delay;
    
      return;
    } // end of SPI_setTxDelay() function
    
    
    void SPI_setTxFifoIntLevel(SPI_Handle spiHandle,const SPI_FifoLevel_e fifoLevel)
    {
      SPI_Obj *spi = (SPI_Obj *)spiHandle;
    
    
      // clear the value
      spi->SPIFFTX &= (~SPI_SPIFFTX_IL_BITS);
    
      // set the bits
      spi->SPIFFTX |= fifoLevel;
    
      return;
    } // end of SPI_setTxFifoIntLevel() function
    
    
    // end of file
    

    /* --COPYRIGHT--,BSD
     * Copyright (c) 2012, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * --/COPYRIGHT--*/
    //! \file   solutions/instaspin_foc/boards/drv8301kit_revD/f28x/f2806xF/src/hal.c
    //! \brief Contains the various functions related to the HAL object (everything outside the CTRL system) 
    //!
    //! (C) Copyright 2011, Texas Instruments, Inc.
    
    
    // **************************************************************************
    // the includes
    
    // drivers
    
    // modules
    
    // platforms
    #include "hal.h"
    #include "user.h"
    #include "hal_obj.h"
    
    #ifdef FLASH
    #pragma CODE_SECTION(HAL_setupFlash,"ramfuncs");
    #endif
    
    // **************************************************************************
    // the defines
    
    
    // **************************************************************************
    // the globals
    
    HAL_Obj hal;
    
    
    // **************************************************************************
    // the functions
    
    void HAL_cal(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // enable the ADC clock
      CLK_enableAdcClock(obj->clkHandle);
    
    
      // Run the Device_cal() function
      // This function copies the ADC and oscillator calibration values from TI reserved
      // OTP into the appropriate trim registers
      // This boot ROM automatically calls this function to calibrate the interal 
      // oscillators and ADC with device specific calibration data.
      // If the boot ROM is bypassed by Code Composer Studio during the development process,
      // then the calibration must be initialized by the application
      ENABLE_PROTECTED_REGISTER_WRITE_MODE;
      (*Device_cal)();
      DISABLE_PROTECTED_REGISTER_WRITE_MODE;
    
      // run offsets calibration in user's memory
      HAL_AdcOffsetSelfCal(handle);
    
      // run oscillator compensation
      HAL_OscTempComp(handle);
    
      // disable the ADC clock
      CLK_disableAdcClock(obj->clkHandle);
    
      return;
    } // end of HAL_cal() function
    
    
    void HAL_OscTempComp(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t Temperature;
    
      // disable the ADCs
      ADC_disable(obj->adcHandle);
    
      // power up the bandgap circuit
      ADC_enableBandGap(obj->adcHandle);
    
      // set the ADC voltage reference source to internal
      ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
    
      // enable the ADC reference buffers
      ADC_enableRefBuffers(obj->adcHandle);
    
      // Set main clock scaling factor (max45MHz clock for the ADC module)
      ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
    
      // power up the ADCs
      ADC_powerUp(obj->adcHandle);
    
      // enable the ADCs
      ADC_enable(obj->adcHandle);
    
      // enable non-overlap mode
      ADC_enableNoOverlapMode(obj->adcHandle);
    
      // connect channel A5 internally to the temperature sensor
      ADC_setTempSensorSrc(obj->adcHandle, ADC_TempSensorSrc_Int);
    
      // set SOC0 channel select to ADCINA5
      ADC_setSocChanNumber(obj->adcHandle, ADC_SocNumber_0, ADC_SocChanNumber_A5);
    
      // set SOC0 acquisition period to 26 ADCCLK
      ADC_setSocSampleDelay(obj->adcHandle, ADC_SocNumber_0, ADC_SocSampleDelay_64_cycles);
    
      // connect ADCINT1 to EOC0
      ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC0);
    
      // clear ADCINT1 flag
      ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
    
      // enable ADCINT1
      ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
    
      // force start of conversion on SOC0
      ADC_setSocFrc(obj->adcHandle, ADC_SocFrc_0);
    
      // wait for end of conversion
      while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
    
      // clear ADCINT1 flag
      ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
    
      Temperature = ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
    
      HAL_osc1Comp(handle, Temperature);
    
      HAL_osc2Comp(handle, Temperature);
    
      return;
    } // end of HAL_OscTempComp() function
    
    
    void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
    {
    	int16_t compOscFineTrim;
    	HAL_Obj *obj = (HAL_Obj *)handle;
    
    	ENABLE_PROTECTED_REGISTER_WRITE_MODE;
    
        compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc1FineTrimSlope()
                          + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc1FineTrimOffset() - OSC_POSTRIM;
    
        if(compOscFineTrim > 31)
          {
            compOscFineTrim = 31;
          }
    	else if(compOscFineTrim < -31)
          {
            compOscFineTrim = -31;
          }
    
        OSC_setTrim(obj->oscHandle, OSC_Number_1, HAL_getOscTrimValue(getOsc1CoarseTrim(), compOscFineTrim));
    
        DISABLE_PROTECTED_REGISTER_WRITE_MODE;
    
        return;
    } // end of HAL_osc1Comp() function
    
    
    void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
    {
    	int16_t compOscFineTrim;
    	HAL_Obj *obj = (HAL_Obj *)handle;
    
    	ENABLE_PROTECTED_REGISTER_WRITE_MODE;
    
        compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc2FineTrimSlope()
                          + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc2FineTrimOffset() - OSC_POSTRIM;
    
        if(compOscFineTrim > 31)
          {
            compOscFineTrim = 31;
          }
    	else if(compOscFineTrim < -31)
          {
            compOscFineTrim = -31;
          }
    
        OSC_setTrim(obj->oscHandle, OSC_Number_2, HAL_getOscTrimValue(getOsc2CoarseTrim(), compOscFineTrim));
    
        DISABLE_PROTECTED_REGISTER_WRITE_MODE;
    
        return;
    } // end of HAL_osc2Comp() function
    
    
    uint16_t HAL_getOscTrimValue(int16_t coarse, int16_t fine)
    {
      uint16_t regValue = 0;
    
      if(fine < 0)
        {
          regValue = ((-fine) | 0x20) << 9;
        }
      else
        {
          regValue = fine << 9;
        }
    
      if(coarse < 0)
        {
          regValue |= ((-coarse) | 0x80);
        }
      else
        {
          regValue |= coarse;
        }
    
      return regValue;
    } // end of HAL_getOscTrimValue() function
    
    
    void HAL_AdcOffsetSelfCal(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t AdcConvMean;
    
      // disable the ADCs
      ADC_disable(obj->adcHandle);
    
      // power up the bandgap circuit
      ADC_enableBandGap(obj->adcHandle);
    
      // set the ADC voltage reference source to internal
      ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
    
      // enable the ADC reference buffers
      ADC_enableRefBuffers(obj->adcHandle);
    
      // Set main clock scaling factor (max45MHz clock for the ADC module)
      ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
    
      // power up the ADCs
      ADC_powerUp(obj->adcHandle);
    
      // enable the ADCs
      ADC_enable(obj->adcHandle);
    
      //Select VREFLO internal connection on B5
      ADC_enableVoltRefLoConv(obj->adcHandle);
    
      //Select channel B5 for all SOC
      HAL_AdcCalChanSelect(handle, ADC_SocChanNumber_B5);
    
      //Apply artificial offset (+80) to account for a negative offset that may reside in the ADC core
      ADC_setOffTrim(obj->adcHandle, 80);
    
      //Capture ADC conversion on VREFLO
      AdcConvMean = HAL_AdcCalConversion(handle);
    
      //Set offtrim register with new value (i.e remove artical offset (+80) and create a two's compliment of the offset error)
      ADC_setOffTrim(obj->adcHandle, 80 - AdcConvMean);
    
      //Select external ADCIN5 input pin on B5
      ADC_disableVoltRefLoConv(obj->adcHandle);
    
      return;
    } // end of HAL_AdcOffsetSelfCal() function
    
    
    void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_8,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_9,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_10,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_11,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_12,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_13,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_14,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_15,chanNumber);
    
      return;
    } // end of HAL_AdcCalChanSelect() function
    
    
    uint16_t HAL_AdcCalConversion(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t index, SampleSize, Mean;
      uint32_t Sum;
      ADC_SocSampleDelay_e ACQPS_Value;
    
      index       = 0;     //initialize index to 0
      SampleSize  = 256;   //set sample size to 256 (**NOTE: Sample size must be multiples of 2^x where is an integer >= 4)
      Sum         = 0;     //set sum to 0
      Mean        = 999;   //initialize mean to known value
    
      //Set the ADC sample window to the desired value (Sample window = ACQPS + 1)
      ACQPS_Value = ADC_SocSampleDelay_7_cycles;
    
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_8,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_9,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_10,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_11,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_12,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_13,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_14,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_15,ACQPS_Value);
    
      // Enabled ADCINT1 and ADCINT2
      ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
      ADC_enableInt(obj->adcHandle, ADC_IntNumber_2);
    
      // Disable continuous sampling for ADCINT1 and ADCINT2
      ADC_setIntMode(obj->adcHandle, ADC_IntNumber_1, ADC_IntMode_EOC);
      ADC_setIntMode(obj->adcHandle, ADC_IntNumber_2, ADC_IntMode_EOC);
    
      //ADCINTs trigger at end of conversion
      ADC_setIntPulseGenMode(obj->adcHandle, ADC_IntPulseGenMode_Prior);
    
      // Setup ADCINT1 and ADCINT2 trigger source
      ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC6);
      ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_2, ADC_IntSrc_EOC14);
    
      // Setup each SOC's ADCINT trigger source
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_0, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_1, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_2, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_3, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_4, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_5, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_6, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_7, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_8, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_9, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_10, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_11, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_12, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_13, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_14, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_15, ADC_Int1TriggersSOC);
    
      // Delay before converting ADC channels
      usDelay(ADC_DELAY_usec);
    
      ADC_setSocFrcWord(obj->adcHandle, 0x00FF);
    
      while( index < SampleSize )
        {
          //Wait for ADCINT1 to trigger, then add ADCRESULT0-7 registers to sum
          while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
    
          //Must clear ADCINT1 flag since INT1CONT = 0
          ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
    
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_1);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_2);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_3);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_4);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_5);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_6);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_7);
    
          //Wait for ADCINT2 to trigger, then add ADCRESULT8-15 registers to sum
          while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_2) == 0){}
    
          //Must clear ADCINT2 flag since INT2CONT = 0
          ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_2);
    
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_8);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_9);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_10);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_11);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_12);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_13);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_14);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_15);
    
          index+=16;
    
      } // end data collection
    
      //Disable ADCINT1 and ADCINT2 to STOP the ping-pong sampling
      ADC_disableInt(obj->adcHandle, ADC_IntNumber_1);
      ADC_disableInt(obj->adcHandle, ADC_IntNumber_2);
    
      //Calculate average ADC sample value
      Mean = Sum / SampleSize;
    
      // Clear start of conversion trigger
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_0, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_1, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_2, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_3, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_4, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_5, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_6, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_7, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_8, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_9, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_10, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_11, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_12, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_13, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_14, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_15, ADC_NoIntTriggersSOC);
    
      //return the average
      return(Mean);
    } // end of HAL_AdcCalConversion() function
    
    
    void HAL_disableWdog(HAL_Handle halHandle)
    {
      HAL_Obj *hal = (HAL_Obj *)halHandle;
    
    
      WDOG_disable(hal->wdogHandle);
    
    
      return;
    } // end of HAL_disableWdog() function
    
    
    void HAL_disableGlobalInts(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CPU_disableGlobalInts(obj->cpuHandle);
    
      return;
    } // end of HAL_disableGlobalInts() function
    
    
    void HAL_enableAdcInts(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // enable the PIE interrupts associated with the ADC interrupts
      PIE_enableAdcInt(obj->pieHandle,ADC_IntNumber_1);
    
    
      // enable the ADC interrupts
      ADC_enableInt(obj->adcHandle,ADC_IntNumber_1);
    
    
      // enable the cpu interrupt for ADC interrupts
      CPU_enableInt(obj->cpuHandle,CPU_IntNumber_10);
    
      return;
    } // end of HAL_enableAdcInts() function
    
    
    void HAL_enableDebugInt(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CPU_enableDebugInt(obj->cpuHandle);
    
      return;
    } // end of HAL_enableDebugInt() function
    
    
    /*void HAL_enableDrv(HAL_Handle handle)//don't need for arduino
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
      DRV8301_enable(obj->drv8301Handle);
    
      return;
    }  // end of HAL_enableDrv() function*/
    
    
    void HAL_enableGlobalInts(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CPU_enableGlobalInts(obj->cpuHandle);
    
      return;
    } // end of HAL_enableGlobalInts() function
    
    
    void HAL_enablePwmInt(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    #ifdef J5
      PIE_enablePwmInt(obj->pieHandle,PWM_Number_4);
    #else
      PIE_enablePwmInt(obj->pieHandle,PWM_Number_1);
    #endif
      
    
    
      // enable the interrupt
      PWM_enableInt(obj->pwmHandle[0]);
    
    
      // enable the cpu interrupt for EPWMx_INT
      CPU_enableInt(obj->cpuHandle,CPU_IntNumber_3);
    
      return;
    } // end of HAL_enablePwmInt() function
    
    
    void HAL_enableTimer0Int(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      PIE_enableTimer0Int(obj->pieHandle);
    
    
      // enable the interrupt
      TIMER_enableInt(obj->timerHandle[0]);
    
    
      // enable the cpu interrupt for TINT0
      CPU_enableInt(obj->cpuHandle,CPU_IntNumber_1);
    
      return;
    } // end of HAL_enablePwmInt() function
    
    
    void HAL_setupFaults(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint_least8_t cnt;
    
    
      // Configure Trip Mechanism for the Motor control software
      // -Cycle by cycle trip on CPU halt
      // -One shot fault trip zone
      // These trips need to be repeated for EPWM1 ,2 & 3
      for(cnt=0;cnt<3;cnt++)
        {
          PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ6_NOT);
    
          PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ3_NOT);
    
          PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ2_NOT);
    
          // What do we want the OST/CBC events to do?
          // TZA events can force EPWMxA
          // TZB events can force EPWMxB
    
          PWM_setTripZoneState_TZA(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
          PWM_setTripZoneState_TZB(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
        }
    
      return;
    } // end of HAL_setupFaults() function
    
    
    HAL_Handle HAL_init(void *pMemory,const size_t numBytes)
    {
      uint_least8_t cnt;
      HAL_Handle handle;
      HAL_Obj *obj;
    
    
      if(numBytes < sizeof(HAL_Obj))
        return((HAL_Handle)NULL);
    
    
      // assign the handle
      handle = (HAL_Handle)pMemory;
    
    
      // assign the object
      obj = (HAL_Obj *)handle;
    
    
      // initialize the watchdog driver
      obj->wdogHandle = WDOG_init((void *)WDOG_BASE_ADDR,sizeof(WDOG_Obj));
    
    
      // disable watchdog
      HAL_disableWdog(handle);
    
    
      // initialize the ADC
      obj->adcHandle = ADC_init((void *)ADC_BASE_ADDR,sizeof(ADC_Obj));
    
    
      // initialize the clock handle
      obj->clkHandle = CLK_init((void *)CLK_BASE_ADDR,sizeof(CLK_Obj));
    
    
      // initialize the CPU handle
      obj->cpuHandle = CPU_init(&cpu,sizeof(cpu));
    
    
      // initialize the FLASH handle
      obj->flashHandle = FLASH_init((void *)FLASH_BASE_ADDR,sizeof(FLASH_Obj));
    
    
      // initialize the GPIO handle
      obj->gpioHandle = GPIO_init((void *)GPIO_BASE_ADDR,sizeof(GPIO_Obj));
    
    
      // initialize the current offset estimator handles
      for(cnt=0;cnt<USER_NUM_CURRENT_SENSORS;cnt++)
        {
          obj->offsetHandle_I[cnt] = OFFSET_init(&obj->offset_I[cnt],sizeof(obj->offset_I[cnt]));
        }
    
    
      // initialize the voltage offset estimator handles
      for(cnt=0;cnt<USER_NUM_VOLTAGE_SENSORS;cnt++)
        {
          obj->offsetHandle_V[cnt] = OFFSET_init(&obj->offset_V[cnt],sizeof(obj->offset_V[cnt]));
        }
    
    
      // initialize the oscillator handle
      obj->oscHandle = OSC_init((void *)OSC_BASE_ADDR,sizeof(OSC_Obj));
    
    
      // initialize the PIE handle
      obj->pieHandle = PIE_init((void *)PIE_BASE_ADDR,sizeof(PIE_Obj));
    
    
      // initialize the PLL handle
      obj->pllHandle = PLL_init((void *)PLL_BASE_ADDR,sizeof(PLL_Obj));
    
    
      // initialize the SPI handles
      obj->spiAHandle = SPI_init((void *)SPIA_BASE_ADDR,sizeof(SPI_Obj));
      obj->spiBHandle = SPI_init((void *)SPIB_BASE_ADDR,sizeof(SPI_Obj));
    
    
      // initialize PWM handles
    #ifdef J5
      obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM4_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM5_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM6_BASE_ADDR,sizeof(PWM_Obj));
    #else
      obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM1_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM2_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM3_BASE_ADDR,sizeof(PWM_Obj));
    #endif
    
    
      // initialize PWM DAC handles
      obj->pwmDacHandle[0] = PWMDAC_init((void *)PWM_ePWM7_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmDacHandle[1] = PWMDAC_init((void *)PWM_ePWM8_BASE_ADDR,sizeof(PWM_Obj));
    
    
      // initialize power handle
      obj->pwrHandle = PWR_init((void *)PWR_BASE_ADDR,sizeof(PWR_Obj));
    
    
      // initialize timer handles
      obj->timerHandle[0] = TIMER_init((void *)TIMER0_BASE_ADDR,sizeof(TIMER_Obj));
      obj->timerHandle[1] = TIMER_init((void *)TIMER1_BASE_ADDR,sizeof(TIMER_Obj));
      obj->timerHandle[2] = TIMER_init((void *)TIMER2_BASE_ADDR,sizeof(TIMER_Obj));
    
    
      // initialize drv8301 interface
      //obj->drv8301Handle = DRV8301_init(&obj->drv8301,sizeof(obj->drv8301));
    
      //initialize the Arduino Interface
      obj->ArduinoHandle = Arduino_init(&obj->Arduino,sizeof(obj->Arduino));
    
    
    #ifdef QEP
      // initialize QEP driver
      obj->qepHandle[0] = QEP_init((void*)QEP1_BASE_ADDR,sizeof(QEP_Obj));
      obj->qepHandle[1] = QEP_init((void*)QEP2_BASE_ADDR,sizeof(QEP_Obj));
    #endif
    
      return(handle);
    } // end of HAL_init() function
    
    
    void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams)
    {
      uint_least8_t cnt;
      HAL_Obj *obj = (HAL_Obj *)handle;
      _iq beta_lp_pu = _IQ(pUserParams->offsetPole_rps/(float_t)pUserParams->ctrlFreq_Hz);
    
    
      HAL_setNumCurrentSensors(handle,pUserParams->numCurrentSensors);
      HAL_setNumVoltageSensors(handle,pUserParams->numVoltageSensors);
    
    
      for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
        {
          HAL_setOffsetBeta_lp_pu(handle,HAL_SensorType_Current,cnt,beta_lp_pu);
          HAL_setOffsetInitCond(handle,HAL_SensorType_Current,cnt,_IQ(0.0));
          HAL_setOffsetValue(handle,HAL_SensorType_Current,cnt,_IQ(0.0));
        }
    
    
      for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
        {
          HAL_setOffsetBeta_lp_pu(handle,HAL_SensorType_Voltage,cnt,beta_lp_pu);
          HAL_setOffsetInitCond(handle,HAL_SensorType_Voltage,cnt,_IQ(0.0));
          HAL_setOffsetValue(handle,HAL_SensorType_Voltage,cnt,_IQ(0.0));
        }
    
    
      // disable global interrupts
      CPU_disableGlobalInts(obj->cpuHandle);
    
    
      // disable cpu interrupts
      CPU_disableInts(obj->cpuHandle);
    
    
      // clear cpu interrupt flags
      CPU_clearIntFlags(obj->cpuHandle);
    
    
      // setup the clocks
      HAL_setupClks(handle);
    
    
      // Setup the PLL
      HAL_setupPll(handle,PLL_ClkFreq_90_MHz);
    
    
      // setup the PIE
      HAL_setupPie(handle);
    
    
      // run the device calibration
      HAL_cal(handle);
    
    
      // setup the peripheral clocks
      HAL_setupPeripheralClks(handle);
    
    
      // setup the GPIOs
      HAL_setupGpios(handle);
    
    
      // setup the flash
      HAL_setupFlash(handle);
    
    
      // setup the ADCs
      HAL_setupAdcs(handle);
    
    
      // setup the PWMs
      HAL_setupPwms(handle,
                    (float_t)pUserParams->systemFreq_MHz,
                    pUserParams->pwmPeriod_usec,
                    USER_NUM_PWM_TICKS_PER_ISR_TICK);
    
    #ifdef QEP
      // setup the QEP
      HAL_setupQEP(handle,HAL_Qep_QEP1);
      HAL_setupQEP(handle,HAL_Qep_QEP2);
    #endif
    
      // setup the spiA
      HAL_setupSpiA(handle);
    
    
      // setup the spiB
      HAL_setupSpiB(handle);
    
    
      // setup the PWM DACs
      HAL_setupPwmDacs(handle);
    
    
      // setup the timers
      HAL_setupTimers(handle,
                      (float_t)pUserParams->systemFreq_MHz);
    
    
      // setup the drv8301/Arduino interface
      HAL_setupGate(handle);
    
    
      // set the default current bias
     {
       uint_least8_t cnt;
       _iq bias = _IQ12mpy(ADC_dataBias,_IQ(pUserParams->current_sf));
       
       for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
         {
           HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
         }
     }
    
    
      //  set the current scale factor
     {
       _iq current_sf = _IQ(pUserParams->current_sf);
    
      HAL_setCurrentScaleFactor(handle,current_sf);
     }
    
    
      // set the default voltage bias
     {
       uint_least8_t cnt;
       _iq bias = _IQ(0.0);
       
       for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
         {
           HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
         }
     }
    
    
      //  set the voltage scale factor
     {
       _iq voltage_sf = _IQ(pUserParams->voltage_sf);
    
      HAL_setVoltageScaleFactor(handle,voltage_sf);
     }
    
     return;
    } // end of HAL_setParams() function
    
    
    void HAL_setupAdcs(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // disable the ADCs
      ADC_disable(obj->adcHandle);
    
    
      // power up the bandgap circuit
      ADC_enableBandGap(obj->adcHandle);
    
    
      // set the ADC voltage reference source to internal 
      ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
    
    
      // enable the ADC reference buffers
      ADC_enableRefBuffers(obj->adcHandle);
    
    
      // Set main clock scaling factor (max45MHz clock for the ADC module)
      ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
    
    
      // power up the ADCs
      ADC_powerUp(obj->adcHandle);
    
    
      // enable the ADCs
      ADC_enable(obj->adcHandle);
    
    
      // set the ADC interrupt pulse generation to prior
      ADC_setIntPulseGenMode(obj->adcHandle,ADC_IntPulseGenMode_Prior);
    
    
      // set the temperature sensor source to external
      ADC_setTempSensorSrc(obj->adcHandle,ADC_TempSensorSrc_Ext);
    
    
      // configure the interrupt sources
      ADC_disableInt(obj->adcHandle,ADC_IntNumber_1);
      ADC_setIntMode(obj->adcHandle,ADC_IntNumber_1,ADC_IntMode_ClearFlag);
      ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_1,ADC_IntSrc_EOC7);
    
    #ifdef J5
      //configure the SOCs for boostxldrv8301_revB on J5 Connection
      // EXT IA-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A3);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
    
      // EXT IA-FB
      // Duplicate conversion due to ADC Initial Conversion bug (SPRZ342)
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A3);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
    
      // EXT IB-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B3);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
    
      // EXT IC-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A4);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb1
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B4);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb2
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A5);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb3
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B5);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
    
      // VDCBUS
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_B7);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM4_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
    #else
      //configure the SOCs for boostxldrv8301_revB on J1 Connection
      // EXT IA-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A0);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
    
      // EXT IA-FB
      // Duplicate conversion due to ADC Initial Conversion bug (SPRZ342)
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A0);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
    
      // EXT IB-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B0);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
    
      // EXT IC-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A1);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb1
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B1);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb2
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A2);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb3
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B2);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
    
      // VDCBUS
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_A7);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
    #endif
      return;
    } // end of HAL_setupAdcs() function
    
    
    void HAL_setupClks(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // enable internal oscillator 1
      CLK_enableOsc1(obj->clkHandle);
    
      // set the oscillator source
      CLK_setOscSrc(obj->clkHandle,CLK_OscSrc_Internal);
    
      // disable the external clock in
      CLK_disableClkIn(obj->clkHandle);
    
      // disable the crystal oscillator
      CLK_disableCrystalOsc(obj->clkHandle);
    
      // disable oscillator 2
      CLK_disableOsc2(obj->clkHandle);
    
      // set the low speed clock prescaler
      CLK_setLowSpdPreScaler(obj->clkHandle,CLK_LowSpdPreScaler_SysClkOut_by_1);
    
      // set the clock out prescaler
      CLK_setClkOutPreScaler(obj->clkHandle,CLK_ClkOutPreScaler_SysClkOut_by_1);
    
      return;
    } // end of HAL_setupClks() function
    
    
    void HAL_setupFlash(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      FLASH_enablePipelineMode(obj->flashHandle);
    
      FLASH_setNumPagedReadWaitStates(obj->flashHandle,FLASH_NumPagedWaitStates_3);
    
      FLASH_setNumRandomReadWaitStates(obj->flashHandle,FLASH_NumRandomWaitStates_3);
    
      FLASH_setOtpWaitStates(obj->flashHandle,FLASH_NumOtpWaitStates_5);
    
      FLASH_setStandbyWaitCount(obj->flashHandle,FLASH_STANDBY_WAIT_COUNT_DEFAULT);
    
      FLASH_setActiveWaitCount(obj->flashHandle,FLASH_ACTIVE_WAIT_COUNT_DEFAULT);
    
      return;
    } // HAL_setupFlash() function
    
    
    /*
    void HAL_setupGate(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      
      DRV8301_setGpioHandle(obj->drv8301Handle,obj->gpioHandle);
    #ifdef J5
      DRV8301_setSpiHandle(obj->drv8301Handle,obj->spiBHandle);
      DRV8301_setGpioNumber(obj->drv8301Handle,GPIO_Number_52);
    #else
      DRV8301_setSpiHandle(obj->drv8301Handle,obj->spiAHandle);
      DRV8301_setGpioNumber(obj->drv8301Handle,GPIO_Number_50);
    #endif
    
      return;
    } // HAL_setupGate() function
    */
    
    void HAL_setupGate(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
      Arduino_setGpioHandle(obj->ArduinoHandle,obj->gpioHandle);
      Arduino_setSpiHandle(obj->ArduinoHandle,obj->spiAHandle);
      Arduino_setGpioNumber(obj->ArduinoHandle,GPIO_Number_50);
    
      return;
    } // HAL_setupGate() function
    
    
    
    
    void HAL_setupGpios(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // PWM1
      GPIO_setMode(obj->gpioHandle,GPIO_Number_0,GPIO_0_Mode_EPWM1A);
    
      // PWM2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_1,GPIO_1_Mode_EPWM1B);
    
      // PWM3
      GPIO_setMode(obj->gpioHandle,GPIO_Number_2,GPIO_2_Mode_EPWM2A);
    
      // PWM4
      GPIO_setMode(obj->gpioHandle,GPIO_Number_3,GPIO_3_Mode_EPWM2B);
    
      // PWM5
      GPIO_setMode(obj->gpioHandle,GPIO_Number_4,GPIO_4_Mode_EPWM3A);
    
      // PWM6
      GPIO_setMode(obj->gpioHandle,GPIO_Number_5,GPIO_5_Mode_EPWM3B);
    
      // PWM4A
      GPIO_setMode(obj->gpioHandle,GPIO_Number_6,GPIO_6_Mode_EPWM4A);
    
      // PWM4B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_7,GPIO_7_Mode_EPWM4B);
    
      // PWM5A
      GPIO_setMode(obj->gpioHandle,GPIO_Number_8,GPIO_8_Mode_EPWM5A);
    
      // PWM5B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_9,GPIO_9_Mode_EPWM5B);
    
      // PWM6A
      GPIO_setMode(obj->gpioHandle,GPIO_Number_10,GPIO_10_Mode_EPWM6A);
    
      // PWM6B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_11,GPIO_11_Mode_EPWM6B);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_12,GPIO_12_Mode_GeneralPurpose);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_13,GPIO_13_Mode_GeneralPurpose);
    
      // SPIB CLK
      GPIO_setMode(obj->gpioHandle,GPIO_Number_14,GPIO_14_Mode_SPICLKB);
    
      // UARTB RX
      GPIO_setMode(obj->gpioHandle,GPIO_Number_15,GPIO_15_Mode_SCIRXDB);
    
      // Set Qualification Period for GPIO16-23, 22*2*(1/90MHz) = 0.48us
      GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_16,22);
    
      // SPIA SIMO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);
    
      // SPIA SOMI
      GPIO_setMode(obj->gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);
    
      // SPIA CLK
      GPIO_setMode(obj->gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);
    
      // SPIA CS
      GPIO_setMode(obj->gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);
      
    #ifdef QEP
      // EQEP1A
      GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_EQEP1A);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_20,GPIO_Qual_Sample_3);
    
      // EQEP1B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_EQEP1B);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_21,GPIO_Qual_Sample_3);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
    
      // EQEP1I
      GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_EQEP1I);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_23,GPIO_Qual_Sample_3);
    #else
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_GeneralPurpose);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_GeneralPurpose);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_GeneralPurpose);
    #endif
    
      // SPIB SIMO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_24,GPIO_24_Mode_SPISIMOB);
    
      // SPIB SOMI
      GPIO_setMode(obj->gpioHandle,GPIO_Number_25,GPIO_25_Mode_SPISOMIB);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_26,GPIO_26_Mode_GeneralPurpose);
    
      // SPIB CS
      GPIO_setMode(obj->gpioHandle,GPIO_Number_27,GPIO_27_Mode_SPISTEB_NOT);
    
      // OCTWn
      GPIO_setMode(obj->gpioHandle,GPIO_Number_28,GPIO_28_Mode_TZ2_NOT);
    
      // FAULTn
      GPIO_setMode(obj->gpioHandle,GPIO_Number_29,GPIO_29_Mode_TZ3_NOT);
    
      // CAN RX
      GPIO_setMode(obj->gpioHandle,GPIO_Number_30,GPIO_30_Mode_CANRXA);
    
      // CAN TX
      GPIO_setMode(obj->gpioHandle,GPIO_Number_31,GPIO_31_Mode_CANTXA);
    
      // I2C Data
      GPIO_setMode(obj->gpioHandle,GPIO_Number_32,GPIO_32_Mode_SDAA);
    
      // I2C Clock
      GPIO_setMode(obj->gpioHandle,GPIO_Number_33,GPIO_33_Mode_SCLA);
    
      // LED D9
      GPIO_setMode(obj->gpioHandle,GPIO_Number_34,GPIO_34_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_34);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_34,GPIO_Direction_Output);
    
      // JTAG
      GPIO_setMode(obj->gpioHandle,GPIO_Number_35,GPIO_35_Mode_JTAG_TDI);
      GPIO_setMode(obj->gpioHandle,GPIO_Number_36,GPIO_36_Mode_JTAG_TMS);
      GPIO_setMode(obj->gpioHandle,GPIO_Number_37,GPIO_37_Mode_JTAG_TDO);
      GPIO_setMode(obj->gpioHandle,GPIO_Number_38,GPIO_38_Mode_JTAG_TCK);
    
      // LED D10
      GPIO_setMode(obj->gpioHandle,GPIO_Number_39,GPIO_39_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_39);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_39,GPIO_Direction_Output);
    
      // DAC1
      GPIO_setMode(obj->gpioHandle,GPIO_Number_40,GPIO_40_Mode_EPWM7A);
    
      // DAC2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_41,GPIO_41_Mode_EPWM7B);
    
      // DAC3
      GPIO_setMode(obj->gpioHandle,GPIO_Number_42,GPIO_42_Mode_EPWM8A);
    
      // DAC4
      GPIO_setMode(obj->gpioHandle,GPIO_Number_43,GPIO_43_Mode_EPWM8B);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_44,GPIO_44_Mode_GeneralPurpose);
    
      // Set Qualification Period for GPIO50-55, 22*2*(1/90MHz) = 0.48us
      GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_50,22);
    
      // DRV8301 Enable Gate
      GPIO_setMode(obj->gpioHandle,GPIO_Number_50,GPIO_50_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_50);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_50,GPIO_Direction_Output);
    
      // DRV8301 DC Calibration
      GPIO_setMode(obj->gpioHandle,GPIO_Number_51,GPIO_51_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_51);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_51,GPIO_Direction_Output);
    
      // DRV8301 Enable Gate
      GPIO_setMode(obj->gpioHandle,GPIO_Number_52,GPIO_52_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_52);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_52,GPIO_Direction_Output);
    
      // DRV8301 Device Calibration
      GPIO_setMode(obj->gpioHandle,GPIO_Number_53,GPIO_53_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_53);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_53,GPIO_Direction_Output);
      
      // Set Qualification Period for GPIO56-58, 22*2*(1/90MHz) = 0.48us
      GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_56,22);
      
    #ifdef QEP
      // EQEP2A
      GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_EQEP2A);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_54,GPIO_Qual_Sample_3);
    
      // EQEP2B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_EQEP2B);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_55,GPIO_Qual_Sample_3);
    
      // EQEP2I
      GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_EQEP2I);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_56,GPIO_Qual_Sample_3);
    #else
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_GeneralPurpose);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_GeneralPurpose);
    
      // GPIO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_GeneralPurpose);
    #endif
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_57,GPIO_57_Mode_GeneralPurpose);
    
      // UARTB TX
      GPIO_setMode(obj->gpioHandle,GPIO_Number_58,GPIO_58_Mode_SCITXDB);
    
      return;
    }  // end of HAL_setupGpios() function
    
    
    void HAL_setupPie(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      PIE_disable(obj->pieHandle);
    
      PIE_disableAllInts(obj->pieHandle);
    
      PIE_clearAllInts(obj->pieHandle);
    
      PIE_clearAllFlags(obj->pieHandle);
    
      PIE_setDefaultIntVectorTable(obj->pieHandle);
    
      PIE_enable(obj->pieHandle);
    
      return;
    } // end of HAL_setupPie() function
    
    
    void HAL_setupPeripheralClks(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CLK_enableAdcClock(obj->clkHandle);
    
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_1);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_2);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_3);
    
      CLK_disableEcap1Clock(obj->clkHandle);
    
      CLK_enableEcanaClock(obj->clkHandle);
    
    #ifdef QEP
      CLK_enableEqep1Clock(obj->clkHandle);
      CLK_enableEqep2Clock(obj->clkHandle);
    #endif
    
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_1);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_2);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_3);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_4);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_5);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_6);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_7);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_8);
    
      CLK_disableHrPwmClock(obj->clkHandle);
    
      CLK_enableI2cClock(obj->clkHandle);
    
      CLK_disableLinAClock(obj->clkHandle);
    
      CLK_disableClaClock(obj->clkHandle);
    
      CLK_disableSciaClock(obj->clkHandle);
      CLK_enableScibClock(obj->clkHandle);
    
      CLK_enableSpiaClock(obj->clkHandle);
      CLK_enableSpibClock(obj->clkHandle);
      
      CLK_enableTbClockSync(obj->clkHandle);
    
      return;
    } // end of HAL_setupPeripheralClks() function
    
    
    void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // make sure PLL is not running in limp mode
      if(PLL_getClkStatus(obj->pllHandle) != PLL_ClkStatus_Normal)
        {
          // reset the clock detect
          PLL_resetClkDetect(obj->pllHandle);
    
          // ???????
          asm("        ESTOP0");
        }
    
    
      // Divide Select must be ClkIn/4 before the clock rate can be changed
      if(PLL_getDivideSelect(obj->pllHandle) != PLL_DivideSelect_ClkIn_by_4)
        {
          PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_4);
        }
    
    
      if(PLL_getClkFreq(obj->pllHandle) != clkFreq)
        {
          // disable the clock detect
          PLL_disableClkDetect(obj->pllHandle);
    
          // set the clock rate
          PLL_setClkFreq(obj->pllHandle,clkFreq);
        }
    
    
      // wait until locked
      while(PLL_getLockStatus(obj->pllHandle) != PLL_LockStatus_Done) {}
    
    
      // enable the clock detect
      PLL_enableClkDetect(obj->pllHandle);
    
    
      // set divide select to ClkIn/2 to get desired clock rate
      // NOTE: clock must be locked before setting this register
      PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_2);
    
      return;
    } // end of HAL_setupPll() function
    
    
    void HAL_setupPwms(HAL_Handle handle,
                       const float_t systemFreq_MHz,
                       const float_t pwmPeriod_usec,
                       const uint_least16_t numPwmTicksPerIsrTick)
    {
      HAL_Obj   *obj = (HAL_Obj *)handle;
      uint16_t   halfPeriod_cycles = (uint16_t)(systemFreq_MHz*pwmPeriod_usec) >> 1;
      uint_least8_t    cnt;
    
    
      // turns off the outputs of the EPWM peripherals which will put the power switches
      // into a high impedance state.
      PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_1]);
      PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_2]);
      PWM_setOneShotTrip(obj->pwmHandle[PWM_Number_3]);
    
      for(cnt=0;cnt<3;cnt++)
        {
          // setup the Time-Base Control Register (TBCTL)
          PWM_setCounterMode(obj->pwmHandle[cnt],PWM_CounterMode_UpDown);
          PWM_disableCounterLoad(obj->pwmHandle[cnt]);
          PWM_setPeriodLoad(obj->pwmHandle[cnt],PWM_PeriodLoad_Immediate);
          PWM_setSyncMode(obj->pwmHandle[cnt],PWM_SyncMode_EPWMxSYNC);
          PWM_setHighSpeedClkDiv(obj->pwmHandle[cnt],PWM_HspClkDiv_by_1);
          PWM_setClkDiv(obj->pwmHandle[cnt],PWM_ClkDiv_by_1);
          PWM_setPhaseDir(obj->pwmHandle[cnt],PWM_PhaseDir_CountUp);
          PWM_setRunMode(obj->pwmHandle[cnt],PWM_RunMode_FreeRun);
    
          // setup the Timer-Based Phase Register (TBPHS)
          PWM_setPhase(obj->pwmHandle[cnt],0);
    
          // setup the Time-Base Counter Register (TBCTR)
          PWM_setCount(obj->pwmHandle[cnt],0);
    
          // setup the Time-Base Period Register (TBPRD)
          // set to zero initially
          PWM_setPeriod(obj->pwmHandle[cnt],0);
    
          // setup the Counter-Compare Control Register (CMPCTL)
          PWM_setLoadMode_CmpA(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
          PWM_setLoadMode_CmpB(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
          PWM_setShadowMode_CmpA(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
          PWM_setShadowMode_CmpB(obj->pwmHandle[cnt],PWM_ShadowMode_Immediate);
    
          // setup the Action-Qualifier Output A Register (AQCTLA) 
          PWM_setActionQual_CntUp_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Set);
          PWM_setActionQual_CntDown_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Clear);
    
          // setup the Dead-Band Generator Control Register (DBCTL)
          PWM_setDeadBandOutputMode(obj->pwmHandle[cnt],PWM_DeadBandOutputMode_EPWMxA_Rising_EPWMxB_Falling);
          PWM_setDeadBandPolarity(obj->pwmHandle[cnt],PWM_DeadBandPolarity_EPWMxB_Inverted);
    
          // setup the Dead-Band Rising Edge Delay Register (DBRED)
          PWM_setDeadBandRisingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBRED_CNT);
    
          // setup the Dead-Band Falling Edge Delay Register (DBFED)
          PWM_setDeadBandFallingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBFED_CNT);
          // setup the PWM-Chopper Control Register (PCCTL)
          PWM_disableChopping(obj->pwmHandle[cnt]);
    
          // setup the Trip Zone Select Register (TZSEL)
          PWM_disableTripZones(obj->pwmHandle[cnt]);
        }
    
    
      // setup the Event Trigger Selection Register (ETSEL)
      PWM_disableInt(obj->pwmHandle[PWM_Number_1]);
      PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualZero);
      PWM_enableSocAPulse(obj->pwmHandle[PWM_Number_1]);
      
    
      // setup the Event Trigger Prescale Register (ETPS)
      if(numPwmTicksPerIsrTick == 3)
        {
          PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_ThirdEvent);
          PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_ThirdEvent);
        }
      else if(numPwmTicksPerIsrTick == 2)
        {
          PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_SecondEvent);
          PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_SecondEvent);
        }
      else
        {
          PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_FirstEvent);
          PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_FirstEvent);
        }
    
    
      // setup the Event Trigger Clear Register (ETCLR)
      PWM_clearIntFlag(obj->pwmHandle[PWM_Number_1]);
      PWM_clearSocAFlag(obj->pwmHandle[PWM_Number_1]);
    
      // first step to synchronize the pwms
      CLK_disableTbClockSync(obj->clkHandle);
    
      // since the PWM is configured as an up/down counter, the period register is set to one-half 
      // of the desired PWM period
      PWM_setPeriod(obj->pwmHandle[PWM_Number_1],halfPeriod_cycles);
      PWM_setPeriod(obj->pwmHandle[PWM_Number_2],halfPeriod_cycles);
      PWM_setPeriod(obj->pwmHandle[PWM_Number_3],halfPeriod_cycles);
    
      // last step to synchronize the pwms
      CLK_enableTbClockSync(obj->clkHandle);
    
      return;
    }  // end of HAL_setupPwms() function
    
    #ifdef QEP
    void HAL_setupQEP(HAL_Handle handle,HAL_QepSelect_e qep)
    {
      HAL_Obj   *obj = (HAL_Obj *)handle;
    
    
      // hold the counter in reset
      QEP_reset_counter(obj->qepHandle[qep]);
    
      // set the QPOSINIT register
      QEP_set_posn_init_count(obj->qepHandle[qep], 0);
    
      // disable all interrupts
      QEP_disable_all_interrupts(obj->qepHandle[qep]);
    
      // clear the interrupt flags
      QEP_clear_all_interrupt_flags(obj->qepHandle[qep]);
    
      // clear the position counter
      QEP_clear_posn_counter(obj->qepHandle[qep]);
    
      // setup the max position
      QEP_set_max_posn_count(obj->qepHandle[qep], (4*USER_MOTOR_ENCODER_LINES)-1);
    
      // setup the QDECCTL register
      QEP_set_QEP_source(obj->qepHandle[qep], QEP_Qsrc_Quad_Count_Mode);
      QEP_disable_sync_out(obj->qepHandle[qep]);
      QEP_set_swap_quad_inputs(obj->qepHandle[qep], QEP_Swap_Not_Swapped);
      QEP_disable_gate_index(obj->qepHandle[qep]);
      QEP_set_ext_clock_rate(obj->qepHandle[qep], QEP_Xcr_2x_Res);
      QEP_set_A_polarity(obj->qepHandle[qep], QEP_Qap_No_Effect);
      QEP_set_B_polarity(obj->qepHandle[qep], QEP_Qbp_No_Effect);
      QEP_set_index_polarity(obj->qepHandle[qep], QEP_Qip_No_Effect);
    
      // setup the QEPCTL register
      QEP_set_emu_control(obj->qepHandle[qep], QEPCTL_Freesoft_Unaffected_Halt);
      QEP_set_posn_count_reset_mode(obj->qepHandle[qep], QEPCTL_Pcrm_Max_Reset);
      QEP_set_strobe_event_init(obj->qepHandle[qep], QEPCTL_Sei_Nothing);
      QEP_set_index_event_init(obj->qepHandle[qep], QEPCTL_Iei_Nothing);
      QEP_set_index_event_latch(obj->qepHandle[qep], QEPCTL_Iel_Rising_Edge);
      QEP_set_soft_init(obj->qepHandle[qep], QEPCTL_Swi_Nothing);
      QEP_disable_unit_timer(obj->qepHandle[qep]);
      QEP_disable_watchdog(obj->qepHandle[qep]);
    
      // setup the QPOSCTL register
      QEP_disable_posn_compare(obj->qepHandle[qep]);
    
      // setup the QCAPCTL register
      QEP_disable_capture(obj->qepHandle[qep]);
    
      // renable the position counter
      QEP_enable_counter(obj->qepHandle[qep]);
    
    
      return;
    }
    #endif
    
    void HAL_setupSpiA(HAL_Handle handle)
    {
      HAL_Obj   *obj = (HAL_Obj *)handle;
    
      SPI_reset(obj->spiAHandle);
      SPI_setMode(obj->spiAHandle,SPI_Mode_Slave);
      SPI_setClkPolarity(obj->spiAHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
      SPI_enableTx(obj->spiAHandle);
      SPI_enableTxFifoEnh(obj->spiAHandle);
      SPI_enableTxFifo(obj->spiAHandle);
      SPI_setTxDelay(obj->spiAHandle,0x0018);
      //SPI_setBaudRate(obj->spiAHandle,(SPI_BaudRate_e)(0x000d));
      SPI_setCharLength(obj->spiAHandle,SPI_CharLength_16_Bits);
      SPI_setSuspend(obj->spiAHandle,SPI_TxSuspend_free);
      SPI_enable(obj->spiAHandle);
    
      return;
    }  // end of HAL_setupSpiA() function
    
    
    void HAL_setupSpiB(HAL_Handle handle)
    {
      HAL_Obj   *obj = (HAL_Obj *)handle;
    
      SPI_reset(obj->spiBHandle);
      SPI_setMode(obj->spiBHandle,SPI_Mode_Master);
      SPI_setClkPolarity(obj->spiBHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
      SPI_enableTx(obj->spiBHandle);
      SPI_enableTxFifoEnh(obj->spiBHandle);
      SPI_enableTxFifo(obj->spiBHandle);
      SPI_setTxDelay(obj->spiBHandle,0x0018);
      SPI_setBaudRate(obj->spiBHandle,(SPI_BaudRate_e)(0x000d));
      SPI_setCharLength(obj->spiBHandle,SPI_CharLength_16_Bits);
      SPI_setSuspend(obj->spiBHandle,SPI_TxSuspend_free);
      SPI_enable(obj->spiBHandle);
    
      return;
    }  // end of HAL_setupSpiB() function
    
    
    void HAL_setupPwmDacs(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t halfPeriod_cycles = 512;       // 3000->10kHz, 1500->20kHz, 1000-> 30kHz, 500->60kHz
      uint_least8_t    cnt;
    
    
      for(cnt=0;cnt<2;cnt++)
        {
          // initialize the Time-Base Control Register (TBCTL)
          PWMDAC_setCounterMode(obj->pwmDacHandle[cnt],PWM_CounterMode_UpDown);
          PWMDAC_disableCounterLoad(obj->pwmDacHandle[cnt]);
          PWMDAC_setPeriodLoad(obj->pwmDacHandle[cnt],PWM_PeriodLoad_Immediate);
          PWMDAC_setSyncMode(obj->pwmDacHandle[cnt],PWM_SyncMode_EPWMxSYNC);
          PWMDAC_setHighSpeedClkDiv(obj->pwmDacHandle[cnt],PWM_HspClkDiv_by_1);
          PWMDAC_setClkDiv(obj->pwmDacHandle[cnt],PWM_ClkDiv_by_1);
          PWMDAC_setPhaseDir(obj->pwmDacHandle[cnt],PWM_PhaseDir_CountUp);
          PWMDAC_setRunMode(obj->pwmDacHandle[cnt],PWM_RunMode_FreeRun);
    
          // initialize the Timer-Based Phase Register (TBPHS)
          PWMDAC_setPhase(obj->pwmDacHandle[cnt],0);
    
          // setup the Time-Base Counter Register (TBCTR)
          PWMDAC_setCount(obj->pwmDacHandle[cnt],0);
    
          // Initialize the Time-Base Period Register (TBPRD)
          // set to zero initially
          PWMDAC_setPeriod(obj->pwmDacHandle[cnt],0);
    
          // initialize the Counter-Compare Control Register (CMPCTL)
          PWMDAC_setLoadMode_CmpA(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
          PWMDAC_setLoadMode_CmpB(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
          PWMDAC_setShadowMode_CmpA(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
          PWMDAC_setShadowMode_CmpB(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
    
          // Initialize the Action-Qualifier Output A Register (AQCTLA) 
          PWMDAC_setActionQual_CntUp_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
          PWMDAC_setActionQual_CntDown_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
          PWMDAC_setActionQual_CntUp_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
          PWMDAC_setActionQual_CntDown_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
    
          // Initialize the Dead-Band Control Register (DBCTL) 
          PWMDAC_disableDeadBand(obj->pwmDacHandle[cnt]);
    
          // Initialize the PWM-Chopper Control Register (PCCTL)
          PWMDAC_disableChopping(obj->pwmDacHandle[cnt]);
    
          // Initialize the Trip-Zone Control Register (TZSEL)
          PWMDAC_disableTripZones(obj->pwmDacHandle[cnt]);
    
          // Initialize the Trip-Zone Control Register (TZCTL)
          PWMDAC_setTripZoneState_TZA(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_TZB(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_DCAEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_DCAEVT2(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_DCBEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
        }
    
      // since the PWM is configured as an up/down counter, the period register is set to one-half 
      // of the desired PWM period
      PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_1],halfPeriod_cycles);
      PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_2],halfPeriod_cycles);
    
      return;
    }  // end of HAL_setupPwmDacs() function
    
    
    void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
      uint32_t  timerPeriod_cnts = (uint32_t)(systemFreq_MHz * (float_t)1000000.0) - 1;
    
      // use timer 0 for frequency diagnostics
      TIMER_setDecimationFactor(obj->timerHandle[0],0);
      TIMER_setEmulationMode(obj->timerHandle[0],TIMER_EmulationMode_RunFree);
      TIMER_setPeriod(obj->timerHandle[0],timerPeriod_cnts);
      TIMER_setPreScaler(obj->timerHandle[0],0);
    
      // use timer 1 for CPU usage diagnostics
      TIMER_setDecimationFactor(obj->timerHandle[1],0);
      TIMER_setEmulationMode(obj->timerHandle[1],TIMER_EmulationMode_RunFree);
      TIMER_setPeriod(obj->timerHandle[1],timerPeriod_cnts);
      TIMER_setPreScaler(obj->timerHandle[1],0);
    
      return;
    }  // end of HAL_setupTimers() function
    
    /*void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_writeData(obj->drv8301Handle,Spi_8301_Vars);
    
      return;
    }  // end of HAL_writeDrvData() function
    
    void HAL_writeDrvData(HAL_Handle handle, SPI_Arduino_Vars_t *Spi_Arduino_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_writeData(obj->ArduinoHandle,Spi_Arduino_Vars);
      
      return;
    }  // end of HAL_writeDrvData() function
    
    void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_readData(obj->drv8301Handle,Spi_8301_Vars);
      
      return;
    }  // end of HAL_readDrvData() function
    
    
    void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_setupSpi(obj->drv8301Handle,Spi_8301_Vars);
    
      return;
    }  // end of HAL_setupDrvSpi() function*/
    
    uint16_t HAL_readArduinoData(HAL_Handle handle)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
      uint16_t dat = 0;
      dat = Arduino_readSpi(obj->ArduinoHandle);
    
      return dat;
    }  // end of HAL_readDrvData() function
    
    
    
    // end of file
    

  • Hi John,

    I am running SPIA in slave mode on the 28069 + DRV8301 using MotorWare, please compare the attached hal.c file to yours.

    Jeff

    /* --COPYRIGHT--,BSD
     * Copyright (c) 2012, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * --/COPYRIGHT--*/
    //! \file   ~/sw/modules/hal/boards/hvkit_rev1p1/f28x/f2806x/src/float/hal.c
    //! \brief  Contains the various functions related to the HAL object
    //!
    //! (C) Copyright 2014, Texas Instruments, Inc.
    
    
    // **************************************************************************
    // the includes
    
    // drivers
    
    // modules
    
    // platforms
    #include "hal.h"
    #include "user.h"
    #include "hal_obj.h"
    
    #ifdef FLASH
    #pragma CODE_SECTION(HAL_setupFlash,"ramfuncs");
    #endif
    
    // **************************************************************************
    // the defines
    
    
    // **************************************************************************
    // the globals
    
    
    // **************************************************************************
    // the functions
    
    void HAL_AdcCalChanSelect(HAL_Handle handle, const ADC_SocChanNumber_e chanNumber)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_8,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_9,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_10,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_11,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_12,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_13,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_14,chanNumber);
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_15,chanNumber);
    
      return;
    } // end of HAL_AdcCalChanSelect() function
    
    
    uint16_t HAL_AdcCalConversion(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t index, SampleSize, Mean;
      uint32_t Sum;
      ADC_SocSampleDelay_e ACQPS_Value;
    
      index       = 0;     //initialize index to 0
      SampleSize  = 256;   //set sample size to 256 (**NOTE: Sample size must be multiples of 2^x where is an integer >= 4)
      Sum         = 0;     //set sum to 0
      Mean        = 999;   //initialize mean to known value
    
      //Set the ADC sample window to the desired value (Sample window = ACQPS + 1)
      ACQPS_Value = ADC_SocSampleDelay_7_cycles;
    
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_8,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_9,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_10,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_11,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_12,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_13,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_14,ACQPS_Value);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_15,ACQPS_Value);
    
      // Enabled ADCINT1 and ADCINT2
      ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
      ADC_enableInt(obj->adcHandle, ADC_IntNumber_2);
    
      // Disable continuous sampling for ADCINT1 and ADCINT2
      ADC_setIntMode(obj->adcHandle, ADC_IntNumber_1, ADC_IntMode_EOC);
      ADC_setIntMode(obj->adcHandle, ADC_IntNumber_2, ADC_IntMode_EOC);
    
      //ADCINTs trigger at end of conversion
      ADC_setIntPulseGenMode(obj->adcHandle, ADC_IntPulseGenMode_Prior);
    
      // Setup ADCINT1 and ADCINT2 trigger source
      ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC6);
      ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_2, ADC_IntSrc_EOC14);
    
      // Setup each SOC's ADCINT trigger source
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_0, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_1, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_2, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_3, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_4, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_5, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_6, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_7, ADC_Int2TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_8, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_9, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_10, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_11, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_12, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_13, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_14, ADC_Int1TriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_15, ADC_Int1TriggersSOC);
    
      // Delay before converting ADC channels
      usDelay(ADC_DELAY_usec);
    
      ADC_setSocFrcWord(obj->adcHandle, 0x00FF);
    
      while( index < SampleSize )
        {
          //Wait for ADCINT1 to trigger, then add ADCRESULT0-7 registers to sum
          while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
    
          //Must clear ADCINT1 flag since INT1CONT = 0
          ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
    
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_1);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_2);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_3);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_4);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_5);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_6);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_7);
    
          //Wait for ADCINT2 to trigger, then add ADCRESULT8-15 registers to sum
          while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_2) == 0){}
    
          //Must clear ADCINT2 flag since INT2CONT = 0
          ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_2);
    
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_8);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_9);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_10);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_11);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_12);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_13);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_14);
          Sum += ADC_readResult(obj->adcHandle, ADC_ResultNumber_15);
    
          index+=16;
    
      } // end data collection
    
      //Disable ADCINT1 and ADCINT2 to STOP the ping-pong sampling
      ADC_disableInt(obj->adcHandle, ADC_IntNumber_1);
      ADC_disableInt(obj->adcHandle, ADC_IntNumber_2);
    
      //Calculate average ADC sample value
      Mean = Sum / SampleSize;
    
      // Clear start of conversion trigger
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_0, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_1, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_2, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_3, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_4, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_5, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_6, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_7, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_8, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_9, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_10, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_11, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_12, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_13, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_14, ADC_NoIntTriggersSOC);
      ADC_setupSocTrigSrc(obj->adcHandle, ADC_SocNumber_15, ADC_NoIntTriggersSOC);
    
      //return the average
      return(Mean);
    } // end of HAL_AdcCalConversion() function
    
    
    void HAL_AdcOffsetSelfCal(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t AdcConvMean;
    
      // disable the ADCs
      ADC_disable(obj->adcHandle);
    
      // power up the bandgap circuit
      ADC_enableBandGap(obj->adcHandle);
    
      // set the ADC voltage reference source to internal
      ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
    
      // enable the ADC reference buffers
      ADC_enableRefBuffers(obj->adcHandle);
    
      // Set main clock scaling factor (max45MHz clock for the ADC module)
      ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
    
      // power up the ADCs
      ADC_powerUp(obj->adcHandle);
    
      // enable the ADCs
      ADC_enable(obj->adcHandle);
    
      //Select VREFLO internal connection on B5
      ADC_enableVoltRefLoConv(obj->adcHandle);
    
      //Select channel B5 for all SOC
      HAL_AdcCalChanSelect(handle, ADC_SocChanNumber_B5);
    
      //Apply artificial offset (+80) to account for a negative offset that may reside in the ADC core
      ADC_setOffTrim(obj->adcHandle, 80);
    
      //Capture ADC conversion on VREFLO
      AdcConvMean = HAL_AdcCalConversion(handle);
    
      //Set offtrim register with new value (i.e remove artical offset (+80) and create a two's compliment of the offset error)
      ADC_setOffTrim(obj->adcHandle, 80 - AdcConvMean);
    
      //Select external ADCIN5 input pin on B5
      ADC_disableVoltRefLoConv(obj->adcHandle);
    
      return;
    } // end of HAL_AdcOffsetSelfCal() function
    
    
    void HAL_cal(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // enable the ADC clock
      CLK_enableAdcClock(obj->clkHandle);
    
    
      // Run the Device_cal() function
      // This function copies the ADC and oscillator calibration values from TI reserved
      // OTP into the appropriate trim registers
      // This boot ROM automatically calls this function to calibrate the interal 
      // oscillators and ADC with device specific calibration data.
      // If the boot ROM is bypassed by Code Composer Studio during the development process,
      // then the calibration must be initialized by the application
      ENABLE_PROTECTED_REGISTER_WRITE_MODE;
      (*Device_cal)();
      DISABLE_PROTECTED_REGISTER_WRITE_MODE;
    
    
      // run offsets calibration in user's memory
      HAL_AdcOffsetSelfCal(handle);
    
    
      // run oscillator compensation
      HAL_OscTempComp(handle);
    
    
      // disable the ADC clock
      CLK_disableAdcClock(obj->clkHandle);
    
      return;
    } // end of HAL_cal() function
    
    
    void HAL_disableGlobalInts(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CPU_disableGlobalInts(obj->cpuHandle);
    
      return;
    } // end of HAL_disableGlobalInts() function
    
    
    void HAL_disableWdog(HAL_Handle halHandle)
    {
      HAL_Obj *hal = (HAL_Obj *)halHandle;
    
    
      WDOG_disable(hal->wdogHandle);
    
    
      return;
    } // end of HAL_disableWdog() function
    
    
    void HAL_enableAdcInts(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // enable the PIE interrupts associated with the ADC interrupts
    //  PIE_enableAdcInt(obj->pieHandle,ADC_IntNumber_1);
    //  PIE_enableAdcInt(obj->pieHandle,ADC_IntNumber_2);
      PIE_enableAdcInt(obj->pieHandle,ADC_IntNumber_6);
    
    
      // enable the ADC interrupts
    //  ADC_enableInt(obj->adcHandle,ADC_IntNumber_1);
    //  ADC_enableInt(obj->adcHandle,ADC_IntNumber_2);
      ADC_enableInt(obj->adcHandle,ADC_IntNumber_6);
    
    
      // enable the cpu interrupt for ADC interrupts
      CPU_enableInt(obj->cpuHandle,CPU_IntNumber_10);
    
      return;
    } // end of HAL_enableAdcInts() function
    
    
    void HAL_enableDebugInt(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CPU_enableDebugInt(obj->cpuHandle);
    
      return;
    } // end of HAL_enableDebugInt() function
    
    
    void HAL_enableDrv(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
      DRV8301_enable(obj->drv8301Handle);
    
      return;
    }  // end of HAL_enableDrv() function
    
    
    void HAL_enableGlobalInts(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CPU_enableGlobalInts(obj->cpuHandle);
    
      return;
    } // end of HAL_enableGlobalInts() function
    
    
    void HAL_enablePwmInt(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      PIE_enablePwmInt(obj->pieHandle,PWM_Number_1);
    
    
      // enable the interrupt
      PWM_enableInt(obj->pwmHandle[PWM_Number_1]);
    
    
      // enable the cpu interrupt for EPWM1_INT
      CPU_enableInt(obj->cpuHandle,CPU_IntNumber_3);
    
      return;
    } // end of HAL_enablePwmInt() function
    
    
    HAL_Handle HAL_init(void *pMemory,const size_t numBytes)
    {
      HAL_Handle handle;
      HAL_Obj *obj;
    
    
      if(numBytes < sizeof(HAL_Obj))
        return((HAL_Handle)NULL);
    
    
      // assign the handle
      handle = (HAL_Handle)pMemory;
    
    
      // assign the object
      obj = (HAL_Obj *)handle;
    
    
      // initialize the watchdog driver
      obj->wdogHandle = WDOG_init((void *)WDOG_BASE_ADDR,sizeof(WDOG_Obj));
    
    
      // disable watchdog
      HAL_disableWdog(handle);
    
    
      // initialize the ADC
      obj->adcHandle = ADC_init((void *)ADC_BASE_ADDR,sizeof(ADC_Obj));
    
    
      // initialize the clock handle
      obj->clkHandle = CLK_init((void *)CLK_BASE_ADDR,sizeof(CLK_Obj));
    
    
      // initialize the CPU handle
      obj->cpuHandle = CPU_init(&cpu,sizeof(cpu));
    
    
      // initialize the FLASH handle
      obj->flashHandle = FLASH_init((void *)FLASH_BASE_ADDR,sizeof(FLASH_Obj));
    
    
      // initialize the GPIO handle
      obj->gpioHandle = GPIO_init((void *)GPIO_BASE_ADDR,sizeof(GPIO_Obj));
    
    
      // initialize the oscillator handle
      obj->oscHandle = OSC_init((void *)OSC_BASE_ADDR,sizeof(OSC_Obj));
    
    
      // initialize the PIE handle
      obj->pieHandle = PIE_init((void *)PIE_BASE_ADDR,sizeof(PIE_Obj));
    
    
      // initialize the PLL handle
      obj->pllHandle = PLL_init((void *)PLL_BASE_ADDR,sizeof(PLL_Obj));
    
    
      // initialize the SPIA handle
      obj->spiAHandle = SPI_init((void *)SPIA_BASE_ADDR,sizeof(SPI_Obj));
    
    
      // initialize the SPIB handle
      obj->spiBHandle = SPI_init((void *)SPIB_BASE_ADDR,sizeof(SPI_Obj));
    
    
      // initialize PWM handles
      obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM1_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmHandle[1] = PWM_init((void *)PWM_ePWM2_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmHandle[2] = PWM_init((void *)PWM_ePWM3_BASE_ADDR,sizeof(PWM_Obj));
    
    
      // initialize PWM DAC handles
      obj->pwmDacHandle[0] = PWMDAC_init((void *)PWM_ePWM6_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmDacHandle[1] = PWMDAC_init((void *)PWM_ePWM5_BASE_ADDR,sizeof(PWM_Obj));
      obj->pwmDacHandle[2] = PWMDAC_init((void *)PWM_ePWM4_BASE_ADDR,sizeof(PWM_Obj));
    
    
      // initialize power handle
      obj->pwrHandle = PWR_init((void *)PWR_BASE_ADDR,sizeof(PWR_Obj));
    
    
      // initialize timer handles
      obj->timerHandle[0] = TIMER_init((void *)TIMER0_BASE_ADDR,sizeof(TIMER_Obj));
      obj->timerHandle[1] = TIMER_init((void *)TIMER1_BASE_ADDR,sizeof(TIMER_Obj));
      obj->timerHandle[2] = TIMER_init((void *)TIMER2_BASE_ADDR,sizeof(TIMER_Obj));
    
    
      // initialize drv8301 interface
      obj->drv8301Handle = DRV8301_init(&obj->drv8301,sizeof(obj->drv8301));
    
    
    #ifdef QEP
      // initialize QEP driver
      obj->qepHandle[0] = QEP_init((void*)QEP1_BASE_ADDR,sizeof(QEP_Obj));
      obj->qepHandle[1] = QEP_init((void*)QEP2_BASE_ADDR,sizeof(QEP_Obj));
    #endif
    
      return(handle);
    } // end of HAL_init() function
    
    
    void HAL_OscTempComp(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint16_t Temperature;
    
    
      // disable the ADCs
      ADC_disable(obj->adcHandle);
    
      // power up the bandgap circuit
      ADC_enableBandGap(obj->adcHandle);
    
      // set the ADC voltage reference source to internal
      ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
    
      // enable the ADC reference buffers
      ADC_enableRefBuffers(obj->adcHandle);
    
      // Set main clock scaling factor (max45MHz clock for the ADC module)
      ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
    
      // power up the ADCs
      ADC_powerUp(obj->adcHandle);
    
      // enable the ADCs
      ADC_enable(obj->adcHandle);
    
      // enable non-overlap mode
      ADC_enableNoOverlapMode(obj->adcHandle);
    
      // connect channel A5 internally to the temperature sensor
      ADC_setTempSensorSrc(obj->adcHandle, ADC_TempSensorSrc_Int);
    
      // set SOC0 channel select to ADCINA5
      ADC_setSocChanNumber(obj->adcHandle, ADC_SocNumber_0, ADC_SocChanNumber_A5);
    
      // set SOC0 acquisition period to 26 ADCCLK
      ADC_setSocSampleDelay(obj->adcHandle, ADC_SocNumber_0, ADC_SocSampleDelay_64_cycles);
    
      // connect ADCINT1 to EOC0
      ADC_setIntSrc(obj->adcHandle, ADC_IntNumber_1, ADC_IntSrc_EOC0);
    
      // clear ADCINT1 flag
      ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
    
      // enable ADCINT1
      ADC_enableInt(obj->adcHandle, ADC_IntNumber_1);
    
      // force start of conversion on SOC0
      ADC_setSocFrc(obj->adcHandle, ADC_SocFrc_0);
    
      // wait for end of conversion
      while (ADC_getIntFlag(obj->adcHandle, ADC_IntNumber_1) == 0){}
    
      // clear ADCINT1 flag
      ADC_clearIntFlag(obj->adcHandle, ADC_IntNumber_1);
    
      Temperature = ADC_readResult(obj->adcHandle, ADC_ResultNumber_0);
    
      HAL_osc1Comp(handle, Temperature);
    
      HAL_osc2Comp(handle, Temperature);
    
      return;
    } // end of HAL_OscTempComp() function
    
    
    void HAL_osc1Comp(HAL_Handle handle, const int16_t sensorSample)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      int16_t compOscFineTrim;
    
    
      ENABLE_PROTECTED_REGISTER_WRITE_MODE;
    
        compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc1FineTrimSlope()
                          + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc1FineTrimOffset() - OSC_POSTRIM;
    
        if(compOscFineTrim > 31)
          {
            compOscFineTrim = 31;
          }
        else if(compOscFineTrim < -31)
          {
            compOscFineTrim = -31;
          }
    
        OSC_setTrim(obj->oscHandle, OSC_Number_1, HAL_computeOscTrimValue(getOsc1CoarseTrim(), compOscFineTrim));
    
        DISABLE_PROTECTED_REGISTER_WRITE_MODE;
    
        return;
    } // end of HAL_osc1Comp() function
    
    
    void HAL_osc2Comp(HAL_Handle handle, const int16_t sensorSample)
    {
      int16_t compOscFineTrim;
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      ENABLE_PROTECTED_REGISTER_WRITE_MODE;
    
      compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32_t)getOsc2FineTrimSlope()
                         + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc2FineTrimOffset() - OSC_POSTRIM;
    
      if(compOscFineTrim > 31)
        {
          compOscFineTrim = 31;
        }
      else if(compOscFineTrim < -31)
        {
          compOscFineTrim = -31;
        }
    
      OSC_setTrim(obj->oscHandle, OSC_Number_2, HAL_computeOscTrimValue(getOsc2CoarseTrim(), compOscFineTrim));
    
      DISABLE_PROTECTED_REGISTER_WRITE_MODE;
    
      return;
    } // end of HAL_osc2Comp() function
    
    
    uint16_t HAL_computeOscTrimValue(int16_t coarse, int16_t fine)
    {
      uint16_t regValue = 0;
    
      if(fine < 0)
        {
          regValue = ((-fine) | 0x20) << 9;
        }
      else
        {
          regValue = fine << 9;
        }
    
      if(coarse < 0)
        {
          regValue |= ((-coarse) | 0x80);
        }
      else
        {
          regValue |= coarse;
        }
    
      return(regValue);
    } // end of HAL_computeOscTrimValue() function
    
    
    void HAL_setParams(HAL_Handle handle,const USER_Params *pUserParams)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      HAL_setNumCurrentSensors(handle,pUserParams->numCurrentSensors);
      HAL_setNumVoltageSensors(handle,pUserParams->numVoltageSensors);
    
    
      // disable global interrupts
      CPU_disableGlobalInts(obj->cpuHandle);
    
    
      // disable cpu interrupts
      CPU_disableInts(obj->cpuHandle);
    
    
      // clear cpu interrupt flags
      CPU_clearIntFlags(obj->cpuHandle);
    
    
      // setup the clocks
      HAL_setupClks(handle);
    
    
      // Setup the PLL
      HAL_setupPll(handle,PLL_ClkFreq_90_MHz);
    
    
      // setup the PIE
      HAL_setupPie(handle);
    
    
      // run the device calibration
      HAL_cal(handle);
    
    
      // setup the peripheral clocks
      HAL_setupPeripheralClks(handle);
    
    
      // setup the GPIOs
      HAL_setupGpios(handle);
    
    
      // setup the flash
      HAL_setupFlash(handle);
    
    
      // setup the ADCs
      HAL_setupAdcs(handle);
    
    
      // setup the PWMs
      HAL_setupPwms(handle,
                    pUserParams->systemFreq_MHz,
                    pUserParams->pwmPeriod_usec,
                    USER_NUM_PWM_TICKS_PER_ISR_TICK);
    
    
      // setup the spiA
      HAL_setupSpiA(handle);
    
    
      // setup the spiB
      HAL_setupSpiB(handle);
    
    
      // setup the PWM DACs
      HAL_setupPwmDacs(handle,
                       pUserParams->systemFreq_MHz,
                       (float_t)60.0);
    
    
      #ifdef QEP
      // setup the QEP
      HAL_setupQEP(obj->qepHandle[0],pUserParams,1000);
      #endif
    
    
      // setup the timers
      HAL_setupTimers(handle,
                      pUserParams->systemFreq_MHz);
    
    
      // setup the drv8301 interface
      HAL_setupGate(handle);
    
    
      //  set the current scale factor
     {
       float_t current_sf = pUserParams->current_sf;
    
      HAL_setCurrentScaleFactor(handle,current_sf);
     }
    
    
      //  set the voltage scale factor
     {
       float_t voltage_sf = pUserParams->voltage_sf;
    
      HAL_setVoltageScaleFactor(handle,voltage_sf);
     }
    
    
      // set the default current bias
     {
       uint_least8_t cnt;
       float_t current_sf = HAL_getCurrentScaleFactor(handle);
       float_t bias = (float_t)ADC_dataBias * current_sf;
       
       for(cnt=0;cnt<HAL_getNumCurrentSensors(handle);cnt++)
         {
           HAL_setBias(handle,HAL_SensorType_Current,cnt,bias);
         }
     }
    
    
      // set the default voltage bias
     {
       uint_least8_t cnt;
       float_t bias = 0.0;
       
       for(cnt=0;cnt<HAL_getNumVoltageSensors(handle);cnt++)
         {
           HAL_setBias(handle,HAL_SensorType_Voltage,cnt,bias);
         }
     }
    
    
     return;
    } // end of HAL_setParams() function
    
    
    void HAL_setupAdcs(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // disable the ADCs
      ADC_disable(obj->adcHandle);
    
    
      // power up the bandgap circuit
      ADC_enableBandGap(obj->adcHandle);
    
    
      // set the ADC voltage reference source to internal 
      ADC_setVoltRefSrc(obj->adcHandle,ADC_VoltageRefSrc_Int);
    
    
      // enable the ADC reference buffers
      ADC_enableRefBuffers(obj->adcHandle);
    
    
      // Set main clock scaling factor (max45MHz clock for the ADC module)
      ADC_setDivideSelect(obj->adcHandle,ADC_DivideSelect_ClkIn_by_2);
    
    
      // power up the ADCs
      ADC_powerUp(obj->adcHandle);
    
    
      // enable the ADCs
      ADC_enable(obj->adcHandle);
    
    
      // set the ADC interrupt pulse generation to prior
      ADC_setIntPulseGenMode(obj->adcHandle,ADC_IntPulseGenMode_Prior);
    
    
      // set the temperature sensor source to internal 
      ADC_setTempSensorSrc(obj->adcHandle,ADC_TempSensorSrc_Int);
    
    
      // configure the interrupt sources
    //  ADC_disableInt(obj->adcHandle,ADC_IntNumber_1);
    //  ADC_setIntMode(obj->adcHandle,ADC_IntNumber_1,ADC_IntMode_ClearFlag);
    //  ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_1,ADC_IntSrc_EOC5);
    
    //  ADC_disableInt(obj->adcHandle,ADC_IntNumber_2);
    //  ADC_setIntMode(obj->adcHandle,ADC_IntNumber_2,ADC_IntMode_ClearFlag);
    //  ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_2,ADC_IntSrc_EOC0);
    
      ADC_disableInt(obj->adcHandle,ADC_IntNumber_6);
      ADC_setIntMode(obj->adcHandle,ADC_IntNumber_6,ADC_IntMode_ClearFlag);
      ADC_setIntSrc(obj->adcHandle,ADC_IntNumber_6,ADC_IntSrc_EOC7);
    
    
      //configure the SOCs for drv8301kit_revD
      // EXT IA-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_0,ADC_SocChanNumber_A6);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_0,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_0,ADC_SocSampleDelay_9_cycles);
    
      // EXT IA-FB
      // Duplicate conversion due to ADC Initial Conversion bug
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_1,ADC_SocChanNumber_A6);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_1,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_1,ADC_SocSampleDelay_9_cycles);
    
      // EXT IB-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_2,ADC_SocChanNumber_B6);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_2,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_2,ADC_SocSampleDelay_9_cycles);
    
      // EXT IC-FB
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_3,ADC_SocChanNumber_A0);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_3,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_3,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb1
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_4,ADC_SocChanNumber_B7);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_4,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_4,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb2
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_5,ADC_SocChanNumber_A7);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_5,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_5,ADC_SocSampleDelay_9_cycles);
    
      // ADC-Vhb3
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_6,ADC_SocChanNumber_B4);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_6,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_6,ADC_SocSampleDelay_9_cycles);
    
      // VDCBUS
      ADC_setSocChanNumber(obj->adcHandle,ADC_SocNumber_7,ADC_SocChanNumber_B2);
      ADC_setSocTrigSrc(obj->adcHandle,ADC_SocNumber_7,ADC_SocTrigSrc_EPWM1_ADCSOCA);
      ADC_setSocSampleDelay(obj->adcHandle,ADC_SocNumber_7,ADC_SocSampleDelay_9_cycles);
    
    
      return;
    } // end of HAL_setupAdcs() function
    
    
    void HAL_setupClks(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // enable internal oscillator 1
      CLK_enableOsc1(obj->clkHandle);
    
      // disable the external clock in
      CLK_disableClkIn(obj->clkHandle);
    
      // disable the crystal oscillator
      CLK_disableCrystalOsc(obj->clkHandle);
    //  CLK_enableCrystalOsc(obj->clkHandle);
    //  usDelay(10000);
    
      // disable oscillator 2
      CLK_disableOsc2(obj->clkHandle);
    //  CLK_enableOsc2(obj->clkHandle);
    
      // set the oscillator 2 source
    //  CLK_setOsc2Src(obj->clkHandle,CLK_Osc2Src_External);
    
      // set the oscillator source
        CLK_setOscSrc(obj->clkHandle,CLK_OscSrc_Internal);
    //  CLK_setOscSrc(obj->clkHandle,CLK_OscSrc_External);
    
      // set the low speed clock prescaler
      CLK_setLowSpdPreScaler(obj->clkHandle,CLK_LowSpdPreScaler_SysClkOut_by_4);
    
      // set the clock out prescaler
      CLK_setClkOutPreScaler(obj->clkHandle,CLK_ClkOutPreScaler_SysClkOut_by_1);
    
      return;
    } // end of HAL_setupClks() function
    
    
    void HAL_setupFlash(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      FLASH_enablePipelineMode(obj->flashHandle);
    
      FLASH_setNumPagedReadWaitStates(obj->flashHandle,FLASH_NumPagedWaitStates_3);
    
      FLASH_setNumRandomReadWaitStates(obj->flashHandle,FLASH_NumRandomWaitStates_3);
    
      FLASH_setOtpWaitStates(obj->flashHandle,FLASH_NumOtpWaitStates_5);
    
      FLASH_setStandbyWaitCount(obj->flashHandle,FLASH_STANDBY_WAIT_COUNT_DEFAULT);
    
      FLASH_setActiveWaitCount(obj->flashHandle,FLASH_ACTIVE_WAIT_COUNT_DEFAULT);
    
      return;
    } // HAL_setupFlash() function
    
    
    void HAL_setupFaults(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
      uint_least8_t cnt;
    
    
      // Configure Trip Mechanism for the Motor control software
      // -Cycle by cycle trip on CPU halt
      // -One shot fault trip zone
      // These trips need to be repeated for EPWM1 ,2 & 3
      for(cnt=0;cnt<3;cnt++)
        {
          PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_CycleByCycle_TZ6_NOT);
    
          PWM_enableTripZoneSrc(obj->pwmHandle[cnt],PWM_TripZoneSrc_OneShot_TZ1_NOT);
    
          // What do we want the OST/CBC events to do?
          // TZA events can force EPWMxA
          // TZB events can force EPWMxB
    
          PWM_setTripZoneState_TZA(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
          PWM_setTripZoneState_TZB(obj->pwmHandle[cnt],PWM_TripZoneState_EPWM_Low);
    
          // Clear faults from flip flop
          GPIO_setLow(obj->gpioHandle,GPIO_Number_9);
          GPIO_setHigh(obj->gpioHandle,GPIO_Number_9);
    
          // Clear any spurious fault
          PWM_clearOneShotTrip(obj->pwmHandle[cnt]);
        }
    
      return;
    } // end of HAL_setupFaults() function
    
    
    void HAL_setupGate(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
      DRV8301_setSpiHandle(obj->drv8301Handle,obj->spiBHandle);
      DRV8301_setGpioHandle(obj->drv8301Handle,obj->gpioHandle);
      DRV8301_setGpioNumber(obj->drv8301Handle,GPIO_Number_51);
    } // HAL_setupGate() function
    
    
    void HAL_setupGpios(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // PWM1
      GPIO_setMode(obj->gpioHandle,GPIO_Number_0,GPIO_0_Mode_EPWM1A);
    
      // PWM2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_1,GPIO_1_Mode_EPWM1B);
    
      // PWM3
      GPIO_setMode(obj->gpioHandle,GPIO_Number_2,GPIO_2_Mode_EPWM2A);
    
      // PWM4
      GPIO_setMode(obj->gpioHandle,GPIO_Number_3,GPIO_3_Mode_EPWM2B);
    
      // PWM5
      GPIO_setMode(obj->gpioHandle,GPIO_Number_4,GPIO_4_Mode_EPWM3A);
    
      // PWM6
      GPIO_setMode(obj->gpioHandle,GPIO_Number_5,GPIO_5_Mode_EPWM3B);
    
      // PWM-DAC4
    //8/25 using for DataAcq MUT-LM clock sync between boards  GPIO_setMode(obj->gpioHandle,GPIO_Number_6,GPIO_6_Mode_EPWM4A);
    
      //DataAcq MUT-LM clock sync between boards GPIO06 is SYNC_INPUT
      GPIO_setMode(obj->gpioHandle,GPIO_Number_6,GPIO_6_Mode_GeneralPurpose);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_6,GPIO_Direction_Input);
      GPIO_setPullup(obj->gpioHandle,GPIO_Number_6, GPIO_Pullup_Enable);
      GPIO_setQualificationPeriod(obj->gpioHandle,GPIO_Number_6, 1);
      GPIO_setQualification(obj->gpioHandle,GPIO_Number_6, GPIO_Qual_Sample_6);
      GPIO_setExtInt(obj->gpioHandle,GPIO_Number_6,CPU_ExtIntNumber_1);
      PIE_setExtIntPolarity(obj->pieHandle,CPU_ExtIntNumber_1,PIE_ExtIntPolarity_RisingAndFallingEdge);
      obj->pieHandle->XINTnCR[CPU_ExtIntNumber_1] |= PIE_XINTnCR_ENABLE_BITS;
    
      // Input SW1
      GPIO_setMode(obj->gpioHandle,GPIO_Number_7,GPIO_7_Mode_GeneralPurpose);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_7,GPIO_Direction_Input);
    
      // ADCSOCAO_NOT
      //8/25 using for DataAcq MUT-LM clock sync between boards    GPIO_setMode(obj->gpioHandle,GPIO_Number_8,GPIO_8_Mode_ADCSOCAO_NOT);
    
      //DataAcq MUT-LM clock sync between boards GPIO08 is SYNC_OUTPUT
      GPIO_setMode(obj->gpioHandle,GPIO_Number_8,GPIO_8_Mode_GeneralPurpose);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_8,GPIO_Direction_Output);
      GPIO_setHigh(obj->gpioHandle,GPIO_Number_8);
    
      // Input SW2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_9,GPIO_9_Mode_GeneralPurpose);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_9,GPIO_Direction_Input);
    
      // PWM-DAC1
    //  GPIO_setMode(obj->gpioHandle,GPIO_Number_10,GPIO_10_Mode_EPWM6A);
    
      // DataAcq handshake to FTDI cable
      GPIO_setMode(obj->gpioHandle,GPIO_Number_10,GPIO_10_Mode_GeneralPurpose);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_10,GPIO_Direction_Output);
    
      // PWM-DAC2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_11,GPIO_11_Mode_EPWM6B);
    
      // DRV8301-LED1
      GPIO_setMode(obj->gpioHandle,GPIO_Number_12,GPIO_12_Mode_GeneralPurpose);
    
      // OCTWn
      GPIO_setMode(obj->gpioHandle,GPIO_Number_13,GPIO_13_Mode_TZ2_NOT);
    
      // FAULTn
      GPIO_setMode(obj->gpioHandle,GPIO_Number_14,GPIO_14_Mode_TZ3_NOT);
    
      // LED2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_15,GPIO_15_Mode_GeneralPurpose);
    
      // SPI-SIMO
      GPIO_setMode(obj->gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);
      GPIO_setPullup(obj->gpioHandle,GPIO_Number_16, GPIO_Pullup_Enable);
    
      // SPI-SOMI
      GPIO_setMode(obj->gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);
      GPIO_setPullup(obj->gpioHandle,GPIO_Number_17, GPIO_Pullup_Enable);
    
      // SPI-CLK
      GPIO_setMode(obj->gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);
      GPIO_setPullup(obj->gpioHandle,GPIO_Number_18, GPIO_Pullup_Enable);
    
      // SPI-STE
      GPIO_setMode(obj->gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);
      GPIO_setPullup(obj->gpioHandle,GPIO_Number_19, GPIO_Pullup_Enable);
    
    #ifdef QEP
      // EQEPA
      GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_EQEP1A);
    
      // EQEPB
      GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_EQEP1B);
    
      // STATUS
      GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
    
      // EQEP1I
      GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_EQEP1I);
    #else
      // EQEPA
      GPIO_setMode(obj->gpioHandle,GPIO_Number_20,GPIO_20_Mode_GeneralPurpose);
    
      // EQEPB
      GPIO_setMode(obj->gpioHandle,GPIO_Number_21,GPIO_21_Mode_GeneralPurpose);
    
      // STATUS
      GPIO_setMode(obj->gpioHandle,GPIO_Number_22,GPIO_22_Mode_GeneralPurpose);
    
      // EQEP1I
      GPIO_setMode(obj->gpioHandle,GPIO_Number_23,GPIO_23_Mode_GeneralPurpose);
    #endif
    
      // SPI SIMO B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_24,GPIO_24_Mode_SPISIMOB);
    
      // SPI SOMI B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_25,GPIO_25_Mode_SPISOMIB);
    
      // SPI CLK B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_26,GPIO_26_Mode_SPICLKB);
    
      // SPI CSn B
      GPIO_setMode(obj->gpioHandle,GPIO_Number_27,GPIO_27_Mode_SPISTEB_NOT);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_28,GPIO_28_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_29,GPIO_29_Mode_GeneralPurpose);
    
      // DataAcq start test from FTDI cable
      GPIO_setMode(obj->gpioHandle,GPIO_Number_30,GPIO_30_Mode_GeneralPurpose);
    
      // ControlCARD LED2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_31,GPIO_31_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_31);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_31,GPIO_Direction_Output);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_32,GPIO_32_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_33,GPIO_33_Mode_GeneralPurpose);
    
      // ControlCARD LED3
      GPIO_setMode(obj->gpioHandle,GPIO_Number_34,GPIO_34_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_34);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_34,GPIO_Direction_Output);
    
      // JTAG
      GPIO_setMode(obj->gpioHandle,GPIO_Number_35,GPIO_35_Mode_JTAG_TDI);
      GPIO_setMode(obj->gpioHandle,GPIO_Number_36,GPIO_36_Mode_JTAG_TMS);
      GPIO_setMode(obj->gpioHandle,GPIO_Number_37,GPIO_37_Mode_JTAG_TDO);
      GPIO_setMode(obj->gpioHandle,GPIO_Number_38,GPIO_38_Mode_JTAG_TCK);
    
      // DRV8301 Enable
      GPIO_setMode(obj->gpioHandle,GPIO_Number_39,GPIO_39_Mode_GeneralPurpose);
    
      // CAP1
      GPIO_setMode(obj->gpioHandle,GPIO_Number_40,GPIO_40_Mode_GeneralPurpose);
    
      // CAP2
      GPIO_setMode(obj->gpioHandle,GPIO_Number_41,GPIO_41_Mode_GeneralPurpose);
    
      // CAP3
      GPIO_setMode(obj->gpioHandle,GPIO_Number_42,GPIO_42_Mode_GeneralPurpose);
    
      // DC_CAL
      GPIO_setMode(obj->gpioHandle,GPIO_Number_43,GPIO_43_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_44,GPIO_44_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_50,GPIO_50_Mode_GeneralPurpose);
    
      // DRV8301 Enable
      GPIO_setMode(obj->gpioHandle,GPIO_Number_51,GPIO_51_Mode_GeneralPurpose);
      GPIO_setLow(obj->gpioHandle,GPIO_Number_51);
      GPIO_setDirection(obj->gpioHandle,GPIO_Number_51,GPIO_Direction_Output);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_52,GPIO_52_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_53,GPIO_53_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_54,GPIO_54_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_55,GPIO_55_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_56,GPIO_56_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_57,GPIO_57_Mode_GeneralPurpose);
    
      // No Connection
      GPIO_setMode(obj->gpioHandle,GPIO_Number_58,GPIO_58_Mode_GeneralPurpose);
    
      return;
    }  // end of HAL_setupGpios() function
    
    
    void HAL_setupPie(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      PIE_disable(obj->pieHandle);
    
      PIE_disableAllInts(obj->pieHandle);
    
      PIE_clearAllInts(obj->pieHandle);
    
      PIE_clearAllFlags(obj->pieHandle);
    
      PIE_setDefaultIntVectorTable(obj->pieHandle);
    
      PIE_enable(obj->pieHandle);
    
      return;
    } // end of HAL_setupPie() function
    
    
    void HAL_setupPeripheralClks(HAL_Handle handle)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      CLK_enableAdcClock(obj->clkHandle);
    
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_1);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_2);
      CLK_enableCompClock(obj->clkHandle,CLK_CompNumber_3);
    
      CLK_enableEcap1Clock(obj->clkHandle);
    
      CLK_disableEcanaClock(obj->clkHandle);
    
    #ifdef QEP
      CLK_enableEqep1Clock(obj->clkHandle);
    #endif
    
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_1);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_2);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_3);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_4);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_5);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_6);
      CLK_enablePwmClock(obj->clkHandle,PWM_Number_7);
    
      CLK_disableHrPwmClock(obj->clkHandle);
    
      CLK_disableI2cClock(obj->clkHandle);
    
      CLK_disableLinAClock(obj->clkHandle);
    
      CLK_disableClaClock(obj->clkHandle);
    
      CLK_enableSciaClock(obj->clkHandle);
    
      CLK_enableSpiaClock(obj->clkHandle);
    
      CLK_enableSpibClock(obj->clkHandle);
      
      CLK_enableTbClockSync(obj->clkHandle);
    
      return;
    } // end of HAL_setupPeripheralClks() function
    
    
    void HAL_setupPll(HAL_Handle handle,const PLL_ClkFreq_e clkFreq)
    {
      HAL_Obj *obj = (HAL_Obj *)handle;
    
    
      // make sure PLL is not running in limp mode
      if(PLL_getClkStatus(obj->pllHandle) != PLL_ClkStatus_Normal)
        {
          // reset the clock detect
          PLL_resetClkDetect(obj->pllHandle);
    
          // ???????
          asm("        ESTOP0");
        }
    
    
      // Divide Select must be ClkIn/4 before the clock rate can be changed
      if(PLL_getDivideSelect(obj->pllHandle) != PLL_DivideSelect_ClkIn_by_4)
        {
          PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_4);
        }
    
    
      if(PLL_getClkFreq(obj->pllHandle) != clkFreq)
        {
          // disable the clock detect
          PLL_disableClkDetect(obj->pllHandle);
    
          // set the clock rate
          PLL_setClkFreq(obj->pllHandle,clkFreq);
        }
    
    
      // wait until locked
      while(PLL_getLockStatus(obj->pllHandle) != PLL_LockStatus_Done) {}
    
    
      // enable the clock detect
      PLL_enableClkDetect(obj->pllHandle);
    
    
      // set divide select to ClkIn/2 to get desired clock rate
      // NOTE: clock must be locked before setting this register
      PLL_setDivideSelect(obj->pllHandle,PLL_DivideSelect_ClkIn_by_2);
    
      return;
    } // end of HAL_setupPll() function
    
    
    void HAL_setupPwms(HAL_Handle handle,
                       const float_t systemFreq_MHz,
                       const float_t pwmPeriod_usec,
                       const uint_least16_t numPwmTicksPerIsrTick)
    {
      HAL_Obj       *obj = (HAL_Obj *)handle;
      uint16_t       halfPeriod_cycles = (uint16_t)(systemFreq_MHz * pwmPeriod_usec / (float_t)2.0);
      uint_least8_t  cnt;
    
    
      for(cnt=0;cnt<3;cnt++)
        {
          // setup the Time-Base Control Register (TBCTL)
          PWM_setCounterMode(obj->pwmHandle[cnt],PWM_CounterMode_UpDown);
          PWM_disableCounterLoad(obj->pwmHandle[cnt]);
          PWM_setPeriodLoad(obj->pwmHandle[cnt],PWM_PeriodLoad_Immediate);
          PWM_setSyncMode(obj->pwmHandle[cnt],PWM_SyncMode_EPWMxSYNC);
          PWM_setHighSpeedClkDiv(obj->pwmHandle[cnt],PWM_HspClkDiv_by_1);
          PWM_setClkDiv(obj->pwmHandle[cnt],PWM_ClkDiv_by_1);
          PWM_setPhaseDir(obj->pwmHandle[cnt],PWM_PhaseDir_CountUp);
          PWM_setRunMode(obj->pwmHandle[cnt],PWM_RunMode_FreeRun);
    
          // setup the Timer-Based Phase Register (TBPHS)
          PWM_setPhase(obj->pwmHandle[cnt],0);
    
          // setup the Time-Base Counter Register (TBCTR)
          PWM_setCount(obj->pwmHandle[cnt],0);
    
          // setup the Time-Base Period Register (TBPRD)
          // set to zero initially
          PWM_setPeriod(obj->pwmHandle[cnt],0);
    
          // setup the Counter-Compare Control Register (CMPCTL)
          PWM_setLoadMode_CmpA(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
          PWM_setLoadMode_CmpB(obj->pwmHandle[cnt],PWM_LoadMode_Zero);
          PWM_setShadowMode_CmpA(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
          PWM_setShadowMode_CmpB(obj->pwmHandle[cnt],PWM_ShadowMode_Shadow);
    
          // setup the Action-Qualifier Output A Register (AQCTLA) 
          PWM_setActionQual_CntUp_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Set);
          PWM_setActionQual_CntDown_CmpA_PwmA(obj->pwmHandle[cnt],PWM_ActionQual_Clear);
    
          // setup the Dead-Band Generator Control Register (DBCTL)
          PWM_setDeadBandOutputMode(obj->pwmHandle[cnt],PWM_DeadBandOutputMode_EPWMxA_Rising_EPWMxB_Falling);
          PWM_setDeadBandPolarity(obj->pwmHandle[cnt],PWM_DeadBandPolarity_EPWMxB_Inverted);
    
          // setup the Dead-Band Rising Edge Delay Register (DBRED)
          PWM_setDeadBandRisingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBRED_CNT);
    
          // setup the Dead-Band Falling Edge Delay Register (DBFED)
          PWM_setDeadBandFallingEdgeDelay(obj->pwmHandle[cnt],HAL_PWM_DBFED_CNT);
    
          // setup the PWM-Chopper Control Register (PCCTL)
          PWM_disableChopping(obj->pwmHandle[cnt]);
    
          // setup the Trip Zone Select Register (TZSEL)
          PWM_disableTripZones(obj->pwmHandle[cnt]);
        }
    
    
      // setup the Event Trigger Selection Register (ETSEL)
      PWM_disableInt(obj->pwmHandle[PWM_Number_1]);
      PWM_setIntMode(obj->pwmHandle[PWM_Number_1],PWM_IntMode_CounterEqualZero);
      PWM_setSocAPulseSrc(obj->pwmHandle[PWM_Number_1],PWM_SocPulseSrc_CounterEqualZero);
      PWM_enableSocAPulse(obj->pwmHandle[PWM_Number_1]);
      
    
      // setup the Event Trigger Prescale Register (ETPS)
      if(numPwmTicksPerIsrTick == 3)
        {
          PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_ThirdEvent);
          PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_ThirdEvent);
        }
      else if(numPwmTicksPerIsrTick == 2)
        {
          PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_SecondEvent);
          PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_SecondEvent);
        }
      else
        {
          PWM_setIntPeriod(obj->pwmHandle[PWM_Number_1],PWM_IntPeriod_FirstEvent);
          PWM_setSocAPeriod(obj->pwmHandle[PWM_Number_1],PWM_SocPeriod_FirstEvent);
        }
    
    
      // setup the Event Trigger Clear Register (ETCLR)
      PWM_clearIntFlag(obj->pwmHandle[PWM_Number_1]);
      PWM_clearSocAFlag(obj->pwmHandle[PWM_Number_1]);
    
    
      // disable the ePWM module time base clock sync signal
      // to synchronize all of the PWMs
      CLK_disableTbClockSync(obj->clkHandle);
    
    
      // since the PWM is configured as an up/down counter, the period register is set to one-half 
      // of the desired PWM period
      PWM_setPeriod(obj->pwmHandle[PWM_Number_1],halfPeriod_cycles);
      PWM_setPeriod(obj->pwmHandle[PWM_Number_2],halfPeriod_cycles);
      PWM_setPeriod(obj->pwmHandle[PWM_Number_3],halfPeriod_cycles);
    
    
      // enable the ePWM module time base clock sync signal
      CLK_enableTbClockSync(obj->clkHandle);
    
      return;
    }  // end of HAL_setupPwms() function
    
    void HAL_setupSpiA(HAL_Handle handle)
    {
      HAL_Obj   *obj = (HAL_Obj *)handle;
    
      SPI_reset(obj->spiAHandle);
      SPI_setClkPolarity(obj->spiAHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
      SPI_disableLoopBack(obj->spiAHandle);
      SPI_setCharLength(obj->spiAHandle,SPI_CharLength_16_Bits);
    
      SPI_setMode(obj->spiAHandle,SPI_Mode_Slave);
    
      SPI_setClkPhase(obj->spiAHandle,SPI_ClkPhase_Normal);
      SPI_enableTx(obj->spiAHandle);
    
      SPI_enableChannels(obj->spiAHandle);
      SPI_enableTxFifoEnh(obj->spiAHandle);
      SPI_enableTxFifo(obj->spiAHandle);
      SPI_setTxDelay(obj->spiAHandle,0);
      SPI_clearTxFifoInt(obj->spiAHandle);
      SPI_enableRxFifo(obj->spiAHandle);
    
      //not needed for slave mode  SPI_setBaudRate(obj->spiAHandle,(SPI_BaudRate_e)(0x0003));
      SPI_setSuspend(obj->spiAHandle,SPI_TxSuspend_free);
      SPI_enable(obj->spiAHandle);
    
      return;
    }  // end of HAL_setupSpiA() function
    
    void HAL_setupSpiB(HAL_Handle handle)
    {
      HAL_Obj   *obj = (HAL_Obj *)handle;
    
      SPI_reset(obj->spiBHandle);
      SPI_setMode(obj->spiBHandle,SPI_Mode_Master);
      SPI_setClkPolarity(obj->spiBHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
      SPI_enableTx(obj->spiBHandle);
      SPI_enableTxFifoEnh(obj->spiBHandle);
      SPI_enableTxFifo(obj->spiBHandle);
      SPI_setTxDelay(obj->spiBHandle,0x0018);
      SPI_setBaudRate(obj->spiBHandle,(SPI_BaudRate_e)(0x0003));
      SPI_setCharLength(obj->spiBHandle,SPI_CharLength_16_Bits);
      SPI_setSuspend(obj->spiBHandle,SPI_TxSuspend_free);
      SPI_enable(obj->spiBHandle);
    
      return;
    }  // end of HAL_setupSpiB() function
    
    
    void HAL_setupPwmDacs(HAL_Handle handle,const float_t systemFreq_MHz,const float_t dacFreq_kHz)
    {
      HAL_Obj       *obj = (HAL_Obj *)handle;
      uint16_t       halfPeriod_cycles = (uint16_t)(systemFreq_MHz * (float_t)1000.0 / ((float_t)2.0 * dacFreq_kHz));
      uint_least8_t  cnt;
    
    
      for(cnt=0;cnt<3;cnt++)
        {
          // initialize the Time-Base Control Register (TBCTL)
          PWMDAC_setCounterMode(obj->pwmDacHandle[cnt],PWM_CounterMode_UpDown);
          PWMDAC_disableCounterLoad(obj->pwmDacHandle[cnt]);
          PWMDAC_setPeriodLoad(obj->pwmDacHandle[cnt],PWM_PeriodLoad_Immediate);
          PWMDAC_setSyncMode(obj->pwmDacHandle[cnt],PWM_SyncMode_EPWMxSYNC);
          PWMDAC_setHighSpeedClkDiv(obj->pwmDacHandle[cnt],PWM_HspClkDiv_by_1);
          PWMDAC_setClkDiv(obj->pwmDacHandle[cnt],PWM_ClkDiv_by_1);
          PWMDAC_setPhaseDir(obj->pwmDacHandle[cnt],PWM_PhaseDir_CountUp);
          PWMDAC_setRunMode(obj->pwmDacHandle[cnt],PWM_RunMode_FreeRun);
    
          // initialize the Timer-Based Phase Register (TBPHS)
          PWMDAC_setPhase(obj->pwmDacHandle[cnt],0);
    
          // setup the Time-Base Counter Register (TBCTR)
          PWMDAC_setCount(obj->pwmDacHandle[cnt],0);
    
          // Initialize the Time-Base Period Register (TBPRD)
          // set to zero initially
          PWMDAC_setPeriod(obj->pwmDacHandle[cnt],0);
    
          // initialize the Counter-Compare Control Register (CMPCTL)
          PWMDAC_setLoadMode_CmpA(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
          PWMDAC_setLoadMode_CmpB(obj->pwmDacHandle[cnt],PWM_LoadMode_Zero);
          PWMDAC_setShadowMode_CmpA(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
          PWMDAC_setShadowMode_CmpB(obj->pwmDacHandle[cnt],PWM_ShadowMode_Shadow);
    
          // Initialize the Action-Qualifier Output A Register (AQCTLA) 
          PWMDAC_setActionQual_CntUp_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
          PWMDAC_setActionQual_CntDown_CmpA_PwmA(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
    
          // account for EPWM6B
          if(cnt == 0)
            {
              PWMDAC_setActionQual_CntUp_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Set);
              PWMDAC_setActionQual_CntDown_CmpB_PwmB(obj->pwmDacHandle[cnt],PWM_ActionQual_Clear);
            }
    
          // Initialize the Dead-Band Control Register (DBCTL) 
          PWMDAC_disableDeadBand(obj->pwmDacHandle[cnt]);
    
          // Initialize the PWM-Chopper Control Register (PCCTL)
          PWMDAC_disableChopping(obj->pwmDacHandle[cnt]);
    
          // Initialize the Trip-Zone Control Register (TZSEL)
          PWMDAC_disableTripZones(obj->pwmDacHandle[cnt]);
    
          // Initialize the Trip-Zone Control Register (TZCTL)
          PWMDAC_setTripZoneState_TZA(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_TZB(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_DCAEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_DCAEVT2(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
          PWMDAC_setTripZoneState_DCBEVT1(obj->pwmDacHandle[cnt],PWM_TripZoneState_HighImp);
        }
    
    
      // disable the ePWM module time base clock sync signal
      // to synchronize all of the PWMs
      CLK_disableTbClockSync(obj->clkHandle);
    
    
      // since the PWM is configured as an up/down counter, the period register is set to one-half 
      // of the desired PWM period
      PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_1],halfPeriod_cycles);
      PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_2],halfPeriod_cycles);
      PWMDAC_setPeriod(obj->pwmDacHandle[PWMDAC_Number_3],halfPeriod_cycles);
    
    
      // enable the ePWM module time base clock sync signal
      CLK_enableTbClockSync(obj->clkHandle);
    
      return;
    }  // end of HAL_setupPwmDacs() function
    
    
    // set up the QEP peripheral
    void HAL_setupQEP(QEP_Handle qepHandle,const USER_Params *pUserParams,const uint32_t unitTimerFreq_Hz)
    {
    
      uint16_t numEncSlots = pUserParams->motor_numEncSlots;
      uint32_t systemFreq_Hz = (uint32_t)(pUserParams->systemFreq_MHz) * 1e6;
      uint32_t unitTimerPrd = systemFreq_Hz / unitTimerFreq_Hz;
    
      // hold the counter in reset
      QEP_reset_counter(qepHandle);
    
      // set the QPOSINIT register
      QEP_set_posn_init_count(qepHandle, 0);
    
      // disable all interrupts
      QEP_disable_all_interrupts(qepHandle);
    
      // clear the interrupt flags
      QEP_clear_all_interrupt_flags(qepHandle);
    
      // clear the position counter
      QEP_clear_posn_counter(qepHandle);
    
      // setup the max position
      QEP_set_max_posn_count(qepHandle, (4*numEncSlots)-1);
    
      // setup the QDECCTL register
      QEP_set_QEP_source(qepHandle, QEP_Qsrc_Quad_Count_Mode);
      QEP_disable_sync_out(qepHandle);
      QEP_set_swap_quad_inputs(qepHandle, QEP_Swap_Not_Swapped);
      QEP_disable_gate_index(qepHandle);
      QEP_set_ext_clock_rate(qepHandle, QEP_Xcr_2x_Res);
      QEP_set_A_polarity(qepHandle, QEP_Qap_No_Effect);
      QEP_set_B_polarity(qepHandle, QEP_Qbp_No_Effect);
      QEP_set_index_polarity(qepHandle, QEP_Qip_No_Effect);
    
      // setup the QEPCTL register
      QEP_set_emu_control(qepHandle, QEPCTL_Freesoft_Unaffected_Halt);
      QEP_set_posn_count_reset_mode(qepHandle, QEPCTL_Pcrm_Max_Reset);
      QEP_set_strobe_event_init(qepHandle, QEPCTL_Sei_Nothing);
      QEP_set_index_event_init(qepHandle, QEPCTL_Iei_Nothing);
      QEP_set_index_event_latch(qepHandle, QEPCTL_Iel_Rising_Edge);
      QEP_set_soft_init(qepHandle, QEPCTL_Swi_Nothing);
      QEP_disable_unit_timer(qepHandle);
      QEP_disable_watchdog(qepHandle);
    
      // setup the QPOSCTL register
      QEP_disable_posn_compare(qepHandle);
    
      // setup the QCAPCTL register for low speed measurement
      QEP_enable_capture(qepHandle);
      QEP_set_capture_prescale(qepHandle,QCAPCTL_Ccps_Capture_Div_1);
    //  QEP_set_capture_prescale(qepHandle,QCAPCTL_Ccps_Capture_Div_32);
      QEP_set_unit_posn_prescale(qepHandle,QCAPCTL_Upps_Div_1_Prescale);
    
      // setup the unit timer for high speed measurement
      QEP_enable_unit_timer(qepHandle);
      QEP_set_unit_period(qepHandle,unitTimerPrd);
      QEP_set_capture_latch_mode(qepHandle,QEPCTL_Qclm_Latch_on_Unit_Timeout);
    
      // renable the position counter
      QEP_enable_counter(qepHandle);
    
      return;
    }  // end of HAL_setupQEP() function
    
    
    void HAL_setupTimers(HAL_Handle handle,const float_t systemFreq_MHz)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
      uint32_t timerPeriod_cnts = (uint32_t)(systemFreq_MHz * (float_t)1000000.0) - 1;
    
    
      // use timer 0 for frequency diagnostics
      TIMER_setDecimationFactor(obj->timerHandle[0],0);
      TIMER_setEmulationMode(obj->timerHandle[0],TIMER_EmulationMode_RunFree);
      TIMER_setPeriod(obj->timerHandle[0],timerPeriod_cnts);
      TIMER_setPreScaler(obj->timerHandle[0],0);
    
      // use timer 1 for CPU usage diagnostics
      TIMER_setDecimationFactor(obj->timerHandle[1],0);
      TIMER_setEmulationMode(obj->timerHandle[1],TIMER_EmulationMode_RunFree);
      TIMER_setPeriod(obj->timerHandle[1],timerPeriod_cnts);
      TIMER_setPreScaler(obj->timerHandle[1],0);
    
      return;
    }  // end of HAL_setupTimers() function
    
    
    void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_writeData(obj->drv8301Handle,Spi_8301_Vars);
    
      return;
    }  // end of HAL_writeDrvData() function
    
    
    void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_readData(obj->drv8301Handle,Spi_8301_Vars);
    
      return;
    }  // end of HAL_readDrvData() function
    
    
    void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
    {
      HAL_Obj  *obj = (HAL_Obj *)handle;
    
      DRV8301_setupSpi(obj->drv8301Handle,Spi_8301_Vars);
    
      return;
    }  // end of HAL_setupDrvSpi() function
    
    
    // end of file
    

  • Hi Jeff, I've compared the two and changed some things around, now I'm able to send things over SPI from SPIB to SPIA.

    Now I'm trying to make SPIA read on an interrupt. However I'm confused where to register the ISR for reading from spi within the HAL infrastructure.
  • Hi John,

    Look at hal.h and see the function HAL_initIntVectorTable(). Add a reference to the SPI interrupt (SPIRXINTA or SPITXINTA) as defined by PIE_Obj in pie.h.

    Jeff



    //! \brief Initializes the interrupt vector table
    //! \param[in] handle The hardware abstraction layer (HAL) handle
    static inline void HAL_initIntVectorTable(HAL_Handle handle)
    {
    HAL_Obj *obj = (HAL_Obj *)handle;
    PIE_Obj *pie = (PIE_Obj *)obj->pieHandle;


    ENABLE_PROTECTED_REGISTER_WRITE_MODE;

    // pie->EPWM1_INT = &led2ISR;
    // pie->ADCINT1 = &led3OffISR;
    // pie->ADCINT2 = &led3OnISR;
    pie->ADCINT6 = &mainISR;

    pie->SPIRXINTA= &Johns_SPI_ISR;

    DISABLE_PROTECTED_REGISTER_WRITE_MODE;

    return;
    } // end of HAL_initIntVectorTable() function
  • Hi, John.I have encountered the same problem.Now I'm using the HVkit with the F28069 control card.I want to add SPI to the project to set F28069 as master while the peripheral PLC as slave.Could tell me how can I do that?Thanks!

  • Hey Lai,

    unfortunately the only thing you can do is dive deep into the datasheet to make this work. There isn't actually code support for making this chip a spi slave. You can use one of the configuration functions to tell the chip to be a slave, but the receiving code and the code to set up spi interrupts doesn't exist. 

    This was so long ago I forgot exactly what I did, but on a high level

    1: read the spi section of the datasheet and write your own functions in the spi library to receiving information as a slave

    2: write a new function in the HAL.c file to interface your new spi slave function to the HAL

    3: test out your spi code by making a simple executable that just sends spi between 2 28069Ms and watch the spi register to see if you're receiving the correct information. 

    4: reverse engineer the motor control code and reference the vector table so you can pull the data once the spi buffer fills up on an interrupt

    I know this isn't the most helpful, but once I decided to just make everything from scratch, it took me ~ a day to get everything working very reliably. 

  • Hi,John.Thank you for your advice!Now I  try to add the drv8301 function to my lab03a first so as to test SPI.I found that SPIB clk could be detected,while I wanted to set SPIA as master.Here is my code

    void HAL_setupSpiA(HAL_Handle handle)
    {
    HAL_Obj *obj = (HAL_Obj *)handle;

    SPI_reset(obj->spiAHandle);
    SPI_setMode(obj->spiAHandle,SPI_Mode_Master);
    SPI_setClkPolarity(obj->spiAHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
    SPI_enableTx(obj->spiAHandle);
    SPI_enableTxFifoEnh(obj->spiAHandle);
    SPI_enableTxFifo(obj->spiAHandle);
    SPI_setTxDelay(obj->spiAHandle,0x0018);
    SPI_setBaudRate(obj->spiAHandle,(SPI_BaudRate_e)(0x000d));
    SPI_setCharLength(obj->spiAHandle,SPI_CharLength_16_Bits);
    SPI_setSuspend(obj->spiAHandle,SPI_TxSuspend_free);
    SPI_enable(obj->spiAHandle);

    return;
    } // end of HAL_setupSpiA() function


    void HAL_setupSpiB(HAL_Handle handle)
    {
    HAL_Obj *obj = (HAL_Obj *)handle;

    SPI_reset(obj->spiBHandle);
    SPI_setClkPolarity(obj->spiBHandle,SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
    SPI_disableLoopBack(obj->spiBHandle);
    SPI_setCharLength(obj->spiBHandle,SPI_CharLength_16_Bits);

    SPI_setMode(obj->spiBHandle,SPI_Mode_Slave);
    SPI_setClkPhase(obj->spiBHandle,SPI_ClkPhase_Normal);//SPI_ClkPhase_Delayed
    SPI_enableTx(obj->spiBHandle);

    SPI_enableChannels(obj->spiBHandle);
    SPI_enableTxFifoEnh(obj->spiBHandle);
    SPI_enableTxFifo(obj->spiBHandle);
    SPI_setTxDelay(obj->spiBHandle,0);
    SPI_clearTxFifoInt(obj->spiBHandle);
    SPI_enableRxFifo(obj->spiBHandle);

    //not needed for slave mode SPI_setBaudRate(obj->spiBHandle,(SPI_BaudRate_e)(0x000d));
    SPI_setSuspend(obj->spiBHandle,SPI_TxSuspend_free);
    SPI_enable(obj->spiBHandle);

    return;
    } // end of HAL_setupSpiB() function

    And GPIO setup:

    // D_SPISIMO_A
    GPIO_setMode(obj->gpioHandle,GPIO_Number_16,GPIO_16_Mode_SPISIMOA);//GPIO_16_Mode_GeneralPurpose
    GPIO_setPullup(obj->gpioHandle,GPIO_Number_16, GPIO_Pullup_Enable);

    // D_SPISOMI_A
    GPIO_setMode(obj->gpioHandle,GPIO_Number_17,GPIO_17_Mode_SPISOMIA);//GPIO_17_Mode_GeneralPurpose
    GPIO_setPullup(obj->gpioHandle,GPIO_Number_17, GPIO_Pullup_Enable);

    // D_SPICLK_A
    GPIO_setMode(obj->gpioHandle,GPIO_Number_18,GPIO_18_Mode_SPICLKA);//GPIO_18_Mode_GeneralPurpose
    GPIO_setPullup(obj->gpioHandle,GPIO_Number_18, GPIO_Pullup_Enable);

    // D_SPISTEA
    GPIO_setMode(obj->gpioHandle,GPIO_Number_19,GPIO_19_Mode_SPISTEA_NOT);//GPIO_19_Mode_GeneralPurpose
    GPIO_setPullup(obj->gpioHandle,GPIO_Number_19, GPIO_Pullup_Enable);

    // D_GPIO_24
    GPIO_setMode(obj->gpioHandle,GPIO_Number_24,GPIO_24_Mode_SPISIMOB);

    // D_GPIO_25
    GPIO_setMode(obj->gpioHandle,GPIO_Number_25,GPIO_25_Mode_SPISOMIB);

    // D_GPIO_26
    GPIO_setMode(obj->gpioHandle,GPIO_Number_26,GPIO_26_Mode_SPICLKB);//GPIO_26_Mode_SPICLKB

    // D_GPIO_27
    GPIO_setMode(obj->gpioHandle,GPIO_Number_27,GPIO_27_Mode_SPISTEB_NOT);

    However I couldn't detect any signals at SPIA clk pin.Are there erros in the configuration?

    Best regards,

    Lai