cancel
Showing results for 
Search instead for 
Did you mean: 

[Coverting Arduini Code -> CubeIDE Code (c) ] HAL_SPI-Transmit function

MSimo.1
Associate II

Hey Guys, iam trying to convert Arduino Code to my C Project in CubeIDE..

I have some troubles(=warnings and iam unsure) with the SPI Trasmit functions.

Could you tell me if my code does the same as the Arduino Code?

ARDUINO : 
*******************
void SX126XLT::readRegisters(uint16_t address, uint8_t *buffer, uint16_t size)
{
#ifdef SX126XDEBUG
  //Serial.println(F("readRegisters()"));
#endif
 
  uint16_t index;
  uint8_t addr_l, addr_h;
 
  addr_h = address >> 8;
  addr_l = address & 0x00FF;
  checkBusy();
 
#ifdef USE_SPI_TRANSACTION     //to use SPI_TRANSACTION enable define at beginning of CPP file 
  SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode));
#endif
 
  digitalWrite(_NSS, LOW);
  SPI.transfer(RADIO_READ_REGISTER);
  SPI.transfer(addr_h);               //MSB
  SPI.transfer(addr_l);               //LSB
  SPI.transfer(0xFF);
  for (index = 0; index < size; index++)
  {
    *(buffer + index) = SPI.transfer(0xFF);
  }
 
  digitalWrite(_NSS, HIGH);
 
#ifdef USE_SPI_TRANSACTION
  SPI.endTransaction();
#endif
 
}
 
void SX126XLT::writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size)
{
#ifdef SX126XDEBUG
  //Serial.println(F("writeRegisters()"));
#endif
  uint8_t addr_l, addr_h;
  uint8_t i;
 
  addr_l = address & 0xff;
  addr_h = address >> 8;
  checkBusy();
 
#ifdef USE_SPI_TRANSACTION     //to use SPI_TRANSACTION enable define at beginning of CPP file 
  SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode));
#endif
 
  digitalWrite(_NSS, LOW);
  SPI.transfer(RADIO_WRITE_REGISTER);
  SPI.transfer(addr_h);   //MSB
  SPI.transfer(addr_l);   //LSB
 
  for (i = 0; i < size; i++)
  {
    SPI.transfer(buffer[i]);
  }
 
  digitalWrite(_NSS, HIGH);
 
#ifdef USE_SPI_TRANSACTION
  SPI.endTransaction();
#endif
}
***************
c in CubeIDE : 
***************
 
void readRegisters(uint16_t address, uint8_t *buffer, uint16_t size)
{
#ifdef SX126XDEBUG
  	uint8_t s[] ="readRegisters() \r\n";
  	tx_com(s, strlen((char const*)s));
#endif
 
  uint16_t index;
  uint8_t addr_l, addr_h;
 
  addr_h = address >> 8;
  addr_l = address & 0x00FF;
  checkBusy();
 
  HAL_GPIO_WritePin(SX1261_NSS_GPIO_Port, SX1261_NSS_Pin, GPIO_PIN_RESET);
 
  HAL_SPI_Transmit(&hspi1, RADIO_READ_REGISTER, sizeof(RADIO_READ_REGISTER), HAL_MAX_DELAY);
  HAL_SPI_Transmit(&hspi1, addr_h, sizeof(addr_h), HAL_MAX_DELAY);
  HAL_SPI_Transmit(&hspi1, addr_l, sizeof(addr_l), HAL_MAX_DELAY);
  HAL_SPI_Transmit(&hspi1, 0xFF, sizeof(0xFF), HAL_MAX_DELAY);
 
  HAL_SPI_TransmitReceive(&hspi1, 0xFF, buffer, size, HAL_MAX_DELAY);
 
  HAL_GPIO_WritePin(SX1261_NSS_GPIO_Port, SX1261_NSS_Pin, GPIO_PIN_SET);
}
 
void writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size){
#ifdef SX126XDEBUG
  	uint8_t s[] ="writeRegisters() \r\n";
  	tx_com(s, strlen((char const*)s));
#endif
  uint8_t addr_l, addr_h;
 
 
  addr_l = address & 0xff;
  addr_h = address >> 8;
  checkBusy();
  
  HAL_SPI_Transmit(&hspi1, RADIO_WRITE_REGISTER, sizeof(RADIO_WRITE_REGISTER), HAL_MAX_DELAY);
  HAL_SPI_Transmit(&hspi1, addr_h, sizeof(addr_h), HAL_MAX_DELAY);  //MSB
  HAL_SPI_Transmit(&hspi1, addr_l, sizeof(addr_l), HAL_MAX_DELAY);  //LSB
 
  HAL_SPI_Transmit(&hspi1, buffer, size, HAL_MAX_DELAY);
 
  HAL_GPIO_WritePin(SX1261_NSS_GPIO_Port, SX1261_NSS_Pin, GPIO_PIN_SET);
 
}

