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

what if i dont want to change the function body?

ptr could already be a pointer ? or am i thinking wrong?

i did not work a lot with pointers (or call by reference in general) I just looked it up in the textbook and iam confused..

lets look at that example:

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

the function body of HAL_SPI_Transmit looks like this :

HAL_StatusTypeDef HAL_SPI_Transmit (SPI_HandleTypeDef * hspi, uint8_t * pData, uint16_t Size, uint32_t Timeout) 

looking at this i think SPI_Transmit needs a pointer to the data to transmit. Doesnt that mean i have to call it with & ?

like this: ?

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

The function gets

CubeIDE gives me a incompatible Pointer warning when i try that ..

would be super nice if you could explain that to me...

thanks!

MSimo.1
Associate II
void startWriteSXBuffer(uint8_t ptr) { 
[...]
HAL_SPI_Transmit(&hspi1, &ptr, 1 , HAL_MAX_DELAY);
}

what about that ? size is 1 because its only an uint8_t ?