cancel
Showing results for 
Search instead for 
Did you mean: 

Translate spi ready from Arduino top stm

SDall
Associate II

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 3
Andrew Neil
Evangelist III

Use this button to properly post source code:

AndrewNeil_0-1708375759459.png

 

 

 

TDK
Guru

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
Evangelist III

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