cancel
Showing results for 
Search instead for 
Did you mean: 

how to use X-NUCLEO-NFC04A1 tag ? Please share its base code .

Pooja Sain
Associate II

Hello,

I already used x-cube-nfc4 as a basic code for application use of RFID tag but it didn't work only it indicates the nfc tap while using near to tag and even this code doesn't support uart. So, i want to know why UART is not supporting in it. I am attaching the code which I used.

#include "main.h"

UART_HandleTypeDef huart;

sURI_Info URI;

extern sCCFileInfo CCFileStruct;

/* Private function prototypes -----------------------------------------------*/

void UARTConsoleConfig( void );

HAL_StatusTypeDef UARTConsolePrint( char *puartmsg );

extern void SystemClock_Config( void );

int main( void )

{

 char uartmsg[6]="hello";

 HAL_Init( );

 /* Configure the System clock */

 SystemClock_Config( );

  

  /* Init of the Nucleo Board led */

// BSP_LED_Init(LED2);

 /* Init of the Leds on X-NUCLEO-NFC04A1 board */

 NFC04A1_LED_Init( );

 NFC04A1_LED_ON( GREEN_LED );

 HAL_Delay( 300 );

 NFC04A1_LED_ON( BLUE_LED );

 HAL_Delay( 300 );

 NFC04A1_LED_ON( YELLOW_LED );

 HAL_Delay( 300 );

  

/* Init UART for display message on console */

UARTConsoleConfig();

UARTConsolePrint(uartmsg);

UARTConsolePrint("HELLO");

UARTConsolePrint( "----------------------------------------" );

 /* Init ST25DV driver */

 while( BSP_NFCTAG_Init( ) != NFCTAG_OK );

 /* Reset Mailbox enable to allow write to EEPROM */

 BSP_NFCTAG_GetExtended_Drv()->ResetMBEN_Dyn( );

  

 /* Check if no NDEF detected, init mem in Tag Type 5 */

 if( NfcType5_NDEFDetection( ) != NDEF_OK )

 {

  CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE;

  CCFileStruct.Version = NFCT5_VERSION_V1_0;

  CCFileStruct.MemorySize = ( ST25DV_MAX_SIZE / 8 ) & 0xFF;

  CCFileStruct.TT5Tag = 0x05;

  /* Init of the Type Tag 5 component (M24LR) */

  while( NfcType5_TT5Init( ) != NFCTAG_OK );

 }

 /* Init done */

 NFC04A1_LED_OFF( GREEN_LED );

 HAL_Delay( 300 );

 NFC04A1_LED_OFF( BLUE_LED );

 HAL_Delay( 300 );

 NFC04A1_LED_OFF( YELLOW_LED );

 HAL_Delay( 300 );

  

 /* Prepare URI NDEF message content */

 strcpy( URI.protocol,URI_ID_0x01_STRING );

 strcpy( URI.URI_Message,"st.com/st25" );

 strcpy( URI.Information,"\0" );

  

 /* Write NDEF to EEPROM */

 while( NDEF_WriteURI( &URI ) != NDEF_OK );

  

 /* Set the LED3 on to indicate Programing done */

 NFC04A1_LED_ON( YELLOW_LED );

  

 /* Infinite loop */

 while (1)

 {  

 }

}

void UARTConsoleConfig( void )

{

 if(HAL_UART_GetState(&huart) == HAL_UART_STATE_RESET)

 {

  huart.Instance      = USART2;

  huart.Init.BaudRate   = 115200;

  huart.Init.WordLength  = UART_WORDLENGTH_8B;

  huart.Init.StopBits   = UART_STOPBITS_1;

  huart.Init.Parity    = UART_PARITY_NONE;

  huart.Init.Mode     = UART_MODE_TX_RX;

  huart.Init.HwFlowCtl   = UART_HWCONTROL_NONE;

  huart.Init.OverSampling = UART_OVERSAMPLING_16;

   

  HAL_UART_Init(&huart);

}

}

HAL_StatusTypeDef UARTConsolePrint( char *puartmsg )

{

 return HAL_UART_Transmit( &huart, (uint8_t *)puartmsg, strlen( puartmsg ), 500);

}

HAL_StatusTypeDef UARTConsoleScan( uint8_t uartchar )

{

 while( HAL_UART_Receive( &huart, &uartchar, 1, 500) == HAL_TIMEOUT );

  

 return HAL_OK;

}

#ifdef USE_FULL_ASSERT

void assert_failed( uint8_t* file, uint32_t line )

 /* Infinite loop */

 while( 1 )

 {

 }

}

#endif

1 ACCEPTED SOLUTION

Accepted Solutions
Rene Lenerve
ST Employee

Hi @Pooja Sain​ ,

I don't understand your point about RFID Tag and MSI, as no clock can be configured in the RFID Tag. The MSI clock has to do with the STM32L0.

I have found a Nucleo L053 (not L073 but close to it) and test it with the configuration clock I proposed earlier and the UART is working as well as Leds.

