cancel
Showing results for 
Search instead for 
Did you mean: 

ST25R3911 high cpu use

zhaocz
Associate II

I have integrated the ST25R3911B into an ARM64 Linux development board, using rfal_v1.3 for communication via SPI.
I am able to poll and read cards successfully, but I've observed higher CPU usage.
Upon inspecting the perf.svg, it appears that the st25r3911ReadMultipleRegisters function is consuming a significant amount of CPU.
Could you assist in analyzing this issue? here is my 

pltf_spi.c

 

 

/******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 STMicroelectronics</center></h2>
  *
  * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/myliberty
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
******************************************************************************/

/*! \file pltf_spi.c
 *
 *  \author Shikha Singh 
 *
 *  \brief Implementation for SPI communication.
 *
 */

/*
 ******************************************************************************
 * INCLUDES
 ******************************************************************************
 */
#include <string.h>
#include <stdio.h>
#include <fcntl.h> 
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include "pltf_spi.h"
#include "st_errno.h"

/*
 ******************************************************************************
 * DEFINES
 ******************************************************************************
 */
#define SPI_MODE_CONFIG		SPI_MODE_1
#define SPI_BITS_PER_WORD	8
#define SPI_MAX_FREQ		1000000
#define ARRAY_SIZE(a)		(sizeof(a) / sizeof(a[0]))

/*
 ******************************************************************************
 * STATIC VARIABLES
 ******************************************************************************
 */
/* ST25R3911XX is connected with Linux host's SPI port /dev/spidev0.0 */
static const char *device = "/dev/spidev0.0";
static int fd = 0;
static int isSPIInit = 0;
/* Lock to serialize SPI communication */
static pthread_mutex_t lockCom;

/*
 ******************************************************************************
 * GLOBAL AND HELPER FUNCTIONS
 ******************************************************************************
 */
ReturnCode spi_init(void)
{
	int ret = 0;
	uint32_t mode = SPI_MODE_CONFIG;
	uint8_t bitsperword = SPI_BITS_PER_WORD; 
	uint32_t speed = SPI_MAX_FREQ; 

	fd = open(device, O_RDWR);
	if (fd < 0) {
		printf("Error: spi device open = %d\n", fd);
		ret = fd;
		goto error;
	}
	
	/* set spi mode */
	ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
	if (ret < 0) {
		printf("Error: SPI mode setting\n");
		goto error;
	}

	/* set spi bits per word */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bitsperword);
 	if (ret < 0) {
		printf("Error: setting spi bitsperword\n");
		goto error;
	}

	/* set spi frequency */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret < 0) {
		printf("Error: setting SPI frequency\n");
		goto error;
 	}

	ret = pthread_mutex_init(&lockCom, NULL);
	if (ret != 0)
	{
		printf("Error: mutex init to protect SPI communication is failed\n");
		goto error;
	}

	
	isSPIInit = 1;
	return ERR_NONE;

error:
	return ERR_IO;
}


HAL_statusTypeDef spiTxRx(const uint8_t *txData, uint8_t *rxData, uint8_t length)
{  
	
	int ret = 0;
	int i;

	/* check if SPI init is done */
	if (!isSPIInit) {
		printf(" error: spi is used for communication before its initialization\n");
		return	HAL_ERROR;
	}

	/* lock for atomic SPI transaction */
	//pthread_mutex_lock(&lock);

	struct spi_ioc_transfer transfer;
	memset(&transfer, 0, sizeof(struct spi_ioc_transfer));

	if (txData)
		transfer.tx_buf = (unsigned long) txData;
	if (rxData)
		transfer.rx_buf = (unsigned long) rxData;	
	transfer.len 		= (unsigned int) length;
	transfer.speed_hz 	= SPI_MAX_FREQ;
	transfer.bits_per_word 	= SPI_BITS_PER_WORD;
	transfer.delay_usecs 	= 0;
	transfer.cs_change	= 0;
	
	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer);
	if (ret < 0) {
		printf("Error: SPI error in data transfer=%d\n",ret);
		/* Unlock the mutex before returning from error case */
		//pthread_mutex_unlock(&lock);
		return HAL_ERROR;
	}

	/* Unlock the mutex */
	//pthread_mutex_unlock(&lock);

	return HAL_OK;
}

void pltf_protect_com(void)
{
	pthread_mutex_lock(&lockCom);
}

void pltf_unprotect_com(void)
{
	pthread_mutex_unlock(&lockCom);
}

void plat_spi_select(void)
{
	usleep(150);
}

void plat_spi_deselect(void)
{
	usleep(150);
}

 

 

This discussion has been locked for participation. If you have a question, please start a new topic in order to ask your question
24 REPLIES 24

Hi zhaocz,

 

I missed that you are on an old version. It has been updated quite some while ago. Please find it at https://www.st.com/en/embedded-software/stsw-st25r009.html 

 

BR, Ulysses

Thank you, I will definitely take a look.

Hi

It's difficult to obtain the software, and it seems that the website response is very slow.
Can you please send it to my email?

Hi Ulysses

I found in my code that INT_PIN is set to 34. However, in the latest demo, INT_PIN is set to 7. Which one should I use?

zhaocz_2-1701309754968.png

zhaocz_1-1701309731264.png

 

Hi

I also noticed in my code that INT_PORT is not assigned a value and remains 0. In the latest demo, INT_PORT is assigned a value.

zhaocz_3-1701310566001.png

 

Hi, 

no idea about your code, why you ended up with 34 (comment claims 7). Maybe you or a collegue adapted it to a different hardware with a different pinout?

BR, Ulysses

Hi

I want to know whether the GPIO pins in the code operate based on the pin names of the ST25R3911 itself or the corresponding names on the mainboard?

Hi,

say you have the define PLTF_GPIO_INTR_PIN, then you need to set it to the gpio value which you need to use inside the RaspberryPi (or whatever the board) to drive the line leading to the IRQ pin of the ST25R3911 (pin 27). 

AFAIK on RPi this would be GPIO7.

 

BR, Ulysses

Hi

I understand. I have a new issue now.
I modified the
gpio_init (gpio7>gpio34) based on stsw-st25r009, and turned off the wake-up mode.
However, I am unable to detect the card (nfca). In the
demoCycle() function, the state remains DEMO_ST_DISCOVERY, but rfalNfcGetState() consistently returns values like 10, 20, and 2.

zhaocz_0-1701745212513.png