0
\$\begingroup\$

My original function had multiple 8-bit writes (wdata variable) and value inside the same array. This was transmitted by SPI master to a register. Now l'm porting to a new project that needs 32-bit address on the slave.

Original:

void register_write(uint8_t value)
{

/* Initialize array */
uint8_t wdata[5];

/* Write command */
wdata[0] = 0x2F;

/* Register address */
wdata[1] = 0x45;
wdata[2] = 0x81;
wdata[3] = 0x23;

/* Data written to register */
wdata[4] = value;

HAL_SPI_Transmit(&hspi2, wdata, 5, HAL_MAX_DELAY);
}

Is this the correct way to modify my register write function for 32-bit address and keep my 8-bit value in the same array? I'm trying to avoid having 2 different HAL_SPI_Transmit for the register address and value.

Modified:

void register_write(uint8_t value)
{

/* Initialize array */
uint32_t wdata[2];

/* Write command */
wdata[0] = 0x2F458123;

/* Data written to register */
wdata[1] = value;

HAL_SPI_Transmit(&hspi2, wdata, 2, HAL_MAX_DELAY);
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

No, those two pieces of code are not even remotely similar. And no, you don't need two HAL_SPI_Write commands if you do it properly.

The first code sends 5 bytes from an array of 5 bytes. The other code has an array of two 32-bit integers, or 8 bytes, but sends only two bytes out which must be invalid.

The conversion of 8-bit data to 32-bit data is also done incorrectly, as this is a little-endian system.

The latter code would send out <0x23, 0x81, 0x45, 0x25, data> which is also incorrect from your original code.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.