Hi,
I recently posted a while back regarding SPI protocol and it's required interface usage with an external module (DC2100A --> LTC6804-2) . As of the last few weeks, I have been dedicating my time attempting to map out bit readings using the external module datasheet and examples. However, I feel like this is not the proper method to go about SPI protocol and may be wasting my time trying to read every byte iteration and reading. The DC2100A model already has SPI functions that were utilized using a PIC controller, but I have been unsuccessful at importing or bringing over the code to CCS. I looked at the LTC6804-2_config.h and LTC-6804.h files but cannot find a way to go about this process.
Would you know of any method to go about importing or utilizing external hardware code (can be found in the DC2100A software guide: http://www.linear.com/solutions/5126)?
Thank you,
Andrew H
In addition, here is the code I have developed thus far:
/*
* main.c
*/
//#include "LTC6804-2.h"
//#include "C:\Users\hoanga\Desktop\DC2100A_Software_and_Users_Guide\DC2100A_Source_AppFW_v_1_2_0_GUI_v1_3_1\DC2100A_Source\Firmware\Source\LTC6804-2.h"
#include <stdio.h>
#include <inttypes.h>
#include <C:\ti\controlSUITE\device_support\F2837xD\v100\F2837xD_common\inc\hw_spi.h>
#include "F28x_Project.h"
//#include LTC6804-2_config.h
void delay_loop(void);
void error(void);
//user
void spi_init_test(void);
void spi_fifo_init_test(void);
void spi_receive(Uint16 a);
void spi_xmit(Uint16 a);
void spi_fifo_init(void);
void spi_init(void);
void LTC_wakeup(void);
void LTC_read_voltages(void);
void LTC_ADC_conversion(void);
void LTC_ADC_poll(void);
void LTC_clear_cells(void);
void main(void) {
Uint16 rdata; // received data
// Step 1. Initialize System C.ontrol:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2837xD_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initialize GPIO:
// This example function is found in the F2837xD_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Setup only the GP I/O only for SPI-A functionality
// This function is found in F2837xD_Spi.c
InitSpiaGpio();
// Step 3. Clear all __interrupts and initialize PIE vector table:
// Disable CPU __interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE __interrupts disabled and flags
// are cleared.
// This function is found in the F2837xD_PieCtrl.c file.
InitPieCtrl();
// Disable CPU __interrupts and clear all CPU __interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the __interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in F2837xD_DefaultIsr.c.
// This function is found in F2837xD_PieVect.c.
InitPieVectTable();
// Step 4. Initialize the Device Peripherals:
//spi_fifo_init(); // Initialize the Spi FIFO
//FIFO needs to be initialized if DMA were to be utilized
//spi_init(); // init SPI
spi_fifo_init_test(); //initialize user SPI FIFO
spi_init_test(); //initialize user SPI
LTC_wakeup();
LTC_ADC_poll();
LTC_ADC_conversion();
LTC_clear_cells();
LTC_read_voltages();
//LTC6804_Init();
// Step 5. User specific code
for(;;)
{
// Wait until data is received
//while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
// Check against sent data
//while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }
//if(rdata != sdata) error();
//printf("%" PRIu16 "\n", rdata);
DELAY_US(1000000);
}
}
void spi_xmit(Uint16 a)
{
SpiaRegs.SPITXBUF=a;
}
void spi_receive(Uint16 a)
{
SpiaRegs.SPIRXBUF = a; //receiving value from the buffer?
}
void spi_fifo_init()
{
// Initialize SPI FIFO registers
SpiaRegs.SPIFFTX.all=0xE040;
SpiaRegs.SPIFFRX.all=0x2044;
SpiaRegs.SPIFFCT.all=0x0;
}
void spi_fifo_init_test()
{
SpiaRegs.SPIFFTX.bit.SPIRST = 0; //TX FIFO SPI Reset
SpiaRegs.SPIFFTX.bit.SPIFFENA = 0; //TX FIFO enhancements (what is that?)
SpiaRegs.SPIFFTX.bit.TXFFIENA = 0; //TX FIFO enable (0 = disable; 1 = enabled)
SpiaRegs.SPIFFTX.bit.TXFFIL = 0; //Transmit FIFO Interrupt Level Bits
SpiaRegs.SPIFFTX.bit.TXFFINT = 0; //TX FIFO Interrupt Bit (Read only)
SpiaRegs.SPIFFTX.bit.TXFFINTCLR = 0; //TX FIFO Interrupt Clear
SpiaRegs.SPIFFTX.bit.SPIRST = 1; //TX FIFO SPI Reset CLEAR
SpiaRegs.SPIFFTX.bit.TXFFST = 0x4;//TX FIFO status (tells you how many words there are (4 max) )
SpiaRegs.SPIFFTX.bit.TXFIFO = 1; //release FIFO from reset//TX FIFO reset
SpiaRegs.SPIFFRX.bit.RXFIFORESET = 0; //Initialize RX FIFO Reset
SpiaRegs.SPIFFRX.bit.RXFFIENA = 0; //RX FIFO Interrupt Enabler (0 = disable; 1 = enabled)
SpiaRegs.SPIFFRX.bit.RXFFINT = 0; //RX FIFO Interrupt Flag
SpiaRegs.SPIFFRX.bit.RXFFINTCLR = 0; //RX FIFO Interrupt Flag Clear
SpiaRegs.SPIFFRX.bit.RXFFOVF = 0; //RX FIFO overflow flag
SpiaRegs.SPIFFRX.bit.RXFFOVFCLR = 0;//RX FIFO overflow flag Clear
SpiaRegs.SPIFFRX.bit.RXFFIL = 0x0; //RX FIFO Interrupt Level Bits --> seems a bit important to stop receive garble
//SpiaRegs.SPIFFRX.bit.RXFFST = //RX FIFO Receive status
SpiaRegs.SPIFFRX.bit.RXFIFORESET = 1; //RX FIFO Reset
//SPI FIFO Control Register
SpiaRegs.SPIFFCT.bit.TXDLY = 0x0;
}
void spi_init()
{
SpiaRegs.SPICCR.all =0x000F; // Reset on, rising edge, 16-bit char bits
SpiaRegs.SPICTL.all =0x0006; // Enable master mode, normal phase,
// enable talk, and SPI int disabled.
SpiaRegs.SPIBRR.all =0x007F;
SpiaRegs.SPICCR.all =0x009F; // Relinquish SPI from Reset
SpiaRegs.SPIPRI.bit.FREE = 1; // Set so breakpoints don't disturb xmission
}
void spi_init_test()
{
//Configure the SPI configuration register bits
SpiaRegs.SPICCR.bit.SPISWRESET = 0; //SPI reset initialization
SpiaRegs.SPICCR.bit.CLKPOLARITY = 0; // controls the shift clock polarity refer to page 1990 of the technical documentation for more info
SpiaRegs.SPICCR.bit.HS_MODE = 0; // high speed mode SPI protocol
//SpiaRegs.SPICCR.bit.SPICHAR = 0x8; // Character length control bits //current somewhat working setting
SpiaRegs.SPICCR.bit.SPICHAR = 0x0060; // Character length control bits
SpiaRegs.SPICCR.bit.SPILBK = 0; //SPI Loopback Mode Select
SpiaRegs.SPICCR.bit.SPISWRESET = 1; //Relinquish SPI from Reset
//SPI Operation Control Register
SpiaRegs.SPICTL.bit.CLK_PHASE = 0; //help control the CLKPOLARITY sequence
SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1; //select the SPI A port to be a master or slave; Slave = 0, Master = 1
SpiaRegs.SPICTL.bit.OVERRUNINTENA = 0; //Overrun Interrupt Enable
SpiaRegs.SPICTL.bit.SPIINTENA = 0; //control whether SPI will run in ISR or not
SpiaRegs.SPICTL.bit.TALK = 1; //Transmit Enable
SpiaRegs.SPIBRR.bit.SPI_BIT_RATE = 0x000013; //set the SPI baud rate
//7Fh (R/W) = SPI Baud Rate = LSPCLK/128
//13h (R/W) = SPI Baud Rate = LSPCLK/20
//all SPI status registers (flags) default value to 0, this just enforces it
SpiaRegs.SPISTS.bit.BUFFULL_FLAG = 0;
SpiaRegs.SPISTS.bit.INT_FLAG = 0;
SpiaRegs.SPISTS.bit.OVERRUN_FLAG = 0;
//all SPI priorities default set to 0, this just enforces it
SpiaRegs.SPIPRI.bit.FREE = 0;
SpiaRegs.SPIPRI.bit.PRIORITY = 0;
SpiaRegs.SPIPRI.bit.SOFT = 0;
SpiaRegs.SPIPRI.bit.STEINV = 0;
SpiaRegs.SPIPRI.bit.TRIWIRE = 0;
//turn of emulation mode
SpiaRegs.SPIRXEMU = 0;
}
void delay_loop()
{
long i;
for (i = 0; i < 1000000; i++) {}
}
void LTC_wakeup()
{
//Wake up serial interface
spi_xmit(0x00);
DELAY_US(300); //tWAKE = 100 uS, datasheet recommended 100*3 uS delay time
spi_xmit(0x00);
DELAY_US(300); //tWAKE = 100 uS, datasheet recommended 100*3 uS delay time
spi_xmit(0x00);
DELAY_US(300); //tWAKE = 100 uS, datasheet recommended 100*3 uS delay time
printf("Checking wakeup SPI protocol \n");
printf("-----");
}
void LTC_ADC_poll()
{
spi_xmit(0x80);
spi_xmit(0x9F);
spi_xmit(0x14);
spi_xmit(0x1C);
spi_xmit(0x48);
Uint16 rdata;
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0x00);
}
void LTC_ADC_conversion()
{
spi_xmit(0x80);
spi_xmit(0x03);
spi_xmit(0x70);
spi_xmit(0xAF);
spi_xmit(0x42);
Uint16 rdata;
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0x80);
}
void LTC_read_voltages()
{
Uint16 rdata;
// Read data
spi_xmit(0x80); //pull CSB low
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0x00); //RDCVA command
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0x04); //RDCVA command continued
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0x07); // respective PEC (packet error code)
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0xC2); //PEC continued
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
DELAY_US(1000);
spi_xmit(0x00); //pull CSB high
}
void LTC_clear_cells()
{
spi_xmit(0x80);
spi_xmit(0x07);
spi_xmit(0x11);
spi_xmit(0xC9);
spi_xmit(0xC0);
Uint16 rdata;
rdata = SpiaRegs.SPIRXBUF;
printf("%" PRIu16 "\n", rdata);
spi_xmit(0x80);
}
void error(void)
{
asm(" ESTOP0"); // Test failed!! Stop!
for (;;);
}