I am trying to Interface Poly Phase Energy Mertering IC ADE7758
Using STM32F411VET6
.
My SPI is working on a prescale of 16, Baud of 6.25 MB/s and MODE 2 i.e CPOL = 1 and CPHA = 0. Here is the snapshot of settings.
My connections are like this.
STM32 - ADE7758
PE11(NSS) - Pin 21(CS)
PE12(SCK) - Pin 23(SCLK)
PE13(MISO) - Pin 24(DOUT)
PE14(MOSI) - Pin 23(DIN)
Here is the global variables and defines
uint8_t aTxBuff[1] = {0};
uint8_t aRxBuff[1] = {0};
#define enableChip HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port,SPI1_NSS_Pin,GPIO_PIN_RESET)
#define disableChip HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port,SPI1_NSS_Pin,GPIO_PIN_SET)
I am trying to read OPMODE(0x13) register.
First i am writing the OPMODE register with a default value of 0x04.
Here is a snapshot of waveform.
My register address is 0x13 and i am writing so i have to logically 'OR' 0x13 with 0x80 i.e My waveform should be 0x93
The Default value on OPMODE register is 0x04
Here is the code i used for writing to ADE7758.
void ADE7758_write8(char reg, unsigned char data)
{
enableChip;
reg|=0x80;
aTxBuff[0] = (unsigned char)reg;
while(HAL_SPI_GetState(&hspi4) == HAL_SPI_STATE_BUSY_TX);
HAL_SPI_Transmit(&hspi4, (uint8_t*)aTxBuff, 1, 1000);
while(HAL_SPI_GetState(&hspi4) == HAL_SPI_STATE_BUSY_TX);
aTxBuff[0] = (unsigned char)data;
HAL_SPI_Transmit(&hspi4, (uint8_t*)aTxBuff, 1, 1000);
disableChip;
}
While writing using SPI to ADE7758 everything shows correct. But the problem occurs when i read back the register.
Here is the code for reading the SPI.
unsigned char ADE7758_read8(char reg)
{
enableChip;
aTxBuff[0] = (unsigned char)reg;
HAL_SPI_TransmitReceive(&hspi4, (uint8_t*)aTxBuff, (uint8_t*)aRxBuff, 1, 1000);
DWT_Delay_us(5);
aTxBuff[0] = 0x00;
HAL_SPI_TransmitReceive(&hspi4, (uint8_t*)aTxBuff, (uint8_t*)aRxBuff, 1, 1000);
disableChip;
return (unsigned char)aRxBuff[0];
}
I have tried to debug the code and constantly monitored value aRxBuff[0]
and the value is arbitary( like 0xFF,0xFC,0xDF etc ).
I don't know weather its the fault of reading times but here is the snapshot of Timing Characteristics of ADE7758
Please suggest where am i going wrong while reading SPI from ADE7758? Is it the fault of the way i am reading SPI using HAL or its the fault of timing?
Any suggestions will be appreciated.