cancel
Showing results for 
Search instead for 
Did you mean: 

How to ensure that an SPI master sends exactly 16 clock pulses to the slave?

xpp07
Senior

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

0690X000006CDVbQAO.png

How can the SPI master send exactly 16 clock pulses to the slave? I'm using L432KC 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;
     }

1 REPLY 1
T J
Lead

// clear CS

uint16_t RxSPI = *((__IO uint16_t *)&hspi1.Instance->DR); // force 16bit transfer

// wait for last bit to leave pin

// set CS

otherwise, did you try to set the buffer to 16 bit ?

does this look ok guys ?

uint16_t SPIRx[64];

HAL_SPI_Receive(&hspi1, (uint16_t *)&SPIRx, 2, 10);