Anda di halaman 1dari 8

Documentation on setting up SPI protocol for ADE7878

The plot above shows SCLK signal on channel 2 and SS signal on channel 1. In order to operate
SPI communication mode in ADE7878, the SS signal is toggled high to low three times and any
write to CONFIG2 register will make SPI port available for communication until hardware reset
is made. The TS7800 Micro-controller provides the SCLK and SS signal to ADE7878 chip that
acts as slave device. The SS device is normally high under powered on.


As we can see that during the write operation, the SCLK goes low for a short period and then
goes high. There are no pulses observed during write operation.
The Write Operation is to write 0x00 to CONFIG2 register to lock the SPI port for further
communication.
During Write Operation,

SCLK clock is low for 19.20ms
The chip is low for 27.20ms
The time interval between low state of SS and low state of SCLK is 5.6ms
Write operation
Read operation

The Read Operation is to read CONFIG2 register to see if data is written properly in the
CONFIG2 register.


During Read operation,
The SS signal goes low for 83.20ms
The time interval between falling edge of CS signal and falling edge of SCLK is 4ms
The time interval between the rising edge of CS signal after write operation and falling
edge of CS signal at the start of read operation is 8s.









Read operation

The figure above shows SS signal on channel 1 and MOSI signal on channel 2. During the write
operation, we can see the MOSI signal.
Write operation

As we can see from the above figure, the MOSI signal goes high to low once during the read
operation.

******************************************************************************
*******
C Coding

*****SPI Initialization routine ******
void initSpi()
{
spi_fd = open("/dev/mem", O_RDWR|O_SYNC);
spi_dioptr = (unsigned char *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE,
MAP_SHARED, spi_fd, SPI_DIOBASE);
SPI_RW |= SPI_CLK; //set the spi_clk bit so spi clk signal starts high by default
}
*******End of SPI initialization routine *****


********Read Function Routine *******
//Write to external device and then read back response.
unsigned int spiRead(unsigned int reg, unsigned char numByte)
Read operation
{
int i;
unsigned char m0c0, m1c0, m0c1, m1c1;
unsigned int ret = 0;

printf("Inside spiRead()\n");
printf("The register is %x\n",reg);
reg= reg | 0x10000 ; // to indicate read operation, bit 0 of 16 bit register is set to 1
printf("The register after writing 1 in Bit 0 of first byte is %x\n",reg);

m1c1 = SPI_RW | SPI_MOSI | SPI_CLK;
m1c0 = m1c1 & ~SPI_CLK;
m0c0 = m1c0 & ~SPI_MOSI;
m0c1 = m1c1 & ~SPI_MOSI;

for(i=0; i < 32; i++)
{
if (reg & 0x80000000)
{
SPI_RW = m1c1;
ret <<= 1;
printf("ret value after shifting is %x\n",ret);
spiDelay(30);
SPI_RW = m1c0;

if (~SPI_RO & SPI_MISO)
{
ret |= 1;
printf("ret value after shifting is %x\n",ret);
}
reg <<= 1;
printf("reg value after shifting is %x\n",reg);
}
else
{
SPI_RW = m0c1;
ret <<= 1;
printf("ret value after shifting is %x\n",ret);
spiDelay(30);
SPI_RW = m0c0;

if (~SPI_RO & SPI_MISO)
{
ret |= 1;
printf("ret value after shifting is %x\n",ret);
}
reg <<= 1;
printf("reg value after shifting is %x\n",reg);
}
}
SPI_RW = m0c1; //SI - clear the spi_clk bit so spi clk signal finish low
printf("The RW register content is %u\n",SPI_RW);
ret = ret >> (((sizeof(reg)-1) - numByte)*8); //eg: right shift 0xD80000 to 0xD8 before return
printf("ret value after shifting is %x\n",ret);
return ret;
}

**************End of Read function routine ***************

*************Write Operation Code: ****************

unsigned int spiWrite(unsigned int reg, unsigned char numByte, unsigned int value)
{
int i;
unsigned char m0c0, m1c0, m0c1, m1c1;
unsigned int sendData = 0;
printf("The value to be written to register is %x\n",value);
printf("Inside spiWrite()\n");
printf("The register is %x\n",reg);
printf("The number of byte is %u\n",numByte);
reg = reg << 16; //left shift by 16 bits so that would put 0x11 in the format of 0x11000000
printf("The register value after shifting 16 bits %x\n",reg);
sendData = reg | value ; // last 16 bits of send data will represent the register and first 16 bits of
Send data will represent the value to be written to the register
printf("The send data is %x\n",sendData);
m1c1 = SPI_RW | SPI_MOSI | SPI_CLK;
m1c0 = m1c1 & ~SPI_CLK;
m0c0 = m1c0 & ~SPI_MOSI;
m0c1 = m1c1 & ~SPI_MOSI;

for(i=0; i < 8*(2+numByte); i++) //16 to 32 clock cycles - 16 to send cmd, remaining cycles to
send register value
{
if (sendData & 0x80000000)
{
SPI_RW = m1c1;
spiDelay(15);
SPI_RW = m1c0;
sendData <<= 1;

}
else
{
SPI_RW = m0c1;
spiDelay(15);
SPI_RW = m0c0;
sendData <<= 1;
}
spiDelay(15);
}
SPI_RW = m0c1; //SI - clear the spi_clk bit so spi clk signal finish low
printf("The send data at the end of write operation %u\n",sendData);
return sendData;
}

*************End of Write Operation Code: ****************


********************************End Code************************************

Anda mungkin juga menyukai