Here is the complete System Clock function i used:

void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  
  /* Enable MSI Oscillator */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.MSICalibrationValue=0x00;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
  {
    /* Initialization Error */
    while(1); 
  }
  
  /* Select MSI as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0)!= HAL_OK)
  {
    /* Initialization Error */
    while(1); 
  }
  
  /* Select PCLK1 as source clock for i2c1 peripheral */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();
  
  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  
}

Pour l'UART voici ce qui est configuré:

UART_HandleTypeDef huart;
char uartmsg[80];
/* Private function prototypes -----------------------------------------------*/
void UARTConsoleConfig( void );
HAL_StatusTypeDef UARTConsolePrint( char *puartmsg );
/* MSP UART Init ------------------------------------------------------------------*/
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  
  /* Enable GPIOC clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  
    /* Peripheral clock enable */
  __HAL_RCC_USART2_CLK_ENABLE();
  
  __HAL_RCC_USART2_FORCE_RESET();
  __HAL_RCC_USART2_RELEASE_RESET();
  
  /* USART2 GPIO Configuration */
  GPIO_InitStruct.Pin 			= GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_InitStruct.Mode 			= GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull 			= GPIO_NOPULL;
  GPIO_InitStruct.Speed 		= GPIO_SPEED_FREQ_MEDIUM;
  GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
 
/* UART Initialisation --------------------------------------------------------------*/
void UARTConsoleConfig( void )
{
  if( HAL_UART_GetState(&huart) == HAL_UART_STATE_RESET )
  {
    huart.Instance           = USART2;
    huart.Init.BaudRate      = 115200;
    huart.Init.WordLength    = UART_WORDLENGTH_8B;
    huart.Init.StopBits      = UART_STOPBITS_1;
    huart.Init.Parity        = UART_PARITY_NONE;
    huart.Init.Mode          = UART_MODE_TX_RX;
    huart.Init.HwFlowCtl     = UART_HWCONTROL_NONE;
    huart.Init.OverSampling  = UART_OVERSAMPLING_16;
    
    HAL_UART_Init( &huart );
	}
}
 
/* UART Write--------------------------------------------------------------------------*/
HAL_StatusTypeDef UARTConsolePrint( char *puartmsg )
{
  return HAL_UART_Transmit( &huart, (uint8_t *)puartmsg, strlen( puartmsg ), 500);
}

The function UARTConsoleConfig is call at the beginning then UARTConsolePrint can be used to send data to the UART. Opening a console on the PC com port used by STMicroelectronics STLink Virtual COM port at 115200 baud, 8 bits data, 1 stop bit, no parity and no flow control, worked for me.

Let me know if you progressed on your issue.

Regards.

View solution in original post

11 REPLIES 11
Rene Lenerve
ST Employee

Hi poojasain0211,

In the code you provide, you are missing the initialization of UART pins, which should be declared in an "msp" function (called inside HAL_UART_Init(&huart);).

Apart from your problem on the UART, I didn't understand what is not working, could you explain a little bit more your problem?

Regards.

Hi Rene Lenerve,

I am using X-NUCLEO-NFC04A1 expansion board with STM32 NUCLEOL0-73RZ board and this expansion board doesn't have specific UART pins in datasheet . This code is provided as base code from www.ST.com which I already attached with query and secondly, I have another code for I2C for same board and it supports UART and send the data on it. This I2C code is also provided by ST.com. So I added UART files and functions but I am not getting the points lack behind in it. Thanks for your reply and I will appreciate more answers.

Rene Lenerve
ST Employee

Hi poojasain0211,

The X-NUCLEO-NFC04A1 doesn't have UART on it because the NFC component is only using the I²C interface (MCU side). But as of all nucleo expansion boards, it has an Arduino connector which is common to nucleo boards and if you look closer to the Datasheet of the STM32L073RZ (https://www.st.com/resource/en/datasheet/stm32l073rz.pdf) and to the schematics of the STM32 NUCLEOL073RZ board (https://www.st.com/resource/en/schematic_pack/nucleo_64pins_sch.zip) you will see that the pins PA2 and PA3 can be muxed on the USART2_TX/USART2_RX and are available on the Arduino connector too.

The NFC04 software package is using this uart IP but not directly on the connector. As these pins are also connected to the embedded ST-Link debugger it takes advantage of the USB to UART bridge it provides. So you can open an UART console on PC, therefore it should display some log message (only with I2C_Protection and Mailbox examples). And you can use these examples for the UART part (if you were not already using it).

And to answer your question if I understood it correctly, and as I mentionned in my last message, in the copy/paste of your code, you forgot the MSP initialization of the UART. This initialization function can be found in the file stm32l0xx_hal_msp.c (void HAL_UART_MspInit(UART_HandleTypeDef *huart)) which will configure the muxed pins for the UART IP, which could be explain why the UART is not working.

Hope this can help you.

Regards.

Hello Rene Lenerve,

