2019-02-12 07:42 PM
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
2019-02-12 08:00 PM
You'd need to define hspi as an extern variable in the secondary files.
2019-02-12 08:00 PM
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
2019-02-12 08:04 PM
THANK YOU BOTH!
As you can see I am somewhat new to all of this, but am learning!
Rusty
2019-02-12 10:51 PM
Get used in your IDE for the handy "find in files" command...