0

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.

SPI_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

ADDRESS_WRITE

The Default value on OPMODE register is 0x04

DATA_WRITE

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

TIMING_CHARACTERSTICS

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.

Ehsan Habib
  • 135
  • 1
  • 5
  • 12
  • 1) Post definition of `aTxBuff`. 2) I'd expect a `while(HAL_SPI_GetState(&hspi4) == HAL_SPI_STATE_BUSY_TX);` before transmitting 3) Why is receive code not using the return value of `HAL_SPI_TransmitReceiv()`? – chux - Reinstate Monica Oct 28 '18 at 14:43
  • 1. `aTxBuff` definition - `uint8_t aTxBuff[1] = {0};` 3. HAL_SPI_TransmitReceive() returns ErrorCode but not value. – Ehsan Habib Oct 28 '18 at 14:56
  • An error code has a value. When code does not work as expected, a good first step is to insure that the various functions return values do not indicate error. Good luck. – chux - Reinstate Monica Oct 28 '18 at 14:59
  • No there is no error I have used this. `if (HAL_SPI_TransmitReceive(&hspi4, (uint8_t*)aTxBuff, (uint8_t*)aRxBuff, 1, 1000) != HAL_OK)` – Ehsan Habib Oct 28 '18 at 15:03
  • Reduce your SPI clock speed to 500 kHz, use the greatest prescaler. Mode 2 is the correct SPI mode to use. There's no need for this line: `while(HAL_SPI_GetState()...` since HAL_SPI_Transmit takes care of all that for you. Use pastebin to share your SPI peripheral init code – SoreDakeNoKoto Oct 28 '18 at 17:57
  • I suggest you check this out and port it: https://github.com/fngstudios/ADE7758/blob/master/ADE7758.cpp – SoreDakeNoKoto Oct 28 '18 at 18:02
  • I have reduced the SPI clock but the result is same as (0xD0,0xDB,0xBF,0xB0 etc instead of 0x04). Here is the pastebin https://pastebin.com/RXs3UT1K for SPI.c – Ehsan Habib Oct 28 '18 at 18:09
  • @TisteAndii I have already checked the github link as well as mbed Os link for ADE7758. But at first i wanted to read a default register and the further for VRMS, IRMS i could have taken the help of that codes. – Ehsan Habib Oct 28 '18 at 18:12
  • If you check read8 and write8 in the Arduino code, you'll see there are 50us delays between SPI transactions. You can try inserting those and see if it helps. Then you should also check that your circuit matches the test circuit in the datasheet, with the crystal, caps etc – SoreDakeNoKoto Oct 28 '18 at 18:32
  • i have used 50us delays one thing changed is now i m constantly getting a single value `0xBF`. I tried to increase the delays but the result is same. My circuit is having 22pf cap and 10MHz crystal attached to ADE – Ehsan Habib Oct 28 '18 at 19:35

0 Answers0