cancel
Showing results for 
Search instead for 
Did you mean: 

Proper use of the HAL library for SPI communications.

Rusty Jones
Associate

Proper use of the HAL library for SPI communications.

OK, first, I am very new to this and I am slowly learning as I go.

The project I am working on is a radio beacon in the 430MHz range. I have a Nucleo-F446RE board and a RFM23B transceiver module. I am wanting to control the radio via SPI. Following a YouTube video to use SPI I have the following lines of code

uint8_t spiReadRegister(uint8_t address)
{
	uint8_t spiTxBuf[2], spiRxBuf[2];
	uint8_t result;
	spiRxBuf[0] = address;
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET); //Bring nSEL low
	HAL_SPI_Receive(&hspi1, spiRxBuf, 1, 50); // read 1 byte of data, wait 50usec
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); //Bring nSEL high
	result = spiRxBuf;
	return(result);
}

 the code in is a separate src file "SPI.c" and I get the following error

"'hspi1' undeclared (first use in this function)" for the line "HAL_SPI_Receive(&hspi1, spiRxBuf, 1, 50); // read 1 byte of data, wait 50usec"

Now if I put the same block of code into the main.c file it has no error

What am I doing wrong??

Thanks

Rusty

4 REPLIES 4

You'd need to define hspi as an extern​ variable in the secondary files.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
alister
Lead

The hspi1 is defined in main.c:

SPI_HandleTypeDef hspi1;

You can declare it in your spi.c as follows:

extern SPI_HandleTypeDef hspi1;

Alister

Rusty Jones
Associate

THANK YOU BOTH!

As you can see I am somewhat new to all of this, but am learning!

Rusty

S.Ma
Principal

Get used in your IDE for the handy "find in files" command...