0
\$\begingroup\$

I'm using this current sensor: TLI4970-D050T4 According to the datasheet:

enter image description here

How can the SPI master send exactly 16 clock pulses to the slave, so I don't receive the 0's?

When I'm reading this sensor, I get a lot of 0's. So, I thought that it might be the reason, that I'm sending more than 16 clocks pulses. I'm using an STM32 Nucleo running at 2 MHz, but the SPI clock has been prescaled to 1 MHz. I would appreciate any advice. This is the code that I use to read the sensor and calculate current:

while (1)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_SPI_Receive(&hspi1, (uint8_t *)&SPIRx, 2, 10);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
motorCurrent=motor_current();

float motor_current() {

    float Iout;
    float BIout;
    int16_t Bit15;
     Bit15 = SPIRx&0x8000;
            if(Bit15==0x8000){
                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
            }
            else {
                BIout= SPIRx&0x1FFF;
                Iout = (BIout-4096)/80;
            }
        return Iout;
     }
\$\endgroup\$
9
  • \$\begingroup\$ Are you able to monitor the SPI bus on an o-scope or logic analyzer? \$\endgroup\$
    – Annie
    Commented Oct 18, 2018 at 22:41
  • \$\begingroup\$ I could. Just that I don't have the scope right now. It might be next week. \$\endgroup\$
    – user115094
    Commented Oct 18, 2018 at 22:44
  • \$\begingroup\$ There's no reason the SPI should send anything other than the number of clock pulses you specify. I just don't see how you're setting it up in the snippet you provided. \$\endgroup\$
    – Dave Tweed
    Commented Oct 18, 2018 at 23:14
  • \$\begingroup\$ While you're waiting on that scope, you may want to check 4.1.2 in the infineon datasheet and cross-check it with the SPI parameters on your Nucleo to make sure the CS and SCLK setup time is met and that the CPHA, CPOL parameters are good. I don't know which specific processor you're using, but a 2MHz CPU speed sounds awfully slow, usually peripheral clocking has prescalers to slow down a system clock that the processor runs at. Last note, the CS and SPI clocking process are usually separate events in terms of code synch, so when you pull that CS low and immediately start the SPI transfer \$\endgroup\$
    – user201365
    Commented Oct 18, 2018 at 23:32
  • \$\begingroup\$ you may not meet the infineon's minimum setup time from chip select to first SPI clock pulse if your CPU internal speed is realy fast. \$\endgroup\$
    – user201365
    Commented Oct 18, 2018 at 23:33

1 Answer 1

1
\$\begingroup\$

You are using HAL_SPI_Receive() wrong, second parameter should be your buffer not SPI register. Other potential problem is that you have short timeout (fourth parameter) with slow SPI clock.

You can also make living easier if you look at your hspi1 configuration and change data size to 16 bits.

EDIT

If second parameter is your buffer and not SPI register, that I would still increase timeout and check if HAL_SPI_Receive() return success or error.

\$\endgroup\$
7
  • \$\begingroup\$ SPIRx does not look like a register definition for STM32. Usually it would be SPI1->RX \$\endgroup\$
    – jaskij
    Commented Oct 19, 2018 at 8:13
  • \$\begingroup\$ You are probably right, but STM32 sometimes use NAMEx in examples \$\endgroup\$
    – Rokta
    Commented Oct 19, 2018 at 8:29
  • \$\begingroup\$ SPIRx is just a variable a created to store the SPI data. \$\endgroup\$
    – user115094
    Commented Oct 19, 2018 at 10:52
  • \$\begingroup\$ Should I read the SPI in the main loop or create a timer based interrupt to read SPI at a fixed frequency? \$\endgroup\$
    – user115094
    Commented Oct 19, 2018 at 11:47
  • \$\begingroup\$ depend on what you do with information from sensor, if you want to integrate it then interrupt is better, if you want to only display it then it could be in main loop. \$\endgroup\$
    – Rokta
    Commented Oct 19, 2018 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.