cancel
Showing results for 
Search instead for 
Did you mean: 

I'm using STM32F303RBTx to write at ST25TV... using simples commands in HAL driver lib...but I2C2 not works...someone have a idea? my project is atached.

JRayc.1
Associate

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

#define I2C_ADDRESS 0x0F

#define TXBUFFERSIZE 1

#define RXBUFFERSIZE 1

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c2;

/* USER CODE BEGIN PV */

uint8_t aTxBuffer[]={ 5};

uint8_t aRxBuffer[RXBUFFERSIZE];

/* USER CODE END PV */

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

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C2_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C2_Init();

 /* USER CODE BEGIN 2 */

 HAL_I2C_Init(&hi2c2);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 /* Turn LED2 on: Transfer in Transmission process is correct */

 HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);

 /*##-2- Start the transmission process #####################################*/

 /* While the I2C in reception process, user can transmit data through

   "aTxBuffer" buffer */

 /* Timeout is set to 10S */

 while(HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 10000)!= HAL_OK)

 {

  /* Error_Handler() function is called when Timeout error occurs.

    When Acknowledge failure occurs (Slave don't acknowledge its address)

    Master restarts communication */

  if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)

  {

   Error_Handler();

  }

  HAL_Delay(300);

 HAL_GPIO_TogglePin(LED_INST_GPIO_Port,LED_INST_Pin);

 HAL_GPIO_TogglePin(LED_S_GPIO_Port,LED_S_Pin);

 }

 /*##-3- Put I2C peripheral in reception process ############################*/

 /* Timeout is set to 10S */

 while(HAL_I2C_Master_Receive(&hi2c2, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 10000) != HAL_OK)

 {

  /* Error_Handler() function is called when Timeout error occurs.

    When Acknowledge failure occurs (Slave don't acknowledge it's address)

    Master restarts communication */

  if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)

  {

   Error_Handler();

  }

 }

 /* Turn LED2 off: Transfer in reception process is correct */

 HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);//BSP_LED_Off(LED2);

 HAL_Delay(1000);

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

1 ACCEPTED SOLUTION

Accepted Solutions
Rene Lenerve
ST Employee

Hi @JRayc.1​ ,

SystemClock_Config should configure system Colck and also peripheral clocks (depends on MCU)

MX_I2C2_Init is used to configure the I²C IP (configure all I²C registers) and the IOs pin for I²C feature.

HAL_I2C_Init is redondant as this is already call in the MX_I2C2_Init function.

now the I2C communication is ready to receive and transmit, the start condition is managed in HAL_I2C_Master_Transmit, the stop condition too (if not set to software management).

Best Regards.

View solution in original post

3 REPLIES 3
Rene Lenerve
ST Employee

Hello @JRayc.1​ ,

Are you sure you are using a ST25TV? If correct, this one is a simple tag and has no other interface than RF. So that would explain why you are not communicating successfuly.

If you are using a real dual interface NFC chip (for example ST25DV), what I can notice is that the constant I2C_ADDRESS has the value 0x0F which is not correct, for the ST25DV it must be set to 0xA6 (for user memory) or 0xAE (for system memory).

Hope this can help you.

Best Regards.

JRayc.1
Associate

Thanks for your help... I found the a the problem the I2C2 isn't work...

One question: When I use the function HAL_I2C_Master_Transmit(), I have to START the I2C2, I supouse that this is made by the  MX_I2C2_Init(), that was created by the CubeMX. Is it?

thanks again!

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

int main(void)

{

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* Configure the system clock */

 SystemClock_Config();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C2_Init();

 /* USER CODE BEGIN 2 */

 HAL_I2C_Init(&hi2c2); 

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 /* Turn LED2 on: Transfer in Transmission process is correct */

 HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);

 /*##-2- Start the transmission process #####################################*/

 /* While the I2C in reception process, user can transmit data through   "aTxBuffer" buffer */

 /* Timeout is set to 10S */

 while(HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 10000)!= HAL_OK)

 {

  if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)  {

   Error_Handler();

  }

 HAL_Delay(300);

 HAL_GPIO_TogglePin(LED_INST_GPIO_Port,LED_INST_Pin);

 HAL_GPIO_TogglePin(LED_S_GPIO_Port,LED_S_Pin);

 }

 /*##-3- Put I2C peripheral in reception process ############################*/

 /* Timeout is set to 10S */

 while(HAL_I2C_Master_Receive(&hi2c2, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 10000) != HAL_OK)

 {

   if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)   {

    Error_Handler();

   }

 }

 /* Turn LED2 off: Transfer in reception process is correct */

 HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);//BSP_LED_Off(LED2);

 HAL_Delay(1000);

   /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

Rene Lenerve
ST Employee

Hi @JRayc.1​ ,

SystemClock_Config should configure system Colck and also peripheral clocks (depends on MCU)

MX_I2C2_Init is used to configure the I²C IP (configure all I²C registers) and the IOs pin for I²C feature.

HAL_I2C_Init is redondant as this is already call in the MX_I2C2_Init function.

now the I2C communication is ready to receive and transmit, the start condition is managed in HAL_I2C_Master_Transmit, the stop condition too (if not set to software management).

Best Regards.