I dont know if iam using the right functions and if i have the right arguments .

Esspecially the 2nd Argument in the HAL_SPI_Transmit(); function ..

CubeIDE gives me a warning : makes pointer from integer without a cast [-Wint-conversion]

thank you

12 REPLIES 12
prain
Senior III

you should use​ & operator to convert uint8_t to uint8_t* (a pointer). like this:

HAL_SPI_Transmit(&hspi1, &addr_h, sizeof(addr_h), HAL_MAX_DELAY);

MSimo.1
Associate II

alright, thank you very much. That makes sence to me because the HAL Manual says : HAL_StatusTypeDef HAL_SPI_Transmit (SPI_HandleTypeDef * hspi, uint8_t * pData, uint16_t Size, uint32_t Timeout)

next question:

The Arduino Codes sends byte by byte in a loop :

  SPI.transfer(addr_h);   //MSB
  SPI.transfer(addr_l);   //LSB
 
  for (i = 0; i < size; i++)
  {
    SPI.transfer(buffer[i]);
  }

is is correct to replace that with one HAL_SPI_Transmit method?

  HAL_SPI_Transmit(&hspi1, &addr_h, sizeof(addr_h), HAL_MAX_DELAY);  //MSB
  HAL_SPI_Transmit(&hspi1, &addr_l, sizeof(addr_l), HAL_MAX_DELAY);  //LSB
 
  HAL_SPI_Transmit(&hspi1, &buffer, size, HAL_MAX_DELAY);

annother question:

&addr_h is a pointer to an adress. HAL_SPI_Transmit needs the number of bytes to transmit in the 3rd argument. Is it correct to say sizeof(addr-h) ? i guess not..

is it sizeof(&addr_h) ? or should i just say the length is 4 because the adress stores 32bit? am i thinking right?

1- yes, you can send multiple bytes at once.

2- sizeof(addr_h) is correct. sizeof will return number of bytes that the input variable occupied.

MSimo.1
Associate II

got it!

what about the following:

void readCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size){
[...]
  HAL_SPI_Transmit(&hspi1, &Opcode, sizeof(Opcode), HAL_MAX_DELAY);
  HAL_SPI_Transmit(&hspi1, 0xFF, sizeof(0xFF), HAL_MAX_DELAY);
  HAL_SPI_TransmitReceive(&hspi1, 0xFF, buffer, size, HAL_MAX_DELAY); }

line 4 and 5 are giving me the warning: passing argument 2 of 'HAL_SPI_Transmit' makes pointer from integer without a cast [-Wint-conversion]

The Argument 0xFF istn stored anywhere, so i cant create a pointer pointing to it, right?

i translated that from :

void SX126XLT::readCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size){ [...]
   SPI.transfer(Opcode);
   SPI.transfer(0xFF);
   for ( i = 0; i < size; i++ ){
      *(buffer + i) = SPI.transfer(0xFF);
   }
}

i just wanna make sure that my code is doint the same thing as the arduino code

thank you guys, iam learning a lot here;)

You can put 0xFF in an array like

uint8_t tx_buffer[] = { 0xFF };
HAL_SPI_Transmit(&hspi1, tx_buffer, sizeof(tx_buffer), HAL_MAX_DELAY);

why would you suggest an array and not a simple variable? would that be a pointer then?

You can use a simple variable as well. There is barely any difference but the array is more generic as in

    uint8_t tx_buffer[] = { 0xFF, 0xAA, 0x55 };
    HAL_SPI_Transmit(&hspi1, tx_buffer, sizeof(tx_buffer), HAL_MAX_DELAY);

and similiarily for rx_buffer of known length.

> would that be a pointer then?

Arrays decay to pointers in C automatically, i.e. tx_buffer can be used instead of &tx_buffer[0].

that makes sence! thanks

void startWriteSXBuffer(uint8_t ptr) {
[...]
HAL_SPI_Transmit(&hspi1, ??ptr, sizeof(ptr), HAL_MAX_DELAY);
}
 

how i can solve this ? HAL_SPI_Transmit needs a pointer in the 2nd Argument.

Should i create a pointer variable?

You should learn more about C.

    void startWriteSXBuffer(uint8_t *ptr,  uint16_t size) {
    [...]
    HAL_SPI_Transmit(&hspi1, ptr, size, HAL_MAX_DELAY);
    }
     

C pointers are dumb and don't know the size of the thing they are pointing at.