cancel
Showing results for 
Search instead for 
Did you mean: 

Function AFERead16BitBuffer() function in afe_config.c does not return a status

mike_m
Associate

I am running the Analog Devices EVAL-ADE9430 board and noticed if I enable CRC checking reading 32 bit registers fail while 16-bit registers always pass.

I trace it down to the  AFERead16BitBuffer() function not returning a status after reading the register over the SPI bus. This allows 16 bit register reads to work all the time while 32 Bit registers fail all the time (that's another story).  

See line 12 below:     HAL_SPI_Receive(&hSPI, (uint8_t *)&pData[0], 2 * numSamples, SPI_TIMEOUT);

uint32_t AFERead16BitBuffer(uint16_t addr, uint16_t numSamples, uint16_t *pData)
{
    int32_t status = SYS_STATUS_SUCCESS;

    cmdBuffer[0] = (addr >> 4);
    cmdBuffer[1] = ((addr & 0x0F) << 4) + 8;

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
    status = HAL_SPI_Transmit(&hSPI, (uint8_t *)&cmdBuffer, 2, SPI_TIMEOUT);
    if (status == 0)
    {
        HAL_SPI_Receive(&hSPI, (uint8_t *)&pData[0], 2 * numSamples,     
                        SPI_TIMEOUT);
    }
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);

    SwapEndian16Bit(&pData[0], numSamples);

    DEBUG_MSG("REG16RD,0x%x,0x%x", addr, pData[0]);

    return status;
}

Compare this to the uint32_t AFERead32BitBuffer(), line 17 below which returns a status. 

uint32_t AFERead32BitBuffer(uint16_t addr, uint16_t numSamples, uint32_t *pData)
{
    int32_t status = SYS_STATUS_SUCCESS;

    cmdBuffer[0] = (addr >> 4);
    cmdBuffer[1] = ((addr & 0x0F) << 4) + 8;

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
    status = HAL_SPI_Transmit(&hSPI, (uint8_t *)&cmdBuffer, 2, SPI_TIMEOUT);
    /* FIXME : Handling SPI_Transmit error*/
    if (status != 0)
    {
        status = HAL_SPI_Transmit(&hSPI, (uint8_t *)&cmdBuffer, 2, SPI_TIMEOUT);
    }
    if (status == 0)
    {
        status = HAL_SPI_Receive(&hSPI, (uint8_t *)&pData[0], 4 * numSamples,
                                 SPI_TIMEOUT);
    }
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
    SwapEndian32Bit(&pData[0], numSamples);

    DEBUG_MSG("REG32RD,0x%x,0x%x", addr, pData[0]);

    return status;
}

Thanks - mike



6 REPLIES 6
Pavel A.
Super User

Are you sure that the CRC algo of the STM32 SPI matches the CRC algo of the AD device? Would it be better to do the CRC in software?

 

 

Amy323
Visitor

In AFERead16BitBuffer(), it looks like the receive result is getting ignored. You set status from HAL_SPI_Transmit(), but HAL_SPI_Receive() runs without assigning its return value back to status.

So this line:

HAL_SPI_Receive(&hSPI, (uint8_t *)&pData[0], 2 * numSamples, SPI_TIMEOUT);

could probably be:

status = HAL_SPI_Receive(&hSPI, (uint8_t *)&pData[0], 2 * numSamples, SPI_TIMEOUT);

That would make the function report a receive failure properly, same as the 32-bit version. Also worth being careful with the endian swap and debug print after a failed receive, since the buffer may not contain valid data in that case.

Pavel’s point about CRC is worth checking too, but the missing status assignment in the 16-bit path does look like a real bug either way.

Hi Pavel,

Thanks, the CRC does not match but that is not what the problem is. The problem is the AFERead16Bitbuffere() function is never returning a status.  

Best Regards - mike

Hi Amy323,

Thanks, the CRC does not match but as you know I am writing about the bug in the code where the AFERead16Bitbuffere() function is never returning a status. Hopefully this is the proper place to write about bugs? 

Best Regards - mike

Hello @mike_m 

What happens during step-by-step execution of the code? Does it stop at a particular instruction?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

Hi Saket_Om, 

Thanks, the issue is the AFERead16Bitbuffere() function is never returning a status. It should return a status after performing the SPI Bus operation. 

Best Regards - mike