Skip to main content
SDall
Associate III
February 19, 2024
Question

Translate spi ready from Arduino top stm

  • February 19, 2024
  • 3 replies
  • 1289 views

Hi I wanted to translate the following Arduino code with hal stm:

uint32_t TMC5160_SPI::readRegister(uint8_t address)
{
// request the read for the address
_beginTransaction();
_spi->transfer(address);
_spi->transfer(0x00);
_spi->transfer(0x00);
_spi->transfer(0x00);
_spi->transfer(0x00);
_endTransaction();

//Delay for the minimum CSN high time (2tclk + 10ns -> 176ns with the default 12MHz clock)
#ifdef TEENSYDUINO
delayNanoseconds(2 * 1000000000 / _fclk + 10);
#else
delayMicroseconds(1);
#endif

// read it in the second cycle
_beginTransaction();
_spi->transfer(address);
uint32_t value = 0;
value |= (uint32_t)_spi->transfer(0x00) << 24;
value |= (uint32_t)_spi->transfer(0x00) << 16;
value |= (uint32_t)_spi->transfer(0x00) << 8;
value |= (uint32_t)_spi->transfer(0x00);
_endTransaction();

_lastRegisterReadSuccess = true; // In SPI mode there is no way to know if the TMC5130 is plugged...

return value;
}

Thanks

3 replies

Andrew Neil
Super User
February 19, 2024

Use this button to properly post source code:

AndrewNeil_0-1708375759459.png

 

 

 

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.
TDK
Super User
February 20, 2024

Is there a question?

What chip? Should be examples in the CubeMX repository for how to interface with SPI.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Andrew Neil
Super User
February 20, 2024

You know that there's an Arduino Core available for STM32?

https://github.com/stm32duino

https://www.arduino.cc/reference/en/libraries/stm32duino-examples/

 


@SDall wrote:

I wanted to translate the following Arduino code with hal stm


So what have you tried? Where are you stuck?

  • Study the HAL documentation for the STM32 chip(s) of interest
  • Try the SPI examples
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.