I agreed to your point and Initialized Msp. But still UART is not working .Thank you for your help but it didn't work. If you have any other idea please share it. I am looking for it.

Regards.

Rene Lenerve
ST Employee

Hi poojasain0211,

Could you give more details on what went wrong and what you have done to try to detect some clue on your problem and have the correct remedy.

Did you activate the UART Clock?

Did you correctly configure the clock tree for the UART?

Did you enable the UART define (#define HAL_UART_MODULE_ENABLED) in stm32f4xx_hal_conf.h file?

Did you use the debugger and try to see where the problem occurs?

...

Regards.

Hi Rene Lenerve,

Firstly, I make to clear my problem and what I wanna to do. Basically I wanna to run the nfc application of Rfid tag(X-NUCLEO_NFC04A1) with mobile. So, when we bring closer mobile to this rfid tag then www.st.com website should be open in mobile and the problem is it is not working for this nfc application. For that I am figuring out the problem.

Yes, UART clock is activated.

Yes, I configured the clock tree for the UART correctly.

Yes, #define HAL_UART_MODULE_ENABLED is enabled.

Actually, I am using Nucleo -L073 board so it has inbuilt debugger using USB cable only. I tried to figure out the problem in code while debugging but I didn't find anything yet.

..............

Regards.

Rene Lenerve
ST Employee

Hello @Pooja Sain​ ,

Looking to the software package of NFC4, I think something that can cause your issue is the clock configuration for the L053 msp config (if you got the code from that platform). It seems that the clock configuration is set to use the HSE, but the quartz is not soldered on the Nucleo board, so modifying it could make it work.

Clock Config example for "void SystemClock_Config( void )" in stm32l0xx_hal_msp.c:

...
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.MSICalibrationValue=0x00;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
...

Let me know if that helped you.

Regards.

Hi Rene,

I changed clock configuration as per you advised but RFID tag not support MSI. Even now LEDs are not blinking in RFID tag(X-NUCLEO-NFC04A1) , basically the board is not supporting it. :worried_face:

Rene Lenerve
ST Employee

Hi @Pooja Sain​ ,

I don't understand your point about RFID Tag and MSI, as no clock can be configured in the RFID Tag. The MSI clock has to do with the STM32L0.

I have found a Nucleo L053 (not L073 but close to it) and test it with the configuration clock I proposed earlier and the UART is working as well as Leds.

Here is the complete System Clock function i used:

void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  
  /* Enable MSI Oscillator */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.MSICalibrationValue=0x00;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
  {
    /* Initialization Error */
    while(1); 
  }
  
  /* Select MSI as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0)!= HAL_OK)
  {
    /* Initialization Error */
    while(1); 
  }
  
  /* Select PCLK1 as source clock for i2c1 peripheral */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();
  
  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  
}

Pour l'UART voici ce qui est configuré:

UART_HandleTypeDef huart;
char uartmsg[80];
/* Private function prototypes -----------------------------------------------*/
void UARTConsoleConfig( void );
HAL_StatusTypeDef UARTConsolePrint( char *puartmsg );
/* MSP UART Init ------------------------------------------------------------------*/
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  
  /* Enable GPIOC clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  
    /* Peripheral clock enable */
  __HAL_RCC_USART2_CLK_ENABLE();
  
  __HAL_RCC_USART2_FORCE_RESET();
  __HAL_RCC_USART2_RELEASE_RESET();
  
  /* USART2 GPIO Configuration */
  GPIO_InitStruct.Pin 			= GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_InitStruct.Mode 			= GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull 			= GPIO_NOPULL;
  GPIO_InitStruct.Speed 		= GPIO_SPEED_FREQ_MEDIUM;
  GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
 
/* UART Initialisation --------------------------------------------------------------*/
void UARTConsoleConfig( void )
{
  if( HAL_UART_GetState(&huart) == HAL_UART_STATE_RESET )
  {
    huart.Instance           = USART2;
    huart.Init.BaudRate      = 115200;
    huart.Init.WordLength    = UART_WORDLENGTH_8B;
    huart.Init.StopBits      = UART_STOPBITS_1;
    huart.Init.Parity        = UART_PARITY_NONE;
    huart.Init.Mode          = UART_MODE_TX_RX;
    huart.Init.HwFlowCtl     = UART_HWCONTROL_NONE;
    huart.Init.OverSampling  = UART_OVERSAMPLING_16;
    
    HAL_UART_Init( &huart );
	}
}
 
/* UART Write--------------------------------------------------------------------------*/
HAL_StatusTypeDef UARTConsolePrint( char *puartmsg )
{
  return HAL_UART_Transmit( &huart, (uint8_t *)puartmsg, strlen( puartmsg ), 500);
}

The function UARTConsoleConfig is call at the beginning then UARTConsolePrint can be used to send data to the UART. Opening a console on the PC com port used by STMicroelectronics STLink Virtual COM port at 115200 baud, 8 bits data, 1 stop bit, no parity and no flow control, worked for me.

Let me know if you progressed on your issue.

Regards.