Skip to main content
Visitor II
November 30, 2023
Question

SPI Receive

  • November 30, 2023
  • 2 replies
  • 3193 views

Hi, I am working on SPI to communicate with the AD9959 from analog device, I'm analyzing the MOSI and MISO signal with probes on an oscilloscope and can see that  I can successfully write and read from the device, the thing is that HAL_SPI_Receive() send back the received buffer pRxData with the value 0 instead of the value from the MISO line, is there a reason for that ? I'm pretty sure I respect the specification of the spi communication for the device. In my case i set it on spi single-bit 3 wire mode to use MOSI and MISO lines, and on my spi parameters, I'm in spi mode 0 as it should by looking at the datasheet.

 

I dragged the datasheet below for those who are interested with the code of my read function. The variable which is suppose to hold the value returned by the slave (AD9959) in the MISO  line (receivdata) is always 0 but the MISO line returned the value that have been written to the register.

 

Thank you for your time.

 

uint32_t AD9959_Read(AD9959_Handler *device, AD9959_REG_ADDR reg_addr, AD9959_CHANNEL channel) 
{
uint8_t read_instruction = READ_INST | reg_addr;
 
/*Finding the length of the register to be read in terms of bytes*/
uint8_t len = device->registers[reg_addr].size;
 
uint8_t receivdata[4];
 
uint32_t val = 0;
 
AD9959_SetActiveChannel(device, channel);
 
HAL_GPIO_WritePin(device->cs_port,device->cs_pin, GPIO_PIN_RESET);
 
HAL_SPI_Transmit(device->SPI_Handler, &read_instruction,1,HAL_MAX_DELAY);
//
 
HAL_SPI_Receive(device->SPI_Handler, (uint8_t *)receivdata, len, HAL_MAX_DELAY);
 
HAL_GPIO_WritePin(device->cs_port,device->cs_pin, GPIO_PIN_SET);
 
int i = 0;
while(len-- >0){
val |= (*(receivdata + i) >> len*8) & 0xFF;
i++;
}
 
return val;
}
 
    This topic has been closed for replies.

    2 replies

    Super User
    November 30, 2023

    > spi single-bit 3 wire mode to use MOSI and MISO lines

    MISO is connected to SDIO_2? Recheck your pin assignments. Perhaps share a schematic.

    VuekoAuthor
    Visitor II
    December 1, 2023

    Hi @TDK yes MISO is connected to SDIO_2 as MOSI is to SDIO_0, SDIO_3 is grounded and SDIO_1 is keep at float, I'm 100% sure of my connections here is a schematic of the AD9959 from the user guide, you'll find it at the end of the documents. I can share a picture of my SPI configuration

    Vueko_0-1701437131131.png

     

    Super User
    December 1, 2023

    > val |= (*(receivdata + i) >> len*8) & 0xFF;

    How are you confirming that the data coming back is 0? Looks like your code will set val=0 regardless of the data in receivdata. len*8 is presumably at least 8. shift a uin8_t to the right by 8 or more bits and you get a 0. At minimum, this is a bug.

    VuekoAuthor
    Visitor II
    December 1, 2023

    In my main.c i use the function like this

    readval = AD9959_Read();

    the value is always 0 even after changing > val |= (*(receivdata + i) >> len*8) & 0xFF  to > val |= (*(receivdata + i) << len*8) & 0xFF

     

    so i assume receivdata is always 0

    Super User
    December 1, 2023

    That code still has bugs.

    Maybe debug that separately from the SPI issue. Give receivdata correct values, step through your loop, and watch val update. You will see the error.