Skip to main content
PLee.3
Associate II
February 17, 2024
Question

SPI Send (sizeof) too big

  • February 17, 2024
  • 3 replies
  • 2146 views

Hi, I am wondering if anyone has any experience with HAL SPI as when I try in 16-bit mode to make a single packet of data manually and use (sizeof xxx) it sends 2x data packets to the oscilloscope and when I specify the packet size as "1" I get the correct waveform? I show it below the code and attach the scope readings:-

uint16_t packet;

packet = 0b0111 1111 1000 0000;  // send 8 MSB bits in 16 bit mode

HAL_SPI_Transmit (&hspi1, &packet, sizeof(packet), HAL_MAX_DELAY); // sends 2 packets?
HAL_SPI_Transmit (&hspi1, &packet, 1, HAL_MAX_DELAY); // sends correct 1 packet.

 

3 replies

waclawek.jan
Super User
February 17, 2024

sizeof(xxx) returns number of bytes xxx occupies, i.e. in case of uint16_t it's 2.

That's why

> it sends 2x data packets

JW

Andrew Neil
Super User
February 17, 2024

Documentation should be clearer:

AndrewNeil_0-1708173492403.png

Always give units !

Note also that units are needed for the Timeout parameter.

@Imen.D 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
waclawek.jan
Super User
February 18, 2024

Yes, but what are units in this particular case?

Pieces. It even has an abbreviation (pcs). Not an SI unit, though.

Would you be satisfied with this "unit"?

JW

Technical Moderator
February 18, 2024

Hello,

@Andrew Neil , thank you for your feedback and for helping us improve the ST documentations!

Your request has been escalated via internal ticket number 173729.

(PS: internal ticket number 173729 is only for reference, not available outside of ST).

"When your question is answered, please close this topic by clicking ""Accept as Solution"".ThanksImen"
PLee.3
PLee.3Author
Associate II
February 21, 2024

Hi, thanks for the information it seems this "sizeof" is an internal HAL based integer which tells the size of a data packet or array so the correct amount of packets can be sent and is 2^16 (65,535).

This means potentially you can have a pointer or array that can be up too 65,535 in length.