cancel
Showing results for 
Search instead for 
Did you mean: 

nRF24L01 & STM32F103C8 - How to use SPI for a transceiver code

igtaba
Associate

Hello people, I need help for a University project. I'm trying to establish communication between 2 STM32F103 with a pair of nRF24L01. I am a complete newbie to programming MCU.

I have tried a lot of libraries but none that I could compiled in uVision 5 using the MxCUBE and HAL libraries, so after a while I learned how the libraries works for the registers but when I try to modified o write the SPI required for the write or read process between the MCU and the nRF24L01 I cant make it work

The portion of code I'm using as and examples, use this function every time is need to write or read a register to the nrf24l01:

// Low level SPI transmit/receive function (hardware depended)
// input:   data     -  is the value to transmit via SPI
// return: value received from SPI
uint8_t nRF24_LL_RW(uint8_t dataIn) {
	 // Wait until TX buffer is empty
	while (SPI_I2S_GetFlagStatus(nRF24_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET);
	// Send byte to SPI (TXE cleared)
	SPI_I2S_SendData(nRF24_SPI_PORT, data);
	// Wait while receive buffer is empty
	while (SPI_I2S_GetFlagStatus(nRF24_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET);
	// Return received byte
	return (uint8_t)SPI_I2S_ReceiveData(nRF24_SPI_PORT);
}

And I'm trying to replace it with the HAL function

extern hspi1; //This is done at the top of the file
// Low level SPI transmit/receive function (hardware depended)
// input:
//   data - value to transmit via SPI
// return: value received from SPI
uint8_t nRF24_LL_RW(uint8_t dataIn) {
	uint8_t dataOut;
	HAL_SPI_TransmitReceive(&hspi1, &dataIn, &dataOut, 1,1000 );
        return dataOut;
}

So, in the functions where I'm using the nRF24_LL_RW() the code changes, from this:

// Read a multi-byte register
// input:
//   reg - number of register to read
//   pBuf - pointer to the buffer for register data
//   count - number of bytes to read
static void nRF24_ReadMBReg(uint8_t reg, uint8_t *pBuf, uint8_t count) {
	nRF24_CSN_L();
	nRF24_LL_RW(reg);
	while (count--) {
		*pBuf++ = nRF24_LL_RW(nRF24_CMD_NOP);
	}
	nRF24_CSN_H();
}
 
// Write a multi-byte register
// input:
//   reg - number of register to write
//   pBuf - pointer to the buffer with data to write
//   count - number of bytes to write
static void nRF24_WriteMBReg(uint8_t reg, uint8_t *pBuf, uint8_t count) {
	nRF24_CSN_L();
	nRF24_LL_RW(reg);
	while (count--) {
		nRF24_LL_RW(*pBuf++);
	}
	nRF24_CSN_H();
}

to this:

// Read a multi-byte register
// input:
//   reg - number of register to read
//   pBuf - pointer to the buffer for register data
//   count - number of bytes to read
static void nRF24_ReadMBReg(uint8_t reg, uint8_t *pBuf, uint8_t count) {
	nRF24_CSN_L();
	nRF24_LL_RW(reg);
	
		*pBuf = nRF24_LL_RW(nRF24_CMD_NOP);
	
	nRF24_CSN_H();
}
 
// Write a multi-byte register
// input:
//   reg - number of register to write
//   pBuf - pointer to the buffer with data to write
//   count - number of bytes to write
static void nRF24_WriteMBReg(uint8_t reg, uint8_t *pBuf, uint8_t count) {
	nRF24_CSN_L();
	nRF24_LL_RW(reg);
	
		nRF24_LL_RW(*pBuf);
	
	nRF24_CSN_H();
}

Then I came to the problem of writing a transceiver code for the main program, since I did not understand the examples founded online, I wrote something like this

//This is implemented inside the while(1) and before of this all the initialization was done at the start of the main()
        //I write the payload to send to the nRF24L01 
        nRF24_WritePayload(nRF24_payload, 8);
	
	nRF24_CE_H(); //Pin CE to High to send
        HAL_Delay(100); //To give time for the transmition to be done
 
	//Read the payload received	
	if (nRF24_GetStatus_RXFIFO() != nRF24_STATUS_RXFIFO_EMPTY) {
        // the RX FIFO have some data, take a note what nRF24 can hold up to three payloads of 32 bytes...
        pipe = nRF24_ReadPayload(nRF24_payloadB, &payload_length); // read a payload to buffer
        nRF24_ClearIRQFlags(); // clear any pending IRQ bits
	}

The library I'm trying to modify is LonelyWolf's github code, and is in the link attached

So if anyone has any help on how to make the library work by modifying the SPI function to send or the part of the main program where I have wrote the code to make it work like a transceiver.

I dont know if a transceiver is code like that or how I have to code it

I dont know if the use of the HAL library is the correct one to write into to nRF24L01

and even my professor doesnt have any idea of this, so I'm in the blue

1 REPLY 1
neurodroid
Associate

I guess you did a mistake in the nRF24_ReadMBReg() and nRF24_WriteMBReg(). These functions are intend to transfer a multiple bytes, but in your version the only one is transmitted. Probably you should in these function replace a call to nRF24_LL_RW() with call to HAL_SPI_TransmitReceive(), specifying a value of the count variable to the Size variable. The nRF24_*Payload() functions uses multibyte transmission via the nRF24_*MBReg(), thus in your case there will be a read/write of just one byte.

The main idea of the library is that it is necessary by means of the macro nRF24_LL_RW() to implement the send/receive of a single byte via SPI, and the library will do the rest. So you need to do a minimal modifications for porting to different hardware.